@sdk-it/rpc 0.29.0 → 0.30.0
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1732 -26
- package/dist/index.js.map +4 -4
- package/dist/lib/rpc.d.ts +1 -0
- package/dist/lib/rpc.d.ts.map +1 -1
- package/dist/lib/zod.d.ts +31 -0
- package/dist/lib/zod.d.ts.map +1 -0
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,16 +1,1334 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __export = (target, all) => {
|
|
3
|
-
for (var
|
|
4
|
-
__defProp(target,
|
|
3
|
+
for (var name16 in all)
|
|
4
|
+
__defProp(target, name16, { get: all[name16], enumerable: true });
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
+
// node_modules/ai/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
8
|
+
var marker = "vercel.ai.error";
|
|
9
|
+
var symbol = Symbol.for(marker);
|
|
10
|
+
var _a;
|
|
11
|
+
var _AISDKError = class _AISDKError2 extends Error {
|
|
12
|
+
/**
|
|
13
|
+
* Creates an AI SDK Error.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} params - The parameters for creating the error.
|
|
16
|
+
* @param {string} params.name - The name of the error.
|
|
17
|
+
* @param {string} params.message - The error message.
|
|
18
|
+
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
19
|
+
*/
|
|
20
|
+
constructor({
|
|
21
|
+
name: name143,
|
|
22
|
+
message,
|
|
23
|
+
cause
|
|
24
|
+
}) {
|
|
25
|
+
super(message);
|
|
26
|
+
this[_a] = true;
|
|
27
|
+
this.name = name143;
|
|
28
|
+
this.cause = cause;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Checks if the given error is an AI SDK Error.
|
|
32
|
+
* @param {unknown} error - The error to check.
|
|
33
|
+
* @returns {boolean} True if the error is an AI SDK Error, false otherwise.
|
|
34
|
+
*/
|
|
35
|
+
static isInstance(error) {
|
|
36
|
+
return _AISDKError2.hasMarker(error, marker);
|
|
37
|
+
}
|
|
38
|
+
static hasMarker(error, marker153) {
|
|
39
|
+
const markerSymbol = Symbol.for(marker153);
|
|
40
|
+
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
_a = symbol;
|
|
44
|
+
var AISDKError = _AISDKError;
|
|
45
|
+
var name = "AI_APICallError";
|
|
46
|
+
var marker2 = `vercel.ai.error.${name}`;
|
|
47
|
+
var symbol2 = Symbol.for(marker2);
|
|
48
|
+
var _a2;
|
|
49
|
+
_a2 = symbol2;
|
|
50
|
+
var name2 = "AI_EmptyResponseBodyError";
|
|
51
|
+
var marker3 = `vercel.ai.error.${name2}`;
|
|
52
|
+
var symbol3 = Symbol.for(marker3);
|
|
53
|
+
var _a3;
|
|
54
|
+
_a3 = symbol3;
|
|
55
|
+
function getErrorMessage(error) {
|
|
56
|
+
if (error == null) {
|
|
57
|
+
return "unknown error";
|
|
58
|
+
}
|
|
59
|
+
if (typeof error === "string") {
|
|
60
|
+
return error;
|
|
61
|
+
}
|
|
62
|
+
if (error instanceof Error) {
|
|
63
|
+
return error.message;
|
|
64
|
+
}
|
|
65
|
+
return JSON.stringify(error);
|
|
66
|
+
}
|
|
67
|
+
var name3 = "AI_InvalidArgumentError";
|
|
68
|
+
var marker4 = `vercel.ai.error.${name3}`;
|
|
69
|
+
var symbol4 = Symbol.for(marker4);
|
|
70
|
+
var _a4;
|
|
71
|
+
var InvalidArgumentError = class extends AISDKError {
|
|
72
|
+
constructor({
|
|
73
|
+
message,
|
|
74
|
+
cause,
|
|
75
|
+
argument
|
|
76
|
+
}) {
|
|
77
|
+
super({ name: name3, message, cause });
|
|
78
|
+
this[_a4] = true;
|
|
79
|
+
this.argument = argument;
|
|
80
|
+
}
|
|
81
|
+
static isInstance(error) {
|
|
82
|
+
return AISDKError.hasMarker(error, marker4);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
_a4 = symbol4;
|
|
86
|
+
var name4 = "AI_InvalidPromptError";
|
|
87
|
+
var marker5 = `vercel.ai.error.${name4}`;
|
|
88
|
+
var symbol5 = Symbol.for(marker5);
|
|
89
|
+
var _a5;
|
|
90
|
+
_a5 = symbol5;
|
|
91
|
+
var name5 = "AI_InvalidResponseDataError";
|
|
92
|
+
var marker6 = `vercel.ai.error.${name5}`;
|
|
93
|
+
var symbol6 = Symbol.for(marker6);
|
|
94
|
+
var _a6;
|
|
95
|
+
_a6 = symbol6;
|
|
96
|
+
var name6 = "AI_JSONParseError";
|
|
97
|
+
var marker7 = `vercel.ai.error.${name6}`;
|
|
98
|
+
var symbol7 = Symbol.for(marker7);
|
|
99
|
+
var _a7;
|
|
100
|
+
var JSONParseError = class extends AISDKError {
|
|
101
|
+
constructor({ text: text2, cause }) {
|
|
102
|
+
super({
|
|
103
|
+
name: name6,
|
|
104
|
+
message: `JSON parsing failed: Text: ${text2}.
|
|
105
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
106
|
+
cause
|
|
107
|
+
});
|
|
108
|
+
this[_a7] = true;
|
|
109
|
+
this.text = text2;
|
|
110
|
+
}
|
|
111
|
+
static isInstance(error) {
|
|
112
|
+
return AISDKError.hasMarker(error, marker7);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
_a7 = symbol7;
|
|
116
|
+
var name7 = "AI_LoadAPIKeyError";
|
|
117
|
+
var marker8 = `vercel.ai.error.${name7}`;
|
|
118
|
+
var symbol8 = Symbol.for(marker8);
|
|
119
|
+
var _a8;
|
|
120
|
+
_a8 = symbol8;
|
|
121
|
+
var name8 = "AI_LoadSettingError";
|
|
122
|
+
var marker9 = `vercel.ai.error.${name8}`;
|
|
123
|
+
var symbol9 = Symbol.for(marker9);
|
|
124
|
+
var _a9;
|
|
125
|
+
_a9 = symbol9;
|
|
126
|
+
var name9 = "AI_NoContentGeneratedError";
|
|
127
|
+
var marker10 = `vercel.ai.error.${name9}`;
|
|
128
|
+
var symbol10 = Symbol.for(marker10);
|
|
129
|
+
var _a10;
|
|
130
|
+
_a10 = symbol10;
|
|
131
|
+
var name10 = "AI_NoSuchModelError";
|
|
132
|
+
var marker11 = `vercel.ai.error.${name10}`;
|
|
133
|
+
var symbol11 = Symbol.for(marker11);
|
|
134
|
+
var _a11;
|
|
135
|
+
_a11 = symbol11;
|
|
136
|
+
var name11 = "AI_TooManyEmbeddingValuesForCallError";
|
|
137
|
+
var marker12 = `vercel.ai.error.${name11}`;
|
|
138
|
+
var symbol12 = Symbol.for(marker12);
|
|
139
|
+
var _a12;
|
|
140
|
+
_a12 = symbol12;
|
|
141
|
+
var name12 = "AI_TypeValidationError";
|
|
142
|
+
var marker13 = `vercel.ai.error.${name12}`;
|
|
143
|
+
var symbol13 = Symbol.for(marker13);
|
|
144
|
+
var _a13;
|
|
145
|
+
var _TypeValidationError = class _TypeValidationError2 extends AISDKError {
|
|
146
|
+
constructor({ value, cause }) {
|
|
147
|
+
super({
|
|
148
|
+
name: name12,
|
|
149
|
+
message: `Type validation failed: Value: ${JSON.stringify(value)}.
|
|
150
|
+
Error message: ${getErrorMessage(cause)}`,
|
|
151
|
+
cause
|
|
152
|
+
});
|
|
153
|
+
this[_a13] = true;
|
|
154
|
+
this.value = value;
|
|
155
|
+
}
|
|
156
|
+
static isInstance(error) {
|
|
157
|
+
return AISDKError.hasMarker(error, marker13);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Wraps an error into a TypeValidationError.
|
|
161
|
+
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
162
|
+
* Otherwise, it creates a new TypeValidationError.
|
|
163
|
+
*
|
|
164
|
+
* @param {Object} params - The parameters for wrapping the error.
|
|
165
|
+
* @param {unknown} params.value - The value that failed validation.
|
|
166
|
+
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
167
|
+
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
168
|
+
*/
|
|
169
|
+
static wrap({
|
|
170
|
+
value,
|
|
171
|
+
cause
|
|
172
|
+
}) {
|
|
173
|
+
return _TypeValidationError2.isInstance(cause) && cause.value === value ? cause : new _TypeValidationError2({ value, cause });
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
_a13 = symbol13;
|
|
177
|
+
var TypeValidationError = _TypeValidationError;
|
|
178
|
+
var name13 = "AI_UnsupportedFunctionalityError";
|
|
179
|
+
var marker14 = `vercel.ai.error.${name13}`;
|
|
180
|
+
var symbol14 = Symbol.for(marker14);
|
|
181
|
+
var _a14;
|
|
182
|
+
_a14 = symbol14;
|
|
183
|
+
|
|
184
|
+
// node_modules/ai/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
185
|
+
import * as z4 from "zod/v4";
|
|
186
|
+
import zodToJsonSchema from "zod-to-json-schema";
|
|
187
|
+
var createIdGenerator = ({
|
|
188
|
+
prefix,
|
|
189
|
+
size = 16,
|
|
190
|
+
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
|
|
191
|
+
separator = "-"
|
|
192
|
+
} = {}) => {
|
|
193
|
+
const generator = () => {
|
|
194
|
+
const alphabetLength = alphabet.length;
|
|
195
|
+
const chars = new Array(size);
|
|
196
|
+
for (let i = 0; i < size; i++) {
|
|
197
|
+
chars[i] = alphabet[Math.random() * alphabetLength | 0];
|
|
198
|
+
}
|
|
199
|
+
return chars.join("");
|
|
200
|
+
};
|
|
201
|
+
if (prefix == null) {
|
|
202
|
+
return generator;
|
|
203
|
+
}
|
|
204
|
+
if (alphabet.includes(separator)) {
|
|
205
|
+
throw new InvalidArgumentError({
|
|
206
|
+
argument: "separator",
|
|
207
|
+
message: `The separator "${separator}" must not be part of the alphabet "${alphabet}".`
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return () => `${prefix}${separator}${generator()}`;
|
|
211
|
+
};
|
|
212
|
+
var generateId = createIdGenerator();
|
|
213
|
+
var suspectProtoRx = /"__proto__"\s*:/;
|
|
214
|
+
var suspectConstructorRx = /"constructor"\s*:/;
|
|
215
|
+
function _parse(text2) {
|
|
216
|
+
const obj = JSON.parse(text2);
|
|
217
|
+
if (obj === null || typeof obj !== "object") {
|
|
218
|
+
return obj;
|
|
219
|
+
}
|
|
220
|
+
if (suspectProtoRx.test(text2) === false && suspectConstructorRx.test(text2) === false) {
|
|
221
|
+
return obj;
|
|
222
|
+
}
|
|
223
|
+
return filter(obj);
|
|
224
|
+
}
|
|
225
|
+
function filter(obj) {
|
|
226
|
+
let next = [obj];
|
|
227
|
+
while (next.length) {
|
|
228
|
+
const nodes = next;
|
|
229
|
+
next = [];
|
|
230
|
+
for (const node of nodes) {
|
|
231
|
+
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
232
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
233
|
+
}
|
|
234
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
235
|
+
throw new SyntaxError("Object contains forbidden prototype property");
|
|
236
|
+
}
|
|
237
|
+
for (const key in node) {
|
|
238
|
+
const value = node[key];
|
|
239
|
+
if (value && typeof value === "object") {
|
|
240
|
+
next.push(value);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return obj;
|
|
246
|
+
}
|
|
247
|
+
function secureJsonParse(text2) {
|
|
248
|
+
const { stackTraceLimit } = Error;
|
|
249
|
+
Error.stackTraceLimit = 0;
|
|
250
|
+
try {
|
|
251
|
+
return _parse(text2);
|
|
252
|
+
} finally {
|
|
253
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
var validatorSymbol = Symbol.for("vercel.ai.validator");
|
|
257
|
+
function validator(validate) {
|
|
258
|
+
return { [validatorSymbol]: true, validate };
|
|
259
|
+
}
|
|
260
|
+
function isValidator(value) {
|
|
261
|
+
return typeof value === "object" && value !== null && validatorSymbol in value && value[validatorSymbol] === true && "validate" in value;
|
|
262
|
+
}
|
|
263
|
+
function asValidator(value) {
|
|
264
|
+
return isValidator(value) ? value : standardSchemaValidator(value);
|
|
265
|
+
}
|
|
266
|
+
function standardSchemaValidator(standardSchema) {
|
|
267
|
+
return validator(async (value) => {
|
|
268
|
+
const result = await standardSchema["~standard"].validate(value);
|
|
269
|
+
return result.issues == null ? { success: true, value: result.value } : {
|
|
270
|
+
success: false,
|
|
271
|
+
error: new TypeValidationError({
|
|
272
|
+
value,
|
|
273
|
+
cause: result.issues
|
|
274
|
+
})
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
async function safeValidateTypes({
|
|
279
|
+
value,
|
|
280
|
+
schema
|
|
281
|
+
}) {
|
|
282
|
+
const validator2 = asValidator(schema);
|
|
283
|
+
try {
|
|
284
|
+
if (validator2.validate == null) {
|
|
285
|
+
return { success: true, value, rawValue: value };
|
|
286
|
+
}
|
|
287
|
+
const result = await validator2.validate(value);
|
|
288
|
+
if (result.success) {
|
|
289
|
+
return { success: true, value: result.value, rawValue: value };
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
success: false,
|
|
293
|
+
error: TypeValidationError.wrap({ value, cause: result.error }),
|
|
294
|
+
rawValue: value
|
|
295
|
+
};
|
|
296
|
+
} catch (error) {
|
|
297
|
+
return {
|
|
298
|
+
success: false,
|
|
299
|
+
error: TypeValidationError.wrap({ value, cause: error }),
|
|
300
|
+
rawValue: value
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
async function safeParseJSON({
|
|
305
|
+
text: text2,
|
|
306
|
+
schema
|
|
307
|
+
}) {
|
|
308
|
+
try {
|
|
309
|
+
const value = secureJsonParse(text2);
|
|
310
|
+
if (schema == null) {
|
|
311
|
+
return { success: true, value, rawValue: value };
|
|
312
|
+
}
|
|
313
|
+
return await safeValidateTypes({ value, schema });
|
|
314
|
+
} catch (error) {
|
|
315
|
+
return {
|
|
316
|
+
success: false,
|
|
317
|
+
error: JSONParseError.isInstance(error) ? error : new JSONParseError({ text: text2, cause: error }),
|
|
318
|
+
rawValue: void 0
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function tool(tool2) {
|
|
323
|
+
return tool2;
|
|
324
|
+
}
|
|
325
|
+
function zod3Schema(zodSchema2, options) {
|
|
326
|
+
var _a16;
|
|
327
|
+
const useReferences = (_a16 = options == null ? void 0 : options.useReferences) != null ? _a16 : false;
|
|
328
|
+
return jsonSchema(
|
|
329
|
+
zodToJsonSchema(zodSchema2, {
|
|
330
|
+
$refStrategy: useReferences ? "root" : "none",
|
|
331
|
+
target: "jsonSchema7"
|
|
332
|
+
// note: openai mode breaks various gemini conversions
|
|
333
|
+
}),
|
|
334
|
+
{
|
|
335
|
+
validate: async (value) => {
|
|
336
|
+
const result = await zodSchema2.safeParseAsync(value);
|
|
337
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
function zod4Schema(zodSchema2, options) {
|
|
343
|
+
var _a16;
|
|
344
|
+
const useReferences = (_a16 = options == null ? void 0 : options.useReferences) != null ? _a16 : false;
|
|
345
|
+
const z4JSONSchema = z4.toJSONSchema(zodSchema2, {
|
|
346
|
+
target: "draft-7",
|
|
347
|
+
io: "output",
|
|
348
|
+
reused: useReferences ? "ref" : "inline"
|
|
349
|
+
});
|
|
350
|
+
return jsonSchema(z4JSONSchema, {
|
|
351
|
+
validate: async (value) => {
|
|
352
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
|
353
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
function isZod4Schema(zodSchema2) {
|
|
358
|
+
return "_zod" in zodSchema2;
|
|
359
|
+
}
|
|
360
|
+
function zodSchema(zodSchema2, options) {
|
|
361
|
+
if (isZod4Schema(zodSchema2)) {
|
|
362
|
+
return zod4Schema(zodSchema2, options);
|
|
363
|
+
} else {
|
|
364
|
+
return zod3Schema(zodSchema2, options);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
var schemaSymbol = Symbol.for("vercel.ai.schema");
|
|
368
|
+
function jsonSchema(jsonSchema2, {
|
|
369
|
+
validate
|
|
370
|
+
} = {}) {
|
|
371
|
+
return {
|
|
372
|
+
[schemaSymbol]: true,
|
|
373
|
+
_type: void 0,
|
|
374
|
+
// should never be used directly
|
|
375
|
+
[validatorSymbol]: true,
|
|
376
|
+
jsonSchema: jsonSchema2,
|
|
377
|
+
validate
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function isSchema(value) {
|
|
381
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
|
382
|
+
}
|
|
383
|
+
function asSchema(schema) {
|
|
384
|
+
return schema == null ? jsonSchema({
|
|
385
|
+
properties: {},
|
|
386
|
+
additionalProperties: false
|
|
387
|
+
}) : isSchema(schema) ? schema : zodSchema(schema);
|
|
388
|
+
}
|
|
389
|
+
var { btoa, atob } = globalThis;
|
|
390
|
+
|
|
391
|
+
// node_modules/ai/dist/index.mjs
|
|
392
|
+
import { z } from "zod/v4";
|
|
393
|
+
import { z as z6 } from "zod/v4";
|
|
394
|
+
import { z as z5 } from "zod/v4";
|
|
395
|
+
import { z as z3 } from "zod/v4";
|
|
396
|
+
import { z as z2 } from "zod/v4";
|
|
397
|
+
import { z as z42 } from "zod/v4";
|
|
398
|
+
import { z as z7 } from "zod/v4";
|
|
399
|
+
import { z as z9 } from "zod/v4";
|
|
400
|
+
import { z as z8 } from "zod/v4";
|
|
401
|
+
var __defProp2 = Object.defineProperty;
|
|
402
|
+
var __export2 = (target, all) => {
|
|
403
|
+
for (var name16 in all)
|
|
404
|
+
__defProp2(target, name16, { get: all[name16], enumerable: true });
|
|
405
|
+
};
|
|
406
|
+
var name14 = "AI_NoOutputSpecifiedError";
|
|
407
|
+
var marker15 = `vercel.ai.error.${name14}`;
|
|
408
|
+
var symbol15 = Symbol.for(marker15);
|
|
409
|
+
var _a15;
|
|
410
|
+
_a15 = symbol15;
|
|
411
|
+
var name22 = "AI_InvalidArgumentError";
|
|
412
|
+
var marker22 = `vercel.ai.error.${name22}`;
|
|
413
|
+
var symbol22 = Symbol.for(marker22);
|
|
414
|
+
var _a22;
|
|
415
|
+
_a22 = symbol22;
|
|
416
|
+
var name32 = "AI_RetryError";
|
|
417
|
+
var marker32 = `vercel.ai.error.${name32}`;
|
|
418
|
+
var symbol32 = Symbol.for(marker32);
|
|
419
|
+
var _a32;
|
|
420
|
+
_a32 = symbol32;
|
|
421
|
+
var name42 = "AI_DownloadError";
|
|
422
|
+
var marker42 = `vercel.ai.error.${name42}`;
|
|
423
|
+
var symbol42 = Symbol.for(marker42);
|
|
424
|
+
var _a42;
|
|
425
|
+
_a42 = symbol42;
|
|
426
|
+
var name52 = "AI_InvalidDataContentError";
|
|
427
|
+
var marker52 = `vercel.ai.error.${name52}`;
|
|
428
|
+
var symbol52 = Symbol.for(marker52);
|
|
429
|
+
var _a52;
|
|
430
|
+
_a52 = symbol52;
|
|
431
|
+
var dataContentSchema = z.union([
|
|
432
|
+
z.string(),
|
|
433
|
+
z.instanceof(Uint8Array),
|
|
434
|
+
z.instanceof(ArrayBuffer),
|
|
435
|
+
z.custom(
|
|
436
|
+
// Buffer might not be available in some environments such as CloudFlare:
|
|
437
|
+
(value) => {
|
|
438
|
+
var _a16, _b;
|
|
439
|
+
return (_b = (_a16 = globalThis.Buffer) == null ? void 0 : _a16.isBuffer(value)) != null ? _b : false;
|
|
440
|
+
},
|
|
441
|
+
{ message: "Must be a Buffer" }
|
|
442
|
+
)
|
|
443
|
+
]);
|
|
444
|
+
var name62 = "AI_InvalidMessageRoleError";
|
|
445
|
+
var marker62 = `vercel.ai.error.${name62}`;
|
|
446
|
+
var symbol62 = Symbol.for(marker62);
|
|
447
|
+
var _a62;
|
|
448
|
+
_a62 = symbol62;
|
|
449
|
+
var name72 = "AI_InvalidStreamPartError";
|
|
450
|
+
var marker72 = `vercel.ai.error.${name72}`;
|
|
451
|
+
var symbol72 = Symbol.for(marker72);
|
|
452
|
+
var _a72;
|
|
453
|
+
_a72 = symbol72;
|
|
454
|
+
var name82 = "AI_InvalidToolInputError";
|
|
455
|
+
var marker82 = `vercel.ai.error.${name82}`;
|
|
456
|
+
var symbol82 = Symbol.for(marker82);
|
|
457
|
+
var _a82;
|
|
458
|
+
_a82 = symbol82;
|
|
459
|
+
var name92 = "AI_MCPClientError";
|
|
460
|
+
var marker92 = `vercel.ai.error.${name92}`;
|
|
461
|
+
var symbol92 = Symbol.for(marker92);
|
|
462
|
+
var _a92;
|
|
463
|
+
_a92 = symbol92;
|
|
464
|
+
var name102 = "AI_NoImageGeneratedError";
|
|
465
|
+
var marker102 = `vercel.ai.error.${name102}`;
|
|
466
|
+
var symbol102 = Symbol.for(marker102);
|
|
467
|
+
var _a102;
|
|
468
|
+
_a102 = symbol102;
|
|
469
|
+
var name112 = "AI_NoObjectGeneratedError";
|
|
470
|
+
var marker112 = `vercel.ai.error.${name112}`;
|
|
471
|
+
var symbol112 = Symbol.for(marker112);
|
|
472
|
+
var _a112;
|
|
473
|
+
var NoObjectGeneratedError = class extends AISDKError {
|
|
474
|
+
constructor({
|
|
475
|
+
message = "No object generated.",
|
|
476
|
+
cause,
|
|
477
|
+
text: text2,
|
|
478
|
+
response,
|
|
479
|
+
usage,
|
|
480
|
+
finishReason
|
|
481
|
+
}) {
|
|
482
|
+
super({ name: name112, message, cause });
|
|
483
|
+
this[_a112] = true;
|
|
484
|
+
this.text = text2;
|
|
485
|
+
this.response = response;
|
|
486
|
+
this.usage = usage;
|
|
487
|
+
this.finishReason = finishReason;
|
|
488
|
+
}
|
|
489
|
+
static isInstance(error) {
|
|
490
|
+
return AISDKError.hasMarker(error, marker112);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
_a112 = symbol112;
|
|
494
|
+
var name122 = "AI_NoSuchToolError";
|
|
495
|
+
var marker122 = `vercel.ai.error.${name122}`;
|
|
496
|
+
var symbol122 = Symbol.for(marker122);
|
|
497
|
+
var _a122;
|
|
498
|
+
_a122 = symbol122;
|
|
499
|
+
var name132 = "AI_ToolCallRepairError";
|
|
500
|
+
var marker132 = `vercel.ai.error.${name132}`;
|
|
501
|
+
var symbol132 = Symbol.for(marker132);
|
|
502
|
+
var _a132;
|
|
503
|
+
_a132 = symbol132;
|
|
504
|
+
var name142 = "AI_MessageConversionError";
|
|
505
|
+
var marker142 = `vercel.ai.error.${name142}`;
|
|
506
|
+
var symbol142 = Symbol.for(marker142);
|
|
507
|
+
var _a142;
|
|
508
|
+
_a142 = symbol142;
|
|
509
|
+
var jsonValueSchema = z2.lazy(
|
|
510
|
+
() => z2.union([
|
|
511
|
+
z2.null(),
|
|
512
|
+
z2.string(),
|
|
513
|
+
z2.number(),
|
|
514
|
+
z2.boolean(),
|
|
515
|
+
z2.record(z2.string(), jsonValueSchema),
|
|
516
|
+
z2.array(jsonValueSchema)
|
|
517
|
+
])
|
|
518
|
+
);
|
|
519
|
+
var providerMetadataSchema = z3.record(
|
|
520
|
+
z3.string(),
|
|
521
|
+
z3.record(z3.string(), jsonValueSchema)
|
|
522
|
+
);
|
|
523
|
+
var textPartSchema = z42.object({
|
|
524
|
+
type: z42.literal("text"),
|
|
525
|
+
text: z42.string(),
|
|
526
|
+
providerOptions: providerMetadataSchema.optional()
|
|
527
|
+
});
|
|
528
|
+
var imagePartSchema = z42.object({
|
|
529
|
+
type: z42.literal("image"),
|
|
530
|
+
image: z42.union([dataContentSchema, z42.instanceof(URL)]),
|
|
531
|
+
mediaType: z42.string().optional(),
|
|
532
|
+
providerOptions: providerMetadataSchema.optional()
|
|
533
|
+
});
|
|
534
|
+
var filePartSchema = z42.object({
|
|
535
|
+
type: z42.literal("file"),
|
|
536
|
+
data: z42.union([dataContentSchema, z42.instanceof(URL)]),
|
|
537
|
+
filename: z42.string().optional(),
|
|
538
|
+
mediaType: z42.string(),
|
|
539
|
+
providerOptions: providerMetadataSchema.optional()
|
|
540
|
+
});
|
|
541
|
+
var reasoningPartSchema = z42.object({
|
|
542
|
+
type: z42.literal("reasoning"),
|
|
543
|
+
text: z42.string(),
|
|
544
|
+
providerOptions: providerMetadataSchema.optional()
|
|
545
|
+
});
|
|
546
|
+
var toolCallPartSchema = z42.object({
|
|
547
|
+
type: z42.literal("tool-call"),
|
|
548
|
+
toolCallId: z42.string(),
|
|
549
|
+
toolName: z42.string(),
|
|
550
|
+
input: z42.unknown(),
|
|
551
|
+
providerOptions: providerMetadataSchema.optional(),
|
|
552
|
+
providerExecuted: z42.boolean().optional()
|
|
553
|
+
});
|
|
554
|
+
var outputSchema = z42.discriminatedUnion("type", [
|
|
555
|
+
z42.object({
|
|
556
|
+
type: z42.literal("text"),
|
|
557
|
+
value: z42.string()
|
|
558
|
+
}),
|
|
559
|
+
z42.object({
|
|
560
|
+
type: z42.literal("json"),
|
|
561
|
+
value: jsonValueSchema
|
|
562
|
+
}),
|
|
563
|
+
z42.object({
|
|
564
|
+
type: z42.literal("error-text"),
|
|
565
|
+
value: z42.string()
|
|
566
|
+
}),
|
|
567
|
+
z42.object({
|
|
568
|
+
type: z42.literal("error-json"),
|
|
569
|
+
value: jsonValueSchema
|
|
570
|
+
}),
|
|
571
|
+
z42.object({
|
|
572
|
+
type: z42.literal("content"),
|
|
573
|
+
value: z42.array(
|
|
574
|
+
z42.union([
|
|
575
|
+
z42.object({
|
|
576
|
+
type: z42.literal("text"),
|
|
577
|
+
text: z42.string()
|
|
578
|
+
}),
|
|
579
|
+
z42.object({
|
|
580
|
+
type: z42.literal("media"),
|
|
581
|
+
data: z42.string(),
|
|
582
|
+
mediaType: z42.string()
|
|
583
|
+
})
|
|
584
|
+
])
|
|
585
|
+
)
|
|
586
|
+
})
|
|
587
|
+
]);
|
|
588
|
+
var toolResultPartSchema = z42.object({
|
|
589
|
+
type: z42.literal("tool-result"),
|
|
590
|
+
toolCallId: z42.string(),
|
|
591
|
+
toolName: z42.string(),
|
|
592
|
+
output: outputSchema,
|
|
593
|
+
providerOptions: providerMetadataSchema.optional()
|
|
594
|
+
});
|
|
595
|
+
var systemModelMessageSchema = z5.object(
|
|
596
|
+
{
|
|
597
|
+
role: z5.literal("system"),
|
|
598
|
+
content: z5.string(),
|
|
599
|
+
providerOptions: providerMetadataSchema.optional()
|
|
600
|
+
}
|
|
601
|
+
);
|
|
602
|
+
var userModelMessageSchema = z5.object({
|
|
603
|
+
role: z5.literal("user"),
|
|
604
|
+
content: z5.union([
|
|
605
|
+
z5.string(),
|
|
606
|
+
z5.array(z5.union([textPartSchema, imagePartSchema, filePartSchema]))
|
|
607
|
+
]),
|
|
608
|
+
providerOptions: providerMetadataSchema.optional()
|
|
609
|
+
});
|
|
610
|
+
var assistantModelMessageSchema = z5.object({
|
|
611
|
+
role: z5.literal("assistant"),
|
|
612
|
+
content: z5.union([
|
|
613
|
+
z5.string(),
|
|
614
|
+
z5.array(
|
|
615
|
+
z5.union([
|
|
616
|
+
textPartSchema,
|
|
617
|
+
filePartSchema,
|
|
618
|
+
reasoningPartSchema,
|
|
619
|
+
toolCallPartSchema,
|
|
620
|
+
toolResultPartSchema
|
|
621
|
+
])
|
|
622
|
+
)
|
|
623
|
+
]),
|
|
624
|
+
providerOptions: providerMetadataSchema.optional()
|
|
625
|
+
});
|
|
626
|
+
var toolModelMessageSchema = z5.object({
|
|
627
|
+
role: z5.literal("tool"),
|
|
628
|
+
content: z5.array(toolResultPartSchema),
|
|
629
|
+
providerOptions: providerMetadataSchema.optional()
|
|
630
|
+
});
|
|
631
|
+
var modelMessageSchema = z5.union([
|
|
632
|
+
systemModelMessageSchema,
|
|
633
|
+
userModelMessageSchema,
|
|
634
|
+
assistantModelMessageSchema,
|
|
635
|
+
toolModelMessageSchema
|
|
636
|
+
]);
|
|
637
|
+
var originalGenerateId = createIdGenerator({
|
|
638
|
+
prefix: "aitxt",
|
|
639
|
+
size: 24
|
|
640
|
+
});
|
|
641
|
+
var JsonToSseTransformStream = class extends TransformStream {
|
|
642
|
+
constructor() {
|
|
643
|
+
super({
|
|
644
|
+
transform(part, controller) {
|
|
645
|
+
controller.enqueue(`data: ${JSON.stringify(part)}
|
|
646
|
+
|
|
647
|
+
`);
|
|
648
|
+
},
|
|
649
|
+
flush(controller) {
|
|
650
|
+
controller.enqueue("data: [DONE]\n\n");
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
var uiMessageChunkSchema = z7.union([
|
|
656
|
+
z7.strictObject({
|
|
657
|
+
type: z7.literal("text-start"),
|
|
658
|
+
id: z7.string(),
|
|
659
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
660
|
+
}),
|
|
661
|
+
z7.strictObject({
|
|
662
|
+
type: z7.literal("text-delta"),
|
|
663
|
+
id: z7.string(),
|
|
664
|
+
delta: z7.string(),
|
|
665
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
666
|
+
}),
|
|
667
|
+
z7.strictObject({
|
|
668
|
+
type: z7.literal("text-end"),
|
|
669
|
+
id: z7.string(),
|
|
670
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
671
|
+
}),
|
|
672
|
+
z7.strictObject({
|
|
673
|
+
type: z7.literal("error"),
|
|
674
|
+
errorText: z7.string()
|
|
675
|
+
}),
|
|
676
|
+
z7.strictObject({
|
|
677
|
+
type: z7.literal("tool-input-start"),
|
|
678
|
+
toolCallId: z7.string(),
|
|
679
|
+
toolName: z7.string(),
|
|
680
|
+
providerExecuted: z7.boolean().optional()
|
|
681
|
+
}),
|
|
682
|
+
z7.strictObject({
|
|
683
|
+
type: z7.literal("tool-input-delta"),
|
|
684
|
+
toolCallId: z7.string(),
|
|
685
|
+
inputTextDelta: z7.string()
|
|
686
|
+
}),
|
|
687
|
+
z7.strictObject({
|
|
688
|
+
type: z7.literal("tool-input-available"),
|
|
689
|
+
toolCallId: z7.string(),
|
|
690
|
+
toolName: z7.string(),
|
|
691
|
+
input: z7.unknown(),
|
|
692
|
+
providerExecuted: z7.boolean().optional(),
|
|
693
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
694
|
+
}),
|
|
695
|
+
z7.strictObject({
|
|
696
|
+
type: z7.literal("tool-output-available"),
|
|
697
|
+
toolCallId: z7.string(),
|
|
698
|
+
output: z7.unknown(),
|
|
699
|
+
providerExecuted: z7.boolean().optional()
|
|
700
|
+
}),
|
|
701
|
+
z7.strictObject({
|
|
702
|
+
type: z7.literal("tool-output-error"),
|
|
703
|
+
toolCallId: z7.string(),
|
|
704
|
+
errorText: z7.string(),
|
|
705
|
+
providerExecuted: z7.boolean().optional()
|
|
706
|
+
}),
|
|
707
|
+
z7.strictObject({
|
|
708
|
+
type: z7.literal("reasoning"),
|
|
709
|
+
text: z7.string(),
|
|
710
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
711
|
+
}),
|
|
712
|
+
z7.strictObject({
|
|
713
|
+
type: z7.literal("reasoning-start"),
|
|
714
|
+
id: z7.string(),
|
|
715
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
716
|
+
}),
|
|
717
|
+
z7.strictObject({
|
|
718
|
+
type: z7.literal("reasoning-delta"),
|
|
719
|
+
id: z7.string(),
|
|
720
|
+
delta: z7.string(),
|
|
721
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
722
|
+
}),
|
|
723
|
+
z7.strictObject({
|
|
724
|
+
type: z7.literal("reasoning-end"),
|
|
725
|
+
id: z7.string(),
|
|
726
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
727
|
+
}),
|
|
728
|
+
z7.strictObject({
|
|
729
|
+
type: z7.literal("reasoning-part-finish")
|
|
730
|
+
}),
|
|
731
|
+
z7.strictObject({
|
|
732
|
+
type: z7.literal("source-url"),
|
|
733
|
+
sourceId: z7.string(),
|
|
734
|
+
url: z7.string(),
|
|
735
|
+
title: z7.string().optional(),
|
|
736
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
737
|
+
}),
|
|
738
|
+
z7.strictObject({
|
|
739
|
+
type: z7.literal("source-document"),
|
|
740
|
+
sourceId: z7.string(),
|
|
741
|
+
mediaType: z7.string(),
|
|
742
|
+
title: z7.string(),
|
|
743
|
+
filename: z7.string().optional(),
|
|
744
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
745
|
+
}),
|
|
746
|
+
z7.strictObject({
|
|
747
|
+
type: z7.literal("file"),
|
|
748
|
+
url: z7.string(),
|
|
749
|
+
mediaType: z7.string(),
|
|
750
|
+
providerMetadata: providerMetadataSchema.optional()
|
|
751
|
+
}),
|
|
752
|
+
z7.strictObject({
|
|
753
|
+
type: z7.string().startsWith("data-"),
|
|
754
|
+
id: z7.string().optional(),
|
|
755
|
+
data: z7.unknown(),
|
|
756
|
+
transient: z7.boolean().optional()
|
|
757
|
+
}),
|
|
758
|
+
z7.strictObject({
|
|
759
|
+
type: z7.literal("start-step")
|
|
760
|
+
}),
|
|
761
|
+
z7.strictObject({
|
|
762
|
+
type: z7.literal("finish-step")
|
|
763
|
+
}),
|
|
764
|
+
z7.strictObject({
|
|
765
|
+
type: z7.literal("start"),
|
|
766
|
+
messageId: z7.string().optional(),
|
|
767
|
+
messageMetadata: z7.unknown().optional()
|
|
768
|
+
}),
|
|
769
|
+
z7.strictObject({
|
|
770
|
+
type: z7.literal("finish"),
|
|
771
|
+
messageMetadata: z7.unknown().optional()
|
|
772
|
+
}),
|
|
773
|
+
z7.strictObject({
|
|
774
|
+
type: z7.literal("abort")
|
|
775
|
+
}),
|
|
776
|
+
z7.strictObject({
|
|
777
|
+
type: z7.literal("message-metadata"),
|
|
778
|
+
messageMetadata: z7.unknown()
|
|
779
|
+
})
|
|
780
|
+
]);
|
|
781
|
+
function fixJson(input) {
|
|
782
|
+
const stack = ["ROOT"];
|
|
783
|
+
let lastValidIndex = -1;
|
|
784
|
+
let literalStart = null;
|
|
785
|
+
function processValueStart(char, i, swapState) {
|
|
786
|
+
{
|
|
787
|
+
switch (char) {
|
|
788
|
+
case '"': {
|
|
789
|
+
lastValidIndex = i;
|
|
790
|
+
stack.pop();
|
|
791
|
+
stack.push(swapState);
|
|
792
|
+
stack.push("INSIDE_STRING");
|
|
793
|
+
break;
|
|
794
|
+
}
|
|
795
|
+
case "f":
|
|
796
|
+
case "t":
|
|
797
|
+
case "n": {
|
|
798
|
+
lastValidIndex = i;
|
|
799
|
+
literalStart = i;
|
|
800
|
+
stack.pop();
|
|
801
|
+
stack.push(swapState);
|
|
802
|
+
stack.push("INSIDE_LITERAL");
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
case "-": {
|
|
806
|
+
stack.pop();
|
|
807
|
+
stack.push(swapState);
|
|
808
|
+
stack.push("INSIDE_NUMBER");
|
|
809
|
+
break;
|
|
810
|
+
}
|
|
811
|
+
case "0":
|
|
812
|
+
case "1":
|
|
813
|
+
case "2":
|
|
814
|
+
case "3":
|
|
815
|
+
case "4":
|
|
816
|
+
case "5":
|
|
817
|
+
case "6":
|
|
818
|
+
case "7":
|
|
819
|
+
case "8":
|
|
820
|
+
case "9": {
|
|
821
|
+
lastValidIndex = i;
|
|
822
|
+
stack.pop();
|
|
823
|
+
stack.push(swapState);
|
|
824
|
+
stack.push("INSIDE_NUMBER");
|
|
825
|
+
break;
|
|
826
|
+
}
|
|
827
|
+
case "{": {
|
|
828
|
+
lastValidIndex = i;
|
|
829
|
+
stack.pop();
|
|
830
|
+
stack.push(swapState);
|
|
831
|
+
stack.push("INSIDE_OBJECT_START");
|
|
832
|
+
break;
|
|
833
|
+
}
|
|
834
|
+
case "[": {
|
|
835
|
+
lastValidIndex = i;
|
|
836
|
+
stack.pop();
|
|
837
|
+
stack.push(swapState);
|
|
838
|
+
stack.push("INSIDE_ARRAY_START");
|
|
839
|
+
break;
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
function processAfterObjectValue(char, i) {
|
|
845
|
+
switch (char) {
|
|
846
|
+
case ",": {
|
|
847
|
+
stack.pop();
|
|
848
|
+
stack.push("INSIDE_OBJECT_AFTER_COMMA");
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
case "}": {
|
|
852
|
+
lastValidIndex = i;
|
|
853
|
+
stack.pop();
|
|
854
|
+
break;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
function processAfterArrayValue(char, i) {
|
|
859
|
+
switch (char) {
|
|
860
|
+
case ",": {
|
|
861
|
+
stack.pop();
|
|
862
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
case "]": {
|
|
866
|
+
lastValidIndex = i;
|
|
867
|
+
stack.pop();
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
for (let i = 0; i < input.length; i++) {
|
|
873
|
+
const char = input[i];
|
|
874
|
+
const currentState = stack[stack.length - 1];
|
|
875
|
+
switch (currentState) {
|
|
876
|
+
case "ROOT":
|
|
877
|
+
processValueStart(char, i, "FINISH");
|
|
878
|
+
break;
|
|
879
|
+
case "INSIDE_OBJECT_START": {
|
|
880
|
+
switch (char) {
|
|
881
|
+
case '"': {
|
|
882
|
+
stack.pop();
|
|
883
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
884
|
+
break;
|
|
885
|
+
}
|
|
886
|
+
case "}": {
|
|
887
|
+
lastValidIndex = i;
|
|
888
|
+
stack.pop();
|
|
889
|
+
break;
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
break;
|
|
893
|
+
}
|
|
894
|
+
case "INSIDE_OBJECT_AFTER_COMMA": {
|
|
895
|
+
switch (char) {
|
|
896
|
+
case '"': {
|
|
897
|
+
stack.pop();
|
|
898
|
+
stack.push("INSIDE_OBJECT_KEY");
|
|
899
|
+
break;
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
break;
|
|
903
|
+
}
|
|
904
|
+
case "INSIDE_OBJECT_KEY": {
|
|
905
|
+
switch (char) {
|
|
906
|
+
case '"': {
|
|
907
|
+
stack.pop();
|
|
908
|
+
stack.push("INSIDE_OBJECT_AFTER_KEY");
|
|
909
|
+
break;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
break;
|
|
913
|
+
}
|
|
914
|
+
case "INSIDE_OBJECT_AFTER_KEY": {
|
|
915
|
+
switch (char) {
|
|
916
|
+
case ":": {
|
|
917
|
+
stack.pop();
|
|
918
|
+
stack.push("INSIDE_OBJECT_BEFORE_VALUE");
|
|
919
|
+
break;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
break;
|
|
923
|
+
}
|
|
924
|
+
case "INSIDE_OBJECT_BEFORE_VALUE": {
|
|
925
|
+
processValueStart(char, i, "INSIDE_OBJECT_AFTER_VALUE");
|
|
926
|
+
break;
|
|
927
|
+
}
|
|
928
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
929
|
+
processAfterObjectValue(char, i);
|
|
930
|
+
break;
|
|
931
|
+
}
|
|
932
|
+
case "INSIDE_STRING": {
|
|
933
|
+
switch (char) {
|
|
934
|
+
case '"': {
|
|
935
|
+
stack.pop();
|
|
936
|
+
lastValidIndex = i;
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
case "\\": {
|
|
940
|
+
stack.push("INSIDE_STRING_ESCAPE");
|
|
941
|
+
break;
|
|
942
|
+
}
|
|
943
|
+
default: {
|
|
944
|
+
lastValidIndex = i;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
case "INSIDE_ARRAY_START": {
|
|
950
|
+
switch (char) {
|
|
951
|
+
case "]": {
|
|
952
|
+
lastValidIndex = i;
|
|
953
|
+
stack.pop();
|
|
954
|
+
break;
|
|
955
|
+
}
|
|
956
|
+
default: {
|
|
957
|
+
lastValidIndex = i;
|
|
958
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
959
|
+
break;
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
break;
|
|
963
|
+
}
|
|
964
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
965
|
+
switch (char) {
|
|
966
|
+
case ",": {
|
|
967
|
+
stack.pop();
|
|
968
|
+
stack.push("INSIDE_ARRAY_AFTER_COMMA");
|
|
969
|
+
break;
|
|
970
|
+
}
|
|
971
|
+
case "]": {
|
|
972
|
+
lastValidIndex = i;
|
|
973
|
+
stack.pop();
|
|
974
|
+
break;
|
|
975
|
+
}
|
|
976
|
+
default: {
|
|
977
|
+
lastValidIndex = i;
|
|
978
|
+
break;
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
case "INSIDE_ARRAY_AFTER_COMMA": {
|
|
984
|
+
processValueStart(char, i, "INSIDE_ARRAY_AFTER_VALUE");
|
|
985
|
+
break;
|
|
986
|
+
}
|
|
987
|
+
case "INSIDE_STRING_ESCAPE": {
|
|
988
|
+
stack.pop();
|
|
989
|
+
lastValidIndex = i;
|
|
990
|
+
break;
|
|
991
|
+
}
|
|
992
|
+
case "INSIDE_NUMBER": {
|
|
993
|
+
switch (char) {
|
|
994
|
+
case "0":
|
|
995
|
+
case "1":
|
|
996
|
+
case "2":
|
|
997
|
+
case "3":
|
|
998
|
+
case "4":
|
|
999
|
+
case "5":
|
|
1000
|
+
case "6":
|
|
1001
|
+
case "7":
|
|
1002
|
+
case "8":
|
|
1003
|
+
case "9": {
|
|
1004
|
+
lastValidIndex = i;
|
|
1005
|
+
break;
|
|
1006
|
+
}
|
|
1007
|
+
case "e":
|
|
1008
|
+
case "E":
|
|
1009
|
+
case "-":
|
|
1010
|
+
case ".": {
|
|
1011
|
+
break;
|
|
1012
|
+
}
|
|
1013
|
+
case ",": {
|
|
1014
|
+
stack.pop();
|
|
1015
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1016
|
+
processAfterArrayValue(char, i);
|
|
1017
|
+
}
|
|
1018
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1019
|
+
processAfterObjectValue(char, i);
|
|
1020
|
+
}
|
|
1021
|
+
break;
|
|
1022
|
+
}
|
|
1023
|
+
case "}": {
|
|
1024
|
+
stack.pop();
|
|
1025
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1026
|
+
processAfterObjectValue(char, i);
|
|
1027
|
+
}
|
|
1028
|
+
break;
|
|
1029
|
+
}
|
|
1030
|
+
case "]": {
|
|
1031
|
+
stack.pop();
|
|
1032
|
+
if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1033
|
+
processAfterArrayValue(char, i);
|
|
1034
|
+
}
|
|
1035
|
+
break;
|
|
1036
|
+
}
|
|
1037
|
+
default: {
|
|
1038
|
+
stack.pop();
|
|
1039
|
+
break;
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
case "INSIDE_LITERAL": {
|
|
1045
|
+
const partialLiteral = input.substring(literalStart, i + 1);
|
|
1046
|
+
if (!"false".startsWith(partialLiteral) && !"true".startsWith(partialLiteral) && !"null".startsWith(partialLiteral)) {
|
|
1047
|
+
stack.pop();
|
|
1048
|
+
if (stack[stack.length - 1] === "INSIDE_OBJECT_AFTER_VALUE") {
|
|
1049
|
+
processAfterObjectValue(char, i);
|
|
1050
|
+
} else if (stack[stack.length - 1] === "INSIDE_ARRAY_AFTER_VALUE") {
|
|
1051
|
+
processAfterArrayValue(char, i);
|
|
1052
|
+
}
|
|
1053
|
+
} else {
|
|
1054
|
+
lastValidIndex = i;
|
|
1055
|
+
}
|
|
1056
|
+
break;
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
let result = input.slice(0, lastValidIndex + 1);
|
|
1061
|
+
for (let i = stack.length - 1; i >= 0; i--) {
|
|
1062
|
+
const state = stack[i];
|
|
1063
|
+
switch (state) {
|
|
1064
|
+
case "INSIDE_STRING": {
|
|
1065
|
+
result += '"';
|
|
1066
|
+
break;
|
|
1067
|
+
}
|
|
1068
|
+
case "INSIDE_OBJECT_KEY":
|
|
1069
|
+
case "INSIDE_OBJECT_AFTER_KEY":
|
|
1070
|
+
case "INSIDE_OBJECT_AFTER_COMMA":
|
|
1071
|
+
case "INSIDE_OBJECT_START":
|
|
1072
|
+
case "INSIDE_OBJECT_BEFORE_VALUE":
|
|
1073
|
+
case "INSIDE_OBJECT_AFTER_VALUE": {
|
|
1074
|
+
result += "}";
|
|
1075
|
+
break;
|
|
1076
|
+
}
|
|
1077
|
+
case "INSIDE_ARRAY_START":
|
|
1078
|
+
case "INSIDE_ARRAY_AFTER_COMMA":
|
|
1079
|
+
case "INSIDE_ARRAY_AFTER_VALUE": {
|
|
1080
|
+
result += "]";
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
case "INSIDE_LITERAL": {
|
|
1084
|
+
const partialLiteral = input.substring(literalStart, input.length);
|
|
1085
|
+
if ("true".startsWith(partialLiteral)) {
|
|
1086
|
+
result += "true".slice(partialLiteral.length);
|
|
1087
|
+
} else if ("false".startsWith(partialLiteral)) {
|
|
1088
|
+
result += "false".slice(partialLiteral.length);
|
|
1089
|
+
} else if ("null".startsWith(partialLiteral)) {
|
|
1090
|
+
result += "null".slice(partialLiteral.length);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
return result;
|
|
1096
|
+
}
|
|
1097
|
+
async function parsePartialJson(jsonText) {
|
|
1098
|
+
if (jsonText === void 0) {
|
|
1099
|
+
return { value: void 0, state: "undefined-input" };
|
|
1100
|
+
}
|
|
1101
|
+
let result = await safeParseJSON({ text: jsonText });
|
|
1102
|
+
if (result.success) {
|
|
1103
|
+
return { value: result.value, state: "successful-parse" };
|
|
1104
|
+
}
|
|
1105
|
+
result = await safeParseJSON({ text: fixJson(jsonText) });
|
|
1106
|
+
if (result.success) {
|
|
1107
|
+
return { value: result.value, state: "repaired-parse" };
|
|
1108
|
+
}
|
|
1109
|
+
return { value: void 0, state: "failed-parse" };
|
|
1110
|
+
}
|
|
1111
|
+
var originalGenerateId2 = createIdGenerator({
|
|
1112
|
+
prefix: "aitxt",
|
|
1113
|
+
size: 24
|
|
1114
|
+
});
|
|
1115
|
+
var originalGenerateId3 = createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
1116
|
+
var originalGenerateId4 = createIdGenerator({ prefix: "aiobj", size: 24 });
|
|
1117
|
+
var output_exports = {};
|
|
1118
|
+
__export2(output_exports, {
|
|
1119
|
+
object: () => object,
|
|
1120
|
+
text: () => text
|
|
1121
|
+
});
|
|
1122
|
+
var text = () => ({
|
|
1123
|
+
type: "text",
|
|
1124
|
+
responseFormat: { type: "text" },
|
|
1125
|
+
async parsePartial({ text: text2 }) {
|
|
1126
|
+
return { partial: text2 };
|
|
1127
|
+
},
|
|
1128
|
+
async parseOutput({ text: text2 }) {
|
|
1129
|
+
return text2;
|
|
1130
|
+
}
|
|
1131
|
+
});
|
|
1132
|
+
var object = ({
|
|
1133
|
+
schema: inputSchema
|
|
1134
|
+
}) => {
|
|
1135
|
+
const schema = asSchema(inputSchema);
|
|
1136
|
+
return {
|
|
1137
|
+
type: "object",
|
|
1138
|
+
responseFormat: {
|
|
1139
|
+
type: "json",
|
|
1140
|
+
schema: schema.jsonSchema
|
|
1141
|
+
},
|
|
1142
|
+
async parsePartial({ text: text2 }) {
|
|
1143
|
+
const result = await parsePartialJson(text2);
|
|
1144
|
+
switch (result.state) {
|
|
1145
|
+
case "failed-parse":
|
|
1146
|
+
case "undefined-input":
|
|
1147
|
+
return void 0;
|
|
1148
|
+
case "repaired-parse":
|
|
1149
|
+
case "successful-parse":
|
|
1150
|
+
return {
|
|
1151
|
+
// Note: currently no validation of partial results:
|
|
1152
|
+
partial: result.value
|
|
1153
|
+
};
|
|
1154
|
+
default: {
|
|
1155
|
+
const _exhaustiveCheck = result.state;
|
|
1156
|
+
throw new Error(`Unsupported parse state: ${_exhaustiveCheck}`);
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
},
|
|
1160
|
+
async parseOutput({ text: text2 }, context) {
|
|
1161
|
+
const parseResult = await safeParseJSON({ text: text2 });
|
|
1162
|
+
if (!parseResult.success) {
|
|
1163
|
+
throw new NoObjectGeneratedError({
|
|
1164
|
+
message: "No object generated: could not parse the response.",
|
|
1165
|
+
cause: parseResult.error,
|
|
1166
|
+
text: text2,
|
|
1167
|
+
response: context.response,
|
|
1168
|
+
usage: context.usage,
|
|
1169
|
+
finishReason: context.finishReason
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
const validationResult = await safeValidateTypes({
|
|
1173
|
+
value: parseResult.value,
|
|
1174
|
+
schema
|
|
1175
|
+
});
|
|
1176
|
+
if (!validationResult.success) {
|
|
1177
|
+
throw new NoObjectGeneratedError({
|
|
1178
|
+
message: "No object generated: response did not match schema.",
|
|
1179
|
+
cause: validationResult.error,
|
|
1180
|
+
text: text2,
|
|
1181
|
+
response: context.response,
|
|
1182
|
+
usage: context.usage,
|
|
1183
|
+
finishReason: context.finishReason
|
|
1184
|
+
});
|
|
1185
|
+
}
|
|
1186
|
+
return validationResult.value;
|
|
1187
|
+
}
|
|
1188
|
+
};
|
|
1189
|
+
};
|
|
1190
|
+
var name15 = "AI_NoSuchProviderError";
|
|
1191
|
+
var marker152 = `vercel.ai.error.${name15}`;
|
|
1192
|
+
var symbol152 = Symbol.for(marker152);
|
|
1193
|
+
var _a152;
|
|
1194
|
+
_a152 = symbol152;
|
|
1195
|
+
var ClientOrServerImplementationSchema = z8.object({
|
|
1196
|
+
name: z8.string(),
|
|
1197
|
+
version: z8.string()
|
|
1198
|
+
}).passthrough();
|
|
1199
|
+
var BaseParamsSchema = z8.object({
|
|
1200
|
+
_meta: z8.optional(z8.object({}).passthrough())
|
|
1201
|
+
}).passthrough();
|
|
1202
|
+
var ResultSchema = BaseParamsSchema;
|
|
1203
|
+
var RequestSchema = z8.object({
|
|
1204
|
+
method: z8.string(),
|
|
1205
|
+
params: z8.optional(BaseParamsSchema)
|
|
1206
|
+
});
|
|
1207
|
+
var ServerCapabilitiesSchema = z8.object({
|
|
1208
|
+
experimental: z8.optional(z8.object({}).passthrough()),
|
|
1209
|
+
logging: z8.optional(z8.object({}).passthrough()),
|
|
1210
|
+
prompts: z8.optional(
|
|
1211
|
+
z8.object({
|
|
1212
|
+
listChanged: z8.optional(z8.boolean())
|
|
1213
|
+
}).passthrough()
|
|
1214
|
+
),
|
|
1215
|
+
resources: z8.optional(
|
|
1216
|
+
z8.object({
|
|
1217
|
+
subscribe: z8.optional(z8.boolean()),
|
|
1218
|
+
listChanged: z8.optional(z8.boolean())
|
|
1219
|
+
}).passthrough()
|
|
1220
|
+
),
|
|
1221
|
+
tools: z8.optional(
|
|
1222
|
+
z8.object({
|
|
1223
|
+
listChanged: z8.optional(z8.boolean())
|
|
1224
|
+
}).passthrough()
|
|
1225
|
+
)
|
|
1226
|
+
}).passthrough();
|
|
1227
|
+
var InitializeResultSchema = ResultSchema.extend({
|
|
1228
|
+
protocolVersion: z8.string(),
|
|
1229
|
+
capabilities: ServerCapabilitiesSchema,
|
|
1230
|
+
serverInfo: ClientOrServerImplementationSchema,
|
|
1231
|
+
instructions: z8.optional(z8.string())
|
|
1232
|
+
});
|
|
1233
|
+
var PaginatedResultSchema = ResultSchema.extend({
|
|
1234
|
+
nextCursor: z8.optional(z8.string())
|
|
1235
|
+
});
|
|
1236
|
+
var ToolSchema = z8.object({
|
|
1237
|
+
name: z8.string(),
|
|
1238
|
+
description: z8.optional(z8.string()),
|
|
1239
|
+
inputSchema: z8.object({
|
|
1240
|
+
type: z8.literal("object"),
|
|
1241
|
+
properties: z8.optional(z8.object({}).passthrough())
|
|
1242
|
+
}).passthrough()
|
|
1243
|
+
}).passthrough();
|
|
1244
|
+
var ListToolsResultSchema = PaginatedResultSchema.extend({
|
|
1245
|
+
tools: z8.array(ToolSchema)
|
|
1246
|
+
});
|
|
1247
|
+
var TextContentSchema = z8.object({
|
|
1248
|
+
type: z8.literal("text"),
|
|
1249
|
+
text: z8.string()
|
|
1250
|
+
}).passthrough();
|
|
1251
|
+
var ImageContentSchema = z8.object({
|
|
1252
|
+
type: z8.literal("image"),
|
|
1253
|
+
data: z8.string().base64(),
|
|
1254
|
+
mimeType: z8.string()
|
|
1255
|
+
}).passthrough();
|
|
1256
|
+
var ResourceContentsSchema = z8.object({
|
|
1257
|
+
/**
|
|
1258
|
+
* The URI of this resource.
|
|
1259
|
+
*/
|
|
1260
|
+
uri: z8.string(),
|
|
1261
|
+
/**
|
|
1262
|
+
* The MIME type of this resource, if known.
|
|
1263
|
+
*/
|
|
1264
|
+
mimeType: z8.optional(z8.string())
|
|
1265
|
+
}).passthrough();
|
|
1266
|
+
var TextResourceContentsSchema = ResourceContentsSchema.extend({
|
|
1267
|
+
text: z8.string()
|
|
1268
|
+
});
|
|
1269
|
+
var BlobResourceContentsSchema = ResourceContentsSchema.extend({
|
|
1270
|
+
blob: z8.string().base64()
|
|
1271
|
+
});
|
|
1272
|
+
var EmbeddedResourceSchema = z8.object({
|
|
1273
|
+
type: z8.literal("resource"),
|
|
1274
|
+
resource: z8.union([TextResourceContentsSchema, BlobResourceContentsSchema])
|
|
1275
|
+
}).passthrough();
|
|
1276
|
+
var CallToolResultSchema = ResultSchema.extend({
|
|
1277
|
+
content: z8.array(
|
|
1278
|
+
z8.union([TextContentSchema, ImageContentSchema, EmbeddedResourceSchema])
|
|
1279
|
+
),
|
|
1280
|
+
isError: z8.boolean().default(false).optional()
|
|
1281
|
+
}).or(
|
|
1282
|
+
ResultSchema.extend({
|
|
1283
|
+
toolResult: z8.unknown()
|
|
1284
|
+
})
|
|
1285
|
+
);
|
|
1286
|
+
var JSONRPC_VERSION = "2.0";
|
|
1287
|
+
var JSONRPCRequestSchema = z9.object({
|
|
1288
|
+
jsonrpc: z9.literal(JSONRPC_VERSION),
|
|
1289
|
+
id: z9.union([z9.string(), z9.number().int()])
|
|
1290
|
+
}).merge(RequestSchema).strict();
|
|
1291
|
+
var JSONRPCResponseSchema = z9.object({
|
|
1292
|
+
jsonrpc: z9.literal(JSONRPC_VERSION),
|
|
1293
|
+
id: z9.union([z9.string(), z9.number().int()]),
|
|
1294
|
+
result: ResultSchema
|
|
1295
|
+
}).strict();
|
|
1296
|
+
var JSONRPCErrorSchema = z9.object({
|
|
1297
|
+
jsonrpc: z9.literal(JSONRPC_VERSION),
|
|
1298
|
+
id: z9.union([z9.string(), z9.number().int()]),
|
|
1299
|
+
error: z9.object({
|
|
1300
|
+
code: z9.number().int(),
|
|
1301
|
+
message: z9.string(),
|
|
1302
|
+
data: z9.optional(z9.unknown())
|
|
1303
|
+
})
|
|
1304
|
+
}).strict();
|
|
1305
|
+
var JSONRPCNotificationSchema = z9.object({
|
|
1306
|
+
jsonrpc: z9.literal(JSONRPC_VERSION)
|
|
1307
|
+
}).merge(
|
|
1308
|
+
z9.object({
|
|
1309
|
+
method: z9.string(),
|
|
1310
|
+
params: z9.optional(BaseParamsSchema)
|
|
1311
|
+
})
|
|
1312
|
+
).strict();
|
|
1313
|
+
var JSONRPCMessageSchema = z9.union([
|
|
1314
|
+
JSONRPCRequestSchema,
|
|
1315
|
+
JSONRPCNotificationSchema,
|
|
1316
|
+
JSONRPCResponseSchema,
|
|
1317
|
+
JSONRPCErrorSchema
|
|
1318
|
+
]);
|
|
1319
|
+
|
|
7
1320
|
// packages/rpc/src/lib/rpc.ts
|
|
8
|
-
import { z as
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
1321
|
+
import { z as z12 } from "zod";
|
|
1322
|
+
import { forEachOperation, loadSpec, toIR } from "@sdk-it/spec";
|
|
1323
|
+
import {
|
|
1324
|
+
buildInput,
|
|
1325
|
+
inputToPath,
|
|
1326
|
+
operationSchema,
|
|
1327
|
+
toHttpOutput
|
|
1328
|
+
} from "@sdk-it/typescript";
|
|
11
1329
|
|
|
12
1330
|
// packages/rpc/src/lib/http/dispatcher.ts
|
|
13
|
-
import
|
|
1331
|
+
import z10 from "zod";
|
|
14
1332
|
|
|
15
1333
|
// packages/rpc/src/lib/http/interceptors.ts
|
|
16
1334
|
var createHeadersInterceptor = (defaultHeaders, requestHeaders) => {
|
|
@@ -198,8 +1516,8 @@ async function buffered(response) {
|
|
|
198
1516
|
case "application/xml":
|
|
199
1517
|
return response.text();
|
|
200
1518
|
case "application/x-www-form-urlencoded": {
|
|
201
|
-
const
|
|
202
|
-
return Object.fromEntries(new URLSearchParams(
|
|
1519
|
+
const text2 = await response.text();
|
|
1520
|
+
return Object.fromEntries(new URLSearchParams(text2));
|
|
203
1521
|
}
|
|
204
1522
|
case "multipart/form-data":
|
|
205
1523
|
return response.formData();
|
|
@@ -580,7 +1898,7 @@ var GatewayTimeout = class _GatewayTimeout extends APIError {
|
|
|
580
1898
|
};
|
|
581
1899
|
|
|
582
1900
|
// packages/rpc/src/lib/http/dispatcher.ts
|
|
583
|
-
var fetchType =
|
|
1901
|
+
var fetchType = z10.function().args(z10.instanceof(Request)).returns(z10.promise(z10.instanceof(Response))).optional();
|
|
584
1902
|
async function parse2(outputs, response) {
|
|
585
1903
|
let output = null;
|
|
586
1904
|
let parser = buffered;
|
|
@@ -649,11 +1967,386 @@ var Dispatcher = class {
|
|
|
649
1967
|
}
|
|
650
1968
|
};
|
|
651
1969
|
|
|
1970
|
+
// packages/rpc/src/lib/zod.ts
|
|
1971
|
+
import { z as z11 } from "zod";
|
|
1972
|
+
var RuntimeZodConverter = class {
|
|
1973
|
+
constructor(resolveRef) {
|
|
1974
|
+
this.resolveRef = resolveRef;
|
|
1975
|
+
}
|
|
1976
|
+
#resolvedRefs = /* @__PURE__ */ new Map();
|
|
1977
|
+
#object(schema) {
|
|
1978
|
+
const properties = schema.properties || {};
|
|
1979
|
+
const required = schema.required || [];
|
|
1980
|
+
const shape = {};
|
|
1981
|
+
for (const [key, propSchema] of Object.entries(properties)) {
|
|
1982
|
+
const isRequired = required.includes(key);
|
|
1983
|
+
shape[key] = this.handle(propSchema, isRequired);
|
|
1984
|
+
}
|
|
1985
|
+
let result = z11.object(shape);
|
|
1986
|
+
if (schema.additionalProperties) {
|
|
1987
|
+
if (typeof schema.additionalProperties === "object") {
|
|
1988
|
+
const addPropSchema = this.handle(schema.additionalProperties, true);
|
|
1989
|
+
result = result.catchall(addPropSchema);
|
|
1990
|
+
} else if (schema.additionalProperties === true) {
|
|
1991
|
+
result = result.catchall(z11.unknown());
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
return result;
|
|
1995
|
+
}
|
|
1996
|
+
#array(schema) {
|
|
1997
|
+
const { items } = schema;
|
|
1998
|
+
if (!items) {
|
|
1999
|
+
let result2 = z11.array(z11.unknown());
|
|
2000
|
+
if (schema.default !== void 0) {
|
|
2001
|
+
result2 = z11.array(z11.unknown()).default(schema.default);
|
|
2002
|
+
}
|
|
2003
|
+
return result2;
|
|
2004
|
+
}
|
|
2005
|
+
if (Array.isArray(items)) {
|
|
2006
|
+
const tupleItems = items.map((sub) => this.handle(sub, true));
|
|
2007
|
+
return z11.tuple(tupleItems);
|
|
2008
|
+
}
|
|
2009
|
+
const itemsSchema = this.handle(items, true);
|
|
2010
|
+
let result = z11.array(itemsSchema);
|
|
2011
|
+
if (schema.default !== void 0) {
|
|
2012
|
+
result = z11.array(itemsSchema).default(schema.default);
|
|
2013
|
+
}
|
|
2014
|
+
return result;
|
|
2015
|
+
}
|
|
2016
|
+
#ref($ref) {
|
|
2017
|
+
const cached = this.#resolvedRefs.get($ref);
|
|
2018
|
+
if (cached) {
|
|
2019
|
+
return cached;
|
|
2020
|
+
}
|
|
2021
|
+
if (!this.resolveRef) {
|
|
2022
|
+
throw new Error(
|
|
2023
|
+
`Cannot resolve reference ${$ref} - no resolver provided`
|
|
2024
|
+
);
|
|
2025
|
+
}
|
|
2026
|
+
const resolvedSchema = this.resolveRef($ref);
|
|
2027
|
+
const zodSchema2 = this.handle(resolvedSchema, true);
|
|
2028
|
+
this.#resolvedRefs.set($ref, zodSchema2);
|
|
2029
|
+
return zodSchema2;
|
|
2030
|
+
}
|
|
2031
|
+
#toIntersection(schemas) {
|
|
2032
|
+
const [left, ...right] = schemas;
|
|
2033
|
+
if (!right.length) {
|
|
2034
|
+
return left;
|
|
2035
|
+
}
|
|
2036
|
+
return z11.intersection(left, this.#toIntersection(right));
|
|
2037
|
+
}
|
|
2038
|
+
allOf(schemas) {
|
|
2039
|
+
const allOfSchemas = schemas.map((sub) => this.handle(sub, true));
|
|
2040
|
+
if (allOfSchemas.length === 0) {
|
|
2041
|
+
return z11.unknown();
|
|
2042
|
+
}
|
|
2043
|
+
if (allOfSchemas.length === 1) {
|
|
2044
|
+
return allOfSchemas[0];
|
|
2045
|
+
}
|
|
2046
|
+
return this.#toIntersection(allOfSchemas);
|
|
2047
|
+
}
|
|
2048
|
+
anyOf(schemas) {
|
|
2049
|
+
const anyOfSchemas = schemas.map((sub) => this.handle(sub, true));
|
|
2050
|
+
if (anyOfSchemas.length === 1) {
|
|
2051
|
+
return anyOfSchemas[0];
|
|
2052
|
+
}
|
|
2053
|
+
return z11.union(anyOfSchemas);
|
|
2054
|
+
}
|
|
2055
|
+
oneOf(schemas) {
|
|
2056
|
+
const oneOfSchemas = schemas.map((sub) => this.handle(sub, true));
|
|
2057
|
+
if (oneOfSchemas.length === 1) {
|
|
2058
|
+
return oneOfSchemas[0];
|
|
2059
|
+
}
|
|
2060
|
+
return z11.union(oneOfSchemas);
|
|
2061
|
+
}
|
|
2062
|
+
enum(type, values) {
|
|
2063
|
+
if (values.length === 1) {
|
|
2064
|
+
return z11.literal(values[0]);
|
|
2065
|
+
}
|
|
2066
|
+
if (type === "integer") {
|
|
2067
|
+
if (values.length >= 2) {
|
|
2068
|
+
const [first, second, ...rest] = values.map(
|
|
2069
|
+
(val) => z11.literal(val)
|
|
2070
|
+
);
|
|
2071
|
+
return z11.union([first, second, ...rest]);
|
|
2072
|
+
}
|
|
2073
|
+
return z11.literal(values[0]);
|
|
2074
|
+
}
|
|
2075
|
+
const stringValues = values;
|
|
2076
|
+
if (stringValues.length >= 2) {
|
|
2077
|
+
const [first, ...rest] = stringValues;
|
|
2078
|
+
return z11.enum([first, ...rest]);
|
|
2079
|
+
}
|
|
2080
|
+
return z11.literal(stringValues[0]);
|
|
2081
|
+
}
|
|
2082
|
+
/**
|
|
2083
|
+
* Handle a `string` schema with possible format keywords (JSON Schema).
|
|
2084
|
+
*/
|
|
2085
|
+
string(schema) {
|
|
2086
|
+
if (schema.contentEncoding === "binary") {
|
|
2087
|
+
return z11.instanceof(Blob);
|
|
2088
|
+
}
|
|
2089
|
+
let base = z11.string();
|
|
2090
|
+
switch (schema.format) {
|
|
2091
|
+
case "date-time":
|
|
2092
|
+
case "datetime":
|
|
2093
|
+
base = z11.string().datetime();
|
|
2094
|
+
break;
|
|
2095
|
+
case "date":
|
|
2096
|
+
base = z11.coerce.date();
|
|
2097
|
+
break;
|
|
2098
|
+
case "time":
|
|
2099
|
+
base = z11.string();
|
|
2100
|
+
break;
|
|
2101
|
+
case "email":
|
|
2102
|
+
base = z11.string().email();
|
|
2103
|
+
break;
|
|
2104
|
+
case "uuid":
|
|
2105
|
+
base = z11.string().uuid();
|
|
2106
|
+
break;
|
|
2107
|
+
case "url":
|
|
2108
|
+
case "uri":
|
|
2109
|
+
base = z11.string().url();
|
|
2110
|
+
break;
|
|
2111
|
+
case "ipv4":
|
|
2112
|
+
base = z11.string().ip({ version: "v4" });
|
|
2113
|
+
break;
|
|
2114
|
+
case "ipv6":
|
|
2115
|
+
base = z11.string().ip({ version: "v6" });
|
|
2116
|
+
break;
|
|
2117
|
+
case "byte":
|
|
2118
|
+
case "binary":
|
|
2119
|
+
base = z11.instanceof(Blob);
|
|
2120
|
+
break;
|
|
2121
|
+
case "int64":
|
|
2122
|
+
base = z11.string();
|
|
2123
|
+
break;
|
|
2124
|
+
default:
|
|
2125
|
+
break;
|
|
2126
|
+
}
|
|
2127
|
+
return base;
|
|
2128
|
+
}
|
|
2129
|
+
/**
|
|
2130
|
+
* Handle number/integer constraints from OpenAPI/JSON Schema.
|
|
2131
|
+
*/
|
|
2132
|
+
#number(schema) {
|
|
2133
|
+
let base = z11.number();
|
|
2134
|
+
if (schema.format === "int64") {
|
|
2135
|
+
base = z11.bigint();
|
|
2136
|
+
}
|
|
2137
|
+
if (schema.format === "int32") {
|
|
2138
|
+
base = z11.number().int();
|
|
2139
|
+
}
|
|
2140
|
+
if (typeof schema.exclusiveMinimum === "number") {
|
|
2141
|
+
if (schema.format === "int64") {
|
|
2142
|
+
base = base.gt(BigInt(schema.exclusiveMinimum));
|
|
2143
|
+
} else {
|
|
2144
|
+
base = base.gt(schema.exclusiveMinimum);
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
if (typeof schema.exclusiveMaximum === "number") {
|
|
2148
|
+
if (schema.format === "int64") {
|
|
2149
|
+
base = base.lt(BigInt(schema.exclusiveMaximum));
|
|
2150
|
+
} else {
|
|
2151
|
+
base = base.lt(schema.exclusiveMaximum);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
if (typeof schema.minimum === "number") {
|
|
2155
|
+
if (schema.format === "int64") {
|
|
2156
|
+
base = base.min(BigInt(schema.minimum));
|
|
2157
|
+
} else {
|
|
2158
|
+
base = base.min(schema.minimum);
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
if (typeof schema.maximum === "number") {
|
|
2162
|
+
if (schema.format === "int64") {
|
|
2163
|
+
base = base.max(BigInt(schema.maximum));
|
|
2164
|
+
} else {
|
|
2165
|
+
base = base.max(schema.maximum);
|
|
2166
|
+
}
|
|
2167
|
+
}
|
|
2168
|
+
if (typeof schema.multipleOf === "number") {
|
|
2169
|
+
const multipleOf = schema.multipleOf;
|
|
2170
|
+
base = base.refine(
|
|
2171
|
+
(val) => Number.isInteger(Number(val) / multipleOf),
|
|
2172
|
+
`Must be a multiple of ${multipleOf}`
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
return base;
|
|
2176
|
+
}
|
|
2177
|
+
/**
|
|
2178
|
+
* Convert a basic type to Zod schema with proper chaining
|
|
2179
|
+
*/
|
|
2180
|
+
normal(type, schema, required = false, nullable = false) {
|
|
2181
|
+
let base;
|
|
2182
|
+
switch (type) {
|
|
2183
|
+
case "string":
|
|
2184
|
+
base = this.string(schema);
|
|
2185
|
+
break;
|
|
2186
|
+
case "number":
|
|
2187
|
+
case "integer":
|
|
2188
|
+
base = this.#number(schema);
|
|
2189
|
+
break;
|
|
2190
|
+
case "boolean":
|
|
2191
|
+
base = z11.boolean();
|
|
2192
|
+
break;
|
|
2193
|
+
case "object":
|
|
2194
|
+
base = this.#object(schema);
|
|
2195
|
+
break;
|
|
2196
|
+
case "array":
|
|
2197
|
+
base = this.#array(schema);
|
|
2198
|
+
break;
|
|
2199
|
+
case "null":
|
|
2200
|
+
base = z11.null();
|
|
2201
|
+
break;
|
|
2202
|
+
default:
|
|
2203
|
+
base = z11.unknown();
|
|
2204
|
+
break;
|
|
2205
|
+
}
|
|
2206
|
+
if (nullable) {
|
|
2207
|
+
base = base.nullable();
|
|
2208
|
+
}
|
|
2209
|
+
if (schema.default !== void 0) {
|
|
2210
|
+
if (schema.format === "int64" && typeof schema.default === "number") {
|
|
2211
|
+
base = z11.bigint().default(BigInt(schema.default));
|
|
2212
|
+
} else {
|
|
2213
|
+
const defaultValue = schema.default;
|
|
2214
|
+
switch (type) {
|
|
2215
|
+
case "string":
|
|
2216
|
+
base = this.string(schema).default(defaultValue);
|
|
2217
|
+
break;
|
|
2218
|
+
case "number":
|
|
2219
|
+
case "integer":
|
|
2220
|
+
base = this.#number(schema);
|
|
2221
|
+
break;
|
|
2222
|
+
case "boolean":
|
|
2223
|
+
base = z11.boolean().default(defaultValue);
|
|
2224
|
+
break;
|
|
2225
|
+
case "object":
|
|
2226
|
+
base = this.#object(schema).default(defaultValue);
|
|
2227
|
+
break;
|
|
2228
|
+
case "array":
|
|
2229
|
+
break;
|
|
2230
|
+
case "null":
|
|
2231
|
+
base = z11.null().default(defaultValue);
|
|
2232
|
+
break;
|
|
2233
|
+
default:
|
|
2234
|
+
base = z11.unknown().default(defaultValue);
|
|
2235
|
+
break;
|
|
2236
|
+
}
|
|
2237
|
+
}
|
|
2238
|
+
}
|
|
2239
|
+
if (!required) {
|
|
2240
|
+
base = base.optional();
|
|
2241
|
+
}
|
|
2242
|
+
if (schema["x-prefix"]) {
|
|
2243
|
+
const prefix = schema["x-prefix"];
|
|
2244
|
+
if (!required) {
|
|
2245
|
+
base = base.transform((val) => val ? `${prefix}${val}` : void 0);
|
|
2246
|
+
} else {
|
|
2247
|
+
base = base.transform((val) => `${prefix}${val}`);
|
|
2248
|
+
}
|
|
2249
|
+
}
|
|
2250
|
+
return base;
|
|
2251
|
+
}
|
|
2252
|
+
handle(schema, required = false) {
|
|
2253
|
+
if ("$ref" in schema) {
|
|
2254
|
+
let result = this.#ref(schema.$ref);
|
|
2255
|
+
if (!required) {
|
|
2256
|
+
result = result.optional();
|
|
2257
|
+
}
|
|
2258
|
+
return result;
|
|
2259
|
+
}
|
|
2260
|
+
if (schema.allOf && Array.isArray(schema.allOf)) {
|
|
2261
|
+
let result = this.allOf(schema.allOf);
|
|
2262
|
+
if (!required) {
|
|
2263
|
+
result = result.optional();
|
|
2264
|
+
}
|
|
2265
|
+
return result;
|
|
2266
|
+
}
|
|
2267
|
+
if (schema.anyOf && Array.isArray(schema.anyOf)) {
|
|
2268
|
+
let result = this.anyOf(schema.anyOf);
|
|
2269
|
+
if (!required) {
|
|
2270
|
+
result = result.optional();
|
|
2271
|
+
}
|
|
2272
|
+
return result;
|
|
2273
|
+
}
|
|
2274
|
+
if (schema.oneOf && Array.isArray(schema.oneOf) && schema.oneOf.length) {
|
|
2275
|
+
let result = this.oneOf(schema.oneOf);
|
|
2276
|
+
if (!required) {
|
|
2277
|
+
result = result.optional();
|
|
2278
|
+
}
|
|
2279
|
+
return result;
|
|
2280
|
+
}
|
|
2281
|
+
if (schema.enum && Array.isArray(schema.enum)) {
|
|
2282
|
+
let result = this.enum(schema.type, schema.enum);
|
|
2283
|
+
if (schema.default !== void 0 && schema.enum.includes(schema.default)) {
|
|
2284
|
+
const defaultValue = schema.default;
|
|
2285
|
+
const enumValues = schema.enum;
|
|
2286
|
+
const enumType = schema.type;
|
|
2287
|
+
if (enumValues.length === 1) {
|
|
2288
|
+
result = z11.literal(enumValues[0]).default(defaultValue);
|
|
2289
|
+
} else if (enumType === "integer") {
|
|
2290
|
+
if (enumValues.length >= 2) {
|
|
2291
|
+
const [first, second, ...rest] = enumValues.map(
|
|
2292
|
+
(val) => z11.literal(val)
|
|
2293
|
+
);
|
|
2294
|
+
result = z11.union([first, second, ...rest]).default(defaultValue);
|
|
2295
|
+
} else {
|
|
2296
|
+
result = z11.literal(enumValues[0]).default(defaultValue);
|
|
2297
|
+
}
|
|
2298
|
+
} else {
|
|
2299
|
+
const stringValues = enumValues;
|
|
2300
|
+
if (stringValues.length >= 2) {
|
|
2301
|
+
const [first, ...rest] = stringValues;
|
|
2302
|
+
result = z11.enum([first, ...rest]).default(defaultValue);
|
|
2303
|
+
} else {
|
|
2304
|
+
result = z11.literal(stringValues[0]).default(defaultValue);
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
}
|
|
2308
|
+
if (!required) {
|
|
2309
|
+
result = result.optional();
|
|
2310
|
+
}
|
|
2311
|
+
return result;
|
|
2312
|
+
}
|
|
2313
|
+
const types = Array.isArray(schema.type) ? schema.type : schema.type ? [schema.type] : [];
|
|
2314
|
+
if ("nullable" in schema && schema.nullable) {
|
|
2315
|
+
types.push("null");
|
|
2316
|
+
} else if (schema.default === null) {
|
|
2317
|
+
types.push("null");
|
|
2318
|
+
}
|
|
2319
|
+
if (!types.length) {
|
|
2320
|
+
return required ? z11.unknown() : z11.unknown().optional();
|
|
2321
|
+
}
|
|
2322
|
+
if (types.length > 1) {
|
|
2323
|
+
const realTypes = types.filter((t) => t !== "null");
|
|
2324
|
+
if (realTypes.length === 1 && types.includes("null")) {
|
|
2325
|
+
return this.normal(realTypes[0], schema, required, true);
|
|
2326
|
+
}
|
|
2327
|
+
const subSchemas = types.map((t) => this.normal(t, schema, true));
|
|
2328
|
+
if (subSchemas.length >= 2) {
|
|
2329
|
+
const [first, second, ...rest] = subSchemas;
|
|
2330
|
+
let result = z11.union([first, second, ...rest]);
|
|
2331
|
+
if (!required) {
|
|
2332
|
+
result = result.optional();
|
|
2333
|
+
}
|
|
2334
|
+
return result;
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
return this.normal(types[0], schema, required, false);
|
|
2338
|
+
}
|
|
2339
|
+
};
|
|
2340
|
+
function schemaToZod(schema, options) {
|
|
2341
|
+
const converter = new RuntimeZodConverter(options?.resolveRef);
|
|
2342
|
+
return converter.handle(schema, options?.required ?? false);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
652
2345
|
// packages/rpc/src/lib/rpc.ts
|
|
653
|
-
var optionsSchema =
|
|
654
|
-
token:
|
|
2346
|
+
var optionsSchema = z12.object({
|
|
2347
|
+
token: z12.string().optional().transform((val) => val ? `Bearer ${val}` : void 0),
|
|
655
2348
|
fetch: fetchType,
|
|
656
|
-
baseUrl:
|
|
2349
|
+
baseUrl: z12.string()
|
|
657
2350
|
});
|
|
658
2351
|
var Client = class {
|
|
659
2352
|
options;
|
|
@@ -697,7 +2390,7 @@ var Client = class {
|
|
|
697
2390
|
};
|
|
698
2391
|
async function rpc(openapi, options) {
|
|
699
2392
|
const spec = await loadSpec(openapi);
|
|
700
|
-
const ir =
|
|
2393
|
+
const ir = toIR({ spec, responses: { flattenErrorResponses: true } });
|
|
701
2394
|
const schemas = {};
|
|
702
2395
|
forEachOperation(ir, (entry, operation) => {
|
|
703
2396
|
const endpoint = `${entry.method.toUpperCase()} ${entry.path}`;
|
|
@@ -708,7 +2401,6 @@ async function rpc(openapi, options) {
|
|
|
708
2401
|
formdata,
|
|
709
2402
|
empty
|
|
710
2403
|
};
|
|
711
|
-
console.log(operation.responses);
|
|
712
2404
|
const outputs = Object.keys(operation.responses).flatMap(
|
|
713
2405
|
(status) => toHttpOutput(
|
|
714
2406
|
spec,
|
|
@@ -720,24 +2412,18 @@ async function rpc(openapi, options) {
|
|
|
720
2412
|
);
|
|
721
2413
|
schemas[endpoint] = {
|
|
722
2414
|
output: outputs.map((it) => response_exports[it.replace("http.", "")]),
|
|
723
|
-
|
|
2415
|
+
schemas: schemaToZod(operationSchema(ir, operation, details.ct)),
|
|
2416
|
+
async dispatch(input, options2) {
|
|
2417
|
+
const dispatcher = new Dispatcher(options2.interceptors, options2.fetch);
|
|
724
2418
|
const serializer = contentTypeMap[details.outgoingContentType] || defaultSerializer(details.outgoingContentType);
|
|
725
|
-
|
|
2419
|
+
const request = toRequest(
|
|
726
2420
|
endpoint,
|
|
727
2421
|
serializer(input, inputToPath(operation, details.inputs))
|
|
728
2422
|
);
|
|
729
|
-
|
|
730
|
-
async dispatch(input, options2) {
|
|
731
|
-
const dispatcher = new Dispatcher(options2.interceptors, options2.fetch);
|
|
732
|
-
const result = await dispatcher.send(
|
|
733
|
-
this.toRequest(input),
|
|
734
|
-
this.output
|
|
735
|
-
);
|
|
736
|
-
return result;
|
|
2423
|
+
return dispatcher.send(request, this.output);
|
|
737
2424
|
}
|
|
738
2425
|
};
|
|
739
2426
|
});
|
|
740
|
-
console.log(schemas);
|
|
741
2427
|
return new Client(
|
|
742
2428
|
{
|
|
743
2429
|
...options,
|
|
@@ -746,11 +2432,31 @@ async function rpc(openapi, options) {
|
|
|
746
2432
|
schemas
|
|
747
2433
|
);
|
|
748
2434
|
}
|
|
2435
|
+
async function toTools(rpc2) {
|
|
2436
|
+
const tools = {};
|
|
2437
|
+
for (const [endpoint, route] of Object.entries(rpc2.schemas)) {
|
|
2438
|
+
const operation = route.schemas;
|
|
2439
|
+
tools[route.schemas.operationId] = tool({
|
|
2440
|
+
description: operation.description,
|
|
2441
|
+
type: "function",
|
|
2442
|
+
inputSchema: route.schemas,
|
|
2443
|
+
execute: async (input) => {
|
|
2444
|
+
console.log("Executing tool with input:", input);
|
|
2445
|
+
const response = await rpc2.request(endpoint, input);
|
|
2446
|
+
return JSON.stringify(response);
|
|
2447
|
+
}
|
|
2448
|
+
});
|
|
2449
|
+
}
|
|
2450
|
+
return tools;
|
|
2451
|
+
}
|
|
749
2452
|
function defaultSerializer(ct) {
|
|
750
2453
|
throw new Error(`Unsupported content type: ${ct}`);
|
|
751
2454
|
}
|
|
752
2455
|
export {
|
|
753
2456
|
Client,
|
|
754
|
-
|
|
2457
|
+
RuntimeZodConverter,
|
|
2458
|
+
rpc,
|
|
2459
|
+
schemaToZod,
|
|
2460
|
+
toTools
|
|
755
2461
|
};
|
|
756
2462
|
//# sourceMappingURL=index.js.map
|