ai 3.1.5 → 3.1.7

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.mts CHANGED
@@ -591,6 +591,24 @@ onlyBar('bar');
591
591
  */
592
592
  type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
593
593
 
594
+ /**
595
+ Typed tool call that is returned by generateText and streamText.
596
+ It contains the tool call ID, the tool name, and the tool arguments.
597
+ */
598
+ interface ToolCall$1<NAME extends string, ARGS> {
599
+ /**
600
+ ID of the tool call. This ID is used to match the tool call with the tool result.
601
+ */
602
+ toolCallId: string;
603
+ /**
604
+ Name of the tool that is being called.
605
+ */
606
+ toolName: NAME;
607
+ /**
608
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
609
+ */
610
+ args: ARGS;
611
+ }
594
612
  type ToToolCall<TOOLS extends Record<string, CoreTool>> = ValueOf<{
595
613
  [NAME in keyof TOOLS]: {
596
614
  type: 'tool-call';
@@ -601,6 +619,28 @@ type ToToolCall<TOOLS extends Record<string, CoreTool>> = ValueOf<{
601
619
  }>;
602
620
  type ToToolCallArray<TOOLS extends Record<string, CoreTool>> = Array<ToToolCall<TOOLS>>;
603
621
 
622
+ /**
623
+ Typed tool result that is returned by generateText and streamText.
624
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
625
+ */
626
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
627
+ /**
628
+ ID of the tool call. This ID is used to match the tool call with the tool result.
629
+ */
630
+ toolCallId: string;
631
+ /**
632
+ Name of the tool that was called.
633
+ */
634
+ toolName: NAME;
635
+ /**
636
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
637
+ */
638
+ args: ARGS;
639
+ /**
640
+ Result of the tool call. This is the result of the tool's execution.
641
+ */
642
+ result: RESULT;
643
+ }
604
644
  type ToToolsWithExecute<TOOLS extends Record<string, CoreTool>> = {
605
645
  [K in keyof TOOLS as TOOLS[K] extends {
606
646
  execute: any;
@@ -854,7 +894,7 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
854
894
 
855
895
  @returns an `AIStream` object.
856
896
  */
857
- toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<any>;
897
+ toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<Uint8Array>;
858
898
  /**
859
899
  Writes stream data output to a Node.js response-like object.
860
900
  It sets a `Content-Type` header to `text/plain; charset=utf-8` and
@@ -902,6 +942,16 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
902
942
  */
903
943
  declare const experimental_streamText: typeof streamText;
904
944
 
945
+ /**
946
+ Converts an array of messages from useChat into an array of CoreMessages that can be used
947
+ with the AI core functions (e.g. `streamText`).
948
+ */
949
+ declare function convertToCoreMessages(messages: Array<{
950
+ role: 'user' | 'assistant';
951
+ content: string;
952
+ toolInvocations?: Array<ToolResult<string, unknown, unknown>>;
953
+ }>): CoreMessage[];
954
+
905
955
  interface FunctionCall$1 {
906
956
  /**
907
957
  * The arguments to call the function with, as generated by the model in JSON
@@ -970,6 +1020,12 @@ interface Function {
970
1020
  description?: string;
971
1021
  }
972
1022
  type IdGenerator = () => string;
1023
+ /**
1024
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
1025
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
1026
+ Once the call is complete, the invocation is a tool result.
1027
+ */
1028
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
973
1029
  /**
974
1030
  * Shared types between the API and UI packages.
975
1031
  */
@@ -978,9 +1034,13 @@ interface Message$1 {
978
1034
  tool_call_id?: string;
979
1035
  createdAt?: Date;
980
1036
  content: string;
1037
+ /**
1038
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
1039
+ */
981
1040
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
982
1041
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
983
1042
  /**
1043
+ *
984
1044
  * If the message has a role of `function`, the `name` field is the name of the function.
985
1045
  * Otherwise, the name field should not be set.
986
1046
  */
@@ -1001,6 +1061,11 @@ interface Message$1 {
1001
1061
  * Additional message-specific information added on the server via StreamData
1002
1062
  */
1003
1063
  annotations?: JSONValue[] | undefined;
1064
+ /**
1065
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
1066
+ that the assistant made as part of this message.
1067
+ */
1068
+ toolInvocations?: Array<ToolInvocation>;
1004
1069
  }
1005
1070
  type CreateMessage = Omit<Message$1, 'id'> & {
1006
1071
  id?: Message$1['id'];
@@ -1206,18 +1271,20 @@ declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control
1206
1271
  messageId: string;
1207
1272
  }>;
1208
1273
  declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
1209
- declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
1274
+ declare const toolCallsStreamPart: StreamPart<'7', 'tool_calls', {
1210
1275
  tool_calls: ToolCall[];
1211
1276
  }>;
1212
1277
  declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
1213
- type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallStreamPart | typeof messageAnnotationsStreamPart;
1278
+ declare const toolCallStreamPart: StreamPart<'9', 'tool_call', ToolCall$1<string, any>>;
1279
+ declare const toolResultStreamPart: StreamPart<'a', 'tool_result', ToolResult<string, any, any>>;
1280
+ type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallsStreamPart | typeof messageAnnotationsStreamPart | typeof toolCallStreamPart | typeof toolResultStreamPart;
1214
1281
  /**
1215
1282
  * Maps the type of a stream part to its value type.
1216
1283
  */
1217
1284
  type StreamPartValueType = {
1218
1285
  [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];
1219
1286
  };
1220
- 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> | ReturnType<typeof messageAnnotationsStreamPart.parse>;
1287
+ 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 toolCallsStreamPart.parse> | ReturnType<typeof messageAnnotationsStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse> | ReturnType<typeof toolResultStreamPart.parse>;
1221
1288
  /**
1222
1289
  * The map of prefixes for data in the stream
1223
1290
  *
@@ -1250,6 +1317,8 @@ declare const StreamStringPrefixes: {
1250
1317
  readonly data_message: "6";
1251
1318
  readonly tool_calls: "7";
1252
1319
  readonly message_annotations: "8";
1320
+ readonly tool_call: "9";
1321
+ readonly tool_result: "a";
1253
1322
  };
1254
1323
  /**
1255
1324
  Parses a stream part from a string.
@@ -1292,7 +1361,7 @@ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefi
1292
1361
  declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
1293
1362
  declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
1294
1363
 
1295
- 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` | `8:${string}\n`;
1364
+ 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` | `8:${string}\n` | `9:${string}\n` | `a:${string}\n`;
1296
1365
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
1297
1366
 
1298
1367
  declare interface AzureChatCompletions {
@@ -2008,11 +2077,16 @@ type Payload = {
2008
2077
  ui: UINode | Promise<UINode>;
2009
2078
  content: string;
2010
2079
  };
2080
+ /**
2081
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
2082
+ */
2011
2083
  type ReactResponseRow = Payload & {
2012
2084
  next: null | Promise<ReactResponseRow>;
2013
2085
  };
2014
2086
  /**
2015
- * A utility class for streaming React responses.
2087
+ A utility class for streaming React responses.
2088
+
2089
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
2016
2090
  */
2017
2091
  declare class experimental_StreamingReactResponse {
2018
2092
  constructor(res: ReadableStream, options?: {
@@ -2039,4 +2113,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
2039
2113
  status?: number;
2040
2114
  }): void;
2041
2115
 
2042
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolResultPart, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
2116
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
package/dist/index.d.ts CHANGED
@@ -591,6 +591,24 @@ onlyBar('bar');
591
591
  */
592
592
  type ValueOf<ObjectType, ValueType extends keyof ObjectType = keyof ObjectType> = ObjectType[ValueType];
593
593
 
594
+ /**
595
+ Typed tool call that is returned by generateText and streamText.
596
+ It contains the tool call ID, the tool name, and the tool arguments.
597
+ */
598
+ interface ToolCall$1<NAME extends string, ARGS> {
599
+ /**
600
+ ID of the tool call. This ID is used to match the tool call with the tool result.
601
+ */
602
+ toolCallId: string;
603
+ /**
604
+ Name of the tool that is being called.
605
+ */
606
+ toolName: NAME;
607
+ /**
608
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
609
+ */
610
+ args: ARGS;
611
+ }
594
612
  type ToToolCall<TOOLS extends Record<string, CoreTool>> = ValueOf<{
595
613
  [NAME in keyof TOOLS]: {
596
614
  type: 'tool-call';
@@ -601,6 +619,28 @@ type ToToolCall<TOOLS extends Record<string, CoreTool>> = ValueOf<{
601
619
  }>;
602
620
  type ToToolCallArray<TOOLS extends Record<string, CoreTool>> = Array<ToToolCall<TOOLS>>;
603
621
 
622
+ /**
623
+ Typed tool result that is returned by generateText and streamText.
624
+ It contains the tool call ID, the tool name, the tool arguments, and the tool result.
625
+ */
626
+ interface ToolResult<NAME extends string, ARGS, RESULT> {
627
+ /**
628
+ ID of the tool call. This ID is used to match the tool call with the tool result.
629
+ */
630
+ toolCallId: string;
631
+ /**
632
+ Name of the tool that was called.
633
+ */
634
+ toolName: NAME;
635
+ /**
636
+ Arguments of the tool call. This is a JSON-serializable object that matches the tool's input schema.
637
+ */
638
+ args: ARGS;
639
+ /**
640
+ Result of the tool call. This is the result of the tool's execution.
641
+ */
642
+ result: RESULT;
643
+ }
604
644
  type ToToolsWithExecute<TOOLS extends Record<string, CoreTool>> = {
605
645
  [K in keyof TOOLS as TOOLS[K] extends {
606
646
  execute: any;
@@ -854,7 +894,7 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
854
894
 
855
895
  @returns an `AIStream` object.
856
896
  */
857
- toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<any>;
897
+ toAIStream(callbacks?: AIStreamCallbacksAndOptions): ReadableStream<Uint8Array>;
858
898
  /**
859
899
  Writes stream data output to a Node.js response-like object.
860
900
  It sets a `Content-Type` header to `text/plain; charset=utf-8` and
@@ -902,6 +942,16 @@ declare class StreamTextResult<TOOLS extends Record<string, CoreTool>> {
902
942
  */
903
943
  declare const experimental_streamText: typeof streamText;
904
944
 
945
+ /**
946
+ Converts an array of messages from useChat into an array of CoreMessages that can be used
947
+ with the AI core functions (e.g. `streamText`).
948
+ */
949
+ declare function convertToCoreMessages(messages: Array<{
950
+ role: 'user' | 'assistant';
951
+ content: string;
952
+ toolInvocations?: Array<ToolResult<string, unknown, unknown>>;
953
+ }>): CoreMessage[];
954
+
905
955
  interface FunctionCall$1 {
906
956
  /**
907
957
  * The arguments to call the function with, as generated by the model in JSON
@@ -970,6 +1020,12 @@ interface Function {
970
1020
  description?: string;
971
1021
  }
972
1022
  type IdGenerator = () => string;
1023
+ /**
1024
+ Tool invocations are either tool calls or tool results. For each assistant tool call,
1025
+ there is one tool invocation. While the call is in progress, the invocation is a tool call.
1026
+ Once the call is complete, the invocation is a tool result.
1027
+ */
1028
+ type ToolInvocation = ToolCall$1<string, any> | ToolResult<string, any, any>;
973
1029
  /**
974
1030
  * Shared types between the API and UI packages.
975
1031
  */
@@ -978,9 +1034,13 @@ interface Message$1 {
978
1034
  tool_call_id?: string;
979
1035
  createdAt?: Date;
980
1036
  content: string;
1037
+ /**
1038
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
1039
+ */
981
1040
  ui?: string | JSX.Element | JSX.Element[] | null | undefined;
982
1041
  role: 'system' | 'user' | 'assistant' | 'function' | 'data' | 'tool';
983
1042
  /**
1043
+ *
984
1044
  * If the message has a role of `function`, the `name` field is the name of the function.
985
1045
  * Otherwise, the name field should not be set.
986
1046
  */
@@ -1001,6 +1061,11 @@ interface Message$1 {
1001
1061
  * Additional message-specific information added on the server via StreamData
1002
1062
  */
1003
1063
  annotations?: JSONValue[] | undefined;
1064
+ /**
1065
+ Tool invocations (that can be tool calls or tool results, depending on whether or not the invocation has finished)
1066
+ that the assistant made as part of this message.
1067
+ */
1068
+ toolInvocations?: Array<ToolInvocation>;
1004
1069
  }
1005
1070
  type CreateMessage = Omit<Message$1, 'id'> & {
1006
1071
  id?: Message$1['id'];
@@ -1206,18 +1271,20 @@ declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control
1206
1271
  messageId: string;
1207
1272
  }>;
1208
1273
  declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
1209
- declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
1274
+ declare const toolCallsStreamPart: StreamPart<'7', 'tool_calls', {
1210
1275
  tool_calls: ToolCall[];
1211
1276
  }>;
1212
1277
  declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
1213
- type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallStreamPart | typeof messageAnnotationsStreamPart;
1278
+ declare const toolCallStreamPart: StreamPart<'9', 'tool_call', ToolCall$1<string, any>>;
1279
+ declare const toolResultStreamPart: StreamPart<'a', 'tool_result', ToolResult<string, any, any>>;
1280
+ type StreamParts = typeof textStreamPart | typeof functionCallStreamPart | typeof dataStreamPart | typeof errorStreamPart | typeof assistantMessageStreamPart | typeof assistantControlDataStreamPart | typeof dataMessageStreamPart | typeof toolCallsStreamPart | typeof messageAnnotationsStreamPart | typeof toolCallStreamPart | typeof toolResultStreamPart;
1214
1281
  /**
1215
1282
  * Maps the type of a stream part to its value type.
1216
1283
  */
1217
1284
  type StreamPartValueType = {
1218
1285
  [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];
1219
1286
  };
1220
- 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> | ReturnType<typeof messageAnnotationsStreamPart.parse>;
1287
+ 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 toolCallsStreamPart.parse> | ReturnType<typeof messageAnnotationsStreamPart.parse> | ReturnType<typeof toolCallStreamPart.parse> | ReturnType<typeof toolResultStreamPart.parse>;
1221
1288
  /**
1222
1289
  * The map of prefixes for data in the stream
1223
1290
  *
@@ -1250,6 +1317,8 @@ declare const StreamStringPrefixes: {
1250
1317
  readonly data_message: "6";
1251
1318
  readonly tool_calls: "7";
1252
1319
  readonly message_annotations: "8";
1320
+ readonly tool_call: "9";
1321
+ readonly tool_result: "a";
1253
1322
  };
1254
1323
  /**
1255
1324
  Parses a stream part from a string.
@@ -1292,7 +1361,7 @@ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefi
1292
1361
  declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
1293
1362
  declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
1294
1363
 
1295
- 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` | `8:${string}\n`;
1364
+ 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` | `8:${string}\n` | `9:${string}\n` | `a:${string}\n`;
1296
1365
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
1297
1366
 
1298
1367
  declare interface AzureChatCompletions {
@@ -2008,11 +2077,16 @@ type Payload = {
2008
2077
  ui: UINode | Promise<UINode>;
2009
2078
  content: string;
2010
2079
  };
2080
+ /**
2081
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
2082
+ */
2011
2083
  type ReactResponseRow = Payload & {
2012
2084
  next: null | Promise<ReactResponseRow>;
2013
2085
  };
2014
2086
  /**
2015
- * A utility class for streaming React responses.
2087
+ A utility class for streaming React responses.
2088
+
2089
+ @deprecated Use AI SDK RSC instead: https://sdk.vercel.ai/docs/ai-sdk-rsc
2016
2090
  */
2017
2091
  declare class experimental_StreamingReactResponse {
2018
2092
  constructor(res: ReadableStream, options?: {
@@ -2039,4 +2113,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
2039
2113
  status?: number;
2040
2114
  }): void;
2041
2115
 
2042
- export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolResultPart, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
2116
+ export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, ExperimentalAssistantMessage, ExperimentalMessage, ExperimentalTool, ExperimentalToolMessage, ExperimentalUserMessage, FinishReason, Function, FunctionCall$1 as FunctionCall, FunctionCallHandler, FunctionCallPayload, GenerateObjectResult, GenerateTextResult, GoogleGenerativeAIStream, HuggingFaceStream, IdGenerator, ImagePart, InkeepAIStreamCallbacksAndOptions, InkeepChatResultCallbacks, InkeepOnFinalMetadata, InkeepStream, JSONValue, langchainAdapter as LangChainAdapter, LangChainStream, LanguageModel, LogProbs, Message$1 as Message, MistralStream, ObjectStreamPart, ObjectStreamPartInput, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, experimental_AssistantResponse, experimental_StreamData, experimental_StreamingReactResponse, experimental_generateObject, experimental_generateText, experimental_streamObject, experimental_streamText, formatStreamPart, generateId, generateObject, generateText, isStreamStringEqualToType, generateId as nanoid, parseStreamPart, readDataStream, readableFromAsyncIterable, streamObject, streamText, streamToResponse, tool, trimStartOfStreamHelper };
package/dist/index.js CHANGED
@@ -71,6 +71,7 @@ __export(streams_exports, {
71
71
  UnsupportedJSONSchemaError: () => import_provider8.UnsupportedJSONSchemaError,
72
72
  convertDataContentToBase64String: () => convertDataContentToBase64String,
73
73
  convertDataContentToUint8Array: () => convertDataContentToUint8Array,
74
+ convertToCoreMessages: () => convertToCoreMessages,
74
75
  createCallbacksTransformer: () => createCallbacksTransformer,
75
76
  createChunkDecoder: () => createChunkDecoder,
76
77
  createEventStreamTransformer: () => createEventStreamTransformer,
@@ -145,7 +146,15 @@ function convertDataContentToUint8Array(content) {
145
146
  return content;
146
147
  }
147
148
  if (typeof content === "string") {
148
- return (0, import_provider_utils.convertBase64ToUint8Array)(content);
149
+ try {
150
+ return (0, import_provider_utils.convertBase64ToUint8Array)(content);
151
+ } catch (error) {
152
+ throw new import_provider.InvalidDataContentError({
153
+ message: "Invalid data content. Content string is not a base64-encoded image.",
154
+ content,
155
+ cause: error
156
+ });
157
+ }
149
158
  }
150
159
  if (content instanceof ArrayBuffer) {
151
160
  return new Uint8Array(content);
@@ -1572,8 +1581,65 @@ var StreamTextResult = class {
1572
1581
 
1573
1582
  @returns an `AIStream` object.
1574
1583
  */
1575
- toAIStream(callbacks) {
1576
- return this.textStream.pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(createStreamDataTransformer());
1584
+ toAIStream(callbacks = {}) {
1585
+ let aggregatedResponse = "";
1586
+ const callbackTransformer = new TransformStream({
1587
+ async start() {
1588
+ if (callbacks.onStart)
1589
+ await callbacks.onStart();
1590
+ },
1591
+ async transform(chunk, controller) {
1592
+ controller.enqueue(chunk);
1593
+ if (chunk.type === "text-delta") {
1594
+ const textDelta = chunk.textDelta;
1595
+ aggregatedResponse += textDelta;
1596
+ if (callbacks.onToken)
1597
+ await callbacks.onToken(textDelta);
1598
+ if (callbacks.onText)
1599
+ await callbacks.onText(textDelta);
1600
+ }
1601
+ },
1602
+ async flush() {
1603
+ if (callbacks.onCompletion)
1604
+ await callbacks.onCompletion(aggregatedResponse);
1605
+ if (callbacks.onFinal)
1606
+ await callbacks.onFinal(aggregatedResponse);
1607
+ }
1608
+ });
1609
+ const streamDataTransformer = new TransformStream({
1610
+ transform: async (chunk, controller) => {
1611
+ switch (chunk.type) {
1612
+ case "text-delta":
1613
+ controller.enqueue(formatStreamPart("text", chunk.textDelta));
1614
+ break;
1615
+ case "tool-call":
1616
+ controller.enqueue(
1617
+ formatStreamPart("tool_call", {
1618
+ toolCallId: chunk.toolCallId,
1619
+ toolName: chunk.toolName,
1620
+ args: chunk.args
1621
+ })
1622
+ );
1623
+ break;
1624
+ case "tool-result":
1625
+ controller.enqueue(
1626
+ formatStreamPart("tool_result", {
1627
+ toolCallId: chunk.toolCallId,
1628
+ toolName: chunk.toolName,
1629
+ args: chunk.args,
1630
+ result: chunk.result
1631
+ })
1632
+ );
1633
+ break;
1634
+ case "error":
1635
+ controller.enqueue(
1636
+ formatStreamPart("error", JSON.stringify(chunk.error))
1637
+ );
1638
+ break;
1639
+ }
1640
+ }
1641
+ });
1642
+ return this.fullStream.pipeThrough(callbackTransformer).pipeThrough(streamDataTransformer).pipeThrough(new TextEncoderStream());
1577
1643
  }
1578
1644
  /**
1579
1645
  Writes stream data output to a Node.js response-like object.
@@ -1679,6 +1745,55 @@ var StreamTextResult = class {
1679
1745
  };
1680
1746
  var experimental_streamText = streamText;
1681
1747
 
1748
+ // core/prompt/convert-to-core-messages.ts
1749
+ function convertToCoreMessages(messages) {
1750
+ const coreMessages = [];
1751
+ for (const { role, content, toolInvocations } of messages) {
1752
+ switch (role) {
1753
+ case "user": {
1754
+ coreMessages.push({ role: "user", content });
1755
+ break;
1756
+ }
1757
+ case "assistant": {
1758
+ if (toolInvocations == null) {
1759
+ coreMessages.push({ role: "assistant", content });
1760
+ break;
1761
+ }
1762
+ coreMessages.push({
1763
+ role: "assistant",
1764
+ content: [
1765
+ { type: "text", text: content },
1766
+ ...toolInvocations.map(({ toolCallId, toolName, args }) => ({
1767
+ type: "tool-call",
1768
+ toolCallId,
1769
+ toolName,
1770
+ args
1771
+ }))
1772
+ ]
1773
+ });
1774
+ coreMessages.push({
1775
+ role: "tool",
1776
+ content: toolInvocations.map(
1777
+ ({ toolCallId, toolName, args, result }) => ({
1778
+ type: "tool-result",
1779
+ toolCallId,
1780
+ toolName,
1781
+ args,
1782
+ result
1783
+ })
1784
+ )
1785
+ });
1786
+ break;
1787
+ }
1788
+ default: {
1789
+ const _exhaustiveCheck = role;
1790
+ throw new Error(`Unhandled role: ${_exhaustiveCheck}`);
1791
+ }
1792
+ }
1793
+ }
1794
+ return coreMessages;
1795
+ }
1796
+
1682
1797
  // core/tool/tool.ts
1683
1798
  function tool(tool2) {
1684
1799
  return tool2;
@@ -1783,7 +1898,7 @@ var dataMessageStreamPart = {
1783
1898
  };
1784
1899
  }
1785
1900
  };
1786
- var toolCallStreamPart = {
1901
+ var toolCallsStreamPart = {
1787
1902
  code: "7",
1788
1903
  name: "tool_calls",
1789
1904
  parse: (value) => {
@@ -1810,6 +1925,36 @@ var messageAnnotationsStreamPart = {
1810
1925
  return { type: "message_annotations", value };
1811
1926
  }
1812
1927
  };
1928
+ var toolCallStreamPart = {
1929
+ code: "9",
1930
+ name: "tool_call",
1931
+ parse: (value) => {
1932
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
1933
+ throw new Error(
1934
+ '"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
1935
+ );
1936
+ }
1937
+ return {
1938
+ type: "tool_call",
1939
+ value
1940
+ };
1941
+ }
1942
+ };
1943
+ var toolResultStreamPart = {
1944
+ code: "a",
1945
+ name: "tool_result",
1946
+ parse: (value) => {
1947
+ if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object" || !("result" in value)) {
1948
+ throw new Error(
1949
+ '"tool_result" parts expect an object with a "toolCallId", "toolName", "args", and "result" property.'
1950
+ );
1951
+ }
1952
+ return {
1953
+ type: "tool_result",
1954
+ value
1955
+ };
1956
+ }
1957
+ };
1813
1958
  var streamParts = [
1814
1959
  textStreamPart,
1815
1960
  functionCallStreamPart,
@@ -1818,8 +1963,10 @@ var streamParts = [
1818
1963
  assistantMessageStreamPart,
1819
1964
  assistantControlDataStreamPart,
1820
1965
  dataMessageStreamPart,
1966
+ toolCallsStreamPart,
1967
+ messageAnnotationsStreamPart,
1821
1968
  toolCallStreamPart,
1822
- messageAnnotationsStreamPart
1969
+ toolResultStreamPart
1823
1970
  ];
1824
1971
  var streamPartsByCode = {
1825
1972
  [textStreamPart.code]: textStreamPart,
@@ -1829,8 +1976,10 @@ var streamPartsByCode = {
1829
1976
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1830
1977
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1831
1978
  [dataMessageStreamPart.code]: dataMessageStreamPart,
1979
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
1980
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
1832
1981
  [toolCallStreamPart.code]: toolCallStreamPart,
1833
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1982
+ [toolResultStreamPart.code]: toolResultStreamPart
1834
1983
  };
1835
1984
  var StreamStringPrefixes = {
1836
1985
  [textStreamPart.name]: textStreamPart.code,
@@ -1840,8 +1989,10 @@ var StreamStringPrefixes = {
1840
1989
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1841
1990
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1842
1991
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1992
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
1993
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
1843
1994
  [toolCallStreamPart.name]: toolCallStreamPart.code,
1844
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1995
+ [toolResultStreamPart.name]: toolResultStreamPart.code
1845
1996
  };
1846
1997
  var validCodes = streamParts.map((part) => part.code);
1847
1998
  var parseStreamPart = (line) => {
@@ -2944,6 +3095,40 @@ async function parseComplexResponse({
2944
3095
  };
2945
3096
  }
2946
3097
  }
3098
+ if (type === "tool_call") {
3099
+ if (prefixMap.text == null) {
3100
+ prefixMap.text = {
3101
+ id: generateId2(),
3102
+ role: "assistant",
3103
+ content: "",
3104
+ createdAt
3105
+ };
3106
+ }
3107
+ if (prefixMap.text.toolInvocations == null) {
3108
+ prefixMap.text.toolInvocations = [];
3109
+ }
3110
+ prefixMap.text.toolInvocations.push(value);
3111
+ } else if (type === "tool_result") {
3112
+ if (prefixMap.text == null) {
3113
+ prefixMap.text = {
3114
+ id: generateId2(),
3115
+ role: "assistant",
3116
+ content: "",
3117
+ createdAt
3118
+ };
3119
+ }
3120
+ if (prefixMap.text.toolInvocations == null) {
3121
+ prefixMap.text.toolInvocations = [];
3122
+ }
3123
+ const toolInvocationIndex = prefixMap.text.toolInvocations.findIndex(
3124
+ (invocation) => invocation.toolCallId === value.toolCallId
3125
+ );
3126
+ if (toolInvocationIndex !== -1) {
3127
+ prefixMap.text.toolInvocations[toolInvocationIndex] = value;
3128
+ } else {
3129
+ prefixMap.text.toolInvocations.push(value);
3130
+ }
3131
+ }
2947
3132
  let functionCallMessage = null;
2948
3133
  if (type === "function_call") {
2949
3134
  prefixMap["function_call"] = {
@@ -3138,6 +3323,7 @@ function streamToResponse(res, response, init) {
3138
3323
  UnsupportedJSONSchemaError,
3139
3324
  convertDataContentToBase64String,
3140
3325
  convertDataContentToUint8Array,
3326
+ convertToCoreMessages,
3141
3327
  createCallbacksTransformer,
3142
3328
  createChunkDecoder,
3143
3329
  createEventStreamTransformer,