ai 3.1.22 → 3.1.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +52 -8
- package/dist/index.d.ts +52 -8
- package/dist/index.js +53 -37
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -37
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/react/dist/index.d.mts +31 -3
- package/react/dist/index.d.ts +31 -3
- package/react/dist/index.js +4 -3
- package/react/dist/index.js.map +1 -1
- package/react/dist/index.mjs +4 -3
- package/react/dist/index.mjs.map +1 -1
- package/rsc/dist/index.d.ts +7 -2
- package/rsc/dist/rsc-server.d.mts +7 -2
- package/rsc/dist/rsc-server.mjs.map +1 -1
- package/rsc/dist/rsc-shared.d.mts +6 -1
- package/solid/dist/index.d.mts +24 -0
- package/solid/dist/index.d.ts +24 -0
- package/svelte/dist/index.d.mts +24 -0
- package/svelte/dist/index.d.ts +24 -0
- package/vue/dist/index.d.mts +24 -0
- package/vue/dist/index.d.ts +24 -0
package/dist/index.d.mts
CHANGED
@@ -168,9 +168,21 @@ declare class EmbedManyResult<VALUE> {
|
|
168
168
|
});
|
169
169
|
}
|
170
170
|
|
171
|
+
/**
|
172
|
+
Represents the number of tokens used in a prompt and completion.
|
173
|
+
*/
|
171
174
|
type TokenUsage = {
|
175
|
+
/**
|
176
|
+
The number of tokens used in the prompt
|
177
|
+
*/
|
172
178
|
promptTokens: number;
|
179
|
+
/**
|
180
|
+
The number of tokens used in the completion.
|
181
|
+
*/
|
173
182
|
completionTokens: number;
|
183
|
+
/**
|
184
|
+
The total number of tokens used (promptTokens + completionTokens).
|
185
|
+
*/
|
174
186
|
totalTokens: number;
|
175
187
|
};
|
176
188
|
|
@@ -595,7 +607,7 @@ Default and recommended: 'auto' (best mode for the model).
|
|
595
607
|
*/
|
596
608
|
mode?: 'auto' | 'json' | 'tool' | 'grammar';
|
597
609
|
}): Promise<StreamObjectResult<T>>;
|
598
|
-
type
|
610
|
+
type ObjectStreamInputPart = {
|
599
611
|
type: 'error';
|
600
612
|
error: unknown;
|
601
613
|
} | {
|
@@ -608,7 +620,7 @@ type ObjectStreamPartInput = {
|
|
608
620
|
totalTokens: number;
|
609
621
|
};
|
610
622
|
};
|
611
|
-
type ObjectStreamPart<T> =
|
623
|
+
type ObjectStreamPart<T> = ObjectStreamInputPart | {
|
612
624
|
type: 'object';
|
613
625
|
object: DeepPartial<T>;
|
614
626
|
};
|
@@ -616,12 +628,16 @@ type ObjectStreamPart<T> = ObjectStreamPartInput | {
|
|
616
628
|
The result of a `streamObject` call that contains the partial object stream and additional information.
|
617
629
|
*/
|
618
630
|
declare class StreamObjectResult<T> {
|
619
|
-
|
631
|
+
readonly originalStream: ReadableStream<ObjectStreamPart<T>>;
|
620
632
|
/**
|
621
633
|
Warnings from the model provider (e.g. unsupported settings)
|
622
634
|
*/
|
623
635
|
readonly warnings: CallWarning[] | undefined;
|
624
636
|
/**
|
637
|
+
The token usage of the generated response. Resolved when the response is finished.
|
638
|
+
*/
|
639
|
+
readonly usage: Promise<TokenUsage>;
|
640
|
+
/**
|
625
641
|
Optional raw response data.
|
626
642
|
*/
|
627
643
|
rawResponse?: {
|
@@ -631,7 +647,7 @@ declare class StreamObjectResult<T> {
|
|
631
647
|
headers?: Record<string, string>;
|
632
648
|
};
|
633
649
|
constructor({ stream, warnings, rawResponse, }: {
|
634
|
-
stream: ReadableStream<string |
|
650
|
+
stream: ReadableStream<string | ObjectStreamInputPart>;
|
635
651
|
warnings: CallWarning[] | undefined;
|
636
652
|
rawResponse?: {
|
637
653
|
headers?: Record<string, string>;
|
@@ -841,12 +857,12 @@ If set and supported by the model, calls will generate deterministic results.
|
|
841
857
|
@param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
|
842
858
|
@param abortSignal - An optional abort signal that can be used to cancel the call.
|
843
859
|
|
844
|
-
@param
|
860
|
+
@param maxToolRoundtrips - Maximal number of automatic roundtrips for tool calls.
|
845
861
|
|
846
862
|
@returns
|
847
863
|
A result object that contains the generated text, the results of the tool calls, and additional information.
|
848
864
|
*/
|
849
|
-
declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, maxAutomaticRoundtrips, ...settings }: CallSettings & Prompt & {
|
865
|
+
declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, maxAutomaticRoundtrips, maxToolRoundtrips, ...settings }: CallSettings & Prompt & {
|
850
866
|
/**
|
851
867
|
The language model to use.
|
852
868
|
*/
|
@@ -860,6 +876,10 @@ The tool choice strategy. Default: 'auto'.
|
|
860
876
|
*/
|
861
877
|
toolChoice?: CoreToolChoice<TOOLS>;
|
862
878
|
/**
|
879
|
+
@deprecated Use `maxToolRoundtrips` instead.
|
880
|
+
*/
|
881
|
+
maxAutomaticRoundtrips?: number;
|
882
|
+
/**
|
863
883
|
Maximal number of automatic roundtrips for tool calls.
|
864
884
|
|
865
885
|
An automatic tool call roundtrip is another LLM call with the
|
@@ -871,7 +891,7 @@ case of misconfigured tools.
|
|
871
891
|
|
872
892
|
By default, it's set to 0, which will disable the feature.
|
873
893
|
*/
|
874
|
-
|
894
|
+
maxToolRoundtrips?: number;
|
875
895
|
}): Promise<GenerateTextResult<TOOLS>>;
|
876
896
|
/**
|
877
897
|
The result of a `generateText` call.
|
@@ -1366,15 +1386,39 @@ type FunctionCallHandler = (chatMessages: Message$1[], functionCall: FunctionCal
|
|
1366
1386
|
*/
|
1367
1387
|
type ToolCallHandler = (chatMessages: Message$1[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
|
1368
1388
|
type RequestOptions = {
|
1389
|
+
/**
|
1390
|
+
An optional object of headers to be passed to the API endpoint.
|
1391
|
+
*/
|
1369
1392
|
headers?: Record<string, string> | Headers;
|
1393
|
+
/**
|
1394
|
+
An optional object to be passed to the API endpoint.
|
1395
|
+
*/
|
1370
1396
|
body?: object;
|
1371
1397
|
};
|
1372
1398
|
type ChatRequestOptions = {
|
1399
|
+
/**
|
1400
|
+
The options to be passed to the fetch call.
|
1401
|
+
*/
|
1373
1402
|
options?: RequestOptions;
|
1403
|
+
/**
|
1404
|
+
@deprecated
|
1405
|
+
*/
|
1374
1406
|
functions?: Array<Function>;
|
1407
|
+
/**
|
1408
|
+
@deprecated
|
1409
|
+
*/
|
1375
1410
|
function_call?: FunctionCall$1;
|
1411
|
+
/**
|
1412
|
+
@deprecated
|
1413
|
+
*/
|
1376
1414
|
tools?: Array<Tool>;
|
1415
|
+
/**
|
1416
|
+
@deprecated
|
1417
|
+
*/
|
1377
1418
|
tool_choice?: ToolChoice;
|
1419
|
+
/**
|
1420
|
+
Additional data to be sent to the server.
|
1421
|
+
*/
|
1378
1422
|
data?: Record<string, string>;
|
1379
1423
|
};
|
1380
1424
|
type UseChatOptions = {
|
@@ -2417,4 +2461,4 @@ declare class StreamingTextResponse extends Response {
|
|
2417
2461
|
constructor(res: ReadableStream, init?: ResponseInit, data?: StreamData);
|
2418
2462
|
}
|
2419
2463
|
|
2420
|
-
export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, 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,
|
2464
|
+
export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, 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, ObjectStreamInputPart, ObjectStreamPart, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, TokenUsage, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, 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
@@ -168,9 +168,21 @@ declare class EmbedManyResult<VALUE> {
|
|
168
168
|
});
|
169
169
|
}
|
170
170
|
|
171
|
+
/**
|
172
|
+
Represents the number of tokens used in a prompt and completion.
|
173
|
+
*/
|
171
174
|
type TokenUsage = {
|
175
|
+
/**
|
176
|
+
The number of tokens used in the prompt
|
177
|
+
*/
|
172
178
|
promptTokens: number;
|
179
|
+
/**
|
180
|
+
The number of tokens used in the completion.
|
181
|
+
*/
|
173
182
|
completionTokens: number;
|
183
|
+
/**
|
184
|
+
The total number of tokens used (promptTokens + completionTokens).
|
185
|
+
*/
|
174
186
|
totalTokens: number;
|
175
187
|
};
|
176
188
|
|
@@ -595,7 +607,7 @@ Default and recommended: 'auto' (best mode for the model).
|
|
595
607
|
*/
|
596
608
|
mode?: 'auto' | 'json' | 'tool' | 'grammar';
|
597
609
|
}): Promise<StreamObjectResult<T>>;
|
598
|
-
type
|
610
|
+
type ObjectStreamInputPart = {
|
599
611
|
type: 'error';
|
600
612
|
error: unknown;
|
601
613
|
} | {
|
@@ -608,7 +620,7 @@ type ObjectStreamPartInput = {
|
|
608
620
|
totalTokens: number;
|
609
621
|
};
|
610
622
|
};
|
611
|
-
type ObjectStreamPart<T> =
|
623
|
+
type ObjectStreamPart<T> = ObjectStreamInputPart | {
|
612
624
|
type: 'object';
|
613
625
|
object: DeepPartial<T>;
|
614
626
|
};
|
@@ -616,12 +628,16 @@ type ObjectStreamPart<T> = ObjectStreamPartInput | {
|
|
616
628
|
The result of a `streamObject` call that contains the partial object stream and additional information.
|
617
629
|
*/
|
618
630
|
declare class StreamObjectResult<T> {
|
619
|
-
|
631
|
+
readonly originalStream: ReadableStream<ObjectStreamPart<T>>;
|
620
632
|
/**
|
621
633
|
Warnings from the model provider (e.g. unsupported settings)
|
622
634
|
*/
|
623
635
|
readonly warnings: CallWarning[] | undefined;
|
624
636
|
/**
|
637
|
+
The token usage of the generated response. Resolved when the response is finished.
|
638
|
+
*/
|
639
|
+
readonly usage: Promise<TokenUsage>;
|
640
|
+
/**
|
625
641
|
Optional raw response data.
|
626
642
|
*/
|
627
643
|
rawResponse?: {
|
@@ -631,7 +647,7 @@ declare class StreamObjectResult<T> {
|
|
631
647
|
headers?: Record<string, string>;
|
632
648
|
};
|
633
649
|
constructor({ stream, warnings, rawResponse, }: {
|
634
|
-
stream: ReadableStream<string |
|
650
|
+
stream: ReadableStream<string | ObjectStreamInputPart>;
|
635
651
|
warnings: CallWarning[] | undefined;
|
636
652
|
rawResponse?: {
|
637
653
|
headers?: Record<string, string>;
|
@@ -841,12 +857,12 @@ If set and supported by the model, calls will generate deterministic results.
|
|
841
857
|
@param maxRetries - Maximum number of retries. Set to 0 to disable retries. Default: 2.
|
842
858
|
@param abortSignal - An optional abort signal that can be used to cancel the call.
|
843
859
|
|
844
|
-
@param
|
860
|
+
@param maxToolRoundtrips - Maximal number of automatic roundtrips for tool calls.
|
845
861
|
|
846
862
|
@returns
|
847
863
|
A result object that contains the generated text, the results of the tool calls, and additional information.
|
848
864
|
*/
|
849
|
-
declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, maxAutomaticRoundtrips, ...settings }: CallSettings & Prompt & {
|
865
|
+
declare function generateText<TOOLS extends Record<string, CoreTool>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, maxAutomaticRoundtrips, maxToolRoundtrips, ...settings }: CallSettings & Prompt & {
|
850
866
|
/**
|
851
867
|
The language model to use.
|
852
868
|
*/
|
@@ -860,6 +876,10 @@ The tool choice strategy. Default: 'auto'.
|
|
860
876
|
*/
|
861
877
|
toolChoice?: CoreToolChoice<TOOLS>;
|
862
878
|
/**
|
879
|
+
@deprecated Use `maxToolRoundtrips` instead.
|
880
|
+
*/
|
881
|
+
maxAutomaticRoundtrips?: number;
|
882
|
+
/**
|
863
883
|
Maximal number of automatic roundtrips for tool calls.
|
864
884
|
|
865
885
|
An automatic tool call roundtrip is another LLM call with the
|
@@ -871,7 +891,7 @@ case of misconfigured tools.
|
|
871
891
|
|
872
892
|
By default, it's set to 0, which will disable the feature.
|
873
893
|
*/
|
874
|
-
|
894
|
+
maxToolRoundtrips?: number;
|
875
895
|
}): Promise<GenerateTextResult<TOOLS>>;
|
876
896
|
/**
|
877
897
|
The result of a `generateText` call.
|
@@ -1366,15 +1386,39 @@ type FunctionCallHandler = (chatMessages: Message$1[], functionCall: FunctionCal
|
|
1366
1386
|
*/
|
1367
1387
|
type ToolCallHandler = (chatMessages: Message$1[], toolCalls: ToolCall[]) => Promise<ChatRequest | void>;
|
1368
1388
|
type RequestOptions = {
|
1389
|
+
/**
|
1390
|
+
An optional object of headers to be passed to the API endpoint.
|
1391
|
+
*/
|
1369
1392
|
headers?: Record<string, string> | Headers;
|
1393
|
+
/**
|
1394
|
+
An optional object to be passed to the API endpoint.
|
1395
|
+
*/
|
1370
1396
|
body?: object;
|
1371
1397
|
};
|
1372
1398
|
type ChatRequestOptions = {
|
1399
|
+
/**
|
1400
|
+
The options to be passed to the fetch call.
|
1401
|
+
*/
|
1373
1402
|
options?: RequestOptions;
|
1403
|
+
/**
|
1404
|
+
@deprecated
|
1405
|
+
*/
|
1374
1406
|
functions?: Array<Function>;
|
1407
|
+
/**
|
1408
|
+
@deprecated
|
1409
|
+
*/
|
1375
1410
|
function_call?: FunctionCall$1;
|
1411
|
+
/**
|
1412
|
+
@deprecated
|
1413
|
+
*/
|
1376
1414
|
tools?: Array<Tool>;
|
1415
|
+
/**
|
1416
|
+
@deprecated
|
1417
|
+
*/
|
1377
1418
|
tool_choice?: ToolChoice;
|
1419
|
+
/**
|
1420
|
+
Additional data to be sent to the server.
|
1421
|
+
*/
|
1378
1422
|
data?: Record<string, string>;
|
1379
1423
|
};
|
1380
1424
|
type UseChatOptions = {
|
@@ -2417,4 +2461,4 @@ declare class StreamingTextResponse extends Response {
|
|
2417
2461
|
constructor(res: ReadableStream, init?: ResponseInit, data?: StreamData);
|
2418
2462
|
}
|
2419
2463
|
|
2420
|
-
export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, 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,
|
2464
|
+
export { AIStream, AIStreamCallbacksAndOptions, AIStreamParser, AIStreamParserOptions, AWSBedrockAnthropicMessagesStream, AWSBedrockAnthropicStream, AWSBedrockCohereStream, AWSBedrockLlama2Stream, AWSBedrockStream, AnthropicStream, AssistantContent, AssistantMessage, AssistantResponse, AssistantStatus, CallWarning, ChatRequest, ChatRequestOptions, CohereStream, CompletionUsage, CoreAssistantMessage, CoreMessage, CoreSystemMessage, CoreTool, CoreToolChoice, CoreToolMessage, CoreUserMessage, CreateMessage, DataContent, DataMessage, DeepPartial, EmbedManyResult, EmbedResult, Embedding, EmbeddingModel, 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, ObjectStreamInputPart, ObjectStreamPart, OpenAIStream, OpenAIStreamCallbacks, ReactResponseRow, ReplicateStream, RequestOptions, StreamData, StreamObjectResult, StreamPart, StreamString, StreamTextResult, StreamingTextResponse, TextPart$1 as TextPart, TextStreamPart, TokenUsage, Tool, ToolCall, ToolCallHandler, ToolCallPart, ToolCallPayload, ToolChoice, ToolContent, ToolInvocation, ToolResultPart, UseAssistantOptions, UseChatOptions, UseCompletionOptions, UserContent, convertDataContentToBase64String, convertDataContentToUint8Array, convertToCoreMessages, createCallbacksTransformer, createChunkDecoder, createEventStreamTransformer, createStreamDataTransformer, embed, embedMany, 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
@@ -1212,57 +1212,72 @@ var StreamObjectResult = class {
|
|
1212
1212
|
warnings,
|
1213
1213
|
rawResponse
|
1214
1214
|
}) {
|
1215
|
-
this.originalStream = stream;
|
1216
1215
|
this.warnings = warnings;
|
1217
1216
|
this.rawResponse = rawResponse;
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
let latestObject = void 0;
|
1222
|
-
return createAsyncIterableStream(this.originalStream, {
|
1223
|
-
transform(chunk, controller) {
|
1224
|
-
if (typeof chunk === "string") {
|
1225
|
-
accumulatedText += chunk;
|
1226
|
-
const currentObject = parsePartialJson(
|
1227
|
-
accumulatedText
|
1228
|
-
);
|
1229
|
-
if (!isDeepEqualData(latestObject, currentObject)) {
|
1230
|
-
latestObject = currentObject;
|
1231
|
-
controller.enqueue(currentObject);
|
1232
|
-
}
|
1233
|
-
} else if (chunk.type === "error") {
|
1234
|
-
throw chunk.error;
|
1235
|
-
}
|
1236
|
-
}
|
1217
|
+
let resolveUsage;
|
1218
|
+
this.usage = new Promise((resolve) => {
|
1219
|
+
resolveUsage = resolve;
|
1237
1220
|
});
|
1238
|
-
|
1239
|
-
get fullStream() {
|
1221
|
+
let usage;
|
1240
1222
|
let accumulatedText = "";
|
1241
1223
|
let latestObject = void 0;
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
latestObject
|
1251
|
-
|
1224
|
+
this.originalStream = stream.pipeThrough(
|
1225
|
+
new TransformStream({
|
1226
|
+
async transform(chunk, controller) {
|
1227
|
+
if (typeof chunk === "string") {
|
1228
|
+
accumulatedText += chunk;
|
1229
|
+
const currentObject = parsePartialJson(
|
1230
|
+
accumulatedText
|
1231
|
+
);
|
1232
|
+
if (!isDeepEqualData(latestObject, currentObject)) {
|
1233
|
+
latestObject = currentObject;
|
1234
|
+
controller.enqueue({ type: "object", object: currentObject });
|
1235
|
+
}
|
1236
|
+
return;
|
1252
1237
|
}
|
1253
|
-
} else {
|
1254
1238
|
switch (chunk.type) {
|
1255
|
-
case "finish":
|
1239
|
+
case "finish": {
|
1240
|
+
usage = calculateTokenUsage(chunk.usage);
|
1256
1241
|
controller.enqueue({
|
1257
1242
|
...chunk,
|
1258
|
-
usage
|
1243
|
+
usage
|
1259
1244
|
});
|
1245
|
+
resolveUsage(usage);
|
1260
1246
|
break;
|
1261
|
-
|
1247
|
+
}
|
1248
|
+
default: {
|
1262
1249
|
controller.enqueue(chunk);
|
1263
1250
|
break;
|
1251
|
+
}
|
1264
1252
|
}
|
1265
1253
|
}
|
1254
|
+
})
|
1255
|
+
);
|
1256
|
+
}
|
1257
|
+
get partialObjectStream() {
|
1258
|
+
return createAsyncIterableStream(this.originalStream, {
|
1259
|
+
transform(chunk, controller) {
|
1260
|
+
switch (chunk.type) {
|
1261
|
+
case "object":
|
1262
|
+
controller.enqueue(chunk.object);
|
1263
|
+
break;
|
1264
|
+
case "finish":
|
1265
|
+
break;
|
1266
|
+
case "error":
|
1267
|
+
controller.error(chunk.error);
|
1268
|
+
break;
|
1269
|
+
default: {
|
1270
|
+
const _exhaustiveCheck = chunk;
|
1271
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
1272
|
+
}
|
1273
|
+
}
|
1274
|
+
}
|
1275
|
+
});
|
1276
|
+
}
|
1277
|
+
get fullStream() {
|
1278
|
+
return createAsyncIterableStream(this.originalStream, {
|
1279
|
+
transform(chunk, controller) {
|
1280
|
+
controller.enqueue(chunk);
|
1266
1281
|
}
|
1267
1282
|
});
|
1268
1283
|
}
|
@@ -1344,6 +1359,7 @@ async function generateText({
|
|
1344
1359
|
maxRetries,
|
1345
1360
|
abortSignal,
|
1346
1361
|
maxAutomaticRoundtrips = 0,
|
1362
|
+
maxToolRoundtrips = maxAutomaticRoundtrips,
|
1347
1363
|
...settings
|
1348
1364
|
}) {
|
1349
1365
|
var _a, _b, _c;
|
@@ -1388,7 +1404,7 @@ async function generateText({
|
|
1388
1404
|
// there are tool calls:
|
1389
1405
|
currentToolCalls.length > 0 && // all current tool calls have results:
|
1390
1406
|
currentToolResults.length === currentToolCalls.length && // the number of roundtrips is less than the maximum:
|
1391
|
-
roundtrips++ <
|
1407
|
+
roundtrips++ < maxToolRoundtrips
|
1392
1408
|
);
|
1393
1409
|
return new GenerateTextResult({
|
1394
1410
|
// Always return a string so that the caller doesn't have to check for undefined.
|