ai 7.0.114 → 7.0.116
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 +23 -0
- package/dist/index.js +951 -1250
- package/dist/index.js.map +1 -1
- package/dist/internal/index.js +159 -212
- package/dist/internal/index.js.map +1 -1
- package/docs/03-agents/08-terminal-ui.mdx +12 -8
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +6 -1
- package/docs/03-ai-sdk-harnesses/02-harness-agent.mdx +97 -107
- package/docs/03-ai-sdk-harnesses/03-tools.mdx +1 -22
- package/docs/03-ai-sdk-harnesses/04-skills.mdx +0 -4
- package/docs/03-ai-sdk-harnesses/06-workflow-utilities.mdx +43 -9
- package/docs/03-ai-sdk-harnesses/07-ui.mdx +33 -30
- package/docs/03-ai-sdk-harnesses/08-terminal-ui.mdx +8 -6
- package/docs/06-advanced/11-secure-url-fetching.mdx +39 -0
- package/docs/07-reference/06-ai-sdk-tui/01-run-agent-tui.mdx +12 -8
- package/package.json +12 -12
- package/src/prompt/file-part-data.ts +9 -3
- package/src/util/download/download.ts +2 -2
package/dist/internal/index.js
CHANGED
|
@@ -10,17 +10,16 @@ function asAsyncIterableStream(stream) {
|
|
|
10
10
|
const reader = this.getReader();
|
|
11
11
|
let finished = false;
|
|
12
12
|
async function cleanup(cancelStream) {
|
|
13
|
-
var _a12;
|
|
14
13
|
if (finished) return;
|
|
15
14
|
finished = true;
|
|
16
15
|
try {
|
|
17
16
|
if (cancelStream) {
|
|
18
|
-
await
|
|
17
|
+
await reader.cancel?.();
|
|
19
18
|
}
|
|
20
19
|
} finally {
|
|
21
20
|
try {
|
|
22
21
|
reader.releaseLock();
|
|
23
|
-
} catch
|
|
22
|
+
} catch {
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
}
|
|
@@ -85,13 +84,13 @@ import {
|
|
|
85
84
|
DownloadError,
|
|
86
85
|
readResponseWithSizeLimit,
|
|
87
86
|
DEFAULT_MAX_DOWNLOAD_SIZE,
|
|
88
|
-
|
|
87
|
+
fetchUntrustedUrl,
|
|
89
88
|
withUserAgentSuffix,
|
|
90
89
|
getRuntimeEnvironmentUserAgent
|
|
91
90
|
} from "@ai-sdk/provider-utils";
|
|
92
91
|
|
|
93
92
|
// src/version.ts
|
|
94
|
-
var VERSION = true ? "7.0.
|
|
93
|
+
var VERSION = true ? "7.0.116" : "0.0.0-test";
|
|
95
94
|
|
|
96
95
|
// src/util/download/download.ts
|
|
97
96
|
var download = async ({
|
|
@@ -99,7 +98,6 @@ var download = async ({
|
|
|
99
98
|
maxBytes,
|
|
100
99
|
abortSignal
|
|
101
100
|
}) => {
|
|
102
|
-
var _a12;
|
|
103
101
|
const urlText = url.toString();
|
|
104
102
|
try {
|
|
105
103
|
const headers = withUserAgentSuffix(
|
|
@@ -107,7 +105,7 @@ var download = async ({
|
|
|
107
105
|
`ai-sdk/${VERSION}`,
|
|
108
106
|
getRuntimeEnvironmentUserAgent()
|
|
109
107
|
);
|
|
110
|
-
const response = await
|
|
108
|
+
const response = await fetchUntrustedUrl({
|
|
111
109
|
url: urlText,
|
|
112
110
|
headers,
|
|
113
111
|
abortSignal
|
|
@@ -123,11 +121,11 @@ var download = async ({
|
|
|
123
121
|
const data = await readResponseWithSizeLimit({
|
|
124
122
|
response,
|
|
125
123
|
url: urlText,
|
|
126
|
-
maxBytes: maxBytes
|
|
124
|
+
maxBytes: maxBytes ?? DEFAULT_MAX_DOWNLOAD_SIZE
|
|
127
125
|
});
|
|
128
126
|
return {
|
|
129
127
|
data,
|
|
130
|
-
mediaType:
|
|
128
|
+
mediaType: response.headers.get("content-type") ?? void 0
|
|
131
129
|
};
|
|
132
130
|
} catch (error) {
|
|
133
131
|
if (DownloadError.isInstance(error)) {
|
|
@@ -214,7 +212,7 @@ function splitDataUrl(dataUrl) {
|
|
|
214
212
|
mediaType: header.split(";")[0].split(":")[1],
|
|
215
213
|
base64Content
|
|
216
214
|
};
|
|
217
|
-
} catch
|
|
215
|
+
} catch {
|
|
218
216
|
return {
|
|
219
217
|
mediaType: void 0,
|
|
220
218
|
base64Content: void 0
|
|
@@ -228,7 +226,7 @@ function isTaggedFileData(value) {
|
|
|
228
226
|
const type = value.type;
|
|
229
227
|
return type === "data" || type === "url" || type === "reference" || type === "text";
|
|
230
228
|
}
|
|
231
|
-
function convertUrlToFilePartData(url) {
|
|
229
|
+
function convertUrlToFilePartData(url, originalUrl) {
|
|
232
230
|
if (url.protocol === "data:") {
|
|
233
231
|
const { mediaType, base64Content } = splitDataUrl(url.toString());
|
|
234
232
|
if (mediaType == null || base64Content == null) {
|
|
@@ -239,7 +237,10 @@ function convertUrlToFilePartData(url) {
|
|
|
239
237
|
}
|
|
240
238
|
return { data: { type: "data", data: base64Content }, mediaType };
|
|
241
239
|
}
|
|
242
|
-
return {
|
|
240
|
+
return {
|
|
241
|
+
data: { type: "url", url, ...originalUrl != null ? { originalUrl } : {} },
|
|
242
|
+
mediaType: void 0
|
|
243
|
+
};
|
|
243
244
|
}
|
|
244
245
|
function convertUrlStringToFilePartData(content) {
|
|
245
246
|
const result = convertUrlToFilePartData(new URL(content));
|
|
@@ -281,7 +282,7 @@ function convertToLanguageModelV4FilePart(content) {
|
|
|
281
282
|
}
|
|
282
283
|
return convertInlineDataToFilePartData(content.data);
|
|
283
284
|
case "url":
|
|
284
|
-
return convertUrlToFilePartData(content.url);
|
|
285
|
+
return convertUrlToFilePartData(content.url, content.originalUrl);
|
|
285
286
|
case "reference":
|
|
286
287
|
return {
|
|
287
288
|
data: { type: "reference", reference: content.reference },
|
|
@@ -300,7 +301,7 @@ function convertToLanguageModelV4FilePart(content) {
|
|
|
300
301
|
if (typeof content === "string") {
|
|
301
302
|
try {
|
|
302
303
|
return convertUrlStringToFilePartData(content);
|
|
303
|
-
} catch
|
|
304
|
+
} catch {
|
|
304
305
|
return convertInlineDataToFilePartData(content);
|
|
305
306
|
}
|
|
306
307
|
}
|
|
@@ -445,7 +446,7 @@ async function convertToLanguageModelPrompt({
|
|
|
445
446
|
}) {
|
|
446
447
|
const downloadedAssets = await downloadAssets(
|
|
447
448
|
prompt.messages,
|
|
448
|
-
download2
|
|
449
|
+
download2 ?? createDefaultDownloadFunction(void 0, abortSignal),
|
|
449
450
|
supportedUrls
|
|
450
451
|
);
|
|
451
452
|
const approvalIdToToolCallId = /* @__PURE__ */ new Map();
|
|
@@ -491,7 +492,7 @@ async function convertToLanguageModelPrompt({
|
|
|
491
492
|
continue;
|
|
492
493
|
}
|
|
493
494
|
const lastCombinedMessage = combinedMessages.at(-1);
|
|
494
|
-
if (
|
|
495
|
+
if (lastCombinedMessage?.role === "tool") {
|
|
495
496
|
const lastContentPart = lastCombinedMessage.content.at(-1);
|
|
496
497
|
if (lastContentPart != null && lastCombinedMessage.providerOptions != null) {
|
|
497
498
|
lastContentPart.providerOptions = mergeObjects(
|
|
@@ -628,7 +629,7 @@ function convertToLanguageModelMessage({
|
|
|
628
629
|
type: "file",
|
|
629
630
|
data,
|
|
630
631
|
filename: part.filename,
|
|
631
|
-
mediaType: mediaType
|
|
632
|
+
mediaType: mediaType ?? part.mediaType,
|
|
632
633
|
providerOptions
|
|
633
634
|
};
|
|
634
635
|
}
|
|
@@ -651,7 +652,7 @@ function convertToLanguageModelMessage({
|
|
|
651
652
|
return {
|
|
652
653
|
type: "reasoning-file",
|
|
653
654
|
data,
|
|
654
|
-
mediaType: mediaType
|
|
655
|
+
mediaType: mediaType ?? part.mediaType,
|
|
655
656
|
providerOptions
|
|
656
657
|
};
|
|
657
658
|
}
|
|
@@ -741,14 +742,13 @@ function convertToLanguageModelMessage({
|
|
|
741
742
|
}
|
|
742
743
|
}
|
|
743
744
|
function convertImagePartToFilePart(part) {
|
|
744
|
-
var _a12;
|
|
745
745
|
if (part.type !== "image") {
|
|
746
746
|
return part;
|
|
747
747
|
}
|
|
748
748
|
return {
|
|
749
749
|
type: "file",
|
|
750
750
|
data: part.image,
|
|
751
|
-
mediaType:
|
|
751
|
+
mediaType: part.mediaType ?? "image",
|
|
752
752
|
providerOptions: part.providerOptions
|
|
753
753
|
};
|
|
754
754
|
}
|
|
@@ -825,7 +825,7 @@ function convertPartToLanguageModelPart(part, downloadedAssets) {
|
|
|
825
825
|
};
|
|
826
826
|
}
|
|
827
827
|
const { data: normalizedData, mediaType: dataUrlMediaType } = convertToLanguageModelV4FilePart(part.data);
|
|
828
|
-
let mediaType = dataUrlMediaType
|
|
828
|
+
let mediaType = dataUrlMediaType ?? part.mediaType;
|
|
829
829
|
let data = normalizedData;
|
|
830
830
|
if (data.type === "url") {
|
|
831
831
|
const downloadedFile = downloadedAssets[data.url.toString()];
|
|
@@ -870,7 +870,6 @@ function mapToolResultOutput({
|
|
|
870
870
|
return {
|
|
871
871
|
type: "content",
|
|
872
872
|
value: output.value.map((item) => {
|
|
873
|
-
var _a12;
|
|
874
873
|
switch (item.type) {
|
|
875
874
|
case "file": {
|
|
876
875
|
const convertedPart = convertPartToLanguageModelPart(
|
|
@@ -899,7 +898,7 @@ function mapToolResultOutput({
|
|
|
899
898
|
};
|
|
900
899
|
}
|
|
901
900
|
case "file-url": {
|
|
902
|
-
const mediaType =
|
|
901
|
+
const mediaType = item.mediaType ?? getMediaTypeFromUrl(item.url);
|
|
903
902
|
const url = new URL(item.url);
|
|
904
903
|
let message = `The "file-url" type for tool result content is deprecated. Use the "file" type with mediaType and { type: 'url', url } instead.`;
|
|
905
904
|
if (!item.mediaType) {
|
|
@@ -1065,14 +1064,13 @@ var URL_EXTENSION_TO_MEDIA_TYPE = {
|
|
|
1065
1064
|
ogg: "audio/ogg"
|
|
1066
1065
|
};
|
|
1067
1066
|
function getMediaTypeFromUrl(url, fallbackMediaType = "application/octet-stream") {
|
|
1068
|
-
var _a12;
|
|
1069
1067
|
try {
|
|
1070
1068
|
const pathname = new URL(url).pathname;
|
|
1071
|
-
const fileExtension =
|
|
1069
|
+
const fileExtension = pathname.split(".").pop()?.toLowerCase();
|
|
1072
1070
|
if (fileExtension && Object.hasOwn(URL_EXTENSION_TO_MEDIA_TYPE, fileExtension)) {
|
|
1073
1071
|
return URL_EXTENSION_TO_MEDIA_TYPE[fileExtension];
|
|
1074
1072
|
}
|
|
1075
|
-
} catch
|
|
1073
|
+
} catch {
|
|
1076
1074
|
}
|
|
1077
1075
|
return fallbackMediaType;
|
|
1078
1076
|
}
|
|
@@ -1091,7 +1089,7 @@ async function createToolModelOutput({
|
|
|
1091
1089
|
} else if (errorMode === "json") {
|
|
1092
1090
|
return { type: "error-json", value: toJSONValue(output) };
|
|
1093
1091
|
}
|
|
1094
|
-
if (tool2
|
|
1092
|
+
if (tool2?.toModelOutput) {
|
|
1095
1093
|
return await tool2.toModelOutput({ toolCallId, input, output });
|
|
1096
1094
|
}
|
|
1097
1095
|
return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
|
|
@@ -1804,7 +1802,7 @@ function prepareRetries({
|
|
|
1804
1802
|
});
|
|
1805
1803
|
}
|
|
1806
1804
|
}
|
|
1807
|
-
const maxRetriesResult = maxRetries
|
|
1805
|
+
const maxRetriesResult = maxRetries ?? defaultMaxRetries;
|
|
1808
1806
|
return {
|
|
1809
1807
|
maxRetries: maxRetriesResult,
|
|
1810
1808
|
retry: retryWithExponentialBackoffRespectingRetryHeaders({
|
|
@@ -1854,39 +1852,38 @@ function createNullLanguageModelUsage() {
|
|
|
1854
1852
|
};
|
|
1855
1853
|
}
|
|
1856
1854
|
function addLanguageModelUsage(usage1, usage2) {
|
|
1857
|
-
var _a12, _b12, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
1858
1855
|
return {
|
|
1859
1856
|
inputTokens: addTokenCounts(usage1.inputTokens, usage2.inputTokens),
|
|
1860
1857
|
inputTokenDetails: {
|
|
1861
1858
|
noCacheTokens: addTokenCounts(
|
|
1862
|
-
|
|
1863
|
-
|
|
1859
|
+
usage1.inputTokenDetails?.noCacheTokens,
|
|
1860
|
+
usage2.inputTokenDetails?.noCacheTokens
|
|
1864
1861
|
),
|
|
1865
1862
|
cacheReadTokens: addTokenCounts(
|
|
1866
|
-
|
|
1867
|
-
|
|
1863
|
+
usage1.inputTokenDetails?.cacheReadTokens,
|
|
1864
|
+
usage2.inputTokenDetails?.cacheReadTokens
|
|
1868
1865
|
),
|
|
1869
1866
|
cacheWriteTokens: addTokenCounts(
|
|
1870
|
-
|
|
1871
|
-
|
|
1867
|
+
usage1.inputTokenDetails?.cacheWriteTokens,
|
|
1868
|
+
usage2.inputTokenDetails?.cacheWriteTokens
|
|
1872
1869
|
)
|
|
1873
1870
|
},
|
|
1874
1871
|
outputTokens: addTokenCounts(usage1.outputTokens, usage2.outputTokens),
|
|
1875
1872
|
outputTokenDetails: {
|
|
1876
1873
|
textTokens: addTokenCounts(
|
|
1877
|
-
|
|
1878
|
-
|
|
1874
|
+
usage1.outputTokenDetails?.textTokens,
|
|
1875
|
+
usage2.outputTokenDetails?.textTokens
|
|
1879
1876
|
),
|
|
1880
1877
|
reasoningTokens: addTokenCounts(
|
|
1881
|
-
|
|
1882
|
-
|
|
1878
|
+
usage1.outputTokenDetails?.reasoningTokens,
|
|
1879
|
+
usage2.outputTokenDetails?.reasoningTokens
|
|
1883
1880
|
)
|
|
1884
1881
|
},
|
|
1885
1882
|
totalTokens: addTokenCounts(usage1.totalTokens, usage2.totalTokens)
|
|
1886
1883
|
};
|
|
1887
1884
|
}
|
|
1888
1885
|
function addTokenCounts(tokenCount1, tokenCount2) {
|
|
1889
|
-
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1
|
|
1886
|
+
return tokenCount1 == null && tokenCount2 == null ? void 0 : (tokenCount1 ?? 0) + (tokenCount2 ?? 0);
|
|
1890
1887
|
}
|
|
1891
1888
|
|
|
1892
1889
|
// src/model/resolve-model.ts
|
|
@@ -2488,8 +2485,7 @@ function resolveLanguageModel(model) {
|
|
|
2488
2485
|
return asLanguageModelV4(model);
|
|
2489
2486
|
}
|
|
2490
2487
|
function getGlobalProvider() {
|
|
2491
|
-
|
|
2492
|
-
const provider = (_a12 = globalThis.AI_SDK_DEFAULT_PROVIDER) != null ? _a12 : gateway;
|
|
2488
|
+
const provider = globalThis.AI_SDK_DEFAULT_PROVIDER ?? gateway;
|
|
2493
2489
|
return asProviderV4(provider);
|
|
2494
2490
|
}
|
|
2495
2491
|
|
|
@@ -2507,7 +2503,7 @@ function mergeCallbacks(...callbacks) {
|
|
|
2507
2503
|
return async (event) => {
|
|
2508
2504
|
await Promise.allSettled(
|
|
2509
2505
|
callbacks.map(async (callback) => {
|
|
2510
|
-
await
|
|
2506
|
+
await callback?.(event);
|
|
2511
2507
|
})
|
|
2512
2508
|
);
|
|
2513
2509
|
};
|
|
@@ -2526,7 +2522,7 @@ function filterIncludedContext({
|
|
|
2526
2522
|
}
|
|
2527
2523
|
return Object.fromEntries(
|
|
2528
2524
|
Object.entries(context).filter(
|
|
2529
|
-
([key]) =>
|
|
2525
|
+
([key]) => includeContext?.[key] === true
|
|
2530
2526
|
)
|
|
2531
2527
|
);
|
|
2532
2528
|
}
|
|
@@ -2553,7 +2549,7 @@ function filterToolContext({
|
|
|
2553
2549
|
toolContext,
|
|
2554
2550
|
includeToolsContext
|
|
2555
2551
|
}) {
|
|
2556
|
-
const includeToolContext = includeToolsContext
|
|
2552
|
+
const includeToolContext = includeToolsContext?.[toolName];
|
|
2557
2553
|
return filterIncludedContext({
|
|
2558
2554
|
context: toolContext,
|
|
2559
2555
|
includeContext: includeToolContext
|
|
@@ -2565,8 +2561,7 @@ var AI_SDK_TELEMETRY_TRACING_CHANNEL = "ai:telemetry";
|
|
|
2565
2561
|
|
|
2566
2562
|
// src/util/is-node-runtime.ts
|
|
2567
2563
|
function isNodeRuntime() {
|
|
2568
|
-
|
|
2569
|
-
return typeof process !== "undefined" && ((_a12 = process.release) == null ? void 0 : _a12.name) === "node";
|
|
2564
|
+
return typeof process !== "undefined" && process.release?.name === "node";
|
|
2570
2565
|
}
|
|
2571
2566
|
|
|
2572
2567
|
// src/telemetry/tracing-channel-publisher.ts
|
|
@@ -2583,19 +2578,16 @@ async function loadDiagnosticsChannel() {
|
|
|
2583
2578
|
return diagnosticsChannelPromise;
|
|
2584
2579
|
}
|
|
2585
2580
|
function loadBuiltinModule(id) {
|
|
2586
|
-
var _a12;
|
|
2587
2581
|
const processWithBuiltins = globalThis.process;
|
|
2588
2582
|
try {
|
|
2589
|
-
return
|
|
2590
|
-
} catch
|
|
2583
|
+
return processWithBuiltins?.getBuiltinModule?.(id);
|
|
2584
|
+
} catch {
|
|
2591
2585
|
return void 0;
|
|
2592
2586
|
}
|
|
2593
2587
|
}
|
|
2594
2588
|
async function runWithTracingChannelSpan(message, execute) {
|
|
2595
|
-
var _a12;
|
|
2596
2589
|
const diagnosticsChannel = await loadDiagnosticsChannel();
|
|
2597
|
-
const tracingChannel =
|
|
2598
|
-
diagnosticsChannel,
|
|
2590
|
+
const tracingChannel = diagnosticsChannel?.tracingChannel?.(
|
|
2599
2591
|
AI_SDK_TELEMETRY_TRACING_CHANNEL
|
|
2600
2592
|
);
|
|
2601
2593
|
if (tracingChannel == null || tracingChannel.hasSubscribers === false) {
|
|
@@ -2628,7 +2620,7 @@ async function runWithTracingChannelSpan(message, execute) {
|
|
|
2628
2620
|
};
|
|
2629
2621
|
try {
|
|
2630
2622
|
return await tracingChannel.tracePromise(tracedExecute, message);
|
|
2631
|
-
} catch
|
|
2623
|
+
} catch {
|
|
2632
2624
|
if (hasExecutionError) {
|
|
2633
2625
|
throw executionError;
|
|
2634
2626
|
}
|
|
@@ -2645,7 +2637,6 @@ function openTelemetryChannelSpanContext({
|
|
|
2645
2637
|
message,
|
|
2646
2638
|
completion
|
|
2647
2639
|
}) {
|
|
2648
|
-
var _a12;
|
|
2649
2640
|
if (!isNodeRuntime()) {
|
|
2650
2641
|
return void 0;
|
|
2651
2642
|
}
|
|
@@ -2653,8 +2644,7 @@ function openTelemetryChannelSpanContext({
|
|
|
2653
2644
|
"node:diagnostics_channel"
|
|
2654
2645
|
);
|
|
2655
2646
|
const asyncHooks = loadBuiltinModule("node:async_hooks");
|
|
2656
|
-
const tracingChannel =
|
|
2657
|
-
diagnosticsChannel,
|
|
2647
|
+
const tracingChannel = diagnosticsChannel?.tracingChannel?.(
|
|
2658
2648
|
AI_SDK_TELEMETRY_TRACING_CHANNEL
|
|
2659
2649
|
);
|
|
2660
2650
|
if (tracingChannel == null || tracingChannel.hasSubscribers === false || asyncHooks == null) {
|
|
@@ -2668,7 +2658,7 @@ function openTelemetryChannelSpanContext({
|
|
|
2668
2658
|
const safePublish = (publish) => {
|
|
2669
2659
|
try {
|
|
2670
2660
|
publish();
|
|
2671
|
-
} catch
|
|
2661
|
+
} catch {
|
|
2672
2662
|
}
|
|
2673
2663
|
};
|
|
2674
2664
|
const publishAsyncEnd = ({
|
|
@@ -2705,8 +2695,7 @@ function openTelemetryChannelSpanContext({
|
|
|
2705
2695
|
|
|
2706
2696
|
// src/telemetry/telemetry-registry.ts
|
|
2707
2697
|
function getGlobalTelemetryIntegrations() {
|
|
2708
|
-
|
|
2709
|
-
return (_a12 = globalThis.AI_SDK_TELEMETRY_INTEGRATIONS) != null ? _a12 : [];
|
|
2698
|
+
return globalThis.AI_SDK_TELEMETRY_INTEGRATIONS ?? [];
|
|
2710
2699
|
}
|
|
2711
2700
|
|
|
2712
2701
|
// src/telemetry/create-telemetry-dispatcher.ts
|
|
@@ -2745,23 +2734,20 @@ function augmentEvent(event, telemetry, filterContext = false) {
|
|
|
2745
2734
|
function createTelemetryDispatcher({
|
|
2746
2735
|
telemetry
|
|
2747
2736
|
}) {
|
|
2748
|
-
if (
|
|
2737
|
+
if (telemetry?.isEnabled === false) {
|
|
2749
2738
|
return {};
|
|
2750
2739
|
}
|
|
2751
|
-
const localIntegrations = telemetry
|
|
2740
|
+
const localIntegrations = telemetry?.integrations;
|
|
2752
2741
|
const integrations = localIntegrations != null ? asArray3(localIntegrations) : getGlobalTelemetryIntegrations();
|
|
2753
2742
|
const telemetryMetadata = {
|
|
2754
|
-
recordInputs: telemetry
|
|
2755
|
-
recordOutputs: telemetry
|
|
2756
|
-
functionId: telemetry
|
|
2757
|
-
includeRuntimeContext: telemetry
|
|
2758
|
-
includeToolsContext: telemetry
|
|
2743
|
+
recordInputs: telemetry?.recordInputs,
|
|
2744
|
+
recordOutputs: telemetry?.recordOutputs,
|
|
2745
|
+
functionId: telemetry?.functionId,
|
|
2746
|
+
includeRuntimeContext: telemetry?.includeRuntimeContext,
|
|
2747
|
+
includeToolsContext: telemetry?.includeToolsContext
|
|
2759
2748
|
};
|
|
2760
2749
|
const mergeTelemetryCallback = (key) => {
|
|
2761
|
-
const integrationCallbacks = integrations.map((integration) =>
|
|
2762
|
-
var _a12;
|
|
2763
|
-
return (_a12 = integration[key]) == null ? void 0 : _a12.bind(integration);
|
|
2764
|
-
}).filter(Boolean).map(
|
|
2750
|
+
const integrationCallbacks = integrations.map((integration) => integration[key]?.bind(integration)).filter(Boolean).map(
|
|
2765
2751
|
(callback) => ((event) => callback(augmentEvent(event, telemetryMetadata)))
|
|
2766
2752
|
);
|
|
2767
2753
|
const mergedIntegrationCallback = mergeCallbacks(...integrationCallbacks);
|
|
@@ -2769,14 +2755,8 @@ function createTelemetryDispatcher({
|
|
|
2769
2755
|
await mergedIntegrationCallback(event);
|
|
2770
2756
|
};
|
|
2771
2757
|
};
|
|
2772
|
-
const executeLanguageModelCallWrappers = integrations.map((integration) =>
|
|
2773
|
-
|
|
2774
|
-
return (_a12 = integration.executeLanguageModelCall) == null ? void 0 : _a12.bind(integration);
|
|
2775
|
-
}).filter(Boolean);
|
|
2776
|
-
const executeToolWrappers = integrations.map((integration) => {
|
|
2777
|
-
var _a12;
|
|
2778
|
-
return (_a12 = integration.executeTool) == null ? void 0 : _a12.bind(integration);
|
|
2779
|
-
}).filter(Boolean);
|
|
2758
|
+
const executeLanguageModelCallWrappers = integrations.map((integration) => integration.executeLanguageModelCall?.bind(integration)).filter(Boolean);
|
|
2759
|
+
const executeToolWrappers = integrations.map((integration) => integration.executeTool?.bind(integration)).filter(Boolean);
|
|
2780
2760
|
return {
|
|
2781
2761
|
runInTracingChannelSpan: async ({ type, event, execute }) => await runWithTracingChannelSpan(
|
|
2782
2762
|
{
|
|
@@ -3007,127 +2987,100 @@ function createRestrictedTelemetryDispatcher({
|
|
|
3007
2987
|
const telemetryDispatcher = createTelemetryDispatcher({ telemetry });
|
|
3008
2988
|
return {
|
|
3009
2989
|
...telemetryDispatcher,
|
|
3010
|
-
onStart: (event) => {
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
2990
|
+
onStart: (event) => telemetryDispatcher.onStart?.({
|
|
2991
|
+
...event,
|
|
2992
|
+
runtimeContext: filterIncludedContext({
|
|
2993
|
+
context: event.runtimeContext,
|
|
2994
|
+
includeContext: includeRuntimeContext
|
|
2995
|
+
}),
|
|
2996
|
+
toolsContext: filterToolsContext({
|
|
2997
|
+
toolsContext: event.toolsContext,
|
|
2998
|
+
includeToolsContext
|
|
2999
|
+
})
|
|
3000
|
+
}),
|
|
3001
|
+
onStepStart: (event) => telemetryDispatcher.onStepStart?.({
|
|
3002
|
+
...event,
|
|
3003
|
+
runtimeContext: filterIncludedContext({
|
|
3004
|
+
context: event.runtimeContext,
|
|
3005
|
+
includeContext: includeRuntimeContext
|
|
3006
|
+
}),
|
|
3007
|
+
steps: event.steps.map(
|
|
3008
|
+
(step) => restrictStepResult({
|
|
3009
|
+
step,
|
|
3010
|
+
includeRuntimeContext,
|
|
3020
3011
|
includeToolsContext
|
|
3021
3012
|
})
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3013
|
+
),
|
|
3014
|
+
toolsContext: filterToolsContext({
|
|
3015
|
+
toolsContext: event.toolsContext,
|
|
3016
|
+
includeToolsContext
|
|
3017
|
+
})
|
|
3018
|
+
}),
|
|
3019
|
+
onStepEnd: (event) => telemetryDispatcher.onStepEnd?.(
|
|
3020
|
+
restrictStepResult({
|
|
3021
|
+
step: event,
|
|
3022
|
+
includeRuntimeContext,
|
|
3023
|
+
includeToolsContext
|
|
3024
|
+
})
|
|
3025
|
+
),
|
|
3026
|
+
onStepFinish: (event) => telemetryDispatcher.onStepEnd?.(
|
|
3027
|
+
restrictStepResult({
|
|
3028
|
+
step: event,
|
|
3029
|
+
includeRuntimeContext,
|
|
3030
|
+
includeToolsContext
|
|
3031
|
+
})
|
|
3032
|
+
),
|
|
3033
|
+
onEnd: (event) => telemetryDispatcher.onEnd?.(
|
|
3034
|
+
((restrictedSteps) => {
|
|
3035
|
+
return {
|
|
3036
|
+
...event,
|
|
3037
|
+
runtimeContext: filterIncludedContext({
|
|
3038
|
+
context: event.runtimeContext,
|
|
3039
|
+
includeContext: includeRuntimeContext
|
|
3040
|
+
}),
|
|
3041
|
+
steps: restrictedSteps,
|
|
3042
|
+
finalStep: restrictedSteps.at(-1),
|
|
3043
|
+
toolsContext: filterToolsContext({
|
|
3044
|
+
toolsContext: event.toolsContext,
|
|
3036
3045
|
includeToolsContext
|
|
3037
3046
|
})
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
includeToolsContext
|
|
3042
|
-
})
|
|
3043
|
-
});
|
|
3044
|
-
},
|
|
3045
|
-
onStepEnd: (event) => {
|
|
3046
|
-
var _a12;
|
|
3047
|
-
return (_a12 = telemetryDispatcher.onStepEnd) == null ? void 0 : _a12.call(
|
|
3048
|
-
telemetryDispatcher,
|
|
3049
|
-
restrictStepResult({
|
|
3050
|
-
step: event,
|
|
3051
|
-
includeRuntimeContext,
|
|
3052
|
-
includeToolsContext
|
|
3053
|
-
})
|
|
3054
|
-
);
|
|
3055
|
-
},
|
|
3056
|
-
onStepFinish: (event) => {
|
|
3057
|
-
var _a12;
|
|
3058
|
-
return (_a12 = telemetryDispatcher.onStepEnd) == null ? void 0 : _a12.call(
|
|
3059
|
-
telemetryDispatcher,
|
|
3060
|
-
restrictStepResult({
|
|
3061
|
-
step: event,
|
|
3062
|
-
includeRuntimeContext,
|
|
3063
|
-
includeToolsContext
|
|
3064
|
-
})
|
|
3065
|
-
);
|
|
3066
|
-
},
|
|
3067
|
-
onEnd: (event) => {
|
|
3068
|
-
var _a12;
|
|
3069
|
-
return (_a12 = telemetryDispatcher.onEnd) == null ? void 0 : _a12.call(
|
|
3070
|
-
telemetryDispatcher,
|
|
3071
|
-
((restrictedSteps) => {
|
|
3072
|
-
return {
|
|
3073
|
-
...event,
|
|
3074
|
-
runtimeContext: filterIncludedContext({
|
|
3075
|
-
context: event.runtimeContext,
|
|
3076
|
-
includeContext: includeRuntimeContext
|
|
3077
|
-
}),
|
|
3078
|
-
steps: restrictedSteps,
|
|
3079
|
-
finalStep: restrictedSteps.at(-1),
|
|
3080
|
-
toolsContext: filterToolsContext({
|
|
3081
|
-
toolsContext: event.toolsContext,
|
|
3082
|
-
includeToolsContext
|
|
3083
|
-
})
|
|
3084
|
-
};
|
|
3085
|
-
})(
|
|
3086
|
-
event.steps.map(
|
|
3087
|
-
(step) => restrictStepResult({
|
|
3088
|
-
step,
|
|
3089
|
-
includeRuntimeContext,
|
|
3090
|
-
includeToolsContext
|
|
3091
|
-
})
|
|
3092
|
-
)
|
|
3093
|
-
)
|
|
3094
|
-
);
|
|
3095
|
-
},
|
|
3096
|
-
onAbort: (event) => {
|
|
3097
|
-
var _a12;
|
|
3098
|
-
return (_a12 = telemetryDispatcher.onAbort) == null ? void 0 : _a12.call(telemetryDispatcher, {
|
|
3099
|
-
...event,
|
|
3100
|
-
steps: event.steps.map(
|
|
3047
|
+
};
|
|
3048
|
+
})(
|
|
3049
|
+
event.steps.map(
|
|
3101
3050
|
(step) => restrictStepResult({
|
|
3102
3051
|
step,
|
|
3103
3052
|
includeRuntimeContext,
|
|
3104
3053
|
includeToolsContext
|
|
3105
3054
|
})
|
|
3106
3055
|
)
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3114
|
-
|
|
3115
|
-
toolContext: event.toolContext,
|
|
3116
|
-
includeToolsContext
|
|
3117
|
-
})
|
|
3118
|
-
});
|
|
3119
|
-
},
|
|
3120
|
-
onToolExecutionEnd: (event) => {
|
|
3121
|
-
var _a12;
|
|
3122
|
-
return (_a12 = telemetryDispatcher.onToolExecutionEnd) == null ? void 0 : _a12.call(telemetryDispatcher, {
|
|
3123
|
-
...event,
|
|
3124
|
-
toolContext: filterToolContext({
|
|
3125
|
-
toolName: event.toolCall.toolName,
|
|
3126
|
-
toolContext: event.toolContext,
|
|
3056
|
+
)
|
|
3057
|
+
),
|
|
3058
|
+
onAbort: (event) => telemetryDispatcher.onAbort?.({
|
|
3059
|
+
...event,
|
|
3060
|
+
steps: event.steps.map(
|
|
3061
|
+
(step) => restrictStepResult({
|
|
3062
|
+
step,
|
|
3063
|
+
includeRuntimeContext,
|
|
3127
3064
|
includeToolsContext
|
|
3128
3065
|
})
|
|
3129
|
-
|
|
3130
|
-
}
|
|
3066
|
+
)
|
|
3067
|
+
}),
|
|
3068
|
+
onToolExecutionStart: (event) => telemetryDispatcher.onToolExecutionStart?.({
|
|
3069
|
+
...event,
|
|
3070
|
+
toolContext: filterToolContext({
|
|
3071
|
+
toolName: event.toolCall.toolName,
|
|
3072
|
+
toolContext: event.toolContext,
|
|
3073
|
+
includeToolsContext
|
|
3074
|
+
})
|
|
3075
|
+
}),
|
|
3076
|
+
onToolExecutionEnd: (event) => telemetryDispatcher.onToolExecutionEnd?.({
|
|
3077
|
+
...event,
|
|
3078
|
+
toolContext: filterToolContext({
|
|
3079
|
+
toolName: event.toolCall.toolName,
|
|
3080
|
+
toolContext: event.toolContext,
|
|
3081
|
+
includeToolsContext
|
|
3082
|
+
})
|
|
3083
|
+
})
|
|
3131
3084
|
};
|
|
3132
3085
|
}
|
|
3133
3086
|
|
|
@@ -3188,14 +3141,13 @@ async function parseToolCall({
|
|
|
3188
3141
|
}
|
|
3189
3142
|
let repairedToolCall = null;
|
|
3190
3143
|
try {
|
|
3191
|
-
abortSignal
|
|
3144
|
+
abortSignal?.throwIfAborted();
|
|
3192
3145
|
repairedToolCall = await waitForPromiseWithAbortSignal({
|
|
3193
3146
|
promise: repairToolCall({
|
|
3194
3147
|
toolCall,
|
|
3195
3148
|
tools,
|
|
3196
3149
|
inputSchema: async ({ toolName }) => {
|
|
3197
|
-
|
|
3198
|
-
const inputSchema = (_a12 = getOwn(tools, toolName)) == null ? void 0 : _a12.inputSchema;
|
|
3150
|
+
const inputSchema = getOwn(tools, toolName)?.inputSchema;
|
|
3199
3151
|
return await asSchema2(inputSchema).jsonSchema;
|
|
3200
3152
|
},
|
|
3201
3153
|
instructions,
|
|
@@ -3207,7 +3159,7 @@ async function parseToolCall({
|
|
|
3207
3159
|
abortSignal
|
|
3208
3160
|
});
|
|
3209
3161
|
} catch (repairError) {
|
|
3210
|
-
abortSignal
|
|
3162
|
+
abortSignal?.throwIfAborted();
|
|
3211
3163
|
throw new ToolCallRepairError({
|
|
3212
3164
|
cause: repairError,
|
|
3213
3165
|
originalError: error
|
|
@@ -3220,11 +3172,11 @@ async function parseToolCall({
|
|
|
3220
3172
|
toolCall: await doParseToolCall({ toolCall: repairedToolCall, tools }),
|
|
3221
3173
|
refineToolInput
|
|
3222
3174
|
});
|
|
3223
|
-
abortSignal
|
|
3175
|
+
abortSignal?.throwIfAborted();
|
|
3224
3176
|
return parsedRepairedToolCall;
|
|
3225
3177
|
}
|
|
3226
3178
|
} catch (error) {
|
|
3227
|
-
abortSignal
|
|
3179
|
+
abortSignal?.throwIfAborted();
|
|
3228
3180
|
const parsedInput = await safeParseJSON({ text: toolCall.input });
|
|
3229
3181
|
const input = parsedInput.success ? parsedInput.value : toolCall.input;
|
|
3230
3182
|
const tool2 = getOwn(tools, toolCall.toolName);
|
|
@@ -3236,10 +3188,10 @@ async function parseToolCall({
|
|
|
3236
3188
|
dynamic: true,
|
|
3237
3189
|
invalid: true,
|
|
3238
3190
|
error,
|
|
3239
|
-
title: tool2
|
|
3191
|
+
title: tool2?.title,
|
|
3240
3192
|
providerExecuted: toolCall.providerExecuted,
|
|
3241
3193
|
providerMetadata: toolCall.providerMetadata,
|
|
3242
|
-
...
|
|
3194
|
+
...tool2?.metadata != null ? { toolMetadata: tool2.metadata } : {}
|
|
3243
3195
|
};
|
|
3244
3196
|
}
|
|
3245
3197
|
}
|
|
@@ -3397,25 +3349,22 @@ function createToolSearchState({
|
|
|
3397
3349
|
tools,
|
|
3398
3350
|
toolCallers
|
|
3399
3351
|
}) {
|
|
3400
|
-
const searchTools = Object.entries(tools
|
|
3352
|
+
const searchTools = Object.entries(tools ?? {}).filter(
|
|
3401
3353
|
([, tool2]) => tool2.deferLoading || isToolSearch(tool2)
|
|
3402
3354
|
);
|
|
3403
3355
|
if (searchTools.length === 0) {
|
|
3404
3356
|
return (activeTools) => activeTools;
|
|
3405
3357
|
}
|
|
3406
3358
|
const discovered = /* @__PURE__ */ new Set();
|
|
3407
|
-
const getCallers = (name12) =>
|
|
3408
|
-
var _a12;
|
|
3409
|
-
return (_a12 = getOwn(toolCallers, name12)) != null ? _a12 : [DIRECT_TOOL_CALL];
|
|
3410
|
-
};
|
|
3359
|
+
const getCallers = (name12) => getOwn(toolCallers, name12) ?? [DIRECT_TOOL_CALL];
|
|
3411
3360
|
for (const [name12, tool2] of searchTools) {
|
|
3412
3361
|
const callers = getCallers(name12);
|
|
3413
3362
|
if (callers.some((name13) => {
|
|
3414
3363
|
if (name13 === DIRECT_TOOL_CALL) {
|
|
3415
3364
|
return false;
|
|
3416
3365
|
}
|
|
3417
|
-
const caller = experimental_getToolCaller2(tools
|
|
3418
|
-
return
|
|
3366
|
+
const caller = experimental_getToolCaller2(tools?.[name13]);
|
|
3367
|
+
return caller?.type !== "local" || caller.prepareModelMessage == null;
|
|
3419
3368
|
}) || isToolSearch(tool2) && tool2.deferLoading) {
|
|
3420
3369
|
throw new InvalidArgumentError({
|
|
3421
3370
|
parameter: "tools",
|
|
@@ -3454,7 +3403,7 @@ function createToolSearchState({
|
|
|
3454
3403
|
experimental_sandbox
|
|
3455
3404
|
});
|
|
3456
3405
|
const nameTerms = tokenize(name12);
|
|
3457
|
-
const descriptionTerms = tokenize(description
|
|
3406
|
+
const descriptionTerms = tokenize(description ?? "");
|
|
3458
3407
|
const score = terms.reduce(
|
|
3459
3408
|
(score2, term) => score2 + (nameTerms.includes(term) ? 2 : 0) + (descriptionTerms.includes(term) ? 1 : 0),
|
|
3460
3409
|
0
|
|
@@ -3478,8 +3427,7 @@ function createToolSearchState({
|
|
|
3478
3427
|
};
|
|
3479
3428
|
}
|
|
3480
3429
|
function tokenize(text) {
|
|
3481
|
-
|
|
3482
|
-
return (_a12 = text.replace(/([a-z\d])([A-Z])/g, "$1 $2").toLowerCase().match(/[\p{L}\p{N}]+/gu)) != null ? _a12 : [];
|
|
3430
|
+
return text.replace(/([a-z\d])([A-Z])/g, "$1 $2").toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [];
|
|
3483
3431
|
}
|
|
3484
3432
|
|
|
3485
3433
|
// src/generate-text/collect-tool-approvals.ts
|
|
@@ -3487,7 +3435,7 @@ function collectToolApprovals({
|
|
|
3487
3435
|
messages
|
|
3488
3436
|
}) {
|
|
3489
3437
|
const lastMessage = messages.at(-1);
|
|
3490
|
-
if (
|
|
3438
|
+
if (lastMessage?.role != "tool") {
|
|
3491
3439
|
return {
|
|
3492
3440
|
approvedToolApprovals: [],
|
|
3493
3441
|
deniedToolApprovals: []
|
|
@@ -3731,13 +3679,13 @@ async function resolveToolApproval({
|
|
|
3731
3679
|
toolContext: await validateToolContext({
|
|
3732
3680
|
toolName,
|
|
3733
3681
|
context: getOwn(toolsContext, toolName),
|
|
3734
|
-
contextSchema: tool2
|
|
3682
|
+
contextSchema: tool2?.contextSchema
|
|
3735
3683
|
}),
|
|
3736
3684
|
runtimeContext
|
|
3737
3685
|
}) : userDefinedToolApprovalStatus;
|
|
3738
3686
|
return normalizeToolApprovalStatus(approvalStatus);
|
|
3739
3687
|
}
|
|
3740
|
-
if (
|
|
3688
|
+
if (tool2?.needsApproval == null) {
|
|
3741
3689
|
return { type: "not-applicable" };
|
|
3742
3690
|
}
|
|
3743
3691
|
const needsApproval = typeof tool2.needsApproval === "function" ? await tool2.needsApproval(input, {
|
|
@@ -3746,7 +3694,7 @@ async function resolveToolApproval({
|
|
|
3746
3694
|
context: await validateToolContext({
|
|
3747
3695
|
toolName,
|
|
3748
3696
|
context: getOwn(toolsContext, toolName),
|
|
3749
|
-
contextSchema: tool2
|
|
3697
|
+
contextSchema: tool2?.contextSchema
|
|
3750
3698
|
})
|
|
3751
3699
|
}) : tool2.needsApproval;
|
|
3752
3700
|
return needsApproval ? { type: "user-approval" } : { type: "not-applicable" };
|
|
@@ -3766,7 +3714,6 @@ async function validateApprovedToolApprovals({
|
|
|
3766
3714
|
toolApprovalSecret,
|
|
3767
3715
|
refineToolInput
|
|
3768
3716
|
}) {
|
|
3769
|
-
var _a12;
|
|
3770
3717
|
const approved = [];
|
|
3771
3718
|
const denied = [];
|
|
3772
3719
|
const invalid = [];
|
|
@@ -3853,7 +3800,7 @@ async function validateApprovedToolApprovals({
|
|
|
3853
3800
|
approvalResponse: {
|
|
3854
3801
|
...approval.approvalResponse,
|
|
3855
3802
|
approved: false,
|
|
3856
|
-
reason:
|
|
3803
|
+
reason: approvalStatus.reason ?? approval.approvalResponse.reason
|
|
3857
3804
|
}
|
|
3858
3805
|
});
|
|
3859
3806
|
} else {
|