assistant-stream 0.0.19 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -36,31 +36,199 @@ __export(index_exports, {
36
36
  DataStreamEncoder: () => DataStreamEncoder,
37
37
  PlainTextDecoder: () => PlainTextDecoder,
38
38
  PlainTextEncoder: () => PlainTextEncoder,
39
- createAssistantRun: () => createAssistantRun
39
+ createAssistantStream: () => createAssistantStream,
40
+ createAssistantStreamResponse: () => createAssistantStreamResponse
40
41
  });
41
42
  module.exports = __toCommonJS(index_exports);
42
43
 
43
44
  // src/core/AssistantStream.ts
44
- var AssistantStream = class _AssistantStream {
45
- constructor(readable) {
46
- this.readable = readable;
47
- this.readable = readable;
45
+ var AssistantStream = {
46
+ toResponse(stream, transformer) {
47
+ return new Response(AssistantStream.toByteStream(stream, transformer));
48
+ },
49
+ fromResponse(response, transformer) {
50
+ return AssistantStream.fromByteStream(response.body, transformer);
51
+ },
52
+ toByteStream(stream, transformer) {
53
+ return stream.pipeThrough(transformer);
54
+ },
55
+ fromByteStream(readable, transformer) {
56
+ return readable.pipeThrough(transformer);
48
57
  }
49
- toResponse(transformer) {
50
- return new Response(this.toByteStream(transformer));
58
+ };
59
+
60
+ // src/core/utils/PipeableTransformStream.ts
61
+ var PipeableTransformStream = class extends TransformStream {
62
+ constructor(transform) {
63
+ super();
64
+ const readable = transform(super.readable);
65
+ Object.defineProperty(this, "readable", {
66
+ value: readable,
67
+ writable: false
68
+ });
51
69
  }
52
- static fromResponse(response, transformer) {
53
- return _AssistantStream.fromByteStream(response.body, transformer);
70
+ };
71
+
72
+ // src/core/serialization/DataStream.ts
73
+ var DataStreamEncoder = class {
74
+ _transformStream;
75
+ get writable() {
76
+ return this._transformStream.writable;
54
77
  }
55
- toByteStream(transformer) {
56
- return this.readable.pipeThrough(transformer);
78
+ get readable() {
79
+ return this._transformStream.readable;
57
80
  }
58
- static fromByteStream(readable, transformer) {
59
- return new _AssistantStream(readable.pipeThrough(transformer));
81
+ constructor() {
82
+ this._transformStream = new PipeableTransformStream((readable) => {
83
+ const transform = new TransformStream({
84
+ transform(chunk, controller) {
85
+ const type = chunk.type;
86
+ switch (type) {
87
+ case "text-delta":
88
+ controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
89
+ break;
90
+ case "tool-call-begin":
91
+ controller.enqueue(
92
+ "b:" + JSON.stringify({
93
+ toolCallId: chunk.toolCallId,
94
+ toolName: chunk.toolName
95
+ }) + "\n"
96
+ );
97
+ break;
98
+ case "tool-call-delta":
99
+ controller.enqueue(
100
+ "c:" + JSON.stringify({
101
+ toolCallId: chunk.toolCallId,
102
+ argsTextDelta: chunk.argsTextDelta
103
+ }) + "\n"
104
+ );
105
+ break;
106
+ case "tool-result":
107
+ controller.enqueue(
108
+ "a:" + JSON.stringify({
109
+ toolCallId: chunk.toolCallId,
110
+ result: chunk.result
111
+ }) + "\n"
112
+ );
113
+ break;
114
+ case "error":
115
+ controller.enqueue(`3:${JSON.stringify(chunk.error)}
116
+ `);
117
+ break;
118
+ default:
119
+ const exhaustiveCheck = type;
120
+ throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
121
+ }
122
+ }
123
+ });
124
+ return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
125
+ });
60
126
  }
61
- tee() {
62
- const [readable1, readable2] = this.readable.tee();
63
- return [new _AssistantStream(readable1), new _AssistantStream(readable2)];
127
+ };
128
+ var decodeStreamPart = (part) => {
129
+ const index = part.indexOf(":");
130
+ if (index === -1) throw new Error("Invalid stream part");
131
+ return {
132
+ type: part.slice(0, index),
133
+ value: JSON.parse(part.slice(index + 1))
134
+ };
135
+ };
136
+ var DataStreamDecoder = class {
137
+ _transformStream;
138
+ get writable() {
139
+ return this._transformStream.writable;
140
+ }
141
+ get readable() {
142
+ return this._transformStream.readable;
143
+ }
144
+ constructor() {
145
+ this._transformStream = new PipeableTransformStream((readable) => {
146
+ const transform = new TransformStream({
147
+ transform(chunk, controller) {
148
+ const { type, value } = decodeStreamPart(chunk);
149
+ switch (type) {
150
+ case "0":
151
+ controller.enqueue({
152
+ type: "text-delta",
153
+ textDelta: value
154
+ });
155
+ break;
156
+ case "b": {
157
+ const { toolCallId, toolName } = value;
158
+ controller.enqueue({
159
+ type: "tool-call-begin",
160
+ toolCallId,
161
+ toolName
162
+ });
163
+ break;
164
+ }
165
+ case "c": {
166
+ const { toolCallId, argsTextDelta } = value;
167
+ controller.enqueue({
168
+ type: "tool-call-delta",
169
+ toolCallId,
170
+ argsTextDelta
171
+ });
172
+ break;
173
+ }
174
+ case "a": {
175
+ const { toolCallId, result } = value;
176
+ controller.enqueue({
177
+ type: "tool-result",
178
+ toolCallId,
179
+ result
180
+ });
181
+ break;
182
+ }
183
+ case "9": {
184
+ const { toolCallId, args } = value;
185
+ controller.enqueue({
186
+ type: "tool-call-begin",
187
+ toolCallId,
188
+ toolName: toolCallId
189
+ });
190
+ controller.enqueue({
191
+ type: "tool-call-delta",
192
+ toolCallId,
193
+ argsTextDelta: JSON.stringify(args)
194
+ });
195
+ break;
196
+ }
197
+ case "2":
198
+ case "3":
199
+ case "8":
200
+ case "d":
201
+ case "e": {
202
+ break;
203
+ }
204
+ default:
205
+ const exhaustiveCheck = type;
206
+ throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
207
+ }
208
+ }
209
+ });
210
+ return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
211
+ });
212
+ }
213
+ };
214
+ var ChunkByLineStream = class extends TransformStream {
215
+ buffer = "";
216
+ constructor() {
217
+ super({
218
+ transform: (chunk, controller) => {
219
+ this.buffer += chunk;
220
+ const lines = this.buffer.split("\n");
221
+ for (let i = 0; i < lines.length - 1; i++) {
222
+ controller.enqueue(lines[i]);
223
+ }
224
+ this.buffer = lines[lines.length - 1];
225
+ },
226
+ flush: (controller) => {
227
+ if (this.buffer) {
228
+ controller.enqueue(this.buffer);
229
+ }
230
+ }
231
+ });
64
232
  }
65
233
  };
66
234
 
@@ -89,19 +257,17 @@ var TextStreamControllerImpl = class {
89
257
  }
90
258
  };
91
259
  var createTextStream = (readable) => {
92
- return new AssistantStream(
93
- new ReadableStream({
94
- start(c) {
95
- return readable.start?.(new TextStreamControllerImpl(c));
96
- },
97
- pull(c) {
98
- return readable.pull?.(new TextStreamControllerImpl(c));
99
- },
100
- cancel(c) {
101
- return readable.cancel?.(c);
102
- }
103
- })
104
- );
260
+ return new ReadableStream({
261
+ start(c) {
262
+ return readable.start?.(new TextStreamControllerImpl(c));
263
+ },
264
+ pull(c) {
265
+ return readable.pull?.(new TextStreamControllerImpl(c));
266
+ },
267
+ cancel(c) {
268
+ return readable.cancel?.(c);
269
+ }
270
+ });
105
271
  };
106
272
 
107
273
  // src/core/modules/tool-call.ts
@@ -119,7 +285,7 @@ var ToolCallStreamControllerImpl = class {
119
285
  this._argsTextController = c;
120
286
  }
121
287
  });
122
- stream.readable.pipeTo(
288
+ stream.pipeTo(
123
289
  new WritableStream({
124
290
  write: (chunk) => {
125
291
  if (chunk.type !== "text-delta")
@@ -159,19 +325,17 @@ var createToolCallStream = (readable) => {
159
325
  toolCallId: readable.toolCallId,
160
326
  toolName: readable.toolName
161
327
  };
162
- return new AssistantStream(
163
- new ReadableStream({
164
- start(c) {
165
- return readable.start?.(new ToolCallStreamControllerImpl(c, options));
166
- },
167
- pull(c) {
168
- return readable.pull?.(new ToolCallStreamControllerImpl(c, options));
169
- },
170
- cancel(c) {
171
- return readable.cancel?.(c);
172
- }
173
- })
174
- );
328
+ return new ReadableStream({
329
+ start(c) {
330
+ return readable.start?.(new ToolCallStreamControllerImpl(c, options));
331
+ },
332
+ pull(c) {
333
+ return readable.pull?.(new ToolCallStreamControllerImpl(c, options));
334
+ },
335
+ cancel(c) {
336
+ return readable.cancel?.(c);
337
+ }
338
+ });
175
339
  };
176
340
 
177
341
  // src/core/modules/runs.ts
@@ -244,7 +408,7 @@ var createMergeStream = () => {
244
408
  throw new Error(
245
409
  "Cannot add streams after the run callback has settled."
246
410
  );
247
- const item = { reader: stream.readable.getReader() };
411
+ const item = { reader: stream.getReader() };
248
412
  list.push(item);
249
413
  if (list.length === 1) {
250
414
  handlePull(item);
@@ -262,7 +426,7 @@ var RunControllerImpl = class {
262
426
  this._merge.seal();
263
427
  this._textPartController?.close();
264
428
  }
265
- appendStep(stream) {
429
+ merge(stream) {
266
430
  this._merge.addStream(stream);
267
431
  }
268
432
  appendText(textDelta) {
@@ -278,38 +442,43 @@ var RunControllerImpl = class {
278
442
  controller = c;
279
443
  }
280
444
  });
281
- this.appendStep(textStream);
445
+ this.merge(textStream);
282
446
  return controller;
283
447
  }
284
448
  addToolCallPart(options) {
285
- const opt = typeof options === "string" ? { toolName: options, toolCallId: generateId() } : options;
449
+ const opt = typeof options === "string" ? { toolName: options } : options;
286
450
  let controller;
287
451
  const toolCallStream = createToolCallStream({
288
- toolCallId: opt.toolCallId,
452
+ toolCallId: opt.toolCallId ?? generateId(),
289
453
  toolName: opt.toolName,
290
454
  start(c) {
291
455
  controller = c;
292
456
  }
293
457
  });
294
- this.appendStep(toolCallStream);
458
+ this.merge(toolCallStream);
459
+ if (opt.args !== void 0) {
460
+ controller.argsText.append(JSON.stringify(opt.args));
461
+ controller.argsText.close();
462
+ }
463
+ if (opt !== void 0) {
464
+ controller.setResult(opt.result);
465
+ }
295
466
  return controller;
296
467
  }
297
468
  addError(error) {
298
469
  this._merge.addStream(
299
- new AssistantStream(
300
- new ReadableStream({
301
- start(c) {
302
- c.enqueue({
303
- type: "error",
304
- error
305
- });
306
- }
307
- })
308
- )
470
+ new ReadableStream({
471
+ start(c) {
472
+ c.enqueue({
473
+ type: "error",
474
+ error
475
+ });
476
+ }
477
+ })
309
478
  );
310
479
  }
311
480
  };
312
- function createAssistantRun(callback) {
481
+ function createAssistantStream(callback) {
313
482
  const controller = new RunControllerImpl();
314
483
  const promiseOrVoid = callback(controller);
315
484
  if (promiseOrVoid instanceof Promise) {
@@ -327,183 +496,14 @@ function createAssistantRun(callback) {
327
496
  } else {
328
497
  controller.close();
329
498
  }
330
- return new AssistantStream(controller.getReadable());
499
+ return controller.getReadable();
500
+ }
501
+ function createAssistantStreamResponse(callback) {
502
+ return AssistantStream.toResponse(
503
+ createAssistantStream(callback),
504
+ new DataStreamEncoder()
505
+ );
331
506
  }
332
-
333
- // src/core/utils/PipeableTransformStream.ts
334
- var PipeableTransformStream = class extends TransformStream {
335
- constructor(transform) {
336
- super();
337
- const readable = transform(super.readable);
338
- Object.defineProperty(this, "readable", {
339
- value: readable,
340
- writable: false
341
- });
342
- }
343
- };
344
-
345
- // src/core/serialization/DataStream.ts
346
- var DataStreamEncoder = class {
347
- _transformStream;
348
- get writable() {
349
- return this._transformStream.writable;
350
- }
351
- get readable() {
352
- return this._transformStream.readable;
353
- }
354
- constructor() {
355
- this._transformStream = new PipeableTransformStream((readable) => {
356
- const transform = new TransformStream({
357
- transform(chunk, controller) {
358
- const type = chunk.type;
359
- switch (type) {
360
- case "text-delta":
361
- controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
362
- break;
363
- case "tool-call-begin":
364
- controller.enqueue(
365
- "b:" + JSON.stringify({
366
- toolCallId: chunk.toolCallId,
367
- toolName: chunk.toolName
368
- }) + "\n"
369
- );
370
- break;
371
- case "tool-call-delta":
372
- controller.enqueue(
373
- "c:" + JSON.stringify({
374
- toolCallId: chunk.toolCallId,
375
- argsTextDelta: chunk.argsTextDelta
376
- }) + "\n"
377
- );
378
- break;
379
- case "tool-result":
380
- controller.enqueue(
381
- "a:" + JSON.stringify({
382
- toolCallId: chunk.toolCallId,
383
- result: chunk.result
384
- }) + "\n"
385
- );
386
- break;
387
- case "error":
388
- controller.enqueue(`3:${JSON.stringify(chunk.error)}
389
- `);
390
- break;
391
- default:
392
- const exhaustiveCheck = type;
393
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
394
- }
395
- }
396
- });
397
- return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
398
- });
399
- }
400
- };
401
- var decodeStreamPart = (part) => {
402
- const index = part.indexOf(":");
403
- if (index === -1) throw new Error("Invalid stream part");
404
- return {
405
- type: part.slice(0, index),
406
- value: JSON.parse(part.slice(index + 1))
407
- };
408
- };
409
- var DataStreamDecoder = class {
410
- _transformStream;
411
- get writable() {
412
- return this._transformStream.writable;
413
- }
414
- get readable() {
415
- return this._transformStream.readable;
416
- }
417
- constructor() {
418
- this._transformStream = new PipeableTransformStream((readable) => {
419
- const transform = new TransformStream({
420
- transform(chunk, controller) {
421
- const { type, value } = decodeStreamPart(chunk);
422
- switch (type) {
423
- case "0":
424
- controller.enqueue({
425
- type: "text-delta",
426
- textDelta: value
427
- });
428
- break;
429
- case "b": {
430
- const { toolCallId, toolName } = value;
431
- controller.enqueue({
432
- type: "tool-call-begin",
433
- toolCallId,
434
- toolName
435
- });
436
- break;
437
- }
438
- case "c": {
439
- const { toolCallId, argsTextDelta } = value;
440
- controller.enqueue({
441
- type: "tool-call-delta",
442
- toolCallId,
443
- argsTextDelta
444
- });
445
- break;
446
- }
447
- case "a": {
448
- const { toolCallId, result } = value;
449
- controller.enqueue({
450
- type: "tool-result",
451
- toolCallId,
452
- result
453
- });
454
- break;
455
- }
456
- case "9": {
457
- const { toolCallId, args } = value;
458
- controller.enqueue({
459
- type: "tool-call-begin",
460
- toolCallId,
461
- toolName: toolCallId
462
- });
463
- controller.enqueue({
464
- type: "tool-call-delta",
465
- toolCallId,
466
- argsTextDelta: JSON.stringify(args)
467
- });
468
- break;
469
- }
470
- case "2":
471
- case "3":
472
- case "8":
473
- case "d":
474
- case "e": {
475
- break;
476
- }
477
- default:
478
- const exhaustiveCheck = type;
479
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
480
- }
481
- }
482
- });
483
- return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
484
- });
485
- }
486
- };
487
- var ChunkByLineStream = class extends TransformStream {
488
- buffer = "";
489
- constructor() {
490
- super({
491
- transform: (chunk, controller) => {
492
- this.buffer += chunk;
493
- const lines = this.buffer.split("\n");
494
- for (let i = 0; i < lines.length - 1; i++) {
495
- controller.enqueue(lines[i]);
496
- }
497
- this.buffer = lines[lines.length - 1];
498
- },
499
- flush: (controller) => {
500
- if (this.buffer) {
501
- controller.enqueue(this.buffer);
502
- }
503
- }
504
- });
505
- }
506
- };
507
507
 
508
508
  // src/core/serialization/PlainText.ts
509
509
  var PlainTextEncoder = class {
@@ -1056,7 +1056,7 @@ var AssistantMessageStream = class _AssistantMessageStream {
1056
1056
  }
1057
1057
  static fromAssistantStream(stream) {
1058
1058
  return new _AssistantMessageStream(
1059
- stream.readable.pipeThrough(assistantMessageAccumulator())
1059
+ stream.pipeThrough(assistantMessageAccumulator())
1060
1060
  );
1061
1061
  }
1062
1062
  async unstable_result() {
@@ -1102,6 +1102,7 @@ var AssistantMessageStream = class _AssistantMessageStream {
1102
1102
  DataStreamEncoder,
1103
1103
  PlainTextDecoder,
1104
1104
  PlainTextEncoder,
1105
- createAssistantRun
1105
+ createAssistantStream,
1106
+ createAssistantStreamResponse
1106
1107
  });
1107
1108
  //# sourceMappingURL=index.js.map