ai 2.2.22 → 2.2.24
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 +59 -25
- package/dist/index.js +318 -39
- package/dist/index.mjs +317 -36
- package/package.json +10 -6
- package/react/dist/index.d.ts +40 -2
- package/react/dist/index.js +445 -195
- package/react/dist/index.mjs +444 -195
- package/solid/dist/index.d.ts +7 -1
- package/solid/dist/index.js +426 -107
- package/solid/dist/index.mjs +426 -107
- package/svelte/dist/index.d.ts +2 -0
- package/svelte/dist/index.js +124 -25
- package/svelte/dist/index.mjs +124 -25
- package/vue/dist/index.d.ts +1 -0
- package/vue/dist/index.js +124 -25
- package/vue/dist/index.mjs +124 -25
package/dist/index.d.ts
CHANGED
@@ -64,6 +64,7 @@ type ChatRequest = {
|
|
64
64
|
options?: RequestOptions;
|
65
65
|
functions?: Array<Function>;
|
66
66
|
function_call?: FunctionCall;
|
67
|
+
data?: Record<string, string>;
|
67
68
|
};
|
68
69
|
type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
|
69
70
|
type RequestOptions = {
|
@@ -74,6 +75,7 @@ type ChatRequestOptions = {
|
|
74
75
|
options?: RequestOptions;
|
75
76
|
functions?: Array<Function>;
|
76
77
|
function_call?: FunctionCall;
|
78
|
+
data?: Record<string, string>;
|
77
79
|
};
|
78
80
|
type UseChatOptions = {
|
79
81
|
/**
|
@@ -202,6 +204,16 @@ type UseCompletionOptions = {
|
|
202
204
|
type JSONValue = null | string | number | boolean | {
|
203
205
|
[x: string]: JSONValue;
|
204
206
|
} | Array<JSONValue>;
|
207
|
+
type AssistantMessage = {
|
208
|
+
id: string;
|
209
|
+
role: 'assistant';
|
210
|
+
content: Array<{
|
211
|
+
type: 'text';
|
212
|
+
text: {
|
213
|
+
value: string;
|
214
|
+
};
|
215
|
+
}>;
|
216
|
+
};
|
205
217
|
|
206
218
|
type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
|
207
219
|
/**
|
@@ -578,18 +590,26 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
|
|
578
590
|
headers?: Record<string, string>;
|
579
591
|
}): Promise<ReadableStream>;
|
580
592
|
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
}
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
+
interface StreamPart<CODE extends string, NAME extends string, TYPE> {
|
594
|
+
code: CODE;
|
595
|
+
name: NAME;
|
596
|
+
parse: (value: JSONValue) => {
|
597
|
+
type: NAME;
|
598
|
+
value: TYPE;
|
599
|
+
};
|
600
|
+
}
|
601
|
+
declare const textStreamPart: StreamPart<'0', 'text', string>;
|
602
|
+
declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
|
603
|
+
function_call: FunctionCall;
|
604
|
+
}>;
|
605
|
+
declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
|
606
|
+
declare const errorStreamPart: StreamPart<'3', 'error', string>;
|
607
|
+
declare const assistantMessage: StreamPart<'4', 'assistant_message', AssistantMessage>;
|
608
|
+
declare const assistantControlData: StreamPart<'5', 'assistant_control_data', {
|
609
|
+
threadId: string;
|
610
|
+
messageId: string;
|
611
|
+
}>;
|
612
|
+
type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessage.parse> | ReturnType<typeof assistantControlData.parse>;
|
593
613
|
/**
|
594
614
|
* The map of prefixes for data in the stream
|
595
615
|
*
|
@@ -611,20 +631,22 @@ declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | und
|
|
611
631
|
*```
|
612
632
|
*/
|
613
633
|
declare const StreamStringPrefixes: {
|
614
|
-
readonly text: 0;
|
615
|
-
readonly function_call: 1;
|
616
|
-
readonly data: 2;
|
634
|
+
readonly text: "0";
|
635
|
+
readonly function_call: "1";
|
636
|
+
readonly data: "2";
|
637
|
+
readonly error: "3";
|
638
|
+
readonly assistant_message: "4";
|
639
|
+
readonly assistant_control_data: "5";
|
617
640
|
};
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
declare
|
641
|
+
|
642
|
+
declare const nanoid: (size?: number | undefined) => string;
|
643
|
+
declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
|
644
|
+
declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
|
645
|
+
declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
|
646
|
+
declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
|
647
|
+
|
648
|
+
declare const isStreamStringEqualToType: (type: keyof typeof StreamStringPrefixes, value: string) => value is `0:${string}\n` | `1:${string}\n` | `2:${string}\n` | `3:${string}\n` | `4:${string}\n` | `5:${string}\n`;
|
623
649
|
type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
|
624
|
-
declare const getStreamStringTypeAndValue: (line: string) => {
|
625
|
-
type: keyof typeof StreamStringPrefixes;
|
626
|
-
value: string;
|
627
|
-
};
|
628
650
|
/**
|
629
651
|
* A header sent to the client so it knows how to handle parsing the stream (as a deprecated text response or using the new prefixed protocol)
|
630
652
|
*/
|
@@ -639,6 +661,7 @@ declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
|
639
661
|
* It is naive as unlike the StreamingTextResponse, it does not send the diff
|
640
662
|
* between the rows, but flushing the full payload on each row.
|
641
663
|
*/
|
664
|
+
|
642
665
|
type UINode = string | JSX.Element | JSX.Element[] | null | undefined;
|
643
666
|
type Payload = {
|
644
667
|
ui: UINode | Promise<UINode>;
|
@@ -654,8 +677,19 @@ declare class experimental_StreamingReactResponse {
|
|
654
677
|
constructor(res: ReadableStream, options?: {
|
655
678
|
ui?: (message: {
|
656
679
|
content: string;
|
680
|
+
data?: JSONValue[] | undefined;
|
657
681
|
}) => UINode | Promise<UINode>;
|
682
|
+
data?: experimental_StreamData;
|
658
683
|
});
|
659
684
|
}
|
660
685
|
|
661
|
-
|
686
|
+
declare function experimental_AssistantResponse({ threadId, messageId }: {
|
687
|
+
threadId: string;
|
688
|
+
messageId: string;
|
689
|
+
}, process: (stream: {
|
690
|
+
threadId: string;
|
691
|
+
messageId: string;
|
692
|
+
sendMessage: (message: AssistantMessage) => void;
|
693
|
+
}) => Promise<void>): Response;
|
694
|
+
|
695
|
+
export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AnthropicStream, AssistantMessage, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, FunctionCall, FunctionCallHandler, FunctionCallPayload, HuggingFaceStream, JSONValue, LangChainStream, Message, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamString, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, isStreamStringEqualToType, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
|
package/dist/index.js
CHANGED
@@ -28,16 +28,14 @@ __export(streams_exports, {
|
|
28
28
|
LangChainStream: () => LangChainStream,
|
29
29
|
OpenAIStream: () => OpenAIStream,
|
30
30
|
ReplicateStream: () => ReplicateStream,
|
31
|
-
StreamStringPrefixes: () => StreamStringPrefixes,
|
32
31
|
StreamingTextResponse: () => StreamingTextResponse,
|
33
32
|
createCallbacksTransformer: () => createCallbacksTransformer,
|
34
33
|
createChunkDecoder: () => createChunkDecoder,
|
35
34
|
createEventStreamTransformer: () => createEventStreamTransformer,
|
36
35
|
createStreamDataTransformer: () => createStreamDataTransformer,
|
36
|
+
experimental_AssistantResponse: () => experimental_AssistantResponse,
|
37
37
|
experimental_StreamData: () => experimental_StreamData,
|
38
38
|
experimental_StreamingReactResponse: () => experimental_StreamingReactResponse,
|
39
|
-
getStreamString: () => getStreamString,
|
40
|
-
getStreamStringTypeAndValue: () => getStreamStringTypeAndValue,
|
41
39
|
isStreamStringEqualToType: () => isStreamStringEqualToType,
|
42
40
|
nanoid: () => nanoid,
|
43
41
|
readableFromAsyncIterable: () => readableFromAsyncIterable,
|
@@ -162,6 +160,135 @@ function readableFromAsyncIterable(iterable) {
|
|
162
160
|
});
|
163
161
|
}
|
164
162
|
|
163
|
+
// shared/stream-parts.ts
|
164
|
+
var textStreamPart = {
|
165
|
+
code: "0",
|
166
|
+
name: "text",
|
167
|
+
parse: (value) => {
|
168
|
+
if (typeof value !== "string") {
|
169
|
+
throw new Error('"text" parts expect a string value.');
|
170
|
+
}
|
171
|
+
return { type: "text", value };
|
172
|
+
}
|
173
|
+
};
|
174
|
+
var functionCallStreamPart = {
|
175
|
+
code: "1",
|
176
|
+
name: "function_call",
|
177
|
+
parse: (value) => {
|
178
|
+
if (value == null || typeof value !== "object" || !("function_call" in value) || typeof value.function_call !== "object" || value.function_call == null || !("name" in value.function_call) || !("arguments" in value.function_call) || typeof value.function_call.name !== "string" || typeof value.function_call.arguments !== "string") {
|
179
|
+
throw new Error(
|
180
|
+
'"function_call" parts expect an object with a "function_call" property.'
|
181
|
+
);
|
182
|
+
}
|
183
|
+
return {
|
184
|
+
type: "function_call",
|
185
|
+
value
|
186
|
+
};
|
187
|
+
}
|
188
|
+
};
|
189
|
+
var dataStreamPart = {
|
190
|
+
code: "2",
|
191
|
+
name: "data",
|
192
|
+
parse: (value) => {
|
193
|
+
if (!Array.isArray(value)) {
|
194
|
+
throw new Error('"data" parts expect an array value.');
|
195
|
+
}
|
196
|
+
return { type: "data", value };
|
197
|
+
}
|
198
|
+
};
|
199
|
+
var errorStreamPart = {
|
200
|
+
code: "3",
|
201
|
+
name: "error",
|
202
|
+
parse: (value) => {
|
203
|
+
if (typeof value !== "string") {
|
204
|
+
throw new Error('"error" parts expect a string value.');
|
205
|
+
}
|
206
|
+
return { type: "error", value };
|
207
|
+
}
|
208
|
+
};
|
209
|
+
var assistantMessage = {
|
210
|
+
code: "4",
|
211
|
+
name: "assistant_message",
|
212
|
+
parse: (value) => {
|
213
|
+
if (value == null || typeof value !== "object" || !("id" in value) || !("role" in value) || !("content" in value) || typeof value.id !== "string" || typeof value.role !== "string" || value.role !== "assistant" || !Array.isArray(value.content) || !value.content.every(
|
214
|
+
(item) => item != null && typeof item === "object" && "type" in item && item.type === "text" && "text" in item && item.text != null && typeof item.text === "object" && "value" in item.text && typeof item.text.value === "string"
|
215
|
+
)) {
|
216
|
+
throw new Error(
|
217
|
+
'"assistant_message" parts expect an object with an "id", "role", and "content" property.'
|
218
|
+
);
|
219
|
+
}
|
220
|
+
return {
|
221
|
+
type: "assistant_message",
|
222
|
+
value
|
223
|
+
};
|
224
|
+
}
|
225
|
+
};
|
226
|
+
var assistantControlData = {
|
227
|
+
code: "5",
|
228
|
+
name: "assistant_control_data",
|
229
|
+
parse: (value) => {
|
230
|
+
if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
|
231
|
+
throw new Error(
|
232
|
+
'"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
|
233
|
+
);
|
234
|
+
}
|
235
|
+
return {
|
236
|
+
type: "assistant_control_data",
|
237
|
+
value: {
|
238
|
+
threadId: value.threadId,
|
239
|
+
messageId: value.messageId
|
240
|
+
}
|
241
|
+
};
|
242
|
+
}
|
243
|
+
};
|
244
|
+
var streamParts = [
|
245
|
+
textStreamPart,
|
246
|
+
functionCallStreamPart,
|
247
|
+
dataStreamPart,
|
248
|
+
errorStreamPart,
|
249
|
+
assistantMessage,
|
250
|
+
assistantControlData
|
251
|
+
];
|
252
|
+
var streamPartsByCode = {
|
253
|
+
[textStreamPart.code]: textStreamPart,
|
254
|
+
[functionCallStreamPart.code]: functionCallStreamPart,
|
255
|
+
[dataStreamPart.code]: dataStreamPart,
|
256
|
+
[errorStreamPart.code]: errorStreamPart,
|
257
|
+
[assistantMessage.code]: assistantMessage,
|
258
|
+
[assistantControlData.code]: assistantControlData
|
259
|
+
};
|
260
|
+
var StreamStringPrefixes = {
|
261
|
+
[textStreamPart.name]: textStreamPart.code,
|
262
|
+
[functionCallStreamPart.name]: functionCallStreamPart.code,
|
263
|
+
[dataStreamPart.name]: dataStreamPart.code,
|
264
|
+
[errorStreamPart.name]: errorStreamPart.code,
|
265
|
+
[assistantMessage.name]: assistantMessage.code,
|
266
|
+
[assistantControlData.name]: assistantControlData.code
|
267
|
+
};
|
268
|
+
var validCodes = streamParts.map((part) => part.code);
|
269
|
+
var parseStreamPart = (line) => {
|
270
|
+
const firstSeparatorIndex = line.indexOf(":");
|
271
|
+
if (firstSeparatorIndex === -1) {
|
272
|
+
throw new Error("Failed to parse stream string. No separator found.");
|
273
|
+
}
|
274
|
+
const prefix = line.slice(0, firstSeparatorIndex);
|
275
|
+
if (!validCodes.includes(prefix)) {
|
276
|
+
throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
|
277
|
+
}
|
278
|
+
const code = prefix;
|
279
|
+
const textValue = line.slice(firstSeparatorIndex + 1);
|
280
|
+
const jsonValue = JSON.parse(textValue);
|
281
|
+
return streamPartsByCode[code].parse(jsonValue);
|
282
|
+
};
|
283
|
+
function formatStreamPart(type, value) {
|
284
|
+
const streamPart = streamParts.find((part) => part.name === type);
|
285
|
+
if (!streamPart) {
|
286
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
287
|
+
}
|
288
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
289
|
+
`;
|
290
|
+
}
|
291
|
+
|
165
292
|
// shared/utils.ts
|
166
293
|
var import_non_secure = require("nanoid/non-secure");
|
167
294
|
var nanoid = (0, import_non_secure.customAlphabet)(
|
@@ -179,36 +306,10 @@ function createChunkDecoder(complex) {
|
|
179
306
|
}
|
180
307
|
return function(chunk) {
|
181
308
|
const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
|
182
|
-
return decoded.map(
|
309
|
+
return decoded.map(parseStreamPart).filter(Boolean);
|
183
310
|
};
|
184
311
|
}
|
185
|
-
var StreamStringPrefixes = {
|
186
|
-
text: 0,
|
187
|
-
function_call: 1,
|
188
|
-
data: 2
|
189
|
-
// user_err: 3?
|
190
|
-
};
|
191
312
|
var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
|
192
|
-
var getStreamString = (type, value) => `${StreamStringPrefixes[type]}:${JSON.stringify(value)}
|
193
|
-
`;
|
194
|
-
var getStreamStringTypeAndValue = (line) => {
|
195
|
-
const firstSeperatorIndex = line.indexOf(":");
|
196
|
-
const prefix = line.slice(0, firstSeperatorIndex);
|
197
|
-
const type = Object.keys(StreamStringPrefixes).find(
|
198
|
-
(key) => StreamStringPrefixes[key] === Number(prefix)
|
199
|
-
);
|
200
|
-
const val = line.slice(firstSeperatorIndex + 1);
|
201
|
-
let parsedVal = val;
|
202
|
-
if (!val) {
|
203
|
-
return { type, value: "" };
|
204
|
-
}
|
205
|
-
try {
|
206
|
-
parsedVal = JSON.parse(val);
|
207
|
-
} catch (e) {
|
208
|
-
console.error("Failed to parse JSON value:", val);
|
209
|
-
}
|
210
|
-
return { type, value: parsedVal };
|
211
|
-
};
|
212
313
|
var COMPLEX_HEADER = "X-Experimental-Stream-Data";
|
213
314
|
|
214
315
|
// streams/stream-data.ts
|
@@ -234,7 +335,7 @@ var experimental_StreamData = class {
|
|
234
335
|
transform: async (chunk, controller) => {
|
235
336
|
if (self.data.length > 0) {
|
236
337
|
const encodedData = self.encoder.encode(
|
237
|
-
|
338
|
+
formatStreamPart("data", self.data)
|
238
339
|
);
|
239
340
|
self.data = [];
|
240
341
|
controller.enqueue(encodedData);
|
@@ -253,7 +354,7 @@ var experimental_StreamData = class {
|
|
253
354
|
}
|
254
355
|
if (self.data.length) {
|
255
356
|
const encodedData = self.encoder.encode(
|
256
|
-
|
357
|
+
formatStreamPart("data", self.data)
|
257
358
|
);
|
258
359
|
controller.enqueue(encodedData);
|
259
360
|
}
|
@@ -291,7 +392,7 @@ function createStreamDataTransformer(experimental_streamData) {
|
|
291
392
|
return new TransformStream({
|
292
393
|
transform: async (chunk, controller) => {
|
293
394
|
const message = decoder.decode(chunk);
|
294
|
-
controller.enqueue(encoder.encode(
|
395
|
+
controller.enqueue(encoder.encode(formatStreamPart("text", message)));
|
295
396
|
}
|
296
397
|
});
|
297
398
|
}
|
@@ -399,7 +500,7 @@ function createFunctionCallTransformer(callbacks) {
|
|
399
500
|
}
|
400
501
|
if (!isFunctionStreamingIn) {
|
401
502
|
controller.enqueue(
|
402
|
-
isComplexMode ? textEncoder.encode(
|
503
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", message)) : chunk
|
403
504
|
);
|
404
505
|
return;
|
405
506
|
} else {
|
@@ -441,13 +542,17 @@ function createFunctionCallTransformer(callbacks) {
|
|
441
542
|
if (!functionResponse) {
|
442
543
|
controller.enqueue(
|
443
544
|
textEncoder.encode(
|
444
|
-
isComplexMode ?
|
545
|
+
isComplexMode ? formatStreamPart(
|
546
|
+
"function_call",
|
547
|
+
// parse to prevent double-encoding:
|
548
|
+
JSON.parse(aggregatedResponse)
|
549
|
+
) : aggregatedResponse
|
445
550
|
)
|
446
551
|
);
|
447
552
|
return;
|
448
553
|
} else if (typeof functionResponse === "string") {
|
449
554
|
controller.enqueue(
|
450
|
-
isComplexMode ? textEncoder.encode(
|
555
|
+
isComplexMode ? textEncoder.encode(formatStreamPart("text", functionResponse)) : textEncoder.encode(functionResponse)
|
451
556
|
);
|
452
557
|
return;
|
453
558
|
}
|
@@ -712,6 +817,100 @@ async function ReplicateStream(res, cb, options) {
|
|
712
817
|
);
|
713
818
|
}
|
714
819
|
|
820
|
+
// shared/parse-complex-response.ts
|
821
|
+
async function parseComplexResponse({
|
822
|
+
reader,
|
823
|
+
abortControllerRef,
|
824
|
+
update,
|
825
|
+
onFinish,
|
826
|
+
generateId = nanoid,
|
827
|
+
getCurrentDate = () => /* @__PURE__ */ new Date()
|
828
|
+
}) {
|
829
|
+
const createdAt = getCurrentDate();
|
830
|
+
const decode = createChunkDecoder(true);
|
831
|
+
const prefixMap = {
|
832
|
+
data: []
|
833
|
+
};
|
834
|
+
const NEWLINE = "\n".charCodeAt(0);
|
835
|
+
const chunks = [];
|
836
|
+
let totalLength = 0;
|
837
|
+
while (true) {
|
838
|
+
const { value } = await reader.read();
|
839
|
+
if (value) {
|
840
|
+
chunks.push(value);
|
841
|
+
totalLength += value.length;
|
842
|
+
if (value[value.length - 1] !== NEWLINE) {
|
843
|
+
continue;
|
844
|
+
}
|
845
|
+
}
|
846
|
+
if (chunks.length === 0) {
|
847
|
+
break;
|
848
|
+
}
|
849
|
+
let concatenatedChunks = new Uint8Array(totalLength);
|
850
|
+
let offset = 0;
|
851
|
+
for (const chunk of chunks) {
|
852
|
+
concatenatedChunks.set(chunk, offset);
|
853
|
+
offset += chunk.length;
|
854
|
+
}
|
855
|
+
chunks.length = 0;
|
856
|
+
totalLength = 0;
|
857
|
+
const lines = decode(concatenatedChunks);
|
858
|
+
if (typeof lines === "string") {
|
859
|
+
throw new Error(
|
860
|
+
"Invalid response format. Complex mode was set but the response is a string. This should never happen."
|
861
|
+
);
|
862
|
+
}
|
863
|
+
for (const { type, value: value2 } of lines) {
|
864
|
+
if (type === "text") {
|
865
|
+
if (prefixMap["text"]) {
|
866
|
+
prefixMap["text"] = {
|
867
|
+
...prefixMap["text"],
|
868
|
+
content: (prefixMap["text"].content || "") + value2
|
869
|
+
};
|
870
|
+
} else {
|
871
|
+
prefixMap["text"] = {
|
872
|
+
id: generateId(),
|
873
|
+
role: "assistant",
|
874
|
+
content: value2,
|
875
|
+
createdAt
|
876
|
+
};
|
877
|
+
}
|
878
|
+
}
|
879
|
+
let functionCallMessage = null;
|
880
|
+
if (type === "function_call") {
|
881
|
+
prefixMap["function_call"] = {
|
882
|
+
id: generateId(),
|
883
|
+
role: "assistant",
|
884
|
+
content: "",
|
885
|
+
function_call: value2.function_call,
|
886
|
+
name: value2.function_call.name,
|
887
|
+
createdAt
|
888
|
+
};
|
889
|
+
functionCallMessage = prefixMap["function_call"];
|
890
|
+
}
|
891
|
+
if (type === "data") {
|
892
|
+
prefixMap["data"].push(...value2);
|
893
|
+
}
|
894
|
+
const responseMessage = prefixMap["text"];
|
895
|
+
const merged = [functionCallMessage, responseMessage].filter(
|
896
|
+
Boolean
|
897
|
+
);
|
898
|
+
update(merged, [...prefixMap["data"]]);
|
899
|
+
if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
|
900
|
+
reader.cancel();
|
901
|
+
break;
|
902
|
+
}
|
903
|
+
}
|
904
|
+
}
|
905
|
+
onFinish == null ? void 0 : onFinish(prefixMap);
|
906
|
+
return {
|
907
|
+
messages: [prefixMap.text, prefixMap.function_call].filter(
|
908
|
+
Boolean
|
909
|
+
),
|
910
|
+
data: prefixMap.data
|
911
|
+
};
|
912
|
+
}
|
913
|
+
|
715
914
|
// streams/streaming-react-response.ts
|
716
915
|
var experimental_StreamingReactResponse = class {
|
717
916
|
constructor(res, options) {
|
@@ -720,6 +919,39 @@ var experimental_StreamingReactResponse = class {
|
|
720
919
|
let next = new Promise((resolve) => {
|
721
920
|
resolveFunc = resolve;
|
722
921
|
});
|
922
|
+
if (options == null ? void 0 : options.data) {
|
923
|
+
const processedStream = res.pipeThrough(
|
924
|
+
options.data.stream
|
925
|
+
);
|
926
|
+
let lastPayload = void 0;
|
927
|
+
parseComplexResponse({
|
928
|
+
reader: processedStream.getReader(),
|
929
|
+
update: (merged, data) => {
|
930
|
+
var _a, _b, _c;
|
931
|
+
const content2 = (_b = (_a = merged[0]) == null ? void 0 : _a.content) != null ? _b : "";
|
932
|
+
const ui = ((_c = options == null ? void 0 : options.ui) == null ? void 0 : _c.call(options, { content: content2, data })) || content2;
|
933
|
+
const payload = { ui, content: content2 };
|
934
|
+
const resolvePrevious = resolveFunc;
|
935
|
+
const nextRow = new Promise((resolve) => {
|
936
|
+
resolveFunc = resolve;
|
937
|
+
});
|
938
|
+
resolvePrevious({
|
939
|
+
next: nextRow,
|
940
|
+
...payload
|
941
|
+
});
|
942
|
+
lastPayload = payload;
|
943
|
+
},
|
944
|
+
onFinish: () => {
|
945
|
+
if (lastPayload !== void 0) {
|
946
|
+
resolveFunc({
|
947
|
+
next: null,
|
948
|
+
...lastPayload
|
949
|
+
});
|
950
|
+
}
|
951
|
+
}
|
952
|
+
});
|
953
|
+
return next;
|
954
|
+
}
|
723
955
|
let content = "";
|
724
956
|
const decode = createChunkDecoder();
|
725
957
|
const reader = res.getReader();
|
@@ -751,6 +983,55 @@ var experimental_StreamingReactResponse = class {
|
|
751
983
|
return next;
|
752
984
|
}
|
753
985
|
};
|
986
|
+
|
987
|
+
// streams/assistant-response.ts
|
988
|
+
function experimental_AssistantResponse({ threadId, messageId }, process2) {
|
989
|
+
const stream = new ReadableStream({
|
990
|
+
async start(controller) {
|
991
|
+
var _a;
|
992
|
+
const textEncoder = new TextEncoder();
|
993
|
+
const sendMessage = (message) => {
|
994
|
+
controller.enqueue(
|
995
|
+
textEncoder.encode(formatStreamPart("assistant_message", message))
|
996
|
+
);
|
997
|
+
};
|
998
|
+
const sendError = (errorMessage) => {
|
999
|
+
controller.enqueue(
|
1000
|
+
textEncoder.encode(formatStreamPart("error", errorMessage))
|
1001
|
+
);
|
1002
|
+
};
|
1003
|
+
controller.enqueue(
|
1004
|
+
textEncoder.encode(
|
1005
|
+
formatStreamPart("assistant_control_data", {
|
1006
|
+
threadId,
|
1007
|
+
messageId
|
1008
|
+
})
|
1009
|
+
)
|
1010
|
+
);
|
1011
|
+
try {
|
1012
|
+
await process2({
|
1013
|
+
threadId,
|
1014
|
+
messageId,
|
1015
|
+
sendMessage
|
1016
|
+
});
|
1017
|
+
} catch (error) {
|
1018
|
+
sendError((_a = error.message) != null ? _a : `${error}`);
|
1019
|
+
} finally {
|
1020
|
+
controller.close();
|
1021
|
+
}
|
1022
|
+
},
|
1023
|
+
pull(controller) {
|
1024
|
+
},
|
1025
|
+
cancel() {
|
1026
|
+
}
|
1027
|
+
});
|
1028
|
+
return new Response(stream, {
|
1029
|
+
status: 200,
|
1030
|
+
headers: {
|
1031
|
+
"Content-Type": "text/plain; charset=utf-8"
|
1032
|
+
}
|
1033
|
+
});
|
1034
|
+
}
|
754
1035
|
// Annotate the CommonJS export names for ESM import in node:
|
755
1036
|
0 && (module.exports = {
|
756
1037
|
AIStream,
|
@@ -761,16 +1042,14 @@ var experimental_StreamingReactResponse = class {
|
|
761
1042
|
LangChainStream,
|
762
1043
|
OpenAIStream,
|
763
1044
|
ReplicateStream,
|
764
|
-
StreamStringPrefixes,
|
765
1045
|
StreamingTextResponse,
|
766
1046
|
createCallbacksTransformer,
|
767
1047
|
createChunkDecoder,
|
768
1048
|
createEventStreamTransformer,
|
769
1049
|
createStreamDataTransformer,
|
1050
|
+
experimental_AssistantResponse,
|
770
1051
|
experimental_StreamData,
|
771
1052
|
experimental_StreamingReactResponse,
|
772
|
-
getStreamString,
|
773
|
-
getStreamStringTypeAndValue,
|
774
1053
|
isStreamStringEqualToType,
|
775
1054
|
nanoid,
|
776
1055
|
readableFromAsyncIterable,
|