@ai-sdk/provider-utils 5.0.76 → 5.0.78
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 +12 -0
- package/dist/index.d.ts +25 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/fetch-with-validated-redirects.ts +21 -1
- package/src/index.ts +4 -0
- package/src/provider-defined-tool-factory.ts +11 -2
- package/src/provider-executed-tool-factory.ts +6 -1
- package/src/telemetry-redaction.ts +32 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { DownloadError } from './download-error';
|
|
|
3
3
|
import type { FetchFunction } from './fetch-function';
|
|
4
4
|
import { isBrowserRuntime } from './is-browser-runtime';
|
|
5
5
|
import { isSameOrigin } from './is-same-origin';
|
|
6
|
-
import { getDefaultDownloadFetch } from './safe-node-fetch';
|
|
6
|
+
import { getDefaultDownloadFetch, isNodeRuntime } from './safe-node-fetch';
|
|
7
7
|
import { sanitizeRequestHeaders } from './sanitize-request-headers';
|
|
8
8
|
import { validateDownloadUrl } from './validate-download-url';
|
|
9
9
|
|
|
@@ -39,6 +39,7 @@ export async function fetchWithValidatedEndpoint({
|
|
|
39
39
|
fetch: customFetch,
|
|
40
40
|
trustedOrigin,
|
|
41
41
|
redirect = 'error',
|
|
42
|
+
requireDnsValidation = false,
|
|
42
43
|
}: {
|
|
43
44
|
url: string | URL;
|
|
44
45
|
init?: RequestInit;
|
|
@@ -49,6 +50,11 @@ export async function fetchWithValidatedEndpoint({
|
|
|
49
50
|
*/
|
|
50
51
|
trustedOrigin?: string;
|
|
51
52
|
redirect?: 'error' | 'manual';
|
|
53
|
+
/**
|
|
54
|
+
* Reject untrusted URLs on server runtimes without a DNS-pinned default
|
|
55
|
+
* transport unless an explicit custom fetch provides equivalent validation.
|
|
56
|
+
*/
|
|
57
|
+
requireDnsValidation?: boolean;
|
|
52
58
|
}): Promise<Response> {
|
|
53
59
|
const urlText = url.toString();
|
|
54
60
|
const isTrusted =
|
|
@@ -56,6 +62,20 @@ export async function fetchWithValidatedEndpoint({
|
|
|
56
62
|
|
|
57
63
|
if (!isTrusted) {
|
|
58
64
|
validateDownloadUrl(urlText);
|
|
65
|
+
|
|
66
|
+
if (
|
|
67
|
+
requireDnsValidation &&
|
|
68
|
+
(customFetch == null || customFetch === globalThis.fetch) &&
|
|
69
|
+
!isNodeRuntime() &&
|
|
70
|
+
// A window alias alone does not distinguish older Deno from a browser.
|
|
71
|
+
!(isBrowserRuntime() && typeof document !== 'undefined')
|
|
72
|
+
) {
|
|
73
|
+
throw new DownloadError({
|
|
74
|
+
url: urlText,
|
|
75
|
+
message:
|
|
76
|
+
'This server runtime requires a custom fetch with connect-time DNS validation for untrusted URLs',
|
|
77
|
+
});
|
|
78
|
+
}
|
|
59
79
|
}
|
|
60
80
|
|
|
61
81
|
const fetch =
|
package/src/index.ts
CHANGED
|
@@ -117,6 +117,10 @@ export {
|
|
|
117
117
|
export { serializeModelOptions } from './serialize-model-options';
|
|
118
118
|
export { SerializationError } from './serialization-error';
|
|
119
119
|
export { secureJsonParse } from './secure-json-parse';
|
|
120
|
+
export {
|
|
121
|
+
withTelemetryRedaction,
|
|
122
|
+
redactTelemetryValue,
|
|
123
|
+
} from './telemetry-redaction';
|
|
120
124
|
export {
|
|
121
125
|
SPEECH_STREAM_START_FRAME_TYPE as EXPERIMENTAL_SPEECH_STREAM_START_FRAME_TYPE,
|
|
122
126
|
SPEECH_STREAM_TEXT_DELTA_FRAME_TYPE as EXPERIMENTAL_SPEECH_STREAM_TEXT_DELTA_FRAME_TYPE,
|
|
@@ -2,6 +2,7 @@ import { tool, type ProviderDefinedTool, type Tool } from './types/tool';
|
|
|
2
2
|
import type { FlexibleSchema } from './schema';
|
|
3
3
|
import type { Context } from './types/context';
|
|
4
4
|
import type { ToolExecuteFunction } from './types/tool-execute-function';
|
|
5
|
+
import { withTelemetryRedaction } from './telemetry-redaction';
|
|
5
6
|
/**
|
|
6
7
|
* A provider-defined tool is a tool for which the provider defines the input
|
|
7
8
|
* and output schemas, but does not execute the tool.
|
|
@@ -28,9 +29,12 @@ export function createProviderDefinedToolFactory<
|
|
|
28
29
|
>({
|
|
29
30
|
id,
|
|
30
31
|
inputSchema,
|
|
32
|
+
redactArgs,
|
|
31
33
|
}: {
|
|
32
34
|
id: `${string}.${string}`;
|
|
33
35
|
inputSchema: FlexibleSchema<INPUT>;
|
|
36
|
+
/** Provider-owned telemetry view of the tool configuration. */
|
|
37
|
+
redactArgs?: (args: Record<string, unknown>) => Record<string, unknown>;
|
|
34
38
|
}): ProviderDefinedToolFactory<INPUT, ARGS, CONTEXT> {
|
|
35
39
|
return <OUTPUT>({
|
|
36
40
|
execute,
|
|
@@ -54,7 +58,8 @@ export function createProviderDefinedToolFactory<
|
|
|
54
58
|
type: 'provider',
|
|
55
59
|
isProviderExecuted: false,
|
|
56
60
|
id,
|
|
57
|
-
args
|
|
61
|
+
args:
|
|
62
|
+
redactArgs == null ? args : withTelemetryRedaction(args, redactArgs),
|
|
58
63
|
inputSchema,
|
|
59
64
|
outputSchema,
|
|
60
65
|
execute,
|
|
@@ -91,10 +96,13 @@ export function createProviderDefinedToolFactoryWithOutputSchema<
|
|
|
91
96
|
id,
|
|
92
97
|
inputSchema,
|
|
93
98
|
outputSchema,
|
|
99
|
+
redactArgs,
|
|
94
100
|
}: {
|
|
95
101
|
id: `${string}.${string}`;
|
|
96
102
|
inputSchema: FlexibleSchema<INPUT>;
|
|
97
103
|
outputSchema: FlexibleSchema<OUTPUT>;
|
|
104
|
+
/** Provider-owned telemetry view of the tool configuration. */
|
|
105
|
+
redactArgs?: (args: Record<string, unknown>) => Record<string, unknown>;
|
|
98
106
|
}): ProviderDefinedToolFactoryWithOutputSchema<INPUT, OUTPUT, ARGS, CONTEXT> {
|
|
99
107
|
return ({
|
|
100
108
|
execute,
|
|
@@ -116,7 +124,8 @@ export function createProviderDefinedToolFactoryWithOutputSchema<
|
|
|
116
124
|
type: 'provider',
|
|
117
125
|
isProviderExecuted: false,
|
|
118
126
|
id,
|
|
119
|
-
args
|
|
127
|
+
args:
|
|
128
|
+
redactArgs == null ? args : withTelemetryRedaction(args, redactArgs),
|
|
120
129
|
inputSchema,
|
|
121
130
|
outputSchema,
|
|
122
131
|
execute,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { FlexibleSchema } from './schema';
|
|
2
2
|
import type { Context } from './types/context';
|
|
3
3
|
import { tool, type ProviderExecutedTool, type Tool } from './types/tool';
|
|
4
|
+
import { withTelemetryRedaction } from './telemetry-redaction';
|
|
4
5
|
/**
|
|
5
6
|
* A provider-executed tool is a tool for which the provider executes the tool.
|
|
6
7
|
*/
|
|
@@ -27,6 +28,7 @@ export function createProviderExecutedToolFactory<
|
|
|
27
28
|
inputSchema,
|
|
28
29
|
outputSchema,
|
|
29
30
|
supportsDeferredResults,
|
|
31
|
+
redactArgs,
|
|
30
32
|
}: {
|
|
31
33
|
id: `${string}.${string}`;
|
|
32
34
|
inputSchema: FlexibleSchema<INPUT>;
|
|
@@ -43,6 +45,8 @@ export function createProviderExecutedToolFactory<
|
|
|
43
45
|
* @default false
|
|
44
46
|
*/
|
|
45
47
|
supportsDeferredResults?: boolean;
|
|
48
|
+
/** Provider-owned telemetry view of the tool configuration. */
|
|
49
|
+
redactArgs?: (args: Record<string, unknown>) => Record<string, unknown>;
|
|
46
50
|
}): ProviderExecutedToolFactory<INPUT, OUTPUT, ARGS, CONTEXT> {
|
|
47
51
|
return ({
|
|
48
52
|
onInputStart,
|
|
@@ -58,7 +62,8 @@ export function createProviderExecutedToolFactory<
|
|
|
58
62
|
type: 'provider',
|
|
59
63
|
isProviderExecuted: true,
|
|
60
64
|
id,
|
|
61
|
-
args
|
|
65
|
+
args:
|
|
66
|
+
redactArgs == null ? args : withTelemetryRedaction(args, redactArgs),
|
|
62
67
|
inputSchema,
|
|
63
68
|
outputSchema,
|
|
64
69
|
onInputStart,
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const telemetryRedaction = Symbol.for('vercel.ai.telemetry.redact');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Attach a provider-owned telemetry view without changing the original value.
|
|
5
|
+
* The annotation is not serialized or included in enumerable application data.
|
|
6
|
+
* Keep the annotated object when forwarding tool arguments or provider metadata.
|
|
7
|
+
*/
|
|
8
|
+
export function withTelemetryRedaction<T extends Record<string, unknown>>(
|
|
9
|
+
value: T,
|
|
10
|
+
redact: (value: T) => T,
|
|
11
|
+
): T {
|
|
12
|
+
return Object.create(Object.getPrototypeOf(value), {
|
|
13
|
+
...Object.getOwnPropertyDescriptors(value),
|
|
14
|
+
[telemetryRedaction]: { value: redact, configurable: true },
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Apply the provider's telemetry view, leaving unannotated values unchanged. */
|
|
19
|
+
export function redactTelemetryValue<T>(value: T): T | undefined {
|
|
20
|
+
if (value == null || typeof value !== 'object') return value;
|
|
21
|
+
const redact = Object.getOwnPropertyDescriptor(
|
|
22
|
+
value,
|
|
23
|
+
telemetryRedaction,
|
|
24
|
+
)?.value;
|
|
25
|
+
if (typeof redact !== 'function') return value;
|
|
26
|
+
try {
|
|
27
|
+
return redact(value);
|
|
28
|
+
} catch {
|
|
29
|
+
// A broken provider redactor must neither expose credentials nor fail the call.
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
}
|