ai 2.2.35 → 2.2.36
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.d.ts +159 -159
- package/dist/index.js +731 -728
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +733 -730
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
@@ -1,122 +1,5 @@
|
|
1
|
-
//
|
2
|
-
import {
|
3
|
-
createParser
|
4
|
-
} from "eventsource-parser";
|
5
|
-
function createEventStreamTransformer(customParser) {
|
6
|
-
const textDecoder = new TextDecoder();
|
7
|
-
let eventSourceParser;
|
8
|
-
return new TransformStream({
|
9
|
-
async start(controller) {
|
10
|
-
eventSourceParser = createParser(
|
11
|
-
(event) => {
|
12
|
-
if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
|
13
|
-
// @see https://replicate.com/docs/streaming
|
14
|
-
event.event === "done") {
|
15
|
-
controller.terminate();
|
16
|
-
return;
|
17
|
-
}
|
18
|
-
if ("data" in event) {
|
19
|
-
const parsedMessage = customParser ? customParser(event.data, {
|
20
|
-
event: event.event
|
21
|
-
}) : event.data;
|
22
|
-
if (parsedMessage)
|
23
|
-
controller.enqueue(parsedMessage);
|
24
|
-
}
|
25
|
-
}
|
26
|
-
);
|
27
|
-
},
|
28
|
-
transform(chunk) {
|
29
|
-
eventSourceParser.feed(textDecoder.decode(chunk));
|
30
|
-
}
|
31
|
-
});
|
32
|
-
}
|
33
|
-
function createCallbacksTransformer(cb) {
|
34
|
-
const textEncoder = new TextEncoder();
|
35
|
-
let aggregatedResponse = "";
|
36
|
-
const callbacks = cb || {};
|
37
|
-
return new TransformStream({
|
38
|
-
async start() {
|
39
|
-
if (callbacks.onStart)
|
40
|
-
await callbacks.onStart();
|
41
|
-
},
|
42
|
-
async transform(message, controller) {
|
43
|
-
controller.enqueue(textEncoder.encode(message));
|
44
|
-
aggregatedResponse += message;
|
45
|
-
if (callbacks.onToken)
|
46
|
-
await callbacks.onToken(message);
|
47
|
-
},
|
48
|
-
async flush() {
|
49
|
-
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
50
|
-
if (callbacks.onCompletion) {
|
51
|
-
await callbacks.onCompletion(aggregatedResponse);
|
52
|
-
}
|
53
|
-
if (callbacks.onFinal && !isOpenAICallbacks) {
|
54
|
-
await callbacks.onFinal(aggregatedResponse);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
});
|
58
|
-
}
|
59
|
-
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
60
|
-
return "experimental_onFunctionCall" in callbacks;
|
61
|
-
}
|
62
|
-
function trimStartOfStreamHelper() {
|
63
|
-
let isStreamStart = true;
|
64
|
-
return (text) => {
|
65
|
-
if (isStreamStart) {
|
66
|
-
text = text.trimStart();
|
67
|
-
if (text)
|
68
|
-
isStreamStart = false;
|
69
|
-
}
|
70
|
-
return text;
|
71
|
-
};
|
72
|
-
}
|
73
|
-
function AIStream(response, customParser, callbacks) {
|
74
|
-
if (!response.ok) {
|
75
|
-
if (response.body) {
|
76
|
-
const reader = response.body.getReader();
|
77
|
-
return new ReadableStream({
|
78
|
-
async start(controller) {
|
79
|
-
const { done, value } = await reader.read();
|
80
|
-
if (!done) {
|
81
|
-
const errorText = new TextDecoder().decode(value);
|
82
|
-
controller.error(new Error(`Response error: ${errorText}`));
|
83
|
-
}
|
84
|
-
}
|
85
|
-
});
|
86
|
-
} else {
|
87
|
-
return new ReadableStream({
|
88
|
-
start(controller) {
|
89
|
-
controller.error(new Error("Response error: No response body"));
|
90
|
-
}
|
91
|
-
});
|
92
|
-
}
|
93
|
-
}
|
94
|
-
const responseBodyStream = response.body || createEmptyReadableStream();
|
95
|
-
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
96
|
-
}
|
97
|
-
function createEmptyReadableStream() {
|
98
|
-
return new ReadableStream({
|
99
|
-
start(controller) {
|
100
|
-
controller.close();
|
101
|
-
}
|
102
|
-
});
|
103
|
-
}
|
104
|
-
function readableFromAsyncIterable(iterable) {
|
105
|
-
let it = iterable[Symbol.asyncIterator]();
|
106
|
-
return new ReadableStream({
|
107
|
-
async pull(controller) {
|
108
|
-
const { done, value } = await it.next();
|
109
|
-
if (done)
|
110
|
-
controller.close();
|
111
|
-
else
|
112
|
-
controller.enqueue(value);
|
113
|
-
},
|
114
|
-
async cancel(reason) {
|
115
|
-
var _a;
|
116
|
-
await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
|
117
|
-
}
|
118
|
-
});
|
119
|
-
}
|
1
|
+
// shared/utils.ts
|
2
|
+
import { customAlphabet } from "nanoid/non-secure";
|
120
3
|
|
121
4
|
// shared/stream-parts.ts
|
122
5
|
var textStreamPart = {
|
@@ -298,538 +181,392 @@ function formatStreamPart(type, value) {
|
|
298
181
|
`;
|
299
182
|
}
|
300
183
|
|
301
|
-
//
|
302
|
-
var
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
// in case we're doing async work
|
308
|
-
this.isClosedPromise = null;
|
309
|
-
this.isClosedPromiseResolver = void 0;
|
310
|
-
this.isClosed = false;
|
311
|
-
// array to store appended data
|
312
|
-
this.data = [];
|
313
|
-
this.messageAnnotations = [];
|
314
|
-
this.isClosedPromise = new Promise((resolve) => {
|
315
|
-
this.isClosedPromiseResolver = resolve;
|
316
|
-
});
|
317
|
-
const self = this;
|
318
|
-
this.stream = new TransformStream({
|
319
|
-
start: async (controller) => {
|
320
|
-
self.controller = controller;
|
321
|
-
},
|
322
|
-
transform: async (chunk, controller) => {
|
323
|
-
if (self.data.length > 0) {
|
324
|
-
const encodedData = self.encoder.encode(
|
325
|
-
formatStreamPart("data", self.data)
|
326
|
-
);
|
327
|
-
self.data = [];
|
328
|
-
controller.enqueue(encodedData);
|
329
|
-
}
|
330
|
-
if (self.messageAnnotations.length) {
|
331
|
-
const encodedMessageAnnotations = self.encoder.encode(
|
332
|
-
formatStreamPart("message_annotations", self.messageAnnotations)
|
333
|
-
);
|
334
|
-
self.messageAnnotations = [];
|
335
|
-
controller.enqueue(encodedMessageAnnotations);
|
336
|
-
}
|
337
|
-
controller.enqueue(chunk);
|
338
|
-
},
|
339
|
-
async flush(controller) {
|
340
|
-
const warningTimeout = process.env.NODE_ENV === "development" ? setTimeout(() => {
|
341
|
-
console.warn(
|
342
|
-
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
343
|
-
);
|
344
|
-
}, 3e3) : null;
|
345
|
-
await self.isClosedPromise;
|
346
|
-
if (warningTimeout !== null) {
|
347
|
-
clearTimeout(warningTimeout);
|
348
|
-
}
|
349
|
-
if (self.data.length) {
|
350
|
-
const encodedData = self.encoder.encode(
|
351
|
-
formatStreamPart("data", self.data)
|
352
|
-
);
|
353
|
-
controller.enqueue(encodedData);
|
354
|
-
}
|
355
|
-
if (self.messageAnnotations.length) {
|
356
|
-
const encodedData = self.encoder.encode(
|
357
|
-
formatStreamPart("message_annotations", self.messageAnnotations)
|
358
|
-
);
|
359
|
-
controller.enqueue(encodedData);
|
360
|
-
}
|
361
|
-
}
|
362
|
-
});
|
363
|
-
}
|
364
|
-
async close() {
|
365
|
-
var _a;
|
366
|
-
if (this.isClosed) {
|
367
|
-
throw new Error("Data Stream has already been closed.");
|
368
|
-
}
|
369
|
-
if (!this.controller) {
|
370
|
-
throw new Error("Stream controller is not initialized.");
|
371
|
-
}
|
372
|
-
(_a = this.isClosedPromiseResolver) == null ? void 0 : _a.call(this);
|
373
|
-
this.isClosed = true;
|
374
|
-
}
|
375
|
-
append(value) {
|
376
|
-
if (this.isClosed) {
|
377
|
-
throw new Error("Data Stream has already been closed.");
|
378
|
-
}
|
379
|
-
this.data.push(value);
|
380
|
-
}
|
381
|
-
appendMessageAnnotation(value) {
|
382
|
-
if (this.isClosed) {
|
383
|
-
throw new Error("Data Stream has already been closed.");
|
384
|
-
}
|
385
|
-
this.messageAnnotations.push(value);
|
386
|
-
}
|
387
|
-
};
|
388
|
-
function createStreamDataTransformer(experimental_streamData) {
|
389
|
-
if (!experimental_streamData) {
|
390
|
-
return new TransformStream({
|
391
|
-
transform: async (chunk, controller) => {
|
392
|
-
controller.enqueue(chunk);
|
393
|
-
}
|
394
|
-
});
|
395
|
-
}
|
396
|
-
const encoder = new TextEncoder();
|
397
|
-
const decoder = new TextDecoder();
|
398
|
-
return new TransformStream({
|
399
|
-
transform: async (chunk, controller) => {
|
400
|
-
const message = decoder.decode(chunk);
|
401
|
-
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
402
|
-
}
|
403
|
-
});
|
404
|
-
}
|
405
|
-
|
406
|
-
// streams/aws-bedrock-stream.ts
|
407
|
-
async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
|
408
|
-
var _a, _b;
|
184
|
+
// shared/utils.ts
|
185
|
+
var nanoid = customAlphabet(
|
186
|
+
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
187
|
+
7
|
188
|
+
);
|
189
|
+
function createChunkDecoder(complex) {
|
409
190
|
const decoder = new TextDecoder();
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
if (delta != null) {
|
417
|
-
yield delta;
|
418
|
-
}
|
419
|
-
}
|
191
|
+
if (!complex) {
|
192
|
+
return function(chunk) {
|
193
|
+
if (!chunk)
|
194
|
+
return "";
|
195
|
+
return decoder.decode(chunk, { stream: true });
|
196
|
+
};
|
420
197
|
}
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
}
|
425
|
-
function AWSBedrockCohereStream(response, callbacks) {
|
426
|
-
return AWSBedrockStream(
|
427
|
-
response,
|
428
|
-
callbacks,
|
429
|
-
// As of 2023-11-17, Bedrock does not support streaming for Cohere,
|
430
|
-
// so we take the full generation:
|
431
|
-
(chunk) => {
|
432
|
-
var _a, _b;
|
433
|
-
return (_b = (_a = chunk.generations) == null ? void 0 : _a[0]) == null ? void 0 : _b.text;
|
434
|
-
}
|
435
|
-
);
|
436
|
-
}
|
437
|
-
function AWSBedrockLlama2Stream(response, callbacks) {
|
438
|
-
return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
|
439
|
-
}
|
440
|
-
function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
|
441
|
-
return readableFromAsyncIterable(
|
442
|
-
asDeltaIterable(response, extractTextDeltaFromChunk)
|
443
|
-
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
444
|
-
createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
|
445
|
-
);
|
446
|
-
}
|
447
|
-
|
448
|
-
// shared/utils.ts
|
449
|
-
import { customAlphabet } from "nanoid/non-secure";
|
450
|
-
var nanoid = customAlphabet(
|
451
|
-
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
452
|
-
7
|
453
|
-
);
|
454
|
-
function createChunkDecoder(complex) {
|
455
|
-
const decoder = new TextDecoder();
|
456
|
-
if (!complex) {
|
457
|
-
return function(chunk) {
|
458
|
-
if (!chunk)
|
459
|
-
return "";
|
460
|
-
return decoder.decode(chunk, { stream: true });
|
461
|
-
};
|
462
|
-
}
|
463
|
-
return function(chunk) {
|
464
|
-
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
465
|
-
return decoded.map(parseStreamPart).filter(Boolean);
|
466
|
-
};
|
198
|
+
return function(chunk) {
|
199
|
+
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
200
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
201
|
+
};
|
467
202
|
}
|
468
203
|
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
469
204
|
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
470
205
|
|
471
|
-
// streams/
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
index,
|
496
|
-
id: toolCall.id,
|
497
|
-
function: toolCall.function,
|
498
|
-
type: toolCall.type
|
499
|
-
})) : void 0
|
500
|
-
},
|
501
|
-
finish_reason: choice.finishReason,
|
502
|
-
index: choice.index
|
503
|
-
};
|
504
|
-
})
|
505
|
-
};
|
506
|
-
}
|
507
|
-
const text = extract(chunk);
|
508
|
-
if (text)
|
509
|
-
yield text;
|
510
|
-
}
|
511
|
-
}
|
512
|
-
function chunkToText() {
|
513
|
-
const trimStartOfStream = trimStartOfStreamHelper();
|
514
|
-
let isFunctionStreamingIn;
|
515
|
-
return (json) => {
|
516
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
517
|
-
if (isChatCompletionChunk(json)) {
|
518
|
-
const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
|
519
|
-
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
520
|
-
isFunctionStreamingIn = true;
|
521
|
-
return `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`;
|
522
|
-
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
523
|
-
isFunctionStreamingIn = true;
|
524
|
-
const toolCall = delta.tool_calls[0];
|
525
|
-
if (toolCall.index === 0) {
|
526
|
-
return `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`;
|
527
|
-
} else {
|
528
|
-
return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`;
|
206
|
+
// streams/ai-stream.ts
|
207
|
+
import {
|
208
|
+
createParser
|
209
|
+
} from "eventsource-parser";
|
210
|
+
function createEventStreamTransformer(customParser) {
|
211
|
+
const textDecoder = new TextDecoder();
|
212
|
+
let eventSourceParser;
|
213
|
+
return new TransformStream({
|
214
|
+
async start(controller) {
|
215
|
+
eventSourceParser = createParser(
|
216
|
+
(event) => {
|
217
|
+
if ("data" in event && event.type === "event" && event.data === "[DONE]" || // Replicate doesn't send [DONE] but does send a 'done' event
|
218
|
+
// @see https://replicate.com/docs/streaming
|
219
|
+
event.event === "done") {
|
220
|
+
controller.terminate();
|
221
|
+
return;
|
222
|
+
}
|
223
|
+
if ("data" in event) {
|
224
|
+
const parsedMessage = customParser ? customParser(event.data, {
|
225
|
+
event: event.event
|
226
|
+
}) : event.data;
|
227
|
+
if (parsedMessage)
|
228
|
+
controller.enqueue(parsedMessage);
|
229
|
+
}
|
529
230
|
}
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
} else if (isFunctionStreamingIn && (((_o = json.choices[0]) == null ? void 0 : _o.finish_reason) === "function_call" || ((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "stop")) {
|
535
|
-
isFunctionStreamingIn = false;
|
536
|
-
return '"}}';
|
537
|
-
} else if (isFunctionStreamingIn && ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "tool_calls") {
|
538
|
-
isFunctionStreamingIn = false;
|
539
|
-
return '"}}]}';
|
540
|
-
}
|
231
|
+
);
|
232
|
+
},
|
233
|
+
transform(chunk) {
|
234
|
+
eventSourceParser.feed(textDecoder.decode(chunk));
|
541
235
|
}
|
542
|
-
|
543
|
-
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
544
|
-
);
|
545
|
-
return text;
|
546
|
-
};
|
547
|
-
function cleanupArguments(argumentChunk) {
|
548
|
-
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
549
|
-
return `${escapedPartialJson}`;
|
550
|
-
}
|
551
|
-
}
|
552
|
-
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
553
|
-
"internal_openai_fn_messages"
|
554
|
-
);
|
555
|
-
function isChatCompletionChunk(data) {
|
556
|
-
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
557
|
-
}
|
558
|
-
function isCompletion(data) {
|
559
|
-
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
560
|
-
}
|
561
|
-
function OpenAIStream(res, callbacks) {
|
562
|
-
const cb = callbacks;
|
563
|
-
let stream;
|
564
|
-
if (Symbol.asyncIterator in res) {
|
565
|
-
stream = readableFromAsyncIterable(streamable(res)).pipeThrough(
|
566
|
-
createCallbacksTransformer(
|
567
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
568
|
-
...cb,
|
569
|
-
onFinal: void 0
|
570
|
-
} : {
|
571
|
-
...cb
|
572
|
-
}
|
573
|
-
)
|
574
|
-
);
|
575
|
-
} else {
|
576
|
-
stream = AIStream(
|
577
|
-
res,
|
578
|
-
parseOpenAIStream(),
|
579
|
-
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
580
|
-
...cb,
|
581
|
-
onFinal: void 0
|
582
|
-
} : {
|
583
|
-
...cb
|
584
|
-
}
|
585
|
-
);
|
586
|
-
}
|
587
|
-
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
588
|
-
const functionCallTransformer = createFunctionCallTransformer(cb);
|
589
|
-
return stream.pipeThrough(functionCallTransformer);
|
590
|
-
} else {
|
591
|
-
return stream.pipeThrough(
|
592
|
-
createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData)
|
593
|
-
);
|
594
|
-
}
|
236
|
+
});
|
595
237
|
}
|
596
|
-
function
|
238
|
+
function createCallbacksTransformer(cb) {
|
597
239
|
const textEncoder = new TextEncoder();
|
598
|
-
let isFirstChunk = true;
|
599
240
|
let aggregatedResponse = "";
|
600
|
-
|
601
|
-
let isFunctionStreamingIn = false;
|
602
|
-
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
603
|
-
const isComplexMode = callbacks == null ? void 0 : callbacks.experimental_streamData;
|
604
|
-
const decode = createChunkDecoder();
|
241
|
+
const callbacks = cb || {};
|
605
242
|
return new TransformStream({
|
606
|
-
async
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
243
|
+
async start() {
|
244
|
+
if (callbacks.onStart)
|
245
|
+
await callbacks.onStart();
|
246
|
+
},
|
247
|
+
async transform(message, controller) {
|
248
|
+
controller.enqueue(textEncoder.encode(message));
|
249
|
+
aggregatedResponse += message;
|
250
|
+
if (callbacks.onToken)
|
251
|
+
await callbacks.onToken(message);
|
252
|
+
},
|
253
|
+
async flush() {
|
254
|
+
const isOpenAICallbacks = isOfTypeOpenAIStreamCallbacks(callbacks);
|
255
|
+
if (callbacks.onCompletion) {
|
256
|
+
await callbacks.onCompletion(aggregatedResponse);
|
615
257
|
}
|
616
|
-
if (!
|
617
|
-
|
618
|
-
isComplexMode ? textEncoder.encode(formatStreamPart("text", message)) : chunk
|
619
|
-
);
|
620
|
-
return;
|
621
|
-
} else {
|
622
|
-
aggregatedResponse += message;
|
258
|
+
if (callbacks.onFinal && !isOpenAICallbacks) {
|
259
|
+
await callbacks.onFinal(aggregatedResponse);
|
623
260
|
}
|
624
|
-
}
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
{
|
652
|
-
role: "assistant",
|
653
|
-
content: "",
|
654
|
-
function_call: payload.function_call
|
655
|
-
},
|
656
|
-
{
|
657
|
-
role: "function",
|
658
|
-
name: payload.function_call.name,
|
659
|
-
content: JSON.stringify(result)
|
660
|
-
}
|
661
|
-
];
|
662
|
-
return newFunctionCallMessages;
|
663
|
-
}
|
664
|
-
);
|
665
|
-
}
|
666
|
-
if (callbacks.experimental_onToolCall) {
|
667
|
-
const toolCalls = {
|
668
|
-
tools: []
|
669
|
-
};
|
670
|
-
for (const tool of payload.tool_calls) {
|
671
|
-
toolCalls.tools.push({
|
672
|
-
id: tool.id,
|
673
|
-
type: "function",
|
674
|
-
func: {
|
675
|
-
name: tool.function.name,
|
676
|
-
arguments: tool.function.arguments
|
677
|
-
}
|
678
|
-
});
|
679
|
-
}
|
680
|
-
let responseIndex = 0;
|
681
|
-
try {
|
682
|
-
functionResponse = await callbacks.experimental_onToolCall(
|
683
|
-
toolCalls,
|
684
|
-
(result) => {
|
685
|
-
if (result) {
|
686
|
-
const { tool_call_id, function_name, tool_call_result } = result;
|
687
|
-
newFunctionCallMessages = [
|
688
|
-
...newFunctionCallMessages,
|
689
|
-
// Only append the assistant message if it's the first response
|
690
|
-
...responseIndex === 0 ? [
|
691
|
-
{
|
692
|
-
role: "assistant",
|
693
|
-
content: "",
|
694
|
-
tool_calls: payload.tool_calls.map(
|
695
|
-
(tc) => ({
|
696
|
-
id: tc.id,
|
697
|
-
type: "function",
|
698
|
-
function: {
|
699
|
-
name: tc.function.name,
|
700
|
-
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
701
|
-
arguments: JSON.stringify(
|
702
|
-
tc.function.arguments
|
703
|
-
)
|
704
|
-
}
|
705
|
-
})
|
706
|
-
)
|
707
|
-
}
|
708
|
-
] : [],
|
709
|
-
// Append the function call result message
|
710
|
-
{
|
711
|
-
role: "tool",
|
712
|
-
tool_call_id,
|
713
|
-
name: function_name,
|
714
|
-
content: JSON.stringify(tool_call_result)
|
715
|
-
}
|
716
|
-
];
|
717
|
-
responseIndex++;
|
718
|
-
}
|
719
|
-
return newFunctionCallMessages;
|
720
|
-
}
|
721
|
-
);
|
722
|
-
} catch (e) {
|
723
|
-
console.error("Error calling experimental_onToolCall:", e);
|
724
|
-
}
|
725
|
-
}
|
726
|
-
if (!functionResponse) {
|
727
|
-
controller.enqueue(
|
728
|
-
textEncoder.encode(
|
729
|
-
isComplexMode ? formatStreamPart(
|
730
|
-
payload.function_call ? "function_call" : "tool_calls",
|
731
|
-
// parse to prevent double-encoding:
|
732
|
-
JSON.parse(aggregatedResponse)
|
733
|
-
) : aggregatedResponse
|
734
|
-
)
|
735
|
-
);
|
736
|
-
return;
|
737
|
-
} else if (typeof functionResponse === "string") {
|
738
|
-
controller.enqueue(
|
739
|
-
isComplexMode ? textEncoder.encode(formatStreamPart("text", functionResponse)) : textEncoder.encode(functionResponse)
|
740
|
-
);
|
741
|
-
return;
|
742
|
-
}
|
743
|
-
const filteredCallbacks = {
|
744
|
-
...callbacks,
|
745
|
-
onStart: void 0
|
746
|
-
};
|
747
|
-
callbacks.onFinal = void 0;
|
748
|
-
const openAIStream = OpenAIStream(functionResponse, {
|
749
|
-
...filteredCallbacks,
|
750
|
-
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
751
|
-
});
|
752
|
-
const reader = openAIStream.getReader();
|
753
|
-
while (true) {
|
754
|
-
const { done, value } = await reader.read();
|
755
|
-
if (done) {
|
756
|
-
break;
|
757
|
-
}
|
758
|
-
controller.enqueue(value);
|
261
|
+
}
|
262
|
+
});
|
263
|
+
}
|
264
|
+
function isOfTypeOpenAIStreamCallbacks(callbacks) {
|
265
|
+
return "experimental_onFunctionCall" in callbacks;
|
266
|
+
}
|
267
|
+
function trimStartOfStreamHelper() {
|
268
|
+
let isStreamStart = true;
|
269
|
+
return (text) => {
|
270
|
+
if (isStreamStart) {
|
271
|
+
text = text.trimStart();
|
272
|
+
if (text)
|
273
|
+
isStreamStart = false;
|
274
|
+
}
|
275
|
+
return text;
|
276
|
+
};
|
277
|
+
}
|
278
|
+
function AIStream(response, customParser, callbacks) {
|
279
|
+
if (!response.ok) {
|
280
|
+
if (response.body) {
|
281
|
+
const reader = response.body.getReader();
|
282
|
+
return new ReadableStream({
|
283
|
+
async start(controller) {
|
284
|
+
const { done, value } = await reader.read();
|
285
|
+
if (!done) {
|
286
|
+
const errorText = new TextDecoder().decode(value);
|
287
|
+
controller.error(new Error(`Response error: ${errorText}`));
|
759
288
|
}
|
760
289
|
}
|
761
|
-
}
|
762
|
-
|
763
|
-
|
290
|
+
});
|
291
|
+
} else {
|
292
|
+
return new ReadableStream({
|
293
|
+
start(controller) {
|
294
|
+
controller.error(new Error("Response error: No response body"));
|
764
295
|
}
|
765
|
-
}
|
296
|
+
});
|
297
|
+
}
|
298
|
+
}
|
299
|
+
const responseBodyStream = response.body || createEmptyReadableStream();
|
300
|
+
return responseBodyStream.pipeThrough(createEventStreamTransformer(customParser)).pipeThrough(createCallbacksTransformer(callbacks));
|
301
|
+
}
|
302
|
+
function createEmptyReadableStream() {
|
303
|
+
return new ReadableStream({
|
304
|
+
start(controller) {
|
305
|
+
controller.close();
|
306
|
+
}
|
307
|
+
});
|
308
|
+
}
|
309
|
+
function readableFromAsyncIterable(iterable) {
|
310
|
+
let it = iterable[Symbol.asyncIterator]();
|
311
|
+
return new ReadableStream({
|
312
|
+
async pull(controller) {
|
313
|
+
const { done, value } = await it.next();
|
314
|
+
if (done)
|
315
|
+
controller.close();
|
316
|
+
else
|
317
|
+
controller.enqueue(value);
|
318
|
+
},
|
319
|
+
async cancel(reason) {
|
320
|
+
var _a;
|
321
|
+
await ((_a = it.return) == null ? void 0 : _a.call(it, reason));
|
766
322
|
}
|
767
323
|
});
|
768
324
|
}
|
769
325
|
|
770
|
-
// streams/
|
771
|
-
var
|
772
|
-
constructor(
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
326
|
+
// streams/stream-data.ts
|
327
|
+
var experimental_StreamData = class {
|
328
|
+
constructor() {
|
329
|
+
this.encoder = new TextEncoder();
|
330
|
+
this.controller = null;
|
331
|
+
// closing the stream is synchronous, but we want to return a promise
|
332
|
+
// in case we're doing async work
|
333
|
+
this.isClosedPromise = null;
|
334
|
+
this.isClosedPromiseResolver = void 0;
|
335
|
+
this.isClosed = false;
|
336
|
+
// array to store appended data
|
337
|
+
this.data = [];
|
338
|
+
this.messageAnnotations = [];
|
339
|
+
this.isClosedPromise = new Promise((resolve) => {
|
340
|
+
this.isClosedPromiseResolver = resolve;
|
341
|
+
});
|
342
|
+
const self = this;
|
343
|
+
this.stream = new TransformStream({
|
344
|
+
start: async (controller) => {
|
345
|
+
self.controller = controller;
|
346
|
+
},
|
347
|
+
transform: async (chunk, controller) => {
|
348
|
+
if (self.data.length > 0) {
|
349
|
+
const encodedData = self.encoder.encode(
|
350
|
+
formatStreamPart("data", self.data)
|
351
|
+
);
|
352
|
+
self.data = [];
|
353
|
+
controller.enqueue(encodedData);
|
354
|
+
}
|
355
|
+
if (self.messageAnnotations.length) {
|
356
|
+
const encodedMessageAnnotations = self.encoder.encode(
|
357
|
+
formatStreamPart("message_annotations", self.messageAnnotations)
|
358
|
+
);
|
359
|
+
self.messageAnnotations = [];
|
360
|
+
controller.enqueue(encodedMessageAnnotations);
|
361
|
+
}
|
362
|
+
controller.enqueue(chunk);
|
363
|
+
},
|
364
|
+
async flush(controller) {
|
365
|
+
const warningTimeout = process.env.NODE_ENV === "development" ? setTimeout(() => {
|
366
|
+
console.warn(
|
367
|
+
"The data stream is hanging. Did you forget to close it with `data.close()`?"
|
368
|
+
);
|
369
|
+
}, 3e3) : null;
|
370
|
+
await self.isClosedPromise;
|
371
|
+
if (warningTimeout !== null) {
|
372
|
+
clearTimeout(warningTimeout);
|
373
|
+
}
|
374
|
+
if (self.data.length) {
|
375
|
+
const encodedData = self.encoder.encode(
|
376
|
+
formatStreamPart("data", self.data)
|
377
|
+
);
|
378
|
+
controller.enqueue(encodedData);
|
379
|
+
}
|
380
|
+
if (self.messageAnnotations.length) {
|
381
|
+
const encodedData = self.encoder.encode(
|
382
|
+
formatStreamPart("message_annotations", self.messageAnnotations)
|
383
|
+
);
|
384
|
+
controller.enqueue(encodedData);
|
385
|
+
}
|
784
386
|
}
|
785
387
|
});
|
786
388
|
}
|
389
|
+
async close() {
|
390
|
+
var _a;
|
391
|
+
if (this.isClosed) {
|
392
|
+
throw new Error("Data Stream has already been closed.");
|
393
|
+
}
|
394
|
+
if (!this.controller) {
|
395
|
+
throw new Error("Stream controller is not initialized.");
|
396
|
+
}
|
397
|
+
(_a = this.isClosedPromiseResolver) == null ? void 0 : _a.call(this);
|
398
|
+
this.isClosed = true;
|
399
|
+
}
|
400
|
+
append(value) {
|
401
|
+
if (this.isClosed) {
|
402
|
+
throw new Error("Data Stream has already been closed.");
|
403
|
+
}
|
404
|
+
this.data.push(value);
|
405
|
+
}
|
406
|
+
appendMessageAnnotation(value) {
|
407
|
+
if (this.isClosed) {
|
408
|
+
throw new Error("Data Stream has already been closed.");
|
409
|
+
}
|
410
|
+
this.messageAnnotations.push(value);
|
411
|
+
}
|
787
412
|
};
|
788
|
-
function
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
const reader = res.getReader();
|
794
|
-
function read() {
|
795
|
-
reader.read().then(({ done, value }) => {
|
796
|
-
if (done) {
|
797
|
-
response.end();
|
798
|
-
return;
|
413
|
+
function createStreamDataTransformer(experimental_streamData) {
|
414
|
+
if (!experimental_streamData) {
|
415
|
+
return new TransformStream({
|
416
|
+
transform: async (chunk, controller) => {
|
417
|
+
controller.enqueue(chunk);
|
799
418
|
}
|
800
|
-
response.write(value);
|
801
|
-
read();
|
802
419
|
});
|
803
420
|
}
|
804
|
-
|
421
|
+
const encoder = new TextEncoder();
|
422
|
+
const decoder = new TextDecoder();
|
423
|
+
return new TransformStream({
|
424
|
+
transform: async (chunk, controller) => {
|
425
|
+
const message = decoder.decode(chunk);
|
426
|
+
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
427
|
+
}
|
428
|
+
});
|
805
429
|
}
|
806
430
|
|
807
|
-
// streams/
|
808
|
-
function
|
809
|
-
|
810
|
-
return
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
431
|
+
// streams/anthropic-stream.ts
|
432
|
+
function parseAnthropicStream() {
|
433
|
+
let previous = "";
|
434
|
+
return (data) => {
|
435
|
+
const json = JSON.parse(data);
|
436
|
+
if ("error" in json) {
|
437
|
+
throw new Error(`${json.error.type}: ${json.error.message}`);
|
438
|
+
}
|
439
|
+
if (!("completion" in json)) {
|
440
|
+
return;
|
441
|
+
}
|
442
|
+
const text = json.completion;
|
443
|
+
if (!previous || text.length > previous.length && text.startsWith(previous)) {
|
444
|
+
const delta = text.slice(previous.length);
|
445
|
+
previous = text;
|
446
|
+
return delta;
|
447
|
+
}
|
448
|
+
return text;
|
449
|
+
};
|
450
|
+
}
|
451
|
+
async function* streamable(stream) {
|
452
|
+
for await (const chunk of stream) {
|
453
|
+
if ("completion" in chunk) {
|
454
|
+
const text = chunk.completion;
|
455
|
+
if (text)
|
456
|
+
yield text;
|
457
|
+
} else if ("delta" in chunk) {
|
458
|
+
const { delta } = chunk;
|
459
|
+
if ("text" in delta) {
|
460
|
+
const text = delta.text;
|
461
|
+
if (text)
|
462
|
+
yield text;
|
817
463
|
}
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
464
|
+
}
|
465
|
+
}
|
466
|
+
}
|
467
|
+
function AnthropicStream(res, cb) {
|
468
|
+
if (Symbol.asyncIterator in res) {
|
469
|
+
return readableFromAsyncIterable(streamable(res)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
|
470
|
+
} else {
|
471
|
+
return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
|
472
|
+
createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData)
|
473
|
+
);
|
474
|
+
}
|
475
|
+
}
|
476
|
+
|
477
|
+
// streams/assistant-response.ts
|
478
|
+
function experimental_AssistantResponse({ threadId, messageId }, process2) {
|
479
|
+
const stream = new ReadableStream({
|
480
|
+
async start(controller) {
|
481
|
+
var _a;
|
482
|
+
const textEncoder = new TextEncoder();
|
483
|
+
const sendMessage = (message) => {
|
484
|
+
controller.enqueue(
|
485
|
+
textEncoder.encode(formatStreamPart("assistant_message", message))
|
486
|
+
);
|
487
|
+
};
|
488
|
+
const sendDataMessage = (message) => {
|
489
|
+
controller.enqueue(
|
490
|
+
textEncoder.encode(formatStreamPart("data_message", message))
|
491
|
+
);
|
492
|
+
};
|
493
|
+
const sendError = (errorMessage) => {
|
494
|
+
controller.enqueue(
|
495
|
+
textEncoder.encode(formatStreamPart("error", errorMessage))
|
496
|
+
);
|
497
|
+
};
|
498
|
+
controller.enqueue(
|
499
|
+
textEncoder.encode(
|
500
|
+
formatStreamPart("assistant_control_data", {
|
501
|
+
threadId,
|
502
|
+
messageId
|
503
|
+
})
|
504
|
+
)
|
505
|
+
);
|
506
|
+
try {
|
507
|
+
await process2({
|
508
|
+
threadId,
|
509
|
+
messageId,
|
510
|
+
sendMessage,
|
511
|
+
sendDataMessage
|
512
|
+
});
|
513
|
+
} catch (error) {
|
514
|
+
sendError((_a = error.message) != null ? _a : `${error}`);
|
515
|
+
} finally {
|
516
|
+
controller.close();
|
823
517
|
}
|
824
|
-
|
825
|
-
|
518
|
+
},
|
519
|
+
pull(controller) {
|
520
|
+
},
|
521
|
+
cancel() {
|
522
|
+
}
|
523
|
+
});
|
524
|
+
return new Response(stream, {
|
525
|
+
status: 200,
|
526
|
+
headers: {
|
527
|
+
"Content-Type": "text/plain; charset=utf-8"
|
528
|
+
}
|
529
|
+
});
|
530
|
+
}
|
531
|
+
|
532
|
+
// streams/aws-bedrock-stream.ts
|
533
|
+
async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
|
534
|
+
var _a, _b;
|
535
|
+
const decoder = new TextDecoder();
|
536
|
+
for await (const chunk of (_a = response.body) != null ? _a : []) {
|
537
|
+
const bytes = (_b = chunk.chunk) == null ? void 0 : _b.bytes;
|
538
|
+
if (bytes != null) {
|
539
|
+
const chunkText = decoder.decode(bytes);
|
540
|
+
const chunkJSON = JSON.parse(chunkText);
|
541
|
+
const delta = extractTextDeltaFromChunk(chunkJSON);
|
542
|
+
if (delta != null) {
|
543
|
+
yield delta;
|
826
544
|
}
|
827
|
-
controller.enqueue(text);
|
828
545
|
}
|
829
|
-
}
|
546
|
+
}
|
830
547
|
}
|
831
|
-
function
|
832
|
-
return
|
548
|
+
function AWSBedrockAnthropicStream(response, callbacks) {
|
549
|
+
return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
|
550
|
+
}
|
551
|
+
function AWSBedrockCohereStream(response, callbacks) {
|
552
|
+
return AWSBedrockStream(
|
553
|
+
response,
|
554
|
+
callbacks,
|
555
|
+
// As of 2023-11-17, Bedrock does not support streaming for Cohere,
|
556
|
+
// so we take the full generation:
|
557
|
+
(chunk) => {
|
558
|
+
var _a, _b;
|
559
|
+
return (_b = (_a = chunk.generations) == null ? void 0 : _a[0]) == null ? void 0 : _b.text;
|
560
|
+
}
|
561
|
+
);
|
562
|
+
}
|
563
|
+
function AWSBedrockLlama2Stream(response, callbacks) {
|
564
|
+
return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
|
565
|
+
}
|
566
|
+
function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
|
567
|
+
return readableFromAsyncIterable(
|
568
|
+
asDeltaIterable(response, extractTextDeltaFromChunk)
|
569
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
833
570
|
createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
|
834
571
|
);
|
835
572
|
}
|
@@ -862,7 +599,7 @@ async function readAndProcessLines(reader, controller) {
|
|
862
599
|
}
|
863
600
|
controller.close();
|
864
601
|
}
|
865
|
-
function
|
602
|
+
function createParser2(res) {
|
866
603
|
var _a;
|
867
604
|
const reader = (_a = res.body) == null ? void 0 : _a.getReader();
|
868
605
|
return new ReadableStream({
|
@@ -890,56 +627,58 @@ function CohereStream(reader, callbacks) {
|
|
890
627
|
createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
|
891
628
|
);
|
892
629
|
} else {
|
893
|
-
return
|
630
|
+
return createParser2(reader).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
894
631
|
createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
|
895
632
|
);
|
896
633
|
}
|
897
634
|
}
|
898
635
|
|
899
|
-
// streams/
|
900
|
-
function
|
901
|
-
|
902
|
-
|
903
|
-
const
|
904
|
-
if (
|
905
|
-
|
906
|
-
}
|
907
|
-
if (!("completion" in json)) {
|
908
|
-
return;
|
636
|
+
// streams/google-generative-ai-stream.ts
|
637
|
+
async function* streamable3(response) {
|
638
|
+
var _a, _b, _c;
|
639
|
+
for await (const chunk of response.stream) {
|
640
|
+
const parts = (_c = (_b = (_a = chunk.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
|
641
|
+
if (parts === void 0) {
|
642
|
+
continue;
|
909
643
|
}
|
910
|
-
const
|
911
|
-
if (
|
912
|
-
|
913
|
-
previous = text;
|
914
|
-
return delta;
|
644
|
+
const firstPart = parts[0];
|
645
|
+
if (typeof firstPart.text === "string") {
|
646
|
+
yield firstPart.text;
|
915
647
|
}
|
916
|
-
|
917
|
-
};
|
648
|
+
}
|
918
649
|
}
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
650
|
+
function GoogleGenerativeAIStream(response, cb) {
|
651
|
+
return readableFromAsyncIterable(streamable3(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
|
652
|
+
}
|
653
|
+
|
654
|
+
// streams/huggingface-stream.ts
|
655
|
+
function createParser3(res) {
|
656
|
+
const trimStartOfStream = trimStartOfStreamHelper();
|
657
|
+
return new ReadableStream({
|
658
|
+
async pull(controller) {
|
659
|
+
var _a, _b;
|
660
|
+
const { value, done } = await res.next();
|
661
|
+
if (done) {
|
662
|
+
controller.close();
|
663
|
+
return;
|
664
|
+
}
|
665
|
+
const text = trimStartOfStream((_b = (_a = value.token) == null ? void 0 : _a.text) != null ? _b : "");
|
666
|
+
if (!text)
|
667
|
+
return;
|
668
|
+
if (value.generated_text != null && value.generated_text.length > 0) {
|
669
|
+
return;
|
670
|
+
}
|
671
|
+
if (text === "</s>" || text === "<|endoftext|>" || text === "<|end|>") {
|
672
|
+
return;
|
931
673
|
}
|
674
|
+
controller.enqueue(text);
|
932
675
|
}
|
933
|
-
}
|
676
|
+
});
|
934
677
|
}
|
935
|
-
function
|
936
|
-
|
937
|
-
|
938
|
-
|
939
|
-
return AIStream(res, parseAnthropicStream(), cb).pipeThrough(
|
940
|
-
createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData)
|
941
|
-
);
|
942
|
-
}
|
678
|
+
function HuggingFaceStream(res, callbacks) {
|
679
|
+
return createParser3(res).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
|
680
|
+
createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
|
681
|
+
);
|
943
682
|
}
|
944
683
|
|
945
684
|
// streams/inkeep-stream.ts
|
@@ -1038,7 +777,307 @@ function LangChainStream(callbacks) {
|
|
1038
777
|
await handleError(e, runId);
|
1039
778
|
}
|
1040
779
|
}
|
1041
|
-
};
|
780
|
+
};
|
781
|
+
}
|
782
|
+
|
783
|
+
// streams/openai-stream.ts
|
784
|
+
function parseOpenAIStream() {
|
785
|
+
const extract = chunkToText();
|
786
|
+
return (data) => extract(JSON.parse(data));
|
787
|
+
}
|
788
|
+
async function* streamable4(stream) {
|
789
|
+
const extract = chunkToText();
|
790
|
+
for await (let chunk of stream) {
|
791
|
+
if ("promptFilterResults" in chunk) {
|
792
|
+
chunk = {
|
793
|
+
id: chunk.id,
|
794
|
+
created: chunk.created.getDate(),
|
795
|
+
object: chunk.object,
|
796
|
+
// not exposed by Azure API
|
797
|
+
model: chunk.model,
|
798
|
+
// not exposed by Azure API
|
799
|
+
choices: chunk.choices.map((choice) => {
|
800
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
801
|
+
return {
|
802
|
+
delta: {
|
803
|
+
content: (_a = choice.delta) == null ? void 0 : _a.content,
|
804
|
+
function_call: (_b = choice.delta) == null ? void 0 : _b.functionCall,
|
805
|
+
role: (_c = choice.delta) == null ? void 0 : _c.role,
|
806
|
+
tool_calls: ((_e = (_d = choice.delta) == null ? void 0 : _d.toolCalls) == null ? void 0 : _e.length) ? (_g = (_f = choice.delta) == null ? void 0 : _f.toolCalls) == null ? void 0 : _g.map((toolCall, index) => ({
|
807
|
+
index,
|
808
|
+
id: toolCall.id,
|
809
|
+
function: toolCall.function,
|
810
|
+
type: toolCall.type
|
811
|
+
})) : void 0
|
812
|
+
},
|
813
|
+
finish_reason: choice.finishReason,
|
814
|
+
index: choice.index
|
815
|
+
};
|
816
|
+
})
|
817
|
+
};
|
818
|
+
}
|
819
|
+
const text = extract(chunk);
|
820
|
+
if (text)
|
821
|
+
yield text;
|
822
|
+
}
|
823
|
+
}
|
824
|
+
function chunkToText() {
|
825
|
+
const trimStartOfStream = trimStartOfStreamHelper();
|
826
|
+
let isFunctionStreamingIn;
|
827
|
+
return (json) => {
|
828
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
|
829
|
+
if (isChatCompletionChunk(json)) {
|
830
|
+
const delta = (_a = json.choices[0]) == null ? void 0 : _a.delta;
|
831
|
+
if ((_b = delta.function_call) == null ? void 0 : _b.name) {
|
832
|
+
isFunctionStreamingIn = true;
|
833
|
+
return `{"function_call": {"name": "${delta.function_call.name}", "arguments": "`;
|
834
|
+
} else if ((_e = (_d = (_c = delta.tool_calls) == null ? void 0 : _c[0]) == null ? void 0 : _d.function) == null ? void 0 : _e.name) {
|
835
|
+
isFunctionStreamingIn = true;
|
836
|
+
const toolCall = delta.tool_calls[0];
|
837
|
+
if (toolCall.index === 0) {
|
838
|
+
return `{"tool_calls":[ {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_f = toolCall.function) == null ? void 0 : _f.name}", "arguments": "`;
|
839
|
+
} else {
|
840
|
+
return `"}}, {"id": "${toolCall.id}", "type": "function", "function": {"name": "${(_g = toolCall.function) == null ? void 0 : _g.name}", "arguments": "`;
|
841
|
+
}
|
842
|
+
} else if ((_h = delta.function_call) == null ? void 0 : _h.arguments) {
|
843
|
+
return cleanupArguments((_i = delta.function_call) == null ? void 0 : _i.arguments);
|
844
|
+
} else if ((_k = (_j = delta.tool_calls) == null ? void 0 : _j[0].function) == null ? void 0 : _k.arguments) {
|
845
|
+
return cleanupArguments((_n = (_m = (_l = delta.tool_calls) == null ? void 0 : _l[0]) == null ? void 0 : _m.function) == null ? void 0 : _n.arguments);
|
846
|
+
} else if (isFunctionStreamingIn && (((_o = json.choices[0]) == null ? void 0 : _o.finish_reason) === "function_call" || ((_p = json.choices[0]) == null ? void 0 : _p.finish_reason) === "stop")) {
|
847
|
+
isFunctionStreamingIn = false;
|
848
|
+
return '"}}';
|
849
|
+
} else if (isFunctionStreamingIn && ((_q = json.choices[0]) == null ? void 0 : _q.finish_reason) === "tool_calls") {
|
850
|
+
isFunctionStreamingIn = false;
|
851
|
+
return '"}}]}';
|
852
|
+
}
|
853
|
+
}
|
854
|
+
const text = trimStartOfStream(
|
855
|
+
isChatCompletionChunk(json) && json.choices[0].delta.content ? json.choices[0].delta.content : isCompletion(json) ? json.choices[0].text : ""
|
856
|
+
);
|
857
|
+
return text;
|
858
|
+
};
|
859
|
+
function cleanupArguments(argumentChunk) {
|
860
|
+
let escapedPartialJson = argumentChunk.replace(/\\/g, "\\\\").replace(/\//g, "\\/").replace(/"/g, '\\"').replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t").replace(/\f/g, "\\f");
|
861
|
+
return `${escapedPartialJson}`;
|
862
|
+
}
|
863
|
+
}
|
864
|
+
var __internal__OpenAIFnMessagesSymbol = Symbol(
|
865
|
+
"internal_openai_fn_messages"
|
866
|
+
);
|
867
|
+
function isChatCompletionChunk(data) {
|
868
|
+
return "choices" in data && data.choices && data.choices[0] && "delta" in data.choices[0];
|
869
|
+
}
|
870
|
+
function isCompletion(data) {
|
871
|
+
return "choices" in data && data.choices && data.choices[0] && "text" in data.choices[0];
|
872
|
+
}
|
873
|
+
function OpenAIStream(res, callbacks) {
|
874
|
+
const cb = callbacks;
|
875
|
+
let stream;
|
876
|
+
if (Symbol.asyncIterator in res) {
|
877
|
+
stream = readableFromAsyncIterable(streamable4(res)).pipeThrough(
|
878
|
+
createCallbacksTransformer(
|
879
|
+
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
880
|
+
...cb,
|
881
|
+
onFinal: void 0
|
882
|
+
} : {
|
883
|
+
...cb
|
884
|
+
}
|
885
|
+
)
|
886
|
+
);
|
887
|
+
} else {
|
888
|
+
stream = AIStream(
|
889
|
+
res,
|
890
|
+
parseOpenAIStream(),
|
891
|
+
(cb == null ? void 0 : cb.experimental_onFunctionCall) || (cb == null ? void 0 : cb.experimental_onToolCall) ? {
|
892
|
+
...cb,
|
893
|
+
onFinal: void 0
|
894
|
+
} : {
|
895
|
+
...cb
|
896
|
+
}
|
897
|
+
);
|
898
|
+
}
|
899
|
+
if (cb && (cb.experimental_onFunctionCall || cb.experimental_onToolCall)) {
|
900
|
+
const functionCallTransformer = createFunctionCallTransformer(cb);
|
901
|
+
return stream.pipeThrough(functionCallTransformer);
|
902
|
+
} else {
|
903
|
+
return stream.pipeThrough(
|
904
|
+
createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData)
|
905
|
+
);
|
906
|
+
}
|
907
|
+
}
|
908
|
+
function createFunctionCallTransformer(callbacks) {
|
909
|
+
const textEncoder = new TextEncoder();
|
910
|
+
let isFirstChunk = true;
|
911
|
+
let aggregatedResponse = "";
|
912
|
+
let aggregatedFinalCompletionResponse = "";
|
913
|
+
let isFunctionStreamingIn = false;
|
914
|
+
let functionCallMessages = callbacks[__internal__OpenAIFnMessagesSymbol] || [];
|
915
|
+
const isComplexMode = callbacks == null ? void 0 : callbacks.experimental_streamData;
|
916
|
+
const decode = createChunkDecoder();
|
917
|
+
return new TransformStream({
|
918
|
+
async transform(chunk, controller) {
|
919
|
+
const message = decode(chunk);
|
920
|
+
aggregatedFinalCompletionResponse += message;
|
921
|
+
const shouldHandleAsFunction = isFirstChunk && (message.startsWith('{"function_call":') || message.startsWith('{"tool_calls":'));
|
922
|
+
if (shouldHandleAsFunction) {
|
923
|
+
isFunctionStreamingIn = true;
|
924
|
+
aggregatedResponse += message;
|
925
|
+
isFirstChunk = false;
|
926
|
+
return;
|
927
|
+
}
|
928
|
+
if (!isFunctionStreamingIn) {
|
929
|
+
controller.enqueue(
|
930
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", message)) : chunk
|
931
|
+
);
|
932
|
+
return;
|
933
|
+
} else {
|
934
|
+
aggregatedResponse += message;
|
935
|
+
}
|
936
|
+
},
|
937
|
+
async flush(controller) {
|
938
|
+
try {
|
939
|
+
if (!isFirstChunk && isFunctionStreamingIn && (callbacks.experimental_onFunctionCall || callbacks.experimental_onToolCall)) {
|
940
|
+
isFunctionStreamingIn = false;
|
941
|
+
const payload = JSON.parse(aggregatedResponse);
|
942
|
+
let newFunctionCallMessages = [
|
943
|
+
...functionCallMessages
|
944
|
+
];
|
945
|
+
let functionResponse = void 0;
|
946
|
+
if (callbacks.experimental_onFunctionCall) {
|
947
|
+
if (payload.function_call === void 0) {
|
948
|
+
console.warn(
|
949
|
+
"experimental_onFunctionCall should not be defined when using tools"
|
950
|
+
);
|
951
|
+
}
|
952
|
+
const argumentsPayload = JSON.parse(
|
953
|
+
payload.function_call.arguments
|
954
|
+
);
|
955
|
+
functionResponse = await callbacks.experimental_onFunctionCall(
|
956
|
+
{
|
957
|
+
name: payload.function_call.name,
|
958
|
+
arguments: argumentsPayload
|
959
|
+
},
|
960
|
+
(result) => {
|
961
|
+
newFunctionCallMessages = [
|
962
|
+
...functionCallMessages,
|
963
|
+
{
|
964
|
+
role: "assistant",
|
965
|
+
content: "",
|
966
|
+
function_call: payload.function_call
|
967
|
+
},
|
968
|
+
{
|
969
|
+
role: "function",
|
970
|
+
name: payload.function_call.name,
|
971
|
+
content: JSON.stringify(result)
|
972
|
+
}
|
973
|
+
];
|
974
|
+
return newFunctionCallMessages;
|
975
|
+
}
|
976
|
+
);
|
977
|
+
}
|
978
|
+
if (callbacks.experimental_onToolCall) {
|
979
|
+
const toolCalls = {
|
980
|
+
tools: []
|
981
|
+
};
|
982
|
+
for (const tool of payload.tool_calls) {
|
983
|
+
toolCalls.tools.push({
|
984
|
+
id: tool.id,
|
985
|
+
type: "function",
|
986
|
+
func: {
|
987
|
+
name: tool.function.name,
|
988
|
+
arguments: tool.function.arguments
|
989
|
+
}
|
990
|
+
});
|
991
|
+
}
|
992
|
+
let responseIndex = 0;
|
993
|
+
try {
|
994
|
+
functionResponse = await callbacks.experimental_onToolCall(
|
995
|
+
toolCalls,
|
996
|
+
(result) => {
|
997
|
+
if (result) {
|
998
|
+
const { tool_call_id, function_name, tool_call_result } = result;
|
999
|
+
newFunctionCallMessages = [
|
1000
|
+
...newFunctionCallMessages,
|
1001
|
+
// Only append the assistant message if it's the first response
|
1002
|
+
...responseIndex === 0 ? [
|
1003
|
+
{
|
1004
|
+
role: "assistant",
|
1005
|
+
content: "",
|
1006
|
+
tool_calls: payload.tool_calls.map(
|
1007
|
+
(tc) => ({
|
1008
|
+
id: tc.id,
|
1009
|
+
type: "function",
|
1010
|
+
function: {
|
1011
|
+
name: tc.function.name,
|
1012
|
+
// we send the arguments an object to the user, but as the API expects a string, we need to stringify it
|
1013
|
+
arguments: JSON.stringify(
|
1014
|
+
tc.function.arguments
|
1015
|
+
)
|
1016
|
+
}
|
1017
|
+
})
|
1018
|
+
)
|
1019
|
+
}
|
1020
|
+
] : [],
|
1021
|
+
// Append the function call result message
|
1022
|
+
{
|
1023
|
+
role: "tool",
|
1024
|
+
tool_call_id,
|
1025
|
+
name: function_name,
|
1026
|
+
content: JSON.stringify(tool_call_result)
|
1027
|
+
}
|
1028
|
+
];
|
1029
|
+
responseIndex++;
|
1030
|
+
}
|
1031
|
+
return newFunctionCallMessages;
|
1032
|
+
}
|
1033
|
+
);
|
1034
|
+
} catch (e) {
|
1035
|
+
console.error("Error calling experimental_onToolCall:", e);
|
1036
|
+
}
|
1037
|
+
}
|
1038
|
+
if (!functionResponse) {
|
1039
|
+
controller.enqueue(
|
1040
|
+
textEncoder.encode(
|
1041
|
+
isComplexMode ? formatStreamPart(
|
1042
|
+
payload.function_call ? "function_call" : "tool_calls",
|
1043
|
+
// parse to prevent double-encoding:
|
1044
|
+
JSON.parse(aggregatedResponse)
|
1045
|
+
) : aggregatedResponse
|
1046
|
+
)
|
1047
|
+
);
|
1048
|
+
return;
|
1049
|
+
} else if (typeof functionResponse === "string") {
|
1050
|
+
controller.enqueue(
|
1051
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", functionResponse)) : textEncoder.encode(functionResponse)
|
1052
|
+
);
|
1053
|
+
aggregatedFinalCompletionResponse = functionResponse;
|
1054
|
+
return;
|
1055
|
+
}
|
1056
|
+
const filteredCallbacks = {
|
1057
|
+
...callbacks,
|
1058
|
+
onStart: void 0
|
1059
|
+
};
|
1060
|
+
callbacks.onFinal = void 0;
|
1061
|
+
const openAIStream = OpenAIStream(functionResponse, {
|
1062
|
+
...filteredCallbacks,
|
1063
|
+
[__internal__OpenAIFnMessagesSymbol]: newFunctionCallMessages
|
1064
|
+
});
|
1065
|
+
const reader = openAIStream.getReader();
|
1066
|
+
while (true) {
|
1067
|
+
const { done, value } = await reader.read();
|
1068
|
+
if (done) {
|
1069
|
+
break;
|
1070
|
+
}
|
1071
|
+
controller.enqueue(value);
|
1072
|
+
}
|
1073
|
+
}
|
1074
|
+
} finally {
|
1075
|
+
if (callbacks.onFinal && aggregatedFinalCompletionResponse) {
|
1076
|
+
await callbacks.onFinal(aggregatedFinalCompletionResponse);
|
1077
|
+
}
|
1078
|
+
}
|
1079
|
+
}
|
1080
|
+
});
|
1042
1081
|
}
|
1043
1082
|
|
1044
1083
|
// streams/replicate-stream.ts
|
@@ -1063,79 +1102,6 @@ async function ReplicateStream(res, cb, options) {
|
|
1063
1102
|
);
|
1064
1103
|
}
|
1065
1104
|
|
1066
|
-
// streams/assistant-response.ts
|
1067
|
-
function experimental_AssistantResponse({ threadId, messageId }, process2) {
|
1068
|
-
const stream = new ReadableStream({
|
1069
|
-
async start(controller) {
|
1070
|
-
var _a;
|
1071
|
-
const textEncoder = new TextEncoder();
|
1072
|
-
const sendMessage = (message) => {
|
1073
|
-
controller.enqueue(
|
1074
|
-
textEncoder.encode(formatStreamPart("assistant_message", message))
|
1075
|
-
);
|
1076
|
-
};
|
1077
|
-
const sendDataMessage = (message) => {
|
1078
|
-
controller.enqueue(
|
1079
|
-
textEncoder.encode(formatStreamPart("data_message", message))
|
1080
|
-
);
|
1081
|
-
};
|
1082
|
-
const sendError = (errorMessage) => {
|
1083
|
-
controller.enqueue(
|
1084
|
-
textEncoder.encode(formatStreamPart("error", errorMessage))
|
1085
|
-
);
|
1086
|
-
};
|
1087
|
-
controller.enqueue(
|
1088
|
-
textEncoder.encode(
|
1089
|
-
formatStreamPart("assistant_control_data", {
|
1090
|
-
threadId,
|
1091
|
-
messageId
|
1092
|
-
})
|
1093
|
-
)
|
1094
|
-
);
|
1095
|
-
try {
|
1096
|
-
await process2({
|
1097
|
-
threadId,
|
1098
|
-
messageId,
|
1099
|
-
sendMessage,
|
1100
|
-
sendDataMessage
|
1101
|
-
});
|
1102
|
-
} catch (error) {
|
1103
|
-
sendError((_a = error.message) != null ? _a : `${error}`);
|
1104
|
-
} finally {
|
1105
|
-
controller.close();
|
1106
|
-
}
|
1107
|
-
},
|
1108
|
-
pull(controller) {
|
1109
|
-
},
|
1110
|
-
cancel() {
|
1111
|
-
}
|
1112
|
-
});
|
1113
|
-
return new Response(stream, {
|
1114
|
-
status: 200,
|
1115
|
-
headers: {
|
1116
|
-
"Content-Type": "text/plain; charset=utf-8"
|
1117
|
-
}
|
1118
|
-
});
|
1119
|
-
}
|
1120
|
-
|
1121
|
-
// streams/google-generative-ai-stream.ts
|
1122
|
-
async function* streamable4(response) {
|
1123
|
-
var _a, _b, _c;
|
1124
|
-
for await (const chunk of response.stream) {
|
1125
|
-
const parts = (_c = (_b = (_a = chunk.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts;
|
1126
|
-
if (parts === void 0) {
|
1127
|
-
continue;
|
1128
|
-
}
|
1129
|
-
const firstPart = parts[0];
|
1130
|
-
if (typeof firstPart.text === "string") {
|
1131
|
-
yield firstPart.text;
|
1132
|
-
}
|
1133
|
-
}
|
1134
|
-
}
|
1135
|
-
function GoogleGenerativeAIStream(response, cb) {
|
1136
|
-
return readableFromAsyncIterable(streamable4(response)).pipeThrough(createCallbacksTransformer(cb)).pipeThrough(createStreamDataTransformer(cb == null ? void 0 : cb.experimental_streamData));
|
1137
|
-
}
|
1138
|
-
|
1139
1105
|
// shared/read-data-stream.ts
|
1140
1106
|
var NEWLINE = "\n".charCodeAt(0);
|
1141
1107
|
function concatChunks(chunks, totalLength) {
|
@@ -1364,6 +1330,43 @@ var experimental_StreamingReactResponse = class {
|
|
1364
1330
|
return next;
|
1365
1331
|
}
|
1366
1332
|
};
|
1333
|
+
|
1334
|
+
// streams/streaming-text-response.ts
|
1335
|
+
var StreamingTextResponse = class extends Response {
|
1336
|
+
constructor(res, init, data) {
|
1337
|
+
let processedStream = res;
|
1338
|
+
if (data) {
|
1339
|
+
processedStream = res.pipeThrough(data.stream);
|
1340
|
+
}
|
1341
|
+
super(processedStream, {
|
1342
|
+
...init,
|
1343
|
+
status: 200,
|
1344
|
+
headers: {
|
1345
|
+
"Content-Type": "text/plain; charset=utf-8",
|
1346
|
+
[COMPLEX_HEADER]: data ? "true" : "false",
|
1347
|
+
...init == null ? void 0 : init.headers
|
1348
|
+
}
|
1349
|
+
});
|
1350
|
+
}
|
1351
|
+
};
|
1352
|
+
function streamToResponse(res, response, init) {
|
1353
|
+
response.writeHead((init == null ? void 0 : init.status) || 200, {
|
1354
|
+
"Content-Type": "text/plain; charset=utf-8",
|
1355
|
+
...init == null ? void 0 : init.headers
|
1356
|
+
});
|
1357
|
+
const reader = res.getReader();
|
1358
|
+
function read() {
|
1359
|
+
reader.read().then(({ done, value }) => {
|
1360
|
+
if (done) {
|
1361
|
+
response.end();
|
1362
|
+
return;
|
1363
|
+
}
|
1364
|
+
response.write(value);
|
1365
|
+
read();
|
1366
|
+
});
|
1367
|
+
}
|
1368
|
+
read();
|
1369
|
+
}
|
1367
1370
|
export {
|
1368
1371
|
AIStream,
|
1369
1372
|
AWSBedrockAnthropicStream,
|