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.mjs CHANGED
@@ -1,7 +1,197 @@
1
1
  import {
2
- AssistantStream,
3
2
  generateId
4
- } from "./chunk-K4MZ4HSJ.mjs";
3
+ } from "./chunk-ZSSWV6GU.mjs";
4
+
5
+ // src/core/AssistantStream.ts
6
+ var AssistantStream = {
7
+ toResponse(stream, transformer) {
8
+ return new Response(AssistantStream.toByteStream(stream, transformer));
9
+ },
10
+ fromResponse(response, transformer) {
11
+ return AssistantStream.fromByteStream(response.body, transformer);
12
+ },
13
+ toByteStream(stream, transformer) {
14
+ return stream.pipeThrough(transformer);
15
+ },
16
+ fromByteStream(readable, transformer) {
17
+ return readable.pipeThrough(transformer);
18
+ }
19
+ };
20
+
21
+ // src/core/utils/PipeableTransformStream.ts
22
+ var PipeableTransformStream = class extends TransformStream {
23
+ constructor(transform) {
24
+ super();
25
+ const readable = transform(super.readable);
26
+ Object.defineProperty(this, "readable", {
27
+ value: readable,
28
+ writable: false
29
+ });
30
+ }
31
+ };
32
+
33
+ // src/core/serialization/DataStream.ts
34
+ var DataStreamEncoder = class {
35
+ _transformStream;
36
+ get writable() {
37
+ return this._transformStream.writable;
38
+ }
39
+ get readable() {
40
+ return this._transformStream.readable;
41
+ }
42
+ constructor() {
43
+ this._transformStream = new PipeableTransformStream((readable) => {
44
+ const transform = new TransformStream({
45
+ transform(chunk, controller) {
46
+ const type = chunk.type;
47
+ switch (type) {
48
+ case "text-delta":
49
+ controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
50
+ break;
51
+ case "tool-call-begin":
52
+ controller.enqueue(
53
+ "b:" + JSON.stringify({
54
+ toolCallId: chunk.toolCallId,
55
+ toolName: chunk.toolName
56
+ }) + "\n"
57
+ );
58
+ break;
59
+ case "tool-call-delta":
60
+ controller.enqueue(
61
+ "c:" + JSON.stringify({
62
+ toolCallId: chunk.toolCallId,
63
+ argsTextDelta: chunk.argsTextDelta
64
+ }) + "\n"
65
+ );
66
+ break;
67
+ case "tool-result":
68
+ controller.enqueue(
69
+ "a:" + JSON.stringify({
70
+ toolCallId: chunk.toolCallId,
71
+ result: chunk.result
72
+ }) + "\n"
73
+ );
74
+ break;
75
+ case "error":
76
+ controller.enqueue(`3:${JSON.stringify(chunk.error)}
77
+ `);
78
+ break;
79
+ default:
80
+ const exhaustiveCheck = type;
81
+ throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
82
+ }
83
+ }
84
+ });
85
+ return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
86
+ });
87
+ }
88
+ };
89
+ var decodeStreamPart = (part) => {
90
+ const index = part.indexOf(":");
91
+ if (index === -1) throw new Error("Invalid stream part");
92
+ return {
93
+ type: part.slice(0, index),
94
+ value: JSON.parse(part.slice(index + 1))
95
+ };
96
+ };
97
+ var DataStreamDecoder = class {
98
+ _transformStream;
99
+ get writable() {
100
+ return this._transformStream.writable;
101
+ }
102
+ get readable() {
103
+ return this._transformStream.readable;
104
+ }
105
+ constructor() {
106
+ this._transformStream = new PipeableTransformStream((readable) => {
107
+ const transform = new TransformStream({
108
+ transform(chunk, controller) {
109
+ const { type, value } = decodeStreamPart(chunk);
110
+ switch (type) {
111
+ case "0":
112
+ controller.enqueue({
113
+ type: "text-delta",
114
+ textDelta: value
115
+ });
116
+ break;
117
+ case "b": {
118
+ const { toolCallId, toolName } = value;
119
+ controller.enqueue({
120
+ type: "tool-call-begin",
121
+ toolCallId,
122
+ toolName
123
+ });
124
+ break;
125
+ }
126
+ case "c": {
127
+ const { toolCallId, argsTextDelta } = value;
128
+ controller.enqueue({
129
+ type: "tool-call-delta",
130
+ toolCallId,
131
+ argsTextDelta
132
+ });
133
+ break;
134
+ }
135
+ case "a": {
136
+ const { toolCallId, result } = value;
137
+ controller.enqueue({
138
+ type: "tool-result",
139
+ toolCallId,
140
+ result
141
+ });
142
+ break;
143
+ }
144
+ case "9": {
145
+ const { toolCallId, args } = value;
146
+ controller.enqueue({
147
+ type: "tool-call-begin",
148
+ toolCallId,
149
+ toolName: toolCallId
150
+ });
151
+ controller.enqueue({
152
+ type: "tool-call-delta",
153
+ toolCallId,
154
+ argsTextDelta: JSON.stringify(args)
155
+ });
156
+ break;
157
+ }
158
+ case "2":
159
+ case "3":
160
+ case "8":
161
+ case "d":
162
+ case "e": {
163
+ break;
164
+ }
165
+ default:
166
+ const exhaustiveCheck = type;
167
+ throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
168
+ }
169
+ }
170
+ });
171
+ return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
172
+ });
173
+ }
174
+ };
175
+ var ChunkByLineStream = class extends TransformStream {
176
+ buffer = "";
177
+ constructor() {
178
+ super({
179
+ transform: (chunk, controller) => {
180
+ this.buffer += chunk;
181
+ const lines = this.buffer.split("\n");
182
+ for (let i = 0; i < lines.length - 1; i++) {
183
+ controller.enqueue(lines[i]);
184
+ }
185
+ this.buffer = lines[lines.length - 1];
186
+ },
187
+ flush: (controller) => {
188
+ if (this.buffer) {
189
+ controller.enqueue(this.buffer);
190
+ }
191
+ }
192
+ });
193
+ }
194
+ };
5
195
 
6
196
  // src/core/modules/text.ts
7
197
  var TextStreamControllerImpl = class {
@@ -21,19 +211,17 @@ var TextStreamControllerImpl = class {
21
211
  }
22
212
  };
23
213
  var createTextStream = (readable) => {
24
- return new AssistantStream(
25
- new ReadableStream({
26
- start(c) {
27
- return readable.start?.(new TextStreamControllerImpl(c));
28
- },
29
- pull(c) {
30
- return readable.pull?.(new TextStreamControllerImpl(c));
31
- },
32
- cancel(c) {
33
- return readable.cancel?.(c);
34
- }
35
- })
36
- );
214
+ return new ReadableStream({
215
+ start(c) {
216
+ return readable.start?.(new TextStreamControllerImpl(c));
217
+ },
218
+ pull(c) {
219
+ return readable.pull?.(new TextStreamControllerImpl(c));
220
+ },
221
+ cancel(c) {
222
+ return readable.cancel?.(c);
223
+ }
224
+ });
37
225
  };
38
226
 
39
227
  // src/core/modules/tool-call.ts
@@ -51,7 +239,7 @@ var ToolCallStreamControllerImpl = class {
51
239
  this._argsTextController = c;
52
240
  }
53
241
  });
54
- stream.readable.pipeTo(
242
+ stream.pipeTo(
55
243
  new WritableStream({
56
244
  write: (chunk) => {
57
245
  if (chunk.type !== "text-delta")
@@ -91,19 +279,17 @@ var createToolCallStream = (readable) => {
91
279
  toolCallId: readable.toolCallId,
92
280
  toolName: readable.toolName
93
281
  };
94
- return new AssistantStream(
95
- new ReadableStream({
96
- start(c) {
97
- return readable.start?.(new ToolCallStreamControllerImpl(c, options));
98
- },
99
- pull(c) {
100
- return readable.pull?.(new ToolCallStreamControllerImpl(c, options));
101
- },
102
- cancel(c) {
103
- return readable.cancel?.(c);
104
- }
105
- })
106
- );
282
+ return new ReadableStream({
283
+ start(c) {
284
+ return readable.start?.(new ToolCallStreamControllerImpl(c, options));
285
+ },
286
+ pull(c) {
287
+ return readable.pull?.(new ToolCallStreamControllerImpl(c, options));
288
+ },
289
+ cancel(c) {
290
+ return readable.cancel?.(c);
291
+ }
292
+ });
107
293
  };
108
294
 
109
295
  // src/core/modules/runs.ts
@@ -176,7 +362,7 @@ var createMergeStream = () => {
176
362
  throw new Error(
177
363
  "Cannot add streams after the run callback has settled."
178
364
  );
179
- const item = { reader: stream.readable.getReader() };
365
+ const item = { reader: stream.getReader() };
180
366
  list.push(item);
181
367
  if (list.length === 1) {
182
368
  handlePull(item);
@@ -194,7 +380,7 @@ var RunControllerImpl = class {
194
380
  this._merge.seal();
195
381
  this._textPartController?.close();
196
382
  }
197
- appendStep(stream) {
383
+ merge(stream) {
198
384
  this._merge.addStream(stream);
199
385
  }
200
386
  appendText(textDelta) {
@@ -210,38 +396,43 @@ var RunControllerImpl = class {
210
396
  controller = c;
211
397
  }
212
398
  });
213
- this.appendStep(textStream);
399
+ this.merge(textStream);
214
400
  return controller;
215
401
  }
216
402
  addToolCallPart(options) {
217
- const opt = typeof options === "string" ? { toolName: options, toolCallId: generateId() } : options;
403
+ const opt = typeof options === "string" ? { toolName: options } : options;
218
404
  let controller;
219
405
  const toolCallStream = createToolCallStream({
220
- toolCallId: opt.toolCallId,
406
+ toolCallId: opt.toolCallId ?? generateId(),
221
407
  toolName: opt.toolName,
222
408
  start(c) {
223
409
  controller = c;
224
410
  }
225
411
  });
226
- this.appendStep(toolCallStream);
412
+ this.merge(toolCallStream);
413
+ if (opt.args !== void 0) {
414
+ controller.argsText.append(JSON.stringify(opt.args));
415
+ controller.argsText.close();
416
+ }
417
+ if (opt !== void 0) {
418
+ controller.setResult(opt.result);
419
+ }
227
420
  return controller;
228
421
  }
229
422
  addError(error) {
230
423
  this._merge.addStream(
231
- new AssistantStream(
232
- new ReadableStream({
233
- start(c) {
234
- c.enqueue({
235
- type: "error",
236
- error
237
- });
238
- }
239
- })
240
- )
424
+ new ReadableStream({
425
+ start(c) {
426
+ c.enqueue({
427
+ type: "error",
428
+ error
429
+ });
430
+ }
431
+ })
241
432
  );
242
433
  }
243
434
  };
244
- function createAssistantRun(callback) {
435
+ function createAssistantStream(callback) {
245
436
  const controller = new RunControllerImpl();
246
437
  const promiseOrVoid = callback(controller);
247
438
  if (promiseOrVoid instanceof Promise) {
@@ -259,183 +450,14 @@ function createAssistantRun(callback) {
259
450
  } else {
260
451
  controller.close();
261
452
  }
262
- return new AssistantStream(controller.getReadable());
453
+ return controller.getReadable();
454
+ }
455
+ function createAssistantStreamResponse(callback) {
456
+ return AssistantStream.toResponse(
457
+ createAssistantStream(callback),
458
+ new DataStreamEncoder()
459
+ );
263
460
  }
264
-
265
- // src/core/utils/PipeableTransformStream.ts
266
- var PipeableTransformStream = class extends TransformStream {
267
- constructor(transform) {
268
- super();
269
- const readable = transform(super.readable);
270
- Object.defineProperty(this, "readable", {
271
- value: readable,
272
- writable: false
273
- });
274
- }
275
- };
276
-
277
- // src/core/serialization/DataStream.ts
278
- var DataStreamEncoder = class {
279
- _transformStream;
280
- get writable() {
281
- return this._transformStream.writable;
282
- }
283
- get readable() {
284
- return this._transformStream.readable;
285
- }
286
- constructor() {
287
- this._transformStream = new PipeableTransformStream((readable) => {
288
- const transform = new TransformStream({
289
- transform(chunk, controller) {
290
- const type = chunk.type;
291
- switch (type) {
292
- case "text-delta":
293
- controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
294
- break;
295
- case "tool-call-begin":
296
- controller.enqueue(
297
- "b:" + JSON.stringify({
298
- toolCallId: chunk.toolCallId,
299
- toolName: chunk.toolName
300
- }) + "\n"
301
- );
302
- break;
303
- case "tool-call-delta":
304
- controller.enqueue(
305
- "c:" + JSON.stringify({
306
- toolCallId: chunk.toolCallId,
307
- argsTextDelta: chunk.argsTextDelta
308
- }) + "\n"
309
- );
310
- break;
311
- case "tool-result":
312
- controller.enqueue(
313
- "a:" + JSON.stringify({
314
- toolCallId: chunk.toolCallId,
315
- result: chunk.result
316
- }) + "\n"
317
- );
318
- break;
319
- case "error":
320
- controller.enqueue(`3:${JSON.stringify(chunk.error)}
321
- `);
322
- break;
323
- default:
324
- const exhaustiveCheck = type;
325
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
326
- }
327
- }
328
- });
329
- return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
330
- });
331
- }
332
- };
333
- var decodeStreamPart = (part) => {
334
- const index = part.indexOf(":");
335
- if (index === -1) throw new Error("Invalid stream part");
336
- return {
337
- type: part.slice(0, index),
338
- value: JSON.parse(part.slice(index + 1))
339
- };
340
- };
341
- var DataStreamDecoder = class {
342
- _transformStream;
343
- get writable() {
344
- return this._transformStream.writable;
345
- }
346
- get readable() {
347
- return this._transformStream.readable;
348
- }
349
- constructor() {
350
- this._transformStream = new PipeableTransformStream((readable) => {
351
- const transform = new TransformStream({
352
- transform(chunk, controller) {
353
- const { type, value } = decodeStreamPart(chunk);
354
- switch (type) {
355
- case "0":
356
- controller.enqueue({
357
- type: "text-delta",
358
- textDelta: value
359
- });
360
- break;
361
- case "b": {
362
- const { toolCallId, toolName } = value;
363
- controller.enqueue({
364
- type: "tool-call-begin",
365
- toolCallId,
366
- toolName
367
- });
368
- break;
369
- }
370
- case "c": {
371
- const { toolCallId, argsTextDelta } = value;
372
- controller.enqueue({
373
- type: "tool-call-delta",
374
- toolCallId,
375
- argsTextDelta
376
- });
377
- break;
378
- }
379
- case "a": {
380
- const { toolCallId, result } = value;
381
- controller.enqueue({
382
- type: "tool-result",
383
- toolCallId,
384
- result
385
- });
386
- break;
387
- }
388
- case "9": {
389
- const { toolCallId, args } = value;
390
- controller.enqueue({
391
- type: "tool-call-begin",
392
- toolCallId,
393
- toolName: toolCallId
394
- });
395
- controller.enqueue({
396
- type: "tool-call-delta",
397
- toolCallId,
398
- argsTextDelta: JSON.stringify(args)
399
- });
400
- break;
401
- }
402
- case "2":
403
- case "3":
404
- case "8":
405
- case "d":
406
- case "e": {
407
- break;
408
- }
409
- default:
410
- const exhaustiveCheck = type;
411
- throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
412
- }
413
- }
414
- });
415
- return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
416
- });
417
- }
418
- };
419
- var ChunkByLineStream = class extends TransformStream {
420
- buffer = "";
421
- constructor() {
422
- super({
423
- transform: (chunk, controller) => {
424
- this.buffer += chunk;
425
- const lines = this.buffer.split("\n");
426
- for (let i = 0; i < lines.length - 1; i++) {
427
- controller.enqueue(lines[i]);
428
- }
429
- this.buffer = lines[lines.length - 1];
430
- },
431
- flush: (controller) => {
432
- if (this.buffer) {
433
- controller.enqueue(this.buffer);
434
- }
435
- }
436
- });
437
- }
438
- };
439
461
 
440
462
  // src/core/serialization/PlainText.ts
441
463
  var PlainTextEncoder = class {
@@ -988,7 +1010,7 @@ var AssistantMessageStream = class _AssistantMessageStream {
988
1010
  }
989
1011
  static fromAssistantStream(stream) {
990
1012
  return new _AssistantMessageStream(
991
- stream.readable.pipeThrough(assistantMessageAccumulator())
1013
+ stream.pipeThrough(assistantMessageAccumulator())
992
1014
  );
993
1015
  }
994
1016
  async unstable_result() {
@@ -1033,6 +1055,7 @@ export {
1033
1055
  DataStreamEncoder,
1034
1056
  PlainTextDecoder,
1035
1057
  PlainTextEncoder,
1036
- createAssistantRun
1058
+ createAssistantStream,
1059
+ createAssistantStreamResponse
1037
1060
  };
1038
1061
  //# sourceMappingURL=index.mjs.map