@ai-sdk/provider-utils 3.0.39 → 3.0.41
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 +14 -0
- package/dist/index.d.mts +107 -8
- package/dist/index.d.ts +107 -8
- package/dist/index.js +410 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +410 -30
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __export(index_exports, {
|
|
|
36
36
|
DownloadError: () => DownloadError,
|
|
37
37
|
EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL: () => EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
|
|
38
38
|
EventSourceParserStream: () => import_stream2.EventSourceParserStream,
|
|
39
|
+
StreamingToolCallTracker: () => StreamingToolCallTracker,
|
|
39
40
|
VERSION: () => VERSION,
|
|
40
41
|
asSchema: () => asSchema,
|
|
41
42
|
asValidator: () => asValidator,
|
|
@@ -58,6 +59,7 @@ __export(index_exports, {
|
|
|
58
59
|
dynamicTool: () => dynamicTool,
|
|
59
60
|
executeTool: () => executeTool,
|
|
60
61
|
extractResponseHeaders: () => extractResponseHeaders,
|
|
62
|
+
fetchWithValidatedEndpoint: () => fetchWithValidatedEndpoint,
|
|
61
63
|
fetchWithValidatedRedirects: () => fetchWithValidatedRedirects,
|
|
62
64
|
generateId: () => generateId,
|
|
63
65
|
getErrorMessage: () => getErrorMessage,
|
|
@@ -275,6 +277,15 @@ function isBrowserRuntime(globalThisAny = globalThis) {
|
|
|
275
277
|
return globalThisAny.window != null;
|
|
276
278
|
}
|
|
277
279
|
|
|
280
|
+
// src/is-same-origin.ts
|
|
281
|
+
function isSameOrigin(url, baseUrl) {
|
|
282
|
+
try {
|
|
283
|
+
return new URL(url).origin === new URL(baseUrl).origin;
|
|
284
|
+
} catch (e) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
278
289
|
// src/validate-download-url.ts
|
|
279
290
|
function validateDownloadUrl(url) {
|
|
280
291
|
let parsed;
|
|
@@ -460,34 +471,24 @@ function createSafeLookup(lookup) {
|
|
|
460
471
|
});
|
|
461
472
|
}
|
|
462
473
|
var safeNodeFetchPromise;
|
|
463
|
-
var initialGlobalFetch = globalThis.fetch;
|
|
464
|
-
var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
|
|
465
474
|
function isNodeRuntime() {
|
|
466
|
-
var _a2, _b2;
|
|
475
|
+
var _a2, _b2, _c;
|
|
467
476
|
const runtimeProcess = globalThis.process;
|
|
468
|
-
return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null;
|
|
477
|
+
return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null && ((_c = runtimeProcess.versions) == null ? void 0 : _c.deno) == null && runtimeProcess.title !== "workerd" && globalThis.EdgeRuntime == null;
|
|
469
478
|
}
|
|
470
479
|
async function getDefaultDownloadFetch() {
|
|
471
|
-
if (!isNodeRuntime()
|
|
480
|
+
if (!isNodeRuntime()) {
|
|
472
481
|
return globalThis.fetch;
|
|
473
482
|
}
|
|
474
483
|
return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
|
|
475
484
|
}
|
|
476
|
-
function isNodeDefaultFetch(fetch) {
|
|
477
|
-
if (typeof fetch !== "function") {
|
|
478
|
-
return false;
|
|
479
|
-
}
|
|
480
|
-
const source = Function.prototype.toString.call(fetch);
|
|
481
|
-
return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
|
|
482
|
-
}
|
|
483
485
|
async function createSafeNodeFetch() {
|
|
484
|
-
const [
|
|
486
|
+
const [module2, { lookup }] = await Promise.all([
|
|
485
487
|
loadNodeModule("node:module"),
|
|
486
488
|
loadNodeModule("node:dns")
|
|
487
489
|
]);
|
|
488
|
-
const
|
|
489
|
-
|
|
490
|
-
);
|
|
490
|
+
const nodeRequire = module2.createRequire(getCurrentModulePath());
|
|
491
|
+
const { Agent, fetch } = nodeRequire("undici");
|
|
491
492
|
const dispatcher = new Agent({
|
|
492
493
|
connect: {
|
|
493
494
|
lookup: createSafeLookup(lookup)
|
|
@@ -529,20 +530,47 @@ function getCurrentModulePath() {
|
|
|
529
530
|
|
|
530
531
|
// src/fetch-with-validated-redirects.ts
|
|
531
532
|
var MAX_DOWNLOAD_REDIRECTS = 10;
|
|
533
|
+
async function getValidatedFetch(customFetch) {
|
|
534
|
+
return customFetch == null || customFetch === globalThis.fetch ? await getDefaultDownloadFetch() : customFetch;
|
|
535
|
+
}
|
|
536
|
+
async function fetchWithValidatedEndpoint({
|
|
537
|
+
url,
|
|
538
|
+
init,
|
|
539
|
+
fetch: customFetch,
|
|
540
|
+
trustedOrigin,
|
|
541
|
+
redirect = "error"
|
|
542
|
+
}) {
|
|
543
|
+
const urlText = url.toString();
|
|
544
|
+
const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
|
|
545
|
+
if (!isTrusted) {
|
|
546
|
+
validateDownloadUrl(urlText);
|
|
547
|
+
}
|
|
548
|
+
const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
|
|
549
|
+
return await fetch(url, {
|
|
550
|
+
...init,
|
|
551
|
+
redirect
|
|
552
|
+
});
|
|
553
|
+
}
|
|
532
554
|
async function fetchWithValidatedRedirects({
|
|
533
555
|
url,
|
|
534
556
|
headers,
|
|
535
557
|
abortSignal,
|
|
536
|
-
maxRedirects = MAX_DOWNLOAD_REDIRECTS
|
|
558
|
+
maxRedirects = MAX_DOWNLOAD_REDIRECTS,
|
|
559
|
+
fetch: customFetch,
|
|
560
|
+
trustedOrigin
|
|
537
561
|
}) {
|
|
562
|
+
var _a2;
|
|
538
563
|
const baseInit = { signal: abortSignal };
|
|
539
564
|
if (headers !== void 0) {
|
|
540
565
|
baseInit.headers = headers;
|
|
541
566
|
}
|
|
542
567
|
let currentUrl = url;
|
|
543
568
|
for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
|
|
544
|
-
|
|
545
|
-
|
|
569
|
+
const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
|
|
570
|
+
if (!isTrustedHop) {
|
|
571
|
+
validateDownloadUrl(currentUrl);
|
|
572
|
+
}
|
|
573
|
+
const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
|
|
546
574
|
const response = await fetch(currentUrl, {
|
|
547
575
|
...baseInit,
|
|
548
576
|
redirect: "manual"
|
|
@@ -556,7 +584,7 @@ async function fetchWithValidatedRedirects({
|
|
|
556
584
|
}
|
|
557
585
|
return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
|
|
558
586
|
}
|
|
559
|
-
const location = response.headers.get("location");
|
|
587
|
+
const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
|
|
560
588
|
if (response.status >= 300 && response.status < 400 && location) {
|
|
561
589
|
await cancelResponseBody(response);
|
|
562
590
|
currentUrl = new URL(location, currentUrl).toString();
|
|
@@ -810,7 +838,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
810
838
|
}
|
|
811
839
|
|
|
812
840
|
// src/version.ts
|
|
813
|
-
var VERSION = true ? "3.0.
|
|
841
|
+
var VERSION = true ? "3.0.41" : "0.0.0-test";
|
|
814
842
|
|
|
815
843
|
// src/get-from-api.ts
|
|
816
844
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -921,15 +949,6 @@ function injectJsonInstructionIntoMessages({
|
|
|
921
949
|
];
|
|
922
950
|
}
|
|
923
951
|
|
|
924
|
-
// src/is-same-origin.ts
|
|
925
|
-
function isSameOrigin(url, baseUrl) {
|
|
926
|
-
try {
|
|
927
|
-
return new URL(url).origin === new URL(baseUrl).origin;
|
|
928
|
-
} catch (e) {
|
|
929
|
-
return false;
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
|
|
933
952
|
// src/is-url-supported.ts
|
|
934
953
|
function isUrlSupported({
|
|
935
954
|
mediaType,
|
|
@@ -2994,6 +3013,365 @@ function asSchema(schema) {
|
|
|
2994
3013
|
}) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : zodSchema(schema);
|
|
2995
3014
|
}
|
|
2996
3015
|
|
|
3016
|
+
// src/streaming-tool-call-tracker.ts
|
|
3017
|
+
var import_provider13 = require("@ai-sdk/provider");
|
|
3018
|
+
|
|
3019
|
+
// src/streaming-tool-call-argument-state.ts
|
|
3020
|
+
function startsWithStructuredValue(value) {
|
|
3021
|
+
if (typeof value !== "string") {
|
|
3022
|
+
return false;
|
|
3023
|
+
}
|
|
3024
|
+
const firstCharacter = value.trimStart()[0];
|
|
3025
|
+
return firstCharacter === "{" || firstCharacter === "[";
|
|
3026
|
+
}
|
|
3027
|
+
var StreamingToolCallArgumentState = class {
|
|
3028
|
+
constructor(initialValue = "") {
|
|
3029
|
+
this.structure = { kind: "undetermined" };
|
|
3030
|
+
this.append(initialValue);
|
|
3031
|
+
}
|
|
3032
|
+
get hasCompleteStructuredValue() {
|
|
3033
|
+
return this.structure.kind === "structured" && this.structure.complete === true;
|
|
3034
|
+
}
|
|
3035
|
+
append(delta) {
|
|
3036
|
+
let nextStructure = this.structure;
|
|
3037
|
+
for (const character of delta) {
|
|
3038
|
+
if (nextStructure.kind === "undetermined") {
|
|
3039
|
+
if (/\s/.test(character)) {
|
|
3040
|
+
continue;
|
|
3041
|
+
}
|
|
3042
|
+
if (character !== "{" && character !== "[") {
|
|
3043
|
+
nextStructure = { kind: "other" };
|
|
3044
|
+
continue;
|
|
3045
|
+
}
|
|
3046
|
+
nextStructure = {
|
|
3047
|
+
kind: "structured",
|
|
3048
|
+
stack: [character],
|
|
3049
|
+
inString: false,
|
|
3050
|
+
escaped: false,
|
|
3051
|
+
complete: false
|
|
3052
|
+
};
|
|
3053
|
+
continue;
|
|
3054
|
+
}
|
|
3055
|
+
if (nextStructure.kind !== "structured" || nextStructure.complete) {
|
|
3056
|
+
continue;
|
|
3057
|
+
}
|
|
3058
|
+
if (nextStructure.inString) {
|
|
3059
|
+
if (nextStructure.escaped) {
|
|
3060
|
+
nextStructure.escaped = false;
|
|
3061
|
+
} else if (character === "\\") {
|
|
3062
|
+
nextStructure.escaped = true;
|
|
3063
|
+
} else if (character === '"') {
|
|
3064
|
+
nextStructure.inString = false;
|
|
3065
|
+
}
|
|
3066
|
+
continue;
|
|
3067
|
+
}
|
|
3068
|
+
if (character === '"') {
|
|
3069
|
+
nextStructure.inString = true;
|
|
3070
|
+
} else if (character === "{" || character === "[") {
|
|
3071
|
+
nextStructure.stack.push(character);
|
|
3072
|
+
} else if (character === "}" || character === "]") {
|
|
3073
|
+
const expectedOpening = character === "}" ? "{" : "[";
|
|
3074
|
+
if (nextStructure.stack[nextStructure.stack.length - 1] !== expectedOpening) {
|
|
3075
|
+
nextStructure = { kind: "other" };
|
|
3076
|
+
continue;
|
|
3077
|
+
}
|
|
3078
|
+
nextStructure.stack.pop();
|
|
3079
|
+
if (nextStructure.stack.length === 0) {
|
|
3080
|
+
nextStructure.complete = true;
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
}
|
|
3084
|
+
this.structure = nextStructure;
|
|
3085
|
+
}
|
|
3086
|
+
};
|
|
3087
|
+
|
|
3088
|
+
// src/streaming-tool-call-tracker.ts
|
|
3089
|
+
var StreamingToolCallTracker = class {
|
|
3090
|
+
constructor(controller, options = {}) {
|
|
3091
|
+
this.toolCalls = [];
|
|
3092
|
+
this.toolCallsById = /* @__PURE__ */ new Map();
|
|
3093
|
+
this.toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
3094
|
+
this.usedToolCallIds = /* @__PURE__ */ new Set();
|
|
3095
|
+
this.nextGeneratedIdSuffixes = /* @__PURE__ */ new Map();
|
|
3096
|
+
var _a2, _b2;
|
|
3097
|
+
this.controller = controller;
|
|
3098
|
+
this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
|
|
3099
|
+
this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
|
|
3100
|
+
this.extractMetadata = options.extractMetadata;
|
|
3101
|
+
this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
|
|
3102
|
+
}
|
|
3103
|
+
processDelta(toolCallDelta) {
|
|
3104
|
+
var _a2, _b2;
|
|
3105
|
+
const wireName = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.name;
|
|
3106
|
+
const hasBlankName = typeof wireName === "string" && wireName.trim().length === 0;
|
|
3107
|
+
const wireId = this.getNonBlankString(toolCallDelta.id);
|
|
3108
|
+
const name2 = this.getNonBlankString(wireName);
|
|
3109
|
+
const { index } = toolCallDelta;
|
|
3110
|
+
const resolution = this.resolveToolCall({
|
|
3111
|
+
wireId,
|
|
3112
|
+
index,
|
|
3113
|
+
name: name2,
|
|
3114
|
+
hasExplicitCallStart: name2 != null && startsWithStructuredValue((_b2 = toolCallDelta.function) == null ? void 0 : _b2.arguments)
|
|
3115
|
+
});
|
|
3116
|
+
if (resolution.kind === "ambiguous") {
|
|
3117
|
+
return;
|
|
3118
|
+
}
|
|
3119
|
+
let toolCall;
|
|
3120
|
+
if (resolution.kind === "new") {
|
|
3121
|
+
if (hasBlankName) {
|
|
3122
|
+
return;
|
|
3123
|
+
}
|
|
3124
|
+
toolCall = this.processNewToolCall(toolCallDelta, {
|
|
3125
|
+
wireId,
|
|
3126
|
+
index,
|
|
3127
|
+
name: name2
|
|
3128
|
+
});
|
|
3129
|
+
} else {
|
|
3130
|
+
toolCall = resolution.toolCall;
|
|
3131
|
+
if (wireId != null) {
|
|
3132
|
+
this.associateWireId(toolCall, wireId);
|
|
3133
|
+
}
|
|
3134
|
+
this.processExistingToolCall(toolCall, toolCallDelta);
|
|
3135
|
+
}
|
|
3136
|
+
if (index != null) {
|
|
3137
|
+
this.associateIndex(toolCall, index);
|
|
3138
|
+
}
|
|
3139
|
+
}
|
|
3140
|
+
flush() {
|
|
3141
|
+
const toolCalls = this.toolCalls.every((toolCall) => toolCall.index != null) ? [...this.toolCalls].sort(
|
|
3142
|
+
(a, b) => a.index - b.index || a.sequence - b.sequence
|
|
3143
|
+
) : this.toolCalls;
|
|
3144
|
+
for (const toolCall of toolCalls) {
|
|
3145
|
+
if (!toolCall.hasFinished) {
|
|
3146
|
+
this.finishToolCall(toolCall);
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
}
|
|
3150
|
+
resolveToolCall({
|
|
3151
|
+
wireId,
|
|
3152
|
+
index,
|
|
3153
|
+
name: name2,
|
|
3154
|
+
hasExplicitCallStart
|
|
3155
|
+
}) {
|
|
3156
|
+
const indexedToolCalls = index != null ? this.toolCallsByIndex.get(index) : void 0;
|
|
3157
|
+
const matchingIndexedToolCalls = this.filterToolCallsByName(
|
|
3158
|
+
indexedToolCalls,
|
|
3159
|
+
name2
|
|
3160
|
+
);
|
|
3161
|
+
if (wireId != null) {
|
|
3162
|
+
const toolCallsWithId = this.toolCallsById.get(wireId);
|
|
3163
|
+
if (toolCallsWithId != null) {
|
|
3164
|
+
if (index != null) {
|
|
3165
|
+
const matchingToolCalls = matchingIndexedToolCalls.filter(
|
|
3166
|
+
(toolCall) => toolCallsWithId.has(toolCall)
|
|
3167
|
+
);
|
|
3168
|
+
const matchingToolCall = this.resolveMatchingToolCall(
|
|
3169
|
+
matchingToolCalls,
|
|
3170
|
+
hasExplicitCallStart
|
|
3171
|
+
);
|
|
3172
|
+
if (matchingToolCall.kind !== "new") {
|
|
3173
|
+
return matchingToolCall;
|
|
3174
|
+
}
|
|
3175
|
+
if (name2 != null) {
|
|
3176
|
+
return { kind: "new" };
|
|
3177
|
+
}
|
|
3178
|
+
if (indexedToolCalls != null) {
|
|
3179
|
+
return { kind: "ambiguous" };
|
|
3180
|
+
}
|
|
3181
|
+
return this.resolveMatchingToolCall([...toolCallsWithId], false);
|
|
3182
|
+
}
|
|
3183
|
+
if (name2 != null) {
|
|
3184
|
+
const matchingToolCalls = [...toolCallsWithId].filter(
|
|
3185
|
+
(toolCall) => toolCall.function.name === name2
|
|
3186
|
+
);
|
|
3187
|
+
return this.resolveMatchingToolCall(
|
|
3188
|
+
matchingToolCalls,
|
|
3189
|
+
hasExplicitCallStart
|
|
3190
|
+
);
|
|
3191
|
+
}
|
|
3192
|
+
return this.resolveMatchingToolCall([...toolCallsWithId], false);
|
|
3193
|
+
}
|
|
3194
|
+
if (matchingIndexedToolCalls.length > 0) {
|
|
3195
|
+
return hasExplicitCallStart ? { kind: "new" } : this.resolveMatchingToolCall(matchingIndexedToolCalls, false);
|
|
3196
|
+
}
|
|
3197
|
+
return { kind: "new" };
|
|
3198
|
+
}
|
|
3199
|
+
if (indexedToolCalls != null) {
|
|
3200
|
+
return this.resolveMatchingToolCall(
|
|
3201
|
+
matchingIndexedToolCalls,
|
|
3202
|
+
hasExplicitCallStart
|
|
3203
|
+
);
|
|
3204
|
+
}
|
|
3205
|
+
if (name2 != null) {
|
|
3206
|
+
return { kind: "new" };
|
|
3207
|
+
}
|
|
3208
|
+
const unfinishedToolCalls = this.toolCalls.filter(
|
|
3209
|
+
(toolCall) => !toolCall.hasFinished
|
|
3210
|
+
);
|
|
3211
|
+
if (unfinishedToolCalls.length === 1) {
|
|
3212
|
+
return { kind: "existing", toolCall: unfinishedToolCalls[0] };
|
|
3213
|
+
}
|
|
3214
|
+
return unfinishedToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
|
|
3215
|
+
}
|
|
3216
|
+
filterToolCallsByName(toolCalls, name2) {
|
|
3217
|
+
if (toolCalls == null) {
|
|
3218
|
+
return [];
|
|
3219
|
+
}
|
|
3220
|
+
return [...toolCalls].filter(
|
|
3221
|
+
(toolCall) => name2 == null || toolCall.function.name === name2
|
|
3222
|
+
);
|
|
3223
|
+
}
|
|
3224
|
+
resolveMatchingToolCall(toolCalls, hasExplicitCallStart) {
|
|
3225
|
+
if (toolCalls.length === 0) {
|
|
3226
|
+
return { kind: "new" };
|
|
3227
|
+
}
|
|
3228
|
+
if (!hasExplicitCallStart) {
|
|
3229
|
+
return toolCalls.length === 1 ? { kind: "existing", toolCall: toolCalls[0] } : { kind: "ambiguous" };
|
|
3230
|
+
}
|
|
3231
|
+
const continuableToolCalls = toolCalls.filter(
|
|
3232
|
+
(toolCall) => !toolCall.argumentState.hasCompleteStructuredValue
|
|
3233
|
+
);
|
|
3234
|
+
if (continuableToolCalls.length === 1) {
|
|
3235
|
+
return { kind: "existing", toolCall: continuableToolCalls[0] };
|
|
3236
|
+
}
|
|
3237
|
+
return continuableToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
|
|
3238
|
+
}
|
|
3239
|
+
processNewToolCall(toolCallDelta, {
|
|
3240
|
+
wireId,
|
|
3241
|
+
index,
|
|
3242
|
+
name: name2
|
|
3243
|
+
}) {
|
|
3244
|
+
var _a2, _b2, _c, _d, _e;
|
|
3245
|
+
if (this.typeValidation === "required") {
|
|
3246
|
+
if (toolCallDelta.type !== "function") {
|
|
3247
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3248
|
+
data: toolCallDelta,
|
|
3249
|
+
message: `Expected 'function' type.`
|
|
3250
|
+
});
|
|
3251
|
+
}
|
|
3252
|
+
} else if (this.typeValidation === "if-present") {
|
|
3253
|
+
if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
|
|
3254
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3255
|
+
data: toolCallDelta,
|
|
3256
|
+
message: `Expected 'function' type.`
|
|
3257
|
+
});
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
if (name2 == null) {
|
|
3261
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3262
|
+
data: toolCallDelta,
|
|
3263
|
+
message: `Expected 'function.name' to be a string.`
|
|
3264
|
+
});
|
|
3265
|
+
}
|
|
3266
|
+
const id = this.createToolCallId(wireId);
|
|
3267
|
+
this.controller.enqueue({
|
|
3268
|
+
type: "tool-input-start",
|
|
3269
|
+
id,
|
|
3270
|
+
toolName: name2
|
|
3271
|
+
});
|
|
3272
|
+
const toolCall = {
|
|
3273
|
+
id,
|
|
3274
|
+
index: index != null ? index : void 0,
|
|
3275
|
+
sequence: this.toolCalls.length,
|
|
3276
|
+
type: "function",
|
|
3277
|
+
function: {
|
|
3278
|
+
name: name2,
|
|
3279
|
+
arguments: (_b2 = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null ? _b2 : ""
|
|
3280
|
+
},
|
|
3281
|
+
argumentState: new StreamingToolCallArgumentState(
|
|
3282
|
+
(_d = (_c = toolCallDelta.function) == null ? void 0 : _c.arguments) != null ? _d : ""
|
|
3283
|
+
),
|
|
3284
|
+
hasFinished: false,
|
|
3285
|
+
metadata: (_e = this.extractMetadata) == null ? void 0 : _e.call(this, toolCallDelta)
|
|
3286
|
+
};
|
|
3287
|
+
this.toolCalls.push(toolCall);
|
|
3288
|
+
if (wireId != null) {
|
|
3289
|
+
this.associateWireId(toolCall, wireId);
|
|
3290
|
+
}
|
|
3291
|
+
if (toolCall.function.arguments.length > 0) {
|
|
3292
|
+
this.controller.enqueue({
|
|
3293
|
+
type: "tool-input-delta",
|
|
3294
|
+
id: toolCall.id,
|
|
3295
|
+
delta: toolCall.function.arguments
|
|
3296
|
+
});
|
|
3297
|
+
}
|
|
3298
|
+
return toolCall;
|
|
3299
|
+
}
|
|
3300
|
+
associateWireId(toolCall, wireId) {
|
|
3301
|
+
let toolCallsWithId = this.toolCallsById.get(wireId);
|
|
3302
|
+
if (toolCallsWithId == null) {
|
|
3303
|
+
toolCallsWithId = /* @__PURE__ */ new Set();
|
|
3304
|
+
this.toolCallsById.set(wireId, toolCallsWithId);
|
|
3305
|
+
}
|
|
3306
|
+
toolCallsWithId.add(toolCall);
|
|
3307
|
+
}
|
|
3308
|
+
associateIndex(toolCall, index) {
|
|
3309
|
+
let toolCallsWithIndex = this.toolCallsByIndex.get(index);
|
|
3310
|
+
if (toolCallsWithIndex == null) {
|
|
3311
|
+
toolCallsWithIndex = /* @__PURE__ */ new Set();
|
|
3312
|
+
this.toolCallsByIndex.set(index, toolCallsWithIndex);
|
|
3313
|
+
}
|
|
3314
|
+
toolCallsWithIndex.add(toolCall);
|
|
3315
|
+
}
|
|
3316
|
+
createToolCallId(wireId) {
|
|
3317
|
+
var _a2, _b2;
|
|
3318
|
+
if (wireId != null && !this.usedToolCallIds.has(wireId)) {
|
|
3319
|
+
this.usedToolCallIds.add(wireId);
|
|
3320
|
+
return wireId;
|
|
3321
|
+
}
|
|
3322
|
+
const generatedId = (_a2 = this.getNonBlankString(this._generateId())) != null ? _a2 : "tool-call";
|
|
3323
|
+
if (!this.usedToolCallIds.has(generatedId)) {
|
|
3324
|
+
this.usedToolCallIds.add(generatedId);
|
|
3325
|
+
return generatedId;
|
|
3326
|
+
}
|
|
3327
|
+
const initialSuffix = (_b2 = this.nextGeneratedIdSuffixes.get(generatedId)) != null ? _b2 : 1;
|
|
3328
|
+
const maximumSuffix = initialSuffix + this.usedToolCallIds.size;
|
|
3329
|
+
for (let suffix = initialSuffix; suffix <= maximumSuffix; suffix++) {
|
|
3330
|
+
const suffixedId = `${generatedId}-${suffix}`;
|
|
3331
|
+
if (!this.usedToolCallIds.has(suffixedId)) {
|
|
3332
|
+
this.usedToolCallIds.add(suffixedId);
|
|
3333
|
+
this.nextGeneratedIdSuffixes.set(generatedId, suffix + 1);
|
|
3334
|
+
return suffixedId;
|
|
3335
|
+
}
|
|
3336
|
+
}
|
|
3337
|
+
throw new Error("Failed to create a unique tool call ID.");
|
|
3338
|
+
}
|
|
3339
|
+
getNonBlankString(value) {
|
|
3340
|
+
return value != null && value.trim().length > 0 ? value : void 0;
|
|
3341
|
+
}
|
|
3342
|
+
processExistingToolCall(toolCall, toolCallDelta) {
|
|
3343
|
+
var _a2;
|
|
3344
|
+
if (!toolCall.hasFinished && ((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
|
|
3345
|
+
toolCall.argumentState.append(toolCallDelta.function.arguments);
|
|
3346
|
+
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
3347
|
+
this.controller.enqueue({
|
|
3348
|
+
type: "tool-input-delta",
|
|
3349
|
+
id: toolCall.id,
|
|
3350
|
+
delta: toolCallDelta.function.arguments
|
|
3351
|
+
});
|
|
3352
|
+
}
|
|
3353
|
+
}
|
|
3354
|
+
finishToolCall(toolCall) {
|
|
3355
|
+
var _a2;
|
|
3356
|
+
this.controller.enqueue({
|
|
3357
|
+
type: "tool-input-end",
|
|
3358
|
+
id: toolCall.id
|
|
3359
|
+
});
|
|
3360
|
+
const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
|
|
3361
|
+
this,
|
|
3362
|
+
toolCall.metadata
|
|
3363
|
+
);
|
|
3364
|
+
this.controller.enqueue({
|
|
3365
|
+
type: "tool-call",
|
|
3366
|
+
toolCallId: toolCall.id,
|
|
3367
|
+
toolName: toolCall.function.name,
|
|
3368
|
+
input: toolCall.function.arguments,
|
|
3369
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
3370
|
+
});
|
|
3371
|
+
toolCall.hasFinished = true;
|
|
3372
|
+
}
|
|
3373
|
+
};
|
|
3374
|
+
|
|
2997
3375
|
// src/uint8-utils.ts
|
|
2998
3376
|
var { btoa, atob } = globalThis;
|
|
2999
3377
|
function convertBase64ToUint8Array(base64String) {
|
|
@@ -3052,6 +3430,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
|
3052
3430
|
DownloadError,
|
|
3053
3431
|
EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
|
|
3054
3432
|
EventSourceParserStream,
|
|
3433
|
+
StreamingToolCallTracker,
|
|
3055
3434
|
VERSION,
|
|
3056
3435
|
asSchema,
|
|
3057
3436
|
asValidator,
|
|
@@ -3074,6 +3453,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
|
3074
3453
|
dynamicTool,
|
|
3075
3454
|
executeTool,
|
|
3076
3455
|
extractResponseHeaders,
|
|
3456
|
+
fetchWithValidatedEndpoint,
|
|
3077
3457
|
fetchWithValidatedRedirects,
|
|
3078
3458
|
generateId,
|
|
3079
3459
|
getErrorMessage,
|