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.mjs CHANGED
@@ -710,6 +710,18 @@ function getTotalTimeoutMs(timeout) {
710
710
  }
711
711
  return timeout.totalMs;
712
712
  }
713
+ function getStepTimeoutMs(timeout) {
714
+ if (timeout == null || typeof timeout === "number") {
715
+ return void 0;
716
+ }
717
+ return timeout.stepMs;
718
+ }
719
+ function getChunkTimeoutMs(timeout) {
720
+ if (timeout == null || typeof timeout === "number") {
721
+ return void 0;
722
+ }
723
+ return timeout.chunkMs;
724
+ }
713
725
 
714
726
  // src/prompt/convert-to-language-model-prompt.ts
715
727
  import {
@@ -909,7 +921,7 @@ import {
909
921
  } from "@ai-sdk/provider-utils";
910
922
 
911
923
  // src/version.ts
912
- var VERSION = true ? "6.0.22" : "0.0.0-test";
924
+ var VERSION = true ? "6.0.24" : "0.0.0-test";
913
925
 
914
926
  // src/util/download/download.ts
915
927
  var download = async ({ url }) => {
@@ -3582,9 +3594,12 @@ async function generateText({
3582
3594
  const model = resolveLanguageModel(modelArg);
3583
3595
  const stopConditions = asArray(stopWhen);
3584
3596
  const totalTimeoutMs = getTotalTimeoutMs(timeout);
3597
+ const stepTimeoutMs = getStepTimeoutMs(timeout);
3598
+ const stepAbortController = stepTimeoutMs != null ? new AbortController() : void 0;
3585
3599
  const mergedAbortSignal = mergeAbortSignals(
3586
3600
  abortSignal,
3587
- totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0
3601
+ totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0,
3602
+ stepAbortController == null ? void 0 : stepAbortController.signal
3588
3603
  );
3589
3604
  const { maxRetries, retry } = prepareRetries({
3590
3605
  maxRetries: maxRetriesArg,
@@ -3713,263 +3728,270 @@ async function generateText({
3713
3728
  const steps = [];
3714
3729
  const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
3715
3730
  do {
3716
- const stepInputMessages = [...initialMessages, ...responseMessages];
3717
- const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
3718
- model,
3719
- steps,
3720
- stepNumber: steps.length,
3721
- messages: stepInputMessages,
3722
- experimental_context
3723
- }));
3724
- const stepModel = resolveLanguageModel(
3725
- (_a16 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a16 : model
3726
- );
3727
- const promptMessages = await convertToLanguageModelPrompt({
3728
- prompt: {
3729
- system: (_b = prepareStepResult == null ? void 0 : prepareStepResult.system) != null ? _b : initialPrompt.system,
3730
- messages: (_c = prepareStepResult == null ? void 0 : prepareStepResult.messages) != null ? _c : stepInputMessages
3731
- },
3732
- supportedUrls: await stepModel.supportedUrls,
3733
- download: download2
3734
- });
3735
- experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
3736
- const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
3737
- tools,
3738
- toolChoice: (_e = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _e : toolChoice,
3739
- activeTools: (_f = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _f : activeTools
3740
- });
3741
- currentModelResponse = await retry(
3742
- () => {
3743
- var _a17;
3744
- return recordSpan({
3745
- name: "ai.generateText.doGenerate",
3746
- attributes: selectTelemetryAttributes({
3747
- telemetry,
3748
- attributes: {
3749
- ...assembleOperationName({
3750
- operationId: "ai.generateText.doGenerate",
3751
- telemetry
3752
- }),
3753
- ...baseTelemetryAttributes,
3754
- // model:
3755
- "ai.model.provider": stepModel.provider,
3756
- "ai.model.id": stepModel.modelId,
3757
- // prompt:
3758
- "ai.prompt.messages": {
3759
- input: () => stringifyForTelemetry(promptMessages)
3760
- },
3761
- "ai.prompt.tools": {
3762
- // convert the language model level tools:
3763
- input: () => stepTools == null ? void 0 : stepTools.map((tool2) => JSON.stringify(tool2))
3764
- },
3765
- "ai.prompt.toolChoice": {
3766
- input: () => stepToolChoice != null ? JSON.stringify(stepToolChoice) : void 0
3767
- },
3768
- // standardized gen-ai llm span attributes:
3769
- "gen_ai.system": stepModel.provider,
3770
- "gen_ai.request.model": stepModel.modelId,
3771
- "gen_ai.request.frequency_penalty": settings.frequencyPenalty,
3772
- "gen_ai.request.max_tokens": settings.maxOutputTokens,
3773
- "gen_ai.request.presence_penalty": settings.presencePenalty,
3774
- "gen_ai.request.stop_sequences": settings.stopSequences,
3775
- "gen_ai.request.temperature": (_a17 = settings.temperature) != null ? _a17 : void 0,
3776
- "gen_ai.request.top_k": settings.topK,
3777
- "gen_ai.request.top_p": settings.topP
3731
+ const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
3732
+ try {
3733
+ const stepInputMessages = [...initialMessages, ...responseMessages];
3734
+ const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
3735
+ model,
3736
+ steps,
3737
+ stepNumber: steps.length,
3738
+ messages: stepInputMessages,
3739
+ experimental_context
3740
+ }));
3741
+ const stepModel = resolveLanguageModel(
3742
+ (_a16 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a16 : model
3743
+ );
3744
+ const promptMessages = await convertToLanguageModelPrompt({
3745
+ prompt: {
3746
+ system: (_b = prepareStepResult == null ? void 0 : prepareStepResult.system) != null ? _b : initialPrompt.system,
3747
+ messages: (_c = prepareStepResult == null ? void 0 : prepareStepResult.messages) != null ? _c : stepInputMessages
3748
+ },
3749
+ supportedUrls: await stepModel.supportedUrls,
3750
+ download: download2
3751
+ });
3752
+ experimental_context = (_d = prepareStepResult == null ? void 0 : prepareStepResult.experimental_context) != null ? _d : experimental_context;
3753
+ const { toolChoice: stepToolChoice, tools: stepTools } = await prepareToolsAndToolChoice({
3754
+ tools,
3755
+ toolChoice: (_e = prepareStepResult == null ? void 0 : prepareStepResult.toolChoice) != null ? _e : toolChoice,
3756
+ activeTools: (_f = prepareStepResult == null ? void 0 : prepareStepResult.activeTools) != null ? _f : activeTools
3757
+ });
3758
+ currentModelResponse = await retry(
3759
+ () => {
3760
+ var _a17;
3761
+ return recordSpan({
3762
+ name: "ai.generateText.doGenerate",
3763
+ attributes: selectTelemetryAttributes({
3764
+ telemetry,
3765
+ attributes: {
3766
+ ...assembleOperationName({
3767
+ operationId: "ai.generateText.doGenerate",
3768
+ telemetry
3769
+ }),
3770
+ ...baseTelemetryAttributes,
3771
+ // model:
3772
+ "ai.model.provider": stepModel.provider,
3773
+ "ai.model.id": stepModel.modelId,
3774
+ // prompt:
3775
+ "ai.prompt.messages": {
3776
+ input: () => stringifyForTelemetry(promptMessages)
3777
+ },
3778
+ "ai.prompt.tools": {
3779
+ // convert the language model level tools:
3780
+ input: () => stepTools == null ? void 0 : stepTools.map((tool2) => JSON.stringify(tool2))
3781
+ },
3782
+ "ai.prompt.toolChoice": {
3783
+ input: () => stepToolChoice != null ? JSON.stringify(stepToolChoice) : void 0
3784
+ },
3785
+ // standardized gen-ai llm span attributes:
3786
+ "gen_ai.system": stepModel.provider,
3787
+ "gen_ai.request.model": stepModel.modelId,
3788
+ "gen_ai.request.frequency_penalty": settings.frequencyPenalty,
3789
+ "gen_ai.request.max_tokens": settings.maxOutputTokens,
3790
+ "gen_ai.request.presence_penalty": settings.presencePenalty,
3791
+ "gen_ai.request.stop_sequences": settings.stopSequences,
3792
+ "gen_ai.request.temperature": (_a17 = settings.temperature) != null ? _a17 : void 0,
3793
+ "gen_ai.request.top_k": settings.topK,
3794
+ "gen_ai.request.top_p": settings.topP
3795
+ }
3796
+ }),
3797
+ tracer,
3798
+ fn: async (span2) => {
3799
+ var _a18, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
3800
+ const stepProviderOptions = mergeObjects(
3801
+ providerOptions,
3802
+ prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
3803
+ );
3804
+ const result = await stepModel.doGenerate({
3805
+ ...callSettings2,
3806
+ tools: stepTools,
3807
+ toolChoice: stepToolChoice,
3808
+ responseFormat: await (output == null ? void 0 : output.responseFormat),
3809
+ prompt: promptMessages,
3810
+ providerOptions: stepProviderOptions,
3811
+ abortSignal: mergedAbortSignal,
3812
+ headers: headersWithUserAgent
3813
+ });
3814
+ const responseData = {
3815
+ id: (_b2 = (_a18 = result.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId2(),
3816
+ timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : /* @__PURE__ */ new Date(),
3817
+ modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
3818
+ headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
3819
+ body: (_h2 = result.response) == null ? void 0 : _h2.body
3820
+ };
3821
+ span2.setAttributes(
3822
+ await selectTelemetryAttributes({
3823
+ telemetry,
3824
+ attributes: {
3825
+ "ai.response.finishReason": result.finishReason.unified,
3826
+ "ai.response.text": {
3827
+ output: () => extractTextContent(result.content)
3828
+ },
3829
+ "ai.response.toolCalls": {
3830
+ output: () => {
3831
+ const toolCalls = asToolCalls(result.content);
3832
+ return toolCalls == null ? void 0 : JSON.stringify(toolCalls);
3833
+ }
3834
+ },
3835
+ "ai.response.id": responseData.id,
3836
+ "ai.response.model": responseData.modelId,
3837
+ "ai.response.timestamp": responseData.timestamp.toISOString(),
3838
+ "ai.response.providerMetadata": JSON.stringify(
3839
+ result.providerMetadata
3840
+ ),
3841
+ // TODO rename telemetry attributes to inputTokens and outputTokens
3842
+ "ai.usage.promptTokens": result.usage.inputTokens.total,
3843
+ "ai.usage.completionTokens": result.usage.outputTokens.total,
3844
+ // standardized gen-ai llm span attributes:
3845
+ "gen_ai.response.finish_reasons": [
3846
+ result.finishReason.unified
3847
+ ],
3848
+ "gen_ai.response.id": responseData.id,
3849
+ "gen_ai.response.model": responseData.modelId,
3850
+ "gen_ai.usage.input_tokens": result.usage.inputTokens.total,
3851
+ "gen_ai.usage.output_tokens": result.usage.outputTokens.total
3852
+ }
3853
+ })
3854
+ );
3855
+ return { ...result, response: responseData };
3778
3856
  }
3779
- }),
3780
- tracer,
3781
- fn: async (span2) => {
3782
- var _a18, _b2, _c2, _d2, _e2, _f2, _g2, _h2;
3783
- const stepProviderOptions = mergeObjects(
3784
- providerOptions,
3785
- prepareStepResult == null ? void 0 : prepareStepResult.providerOptions
3786
- );
3787
- const result = await stepModel.doGenerate({
3788
- ...callSettings2,
3789
- tools: stepTools,
3790
- toolChoice: stepToolChoice,
3791
- responseFormat: await (output == null ? void 0 : output.responseFormat),
3792
- prompt: promptMessages,
3793
- providerOptions: stepProviderOptions,
3794
- abortSignal: mergedAbortSignal,
3795
- headers: headersWithUserAgent
3796
- });
3797
- const responseData = {
3798
- id: (_b2 = (_a18 = result.response) == null ? void 0 : _a18.id) != null ? _b2 : generateId2(),
3799
- timestamp: (_d2 = (_c2 = result.response) == null ? void 0 : _c2.timestamp) != null ? _d2 : /* @__PURE__ */ new Date(),
3800
- modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
3801
- headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
3802
- body: (_h2 = result.response) == null ? void 0 : _h2.body
3803
- };
3804
- span2.setAttributes(
3805
- await selectTelemetryAttributes({
3806
- telemetry,
3807
- attributes: {
3808
- "ai.response.finishReason": result.finishReason.unified,
3809
- "ai.response.text": {
3810
- output: () => extractTextContent(result.content)
3811
- },
3812
- "ai.response.toolCalls": {
3813
- output: () => {
3814
- const toolCalls = asToolCalls(result.content);
3815
- return toolCalls == null ? void 0 : JSON.stringify(toolCalls);
3816
- }
3817
- },
3818
- "ai.response.id": responseData.id,
3819
- "ai.response.model": responseData.modelId,
3820
- "ai.response.timestamp": responseData.timestamp.toISOString(),
3821
- "ai.response.providerMetadata": JSON.stringify(
3822
- result.providerMetadata
3823
- ),
3824
- // TODO rename telemetry attributes to inputTokens and outputTokens
3825
- "ai.usage.promptTokens": result.usage.inputTokens.total,
3826
- "ai.usage.completionTokens": result.usage.outputTokens.total,
3827
- // standardized gen-ai llm span attributes:
3828
- "gen_ai.response.finish_reasons": [
3829
- result.finishReason.unified
3830
- ],
3831
- "gen_ai.response.id": responseData.id,
3832
- "gen_ai.response.model": responseData.modelId,
3833
- "gen_ai.usage.input_tokens": result.usage.inputTokens.total,
3834
- "gen_ai.usage.output_tokens": result.usage.outputTokens.total
3835
- }
3836
- })
3837
- );
3838
- return { ...result, response: responseData };
3839
- }
3840
- });
3841
- }
3842
- );
3843
- const stepToolCalls = await Promise.all(
3844
- currentModelResponse.content.filter(
3845
- (part) => part.type === "tool-call"
3846
- ).map(
3847
- (toolCall) => parseToolCall({
3857
+ });
3858
+ }
3859
+ );
3860
+ const stepToolCalls = await Promise.all(
3861
+ currentModelResponse.content.filter(
3862
+ (part) => part.type === "tool-call"
3863
+ ).map(
3864
+ (toolCall) => parseToolCall({
3865
+ toolCall,
3866
+ tools,
3867
+ repairToolCall,
3868
+ system,
3869
+ messages: stepInputMessages
3870
+ })
3871
+ )
3872
+ );
3873
+ const toolApprovalRequests = {};
3874
+ for (const toolCall of stepToolCalls) {
3875
+ if (toolCall.invalid) {
3876
+ continue;
3877
+ }
3878
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3879
+ if (tool2 == null) {
3880
+ continue;
3881
+ }
3882
+ if ((tool2 == null ? void 0 : tool2.onInputAvailable) != null) {
3883
+ await tool2.onInputAvailable({
3884
+ input: toolCall.input,
3885
+ toolCallId: toolCall.toolCallId,
3886
+ messages: stepInputMessages,
3887
+ abortSignal: mergedAbortSignal,
3888
+ experimental_context
3889
+ });
3890
+ }
3891
+ if (await isApprovalNeeded({
3892
+ tool: tool2,
3848
3893
  toolCall,
3849
- tools,
3850
- repairToolCall,
3851
- system,
3852
- messages: stepInputMessages
3853
- })
3854
- )
3855
- );
3856
- const toolApprovalRequests = {};
3857
- for (const toolCall of stepToolCalls) {
3858
- if (toolCall.invalid) {
3859
- continue;
3860
- }
3861
- const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3862
- if (tool2 == null) {
3863
- continue;
3864
- }
3865
- if ((tool2 == null ? void 0 : tool2.onInputAvailable) != null) {
3866
- await tool2.onInputAvailable({
3867
- input: toolCall.input,
3868
- toolCallId: toolCall.toolCallId,
3869
3894
  messages: stepInputMessages,
3870
- abortSignal: mergedAbortSignal,
3871
3895
  experimental_context
3872
- });
3896
+ })) {
3897
+ toolApprovalRequests[toolCall.toolCallId] = {
3898
+ type: "tool-approval-request",
3899
+ approvalId: generateId2(),
3900
+ toolCall
3901
+ };
3902
+ }
3873
3903
  }
3874
- if (await isApprovalNeeded({
3875
- tool: tool2,
3876
- toolCall,
3877
- messages: stepInputMessages,
3878
- experimental_context
3879
- })) {
3880
- toolApprovalRequests[toolCall.toolCallId] = {
3881
- type: "tool-approval-request",
3882
- approvalId: generateId2(),
3883
- toolCall
3884
- };
3904
+ const invalidToolCalls = stepToolCalls.filter(
3905
+ (toolCall) => toolCall.invalid && toolCall.dynamic
3906
+ );
3907
+ clientToolOutputs = [];
3908
+ for (const toolCall of invalidToolCalls) {
3909
+ clientToolOutputs.push({
3910
+ type: "tool-error",
3911
+ toolCallId: toolCall.toolCallId,
3912
+ toolName: toolCall.toolName,
3913
+ input: toolCall.input,
3914
+ error: getErrorMessage5(toolCall.error),
3915
+ dynamic: true
3916
+ });
3885
3917
  }
3886
- }
3887
- const invalidToolCalls = stepToolCalls.filter(
3888
- (toolCall) => toolCall.invalid && toolCall.dynamic
3889
- );
3890
- clientToolOutputs = [];
3891
- for (const toolCall of invalidToolCalls) {
3892
- clientToolOutputs.push({
3893
- type: "tool-error",
3894
- toolCallId: toolCall.toolCallId,
3895
- toolName: toolCall.toolName,
3896
- input: toolCall.input,
3897
- error: getErrorMessage5(toolCall.error),
3898
- dynamic: true
3899
- });
3900
- }
3901
- clientToolCalls = stepToolCalls.filter(
3902
- (toolCall) => !toolCall.providerExecuted
3903
- );
3904
- if (tools != null) {
3905
- clientToolOutputs.push(
3906
- ...await executeTools({
3907
- toolCalls: clientToolCalls.filter(
3908
- (toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
3909
- ),
3910
- tools,
3911
- tracer,
3912
- telemetry,
3913
- messages: stepInputMessages,
3914
- abortSignal: mergedAbortSignal,
3915
- experimental_context
3916
- })
3918
+ clientToolCalls = stepToolCalls.filter(
3919
+ (toolCall) => !toolCall.providerExecuted
3917
3920
  );
3918
- }
3919
- for (const toolCall of stepToolCalls) {
3920
- if (!toolCall.providerExecuted)
3921
- continue;
3922
- const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3923
- if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
3924
- const hasResultInResponse = currentModelResponse.content.some(
3925
- (part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
3921
+ if (tools != null) {
3922
+ clientToolOutputs.push(
3923
+ ...await executeTools({
3924
+ toolCalls: clientToolCalls.filter(
3925
+ (toolCall) => !toolCall.invalid && toolApprovalRequests[toolCall.toolCallId] == null
3926
+ ),
3927
+ tools,
3928
+ tracer,
3929
+ telemetry,
3930
+ messages: stepInputMessages,
3931
+ abortSignal: mergedAbortSignal,
3932
+ experimental_context
3933
+ })
3926
3934
  );
3927
- if (!hasResultInResponse) {
3928
- pendingDeferredToolCalls.set(toolCall.toolCallId, {
3929
- toolName: toolCall.toolName
3930
- });
3935
+ }
3936
+ for (const toolCall of stepToolCalls) {
3937
+ if (!toolCall.providerExecuted)
3938
+ continue;
3939
+ const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
3940
+ if ((tool2 == null ? void 0 : tool2.type) === "provider" && tool2.supportsDeferredResults) {
3941
+ const hasResultInResponse = currentModelResponse.content.some(
3942
+ (part) => part.type === "tool-result" && part.toolCallId === toolCall.toolCallId
3943
+ );
3944
+ if (!hasResultInResponse) {
3945
+ pendingDeferredToolCalls.set(toolCall.toolCallId, {
3946
+ toolName: toolCall.toolName
3947
+ });
3948
+ }
3931
3949
  }
3932
3950
  }
3933
- }
3934
- for (const part of currentModelResponse.content) {
3935
- if (part.type === "tool-result") {
3936
- pendingDeferredToolCalls.delete(part.toolCallId);
3951
+ for (const part of currentModelResponse.content) {
3952
+ if (part.type === "tool-result") {
3953
+ pendingDeferredToolCalls.delete(part.toolCallId);
3954
+ }
3937
3955
  }
3938
- }
3939
- const stepContent = asContent({
3940
- content: currentModelResponse.content,
3941
- toolCalls: stepToolCalls,
3942
- toolOutputs: clientToolOutputs,
3943
- toolApprovalRequests: Object.values(toolApprovalRequests),
3944
- tools
3945
- });
3946
- responseMessages.push(
3947
- ...await toResponseMessages({
3948
- content: stepContent,
3956
+ const stepContent = asContent({
3957
+ content: currentModelResponse.content,
3958
+ toolCalls: stepToolCalls,
3959
+ toolOutputs: clientToolOutputs,
3960
+ toolApprovalRequests: Object.values(toolApprovalRequests),
3949
3961
  tools
3950
- })
3951
- );
3952
- const currentStepResult = new DefaultStepResult({
3953
- content: stepContent,
3954
- finishReason: currentModelResponse.finishReason.unified,
3955
- rawFinishReason: currentModelResponse.finishReason.raw,
3956
- usage: asLanguageModelUsage(currentModelResponse.usage),
3957
- warnings: currentModelResponse.warnings,
3958
- providerMetadata: currentModelResponse.providerMetadata,
3959
- request: (_g = currentModelResponse.request) != null ? _g : {},
3960
- response: {
3961
- ...currentModelResponse.response,
3962
- // deep clone msgs to avoid mutating past messages in multi-step:
3963
- messages: structuredClone(responseMessages)
3962
+ });
3963
+ responseMessages.push(
3964
+ ...await toResponseMessages({
3965
+ content: stepContent,
3966
+ tools
3967
+ })
3968
+ );
3969
+ const currentStepResult = new DefaultStepResult({
3970
+ content: stepContent,
3971
+ finishReason: currentModelResponse.finishReason.unified,
3972
+ rawFinishReason: currentModelResponse.finishReason.raw,
3973
+ usage: asLanguageModelUsage(currentModelResponse.usage),
3974
+ warnings: currentModelResponse.warnings,
3975
+ providerMetadata: currentModelResponse.providerMetadata,
3976
+ request: (_g = currentModelResponse.request) != null ? _g : {},
3977
+ response: {
3978
+ ...currentModelResponse.response,
3979
+ // deep clone msgs to avoid mutating past messages in multi-step:
3980
+ messages: structuredClone(responseMessages)
3981
+ }
3982
+ });
3983
+ logWarnings({
3984
+ warnings: (_h = currentModelResponse.warnings) != null ? _h : [],
3985
+ provider: stepModel.provider,
3986
+ model: stepModel.modelId
3987
+ });
3988
+ steps.push(currentStepResult);
3989
+ await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
3990
+ } finally {
3991
+ if (stepTimeoutId != null) {
3992
+ clearTimeout(stepTimeoutId);
3964
3993
  }
3965
- });
3966
- logWarnings({
3967
- warnings: (_h = currentModelResponse.warnings) != null ? _h : [],
3968
- provider: stepModel.provider,
3969
- model: stepModel.modelId
3970
- });
3971
- steps.push(currentStepResult);
3972
- await (onStepFinish == null ? void 0 : onStepFinish(currentStepResult));
3994
+ }
3973
3995
  } while (
3974
3996
  // Continue if:
3975
3997
  // 1. There are client tool calls that have all been executed, OR
@@ -5676,6 +5698,10 @@ function streamText({
5676
5698
  ...settings
5677
5699
  }) {
5678
5700
  const totalTimeoutMs = getTotalTimeoutMs(timeout);
5701
+ const stepTimeoutMs = getStepTimeoutMs(timeout);
5702
+ const chunkTimeoutMs = getChunkTimeoutMs(timeout);
5703
+ const stepAbortController = stepTimeoutMs != null ? new AbortController() : void 0;
5704
+ const chunkAbortController = chunkTimeoutMs != null ? new AbortController() : void 0;
5679
5705
  return new DefaultStreamTextResult({
5680
5706
  model: resolveLanguageModel(model),
5681
5707
  telemetry,
@@ -5684,8 +5710,14 @@ function streamText({
5684
5710
  maxRetries,
5685
5711
  abortSignal: mergeAbortSignals(
5686
5712
  abortSignal,
5687
- totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0
5713
+ totalTimeoutMs != null ? AbortSignal.timeout(totalTimeoutMs) : void 0,
5714
+ stepAbortController == null ? void 0 : stepAbortController.signal,
5715
+ chunkAbortController == null ? void 0 : chunkAbortController.signal
5688
5716
  ),
5717
+ stepTimeoutMs,
5718
+ stepAbortController,
5719
+ chunkTimeoutMs,
5720
+ chunkAbortController,
5689
5721
  system,
5690
5722
  prompt,
5691
5723
  messages,
@@ -5780,6 +5812,10 @@ var DefaultStreamTextResult = class {
5780
5812
  settings,
5781
5813
  maxRetries: maxRetriesArg,
5782
5814
  abortSignal,
5815
+ stepTimeoutMs,
5816
+ stepAbortController,
5817
+ chunkTimeoutMs,
5818
+ chunkAbortController,
5783
5819
  system,
5784
5820
  prompt,
5785
5821
  messages,
@@ -6245,6 +6281,30 @@ var DefaultStreamTextResult = class {
6245
6281
  }) {
6246
6282
  var _a16, _b, _c, _d, _e, _f;
6247
6283
  const includeRawChunks2 = self.includeRawChunks;
6284
+ const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
6285
+ let chunkTimeoutId = void 0;
6286
+ function resetChunkTimeout() {
6287
+ if (chunkTimeoutMs != null) {
6288
+ if (chunkTimeoutId != null) {
6289
+ clearTimeout(chunkTimeoutId);
6290
+ }
6291
+ chunkTimeoutId = setTimeout(
6292
+ () => chunkAbortController.abort(),
6293
+ chunkTimeoutMs
6294
+ );
6295
+ }
6296
+ }
6297
+ function clearChunkTimeout() {
6298
+ if (chunkTimeoutId != null) {
6299
+ clearTimeout(chunkTimeoutId);
6300
+ chunkTimeoutId = void 0;
6301
+ }
6302
+ }
6303
+ function clearStepTimeout() {
6304
+ if (stepTimeoutId != null) {
6305
+ clearTimeout(stepTimeoutId);
6306
+ }
6307
+ }
6248
6308
  stepFinish = new DelayedPromise();
6249
6309
  const stepInputMessages = [...initialMessages, ...responseMessages];
6250
6310
  const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
@@ -6369,6 +6429,7 @@ var DefaultStreamTextResult = class {
6369
6429
  new TransformStream({
6370
6430
  async transform(chunk, controller) {
6371
6431
  var _a17, _b2, _c2, _d2, _e2;
6432
+ resetChunkTimeout();
6372
6433
  if (chunk.type === "stream-start") {
6373
6434
  warnings = chunk.warnings;
6374
6435
  return;
@@ -6599,6 +6660,8 @@ var DefaultStreamTextResult = class {
6599
6660
  pendingDeferredToolCalls.delete(output2.toolCallId);
6600
6661
  }
6601
6662
  }
6663
+ clearStepTimeout();
6664
+ clearChunkTimeout();
6602
6665
  if (
6603
6666
  // Continue if:
6604
6667
  // 1. There are client tool calls that have all been executed, OR
@@ -10182,7 +10245,16 @@ function smoothStream({
10182
10245
  _internal: { delay: delay2 = originalDelay } = {}
10183
10246
  } = {}) {
10184
10247
  let detectChunk;
10185
- if (typeof chunking === "function") {
10248
+ if (chunking != null && typeof chunking === "object" && "segment" in chunking && typeof chunking.segment === "function") {
10249
+ const segmenter = chunking;
10250
+ detectChunk = (buffer) => {
10251
+ if (buffer.length === 0)
10252
+ return null;
10253
+ const iterator = segmenter.segment(buffer)[Symbol.iterator]();
10254
+ const first = iterator.next().value;
10255
+ return (first == null ? void 0 : first.segment) || null;
10256
+ };
10257
+ } else if (typeof chunking === "function") {
10186
10258
  detectChunk = (buffer) => {
10187
10259
  const match = chunking(buffer);
10188
10260
  if (match == null) {
@@ -10199,11 +10271,11 @@ function smoothStream({
10199
10271
  return match;
10200
10272
  };
10201
10273
  } else {
10202
- const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking;
10274
+ const chunkingRegex = typeof chunking === "string" ? CHUNKING_REGEXPS[chunking] : chunking instanceof RegExp ? chunking : void 0;
10203
10275
  if (chunkingRegex == null) {
10204
10276
  throw new InvalidArgumentError2({
10205
10277
  argument: "chunking",
10206
- message: `Chunking must be "word" or "line" or a RegExp. Received: ${chunking}`
10278
+ message: `Chunking must be "word", "line", a RegExp, an Intl.Segmenter, or a ChunkDetector function. Received: ${chunking}`
10207
10279
  });
10208
10280
  }
10209
10281
  detectChunk = (buffer) => {
@@ -12083,7 +12155,6 @@ export {
12083
12155
  getTextFromDataUrl,
12084
12156
  getToolName,
12085
12157
  getToolOrDynamicToolName,
12086
- getTotalTimeoutMs,
12087
12158
  hasToolCall,
12088
12159
  isDataUIPart,
12089
12160
  isDeepEqualData,