ai 6.0.22 → 6.0.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.js CHANGED
@@ -99,7 +99,6 @@ __export(src_exports, {
99
99
  getTextFromDataUrl: () => getTextFromDataUrl,
100
100
  getToolName: () => getToolName,
101
101
  getToolOrDynamicToolName: () => getToolOrDynamicToolName,
102
- getTotalTimeoutMs: () => getTotalTimeoutMs,
103
102
  hasToolCall: () => hasToolCall,
104
103
  isDataUIPart: () => isDataUIPart,
105
104
  isDeepEqualData: () => isDeepEqualData,
@@ -819,6 +818,18 @@ function getTotalTimeoutMs(timeout) {
819
818
  }
820
819
  return timeout.totalMs;
821
820
  }
821
+ function getStepTimeoutMs(timeout) {
822
+ if (timeout == null || typeof timeout === "number") {
823
+ return void 0;
824
+ }
825
+ return timeout.stepMs;
826
+ }
827
+ function getChunkTimeoutMs(timeout) {
828
+ if (timeout == null || typeof timeout === "number") {
829
+ return void 0;
830
+ }
831
+ return timeout.chunkMs;
832
+ }
822
833
 
823
834
  // src/prompt/convert-to-language-model-prompt.ts
824
835
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
@@ -1013,7 +1024,7 @@ var import_provider_utils3 = require("@ai-sdk/provider-utils");
1013
1024
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1014
1025
 
1015
1026
  // src/version.ts
1016
- var VERSION = true ? "6.0.22" : "0.0.0-test";
1027
+ var VERSION = true ? "6.0.24" : "0.0.0-test";
1017
1028
 
1018
1029
  // src/util/download/download.ts
1019
1030
  var download = async ({ url }) => {
@@ -3667,9 +3678,12 @@ async function generateText({
3667
3678
  const model = resolveLanguageModel(modelArg);
3668
3679
  const stopConditions = asArray(stopWhen);
3669
3680
  const totalTimeoutMs = getTotalTimeoutMs(timeout);
3681
+ const stepTimeoutMs = getStepTimeoutMs(timeout);
3682
+ const stepAbortController = stepTimeoutMs != null ? new AbortController() : void 0;
3670
3683
  const mergedAbortSignal = mergeAbortSignals(
3671
3684
  abortSignal,
3672
- totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0
3685
+ totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0,
3686
+ stepAbortController == null ? void 0 : stepAbortController.signal
3673
3687
  );
3674
3688
  const { maxRetries, retry } = prepareRetries({
3675
3689
  maxRetries: maxRetriesArg,
@@ -3798,263 +3812,270 @@ async function generateText({
3798
3812
  const steps = [];
3799
3813
  const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
3800
3814
  do {
3801
- const stepInputMessages = [...initialMessages, ...responseMessages];
3802
- const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
3803
- model,
3804
- steps,
3805
- stepNumber: steps.length,
3806
- messages: stepInputMessages,
3807
- experimental_context
3808
- }));
3809
- const stepModel = resolveLanguageModel(
3810
- (_a16 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a16 : model
3811
- );
3812
- const promptMessages = await convertToLanguageModelPrompt({
3813
- prompt: {
3814
- system: (_b = prepareStepResult == null ? void 0 : prepareStepResult.system) != null ? _b : initialPrompt.system,
3815
- messages: (_c = prepareStepResult == null ? void 0 : prepareStepResult.messages) != null ? _c : stepInputMessages
3816
- },
3817
- supportedUrls: await stepModel.supportedUrls,
3818
- download: download2
3819
- });
3820
- experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
3821
- const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
3822
- tools,
3823
- toolChoice: (_e = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _e : toolChoice,
3824
- activeTools: (_f = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _f : activeTools
3825
- });
3826
- currentModelResponse = await retry(
3827
- () => {
3828
- var _a17;
3829
- return recordSpan({
3830
- name: "ai.generateText.doGenerate",
3831
- attributes: selectTelemetryAttributes({
3832
- telemetry,
3833
- attributes: {
3834
- ...assembleOperationName({
3835
- operationId: "ai.generateText.doGenerate",
3836
- telemetry
3837
- }),
3838
- ...baseTelemetryAttributes,
3839
- // model:
3840
- "ai.model.provider": stepModel.provider,
3841
- "ai.model.id": stepModel.modelId,
3842
- // prompt:
3843
- "ai.prompt.messages": {
3844
- input: () => stringifyForTelemetry(promptMessages)
3845
- },
3846
- "ai.prompt.tools": {
3847
- // convert the language model level tools:
3848
- input: () => stepTools == null ? void 0 : stepTools.map((tool2) => JSON.stringify(tool2))
3849
- },
3850
- "ai.prompt.toolChoice": {
3851
- input: () => stepToolChoice != null ? JSON.stringify(stepToolChoice) : void 0
3852
- },
3853
- // standardized gen-ai llm span attributes:
3854
- "gen_ai.system": stepModel.provider,
3855
- "gen_ai.request.model": stepModel.modelId,
3856
- "gen_ai.request.frequency_penalty": settings.frequencyPenalty,
3857
- "gen_ai.request.max_tokens": settings.maxOutputTokens,
3858
- "gen_ai.request.presence_penalty": settings.presencePenalty,
3859
- "gen_ai.request.stop_sequences": settings.stopSequences,
3860
- "gen_ai.request.temperature": (_a17 = settings.temperature) != null ? _a17 : void 0,
3861
- "gen_ai.request.top_k": settings.topK,
3862
- "gen_ai.request.top_p": settings.topP
3815
+ const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
3816
+ try {
3817
+ const stepInputMessages = [...initialMessages, ...responseMessages];
3818
+ const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
3819
+ model,
3820
+ steps,
3821
+ stepNumber: steps.length,
3822
+ messages: stepInputMessages,
3823
+ experimental_context
3824
+ }));
3825
+ const stepModel = resolveLanguageModel(
3826
+ (_a16 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a16 : model
3827
+ );
3828
+ const promptMessages = await convertToLanguageModelPrompt({
3829
+ prompt: {
3830
+ system: (_b = prepareStepResult == null ? void 0 : prepareStepResult.system) != null ? _b : initialPrompt.system,
3831
+ messages: (_c = prepareStepResult == null ? void 0 : prepareStepResult.messages) != null ? _c : stepInputMessages
3832
+ },
3833
+ supportedUrls: await stepModel.supportedUrls,
3834
+ download: download2
3835
+ });
3836
+ experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
3837
+ const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
3838
+ tools,
3839
+ toolChoice: (_e = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _e : toolChoice,
3840
+ activeTools: (_f = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _f : activeTools
3841
+ });
3842
+ currentModelResponse = await retry(
3843
+ () => {
3844
+ var _a17;
3845
+ return recordSpan({
3846
+ name: "ai.generateText.doGenerate",
3847
+ attributes: selectTelemetryAttributes({
3848
+ telemetry,
3849
+ attributes: {
3850
+ ...assembleOperationName({
3851
+ operationId: "ai.generateText.doGenerate",
3852
+ telemetry
3853
+ }),
3854
+ ...baseTelemetryAttributes,
3855
+ // model:
3856
+ "ai.model.provider": stepModel.provider,
3857
+ "ai.model.id": stepModel.modelId,
3858
+ // prompt:
3859
+ "ai.prompt.messages": {
3860
+ input: () => stringifyForTelemetry(promptMessages)
3861
+ },
3862
+ "ai.prompt.tools": {
3863
+ // convert the language model level tools:
3864
+ input: () => stepTools == null ? void 0 : stepTools.map((tool2) => JSON.stringify(tool2))
3865
+ },
3866
+ "ai.prompt.toolChoice": {
3867
+ input: () => stepToolChoice != null ? JSON.stringify(stepToolChoice) : void 0
3868
+ },
3869
+ // standardized gen-ai llm span attributes:
3870
+ "gen_ai.system": stepModel.provider,
3871
+ "gen_ai.request.model": stepModel.modelId,
3872
+ "gen_ai.request.frequency_penalty": settings.frequencyPenalty,
3873
+ "gen_ai.request.max_tokens": settings.maxOutputTokens,
3874
+ "gen_ai.request.presence_penalty": settings.presencePenalty,
3875
+ "gen_ai.request.stop_sequences": settings.stopSequences,
3876
+ "gen_ai.request.temperature": (_a17 = settings.temperature) != null ? _a17 : void 0,
3877
+ "gen_ai.request.top_k": settings.topK,
3878
+ "gen_ai.request.top_p": settings.topP
3879
+ }
3880
+ }),
3881
+ tracer,
3882
+ fn: async (span2) => {
3883
+ var _a18, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
3884
+ const stepProviderOptions = mergeObjects(
3885
+ providerOptions,
3886
+ prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
3887
+ );
3888
+ const result = await stepModel.doGenerate({
3889
+ ...callSettings2,
3890
+ tools: stepTools,
3891
+ toolChoice: stepToolChoice,
3892
+ responseFormat: await (output == null ? void 0 : output.responseFormat),
3893
+ prompt: promptMessages,
3894
+ providerOptions: stepProviderOptions,
3895
+ abortSignal: mergedAbortSignal,
3896
+ headers: headersWithUserAgent
3897
+ });
3898
+ const responseData = {
3899
+ id: (_b2 = (_a18 = result.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId2(),
3900
+ timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : /* @__PURE__ */ new Date(),
3901
+ modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
3902
+ headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
3903
+ body: (_h2 = result.response) == null ? void 0 : _h2.body
3904
+ };
3905
+ span2.setAttributes(
3906
+ await selectTelemetryAttributes({
3907
+ telemetry,
3908
+ attributes: {
3909
+ "ai.response.finishReason": result.finishReason.unified,
3910
+ "ai.response.text": {
3911
+ output: () => extractTextContent(result.content)
3912
+ },
3913
+ "ai.response.toolCalls": {
3914
+ output: () => {
3915
+ const toolCalls = asToolCalls(result.content);
3916
+ return toolCalls == null ? void 0 : JSON.stringify(toolCalls);
3917
+ }
3918
+ },
3919
+ "ai.response.id": responseData.id,
3920
+ "ai.response.model": responseData.modelId,
3921
+ "ai.response.timestamp": responseData.timestamp.toISOString(),
3922
+ "ai.response.providerMetadata": JSON.stringify(
3923
+ result.providerMetadata
3924
+ ),
3925
+ // TODO rename telemetry attributes to inputTokens and outputTokens
3926
+ "ai.usage.promptTokens": result.usage.inputTokens.total,
3927
+ "ai.usage.completionTokens": result.usage.outputTokens.total,
3928
+ // standardized gen-ai llm span attributes:
3929
+ "gen_ai.response.finish_reasons": [
3930
+ result.finishReason.unified
3931
+ ],
3932
+ "gen_ai.response.id": responseData.id,
3933
+ "gen_ai.response.model": responseData.modelId,
3934
+ "gen_ai.usage.input_tokens": result.usage.inputTokens.total,
3935
+ "gen_ai.usage.output_tokens": result.usage.outputTokens.total
3936
+ }
3937
+ })
3938
+ );
3939
+ return { ...result, response: responseData };
3863
3940
  }
3864
- }),
3865
- tracer,
3866
- fn: async (span2) => {
3867
- var _a18, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
3868
- const stepProviderOptions = mergeObjects(
3869
- providerOptions,
3870
- prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
3871
- );
3872
- const result = await stepModel.doGenerate({
3873
- ...callSettings2,
3874
- tools: stepTools,
3875
- toolChoice: stepToolChoice,
3876
- responseFormat: await (output == null ? void 0 : output.responseFormat),
3877
- prompt: promptMessages,
3878
- providerOptions: stepProviderOptions,
3879
- abortSignal: mergedAbortSignal,
3880
- headers: headersWithUserAgent
3881
- });
3882
- const responseData = {
3883
- id: (_b2 = (_a18 = result.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId2(),
3884
- timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : /* @__PURE__ */ new Date(),
3885
- modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
3886
- headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
3887
- body: (_h2 = result.response) == null ? void 0 : _h2.body
3888
- };
3889
- span2.setAttributes(
3890
- await selectTelemetryAttributes({
3891
- telemetry,
3892
- attributes: {
3893
- "ai.response.finishReason": result.finishReason.unified,
3894
- "ai.response.text": {
3895
- output: () => extractTextContent(result.content)
3896
- },
3897
- "ai.response.toolCalls": {
3898
- output: () => {
3899
- const toolCalls = asToolCalls(result.content);
3900
- return toolCalls == null ? void 0 : JSON.stringify(toolCalls);
3901
- }
3902
- },
3903
- "ai.response.id": responseData.id,
3904
- "ai.response.model": responseData.modelId,
3905
- "ai.response.timestamp": responseData.timestamp.toISOString(),
3906
- "ai.response.providerMetadata": JSON.stringify(
3907
- result.providerMetadata
3908
- ),
3909
- // TODO rename telemetry attributes to inputTokens and outputTokens
3910
- "ai.usage.promptTokens": result.usage.inputTokens.total,
3911
- "ai.usage.completionTokens": result.usage.outputTokens.total,
3912
- // standardized gen-ai llm span attributes:
3913
- "gen_ai.response.finish_reasons": [
3914
- result.finishReason.unified
3915
- ],
3916
- "gen_ai.response.id": responseData.id,
3917
- "gen_ai.response.model": responseData.modelId,
3918
- "gen_ai.usage.input_tokens": result.usage.inputTokens.total,
3919
- "gen_ai.usage.output_tokens": result.usage.outputTokens.total
3920
- }
3921
- })
3922
- );
3923
- return { ...result, response: responseData };
3924
- }
3925
- });
3926
- }
3927
- );
3928
- const stepToolCalls = await Promise.all(
3929
- currentModelResponse.content.filter(
3930
- (part) => part.type === "tool-call"
3931
- ).map(
3932
- (toolCall) => parseToolCall({
3941
+ });
3942
+ }
3943
+ );
3944
+ const stepToolCalls = await Promise.all(
3945
+ currentModelResponse.content.filter(
3946
+ (part) => part.type === "tool-call"
3947
+ ).map(
3948
+ (toolCall) => parseToolCall({
3949
+ toolCall,
3950
+ tools,
3951
+ repairToolCall,
3952
+ system,
3953
+ messages: stepInputMessages
3954
+ })
3955
+ )
3956
+ );
3957
+ const toolApprovalRequests = {};
3958
+ for (const toolCall of stepToolCalls) {
3959
+ if (toolCall.invalid) {
3960
+ continue;
3961
+ }
3962
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3963
+ if (tool2 == null) {
3964
+ continue;
3965
+ }
3966
+ if ((tool2 == null ? void 0 : tool2.onInputAvailable) != null) {
3967
+ await tool2.onInputAvailable({
3968
+ input: toolCall.input,
3969
+ toolCallId: toolCall.toolCallId,
3970
+ messages: stepInputMessages,
3971
+ abortSignal: mergedAbortSignal,
3972
+ experimental_context
3973
+ });
3974
+ }
3975
+ if (await isApprovalNeeded({
3976
+ tool: tool2,
3933
3977
  toolCall,
3934
- tools,
3935
- repairToolCall,
3936
- system,
3937
- messages: stepInputMessages
3938
- })
3939
- )
3940
- );
3941
- const toolApprovalRequests = {};
3942
- for (const toolCall of stepToolCalls) {
3943
- if (toolCall.invalid) {
3944
- continue;
3945
- }
3946
- const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3947
- if (tool2 == null) {
3948
- continue;
3949
- }
3950
- if ((tool2 == null ? void 0 : tool2.onInputAvailable) != null) {
3951
- await tool2.onInputAvailable({
3952
- input: toolCall.input,
3953
- toolCallId: toolCall.toolCallId,
3954
3978
  messages: stepInputMessages,
3955
- abortSignal: mergedAbortSignal,
3956
3979
  experimental_context
3957
- });
3980
+ })) {
3981
+ toolApprovalRequests[toolCall.toolCallId] = {
3982
+ type: "tool-approval-request",
3983
+ approvalId: generateId2(),
3984
+ toolCall
3985
+ };
3986
+ }
3958
3987
  }
3959
- if (await isApprovalNeeded({
3960
- tool: tool2,
3961
- toolCall,
3962
- messages: stepInputMessages,
3963
- experimental_context
3964
- })) {
3965
- toolApprovalRequests[toolCall.toolCallId] = {
3966
- type: "tool-approval-request",
3967
- approvalId: generateId2(),
3968
- toolCall
3969
- };
3988
+ const invalidToolCalls = stepToolCalls.filter(
3989
+ (toolCall) => toolCall.invalid && toolCall.dynamic
3990
+ );
3991
+ clientToolOutputs = [];
3992
+ for (const toolCall of invalidToolCalls) {
3993
+ clientToolOutputs.push({
3994
+ type: "tool-error",
3995
+ toolCallId: toolCall.toolCallId,
3996
+ toolName: toolCall.toolName,
3997
+ input: toolCall.input,
3998
+ error: (0, import_provider_utils15.getErrorMessage)(toolCall.error),
3999
+ dynamic: true
4000
+ });
3970
4001
  }
3971
- }
3972
- const invalidToolCalls = stepToolCalls.filter(
3973
- (toolCall) => toolCall.invalid && toolCall.dynamic
3974
- );
3975
- clientToolOutputs = [];
3976
- for (const toolCall of invalidToolCalls) {
3977
- clientToolOutputs.push({
3978
- type: "tool-error",
3979
- toolCallId: toolCall.toolCallId,
3980
- toolName: toolCall.toolName,
3981
- input: toolCall.input,
3982
- error: (0, import_provider_utils15.getErrorMessage)(toolCall.error),
3983
- dynamic: true
3984
- });
3985
- }
3986
- clientToolCalls = stepToolCalls.filter(
3987
- (toolCall) => !toolCall.providerExecuted
3988
- );
3989
- if (tools != null) {
3990
- clientToolOutputs.push(
3991
- ...await executeTools({
3992
- toolCalls: clientToolCalls.filter(
3993
- (toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
3994
- ),
3995
- tools,
3996
- tracer,
3997
- telemetry,
3998
- messages: stepInputMessages,
3999
- abortSignal: mergedAbortSignal,
4000
- experimental_context
4001
- })
4002
+ clientToolCalls = stepToolCalls.filter(
4003
+ (toolCall) => !toolCall.providerExecuted
4002
4004
  );
4003
- }
4004
- for (const toolCall of stepToolCalls) {
4005
- if (!toolCall.providerExecuted)
4006
- continue;
4007
- const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
4008
- if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
4009
- const hasResultInResponse = currentModelResponse.content.some(
4010
- (part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
4005
+ if (tools != null) {
4006
+ clientToolOutputs.push(
4007
+ ...await executeTools({
4008
+ toolCalls: clientToolCalls.filter(
4009
+ (toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
4010
+ ),
4011
+ tools,
4012
+ tracer,
4013
+ telemetry,
4014
+ messages: stepInputMessages,
4015
+ abortSignal: mergedAbortSignal,
4016
+ experimental_context
4017
+ })
4011
4018
  );
4012
- if (!hasResultInResponse) {
4013
- pendingDeferredToolCalls.set(toolCall.toolCallId, {
4014
- toolName: toolCall.toolName
4015
- });
4019
+ }
4020
+ for (const toolCall of stepToolCalls) {
4021
+ if (!toolCall.providerExecuted)
4022
+ continue;
4023
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
4024
+ if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
4025
+ const hasResultInResponse = currentModelResponse.content.some(
4026
+ (part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
4027
+ );
4028
+ if (!hasResultInResponse) {
4029
+ pendingDeferredToolCalls.set(toolCall.toolCallId, {
4030
+ toolName: toolCall.toolName
4031
+ });
4032
+ }
4016
4033
  }
4017
4034
  }
4018
- }
4019
- for (const part of currentModelResponse.content) {
4020
- if (part.type === "tool-result") {
4021
- pendingDeferredToolCalls.delete(part.toolCallId);
4035
+ for (const part of currentModelResponse.content) {
4036
+ if (part.type === "tool-result") {
4037
+ pendingDeferredToolCalls.delete(part.toolCallId);
4038
+ }
4022
4039
  }
4023
- }
4024
- const stepContent = asContent({
4025
- content: currentModelResponse.content,
4026
- toolCalls: stepToolCalls,
4027
- toolOutputs: clientToolOutputs,
4028
- toolApprovalRequests: Object.values(toolApprovalRequests),
4029
- tools
4030
- });
4031
- responseMessages.push(
4032
- ...await toResponseMessages({
4033
- content: stepContent,
4040
+ const stepContent = asContent({
4041
+ content: currentModelResponse.content,
4042
+ toolCalls: stepToolCalls,
4043
+ toolOutputs: clientToolOutputs,
4044
+ toolApprovalRequests: Object.values(toolApprovalRequests),
4034
4045
  tools
4035
- })
4036
- );
4037
- const currentStepResult = new DefaultStepResult({
4038
- content: stepContent,
4039
- finishReason: currentModelResponse.finishReason.unified,
4040
- rawFinishReason: currentModelResponse.finishReason.raw,
4041
- usage: asLanguageModelUsage(currentModelResponse.usage),
4042
- warnings: currentModelResponse.warnings,
4043
- providerMetadata: currentModelResponse.providerMetadata,
4044
- request: (_g = currentModelResponse.request) != null ? _g : {},
4045
- response: {
4046
- ...currentModelResponse.response,
4047
- // deep clone msgs to avoid mutating past messages in multi-step:
4048
- messages: structuredClone(responseMessages)
4046
+ });
4047
+ responseMessages.push(
4048
+ ...await toResponseMessages({
4049
+ content: stepContent,
4050
+ tools
4051
+ })
4052
+ );
4053
+ const currentStepResult = new DefaultStepResult({
4054
+ content: stepContent,
4055
+ finishReason: currentModelResponse.finishReason.unified,
4056
+ rawFinishReason: currentModelResponse.finishReason.raw,
4057
+ usage: asLanguageModelUsage(currentModelResponse.usage),
4058
+ warnings: currentModelResponse.warnings,
4059
+ providerMetadata: currentModelResponse.providerMetadata,
4060
+ request: (_g = currentModelResponse.request) != null ? _g : {},
4061
+ response: {
4062
+ ...currentModelResponse.response,
4063
+ // deep clone msgs to avoid mutating past messages in multi-step:
4064
+ messages: structuredClone(responseMessages)
4065
+ }
4066
+ });
4067
+ logWarnings({
4068
+ warnings: (_h = currentModelResponse.warnings) != null ? _h : [],
4069
+ provider: stepModel.provider,
4070
+ model: stepModel.modelId
4071
+ });
4072
+ steps.push(currentStepResult);
4073
+ await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
4074
+ } finally {
4075
+ if (stepTimeoutId != null) {
4076
+ clearTimeout(stepTimeoutId);
4049
4077
  }
4050
- });
4051
- logWarnings({
4052
- warnings: (_h = currentModelResponse.warnings) != null ? _h : [],
4053
- provider: stepModel.provider,
4054
- model: stepModel.modelId
4055
- });
4056
- steps.push(currentStepResult);
4057
- await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
4078
+ }
4058
4079
  } while (
4059
4080
  // Continue if:
4060
4081
  // 1. There are client tool calls that have all been executed, OR
@@ -5753,6 +5774,10 @@ function streamText({
5753
5774
  ...settings
5754
5775
  }) {
5755
5776
  const totalTimeoutMs = getTotalTimeoutMs(timeout);
5777
+ const stepTimeoutMs = getStepTimeoutMs(timeout);
5778
+ const chunkTimeoutMs = getChunkTimeoutMs(timeout);
5779
+ const stepAbortController = stepTimeoutMs != null ? new AbortController() : void 0;
5780
+ const chunkAbortController = chunkTimeoutMs != null ? new AbortController() : void 0;
5756
5781
  return new DefaultStreamTextResult({
5757
5782
  model: resolveLanguageModel(model),
5758
5783
  telemetry,
@@ -5761,8 +5786,14 @@ function streamText({
5761
5786
  maxRetries,
5762
5787
  abortSignal: mergeAbortSignals(
5763
5788
  abortSignal,
5764
- totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0
5789
+ totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0,
5790
+ stepAbortController == null ? void 0 : stepAbortController.signal,
5791
+ chunkAbortController == null ? void 0 : chunkAbortController.signal
5765
5792
  ),
5793
+ stepTimeoutMs,
5794
+ stepAbortController,
5795
+ chunkTimeoutMs,
5796
+ chunkAbortController,
5766
5797
  system,
5767
5798
  prompt,
5768
5799
  messages,
@@ -5857,6 +5888,10 @@ var DefaultStreamTextResult = class {
5857
5888
  settings,
5858
5889
  maxRetries: maxRetriesArg,
5859
5890
  abortSignal,
5891
+ stepTimeoutMs,
5892
+ stepAbortController,
5893
+ chunkTimeoutMs,
5894
+ chunkAbortController,
5860
5895
  system,
5861
5896
  prompt,
5862
5897
  messages,
@@ -6322,6 +6357,30 @@ var DefaultStreamTextResult = class {
6322
6357
  }) {
6323
6358
  var _a16, _b, _c, _d, _e, _f;
6324
6359
  const includeRawChunks2 = self.includeRawChunks;
6360
+ const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
6361
+ let chunkTimeoutId = void 0;
6362
+ function resetChunkTimeout() {
6363
+ if (chunkTimeoutMs != null) {
6364
+ if (chunkTimeoutId != null) {
6365
+ clearTimeout(chunkTimeoutId);
6366
+ }
6367
+ chunkTimeoutId = setTimeout(
6368
+ () => chunkAbortController.abort(),
6369
+ chunkTimeoutMs
6370
+ );
6371
+ }
6372
+ }
6373
+ function clearChunkTimeout() {
6374
+ if (chunkTimeoutId != null) {
6375
+ clearTimeout(chunkTimeoutId);
6376
+ chunkTimeoutId = void 0;
6377
+ }
6378
+ }
6379
+ function clearStepTimeout() {
6380
+ if (stepTimeoutId != null) {
6381
+ clearTimeout(stepTimeoutId);
6382
+ }
6383
+ }
6325
6384
  stepFinish = new import_provider_utils19.DelayedPromise();
6326
6385
  const stepInputMessages = [...initialMessages, ...responseMessages];
6327
6386
  const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
@@ -6446,6 +6505,7 @@ var DefaultStreamTextResult = class {
6446
6505
  new TransformStream({
6447
6506
  async transform(chunk, controller) {
6448
6507
  var _a17, _b2, _c2, _d2, _e2;
6508
+ resetChunkTimeout();
6449
6509
  if (chunk.type === "stream-start") {
6450
6510
  warnings = chunk.warnings;
6451
6511
  return;
@@ -6676,6 +6736,8 @@ var DefaultStreamTextResult = class {
6676
6736
  pendingDeferredToolCalls.delete(output2.toolCallId);
6677
6737
  }
6678
6738
  }
6739
+ clearStepTimeout();
6740
+ clearChunkTimeout();
6679
6741
  if (
6680
6742
  // Continue if:
6681
6743
  // 1. There are client tool calls that have all been executed, OR
@@ -10233,7 +10295,16 @@ function smoothStream({
10233
10295
  _internal: { delay: delay2 = import_provider_utils32.delay } = {}
10234
10296
  } = {}) {
10235
10297
  let detectChunk;
10236
- if (typeof chunking === "function") {
10298
+ if (chunking != null && typeof chunking === "object" && "segment" in chunking && typeof chunking.segment === "function") {
10299
+ const segmenter = chunking;
10300
+ detectChunk = (buffer) => {
10301
+ if (buffer.length === 0)
10302
+ return null;
10303
+ const iterator = segmenter.segment(buffer)[Symbol.iterator]();
10304
+ const first = iterator.next().value;
10305
+ return (first == null ? void 0 : first.segment) || null;
10306
+ };
10307
+ } else if (typeof chunking === "function") {
10237
10308
  detectChunk = (buffer) => {
10238
10309
  const match = chunking(buffer);
10239
10310
  if (match == null) {
@@ -10250,11 +10321,11 @@ function smoothStream({
10250
10321
  return match;
10251
10322
  };
10252
10323
  } else {
10253
- const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
10324
+ const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking instanceof RegExp ? chunking : void 0;
10254
10325
  if (chunkingRegex == null) {
10255
10326
  throw new import_provider28.InvalidArgumentError({
10256
10327
  argument: "chunking",
10257
- message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
10328
+ message: `Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`
10258
10329
  });
10259
10330
  }
10260
10331
  detectChunk = (buffer) => {
@@ -12120,7 +12191,6 @@ var TextStreamChatTransport = class extends HttpChatTransport {
12120
12191
  getTextFromDataUrl,
12121
12192
  getToolName,
12122
12193
  getToolOrDynamicToolName,
12123
- getTotalTimeoutMs,
12124
12194
  hasToolCall,
12125
12195
  isDataUIPart,
12126
12196
  isDeepEqualData,