assistant-stream 0.0.18 → 0.0.20

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,225 +442,68 @@ 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) {
316
- promiseOrVoid.catch((e) => {
317
- controller.addError(e instanceof Error ? e.message : String(e));
318
- throw e;
319
- }).finally(() => controller.close());
485
+ const runTask = async () => {
486
+ try {
487
+ await promiseOrVoid;
488
+ } catch (e) {
489
+ controller.addError(e instanceof Error ? e.message : String(e));
490
+ throw e;
491
+ } finally {
492
+ controller.close();
493
+ }
494
+ };
495
+ runTask();
320
496
  } else {
321
497
  controller.close();
322
498
  }
323
- 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
+ );
324
506
  }
325
-
326
- // src/core/utils/PipeableTransformStream.ts
327
- var PipeableTransformStream = class extends TransformStream {
328
- constructor(transform) {
329
- super();
330
- const readable = transform(super.readable);
331
- Object.defineProperty(this, "readable", {
332
- value: readable,
333
- writable: false
334
- });
335
- }
336
- };
337
-
338
- // src/core/serialization/DataStream.ts
339
- var DataStreamEncoder = class {
340
- _transformStream;
341
- get writable() {
342
- return this._transformStream.writable;
343
- }
344
- get readable() {
345
- return this._transformStream.readable;
346
- }
347
- constructor() {
348
- this._transformStream = new PipeableTransformStream((readable) => {
349
- const transform = new TransformStream({
350
- transform(chunk, controller) {
351
- const type = chunk.type;
352
- switch (type) {
353
- case "text-delta":
354
- controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
355
- break;
356
- case "tool-call-begin":
357
- controller.enqueue(
358
- "b:" + JSON.stringify({
359
- toolCallId: chunk.toolCallId,
360
- toolName: chunk.toolName
361
- }) + "\n"
362
- );
363
- break;
364
- case "tool-call-delta":
365
- controller.enqueue(
366
- "c:" + JSON.stringify({
367
- toolCallId: chunk.toolCallId,
368
- argsTextDelta: chunk.argsTextDelta
369
- }) + "\n"
370
- );
371
- break;
372
- case "tool-result":
373
- controller.enqueue(
374
- "a:" + JSON.stringify({
375
- toolCallId: chunk.toolCallId,
376
- result: chunk.result
377
- }) + "\n"
378
- );
379
- break;
380
- case "error":
381
- controller.enqueue(`3:${JSON.stringify(chunk.error)}
382
- `);
383
- break;
384
- default:
385
- const exhaustiveCheck = type;
386
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
387
- }
388
- }
389
- });
390
- return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
391
- });
392
- }
393
- };
394
- var decodeStreamPart = (part) => {
395
- const index = part.indexOf(":");
396
- if (index === -1) throw new Error("Invalid stream part");
397
- return {
398
- type: part.slice(0, index),
399
- value: JSON.parse(part.slice(index + 1))
400
- };
401
- };
402
- var DataStreamDecoder = class {
403
- _transformStream;
404
- get writable() {
405
- return this._transformStream.writable;
406
- }
407
- get readable() {
408
- return this._transformStream.readable;
409
- }
410
- constructor() {
411
- this._transformStream = new PipeableTransformStream((readable) => {
412
- const transform = new TransformStream({
413
- transform(chunk, controller) {
414
- const { type, value } = decodeStreamPart(chunk);
415
- switch (type) {
416
- case "0":
417
- controller.enqueue({
418
- type: "text-delta",
419
- textDelta: value
420
- });
421
- break;
422
- case "b": {
423
- const { toolCallId, toolName } = value;
424
- controller.enqueue({
425
- type: "tool-call-begin",
426
- toolCallId,
427
- toolName
428
- });
429
- break;
430
- }
431
- case "c": {
432
- const { toolCallId, argsTextDelta } = value;
433
- controller.enqueue({
434
- type: "tool-call-delta",
435
- toolCallId,
436
- argsTextDelta
437
- });
438
- break;
439
- }
440
- case "a": {
441
- const { toolCallId, result } = value;
442
- controller.enqueue({
443
- type: "tool-result",
444
- toolCallId,
445
- result
446
- });
447
- break;
448
- }
449
- case "9": {
450
- const { toolCallId, args } = value;
451
- controller.enqueue({
452
- type: "tool-call-begin",
453
- toolCallId,
454
- toolName: toolCallId
455
- });
456
- controller.enqueue({
457
- type: "tool-call-delta",
458
- toolCallId,
459
- argsTextDelta: JSON.stringify(args)
460
- });
461
- break;
462
- }
463
- case "2":
464
- case "3":
465
- case "8":
466
- case "d":
467
- case "e": {
468
- break;
469
- }
470
- default:
471
- const exhaustiveCheck = type;
472
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
473
- }
474
- }
475
- });
476
- return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
477
- });
478
- }
479
- };
480
- var ChunkByLineStream = class extends TransformStream {
481
- buffer = "";
482
- constructor() {
483
- super({
484
- transform: (chunk, controller) => {
485
- this.buffer += chunk;
486
- const lines = this.buffer.split("\n");
487
- for (let i = 0; i < lines.length - 1; i++) {
488
- controller.enqueue(lines[i]);
489
- }
490
- this.buffer = lines[lines.length - 1];
491
- },
492
- flush: (controller) => {
493
- if (this.buffer) {
494
- controller.enqueue(this.buffer);
495
- }
496
- }
497
- });
498
- }
499
- };
500
507
 
501
508
  // src/core/serialization/PlainText.ts
502
509
  var PlainTextEncoder = class {
@@ -1049,7 +1056,7 @@ var AssistantMessageStream = class _AssistantMessageStream {
1049
1056
  }
1050
1057
  static fromAssistantStream(stream) {
1051
1058
  return new _AssistantMessageStream(
1052
- stream.readable.pipeThrough(assistantMessageAccumulator())
1059
+ stream.pipeThrough(assistantMessageAccumulator())
1053
1060
  );
1054
1061
  }
1055
1062
  async unstable_result() {
@@ -1095,6 +1102,7 @@ var AssistantMessageStream = class _AssistantMessageStream {
1095
1102
  DataStreamEncoder,
1096
1103
  PlainTextDecoder,
1097
1104
  PlainTextEncoder,
1098
- createAssistantRun
1105
+ createAssistantStream,
1106
+ createAssistantStreamResponse
1099
1107
  });
1100
1108
  //# sourceMappingURL=index.js.map