ai 3.1.6 → 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
  */
@@ -1005,6 +1061,11 @@ interface Message$1 {
1005
1061
  * Additional message-specific information added on the server via StreamData
1006
1062
  */
1007
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>;
1008
1069
  }
1009
1070
  type CreateMessage = Omit<Message$1, 'id'> & {
1010
1071
  id?: Message$1['id'];
@@ -1210,18 +1271,20 @@ declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control
1210
1271
  messageId: string;
1211
1272
  }>;
1212
1273
  declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
1213
- declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
1274
+ declare const toolCallsStreamPart: StreamPart<'7', 'tool_calls', {
1214
1275
  tool_calls: ToolCall[];
1215
1276
  }>;
1216
1277
  declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
1217
- 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;
1218
1281
  /**
1219
1282
  * Maps the type of a stream part to its value type.
1220
1283
  */
1221
1284
  type StreamPartValueType = {
1222
1285
  [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];
1223
1286
  };
1224
- 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>;
1225
1288
  /**
1226
1289
  * The map of prefixes for data in the stream
1227
1290
  *
@@ -1254,6 +1317,8 @@ declare const StreamStringPrefixes: {
1254
1317
  readonly data_message: "6";
1255
1318
  readonly tool_calls: "7";
1256
1319
  readonly message_annotations: "8";
1320
+ readonly tool_call: "9";
1321
+ readonly tool_result: "a";
1257
1322
  };
1258
1323
  /**
1259
1324
  Parses a stream part from a string.
@@ -1296,7 +1361,7 @@ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefi
1296
1361
  declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
1297
1362
  declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
1298
1363
 
1299
- 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`;
1300
1365
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
1301
1366
 
1302
1367
  declare interface AzureChatCompletions {
@@ -2048,4 +2113,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
2048
2113
  status?: number;
2049
2114
  }): void;
2050
2115
 
2051
- 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
  */
@@ -1005,6 +1061,11 @@ interface Message$1 {
1005
1061
  * Additional message-specific information added on the server via StreamData
1006
1062
  */
1007
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>;
1008
1069
  }
1009
1070
  type CreateMessage = Omit<Message$1, 'id'> & {
1010
1071
  id?: Message$1['id'];
@@ -1210,18 +1271,20 @@ declare const assistantControlDataStreamPart: StreamPart<'5', 'assistant_control
1210
1271
  messageId: string;
1211
1272
  }>;
1212
1273
  declare const dataMessageStreamPart: StreamPart<'6', 'data_message', DataMessage>;
1213
- declare const toolCallStreamPart: StreamPart<'7', 'tool_calls', {
1274
+ declare const toolCallsStreamPart: StreamPart<'7', 'tool_calls', {
1214
1275
  tool_calls: ToolCall[];
1215
1276
  }>;
1216
1277
  declare const messageAnnotationsStreamPart: StreamPart<'8', 'message_annotations', Array<JSONValue>>;
1217
- 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;
1218
1281
  /**
1219
1282
  * Maps the type of a stream part to its value type.
1220
1283
  */
1221
1284
  type StreamPartValueType = {
1222
1285
  [P in StreamParts as P['name']]: ReturnType<P['parse']>['value'];
1223
1286
  };
1224
- 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>;
1225
1288
  /**
1226
1289
  * The map of prefixes for data in the stream
1227
1290
  *
@@ -1254,6 +1317,8 @@ declare const StreamStringPrefixes: {
1254
1317
  readonly data_message: "6";
1255
1318
  readonly tool_calls: "7";
1256
1319
  readonly message_annotations: "8";
1320
+ readonly tool_call: "9";
1321
+ readonly tool_result: "a";
1257
1322
  };
1258
1323
  /**
1259
1324
  Parses a stream part from a string.
@@ -1296,7 +1361,7 @@ declare function createChunkDecoder(complex: false): (chunk: Uint8Array | undefi
1296
1361
  declare function createChunkDecoder(complex: true): (chunk: Uint8Array | undefined) => StreamPartType[];
1297
1362
  declare function createChunkDecoder(complex?: boolean): (chunk: Uint8Array | undefined) => StreamPartType[] | string;
1298
1363
 
1299
- 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`;
1300
1365
  type StreamString = `${(typeof StreamStringPrefixes)[keyof typeof StreamStringPrefixes]}:${string}\n`;
1301
1366
 
1302
1367
  declare interface AzureChatCompletions {
@@ -2048,4 +2113,4 @@ declare function streamToResponse(res: ReadableStream, response: ServerResponse,
2048
2113
  status?: number;
2049
2114
  }): void;
2050
2115
 
2051
- 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,
@@ -1580,8 +1581,65 @@ var StreamTextResult = class {
1580
1581
 
1581
1582
  @returns an `AIStream` object.
1582
1583
  */
1583
- toAIStream(callbacks) {
1584
- 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());
1585
1643
  }
1586
1644
  /**
1587
1645
  Writes stream data output to a Node.js response-like object.
@@ -1687,6 +1745,55 @@ var StreamTextResult = class {
1687
1745
  };
1688
1746
  var experimental_streamText = streamText;
1689
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
+
1690
1797
  // core/tool/tool.ts
1691
1798
  function tool(tool2) {
1692
1799
  return tool2;
@@ -1791,7 +1898,7 @@ var dataMessageStreamPart = {
1791
1898
  };
1792
1899
  }
1793
1900
  };
1794
- var toolCallStreamPart = {
1901
+ var toolCallsStreamPart = {
1795
1902
  code: "7",
1796
1903
  name: "tool_calls",
1797
1904
  parse: (value) => {
@@ -1818,6 +1925,36 @@ var messageAnnotationsStreamPart = {
1818
1925
  return { type: "message_annotations", value };
1819
1926
  }
1820
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
+ };
1821
1958
  var streamParts = [
1822
1959
  textStreamPart,
1823
1960
  functionCallStreamPart,
@@ -1826,8 +1963,10 @@ var streamParts = [
1826
1963
  assistantMessageStreamPart,
1827
1964
  assistantControlDataStreamPart,
1828
1965
  dataMessageStreamPart,
1966
+ toolCallsStreamPart,
1967
+ messageAnnotationsStreamPart,
1829
1968
  toolCallStreamPart,
1830
- messageAnnotationsStreamPart
1969
+ toolResultStreamPart
1831
1970
  ];
1832
1971
  var streamPartsByCode = {
1833
1972
  [textStreamPart.code]: textStreamPart,
@@ -1837,8 +1976,10 @@ var streamPartsByCode = {
1837
1976
  [assistantMessageStreamPart.code]: assistantMessageStreamPart,
1838
1977
  [assistantControlDataStreamPart.code]: assistantControlDataStreamPart,
1839
1978
  [dataMessageStreamPart.code]: dataMessageStreamPart,
1979
+ [toolCallsStreamPart.code]: toolCallsStreamPart,
1980
+ [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart,
1840
1981
  [toolCallStreamPart.code]: toolCallStreamPart,
1841
- [messageAnnotationsStreamPart.code]: messageAnnotationsStreamPart
1982
+ [toolResultStreamPart.code]: toolResultStreamPart
1842
1983
  };
1843
1984
  var StreamStringPrefixes = {
1844
1985
  [textStreamPart.name]: textStreamPart.code,
@@ -1848,8 +1989,10 @@ var StreamStringPrefixes = {
1848
1989
  [assistantMessageStreamPart.name]: assistantMessageStreamPart.code,
1849
1990
  [assistantControlDataStreamPart.name]: assistantControlDataStreamPart.code,
1850
1991
  [dataMessageStreamPart.name]: dataMessageStreamPart.code,
1992
+ [toolCallsStreamPart.name]: toolCallsStreamPart.code,
1993
+ [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code,
1851
1994
  [toolCallStreamPart.name]: toolCallStreamPart.code,
1852
- [messageAnnotationsStreamPart.name]: messageAnnotationsStreamPart.code
1995
+ [toolResultStreamPart.name]: toolResultStreamPart.code
1853
1996
  };
1854
1997
  var validCodes = streamParts.map((part) => part.code);
1855
1998
  var parseStreamPart = (line) => {
@@ -2952,6 +3095,40 @@ async function parseComplexResponse({
2952
3095
  };
2953
3096
  }
2954
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
+ }
2955
3132
  let functionCallMessage = null;
2956
3133
  if (type === "function_call") {
2957
3134
  prefixMap["function_call"] = {
@@ -3146,6 +3323,7 @@ function streamToResponse(res, response, init) {
3146
3323
  UnsupportedJSONSchemaError,
3147
3324
  convertDataContentToBase64String,
3148
3325
  convertDataContentToUint8Array,
3326
+ convertToCoreMessages,
3149
3327
  createCallbacksTransformer,
3150
3328
  createChunkDecoder,
3151
3329
  createEventStreamTransformer,