assistant-stream 0.0.21 → 0.0.22
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/ai-sdk.d.mts +20 -4
- package/dist/ai-sdk.d.ts +20 -4
- package/dist/ai-sdk.js +691 -42
- package/dist/ai-sdk.js.map +1 -1
- package/dist/ai-sdk.mjs +190 -44
- package/dist/ai-sdk.mjs.map +1 -1
- package/dist/assistant-stream-CEVTPU3I.d.mts +211 -0
- package/dist/assistant-stream-CEVTPU3I.d.ts +211 -0
- package/dist/chunk-DISBVUTK.mjs +932 -0
- package/dist/chunk-DISBVUTK.mjs.map +1 -0
- package/dist/index.d.mts +23 -103
- package/dist/index.d.ts +23 -103
- package/dist/index.js +1189 -490
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +417 -625
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/dist/AssistantStream-dm_T4K6d.d.mts +0 -28
- package/dist/AssistantStream-dm_T4K6d.d.ts +0 -28
- package/dist/chunk-ZSSWV6GU.mjs +0 -11
- package/dist/chunk-ZSSWV6GU.mjs.map +0 -1
|
@@ -0,0 +1,932 @@
|
|
|
1
|
+
// src/core/AssistantStream.ts
|
|
2
|
+
var AssistantStream = {
|
|
3
|
+
toResponse(stream, transformer) {
|
|
4
|
+
return new Response(AssistantStream.toByteStream(stream, transformer), {
|
|
5
|
+
headers: transformer.headers ?? {}
|
|
6
|
+
});
|
|
7
|
+
},
|
|
8
|
+
fromResponse(response, transformer) {
|
|
9
|
+
return AssistantStream.fromByteStream(response.body, transformer);
|
|
10
|
+
},
|
|
11
|
+
toByteStream(stream, transformer) {
|
|
12
|
+
return stream.pipeThrough(transformer);
|
|
13
|
+
},
|
|
14
|
+
fromByteStream(readable, transformer) {
|
|
15
|
+
return readable.pipeThrough(transformer);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// src/core/utils/stream/merge.ts
|
|
20
|
+
var promiseWithResolvers = () => {
|
|
21
|
+
let resolve;
|
|
22
|
+
let reject;
|
|
23
|
+
const promise = new Promise((res, rej) => {
|
|
24
|
+
resolve = res;
|
|
25
|
+
reject = rej;
|
|
26
|
+
});
|
|
27
|
+
return { promise, resolve, reject };
|
|
28
|
+
};
|
|
29
|
+
var createMergeStream = () => {
|
|
30
|
+
const list = [];
|
|
31
|
+
let sealed = false;
|
|
32
|
+
let controller;
|
|
33
|
+
let currentPull;
|
|
34
|
+
const handlePull = (item) => {
|
|
35
|
+
if (!item.promise) {
|
|
36
|
+
item.promise = item.reader.read().then(({ done, value }) => {
|
|
37
|
+
item.promise = void 0;
|
|
38
|
+
if (done) {
|
|
39
|
+
list.splice(list.indexOf(item), 1);
|
|
40
|
+
if (sealed && list.length === 0) {
|
|
41
|
+
controller.close();
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
controller.enqueue(value);
|
|
45
|
+
}
|
|
46
|
+
currentPull?.resolve();
|
|
47
|
+
currentPull = void 0;
|
|
48
|
+
}).catch((e) => {
|
|
49
|
+
console.error(e);
|
|
50
|
+
list.forEach((item2) => {
|
|
51
|
+
item2.reader.cancel();
|
|
52
|
+
});
|
|
53
|
+
list.length = 0;
|
|
54
|
+
controller.error(e);
|
|
55
|
+
currentPull?.reject(e);
|
|
56
|
+
currentPull = void 0;
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const readable = new ReadableStream({
|
|
61
|
+
start(c) {
|
|
62
|
+
controller = c;
|
|
63
|
+
},
|
|
64
|
+
pull() {
|
|
65
|
+
currentPull = promiseWithResolvers();
|
|
66
|
+
list.forEach((item) => {
|
|
67
|
+
handlePull(item);
|
|
68
|
+
});
|
|
69
|
+
return currentPull.promise;
|
|
70
|
+
},
|
|
71
|
+
cancel() {
|
|
72
|
+
list.forEach((item) => {
|
|
73
|
+
item.reader.cancel();
|
|
74
|
+
});
|
|
75
|
+
list.length = 0;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
readable,
|
|
80
|
+
isSealed() {
|
|
81
|
+
return sealed;
|
|
82
|
+
},
|
|
83
|
+
seal() {
|
|
84
|
+
sealed = true;
|
|
85
|
+
if (list.length === 0) controller.close();
|
|
86
|
+
},
|
|
87
|
+
addStream(stream) {
|
|
88
|
+
if (sealed)
|
|
89
|
+
throw new Error(
|
|
90
|
+
"Cannot add streams after the run callback has settled."
|
|
91
|
+
);
|
|
92
|
+
const item = { reader: stream.getReader() };
|
|
93
|
+
list.push(item);
|
|
94
|
+
handlePull(item);
|
|
95
|
+
},
|
|
96
|
+
enqueue(chunk) {
|
|
97
|
+
this.addStream(
|
|
98
|
+
new ReadableStream({
|
|
99
|
+
start(c) {
|
|
100
|
+
c.enqueue(chunk);
|
|
101
|
+
c.close();
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/core/modules/text.ts
|
|
110
|
+
var TextStreamControllerImpl = class {
|
|
111
|
+
_controller;
|
|
112
|
+
_isClosed = false;
|
|
113
|
+
constructor(controller) {
|
|
114
|
+
this._controller = controller;
|
|
115
|
+
}
|
|
116
|
+
append(textDelta) {
|
|
117
|
+
this._controller.enqueue({
|
|
118
|
+
type: "text-delta",
|
|
119
|
+
path: [],
|
|
120
|
+
textDelta
|
|
121
|
+
});
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
close() {
|
|
125
|
+
if (this._isClosed) return;
|
|
126
|
+
this._isClosed = true;
|
|
127
|
+
this._controller.enqueue({
|
|
128
|
+
type: "part-finish",
|
|
129
|
+
path: []
|
|
130
|
+
});
|
|
131
|
+
this._controller.close();
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
var createTextStream = (readable) => {
|
|
135
|
+
return new ReadableStream({
|
|
136
|
+
start(c) {
|
|
137
|
+
return readable.start?.(new TextStreamControllerImpl(c));
|
|
138
|
+
},
|
|
139
|
+
pull(c) {
|
|
140
|
+
return readable.pull?.(new TextStreamControllerImpl(c));
|
|
141
|
+
},
|
|
142
|
+
cancel(c) {
|
|
143
|
+
return readable.cancel?.(c);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
};
|
|
147
|
+
var createTextStreamController = () => {
|
|
148
|
+
let controller;
|
|
149
|
+
const stream = createTextStream({
|
|
150
|
+
start(c) {
|
|
151
|
+
controller = c;
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
return [stream, controller];
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// src/core/modules/tool-call.ts
|
|
158
|
+
var ToolCallStreamControllerImpl = class {
|
|
159
|
+
constructor(_controller) {
|
|
160
|
+
this._controller = _controller;
|
|
161
|
+
const stream = createTextStream({
|
|
162
|
+
start: (c) => {
|
|
163
|
+
this._argsTextController = c;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
stream.pipeTo(
|
|
167
|
+
new WritableStream({
|
|
168
|
+
write: (chunk) => {
|
|
169
|
+
switch (chunk.type) {
|
|
170
|
+
case "text-delta":
|
|
171
|
+
this._controller.enqueue(chunk);
|
|
172
|
+
break;
|
|
173
|
+
case "part-finish":
|
|
174
|
+
this._controller.enqueue({
|
|
175
|
+
type: "tool-call-args-text-finish",
|
|
176
|
+
path: []
|
|
177
|
+
});
|
|
178
|
+
break;
|
|
179
|
+
default:
|
|
180
|
+
throw new Error(`Unexpected chunk type: ${chunk.type}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
})
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
_isClosed = false;
|
|
187
|
+
get argsText() {
|
|
188
|
+
return this._argsTextController;
|
|
189
|
+
}
|
|
190
|
+
_argsTextController;
|
|
191
|
+
setResult(result, isError) {
|
|
192
|
+
this._controller.enqueue({
|
|
193
|
+
type: "result",
|
|
194
|
+
path: [],
|
|
195
|
+
result,
|
|
196
|
+
isError: isError ?? false
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
close() {
|
|
200
|
+
if (this._isClosed) return;
|
|
201
|
+
this._isClosed = true;
|
|
202
|
+
this._argsTextController.close();
|
|
203
|
+
this._controller.enqueue({
|
|
204
|
+
type: "part-finish",
|
|
205
|
+
path: []
|
|
206
|
+
});
|
|
207
|
+
this._controller.close();
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var createToolCallStream = (readable) => {
|
|
211
|
+
return new ReadableStream({
|
|
212
|
+
start(c) {
|
|
213
|
+
return readable.start?.(new ToolCallStreamControllerImpl(c));
|
|
214
|
+
},
|
|
215
|
+
pull(c) {
|
|
216
|
+
return readable.pull?.(new ToolCallStreamControllerImpl(c));
|
|
217
|
+
},
|
|
218
|
+
cancel(c) {
|
|
219
|
+
return readable.cancel?.(c);
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
var createToolCallStreamController = () => {
|
|
224
|
+
let controller;
|
|
225
|
+
const stream = createToolCallStream({
|
|
226
|
+
start(c) {
|
|
227
|
+
controller = c;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
return [stream, controller];
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
// src/core/utils/Counter.ts
|
|
234
|
+
var Counter = class {
|
|
235
|
+
value = -1;
|
|
236
|
+
up() {
|
|
237
|
+
return ++this.value;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// src/core/utils/stream/path-utils.ts
|
|
242
|
+
var PathAppendEncoder = class extends TransformStream {
|
|
243
|
+
constructor(idx) {
|
|
244
|
+
super({
|
|
245
|
+
transform(chunk, controller) {
|
|
246
|
+
controller.enqueue({
|
|
247
|
+
...chunk,
|
|
248
|
+
path: [idx, ...chunk.path]
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var PathAppendDecoder = class extends TransformStream {
|
|
255
|
+
constructor(idx) {
|
|
256
|
+
super({
|
|
257
|
+
transform(chunk, controller) {
|
|
258
|
+
const {
|
|
259
|
+
path: [idx2, ...path]
|
|
260
|
+
} = chunk;
|
|
261
|
+
if (idx !== idx2)
|
|
262
|
+
throw new Error(`Path mismatch: expected ${idx}, got ${idx2}`);
|
|
263
|
+
controller.enqueue({
|
|
264
|
+
...chunk,
|
|
265
|
+
path
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
var PathMergeEncoder = class extends TransformStream {
|
|
272
|
+
constructor(counter) {
|
|
273
|
+
const innerCounter = new Counter();
|
|
274
|
+
const mapping = /* @__PURE__ */ new Map();
|
|
275
|
+
super({
|
|
276
|
+
transform(chunk, controller) {
|
|
277
|
+
if (chunk.type === "part-start" && chunk.path.length === 0) {
|
|
278
|
+
mapping.set(innerCounter.up(), counter.up());
|
|
279
|
+
}
|
|
280
|
+
const [idx, ...path] = chunk.path;
|
|
281
|
+
if (idx === void 0) {
|
|
282
|
+
controller.enqueue(chunk);
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const mappedIdx = mapping.get(idx);
|
|
286
|
+
if (mappedIdx === void 0) throw new Error("Path not found");
|
|
287
|
+
controller.enqueue({
|
|
288
|
+
...chunk,
|
|
289
|
+
path: [mappedIdx, ...path]
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/core/utils/generateId.tsx
|
|
297
|
+
import { customAlphabet } from "nanoid/non-secure";
|
|
298
|
+
var generateId = customAlphabet(
|
|
299
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
300
|
+
7
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
// src/core/modules/assistant-stream.ts
|
|
304
|
+
var AssistantStreamControllerImpl = class {
|
|
305
|
+
_merger = createMergeStream();
|
|
306
|
+
_append;
|
|
307
|
+
_contentCounter = new Counter();
|
|
308
|
+
get __internal_isClosed() {
|
|
309
|
+
return this._merger.isSealed();
|
|
310
|
+
}
|
|
311
|
+
__internal_getReadable() {
|
|
312
|
+
return this._merger.readable;
|
|
313
|
+
}
|
|
314
|
+
_closeSubscriber;
|
|
315
|
+
__internal_subscribeToClose(callback) {
|
|
316
|
+
this._closeSubscriber = callback;
|
|
317
|
+
}
|
|
318
|
+
_addPart(part, stream) {
|
|
319
|
+
this.enqueue({
|
|
320
|
+
type: "part-start",
|
|
321
|
+
part,
|
|
322
|
+
path: []
|
|
323
|
+
});
|
|
324
|
+
this._merger.addStream(
|
|
325
|
+
stream.pipeThrough(new PathAppendEncoder(this._contentCounter.value))
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
merge(stream) {
|
|
329
|
+
this._merger.addStream(
|
|
330
|
+
stream.pipeThrough(new PathMergeEncoder(this._contentCounter))
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
appendText(textDelta) {
|
|
334
|
+
if (this._append?.kind !== "text") {
|
|
335
|
+
if (this._append) {
|
|
336
|
+
this._append.controller.close();
|
|
337
|
+
}
|
|
338
|
+
this._append = {
|
|
339
|
+
kind: "text",
|
|
340
|
+
controller: this.addTextPart()
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
this._append.controller.append(textDelta);
|
|
344
|
+
}
|
|
345
|
+
appendReasoning(textDelta) {
|
|
346
|
+
if (this._append?.kind !== "reasoning") {
|
|
347
|
+
if (this._append) {
|
|
348
|
+
this._append.controller.close();
|
|
349
|
+
}
|
|
350
|
+
this._append = {
|
|
351
|
+
kind: "reasoning",
|
|
352
|
+
controller: this.addReasoningPart()
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
this._append.controller.append(textDelta);
|
|
356
|
+
}
|
|
357
|
+
addTextPart() {
|
|
358
|
+
const [stream, controller] = createTextStreamController();
|
|
359
|
+
this._addPart({ type: "text" }, stream);
|
|
360
|
+
return controller;
|
|
361
|
+
}
|
|
362
|
+
addReasoningPart() {
|
|
363
|
+
const [stream, controller] = createTextStreamController();
|
|
364
|
+
this._addPart({ type: "reasoning" }, stream);
|
|
365
|
+
return controller;
|
|
366
|
+
}
|
|
367
|
+
addToolCallPart(options) {
|
|
368
|
+
const opt = typeof options === "string" ? { toolName: options } : options;
|
|
369
|
+
const toolName = opt.toolName;
|
|
370
|
+
const toolCallId = opt.toolCallId ?? generateId();
|
|
371
|
+
const [stream, controller] = createToolCallStreamController();
|
|
372
|
+
this._addPart({ type: "tool-call", toolName, toolCallId }, stream);
|
|
373
|
+
if (opt.args !== void 0) {
|
|
374
|
+
controller.argsText.append(JSON.stringify(opt.args));
|
|
375
|
+
controller.argsText.close();
|
|
376
|
+
}
|
|
377
|
+
if (opt.result !== void 0) {
|
|
378
|
+
controller.setResult(opt.result, opt.isError);
|
|
379
|
+
}
|
|
380
|
+
return controller;
|
|
381
|
+
}
|
|
382
|
+
appendSource(options) {
|
|
383
|
+
this._addPart(
|
|
384
|
+
options,
|
|
385
|
+
new ReadableStream({
|
|
386
|
+
start(controller) {
|
|
387
|
+
controller.enqueue({
|
|
388
|
+
type: "part-finish",
|
|
389
|
+
path: []
|
|
390
|
+
});
|
|
391
|
+
controller.close();
|
|
392
|
+
}
|
|
393
|
+
})
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
appendFile(options) {
|
|
397
|
+
this._addPart(
|
|
398
|
+
options,
|
|
399
|
+
new ReadableStream({
|
|
400
|
+
start(controller) {
|
|
401
|
+
controller.enqueue({
|
|
402
|
+
type: "part-finish",
|
|
403
|
+
path: []
|
|
404
|
+
});
|
|
405
|
+
controller.close();
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
enqueue(chunk) {
|
|
411
|
+
this._merger.enqueue(chunk);
|
|
412
|
+
if (chunk.type === "part-start" && chunk.path.length === 0) {
|
|
413
|
+
this._contentCounter.up();
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
close() {
|
|
417
|
+
this._merger.seal();
|
|
418
|
+
this._append?.controller?.close();
|
|
419
|
+
this._closeSubscriber?.();
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
function createAssistantStream(callback) {
|
|
423
|
+
const controller = new AssistantStreamControllerImpl();
|
|
424
|
+
let promiseOrVoid;
|
|
425
|
+
try {
|
|
426
|
+
promiseOrVoid = callback(controller);
|
|
427
|
+
} catch (e) {
|
|
428
|
+
if (!controller.__internal_isClosed) {
|
|
429
|
+
controller.enqueue({
|
|
430
|
+
type: "error",
|
|
431
|
+
path: [],
|
|
432
|
+
error: String(e)
|
|
433
|
+
});
|
|
434
|
+
controller.close();
|
|
435
|
+
}
|
|
436
|
+
throw e;
|
|
437
|
+
}
|
|
438
|
+
if (promiseOrVoid instanceof Promise) {
|
|
439
|
+
const runTask = async () => {
|
|
440
|
+
try {
|
|
441
|
+
await promiseOrVoid;
|
|
442
|
+
} catch (e) {
|
|
443
|
+
if (!controller.__internal_isClosed) {
|
|
444
|
+
controller.enqueue({
|
|
445
|
+
type: "error",
|
|
446
|
+
path: [],
|
|
447
|
+
error: String(e)
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
throw e;
|
|
451
|
+
} finally {
|
|
452
|
+
if (!controller.__internal_isClosed) {
|
|
453
|
+
controller.close();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
runTask();
|
|
458
|
+
} else {
|
|
459
|
+
if (!controller.__internal_isClosed) {
|
|
460
|
+
controller.close();
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return controller.__internal_getReadable();
|
|
464
|
+
}
|
|
465
|
+
var promiseWithResolvers2 = function() {
|
|
466
|
+
let resolve;
|
|
467
|
+
let reject;
|
|
468
|
+
const promise = new Promise((res, rej) => {
|
|
469
|
+
resolve = res;
|
|
470
|
+
reject = rej;
|
|
471
|
+
});
|
|
472
|
+
if (!resolve || !reject) throw new Error("Failed to create promise");
|
|
473
|
+
return { promise, resolve, reject };
|
|
474
|
+
};
|
|
475
|
+
function createAssistantStreamController() {
|
|
476
|
+
const { resolve, promise } = promiseWithResolvers2();
|
|
477
|
+
let controller;
|
|
478
|
+
const stream = createAssistantStream((c) => {
|
|
479
|
+
controller = c;
|
|
480
|
+
controller.__internal_subscribeToClose(
|
|
481
|
+
resolve
|
|
482
|
+
);
|
|
483
|
+
return promise;
|
|
484
|
+
});
|
|
485
|
+
return [stream, controller];
|
|
486
|
+
}
|
|
487
|
+
function createAssistantStreamResponse(callback) {
|
|
488
|
+
return AssistantStream.toResponse(
|
|
489
|
+
createAssistantStream(callback),
|
|
490
|
+
new DataStreamEncoder()
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// src/core/utils/stream/AssistantTransformStream.ts
|
|
495
|
+
var AssistantTransformStream = class extends TransformStream {
|
|
496
|
+
constructor(transformer, writableStrategy, readableStrategy) {
|
|
497
|
+
const [stream, runController] = createAssistantStreamController();
|
|
498
|
+
let runPipeTask;
|
|
499
|
+
super(
|
|
500
|
+
{
|
|
501
|
+
start(controller) {
|
|
502
|
+
runPipeTask = stream.pipeTo(
|
|
503
|
+
new WritableStream({
|
|
504
|
+
write(chunk) {
|
|
505
|
+
controller.enqueue(chunk);
|
|
506
|
+
},
|
|
507
|
+
abort(reason) {
|
|
508
|
+
controller.error(reason);
|
|
509
|
+
},
|
|
510
|
+
close() {
|
|
511
|
+
controller.terminate();
|
|
512
|
+
}
|
|
513
|
+
})
|
|
514
|
+
).catch((error) => {
|
|
515
|
+
controller.error(error);
|
|
516
|
+
});
|
|
517
|
+
return transformer.start?.(runController);
|
|
518
|
+
},
|
|
519
|
+
transform(chunk) {
|
|
520
|
+
return transformer.transform?.(chunk, runController);
|
|
521
|
+
},
|
|
522
|
+
async flush() {
|
|
523
|
+
await transformer.flush?.(runController);
|
|
524
|
+
runController.close();
|
|
525
|
+
await runPipeTask;
|
|
526
|
+
}
|
|
527
|
+
},
|
|
528
|
+
writableStrategy,
|
|
529
|
+
readableStrategy
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
// src/core/utils/stream/PipeableTransformStream.ts
|
|
535
|
+
var PipeableTransformStream = class extends TransformStream {
|
|
536
|
+
constructor(transform) {
|
|
537
|
+
super();
|
|
538
|
+
const readable = transform(super.readable);
|
|
539
|
+
Object.defineProperty(this, "readable", {
|
|
540
|
+
value: readable,
|
|
541
|
+
writable: false
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
|
|
546
|
+
// src/core/utils/stream/LineDecoderStream.ts
|
|
547
|
+
var LineDecoderStream = class extends TransformStream {
|
|
548
|
+
buffer = "";
|
|
549
|
+
constructor() {
|
|
550
|
+
super({
|
|
551
|
+
transform: (chunk, controller) => {
|
|
552
|
+
this.buffer += chunk;
|
|
553
|
+
const lines = this.buffer.split("\n");
|
|
554
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
555
|
+
controller.enqueue(lines[i]);
|
|
556
|
+
}
|
|
557
|
+
this.buffer = lines[lines.length - 1] || "";
|
|
558
|
+
},
|
|
559
|
+
flush: (controller) => {
|
|
560
|
+
if (this.buffer) {
|
|
561
|
+
controller.enqueue(this.buffer);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// src/core/serialization/data-stream/serialization.ts
|
|
569
|
+
var DataStreamChunkEncoder = class extends TransformStream {
|
|
570
|
+
constructor() {
|
|
571
|
+
super({
|
|
572
|
+
transform: (chunk, controller) => {
|
|
573
|
+
controller.enqueue(`${chunk.type}:${JSON.stringify(chunk.value)}
|
|
574
|
+
`);
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
var DataStreamChunkDecoder = class extends TransformStream {
|
|
580
|
+
constructor() {
|
|
581
|
+
super({
|
|
582
|
+
transform: (chunk, controller) => {
|
|
583
|
+
const index = chunk.indexOf(":");
|
|
584
|
+
if (index === -1) throw new Error("Invalid stream part");
|
|
585
|
+
controller.enqueue({
|
|
586
|
+
type: chunk.slice(0, index),
|
|
587
|
+
value: JSON.parse(chunk.slice(index + 1))
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
// src/core/utils/stream/AssistantMetaTransformStream.ts
|
|
595
|
+
var AssistantMetaTransformStream = class extends TransformStream {
|
|
596
|
+
constructor() {
|
|
597
|
+
const parts = [];
|
|
598
|
+
super({
|
|
599
|
+
transform(chunk, controller) {
|
|
600
|
+
if (chunk.type === "part-start") {
|
|
601
|
+
if (chunk.path.length !== 0) {
|
|
602
|
+
controller.error(new Error("Nested parts are not supported"));
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
parts.push(chunk.part);
|
|
606
|
+
controller.enqueue(chunk);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
if (chunk.type === "text-delta" || chunk.type === "result" || chunk.type === "part-finish" || chunk.type === "tool-call-args-text-finish") {
|
|
610
|
+
if (chunk.path.length !== 1) {
|
|
611
|
+
controller.error(
|
|
612
|
+
new Error(`${chunk.type} chunks must have a path of length 1`)
|
|
613
|
+
);
|
|
614
|
+
return;
|
|
615
|
+
}
|
|
616
|
+
const idx = chunk.path[0];
|
|
617
|
+
if (idx < 0 || idx >= parts.length) {
|
|
618
|
+
controller.error(new Error(`Invalid path index: ${idx}`));
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
const part = parts[idx];
|
|
622
|
+
controller.enqueue({
|
|
623
|
+
...chunk,
|
|
624
|
+
meta: part
|
|
625
|
+
// TODO
|
|
626
|
+
});
|
|
627
|
+
return;
|
|
628
|
+
}
|
|
629
|
+
controller.enqueue(chunk);
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
// src/core/serialization/data-stream/DataStream.ts
|
|
636
|
+
var DataStreamEncoder = class extends PipeableTransformStream {
|
|
637
|
+
headers = new Headers({
|
|
638
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
639
|
+
"x-vercel-ai-data-stream": "v1"
|
|
640
|
+
});
|
|
641
|
+
constructor() {
|
|
642
|
+
super((readable) => {
|
|
643
|
+
const transform = new TransformStream({
|
|
644
|
+
transform(chunk, controller) {
|
|
645
|
+
const type = chunk.type;
|
|
646
|
+
switch (type) {
|
|
647
|
+
case "part-start": {
|
|
648
|
+
const part = chunk.part;
|
|
649
|
+
if (part.type === "tool-call") {
|
|
650
|
+
const { type: type2, ...value } = part;
|
|
651
|
+
controller.enqueue({
|
|
652
|
+
type: "b" /* StartToolCall */,
|
|
653
|
+
value
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
if (part.type === "source") {
|
|
657
|
+
const { type: type2, ...value } = part;
|
|
658
|
+
controller.enqueue({
|
|
659
|
+
type: "h" /* Source */,
|
|
660
|
+
value
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
case "text-delta": {
|
|
666
|
+
const part = chunk.meta;
|
|
667
|
+
switch (part.type) {
|
|
668
|
+
case "text": {
|
|
669
|
+
controller.enqueue({
|
|
670
|
+
type: "0" /* TextDelta */,
|
|
671
|
+
value: chunk.textDelta
|
|
672
|
+
});
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
case "reasoning": {
|
|
676
|
+
controller.enqueue({
|
|
677
|
+
type: "g" /* ReasoningDelta */,
|
|
678
|
+
value: chunk.textDelta
|
|
679
|
+
});
|
|
680
|
+
break;
|
|
681
|
+
}
|
|
682
|
+
case "tool-call": {
|
|
683
|
+
controller.enqueue({
|
|
684
|
+
type: "c" /* ToolCallDelta */,
|
|
685
|
+
value: {
|
|
686
|
+
toolCallId: part.toolCallId,
|
|
687
|
+
argsTextDelta: chunk.textDelta
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
default:
|
|
693
|
+
throw new Error(
|
|
694
|
+
`Unsupported part type for text-delta: ${part.type}`
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
break;
|
|
698
|
+
}
|
|
699
|
+
case "result": {
|
|
700
|
+
const part = chunk.meta;
|
|
701
|
+
if (part.type !== "tool-call") {
|
|
702
|
+
throw new Error(
|
|
703
|
+
`Result chunk on non-tool-call part not supported: ${part.type}`
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
controller.enqueue({
|
|
707
|
+
type: "a" /* ToolCallResult */,
|
|
708
|
+
value: {
|
|
709
|
+
toolCallId: part.toolCallId,
|
|
710
|
+
result: chunk.result
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
break;
|
|
714
|
+
}
|
|
715
|
+
case "step-start": {
|
|
716
|
+
const { type: type2, ...value } = chunk;
|
|
717
|
+
controller.enqueue({
|
|
718
|
+
type: "f" /* StartStep */,
|
|
719
|
+
value
|
|
720
|
+
});
|
|
721
|
+
break;
|
|
722
|
+
}
|
|
723
|
+
case "step-finish": {
|
|
724
|
+
const { type: type2, ...value } = chunk;
|
|
725
|
+
controller.enqueue({
|
|
726
|
+
type: "e" /* FinishStep */,
|
|
727
|
+
value
|
|
728
|
+
});
|
|
729
|
+
break;
|
|
730
|
+
}
|
|
731
|
+
case "message-finish": {
|
|
732
|
+
const { type: type2, ...value } = chunk;
|
|
733
|
+
controller.enqueue({
|
|
734
|
+
type: "d" /* FinishMessage */,
|
|
735
|
+
value
|
|
736
|
+
});
|
|
737
|
+
break;
|
|
738
|
+
}
|
|
739
|
+
case "error": {
|
|
740
|
+
controller.enqueue({
|
|
741
|
+
type: "3" /* Error */,
|
|
742
|
+
value: chunk.error
|
|
743
|
+
});
|
|
744
|
+
break;
|
|
745
|
+
}
|
|
746
|
+
case "annotations": {
|
|
747
|
+
controller.enqueue({
|
|
748
|
+
type: "8" /* Annotation */,
|
|
749
|
+
value: chunk.annotations
|
|
750
|
+
});
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
case "data": {
|
|
754
|
+
controller.enqueue({
|
|
755
|
+
type: "2" /* Data */,
|
|
756
|
+
value: chunk.data
|
|
757
|
+
});
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
// TODO ignore for now
|
|
761
|
+
// in the future, we should create a handler that waits for text parts to finish before continuing
|
|
762
|
+
case "tool-call-args-text-finish":
|
|
763
|
+
case "part-finish":
|
|
764
|
+
break;
|
|
765
|
+
default: {
|
|
766
|
+
const exhaustiveCheck = type;
|
|
767
|
+
throw new Error(`Unsupported chunk type: ${exhaustiveCheck}`);
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
});
|
|
772
|
+
return readable.pipeThrough(new AssistantMetaTransformStream()).pipeThrough(transform).pipeThrough(new DataStreamChunkEncoder()).pipeThrough(new TextEncoderStream());
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
var TOOL_CALL_ARGS_CLOSING_CHUNKS = [
|
|
777
|
+
"b" /* StartToolCall */,
|
|
778
|
+
"9" /* ToolCall */,
|
|
779
|
+
"0" /* TextDelta */,
|
|
780
|
+
"g" /* ReasoningDelta */,
|
|
781
|
+
"h" /* Source */,
|
|
782
|
+
"3" /* Error */,
|
|
783
|
+
"e" /* FinishStep */,
|
|
784
|
+
"d" /* FinishMessage */
|
|
785
|
+
];
|
|
786
|
+
var DataStreamDecoder = class extends PipeableTransformStream {
|
|
787
|
+
constructor() {
|
|
788
|
+
super((readable) => {
|
|
789
|
+
const toolCallControllers = /* @__PURE__ */ new Map();
|
|
790
|
+
let activeToolCallArgsText;
|
|
791
|
+
const transform = new AssistantTransformStream({
|
|
792
|
+
transform(chunk, controller) {
|
|
793
|
+
const { type, value } = chunk;
|
|
794
|
+
if (TOOL_CALL_ARGS_CLOSING_CHUNKS.includes(type)) {
|
|
795
|
+
activeToolCallArgsText?.close();
|
|
796
|
+
activeToolCallArgsText = void 0;
|
|
797
|
+
}
|
|
798
|
+
switch (type) {
|
|
799
|
+
case "g" /* ReasoningDelta */:
|
|
800
|
+
controller.appendReasoning(value);
|
|
801
|
+
break;
|
|
802
|
+
case "0" /* TextDelta */:
|
|
803
|
+
controller.appendText(value);
|
|
804
|
+
break;
|
|
805
|
+
case "b" /* StartToolCall */: {
|
|
806
|
+
const { toolCallId, toolName } = value;
|
|
807
|
+
const toolCallController = controller.addToolCallPart({
|
|
808
|
+
toolCallId,
|
|
809
|
+
toolName
|
|
810
|
+
});
|
|
811
|
+
toolCallControllers.set(toolCallId, toolCallController);
|
|
812
|
+
activeToolCallArgsText = toolCallController.argsText;
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
case "c" /* ToolCallDelta */: {
|
|
816
|
+
const { toolCallId, argsTextDelta } = value;
|
|
817
|
+
const toolCallController = toolCallControllers.get(toolCallId);
|
|
818
|
+
if (!toolCallController)
|
|
819
|
+
throw new Error(
|
|
820
|
+
"Encountered tool call with unknown id: " + toolCallId
|
|
821
|
+
);
|
|
822
|
+
toolCallController.argsText.append(argsTextDelta);
|
|
823
|
+
break;
|
|
824
|
+
}
|
|
825
|
+
case "a" /* ToolCallResult */: {
|
|
826
|
+
const { toolCallId, result } = value;
|
|
827
|
+
const toolCallController = toolCallControllers.get(toolCallId);
|
|
828
|
+
if (!toolCallController)
|
|
829
|
+
throw new Error(
|
|
830
|
+
"Encountered tool call result with unknown id: " + toolCallId
|
|
831
|
+
);
|
|
832
|
+
toolCallController.setResult(result);
|
|
833
|
+
break;
|
|
834
|
+
}
|
|
835
|
+
case "9" /* ToolCall */: {
|
|
836
|
+
const { toolCallId, toolName, args } = value;
|
|
837
|
+
const toolCallController = controller.addToolCallPart({
|
|
838
|
+
toolCallId,
|
|
839
|
+
toolName
|
|
840
|
+
});
|
|
841
|
+
toolCallControllers.set(toolCallId, toolCallController);
|
|
842
|
+
toolCallController.argsText.append(JSON.stringify(args));
|
|
843
|
+
toolCallController.argsText.close();
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
case "d" /* FinishMessage */:
|
|
847
|
+
controller.enqueue({
|
|
848
|
+
type: "message-finish",
|
|
849
|
+
path: [],
|
|
850
|
+
...value
|
|
851
|
+
});
|
|
852
|
+
break;
|
|
853
|
+
case "f" /* StartStep */:
|
|
854
|
+
controller.enqueue({
|
|
855
|
+
type: "step-start",
|
|
856
|
+
path: [],
|
|
857
|
+
...value
|
|
858
|
+
});
|
|
859
|
+
break;
|
|
860
|
+
case "e" /* FinishStep */:
|
|
861
|
+
controller.enqueue({
|
|
862
|
+
type: "step-finish",
|
|
863
|
+
path: [],
|
|
864
|
+
...value
|
|
865
|
+
});
|
|
866
|
+
break;
|
|
867
|
+
case "2" /* Data */:
|
|
868
|
+
controller.enqueue({
|
|
869
|
+
type: "data",
|
|
870
|
+
path: [],
|
|
871
|
+
data: value
|
|
872
|
+
});
|
|
873
|
+
break;
|
|
874
|
+
case "8" /* Annotation */:
|
|
875
|
+
controller.enqueue({
|
|
876
|
+
type: "annotations",
|
|
877
|
+
path: [],
|
|
878
|
+
annotations: value
|
|
879
|
+
});
|
|
880
|
+
break;
|
|
881
|
+
case "h" /* Source */:
|
|
882
|
+
controller.appendSource({
|
|
883
|
+
type: "source",
|
|
884
|
+
...value
|
|
885
|
+
});
|
|
886
|
+
break;
|
|
887
|
+
case "3" /* Error */:
|
|
888
|
+
controller.enqueue({
|
|
889
|
+
type: "error",
|
|
890
|
+
path: [],
|
|
891
|
+
error: value
|
|
892
|
+
});
|
|
893
|
+
break;
|
|
894
|
+
case "k" /* File */:
|
|
895
|
+
controller.appendFile({
|
|
896
|
+
type: "file",
|
|
897
|
+
...value
|
|
898
|
+
});
|
|
899
|
+
break;
|
|
900
|
+
case "j" /* ReasoningSignature */:
|
|
901
|
+
case "i" /* RedactedReasoning */:
|
|
902
|
+
break;
|
|
903
|
+
default: {
|
|
904
|
+
const exhaustiveCheck = type;
|
|
905
|
+
throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
},
|
|
909
|
+
flush() {
|
|
910
|
+
activeToolCallArgsText?.close();
|
|
911
|
+
activeToolCallArgsText = void 0;
|
|
912
|
+
toolCallControllers.forEach((controller) => controller.close());
|
|
913
|
+
toolCallControllers.clear();
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new LineDecoderStream()).pipeThrough(new DataStreamChunkDecoder()).pipeThrough(transform);
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
};
|
|
920
|
+
|
|
921
|
+
export {
|
|
922
|
+
AssistantStream,
|
|
923
|
+
PipeableTransformStream,
|
|
924
|
+
AssistantMetaTransformStream,
|
|
925
|
+
DataStreamEncoder,
|
|
926
|
+
DataStreamDecoder,
|
|
927
|
+
generateId,
|
|
928
|
+
createAssistantStream,
|
|
929
|
+
createAssistantStreamResponse,
|
|
930
|
+
AssistantTransformStream
|
|
931
|
+
};
|
|
932
|
+
//# sourceMappingURL=chunk-DISBVUTK.mjs.map
|