ai 2.2.29 → 2.2.30

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
@@ -13,6 +13,38 @@ interface FunctionCall {
13
13
  */
14
14
  name?: string;
15
15
  }
16
+ /**
17
+ * The tool calls generated by the model, such as function calls.
18
+ */
19
+ interface ToolCall {
20
+ id: string;
21
+ type: string;
22
+ function: {
23
+ name: string;
24
+ arguments: string;
25
+ };
26
+ }
27
+ /**
28
+ * Controls which (if any) function is called by the model.
29
+ * - none means the model will not call a function and instead generates a message.
30
+ * - auto means the model can pick between generating a message or calling a function.
31
+ * - Specifying a particular function via {"type: "function", "function": {"name": "my_function"}} forces the model to call that function.
32
+ * none is the default when no functions are present. auto is the default if functions are present.
33
+ */
34
+ type ToolChoice = 'none' | 'auto' | {
35
+ type: 'function';
36
+ function: {
37
+ name: string;
38
+ };
39
+ };
40
+ /**
41
+ * A list of tools the model may call. Currently, only functions are supported as a tool.
42
+ * Use this to provide a list of functions the model may generate JSON inputs for.
43
+ */
44
+ interface Tool {
45
+ type: 'function';
46
+ function: Function;
47
+ }
16
48
  interface Function {
17
49
  /**
18
50
  * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
@@ -39,12 +71,13 @@ type IdGenerator = () => string;
39
71
  /**
40
72
  * Shared types between the API and UI packages.
41
73
  */
42
- interface Message {
74
+ interface Message$1 {
43
75
  id: string;
76
+ tool_call_id?: string;
44
77
  createdAt?: Date;
45
78
  content: string;
46
79
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
47
- role: 'system' | 'user' | 'assistant' | 'function' | 'data';
80
+ role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
48
81
  /**
49
82
  * If the message has a role of `function`, the `name` field is the name of the function.
50
83
  * Otherwise, the name field should not be set.
@@ -53,22 +86,30 @@ interface Message {
53
86
  /**
54
87
  * If the assistant role makes a function call, the `function_call` field
55
88
  * contains the function call name and arguments. Otherwise, the field should
56
- * not be set.
89
+ * not be set. (Deprecated and replaced by tool_calls.)
57
90
  */
58
91
  function_call?: string | FunctionCall;
59
92
  data?: JSONValue;
93
+ /**
94
+ * If the assistant role makes a tool call, the `tool_calls` field contains
95
+ * the tool call name and arguments. Otherwise, the field should not be set.
96
+ */
97
+ tool_calls?: string | ToolCall[];
60
98
  }
61
- type CreateMessage = Omit<Message, 'id'> & {
62
- id?: Message['id'];
99
+ type CreateMessage = Omit<Message$1, 'id'> & {
100
+ id?: Message$1['id'];
63
101
  };
64
102
  type ChatRequest = {
65
- messages: Message[];
103
+ messages: Message$1[];
66
104
  options?: RequestOptions;
67
105
  functions?: Array<Function>;
68
106
  function_call?: FunctionCall;
69
107
  data?: Record<string, string>;
108
+ tools?: Array<Tool>;
109
+ tool_choice?: ToolChoice;
70
110
  };
71
- type FunctionCallHandler = (chatMessages: Message[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
111
+ type FunctionCallHandler = (chatMessages: Message$1[], functionCall: FunctionCall) => Promise<ChatRequest | void>;
112
+ type ToolCallHandler = (chatMessages: Message$1[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
72
113
  type RequestOptions = {
73
114
  headers?: Record<string, string> | Headers;
74
115
  body?: object;
@@ -77,6 +118,8 @@ type ChatRequestOptions = {
77
118
  options?: RequestOptions;
78
119
  functions?: Array<Function>;
79
120
  function_call?: FunctionCall;
121
+ tools?: Array<Tool>;
122
+ tool_choice?: ToolChoice;
80
123
  data?: Record<string, string>;
81
124
  };
82
125
  type UseChatOptions = {
@@ -94,7 +137,7 @@ type UseChatOptions = {
94
137
  /**
95
138
  * Initial messages of the chat. Useful to load an existing chat history.
96
139
  */
97
- initialMessages?: Message[];
140
+ initialMessages?: Message$1[];
98
141
  /**
99
142
  * Initial input of the chat.
100
143
  */
@@ -105,6 +148,12 @@ type UseChatOptions = {
105
148
  * automatically to the API and will be used to update the chat.
106
149
  */
107
150
  experimental_onFunctionCall?: FunctionCallHandler;
151
+ /**
152
+ * Callback function to be called when a tool call is received.
153
+ * If the function returns a `ChatRequest` object, the request will be sent
154
+ * automatically to the API and will be used to update the chat.
155
+ */
156
+ experimental_onToolCall?: ToolCallHandler;
108
157
  /**
109
158
  * Callback function to be called when the API response is received.
110
159
  */
@@ -112,7 +161,7 @@ type UseChatOptions = {
112
161
  /**
113
162
  * Callback function to be called when the chat is finished streaming.
114
163
  */
115
- onFinish?: (message: Message) => void;
164
+ onFinish?: (message: Message$1) => void;
116
165
  /**
117
166
  * Callback function to be called when an error is encountered.
118
167
  */
@@ -227,6 +276,76 @@ type DataMessage = {
227
276
  data: JSONValue;
228
277
  };
229
278
 
279
+ interface StreamPart<CODE extends string, NAME extends string, TYPE> {
280
+ code: CODE;
281
+ name: NAME;
282
+ parse: (value: JSONValue) => {
283
+ type: NAME;
284
+ value: TYPE;
285
+ };
286
+ }
287
+ declare const textStreamPart: StreamPart<'0', 'text', string>;
288
+ declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
289
+ function_call: FunctionCall;
290
+ }>;
291
+ declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
292
+ declare const errorStreamPart: StreamPart<'3', 'error', string>;
293
+ declare const assistantMessageStreamPart: StreamPart<'4', 'assistant_message', AssistantMessage>;
294
+ declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control_data', {
295
+ threadId: string;
296
+ messageId: string;
297
+ }>;
298
+ declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
299
+ declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
300
+ tool_calls: ToolCall[];
301
+ }>;
302
+ type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse>;
303
+ /**
304
+ * The map of prefixes for data in the stream
305
+ *
306
+ * - 0: Text from the LLM response
307
+ * - 1: (OpenAI) function_call responses
308
+ * - 2: custom JSON added by the user using `Data`
309
+ * - 6: (OpenAI) tool_call responses
310
+ *
311
+ * Example:
312
+ * ```
313
+ * 0:Vercel
314
+ * 0:'s
315
+ * 0: AI
316
+ * 0: AI
317
+ * 0: SDK
318
+ * 0: is great
319
+ * 0:!
320
+ * 2: { "someJson": "value" }
321
+ * 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
322
+ * 6: {"tool_call": {"id": "tool_0", "type": "function", "function": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}}
323
+ *```
324
+ */
325
+ declare const StreamStringPrefixes: {
326
+ readonly text: "0";
327
+ readonly function_call: "1";
328
+ readonly data: "2";
329
+ readonly error: "3";
330
+ readonly assistant_message: "4";
331
+ readonly assistant_control_data: "5";
332
+ readonly data_message: "6";
333
+ readonly tool_calls: "7";
334
+ };
335
+
336
+ declare const nanoid: (size?: number | undefined) => string;
337
+ declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
338
+ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
339
+ declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
340
+ declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
341
+
342
+ 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` | `6:${string}\n` | `7:${string}\n`;
343
+ type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
344
+ /**
345
+ * 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)
346
+ */
347
+ declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
348
+
230
349
  type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
231
350
  /**
232
351
  * @example
@@ -256,6 +375,46 @@ type OpenAIStreamCallbacks = AIStreamCallbacksAndOptions & {
256
375
  * ```
257
376
  */
258
377
  experimental_onFunctionCall?: (functionCallPayload: FunctionCallPayload, createFunctionCallMessages: (functionCallResult: JSONValue) => CreateMessage[]) => Promise<Response | undefined | void | string | AsyncIterableOpenAIStreamReturnTypes>;
378
+ /**
379
+ * @example
380
+ * ```js
381
+ * const response = await openai.chat.completions.create({
382
+ * model: 'gpt-3.5-turbo-1106', // or gpt-4-1106-preview
383
+ * stream: true,
384
+ * messages,
385
+ * tools,
386
+ * tool_choice: "auto", // auto is default, but we'll be explicit
387
+ * })
388
+ *
389
+ * const stream = OpenAIStream(response, {
390
+ * experimental_onToolCall: async (toolCallPayload, appendToolCallMessages) => {
391
+ * let messages: CreateMessage[] = []
392
+ * // There might be multiple tool calls, so we need to iterate through them
393
+ * for (const tool of toolCallPayload.tools) {
394
+ * // ... run your custom logic here
395
+ * const result = await myFunction(tool.function)
396
+ * // Append the relevant "assistant" and "tool" call messages
397
+ * appendToolCallMessage({tool_call_id:tool.id, function_name:tool.function.name, tool_call_result:result})
398
+ * }
399
+ * // Ask for another completion, or return a string to send to the client as an assistant message.
400
+ * return await openai.chat.completions.create({
401
+ * model: 'gpt-3.5-turbo-1106', // or gpt-4-1106-preview
402
+ * stream: true,
403
+ * // Append the results messages, calling appendToolCallMessage without
404
+ * // any arguments will jsut return the accumulated messages
405
+ * messages: [...messages, ...appendToolCallMessage()],
406
+ * tools,
407
+ * tool_choice: "auto", // auto is default, but we'll be explicit
408
+ * })
409
+ * }
410
+ * })
411
+ * ```
412
+ */
413
+ experimental_onToolCall?: (toolCallPayload: ToolCallPayload, appendToolCallMessage: (result?: {
414
+ tool_call_id: string;
415
+ function_name: string;
416
+ tool_call_result: JSONValue;
417
+ }) => CreateMessage[]) => Promise<Response | undefined | void | string | AsyncIterableOpenAIStreamReturnTypes>;
259
418
  };
260
419
  interface ChatCompletionChunk {
261
420
  id: string;
@@ -291,6 +450,9 @@ interface DeltaToolCall {
291
450
  * The ID of the tool call.
292
451
  */
293
452
  id?: string;
453
+ /**
454
+ * The function that the model called.
455
+ */
294
456
  function?: ToolCallFunction;
295
457
  /**
296
458
  * The type of the tool. Currently, only `function` is supported.
@@ -375,6 +537,16 @@ interface FunctionCallPayload {
375
537
  name: string;
376
538
  arguments: Record<string, unknown>;
377
539
  }
540
+ interface ToolCallPayload {
541
+ tools: {
542
+ id: string;
543
+ type: 'function';
544
+ func: {
545
+ name: string;
546
+ arguments: Record<string, unknown>;
547
+ };
548
+ }[];
549
+ }
378
550
  /**
379
551
  * Configuration options and helper callback methods for AIStream stream lifecycle events.
380
552
  * @interface
@@ -473,57 +645,6 @@ declare function AIStream(response: Response, customParser?: AIStreamParser, cal
473
645
  */
474
646
  declare function readableFromAsyncIterable<T>(iterable: AsyncIterable<T>): ReadableStream<T>;
475
647
 
476
- interface AWSBedrockResponse {
477
- body?: AsyncIterable<{
478
- chunk?: {
479
- bytes?: Uint8Array;
480
- };
481
- }>;
482
- }
483
- declare function AWSBedrockAnthropicStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
484
- declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
485
- declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
486
- declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
487
-
488
- /**
489
- * A stream wrapper to send custom JSON-encoded data back to the client.
490
- */
491
- declare class experimental_StreamData {
492
- private encoder;
493
- private controller;
494
- stream: TransformStream<Uint8Array, Uint8Array>;
495
- private isClosedPromise;
496
- private isClosedPromiseResolver;
497
- private isClosed;
498
- private data;
499
- constructor();
500
- close(): Promise<void>;
501
- append(value: JSONValue): void;
502
- }
503
- /**
504
- * A TransformStream for LLMs that do not have their own transform stream handlers managing encoding (e.g. OpenAIStream has one for function call handling).
505
- * This assumes every chunk is a 'text' chunk.
506
- */
507
- declare function createStreamDataTransformer(experimental_streamData: boolean | undefined): TransformStream<any, any>;
508
-
509
- /**
510
- * A utility class for streaming text responses.
511
- */
512
- declare class StreamingTextResponse extends Response {
513
- constructor(res: ReadableStream, init?: ResponseInit, data?: experimental_StreamData);
514
- }
515
- /**
516
- * A utility function to stream a ReadableStream to a Node.js response-like object.
517
- */
518
- declare function streamToResponse(res: ReadableStream, response: ServerResponse, init?: {
519
- headers?: Record<string, string>;
520
- status?: number;
521
- }): void;
522
-
523
- declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
524
-
525
- declare function CohereStream(reader: Response, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
526
-
527
648
  interface CompletionChunk {
528
649
  /**
529
650
  * The resulting completion up to and excluding the stop sequences.
@@ -544,12 +665,110 @@ interface CompletionChunk {
544
665
  */
545
666
  stop_reason: string;
546
667
  }
668
+ interface Message {
669
+ id: string;
670
+ content: Array<ContentBlock>;
671
+ model: string;
672
+ role: 'assistant';
673
+ stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null;
674
+ stop_sequence: string | null;
675
+ type: 'message';
676
+ }
677
+ interface ContentBlock {
678
+ text: string;
679
+ type: 'text';
680
+ }
681
+ interface TextDelta {
682
+ text: string;
683
+ type: 'text_delta';
684
+ }
685
+ interface ContentBlockDeltaEvent {
686
+ delta: TextDelta;
687
+ index: number;
688
+ type: 'content_block_delta';
689
+ }
690
+ interface ContentBlockStartEvent {
691
+ content_block: ContentBlock;
692
+ index: number;
693
+ type: 'content_block_start';
694
+ }
695
+ interface ContentBlockStopEvent {
696
+ index: number;
697
+ type: 'content_block_stop';
698
+ }
699
+ interface MessageDeltaEventDelta {
700
+ stop_reason: 'end_turn' | 'max_tokens' | 'stop_sequence' | null;
701
+ stop_sequence: string | null;
702
+ }
703
+ interface MessageDeltaEvent {
704
+ delta: MessageDeltaEventDelta;
705
+ type: 'message_delta';
706
+ }
707
+ type MessageStreamEvent = MessageStartEvent | MessageDeltaEvent | MessageStopEvent | ContentBlockStartEvent | ContentBlockDeltaEvent | ContentBlockStopEvent;
708
+ interface MessageStartEvent {
709
+ message: Message;
710
+ type: 'message_start';
711
+ }
712
+ interface MessageStopEvent {
713
+ type: 'message_stop';
714
+ }
547
715
  /**
548
716
  * Accepts either a fetch Response from the Anthropic `POST /v1/complete` endpoint,
549
717
  * or the return value of `await client.completions.create({ stream: true })`
550
718
  * from the `@anthropic-ai/sdk` package.
551
719
  */
552
- declare function AnthropicStream(res: Response | AsyncIterable<CompletionChunk>, cb?: AIStreamCallbacksAndOptions): ReadableStream;
720
+ declare function AnthropicStream(res: Response | AsyncIterable<CompletionChunk> | AsyncIterable<MessageStreamEvent>, cb?: AIStreamCallbacksAndOptions): ReadableStream;
721
+
722
+ type AssistantResponseSettings = {
723
+ threadId: string;
724
+ messageId: string;
725
+ };
726
+ type AssistantResponseCallback = (stream: {
727
+ threadId: string;
728
+ messageId: string;
729
+ sendMessage: (message: AssistantMessage) => void;
730
+ sendDataMessage: (message: DataMessage) => void;
731
+ }) => Promise<void>;
732
+ declare function experimental_AssistantResponse({ threadId, messageId }: AssistantResponseSettings, process: AssistantResponseCallback): Response;
733
+
734
+ interface AWSBedrockResponse {
735
+ body?: AsyncIterable<{
736
+ chunk?: {
737
+ bytes?: Uint8Array;
738
+ };
739
+ }>;
740
+ }
741
+ declare function AWSBedrockAnthropicStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
742
+ declare function AWSBedrockCohereStream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
743
+ declare function AWSBedrockLlama2Stream(response: AWSBedrockResponse, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
744
+ declare function AWSBedrockStream(response: AWSBedrockResponse, callbacks: AIStreamCallbacksAndOptions | undefined, extractTextDeltaFromChunk: (chunk: any) => string): ReadableStream<any>;
745
+
746
+ declare function CohereStream(reader: Response, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
747
+
748
+ interface GenerateContentResponse {
749
+ candidates?: GenerateContentCandidate[];
750
+ }
751
+ interface GenerateContentCandidate {
752
+ index: number;
753
+ content: Content;
754
+ }
755
+ interface Content {
756
+ role: string;
757
+ parts: Part[];
758
+ }
759
+ type Part = TextPart | InlineDataPart;
760
+ interface InlineDataPart {
761
+ text?: never;
762
+ }
763
+ interface TextPart {
764
+ text: string;
765
+ inlineData?: never;
766
+ }
767
+ declare function GoogleGenerativeAIStream(response: {
768
+ stream: AsyncIterable<GenerateContentResponse>;
769
+ }, cb?: AIStreamCallbacksAndOptions): ReadableStream;
770
+
771
+ declare function HuggingFaceStream(res: AsyncGenerator<any>, callbacks?: AIStreamCallbacksAndOptions): ReadableStream;
553
772
 
554
773
  declare function LangChainStream(callbacks?: AIStreamCallbacksAndOptions): {
555
774
  stream: ReadableStream<any>;
@@ -614,69 +833,26 @@ declare function ReplicateStream(res: Prediction, cb?: AIStreamCallbacksAndOptio
614
833
  headers?: Record<string, string>;
615
834
  }): Promise<ReadableStream>;
616
835
 
617
- interface StreamPart<CODE extends string, NAME extends string, TYPE> {
618
- code: CODE;
619
- name: NAME;
620
- parse: (value: JSONValue) => {
621
- type: NAME;
622
- value: TYPE;
623
- };
624
- }
625
- declare const textStreamPart: StreamPart<'0', 'text', string>;
626
- declare const functionCallStreamPart: StreamPart<'1', 'function_call', {
627
- function_call: FunctionCall;
628
- }>;
629
- declare const dataStreamPart: StreamPart<'2', 'data', Array<JSONValue>>;
630
- declare const errorStreamPart: StreamPart<'3', 'error', string>;
631
- declare const assistantMessageStreamPart: StreamPart<'4', 'assistant_message', AssistantMessage>;
632
- declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control_data', {
633
- threadId: string;
634
- messageId: string;
635
- }>;
636
- declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
637
- type StreamPartType = ReturnType<typeof textStreamPart.parse> | ReturnType<typeof functionCallStreamPart.parse> | ReturnType<typeof dataStreamPart.parse> | ReturnType<typeof errorStreamPart.parse> | ReturnType<typeof assistantMessageStreamPart.parse> | ReturnType<typeof assistantControlDataStreamPart.parse> | ReturnType<typeof dataMessageStreamPart.parse>;
638
836
  /**
639
- * The map of prefixes for data in the stream
640
- *
641
- * - 0: Text from the LLM response
642
- * - 1: (OpenAI) function_call responses
643
- * - 2: custom JSON added by the user using `Data`
644
- *
645
- * Example:
646
- * ```
647
- * 0:Vercel
648
- * 0:'s
649
- * 0: AI
650
- * 0: AI
651
- * 0: SDK
652
- * 0: is great
653
- * 0:!
654
- * 2: { "someJson": "value" }
655
- * 1: {"function_call": {"name": "get_current_weather", "arguments": "{\\n\\"location\\": \\"Charlottesville, Virginia\\",\\n\\"format\\": \\"celsius\\"\\n}"}}
656
- *```
837
+ * A stream wrapper to send custom JSON-encoded data back to the client.
657
838
  */
658
- declare const StreamStringPrefixes: {
659
- readonly text: "0";
660
- readonly function_call: "1";
661
- readonly data: "2";
662
- readonly error: "3";
663
- readonly assistant_message: "4";
664
- readonly assistant_control_data: "5";
665
- readonly data_message: "6";
666
- };
667
-
668
- declare const nanoid: (size?: number | undefined) => string;
669
- declare function createChunkDecoder(): (chunk: Uint8Array | undefined) => string;
670
- declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefined) => string;
671
- declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
672
- declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
673
-
674
- 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` | `6:${string}\n`;
675
- type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
839
+ declare class experimental_StreamData {
840
+ private encoder;
841
+ private controller;
842
+ stream: TransformStream<Uint8Array, Uint8Array>;
843
+ private isClosedPromise;
844
+ private isClosedPromiseResolver;
845
+ private isClosed;
846
+ private data;
847
+ constructor();
848
+ close(): Promise<void>;
849
+ append(value: JSONValue): void;
850
+ }
676
851
  /**
677
- * 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)
852
+ * A TransformStream for LLMs that do not have their own transform stream handlers managing encoding (e.g. OpenAIStream has one for function call handling).
853
+ * This assumes every chunk is a 'text' chunk.
678
854
  */
679
- declare const COMPLEX_HEADER = "X-Experimental-Stream-Data";
855
+ declare function createStreamDataTransformer(experimental_streamData: boolean | undefined): TransformStream<any, any>;
680
856
 
681
857
  /**
682
858
  * This is a naive implementation of the streaming React response API.
@@ -710,14 +886,18 @@ declare class experimental_StreamingReactResponse {
710
886
  });
711
887
  }
712
888
 
713
- declare function experimental_AssistantResponse({ threadId, messageId }: {
714
- threadId: string;
715
- messageId: string;
716
- }, process: (stream: {
717
- threadId: string;
718
- messageId: string;
719
- sendMessage: (message: AssistantMessage) => void;
720
- sendDataMessage: (message: DataMessage) => void;
721
- }) => Promise<void>): Response;
889
+ /**
890
+ * A utility class for streaming text responses.
891
+ */
892
+ declare class StreamingTextResponse extends Response {
893
+ constructor(res: ReadableStream, init?: ResponseInit, data?: experimental_StreamData);
894
+ }
895
+ /**
896
+ * A utility function to stream a ReadableStream to a Node.js response-like object.
897
+ */
898
+ declare function streamToResponse(res: ReadableStream, response: ServerResponse, init?: {
899
+ headers?: Record<string, string>;
900
+ status?: number;
901
+ }): void;
722
902
 
723
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantMessage, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, DataMessage, FunctionCall, FunctionCallHandler, FunctionCallPayload, HuggingFaceStream, IdGenerator, 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 };
903
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantMessage, COMPLEX_HEADER, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CreateMessage, DataMessage, Function, FunctionCall, FunctionCallHandler, FunctionCallPayload, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, JSONValue, LangChainStream, Message$1 as Message, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamString, StreamingTextResponse, Tool, ToolCall, ToolCallHandler, ToolCallPayload, ToolChoice, UseChatOptions, UseCompletionOptions, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, isStreamStringEqualToType, nanoid, readableFromAsyncIterable, streamToResponse, trimStartOfStreamHelper };