@mastra/schema-compat 0.0.0-vector-query-tool-provider-options-20250828222356 → 0.0.0-vnext-20251104230439
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 +70 -1
- package/dist/chunk-5WM4A32G.cjs +83 -0
- package/dist/chunk-5WM4A32G.cjs.map +1 -0
- package/dist/chunk-U2HXWNAF.js +77 -0
- package/dist/chunk-U2HXWNAF.js.map +1 -0
- package/dist/index.cjs +3652 -122
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3652 -123
- package/dist/index.js.map +1 -1
- package/dist/json-schema.d.ts +4 -0
- package/dist/json-schema.d.ts.map +1 -0
- package/dist/provider-compats/anthropic.d.ts +2 -2
- package/dist/provider-compats/deepseek.d.ts +2 -2
- package/dist/provider-compats/google.d.ts +2 -2
- package/dist/provider-compats/meta.d.ts +2 -2
- package/dist/provider-compats/openai-reasoning.d.ts +2 -2
- package/dist/provider-compats/openai-reasoning.d.ts.map +1 -1
- package/dist/provider-compats/openai.d.ts +2 -2
- package/dist/schema-compatibility-v3.d.ts +5 -37
- package/dist/schema-compatibility-v3.d.ts.map +1 -1
- package/dist/schema-compatibility-v4.d.ts +5 -37
- package/dist/schema-compatibility-v4.d.ts.map +1 -1
- package/dist/schema-compatibility.d.ts +6 -38
- package/dist/schema-compatibility.d.ts.map +1 -1
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils-test-suite.d.ts.map +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/zod-to-json-test-suite.d.ts +6 -0
- package/dist/zod-to-json-test-suite.d.ts.map +1 -0
- package/dist/zod-to-json.cjs +2 -2
- package/dist/zod-to-json.d.ts +1 -1
- package/dist/zod-to-json.d.ts.map +1 -1
- package/dist/zod-to-json.js +1 -1
- package/package.json +20 -7
- package/.turbo/turbo-build.log +0 -4
- package/dist/chunk-MKYBUMTK.js +0 -27
- package/dist/chunk-MKYBUMTK.js.map +0 -1
- package/dist/chunk-V7Y3FXBJ.cjs +0 -33
- package/dist/chunk-V7Y3FXBJ.cjs.map +0 -1
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -40
- package/src/provider-compats/anthropic.ts +0 -61
- package/src/provider-compats/deepseek.ts +0 -40
- package/src/provider-compats/google.ts +0 -49
- package/src/provider-compats/meta.ts +0 -41
- package/src/provider-compats/openai-reasoning.ts +0 -85
- package/src/provider-compats/openai.ts +0 -65
- package/src/provider-compats.test.ts +0 -407
- package/src/schema-compatibility-v3.ts +0 -664
- package/src/schema-compatibility-v4.test.ts +0 -476
- package/src/schema-compatibility-v4.ts +0 -706
- package/src/schema-compatibility.test.ts +0 -471
- package/src/schema-compatibility.ts +0 -471
- package/src/types.ts +0 -5
- package/src/utils-test-suite.ts +0 -467
- package/src/utils-v3.test.ts +0 -9
- package/src/utils-v4.test.ts +0 -9
- package/src/utils.ts +0 -211
- package/src/zod-to-json.ts +0 -28
- package/src/zodTypes.ts +0 -56
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -22
- package/vitest.config.ts +0 -7
package/dist/index.js
CHANGED
|
@@ -1,15 +1,3575 @@
|
|
|
1
|
-
import { zodToJsonSchema } from './chunk-
|
|
2
|
-
import { z, ZodOptional, ZodObject, ZodArray, ZodUnion, ZodString, ZodNumber, ZodNull, ZodDate, ZodDefault } from 'zod';
|
|
3
|
-
import { jsonSchema } from 'ai';
|
|
1
|
+
import { zodToJsonSchema } from './chunk-U2HXWNAF.js';
|
|
2
|
+
import { z, ZodOptional, ZodObject, ZodArray, ZodUnion, ZodString, ZodNumber, ZodNull, ZodDate, ZodDefault, ZodFirstPartyTypeKind } from 'zod';
|
|
4
3
|
import { convertJsonSchemaToZod } from 'zod-from-json-schema';
|
|
5
4
|
import { convertJsonSchemaToZod as convertJsonSchemaToZod$1 } from 'zod-from-json-schema-v3';
|
|
6
5
|
import { ZodOptional as ZodOptional$1, ZodObject as ZodObject$1, ZodNull as ZodNull$1, ZodArray as ZodArray$1, ZodUnion as ZodUnion$1, ZodString as ZodString$1, ZodNumber as ZodNumber$1, ZodDate as ZodDate$1, ZodDefault as ZodDefault$1, z as z$1 } from 'zod/v4';
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
// ../_ai-sdk-v4/dist/chunk-EDUQOWHB.js
|
|
8
|
+
var __create = Object.create;
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
11
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
15
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
26
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
27
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
28
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
29
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
30
|
+
__defProp(target, "default", { value: mod, enumerable: true }),
|
|
31
|
+
mod
|
|
32
|
+
));
|
|
33
|
+
var require_secure_json_parse = __commonJS({
|
|
34
|
+
"../../node_modules/.pnpm/secure-json-parse@2.7.0/node_modules/secure-json-parse/index.js"(exports, module) {
|
|
35
|
+
var hasBuffer = typeof Buffer !== "undefined";
|
|
36
|
+
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
37
|
+
var suspectConstructorRx = /"(?:c|\\u0063)(?:o|\\u006[Ff])(?:n|\\u006[Ee])(?:s|\\u0073)(?:t|\\u0074)(?:r|\\u0072)(?:u|\\u0075)(?:c|\\u0063)(?:t|\\u0074)(?:o|\\u006[Ff])(?:r|\\u0072)"\s*:/;
|
|
38
|
+
function _parse(text2, reviver, options) {
|
|
39
|
+
if (options == null) {
|
|
40
|
+
if (reviver !== null && typeof reviver === "object") {
|
|
41
|
+
options = reviver;
|
|
42
|
+
reviver = void 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (hasBuffer && Buffer.isBuffer(text2)) {
|
|
46
|
+
text2 = text2.toString();
|
|
47
|
+
}
|
|
48
|
+
if (text2 && text2.charCodeAt(0) === 65279) {
|
|
49
|
+
text2 = text2.slice(1);
|
|
50
|
+
}
|
|
51
|
+
const obj = JSON.parse(text2, reviver);
|
|
52
|
+
if (obj === null || typeof obj !== "object") {
|
|
53
|
+
return obj;
|
|
54
|
+
}
|
|
55
|
+
const protoAction = options && options.protoAction || "error";
|
|
56
|
+
const constructorAction = options && options.constructorAction || "error";
|
|
57
|
+
if (protoAction === "ignore" && constructorAction === "ignore") {
|
|
58
|
+
return obj;
|
|
59
|
+
}
|
|
60
|
+
if (protoAction !== "ignore" && constructorAction !== "ignore") {
|
|
61
|
+
if (suspectProtoRx.test(text2) === false && suspectConstructorRx.test(text2) === false) {
|
|
62
|
+
return obj;
|
|
63
|
+
}
|
|
64
|
+
} else if (protoAction !== "ignore" && constructorAction === "ignore") {
|
|
65
|
+
if (suspectProtoRx.test(text2) === false) {
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
if (suspectConstructorRx.test(text2) === false) {
|
|
70
|
+
return obj;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return filter(obj, { protoAction, constructorAction, safe: options && options.safe });
|
|
74
|
+
}
|
|
75
|
+
function filter(obj, { protoAction = "error", constructorAction = "error", safe } = {}) {
|
|
76
|
+
let next = [obj];
|
|
77
|
+
while (next.length) {
|
|
78
|
+
const nodes = next;
|
|
79
|
+
next = [];
|
|
80
|
+
for (const node of nodes) {
|
|
81
|
+
if (protoAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
82
|
+
if (safe === true) {
|
|
83
|
+
return null;
|
|
84
|
+
} else if (protoAction === "error") {
|
|
85
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
86
|
+
}
|
|
87
|
+
delete node.__proto__;
|
|
88
|
+
}
|
|
89
|
+
if (constructorAction !== "ignore" && Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
90
|
+
if (safe === true) {
|
|
91
|
+
return null;
|
|
92
|
+
} else if (constructorAction === "error") {
|
|
93
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
94
|
+
}
|
|
95
|
+
delete node.constructor;
|
|
96
|
+
}
|
|
97
|
+
for (const key in node) {
|
|
98
|
+
const value = node[key];
|
|
99
|
+
if (value && typeof value === "object") {
|
|
100
|
+
next.push(value);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return obj;
|
|
106
|
+
}
|
|
107
|
+
function parse(text2, reviver, options) {
|
|
108
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
109
|
+
Error.stackTraceLimit = 0;
|
|
110
|
+
try {
|
|
111
|
+
return _parse(text2, reviver, options);
|
|
112
|
+
} finally {
|
|
113
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
function safeParse(text2, reviver) {
|
|
117
|
+
const stackTraceLimit = Error.stackTraceLimit;
|
|
118
|
+
Error.stackTraceLimit = 0;
|
|
119
|
+
try {
|
|
120
|
+
return _parse(text2, reviver, { safe: true });
|
|
121
|
+
} catch (_e) {
|
|
122
|
+
return null;
|
|
123
|
+
} finally {
|
|
124
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
module.exports = parse;
|
|
128
|
+
module.exports.default = parse;
|
|
129
|
+
module.exports.parse = parse;
|
|
130
|
+
module.exports.safeParse = safeParse;
|
|
131
|
+
module.exports.scan = filter;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
var marker = "vercel.ai.error";
|
|
135
|
+
var symbol = Symbol.for(marker);
|
|
136
|
+
var _a;
|
|
137
|
+
var _AISDKError = class _AISDKError2 extends Error {
|
|
138
|
+
/**
|
|
139
|
+
* Creates an AI SDK Error.
|
|
140
|
+
*
|
|
141
|
+
* @param {Object} params - The parameters for creating the error.
|
|
142
|
+
* @param {string} params.name - The name of the error.
|
|
143
|
+
* @param {string} params.message - The error message.
|
|
144
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
145
|
+
*/
|
|
146
|
+
constructor({
|
|
147
|
+
name: name142,
|
|
148
|
+
message,
|
|
149
|
+
cause
|
|
150
|
+
}) {
|
|
151
|
+
super(message);
|
|
152
|
+
this[_a] = true;
|
|
153
|
+
this.name = name142;
|
|
154
|
+
this.cause = cause;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Checks if the given error is an AI SDK Error.
|
|
158
|
+
* @param {unknown} error - The error to check.
|
|
159
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
160
|
+
*/
|
|
161
|
+
static isInstance(error) {
|
|
162
|
+
return _AISDKError2.hasMarker(error, marker);
|
|
163
|
+
}
|
|
164
|
+
static hasMarker(error, marker15) {
|
|
165
|
+
const markerSymbol = Symbol.for(marker15);
|
|
166
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
_a = symbol;
|
|
170
|
+
var AISDKError = _AISDKError;
|
|
171
|
+
function getErrorMessage(error) {
|
|
172
|
+
if (error == null) {
|
|
173
|
+
return "unknown error";
|
|
174
|
+
}
|
|
175
|
+
if (typeof error === "string") {
|
|
176
|
+
return error;
|
|
177
|
+
}
|
|
178
|
+
if (error instanceof Error) {
|
|
179
|
+
return error.message;
|
|
180
|
+
}
|
|
181
|
+
return JSON.stringify(error);
|
|
182
|
+
}
|
|
183
|
+
var name3 = "AI_InvalidArgumentError";
|
|
184
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
185
|
+
var symbol4 = Symbol.for(marker4);
|
|
186
|
+
var _a4;
|
|
187
|
+
var InvalidArgumentError = class extends AISDKError {
|
|
188
|
+
constructor({
|
|
189
|
+
message,
|
|
190
|
+
cause,
|
|
191
|
+
argument
|
|
192
|
+
}) {
|
|
193
|
+
super({ name: name3, message, cause });
|
|
194
|
+
this[_a4] = true;
|
|
195
|
+
this.argument = argument;
|
|
196
|
+
}
|
|
197
|
+
static isInstance(error) {
|
|
198
|
+
return AISDKError.hasMarker(error, marker4);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
_a4 = symbol4;
|
|
202
|
+
var name6 = "AI_JSONParseError";
|
|
203
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
204
|
+
var symbol7 = Symbol.for(marker7);
|
|
205
|
+
var _a7;
|
|
206
|
+
var JSONParseError = class extends AISDKError {
|
|
207
|
+
constructor({ text: text2, cause }) {
|
|
208
|
+
super({
|
|
209
|
+
name: name6,
|
|
210
|
+
message: `JSON parsing failed: Text: ${text2}.
|
|
211
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
212
|
+
cause
|
|
213
|
+
});
|
|
214
|
+
this[_a7] = true;
|
|
215
|
+
this.text = text2;
|
|
216
|
+
}
|
|
217
|
+
static isInstance(error) {
|
|
218
|
+
return AISDKError.hasMarker(error, marker7);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
_a7 = symbol7;
|
|
222
|
+
var name12 = "AI_TypeValidationError";
|
|
223
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
224
|
+
var symbol13 = Symbol.for(marker13);
|
|
225
|
+
var _a13;
|
|
226
|
+
var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
|
|
227
|
+
constructor({ value, cause }) {
|
|
228
|
+
super({
|
|
229
|
+
name: name12,
|
|
230
|
+
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
231
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
232
|
+
cause
|
|
233
|
+
});
|
|
234
|
+
this[_a13] = true;
|
|
235
|
+
this.value = value;
|
|
236
|
+
}
|
|
237
|
+
static isInstance(error) {
|
|
238
|
+
return AISDKError.hasMarker(error, marker13);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Wraps an error into a TypeValidationError.
|
|
242
|
+
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
243
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
244
|
+
*
|
|
245
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
246
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
247
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
248
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
249
|
+
*/
|
|
250
|
+
static wrap({
|
|
251
|
+
value,
|
|
252
|
+
cause
|
|
253
|
+
}) {
|
|
254
|
+
return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
_a13 = symbol13;
|
|
258
|
+
var TypeValidationError = _TypeValidationError;
|
|
259
|
+
var customAlphabet = (alphabet, defaultSize = 21) => {
|
|
260
|
+
return (size = defaultSize) => {
|
|
261
|
+
let id = "";
|
|
262
|
+
let i = size | 0;
|
|
263
|
+
while (i--) {
|
|
264
|
+
id += alphabet[Math.random() * alphabet.length | 0];
|
|
265
|
+
}
|
|
266
|
+
return id;
|
|
267
|
+
};
|
|
268
|
+
};
|
|
269
|
+
var import_secure_json_parse = __toESM(require_secure_json_parse());
|
|
270
|
+
function convertAsyncIteratorToReadableStream(iterator) {
|
|
271
|
+
return new ReadableStream({
|
|
272
|
+
/**
|
|
273
|
+
* Called when the consumer wants to pull more data from the stream.
|
|
274
|
+
*
|
|
275
|
+
* @param {ReadableStreamDefaultController<T>} controller - The controller to enqueue data into the stream.
|
|
276
|
+
* @returns {Promise<void>}
|
|
277
|
+
*/
|
|
278
|
+
async pull(controller) {
|
|
279
|
+
try {
|
|
280
|
+
const { value, done } = await iterator.next();
|
|
281
|
+
if (done) {
|
|
282
|
+
controller.close();
|
|
283
|
+
} else {
|
|
284
|
+
controller.enqueue(value);
|
|
285
|
+
}
|
|
286
|
+
} catch (error) {
|
|
287
|
+
controller.error(error);
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
/**
|
|
291
|
+
* Called when the consumer cancels the stream.
|
|
292
|
+
*/
|
|
293
|
+
cancel() {
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
var createIdGenerator = ({
|
|
298
|
+
prefix,
|
|
299
|
+
size: defaultSize = 16,
|
|
300
|
+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
301
|
+
separator = "-"
|
|
302
|
+
} = {}) => {
|
|
303
|
+
const generator = customAlphabet(alphabet, defaultSize);
|
|
304
|
+
if (prefix == null) {
|
|
305
|
+
return generator;
|
|
306
|
+
}
|
|
307
|
+
if (alphabet.includes(separator)) {
|
|
308
|
+
throw new InvalidArgumentError({
|
|
309
|
+
argument: "separator",
|
|
310
|
+
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
return (size) => `${prefix}${separator}${generator(size)}`;
|
|
314
|
+
};
|
|
315
|
+
createIdGenerator();
|
|
316
|
+
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
317
|
+
function validator(validate) {
|
|
318
|
+
return { [validatorSymbol]: true, validate };
|
|
319
|
+
}
|
|
320
|
+
function isValidator(value) {
|
|
321
|
+
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
322
|
+
}
|
|
323
|
+
function asValidator(value) {
|
|
324
|
+
return isValidator(value) ? value : zodValidator(value);
|
|
325
|
+
}
|
|
326
|
+
function zodValidator(zodSchema2) {
|
|
327
|
+
return validator((value) => {
|
|
328
|
+
const result = zodSchema2.safeParse(value);
|
|
329
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
function safeValidateTypes({
|
|
333
|
+
value,
|
|
334
|
+
schema
|
|
335
|
+
}) {
|
|
336
|
+
const validator2 = asValidator(schema);
|
|
337
|
+
try {
|
|
338
|
+
if (validator2.validate == null) {
|
|
339
|
+
return { success: true, value };
|
|
340
|
+
}
|
|
341
|
+
const result = validator2.validate(value);
|
|
342
|
+
if (result.success) {
|
|
343
|
+
return result;
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
success: false,
|
|
347
|
+
error: TypeValidationError.wrap({ value, cause: result.error })
|
|
348
|
+
};
|
|
349
|
+
} catch (error) {
|
|
350
|
+
return {
|
|
351
|
+
success: false,
|
|
352
|
+
error: TypeValidationError.wrap({ value, cause: error })
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function safeParseJSON({
|
|
357
|
+
text: text2,
|
|
358
|
+
schema
|
|
359
|
+
}) {
|
|
360
|
+
try {
|
|
361
|
+
const value = import_secure_json_parse.default.parse(text2);
|
|
362
|
+
if (schema == null) {
|
|
363
|
+
return { success: true, value, rawValue: value };
|
|
364
|
+
}
|
|
365
|
+
const validationResult = safeValidateTypes({ value, schema });
|
|
366
|
+
return validationResult.success ? { ...validationResult, rawValue: value } : validationResult;
|
|
367
|
+
} catch (error) {
|
|
368
|
+
return {
|
|
369
|
+
success: false,
|
|
370
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text: text2, cause: error })
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
var ignoreOverride = Symbol("Let zodToJsonSchema decide on which parser to use");
|
|
375
|
+
var defaultOptions = {
|
|
376
|
+
name: void 0,
|
|
377
|
+
$refStrategy: "root",
|
|
378
|
+
basePath: ["#"],
|
|
379
|
+
effectStrategy: "input",
|
|
380
|
+
pipeStrategy: "all",
|
|
381
|
+
dateStrategy: "format:date-time",
|
|
382
|
+
mapStrategy: "entries",
|
|
383
|
+
removeAdditionalStrategy: "passthrough",
|
|
384
|
+
allowedAdditionalProperties: true,
|
|
385
|
+
rejectedAdditionalProperties: false,
|
|
386
|
+
definitionPath: "definitions",
|
|
387
|
+
target: "jsonSchema7",
|
|
388
|
+
strictUnions: false,
|
|
389
|
+
definitions: {},
|
|
390
|
+
errorMessages: false,
|
|
391
|
+
markdownDescription: false,
|
|
392
|
+
patternStrategy: "escape",
|
|
393
|
+
applyRegexFlags: false,
|
|
394
|
+
emailStrategy: "format:email",
|
|
395
|
+
base64Strategy: "contentEncoding:base64",
|
|
396
|
+
nameStrategy: "ref",
|
|
397
|
+
openAiAnyTypeName: "OpenAiAnyType"
|
|
398
|
+
};
|
|
399
|
+
var getDefaultOptions = (options) => typeof options === "string" ? {
|
|
400
|
+
...defaultOptions,
|
|
401
|
+
name: options
|
|
402
|
+
} : {
|
|
403
|
+
...defaultOptions,
|
|
404
|
+
...options
|
|
405
|
+
};
|
|
406
|
+
var getRefs = (options) => {
|
|
407
|
+
const _options = getDefaultOptions(options);
|
|
408
|
+
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
|
409
|
+
return {
|
|
410
|
+
..._options,
|
|
411
|
+
flags: { hasReferencedOpenAiAnyType: false },
|
|
412
|
+
currentPath,
|
|
413
|
+
propertyPath: void 0,
|
|
414
|
+
seen: new Map(Object.entries(_options.definitions).map(([name17, def]) => [
|
|
415
|
+
def._def,
|
|
416
|
+
{
|
|
417
|
+
def: def._def,
|
|
418
|
+
path: [..._options.basePath, _options.definitionPath, name17],
|
|
419
|
+
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
|
420
|
+
jsonSchema: void 0
|
|
421
|
+
}
|
|
422
|
+
]))
|
|
423
|
+
};
|
|
424
|
+
};
|
|
425
|
+
function addErrorMessage(res, key, errorMessage, refs) {
|
|
426
|
+
if (!refs?.errorMessages)
|
|
427
|
+
return;
|
|
428
|
+
if (errorMessage) {
|
|
429
|
+
res.errorMessage = {
|
|
430
|
+
...res.errorMessage,
|
|
431
|
+
[key]: errorMessage
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
function setResponseValueAndErrors(res, key, value, errorMessage, refs) {
|
|
436
|
+
res[key] = value;
|
|
437
|
+
addErrorMessage(res, key, errorMessage, refs);
|
|
438
|
+
}
|
|
439
|
+
var getRelativePath = (pathA, pathB) => {
|
|
440
|
+
let i = 0;
|
|
441
|
+
for (; i < pathA.length && i < pathB.length; i++) {
|
|
442
|
+
if (pathA[i] !== pathB[i])
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
|
|
446
|
+
};
|
|
447
|
+
function parseAnyDef(refs) {
|
|
448
|
+
if (refs.target !== "openAi") {
|
|
449
|
+
return {};
|
|
450
|
+
}
|
|
451
|
+
const anyDefinitionPath = [
|
|
452
|
+
...refs.basePath,
|
|
453
|
+
refs.definitionPath,
|
|
454
|
+
refs.openAiAnyTypeName
|
|
455
|
+
];
|
|
456
|
+
refs.flags.hasReferencedOpenAiAnyType = true;
|
|
457
|
+
return {
|
|
458
|
+
$ref: refs.$refStrategy === "relative" ? getRelativePath(anyDefinitionPath, refs.currentPath) : anyDefinitionPath.join("/")
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
function parseArrayDef(def, refs) {
|
|
462
|
+
const res = {
|
|
463
|
+
type: "array"
|
|
464
|
+
};
|
|
465
|
+
if (def.type?._def && def.type?._def?.typeName !== ZodFirstPartyTypeKind.ZodAny) {
|
|
466
|
+
res.items = parseDef(def.type._def, {
|
|
467
|
+
...refs,
|
|
468
|
+
currentPath: [...refs.currentPath, "items"]
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (def.minLength) {
|
|
472
|
+
setResponseValueAndErrors(res, "minItems", def.minLength.value, def.minLength.message, refs);
|
|
473
|
+
}
|
|
474
|
+
if (def.maxLength) {
|
|
475
|
+
setResponseValueAndErrors(res, "maxItems", def.maxLength.value, def.maxLength.message, refs);
|
|
476
|
+
}
|
|
477
|
+
if (def.exactLength) {
|
|
478
|
+
setResponseValueAndErrors(res, "minItems", def.exactLength.value, def.exactLength.message, refs);
|
|
479
|
+
setResponseValueAndErrors(res, "maxItems", def.exactLength.value, def.exactLength.message, refs);
|
|
480
|
+
}
|
|
481
|
+
return res;
|
|
482
|
+
}
|
|
483
|
+
function parseBigintDef(def, refs) {
|
|
484
|
+
const res = {
|
|
485
|
+
type: "integer",
|
|
486
|
+
format: "int64"
|
|
487
|
+
};
|
|
488
|
+
if (!def.checks)
|
|
489
|
+
return res;
|
|
490
|
+
for (const check of def.checks) {
|
|
491
|
+
switch (check.kind) {
|
|
492
|
+
case "min":
|
|
493
|
+
if (refs.target === "jsonSchema7") {
|
|
494
|
+
if (check.inclusive) {
|
|
495
|
+
setResponseValueAndErrors(res, "minimum", check.value, check.message, refs);
|
|
496
|
+
} else {
|
|
497
|
+
setResponseValueAndErrors(res, "exclusiveMinimum", check.value, check.message, refs);
|
|
498
|
+
}
|
|
499
|
+
} else {
|
|
500
|
+
if (!check.inclusive) {
|
|
501
|
+
res.exclusiveMinimum = true;
|
|
502
|
+
}
|
|
503
|
+
setResponseValueAndErrors(res, "minimum", check.value, check.message, refs);
|
|
504
|
+
}
|
|
505
|
+
break;
|
|
506
|
+
case "max":
|
|
507
|
+
if (refs.target === "jsonSchema7") {
|
|
508
|
+
if (check.inclusive) {
|
|
509
|
+
setResponseValueAndErrors(res, "maximum", check.value, check.message, refs);
|
|
510
|
+
} else {
|
|
511
|
+
setResponseValueAndErrors(res, "exclusiveMaximum", check.value, check.message, refs);
|
|
512
|
+
}
|
|
513
|
+
} else {
|
|
514
|
+
if (!check.inclusive) {
|
|
515
|
+
res.exclusiveMaximum = true;
|
|
516
|
+
}
|
|
517
|
+
setResponseValueAndErrors(res, "maximum", check.value, check.message, refs);
|
|
518
|
+
}
|
|
519
|
+
break;
|
|
520
|
+
case "multipleOf":
|
|
521
|
+
setResponseValueAndErrors(res, "multipleOf", check.value, check.message, refs);
|
|
522
|
+
break;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return res;
|
|
526
|
+
}
|
|
527
|
+
function parseBooleanDef() {
|
|
528
|
+
return {
|
|
529
|
+
type: "boolean"
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function parseBrandedDef(_def, refs) {
|
|
533
|
+
return parseDef(_def.type._def, refs);
|
|
534
|
+
}
|
|
535
|
+
var parseCatchDef = (def, refs) => {
|
|
536
|
+
return parseDef(def.innerType._def, refs);
|
|
537
|
+
};
|
|
538
|
+
function parseDateDef(def, refs, overrideDateStrategy) {
|
|
539
|
+
const strategy = overrideDateStrategy ?? refs.dateStrategy;
|
|
540
|
+
if (Array.isArray(strategy)) {
|
|
541
|
+
return {
|
|
542
|
+
anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
switch (strategy) {
|
|
546
|
+
case "string":
|
|
547
|
+
case "format:date-time":
|
|
548
|
+
return {
|
|
549
|
+
type: "string",
|
|
550
|
+
format: "date-time"
|
|
551
|
+
};
|
|
552
|
+
case "format:date":
|
|
553
|
+
return {
|
|
554
|
+
type: "string",
|
|
555
|
+
format: "date"
|
|
556
|
+
};
|
|
557
|
+
case "integer":
|
|
558
|
+
return integerDateParser(def, refs);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
var integerDateParser = (def, refs) => {
|
|
562
|
+
const res = {
|
|
563
|
+
type: "integer",
|
|
564
|
+
format: "unix-time"
|
|
565
|
+
};
|
|
566
|
+
if (refs.target === "openApi3") {
|
|
567
|
+
return res;
|
|
568
|
+
}
|
|
569
|
+
for (const check of def.checks) {
|
|
570
|
+
switch (check.kind) {
|
|
571
|
+
case "min":
|
|
572
|
+
setResponseValueAndErrors(
|
|
573
|
+
res,
|
|
574
|
+
"minimum",
|
|
575
|
+
check.value,
|
|
576
|
+
// This is in milliseconds
|
|
577
|
+
check.message,
|
|
578
|
+
refs
|
|
579
|
+
);
|
|
580
|
+
break;
|
|
581
|
+
case "max":
|
|
582
|
+
setResponseValueAndErrors(
|
|
583
|
+
res,
|
|
584
|
+
"maximum",
|
|
585
|
+
check.value,
|
|
586
|
+
// This is in milliseconds
|
|
587
|
+
check.message,
|
|
588
|
+
refs
|
|
589
|
+
);
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
return res;
|
|
594
|
+
};
|
|
595
|
+
function parseDefaultDef(_def, refs) {
|
|
596
|
+
return {
|
|
597
|
+
...parseDef(_def.innerType._def, refs),
|
|
598
|
+
default: _def.defaultValue()
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function parseEffectsDef(_def, refs) {
|
|
602
|
+
return refs.effectStrategy === "input" ? parseDef(_def.schema._def, refs) : parseAnyDef(refs);
|
|
603
|
+
}
|
|
604
|
+
function parseEnumDef(def) {
|
|
605
|
+
return {
|
|
606
|
+
type: "string",
|
|
607
|
+
enum: Array.from(def.values)
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
var isJsonSchema7AllOfType = (type) => {
|
|
611
|
+
if ("type" in type && type.type === "string")
|
|
612
|
+
return false;
|
|
613
|
+
return "allOf" in type;
|
|
614
|
+
};
|
|
615
|
+
function parseIntersectionDef(def, refs) {
|
|
616
|
+
const allOf = [
|
|
617
|
+
parseDef(def.left._def, {
|
|
618
|
+
...refs,
|
|
619
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
620
|
+
}),
|
|
621
|
+
parseDef(def.right._def, {
|
|
622
|
+
...refs,
|
|
623
|
+
currentPath: [...refs.currentPath, "allOf", "1"]
|
|
624
|
+
})
|
|
625
|
+
].filter((x) => !!x);
|
|
626
|
+
let unevaluatedProperties = refs.target === "jsonSchema2019-09" ? { unevaluatedProperties: false } : void 0;
|
|
627
|
+
const mergedAllOf = [];
|
|
628
|
+
allOf.forEach((schema) => {
|
|
629
|
+
if (isJsonSchema7AllOfType(schema)) {
|
|
630
|
+
mergedAllOf.push(...schema.allOf);
|
|
631
|
+
if (schema.unevaluatedProperties === void 0) {
|
|
632
|
+
unevaluatedProperties = void 0;
|
|
633
|
+
}
|
|
634
|
+
} else {
|
|
635
|
+
let nestedSchema = schema;
|
|
636
|
+
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
|
637
|
+
const { additionalProperties, ...rest } = schema;
|
|
638
|
+
nestedSchema = rest;
|
|
639
|
+
} else {
|
|
640
|
+
unevaluatedProperties = void 0;
|
|
641
|
+
}
|
|
642
|
+
mergedAllOf.push(nestedSchema);
|
|
643
|
+
}
|
|
644
|
+
});
|
|
645
|
+
return mergedAllOf.length ? {
|
|
646
|
+
allOf: mergedAllOf,
|
|
647
|
+
...unevaluatedProperties
|
|
648
|
+
} : void 0;
|
|
649
|
+
}
|
|
650
|
+
function parseLiteralDef(def, refs) {
|
|
651
|
+
const parsedType = typeof def.value;
|
|
652
|
+
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
|
653
|
+
return {
|
|
654
|
+
type: Array.isArray(def.value) ? "array" : "object"
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
if (refs.target === "openApi3") {
|
|
658
|
+
return {
|
|
659
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
|
660
|
+
enum: [def.value]
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
return {
|
|
664
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
|
665
|
+
const: def.value
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
var emojiRegex = void 0;
|
|
669
|
+
var zodPatterns = {
|
|
670
|
+
/**
|
|
671
|
+
* `c` was changed to `[cC]` to replicate /i flag
|
|
672
|
+
*/
|
|
673
|
+
cuid: /^[cC][^\s-]{8,}$/,
|
|
674
|
+
cuid2: /^[0-9a-z]+$/,
|
|
675
|
+
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
676
|
+
/**
|
|
677
|
+
* `a-z` was added to replicate /i flag
|
|
678
|
+
*/
|
|
679
|
+
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
|
680
|
+
/**
|
|
681
|
+
* Constructed a valid Unicode RegExp
|
|
682
|
+
*
|
|
683
|
+
* Lazily instantiate since this type of regex isn't supported
|
|
684
|
+
* in all envs (e.g. React Native).
|
|
685
|
+
*
|
|
686
|
+
* See:
|
|
687
|
+
* https://github.com/colinhacks/zod/issues/2433
|
|
688
|
+
* Fix in Zod:
|
|
689
|
+
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
|
690
|
+
*/
|
|
691
|
+
emoji: () => {
|
|
692
|
+
if (emojiRegex === void 0) {
|
|
693
|
+
emojiRegex = RegExp("^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$", "u");
|
|
694
|
+
}
|
|
695
|
+
return emojiRegex;
|
|
696
|
+
},
|
|
697
|
+
/**
|
|
698
|
+
* Unused
|
|
699
|
+
*/
|
|
700
|
+
uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/,
|
|
701
|
+
/**
|
|
702
|
+
* Unused
|
|
703
|
+
*/
|
|
704
|
+
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
|
|
705
|
+
ipv4Cidr: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/,
|
|
706
|
+
/**
|
|
707
|
+
* Unused
|
|
708
|
+
*/
|
|
709
|
+
ipv6: /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/,
|
|
710
|
+
ipv6Cidr: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
711
|
+
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
|
712
|
+
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
|
713
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
714
|
+
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
|
715
|
+
};
|
|
716
|
+
function parseStringDef(def, refs) {
|
|
717
|
+
const res = {
|
|
718
|
+
type: "string"
|
|
719
|
+
};
|
|
720
|
+
if (def.checks) {
|
|
721
|
+
for (const check of def.checks) {
|
|
722
|
+
switch (check.kind) {
|
|
723
|
+
case "min":
|
|
724
|
+
setResponseValueAndErrors(res, "minLength", typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value, check.message, refs);
|
|
725
|
+
break;
|
|
726
|
+
case "max":
|
|
727
|
+
setResponseValueAndErrors(res, "maxLength", typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value, check.message, refs);
|
|
728
|
+
break;
|
|
729
|
+
case "email":
|
|
730
|
+
switch (refs.emailStrategy) {
|
|
731
|
+
case "format:email":
|
|
732
|
+
addFormat(res, "email", check.message, refs);
|
|
733
|
+
break;
|
|
734
|
+
case "format:idn-email":
|
|
735
|
+
addFormat(res, "idn-email", check.message, refs);
|
|
736
|
+
break;
|
|
737
|
+
case "pattern:zod":
|
|
738
|
+
addPattern(res, zodPatterns.email, check.message, refs);
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
break;
|
|
742
|
+
case "url":
|
|
743
|
+
addFormat(res, "uri", check.message, refs);
|
|
744
|
+
break;
|
|
745
|
+
case "uuid":
|
|
746
|
+
addFormat(res, "uuid", check.message, refs);
|
|
747
|
+
break;
|
|
748
|
+
case "regex":
|
|
749
|
+
addPattern(res, check.regex, check.message, refs);
|
|
750
|
+
break;
|
|
751
|
+
case "cuid":
|
|
752
|
+
addPattern(res, zodPatterns.cuid, check.message, refs);
|
|
753
|
+
break;
|
|
754
|
+
case "cuid2":
|
|
755
|
+
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
|
756
|
+
break;
|
|
757
|
+
case "startsWith":
|
|
758
|
+
addPattern(res, RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`), check.message, refs);
|
|
759
|
+
break;
|
|
760
|
+
case "endsWith":
|
|
761
|
+
addPattern(res, RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`), check.message, refs);
|
|
762
|
+
break;
|
|
763
|
+
case "datetime":
|
|
764
|
+
addFormat(res, "date-time", check.message, refs);
|
|
765
|
+
break;
|
|
766
|
+
case "date":
|
|
767
|
+
addFormat(res, "date", check.message, refs);
|
|
768
|
+
break;
|
|
769
|
+
case "time":
|
|
770
|
+
addFormat(res, "time", check.message, refs);
|
|
771
|
+
break;
|
|
772
|
+
case "duration":
|
|
773
|
+
addFormat(res, "duration", check.message, refs);
|
|
774
|
+
break;
|
|
775
|
+
case "length":
|
|
776
|
+
setResponseValueAndErrors(res, "minLength", typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value, check.message, refs);
|
|
777
|
+
setResponseValueAndErrors(res, "maxLength", typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value, check.message, refs);
|
|
778
|
+
break;
|
|
779
|
+
case "includes": {
|
|
780
|
+
addPattern(res, RegExp(escapeLiteralCheckValue(check.value, refs)), check.message, refs);
|
|
781
|
+
break;
|
|
782
|
+
}
|
|
783
|
+
case "ip": {
|
|
784
|
+
if (check.version !== "v6") {
|
|
785
|
+
addFormat(res, "ipv4", check.message, refs);
|
|
786
|
+
}
|
|
787
|
+
if (check.version !== "v4") {
|
|
788
|
+
addFormat(res, "ipv6", check.message, refs);
|
|
789
|
+
}
|
|
790
|
+
break;
|
|
791
|
+
}
|
|
792
|
+
case "base64url":
|
|
793
|
+
addPattern(res, zodPatterns.base64url, check.message, refs);
|
|
794
|
+
break;
|
|
795
|
+
case "jwt":
|
|
796
|
+
addPattern(res, zodPatterns.jwt, check.message, refs);
|
|
797
|
+
break;
|
|
798
|
+
case "cidr": {
|
|
799
|
+
if (check.version !== "v6") {
|
|
800
|
+
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
|
801
|
+
}
|
|
802
|
+
if (check.version !== "v4") {
|
|
803
|
+
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
|
804
|
+
}
|
|
805
|
+
break;
|
|
806
|
+
}
|
|
807
|
+
case "emoji":
|
|
808
|
+
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
|
809
|
+
break;
|
|
810
|
+
case "ulid": {
|
|
811
|
+
addPattern(res, zodPatterns.ulid, check.message, refs);
|
|
812
|
+
break;
|
|
813
|
+
}
|
|
814
|
+
case "base64": {
|
|
815
|
+
switch (refs.base64Strategy) {
|
|
816
|
+
case "format:binary": {
|
|
817
|
+
addFormat(res, "binary", check.message, refs);
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
case "contentEncoding:base64": {
|
|
821
|
+
setResponseValueAndErrors(res, "contentEncoding", "base64", check.message, refs);
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
case "pattern:zod": {
|
|
825
|
+
addPattern(res, zodPatterns.base64, check.message, refs);
|
|
826
|
+
break;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
case "nanoid": {
|
|
832
|
+
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
return res;
|
|
838
|
+
}
|
|
839
|
+
function escapeLiteralCheckValue(literal, refs) {
|
|
840
|
+
return refs.patternStrategy === "escape" ? escapeNonAlphaNumeric(literal) : literal;
|
|
841
|
+
}
|
|
842
|
+
var ALPHA_NUMERIC = new Set("ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789");
|
|
843
|
+
function escapeNonAlphaNumeric(source) {
|
|
844
|
+
let result = "";
|
|
845
|
+
for (let i = 0; i < source.length; i++) {
|
|
846
|
+
if (!ALPHA_NUMERIC.has(source[i])) {
|
|
847
|
+
result += "\\";
|
|
848
|
+
}
|
|
849
|
+
result += source[i];
|
|
850
|
+
}
|
|
851
|
+
return result;
|
|
852
|
+
}
|
|
853
|
+
function addFormat(schema, value, message, refs) {
|
|
854
|
+
if (schema.format || schema.anyOf?.some((x) => x.format)) {
|
|
855
|
+
if (!schema.anyOf) {
|
|
856
|
+
schema.anyOf = [];
|
|
857
|
+
}
|
|
858
|
+
if (schema.format) {
|
|
859
|
+
schema.anyOf.push({
|
|
860
|
+
format: schema.format,
|
|
861
|
+
...schema.errorMessage && refs.errorMessages && {
|
|
862
|
+
errorMessage: { format: schema.errorMessage.format }
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
delete schema.format;
|
|
866
|
+
if (schema.errorMessage) {
|
|
867
|
+
delete schema.errorMessage.format;
|
|
868
|
+
if (Object.keys(schema.errorMessage).length === 0) {
|
|
869
|
+
delete schema.errorMessage;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
schema.anyOf.push({
|
|
874
|
+
format: value,
|
|
875
|
+
...message && refs.errorMessages && { errorMessage: { format: message } }
|
|
876
|
+
});
|
|
877
|
+
} else {
|
|
878
|
+
setResponseValueAndErrors(schema, "format", value, message, refs);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
function addPattern(schema, regex, message, refs) {
|
|
882
|
+
if (schema.pattern || schema.allOf?.some((x) => x.pattern)) {
|
|
883
|
+
if (!schema.allOf) {
|
|
884
|
+
schema.allOf = [];
|
|
885
|
+
}
|
|
886
|
+
if (schema.pattern) {
|
|
887
|
+
schema.allOf.push({
|
|
888
|
+
pattern: schema.pattern,
|
|
889
|
+
...schema.errorMessage && refs.errorMessages && {
|
|
890
|
+
errorMessage: { pattern: schema.errorMessage.pattern }
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
delete schema.pattern;
|
|
894
|
+
if (schema.errorMessage) {
|
|
895
|
+
delete schema.errorMessage.pattern;
|
|
896
|
+
if (Object.keys(schema.errorMessage).length === 0) {
|
|
897
|
+
delete schema.errorMessage;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
schema.allOf.push({
|
|
902
|
+
pattern: stringifyRegExpWithFlags(regex, refs),
|
|
903
|
+
...message && refs.errorMessages && { errorMessage: { pattern: message } }
|
|
904
|
+
});
|
|
905
|
+
} else {
|
|
906
|
+
setResponseValueAndErrors(schema, "pattern", stringifyRegExpWithFlags(regex, refs), message, refs);
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
function stringifyRegExpWithFlags(regex, refs) {
|
|
910
|
+
if (!refs.applyRegexFlags || !regex.flags) {
|
|
911
|
+
return regex.source;
|
|
912
|
+
}
|
|
913
|
+
const flags = {
|
|
914
|
+
i: regex.flags.includes("i"),
|
|
915
|
+
m: regex.flags.includes("m"),
|
|
916
|
+
s: regex.flags.includes("s")
|
|
917
|
+
// `.` matches newlines
|
|
918
|
+
};
|
|
919
|
+
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
|
920
|
+
let pattern = "";
|
|
921
|
+
let isEscaped = false;
|
|
922
|
+
let inCharGroup = false;
|
|
923
|
+
let inCharRange = false;
|
|
924
|
+
for (let i = 0; i < source.length; i++) {
|
|
925
|
+
if (isEscaped) {
|
|
926
|
+
pattern += source[i];
|
|
927
|
+
isEscaped = false;
|
|
928
|
+
continue;
|
|
929
|
+
}
|
|
930
|
+
if (flags.i) {
|
|
931
|
+
if (inCharGroup) {
|
|
932
|
+
if (source[i].match(/[a-z]/)) {
|
|
933
|
+
if (inCharRange) {
|
|
934
|
+
pattern += source[i];
|
|
935
|
+
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
|
936
|
+
inCharRange = false;
|
|
937
|
+
} else if (source[i + 1] === "-" && source[i + 2]?.match(/[a-z]/)) {
|
|
938
|
+
pattern += source[i];
|
|
939
|
+
inCharRange = true;
|
|
940
|
+
} else {
|
|
941
|
+
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
|
942
|
+
}
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
} else if (source[i].match(/[a-z]/)) {
|
|
946
|
+
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
|
947
|
+
continue;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
if (flags.m) {
|
|
951
|
+
if (source[i] === "^") {
|
|
952
|
+
pattern += `(^|(?<=[\r
|
|
953
|
+
]))`;
|
|
954
|
+
continue;
|
|
955
|
+
} else if (source[i] === "$") {
|
|
956
|
+
pattern += `($|(?=[\r
|
|
957
|
+
]))`;
|
|
958
|
+
continue;
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
if (flags.s && source[i] === ".") {
|
|
962
|
+
pattern += inCharGroup ? `${source[i]}\r
|
|
963
|
+
` : `[${source[i]}\r
|
|
964
|
+
]`;
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
pattern += source[i];
|
|
968
|
+
if (source[i] === "\\") {
|
|
969
|
+
isEscaped = true;
|
|
970
|
+
} else if (inCharGroup && source[i] === "]") {
|
|
971
|
+
inCharGroup = false;
|
|
972
|
+
} else if (!inCharGroup && source[i] === "[") {
|
|
973
|
+
inCharGroup = true;
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return pattern;
|
|
977
|
+
}
|
|
978
|
+
function parseRecordDef(def, refs) {
|
|
979
|
+
if (refs.target === "openAi") {
|
|
980
|
+
console.warn("Warning: OpenAI may not support records in schemas! Try an array of key-value pairs instead.");
|
|
981
|
+
}
|
|
982
|
+
if (refs.target === "openApi3" && def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodEnum) {
|
|
983
|
+
return {
|
|
984
|
+
type: "object",
|
|
985
|
+
required: def.keyType._def.values,
|
|
986
|
+
properties: def.keyType._def.values.reduce((acc, key) => ({
|
|
987
|
+
...acc,
|
|
988
|
+
[key]: parseDef(def.valueType._def, {
|
|
989
|
+
...refs,
|
|
990
|
+
currentPath: [...refs.currentPath, "properties", key]
|
|
991
|
+
}) ?? parseAnyDef(refs)
|
|
992
|
+
}), {}),
|
|
993
|
+
additionalProperties: refs.rejectedAdditionalProperties
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
const schema = {
|
|
997
|
+
type: "object",
|
|
998
|
+
additionalProperties: parseDef(def.valueType._def, {
|
|
999
|
+
...refs,
|
|
1000
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1001
|
+
}) ?? refs.allowedAdditionalProperties
|
|
1002
|
+
};
|
|
1003
|
+
if (refs.target === "openApi3") {
|
|
1004
|
+
return schema;
|
|
1005
|
+
}
|
|
1006
|
+
if (def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodString && def.keyType._def.checks?.length) {
|
|
1007
|
+
const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
|
1008
|
+
return {
|
|
1009
|
+
...schema,
|
|
1010
|
+
propertyNames: keyType
|
|
1011
|
+
};
|
|
1012
|
+
} else if (def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodEnum) {
|
|
1013
|
+
return {
|
|
1014
|
+
...schema,
|
|
1015
|
+
propertyNames: {
|
|
1016
|
+
enum: def.keyType._def.values
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
} else if (def.keyType?._def.typeName === ZodFirstPartyTypeKind.ZodBranded && def.keyType._def.type._def.typeName === ZodFirstPartyTypeKind.ZodString && def.keyType._def.type._def.checks?.length) {
|
|
1020
|
+
const { type, ...keyType } = parseBrandedDef(def.keyType._def, refs);
|
|
1021
|
+
return {
|
|
1022
|
+
...schema,
|
|
1023
|
+
propertyNames: keyType
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
1026
|
+
return schema;
|
|
1027
|
+
}
|
|
1028
|
+
function parseMapDef(def, refs) {
|
|
1029
|
+
if (refs.mapStrategy === "record") {
|
|
1030
|
+
return parseRecordDef(def, refs);
|
|
1031
|
+
}
|
|
1032
|
+
const keys = parseDef(def.keyType._def, {
|
|
1033
|
+
...refs,
|
|
1034
|
+
currentPath: [...refs.currentPath, "items", "items", "0"]
|
|
1035
|
+
}) || parseAnyDef(refs);
|
|
1036
|
+
const values = parseDef(def.valueType._def, {
|
|
1037
|
+
...refs,
|
|
1038
|
+
currentPath: [...refs.currentPath, "items", "items", "1"]
|
|
1039
|
+
}) || parseAnyDef(refs);
|
|
1040
|
+
return {
|
|
1041
|
+
type: "array",
|
|
1042
|
+
maxItems: 125,
|
|
1043
|
+
items: {
|
|
1044
|
+
type: "array",
|
|
1045
|
+
items: [keys, values],
|
|
1046
|
+
minItems: 2,
|
|
1047
|
+
maxItems: 2
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
function parseNativeEnumDef(def) {
|
|
1052
|
+
const object2 = def.values;
|
|
1053
|
+
const actualKeys = Object.keys(def.values).filter((key) => {
|
|
1054
|
+
return typeof object2[object2[key]] !== "number";
|
|
1055
|
+
});
|
|
1056
|
+
const actualValues = actualKeys.map((key) => object2[key]);
|
|
1057
|
+
const parsedTypes = Array.from(new Set(actualValues.map((values) => typeof values)));
|
|
1058
|
+
return {
|
|
1059
|
+
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
|
1060
|
+
enum: actualValues
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
function parseNeverDef(refs) {
|
|
1064
|
+
return refs.target === "openAi" ? void 0 : {
|
|
1065
|
+
not: parseAnyDef({
|
|
1066
|
+
...refs,
|
|
1067
|
+
currentPath: [...refs.currentPath, "not"]
|
|
1068
|
+
})
|
|
1069
|
+
};
|
|
1070
|
+
}
|
|
1071
|
+
function parseNullDef(refs) {
|
|
1072
|
+
return refs.target === "openApi3" ? {
|
|
1073
|
+
enum: ["null"],
|
|
1074
|
+
nullable: true
|
|
1075
|
+
} : {
|
|
1076
|
+
type: "null"
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
var primitiveMappings = {
|
|
1080
|
+
ZodString: "string",
|
|
1081
|
+
ZodNumber: "number",
|
|
1082
|
+
ZodBigInt: "integer",
|
|
1083
|
+
ZodBoolean: "boolean",
|
|
1084
|
+
ZodNull: "null"
|
|
1085
|
+
};
|
|
1086
|
+
function parseUnionDef(def, refs) {
|
|
1087
|
+
if (refs.target === "openApi3")
|
|
1088
|
+
return asAnyOf(def, refs);
|
|
1089
|
+
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
|
1090
|
+
if (options.every((x) => x._def.typeName in primitiveMappings && (!x._def.checks || !x._def.checks.length))) {
|
|
1091
|
+
const types = options.reduce((types2, x) => {
|
|
1092
|
+
const type = primitiveMappings[x._def.typeName];
|
|
1093
|
+
return type && !types2.includes(type) ? [...types2, type] : types2;
|
|
1094
|
+
}, []);
|
|
1095
|
+
return {
|
|
1096
|
+
type: types.length > 1 ? types : types[0]
|
|
1097
|
+
};
|
|
1098
|
+
} else if (options.every((x) => x._def.typeName === "ZodLiteral" && !x.description)) {
|
|
1099
|
+
const types = options.reduce((acc, x) => {
|
|
1100
|
+
const type = typeof x._def.value;
|
|
1101
|
+
switch (type) {
|
|
1102
|
+
case "string":
|
|
1103
|
+
case "number":
|
|
1104
|
+
case "boolean":
|
|
1105
|
+
return [...acc, type];
|
|
1106
|
+
case "bigint":
|
|
1107
|
+
return [...acc, "integer"];
|
|
1108
|
+
case "object":
|
|
1109
|
+
if (x._def.value === null)
|
|
1110
|
+
return [...acc, "null"];
|
|
1111
|
+
case "symbol":
|
|
1112
|
+
case "undefined":
|
|
1113
|
+
case "function":
|
|
1114
|
+
default:
|
|
1115
|
+
return acc;
|
|
1116
|
+
}
|
|
1117
|
+
}, []);
|
|
1118
|
+
if (types.length === options.length) {
|
|
1119
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
|
1120
|
+
return {
|
|
1121
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
|
1122
|
+
enum: options.reduce((acc, x) => {
|
|
1123
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
|
1124
|
+
}, [])
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
|
1128
|
+
return {
|
|
1129
|
+
type: "string",
|
|
1130
|
+
enum: options.reduce((acc, x) => [
|
|
1131
|
+
...acc,
|
|
1132
|
+
...x._def.values.filter((x2) => !acc.includes(x2))
|
|
1133
|
+
], [])
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
return asAnyOf(def, refs);
|
|
1137
|
+
}
|
|
1138
|
+
var asAnyOf = (def, refs) => {
|
|
1139
|
+
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options).map((x, i) => parseDef(x._def, {
|
|
1140
|
+
...refs,
|
|
1141
|
+
currentPath: [...refs.currentPath, "anyOf", `${i}`]
|
|
1142
|
+
})).filter((x) => !!x && (!refs.strictUnions || typeof x === "object" && Object.keys(x).length > 0));
|
|
1143
|
+
return anyOf.length ? { anyOf } : void 0;
|
|
1144
|
+
};
|
|
1145
|
+
function parseNullableDef(def, refs) {
|
|
1146
|
+
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(def.innerType._def.typeName) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
|
1147
|
+
if (refs.target === "openApi3") {
|
|
1148
|
+
return {
|
|
1149
|
+
type: primitiveMappings[def.innerType._def.typeName],
|
|
1150
|
+
nullable: true
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
return {
|
|
1154
|
+
type: [
|
|
1155
|
+
primitiveMappings[def.innerType._def.typeName],
|
|
1156
|
+
"null"
|
|
1157
|
+
]
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
if (refs.target === "openApi3") {
|
|
1161
|
+
const base2 = parseDef(def.innerType._def, {
|
|
1162
|
+
...refs,
|
|
1163
|
+
currentPath: [...refs.currentPath]
|
|
1164
|
+
});
|
|
1165
|
+
if (base2 && "$ref" in base2)
|
|
1166
|
+
return { allOf: [base2], nullable: true };
|
|
1167
|
+
return base2 && { ...base2, nullable: true };
|
|
1168
|
+
}
|
|
1169
|
+
const base = parseDef(def.innerType._def, {
|
|
1170
|
+
...refs,
|
|
1171
|
+
currentPath: [...refs.currentPath, "anyOf", "0"]
|
|
1172
|
+
});
|
|
1173
|
+
return base && { anyOf: [base, { type: "null" }] };
|
|
1174
|
+
}
|
|
1175
|
+
function parseNumberDef(def, refs) {
|
|
1176
|
+
const res = {
|
|
1177
|
+
type: "number"
|
|
1178
|
+
};
|
|
1179
|
+
if (!def.checks)
|
|
1180
|
+
return res;
|
|
1181
|
+
for (const check of def.checks) {
|
|
1182
|
+
switch (check.kind) {
|
|
1183
|
+
case "int":
|
|
1184
|
+
res.type = "integer";
|
|
1185
|
+
addErrorMessage(res, "type", check.message, refs);
|
|
1186
|
+
break;
|
|
1187
|
+
case "min":
|
|
1188
|
+
if (refs.target === "jsonSchema7") {
|
|
1189
|
+
if (check.inclusive) {
|
|
1190
|
+
setResponseValueAndErrors(res, "minimum", check.value, check.message, refs);
|
|
1191
|
+
} else {
|
|
1192
|
+
setResponseValueAndErrors(res, "exclusiveMinimum", check.value, check.message, refs);
|
|
1193
|
+
}
|
|
1194
|
+
} else {
|
|
1195
|
+
if (!check.inclusive) {
|
|
1196
|
+
res.exclusiveMinimum = true;
|
|
1197
|
+
}
|
|
1198
|
+
setResponseValueAndErrors(res, "minimum", check.value, check.message, refs);
|
|
1199
|
+
}
|
|
1200
|
+
break;
|
|
1201
|
+
case "max":
|
|
1202
|
+
if (refs.target === "jsonSchema7") {
|
|
1203
|
+
if (check.inclusive) {
|
|
1204
|
+
setResponseValueAndErrors(res, "maximum", check.value, check.message, refs);
|
|
1205
|
+
} else {
|
|
1206
|
+
setResponseValueAndErrors(res, "exclusiveMaximum", check.value, check.message, refs);
|
|
1207
|
+
}
|
|
1208
|
+
} else {
|
|
1209
|
+
if (!check.inclusive) {
|
|
1210
|
+
res.exclusiveMaximum = true;
|
|
1211
|
+
}
|
|
1212
|
+
setResponseValueAndErrors(res, "maximum", check.value, check.message, refs);
|
|
1213
|
+
}
|
|
1214
|
+
break;
|
|
1215
|
+
case "multipleOf":
|
|
1216
|
+
setResponseValueAndErrors(res, "multipleOf", check.value, check.message, refs);
|
|
1217
|
+
break;
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
return res;
|
|
1221
|
+
}
|
|
1222
|
+
function parseObjectDef(def, refs) {
|
|
1223
|
+
const forceOptionalIntoNullable = refs.target === "openAi";
|
|
1224
|
+
const result = {
|
|
1225
|
+
type: "object",
|
|
1226
|
+
properties: {}
|
|
1227
|
+
};
|
|
1228
|
+
const required = [];
|
|
1229
|
+
const shape = def.shape();
|
|
1230
|
+
for (const propName in shape) {
|
|
1231
|
+
let propDef = shape[propName];
|
|
1232
|
+
if (propDef === void 0 || propDef._def === void 0) {
|
|
1233
|
+
continue;
|
|
1234
|
+
}
|
|
1235
|
+
let propOptional = safeIsOptional(propDef);
|
|
1236
|
+
if (propOptional && forceOptionalIntoNullable) {
|
|
1237
|
+
if (propDef._def.typeName === "ZodOptional") {
|
|
1238
|
+
propDef = propDef._def.innerType;
|
|
1239
|
+
}
|
|
1240
|
+
if (!propDef.isNullable()) {
|
|
1241
|
+
propDef = propDef.nullable();
|
|
1242
|
+
}
|
|
1243
|
+
propOptional = false;
|
|
1244
|
+
}
|
|
1245
|
+
const parsedDef = parseDef(propDef._def, {
|
|
1246
|
+
...refs,
|
|
1247
|
+
currentPath: [...refs.currentPath, "properties", propName],
|
|
1248
|
+
propertyPath: [...refs.currentPath, "properties", propName]
|
|
1249
|
+
});
|
|
1250
|
+
if (parsedDef === void 0) {
|
|
1251
|
+
continue;
|
|
1252
|
+
}
|
|
1253
|
+
result.properties[propName] = parsedDef;
|
|
1254
|
+
if (!propOptional) {
|
|
1255
|
+
required.push(propName);
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
if (required.length) {
|
|
1259
|
+
result.required = required;
|
|
1260
|
+
}
|
|
1261
|
+
const additionalProperties = decideAdditionalProperties(def, refs);
|
|
1262
|
+
if (additionalProperties !== void 0) {
|
|
1263
|
+
result.additionalProperties = additionalProperties;
|
|
1264
|
+
}
|
|
1265
|
+
return result;
|
|
1266
|
+
}
|
|
1267
|
+
function decideAdditionalProperties(def, refs) {
|
|
1268
|
+
if (def.catchall._def.typeName !== "ZodNever") {
|
|
1269
|
+
return parseDef(def.catchall._def, {
|
|
1270
|
+
...refs,
|
|
1271
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1272
|
+
});
|
|
1273
|
+
}
|
|
1274
|
+
switch (def.unknownKeys) {
|
|
1275
|
+
case "passthrough":
|
|
1276
|
+
return refs.allowedAdditionalProperties;
|
|
1277
|
+
case "strict":
|
|
1278
|
+
return refs.rejectedAdditionalProperties;
|
|
1279
|
+
case "strip":
|
|
1280
|
+
return refs.removeAdditionalStrategy === "strict" ? refs.allowedAdditionalProperties : refs.rejectedAdditionalProperties;
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
function safeIsOptional(schema) {
|
|
1284
|
+
try {
|
|
1285
|
+
return schema.isOptional();
|
|
1286
|
+
} catch {
|
|
1287
|
+
return true;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
var parseOptionalDef = (def, refs) => {
|
|
1291
|
+
if (refs.currentPath.toString() === refs.propertyPath?.toString()) {
|
|
1292
|
+
return parseDef(def.innerType._def, refs);
|
|
1293
|
+
}
|
|
1294
|
+
const innerSchema = parseDef(def.innerType._def, {
|
|
1295
|
+
...refs,
|
|
1296
|
+
currentPath: [...refs.currentPath, "anyOf", "1"]
|
|
1297
|
+
});
|
|
1298
|
+
return innerSchema ? {
|
|
1299
|
+
anyOf: [
|
|
1300
|
+
{
|
|
1301
|
+
not: parseAnyDef(refs)
|
|
1302
|
+
},
|
|
1303
|
+
innerSchema
|
|
1304
|
+
]
|
|
1305
|
+
} : parseAnyDef(refs);
|
|
1306
|
+
};
|
|
1307
|
+
var parsePipelineDef = (def, refs) => {
|
|
1308
|
+
if (refs.pipeStrategy === "input") {
|
|
1309
|
+
return parseDef(def.in._def, refs);
|
|
1310
|
+
} else if (refs.pipeStrategy === "output") {
|
|
1311
|
+
return parseDef(def.out._def, refs);
|
|
1312
|
+
}
|
|
1313
|
+
const a = parseDef(def.in._def, {
|
|
1314
|
+
...refs,
|
|
1315
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1316
|
+
});
|
|
1317
|
+
const b = parseDef(def.out._def, {
|
|
1318
|
+
...refs,
|
|
1319
|
+
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
|
1320
|
+
});
|
|
1321
|
+
return {
|
|
1322
|
+
allOf: [a, b].filter((x) => x !== void 0)
|
|
1323
|
+
};
|
|
1324
|
+
};
|
|
1325
|
+
function parsePromiseDef(def, refs) {
|
|
1326
|
+
return parseDef(def.type._def, refs);
|
|
1327
|
+
}
|
|
1328
|
+
function parseSetDef(def, refs) {
|
|
1329
|
+
const items = parseDef(def.valueType._def, {
|
|
1330
|
+
...refs,
|
|
1331
|
+
currentPath: [...refs.currentPath, "items"]
|
|
1332
|
+
});
|
|
1333
|
+
const schema = {
|
|
1334
|
+
type: "array",
|
|
1335
|
+
uniqueItems: true,
|
|
1336
|
+
items
|
|
1337
|
+
};
|
|
1338
|
+
if (def.minSize) {
|
|
1339
|
+
setResponseValueAndErrors(schema, "minItems", def.minSize.value, def.minSize.message, refs);
|
|
1340
|
+
}
|
|
1341
|
+
if (def.maxSize) {
|
|
1342
|
+
setResponseValueAndErrors(schema, "maxItems", def.maxSize.value, def.maxSize.message, refs);
|
|
1343
|
+
}
|
|
1344
|
+
return schema;
|
|
1345
|
+
}
|
|
1346
|
+
function parseTupleDef(def, refs) {
|
|
1347
|
+
if (def.rest) {
|
|
1348
|
+
return {
|
|
1349
|
+
type: "array",
|
|
1350
|
+
minItems: def.items.length,
|
|
1351
|
+
items: def.items.map((x, i) => parseDef(x._def, {
|
|
1352
|
+
...refs,
|
|
1353
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1354
|
+
})).reduce((acc, x) => x === void 0 ? acc : [...acc, x], []),
|
|
1355
|
+
additionalItems: parseDef(def.rest._def, {
|
|
1356
|
+
...refs,
|
|
1357
|
+
currentPath: [...refs.currentPath, "additionalItems"]
|
|
1358
|
+
})
|
|
1359
|
+
};
|
|
1360
|
+
} else {
|
|
1361
|
+
return {
|
|
1362
|
+
type: "array",
|
|
1363
|
+
minItems: def.items.length,
|
|
1364
|
+
maxItems: def.items.length,
|
|
1365
|
+
items: def.items.map((x, i) => parseDef(x._def, {
|
|
1366
|
+
...refs,
|
|
1367
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1368
|
+
})).reduce((acc, x) => x === void 0 ? acc : [...acc, x], [])
|
|
1369
|
+
};
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
function parseUndefinedDef(refs) {
|
|
1373
|
+
return {
|
|
1374
|
+
not: parseAnyDef(refs)
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
function parseUnknownDef(refs) {
|
|
1378
|
+
return parseAnyDef(refs);
|
|
1379
|
+
}
|
|
1380
|
+
var parseReadonlyDef = (def, refs) => {
|
|
1381
|
+
return parseDef(def.innerType._def, refs);
|
|
1382
|
+
};
|
|
1383
|
+
var selectParser = (def, typeName, refs) => {
|
|
1384
|
+
switch (typeName) {
|
|
1385
|
+
case ZodFirstPartyTypeKind.ZodString:
|
|
1386
|
+
return parseStringDef(def, refs);
|
|
1387
|
+
case ZodFirstPartyTypeKind.ZodNumber:
|
|
1388
|
+
return parseNumberDef(def, refs);
|
|
1389
|
+
case ZodFirstPartyTypeKind.ZodObject:
|
|
1390
|
+
return parseObjectDef(def, refs);
|
|
1391
|
+
case ZodFirstPartyTypeKind.ZodBigInt:
|
|
1392
|
+
return parseBigintDef(def, refs);
|
|
1393
|
+
case ZodFirstPartyTypeKind.ZodBoolean:
|
|
1394
|
+
return parseBooleanDef();
|
|
1395
|
+
case ZodFirstPartyTypeKind.ZodDate:
|
|
1396
|
+
return parseDateDef(def, refs);
|
|
1397
|
+
case ZodFirstPartyTypeKind.ZodUndefined:
|
|
1398
|
+
return parseUndefinedDef(refs);
|
|
1399
|
+
case ZodFirstPartyTypeKind.ZodNull:
|
|
1400
|
+
return parseNullDef(refs);
|
|
1401
|
+
case ZodFirstPartyTypeKind.ZodArray:
|
|
1402
|
+
return parseArrayDef(def, refs);
|
|
1403
|
+
case ZodFirstPartyTypeKind.ZodUnion:
|
|
1404
|
+
case ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
|
|
1405
|
+
return parseUnionDef(def, refs);
|
|
1406
|
+
case ZodFirstPartyTypeKind.ZodIntersection:
|
|
1407
|
+
return parseIntersectionDef(def, refs);
|
|
1408
|
+
case ZodFirstPartyTypeKind.ZodTuple:
|
|
1409
|
+
return parseTupleDef(def, refs);
|
|
1410
|
+
case ZodFirstPartyTypeKind.ZodRecord:
|
|
1411
|
+
return parseRecordDef(def, refs);
|
|
1412
|
+
case ZodFirstPartyTypeKind.ZodLiteral:
|
|
1413
|
+
return parseLiteralDef(def, refs);
|
|
1414
|
+
case ZodFirstPartyTypeKind.ZodEnum:
|
|
1415
|
+
return parseEnumDef(def);
|
|
1416
|
+
case ZodFirstPartyTypeKind.ZodNativeEnum:
|
|
1417
|
+
return parseNativeEnumDef(def);
|
|
1418
|
+
case ZodFirstPartyTypeKind.ZodNullable:
|
|
1419
|
+
return parseNullableDef(def, refs);
|
|
1420
|
+
case ZodFirstPartyTypeKind.ZodOptional:
|
|
1421
|
+
return parseOptionalDef(def, refs);
|
|
1422
|
+
case ZodFirstPartyTypeKind.ZodMap:
|
|
1423
|
+
return parseMapDef(def, refs);
|
|
1424
|
+
case ZodFirstPartyTypeKind.ZodSet:
|
|
1425
|
+
return parseSetDef(def, refs);
|
|
1426
|
+
case ZodFirstPartyTypeKind.ZodLazy:
|
|
1427
|
+
return () => def.getter()._def;
|
|
1428
|
+
case ZodFirstPartyTypeKind.ZodPromise:
|
|
1429
|
+
return parsePromiseDef(def, refs);
|
|
1430
|
+
case ZodFirstPartyTypeKind.ZodNaN:
|
|
1431
|
+
case ZodFirstPartyTypeKind.ZodNever:
|
|
1432
|
+
return parseNeverDef(refs);
|
|
1433
|
+
case ZodFirstPartyTypeKind.ZodEffects:
|
|
1434
|
+
return parseEffectsDef(def, refs);
|
|
1435
|
+
case ZodFirstPartyTypeKind.ZodAny:
|
|
1436
|
+
return parseAnyDef(refs);
|
|
1437
|
+
case ZodFirstPartyTypeKind.ZodUnknown:
|
|
1438
|
+
return parseUnknownDef(refs);
|
|
1439
|
+
case ZodFirstPartyTypeKind.ZodDefault:
|
|
1440
|
+
return parseDefaultDef(def, refs);
|
|
1441
|
+
case ZodFirstPartyTypeKind.ZodBranded:
|
|
1442
|
+
return parseBrandedDef(def, refs);
|
|
1443
|
+
case ZodFirstPartyTypeKind.ZodReadonly:
|
|
1444
|
+
return parseReadonlyDef(def, refs);
|
|
1445
|
+
case ZodFirstPartyTypeKind.ZodCatch:
|
|
1446
|
+
return parseCatchDef(def, refs);
|
|
1447
|
+
case ZodFirstPartyTypeKind.ZodPipeline:
|
|
1448
|
+
return parsePipelineDef(def, refs);
|
|
1449
|
+
case ZodFirstPartyTypeKind.ZodFunction:
|
|
1450
|
+
case ZodFirstPartyTypeKind.ZodVoid:
|
|
1451
|
+
case ZodFirstPartyTypeKind.ZodSymbol:
|
|
1452
|
+
return void 0;
|
|
1453
|
+
default:
|
|
1454
|
+
return /* @__PURE__ */ ((_) => void 0)();
|
|
1455
|
+
}
|
|
1456
|
+
};
|
|
1457
|
+
function parseDef(def, refs, forceResolution = false) {
|
|
1458
|
+
const seenItem = refs.seen.get(def);
|
|
1459
|
+
if (refs.override) {
|
|
1460
|
+
const overrideResult = refs.override?.(def, refs, seenItem, forceResolution);
|
|
1461
|
+
if (overrideResult !== ignoreOverride) {
|
|
1462
|
+
return overrideResult;
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
if (seenItem && !forceResolution) {
|
|
1466
|
+
const seenSchema = get$ref(seenItem, refs);
|
|
1467
|
+
if (seenSchema !== void 0) {
|
|
1468
|
+
return seenSchema;
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
|
1472
|
+
refs.seen.set(def, newItem);
|
|
1473
|
+
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
|
1474
|
+
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
|
1475
|
+
if (jsonSchema2) {
|
|
1476
|
+
addMeta(def, refs, jsonSchema2);
|
|
1477
|
+
}
|
|
1478
|
+
if (refs.postProcess) {
|
|
1479
|
+
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
|
1480
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1481
|
+
return postProcessResult;
|
|
1482
|
+
}
|
|
1483
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1484
|
+
return jsonSchema2;
|
|
1485
|
+
}
|
|
1486
|
+
var get$ref = (item, refs) => {
|
|
1487
|
+
switch (refs.$refStrategy) {
|
|
1488
|
+
case "root":
|
|
1489
|
+
return { $ref: item.path.join("/") };
|
|
1490
|
+
case "relative":
|
|
1491
|
+
return { $ref: getRelativePath(refs.currentPath, item.path) };
|
|
1492
|
+
case "none":
|
|
1493
|
+
case "seen": {
|
|
1494
|
+
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
|
|
1495
|
+
console.warn(`Recursive reference detected at ${refs.currentPath.join("/")}! Defaulting to any`);
|
|
1496
|
+
return parseAnyDef(refs);
|
|
1497
|
+
}
|
|
1498
|
+
return refs.$refStrategy === "seen" ? parseAnyDef(refs) : void 0;
|
|
1499
|
+
}
|
|
1500
|
+
}
|
|
1501
|
+
};
|
|
1502
|
+
var addMeta = (def, refs, jsonSchema2) => {
|
|
1503
|
+
if (def.description) {
|
|
1504
|
+
jsonSchema2.description = def.description;
|
|
1505
|
+
if (refs.markdownDescription) {
|
|
1506
|
+
jsonSchema2.markdownDescription = def.description;
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
return jsonSchema2;
|
|
1510
|
+
};
|
|
1511
|
+
var zodToJsonSchema2 = (schema, options) => {
|
|
1512
|
+
const refs = getRefs(options);
|
|
1513
|
+
let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce((acc, [name18, schema2]) => ({
|
|
1514
|
+
...acc,
|
|
1515
|
+
[name18]: parseDef(schema2._def, {
|
|
1516
|
+
...refs,
|
|
1517
|
+
currentPath: [...refs.basePath, refs.definitionPath, name18]
|
|
1518
|
+
}, true) ?? parseAnyDef(refs)
|
|
1519
|
+
}), {}) : void 0;
|
|
1520
|
+
const name17 = typeof options === "string" ? options : options?.nameStrategy === "title" ? void 0 : options?.name;
|
|
1521
|
+
const main = parseDef(schema._def, name17 === void 0 ? refs : {
|
|
1522
|
+
...refs,
|
|
1523
|
+
currentPath: [...refs.basePath, refs.definitionPath, name17]
|
|
1524
|
+
}, false) ?? parseAnyDef(refs);
|
|
1525
|
+
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
|
1526
|
+
if (title !== void 0) {
|
|
1527
|
+
main.title = title;
|
|
1528
|
+
}
|
|
1529
|
+
if (refs.flags.hasReferencedOpenAiAnyType) {
|
|
1530
|
+
if (!definitions) {
|
|
1531
|
+
definitions = {};
|
|
1532
|
+
}
|
|
1533
|
+
if (!definitions[refs.openAiAnyTypeName]) {
|
|
1534
|
+
definitions[refs.openAiAnyTypeName] = {
|
|
1535
|
+
// Skipping "object" as no properties can be defined and additionalProperties must be "false"
|
|
1536
|
+
type: ["string", "number", "integer", "boolean", "array", "null"],
|
|
1537
|
+
items: {
|
|
1538
|
+
$ref: refs.$refStrategy === "relative" ? "1" : [
|
|
1539
|
+
...refs.basePath,
|
|
1540
|
+
refs.definitionPath,
|
|
1541
|
+
refs.openAiAnyTypeName
|
|
1542
|
+
].join("/")
|
|
1543
|
+
}
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
const combined = name17 === void 0 ? definitions ? {
|
|
1548
|
+
...main,
|
|
1549
|
+
[refs.definitionPath]: definitions
|
|
1550
|
+
} : main : {
|
|
1551
|
+
$ref: [
|
|
1552
|
+
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
|
1553
|
+
refs.definitionPath,
|
|
1554
|
+
name17
|
|
1555
|
+
].join("/"),
|
|
1556
|
+
[refs.definitionPath]: {
|
|
1557
|
+
...definitions,
|
|
1558
|
+
[name17]: main
|
|
1559
|
+
}
|
|
1560
|
+
};
|
|
1561
|
+
if (refs.target === "jsonSchema7") {
|
|
1562
|
+
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
|
1563
|
+
} else if (refs.target === "jsonSchema2019-09" || refs.target === "openAi") {
|
|
1564
|
+
combined.$schema = "https://json-schema.org/draft/2019-09/schema#";
|
|
1565
|
+
}
|
|
1566
|
+
if (refs.target === "openAi" && ("anyOf" in combined || "oneOf" in combined || "allOf" in combined || "type" in combined && Array.isArray(combined.type))) {
|
|
1567
|
+
console.warn("Warning: OpenAI may not support schemas with unions as roots! Try wrapping it in an object property.");
|
|
1568
|
+
}
|
|
1569
|
+
return combined;
|
|
1570
|
+
};
|
|
1571
|
+
var esm_default = zodToJsonSchema2;
|
|
1572
|
+
function fixJson(input) {
|
|
1573
|
+
const stack = ["ROOT"];
|
|
1574
|
+
let lastValidIndex = -1;
|
|
1575
|
+
let literalStart = null;
|
|
1576
|
+
function processValueStart(char, i, swapState) {
|
|
1577
|
+
{
|
|
1578
|
+
switch (char) {
|
|
1579
|
+
case '"': {
|
|
1580
|
+
lastValidIndex = i;
|
|
1581
|
+
stack.pop();
|
|
1582
|
+
stack.push(swapState);
|
|
1583
|
+
stack.push("INSIDE_STRING");
|
|
1584
|
+
break;
|
|
1585
|
+
}
|
|
1586
|
+
case "f":
|
|
1587
|
+
case "t":
|
|
1588
|
+
case "n": {
|
|
1589
|
+
lastValidIndex = i;
|
|
1590
|
+
literalStart = i;
|
|
1591
|
+
stack.pop();
|
|
1592
|
+
stack.push(swapState);
|
|
1593
|
+
stack.push("INSIDE_LITERAL");
|
|
1594
|
+
break;
|
|
1595
|
+
}
|
|
1596
|
+
case "-": {
|
|
1597
|
+
stack.pop();
|
|
1598
|
+
stack.push(swapState);
|
|
1599
|
+
stack.push("INSIDE_NUMBER");
|
|
1600
|
+
break;
|
|
1601
|
+
}
|
|
1602
|
+
case "0":
|
|
1603
|
+
case "1":
|
|
1604
|
+
case "2":
|
|
1605
|
+
case "3":
|
|
1606
|
+
case "4":
|
|
1607
|
+
case "5":
|
|
1608
|
+
case "6":
|
|
1609
|
+
case "7":
|
|
1610
|
+
case "8":
|
|
1611
|
+
case "9": {
|
|
1612
|
+
lastValidIndex = i;
|
|
1613
|
+
stack.pop();
|
|
1614
|
+
stack.push(swapState);
|
|
1615
|
+
stack.push("INSIDE_NUMBER");
|
|
1616
|
+
break;
|
|
1617
|
+
}
|
|
1618
|
+
case "{": {
|
|
1619
|
+
lastValidIndex = i;
|
|
1620
|
+
stack.pop();
|
|
1621
|
+
stack.push(swapState);
|
|
1622
|
+
stack.push("INSIDE_OBJECT_START");
|
|
1623
|
+
break;
|
|
1624
|
+
}
|
|
1625
|
+
case "[": {
|
|
1626
|
+
lastValidIndex = i;
|
|
1627
|
+
stack.pop();
|
|
1628
|
+
stack.push(swapState);
|
|
1629
|
+
stack.push("INSIDE_ARRAY_START");
|
|
1630
|
+
break;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
function processAfterObjectValue(char, i) {
|
|
1636
|
+
switch (char) {
|
|
1637
|
+
case ",": {
|
|
1638
|
+
stack.pop();
|
|
1639
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
1640
|
+
break;
|
|
1641
|
+
}
|
|
1642
|
+
case "}": {
|
|
1643
|
+
lastValidIndex = i;
|
|
1644
|
+
stack.pop();
|
|
1645
|
+
break;
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
function processAfterArrayValue(char, i) {
|
|
1650
|
+
switch (char) {
|
|
1651
|
+
case ",": {
|
|
1652
|
+
stack.pop();
|
|
1653
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
1654
|
+
break;
|
|
1655
|
+
}
|
|
1656
|
+
case "]": {
|
|
1657
|
+
lastValidIndex = i;
|
|
1658
|
+
stack.pop();
|
|
1659
|
+
break;
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
for (let i = 0; i < input.length; i++) {
|
|
1664
|
+
const char = input[i];
|
|
1665
|
+
const currentState = stack[stack.length - 1];
|
|
1666
|
+
switch (currentState) {
|
|
1667
|
+
case "ROOT":
|
|
1668
|
+
processValueStart(char, i, "FINISH");
|
|
1669
|
+
break;
|
|
1670
|
+
case "INSIDE_OBJECT_START": {
|
|
1671
|
+
switch (char) {
|
|
1672
|
+
case '"': {
|
|
1673
|
+
stack.pop();
|
|
1674
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
case "}": {
|
|
1678
|
+
lastValidIndex = i;
|
|
1679
|
+
stack.pop();
|
|
1680
|
+
break;
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
break;
|
|
1684
|
+
}
|
|
1685
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
1686
|
+
switch (char) {
|
|
1687
|
+
case '"': {
|
|
1688
|
+
stack.pop();
|
|
1689
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
1690
|
+
break;
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
break;
|
|
1694
|
+
}
|
|
1695
|
+
case "INSIDE_OBJECT_KEY": {
|
|
1696
|
+
switch (char) {
|
|
1697
|
+
case '"': {
|
|
1698
|
+
stack.pop();
|
|
1699
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
1700
|
+
break;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
break;
|
|
1704
|
+
}
|
|
1705
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
1706
|
+
switch (char) {
|
|
1707
|
+
case ":": {
|
|
1708
|
+
stack.pop();
|
|
1709
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
1710
|
+
break;
|
|
1711
|
+
}
|
|
1712
|
+
}
|
|
1713
|
+
break;
|
|
1714
|
+
}
|
|
1715
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
1716
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
1717
|
+
break;
|
|
1718
|
+
}
|
|
1719
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
1720
|
+
processAfterObjectValue(char, i);
|
|
1721
|
+
break;
|
|
1722
|
+
}
|
|
1723
|
+
case "INSIDE_STRING": {
|
|
1724
|
+
switch (char) {
|
|
1725
|
+
case '"': {
|
|
1726
|
+
stack.pop();
|
|
1727
|
+
lastValidIndex = i;
|
|
1728
|
+
break;
|
|
1729
|
+
}
|
|
1730
|
+
case "\\": {
|
|
1731
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
|
1732
|
+
break;
|
|
1733
|
+
}
|
|
1734
|
+
default: {
|
|
1735
|
+
lastValidIndex = i;
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
break;
|
|
1739
|
+
}
|
|
1740
|
+
case "INSIDE_ARRAY_START": {
|
|
1741
|
+
switch (char) {
|
|
1742
|
+
case "]": {
|
|
1743
|
+
lastValidIndex = i;
|
|
1744
|
+
stack.pop();
|
|
1745
|
+
break;
|
|
1746
|
+
}
|
|
1747
|
+
default: {
|
|
1748
|
+
lastValidIndex = i;
|
|
1749
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
1750
|
+
break;
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
break;
|
|
1754
|
+
}
|
|
1755
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
1756
|
+
switch (char) {
|
|
1757
|
+
case ",": {
|
|
1758
|
+
stack.pop();
|
|
1759
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
1760
|
+
break;
|
|
1761
|
+
}
|
|
1762
|
+
case "]": {
|
|
1763
|
+
lastValidIndex = i;
|
|
1764
|
+
stack.pop();
|
|
1765
|
+
break;
|
|
1766
|
+
}
|
|
1767
|
+
default: {
|
|
1768
|
+
lastValidIndex = i;
|
|
1769
|
+
break;
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1772
|
+
break;
|
|
1773
|
+
}
|
|
1774
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
1775
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
1776
|
+
break;
|
|
1777
|
+
}
|
|
1778
|
+
case "INSIDE_STRING_ESCAPE": {
|
|
1779
|
+
stack.pop();
|
|
1780
|
+
lastValidIndex = i;
|
|
1781
|
+
break;
|
|
1782
|
+
}
|
|
1783
|
+
case "INSIDE_NUMBER": {
|
|
1784
|
+
switch (char) {
|
|
1785
|
+
case "0":
|
|
1786
|
+
case "1":
|
|
1787
|
+
case "2":
|
|
1788
|
+
case "3":
|
|
1789
|
+
case "4":
|
|
1790
|
+
case "5":
|
|
1791
|
+
case "6":
|
|
1792
|
+
case "7":
|
|
1793
|
+
case "8":
|
|
1794
|
+
case "9": {
|
|
1795
|
+
lastValidIndex = i;
|
|
1796
|
+
break;
|
|
1797
|
+
}
|
|
1798
|
+
case "e":
|
|
1799
|
+
case "E":
|
|
1800
|
+
case "-":
|
|
1801
|
+
case ".": {
|
|
1802
|
+
break;
|
|
1803
|
+
}
|
|
1804
|
+
case ",": {
|
|
1805
|
+
stack.pop();
|
|
1806
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1807
|
+
processAfterArrayValue(char, i);
|
|
1808
|
+
}
|
|
1809
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1810
|
+
processAfterObjectValue(char, i);
|
|
1811
|
+
}
|
|
1812
|
+
break;
|
|
1813
|
+
}
|
|
1814
|
+
case "}": {
|
|
1815
|
+
stack.pop();
|
|
1816
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1817
|
+
processAfterObjectValue(char, i);
|
|
1818
|
+
}
|
|
1819
|
+
break;
|
|
1820
|
+
}
|
|
1821
|
+
case "]": {
|
|
1822
|
+
stack.pop();
|
|
1823
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1824
|
+
processAfterArrayValue(char, i);
|
|
1825
|
+
}
|
|
1826
|
+
break;
|
|
1827
|
+
}
|
|
1828
|
+
default: {
|
|
1829
|
+
stack.pop();
|
|
1830
|
+
break;
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
break;
|
|
1834
|
+
}
|
|
1835
|
+
case "INSIDE_LITERAL": {
|
|
1836
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
|
1837
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
1838
|
+
stack.pop();
|
|
1839
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1840
|
+
processAfterObjectValue(char, i);
|
|
1841
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1842
|
+
processAfterArrayValue(char, i);
|
|
1843
|
+
}
|
|
1844
|
+
} else {
|
|
1845
|
+
lastValidIndex = i;
|
|
1846
|
+
}
|
|
1847
|
+
break;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
let result = input.slice(0, lastValidIndex + 1);
|
|
1852
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
1853
|
+
const state = stack[i];
|
|
1854
|
+
switch (state) {
|
|
1855
|
+
case "INSIDE_STRING": {
|
|
1856
|
+
result += '"';
|
|
1857
|
+
break;
|
|
1858
|
+
}
|
|
1859
|
+
case "INSIDE_OBJECT_KEY":
|
|
1860
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
|
1861
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
1862
|
+
case "INSIDE_OBJECT_START":
|
|
1863
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
1864
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
1865
|
+
result += "}";
|
|
1866
|
+
break;
|
|
1867
|
+
}
|
|
1868
|
+
case "INSIDE_ARRAY_START":
|
|
1869
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
1870
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
1871
|
+
result += "]";
|
|
1872
|
+
break;
|
|
1873
|
+
}
|
|
1874
|
+
case "INSIDE_LITERAL": {
|
|
1875
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
|
1876
|
+
if ("true".startsWith(partialLiteral)) {
|
|
1877
|
+
result += "true".slice(partialLiteral.length);
|
|
1878
|
+
} else if ("false".startsWith(partialLiteral)) {
|
|
1879
|
+
result += "false".slice(partialLiteral.length);
|
|
1880
|
+
} else if ("null".startsWith(partialLiteral)) {
|
|
1881
|
+
result += "null".slice(partialLiteral.length);
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
return result;
|
|
1887
|
+
}
|
|
1888
|
+
function parsePartialJson(jsonText) {
|
|
1889
|
+
if (jsonText === void 0) {
|
|
1890
|
+
return { value: void 0, state: "undefined-input" };
|
|
1891
|
+
}
|
|
1892
|
+
let result = safeParseJSON({ text: jsonText });
|
|
1893
|
+
if (result.success) {
|
|
1894
|
+
return { value: result.value, state: "successful-parse" };
|
|
1895
|
+
}
|
|
1896
|
+
result = safeParseJSON({ text: fixJson(jsonText) });
|
|
1897
|
+
if (result.success) {
|
|
1898
|
+
return { value: result.value, state: "repaired-parse" };
|
|
1899
|
+
}
|
|
1900
|
+
return { value: void 0, state: "failed-parse" };
|
|
1901
|
+
}
|
|
1902
|
+
var textStreamPart2 = {
|
|
1903
|
+
code: "0",
|
|
1904
|
+
name: "text",
|
|
1905
|
+
parse: (value) => {
|
|
1906
|
+
if (typeof value !== "string") {
|
|
1907
|
+
throw new Error('"text" parts expect a string value.');
|
|
1908
|
+
}
|
|
1909
|
+
return { type: "text", value };
|
|
1910
|
+
}
|
|
1911
|
+
};
|
|
1912
|
+
var dataStreamPart = {
|
|
1913
|
+
code: "2",
|
|
1914
|
+
name: "data",
|
|
1915
|
+
parse: (value) => {
|
|
1916
|
+
if (!Array.isArray(value)) {
|
|
1917
|
+
throw new Error('"data" parts expect an array value.');
|
|
1918
|
+
}
|
|
1919
|
+
return { type: "data", value };
|
|
1920
|
+
}
|
|
1921
|
+
};
|
|
1922
|
+
var errorStreamPart2 = {
|
|
1923
|
+
code: "3",
|
|
1924
|
+
name: "error",
|
|
1925
|
+
parse: (value) => {
|
|
1926
|
+
if (typeof value !== "string") {
|
|
1927
|
+
throw new Error('"error" parts expect a string value.');
|
|
1928
|
+
}
|
|
1929
|
+
return { type: "error", value };
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
var messageAnnotationsStreamPart = {
|
|
1933
|
+
code: "8",
|
|
1934
|
+
name: "message_annotations",
|
|
1935
|
+
parse: (value) => {
|
|
1936
|
+
if (!Array.isArray(value)) {
|
|
1937
|
+
throw new Error('"message_annotations" parts expect an array value.');
|
|
1938
|
+
}
|
|
1939
|
+
return { type: "message_annotations", value };
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
var toolCallStreamPart = {
|
|
1943
|
+
code: "9",
|
|
1944
|
+
name: "tool_call",
|
|
1945
|
+
parse: (value) => {
|
|
1946
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string" || !("args" in value) || typeof value.args !== "object") {
|
|
1947
|
+
throw new Error(
|
|
1948
|
+
'"tool_call" parts expect an object with a "toolCallId", "toolName", and "args" property.'
|
|
1949
|
+
);
|
|
1950
|
+
}
|
|
1951
|
+
return {
|
|
1952
|
+
type: "tool_call",
|
|
1953
|
+
value
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
};
|
|
1957
|
+
var toolResultStreamPart = {
|
|
1958
|
+
code: "a",
|
|
1959
|
+
name: "tool_result",
|
|
1960
|
+
parse: (value) => {
|
|
1961
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("result" in value)) {
|
|
1962
|
+
throw new Error(
|
|
1963
|
+
'"tool_result" parts expect an object with a "toolCallId" and a "result" property.'
|
|
1964
|
+
);
|
|
1965
|
+
}
|
|
1966
|
+
return {
|
|
1967
|
+
type: "tool_result",
|
|
1968
|
+
value
|
|
1969
|
+
};
|
|
1970
|
+
}
|
|
1971
|
+
};
|
|
1972
|
+
var toolCallStreamingStartStreamPart = {
|
|
1973
|
+
code: "b",
|
|
1974
|
+
name: "tool_call_streaming_start",
|
|
1975
|
+
parse: (value) => {
|
|
1976
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("toolName" in value) || typeof value.toolName !== "string") {
|
|
1977
|
+
throw new Error(
|
|
1978
|
+
'"tool_call_streaming_start" parts expect an object with a "toolCallId" and "toolName" property.'
|
|
1979
|
+
);
|
|
1980
|
+
}
|
|
1981
|
+
return {
|
|
1982
|
+
type: "tool_call_streaming_start",
|
|
1983
|
+
value
|
|
1984
|
+
};
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
var toolCallDeltaStreamPart = {
|
|
1988
|
+
code: "c",
|
|
1989
|
+
name: "tool_call_delta",
|
|
1990
|
+
parse: (value) => {
|
|
1991
|
+
if (value == null || typeof value !== "object" || !("toolCallId" in value) || typeof value.toolCallId !== "string" || !("argsTextDelta" in value) || typeof value.argsTextDelta !== "string") {
|
|
1992
|
+
throw new Error(
|
|
1993
|
+
'"tool_call_delta" parts expect an object with a "toolCallId" and "argsTextDelta" property.'
|
|
1994
|
+
);
|
|
1995
|
+
}
|
|
1996
|
+
return {
|
|
1997
|
+
type: "tool_call_delta",
|
|
1998
|
+
value
|
|
1999
|
+
};
|
|
2000
|
+
}
|
|
2001
|
+
};
|
|
2002
|
+
var finishMessageStreamPart = {
|
|
2003
|
+
code: "d",
|
|
2004
|
+
name: "finish_message",
|
|
2005
|
+
parse: (value) => {
|
|
2006
|
+
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
2007
|
+
throw new Error(
|
|
2008
|
+
'"finish_message" parts expect an object with a "finishReason" property.'
|
|
2009
|
+
);
|
|
2010
|
+
}
|
|
2011
|
+
const result = {
|
|
2012
|
+
finishReason: value.finishReason
|
|
2013
|
+
};
|
|
2014
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
2015
|
+
result.usage = {
|
|
2016
|
+
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
2017
|
+
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
2018
|
+
};
|
|
2019
|
+
}
|
|
2020
|
+
return {
|
|
2021
|
+
type: "finish_message",
|
|
2022
|
+
value: result
|
|
2023
|
+
};
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
var finishStepStreamPart = {
|
|
2027
|
+
code: "e",
|
|
2028
|
+
name: "finish_step",
|
|
2029
|
+
parse: (value) => {
|
|
2030
|
+
if (value == null || typeof value !== "object" || !("finishReason" in value) || typeof value.finishReason !== "string") {
|
|
2031
|
+
throw new Error(
|
|
2032
|
+
'"finish_step" parts expect an object with a "finishReason" property.'
|
|
2033
|
+
);
|
|
2034
|
+
}
|
|
2035
|
+
const result = {
|
|
2036
|
+
finishReason: value.finishReason,
|
|
2037
|
+
isContinued: false
|
|
2038
|
+
};
|
|
2039
|
+
if ("usage" in value && value.usage != null && typeof value.usage === "object" && "promptTokens" in value.usage && "completionTokens" in value.usage) {
|
|
2040
|
+
result.usage = {
|
|
2041
|
+
promptTokens: typeof value.usage.promptTokens === "number" ? value.usage.promptTokens : Number.NaN,
|
|
2042
|
+
completionTokens: typeof value.usage.completionTokens === "number" ? value.usage.completionTokens : Number.NaN
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
if ("isContinued" in value && typeof value.isContinued === "boolean") {
|
|
2046
|
+
result.isContinued = value.isContinued;
|
|
2047
|
+
}
|
|
2048
|
+
return {
|
|
2049
|
+
type: "finish_step",
|
|
2050
|
+
value: result
|
|
2051
|
+
};
|
|
2052
|
+
}
|
|
2053
|
+
};
|
|
2054
|
+
var startStepStreamPart = {
|
|
2055
|
+
code: "f",
|
|
2056
|
+
name: "start_step",
|
|
2057
|
+
parse: (value) => {
|
|
2058
|
+
if (value == null || typeof value !== "object" || !("messageId" in value) || typeof value.messageId !== "string") {
|
|
2059
|
+
throw new Error(
|
|
2060
|
+
'"start_step" parts expect an object with an "id" property.'
|
|
2061
|
+
);
|
|
2062
|
+
}
|
|
2063
|
+
return {
|
|
2064
|
+
type: "start_step",
|
|
2065
|
+
value: {
|
|
2066
|
+
messageId: value.messageId
|
|
2067
|
+
}
|
|
2068
|
+
};
|
|
2069
|
+
}
|
|
2070
|
+
};
|
|
2071
|
+
var reasoningStreamPart = {
|
|
2072
|
+
code: "g",
|
|
2073
|
+
name: "reasoning",
|
|
2074
|
+
parse: (value) => {
|
|
2075
|
+
if (typeof value !== "string") {
|
|
2076
|
+
throw new Error('"reasoning" parts expect a string value.');
|
|
2077
|
+
}
|
|
2078
|
+
return { type: "reasoning", value };
|
|
2079
|
+
}
|
|
2080
|
+
};
|
|
2081
|
+
var sourcePart = {
|
|
2082
|
+
code: "h",
|
|
2083
|
+
name: "source",
|
|
2084
|
+
parse: (value) => {
|
|
2085
|
+
if (value == null || typeof value !== "object") {
|
|
2086
|
+
throw new Error('"source" parts expect a Source object.');
|
|
2087
|
+
}
|
|
2088
|
+
return {
|
|
2089
|
+
type: "source",
|
|
2090
|
+
value
|
|
2091
|
+
};
|
|
2092
|
+
}
|
|
2093
|
+
};
|
|
2094
|
+
var redactedReasoningStreamPart = {
|
|
2095
|
+
code: "i",
|
|
2096
|
+
name: "redacted_reasoning",
|
|
2097
|
+
parse: (value) => {
|
|
2098
|
+
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string") {
|
|
2099
|
+
throw new Error(
|
|
2100
|
+
'"redacted_reasoning" parts expect an object with a "data" property.'
|
|
2101
|
+
);
|
|
2102
|
+
}
|
|
2103
|
+
return { type: "redacted_reasoning", value: { data: value.data } };
|
|
2104
|
+
}
|
|
2105
|
+
};
|
|
2106
|
+
var reasoningSignatureStreamPart = {
|
|
2107
|
+
code: "j",
|
|
2108
|
+
name: "reasoning_signature",
|
|
2109
|
+
parse: (value) => {
|
|
2110
|
+
if (value == null || typeof value !== "object" || !("signature" in value) || typeof value.signature !== "string") {
|
|
2111
|
+
throw new Error(
|
|
2112
|
+
'"reasoning_signature" parts expect an object with a "signature" property.'
|
|
2113
|
+
);
|
|
2114
|
+
}
|
|
2115
|
+
return {
|
|
2116
|
+
type: "reasoning_signature",
|
|
2117
|
+
value: { signature: value.signature }
|
|
2118
|
+
};
|
|
2119
|
+
}
|
|
2120
|
+
};
|
|
2121
|
+
var fileStreamPart = {
|
|
2122
|
+
code: "k",
|
|
2123
|
+
name: "file",
|
|
2124
|
+
parse: (value) => {
|
|
2125
|
+
if (value == null || typeof value !== "object" || !("data" in value) || typeof value.data !== "string" || !("mimeType" in value) || typeof value.mimeType !== "string") {
|
|
2126
|
+
throw new Error(
|
|
2127
|
+
'"file" parts expect an object with a "data" and "mimeType" property.'
|
|
2128
|
+
);
|
|
2129
|
+
}
|
|
2130
|
+
return { type: "file", value };
|
|
2131
|
+
}
|
|
2132
|
+
};
|
|
2133
|
+
var dataStreamParts = [
|
|
2134
|
+
textStreamPart2,
|
|
2135
|
+
dataStreamPart,
|
|
2136
|
+
errorStreamPart2,
|
|
2137
|
+
messageAnnotationsStreamPart,
|
|
2138
|
+
toolCallStreamPart,
|
|
2139
|
+
toolResultStreamPart,
|
|
2140
|
+
toolCallStreamingStartStreamPart,
|
|
2141
|
+
toolCallDeltaStreamPart,
|
|
2142
|
+
finishMessageStreamPart,
|
|
2143
|
+
finishStepStreamPart,
|
|
2144
|
+
startStepStreamPart,
|
|
2145
|
+
reasoningStreamPart,
|
|
2146
|
+
sourcePart,
|
|
2147
|
+
redactedReasoningStreamPart,
|
|
2148
|
+
reasoningSignatureStreamPart,
|
|
2149
|
+
fileStreamPart
|
|
2150
|
+
];
|
|
2151
|
+
Object.fromEntries(
|
|
2152
|
+
dataStreamParts.map((part) => [part.code, part])
|
|
2153
|
+
);
|
|
2154
|
+
Object.fromEntries(
|
|
2155
|
+
dataStreamParts.map((part) => [part.name, part.code])
|
|
2156
|
+
);
|
|
2157
|
+
function formatDataStreamPart(type, value) {
|
|
2158
|
+
const streamPart = dataStreamParts.find((part) => part.name === type);
|
|
2159
|
+
if (!streamPart) {
|
|
2160
|
+
throw new Error(`Invalid stream part type: ${type}`);
|
|
2161
|
+
}
|
|
2162
|
+
return `${streamPart.code}:${JSON.stringify(value)}
|
|
2163
|
+
`;
|
|
2164
|
+
}
|
|
2165
|
+
function zodSchema(zodSchema2, options) {
|
|
2166
|
+
var _a17;
|
|
2167
|
+
const useReferences = (_a17 = void 0) != null ? _a17 : false;
|
|
2168
|
+
return jsonSchema(
|
|
2169
|
+
esm_default(zodSchema2, {
|
|
2170
|
+
$refStrategy: useReferences ? "root" : "none",
|
|
2171
|
+
target: "jsonSchema7"
|
|
2172
|
+
// note: openai mode breaks various gemini conversions
|
|
2173
|
+
}),
|
|
2174
|
+
{
|
|
2175
|
+
validate: (value) => {
|
|
2176
|
+
const result = zodSchema2.safeParse(value);
|
|
2177
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
);
|
|
2181
|
+
}
|
|
2182
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
2183
|
+
function jsonSchema(jsonSchema2, {
|
|
2184
|
+
validate
|
|
2185
|
+
} = {}) {
|
|
2186
|
+
return {
|
|
2187
|
+
[schemaSymbol]: true,
|
|
2188
|
+
_type: void 0,
|
|
2189
|
+
// should never be used directly
|
|
2190
|
+
[validatorSymbol]: true,
|
|
2191
|
+
jsonSchema: jsonSchema2,
|
|
2192
|
+
validate
|
|
2193
|
+
};
|
|
2194
|
+
}
|
|
2195
|
+
function isSchema(value) {
|
|
2196
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
|
2197
|
+
}
|
|
2198
|
+
function asSchema(schema) {
|
|
2199
|
+
return isSchema(schema) ? schema : zodSchema(schema);
|
|
2200
|
+
}
|
|
2201
|
+
var _globalThis = typeof globalThis === "object" ? globalThis : global;
|
|
2202
|
+
var VERSION = "1.9.0";
|
|
2203
|
+
var re = /^(\d+)\.(\d+)\.(\d+)(-(.+))?$/;
|
|
2204
|
+
function _makeCompatibilityCheck(ownVersion) {
|
|
2205
|
+
var acceptedVersions = /* @__PURE__ */ new Set([ownVersion]);
|
|
2206
|
+
var rejectedVersions = /* @__PURE__ */ new Set();
|
|
2207
|
+
var myVersionMatch = ownVersion.match(re);
|
|
2208
|
+
if (!myVersionMatch) {
|
|
2209
|
+
return function() {
|
|
2210
|
+
return false;
|
|
2211
|
+
};
|
|
2212
|
+
}
|
|
2213
|
+
var ownVersionParsed = {
|
|
2214
|
+
major: +myVersionMatch[1],
|
|
2215
|
+
minor: +myVersionMatch[2],
|
|
2216
|
+
patch: +myVersionMatch[3],
|
|
2217
|
+
prerelease: myVersionMatch[4]
|
|
2218
|
+
};
|
|
2219
|
+
if (ownVersionParsed.prerelease != null) {
|
|
2220
|
+
return function isExactmatch(globalVersion) {
|
|
2221
|
+
return globalVersion === ownVersion;
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
function _reject(v) {
|
|
2225
|
+
rejectedVersions.add(v);
|
|
2226
|
+
return false;
|
|
2227
|
+
}
|
|
2228
|
+
function _accept(v) {
|
|
2229
|
+
acceptedVersions.add(v);
|
|
2230
|
+
return true;
|
|
2231
|
+
}
|
|
2232
|
+
return function isCompatible2(globalVersion) {
|
|
2233
|
+
if (acceptedVersions.has(globalVersion)) {
|
|
2234
|
+
return true;
|
|
2235
|
+
}
|
|
2236
|
+
if (rejectedVersions.has(globalVersion)) {
|
|
2237
|
+
return false;
|
|
2238
|
+
}
|
|
2239
|
+
var globalVersionMatch = globalVersion.match(re);
|
|
2240
|
+
if (!globalVersionMatch) {
|
|
2241
|
+
return _reject(globalVersion);
|
|
2242
|
+
}
|
|
2243
|
+
var globalVersionParsed = {
|
|
2244
|
+
major: +globalVersionMatch[1],
|
|
2245
|
+
minor: +globalVersionMatch[2],
|
|
2246
|
+
patch: +globalVersionMatch[3],
|
|
2247
|
+
prerelease: globalVersionMatch[4]
|
|
2248
|
+
};
|
|
2249
|
+
if (globalVersionParsed.prerelease != null) {
|
|
2250
|
+
return _reject(globalVersion);
|
|
2251
|
+
}
|
|
2252
|
+
if (ownVersionParsed.major !== globalVersionParsed.major) {
|
|
2253
|
+
return _reject(globalVersion);
|
|
2254
|
+
}
|
|
2255
|
+
if (ownVersionParsed.major === 0) {
|
|
2256
|
+
if (ownVersionParsed.minor === globalVersionParsed.minor && ownVersionParsed.patch <= globalVersionParsed.patch) {
|
|
2257
|
+
return _accept(globalVersion);
|
|
2258
|
+
}
|
|
2259
|
+
return _reject(globalVersion);
|
|
2260
|
+
}
|
|
2261
|
+
if (ownVersionParsed.minor <= globalVersionParsed.minor) {
|
|
2262
|
+
return _accept(globalVersion);
|
|
2263
|
+
}
|
|
2264
|
+
return _reject(globalVersion);
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
var isCompatible = _makeCompatibilityCheck(VERSION);
|
|
2268
|
+
var major = VERSION.split(".")[0];
|
|
2269
|
+
var GLOBAL_OPENTELEMETRY_API_KEY = Symbol.for("opentelemetry.js.api." + major);
|
|
2270
|
+
var _global = _globalThis;
|
|
2271
|
+
function registerGlobal(type, instance, diag, allowOverride) {
|
|
2272
|
+
var _a17;
|
|
2273
|
+
if (allowOverride === void 0) {
|
|
2274
|
+
allowOverride = false;
|
|
2275
|
+
}
|
|
2276
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY] = (_a17 = _global[GLOBAL_OPENTELEMETRY_API_KEY]) !== null && _a17 !== void 0 ? _a17 : {
|
|
2277
|
+
version: VERSION
|
|
2278
|
+
};
|
|
2279
|
+
if (!allowOverride && api[type]) {
|
|
2280
|
+
var err = new Error("@opentelemetry/api: Attempted duplicate registration of API: " + type);
|
|
2281
|
+
diag.error(err.stack || err.message);
|
|
2282
|
+
return false;
|
|
2283
|
+
}
|
|
2284
|
+
if (api.version !== VERSION) {
|
|
2285
|
+
var err = new Error("@opentelemetry/api: Registration of version v" + api.version + " for " + type + " does not match previously registered API v" + VERSION);
|
|
2286
|
+
diag.error(err.stack || err.message);
|
|
2287
|
+
return false;
|
|
2288
|
+
}
|
|
2289
|
+
api[type] = instance;
|
|
2290
|
+
diag.debug("@opentelemetry/api: Registered a global for " + type + " v" + VERSION + ".");
|
|
2291
|
+
return true;
|
|
2292
|
+
}
|
|
2293
|
+
function getGlobal(type) {
|
|
2294
|
+
var _a17, _b;
|
|
2295
|
+
var globalVersion = (_a17 = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _a17 === void 0 ? void 0 : _a17.version;
|
|
2296
|
+
if (!globalVersion || !isCompatible(globalVersion)) {
|
|
2297
|
+
return;
|
|
2298
|
+
}
|
|
2299
|
+
return (_b = _global[GLOBAL_OPENTELEMETRY_API_KEY]) === null || _b === void 0 ? void 0 : _b[type];
|
|
2300
|
+
}
|
|
2301
|
+
function unregisterGlobal(type, diag) {
|
|
2302
|
+
diag.debug("@opentelemetry/api: Unregistering a global for " + type + " v" + VERSION + ".");
|
|
2303
|
+
var api = _global[GLOBAL_OPENTELEMETRY_API_KEY];
|
|
2304
|
+
if (api) {
|
|
2305
|
+
delete api[type];
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
var __read = function(o, n) {
|
|
2309
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
2310
|
+
if (!m) return o;
|
|
2311
|
+
var i = m.call(o), r, ar = [], e;
|
|
2312
|
+
try {
|
|
2313
|
+
while (!(r = i.next()).done) ar.push(r.value);
|
|
2314
|
+
} catch (error) {
|
|
2315
|
+
e = { error };
|
|
2316
|
+
} finally {
|
|
2317
|
+
try {
|
|
2318
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
2319
|
+
} finally {
|
|
2320
|
+
if (e) throw e.error;
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
return ar;
|
|
2324
|
+
};
|
|
2325
|
+
var __spreadArray = function(to, from, pack) {
|
|
2326
|
+
if (arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
2327
|
+
if (ar || !(i in from)) {
|
|
2328
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
2329
|
+
ar[i] = from[i];
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
2333
|
+
};
|
|
2334
|
+
var DiagComponentLogger = (
|
|
2335
|
+
/** @class */
|
|
2336
|
+
(function() {
|
|
2337
|
+
function DiagComponentLogger2(props) {
|
|
2338
|
+
this._namespace = props.namespace || "DiagComponentLogger";
|
|
2339
|
+
}
|
|
2340
|
+
DiagComponentLogger2.prototype.debug = function() {
|
|
2341
|
+
var args = [];
|
|
2342
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2343
|
+
args[_i] = arguments[_i];
|
|
2344
|
+
}
|
|
2345
|
+
return logProxy("debug", this._namespace, args);
|
|
2346
|
+
};
|
|
2347
|
+
DiagComponentLogger2.prototype.error = function() {
|
|
2348
|
+
var args = [];
|
|
2349
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2350
|
+
args[_i] = arguments[_i];
|
|
2351
|
+
}
|
|
2352
|
+
return logProxy("error", this._namespace, args);
|
|
2353
|
+
};
|
|
2354
|
+
DiagComponentLogger2.prototype.info = function() {
|
|
2355
|
+
var args = [];
|
|
2356
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2357
|
+
args[_i] = arguments[_i];
|
|
2358
|
+
}
|
|
2359
|
+
return logProxy("info", this._namespace, args);
|
|
2360
|
+
};
|
|
2361
|
+
DiagComponentLogger2.prototype.warn = function() {
|
|
2362
|
+
var args = [];
|
|
2363
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2364
|
+
args[_i] = arguments[_i];
|
|
2365
|
+
}
|
|
2366
|
+
return logProxy("warn", this._namespace, args);
|
|
2367
|
+
};
|
|
2368
|
+
DiagComponentLogger2.prototype.verbose = function() {
|
|
2369
|
+
var args = [];
|
|
2370
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2371
|
+
args[_i] = arguments[_i];
|
|
2372
|
+
}
|
|
2373
|
+
return logProxy("verbose", this._namespace, args);
|
|
2374
|
+
};
|
|
2375
|
+
return DiagComponentLogger2;
|
|
2376
|
+
})()
|
|
2377
|
+
);
|
|
2378
|
+
function logProxy(funcName, namespace, args) {
|
|
2379
|
+
var logger = getGlobal("diag");
|
|
2380
|
+
if (!logger) {
|
|
2381
|
+
return;
|
|
2382
|
+
}
|
|
2383
|
+
args.unshift(namespace);
|
|
2384
|
+
return logger[funcName].apply(logger, __spreadArray([], __read(args), false));
|
|
2385
|
+
}
|
|
2386
|
+
var DiagLogLevel;
|
|
2387
|
+
(function(DiagLogLevel2) {
|
|
2388
|
+
DiagLogLevel2[DiagLogLevel2["NONE"] = 0] = "NONE";
|
|
2389
|
+
DiagLogLevel2[DiagLogLevel2["ERROR"] = 30] = "ERROR";
|
|
2390
|
+
DiagLogLevel2[DiagLogLevel2["WARN"] = 50] = "WARN";
|
|
2391
|
+
DiagLogLevel2[DiagLogLevel2["INFO"] = 60] = "INFO";
|
|
2392
|
+
DiagLogLevel2[DiagLogLevel2["DEBUG"] = 70] = "DEBUG";
|
|
2393
|
+
DiagLogLevel2[DiagLogLevel2["VERBOSE"] = 80] = "VERBOSE";
|
|
2394
|
+
DiagLogLevel2[DiagLogLevel2["ALL"] = 9999] = "ALL";
|
|
2395
|
+
})(DiagLogLevel || (DiagLogLevel = {}));
|
|
2396
|
+
function createLogLevelDiagLogger(maxLevel, logger) {
|
|
2397
|
+
if (maxLevel < DiagLogLevel.NONE) {
|
|
2398
|
+
maxLevel = DiagLogLevel.NONE;
|
|
2399
|
+
} else if (maxLevel > DiagLogLevel.ALL) {
|
|
2400
|
+
maxLevel = DiagLogLevel.ALL;
|
|
2401
|
+
}
|
|
2402
|
+
logger = logger || {};
|
|
2403
|
+
function _filterFunc(funcName, theLevel) {
|
|
2404
|
+
var theFunc = logger[funcName];
|
|
2405
|
+
if (typeof theFunc === "function" && maxLevel >= theLevel) {
|
|
2406
|
+
return theFunc.bind(logger);
|
|
2407
|
+
}
|
|
2408
|
+
return function() {
|
|
2409
|
+
};
|
|
2410
|
+
}
|
|
2411
|
+
return {
|
|
2412
|
+
error: _filterFunc("error", DiagLogLevel.ERROR),
|
|
2413
|
+
warn: _filterFunc("warn", DiagLogLevel.WARN),
|
|
2414
|
+
info: _filterFunc("info", DiagLogLevel.INFO),
|
|
2415
|
+
debug: _filterFunc("debug", DiagLogLevel.DEBUG),
|
|
2416
|
+
verbose: _filterFunc("verbose", DiagLogLevel.VERBOSE)
|
|
2417
|
+
};
|
|
2418
|
+
}
|
|
2419
|
+
var __read2 = function(o, n) {
|
|
2420
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
2421
|
+
if (!m) return o;
|
|
2422
|
+
var i = m.call(o), r, ar = [], e;
|
|
2423
|
+
try {
|
|
2424
|
+
while (!(r = i.next()).done) ar.push(r.value);
|
|
2425
|
+
} catch (error) {
|
|
2426
|
+
e = { error };
|
|
2427
|
+
} finally {
|
|
2428
|
+
try {
|
|
2429
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
2430
|
+
} finally {
|
|
2431
|
+
if (e) throw e.error;
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
return ar;
|
|
2435
|
+
};
|
|
2436
|
+
var __spreadArray2 = function(to, from, pack) {
|
|
2437
|
+
if (arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
2438
|
+
if (ar || !(i in from)) {
|
|
2439
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
2440
|
+
ar[i] = from[i];
|
|
2441
|
+
}
|
|
2442
|
+
}
|
|
2443
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
2444
|
+
};
|
|
2445
|
+
var API_NAME = "diag";
|
|
2446
|
+
var DiagAPI = (
|
|
2447
|
+
/** @class */
|
|
2448
|
+
(function() {
|
|
2449
|
+
function DiagAPI2() {
|
|
2450
|
+
function _logProxy(funcName) {
|
|
2451
|
+
return function() {
|
|
2452
|
+
var args = [];
|
|
2453
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
2454
|
+
args[_i] = arguments[_i];
|
|
2455
|
+
}
|
|
2456
|
+
var logger = getGlobal("diag");
|
|
2457
|
+
if (!logger)
|
|
2458
|
+
return;
|
|
2459
|
+
return logger[funcName].apply(logger, __spreadArray2([], __read2(args), false));
|
|
2460
|
+
};
|
|
2461
|
+
}
|
|
2462
|
+
var self = this;
|
|
2463
|
+
var setLogger = function(logger, optionsOrLogLevel) {
|
|
2464
|
+
var _a17, _b, _c;
|
|
2465
|
+
if (optionsOrLogLevel === void 0) {
|
|
2466
|
+
optionsOrLogLevel = { logLevel: DiagLogLevel.INFO };
|
|
2467
|
+
}
|
|
2468
|
+
if (logger === self) {
|
|
2469
|
+
var err = new Error("Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation");
|
|
2470
|
+
self.error((_a17 = err.stack) !== null && _a17 !== void 0 ? _a17 : err.message);
|
|
2471
|
+
return false;
|
|
2472
|
+
}
|
|
2473
|
+
if (typeof optionsOrLogLevel === "number") {
|
|
2474
|
+
optionsOrLogLevel = {
|
|
2475
|
+
logLevel: optionsOrLogLevel
|
|
2476
|
+
};
|
|
2477
|
+
}
|
|
2478
|
+
var oldLogger = getGlobal("diag");
|
|
2479
|
+
var newLogger = createLogLevelDiagLogger((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : DiagLogLevel.INFO, logger);
|
|
2480
|
+
if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
|
|
2481
|
+
var stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : "<failed to generate stacktrace>";
|
|
2482
|
+
oldLogger.warn("Current logger will be overwritten from " + stack);
|
|
2483
|
+
newLogger.warn("Current logger will overwrite one already registered from " + stack);
|
|
2484
|
+
}
|
|
2485
|
+
return registerGlobal("diag", newLogger, self, true);
|
|
2486
|
+
};
|
|
2487
|
+
self.setLogger = setLogger;
|
|
2488
|
+
self.disable = function() {
|
|
2489
|
+
unregisterGlobal(API_NAME, self);
|
|
2490
|
+
};
|
|
2491
|
+
self.createComponentLogger = function(options) {
|
|
2492
|
+
return new DiagComponentLogger(options);
|
|
2493
|
+
};
|
|
2494
|
+
self.verbose = _logProxy("verbose");
|
|
2495
|
+
self.debug = _logProxy("debug");
|
|
2496
|
+
self.info = _logProxy("info");
|
|
2497
|
+
self.warn = _logProxy("warn");
|
|
2498
|
+
self.error = _logProxy("error");
|
|
2499
|
+
}
|
|
2500
|
+
DiagAPI2.instance = function() {
|
|
2501
|
+
if (!this._instance) {
|
|
2502
|
+
this._instance = new DiagAPI2();
|
|
2503
|
+
}
|
|
2504
|
+
return this._instance;
|
|
2505
|
+
};
|
|
2506
|
+
return DiagAPI2;
|
|
2507
|
+
})()
|
|
2508
|
+
);
|
|
2509
|
+
function createContextKey(description) {
|
|
2510
|
+
return Symbol.for(description);
|
|
2511
|
+
}
|
|
2512
|
+
var BaseContext = (
|
|
2513
|
+
/** @class */
|
|
2514
|
+
/* @__PURE__ */ (function() {
|
|
2515
|
+
function BaseContext2(parentContext) {
|
|
2516
|
+
var self = this;
|
|
2517
|
+
self._currentContext = parentContext ? new Map(parentContext) : /* @__PURE__ */ new Map();
|
|
2518
|
+
self.getValue = function(key) {
|
|
2519
|
+
return self._currentContext.get(key);
|
|
2520
|
+
};
|
|
2521
|
+
self.setValue = function(key, value) {
|
|
2522
|
+
var context = new BaseContext2(self._currentContext);
|
|
2523
|
+
context._currentContext.set(key, value);
|
|
2524
|
+
return context;
|
|
2525
|
+
};
|
|
2526
|
+
self.deleteValue = function(key) {
|
|
2527
|
+
var context = new BaseContext2(self._currentContext);
|
|
2528
|
+
context._currentContext.delete(key);
|
|
2529
|
+
return context;
|
|
2530
|
+
};
|
|
2531
|
+
}
|
|
2532
|
+
return BaseContext2;
|
|
2533
|
+
})()
|
|
2534
|
+
);
|
|
2535
|
+
var ROOT_CONTEXT = new BaseContext();
|
|
2536
|
+
var __read3 = function(o, n) {
|
|
2537
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
2538
|
+
if (!m) return o;
|
|
2539
|
+
var i = m.call(o), r, ar = [], e;
|
|
2540
|
+
try {
|
|
2541
|
+
while (!(r = i.next()).done) ar.push(r.value);
|
|
2542
|
+
} catch (error) {
|
|
2543
|
+
e = { error };
|
|
2544
|
+
} finally {
|
|
2545
|
+
try {
|
|
2546
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
2547
|
+
} finally {
|
|
2548
|
+
if (e) throw e.error;
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
return ar;
|
|
2552
|
+
};
|
|
2553
|
+
var __spreadArray3 = function(to, from, pack) {
|
|
2554
|
+
if (arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
2555
|
+
if (ar || !(i in from)) {
|
|
2556
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
2557
|
+
ar[i] = from[i];
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
2561
|
+
};
|
|
2562
|
+
var NoopContextManager = (
|
|
2563
|
+
/** @class */
|
|
2564
|
+
(function() {
|
|
2565
|
+
function NoopContextManager2() {
|
|
2566
|
+
}
|
|
2567
|
+
NoopContextManager2.prototype.active = function() {
|
|
2568
|
+
return ROOT_CONTEXT;
|
|
2569
|
+
};
|
|
2570
|
+
NoopContextManager2.prototype.with = function(_context, fn, thisArg) {
|
|
2571
|
+
var args = [];
|
|
2572
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
2573
|
+
args[_i - 3] = arguments[_i];
|
|
2574
|
+
}
|
|
2575
|
+
return fn.call.apply(fn, __spreadArray3([thisArg], __read3(args), false));
|
|
2576
|
+
};
|
|
2577
|
+
NoopContextManager2.prototype.bind = function(_context, target) {
|
|
2578
|
+
return target;
|
|
2579
|
+
};
|
|
2580
|
+
NoopContextManager2.prototype.enable = function() {
|
|
2581
|
+
return this;
|
|
2582
|
+
};
|
|
2583
|
+
NoopContextManager2.prototype.disable = function() {
|
|
2584
|
+
return this;
|
|
2585
|
+
};
|
|
2586
|
+
return NoopContextManager2;
|
|
2587
|
+
})()
|
|
2588
|
+
);
|
|
2589
|
+
var __read4 = function(o, n) {
|
|
2590
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
2591
|
+
if (!m) return o;
|
|
2592
|
+
var i = m.call(o), r, ar = [], e;
|
|
2593
|
+
try {
|
|
2594
|
+
while (!(r = i.next()).done) ar.push(r.value);
|
|
2595
|
+
} catch (error) {
|
|
2596
|
+
e = { error };
|
|
2597
|
+
} finally {
|
|
2598
|
+
try {
|
|
2599
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
2600
|
+
} finally {
|
|
2601
|
+
if (e) throw e.error;
|
|
2602
|
+
}
|
|
2603
|
+
}
|
|
2604
|
+
return ar;
|
|
2605
|
+
};
|
|
2606
|
+
var __spreadArray4 = function(to, from, pack) {
|
|
2607
|
+
if (arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
2608
|
+
if (ar || !(i in from)) {
|
|
2609
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
2610
|
+
ar[i] = from[i];
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
2614
|
+
};
|
|
2615
|
+
var API_NAME2 = "context";
|
|
2616
|
+
var NOOP_CONTEXT_MANAGER = new NoopContextManager();
|
|
2617
|
+
var ContextAPI = (
|
|
2618
|
+
/** @class */
|
|
2619
|
+
(function() {
|
|
2620
|
+
function ContextAPI2() {
|
|
2621
|
+
}
|
|
2622
|
+
ContextAPI2.getInstance = function() {
|
|
2623
|
+
if (!this._instance) {
|
|
2624
|
+
this._instance = new ContextAPI2();
|
|
2625
|
+
}
|
|
2626
|
+
return this._instance;
|
|
2627
|
+
};
|
|
2628
|
+
ContextAPI2.prototype.setGlobalContextManager = function(contextManager) {
|
|
2629
|
+
return registerGlobal(API_NAME2, contextManager, DiagAPI.instance());
|
|
2630
|
+
};
|
|
2631
|
+
ContextAPI2.prototype.active = function() {
|
|
2632
|
+
return this._getContextManager().active();
|
|
2633
|
+
};
|
|
2634
|
+
ContextAPI2.prototype.with = function(context, fn, thisArg) {
|
|
2635
|
+
var _a17;
|
|
2636
|
+
var args = [];
|
|
2637
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
2638
|
+
args[_i - 3] = arguments[_i];
|
|
2639
|
+
}
|
|
2640
|
+
return (_a17 = this._getContextManager()).with.apply(_a17, __spreadArray4([context, fn, thisArg], __read4(args), false));
|
|
2641
|
+
};
|
|
2642
|
+
ContextAPI2.prototype.bind = function(context, target) {
|
|
2643
|
+
return this._getContextManager().bind(context, target);
|
|
2644
|
+
};
|
|
2645
|
+
ContextAPI2.prototype._getContextManager = function() {
|
|
2646
|
+
return getGlobal(API_NAME2) || NOOP_CONTEXT_MANAGER;
|
|
2647
|
+
};
|
|
2648
|
+
ContextAPI2.prototype.disable = function() {
|
|
2649
|
+
this._getContextManager().disable();
|
|
2650
|
+
unregisterGlobal(API_NAME2, DiagAPI.instance());
|
|
2651
|
+
};
|
|
2652
|
+
return ContextAPI2;
|
|
2653
|
+
})()
|
|
2654
|
+
);
|
|
2655
|
+
var TraceFlags;
|
|
2656
|
+
(function(TraceFlags2) {
|
|
2657
|
+
TraceFlags2[TraceFlags2["NONE"] = 0] = "NONE";
|
|
2658
|
+
TraceFlags2[TraceFlags2["SAMPLED"] = 1] = "SAMPLED";
|
|
2659
|
+
})(TraceFlags || (TraceFlags = {}));
|
|
2660
|
+
var INVALID_SPANID = "0000000000000000";
|
|
2661
|
+
var INVALID_TRACEID = "00000000000000000000000000000000";
|
|
2662
|
+
var INVALID_SPAN_CONTEXT = {
|
|
2663
|
+
traceId: INVALID_TRACEID,
|
|
2664
|
+
spanId: INVALID_SPANID,
|
|
2665
|
+
traceFlags: TraceFlags.NONE
|
|
2666
|
+
};
|
|
2667
|
+
var NonRecordingSpan = (
|
|
2668
|
+
/** @class */
|
|
2669
|
+
(function() {
|
|
2670
|
+
function NonRecordingSpan2(_spanContext) {
|
|
2671
|
+
if (_spanContext === void 0) {
|
|
2672
|
+
_spanContext = INVALID_SPAN_CONTEXT;
|
|
2673
|
+
}
|
|
2674
|
+
this._spanContext = _spanContext;
|
|
2675
|
+
}
|
|
2676
|
+
NonRecordingSpan2.prototype.spanContext = function() {
|
|
2677
|
+
return this._spanContext;
|
|
2678
|
+
};
|
|
2679
|
+
NonRecordingSpan2.prototype.setAttribute = function(_key, _value) {
|
|
2680
|
+
return this;
|
|
2681
|
+
};
|
|
2682
|
+
NonRecordingSpan2.prototype.setAttributes = function(_attributes) {
|
|
2683
|
+
return this;
|
|
2684
|
+
};
|
|
2685
|
+
NonRecordingSpan2.prototype.addEvent = function(_name, _attributes) {
|
|
2686
|
+
return this;
|
|
2687
|
+
};
|
|
2688
|
+
NonRecordingSpan2.prototype.addLink = function(_link) {
|
|
2689
|
+
return this;
|
|
2690
|
+
};
|
|
2691
|
+
NonRecordingSpan2.prototype.addLinks = function(_links) {
|
|
2692
|
+
return this;
|
|
2693
|
+
};
|
|
2694
|
+
NonRecordingSpan2.prototype.setStatus = function(_status) {
|
|
2695
|
+
return this;
|
|
2696
|
+
};
|
|
2697
|
+
NonRecordingSpan2.prototype.updateName = function(_name) {
|
|
2698
|
+
return this;
|
|
2699
|
+
};
|
|
2700
|
+
NonRecordingSpan2.prototype.end = function(_endTime) {
|
|
2701
|
+
};
|
|
2702
|
+
NonRecordingSpan2.prototype.isRecording = function() {
|
|
2703
|
+
return false;
|
|
2704
|
+
};
|
|
2705
|
+
NonRecordingSpan2.prototype.recordException = function(_exception, _time) {
|
|
2706
|
+
};
|
|
2707
|
+
return NonRecordingSpan2;
|
|
2708
|
+
})()
|
|
2709
|
+
);
|
|
2710
|
+
var SPAN_KEY = createContextKey("OpenTelemetry Context Key SPAN");
|
|
2711
|
+
function getSpan(context) {
|
|
2712
|
+
return context.getValue(SPAN_KEY) || void 0;
|
|
2713
|
+
}
|
|
2714
|
+
function getActiveSpan() {
|
|
2715
|
+
return getSpan(ContextAPI.getInstance().active());
|
|
2716
|
+
}
|
|
2717
|
+
function setSpan(context, span) {
|
|
2718
|
+
return context.setValue(SPAN_KEY, span);
|
|
2719
|
+
}
|
|
2720
|
+
function deleteSpan(context) {
|
|
2721
|
+
return context.deleteValue(SPAN_KEY);
|
|
2722
|
+
}
|
|
2723
|
+
function setSpanContext(context, spanContext) {
|
|
2724
|
+
return setSpan(context, new NonRecordingSpan(spanContext));
|
|
2725
|
+
}
|
|
2726
|
+
function getSpanContext(context) {
|
|
2727
|
+
var _a17;
|
|
2728
|
+
return (_a17 = getSpan(context)) === null || _a17 === void 0 ? void 0 : _a17.spanContext();
|
|
2729
|
+
}
|
|
2730
|
+
var VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i;
|
|
2731
|
+
var VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i;
|
|
2732
|
+
function isValidTraceId(traceId) {
|
|
2733
|
+
return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID;
|
|
2734
|
+
}
|
|
2735
|
+
function isValidSpanId(spanId) {
|
|
2736
|
+
return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID;
|
|
2737
|
+
}
|
|
2738
|
+
function isSpanContextValid(spanContext) {
|
|
2739
|
+
return isValidTraceId(spanContext.traceId) && isValidSpanId(spanContext.spanId);
|
|
2740
|
+
}
|
|
2741
|
+
function wrapSpanContext(spanContext) {
|
|
2742
|
+
return new NonRecordingSpan(spanContext);
|
|
2743
|
+
}
|
|
2744
|
+
var contextApi = ContextAPI.getInstance();
|
|
2745
|
+
var NoopTracer = (
|
|
2746
|
+
/** @class */
|
|
2747
|
+
(function() {
|
|
2748
|
+
function NoopTracer2() {
|
|
2749
|
+
}
|
|
2750
|
+
NoopTracer2.prototype.startSpan = function(name17, options, context) {
|
|
2751
|
+
if (context === void 0) {
|
|
2752
|
+
context = contextApi.active();
|
|
2753
|
+
}
|
|
2754
|
+
var root = Boolean(options === null || options === void 0 ? void 0 : options.root);
|
|
2755
|
+
if (root) {
|
|
2756
|
+
return new NonRecordingSpan();
|
|
2757
|
+
}
|
|
2758
|
+
var parentFromContext = context && getSpanContext(context);
|
|
2759
|
+
if (isSpanContext(parentFromContext) && isSpanContextValid(parentFromContext)) {
|
|
2760
|
+
return new NonRecordingSpan(parentFromContext);
|
|
2761
|
+
} else {
|
|
2762
|
+
return new NonRecordingSpan();
|
|
2763
|
+
}
|
|
2764
|
+
};
|
|
2765
|
+
NoopTracer2.prototype.startActiveSpan = function(name17, arg2, arg3, arg4) {
|
|
2766
|
+
var opts;
|
|
2767
|
+
var ctx;
|
|
2768
|
+
var fn;
|
|
2769
|
+
if (arguments.length < 2) {
|
|
2770
|
+
return;
|
|
2771
|
+
} else if (arguments.length === 2) {
|
|
2772
|
+
fn = arg2;
|
|
2773
|
+
} else if (arguments.length === 3) {
|
|
2774
|
+
opts = arg2;
|
|
2775
|
+
fn = arg3;
|
|
2776
|
+
} else {
|
|
2777
|
+
opts = arg2;
|
|
2778
|
+
ctx = arg3;
|
|
2779
|
+
fn = arg4;
|
|
2780
|
+
}
|
|
2781
|
+
var parentContext = ctx !== null && ctx !== void 0 ? ctx : contextApi.active();
|
|
2782
|
+
var span = this.startSpan(name17, opts, parentContext);
|
|
2783
|
+
var contextWithSpanSet = setSpan(parentContext, span);
|
|
2784
|
+
return contextApi.with(contextWithSpanSet, fn, void 0, span);
|
|
2785
|
+
};
|
|
2786
|
+
return NoopTracer2;
|
|
2787
|
+
})()
|
|
2788
|
+
);
|
|
2789
|
+
function isSpanContext(spanContext) {
|
|
2790
|
+
return typeof spanContext === "object" && typeof spanContext["spanId"] === "string" && typeof spanContext["traceId"] === "string" && typeof spanContext["traceFlags"] === "number";
|
|
2791
|
+
}
|
|
2792
|
+
var NOOP_TRACER = new NoopTracer();
|
|
2793
|
+
var ProxyTracer = (
|
|
2794
|
+
/** @class */
|
|
2795
|
+
(function() {
|
|
2796
|
+
function ProxyTracer2(_provider, name17, version, options) {
|
|
2797
|
+
this._provider = _provider;
|
|
2798
|
+
this.name = name17;
|
|
2799
|
+
this.version = version;
|
|
2800
|
+
this.options = options;
|
|
2801
|
+
}
|
|
2802
|
+
ProxyTracer2.prototype.startSpan = function(name17, options, context) {
|
|
2803
|
+
return this._getTracer().startSpan(name17, options, context);
|
|
2804
|
+
};
|
|
2805
|
+
ProxyTracer2.prototype.startActiveSpan = function(_name, _options, _context, _fn) {
|
|
2806
|
+
var tracer = this._getTracer();
|
|
2807
|
+
return Reflect.apply(tracer.startActiveSpan, tracer, arguments);
|
|
2808
|
+
};
|
|
2809
|
+
ProxyTracer2.prototype._getTracer = function() {
|
|
2810
|
+
if (this._delegate) {
|
|
2811
|
+
return this._delegate;
|
|
2812
|
+
}
|
|
2813
|
+
var tracer = this._provider.getDelegateTracer(this.name, this.version, this.options);
|
|
2814
|
+
if (!tracer) {
|
|
2815
|
+
return NOOP_TRACER;
|
|
2816
|
+
}
|
|
2817
|
+
this._delegate = tracer;
|
|
2818
|
+
return this._delegate;
|
|
2819
|
+
};
|
|
2820
|
+
return ProxyTracer2;
|
|
2821
|
+
})()
|
|
2822
|
+
);
|
|
2823
|
+
var NoopTracerProvider = (
|
|
2824
|
+
/** @class */
|
|
2825
|
+
(function() {
|
|
2826
|
+
function NoopTracerProvider2() {
|
|
2827
|
+
}
|
|
2828
|
+
NoopTracerProvider2.prototype.getTracer = function(_name, _version, _options) {
|
|
2829
|
+
return new NoopTracer();
|
|
2830
|
+
};
|
|
2831
|
+
return NoopTracerProvider2;
|
|
2832
|
+
})()
|
|
2833
|
+
);
|
|
2834
|
+
var NOOP_TRACER_PROVIDER = new NoopTracerProvider();
|
|
2835
|
+
var ProxyTracerProvider = (
|
|
2836
|
+
/** @class */
|
|
2837
|
+
(function() {
|
|
2838
|
+
function ProxyTracerProvider2() {
|
|
2839
|
+
}
|
|
2840
|
+
ProxyTracerProvider2.prototype.getTracer = function(name17, version, options) {
|
|
2841
|
+
var _a17;
|
|
2842
|
+
return (_a17 = this.getDelegateTracer(name17, version, options)) !== null && _a17 !== void 0 ? _a17 : new ProxyTracer(this, name17, version, options);
|
|
2843
|
+
};
|
|
2844
|
+
ProxyTracerProvider2.prototype.getDelegate = function() {
|
|
2845
|
+
var _a17;
|
|
2846
|
+
return (_a17 = this._delegate) !== null && _a17 !== void 0 ? _a17 : NOOP_TRACER_PROVIDER;
|
|
2847
|
+
};
|
|
2848
|
+
ProxyTracerProvider2.prototype.setDelegate = function(delegate) {
|
|
2849
|
+
this._delegate = delegate;
|
|
2850
|
+
};
|
|
2851
|
+
ProxyTracerProvider2.prototype.getDelegateTracer = function(name17, version, options) {
|
|
2852
|
+
var _a17;
|
|
2853
|
+
return (_a17 = this._delegate) === null || _a17 === void 0 ? void 0 : _a17.getTracer(name17, version, options);
|
|
2854
|
+
};
|
|
2855
|
+
return ProxyTracerProvider2;
|
|
2856
|
+
})()
|
|
2857
|
+
);
|
|
2858
|
+
var SpanStatusCode;
|
|
2859
|
+
(function(SpanStatusCode2) {
|
|
2860
|
+
SpanStatusCode2[SpanStatusCode2["UNSET"] = 0] = "UNSET";
|
|
2861
|
+
SpanStatusCode2[SpanStatusCode2["OK"] = 1] = "OK";
|
|
2862
|
+
SpanStatusCode2[SpanStatusCode2["ERROR"] = 2] = "ERROR";
|
|
2863
|
+
})(SpanStatusCode || (SpanStatusCode = {}));
|
|
2864
|
+
var API_NAME3 = "trace";
|
|
2865
|
+
var TraceAPI = (
|
|
2866
|
+
/** @class */
|
|
2867
|
+
(function() {
|
|
2868
|
+
function TraceAPI2() {
|
|
2869
|
+
this._proxyTracerProvider = new ProxyTracerProvider();
|
|
2870
|
+
this.wrapSpanContext = wrapSpanContext;
|
|
2871
|
+
this.isSpanContextValid = isSpanContextValid;
|
|
2872
|
+
this.deleteSpan = deleteSpan;
|
|
2873
|
+
this.getSpan = getSpan;
|
|
2874
|
+
this.getActiveSpan = getActiveSpan;
|
|
2875
|
+
this.getSpanContext = getSpanContext;
|
|
2876
|
+
this.setSpan = setSpan;
|
|
2877
|
+
this.setSpanContext = setSpanContext;
|
|
2878
|
+
}
|
|
2879
|
+
TraceAPI2.getInstance = function() {
|
|
2880
|
+
if (!this._instance) {
|
|
2881
|
+
this._instance = new TraceAPI2();
|
|
2882
|
+
}
|
|
2883
|
+
return this._instance;
|
|
2884
|
+
};
|
|
2885
|
+
TraceAPI2.prototype.setGlobalTracerProvider = function(provider) {
|
|
2886
|
+
var success = registerGlobal(API_NAME3, this._proxyTracerProvider, DiagAPI.instance());
|
|
2887
|
+
if (success) {
|
|
2888
|
+
this._proxyTracerProvider.setDelegate(provider);
|
|
2889
|
+
}
|
|
2890
|
+
return success;
|
|
2891
|
+
};
|
|
2892
|
+
TraceAPI2.prototype.getTracerProvider = function() {
|
|
2893
|
+
return getGlobal(API_NAME3) || this._proxyTracerProvider;
|
|
2894
|
+
};
|
|
2895
|
+
TraceAPI2.prototype.getTracer = function(name17, version) {
|
|
2896
|
+
return this.getTracerProvider().getTracer(name17, version);
|
|
2897
|
+
};
|
|
2898
|
+
TraceAPI2.prototype.disable = function() {
|
|
2899
|
+
unregisterGlobal(API_NAME3, DiagAPI.instance());
|
|
2900
|
+
this._proxyTracerProvider = new ProxyTracerProvider();
|
|
2901
|
+
};
|
|
2902
|
+
return TraceAPI2;
|
|
2903
|
+
})()
|
|
2904
|
+
);
|
|
2905
|
+
TraceAPI.getInstance();
|
|
2906
|
+
var __defProp2 = Object.defineProperty;
|
|
2907
|
+
var __export = (target, all) => {
|
|
2908
|
+
for (var name17 in all)
|
|
2909
|
+
__defProp2(target, name17, { get: all[name17], enumerable: true });
|
|
2910
|
+
};
|
|
2911
|
+
function prepareResponseHeaders(headers, {
|
|
2912
|
+
contentType,
|
|
2913
|
+
dataStreamVersion
|
|
2914
|
+
}) {
|
|
2915
|
+
const responseHeaders = new Headers(headers != null ? headers : {});
|
|
2916
|
+
if (!responseHeaders.has("Content-Type")) {
|
|
2917
|
+
responseHeaders.set("Content-Type", contentType);
|
|
2918
|
+
}
|
|
2919
|
+
{
|
|
2920
|
+
responseHeaders.set("X-Vercel-AI-Data-Stream", dataStreamVersion);
|
|
2921
|
+
}
|
|
2922
|
+
return responseHeaders;
|
|
2923
|
+
}
|
|
2924
|
+
var name42 = "AI_NoObjectGeneratedError";
|
|
2925
|
+
var marker42 = `vercel.ai.error.${name42}`;
|
|
2926
|
+
var symbol42 = Symbol.for(marker42);
|
|
2927
|
+
var _a42;
|
|
2928
|
+
var NoObjectGeneratedError = class extends AISDKError {
|
|
2929
|
+
constructor({
|
|
2930
|
+
message = "No object generated.",
|
|
2931
|
+
cause,
|
|
2932
|
+
text: text2,
|
|
2933
|
+
response,
|
|
2934
|
+
usage,
|
|
2935
|
+
finishReason
|
|
2936
|
+
}) {
|
|
2937
|
+
super({ name: name42, message, cause });
|
|
2938
|
+
this[_a42] = true;
|
|
2939
|
+
this.text = text2;
|
|
2940
|
+
this.response = response;
|
|
2941
|
+
this.usage = usage;
|
|
2942
|
+
this.finishReason = finishReason;
|
|
2943
|
+
}
|
|
2944
|
+
static isInstance(error) {
|
|
2945
|
+
return AISDKError.hasMarker(error, marker42);
|
|
2946
|
+
}
|
|
2947
|
+
};
|
|
2948
|
+
_a42 = symbol42;
|
|
2949
|
+
var dataContentSchema = z.union([
|
|
2950
|
+
z.string(),
|
|
2951
|
+
z.instanceof(Uint8Array),
|
|
2952
|
+
z.instanceof(ArrayBuffer),
|
|
2953
|
+
z.custom(
|
|
2954
|
+
// Buffer might not be available in some environments such as CloudFlare:
|
|
2955
|
+
(value) => {
|
|
2956
|
+
var _a17, _b;
|
|
2957
|
+
return (_b = (_a17 = globalThis.Buffer) == null ? void 0 : _a17.isBuffer(value)) != null ? _b : false;
|
|
2958
|
+
},
|
|
2959
|
+
{ message: "Must be a Buffer" }
|
|
2960
|
+
)
|
|
2961
|
+
]);
|
|
2962
|
+
var jsonValueSchema = z.lazy(
|
|
2963
|
+
() => z.union([
|
|
2964
|
+
z.null(),
|
|
2965
|
+
z.string(),
|
|
2966
|
+
z.number(),
|
|
2967
|
+
z.boolean(),
|
|
2968
|
+
z.record(z.string(), jsonValueSchema),
|
|
2969
|
+
z.array(jsonValueSchema)
|
|
2970
|
+
])
|
|
2971
|
+
);
|
|
2972
|
+
var providerMetadataSchema = z.record(
|
|
2973
|
+
z.string(),
|
|
2974
|
+
z.record(z.string(), jsonValueSchema)
|
|
2975
|
+
);
|
|
2976
|
+
var toolResultContentSchema = z.array(
|
|
2977
|
+
z.union([
|
|
2978
|
+
z.object({ type: z.literal("text"), text: z.string() }),
|
|
2979
|
+
z.object({
|
|
2980
|
+
type: z.literal("image"),
|
|
2981
|
+
data: z.string(),
|
|
2982
|
+
mimeType: z.string().optional()
|
|
2983
|
+
})
|
|
2984
|
+
])
|
|
2985
|
+
);
|
|
2986
|
+
var textPartSchema = z.object({
|
|
2987
|
+
type: z.literal("text"),
|
|
2988
|
+
text: z.string(),
|
|
2989
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
2990
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
2991
|
+
});
|
|
2992
|
+
var imagePartSchema = z.object({
|
|
2993
|
+
type: z.literal("image"),
|
|
2994
|
+
image: z.union([dataContentSchema, z.instanceof(URL)]),
|
|
2995
|
+
mimeType: z.string().optional(),
|
|
2996
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
2997
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
2998
|
+
});
|
|
2999
|
+
var filePartSchema = z.object({
|
|
3000
|
+
type: z.literal("file"),
|
|
3001
|
+
data: z.union([dataContentSchema, z.instanceof(URL)]),
|
|
3002
|
+
filename: z.string().optional(),
|
|
3003
|
+
mimeType: z.string(),
|
|
3004
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3005
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3006
|
+
});
|
|
3007
|
+
var reasoningPartSchema = z.object({
|
|
3008
|
+
type: z.literal("reasoning"),
|
|
3009
|
+
text: z.string(),
|
|
3010
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3011
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3012
|
+
});
|
|
3013
|
+
var redactedReasoningPartSchema = z.object({
|
|
3014
|
+
type: z.literal("redacted-reasoning"),
|
|
3015
|
+
data: z.string(),
|
|
3016
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3017
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3018
|
+
});
|
|
3019
|
+
var toolCallPartSchema = z.object({
|
|
3020
|
+
type: z.literal("tool-call"),
|
|
3021
|
+
toolCallId: z.string(),
|
|
3022
|
+
toolName: z.string(),
|
|
3023
|
+
args: z.unknown(),
|
|
3024
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3025
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3026
|
+
});
|
|
3027
|
+
var toolResultPartSchema = z.object({
|
|
3028
|
+
type: z.literal("tool-result"),
|
|
3029
|
+
toolCallId: z.string(),
|
|
3030
|
+
toolName: z.string(),
|
|
3031
|
+
result: z.unknown(),
|
|
3032
|
+
content: toolResultContentSchema.optional(),
|
|
3033
|
+
isError: z.boolean().optional(),
|
|
3034
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3035
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3036
|
+
});
|
|
3037
|
+
var coreSystemMessageSchema = z.object({
|
|
3038
|
+
role: z.literal("system"),
|
|
3039
|
+
content: z.string(),
|
|
3040
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3041
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3042
|
+
});
|
|
3043
|
+
var coreUserMessageSchema = z.object({
|
|
3044
|
+
role: z.literal("user"),
|
|
3045
|
+
content: z.union([
|
|
3046
|
+
z.string(),
|
|
3047
|
+
z.array(z.union([textPartSchema, imagePartSchema, filePartSchema]))
|
|
3048
|
+
]),
|
|
3049
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3050
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3051
|
+
});
|
|
3052
|
+
var coreAssistantMessageSchema = z.object({
|
|
3053
|
+
role: z.literal("assistant"),
|
|
3054
|
+
content: z.union([
|
|
3055
|
+
z.string(),
|
|
3056
|
+
z.array(
|
|
3057
|
+
z.union([
|
|
3058
|
+
textPartSchema,
|
|
3059
|
+
filePartSchema,
|
|
3060
|
+
reasoningPartSchema,
|
|
3061
|
+
redactedReasoningPartSchema,
|
|
3062
|
+
toolCallPartSchema
|
|
3063
|
+
])
|
|
3064
|
+
)
|
|
3065
|
+
]),
|
|
3066
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3067
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3068
|
+
});
|
|
3069
|
+
var coreToolMessageSchema = z.object({
|
|
3070
|
+
role: z.literal("tool"),
|
|
3071
|
+
content: z.array(toolResultPartSchema),
|
|
3072
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
3073
|
+
experimental_providerMetadata: providerMetadataSchema.optional()
|
|
3074
|
+
});
|
|
3075
|
+
z.union([
|
|
3076
|
+
coreSystemMessageSchema,
|
|
3077
|
+
coreUserMessageSchema,
|
|
3078
|
+
coreAssistantMessageSchema,
|
|
3079
|
+
coreToolMessageSchema
|
|
3080
|
+
]);
|
|
3081
|
+
var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
|
|
3082
|
+
var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
|
|
3083
|
+
var DEFAULT_GENERIC_SUFFIX = "You MUST answer with JSON.";
|
|
3084
|
+
function injectJsonInstruction({
|
|
3085
|
+
prompt,
|
|
3086
|
+
schema,
|
|
3087
|
+
schemaPrefix = schema != null ? DEFAULT_SCHEMA_PREFIX : void 0,
|
|
3088
|
+
schemaSuffix = schema != null ? DEFAULT_SCHEMA_SUFFIX : DEFAULT_GENERIC_SUFFIX
|
|
3089
|
+
}) {
|
|
3090
|
+
return [
|
|
3091
|
+
prompt != null && prompt.length > 0 ? prompt : void 0,
|
|
3092
|
+
prompt != null && prompt.length > 0 ? "" : void 0,
|
|
3093
|
+
// add a newline if prompt is not null
|
|
3094
|
+
schemaPrefix,
|
|
3095
|
+
schema != null ? JSON.stringify(schema) : void 0,
|
|
3096
|
+
schemaSuffix
|
|
3097
|
+
].filter((line) => line != null).join("\n");
|
|
3098
|
+
}
|
|
3099
|
+
createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
3100
|
+
createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
3101
|
+
createIdGenerator({
|
|
3102
|
+
prefix: "aitxt",
|
|
3103
|
+
size: 24
|
|
3104
|
+
});
|
|
3105
|
+
createIdGenerator({
|
|
3106
|
+
prefix: "msg",
|
|
3107
|
+
size: 24
|
|
3108
|
+
});
|
|
3109
|
+
var output_exports = {};
|
|
3110
|
+
__export(output_exports, {
|
|
3111
|
+
object: () => object,
|
|
3112
|
+
text: () => text
|
|
3113
|
+
});
|
|
3114
|
+
var text = () => ({
|
|
3115
|
+
type: "text",
|
|
3116
|
+
responseFormat: () => ({ type: "text" }),
|
|
3117
|
+
injectIntoSystemPrompt({ system }) {
|
|
3118
|
+
return system;
|
|
3119
|
+
},
|
|
3120
|
+
parsePartial({ text: text2 }) {
|
|
3121
|
+
return { partial: text2 };
|
|
3122
|
+
},
|
|
3123
|
+
parseOutput({ text: text2 }) {
|
|
3124
|
+
return text2;
|
|
3125
|
+
}
|
|
3126
|
+
});
|
|
3127
|
+
var object = ({
|
|
3128
|
+
schema: inputSchema
|
|
3129
|
+
}) => {
|
|
3130
|
+
const schema = asSchema(inputSchema);
|
|
3131
|
+
return {
|
|
3132
|
+
type: "object",
|
|
3133
|
+
responseFormat: ({ model }) => ({
|
|
3134
|
+
type: "json",
|
|
3135
|
+
schema: model.supportsStructuredOutputs ? schema.jsonSchema : void 0
|
|
3136
|
+
}),
|
|
3137
|
+
injectIntoSystemPrompt({ system, model }) {
|
|
3138
|
+
return model.supportsStructuredOutputs ? system : injectJsonInstruction({
|
|
3139
|
+
prompt: system,
|
|
3140
|
+
schema: schema.jsonSchema
|
|
3141
|
+
});
|
|
3142
|
+
},
|
|
3143
|
+
parsePartial({ text: text2 }) {
|
|
3144
|
+
const result = parsePartialJson(text2);
|
|
3145
|
+
switch (result.state) {
|
|
3146
|
+
case "failed-parse":
|
|
3147
|
+
case "undefined-input":
|
|
3148
|
+
return void 0;
|
|
3149
|
+
case "repaired-parse":
|
|
3150
|
+
case "successful-parse":
|
|
3151
|
+
return {
|
|
3152
|
+
// Note: currently no validation of partial results:
|
|
3153
|
+
partial: result.value
|
|
3154
|
+
};
|
|
3155
|
+
default: {
|
|
3156
|
+
const _exhaustiveCheck = result.state;
|
|
3157
|
+
throw new Error(`Unsupported parse state: ${_exhaustiveCheck}`);
|
|
3158
|
+
}
|
|
3159
|
+
}
|
|
3160
|
+
},
|
|
3161
|
+
parseOutput({ text: text2 }, context) {
|
|
3162
|
+
const parseResult = safeParseJSON({ text: text2 });
|
|
3163
|
+
if (!parseResult.success) {
|
|
3164
|
+
throw new NoObjectGeneratedError({
|
|
3165
|
+
message: "No object generated: could not parse the response.",
|
|
3166
|
+
cause: parseResult.error,
|
|
3167
|
+
text: text2,
|
|
3168
|
+
response: context.response,
|
|
3169
|
+
usage: context.usage,
|
|
3170
|
+
finishReason: context.finishReason
|
|
3171
|
+
});
|
|
3172
|
+
}
|
|
3173
|
+
const validationResult = safeValidateTypes({
|
|
3174
|
+
value: parseResult.value,
|
|
3175
|
+
schema
|
|
3176
|
+
});
|
|
3177
|
+
if (!validationResult.success) {
|
|
3178
|
+
throw new NoObjectGeneratedError({
|
|
3179
|
+
message: "No object generated: response did not match schema.",
|
|
3180
|
+
cause: validationResult.error,
|
|
3181
|
+
text: text2,
|
|
3182
|
+
response: context.response,
|
|
3183
|
+
usage: context.usage,
|
|
3184
|
+
finishReason: context.finishReason
|
|
3185
|
+
});
|
|
3186
|
+
}
|
|
3187
|
+
return validationResult.value;
|
|
3188
|
+
}
|
|
3189
|
+
};
|
|
3190
|
+
};
|
|
3191
|
+
function mergeStreams(stream1, stream2) {
|
|
3192
|
+
const reader1 = stream1.getReader();
|
|
3193
|
+
const reader2 = stream2.getReader();
|
|
3194
|
+
let lastRead1 = void 0;
|
|
3195
|
+
let lastRead2 = void 0;
|
|
3196
|
+
let stream1Done = false;
|
|
3197
|
+
let stream2Done = false;
|
|
3198
|
+
async function readStream1(controller) {
|
|
3199
|
+
try {
|
|
3200
|
+
if (lastRead1 == null) {
|
|
3201
|
+
lastRead1 = reader1.read();
|
|
3202
|
+
}
|
|
3203
|
+
const result = await lastRead1;
|
|
3204
|
+
lastRead1 = void 0;
|
|
3205
|
+
if (!result.done) {
|
|
3206
|
+
controller.enqueue(result.value);
|
|
3207
|
+
} else {
|
|
3208
|
+
controller.close();
|
|
3209
|
+
}
|
|
3210
|
+
} catch (error) {
|
|
3211
|
+
controller.error(error);
|
|
3212
|
+
}
|
|
3213
|
+
}
|
|
3214
|
+
async function readStream2(controller) {
|
|
3215
|
+
try {
|
|
3216
|
+
if (lastRead2 == null) {
|
|
3217
|
+
lastRead2 = reader2.read();
|
|
3218
|
+
}
|
|
3219
|
+
const result = await lastRead2;
|
|
3220
|
+
lastRead2 = void 0;
|
|
3221
|
+
if (!result.done) {
|
|
3222
|
+
controller.enqueue(result.value);
|
|
3223
|
+
} else {
|
|
3224
|
+
controller.close();
|
|
3225
|
+
}
|
|
3226
|
+
} catch (error) {
|
|
3227
|
+
controller.error(error);
|
|
3228
|
+
}
|
|
3229
|
+
}
|
|
3230
|
+
return new ReadableStream({
|
|
3231
|
+
async pull(controller) {
|
|
3232
|
+
try {
|
|
3233
|
+
if (stream1Done) {
|
|
3234
|
+
await readStream2(controller);
|
|
3235
|
+
return;
|
|
3236
|
+
}
|
|
3237
|
+
if (stream2Done) {
|
|
3238
|
+
await readStream1(controller);
|
|
3239
|
+
return;
|
|
3240
|
+
}
|
|
3241
|
+
if (lastRead1 == null) {
|
|
3242
|
+
lastRead1 = reader1.read();
|
|
3243
|
+
}
|
|
3244
|
+
if (lastRead2 == null) {
|
|
3245
|
+
lastRead2 = reader2.read();
|
|
3246
|
+
}
|
|
3247
|
+
const { result, reader } = await Promise.race([
|
|
3248
|
+
lastRead1.then((result2) => ({ result: result2, reader: reader1 })),
|
|
3249
|
+
lastRead2.then((result2) => ({ result: result2, reader: reader2 }))
|
|
3250
|
+
]);
|
|
3251
|
+
if (!result.done) {
|
|
3252
|
+
controller.enqueue(result.value);
|
|
3253
|
+
}
|
|
3254
|
+
if (reader === reader1) {
|
|
3255
|
+
lastRead1 = void 0;
|
|
3256
|
+
if (result.done) {
|
|
3257
|
+
await readStream2(controller);
|
|
3258
|
+
stream1Done = true;
|
|
3259
|
+
}
|
|
3260
|
+
} else {
|
|
3261
|
+
lastRead2 = void 0;
|
|
3262
|
+
if (result.done) {
|
|
3263
|
+
stream2Done = true;
|
|
3264
|
+
await readStream1(controller);
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
} catch (error) {
|
|
3268
|
+
controller.error(error);
|
|
3269
|
+
}
|
|
3270
|
+
},
|
|
3271
|
+
cancel() {
|
|
3272
|
+
reader1.cancel();
|
|
3273
|
+
reader2.cancel();
|
|
3274
|
+
}
|
|
3275
|
+
});
|
|
3276
|
+
}
|
|
3277
|
+
createIdGenerator({
|
|
3278
|
+
prefix: "aitxt",
|
|
3279
|
+
size: 24
|
|
3280
|
+
});
|
|
3281
|
+
createIdGenerator({
|
|
3282
|
+
prefix: "msg",
|
|
3283
|
+
size: 24
|
|
3284
|
+
});
|
|
3285
|
+
var ClientOrServerImplementationSchema = z.object({
|
|
3286
|
+
name: z.string(),
|
|
3287
|
+
version: z.string()
|
|
3288
|
+
}).passthrough();
|
|
3289
|
+
var BaseParamsSchema = z.object({
|
|
3290
|
+
_meta: z.optional(z.object({}).passthrough())
|
|
3291
|
+
}).passthrough();
|
|
3292
|
+
var ResultSchema = BaseParamsSchema;
|
|
3293
|
+
var RequestSchema = z.object({
|
|
3294
|
+
method: z.string(),
|
|
3295
|
+
params: z.optional(BaseParamsSchema)
|
|
3296
|
+
});
|
|
3297
|
+
var ServerCapabilitiesSchema = z.object({
|
|
3298
|
+
experimental: z.optional(z.object({}).passthrough()),
|
|
3299
|
+
logging: z.optional(z.object({}).passthrough()),
|
|
3300
|
+
prompts: z.optional(
|
|
3301
|
+
z.object({
|
|
3302
|
+
listChanged: z.optional(z.boolean())
|
|
3303
|
+
}).passthrough()
|
|
3304
|
+
),
|
|
3305
|
+
resources: z.optional(
|
|
3306
|
+
z.object({
|
|
3307
|
+
subscribe: z.optional(z.boolean()),
|
|
3308
|
+
listChanged: z.optional(z.boolean())
|
|
3309
|
+
}).passthrough()
|
|
3310
|
+
),
|
|
3311
|
+
tools: z.optional(
|
|
3312
|
+
z.object({
|
|
3313
|
+
listChanged: z.optional(z.boolean())
|
|
3314
|
+
}).passthrough()
|
|
3315
|
+
)
|
|
3316
|
+
}).passthrough();
|
|
3317
|
+
ResultSchema.extend({
|
|
3318
|
+
protocolVersion: z.string(),
|
|
3319
|
+
capabilities: ServerCapabilitiesSchema,
|
|
3320
|
+
serverInfo: ClientOrServerImplementationSchema,
|
|
3321
|
+
instructions: z.optional(z.string())
|
|
3322
|
+
});
|
|
3323
|
+
var PaginatedResultSchema = ResultSchema.extend({
|
|
3324
|
+
nextCursor: z.optional(z.string())
|
|
3325
|
+
});
|
|
3326
|
+
var ToolSchema = z.object({
|
|
3327
|
+
name: z.string(),
|
|
3328
|
+
description: z.optional(z.string()),
|
|
3329
|
+
inputSchema: z.object({
|
|
3330
|
+
type: z.literal("object"),
|
|
3331
|
+
properties: z.optional(z.object({}).passthrough())
|
|
3332
|
+
}).passthrough()
|
|
3333
|
+
}).passthrough();
|
|
3334
|
+
PaginatedResultSchema.extend({
|
|
3335
|
+
tools: z.array(ToolSchema)
|
|
3336
|
+
});
|
|
3337
|
+
var TextContentSchema = z.object({
|
|
3338
|
+
type: z.literal("text"),
|
|
3339
|
+
text: z.string()
|
|
3340
|
+
}).passthrough();
|
|
3341
|
+
var ImageContentSchema = z.object({
|
|
3342
|
+
type: z.literal("image"),
|
|
3343
|
+
data: z.string().base64(),
|
|
3344
|
+
mimeType: z.string()
|
|
3345
|
+
}).passthrough();
|
|
3346
|
+
var ResourceContentsSchema = z.object({
|
|
3347
|
+
/**
|
|
3348
|
+
* The URI of this resource.
|
|
3349
|
+
*/
|
|
3350
|
+
uri: z.string(),
|
|
3351
|
+
/**
|
|
3352
|
+
* The MIME type of this resource, if known.
|
|
3353
|
+
*/
|
|
3354
|
+
mimeType: z.optional(z.string())
|
|
3355
|
+
}).passthrough();
|
|
3356
|
+
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
|
3357
|
+
text: z.string()
|
|
3358
|
+
});
|
|
3359
|
+
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
|
3360
|
+
blob: z.string().base64()
|
|
3361
|
+
});
|
|
3362
|
+
var EmbeddedResourceSchema = z.object({
|
|
3363
|
+
type: z.literal("resource"),
|
|
3364
|
+
resource: z.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
|
3365
|
+
}).passthrough();
|
|
3366
|
+
ResultSchema.extend({
|
|
3367
|
+
content: z.array(
|
|
3368
|
+
z.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
|
|
3369
|
+
),
|
|
3370
|
+
isError: z.boolean().default(false).optional()
|
|
3371
|
+
}).or(
|
|
3372
|
+
ResultSchema.extend({
|
|
3373
|
+
toolResult: z.unknown()
|
|
3374
|
+
})
|
|
3375
|
+
);
|
|
3376
|
+
var JSONRPC_VERSION = "2.0";
|
|
3377
|
+
var JSONRPCRequestSchema = z.object({
|
|
3378
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
3379
|
+
id: z.union([z.string(), z.number().int()])
|
|
3380
|
+
}).merge(RequestSchema).strict();
|
|
3381
|
+
var JSONRPCResponseSchema = z.object({
|
|
3382
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
3383
|
+
id: z.union([z.string(), z.number().int()]),
|
|
3384
|
+
result: ResultSchema
|
|
3385
|
+
}).strict();
|
|
3386
|
+
var JSONRPCErrorSchema = z.object({
|
|
3387
|
+
jsonrpc: z.literal(JSONRPC_VERSION),
|
|
3388
|
+
id: z.union([z.string(), z.number().int()]),
|
|
3389
|
+
error: z.object({
|
|
3390
|
+
code: z.number().int(),
|
|
3391
|
+
message: z.string(),
|
|
3392
|
+
data: z.optional(z.unknown())
|
|
3393
|
+
})
|
|
3394
|
+
}).strict();
|
|
3395
|
+
var JSONRPCNotificationSchema = z.object({
|
|
3396
|
+
jsonrpc: z.literal(JSONRPC_VERSION)
|
|
3397
|
+
}).merge(
|
|
3398
|
+
z.object({
|
|
3399
|
+
method: z.string(),
|
|
3400
|
+
params: z.optional(BaseParamsSchema)
|
|
3401
|
+
})
|
|
3402
|
+
).strict();
|
|
3403
|
+
z.union([
|
|
3404
|
+
JSONRPCRequestSchema,
|
|
3405
|
+
JSONRPCNotificationSchema,
|
|
3406
|
+
JSONRPCResponseSchema,
|
|
3407
|
+
JSONRPCErrorSchema
|
|
3408
|
+
]);
|
|
3409
|
+
var langchain_adapter_exports = {};
|
|
3410
|
+
__export(langchain_adapter_exports, {
|
|
3411
|
+
mergeIntoDataStream: () => mergeIntoDataStream,
|
|
3412
|
+
toDataStream: () => toDataStream,
|
|
3413
|
+
toDataStreamResponse: () => toDataStreamResponse
|
|
3414
|
+
});
|
|
3415
|
+
function createCallbacksTransformer(callbacks = {}) {
|
|
3416
|
+
const textEncoder = new TextEncoder();
|
|
3417
|
+
let aggregatedResponse = "";
|
|
3418
|
+
return new TransformStream({
|
|
3419
|
+
async start() {
|
|
3420
|
+
if (callbacks.onStart)
|
|
3421
|
+
await callbacks.onStart();
|
|
3422
|
+
},
|
|
3423
|
+
async transform(message, controller) {
|
|
3424
|
+
controller.enqueue(textEncoder.encode(message));
|
|
3425
|
+
aggregatedResponse += message;
|
|
3426
|
+
if (callbacks.onToken)
|
|
3427
|
+
await callbacks.onToken(message);
|
|
3428
|
+
if (callbacks.onText && typeof message === "string") {
|
|
3429
|
+
await callbacks.onText(message);
|
|
3430
|
+
}
|
|
3431
|
+
},
|
|
3432
|
+
async flush() {
|
|
3433
|
+
if (callbacks.onCompletion) {
|
|
3434
|
+
await callbacks.onCompletion(aggregatedResponse);
|
|
3435
|
+
}
|
|
3436
|
+
if (callbacks.onFinal) {
|
|
3437
|
+
await callbacks.onFinal(aggregatedResponse);
|
|
3438
|
+
}
|
|
3439
|
+
}
|
|
3440
|
+
});
|
|
3441
|
+
}
|
|
3442
|
+
function toDataStreamInternal(stream, callbacks) {
|
|
3443
|
+
return stream.pipeThrough(
|
|
3444
|
+
new TransformStream({
|
|
3445
|
+
transform: async (value, controller) => {
|
|
3446
|
+
var _a17;
|
|
3447
|
+
if (typeof value === "string") {
|
|
3448
|
+
controller.enqueue(value);
|
|
3449
|
+
return;
|
|
3450
|
+
}
|
|
3451
|
+
if ("event" in value) {
|
|
3452
|
+
if (value.event === "on_chat_model_stream") {
|
|
3453
|
+
forwardAIMessageChunk(
|
|
3454
|
+
(_a17 = value.data) == null ? void 0 : _a17.chunk,
|
|
3455
|
+
controller
|
|
3456
|
+
);
|
|
3457
|
+
}
|
|
3458
|
+
return;
|
|
3459
|
+
}
|
|
3460
|
+
forwardAIMessageChunk(value, controller);
|
|
3461
|
+
}
|
|
3462
|
+
})
|
|
3463
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
|
3464
|
+
new TransformStream({
|
|
3465
|
+
transform: async (chunk, controller) => {
|
|
3466
|
+
controller.enqueue(formatDataStreamPart("text", chunk));
|
|
3467
|
+
}
|
|
3468
|
+
})
|
|
3469
|
+
);
|
|
3470
|
+
}
|
|
3471
|
+
function toDataStream(stream, callbacks) {
|
|
3472
|
+
return toDataStreamInternal(stream, callbacks).pipeThrough(
|
|
3473
|
+
new TextEncoderStream()
|
|
3474
|
+
);
|
|
3475
|
+
}
|
|
3476
|
+
function toDataStreamResponse(stream, options) {
|
|
3477
|
+
var _a17;
|
|
3478
|
+
const dataStream = toDataStreamInternal(
|
|
3479
|
+
stream,
|
|
3480
|
+
options == null ? void 0 : options.callbacks
|
|
3481
|
+
).pipeThrough(new TextEncoderStream());
|
|
3482
|
+
const data = options == null ? void 0 : options.data;
|
|
3483
|
+
const init = options == null ? void 0 : options.init;
|
|
3484
|
+
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
|
3485
|
+
return new Response(responseStream, {
|
|
3486
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
|
3487
|
+
statusText: init == null ? void 0 : init.statusText,
|
|
3488
|
+
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
|
3489
|
+
contentType: "text/plain; charset=utf-8",
|
|
3490
|
+
dataStreamVersion: "v1"
|
|
3491
|
+
})
|
|
3492
|
+
});
|
|
3493
|
+
}
|
|
3494
|
+
function mergeIntoDataStream(stream, options) {
|
|
3495
|
+
options.dataStream.merge(toDataStreamInternal(stream, options.callbacks));
|
|
3496
|
+
}
|
|
3497
|
+
function forwardAIMessageChunk(chunk, controller) {
|
|
3498
|
+
if (typeof chunk.content === "string") {
|
|
3499
|
+
controller.enqueue(chunk.content);
|
|
3500
|
+
} else {
|
|
3501
|
+
const content = chunk.content;
|
|
3502
|
+
for (const item of content) {
|
|
3503
|
+
if (item.type === "text") {
|
|
3504
|
+
controller.enqueue(item.text);
|
|
3505
|
+
}
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
}
|
|
3509
|
+
var llamaindex_adapter_exports = {};
|
|
3510
|
+
__export(llamaindex_adapter_exports, {
|
|
3511
|
+
mergeIntoDataStream: () => mergeIntoDataStream2,
|
|
3512
|
+
toDataStream: () => toDataStream2,
|
|
3513
|
+
toDataStreamResponse: () => toDataStreamResponse2
|
|
3514
|
+
});
|
|
3515
|
+
function toDataStreamInternal2(stream, callbacks) {
|
|
3516
|
+
const trimStart = trimStartOfStream();
|
|
3517
|
+
return convertAsyncIteratorToReadableStream(stream[Symbol.asyncIterator]()).pipeThrough(
|
|
3518
|
+
new TransformStream({
|
|
3519
|
+
async transform(message, controller) {
|
|
3520
|
+
controller.enqueue(trimStart(message.delta));
|
|
3521
|
+
}
|
|
3522
|
+
})
|
|
3523
|
+
).pipeThrough(createCallbacksTransformer(callbacks)).pipeThrough(new TextDecoderStream()).pipeThrough(
|
|
3524
|
+
new TransformStream({
|
|
3525
|
+
transform: async (chunk, controller) => {
|
|
3526
|
+
controller.enqueue(formatDataStreamPart("text", chunk));
|
|
3527
|
+
}
|
|
3528
|
+
})
|
|
3529
|
+
);
|
|
3530
|
+
}
|
|
3531
|
+
function toDataStream2(stream, callbacks) {
|
|
3532
|
+
return toDataStreamInternal2(stream, callbacks).pipeThrough(
|
|
3533
|
+
new TextEncoderStream()
|
|
3534
|
+
);
|
|
3535
|
+
}
|
|
3536
|
+
function toDataStreamResponse2(stream, options = {}) {
|
|
3537
|
+
var _a17;
|
|
3538
|
+
const { init, data, callbacks } = options;
|
|
3539
|
+
const dataStream = toDataStreamInternal2(stream, callbacks).pipeThrough(
|
|
3540
|
+
new TextEncoderStream()
|
|
3541
|
+
);
|
|
3542
|
+
const responseStream = data ? mergeStreams(data.stream, dataStream) : dataStream;
|
|
3543
|
+
return new Response(responseStream, {
|
|
3544
|
+
status: (_a17 = init == null ? void 0 : init.status) != null ? _a17 : 200,
|
|
3545
|
+
statusText: init == null ? void 0 : init.statusText,
|
|
3546
|
+
headers: prepareResponseHeaders(init == null ? void 0 : init.headers, {
|
|
3547
|
+
contentType: "text/plain; charset=utf-8",
|
|
3548
|
+
dataStreamVersion: "v1"
|
|
3549
|
+
})
|
|
3550
|
+
});
|
|
3551
|
+
}
|
|
3552
|
+
function mergeIntoDataStream2(stream, options) {
|
|
3553
|
+
options.dataStream.merge(toDataStreamInternal2(stream, options.callbacks));
|
|
3554
|
+
}
|
|
3555
|
+
function trimStartOfStream() {
|
|
3556
|
+
let isStreamStart = true;
|
|
3557
|
+
return (text2) => {
|
|
3558
|
+
if (isStreamStart) {
|
|
3559
|
+
text2 = text2.trimStart();
|
|
3560
|
+
if (text2)
|
|
3561
|
+
isStreamStart = false;
|
|
3562
|
+
}
|
|
3563
|
+
return text2;
|
|
3564
|
+
};
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
// src/utils.ts
|
|
3568
|
+
function convertZodSchemaToAISDKSchema(zodSchema2, target = "jsonSchema7") {
|
|
3569
|
+
const jsonSchemaToUse = zodToJsonSchema(zodSchema2, target);
|
|
10
3570
|
return jsonSchema(jsonSchemaToUse, {
|
|
11
3571
|
validate: (value) => {
|
|
12
|
-
const result =
|
|
3572
|
+
const result = zodSchema2.safeParse(value);
|
|
13
3573
|
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
14
3574
|
}
|
|
15
3575
|
});
|
|
@@ -41,21 +3601,21 @@ function applyCompatLayer({
|
|
|
41
3601
|
compatLayers,
|
|
42
3602
|
mode
|
|
43
3603
|
}) {
|
|
44
|
-
let
|
|
3604
|
+
let zodSchema2;
|
|
45
3605
|
if (!isZodType(schema)) {
|
|
46
|
-
|
|
3606
|
+
zodSchema2 = convertSchemaToZod(schema);
|
|
47
3607
|
} else {
|
|
48
|
-
|
|
3608
|
+
zodSchema2 = schema;
|
|
49
3609
|
}
|
|
50
3610
|
for (const compat of compatLayers) {
|
|
51
3611
|
if (compat.shouldApply()) {
|
|
52
|
-
return mode === "jsonSchema" ? compat.processToJSONSchema(
|
|
3612
|
+
return mode === "jsonSchema" ? compat.processToJSONSchema(zodSchema2) : compat.processToAISDKSchema(zodSchema2);
|
|
53
3613
|
}
|
|
54
3614
|
}
|
|
55
3615
|
if (mode === "jsonSchema") {
|
|
56
|
-
return zodToJsonSchema(
|
|
3616
|
+
return zodToJsonSchema(zodSchema2, "jsonSchema7");
|
|
57
3617
|
} else {
|
|
58
|
-
return convertZodSchemaToAISDKSchema(
|
|
3618
|
+
return convertZodSchemaToAISDKSchema(zodSchema2);
|
|
59
3619
|
}
|
|
60
3620
|
}
|
|
61
3621
|
|
|
@@ -229,8 +3789,8 @@ var SchemaCompatLayer = class {
|
|
|
229
3789
|
* @returns The updated description with constraints, or undefined if no constraints
|
|
230
3790
|
*/
|
|
231
3791
|
mergeParameterDescription(description, constraints) {
|
|
232
|
-
if (
|
|
233
|
-
return (description ? description + "\n" : "") +
|
|
3792
|
+
if (constraints.length > 0) {
|
|
3793
|
+
return (description ? description + "\n" : "") + `constraints: ${constraints.join(`, `)}`;
|
|
234
3794
|
} else {
|
|
235
3795
|
return description;
|
|
236
3796
|
}
|
|
@@ -260,24 +3820,24 @@ var SchemaCompatLayer = class {
|
|
|
260
3820
|
const zodArrayDef = value._def;
|
|
261
3821
|
const processedType = this.processZodType(zodArrayDef.type);
|
|
262
3822
|
let result = z.array(processedType);
|
|
263
|
-
const constraints =
|
|
3823
|
+
const constraints = [];
|
|
264
3824
|
if (zodArrayDef.minLength?.value !== void 0) {
|
|
265
3825
|
if (handleChecks.includes("min")) {
|
|
266
|
-
constraints.
|
|
3826
|
+
constraints.push(`minimum length ${zodArrayDef.minLength.value}`);
|
|
267
3827
|
} else {
|
|
268
3828
|
result = result.min(zodArrayDef.minLength.value);
|
|
269
3829
|
}
|
|
270
3830
|
}
|
|
271
3831
|
if (zodArrayDef.maxLength?.value !== void 0) {
|
|
272
3832
|
if (handleChecks.includes("max")) {
|
|
273
|
-
constraints.
|
|
3833
|
+
constraints.push(`maximum length ${zodArrayDef.maxLength.value}`);
|
|
274
3834
|
} else {
|
|
275
3835
|
result = result.max(zodArrayDef.maxLength.value);
|
|
276
3836
|
}
|
|
277
3837
|
}
|
|
278
3838
|
if (zodArrayDef.exactLength?.value !== void 0) {
|
|
279
3839
|
if (handleChecks.includes("length")) {
|
|
280
|
-
constraints.
|
|
3840
|
+
constraints.push(`exact length ${zodArrayDef.exactLength.value}`);
|
|
281
3841
|
} else {
|
|
282
3842
|
result = result.length(zodArrayDef.exactLength.value);
|
|
283
3843
|
}
|
|
@@ -312,7 +3872,7 @@ var SchemaCompatLayer = class {
|
|
|
312
3872
|
* @returns The processed Zod string
|
|
313
3873
|
*/
|
|
314
3874
|
defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS) {
|
|
315
|
-
const constraints =
|
|
3875
|
+
const constraints = [];
|
|
316
3876
|
const checks = value._def.checks || [];
|
|
317
3877
|
const newChecks = [];
|
|
318
3878
|
for (const check of checks) {
|
|
@@ -320,38 +3880,20 @@ var SchemaCompatLayer = class {
|
|
|
320
3880
|
if (handleChecks.includes(check.kind)) {
|
|
321
3881
|
switch (check.kind) {
|
|
322
3882
|
case "regex": {
|
|
323
|
-
constraints.regex
|
|
324
|
-
pattern: check.regex.source,
|
|
325
|
-
flags: check.regex.flags
|
|
326
|
-
};
|
|
327
|
-
break;
|
|
328
|
-
}
|
|
329
|
-
case "emoji": {
|
|
330
|
-
constraints.emoji = true;
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
333
|
-
case "email": {
|
|
334
|
-
constraints.email = true;
|
|
335
|
-
break;
|
|
336
|
-
}
|
|
337
|
-
case "url": {
|
|
338
|
-
constraints.url = true;
|
|
339
|
-
break;
|
|
340
|
-
}
|
|
341
|
-
case "uuid": {
|
|
342
|
-
constraints.uuid = true;
|
|
3883
|
+
constraints.push(`input must match this regex ${check.regex.source}`);
|
|
343
3884
|
break;
|
|
344
3885
|
}
|
|
3886
|
+
case "emoji":
|
|
3887
|
+
case "email":
|
|
3888
|
+
case "url":
|
|
3889
|
+
case "uuid":
|
|
345
3890
|
case "cuid": {
|
|
346
|
-
constraints.
|
|
347
|
-
break;
|
|
348
|
-
}
|
|
349
|
-
case "min": {
|
|
350
|
-
constraints.minLength = check.value;
|
|
3891
|
+
constraints.push(`a valid ${check.kind}`);
|
|
351
3892
|
break;
|
|
352
3893
|
}
|
|
3894
|
+
case "min":
|
|
353
3895
|
case "max": {
|
|
354
|
-
constraints.
|
|
3896
|
+
constraints.push(`${check.kind}imum length ${check.value}`);
|
|
355
3897
|
break;
|
|
356
3898
|
}
|
|
357
3899
|
}
|
|
@@ -378,7 +3920,7 @@ var SchemaCompatLayer = class {
|
|
|
378
3920
|
* @returns The processed Zod number
|
|
379
3921
|
*/
|
|
380
3922
|
defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS) {
|
|
381
|
-
const constraints =
|
|
3923
|
+
const constraints = [];
|
|
382
3924
|
const checks = value._def.checks || [];
|
|
383
3925
|
const newChecks = [];
|
|
384
3926
|
for (const check of checks) {
|
|
@@ -387,20 +3929,20 @@ var SchemaCompatLayer = class {
|
|
|
387
3929
|
switch (check.kind) {
|
|
388
3930
|
case "min":
|
|
389
3931
|
if (check.inclusive) {
|
|
390
|
-
constraints.
|
|
3932
|
+
constraints.push(`greater than or equal to ${check.value}`);
|
|
391
3933
|
} else {
|
|
392
|
-
constraints.
|
|
3934
|
+
constraints.push(`greater than ${check.value}`);
|
|
393
3935
|
}
|
|
394
3936
|
break;
|
|
395
3937
|
case "max":
|
|
396
3938
|
if (check.inclusive) {
|
|
397
|
-
constraints.
|
|
3939
|
+
constraints.push(`lower than or equal to ${check.value}`);
|
|
398
3940
|
} else {
|
|
399
|
-
constraints.
|
|
3941
|
+
constraints.push(`lower than ${check.value}`);
|
|
400
3942
|
}
|
|
401
3943
|
break;
|
|
402
3944
|
case "multipleOf": {
|
|
403
|
-
constraints.
|
|
3945
|
+
constraints.push(`multiple of ${check.value}`);
|
|
404
3946
|
break;
|
|
405
3947
|
}
|
|
406
3948
|
}
|
|
@@ -435,7 +3977,7 @@ var SchemaCompatLayer = class {
|
|
|
435
3977
|
* @returns A Zod string schema representing the date in ISO format
|
|
436
3978
|
*/
|
|
437
3979
|
defaultZodDateHandler(value) {
|
|
438
|
-
const constraints =
|
|
3980
|
+
const constraints = [];
|
|
439
3981
|
const checks = value._def.checks || [];
|
|
440
3982
|
for (const check of checks) {
|
|
441
3983
|
if ("kind" in check) {
|
|
@@ -443,19 +3985,19 @@ var SchemaCompatLayer = class {
|
|
|
443
3985
|
case "min":
|
|
444
3986
|
const minDate = new Date(check.value);
|
|
445
3987
|
if (!isNaN(minDate.getTime())) {
|
|
446
|
-
constraints.
|
|
3988
|
+
constraints.push(`Date must be newer than ${minDate.toISOString()} (ISO)`);
|
|
447
3989
|
}
|
|
448
3990
|
break;
|
|
449
3991
|
case "max":
|
|
450
3992
|
const maxDate = new Date(check.value);
|
|
451
3993
|
if (!isNaN(maxDate.getTime())) {
|
|
452
|
-
constraints.
|
|
3994
|
+
constraints.push(`Date must be older than ${maxDate.toISOString()} (ISO)`);
|
|
453
3995
|
}
|
|
454
3996
|
break;
|
|
455
3997
|
}
|
|
456
3998
|
}
|
|
457
3999
|
}
|
|
458
|
-
constraints.
|
|
4000
|
+
constraints.push(`Date format is date-time`);
|
|
459
4001
|
let result = z.string().describe("date-time");
|
|
460
4002
|
const description = this.mergeParameterDescription(value.description, constraints);
|
|
461
4003
|
if (description) {
|
|
@@ -483,8 +4025,8 @@ var SchemaCompatLayer = class {
|
|
|
483
4025
|
* @param zodSchema - The Zod object schema to process
|
|
484
4026
|
* @returns An AI SDK Schema with provider-specific compatibility applied
|
|
485
4027
|
*/
|
|
486
|
-
processToAISDKSchema(
|
|
487
|
-
const processedSchema = this.processZodType(
|
|
4028
|
+
processToAISDKSchema(zodSchema2) {
|
|
4029
|
+
const processedSchema = this.processZodType(zodSchema2);
|
|
488
4030
|
return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
|
|
489
4031
|
}
|
|
490
4032
|
/**
|
|
@@ -493,8 +4035,8 @@ var SchemaCompatLayer = class {
|
|
|
493
4035
|
* @param zodSchema - The Zod object schema to process
|
|
494
4036
|
* @returns A JSONSchema7 object with provider-specific compatibility applied
|
|
495
4037
|
*/
|
|
496
|
-
processToJSONSchema(
|
|
497
|
-
return this.processToAISDKSchema(
|
|
4038
|
+
processToJSONSchema(zodSchema2) {
|
|
4039
|
+
return this.processToAISDKSchema(zodSchema2).jsonSchema;
|
|
498
4040
|
}
|
|
499
4041
|
};
|
|
500
4042
|
var ALL_STRING_CHECKS2 = [
|
|
@@ -663,8 +4205,8 @@ var SchemaCompatLayer2 = class {
|
|
|
663
4205
|
* @returns The updated description with constraints, or undefined if no constraints
|
|
664
4206
|
*/
|
|
665
4207
|
mergeParameterDescription(description, constraints) {
|
|
666
|
-
if (
|
|
667
|
-
return (description ? description + "\n" : "") +
|
|
4208
|
+
if (constraints.length > 0) {
|
|
4209
|
+
return (description ? description + "\n" : "") + `constraints: ${constraints.join(`, `)}`;
|
|
668
4210
|
} else {
|
|
669
4211
|
return description;
|
|
670
4212
|
}
|
|
@@ -694,26 +4236,26 @@ var SchemaCompatLayer2 = class {
|
|
|
694
4236
|
const zodArrayDef = value._zod.def;
|
|
695
4237
|
const processedType = this.processZodType(zodArrayDef.element);
|
|
696
4238
|
let result = z$1.array(processedType);
|
|
697
|
-
const constraints =
|
|
4239
|
+
const constraints = [];
|
|
698
4240
|
if (zodArrayDef.checks) {
|
|
699
4241
|
for (const check of zodArrayDef.checks) {
|
|
700
4242
|
if (check._zod.def.check === "min_length") {
|
|
701
4243
|
if (handleChecks.includes("min")) {
|
|
702
|
-
constraints.
|
|
4244
|
+
constraints.push(`minimum length ${check._zod.def.minimum}`);
|
|
703
4245
|
} else {
|
|
704
4246
|
result = result.min(check._zod.def.minimum);
|
|
705
4247
|
}
|
|
706
4248
|
}
|
|
707
4249
|
if (check._zod.def.check === "max_length") {
|
|
708
4250
|
if (handleChecks.includes("max")) {
|
|
709
|
-
constraints.
|
|
4251
|
+
constraints.push(`maximum length ${check._zod.def.maximum}`);
|
|
710
4252
|
} else {
|
|
711
4253
|
result = result.max(check._zod.def.maximum);
|
|
712
4254
|
}
|
|
713
4255
|
}
|
|
714
4256
|
if (check._zod.def.check === "length_equals") {
|
|
715
4257
|
if (handleChecks.includes("length")) {
|
|
716
|
-
constraints.
|
|
4258
|
+
constraints.push(`exact length ${check._zod.def.length}`);
|
|
717
4259
|
} else {
|
|
718
4260
|
result = result.length(check._zod.def.length);
|
|
719
4261
|
}
|
|
@@ -752,7 +4294,7 @@ var SchemaCompatLayer2 = class {
|
|
|
752
4294
|
* @returns The processed Zod string
|
|
753
4295
|
*/
|
|
754
4296
|
defaultZodStringHandler(value, handleChecks = ALL_STRING_CHECKS2) {
|
|
755
|
-
const constraints =
|
|
4297
|
+
const constraints = [];
|
|
756
4298
|
const checks = value._zod.def.checks || [];
|
|
757
4299
|
const newChecks = [];
|
|
758
4300
|
if (checks) {
|
|
@@ -760,36 +4302,23 @@ var SchemaCompatLayer2 = class {
|
|
|
760
4302
|
if (handleChecks.includes(check._zod.def.check)) {
|
|
761
4303
|
switch (check._zod.def.check) {
|
|
762
4304
|
case "min_length":
|
|
763
|
-
constraints.
|
|
4305
|
+
constraints.push(`minimum length ${check._zod.def.minimum}`);
|
|
764
4306
|
break;
|
|
765
4307
|
case "max_length":
|
|
766
|
-
constraints.
|
|
4308
|
+
constraints.push(`maximum length ${check._zod.def.maximum}`);
|
|
767
4309
|
break;
|
|
768
4310
|
case "string_format":
|
|
769
4311
|
{
|
|
770
4312
|
switch (check._zod.def.format) {
|
|
771
4313
|
case "email":
|
|
772
|
-
constraints.email = true;
|
|
773
|
-
break;
|
|
774
4314
|
case "url":
|
|
775
|
-
constraints.url = true;
|
|
776
|
-
break;
|
|
777
4315
|
case "emoji":
|
|
778
|
-
constraints.emoji = true;
|
|
779
|
-
break;
|
|
780
4316
|
case "uuid":
|
|
781
|
-
constraints.uuid = true;
|
|
782
|
-
break;
|
|
783
4317
|
case "cuid":
|
|
784
|
-
constraints.
|
|
4318
|
+
constraints.push(`a valid ${check._zod.def.format}`);
|
|
785
4319
|
break;
|
|
786
4320
|
case "regex":
|
|
787
|
-
constraints.regex
|
|
788
|
-
// @ts-expect-error - fix later
|
|
789
|
-
pattern: check._zod.def.pattern,
|
|
790
|
-
// @ts-expect-error - fix later
|
|
791
|
-
flags: check._zod.def.flags
|
|
792
|
-
};
|
|
4321
|
+
constraints.push(`input must match this regex ${check._zod.def.pattern}`);
|
|
793
4322
|
break;
|
|
794
4323
|
}
|
|
795
4324
|
}
|
|
@@ -820,7 +4349,7 @@ var SchemaCompatLayer2 = class {
|
|
|
820
4349
|
* @returns The processed Zod number
|
|
821
4350
|
*/
|
|
822
4351
|
defaultZodNumberHandler(value, handleChecks = ALL_NUMBER_CHECKS2) {
|
|
823
|
-
const constraints =
|
|
4352
|
+
const constraints = [];
|
|
824
4353
|
const checks = value._zod.def.checks || [];
|
|
825
4354
|
const newChecks = [];
|
|
826
4355
|
if (checks) {
|
|
@@ -829,20 +4358,20 @@ var SchemaCompatLayer2 = class {
|
|
|
829
4358
|
switch (check._zod.def.check) {
|
|
830
4359
|
case "greater_than":
|
|
831
4360
|
if (check._zod.def.inclusive) {
|
|
832
|
-
constraints.
|
|
4361
|
+
constraints.push(`greater than or equal to ${check._zod.def.value}`);
|
|
833
4362
|
} else {
|
|
834
|
-
constraints.
|
|
4363
|
+
constraints.push(`greater than ${check._zod.def.value}`);
|
|
835
4364
|
}
|
|
836
4365
|
break;
|
|
837
4366
|
case "less_than":
|
|
838
4367
|
if (check._zod.def.inclusive) {
|
|
839
|
-
constraints.
|
|
4368
|
+
constraints.push(`lower than or equal to ${check._zod.def.value}`);
|
|
840
4369
|
} else {
|
|
841
|
-
constraints.
|
|
4370
|
+
constraints.push(`lower than ${check._zod.def.value}`);
|
|
842
4371
|
}
|
|
843
4372
|
break;
|
|
844
4373
|
case "multiple_of": {
|
|
845
|
-
constraints.
|
|
4374
|
+
constraints.push(`multiple of ${check._zod.def.value}`);
|
|
846
4375
|
break;
|
|
847
4376
|
}
|
|
848
4377
|
}
|
|
@@ -879,7 +4408,7 @@ var SchemaCompatLayer2 = class {
|
|
|
879
4408
|
* @returns A Zod string schema representing the date in ISO format
|
|
880
4409
|
*/
|
|
881
4410
|
defaultZodDateHandler(value) {
|
|
882
|
-
const constraints =
|
|
4411
|
+
const constraints = [];
|
|
883
4412
|
const checks = value._zod.def.checks || [];
|
|
884
4413
|
if (checks) {
|
|
885
4414
|
for (const check of checks) {
|
|
@@ -887,19 +4416,19 @@ var SchemaCompatLayer2 = class {
|
|
|
887
4416
|
case "less_than":
|
|
888
4417
|
const minDate = new Date(check._zod.def.value);
|
|
889
4418
|
if (!isNaN(minDate.getTime())) {
|
|
890
|
-
constraints.
|
|
4419
|
+
constraints.push(`Date must be newer than ${minDate.toISOString()} (ISO)`);
|
|
891
4420
|
}
|
|
892
4421
|
break;
|
|
893
4422
|
case "greater_than":
|
|
894
4423
|
const maxDate = new Date(check._zod.def.value);
|
|
895
4424
|
if (!isNaN(maxDate.getTime())) {
|
|
896
|
-
constraints.
|
|
4425
|
+
constraints.push(`Date must be older than ${maxDate.toISOString()} (ISO)`);
|
|
897
4426
|
}
|
|
898
4427
|
break;
|
|
899
4428
|
}
|
|
900
4429
|
}
|
|
901
4430
|
}
|
|
902
|
-
constraints.
|
|
4431
|
+
constraints.push(`Date format is date-time`);
|
|
903
4432
|
let result = z$1.string().describe("date-time");
|
|
904
4433
|
const description = this.mergeParameterDescription(value.description, constraints);
|
|
905
4434
|
if (description) {
|
|
@@ -927,8 +4456,8 @@ var SchemaCompatLayer2 = class {
|
|
|
927
4456
|
* @param zodSchema - The Zod object schema to process
|
|
928
4457
|
* @returns An AI SDK Schema with provider-specific compatibility applied
|
|
929
4458
|
*/
|
|
930
|
-
processToAISDKSchema(
|
|
931
|
-
const processedSchema = this.processZodType(
|
|
4459
|
+
processToAISDKSchema(zodSchema2) {
|
|
4460
|
+
const processedSchema = this.processZodType(zodSchema2);
|
|
932
4461
|
return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
|
|
933
4462
|
}
|
|
934
4463
|
/**
|
|
@@ -937,8 +4466,8 @@ var SchemaCompatLayer2 = class {
|
|
|
937
4466
|
* @param zodSchema - The Zod object schema to process
|
|
938
4467
|
* @returns A JSONSchema7 object with provider-specific compatibility applied
|
|
939
4468
|
*/
|
|
940
|
-
processToJSONSchema(
|
|
941
|
-
return this.processToAISDKSchema(
|
|
4469
|
+
processToJSONSchema(zodSchema2) {
|
|
4470
|
+
return this.processToAISDKSchema(zodSchema2).jsonSchema;
|
|
942
4471
|
}
|
|
943
4472
|
};
|
|
944
4473
|
|
|
@@ -1125,8 +4654,8 @@ var SchemaCompatLayer3 = class {
|
|
|
1125
4654
|
* @param zodSchema - The Zod object schema to process
|
|
1126
4655
|
* @returns An AI SDK Schema with provider-specific compatibility applied
|
|
1127
4656
|
*/
|
|
1128
|
-
processToAISDKSchema(
|
|
1129
|
-
const processedSchema = this.processZodType(
|
|
4657
|
+
processToAISDKSchema(zodSchema2) {
|
|
4658
|
+
const processedSchema = this.processZodType(zodSchema2);
|
|
1130
4659
|
return convertZodSchemaToAISDKSchema(processedSchema, this.getSchemaTarget());
|
|
1131
4660
|
}
|
|
1132
4661
|
/**
|
|
@@ -1135,38 +4664,38 @@ var SchemaCompatLayer3 = class {
|
|
|
1135
4664
|
* @param zodSchema - The Zod object schema to process
|
|
1136
4665
|
* @returns A JSONSchema7 object with provider-specific compatibility applied
|
|
1137
4666
|
*/
|
|
1138
|
-
processToJSONSchema(
|
|
1139
|
-
return this.processToAISDKSchema(
|
|
4667
|
+
processToJSONSchema(zodSchema2) {
|
|
4668
|
+
return this.processToAISDKSchema(zodSchema2).jsonSchema;
|
|
1140
4669
|
}
|
|
1141
4670
|
};
|
|
1142
4671
|
|
|
1143
4672
|
// src/zodTypes.ts
|
|
1144
|
-
function isOptional2(
|
|
1145
|
-
return (v) => v instanceof
|
|
4673
|
+
function isOptional2(z11) {
|
|
4674
|
+
return (v) => v instanceof z11["ZodOptional"];
|
|
1146
4675
|
}
|
|
1147
|
-
function isObj2(
|
|
1148
|
-
return (v) => v instanceof
|
|
4676
|
+
function isObj2(z11) {
|
|
4677
|
+
return (v) => v instanceof z11["ZodObject"];
|
|
1149
4678
|
}
|
|
1150
|
-
function isNull(
|
|
1151
|
-
return (v) => v instanceof
|
|
4679
|
+
function isNull(z11) {
|
|
4680
|
+
return (v) => v instanceof z11["ZodNull"];
|
|
1152
4681
|
}
|
|
1153
|
-
function isArr2(
|
|
1154
|
-
return (v) => v instanceof
|
|
4682
|
+
function isArr2(z11) {
|
|
4683
|
+
return (v) => v instanceof z11["ZodArray"];
|
|
1155
4684
|
}
|
|
1156
|
-
function isUnion2(
|
|
1157
|
-
return (v) => v instanceof
|
|
4685
|
+
function isUnion2(z11) {
|
|
4686
|
+
return (v) => v instanceof z11["ZodUnion"];
|
|
1158
4687
|
}
|
|
1159
|
-
function isString2(
|
|
1160
|
-
return (v) => v instanceof
|
|
4688
|
+
function isString2(z11) {
|
|
4689
|
+
return (v) => v instanceof z11["ZodString"];
|
|
1161
4690
|
}
|
|
1162
|
-
function isNumber2(
|
|
1163
|
-
return (v) => v instanceof
|
|
4691
|
+
function isNumber2(z11) {
|
|
4692
|
+
return (v) => v instanceof z11["ZodNumber"];
|
|
1164
4693
|
}
|
|
1165
|
-
function isDate(
|
|
1166
|
-
return (v) => v instanceof
|
|
4694
|
+
function isDate(z11) {
|
|
4695
|
+
return (v) => v instanceof z11["ZodDate"];
|
|
1167
4696
|
}
|
|
1168
|
-
function isDefault(
|
|
1169
|
-
return (v) => v instanceof
|
|
4697
|
+
function isDefault(z11) {
|
|
4698
|
+
return (v) => v instanceof z11["ZodDefault"];
|
|
1170
4699
|
}
|
|
1171
4700
|
|
|
1172
4701
|
// src/provider-compats/anthropic.ts
|
|
@@ -1367,10 +4896,10 @@ var OpenAIReasoningSchemaCompatLayer = class extends SchemaCompatLayer3 {
|
|
|
1367
4896
|
} else if (isDefault(z)(value)) {
|
|
1368
4897
|
const defaultDef = value._def;
|
|
1369
4898
|
const innerType = defaultDef.innerType;
|
|
1370
|
-
const defaultValue = defaultDef.defaultValue();
|
|
1371
|
-
const constraints =
|
|
4899
|
+
const defaultValue = typeof defaultDef.defaultValue === "function" ? defaultDef.defaultValue() : defaultDef.defaultValue;
|
|
4900
|
+
const constraints = [];
|
|
1372
4901
|
if (defaultValue !== void 0) {
|
|
1373
|
-
constraints.
|
|
4902
|
+
constraints.push(`the default value is ${defaultValue}`);
|
|
1374
4903
|
}
|
|
1375
4904
|
const description = this.mergeParameterDescription(value.description, constraints);
|
|
1376
4905
|
let result = this.processZodType(innerType);
|
|
@@ -1394,6 +4923,6 @@ Argument was an "any" type, but you (the LLM) do not support "any", so it was ca
|
|
|
1394
4923
|
}
|
|
1395
4924
|
};
|
|
1396
4925
|
|
|
1397
|
-
export { ALL_ARRAY_CHECKS, ALL_NUMBER_CHECKS, ALL_STRING_CHECKS, ALL_ZOD_TYPES, AnthropicSchemaCompatLayer, DeepSeekSchemaCompatLayer, GoogleSchemaCompatLayer, MetaSchemaCompatLayer, OpenAIReasoningSchemaCompatLayer, OpenAISchemaCompatLayer, SUPPORTED_ZOD_TYPES, SchemaCompatLayer3 as SchemaCompatLayer, SchemaCompatLayer as SchemaCompatLayerV3, SchemaCompatLayer2 as SchemaCompatLayerV4, UNSUPPORTED_ZOD_TYPES, applyCompatLayer, convertSchemaToZod, convertZodSchemaToAISDKSchema, isArr, isNumber, isObj, isOptional, isString, isUnion };
|
|
4926
|
+
export { ALL_ARRAY_CHECKS, ALL_NUMBER_CHECKS, ALL_STRING_CHECKS, ALL_ZOD_TYPES, AnthropicSchemaCompatLayer, DeepSeekSchemaCompatLayer, GoogleSchemaCompatLayer, MetaSchemaCompatLayer, OpenAIReasoningSchemaCompatLayer, OpenAISchemaCompatLayer, SUPPORTED_ZOD_TYPES, SchemaCompatLayer3 as SchemaCompatLayer, SchemaCompatLayer as SchemaCompatLayerV3, SchemaCompatLayer2 as SchemaCompatLayerV4, UNSUPPORTED_ZOD_TYPES, applyCompatLayer, convertSchemaToZod, convertZodSchemaToAISDKSchema, isArr, isNumber, isObj, isOptional, isString, isUnion, jsonSchema };
|
|
1398
4927
|
//# sourceMappingURL=index.js.map
|
|
1399
4928
|
//# sourceMappingURL=index.js.map
|