ai 2.2.23 → 2.2.25

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 CHANGED
@@ -204,6 +204,16 @@ type UseCompletionOptions = {
204
204
  type JSONValue = null | string | number | boolean | {
205
205
  [x: string]: JSONValue;
206
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
+ };
207
217
 
208
218
  type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
209
219
  /**
@@ -451,6 +461,18 @@ declare function AIStream(response: Response, customParser?: AIStreamParser, cal
451
461
  */
452
462
  declare function readableFromAsyncIterable<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
453
463
 
464
+ interface AWSBedrockResponse {
465
+ body?: AsyncIterable<{
466
+ chunk?: {
467
+ bytes?: Uint8Array;
468
+ };
469
+ }>;
470
+ }
471
+ declare function AWSBedrockAnthropicStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
472
+ declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
473
+ declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
474
+ declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
475
+
454
476
  /**
455
477
  * A stream wrapper to send custom JSON-encoded data back to the client.
456
478
  */
@@ -580,18 +602,26 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
580
602
  headers?: Record<string, string>;
581
603
  }): Promise<ReadableStream>;
582
604
 
583
- declare const nanoid: (size?: number | undefined) => string;
584
- declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
585
- declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
586
- declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => {
587
- type: keyof typeof StreamStringPrefixes;
588
- value: string;
589
- }[];
590
- declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => {
591
- type: keyof typeof StreamStringPrefixes;
592
- value: string;
593
- }[] | string;
594
-
605
+ interface StreamPart<CODE extends string, NAME extends string, TYPE> {
606
+ code: CODE;
607
+ name: NAME;
608
+ parse: (value: JSONValue) => {
609
+ type: NAME;
610
+ value: TYPE;
611
+ };
612
+ }
613
+ declare const textStreamPart: StreamPart<'0', 'text', string>;
614
+ declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
615
+ function_call: FunctionCall;
616
+ }>;
617
+ declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
618
+ declare const errorStreamPart: StreamPart<'3', 'error', string>;
619
+ declare const assistantMessage: StreamPart<'4', 'assistant_message', AssistantMessage>;
620
+ declare const assistantControlData: StreamPart<'5', 'assistant_control_data', {
621
+ threadId: string;
622
+ messageId: string;
623
+ }>;
624
+ 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>;
595
625
  /**
596
626
  * The map of prefixes for data in the stream
597
627
  *
@@ -613,20 +643,22 @@ declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | und
613
643
  *```
614
644
  */
615
645
  declare const StreamStringPrefixes: {
616
- readonly text: 0;
617
- readonly function_call: 1;
618
- readonly data: 2;
646
+ readonly text: "0";
647
+ readonly function_call: "1";
648
+ readonly data: "2";
649
+ readonly error: "3";
650
+ readonly assistant_message: "4";
651
+ readonly assistant_control_data: "5";
619
652
  };
620
- declare const isStreamStringEqualToType: (type: keyof typeof StreamStringPrefixes, value: string) => value is `0:${string}\n` | `1:${string}\n` | `2:${string}\n`;
621
- /**
622
- * Prepends a string with a prefix from the `StreamChunkPrefixes`, JSON-ifies it, and appends a new line.
623
- */
624
- declare const getStreamString: (type: keyof typeof StreamStringPrefixes, value: JSONValue) => StreamString;
653
+
654
+ declare const nanoid: (size?: number | undefined) => string;
655
+ declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
656
+ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
657
+ declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
658
+ declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
659
+
660
+ 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`;
625
661
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
626
- declare const getStreamStringTypeAndValue: (line: string) => {
627
- type: keyof typeof StreamStringPrefixes;
628
- value: JSONValue;
629
- };
630
662
  /**
631
663
  * 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)
632
664
  */
@@ -663,4 +695,13 @@ declare class experimental_StreamingReactResponse {
663
695
  });
664
696
  }
665
697
 
666
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AnthropicStream, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, FunctionCall, FunctionCallHandler, FunctionCallPayload, HuggingFaceStream, JSONValue, LangChainStream, Message, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamString, StreamStringPrefixes, StreamingTextResponse, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_StreamData, experimental_StreamingReactResponse, getStreamString, getStreamStringTypeAndValue, isStreamStringEqualToType, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };
698
+ declare function experimental_AssistantResponse({ threadId, messageId }: {
699
+ threadId: string;
700
+ messageId: string;
701
+ }, process: (stream: {
702
+ threadId: string;
703
+ messageId: string;
704
+ sendMessage: (message: AssistantMessage) => void;
705
+ }) => Promise<void>): Response;
706
+
707
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, 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
@@ -21,6 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var streams_exports = {};
22
22
  __export(streams_exports, {
23
23
  AIStream: () => AIStream,
24
+ AWSBedrockAnthropicStream: () => AWSBedrockAnthropicStream,
25
+ AWSBedrockCohereStream: () => AWSBedrockCohereStream,
26
+ AWSBedrockLlama2Stream: () => AWSBedrockLlama2Stream,
27
+ AWSBedrockStream: () => AWSBedrockStream,
24
28
  AnthropicStream: () => AnthropicStream,
25
29
  COMPLEX_HEADER: () => COMPLEX_HEADER,
26
30
  CohereStream: () => CohereStream,
@@ -28,16 +32,14 @@ __export(streams_exports, {
28
32
  LangChainStream: () => LangChainStream,
29
33
  OpenAIStream: () => OpenAIStream,
30
34
  ReplicateStream: () => ReplicateStream,
31
- StreamStringPrefixes: () => StreamStringPrefixes,
32
35
  StreamingTextResponse: () => StreamingTextResponse,
33
36
  createCallbacksTransformer: () => createCallbacksTransformer,
34
37
  createChunkDecoder: () => createChunkDecoder,
35
38
  createEventStreamTransformer: () => createEventStreamTransformer,
36
39
  createStreamDataTransformer: () => createStreamDataTransformer,
40
+ experimental_AssistantResponse: () => experimental_AssistantResponse,
37
41
  experimental_StreamData: () => experimental_StreamData,
38
42
  experimental_StreamingReactResponse: () => experimental_StreamingReactResponse,
39
- getStreamString: () => getStreamString,
40
- getStreamStringTypeAndValue: () => getStreamStringTypeAndValue,
41
43
  isStreamStringEqualToType: () => isStreamStringEqualToType,
42
44
  nanoid: () => nanoid,
43
45
  readableFromAsyncIterable: () => readableFromAsyncIterable,
@@ -162,57 +164,134 @@ function readableFromAsyncIterable(iterable) {
162
164
  });
163
165
  }
164
166
 
165
- // shared/utils.ts
166
- var import_non_secure = require("nanoid/non-secure");
167
- var nanoid = (0, import_non_secure.customAlphabet)(
168
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
169
- 7
170
- );
171
- function createChunkDecoder(complex) {
172
- const decoder = new TextDecoder();
173
- if (!complex) {
174
- return function(chunk) {
175
- if (!chunk)
176
- return "";
177
- return decoder.decode(chunk, { stream: true });
167
+ // shared/stream-parts.ts
168
+ var textStreamPart = {
169
+ code: "0",
170
+ name: "text",
171
+ parse: (value) => {
172
+ if (typeof value !== "string") {
173
+ throw new Error('"text" parts expect a string value.');
174
+ }
175
+ return { type: "text", value };
176
+ }
177
+ };
178
+ var functionCallStreamPart = {
179
+ code: "1",
180
+ name: "function_call",
181
+ parse: (value) => {
182
+ 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") {
183
+ throw new Error(
184
+ '"function_call" parts expect an object with a "function_call" property.'
185
+ );
186
+ }
187
+ return {
188
+ type: "function_call",
189
+ value
178
190
  };
179
191
  }
180
- return function(chunk) {
181
- const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
182
- return decoded.map(getStreamStringTypeAndValue).filter(Boolean);
183
- };
184
- }
185
- var StreamStringPrefixes = {
186
- text: 0,
187
- function_call: 1,
188
- data: 2
189
- // user_err: 3?
190
192
  };
191
- 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
- if (firstSeperatorIndex === -1) {
197
- throw new Error("Failed to parse stream string");
193
+ var dataStreamPart = {
194
+ code: "2",
195
+ name: "data",
196
+ parse: (value) => {
197
+ if (!Array.isArray(value)) {
198
+ throw new Error('"data" parts expect an array value.');
199
+ }
200
+ return { type: "data", value };
198
201
  }
199
- const prefix = line.slice(0, firstSeperatorIndex);
200
- const type = Object.keys(StreamStringPrefixes).find(
201
- (key) => StreamStringPrefixes[key] === Number(prefix)
202
- );
203
- const val = line.slice(firstSeperatorIndex + 1);
204
- let parsedVal = val;
205
- if (!val) {
206
- return { type, value: "" };
202
+ };
203
+ var errorStreamPart = {
204
+ code: "3",
205
+ name: "error",
206
+ parse: (value) => {
207
+ if (typeof value !== "string") {
208
+ throw new Error('"error" parts expect a string value.');
209
+ }
210
+ return { type: "error", value };
207
211
  }
208
- try {
209
- parsedVal = JSON.parse(val);
210
- } catch (e) {
211
- console.error("Failed to parse JSON value:", val);
212
+ };
213
+ var assistantMessage = {
214
+ code: "4",
215
+ name: "assistant_message",
216
+ parse: (value) => {
217
+ 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(
218
+ (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"
219
+ )) {
220
+ throw new Error(
221
+ '"assistant_message" parts expect an object with an "id", "role", and "content" property.'
222
+ );
223
+ }
224
+ return {
225
+ type: "assistant_message",
226
+ value
227
+ };
212
228
  }
213
- return { type, value: parsedVal };
214
229
  };
215
- var COMPLEX_HEADER = "X-Experimental-Stream-Data";
230
+ var assistantControlData = {
231
+ code: "5",
232
+ name: "assistant_control_data",
233
+ parse: (value) => {
234
+ if (value == null || typeof value !== "object" || !("threadId" in value) || !("messageId" in value) || typeof value.threadId !== "string" || typeof value.messageId !== "string") {
235
+ throw new Error(
236
+ '"assistant_control_data" parts expect an object with a "threadId" and "messageId" property.'
237
+ );
238
+ }
239
+ return {
240
+ type: "assistant_control_data",
241
+ value: {
242
+ threadId: value.threadId,
243
+ messageId: value.messageId
244
+ }
245
+ };
246
+ }
247
+ };
248
+ var streamParts = [
249
+ textStreamPart,
250
+ functionCallStreamPart,
251
+ dataStreamPart,
252
+ errorStreamPart,
253
+ assistantMessage,
254
+ assistantControlData
255
+ ];
256
+ var streamPartsByCode = {
257
+ [textStreamPart.code]: textStreamPart,
258
+ [functionCallStreamPart.code]: functionCallStreamPart,
259
+ [dataStreamPart.code]: dataStreamPart,
260
+ [errorStreamPart.code]: errorStreamPart,
261
+ [assistantMessage.code]: assistantMessage,
262
+ [assistantControlData.code]: assistantControlData
263
+ };
264
+ var StreamStringPrefixes = {
265
+ [textStreamPart.name]: textStreamPart.code,
266
+ [functionCallStreamPart.name]: functionCallStreamPart.code,
267
+ [dataStreamPart.name]: dataStreamPart.code,
268
+ [errorStreamPart.name]: errorStreamPart.code,
269
+ [assistantMessage.name]: assistantMessage.code,
270
+ [assistantControlData.name]: assistantControlData.code
271
+ };
272
+ var validCodes = streamParts.map((part) => part.code);
273
+ var parseStreamPart = (line) => {
274
+ const firstSeparatorIndex = line.indexOf(":");
275
+ if (firstSeparatorIndex === -1) {
276
+ throw new Error("Failed to parse stream string. No separator found.");
277
+ }
278
+ const prefix = line.slice(0, firstSeparatorIndex);
279
+ if (!validCodes.includes(prefix)) {
280
+ throw new Error(`Failed to parse stream string. Invalid code ${prefix}.`);
281
+ }
282
+ const code = prefix;
283
+ const textValue = line.slice(firstSeparatorIndex + 1);
284
+ const jsonValue = JSON.parse(textValue);
285
+ return streamPartsByCode[code].parse(jsonValue);
286
+ };
287
+ function formatStreamPart(type, value) {
288
+ const streamPart = streamParts.find((part) => part.name === type);
289
+ if (!streamPart) {
290
+ throw new Error(`Invalid stream part type: ${type}`);
291
+ }
292
+ return `${streamPart.code}:${JSON.stringify(value)}
293
+ `;
294
+ }
216
295
 
217
296
  // streams/stream-data.ts
218
297
  var experimental_StreamData = class {
@@ -237,7 +316,7 @@ var experimental_StreamData = class {
237
316
  transform: async (chunk, controller) => {
238
317
  if (self.data.length > 0) {
239
318
  const encodedData = self.encoder.encode(
240
- getStreamString("data", JSON.stringify(self.data))
319
+ formatStreamPart("data", self.data)
241
320
  );
242
321
  self.data = [];
243
322
  controller.enqueue(encodedData);
@@ -256,7 +335,7 @@ var experimental_StreamData = class {
256
335
  }
257
336
  if (self.data.length) {
258
337
  const encodedData = self.encoder.encode(
259
- getStreamString("data", JSON.stringify(self.data))
338
+ formatStreamPart("data", self.data)
260
339
  );
261
340
  controller.enqueue(encodedData);
262
341
  }
@@ -294,11 +373,76 @@ function createStreamDataTransformer(experimental_streamData) {
294
373
  return new TransformStream({
295
374
  transform: async (chunk, controller) => {
296
375
  const message = decoder.decode(chunk);
297
- controller.enqueue(encoder.encode(getStreamString("text", message)));
376
+ controller.enqueue(encoder.encode(formatStreamPart("text", message)));
298
377
  }
299
378
  });
300
379
  }
301
380
 
381
+ // streams/aws-bedrock-stream.ts
382
+ async function* asDeltaIterable(response, extractTextDeltaFromChunk) {
383
+ var _a, _b;
384
+ const decoder = new TextDecoder();
385
+ for await (const chunk of (_a = response.body) != null ? _a : []) {
386
+ const bytes = (_b = chunk.chunk) == null ? void 0 : _b.bytes;
387
+ if (bytes != null) {
388
+ const chunkText = decoder.decode(bytes);
389
+ const chunkJSON = JSON.parse(chunkText);
390
+ const delta = extractTextDeltaFromChunk(chunkJSON);
391
+ if (delta != null) {
392
+ yield delta;
393
+ }
394
+ }
395
+ }
396
+ }
397
+ function AWSBedrockAnthropicStream(response, callbacks) {
398
+ return AWSBedrockStream(response, callbacks, (chunk) => chunk.completion);
399
+ }
400
+ function AWSBedrockCohereStream(response, callbacks) {
401
+ return AWSBedrockStream(
402
+ response,
403
+ callbacks,
404
+ // As of 2023-11-17, Bedrock does not support streaming for Cohere,
405
+ // so we take the full generation:
406
+ (chunk) => {
407
+ var _a, _b;
408
+ return (_b = (_a = chunk.generations) == null ? void 0 : _a[0]) == null ? void 0 : _b.text;
409
+ }
410
+ );
411
+ }
412
+ function AWSBedrockLlama2Stream(response, callbacks) {
413
+ return AWSBedrockStream(response, callbacks, (chunk) => chunk.generation);
414
+ }
415
+ function AWSBedrockStream(response, callbacks, extractTextDeltaFromChunk) {
416
+ return readableFromAsyncIterable(
417
+ asDeltaIterable(response, extractTextDeltaFromChunk)
418
+ ).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(
419
+ createStreamDataTransformer(callbacks == null ? void 0 : callbacks.experimental_streamData)
420
+ );
421
+ }
422
+
423
+ // shared/utils.ts
424
+ var import_non_secure = require("nanoid/non-secure");
425
+ var nanoid = (0, import_non_secure.customAlphabet)(
426
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
427
+ 7
428
+ );
429
+ function createChunkDecoder(complex) {
430
+ const decoder = new TextDecoder();
431
+ if (!complex) {
432
+ return function(chunk) {
433
+ if (!chunk)
434
+ return "";
435
+ return decoder.decode(chunk, { stream: true });
436
+ };
437
+ }
438
+ return function(chunk) {
439
+ const decoded = decoder.decode(chunk, { stream: true }).split("\n").filter((line) => line !== "");
440
+ return decoded.map(parseStreamPart).filter(Boolean);
441
+ };
442
+ }
443
+ var isStreamStringEqualToType = (type, value) => value.startsWith(`${StreamStringPrefixes[type]}:`) && value.endsWith("\n");
444
+ var COMPLEX_HEADER = "X-Experimental-Stream-Data";
445
+
302
446
  // streams/openai-stream.ts
303
447
  function parseOpenAIStream() {
304
448
  const extract = chunkToText();
@@ -402,7 +546,7 @@ function createFunctionCallTransformer(callbacks) {
402
546
  }
403
547
  if (!isFunctionStreamingIn) {
404
548
  controller.enqueue(
405
- isComplexMode ? textEncoder.encode(getStreamString("text", message)) : chunk
549
+ isComplexMode ? textEncoder.encode(formatStreamPart("text", message)) : chunk
406
550
  );
407
551
  return;
408
552
  } else {
@@ -444,13 +588,17 @@ function createFunctionCallTransformer(callbacks) {
444
588
  if (!functionResponse) {
445
589
  controller.enqueue(
446
590
  textEncoder.encode(
447
- isComplexMode ? getStreamString("function_call", aggregatedResponse) : aggregatedResponse
591
+ isComplexMode ? formatStreamPart(
592
+ "function_call",
593
+ // parse to prevent double-encoding:
594
+ JSON.parse(aggregatedResponse)
595
+ ) : aggregatedResponse
448
596
  )
449
597
  );
450
598
  return;
451
599
  } else if (typeof functionResponse === "string") {
452
600
  controller.enqueue(
453
- isComplexMode ? textEncoder.encode(getStreamString("text", functionResponse)) : textEncoder.encode(functionResponse)
601
+ isComplexMode ? textEncoder.encode(formatStreamPart("text", functionResponse)) : textEncoder.encode(functionResponse)
454
602
  );
455
603
  return;
456
604
  }
@@ -715,18 +863,22 @@ async function ReplicateStream(res, cb, options) {
715
863
  );
716
864
  }
717
865
 
718
- // react/parse-complex-response.ts
866
+ // shared/parse-complex-response.ts
719
867
  async function parseComplexResponse({
720
868
  reader,
721
869
  abortControllerRef,
722
870
  update,
723
- onFinish
871
+ onFinish,
872
+ generateId = nanoid,
873
+ getCurrentDate = () => /* @__PURE__ */ new Date()
724
874
  }) {
875
+ const createdAt = getCurrentDate();
725
876
  const decode = createChunkDecoder(true);
726
- const createdAt = /* @__PURE__ */ new Date();
727
- const prefixMap = {};
877
+ const prefixMap = {
878
+ data: []
879
+ };
728
880
  const NEWLINE = "\n".charCodeAt(0);
729
- let chunks = [];
881
+ const chunks = [];
730
882
  let totalLength = 0;
731
883
  while (true) {
732
884
  const { value } = await reader.read();
@@ -763,7 +915,7 @@ async function parseComplexResponse({
763
915
  };
764
916
  } else {
765
917
  prefixMap["text"] = {
766
- id: nanoid(),
918
+ id: generateId(),
767
919
  role: "assistant",
768
920
  content: value2,
769
921
  createdAt
@@ -772,37 +924,24 @@ async function parseComplexResponse({
772
924
  }
773
925
  let functionCallMessage = null;
774
926
  if (type === "function_call") {
775
- prefixMap["function_call"] = value2;
776
- let functionCall = prefixMap["function_call"];
777
- if (functionCall && typeof functionCall === "string") {
778
- const parsedFunctionCall = JSON.parse(
779
- functionCall
780
- ).function_call;
781
- functionCallMessage = {
782
- id: nanoid(),
783
- role: "assistant",
784
- content: "",
785
- function_call: parsedFunctionCall,
786
- name: parsedFunctionCall.name,
787
- createdAt
788
- };
789
- prefixMap["function_call"] = functionCallMessage;
790
- }
927
+ prefixMap["function_call"] = {
928
+ id: generateId(),
929
+ role: "assistant",
930
+ content: "",
931
+ function_call: value2.function_call,
932
+ name: value2.function_call.name,
933
+ createdAt
934
+ };
935
+ functionCallMessage = prefixMap["function_call"];
791
936
  }
792
937
  if (type === "data") {
793
- const parsedValue = JSON.parse(value2);
794
- if (prefixMap["data"]) {
795
- prefixMap["data"] = [...prefixMap["data"], ...parsedValue];
796
- } else {
797
- prefixMap["data"] = parsedValue;
798
- }
938
+ prefixMap["data"].push(...value2);
799
939
  }
800
- const data = prefixMap["data"];
801
940
  const responseMessage = prefixMap["text"];
802
941
  const merged = [functionCallMessage, responseMessage].filter(
803
942
  Boolean
804
943
  );
805
- update(merged, data);
944
+ update(merged, [...prefixMap["data"]]);
806
945
  if ((abortControllerRef == null ? void 0 : abortControllerRef.current) === null) {
807
946
  reader.cancel();
808
947
  break;
@@ -810,7 +949,12 @@ async function parseComplexResponse({
810
949
  }
811
950
  }
812
951
  onFinish == null ? void 0 : onFinish(prefixMap);
813
- return prefixMap;
952
+ return {
953
+ messages: [prefixMap.text, prefixMap.function_call].filter(
954
+ Boolean
955
+ ),
956
+ data: prefixMap.data
957
+ };
814
958
  }
815
959
 
816
960
  // streams/streaming-react-response.ts
@@ -885,9 +1029,62 @@ var experimental_StreamingReactResponse = class {
885
1029
  return next;
886
1030
  }
887
1031
  };
1032
+
1033
+ // streams/assistant-response.ts
1034
+ function experimental_AssistantResponse({ threadId, messageId }, process2) {
1035
+ const stream = new ReadableStream({
1036
+ async start(controller) {
1037
+ var _a;
1038
+ const textEncoder = new TextEncoder();
1039
+ const sendMessage = (message) => {
1040
+ controller.enqueue(
1041
+ textEncoder.encode(formatStreamPart("assistant_message", message))
1042
+ );
1043
+ };
1044
+ const sendError = (errorMessage) => {
1045
+ controller.enqueue(
1046
+ textEncoder.encode(formatStreamPart("error", errorMessage))
1047
+ );
1048
+ };
1049
+ controller.enqueue(
1050
+ textEncoder.encode(
1051
+ formatStreamPart("assistant_control_data", {
1052
+ threadId,
1053
+ messageId
1054
+ })
1055
+ )
1056
+ );
1057
+ try {
1058
+ await process2({
1059
+ threadId,
1060
+ messageId,
1061
+ sendMessage
1062
+ });
1063
+ } catch (error) {
1064
+ sendError((_a = error.message) != null ? _a : `${error}`);
1065
+ } finally {
1066
+ controller.close();
1067
+ }
1068
+ },
1069
+ pull(controller) {
1070
+ },
1071
+ cancel() {
1072
+ }
1073
+ });
1074
+ return new Response(stream, {
1075
+ status: 200,
1076
+ headers: {
1077
+ "Content-Type": "text/plain; charset=utf-8"
1078
+ }
1079
+ });
1080
+ }
888
1081
  // Annotate the CommonJS export names for ESM import in node:
889
1082
  0 && (module.exports = {
890
1083
  AIStream,
1084
+ AWSBedrockAnthropicStream,
1085
+ AWSBedrockCohereStream,
1086
+ AWSBedrockLlama2Stream,
1087
+ AWSBedrockStream,
891
1088
  AnthropicStream,
892
1089
  COMPLEX_HEADER,
893
1090
  CohereStream,
@@ -895,16 +1092,14 @@ var experimental_StreamingReactResponse = class {
895
1092
  LangChainStream,
896
1093
  OpenAIStream,
897
1094
  ReplicateStream,
898
- StreamStringPrefixes,
899
1095
  StreamingTextResponse,
900
1096
  createCallbacksTransformer,
901
1097
  createChunkDecoder,
902
1098
  createEventStreamTransformer,
903
1099
  createStreamDataTransformer,
1100
+ experimental_AssistantResponse,
904
1101
  experimental_StreamData,
905
1102
  experimental_StreamingReactResponse,
906
- getStreamString,
907
- getStreamStringTypeAndValue,
908
1103
  isStreamStringEqualToType,
909
1104
  nanoid,
910
1105
  readableFromAsyncIterable,