@ai-sdk/provider-utils 4.0.0-beta.10 → 4.0.0-beta.12
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 +13 -0
- package/dist/index.d.mts +68 -47
- package/dist/index.d.ts +68 -47
- package/dist/index.js +155 -82
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +153 -81
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -2
package/dist/index.mjs
CHANGED
@@ -73,45 +73,6 @@ function extractResponseHeaders(response) {
|
|
73
73
|
return Object.fromEntries([...response.headers]);
|
74
74
|
}
|
75
75
|
|
76
|
-
// src/get-runtime-environment-user-agent.ts
|
77
|
-
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
78
|
-
var _a, _b, _c;
|
79
|
-
if (globalThisAny.window) {
|
80
|
-
return `runtime/browser`;
|
81
|
-
}
|
82
|
-
if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
|
83
|
-
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
84
|
-
}
|
85
|
-
if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
|
86
|
-
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
87
|
-
}
|
88
|
-
if (globalThisAny.EdgeRuntime) {
|
89
|
-
return `runtime/vercel-edge`;
|
90
|
-
}
|
91
|
-
return "runtime/unknown";
|
92
|
-
}
|
93
|
-
|
94
|
-
// src/remove-undefined-entries.ts
|
95
|
-
function removeUndefinedEntries(record) {
|
96
|
-
return Object.fromEntries(
|
97
|
-
Object.entries(record).filter(([_key, value]) => value != null)
|
98
|
-
);
|
99
|
-
}
|
100
|
-
|
101
|
-
// src/with-user-agent-suffix.ts
|
102
|
-
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
103
|
-
const cleanedHeaders = removeUndefinedEntries(
|
104
|
-
headers != null ? headers : {}
|
105
|
-
);
|
106
|
-
const normalizedHeaders = new Headers(cleanedHeaders);
|
107
|
-
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
108
|
-
normalizedHeaders.set(
|
109
|
-
"user-agent",
|
110
|
-
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
111
|
-
);
|
112
|
-
return Object.fromEntries(normalizedHeaders);
|
113
|
-
}
|
114
|
-
|
115
76
|
// src/generate-id.ts
|
116
77
|
import { InvalidArgumentError } from "@ai-sdk/provider";
|
117
78
|
var createIdGenerator = ({
|
@@ -193,8 +154,47 @@ function handleFetchError({
|
|
193
154
|
return error;
|
194
155
|
}
|
195
156
|
|
157
|
+
// src/get-runtime-environment-user-agent.ts
|
158
|
+
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
159
|
+
var _a, _b, _c;
|
160
|
+
if (globalThisAny.window) {
|
161
|
+
return `runtime/browser`;
|
162
|
+
}
|
163
|
+
if ((_a = globalThisAny.navigator) == null ? void 0 : _a.userAgent) {
|
164
|
+
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
165
|
+
}
|
166
|
+
if ((_c = (_b = globalThisAny.process) == null ? void 0 : _b.versions) == null ? void 0 : _c.node) {
|
167
|
+
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
168
|
+
}
|
169
|
+
if (globalThisAny.EdgeRuntime) {
|
170
|
+
return `runtime/vercel-edge`;
|
171
|
+
}
|
172
|
+
return "runtime/unknown";
|
173
|
+
}
|
174
|
+
|
175
|
+
// src/remove-undefined-entries.ts
|
176
|
+
function removeUndefinedEntries(record) {
|
177
|
+
return Object.fromEntries(
|
178
|
+
Object.entries(record).filter(([_key, value]) => value != null)
|
179
|
+
);
|
180
|
+
}
|
181
|
+
|
182
|
+
// src/with-user-agent-suffix.ts
|
183
|
+
function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
184
|
+
const cleanedHeaders = removeUndefinedEntries(
|
185
|
+
headers != null ? headers : {}
|
186
|
+
);
|
187
|
+
const normalizedHeaders = new Headers(cleanedHeaders);
|
188
|
+
const currentUserAgentHeader = normalizedHeaders.get("user-agent") || "";
|
189
|
+
normalizedHeaders.set(
|
190
|
+
"user-agent",
|
191
|
+
[currentUserAgentHeader, ...userAgentSuffixParts].filter(Boolean).join(" ")
|
192
|
+
);
|
193
|
+
return Object.fromEntries(normalizedHeaders);
|
194
|
+
}
|
195
|
+
|
196
196
|
// src/version.ts
|
197
|
-
var VERSION = true ? "4.0.0-beta.
|
197
|
+
var VERSION = true ? "4.0.0-beta.12" : "0.0.0-test";
|
198
198
|
|
199
199
|
// src/get-from-api.ts
|
200
200
|
var getOriginalFetch = () => globalThis.fetch;
|
@@ -483,12 +483,21 @@ function validator(validate) {
|
|
483
483
|
function isValidator(value) {
|
484
484
|
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
485
485
|
}
|
486
|
+
function lazyValidator(createValidator) {
|
487
|
+
let validator2;
|
488
|
+
return () => {
|
489
|
+
if (validator2 == null) {
|
490
|
+
validator2 = createValidator();
|
491
|
+
}
|
492
|
+
return validator2;
|
493
|
+
};
|
494
|
+
}
|
486
495
|
function asValidator(value) {
|
487
|
-
return isValidator(value) ? value : standardSchemaValidator(value);
|
496
|
+
return isValidator(value) ? value : typeof value === "function" ? value() : standardSchemaValidator(value);
|
488
497
|
}
|
489
|
-
function standardSchemaValidator(
|
498
|
+
function standardSchemaValidator(standardSchema2) {
|
490
499
|
return validator(async (value) => {
|
491
|
-
const result = await
|
500
|
+
const result = await standardSchema2["~standard"].validate(value);
|
492
501
|
return result.issues == null ? { success: true, value: result.value } : {
|
493
502
|
success: false,
|
494
503
|
error: new TypeValidationError({
|
@@ -984,9 +993,20 @@ var createStatusCodeErrorResponseHandler = () => async ({ response, url, request
|
|
984
993
|
};
|
985
994
|
};
|
986
995
|
|
987
|
-
// src/
|
996
|
+
// src/schema.ts
|
997
|
+
import { TypeValidationError as TypeValidationError4 } from "@ai-sdk/provider";
|
988
998
|
import * as z4 from "zod/v4";
|
989
999
|
|
1000
|
+
// src/valibot-to-json-schema/valibot-to-json-schema.ts
|
1001
|
+
var valibotToJsonSchema = async (schema) => {
|
1002
|
+
try {
|
1003
|
+
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
1004
|
+
return toJsonSchema(schema);
|
1005
|
+
} catch (e) {
|
1006
|
+
throw new Error(`Failed to import @valibot/to-json-schema`);
|
1007
|
+
}
|
1008
|
+
};
|
1009
|
+
|
990
1010
|
// src/zod-to-json-schema/get-relative-path.ts
|
991
1011
|
var getRelativePath = (pathA, pathB) => {
|
992
1012
|
let i = 0;
|
@@ -2170,12 +2190,84 @@ var zodToJsonSchema = (schema, options) => {
|
|
2170
2190
|
// src/zod-to-json-schema/index.ts
|
2171
2191
|
var zod_to_json_schema_default = zodToJsonSchema;
|
2172
2192
|
|
2173
|
-
// src/
|
2193
|
+
// src/schema.ts
|
2194
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
2195
|
+
function lazySchema(createSchema) {
|
2196
|
+
let schema;
|
2197
|
+
return () => {
|
2198
|
+
if (schema == null) {
|
2199
|
+
schema = createSchema();
|
2200
|
+
}
|
2201
|
+
return schema;
|
2202
|
+
};
|
2203
|
+
}
|
2204
|
+
function jsonSchema(jsonSchema2, {
|
2205
|
+
validate
|
2206
|
+
} = {}) {
|
2207
|
+
return {
|
2208
|
+
[schemaSymbol]: true,
|
2209
|
+
_type: void 0,
|
2210
|
+
// should never be used directly
|
2211
|
+
[validatorSymbol]: true,
|
2212
|
+
get jsonSchema() {
|
2213
|
+
if (typeof jsonSchema2 === "function") {
|
2214
|
+
jsonSchema2 = jsonSchema2();
|
2215
|
+
}
|
2216
|
+
return jsonSchema2;
|
2217
|
+
},
|
2218
|
+
validate
|
2219
|
+
};
|
2220
|
+
}
|
2221
|
+
function isSchema(value) {
|
2222
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
2223
|
+
}
|
2224
|
+
function asSchema(schema) {
|
2225
|
+
return schema == null ? jsonSchema({
|
2226
|
+
properties: {},
|
2227
|
+
additionalProperties: false
|
2228
|
+
}) : isSchema(schema) ? schema : typeof schema === "function" ? schema() : standardSchema(schema);
|
2229
|
+
}
|
2230
|
+
function standardSchema(standardSchema2) {
|
2231
|
+
const vendor = standardSchema2["~standard"].vendor;
|
2232
|
+
switch (vendor) {
|
2233
|
+
case "zod": {
|
2234
|
+
return zodSchema(
|
2235
|
+
standardSchema2
|
2236
|
+
);
|
2237
|
+
}
|
2238
|
+
case "valibot": {
|
2239
|
+
return standardSchemaWithJsonSchemaResolver(
|
2240
|
+
standardSchema2,
|
2241
|
+
valibotToJsonSchema
|
2242
|
+
);
|
2243
|
+
}
|
2244
|
+
default: {
|
2245
|
+
return standardSchemaWithJsonSchemaResolver(standardSchema2, () => {
|
2246
|
+
throw new Error(`Unsupported standard schema vendor: ${vendor}`);
|
2247
|
+
});
|
2248
|
+
}
|
2249
|
+
}
|
2250
|
+
}
|
2251
|
+
function standardSchemaWithJsonSchemaResolver(standardSchema2, jsonSchemaResolver) {
|
2252
|
+
return jsonSchema(jsonSchemaResolver(standardSchema2), {
|
2253
|
+
validate: async (value) => {
|
2254
|
+
const result = await standardSchema2["~standard"].validate(value);
|
2255
|
+
return "value" in result ? { success: true, value: result.value } : {
|
2256
|
+
success: false,
|
2257
|
+
error: new TypeValidationError4({
|
2258
|
+
value,
|
2259
|
+
cause: result.issues
|
2260
|
+
})
|
2261
|
+
};
|
2262
|
+
}
|
2263
|
+
});
|
2264
|
+
}
|
2174
2265
|
function zod3Schema(zodSchema2, options) {
|
2175
2266
|
var _a;
|
2176
2267
|
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
2177
2268
|
return jsonSchema(
|
2178
|
-
|
2269
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
2270
|
+
() => zod_to_json_schema_default(zodSchema2, {
|
2179
2271
|
$refStrategy: useReferences ? "root" : "none"
|
2180
2272
|
}),
|
2181
2273
|
{
|
@@ -2189,17 +2281,20 @@ function zod3Schema(zodSchema2, options) {
|
|
2189
2281
|
function zod4Schema(zodSchema2, options) {
|
2190
2282
|
var _a;
|
2191
2283
|
const useReferences = (_a = options == null ? void 0 : options.useReferences) != null ? _a : false;
|
2192
|
-
|
2193
|
-
|
2194
|
-
|
2195
|
-
|
2196
|
-
|
2197
|
-
|
2198
|
-
|
2199
|
-
|
2200
|
-
|
2284
|
+
return jsonSchema(
|
2285
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
2286
|
+
() => z4.toJSONSchema(zodSchema2, {
|
2287
|
+
target: "draft-7",
|
2288
|
+
io: "output",
|
2289
|
+
reused: useReferences ? "ref" : "inline"
|
2290
|
+
}),
|
2291
|
+
{
|
2292
|
+
validate: async (value) => {
|
2293
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
2294
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
2295
|
+
}
|
2201
2296
|
}
|
2202
|
-
|
2297
|
+
);
|
2203
2298
|
}
|
2204
2299
|
function isZod4Schema(zodSchema2) {
|
2205
2300
|
return "_zod" in zodSchema2;
|
@@ -2212,30 +2307,6 @@ function zodSchema(zodSchema2, options) {
|
|
2212
2307
|
}
|
2213
2308
|
}
|
2214
2309
|
|
2215
|
-
// src/schema.ts
|
2216
|
-
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
2217
|
-
function jsonSchema(jsonSchema2, {
|
2218
|
-
validate
|
2219
|
-
} = {}) {
|
2220
|
-
return {
|
2221
|
-
[schemaSymbol]: true,
|
2222
|
-
_type: void 0,
|
2223
|
-
// should never be used directly
|
2224
|
-
[validatorSymbol]: true,
|
2225
|
-
jsonSchema: jsonSchema2,
|
2226
|
-
validate
|
2227
|
-
};
|
2228
|
-
}
|
2229
|
-
function isSchema(value) {
|
2230
|
-
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
2231
|
-
}
|
2232
|
-
function asSchema(schema) {
|
2233
|
-
return schema == null ? jsonSchema({
|
2234
|
-
properties: {},
|
2235
|
-
additionalProperties: false
|
2236
|
-
}) : isSchema(schema) ? schema : zodSchema(schema);
|
2237
|
-
}
|
2238
|
-
|
2239
2310
|
// src/uint8-utils.ts
|
2240
2311
|
var { btoa, atob } = globalThis;
|
2241
2312
|
function convertBase64ToUint8Array(base64String) {
|
@@ -2321,6 +2392,8 @@ export {
|
|
2321
2392
|
isUrlSupported,
|
2322
2393
|
isValidator,
|
2323
2394
|
jsonSchema,
|
2395
|
+
lazySchema,
|
2396
|
+
lazyValidator,
|
2324
2397
|
loadApiKey,
|
2325
2398
|
loadOptionalSetting,
|
2326
2399
|
loadSetting,
|
@@ -2339,7 +2412,6 @@ export {
|
|
2339
2412
|
tool,
|
2340
2413
|
validateTypes,
|
2341
2414
|
validator,
|
2342
|
-
validatorSymbol,
|
2343
2415
|
withUserAgentSuffix,
|
2344
2416
|
withoutTrailingSlash,
|
2345
2417
|
zodSchema
|