assistant-stream 0.0.0 → 0.0.2
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/AssistantStream-CYbWmRWJ.d.mts +27 -0
- package/dist/AssistantStream-CYbWmRWJ.d.ts +27 -0
- package/dist/ai-sdk.d.mts +7 -0
- package/dist/ai-sdk.d.ts +7 -0
- package/dist/ai-sdk.js +176 -0
- package/dist/ai-sdk.js.map +1 -0
- package/dist/ai-sdk.mjs +122 -0
- package/dist/ai-sdk.mjs.map +1 -0
- package/dist/chunk-7CCRZC5D.mjs +969 -0
- package/dist/chunk-7CCRZC5D.mjs.map +1 -0
- package/dist/index.d.mts +99 -0
- package/dist/index.d.ts +99 -0
- package/dist/index.js +1012 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +20 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +59 -3
|
@@ -0,0 +1,969 @@
|
|
|
1
|
+
// src/core/AssistantStream.ts
|
|
2
|
+
var AssistantStream = class _AssistantStream {
|
|
3
|
+
constructor(readable) {
|
|
4
|
+
this.readable = readable;
|
|
5
|
+
this.readable = readable;
|
|
6
|
+
}
|
|
7
|
+
toResponse(transformer) {
|
|
8
|
+
return new Response(this.toByteStream(transformer));
|
|
9
|
+
}
|
|
10
|
+
static fromResponse(response, transformer) {
|
|
11
|
+
return _AssistantStream.fromByteStream(response.body, transformer);
|
|
12
|
+
}
|
|
13
|
+
toByteStream(transformer) {
|
|
14
|
+
return this.readable.pipeThrough(transformer);
|
|
15
|
+
}
|
|
16
|
+
static fromByteStream(readable, transformer) {
|
|
17
|
+
return new _AssistantStream(readable.pipeThrough(transformer));
|
|
18
|
+
}
|
|
19
|
+
tee() {
|
|
20
|
+
const [readable1, readable2] = this.readable.tee();
|
|
21
|
+
return [new _AssistantStream(readable1), new _AssistantStream(readable2)];
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/core/utils/generateId.tsx
|
|
26
|
+
import { customAlphabet } from "nanoid/non-secure";
|
|
27
|
+
var generateId = customAlphabet(
|
|
28
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
29
|
+
7
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// src/core/modules/text.ts
|
|
33
|
+
var TextStreamControllerImpl = class {
|
|
34
|
+
_controller;
|
|
35
|
+
constructor(controller) {
|
|
36
|
+
this._controller = controller;
|
|
37
|
+
}
|
|
38
|
+
append(textDelta) {
|
|
39
|
+
this._controller.enqueue({
|
|
40
|
+
type: "text-delta",
|
|
41
|
+
textDelta
|
|
42
|
+
});
|
|
43
|
+
return this;
|
|
44
|
+
}
|
|
45
|
+
close() {
|
|
46
|
+
this._controller.close();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
var createTextStream = (readable) => {
|
|
50
|
+
return new AssistantStream(
|
|
51
|
+
new ReadableStream({
|
|
52
|
+
start(c) {
|
|
53
|
+
return readable.start?.(new TextStreamControllerImpl(c));
|
|
54
|
+
},
|
|
55
|
+
pull(c) {
|
|
56
|
+
return readable.pull?.(new TextStreamControllerImpl(c));
|
|
57
|
+
},
|
|
58
|
+
cancel(c) {
|
|
59
|
+
return readable.cancel?.(c);
|
|
60
|
+
}
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/core/modules/tool-call.ts
|
|
66
|
+
var ToolCallStreamControllerImpl = class {
|
|
67
|
+
constructor(_controller, _options) {
|
|
68
|
+
this._controller = _controller;
|
|
69
|
+
this._options = _options;
|
|
70
|
+
this._controller.enqueue({
|
|
71
|
+
type: "tool-call-begin",
|
|
72
|
+
toolCallId: this._options.toolCallId,
|
|
73
|
+
toolName: this._options.toolName
|
|
74
|
+
});
|
|
75
|
+
const stream = createTextStream({
|
|
76
|
+
start: (c) => {
|
|
77
|
+
this._argsTextController = c;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
stream.readable.pipeTo(
|
|
81
|
+
new WritableStream({
|
|
82
|
+
write: (chunk) => {
|
|
83
|
+
if (chunk.type !== "text-delta")
|
|
84
|
+
throw new Error("Unexpected chunk type");
|
|
85
|
+
this._controller.enqueue({
|
|
86
|
+
type: "tool-call-delta",
|
|
87
|
+
toolCallId: this._options.toolCallId,
|
|
88
|
+
argsTextDelta: chunk.textDelta
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
get toolCallId() {
|
|
95
|
+
return this._options.toolCallId;
|
|
96
|
+
}
|
|
97
|
+
get toolName() {
|
|
98
|
+
return this._options.toolName;
|
|
99
|
+
}
|
|
100
|
+
get argsText() {
|
|
101
|
+
return this._argsTextController;
|
|
102
|
+
}
|
|
103
|
+
_argsTextController;
|
|
104
|
+
setResult(result) {
|
|
105
|
+
this._controller.enqueue({
|
|
106
|
+
type: "tool-result",
|
|
107
|
+
toolCallId: this._options.toolCallId,
|
|
108
|
+
result
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
close() {
|
|
112
|
+
this._controller.close();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
var createToolCallStream = (readable) => {
|
|
116
|
+
const options = {
|
|
117
|
+
toolCallId: readable.toolCallId,
|
|
118
|
+
toolName: readable.toolName
|
|
119
|
+
};
|
|
120
|
+
return new AssistantStream(
|
|
121
|
+
new ReadableStream({
|
|
122
|
+
start(c) {
|
|
123
|
+
return readable.start?.(new ToolCallStreamControllerImpl(c, options));
|
|
124
|
+
},
|
|
125
|
+
pull(c) {
|
|
126
|
+
return readable.pull?.(new ToolCallStreamControllerImpl(c, options));
|
|
127
|
+
},
|
|
128
|
+
cancel(c) {
|
|
129
|
+
return readable.cancel?.(c);
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/core/modules/runs.ts
|
|
136
|
+
var promiseWithResolvers = () => {
|
|
137
|
+
let resolve;
|
|
138
|
+
let reject;
|
|
139
|
+
const promise = new Promise((res, rej) => {
|
|
140
|
+
resolve = res;
|
|
141
|
+
reject = rej;
|
|
142
|
+
});
|
|
143
|
+
return { promise, resolve, reject };
|
|
144
|
+
};
|
|
145
|
+
var createMergeStream = () => {
|
|
146
|
+
const list = [];
|
|
147
|
+
let sealed = false;
|
|
148
|
+
let controller;
|
|
149
|
+
let currentPull;
|
|
150
|
+
const handlePull = (item) => {
|
|
151
|
+
if (!item.promise) {
|
|
152
|
+
item.promise = item.reader.read().then(({ done, value }) => {
|
|
153
|
+
item.promise = void 0;
|
|
154
|
+
if (done) {
|
|
155
|
+
list.splice(list.indexOf(item), 1);
|
|
156
|
+
if (sealed && list.length === 0) {
|
|
157
|
+
controller.close();
|
|
158
|
+
}
|
|
159
|
+
} else {
|
|
160
|
+
controller.enqueue(value);
|
|
161
|
+
}
|
|
162
|
+
currentPull?.resolve();
|
|
163
|
+
currentPull = void 0;
|
|
164
|
+
}).catch((e) => {
|
|
165
|
+
list.forEach((item2) => {
|
|
166
|
+
item2.reader.cancel();
|
|
167
|
+
});
|
|
168
|
+
list.length = 0;
|
|
169
|
+
controller.error(e);
|
|
170
|
+
currentPull?.reject(e);
|
|
171
|
+
currentPull = void 0;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const readable = new ReadableStream({
|
|
176
|
+
start(c) {
|
|
177
|
+
controller = c;
|
|
178
|
+
},
|
|
179
|
+
async pull() {
|
|
180
|
+
list.map((item) => {
|
|
181
|
+
handlePull(item);
|
|
182
|
+
return item.promise;
|
|
183
|
+
});
|
|
184
|
+
currentPull = promiseWithResolvers();
|
|
185
|
+
return currentPull.promise;
|
|
186
|
+
},
|
|
187
|
+
cancel() {
|
|
188
|
+
list.forEach((item) => {
|
|
189
|
+
item.reader.cancel();
|
|
190
|
+
});
|
|
191
|
+
list.length = 0;
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
stream: readable,
|
|
196
|
+
seal() {
|
|
197
|
+
sealed = true;
|
|
198
|
+
if (list.length === 0) controller.close();
|
|
199
|
+
},
|
|
200
|
+
addStream(stream) {
|
|
201
|
+
if (sealed)
|
|
202
|
+
throw new Error(
|
|
203
|
+
"Cannot add streams after the run callback has settled."
|
|
204
|
+
);
|
|
205
|
+
const item = { reader: stream.readable.getReader() };
|
|
206
|
+
list.push(item);
|
|
207
|
+
if (list.length === 1) {
|
|
208
|
+
handlePull(item);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
var RunControllerImpl = class {
|
|
214
|
+
_merge = createMergeStream();
|
|
215
|
+
_textPartController;
|
|
216
|
+
getReadable() {
|
|
217
|
+
return this._merge.stream;
|
|
218
|
+
}
|
|
219
|
+
close() {
|
|
220
|
+
this._merge.seal();
|
|
221
|
+
this._textPartController?.close();
|
|
222
|
+
}
|
|
223
|
+
appendStep(stream) {
|
|
224
|
+
this._merge.addStream(stream);
|
|
225
|
+
}
|
|
226
|
+
appendText(textDelta) {
|
|
227
|
+
if (!this._textPartController) {
|
|
228
|
+
this._textPartController = this.addTextPart();
|
|
229
|
+
}
|
|
230
|
+
this._textPartController.append(textDelta);
|
|
231
|
+
}
|
|
232
|
+
addTextPart() {
|
|
233
|
+
let controller;
|
|
234
|
+
const textStream = createTextStream({
|
|
235
|
+
start(c) {
|
|
236
|
+
controller = c;
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
this.appendStep(textStream);
|
|
240
|
+
return controller;
|
|
241
|
+
}
|
|
242
|
+
addToolCallPart(options) {
|
|
243
|
+
const opt = typeof options === "string" ? { toolName: options, toolCallId: generateId() } : options;
|
|
244
|
+
let controller;
|
|
245
|
+
const toolCallStream = createToolCallStream({
|
|
246
|
+
toolCallId: opt.toolCallId,
|
|
247
|
+
toolName: opt.toolName,
|
|
248
|
+
start(c) {
|
|
249
|
+
controller = c;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
this.appendStep(toolCallStream);
|
|
253
|
+
return controller;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
function createAssistantRun(callback) {
|
|
257
|
+
const controller = new RunControllerImpl();
|
|
258
|
+
const promiseOrVoid = callback(controller);
|
|
259
|
+
if (promiseOrVoid instanceof Promise) {
|
|
260
|
+
promiseOrVoid.finally(() => controller.close());
|
|
261
|
+
} else {
|
|
262
|
+
controller.close();
|
|
263
|
+
}
|
|
264
|
+
return new AssistantStream(controller.getReadable());
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// src/core/utils/PipeableTransformStream.ts
|
|
268
|
+
var PipeableTransformStream = class extends TransformStream {
|
|
269
|
+
constructor(transform) {
|
|
270
|
+
super();
|
|
271
|
+
const readable = transform(super.readable);
|
|
272
|
+
Object.defineProperty(this, "readable", {
|
|
273
|
+
value: readable,
|
|
274
|
+
writable: false
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// src/core/serialization/DataStream.ts
|
|
280
|
+
var DataStreamEncoder = class {
|
|
281
|
+
_transformStream;
|
|
282
|
+
get writable() {
|
|
283
|
+
return this._transformStream.writable;
|
|
284
|
+
}
|
|
285
|
+
get readable() {
|
|
286
|
+
return this._transformStream.readable;
|
|
287
|
+
}
|
|
288
|
+
constructor() {
|
|
289
|
+
this._transformStream = new PipeableTransformStream((readable) => {
|
|
290
|
+
const transform = new TransformStream({
|
|
291
|
+
transform(chunk, controller) {
|
|
292
|
+
const type = chunk.type;
|
|
293
|
+
switch (type) {
|
|
294
|
+
case "text-delta":
|
|
295
|
+
controller.enqueue("0:" + JSON.stringify(chunk.textDelta) + "\n");
|
|
296
|
+
break;
|
|
297
|
+
case "tool-call-begin":
|
|
298
|
+
controller.enqueue(
|
|
299
|
+
"b:" + JSON.stringify({
|
|
300
|
+
toolCallId: chunk.toolCallId,
|
|
301
|
+
toolName: chunk.toolName
|
|
302
|
+
}) + "\n"
|
|
303
|
+
);
|
|
304
|
+
break;
|
|
305
|
+
case "tool-call-delta":
|
|
306
|
+
controller.enqueue(
|
|
307
|
+
"c:" + JSON.stringify({
|
|
308
|
+
toolCallId: chunk.toolCallId,
|
|
309
|
+
argsTextDelta: chunk.argsTextDelta
|
|
310
|
+
}) + "\n"
|
|
311
|
+
);
|
|
312
|
+
break;
|
|
313
|
+
case "tool-result":
|
|
314
|
+
controller.enqueue(
|
|
315
|
+
"a:" + JSON.stringify({
|
|
316
|
+
toolCallId: chunk.toolCallId,
|
|
317
|
+
result: chunk.result
|
|
318
|
+
}) + "\n"
|
|
319
|
+
);
|
|
320
|
+
break;
|
|
321
|
+
default:
|
|
322
|
+
const exhaustiveCheck = type;
|
|
323
|
+
throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
var decodeStreamPart = (part) => {
|
|
332
|
+
const index = part.indexOf(":");
|
|
333
|
+
if (index === -1) throw new Error("Invalid stream part");
|
|
334
|
+
return {
|
|
335
|
+
type: part.slice(0, index),
|
|
336
|
+
value: JSON.parse(part.slice(index + 1))
|
|
337
|
+
};
|
|
338
|
+
};
|
|
339
|
+
var DataStreamDecoder = 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, value } = decodeStreamPart(chunk);
|
|
352
|
+
switch (type) {
|
|
353
|
+
case "0":
|
|
354
|
+
controller.enqueue({
|
|
355
|
+
type: "text-delta",
|
|
356
|
+
textDelta: value
|
|
357
|
+
});
|
|
358
|
+
break;
|
|
359
|
+
case "b": {
|
|
360
|
+
const { toolCallId, toolName } = value;
|
|
361
|
+
controller.enqueue({
|
|
362
|
+
type: "tool-call-begin",
|
|
363
|
+
toolCallId,
|
|
364
|
+
toolName
|
|
365
|
+
});
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case "c": {
|
|
369
|
+
const { toolCallId, argsTextDelta } = value;
|
|
370
|
+
controller.enqueue({
|
|
371
|
+
type: "tool-call-delta",
|
|
372
|
+
toolCallId,
|
|
373
|
+
argsTextDelta
|
|
374
|
+
});
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
case "a": {
|
|
378
|
+
const { toolCallId, result } = value;
|
|
379
|
+
controller.enqueue({
|
|
380
|
+
type: "tool-result",
|
|
381
|
+
toolCallId,
|
|
382
|
+
result
|
|
383
|
+
});
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
default:
|
|
387
|
+
const exhaustiveCheck = type;
|
|
388
|
+
throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
return readable.pipeThrough(new TextDecoderStream()).pipeThrough(new ChunkByLineStream()).pipeThrough(transform);
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
var ChunkByLineStream = class extends TransformStream {
|
|
397
|
+
buffer = "";
|
|
398
|
+
constructor() {
|
|
399
|
+
super({
|
|
400
|
+
transform: (chunk, controller) => {
|
|
401
|
+
this.buffer += chunk;
|
|
402
|
+
const lines = this.buffer.split("\n");
|
|
403
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
404
|
+
controller.enqueue(lines[i]);
|
|
405
|
+
}
|
|
406
|
+
this.buffer = lines[lines.length - 1];
|
|
407
|
+
},
|
|
408
|
+
flush: (controller) => {
|
|
409
|
+
if (this.buffer) {
|
|
410
|
+
controller.enqueue(this.buffer);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
// src/core/serialization/PlainText.ts
|
|
418
|
+
var PlainTextEncoder = class {
|
|
419
|
+
_transformStream;
|
|
420
|
+
get writable() {
|
|
421
|
+
return this._transformStream.writable;
|
|
422
|
+
}
|
|
423
|
+
get readable() {
|
|
424
|
+
return this._transformStream.readable;
|
|
425
|
+
}
|
|
426
|
+
constructor() {
|
|
427
|
+
this._transformStream = new PipeableTransformStream((readable) => {
|
|
428
|
+
const transform = new TransformStream({
|
|
429
|
+
transform(chunk, controller) {
|
|
430
|
+
const type = chunk.type;
|
|
431
|
+
switch (type) {
|
|
432
|
+
case "text-delta":
|
|
433
|
+
controller.enqueue(chunk.textDelta);
|
|
434
|
+
break;
|
|
435
|
+
case "tool-call-begin":
|
|
436
|
+
case "tool-call-delta":
|
|
437
|
+
case "tool-result":
|
|
438
|
+
throw new Error(
|
|
439
|
+
`tool-call-begin and tool-call-delta are not supported in plain text serialization`
|
|
440
|
+
);
|
|
441
|
+
default:
|
|
442
|
+
const exhaustiveCheck = type;
|
|
443
|
+
throw new Error(`unsupported chunk type: ${exhaustiveCheck}`);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
return readable.pipeThrough(transform).pipeThrough(new TextEncoderStream());
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
var PlainTextDecoder = class {
|
|
452
|
+
_transformStream;
|
|
453
|
+
get writable() {
|
|
454
|
+
return this._transformStream.writable;
|
|
455
|
+
}
|
|
456
|
+
get readable() {
|
|
457
|
+
return this._transformStream.readable;
|
|
458
|
+
}
|
|
459
|
+
constructor() {
|
|
460
|
+
this._transformStream = new PipeableTransformStream((readable) => {
|
|
461
|
+
const transform = new TransformStream({
|
|
462
|
+
transform(chunk, controller) {
|
|
463
|
+
controller.enqueue({
|
|
464
|
+
type: "text-delta",
|
|
465
|
+
textDelta: chunk
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
return readable.pipeThrough(new TextDecoderStream()).pipeThrough(transform);
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
|
|
474
|
+
// src/core/accumulators/partial-json/parse-partial-json.ts
|
|
475
|
+
import sjson from "secure-json-parse";
|
|
476
|
+
|
|
477
|
+
// src/core/accumulators/partial-json/fix-json.ts
|
|
478
|
+
function fixJson(input) {
|
|
479
|
+
const stack = ["ROOT"];
|
|
480
|
+
let lastValidIndex = -1;
|
|
481
|
+
let literalStart = null;
|
|
482
|
+
function processValueStart(char, i, swapState) {
|
|
483
|
+
{
|
|
484
|
+
switch (char) {
|
|
485
|
+
case '"': {
|
|
486
|
+
lastValidIndex = i;
|
|
487
|
+
stack.pop();
|
|
488
|
+
stack.push(swapState);
|
|
489
|
+
stack.push("INSIDE_STRING");
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
case "f":
|
|
493
|
+
case "t":
|
|
494
|
+
case "n": {
|
|
495
|
+
lastValidIndex = i;
|
|
496
|
+
literalStart = i;
|
|
497
|
+
stack.pop();
|
|
498
|
+
stack.push(swapState);
|
|
499
|
+
stack.push("INSIDE_LITERAL");
|
|
500
|
+
break;
|
|
501
|
+
}
|
|
502
|
+
case "-": {
|
|
503
|
+
stack.pop();
|
|
504
|
+
stack.push(swapState);
|
|
505
|
+
stack.push("INSIDE_NUMBER");
|
|
506
|
+
break;
|
|
507
|
+
}
|
|
508
|
+
case "0":
|
|
509
|
+
case "1":
|
|
510
|
+
case "2":
|
|
511
|
+
case "3":
|
|
512
|
+
case "4":
|
|
513
|
+
case "5":
|
|
514
|
+
case "6":
|
|
515
|
+
case "7":
|
|
516
|
+
case "8":
|
|
517
|
+
case "9": {
|
|
518
|
+
lastValidIndex = i;
|
|
519
|
+
stack.pop();
|
|
520
|
+
stack.push(swapState);
|
|
521
|
+
stack.push("INSIDE_NUMBER");
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
case "{": {
|
|
525
|
+
lastValidIndex = i;
|
|
526
|
+
stack.pop();
|
|
527
|
+
stack.push(swapState);
|
|
528
|
+
stack.push("INSIDE_OBJECT_START");
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
case "[": {
|
|
532
|
+
lastValidIndex = i;
|
|
533
|
+
stack.pop();
|
|
534
|
+
stack.push(swapState);
|
|
535
|
+
stack.push("INSIDE_ARRAY_START");
|
|
536
|
+
break;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
function processAfterObjectValue(char, i) {
|
|
542
|
+
switch (char) {
|
|
543
|
+
case ",": {
|
|
544
|
+
stack.pop();
|
|
545
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
case "}": {
|
|
549
|
+
lastValidIndex = i;
|
|
550
|
+
stack.pop();
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
function processAfterArrayValue(char, i) {
|
|
556
|
+
switch (char) {
|
|
557
|
+
case ",": {
|
|
558
|
+
stack.pop();
|
|
559
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
case "]": {
|
|
563
|
+
lastValidIndex = i;
|
|
564
|
+
stack.pop();
|
|
565
|
+
break;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
for (let i = 0; i < input.length; i++) {
|
|
570
|
+
const char = input[i];
|
|
571
|
+
const currentState = stack[stack.length - 1];
|
|
572
|
+
switch (currentState) {
|
|
573
|
+
case "ROOT":
|
|
574
|
+
processValueStart(char, i, "FINISH");
|
|
575
|
+
break;
|
|
576
|
+
case "INSIDE_OBJECT_START": {
|
|
577
|
+
switch (char) {
|
|
578
|
+
case '"': {
|
|
579
|
+
stack.pop();
|
|
580
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
case "}": {
|
|
584
|
+
lastValidIndex = i;
|
|
585
|
+
stack.pop();
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
592
|
+
switch (char) {
|
|
593
|
+
case '"': {
|
|
594
|
+
stack.pop();
|
|
595
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
596
|
+
break;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
case "INSIDE_OBJECT_KEY": {
|
|
602
|
+
switch (char) {
|
|
603
|
+
case '"': {
|
|
604
|
+
stack.pop();
|
|
605
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
606
|
+
break;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
break;
|
|
610
|
+
}
|
|
611
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
612
|
+
switch (char) {
|
|
613
|
+
case ":": {
|
|
614
|
+
stack.pop();
|
|
615
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
622
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
626
|
+
processAfterObjectValue(char, i);
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
case "INSIDE_STRING": {
|
|
630
|
+
switch (char) {
|
|
631
|
+
case '"': {
|
|
632
|
+
stack.pop();
|
|
633
|
+
lastValidIndex = i;
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
case "\\": {
|
|
637
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
|
638
|
+
break;
|
|
639
|
+
}
|
|
640
|
+
default: {
|
|
641
|
+
lastValidIndex = i;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
case "INSIDE_ARRAY_START": {
|
|
647
|
+
switch (char) {
|
|
648
|
+
case "]": {
|
|
649
|
+
lastValidIndex = i;
|
|
650
|
+
stack.pop();
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
default: {
|
|
654
|
+
lastValidIndex = i;
|
|
655
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
662
|
+
switch (char) {
|
|
663
|
+
case ",": {
|
|
664
|
+
stack.pop();
|
|
665
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
666
|
+
break;
|
|
667
|
+
}
|
|
668
|
+
case "]": {
|
|
669
|
+
lastValidIndex = i;
|
|
670
|
+
stack.pop();
|
|
671
|
+
break;
|
|
672
|
+
}
|
|
673
|
+
default: {
|
|
674
|
+
lastValidIndex = i;
|
|
675
|
+
break;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
681
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
case "INSIDE_STRING_ESCAPE": {
|
|
685
|
+
stack.pop();
|
|
686
|
+
lastValidIndex = i;
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
case "INSIDE_NUMBER": {
|
|
690
|
+
switch (char) {
|
|
691
|
+
case "0":
|
|
692
|
+
case "1":
|
|
693
|
+
case "2":
|
|
694
|
+
case "3":
|
|
695
|
+
case "4":
|
|
696
|
+
case "5":
|
|
697
|
+
case "6":
|
|
698
|
+
case "7":
|
|
699
|
+
case "8":
|
|
700
|
+
case "9": {
|
|
701
|
+
lastValidIndex = i;
|
|
702
|
+
break;
|
|
703
|
+
}
|
|
704
|
+
case "e":
|
|
705
|
+
case "E":
|
|
706
|
+
case "-":
|
|
707
|
+
case ".": {
|
|
708
|
+
break;
|
|
709
|
+
}
|
|
710
|
+
case ",": {
|
|
711
|
+
stack.pop();
|
|
712
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
713
|
+
processAfterArrayValue(char, i);
|
|
714
|
+
}
|
|
715
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
716
|
+
processAfterObjectValue(char, i);
|
|
717
|
+
}
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
case "}": {
|
|
721
|
+
stack.pop();
|
|
722
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
723
|
+
processAfterObjectValue(char, i);
|
|
724
|
+
}
|
|
725
|
+
break;
|
|
726
|
+
}
|
|
727
|
+
case "]": {
|
|
728
|
+
stack.pop();
|
|
729
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
730
|
+
processAfterArrayValue(char, i);
|
|
731
|
+
}
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
default: {
|
|
735
|
+
stack.pop();
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
case "INSIDE_LITERAL": {
|
|
742
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
|
743
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
744
|
+
stack.pop();
|
|
745
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
746
|
+
processAfterObjectValue(char, i);
|
|
747
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
748
|
+
processAfterArrayValue(char, i);
|
|
749
|
+
}
|
|
750
|
+
} else {
|
|
751
|
+
lastValidIndex = i;
|
|
752
|
+
}
|
|
753
|
+
break;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
let result = input.slice(0, lastValidIndex + 1);
|
|
758
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
759
|
+
const state = stack[i];
|
|
760
|
+
switch (state) {
|
|
761
|
+
case "INSIDE_STRING": {
|
|
762
|
+
result += '"';
|
|
763
|
+
break;
|
|
764
|
+
}
|
|
765
|
+
case "INSIDE_OBJECT_KEY":
|
|
766
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
|
767
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
768
|
+
case "INSIDE_OBJECT_START":
|
|
769
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
770
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
771
|
+
result += "}";
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
case "INSIDE_ARRAY_START":
|
|
775
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
776
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
777
|
+
result += "]";
|
|
778
|
+
break;
|
|
779
|
+
}
|
|
780
|
+
case "INSIDE_LITERAL": {
|
|
781
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
|
782
|
+
if ("true".startsWith(partialLiteral)) {
|
|
783
|
+
result += "true".slice(partialLiteral.length);
|
|
784
|
+
} else if ("false".startsWith(partialLiteral)) {
|
|
785
|
+
result += "false".slice(partialLiteral.length);
|
|
786
|
+
} else if ("null".startsWith(partialLiteral)) {
|
|
787
|
+
result += "null".slice(partialLiteral.length);
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return result;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// src/core/accumulators/partial-json/parse-partial-json.ts
|
|
796
|
+
var parsePartialJson = (json) => {
|
|
797
|
+
try {
|
|
798
|
+
return sjson.parse(json);
|
|
799
|
+
} catch {
|
|
800
|
+
try {
|
|
801
|
+
return sjson.parse(fixJson(json));
|
|
802
|
+
} catch {
|
|
803
|
+
return void 0;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
|
|
808
|
+
// src/core/accumulators/assistant-message.ts
|
|
809
|
+
function createAsyncIterableStream(source, transform) {
|
|
810
|
+
const transformedStream = source.pipeThrough(transform);
|
|
811
|
+
transformedStream[Symbol.asyncIterator] = () => {
|
|
812
|
+
const reader = transformedStream.getReader();
|
|
813
|
+
return {
|
|
814
|
+
async next() {
|
|
815
|
+
const { done, value } = await reader.read();
|
|
816
|
+
return done ? { done: true, value: void 0 } : { done: false, value };
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
};
|
|
820
|
+
return transformedStream;
|
|
821
|
+
}
|
|
822
|
+
var toAssistantMessageStream = (stream) => {
|
|
823
|
+
let message = {
|
|
824
|
+
role: "assistant",
|
|
825
|
+
content: [],
|
|
826
|
+
status: { type: "running" },
|
|
827
|
+
metadata: {
|
|
828
|
+
steps: [],
|
|
829
|
+
custom: {}
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
const transformer = new TransformStream({
|
|
833
|
+
transform(chunk, controller) {
|
|
834
|
+
const { type } = chunk;
|
|
835
|
+
switch (type) {
|
|
836
|
+
case "text-delta": {
|
|
837
|
+
message = appendOrUpdateText(message, chunk.textDelta);
|
|
838
|
+
controller.enqueue(message);
|
|
839
|
+
break;
|
|
840
|
+
}
|
|
841
|
+
case "tool-call-begin": {
|
|
842
|
+
const { toolCallId, toolName } = chunk;
|
|
843
|
+
message = appendToolCall(message, toolCallId, toolName);
|
|
844
|
+
controller.enqueue(message);
|
|
845
|
+
break;
|
|
846
|
+
}
|
|
847
|
+
case "tool-call-delta": {
|
|
848
|
+
const { toolCallId, argsTextDelta } = chunk;
|
|
849
|
+
message = appendToolArgsTextDelta(message, toolCallId, argsTextDelta);
|
|
850
|
+
controller.enqueue(message);
|
|
851
|
+
break;
|
|
852
|
+
}
|
|
853
|
+
case "tool-result": {
|
|
854
|
+
const { toolCallId, result } = chunk;
|
|
855
|
+
message = setToolResult(message, toolCallId, result);
|
|
856
|
+
controller.enqueue(message);
|
|
857
|
+
break;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
},
|
|
861
|
+
flush(controller) {
|
|
862
|
+
message = appendOrUpdateFinish(message);
|
|
863
|
+
controller.enqueue(message);
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
return createAsyncIterableStream(stream.readable, transformer);
|
|
867
|
+
};
|
|
868
|
+
var appendOrUpdateText = (message, textDelta) => {
|
|
869
|
+
let contentParts = message.content ?? [];
|
|
870
|
+
let contentPart = message.content?.at(-1);
|
|
871
|
+
if (contentPart?.type !== "text") {
|
|
872
|
+
contentPart = {
|
|
873
|
+
type: "text",
|
|
874
|
+
text: textDelta,
|
|
875
|
+
status: { type: "running" }
|
|
876
|
+
};
|
|
877
|
+
} else {
|
|
878
|
+
contentParts = contentParts.slice(0, -1);
|
|
879
|
+
contentPart = {
|
|
880
|
+
type: "text",
|
|
881
|
+
text: contentPart.text + textDelta,
|
|
882
|
+
status: { type: "running" }
|
|
883
|
+
};
|
|
884
|
+
}
|
|
885
|
+
return {
|
|
886
|
+
...message,
|
|
887
|
+
content: contentParts.concat([contentPart])
|
|
888
|
+
};
|
|
889
|
+
};
|
|
890
|
+
var appendToolCall = (message, toolCallId, toolName) => {
|
|
891
|
+
return {
|
|
892
|
+
...message,
|
|
893
|
+
content: [
|
|
894
|
+
...message.content,
|
|
895
|
+
{
|
|
896
|
+
type: "tool-call",
|
|
897
|
+
toolCallId,
|
|
898
|
+
toolName,
|
|
899
|
+
argsText: "",
|
|
900
|
+
args: {},
|
|
901
|
+
status: { type: "running", isArgsComplete: false }
|
|
902
|
+
}
|
|
903
|
+
]
|
|
904
|
+
};
|
|
905
|
+
};
|
|
906
|
+
var appendToolArgsTextDelta = (message, toolCallId, argsTextDelta) => {
|
|
907
|
+
const contentPartIdx = message.content.findIndex(
|
|
908
|
+
(part) => part.type === "tool-call" && part.toolCallId === toolCallId
|
|
909
|
+
);
|
|
910
|
+
if (contentPartIdx === -1)
|
|
911
|
+
throw new Error(
|
|
912
|
+
`Received tool call delta for unknown tool call "${toolCallId}".`
|
|
913
|
+
);
|
|
914
|
+
const contentPart = message.content[contentPartIdx];
|
|
915
|
+
const newArgsText = contentPart.argsText + argsTextDelta;
|
|
916
|
+
return {
|
|
917
|
+
...message,
|
|
918
|
+
content: [
|
|
919
|
+
...message.content.slice(0, contentPartIdx),
|
|
920
|
+
{
|
|
921
|
+
...contentPart,
|
|
922
|
+
argsText: newArgsText,
|
|
923
|
+
args: parsePartialJson(newArgsText)
|
|
924
|
+
},
|
|
925
|
+
...message.content.slice(contentPartIdx + 1)
|
|
926
|
+
]
|
|
927
|
+
};
|
|
928
|
+
};
|
|
929
|
+
var setToolResult = (message, toolCallId, result) => {
|
|
930
|
+
let found = false;
|
|
931
|
+
const newContentParts = message.content?.map((part) => {
|
|
932
|
+
if (part.type !== "tool-call" || part.toolCallId !== toolCallId)
|
|
933
|
+
return part;
|
|
934
|
+
found = true;
|
|
935
|
+
return {
|
|
936
|
+
...part,
|
|
937
|
+
result
|
|
938
|
+
};
|
|
939
|
+
});
|
|
940
|
+
if (!found)
|
|
941
|
+
throw new Error(
|
|
942
|
+
`Received tool result for unknown tool call "${toolCallId}". This is likely an internal bug in assistant-ui.`
|
|
943
|
+
);
|
|
944
|
+
return {
|
|
945
|
+
...message,
|
|
946
|
+
content: newContentParts
|
|
947
|
+
};
|
|
948
|
+
};
|
|
949
|
+
var appendOrUpdateFinish = (message) => {
|
|
950
|
+
return {
|
|
951
|
+
...message,
|
|
952
|
+
status: {
|
|
953
|
+
type: "complete",
|
|
954
|
+
reason: "unknown"
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
export {
|
|
960
|
+
AssistantStream,
|
|
961
|
+
generateId,
|
|
962
|
+
createAssistantRun,
|
|
963
|
+
DataStreamEncoder,
|
|
964
|
+
DataStreamDecoder,
|
|
965
|
+
PlainTextEncoder,
|
|
966
|
+
PlainTextDecoder,
|
|
967
|
+
toAssistantMessageStream
|
|
968
|
+
};
|
|
969
|
+
//# sourceMappingURL=chunk-7CCRZC5D.mjs.map
|