@ai-sdk/provider-utils 4.0.55 → 4.0.56

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 4.0.56
4
+
5
+ ### Patch Changes
6
+
7
+ - 29dc427: fix(provider-utils): preserve streamed tool calls with unreliable IDs and indices
8
+
3
9
  ## 4.0.55
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV3FunctionTool, LanguageModelV3ProviderTool, ImageModelV3File, AISDKError, SharedV3ProviderOptions, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, JSONObject, TypeValidationContext } from '@ai-sdk/provider';
1
+ import { LanguageModelV3FunctionTool, LanguageModelV3ProviderTool, ImageModelV3File, AISDKError, SharedV3ProviderOptions, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, JSONObject, LanguageModelV3StreamPart, SharedV3ProviderMetadata, TypeValidationContext } from '@ai-sdk/provider';
2
2
  import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
3
3
  export * from '@standard-schema/spec';
4
4
  import * as z3 from 'zod/v3';
@@ -1432,6 +1432,73 @@ declare const retryWithExponentialBackoff: ({ maxRetries, initialDelayInMs, back
1432
1432
 
1433
1433
  declare function secureJsonParse(text: string): any;
1434
1434
 
1435
+ /**
1436
+ * Minimal interface for a streaming tool call delta from an OpenAI-compatible API.
1437
+ */
1438
+ interface StreamingToolCallDelta {
1439
+ index?: number | null;
1440
+ id?: string | null;
1441
+ type?: string | null;
1442
+ function?: {
1443
+ name?: string | null;
1444
+ arguments?: string | null;
1445
+ } | null;
1446
+ }
1447
+ interface StreamingToolCallTrackerOptions<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
1448
+ /**
1449
+ * ID generator function for tool call IDs.
1450
+ * Blank or repeated outputs are converted to usable unique IDs.
1451
+ * Defaults to the standard generateId.
1452
+ */
1453
+ generateId?: () => string;
1454
+ /**
1455
+ * How to validate the `type` field on new tool call deltas.
1456
+ * - `'none'`: no validation (default)
1457
+ * - `'if-present'`: throw if type is present and not `'function'`
1458
+ * - `'required'`: throw if type is not exactly `'function'`
1459
+ */
1460
+ typeValidation?: 'none' | 'if-present' | 'required';
1461
+ /**
1462
+ * Extract provider-specific metadata from a tool call delta.
1463
+ * Called once when a new tool call is detected.
1464
+ */
1465
+ extractMetadata?: (delta: DELTA) => SharedV3ProviderMetadata | undefined;
1466
+ /**
1467
+ * Build the `providerMetadata` object for a `tool-call` event.
1468
+ */
1469
+ buildToolCallProviderMetadata?: (metadata: SharedV3ProviderMetadata | undefined) => SharedV3ProviderMetadata | undefined;
1470
+ }
1471
+ type StreamingToolCallTrackerController = Pick<TransformStreamDefaultController<LanguageModelV3StreamPart>, 'enqueue'>;
1472
+ /**
1473
+ * Tracks streaming tool call state across multiple deltas from an
1474
+ * OpenAI-compatible chat completion stream.
1475
+ */
1476
+ declare class StreamingToolCallTracker<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
1477
+ private toolCalls;
1478
+ private toolCallsById;
1479
+ private toolCallsByIndex;
1480
+ private usedToolCallIds;
1481
+ private nextGeneratedIdSuffixes;
1482
+ private readonly controller;
1483
+ private readonly _generateId;
1484
+ private readonly typeValidation;
1485
+ private readonly extractMetadata?;
1486
+ private readonly buildToolCallProviderMetadata?;
1487
+ constructor(controller: StreamingToolCallTrackerController, options?: StreamingToolCallTrackerOptions<DELTA>);
1488
+ processDelta(toolCallDelta: DELTA): void;
1489
+ flush(): void;
1490
+ private resolveToolCall;
1491
+ private filterToolCallsByName;
1492
+ private resolveMatchingToolCall;
1493
+ private processNewToolCall;
1494
+ private associateWireId;
1495
+ private associateIndex;
1496
+ private createToolCallId;
1497
+ private getNonBlankString;
1498
+ private processExistingToolCall;
1499
+ private finishToolCall;
1500
+ }
1501
+
1435
1502
  /**
1436
1503
  * Strips file extension segments from a filename.
1437
1504
  *
@@ -1589,4 +1656,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1589
1656
  */
1590
1657
  type ToolCallOptions = ToolExecutionOptions;
1591
1658
 
1592
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1659
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { LanguageModelV3FunctionTool, LanguageModelV3ProviderTool, ImageModelV3File, AISDKError, SharedV3ProviderOptions, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, JSONObject, TypeValidationContext } from '@ai-sdk/provider';
1
+ import { LanguageModelV3FunctionTool, LanguageModelV3ProviderTool, ImageModelV3File, AISDKError, SharedV3ProviderOptions, JSONSchema7, JSONParseError, TypeValidationError, JSONValue, APICallError, LanguageModelV3Prompt, JSONObject, LanguageModelV3StreamPart, SharedV3ProviderMetadata, TypeValidationContext } from '@ai-sdk/provider';
2
2
  import { StandardSchemaV1, StandardJSONSchemaV1 } from '@standard-schema/spec';
3
3
  export * from '@standard-schema/spec';
4
4
  import * as z3 from 'zod/v3';
@@ -1432,6 +1432,73 @@ declare const retryWithExponentialBackoff: ({ maxRetries, initialDelayInMs, back
1432
1432
 
1433
1433
  declare function secureJsonParse(text: string): any;
1434
1434
 
1435
+ /**
1436
+ * Minimal interface for a streaming tool call delta from an OpenAI-compatible API.
1437
+ */
1438
+ interface StreamingToolCallDelta {
1439
+ index?: number | null;
1440
+ id?: string | null;
1441
+ type?: string | null;
1442
+ function?: {
1443
+ name?: string | null;
1444
+ arguments?: string | null;
1445
+ } | null;
1446
+ }
1447
+ interface StreamingToolCallTrackerOptions<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
1448
+ /**
1449
+ * ID generator function for tool call IDs.
1450
+ * Blank or repeated outputs are converted to usable unique IDs.
1451
+ * Defaults to the standard generateId.
1452
+ */
1453
+ generateId?: () => string;
1454
+ /**
1455
+ * How to validate the `type` field on new tool call deltas.
1456
+ * - `'none'`: no validation (default)
1457
+ * - `'if-present'`: throw if type is present and not `'function'`
1458
+ * - `'required'`: throw if type is not exactly `'function'`
1459
+ */
1460
+ typeValidation?: 'none' | 'if-present' | 'required';
1461
+ /**
1462
+ * Extract provider-specific metadata from a tool call delta.
1463
+ * Called once when a new tool call is detected.
1464
+ */
1465
+ extractMetadata?: (delta: DELTA) => SharedV3ProviderMetadata | undefined;
1466
+ /**
1467
+ * Build the `providerMetadata` object for a `tool-call` event.
1468
+ */
1469
+ buildToolCallProviderMetadata?: (metadata: SharedV3ProviderMetadata | undefined) => SharedV3ProviderMetadata | undefined;
1470
+ }
1471
+ type StreamingToolCallTrackerController = Pick<TransformStreamDefaultController<LanguageModelV3StreamPart>, 'enqueue'>;
1472
+ /**
1473
+ * Tracks streaming tool call state across multiple deltas from an
1474
+ * OpenAI-compatible chat completion stream.
1475
+ */
1476
+ declare class StreamingToolCallTracker<DELTA extends StreamingToolCallDelta = StreamingToolCallDelta> {
1477
+ private toolCalls;
1478
+ private toolCallsById;
1479
+ private toolCallsByIndex;
1480
+ private usedToolCallIds;
1481
+ private nextGeneratedIdSuffixes;
1482
+ private readonly controller;
1483
+ private readonly _generateId;
1484
+ private readonly typeValidation;
1485
+ private readonly extractMetadata?;
1486
+ private readonly buildToolCallProviderMetadata?;
1487
+ constructor(controller: StreamingToolCallTrackerController, options?: StreamingToolCallTrackerOptions<DELTA>);
1488
+ processDelta(toolCallDelta: DELTA): void;
1489
+ flush(): void;
1490
+ private resolveToolCall;
1491
+ private filterToolCallsByName;
1492
+ private resolveMatchingToolCall;
1493
+ private processNewToolCall;
1494
+ private associateWireId;
1495
+ private associateIndex;
1496
+ private createToolCallId;
1497
+ private getNonBlankString;
1498
+ private processExistingToolCall;
1499
+ private finishToolCall;
1500
+ }
1501
+
1435
1502
  /**
1436
1503
  * Strips file extension segments from a filename.
1437
1504
  *
@@ -1589,4 +1656,4 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
1589
1656
  */
1590
1657
  type ToolCallOptions = ToolExecutionOptions;
1591
1658
 
1592
- export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
1659
+ export { type AssistantContent, type AssistantModelMessage, DEFAULT_MAX_DOWNLOAD_SIZE, type DataContent, DelayedPromise, DownloadError, EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL, EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER, type EmbeddingModelProviderOptionsTransformer, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type LazySchema, type MaybePromiseLike, type ModelMessage, type ParseResult, type ProviderOptions, type ProviderToolFactory, type ProviderToolFactoryWithOutputSchema, type ReasoningPart, type Resolvable, type ResponseHandler, type RetryDelayProvider, type RetryErrorFactory, type RetryErrorReason, type RetryFunction, type Schema, type ShouldRetryFunction, type StreamingToolCallDelta, StreamingToolCallTracker, type StreamingToolCallTrackerOptions, type SystemModelMessage, type TextPart, type Tool, type ToolApprovalRequest, type ToolApprovalResponse, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolExecutionOptions, type ToolModelMessage, type ToolNameMapping, type ToolNeedsApprovalFunction, type ToolResult, type ToolResultOutput, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, asSchema, cancelResponseBody, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertImageModelFileToDataUri, convertToBase64, convertToFormData, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createProviderToolFactory, createProviderToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, createToolNameMapping, delay, downloadBlob, dynamicTool, executeTool, extractResponseHeaders, fetchWithValidatedRedirects, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isBrowserRuntime, isNonNullable, isParsableJson, isSameOrigin, isUrlSupported, jsonSchema, lazySchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, normalizeHeaders, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, readResponseWithSizeLimit, removeUndefinedEntries, resolve, retryWithExponentialBackoff, safeParseJSON, safeValidateTypes, secureJsonParse, stripFileExtension, tool, validateDownloadUrl, validateTypes, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
package/dist/index.js CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL: () => EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
37
37
  EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER: () => EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER,
38
38
  EventSourceParserStream: () => import_stream2.EventSourceParserStream,
39
+ StreamingToolCallTracker: () => StreamingToolCallTracker,
39
40
  VERSION: () => VERSION,
40
41
  asSchema: () => asSchema,
41
42
  cancelResponseBody: () => cancelResponseBody,
@@ -926,7 +927,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
926
927
  }
927
928
 
928
929
  // src/version.ts
929
- var VERSION = true ? "4.0.55" : "0.0.0-test";
930
+ var VERSION = true ? "4.0.56" : "0.0.0-test";
930
931
 
931
932
  // src/get-from-api.ts
932
933
  var getOriginalFetch = () => globalThis.fetch;
@@ -3173,6 +3174,365 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
3173
3174
  };
3174
3175
  };
3175
3176
 
3177
+ // src/streaming-tool-call-tracker.ts
3178
+ var import_provider13 = require("@ai-sdk/provider");
3179
+
3180
+ // src/streaming-tool-call-argument-state.ts
3181
+ function startsWithStructuredValue(value) {
3182
+ if (typeof value !== "string") {
3183
+ return false;
3184
+ }
3185
+ const firstCharacter = value.trimStart()[0];
3186
+ return firstCharacter === "{" || firstCharacter === "[";
3187
+ }
3188
+ var StreamingToolCallArgumentState = class {
3189
+ constructor(initialValue = "") {
3190
+ this.structure = { kind: "undetermined" };
3191
+ this.append(initialValue);
3192
+ }
3193
+ get hasCompleteStructuredValue() {
3194
+ return this.structure.kind === "structured" && this.structure.complete === true;
3195
+ }
3196
+ append(delta) {
3197
+ let nextStructure = this.structure;
3198
+ for (const character of delta) {
3199
+ if (nextStructure.kind === "undetermined") {
3200
+ if (/\s/.test(character)) {
3201
+ continue;
3202
+ }
3203
+ if (character !== "{" && character !== "[") {
3204
+ nextStructure = { kind: "other" };
3205
+ continue;
3206
+ }
3207
+ nextStructure = {
3208
+ kind: "structured",
3209
+ stack: [character],
3210
+ inString: false,
3211
+ escaped: false,
3212
+ complete: false
3213
+ };
3214
+ continue;
3215
+ }
3216
+ if (nextStructure.kind !== "structured" || nextStructure.complete) {
3217
+ continue;
3218
+ }
3219
+ if (nextStructure.inString) {
3220
+ if (nextStructure.escaped) {
3221
+ nextStructure.escaped = false;
3222
+ } else if (character === "\\") {
3223
+ nextStructure.escaped = true;
3224
+ } else if (character === '"') {
3225
+ nextStructure.inString = false;
3226
+ }
3227
+ continue;
3228
+ }
3229
+ if (character === '"') {
3230
+ nextStructure.inString = true;
3231
+ } else if (character === "{" || character === "[") {
3232
+ nextStructure.stack.push(character);
3233
+ } else if (character === "}" || character === "]") {
3234
+ const expectedOpening = character === "}" ? "{" : "[";
3235
+ if (nextStructure.stack[nextStructure.stack.length - 1] !== expectedOpening) {
3236
+ nextStructure = { kind: "other" };
3237
+ continue;
3238
+ }
3239
+ nextStructure.stack.pop();
3240
+ if (nextStructure.stack.length === 0) {
3241
+ nextStructure.complete = true;
3242
+ }
3243
+ }
3244
+ }
3245
+ this.structure = nextStructure;
3246
+ }
3247
+ };
3248
+
3249
+ // src/streaming-tool-call-tracker.ts
3250
+ var StreamingToolCallTracker = class {
3251
+ constructor(controller, options = {}) {
3252
+ this.toolCalls = [];
3253
+ this.toolCallsById = /* @__PURE__ */ new Map();
3254
+ this.toolCallsByIndex = /* @__PURE__ */ new Map();
3255
+ this.usedToolCallIds = /* @__PURE__ */ new Set();
3256
+ this.nextGeneratedIdSuffixes = /* @__PURE__ */ new Map();
3257
+ var _a2, _b2;
3258
+ this.controller = controller;
3259
+ this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
3260
+ this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
3261
+ this.extractMetadata = options.extractMetadata;
3262
+ this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
3263
+ }
3264
+ processDelta(toolCallDelta) {
3265
+ var _a2, _b2;
3266
+ const wireName = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.name;
3267
+ const hasBlankName = typeof wireName === "string" && wireName.trim().length === 0;
3268
+ const wireId = this.getNonBlankString(toolCallDelta.id);
3269
+ const name2 = this.getNonBlankString(wireName);
3270
+ const { index } = toolCallDelta;
3271
+ const resolution = this.resolveToolCall({
3272
+ wireId,
3273
+ index,
3274
+ name: name2,
3275
+ hasExplicitCallStart: name2 != null && startsWithStructuredValue((_b2 = toolCallDelta.function) == null ? void 0 : _b2.arguments)
3276
+ });
3277
+ if (resolution.kind === "ambiguous") {
3278
+ return;
3279
+ }
3280
+ let toolCall;
3281
+ if (resolution.kind === "new") {
3282
+ if (hasBlankName) {
3283
+ return;
3284
+ }
3285
+ toolCall = this.processNewToolCall(toolCallDelta, {
3286
+ wireId,
3287
+ index,
3288
+ name: name2
3289
+ });
3290
+ } else {
3291
+ toolCall = resolution.toolCall;
3292
+ if (wireId != null) {
3293
+ this.associateWireId(toolCall, wireId);
3294
+ }
3295
+ this.processExistingToolCall(toolCall, toolCallDelta);
3296
+ }
3297
+ if (index != null) {
3298
+ this.associateIndex(toolCall, index);
3299
+ }
3300
+ }
3301
+ flush() {
3302
+ const toolCalls = this.toolCalls.every((toolCall) => toolCall.index != null) ? [...this.toolCalls].sort(
3303
+ (a, b) => a.index - b.index || a.sequence - b.sequence
3304
+ ) : this.toolCalls;
3305
+ for (const toolCall of toolCalls) {
3306
+ if (!toolCall.hasFinished) {
3307
+ this.finishToolCall(toolCall);
3308
+ }
3309
+ }
3310
+ }
3311
+ resolveToolCall({
3312
+ wireId,
3313
+ index,
3314
+ name: name2,
3315
+ hasExplicitCallStart
3316
+ }) {
3317
+ const indexedToolCalls = index != null ? this.toolCallsByIndex.get(index) : void 0;
3318
+ const matchingIndexedToolCalls = this.filterToolCallsByName(
3319
+ indexedToolCalls,
3320
+ name2
3321
+ );
3322
+ if (wireId != null) {
3323
+ const toolCallsWithId = this.toolCallsById.get(wireId);
3324
+ if (toolCallsWithId != null) {
3325
+ if (index != null) {
3326
+ const matchingToolCalls = matchingIndexedToolCalls.filter(
3327
+ (toolCall) => toolCallsWithId.has(toolCall)
3328
+ );
3329
+ const matchingToolCall = this.resolveMatchingToolCall(
3330
+ matchingToolCalls,
3331
+ hasExplicitCallStart
3332
+ );
3333
+ if (matchingToolCall.kind !== "new") {
3334
+ return matchingToolCall;
3335
+ }
3336
+ if (name2 != null) {
3337
+ return { kind: "new" };
3338
+ }
3339
+ if (indexedToolCalls != null) {
3340
+ return { kind: "ambiguous" };
3341
+ }
3342
+ return this.resolveMatchingToolCall([...toolCallsWithId], false);
3343
+ }
3344
+ if (name2 != null) {
3345
+ const matchingToolCalls = [...toolCallsWithId].filter(
3346
+ (toolCall) => toolCall.function.name === name2
3347
+ );
3348
+ return this.resolveMatchingToolCall(
3349
+ matchingToolCalls,
3350
+ hasExplicitCallStart
3351
+ );
3352
+ }
3353
+ return this.resolveMatchingToolCall([...toolCallsWithId], false);
3354
+ }
3355
+ if (matchingIndexedToolCalls.length > 0) {
3356
+ return hasExplicitCallStart ? { kind: "new" } : this.resolveMatchingToolCall(matchingIndexedToolCalls, false);
3357
+ }
3358
+ return { kind: "new" };
3359
+ }
3360
+ if (indexedToolCalls != null) {
3361
+ return this.resolveMatchingToolCall(
3362
+ matchingIndexedToolCalls,
3363
+ hasExplicitCallStart
3364
+ );
3365
+ }
3366
+ if (name2 != null) {
3367
+ return { kind: "new" };
3368
+ }
3369
+ const unfinishedToolCalls = this.toolCalls.filter(
3370
+ (toolCall) => !toolCall.hasFinished
3371
+ );
3372
+ if (unfinishedToolCalls.length === 1) {
3373
+ return { kind: "existing", toolCall: unfinishedToolCalls[0] };
3374
+ }
3375
+ return unfinishedToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
3376
+ }
3377
+ filterToolCallsByName(toolCalls, name2) {
3378
+ if (toolCalls == null) {
3379
+ return [];
3380
+ }
3381
+ return [...toolCalls].filter(
3382
+ (toolCall) => name2 == null || toolCall.function.name === name2
3383
+ );
3384
+ }
3385
+ resolveMatchingToolCall(toolCalls, hasExplicitCallStart) {
3386
+ if (toolCalls.length === 0) {
3387
+ return { kind: "new" };
3388
+ }
3389
+ if (!hasExplicitCallStart) {
3390
+ return toolCalls.length === 1 ? { kind: "existing", toolCall: toolCalls[0] } : { kind: "ambiguous" };
3391
+ }
3392
+ const continuableToolCalls = toolCalls.filter(
3393
+ (toolCall) => !toolCall.argumentState.hasCompleteStructuredValue
3394
+ );
3395
+ if (continuableToolCalls.length === 1) {
3396
+ return { kind: "existing", toolCall: continuableToolCalls[0] };
3397
+ }
3398
+ return continuableToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
3399
+ }
3400
+ processNewToolCall(toolCallDelta, {
3401
+ wireId,
3402
+ index,
3403
+ name: name2
3404
+ }) {
3405
+ var _a2, _b2, _c, _d, _e;
3406
+ if (this.typeValidation === "required") {
3407
+ if (toolCallDelta.type !== "function") {
3408
+ throw new import_provider13.InvalidResponseDataError({
3409
+ data: toolCallDelta,
3410
+ message: `Expected 'function' type.`
3411
+ });
3412
+ }
3413
+ } else if (this.typeValidation === "if-present") {
3414
+ if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
3415
+ throw new import_provider13.InvalidResponseDataError({
3416
+ data: toolCallDelta,
3417
+ message: `Expected 'function' type.`
3418
+ });
3419
+ }
3420
+ }
3421
+ if (name2 == null) {
3422
+ throw new import_provider13.InvalidResponseDataError({
3423
+ data: toolCallDelta,
3424
+ message: `Expected 'function.name' to be a string.`
3425
+ });
3426
+ }
3427
+ const id = this.createToolCallId(wireId);
3428
+ this.controller.enqueue({
3429
+ type: "tool-input-start",
3430
+ id,
3431
+ toolName: name2
3432
+ });
3433
+ const toolCall = {
3434
+ id,
3435
+ index: index != null ? index : void 0,
3436
+ sequence: this.toolCalls.length,
3437
+ type: "function",
3438
+ function: {
3439
+ name: name2,
3440
+ arguments: (_b2 = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null ? _b2 : ""
3441
+ },
3442
+ argumentState: new StreamingToolCallArgumentState(
3443
+ (_d = (_c = toolCallDelta.function) == null ? void 0 : _c.arguments) != null ? _d : ""
3444
+ ),
3445
+ hasFinished: false,
3446
+ metadata: (_e = this.extractMetadata) == null ? void 0 : _e.call(this, toolCallDelta)
3447
+ };
3448
+ this.toolCalls.push(toolCall);
3449
+ if (wireId != null) {
3450
+ this.associateWireId(toolCall, wireId);
3451
+ }
3452
+ if (toolCall.function.arguments.length > 0) {
3453
+ this.controller.enqueue({
3454
+ type: "tool-input-delta",
3455
+ id: toolCall.id,
3456
+ delta: toolCall.function.arguments
3457
+ });
3458
+ }
3459
+ return toolCall;
3460
+ }
3461
+ associateWireId(toolCall, wireId) {
3462
+ let toolCallsWithId = this.toolCallsById.get(wireId);
3463
+ if (toolCallsWithId == null) {
3464
+ toolCallsWithId = /* @__PURE__ */ new Set();
3465
+ this.toolCallsById.set(wireId, toolCallsWithId);
3466
+ }
3467
+ toolCallsWithId.add(toolCall);
3468
+ }
3469
+ associateIndex(toolCall, index) {
3470
+ let toolCallsWithIndex = this.toolCallsByIndex.get(index);
3471
+ if (toolCallsWithIndex == null) {
3472
+ toolCallsWithIndex = /* @__PURE__ */ new Set();
3473
+ this.toolCallsByIndex.set(index, toolCallsWithIndex);
3474
+ }
3475
+ toolCallsWithIndex.add(toolCall);
3476
+ }
3477
+ createToolCallId(wireId) {
3478
+ var _a2, _b2;
3479
+ if (wireId != null && !this.usedToolCallIds.has(wireId)) {
3480
+ this.usedToolCallIds.add(wireId);
3481
+ return wireId;
3482
+ }
3483
+ const generatedId = (_a2 = this.getNonBlankString(this._generateId())) != null ? _a2 : "tool-call";
3484
+ if (!this.usedToolCallIds.has(generatedId)) {
3485
+ this.usedToolCallIds.add(generatedId);
3486
+ return generatedId;
3487
+ }
3488
+ const initialSuffix = (_b2 = this.nextGeneratedIdSuffixes.get(generatedId)) != null ? _b2 : 1;
3489
+ const maximumSuffix = initialSuffix + this.usedToolCallIds.size;
3490
+ for (let suffix = initialSuffix; suffix <= maximumSuffix; suffix++) {
3491
+ const suffixedId = `${generatedId}-${suffix}`;
3492
+ if (!this.usedToolCallIds.has(suffixedId)) {
3493
+ this.usedToolCallIds.add(suffixedId);
3494
+ this.nextGeneratedIdSuffixes.set(generatedId, suffix + 1);
3495
+ return suffixedId;
3496
+ }
3497
+ }
3498
+ throw new Error("Failed to create a unique tool call ID.");
3499
+ }
3500
+ getNonBlankString(value) {
3501
+ return value != null && value.trim().length > 0 ? value : void 0;
3502
+ }
3503
+ processExistingToolCall(toolCall, toolCallDelta) {
3504
+ var _a2;
3505
+ if (!toolCall.hasFinished && ((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
3506
+ toolCall.argumentState.append(toolCallDelta.function.arguments);
3507
+ toolCall.function.arguments += toolCallDelta.function.arguments;
3508
+ this.controller.enqueue({
3509
+ type: "tool-input-delta",
3510
+ id: toolCall.id,
3511
+ delta: toolCallDelta.function.arguments
3512
+ });
3513
+ }
3514
+ }
3515
+ finishToolCall(toolCall) {
3516
+ var _a2;
3517
+ this.controller.enqueue({
3518
+ type: "tool-input-end",
3519
+ id: toolCall.id
3520
+ });
3521
+ const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
3522
+ this,
3523
+ toolCall.metadata
3524
+ );
3525
+ this.controller.enqueue({
3526
+ type: "tool-call",
3527
+ toolCallId: toolCall.id,
3528
+ toolName: toolCall.function.name,
3529
+ input: toolCall.function.arguments,
3530
+ ...providerMetadata ? { providerMetadata } : {}
3531
+ });
3532
+ toolCall.hasFinished = true;
3533
+ }
3534
+ };
3535
+
3176
3536
  // src/strip-file-extension.ts
3177
3537
  function stripFileExtension(filename) {
3178
3538
  const firstDotIndex = filename.indexOf(".");
@@ -3218,6 +3578,7 @@ var import_stream2 = require("eventsource-parser/stream");
3218
3578
  EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
3219
3579
  EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER,
3220
3580
  EventSourceParserStream,
3581
+ StreamingToolCallTracker,
3221
3582
  VERSION,
3222
3583
  asSchema,
3223
3584
  cancelResponseBody,