@ai-sdk/provider-utils 4.0.55 → 4.0.57
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/src/fetch-with-validated-redirects.ts +90 -3
- package/src/index.ts +9 -1
- package/src/safe-node-fetch.ts +20 -25
- package/src/streaming-tool-call-argument-state.ts +102 -0
- package/src/streaming-tool-call-tracker.ts +465 -0
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,
|
|
@@ -60,6 +61,7 @@ __export(index_exports, {
|
|
|
60
61
|
dynamicTool: () => dynamicTool,
|
|
61
62
|
executeTool: () => executeTool,
|
|
62
63
|
extractResponseHeaders: () => extractResponseHeaders,
|
|
64
|
+
fetchWithValidatedEndpoint: () => fetchWithValidatedEndpoint,
|
|
63
65
|
fetchWithValidatedRedirects: () => fetchWithValidatedRedirects,
|
|
64
66
|
generateId: () => generateId,
|
|
65
67
|
getErrorMessage: () => getErrorMessage,
|
|
@@ -352,6 +354,15 @@ function isBrowserRuntime(globalThisAny = globalThis) {
|
|
|
352
354
|
return globalThisAny.window != null;
|
|
353
355
|
}
|
|
354
356
|
|
|
357
|
+
// src/is-same-origin.ts
|
|
358
|
+
function isSameOrigin(url, baseUrl) {
|
|
359
|
+
try {
|
|
360
|
+
return new URL(url).origin === new URL(baseUrl).origin;
|
|
361
|
+
} catch (e) {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
355
366
|
// src/validate-download-url.ts
|
|
356
367
|
function validateDownloadUrl(url) {
|
|
357
368
|
let parsed;
|
|
@@ -537,34 +548,24 @@ function createSafeLookup(lookup) {
|
|
|
537
548
|
});
|
|
538
549
|
}
|
|
539
550
|
var safeNodeFetchPromise;
|
|
540
|
-
var initialGlobalFetch = globalThis.fetch;
|
|
541
|
-
var initialGlobalFetchIsNodeDefault = isNodeDefaultFetch(initialGlobalFetch);
|
|
542
551
|
function isNodeRuntime() {
|
|
543
|
-
var _a2, _b2;
|
|
552
|
+
var _a2, _b2, _c;
|
|
544
553
|
const runtimeProcess = globalThis.process;
|
|
545
|
-
return ((_a2 = runtimeProcess == null ? void 0 : runtimeProcess.release) == null ? void 0 : _a2.name) === "node" && ((_b2 = runtimeProcess.versions) == null ? void 0 : _b2.bun) == null;
|
|
554
|
+
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;
|
|
546
555
|
}
|
|
547
556
|
async function getDefaultDownloadFetch() {
|
|
548
|
-
if (!isNodeRuntime()
|
|
557
|
+
if (!isNodeRuntime()) {
|
|
549
558
|
return globalThis.fetch;
|
|
550
559
|
}
|
|
551
560
|
return safeNodeFetchPromise != null ? safeNodeFetchPromise : safeNodeFetchPromise = createSafeNodeFetch();
|
|
552
561
|
}
|
|
553
|
-
function isNodeDefaultFetch(fetch) {
|
|
554
|
-
if (typeof fetch !== "function") {
|
|
555
|
-
return false;
|
|
556
|
-
}
|
|
557
|
-
const source = Function.prototype.toString.call(fetch);
|
|
558
|
-
return source.includes("internal/deps/undici") || source.includes("lazy loading of undici");
|
|
559
|
-
}
|
|
560
562
|
async function createSafeNodeFetch() {
|
|
561
|
-
const [
|
|
563
|
+
const [module2, { lookup }] = await Promise.all([
|
|
562
564
|
loadNodeModule("node:module"),
|
|
563
565
|
loadNodeModule("node:dns")
|
|
564
566
|
]);
|
|
565
|
-
const
|
|
566
|
-
|
|
567
|
-
);
|
|
567
|
+
const nodeRequire = module2.createRequire(getCurrentModulePath());
|
|
568
|
+
const { Agent, fetch } = nodeRequire("undici");
|
|
568
569
|
const dispatcher = new Agent({
|
|
569
570
|
connect: {
|
|
570
571
|
lookup: createSafeLookup(lookup)
|
|
@@ -606,20 +607,47 @@ function getCurrentModulePath() {
|
|
|
606
607
|
|
|
607
608
|
// src/fetch-with-validated-redirects.ts
|
|
608
609
|
var MAX_DOWNLOAD_REDIRECTS = 10;
|
|
610
|
+
async function getValidatedFetch(customFetch) {
|
|
611
|
+
return customFetch == null || customFetch === globalThis.fetch ? await getDefaultDownloadFetch() : customFetch;
|
|
612
|
+
}
|
|
613
|
+
async function fetchWithValidatedEndpoint({
|
|
614
|
+
url,
|
|
615
|
+
init,
|
|
616
|
+
fetch: customFetch,
|
|
617
|
+
trustedOrigin,
|
|
618
|
+
redirect = "error"
|
|
619
|
+
}) {
|
|
620
|
+
const urlText = url.toString();
|
|
621
|
+
const isTrusted = trustedOrigin !== void 0 && isSameOrigin(urlText, trustedOrigin);
|
|
622
|
+
if (!isTrusted) {
|
|
623
|
+
validateDownloadUrl(urlText);
|
|
624
|
+
}
|
|
625
|
+
const fetch = isTrusted && customFetch != null ? customFetch : isTrusted ? globalThis.fetch : await getValidatedFetch(customFetch);
|
|
626
|
+
return await fetch(url, {
|
|
627
|
+
...init,
|
|
628
|
+
redirect
|
|
629
|
+
});
|
|
630
|
+
}
|
|
609
631
|
async function fetchWithValidatedRedirects({
|
|
610
632
|
url,
|
|
611
633
|
headers,
|
|
612
634
|
abortSignal,
|
|
613
|
-
maxRedirects = MAX_DOWNLOAD_REDIRECTS
|
|
635
|
+
maxRedirects = MAX_DOWNLOAD_REDIRECTS,
|
|
636
|
+
fetch: customFetch,
|
|
637
|
+
trustedOrigin
|
|
614
638
|
}) {
|
|
639
|
+
var _a2;
|
|
615
640
|
const baseInit = { signal: abortSignal };
|
|
616
641
|
if (headers !== void 0) {
|
|
617
642
|
baseInit.headers = headers;
|
|
618
643
|
}
|
|
619
644
|
let currentUrl = url;
|
|
620
645
|
for (let redirectCount = 0; redirectCount <= maxRedirects; redirectCount++) {
|
|
621
|
-
|
|
622
|
-
|
|
646
|
+
const isTrustedHop = trustedOrigin !== void 0 && isSameOrigin(currentUrl, trustedOrigin);
|
|
647
|
+
if (!isTrustedHop) {
|
|
648
|
+
validateDownloadUrl(currentUrl);
|
|
649
|
+
}
|
|
650
|
+
const fetch = isTrustedHop && customFetch != null ? customFetch : isTrustedHop ? globalThis.fetch : await getValidatedFetch(customFetch);
|
|
623
651
|
const response = await fetch(currentUrl, {
|
|
624
652
|
...baseInit,
|
|
625
653
|
redirect: "manual"
|
|
@@ -633,7 +661,7 @@ async function fetchWithValidatedRedirects({
|
|
|
633
661
|
}
|
|
634
662
|
return await fetch(currentUrl, { ...baseInit, redirect: "follow" });
|
|
635
663
|
}
|
|
636
|
-
const location = response.headers.get("location");
|
|
664
|
+
const location = (_a2 = response.headers) == null ? void 0 : _a2.get("location");
|
|
637
665
|
if (response.status >= 300 && response.status < 400 && location) {
|
|
638
666
|
await cancelResponseBody(response);
|
|
639
667
|
currentUrl = new URL(location, currentUrl).toString();
|
|
@@ -926,7 +954,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
926
954
|
}
|
|
927
955
|
|
|
928
956
|
// src/version.ts
|
|
929
|
-
var VERSION = true ? "4.0.
|
|
957
|
+
var VERSION = true ? "4.0.57" : "0.0.0-test";
|
|
930
958
|
|
|
931
959
|
// src/get-from-api.ts
|
|
932
960
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -1042,15 +1070,6 @@ function isNonNullable(value) {
|
|
|
1042
1070
|
return value != null;
|
|
1043
1071
|
}
|
|
1044
1072
|
|
|
1045
|
-
// src/is-same-origin.ts
|
|
1046
|
-
function isSameOrigin(url, baseUrl) {
|
|
1047
|
-
try {
|
|
1048
|
-
return new URL(url).origin === new URL(baseUrl).origin;
|
|
1049
|
-
} catch (e) {
|
|
1050
|
-
return false;
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
1073
|
// src/is-url-supported.ts
|
|
1055
1074
|
function isUrlSupported({
|
|
1056
1075
|
mediaType,
|
|
@@ -3173,6 +3192,365 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
|
3173
3192
|
};
|
|
3174
3193
|
};
|
|
3175
3194
|
|
|
3195
|
+
// src/streaming-tool-call-tracker.ts
|
|
3196
|
+
var import_provider13 = require("@ai-sdk/provider");
|
|
3197
|
+
|
|
3198
|
+
// src/streaming-tool-call-argument-state.ts
|
|
3199
|
+
function startsWithStructuredValue(value) {
|
|
3200
|
+
if (typeof value !== "string") {
|
|
3201
|
+
return false;
|
|
3202
|
+
}
|
|
3203
|
+
const firstCharacter = value.trimStart()[0];
|
|
3204
|
+
return firstCharacter === "{" || firstCharacter === "[";
|
|
3205
|
+
}
|
|
3206
|
+
var StreamingToolCallArgumentState = class {
|
|
3207
|
+
constructor(initialValue = "") {
|
|
3208
|
+
this.structure = { kind: "undetermined" };
|
|
3209
|
+
this.append(initialValue);
|
|
3210
|
+
}
|
|
3211
|
+
get hasCompleteStructuredValue() {
|
|
3212
|
+
return this.structure.kind === "structured" && this.structure.complete === true;
|
|
3213
|
+
}
|
|
3214
|
+
append(delta) {
|
|
3215
|
+
let nextStructure = this.structure;
|
|
3216
|
+
for (const character of delta) {
|
|
3217
|
+
if (nextStructure.kind === "undetermined") {
|
|
3218
|
+
if (/\s/.test(character)) {
|
|
3219
|
+
continue;
|
|
3220
|
+
}
|
|
3221
|
+
if (character !== "{" && character !== "[") {
|
|
3222
|
+
nextStructure = { kind: "other" };
|
|
3223
|
+
continue;
|
|
3224
|
+
}
|
|
3225
|
+
nextStructure = {
|
|
3226
|
+
kind: "structured",
|
|
3227
|
+
stack: [character],
|
|
3228
|
+
inString: false,
|
|
3229
|
+
escaped: false,
|
|
3230
|
+
complete: false
|
|
3231
|
+
};
|
|
3232
|
+
continue;
|
|
3233
|
+
}
|
|
3234
|
+
if (nextStructure.kind !== "structured" || nextStructure.complete) {
|
|
3235
|
+
continue;
|
|
3236
|
+
}
|
|
3237
|
+
if (nextStructure.inString) {
|
|
3238
|
+
if (nextStructure.escaped) {
|
|
3239
|
+
nextStructure.escaped = false;
|
|
3240
|
+
} else if (character === "\\") {
|
|
3241
|
+
nextStructure.escaped = true;
|
|
3242
|
+
} else if (character === '"') {
|
|
3243
|
+
nextStructure.inString = false;
|
|
3244
|
+
}
|
|
3245
|
+
continue;
|
|
3246
|
+
}
|
|
3247
|
+
if (character === '"') {
|
|
3248
|
+
nextStructure.inString = true;
|
|
3249
|
+
} else if (character === "{" || character === "[") {
|
|
3250
|
+
nextStructure.stack.push(character);
|
|
3251
|
+
} else if (character === "}" || character === "]") {
|
|
3252
|
+
const expectedOpening = character === "}" ? "{" : "[";
|
|
3253
|
+
if (nextStructure.stack[nextStructure.stack.length - 1] !== expectedOpening) {
|
|
3254
|
+
nextStructure = { kind: "other" };
|
|
3255
|
+
continue;
|
|
3256
|
+
}
|
|
3257
|
+
nextStructure.stack.pop();
|
|
3258
|
+
if (nextStructure.stack.length === 0) {
|
|
3259
|
+
nextStructure.complete = true;
|
|
3260
|
+
}
|
|
3261
|
+
}
|
|
3262
|
+
}
|
|
3263
|
+
this.structure = nextStructure;
|
|
3264
|
+
}
|
|
3265
|
+
};
|
|
3266
|
+
|
|
3267
|
+
// src/streaming-tool-call-tracker.ts
|
|
3268
|
+
var StreamingToolCallTracker = class {
|
|
3269
|
+
constructor(controller, options = {}) {
|
|
3270
|
+
this.toolCalls = [];
|
|
3271
|
+
this.toolCallsById = /* @__PURE__ */ new Map();
|
|
3272
|
+
this.toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
3273
|
+
this.usedToolCallIds = /* @__PURE__ */ new Set();
|
|
3274
|
+
this.nextGeneratedIdSuffixes = /* @__PURE__ */ new Map();
|
|
3275
|
+
var _a2, _b2;
|
|
3276
|
+
this.controller = controller;
|
|
3277
|
+
this._generateId = (_a2 = options.generateId) != null ? _a2 : generateId;
|
|
3278
|
+
this.typeValidation = (_b2 = options.typeValidation) != null ? _b2 : "none";
|
|
3279
|
+
this.extractMetadata = options.extractMetadata;
|
|
3280
|
+
this.buildToolCallProviderMetadata = options.buildToolCallProviderMetadata;
|
|
3281
|
+
}
|
|
3282
|
+
processDelta(toolCallDelta) {
|
|
3283
|
+
var _a2, _b2;
|
|
3284
|
+
const wireName = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.name;
|
|
3285
|
+
const hasBlankName = typeof wireName === "string" && wireName.trim().length === 0;
|
|
3286
|
+
const wireId = this.getNonBlankString(toolCallDelta.id);
|
|
3287
|
+
const name2 = this.getNonBlankString(wireName);
|
|
3288
|
+
const { index } = toolCallDelta;
|
|
3289
|
+
const resolution = this.resolveToolCall({
|
|
3290
|
+
wireId,
|
|
3291
|
+
index,
|
|
3292
|
+
name: name2,
|
|
3293
|
+
hasExplicitCallStart: name2 != null && startsWithStructuredValue((_b2 = toolCallDelta.function) == null ? void 0 : _b2.arguments)
|
|
3294
|
+
});
|
|
3295
|
+
if (resolution.kind === "ambiguous") {
|
|
3296
|
+
return;
|
|
3297
|
+
}
|
|
3298
|
+
let toolCall;
|
|
3299
|
+
if (resolution.kind === "new") {
|
|
3300
|
+
if (hasBlankName) {
|
|
3301
|
+
return;
|
|
3302
|
+
}
|
|
3303
|
+
toolCall = this.processNewToolCall(toolCallDelta, {
|
|
3304
|
+
wireId,
|
|
3305
|
+
index,
|
|
3306
|
+
name: name2
|
|
3307
|
+
});
|
|
3308
|
+
} else {
|
|
3309
|
+
toolCall = resolution.toolCall;
|
|
3310
|
+
if (wireId != null) {
|
|
3311
|
+
this.associateWireId(toolCall, wireId);
|
|
3312
|
+
}
|
|
3313
|
+
this.processExistingToolCall(toolCall, toolCallDelta);
|
|
3314
|
+
}
|
|
3315
|
+
if (index != null) {
|
|
3316
|
+
this.associateIndex(toolCall, index);
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
flush() {
|
|
3320
|
+
const toolCalls = this.toolCalls.every((toolCall) => toolCall.index != null) ? [...this.toolCalls].sort(
|
|
3321
|
+
(a, b) => a.index - b.index || a.sequence - b.sequence
|
|
3322
|
+
) : this.toolCalls;
|
|
3323
|
+
for (const toolCall of toolCalls) {
|
|
3324
|
+
if (!toolCall.hasFinished) {
|
|
3325
|
+
this.finishToolCall(toolCall);
|
|
3326
|
+
}
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
resolveToolCall({
|
|
3330
|
+
wireId,
|
|
3331
|
+
index,
|
|
3332
|
+
name: name2,
|
|
3333
|
+
hasExplicitCallStart
|
|
3334
|
+
}) {
|
|
3335
|
+
const indexedToolCalls = index != null ? this.toolCallsByIndex.get(index) : void 0;
|
|
3336
|
+
const matchingIndexedToolCalls = this.filterToolCallsByName(
|
|
3337
|
+
indexedToolCalls,
|
|
3338
|
+
name2
|
|
3339
|
+
);
|
|
3340
|
+
if (wireId != null) {
|
|
3341
|
+
const toolCallsWithId = this.toolCallsById.get(wireId);
|
|
3342
|
+
if (toolCallsWithId != null) {
|
|
3343
|
+
if (index != null) {
|
|
3344
|
+
const matchingToolCalls = matchingIndexedToolCalls.filter(
|
|
3345
|
+
(toolCall) => toolCallsWithId.has(toolCall)
|
|
3346
|
+
);
|
|
3347
|
+
const matchingToolCall = this.resolveMatchingToolCall(
|
|
3348
|
+
matchingToolCalls,
|
|
3349
|
+
hasExplicitCallStart
|
|
3350
|
+
);
|
|
3351
|
+
if (matchingToolCall.kind !== "new") {
|
|
3352
|
+
return matchingToolCall;
|
|
3353
|
+
}
|
|
3354
|
+
if (name2 != null) {
|
|
3355
|
+
return { kind: "new" };
|
|
3356
|
+
}
|
|
3357
|
+
if (indexedToolCalls != null) {
|
|
3358
|
+
return { kind: "ambiguous" };
|
|
3359
|
+
}
|
|
3360
|
+
return this.resolveMatchingToolCall([...toolCallsWithId], false);
|
|
3361
|
+
}
|
|
3362
|
+
if (name2 != null) {
|
|
3363
|
+
const matchingToolCalls = [...toolCallsWithId].filter(
|
|
3364
|
+
(toolCall) => toolCall.function.name === name2
|
|
3365
|
+
);
|
|
3366
|
+
return this.resolveMatchingToolCall(
|
|
3367
|
+
matchingToolCalls,
|
|
3368
|
+
hasExplicitCallStart
|
|
3369
|
+
);
|
|
3370
|
+
}
|
|
3371
|
+
return this.resolveMatchingToolCall([...toolCallsWithId], false);
|
|
3372
|
+
}
|
|
3373
|
+
if (matchingIndexedToolCalls.length > 0) {
|
|
3374
|
+
return hasExplicitCallStart ? { kind: "new" } : this.resolveMatchingToolCall(matchingIndexedToolCalls, false);
|
|
3375
|
+
}
|
|
3376
|
+
return { kind: "new" };
|
|
3377
|
+
}
|
|
3378
|
+
if (indexedToolCalls != null) {
|
|
3379
|
+
return this.resolveMatchingToolCall(
|
|
3380
|
+
matchingIndexedToolCalls,
|
|
3381
|
+
hasExplicitCallStart
|
|
3382
|
+
);
|
|
3383
|
+
}
|
|
3384
|
+
if (name2 != null) {
|
|
3385
|
+
return { kind: "new" };
|
|
3386
|
+
}
|
|
3387
|
+
const unfinishedToolCalls = this.toolCalls.filter(
|
|
3388
|
+
(toolCall) => !toolCall.hasFinished
|
|
3389
|
+
);
|
|
3390
|
+
if (unfinishedToolCalls.length === 1) {
|
|
3391
|
+
return { kind: "existing", toolCall: unfinishedToolCalls[0] };
|
|
3392
|
+
}
|
|
3393
|
+
return unfinishedToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
|
|
3394
|
+
}
|
|
3395
|
+
filterToolCallsByName(toolCalls, name2) {
|
|
3396
|
+
if (toolCalls == null) {
|
|
3397
|
+
return [];
|
|
3398
|
+
}
|
|
3399
|
+
return [...toolCalls].filter(
|
|
3400
|
+
(toolCall) => name2 == null || toolCall.function.name === name2
|
|
3401
|
+
);
|
|
3402
|
+
}
|
|
3403
|
+
resolveMatchingToolCall(toolCalls, hasExplicitCallStart) {
|
|
3404
|
+
if (toolCalls.length === 0) {
|
|
3405
|
+
return { kind: "new" };
|
|
3406
|
+
}
|
|
3407
|
+
if (!hasExplicitCallStart) {
|
|
3408
|
+
return toolCalls.length === 1 ? { kind: "existing", toolCall: toolCalls[0] } : { kind: "ambiguous" };
|
|
3409
|
+
}
|
|
3410
|
+
const continuableToolCalls = toolCalls.filter(
|
|
3411
|
+
(toolCall) => !toolCall.argumentState.hasCompleteStructuredValue
|
|
3412
|
+
);
|
|
3413
|
+
if (continuableToolCalls.length === 1) {
|
|
3414
|
+
return { kind: "existing", toolCall: continuableToolCalls[0] };
|
|
3415
|
+
}
|
|
3416
|
+
return continuableToolCalls.length > 1 ? { kind: "ambiguous" } : { kind: "new" };
|
|
3417
|
+
}
|
|
3418
|
+
processNewToolCall(toolCallDelta, {
|
|
3419
|
+
wireId,
|
|
3420
|
+
index,
|
|
3421
|
+
name: name2
|
|
3422
|
+
}) {
|
|
3423
|
+
var _a2, _b2, _c, _d, _e;
|
|
3424
|
+
if (this.typeValidation === "required") {
|
|
3425
|
+
if (toolCallDelta.type !== "function") {
|
|
3426
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3427
|
+
data: toolCallDelta,
|
|
3428
|
+
message: `Expected 'function' type.`
|
|
3429
|
+
});
|
|
3430
|
+
}
|
|
3431
|
+
} else if (this.typeValidation === "if-present") {
|
|
3432
|
+
if (toolCallDelta.type != null && toolCallDelta.type !== "function") {
|
|
3433
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3434
|
+
data: toolCallDelta,
|
|
3435
|
+
message: `Expected 'function' type.`
|
|
3436
|
+
});
|
|
3437
|
+
}
|
|
3438
|
+
}
|
|
3439
|
+
if (name2 == null) {
|
|
3440
|
+
throw new import_provider13.InvalidResponseDataError({
|
|
3441
|
+
data: toolCallDelta,
|
|
3442
|
+
message: `Expected 'function.name' to be a string.`
|
|
3443
|
+
});
|
|
3444
|
+
}
|
|
3445
|
+
const id = this.createToolCallId(wireId);
|
|
3446
|
+
this.controller.enqueue({
|
|
3447
|
+
type: "tool-input-start",
|
|
3448
|
+
id,
|
|
3449
|
+
toolName: name2
|
|
3450
|
+
});
|
|
3451
|
+
const toolCall = {
|
|
3452
|
+
id,
|
|
3453
|
+
index: index != null ? index : void 0,
|
|
3454
|
+
sequence: this.toolCalls.length,
|
|
3455
|
+
type: "function",
|
|
3456
|
+
function: {
|
|
3457
|
+
name: name2,
|
|
3458
|
+
arguments: (_b2 = (_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null ? _b2 : ""
|
|
3459
|
+
},
|
|
3460
|
+
argumentState: new StreamingToolCallArgumentState(
|
|
3461
|
+
(_d = (_c = toolCallDelta.function) == null ? void 0 : _c.arguments) != null ? _d : ""
|
|
3462
|
+
),
|
|
3463
|
+
hasFinished: false,
|
|
3464
|
+
metadata: (_e = this.extractMetadata) == null ? void 0 : _e.call(this, toolCallDelta)
|
|
3465
|
+
};
|
|
3466
|
+
this.toolCalls.push(toolCall);
|
|
3467
|
+
if (wireId != null) {
|
|
3468
|
+
this.associateWireId(toolCall, wireId);
|
|
3469
|
+
}
|
|
3470
|
+
if (toolCall.function.arguments.length > 0) {
|
|
3471
|
+
this.controller.enqueue({
|
|
3472
|
+
type: "tool-input-delta",
|
|
3473
|
+
id: toolCall.id,
|
|
3474
|
+
delta: toolCall.function.arguments
|
|
3475
|
+
});
|
|
3476
|
+
}
|
|
3477
|
+
return toolCall;
|
|
3478
|
+
}
|
|
3479
|
+
associateWireId(toolCall, wireId) {
|
|
3480
|
+
let toolCallsWithId = this.toolCallsById.get(wireId);
|
|
3481
|
+
if (toolCallsWithId == null) {
|
|
3482
|
+
toolCallsWithId = /* @__PURE__ */ new Set();
|
|
3483
|
+
this.toolCallsById.set(wireId, toolCallsWithId);
|
|
3484
|
+
}
|
|
3485
|
+
toolCallsWithId.add(toolCall);
|
|
3486
|
+
}
|
|
3487
|
+
associateIndex(toolCall, index) {
|
|
3488
|
+
let toolCallsWithIndex = this.toolCallsByIndex.get(index);
|
|
3489
|
+
if (toolCallsWithIndex == null) {
|
|
3490
|
+
toolCallsWithIndex = /* @__PURE__ */ new Set();
|
|
3491
|
+
this.toolCallsByIndex.set(index, toolCallsWithIndex);
|
|
3492
|
+
}
|
|
3493
|
+
toolCallsWithIndex.add(toolCall);
|
|
3494
|
+
}
|
|
3495
|
+
createToolCallId(wireId) {
|
|
3496
|
+
var _a2, _b2;
|
|
3497
|
+
if (wireId != null && !this.usedToolCallIds.has(wireId)) {
|
|
3498
|
+
this.usedToolCallIds.add(wireId);
|
|
3499
|
+
return wireId;
|
|
3500
|
+
}
|
|
3501
|
+
const generatedId = (_a2 = this.getNonBlankString(this._generateId())) != null ? _a2 : "tool-call";
|
|
3502
|
+
if (!this.usedToolCallIds.has(generatedId)) {
|
|
3503
|
+
this.usedToolCallIds.add(generatedId);
|
|
3504
|
+
return generatedId;
|
|
3505
|
+
}
|
|
3506
|
+
const initialSuffix = (_b2 = this.nextGeneratedIdSuffixes.get(generatedId)) != null ? _b2 : 1;
|
|
3507
|
+
const maximumSuffix = initialSuffix + this.usedToolCallIds.size;
|
|
3508
|
+
for (let suffix = initialSuffix; suffix <= maximumSuffix; suffix++) {
|
|
3509
|
+
const suffixedId = `${generatedId}-${suffix}`;
|
|
3510
|
+
if (!this.usedToolCallIds.has(suffixedId)) {
|
|
3511
|
+
this.usedToolCallIds.add(suffixedId);
|
|
3512
|
+
this.nextGeneratedIdSuffixes.set(generatedId, suffix + 1);
|
|
3513
|
+
return suffixedId;
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3516
|
+
throw new Error("Failed to create a unique tool call ID.");
|
|
3517
|
+
}
|
|
3518
|
+
getNonBlankString(value) {
|
|
3519
|
+
return value != null && value.trim().length > 0 ? value : void 0;
|
|
3520
|
+
}
|
|
3521
|
+
processExistingToolCall(toolCall, toolCallDelta) {
|
|
3522
|
+
var _a2;
|
|
3523
|
+
if (!toolCall.hasFinished && ((_a2 = toolCallDelta.function) == null ? void 0 : _a2.arguments) != null) {
|
|
3524
|
+
toolCall.argumentState.append(toolCallDelta.function.arguments);
|
|
3525
|
+
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
3526
|
+
this.controller.enqueue({
|
|
3527
|
+
type: "tool-input-delta",
|
|
3528
|
+
id: toolCall.id,
|
|
3529
|
+
delta: toolCallDelta.function.arguments
|
|
3530
|
+
});
|
|
3531
|
+
}
|
|
3532
|
+
}
|
|
3533
|
+
finishToolCall(toolCall) {
|
|
3534
|
+
var _a2;
|
|
3535
|
+
this.controller.enqueue({
|
|
3536
|
+
type: "tool-input-end",
|
|
3537
|
+
id: toolCall.id
|
|
3538
|
+
});
|
|
3539
|
+
const providerMetadata = (_a2 = this.buildToolCallProviderMetadata) == null ? void 0 : _a2.call(
|
|
3540
|
+
this,
|
|
3541
|
+
toolCall.metadata
|
|
3542
|
+
);
|
|
3543
|
+
this.controller.enqueue({
|
|
3544
|
+
type: "tool-call",
|
|
3545
|
+
toolCallId: toolCall.id,
|
|
3546
|
+
toolName: toolCall.function.name,
|
|
3547
|
+
input: toolCall.function.arguments,
|
|
3548
|
+
...providerMetadata ? { providerMetadata } : {}
|
|
3549
|
+
});
|
|
3550
|
+
toolCall.hasFinished = true;
|
|
3551
|
+
}
|
|
3552
|
+
};
|
|
3553
|
+
|
|
3176
3554
|
// src/strip-file-extension.ts
|
|
3177
3555
|
function stripFileExtension(filename) {
|
|
3178
3556
|
const firstDotIndex = filename.indexOf(".");
|
|
@@ -3218,6 +3596,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
|
3218
3596
|
EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL,
|
|
3219
3597
|
EXPERIMENTAL_EMBEDDING_MODEL_PROVIDER_OPTIONS_TRANSFORMER,
|
|
3220
3598
|
EventSourceParserStream,
|
|
3599
|
+
StreamingToolCallTracker,
|
|
3221
3600
|
VERSION,
|
|
3222
3601
|
asSchema,
|
|
3223
3602
|
cancelResponseBody,
|
|
@@ -3242,6 +3621,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
|
3242
3621
|
dynamicTool,
|
|
3243
3622
|
executeTool,
|
|
3244
3623
|
extractResponseHeaders,
|
|
3624
|
+
fetchWithValidatedEndpoint,
|
|
3245
3625
|
fetchWithValidatedRedirects,
|
|
3246
3626
|
generateId,
|
|
3247
3627
|
getErrorMessage,
|