@ai-sdk/provider-utils 3.0.7 → 3.0.9
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.mts +34 -17
- package/dist/index.d.ts +34 -17
- package/dist/index.js +57 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +54 -8
- package/dist/index.mjs.map +1 -1
- package/dist/test/index.js +75 -18693
- package/dist/test/index.js.map +1 -1
- package/dist/test/index.mjs +64 -18665
- package/dist/test/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/test/chunk-D6YTI3O5.mjs +0 -45
- package/dist/test/chunk-D6YTI3O5.mjs.map +0 -1
- package/dist/test/graphql-6JDEV3ML.mjs +0 -12082
- package/dist/test/graphql-6JDEV3ML.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# @ai-sdk/provider-utils
|
2
2
|
|
3
|
+
## 3.0.9
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 0294b58: feat(ai): set `ai`, `@ai-sdk/provider-utils`, and runtime in `user-agent` header
|
8
|
+
|
9
|
+
## 3.0.8
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 99964ed: fix(provider-utils): fix type inference for toModelOutput
|
14
|
+
|
3
15
|
## 3.0.7
|
4
16
|
|
5
17
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
@@ -43,6 +43,20 @@ declare function extractResponseHeaders(response: Response): {
|
|
43
43
|
*/
|
44
44
|
type FetchFunction = typeof globalThis.fetch;
|
45
45
|
|
46
|
+
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Appends suffix parts to the `user-agent` header.
|
50
|
+
* If a `user-agent` header already exists, the suffix parts are appended to it.
|
51
|
+
* If no `user-agent` header exists, a new one is created with the suffix parts.
|
52
|
+
* Automatically removes undefined entries from the headers.
|
53
|
+
*
|
54
|
+
* @param headers - The original headers.
|
55
|
+
* @param userAgentSuffixParts - The parts to append to the `user-agent` header.
|
56
|
+
* @returns The new headers with the `user-agent` header set or updated.
|
57
|
+
*/
|
58
|
+
declare function withUserAgentSuffix(headers: HeadersInit | Record<string, string | undefined> | undefined, ...userAgentSuffixParts: string[]): Record<string, string>;
|
59
|
+
|
46
60
|
/**
|
47
61
|
Creates an ID generator.
|
48
62
|
The total length of the ID is the sum of the prefix, separator, and random part length.
|
@@ -608,6 +622,20 @@ interface ToolCallOptions {
|
|
608
622
|
}
|
609
623
|
type ToolExecuteFunction<INPUT, OUTPUT> = (input: INPUT, options: ToolCallOptions) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
610
624
|
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
625
|
+
type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
626
|
+
/**
|
627
|
+
An async function that is called with the arguments from the tool call and produces a result.
|
628
|
+
If not provided, the tool will not be executed automatically.
|
629
|
+
|
630
|
+
@args is the input of the tool call.
|
631
|
+
@options.abortSignal is a signal that can be used to abort the tool call.
|
632
|
+
*/
|
633
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
634
|
+
outputSchema?: FlexibleSchema<OUTPUT>;
|
635
|
+
} | {
|
636
|
+
outputSchema: FlexibleSchema<OUTPUT>;
|
637
|
+
execute?: never;
|
638
|
+
}>;
|
611
639
|
/**
|
612
640
|
A tool contains the description and the schema of the input that the tool expects.
|
613
641
|
This enables the language model to generate the input.
|
@@ -652,27 +680,14 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
652
680
|
onInputAvailable?: (options: {
|
653
681
|
input: [INPUT] extends [never] ? undefined : INPUT;
|
654
682
|
} & ToolCallOptions) => void | PromiseLike<void>;
|
655
|
-
} &
|
683
|
+
} & ToolOutputProperties<INPUT, OUTPUT> & {
|
656
684
|
/**
|
657
685
|
Optional conversion function that maps the tool result to an output that can be used by the language model.
|
658
686
|
|
659
687
|
If not provided, the tool result will be sent as a JSON object.
|
660
|
-
*/
|
661
|
-
toModelOutput?: (output: OUTPUT) => LanguageModelV2ToolResultPart['output'];
|
662
|
-
} & ({
|
663
|
-
/**
|
664
|
-
An async function that is called with the arguments from the tool call and produces a result.
|
665
|
-
If not provided, the tool will not be executed automatically.
|
666
|
-
|
667
|
-
@args is the input of the tool call.
|
668
|
-
@options.abortSignal is a signal that can be used to abort the tool call.
|
669
688
|
*/
|
670
|
-
|
671
|
-
|
672
|
-
} | {
|
673
|
-
outputSchema: FlexibleSchema<OUTPUT>;
|
674
|
-
execute?: never;
|
675
|
-
})> & ({
|
689
|
+
toModelOutput?: (output: 0 extends 1 & OUTPUT ? any : [OUTPUT] extends [never] ? any : NoInfer<OUTPUT>) => LanguageModelV2ToolResultPart['output'];
|
690
|
+
} & ({
|
676
691
|
/**
|
677
692
|
Tool with user-defined input and output schemas.
|
678
693
|
*/
|
@@ -891,4 +906,6 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
891
906
|
dynamic?: boolean;
|
892
907
|
}
|
893
908
|
|
894
|
-
|
909
|
+
declare const VERSION: string;
|
910
|
+
|
911
|
+
export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
package/dist/index.d.ts
CHANGED
@@ -43,6 +43,20 @@ declare function extractResponseHeaders(response: Response): {
|
|
43
43
|
*/
|
44
44
|
type FetchFunction = typeof globalThis.fetch;
|
45
45
|
|
46
|
+
declare function getRuntimeEnvironmentUserAgent(globalThisAny?: any): string;
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Appends suffix parts to the `user-agent` header.
|
50
|
+
* If a `user-agent` header already exists, the suffix parts are appended to it.
|
51
|
+
* If no `user-agent` header exists, a new one is created with the suffix parts.
|
52
|
+
* Automatically removes undefined entries from the headers.
|
53
|
+
*
|
54
|
+
* @param headers - The original headers.
|
55
|
+
* @param userAgentSuffixParts - The parts to append to the `user-agent` header.
|
56
|
+
* @returns The new headers with the `user-agent` header set or updated.
|
57
|
+
*/
|
58
|
+
declare function withUserAgentSuffix(headers: HeadersInit | Record<string, string | undefined> | undefined, ...userAgentSuffixParts: string[]): Record<string, string>;
|
59
|
+
|
46
60
|
/**
|
47
61
|
Creates an ID generator.
|
48
62
|
The total length of the ID is the sum of the prefix, separator, and random part length.
|
@@ -608,6 +622,20 @@ interface ToolCallOptions {
|
|
608
622
|
}
|
609
623
|
type ToolExecuteFunction<INPUT, OUTPUT> = (input: INPUT, options: ToolCallOptions) => AsyncIterable<OUTPUT> | PromiseLike<OUTPUT> | OUTPUT;
|
610
624
|
type NeverOptional<N, T> = 0 extends 1 & N ? Partial<T> : [N] extends [never] ? Partial<Record<keyof T, undefined>> : T;
|
625
|
+
type ToolOutputProperties<INPUT, OUTPUT> = NeverOptional<OUTPUT, {
|
626
|
+
/**
|
627
|
+
An async function that is called with the arguments from the tool call and produces a result.
|
628
|
+
If not provided, the tool will not be executed automatically.
|
629
|
+
|
630
|
+
@args is the input of the tool call.
|
631
|
+
@options.abortSignal is a signal that can be used to abort the tool call.
|
632
|
+
*/
|
633
|
+
execute: ToolExecuteFunction<INPUT, OUTPUT>;
|
634
|
+
outputSchema?: FlexibleSchema<OUTPUT>;
|
635
|
+
} | {
|
636
|
+
outputSchema: FlexibleSchema<OUTPUT>;
|
637
|
+
execute?: never;
|
638
|
+
}>;
|
611
639
|
/**
|
612
640
|
A tool contains the description and the schema of the input that the tool expects.
|
613
641
|
This enables the language model to generate the input.
|
@@ -652,27 +680,14 @@ type Tool<INPUT extends JSONValue | unknown | never = any, OUTPUT extends JSONVa
|
|
652
680
|
onInputAvailable?: (options: {
|
653
681
|
input: [INPUT] extends [never] ? undefined : INPUT;
|
654
682
|
} & ToolCallOptions) => void | PromiseLike<void>;
|
655
|
-
} &
|
683
|
+
} & ToolOutputProperties<INPUT, OUTPUT> & {
|
656
684
|
/**
|
657
685
|
Optional conversion function that maps the tool result to an output that can be used by the language model.
|
658
686
|
|
659
687
|
If not provided, the tool result will be sent as a JSON object.
|
660
|
-
*/
|
661
|
-
toModelOutput?: (output: OUTPUT) => LanguageModelV2ToolResultPart['output'];
|
662
|
-
} & ({
|
663
|
-
/**
|
664
|
-
An async function that is called with the arguments from the tool call and produces a result.
|
665
|
-
If not provided, the tool will not be executed automatically.
|
666
|
-
|
667
|
-
@args is the input of the tool call.
|
668
|
-
@options.abortSignal is a signal that can be used to abort the tool call.
|
669
688
|
*/
|
670
|
-
|
671
|
-
|
672
|
-
} | {
|
673
|
-
outputSchema: FlexibleSchema<OUTPUT>;
|
674
|
-
execute?: never;
|
675
|
-
})> & ({
|
689
|
+
toModelOutput?: (output: 0 extends 1 & OUTPUT ? any : [OUTPUT] extends [never] ? any : NoInfer<OUTPUT>) => LanguageModelV2ToolResultPart['output'];
|
690
|
+
} & ({
|
676
691
|
/**
|
677
692
|
Tool with user-defined input and output schemas.
|
678
693
|
*/
|
@@ -891,4 +906,6 @@ interface ToolResult<NAME extends string, INPUT, OUTPUT> {
|
|
891
906
|
dynamic?: boolean;
|
892
907
|
}
|
893
908
|
|
894
|
-
|
909
|
+
declare const VERSION: string;
|
910
|
+
|
911
|
+
export { type AssistantContent, type AssistantModelMessage, type DataContent, type FetchFunction, type FilePart, type FlexibleSchema, type IdGenerator, type ImagePart, type InferSchema, type InferToolInput, type InferToolOutput, type ModelMessage, type ParseResult, type ProviderDefinedToolFactory, type ProviderDefinedToolFactoryWithOutputSchema, type ProviderOptions, type ReasoningPart, type Resolvable, type ResponseHandler, type Schema, type SystemModelMessage, type TextPart, type Tool, type ToolCall, type ToolCallOptions, type ToolCallPart, type ToolContent, type ToolExecuteFunction, type ToolModelMessage, type ToolResult, type ToolResultPart, type UserContent, type UserModelMessage, VERSION, type ValidationResult, type Validator, asSchema, asValidator, combineHeaders, convertAsyncIteratorToReadableStream, convertBase64ToUint8Array, convertToBase64, convertUint8ArrayToBase64, createBinaryResponseHandler, createEventSourceResponseHandler, createIdGenerator, createJsonErrorResponseHandler, createJsonResponseHandler, createJsonStreamResponseHandler, createProviderDefinedToolFactory, createProviderDefinedToolFactoryWithOutputSchema, createStatusCodeErrorResponseHandler, delay, dynamicTool, executeTool, extractResponseHeaders, generateId, getErrorMessage, getFromApi, getRuntimeEnvironmentUserAgent, injectJsonInstructionIntoMessages, isAbortError, isParsableJson, isUrlSupported, isValidator, jsonSchema, loadApiKey, loadOptionalSetting, loadSetting, mediaTypeToExtension, parseJSON, parseJsonEventStream, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, removeUndefinedEntries, resolve, safeParseJSON, safeValidateTypes, standardSchemaValidator, tool, validateTypes, validator, validatorSymbol, withUserAgentSuffix, withoutTrailingSlash, zodSchema };
|
package/dist/index.js
CHANGED
@@ -32,6 +32,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
32
32
|
var src_exports = {};
|
33
33
|
__export(src_exports, {
|
34
34
|
EventSourceParserStream: () => import_stream2.EventSourceParserStream,
|
35
|
+
VERSION: () => VERSION,
|
35
36
|
asSchema: () => asSchema,
|
36
37
|
asValidator: () => asValidator,
|
37
38
|
combineHeaders: () => combineHeaders,
|
@@ -55,6 +56,7 @@ __export(src_exports, {
|
|
55
56
|
generateId: () => generateId,
|
56
57
|
getErrorMessage: () => getErrorMessage,
|
57
58
|
getFromApi: () => getFromApi,
|
59
|
+
getRuntimeEnvironmentUserAgent: () => getRuntimeEnvironmentUserAgent,
|
58
60
|
injectJsonInstructionIntoMessages: () => injectJsonInstructionIntoMessages,
|
59
61
|
isAbortError: () => isAbortError,
|
60
62
|
isParsableJson: () => isParsableJson,
|
@@ -80,6 +82,7 @@ __export(src_exports, {
|
|
80
82
|
validateTypes: () => validateTypes,
|
81
83
|
validator: () => validator,
|
82
84
|
validatorSymbol: () => validatorSymbol,
|
85
|
+
withUserAgentSuffix: () => withUserAgentSuffix,
|
83
86
|
withoutTrailingSlash: () => withoutTrailingSlash,
|
84
87
|
zodSchema: () => zodSchema
|
85
88
|
});
|
@@ -160,6 +163,45 @@ function extractResponseHeaders(response) {
|
|
160
163
|
return Object.fromEntries([...response.headers]);
|
161
164
|
}
|
162
165
|
|
166
|
+
// src/get-runtime-environment-user-agent.ts
|
167
|
+
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
168
|
+
var _a, _b, _c;
|
169
|
+
if (globalThisAny.window) {
|
170
|
+
return `runtime/browser`;
|
171
|
+
}
|
172
|
+
if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
|
173
|
+
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
174
|
+
}
|
175
|
+
if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
|
176
|
+
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
177
|
+
}
|
178
|
+
if (globalThisAny.EdgeRuntime) {
|
179
|
+
return `runtime/vercel-edge`;
|
180
|
+
}
|
181
|
+
return "runtime/unknown";
|
182
|
+
}
|
183
|
+
|
184
|
+
// src/remove-undefined-entries.ts
|
185
|
+
function removeUndefinedEntries(record) {
|
186
|
+
return Object.fromEntries(
|
187
|
+
Object.entries(record).filter(([_key, value]) => value != null)
|
188
|
+
);
|
189
|
+
}
|
190
|
+
|
191
|
+
// src/with-user-agent-suffix.ts
|
192
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
193
|
+
const cleanedHeaders = removeUndefinedEntries(
|
194
|
+
headers != null ? headers : {}
|
195
|
+
);
|
196
|
+
const normalizedHeaders = new Headers(cleanedHeaders);
|
197
|
+
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
198
|
+
normalizedHeaders.set(
|
199
|
+
"user-agent",
|
200
|
+
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
201
|
+
);
|
202
|
+
return Object.fromEntries(normalizedHeaders);
|
203
|
+
}
|
204
|
+
|
163
205
|
// src/generate-id.ts
|
164
206
|
var import_provider = require("@ai-sdk/provider");
|
165
207
|
var createIdGenerator = ({
|
@@ -241,12 +283,8 @@ function handleFetchError({
|
|
241
283
|
return error;
|
242
284
|
}
|
243
285
|
|
244
|
-
// src/
|
245
|
-
|
246
|
-
return Object.fromEntries(
|
247
|
-
Object.entries(record).filter(([_key, value]) => value != null)
|
248
|
-
);
|
249
|
-
}
|
286
|
+
// src/version.ts
|
287
|
+
var VERSION = true ? "3.0.9" : "0.0.0-test";
|
250
288
|
|
251
289
|
// src/get-from-api.ts
|
252
290
|
var getOriginalFetch = () => globalThis.fetch;
|
@@ -261,7 +299,11 @@ var getFromApi = async ({
|
|
261
299
|
try {
|
262
300
|
const response = await fetch(url, {
|
263
301
|
method: "GET",
|
264
|
-
headers:
|
302
|
+
headers: withUserAgentSuffix(
|
303
|
+
headers,
|
304
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
305
|
+
getRuntimeEnvironmentUserAgent()
|
306
|
+
),
|
265
307
|
signal: abortSignal
|
266
308
|
});
|
267
309
|
const responseHeaders = extractResponseHeaders(response);
|
@@ -727,7 +769,11 @@ var postToApi = async ({
|
|
727
769
|
try {
|
728
770
|
const response = await fetch(url, {
|
729
771
|
method: "POST",
|
730
|
-
headers:
|
772
|
+
headers: withUserAgentSuffix(
|
773
|
+
headers,
|
774
|
+
`ai-sdk/provider-utils/${VERSION}`,
|
775
|
+
getRuntimeEnvironmentUserAgent()
|
776
|
+
),
|
731
777
|
body: body.content,
|
732
778
|
signal: abortSignal
|
733
779
|
});
|
@@ -2327,6 +2373,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2327
2373
|
// Annotate the CommonJS export names for ESM import in node:
|
2328
2374
|
0 && (module.exports = {
|
2329
2375
|
EventSourceParserStream,
|
2376
|
+
VERSION,
|
2330
2377
|
asSchema,
|
2331
2378
|
asValidator,
|
2332
2379
|
combineHeaders,
|
@@ -2350,6 +2397,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2350
2397
|
generateId,
|
2351
2398
|
getErrorMessage,
|
2352
2399
|
getFromApi,
|
2400
|
+
getRuntimeEnvironmentUserAgent,
|
2353
2401
|
injectJsonInstructionIntoMessages,
|
2354
2402
|
isAbortError,
|
2355
2403
|
isParsableJson,
|
@@ -2375,6 +2423,7 @@ var import_stream2 = require("eventsource-parser/stream");
|
|
2375
2423
|
validateTypes,
|
2376
2424
|
validator,
|
2377
2425
|
validatorSymbol,
|
2426
|
+
withUserAgentSuffix,
|
2378
2427
|
withoutTrailingSlash,
|
2379
2428
|
zodSchema,
|
2380
2429
|
...require("@standard-schema/spec")
|