@ai-sdk/provider-utils 4.0.54 → 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/dist/index.mjs CHANGED
@@ -822,7 +822,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
822
822
  }
823
823
 
824
824
  // src/version.ts
825
- var VERSION = true ? "4.0.54" : "0.0.0-test";
825
+ var VERSION = true ? "4.0.56" : "0.0.0-test";
826
826
 
827
827
  // src/get-from-api.ts
828
828
  var getOriginalFetch = () => globalThis.fetch;
@@ -958,7 +958,9 @@ function isUrlSupported({
958
958
  return Object.entries(supportedUrls).map(([key, value]) => {
959
959
  const mediaType2 = key.toLowerCase();
960
960
  return mediaType2 === "*" || mediaType2 === "*/*" ? { mediaTypePrefix: "", regexes: value } : { mediaTypePrefix: mediaType2.replace(/\*/, ""), regexes: value };
961
- }).filter(({ mediaTypePrefix }) => mediaType.startsWith(mediaTypePrefix)).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
961
+ }).filter(
962
+ ({ mediaTypePrefix }) => mediaTypePrefix === "" || mediaTypePrefix.endsWith("/") ? mediaType.startsWith(mediaTypePrefix) : mediaType === mediaTypePrefix
963
+ ).flatMap(({ regexes }) => regexes).some((pattern) => pattern.test(url));
962
964
  }
963
965
 
964
966
  // src/load-api-key.ts
@@ -3076,6 +3078,367 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
3076
3078
  };
3077
3079
  };
3078
3080
 
3081
+ // src/streaming-tool-call-tracker.ts
3082
+ import {
3083
+ InvalidResponseDataError
3084
+ } from "@ai-sdk/provider";
3085
+
3086
+ // src/streaming-tool-call-argument-state.ts
3087
+ function startsWithStructuredValue(value) {
3088
+ if (typeof value !== "string") {
3089
+ return false;
3090
+ }
3091
+ const firstCharacter = value.trimStart()[0];
3092
+ return firstCharacter === "{" || firstCharacter === "[";
3093
+ }
3094
+ var StreamingToolCallArgumentState = class {
3095
+ constructor(initialValue = "") {
3096
+ this.structure = { kind: "undetermined" };
3097
+ this.append(initialValue);
3098
+ }
3099
+ get hasCompleteStructuredValue() {
3100
+ return this.structure.kind === "structured" && this.structure.complete === true;
3101
+ }
3102
+ append(delta) {
3103
+ let nextStructure = this.structure;
3104
+ for (const character of delta) {
3105
+ if (nextStructure.kind === "undetermined") {
3106
+ if (/\s/.test(character)) {
3107
+ continue;
3108
+ }
3109
+ if (character !== "{" && character !== "[") {
3110
+ nextStructure = { kind: "other" };
3111
+ continue;
3112
+ }
3113
+ nextStructure = {
3114
+ kind: "structured",
3115
+ stack: [character],
3116
+ inString: false,
3117
+ escaped: false,
3118
+ complete: false
3119
+ };
3120
+ continue;
3121
+ }
3122
+ if (nextStructure.kind !== "structured" || nextStructure.complete) {
3123
+ continue;
3124
+ }
3125
+ if (nextStructure.inString) {
3126
+ if (nextStructure.escaped) {
3127
+ nextStructure.escaped = false;
3128
+ } else if (character === "\\") {
3129
+ nextStructure.escaped = true;
3130
+ } else if (character === '"') {
3131
+ nextStructure.inString = false;
3132
+ }
3133
+ continue;
3134
+ }
3135
+ if (character === '"') {
3136
+ nextStructure.inString = true;
3137
+ } else if (character === "{" || character === "[") {
3138
+ nextStructure.stack.push(character);
3139
+ } else if (character === "}" || character === "]") {
3140
+ const expectedOpening = character === "}" ? "{" : "[";
3141
+ if (nextStructure.stack[nextStructure.stack.length - 1] !== expectedOpening) {
3142
+ nextStructure = { kind: "other" };
3143
+ continue;
3144
+ }
3145
+ nextStructure.stack.pop();
3146
+ if (nextStructure.stack.length === 0) {
3147
+ nextStructure.complete = true;
3148
+ }
3149
+ }
3150
+ }
3151
+ this.structure = nextStructure;
3152
+ }
3153
+ };
3154
+
3155
+ // src/streaming-tool-call-tracker.ts
3156
+ var StreamingToolCallTracker = class {
3157
+ constructor(controller, options = {}) {
3158
+ this.toolCalls = [];
3159
+ this.toolCallsById = /* @__PURE__ */ new Map();
3160
+ this.toolCallsByIndex = /* @__PURE__ */ new Map();
3161
+ this.usedToolCallIds = /* @__PURE__ */ new Set();
3162
+ this.nextGeneratedIdSuffixes = /* @__PURE__ */ new Map();
3163
+ var _a2, _b2;
3164
+ this.controller = controller;
3165
+ this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
3166
+ this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
3167
+ this.extractMetadata = options.extractMetadata;
3168
+ this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
3169
+ }
3170
+ processDelta(toolCallDelta) {
3171
+ var _a2, _b2;
3172
+ const wireName = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.name;
3173
+ const hasBlankName = typeof wireName === "string" && wireName.trim().length === 0;
3174
+ const wireId = this.getNonBlankString(toolCallDelta.id);
3175
+ const name2 = this.getNonBlankString(wireName);
3176
+ const { index } = toolCallDelta;
3177
+ const resolution = this.resolveToolCall({
3178
+ wireId,
3179
+ index,
3180
+ name: name2,
3181
+ hasExplicitCallStart: name2 != null && startsWithStructuredValue((_b2 = toolCallDelta.function) == null ? void 0 : _b2.arguments)
3182
+ });
3183
+ if (resolution.kind === "ambiguous") {
3184
+ return;
3185
+ }
3186
+ let toolCall;
3187
+ if (resolution.kind === "new") {
3188
+ if (hasBlankName) {
3189
+ return;
3190
+ }
3191
+ toolCall = this.processNewToolCall(toolCallDelta, {
3192
+ wireId,
3193
+ index,
3194
+ name: name2
3195
+ });
3196
+ } else {
3197
+ toolCall = resolution.toolCall;
3198
+ if (wireId != null) {
3199
+ this.associateWireId(toolCall, wireId);
3200
+ }
3201
+ this.processExistingToolCall(toolCall, toolCallDelta);
3202
+ }
3203
+ if (index != null) {
3204
+ this.associateIndex(toolCall, index);
3205
+ }
3206
+ }
3207
+ flush() {
3208
+ const toolCalls = this.toolCalls.every((toolCall) => toolCall.index != null) ? [...this.toolCalls].sort(
3209
+ (a, b) => a.index - b.index || a.sequence - b.sequence
3210
+ ) : this.toolCalls;
3211
+ for (const toolCall of toolCalls) {
3212
+ if (!toolCall.hasFinished) {
3213
+ this.finishToolCall(toolCall);
3214
+ }
3215
+ }
3216
+ }
3217
+ resolveToolCall({
3218
+ wireId,
3219
+ index,
3220
+ name: name2,
3221
+ hasExplicitCallStart
3222
+ }) {
3223
+ const indexedToolCalls = index != null ? this.toolCallsByIndex.get(index) : void 0;
3224
+ const matchingIndexedToolCalls = this.filterToolCallsByName(
3225
+ indexedToolCalls,
3226
+ name2
3227
+ );
3228
+ if (wireId != null) {
3229
+ const toolCallsWithId = this.toolCallsById.get(wireId);
3230
+ if (toolCallsWithId != null) {
3231
+ if (index != null) {
3232
+ const matchingToolCalls = matchingIndexedToolCalls.filter(
3233
+ (toolCall) => toolCallsWithId.has(toolCall)
3234
+ );
3235
+ const matchingToolCall = this.resolveMatchingToolCall(
3236
+ matchingToolCalls,
3237
+ hasExplicitCallStart
3238
+ );
3239
+ if (matchingToolCall.kind !== "new") {
3240
+ return matchingToolCall;
3241
+ }
3242
+ if (name2 != null) {
3243
+ return { kind: "new" };
3244
+ }
3245
+ if (indexedToolCalls != null) {
3246
+ return { kind: "ambiguous" };
3247
+ }
3248
+ return this.resolveMatchingToolCall([...toolCallsWithId], false);
3249
+ }
3250
+ if (name2 != null) {
3251
+ const matchingToolCalls = [...toolCallsWithId].filter(
3252
+ (toolCall) => toolCall.function.name === name2
3253
+ );
3254
+ return this.resolveMatchingToolCall(
3255
+ matchingToolCalls,
3256
+ hasExplicitCallStart
3257
+ );
3258
+ }
3259
+ return this.resolveMatchingToolCall([...toolCallsWithId], false);
3260
+ }
3261
+ if (matchingIndexedToolCalls.length > 0) {
3262
+ return hasExplicitCallStart ? { kind: "new" } : this.resolveMatchingToolCall(matchingIndexedToolCalls, false);
3263
+ }
3264
+ return { kind: "new" };
3265
+ }
3266
+ if (indexedToolCalls != null) {
3267
+ return this.resolveMatchingToolCall(
3268
+ matchingIndexedToolCalls,
3269
+ hasExplicitCallStart
3270
+ );
3271
+ }
3272
+ if (name2 != null) {
3273
+ return { kind: "new" };
3274
+ }
3275
+ const unfinishedToolCalls = this.toolCalls.filter(
3276
+ (toolCall) => !toolCall.hasFinished
3277
+ );
3278
+ if (unfinishedToolCalls.length === 1) {
3279
+ return { kind: "existing", toolCall: unfinishedToolCalls[0] };
3280
+ }
3281
+ return unfinishedToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
3282
+ }
3283
+ filterToolCallsByName(toolCalls, name2) {
3284
+ if (toolCalls == null) {
3285
+ return [];
3286
+ }
3287
+ return [...toolCalls].filter(
3288
+ (toolCall) => name2 == null || toolCall.function.name === name2
3289
+ );
3290
+ }
3291
+ resolveMatchingToolCall(toolCalls, hasExplicitCallStart) {
3292
+ if (toolCalls.length === 0) {
3293
+ return { kind: "new" };
3294
+ }
3295
+ if (!hasExplicitCallStart) {
3296
+ return toolCalls.length === 1 ? { kind: "existing", toolCall: toolCalls[0] } : { kind: "ambiguous" };
3297
+ }
3298
+ const continuableToolCalls = toolCalls.filter(
3299
+ (toolCall) => !toolCall.argumentState.hasCompleteStructuredValue
3300
+ );
3301
+ if (continuableToolCalls.length === 1) {
3302
+ return { kind: "existing", toolCall: continuableToolCalls[0] };
3303
+ }
3304
+ return continuableToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
3305
+ }
3306
+ processNewToolCall(toolCallDelta, {
3307
+ wireId,
3308
+ index,
3309
+ name: name2
3310
+ }) {
3311
+ var _a2, _b2, _c, _d, _e;
3312
+ if (this.typeValidation === "required") {
3313
+ if (toolCallDelta.type !== "function") {
3314
+ throw new InvalidResponseDataError({
3315
+ data: toolCallDelta,
3316
+ message: `Expected 'function' type.`
3317
+ });
3318
+ }
3319
+ } else if (this.typeValidation === "if-present") {
3320
+ if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
3321
+ throw new InvalidResponseDataError({
3322
+ data: toolCallDelta,
3323
+ message: `Expected 'function' type.`
3324
+ });
3325
+ }
3326
+ }
3327
+ if (name2 == null) {
3328
+ throw new InvalidResponseDataError({
3329
+ data: toolCallDelta,
3330
+ message: `Expected 'function.name' to be a string.`
3331
+ });
3332
+ }
3333
+ const id = this.createToolCallId(wireId);
3334
+ this.controller.enqueue({
3335
+ type: "tool-input-start",
3336
+ id,
3337
+ toolName: name2
3338
+ });
3339
+ const toolCall = {
3340
+ id,
3341
+ index: index != null ? index : void 0,
3342
+ sequence: this.toolCalls.length,
3343
+ type: "function",
3344
+ function: {
3345
+ name: name2,
3346
+ arguments: (_b2 = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null ? _b2 : ""
3347
+ },
3348
+ argumentState: new StreamingToolCallArgumentState(
3349
+ (_d = (_c = toolCallDelta.function) == null ? void 0 : _c.arguments) != null ? _d : ""
3350
+ ),
3351
+ hasFinished: false,
3352
+ metadata: (_e = this.extractMetadata) == null ? void 0 : _e.call(this, toolCallDelta)
3353
+ };
3354
+ this.toolCalls.push(toolCall);
3355
+ if (wireId != null) {
3356
+ this.associateWireId(toolCall, wireId);
3357
+ }
3358
+ if (toolCall.function.arguments.length > 0) {
3359
+ this.controller.enqueue({
3360
+ type: "tool-input-delta",
3361
+ id: toolCall.id,
3362
+ delta: toolCall.function.arguments
3363
+ });
3364
+ }
3365
+ return toolCall;
3366
+ }
3367
+ associateWireId(toolCall, wireId) {
3368
+ let toolCallsWithId = this.toolCallsById.get(wireId);
3369
+ if (toolCallsWithId == null) {
3370
+ toolCallsWithId = /* @__PURE__ */ new Set();
3371
+ this.toolCallsById.set(wireId, toolCallsWithId);
3372
+ }
3373
+ toolCallsWithId.add(toolCall);
3374
+ }
3375
+ associateIndex(toolCall, index) {
3376
+ let toolCallsWithIndex = this.toolCallsByIndex.get(index);
3377
+ if (toolCallsWithIndex == null) {
3378
+ toolCallsWithIndex = /* @__PURE__ */ new Set();
3379
+ this.toolCallsByIndex.set(index, toolCallsWithIndex);
3380
+ }
3381
+ toolCallsWithIndex.add(toolCall);
3382
+ }
3383
+ createToolCallId(wireId) {
3384
+ var _a2, _b2;
3385
+ if (wireId != null && !this.usedToolCallIds.has(wireId)) {
3386
+ this.usedToolCallIds.add(wireId);
3387
+ return wireId;
3388
+ }
3389
+ const generatedId = (_a2 = this.getNonBlankString(this._generateId())) != null ? _a2 : "tool-call";
3390
+ if (!this.usedToolCallIds.has(generatedId)) {
3391
+ this.usedToolCallIds.add(generatedId);
3392
+ return generatedId;
3393
+ }
3394
+ const initialSuffix = (_b2 = this.nextGeneratedIdSuffixes.get(generatedId)) != null ? _b2 : 1;
3395
+ const maximumSuffix = initialSuffix + this.usedToolCallIds.size;
3396
+ for (let suffix = initialSuffix; suffix <= maximumSuffix; suffix++) {
3397
+ const suffixedId = `${generatedId}-${suffix}`;
3398
+ if (!this.usedToolCallIds.has(suffixedId)) {
3399
+ this.usedToolCallIds.add(suffixedId);
3400
+ this.nextGeneratedIdSuffixes.set(generatedId, suffix + 1);
3401
+ return suffixedId;
3402
+ }
3403
+ }
3404
+ throw new Error("Failed to create a unique tool call ID.");
3405
+ }
3406
+ getNonBlankString(value) {
3407
+ return value != null && value.trim().length > 0 ? value : void 0;
3408
+ }
3409
+ processExistingToolCall(toolCall, toolCallDelta) {
3410
+ var _a2;
3411
+ if (!toolCall.hasFinished && ((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
3412
+ toolCall.argumentState.append(toolCallDelta.function.arguments);
3413
+ toolCall.function.arguments += toolCallDelta.function.arguments;
3414
+ this.controller.enqueue({
3415
+ type: "tool-input-delta",
3416
+ id: toolCall.id,
3417
+ delta: toolCallDelta.function.arguments
3418
+ });
3419
+ }
3420
+ }
3421
+ finishToolCall(toolCall) {
3422
+ var _a2;
3423
+ this.controller.enqueue({
3424
+ type: "tool-input-end",
3425
+ id: toolCall.id
3426
+ });
3427
+ const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
3428
+ this,
3429
+ toolCall.metadata
3430
+ );
3431
+ this.controller.enqueue({
3432
+ type: "tool-call",
3433
+ toolCallId: toolCall.id,
3434
+ toolName: toolCall.function.name,
3435
+ input: toolCall.function.arguments,
3436
+ ...providerMetadata ? { providerMetadata } : {}
3437
+ });
3438
+ toolCall.hasFinished = true;
3439
+ }
3440
+ };
3441
+
3079
3442
  // src/strip-file-extension.ts
3080
3443
  function stripFileExtension(filename) {
3081
3444
  const firstDotIndex = filename.indexOf(".");
@@ -3122,6 +3485,7 @@ export {
3122
3485
  EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
3123
3486
  EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER as EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER,
3124
3487
  EventSourceParserStream2 as EventSourceParserStream,
3488
+ StreamingToolCallTracker,
3125
3489
  VERSION,
3126
3490
  asSchema,
3127
3491
  cancelResponseBody,