@apertis/ai-sdk-provider 2.1.0 → 3.0.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/README.md +49 -27
- package/dist/index.cjs +2137 -327
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2137 -327
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// node_modules/.pnpm/@ai-sdk+provider@
|
|
1
|
+
// node_modules/.pnpm/@ai-sdk+provider@3.0.8/node_modules/@ai-sdk/provider/dist/index.mjs
|
|
2
2
|
var marker = "vercel.ai.error";
|
|
3
3
|
var symbol = Symbol.for(marker);
|
|
4
4
|
var _a;
|
|
@@ -13,13 +13,13 @@ var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
|
13
13
|
* @param {unknown} [params.cause] - The underlying cause of the error.
|
|
14
14
|
*/
|
|
15
15
|
constructor({
|
|
16
|
-
name:
|
|
16
|
+
name: name142,
|
|
17
17
|
message,
|
|
18
18
|
cause
|
|
19
19
|
}) {
|
|
20
20
|
super(message);
|
|
21
21
|
this[_a] = true;
|
|
22
|
-
this.name =
|
|
22
|
+
this.name = name142;
|
|
23
23
|
this.cause = cause;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
@@ -30,8 +30,8 @@ var AISDKError = class _AISDKError extends (_b = Error, _a = symbol, _b) {
|
|
|
30
30
|
static isInstance(error) {
|
|
31
31
|
return _AISDKError.hasMarker(error, marker);
|
|
32
32
|
}
|
|
33
|
-
static hasMarker(error,
|
|
34
|
-
const markerSymbol = Symbol.for(
|
|
33
|
+
static hasMarker(error, marker152) {
|
|
34
|
+
const markerSymbol = Symbol.for(marker152);
|
|
35
35
|
return error != null && typeof error === "object" && markerSymbol in error && typeof error[markerSymbol] === "boolean" && error[markerSymbol] === true;
|
|
36
36
|
}
|
|
37
37
|
};
|
|
@@ -268,34 +268,61 @@ var symbol13 = Symbol.for(marker13);
|
|
|
268
268
|
var _a13;
|
|
269
269
|
var _b13;
|
|
270
270
|
var TypeValidationError = class _TypeValidationError extends (_b13 = AISDKError, _a13 = symbol13, _b13) {
|
|
271
|
-
constructor({
|
|
271
|
+
constructor({
|
|
272
|
+
value,
|
|
273
|
+
cause,
|
|
274
|
+
context
|
|
275
|
+
}) {
|
|
276
|
+
let contextPrefix = "Type validation failed";
|
|
277
|
+
if (context == null ? void 0 : context.field) {
|
|
278
|
+
contextPrefix += ` for ${context.field}`;
|
|
279
|
+
}
|
|
280
|
+
if ((context == null ? void 0 : context.entityName) || (context == null ? void 0 : context.entityId)) {
|
|
281
|
+
contextPrefix += " (";
|
|
282
|
+
const parts = [];
|
|
283
|
+
if (context.entityName) {
|
|
284
|
+
parts.push(context.entityName);
|
|
285
|
+
}
|
|
286
|
+
if (context.entityId) {
|
|
287
|
+
parts.push(`id: "${context.entityId}"`);
|
|
288
|
+
}
|
|
289
|
+
contextPrefix += parts.join(", ");
|
|
290
|
+
contextPrefix += ")";
|
|
291
|
+
}
|
|
272
292
|
super({
|
|
273
293
|
name: name12,
|
|
274
|
-
message:
|
|
294
|
+
message: `${contextPrefix}: Value: ${JSON.stringify(value)}.
|
|
275
295
|
Error message: ${getErrorMessage(cause)}`,
|
|
276
296
|
cause
|
|
277
297
|
});
|
|
278
298
|
this[_a13] = true;
|
|
279
299
|
this.value = value;
|
|
300
|
+
this.context = context;
|
|
280
301
|
}
|
|
281
302
|
static isInstance(error) {
|
|
282
303
|
return AISDKError.hasMarker(error, marker13);
|
|
283
304
|
}
|
|
284
305
|
/**
|
|
285
306
|
* Wraps an error into a TypeValidationError.
|
|
286
|
-
* If the cause is already a TypeValidationError with the same value, it returns the cause.
|
|
307
|
+
* If the cause is already a TypeValidationError with the same value and context, it returns the cause.
|
|
287
308
|
* Otherwise, it creates a new TypeValidationError.
|
|
288
309
|
*
|
|
289
310
|
* @param {Object} params - The parameters for wrapping the error.
|
|
290
311
|
* @param {unknown} params.value - The value that failed validation.
|
|
291
312
|
* @param {unknown} params.cause - The original error or cause of the validation failure.
|
|
313
|
+
* @param {TypeValidationContext} params.context - Optional context about what is being validated.
|
|
292
314
|
* @returns {TypeValidationError} A TypeValidationError instance.
|
|
293
315
|
*/
|
|
294
316
|
static wrap({
|
|
295
317
|
value,
|
|
296
|
-
cause
|
|
318
|
+
cause,
|
|
319
|
+
context
|
|
297
320
|
}) {
|
|
298
|
-
|
|
321
|
+
var _a152, _b152, _c;
|
|
322
|
+
if (_TypeValidationError.isInstance(cause) && cause.value === value && ((_a152 = cause.context) == null ? void 0 : _a152.field) === (context == null ? void 0 : context.field) && ((_b152 = cause.context) == null ? void 0 : _b152.entityName) === (context == null ? void 0 : context.entityName) && ((_c = cause.context) == null ? void 0 : _c.entityId) === (context == null ? void 0 : context.entityId)) {
|
|
323
|
+
return cause;
|
|
324
|
+
}
|
|
325
|
+
return new _TypeValidationError({ value, cause, context });
|
|
299
326
|
}
|
|
300
327
|
};
|
|
301
328
|
var name13 = "AI_UnsupportedFunctionalityError";
|
|
@@ -317,6 +344,14 @@ var UnsupportedFunctionalityError = class extends (_b14 = AISDKError, _a14 = sym
|
|
|
317
344
|
}
|
|
318
345
|
};
|
|
319
346
|
|
|
347
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.23_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
348
|
+
import * as z4 from "zod/v4";
|
|
349
|
+
import { ZodFirstPartyTypeKind as ZodFirstPartyTypeKind3 } from "zod/v3";
|
|
350
|
+
import { ZodFirstPartyTypeKind } from "zod/v3";
|
|
351
|
+
import {
|
|
352
|
+
ZodFirstPartyTypeKind as ZodFirstPartyTypeKind2
|
|
353
|
+
} from "zod/v3";
|
|
354
|
+
|
|
320
355
|
// node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/index.js
|
|
321
356
|
var ParseError = class extends Error {
|
|
322
357
|
constructor(message, options) {
|
|
@@ -444,13 +479,7 @@ var EventSourceParserStream = class extends TransformStream {
|
|
|
444
479
|
}
|
|
445
480
|
};
|
|
446
481
|
|
|
447
|
-
// node_modules/.pnpm/@ai-sdk+provider-utils@
|
|
448
|
-
import * as z4 from "zod/v4";
|
|
449
|
-
import { ZodFirstPartyTypeKind as ZodFirstPartyTypeKind3 } from "zod/v3";
|
|
450
|
-
import { ZodFirstPartyTypeKind } from "zod/v3";
|
|
451
|
-
import {
|
|
452
|
-
ZodFirstPartyTypeKind as ZodFirstPartyTypeKind2
|
|
453
|
-
} from "zod/v3";
|
|
482
|
+
// node_modules/.pnpm/@ai-sdk+provider-utils@4.0.23_zod@3.25.76/node_modules/@ai-sdk/provider-utils/dist/index.mjs
|
|
454
483
|
function combineHeaders(...headers) {
|
|
455
484
|
return headers.reduce(
|
|
456
485
|
(combinedHeaders, currentHeaders) => ({
|
|
@@ -463,6 +492,248 @@ function combineHeaders(...headers) {
|
|
|
463
492
|
function extractResponseHeaders(response) {
|
|
464
493
|
return Object.fromEntries([...response.headers]);
|
|
465
494
|
}
|
|
495
|
+
var { btoa, atob } = globalThis;
|
|
496
|
+
function convertBase64ToUint8Array(base64String) {
|
|
497
|
+
const base64Url = base64String.replace(/-/g, "+").replace(/_/g, "/");
|
|
498
|
+
const latin1string = atob(base64Url);
|
|
499
|
+
return Uint8Array.from(latin1string, (byte) => byte.codePointAt(0));
|
|
500
|
+
}
|
|
501
|
+
function convertUint8ArrayToBase64(array) {
|
|
502
|
+
let latin1string = "";
|
|
503
|
+
for (let i = 0; i < array.length; i++) {
|
|
504
|
+
latin1string += String.fromCodePoint(array[i]);
|
|
505
|
+
}
|
|
506
|
+
return btoa(latin1string);
|
|
507
|
+
}
|
|
508
|
+
function convertToBase64(value) {
|
|
509
|
+
return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
|
|
510
|
+
}
|
|
511
|
+
function convertToFormData(input, options = {}) {
|
|
512
|
+
const { useArrayBrackets = true } = options;
|
|
513
|
+
const formData = new FormData();
|
|
514
|
+
for (const [key, value] of Object.entries(input)) {
|
|
515
|
+
if (value == null) {
|
|
516
|
+
continue;
|
|
517
|
+
}
|
|
518
|
+
if (Array.isArray(value)) {
|
|
519
|
+
if (value.length === 1) {
|
|
520
|
+
formData.append(key, value[0]);
|
|
521
|
+
continue;
|
|
522
|
+
}
|
|
523
|
+
const arrayKey = useArrayBrackets ? `${key}[]` : key;
|
|
524
|
+
for (const item of value) {
|
|
525
|
+
formData.append(arrayKey, item);
|
|
526
|
+
}
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
formData.append(key, value);
|
|
530
|
+
}
|
|
531
|
+
return formData;
|
|
532
|
+
}
|
|
533
|
+
var name14 = "AI_DownloadError";
|
|
534
|
+
var marker15 = `vercel.ai.error.${name14}`;
|
|
535
|
+
var symbol15 = Symbol.for(marker15);
|
|
536
|
+
var _a15;
|
|
537
|
+
var _b15;
|
|
538
|
+
var DownloadError = class extends (_b15 = AISDKError, _a15 = symbol15, _b15) {
|
|
539
|
+
constructor({
|
|
540
|
+
url,
|
|
541
|
+
statusCode,
|
|
542
|
+
statusText,
|
|
543
|
+
cause,
|
|
544
|
+
message = cause == null ? `Failed to download ${url}: ${statusCode} ${statusText}` : `Failed to download ${url}: ${cause}`
|
|
545
|
+
}) {
|
|
546
|
+
super({ name: name14, message, cause });
|
|
547
|
+
this[_a15] = true;
|
|
548
|
+
this.url = url;
|
|
549
|
+
this.statusCode = statusCode;
|
|
550
|
+
this.statusText = statusText;
|
|
551
|
+
}
|
|
552
|
+
static isInstance(error) {
|
|
553
|
+
return AISDKError.hasMarker(error, marker15);
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
var DEFAULT_MAX_DOWNLOAD_SIZE = 2 * 1024 * 1024 * 1024;
|
|
557
|
+
async function readResponseWithSizeLimit({
|
|
558
|
+
response,
|
|
559
|
+
url,
|
|
560
|
+
maxBytes = DEFAULT_MAX_DOWNLOAD_SIZE
|
|
561
|
+
}) {
|
|
562
|
+
const contentLength = response.headers.get("content-length");
|
|
563
|
+
if (contentLength != null) {
|
|
564
|
+
const length = parseInt(contentLength, 10);
|
|
565
|
+
if (!isNaN(length) && length > maxBytes) {
|
|
566
|
+
throw new DownloadError({
|
|
567
|
+
url,
|
|
568
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes (Content-Length: ${length}).`
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const body = response.body;
|
|
573
|
+
if (body == null) {
|
|
574
|
+
return new Uint8Array(0);
|
|
575
|
+
}
|
|
576
|
+
const reader = body.getReader();
|
|
577
|
+
const chunks = [];
|
|
578
|
+
let totalBytes = 0;
|
|
579
|
+
try {
|
|
580
|
+
while (true) {
|
|
581
|
+
const { done, value } = await reader.read();
|
|
582
|
+
if (done) {
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
totalBytes += value.length;
|
|
586
|
+
if (totalBytes > maxBytes) {
|
|
587
|
+
throw new DownloadError({
|
|
588
|
+
url,
|
|
589
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes.`
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
chunks.push(value);
|
|
593
|
+
}
|
|
594
|
+
} finally {
|
|
595
|
+
try {
|
|
596
|
+
await reader.cancel();
|
|
597
|
+
} finally {
|
|
598
|
+
reader.releaseLock();
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
const result = new Uint8Array(totalBytes);
|
|
602
|
+
let offset = 0;
|
|
603
|
+
for (const chunk of chunks) {
|
|
604
|
+
result.set(chunk, offset);
|
|
605
|
+
offset += chunk.length;
|
|
606
|
+
}
|
|
607
|
+
return result;
|
|
608
|
+
}
|
|
609
|
+
function validateDownloadUrl(url) {
|
|
610
|
+
let parsed;
|
|
611
|
+
try {
|
|
612
|
+
parsed = new URL(url);
|
|
613
|
+
} catch (e) {
|
|
614
|
+
throw new DownloadError({
|
|
615
|
+
url,
|
|
616
|
+
message: `Invalid URL: ${url}`
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
if (parsed.protocol === "data:") {
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
623
|
+
throw new DownloadError({
|
|
624
|
+
url,
|
|
625
|
+
message: `URL scheme must be http, https, or data, got ${parsed.protocol}`
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
const hostname = parsed.hostname;
|
|
629
|
+
if (!hostname) {
|
|
630
|
+
throw new DownloadError({
|
|
631
|
+
url,
|
|
632
|
+
message: `URL must have a hostname`
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
if (hostname === "localhost" || hostname.endsWith(".local") || hostname.endsWith(".localhost")) {
|
|
636
|
+
throw new DownloadError({
|
|
637
|
+
url,
|
|
638
|
+
message: `URL with hostname ${hostname} is not allowed`
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
if (hostname.startsWith("[") && hostname.endsWith("]")) {
|
|
642
|
+
const ipv6 = hostname.slice(1, -1);
|
|
643
|
+
if (isPrivateIPv6(ipv6)) {
|
|
644
|
+
throw new DownloadError({
|
|
645
|
+
url,
|
|
646
|
+
message: `URL with IPv6 address ${hostname} is not allowed`
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
if (isIPv4(hostname)) {
|
|
652
|
+
if (isPrivateIPv4(hostname)) {
|
|
653
|
+
throw new DownloadError({
|
|
654
|
+
url,
|
|
655
|
+
message: `URL with IP address ${hostname} is not allowed`
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
function isIPv4(hostname) {
|
|
662
|
+
const parts = hostname.split(".");
|
|
663
|
+
if (parts.length !== 4) return false;
|
|
664
|
+
return parts.every((part) => {
|
|
665
|
+
const num = Number(part);
|
|
666
|
+
return Number.isInteger(num) && num >= 0 && num <= 255 && String(num) === part;
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
function isPrivateIPv4(ip) {
|
|
670
|
+
const parts = ip.split(".").map(Number);
|
|
671
|
+
const [a, b] = parts;
|
|
672
|
+
if (a === 0) return true;
|
|
673
|
+
if (a === 10) return true;
|
|
674
|
+
if (a === 127) return true;
|
|
675
|
+
if (a === 169 && b === 254) return true;
|
|
676
|
+
if (a === 172 && b >= 16 && b <= 31) return true;
|
|
677
|
+
if (a === 192 && b === 168) return true;
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
680
|
+
function isPrivateIPv6(ip) {
|
|
681
|
+
const normalized = ip.toLowerCase();
|
|
682
|
+
if (normalized === "::1") return true;
|
|
683
|
+
if (normalized === "::") return true;
|
|
684
|
+
if (normalized.startsWith("::ffff:")) {
|
|
685
|
+
const mappedPart = normalized.slice(7);
|
|
686
|
+
if (isIPv4(mappedPart)) {
|
|
687
|
+
return isPrivateIPv4(mappedPart);
|
|
688
|
+
}
|
|
689
|
+
const hexParts = mappedPart.split(":");
|
|
690
|
+
if (hexParts.length === 2) {
|
|
691
|
+
const high = parseInt(hexParts[0], 16);
|
|
692
|
+
const low = parseInt(hexParts[1], 16);
|
|
693
|
+
if (!isNaN(high) && !isNaN(low)) {
|
|
694
|
+
const a = high >> 8 & 255;
|
|
695
|
+
const b = high & 255;
|
|
696
|
+
const c = low >> 8 & 255;
|
|
697
|
+
const d = low & 255;
|
|
698
|
+
return isPrivateIPv4(`${a}.${b}.${c}.${d}`);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
if (normalized.startsWith("fc") || normalized.startsWith("fd")) return true;
|
|
703
|
+
if (normalized.startsWith("fe80")) return true;
|
|
704
|
+
return false;
|
|
705
|
+
}
|
|
706
|
+
async function downloadBlob(url, options) {
|
|
707
|
+
var _a22, _b22;
|
|
708
|
+
validateDownloadUrl(url);
|
|
709
|
+
try {
|
|
710
|
+
const response = await fetch(url, {
|
|
711
|
+
signal: options == null ? void 0 : options.abortSignal
|
|
712
|
+
});
|
|
713
|
+
if (response.redirected) {
|
|
714
|
+
validateDownloadUrl(response.url);
|
|
715
|
+
}
|
|
716
|
+
if (!response.ok) {
|
|
717
|
+
throw new DownloadError({
|
|
718
|
+
url,
|
|
719
|
+
statusCode: response.status,
|
|
720
|
+
statusText: response.statusText
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
const data = await readResponseWithSizeLimit({
|
|
724
|
+
response,
|
|
725
|
+
url,
|
|
726
|
+
maxBytes: (_a22 = options == null ? void 0 : options.maxBytes) != null ? _a22 : DEFAULT_MAX_DOWNLOAD_SIZE
|
|
727
|
+
});
|
|
728
|
+
const contentType = (_b22 = response.headers.get("content-type")) != null ? _b22 : void 0;
|
|
729
|
+
return new Blob([data], contentType ? { type: contentType } : void 0);
|
|
730
|
+
} catch (error) {
|
|
731
|
+
if (DownloadError.isInstance(error)) {
|
|
732
|
+
throw error;
|
|
733
|
+
}
|
|
734
|
+
throw new DownloadError({ url, cause: error });
|
|
735
|
+
}
|
|
736
|
+
}
|
|
466
737
|
var createIdGenerator = ({
|
|
467
738
|
prefix,
|
|
468
739
|
size = 16,
|
|
@@ -494,6 +765,25 @@ function isAbortError(error) {
|
|
|
494
765
|
error.name === "TimeoutError");
|
|
495
766
|
}
|
|
496
767
|
var FETCH_FAILED_ERROR_MESSAGES = ["fetch failed", "failed to fetch"];
|
|
768
|
+
var BUN_ERROR_CODES = [
|
|
769
|
+
"ConnectionRefused",
|
|
770
|
+
"ConnectionClosed",
|
|
771
|
+
"FailedToOpenSocket",
|
|
772
|
+
"ECONNRESET",
|
|
773
|
+
"ECONNREFUSED",
|
|
774
|
+
"ETIMEDOUT",
|
|
775
|
+
"EPIPE"
|
|
776
|
+
];
|
|
777
|
+
function isBunNetworkError(error) {
|
|
778
|
+
if (!(error instanceof Error)) {
|
|
779
|
+
return false;
|
|
780
|
+
}
|
|
781
|
+
const code = error.code;
|
|
782
|
+
if (typeof code === "string" && BUN_ERROR_CODES.includes(code)) {
|
|
783
|
+
return true;
|
|
784
|
+
}
|
|
785
|
+
return false;
|
|
786
|
+
}
|
|
497
787
|
function handleFetchError({
|
|
498
788
|
error,
|
|
499
789
|
url,
|
|
@@ -515,17 +805,26 @@ function handleFetchError({
|
|
|
515
805
|
});
|
|
516
806
|
}
|
|
517
807
|
}
|
|
808
|
+
if (isBunNetworkError(error)) {
|
|
809
|
+
return new APICallError({
|
|
810
|
+
message: `Cannot connect to API: ${error.message}`,
|
|
811
|
+
cause: error,
|
|
812
|
+
url,
|
|
813
|
+
requestBodyValues,
|
|
814
|
+
isRetryable: true
|
|
815
|
+
});
|
|
816
|
+
}
|
|
518
817
|
return error;
|
|
519
818
|
}
|
|
520
819
|
function getRuntimeEnvironmentUserAgent(globalThisAny = globalThis) {
|
|
521
|
-
var
|
|
820
|
+
var _a22, _b22, _c;
|
|
522
821
|
if (globalThisAny.window) {
|
|
523
822
|
return `runtime/browser`;
|
|
524
823
|
}
|
|
525
|
-
if ((
|
|
824
|
+
if ((_a22 = globalThisAny.navigator) == null ? void 0 : _a22.userAgent) {
|
|
526
825
|
return `runtime/${globalThisAny.navigator.userAgent.toLowerCase()}`;
|
|
527
826
|
}
|
|
528
|
-
if ((_c = (
|
|
827
|
+
if ((_c = (_b22 = globalThisAny.process) == null ? void 0 : _b22.versions) == null ? void 0 : _c.node) {
|
|
529
828
|
return `runtime/node.js/${globalThisAny.process.version.substring(0)}`;
|
|
530
829
|
}
|
|
531
830
|
if (globalThisAny.EdgeRuntime) {
|
|
@@ -563,9 +862,9 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
563
862
|
);
|
|
564
863
|
return Object.fromEntries(normalizedHeaders.entries());
|
|
565
864
|
}
|
|
566
|
-
var VERSION = true ? "
|
|
567
|
-
var suspectProtoRx = /"
|
|
568
|
-
var suspectConstructorRx = /"
|
|
865
|
+
var VERSION = true ? "4.0.23" : "0.0.0-test";
|
|
866
|
+
var suspectProtoRx = /"(?:_|\\u005[Ff])(?:_|\\u005[Ff])(?:p|\\u0070)(?:r|\\u0072)(?:o|\\u006[Ff])(?:t|\\u0074)(?:o|\\u006[Ff])(?:_|\\u005[Ff])(?:_|\\u005[Ff])"\s*:/;
|
|
867
|
+
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*:/;
|
|
569
868
|
function _parse(text) {
|
|
570
869
|
const obj = JSON.parse(text);
|
|
571
870
|
if (obj === null || typeof obj !== "object") {
|
|
@@ -585,7 +884,7 @@ function filter(obj) {
|
|
|
585
884
|
if (Object.prototype.hasOwnProperty.call(node, "__proto__")) {
|
|
586
885
|
throw new SyntaxError("Object contains forbidden prototype property");
|
|
587
886
|
}
|
|
588
|
-
if (Object.prototype.hasOwnProperty.call(node, "constructor") && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
887
|
+
if (Object.prototype.hasOwnProperty.call(node, "constructor") && node.constructor !== null && typeof node.constructor === "object" && Object.prototype.hasOwnProperty.call(node.constructor, "prototype")) {
|
|
589
888
|
throw new SyntaxError("Object contains forbidden prototype property");
|
|
590
889
|
}
|
|
591
890
|
for (const key in node) {
|
|
@@ -596,75 +895,1278 @@ function filter(obj) {
|
|
|
596
895
|
}
|
|
597
896
|
}
|
|
598
897
|
}
|
|
599
|
-
return obj;
|
|
898
|
+
return obj;
|
|
899
|
+
}
|
|
900
|
+
function secureJsonParse(text) {
|
|
901
|
+
const { stackTraceLimit } = Error;
|
|
902
|
+
try {
|
|
903
|
+
Error.stackTraceLimit = 0;
|
|
904
|
+
} catch (e) {
|
|
905
|
+
return _parse(text);
|
|
906
|
+
}
|
|
907
|
+
try {
|
|
908
|
+
return _parse(text);
|
|
909
|
+
} finally {
|
|
910
|
+
Error.stackTraceLimit = stackTraceLimit;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function addAdditionalPropertiesToJsonSchema(jsonSchema2) {
|
|
914
|
+
if (jsonSchema2.type === "object" || Array.isArray(jsonSchema2.type) && jsonSchema2.type.includes("object")) {
|
|
915
|
+
jsonSchema2.additionalProperties = false;
|
|
916
|
+
const { properties } = jsonSchema2;
|
|
917
|
+
if (properties != null) {
|
|
918
|
+
for (const key of Object.keys(properties)) {
|
|
919
|
+
properties[key] = visit(properties[key]);
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
if (jsonSchema2.items != null) {
|
|
924
|
+
jsonSchema2.items = Array.isArray(jsonSchema2.items) ? jsonSchema2.items.map(visit) : visit(jsonSchema2.items);
|
|
925
|
+
}
|
|
926
|
+
if (jsonSchema2.anyOf != null) {
|
|
927
|
+
jsonSchema2.anyOf = jsonSchema2.anyOf.map(visit);
|
|
928
|
+
}
|
|
929
|
+
if (jsonSchema2.allOf != null) {
|
|
930
|
+
jsonSchema2.allOf = jsonSchema2.allOf.map(visit);
|
|
931
|
+
}
|
|
932
|
+
if (jsonSchema2.oneOf != null) {
|
|
933
|
+
jsonSchema2.oneOf = jsonSchema2.oneOf.map(visit);
|
|
934
|
+
}
|
|
935
|
+
const { definitions } = jsonSchema2;
|
|
936
|
+
if (definitions != null) {
|
|
937
|
+
for (const key of Object.keys(definitions)) {
|
|
938
|
+
definitions[key] = visit(definitions[key]);
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
return jsonSchema2;
|
|
942
|
+
}
|
|
943
|
+
function visit(def) {
|
|
944
|
+
if (typeof def === "boolean") return def;
|
|
945
|
+
return addAdditionalPropertiesToJsonSchema(def);
|
|
946
|
+
}
|
|
947
|
+
var ignoreOverride = /* @__PURE__ */ Symbol(
|
|
948
|
+
"Let zodToJsonSchema decide on which parser to use"
|
|
949
|
+
);
|
|
950
|
+
var defaultOptions = {
|
|
951
|
+
name: void 0,
|
|
952
|
+
$refStrategy: "root",
|
|
953
|
+
basePath: ["#"],
|
|
954
|
+
effectStrategy: "input",
|
|
955
|
+
pipeStrategy: "all",
|
|
956
|
+
dateStrategy: "format:date-time",
|
|
957
|
+
mapStrategy: "entries",
|
|
958
|
+
removeAdditionalStrategy: "passthrough",
|
|
959
|
+
allowedAdditionalProperties: true,
|
|
960
|
+
rejectedAdditionalProperties: false,
|
|
961
|
+
definitionPath: "definitions",
|
|
962
|
+
strictUnions: false,
|
|
963
|
+
definitions: {},
|
|
964
|
+
errorMessages: false,
|
|
965
|
+
patternStrategy: "escape",
|
|
966
|
+
applyRegexFlags: false,
|
|
967
|
+
emailStrategy: "format:email",
|
|
968
|
+
base64Strategy: "contentEncoding:base64",
|
|
969
|
+
nameStrategy: "ref"
|
|
970
|
+
};
|
|
971
|
+
var getDefaultOptions = (options) => typeof options === "string" ? {
|
|
972
|
+
...defaultOptions,
|
|
973
|
+
name: options
|
|
974
|
+
} : {
|
|
975
|
+
...defaultOptions,
|
|
976
|
+
...options
|
|
977
|
+
};
|
|
978
|
+
function parseAnyDef() {
|
|
979
|
+
return {};
|
|
980
|
+
}
|
|
981
|
+
function parseArrayDef(def, refs) {
|
|
982
|
+
var _a22, _b22, _c;
|
|
983
|
+
const res = {
|
|
984
|
+
type: "array"
|
|
985
|
+
};
|
|
986
|
+
if (((_a22 = def.type) == null ? void 0 : _a22._def) && ((_c = (_b22 = def.type) == null ? void 0 : _b22._def) == null ? void 0 : _c.typeName) !== ZodFirstPartyTypeKind.ZodAny) {
|
|
987
|
+
res.items = parseDef(def.type._def, {
|
|
988
|
+
...refs,
|
|
989
|
+
currentPath: [...refs.currentPath, "items"]
|
|
990
|
+
});
|
|
991
|
+
}
|
|
992
|
+
if (def.minLength) {
|
|
993
|
+
res.minItems = def.minLength.value;
|
|
994
|
+
}
|
|
995
|
+
if (def.maxLength) {
|
|
996
|
+
res.maxItems = def.maxLength.value;
|
|
997
|
+
}
|
|
998
|
+
if (def.exactLength) {
|
|
999
|
+
res.minItems = def.exactLength.value;
|
|
1000
|
+
res.maxItems = def.exactLength.value;
|
|
1001
|
+
}
|
|
1002
|
+
return res;
|
|
1003
|
+
}
|
|
1004
|
+
function parseBigintDef(def) {
|
|
1005
|
+
const res = {
|
|
1006
|
+
type: "integer",
|
|
1007
|
+
format: "int64"
|
|
1008
|
+
};
|
|
1009
|
+
if (!def.checks) return res;
|
|
1010
|
+
for (const check of def.checks) {
|
|
1011
|
+
switch (check.kind) {
|
|
1012
|
+
case "min":
|
|
1013
|
+
if (check.inclusive) {
|
|
1014
|
+
res.minimum = check.value;
|
|
1015
|
+
} else {
|
|
1016
|
+
res.exclusiveMinimum = check.value;
|
|
1017
|
+
}
|
|
1018
|
+
break;
|
|
1019
|
+
case "max":
|
|
1020
|
+
if (check.inclusive) {
|
|
1021
|
+
res.maximum = check.value;
|
|
1022
|
+
} else {
|
|
1023
|
+
res.exclusiveMaximum = check.value;
|
|
1024
|
+
}
|
|
1025
|
+
break;
|
|
1026
|
+
case "multipleOf":
|
|
1027
|
+
res.multipleOf = check.value;
|
|
1028
|
+
break;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
return res;
|
|
1032
|
+
}
|
|
1033
|
+
function parseBooleanDef() {
|
|
1034
|
+
return { type: "boolean" };
|
|
1035
|
+
}
|
|
1036
|
+
function parseBrandedDef(_def, refs) {
|
|
1037
|
+
return parseDef(_def.type._def, refs);
|
|
1038
|
+
}
|
|
1039
|
+
var parseCatchDef = (def, refs) => {
|
|
1040
|
+
return parseDef(def.innerType._def, refs);
|
|
1041
|
+
};
|
|
1042
|
+
function parseDateDef(def, refs, overrideDateStrategy) {
|
|
1043
|
+
const strategy = overrideDateStrategy != null ? overrideDateStrategy : refs.dateStrategy;
|
|
1044
|
+
if (Array.isArray(strategy)) {
|
|
1045
|
+
return {
|
|
1046
|
+
anyOf: strategy.map((item, i) => parseDateDef(def, refs, item))
|
|
1047
|
+
};
|
|
1048
|
+
}
|
|
1049
|
+
switch (strategy) {
|
|
1050
|
+
case "string":
|
|
1051
|
+
case "format:date-time":
|
|
1052
|
+
return {
|
|
1053
|
+
type: "string",
|
|
1054
|
+
format: "date-time"
|
|
1055
|
+
};
|
|
1056
|
+
case "format:date":
|
|
1057
|
+
return {
|
|
1058
|
+
type: "string",
|
|
1059
|
+
format: "date"
|
|
1060
|
+
};
|
|
1061
|
+
case "integer":
|
|
1062
|
+
return integerDateParser(def);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
var integerDateParser = (def) => {
|
|
1066
|
+
const res = {
|
|
1067
|
+
type: "integer",
|
|
1068
|
+
format: "unix-time"
|
|
1069
|
+
};
|
|
1070
|
+
for (const check of def.checks) {
|
|
1071
|
+
switch (check.kind) {
|
|
1072
|
+
case "min":
|
|
1073
|
+
res.minimum = check.value;
|
|
1074
|
+
break;
|
|
1075
|
+
case "max":
|
|
1076
|
+
res.maximum = check.value;
|
|
1077
|
+
break;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
return res;
|
|
1081
|
+
};
|
|
1082
|
+
function parseDefaultDef(_def, refs) {
|
|
1083
|
+
return {
|
|
1084
|
+
...parseDef(_def.innerType._def, refs),
|
|
1085
|
+
default: _def.defaultValue()
|
|
1086
|
+
};
|
|
1087
|
+
}
|
|
1088
|
+
function parseEffectsDef(_def, refs) {
|
|
1089
|
+
return refs.effectStrategy === "input" ? parseDef(_def.schema._def, refs) : parseAnyDef();
|
|
1090
|
+
}
|
|
1091
|
+
function parseEnumDef(def) {
|
|
1092
|
+
return {
|
|
1093
|
+
type: "string",
|
|
1094
|
+
enum: Array.from(def.values)
|
|
1095
|
+
};
|
|
1096
|
+
}
|
|
1097
|
+
var isJsonSchema7AllOfType = (type) => {
|
|
1098
|
+
if ("type" in type && type.type === "string") return false;
|
|
1099
|
+
return "allOf" in type;
|
|
1100
|
+
};
|
|
1101
|
+
function parseIntersectionDef(def, refs) {
|
|
1102
|
+
const allOf = [
|
|
1103
|
+
parseDef(def.left._def, {
|
|
1104
|
+
...refs,
|
|
1105
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1106
|
+
}),
|
|
1107
|
+
parseDef(def.right._def, {
|
|
1108
|
+
...refs,
|
|
1109
|
+
currentPath: [...refs.currentPath, "allOf", "1"]
|
|
1110
|
+
})
|
|
1111
|
+
].filter((x) => !!x);
|
|
1112
|
+
const mergedAllOf = [];
|
|
1113
|
+
allOf.forEach((schema) => {
|
|
1114
|
+
if (isJsonSchema7AllOfType(schema)) {
|
|
1115
|
+
mergedAllOf.push(...schema.allOf);
|
|
1116
|
+
} else {
|
|
1117
|
+
let nestedSchema = schema;
|
|
1118
|
+
if ("additionalProperties" in schema && schema.additionalProperties === false) {
|
|
1119
|
+
const { additionalProperties, ...rest } = schema;
|
|
1120
|
+
nestedSchema = rest;
|
|
1121
|
+
}
|
|
1122
|
+
mergedAllOf.push(nestedSchema);
|
|
1123
|
+
}
|
|
1124
|
+
});
|
|
1125
|
+
return mergedAllOf.length ? { allOf: mergedAllOf } : void 0;
|
|
1126
|
+
}
|
|
1127
|
+
function parseLiteralDef(def) {
|
|
1128
|
+
const parsedType = typeof def.value;
|
|
1129
|
+
if (parsedType !== "bigint" && parsedType !== "number" && parsedType !== "boolean" && parsedType !== "string") {
|
|
1130
|
+
return {
|
|
1131
|
+
type: Array.isArray(def.value) ? "array" : "object"
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
1134
|
+
return {
|
|
1135
|
+
type: parsedType === "bigint" ? "integer" : parsedType,
|
|
1136
|
+
const: def.value
|
|
1137
|
+
};
|
|
1138
|
+
}
|
|
1139
|
+
var emojiRegex = void 0;
|
|
1140
|
+
var zodPatterns = {
|
|
1141
|
+
/**
|
|
1142
|
+
* `c` was changed to `[cC]` to replicate /i flag
|
|
1143
|
+
*/
|
|
1144
|
+
cuid: /^[cC][^\s-]{8,}$/,
|
|
1145
|
+
cuid2: /^[0-9a-z]+$/,
|
|
1146
|
+
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
1147
|
+
/**
|
|
1148
|
+
* `a-z` was added to replicate /i flag
|
|
1149
|
+
*/
|
|
1150
|
+
email: /^(?!\.)(?!.*\.\.)([a-zA-Z0-9_'+\-\.]*)[a-zA-Z0-9_+-]@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/,
|
|
1151
|
+
/**
|
|
1152
|
+
* Constructed a valid Unicode RegExp
|
|
1153
|
+
*
|
|
1154
|
+
* Lazily instantiate since this type of regex isn't supported
|
|
1155
|
+
* in all envs (e.g. React Native).
|
|
1156
|
+
*
|
|
1157
|
+
* See:
|
|
1158
|
+
* https://github.com/colinhacks/zod/issues/2433
|
|
1159
|
+
* Fix in Zod:
|
|
1160
|
+
* https://github.com/colinhacks/zod/commit/9340fd51e48576a75adc919bff65dbc4a5d4c99b
|
|
1161
|
+
*/
|
|
1162
|
+
emoji: () => {
|
|
1163
|
+
if (emojiRegex === void 0) {
|
|
1164
|
+
emojiRegex = RegExp(
|
|
1165
|
+
"^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$",
|
|
1166
|
+
"u"
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
return emojiRegex;
|
|
1170
|
+
},
|
|
1171
|
+
/**
|
|
1172
|
+
* Unused
|
|
1173
|
+
*/
|
|
1174
|
+
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}$/,
|
|
1175
|
+
/**
|
|
1176
|
+
* Unused
|
|
1177
|
+
*/
|
|
1178
|
+
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])$/,
|
|
1179
|
+
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])$/,
|
|
1180
|
+
/**
|
|
1181
|
+
* Unused
|
|
1182
|
+
*/
|
|
1183
|
+
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})))$/,
|
|
1184
|
+
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])$/,
|
|
1185
|
+
base64: /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/,
|
|
1186
|
+
base64url: /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/,
|
|
1187
|
+
nanoid: /^[a-zA-Z0-9_-]{21}$/,
|
|
1188
|
+
jwt: /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/
|
|
1189
|
+
};
|
|
1190
|
+
function parseStringDef(def, refs) {
|
|
1191
|
+
const res = {
|
|
1192
|
+
type: "string"
|
|
1193
|
+
};
|
|
1194
|
+
if (def.checks) {
|
|
1195
|
+
for (const check of def.checks) {
|
|
1196
|
+
switch (check.kind) {
|
|
1197
|
+
case "min":
|
|
1198
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1199
|
+
break;
|
|
1200
|
+
case "max":
|
|
1201
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1202
|
+
break;
|
|
1203
|
+
case "email":
|
|
1204
|
+
switch (refs.emailStrategy) {
|
|
1205
|
+
case "format:email":
|
|
1206
|
+
addFormat(res, "email", check.message, refs);
|
|
1207
|
+
break;
|
|
1208
|
+
case "format:idn-email":
|
|
1209
|
+
addFormat(res, "idn-email", check.message, refs);
|
|
1210
|
+
break;
|
|
1211
|
+
case "pattern:zod":
|
|
1212
|
+
addPattern(res, zodPatterns.email, check.message, refs);
|
|
1213
|
+
break;
|
|
1214
|
+
}
|
|
1215
|
+
break;
|
|
1216
|
+
case "url":
|
|
1217
|
+
addFormat(res, "uri", check.message, refs);
|
|
1218
|
+
break;
|
|
1219
|
+
case "uuid":
|
|
1220
|
+
addFormat(res, "uuid", check.message, refs);
|
|
1221
|
+
break;
|
|
1222
|
+
case "regex":
|
|
1223
|
+
addPattern(res, check.regex, check.message, refs);
|
|
1224
|
+
break;
|
|
1225
|
+
case "cuid":
|
|
1226
|
+
addPattern(res, zodPatterns.cuid, check.message, refs);
|
|
1227
|
+
break;
|
|
1228
|
+
case "cuid2":
|
|
1229
|
+
addPattern(res, zodPatterns.cuid2, check.message, refs);
|
|
1230
|
+
break;
|
|
1231
|
+
case "startsWith":
|
|
1232
|
+
addPattern(
|
|
1233
|
+
res,
|
|
1234
|
+
RegExp(`^${escapeLiteralCheckValue(check.value, refs)}`),
|
|
1235
|
+
check.message,
|
|
1236
|
+
refs
|
|
1237
|
+
);
|
|
1238
|
+
break;
|
|
1239
|
+
case "endsWith":
|
|
1240
|
+
addPattern(
|
|
1241
|
+
res,
|
|
1242
|
+
RegExp(`${escapeLiteralCheckValue(check.value, refs)}$`),
|
|
1243
|
+
check.message,
|
|
1244
|
+
refs
|
|
1245
|
+
);
|
|
1246
|
+
break;
|
|
1247
|
+
case "datetime":
|
|
1248
|
+
addFormat(res, "date-time", check.message, refs);
|
|
1249
|
+
break;
|
|
1250
|
+
case "date":
|
|
1251
|
+
addFormat(res, "date", check.message, refs);
|
|
1252
|
+
break;
|
|
1253
|
+
case "time":
|
|
1254
|
+
addFormat(res, "time", check.message, refs);
|
|
1255
|
+
break;
|
|
1256
|
+
case "duration":
|
|
1257
|
+
addFormat(res, "duration", check.message, refs);
|
|
1258
|
+
break;
|
|
1259
|
+
case "length":
|
|
1260
|
+
res.minLength = typeof res.minLength === "number" ? Math.max(res.minLength, check.value) : check.value;
|
|
1261
|
+
res.maxLength = typeof res.maxLength === "number" ? Math.min(res.maxLength, check.value) : check.value;
|
|
1262
|
+
break;
|
|
1263
|
+
case "includes": {
|
|
1264
|
+
addPattern(
|
|
1265
|
+
res,
|
|
1266
|
+
RegExp(escapeLiteralCheckValue(check.value, refs)),
|
|
1267
|
+
check.message,
|
|
1268
|
+
refs
|
|
1269
|
+
);
|
|
1270
|
+
break;
|
|
1271
|
+
}
|
|
1272
|
+
case "ip": {
|
|
1273
|
+
if (check.version !== "v6") {
|
|
1274
|
+
addFormat(res, "ipv4", check.message, refs);
|
|
1275
|
+
}
|
|
1276
|
+
if (check.version !== "v4") {
|
|
1277
|
+
addFormat(res, "ipv6", check.message, refs);
|
|
1278
|
+
}
|
|
1279
|
+
break;
|
|
1280
|
+
}
|
|
1281
|
+
case "base64url":
|
|
1282
|
+
addPattern(res, zodPatterns.base64url, check.message, refs);
|
|
1283
|
+
break;
|
|
1284
|
+
case "jwt":
|
|
1285
|
+
addPattern(res, zodPatterns.jwt, check.message, refs);
|
|
1286
|
+
break;
|
|
1287
|
+
case "cidr": {
|
|
1288
|
+
if (check.version !== "v6") {
|
|
1289
|
+
addPattern(res, zodPatterns.ipv4Cidr, check.message, refs);
|
|
1290
|
+
}
|
|
1291
|
+
if (check.version !== "v4") {
|
|
1292
|
+
addPattern(res, zodPatterns.ipv6Cidr, check.message, refs);
|
|
1293
|
+
}
|
|
1294
|
+
break;
|
|
1295
|
+
}
|
|
1296
|
+
case "emoji":
|
|
1297
|
+
addPattern(res, zodPatterns.emoji(), check.message, refs);
|
|
1298
|
+
break;
|
|
1299
|
+
case "ulid": {
|
|
1300
|
+
addPattern(res, zodPatterns.ulid, check.message, refs);
|
|
1301
|
+
break;
|
|
1302
|
+
}
|
|
1303
|
+
case "base64": {
|
|
1304
|
+
switch (refs.base64Strategy) {
|
|
1305
|
+
case "format:binary": {
|
|
1306
|
+
addFormat(res, "binary", check.message, refs);
|
|
1307
|
+
break;
|
|
1308
|
+
}
|
|
1309
|
+
case "contentEncoding:base64": {
|
|
1310
|
+
res.contentEncoding = "base64";
|
|
1311
|
+
break;
|
|
1312
|
+
}
|
|
1313
|
+
case "pattern:zod": {
|
|
1314
|
+
addPattern(res, zodPatterns.base64, check.message, refs);
|
|
1315
|
+
break;
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
break;
|
|
1319
|
+
}
|
|
1320
|
+
case "nanoid": {
|
|
1321
|
+
addPattern(res, zodPatterns.nanoid, check.message, refs);
|
|
1322
|
+
}
|
|
1323
|
+
case "toLowerCase":
|
|
1324
|
+
case "toUpperCase":
|
|
1325
|
+
case "trim":
|
|
1326
|
+
break;
|
|
1327
|
+
default:
|
|
1328
|
+
/* @__PURE__ */ ((_) => {
|
|
1329
|
+
})(check);
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
return res;
|
|
1334
|
+
}
|
|
1335
|
+
function escapeLiteralCheckValue(literal, refs) {
|
|
1336
|
+
return refs.patternStrategy === "escape" ? escapeNonAlphaNumeric(literal) : literal;
|
|
1337
|
+
}
|
|
1338
|
+
var ALPHA_NUMERIC = new Set(
|
|
1339
|
+
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
1340
|
+
);
|
|
1341
|
+
function escapeNonAlphaNumeric(source) {
|
|
1342
|
+
let result = "";
|
|
1343
|
+
for (let i = 0; i < source.length; i++) {
|
|
1344
|
+
if (!ALPHA_NUMERIC.has(source[i])) {
|
|
1345
|
+
result += "\\";
|
|
1346
|
+
}
|
|
1347
|
+
result += source[i];
|
|
1348
|
+
}
|
|
1349
|
+
return result;
|
|
1350
|
+
}
|
|
1351
|
+
function addFormat(schema, value, message, refs) {
|
|
1352
|
+
var _a22;
|
|
1353
|
+
if (schema.format || ((_a22 = schema.anyOf) == null ? void 0 : _a22.some((x) => x.format))) {
|
|
1354
|
+
if (!schema.anyOf) {
|
|
1355
|
+
schema.anyOf = [];
|
|
1356
|
+
}
|
|
1357
|
+
if (schema.format) {
|
|
1358
|
+
schema.anyOf.push({
|
|
1359
|
+
format: schema.format
|
|
1360
|
+
});
|
|
1361
|
+
delete schema.format;
|
|
1362
|
+
}
|
|
1363
|
+
schema.anyOf.push({
|
|
1364
|
+
format: value,
|
|
1365
|
+
...message && refs.errorMessages && { errorMessage: { format: message } }
|
|
1366
|
+
});
|
|
1367
|
+
} else {
|
|
1368
|
+
schema.format = value;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
function addPattern(schema, regex, message, refs) {
|
|
1372
|
+
var _a22;
|
|
1373
|
+
if (schema.pattern || ((_a22 = schema.allOf) == null ? void 0 : _a22.some((x) => x.pattern))) {
|
|
1374
|
+
if (!schema.allOf) {
|
|
1375
|
+
schema.allOf = [];
|
|
1376
|
+
}
|
|
1377
|
+
if (schema.pattern) {
|
|
1378
|
+
schema.allOf.push({
|
|
1379
|
+
pattern: schema.pattern
|
|
1380
|
+
});
|
|
1381
|
+
delete schema.pattern;
|
|
1382
|
+
}
|
|
1383
|
+
schema.allOf.push({
|
|
1384
|
+
pattern: stringifyRegExpWithFlags(regex, refs),
|
|
1385
|
+
...message && refs.errorMessages && { errorMessage: { pattern: message } }
|
|
1386
|
+
});
|
|
1387
|
+
} else {
|
|
1388
|
+
schema.pattern = stringifyRegExpWithFlags(regex, refs);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
function stringifyRegExpWithFlags(regex, refs) {
|
|
1392
|
+
var _a22;
|
|
1393
|
+
if (!refs.applyRegexFlags || !regex.flags) {
|
|
1394
|
+
return regex.source;
|
|
1395
|
+
}
|
|
1396
|
+
const flags = {
|
|
1397
|
+
i: regex.flags.includes("i"),
|
|
1398
|
+
// Case-insensitive
|
|
1399
|
+
m: regex.flags.includes("m"),
|
|
1400
|
+
// `^` and `$` matches adjacent to newline characters
|
|
1401
|
+
s: regex.flags.includes("s")
|
|
1402
|
+
// `.` matches newlines
|
|
1403
|
+
};
|
|
1404
|
+
const source = flags.i ? regex.source.toLowerCase() : regex.source;
|
|
1405
|
+
let pattern = "";
|
|
1406
|
+
let isEscaped = false;
|
|
1407
|
+
let inCharGroup = false;
|
|
1408
|
+
let inCharRange = false;
|
|
1409
|
+
for (let i = 0; i < source.length; i++) {
|
|
1410
|
+
if (isEscaped) {
|
|
1411
|
+
pattern += source[i];
|
|
1412
|
+
isEscaped = false;
|
|
1413
|
+
continue;
|
|
1414
|
+
}
|
|
1415
|
+
if (flags.i) {
|
|
1416
|
+
if (inCharGroup) {
|
|
1417
|
+
if (source[i].match(/[a-z]/)) {
|
|
1418
|
+
if (inCharRange) {
|
|
1419
|
+
pattern += source[i];
|
|
1420
|
+
pattern += `${source[i - 2]}-${source[i]}`.toUpperCase();
|
|
1421
|
+
inCharRange = false;
|
|
1422
|
+
} else if (source[i + 1] === "-" && ((_a22 = source[i + 2]) == null ? void 0 : _a22.match(/[a-z]/))) {
|
|
1423
|
+
pattern += source[i];
|
|
1424
|
+
inCharRange = true;
|
|
1425
|
+
} else {
|
|
1426
|
+
pattern += `${source[i]}${source[i].toUpperCase()}`;
|
|
1427
|
+
}
|
|
1428
|
+
continue;
|
|
1429
|
+
}
|
|
1430
|
+
} else if (source[i].match(/[a-z]/)) {
|
|
1431
|
+
pattern += `[${source[i]}${source[i].toUpperCase()}]`;
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
}
|
|
1435
|
+
if (flags.m) {
|
|
1436
|
+
if (source[i] === "^") {
|
|
1437
|
+
pattern += `(^|(?<=[\r
|
|
1438
|
+
]))`;
|
|
1439
|
+
continue;
|
|
1440
|
+
} else if (source[i] === "$") {
|
|
1441
|
+
pattern += `($|(?=[\r
|
|
1442
|
+
]))`;
|
|
1443
|
+
continue;
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
if (flags.s && source[i] === ".") {
|
|
1447
|
+
pattern += inCharGroup ? `${source[i]}\r
|
|
1448
|
+
` : `[${source[i]}\r
|
|
1449
|
+
]`;
|
|
1450
|
+
continue;
|
|
1451
|
+
}
|
|
1452
|
+
pattern += source[i];
|
|
1453
|
+
if (source[i] === "\\") {
|
|
1454
|
+
isEscaped = true;
|
|
1455
|
+
} else if (inCharGroup && source[i] === "]") {
|
|
1456
|
+
inCharGroup = false;
|
|
1457
|
+
} else if (!inCharGroup && source[i] === "[") {
|
|
1458
|
+
inCharGroup = true;
|
|
1459
|
+
}
|
|
1460
|
+
}
|
|
1461
|
+
try {
|
|
1462
|
+
new RegExp(pattern);
|
|
1463
|
+
} catch (e) {
|
|
1464
|
+
console.warn(
|
|
1465
|
+
`Could not convert regex pattern at ${refs.currentPath.join(
|
|
1466
|
+
"/"
|
|
1467
|
+
)} to a flag-independent form! Falling back to the flag-ignorant source`
|
|
1468
|
+
);
|
|
1469
|
+
return regex.source;
|
|
1470
|
+
}
|
|
1471
|
+
return pattern;
|
|
1472
|
+
}
|
|
1473
|
+
function parseRecordDef(def, refs) {
|
|
1474
|
+
var _a22, _b22, _c, _d, _e, _f;
|
|
1475
|
+
const schema = {
|
|
1476
|
+
type: "object",
|
|
1477
|
+
additionalProperties: (_a22 = parseDef(def.valueType._def, {
|
|
1478
|
+
...refs,
|
|
1479
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1480
|
+
})) != null ? _a22 : refs.allowedAdditionalProperties
|
|
1481
|
+
};
|
|
1482
|
+
if (((_b22 = def.keyType) == null ? void 0 : _b22._def.typeName) === ZodFirstPartyTypeKind2.ZodString && ((_c = def.keyType._def.checks) == null ? void 0 : _c.length)) {
|
|
1483
|
+
const { type, ...keyType } = parseStringDef(def.keyType._def, refs);
|
|
1484
|
+
return {
|
|
1485
|
+
...schema,
|
|
1486
|
+
propertyNames: keyType
|
|
1487
|
+
};
|
|
1488
|
+
} else if (((_d = def.keyType) == null ? void 0 : _d._def.typeName) === ZodFirstPartyTypeKind2.ZodEnum) {
|
|
1489
|
+
return {
|
|
1490
|
+
...schema,
|
|
1491
|
+
propertyNames: {
|
|
1492
|
+
enum: def.keyType._def.values
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
} else if (((_e = def.keyType) == null ? void 0 : _e._def.typeName) === ZodFirstPartyTypeKind2.ZodBranded && def.keyType._def.type._def.typeName === ZodFirstPartyTypeKind2.ZodString && ((_f = def.keyType._def.type._def.checks) == null ? void 0 : _f.length)) {
|
|
1496
|
+
const { type, ...keyType } = parseBrandedDef(
|
|
1497
|
+
def.keyType._def,
|
|
1498
|
+
refs
|
|
1499
|
+
);
|
|
1500
|
+
return {
|
|
1501
|
+
...schema,
|
|
1502
|
+
propertyNames: keyType
|
|
1503
|
+
};
|
|
1504
|
+
}
|
|
1505
|
+
return schema;
|
|
1506
|
+
}
|
|
1507
|
+
function parseMapDef(def, refs) {
|
|
1508
|
+
if (refs.mapStrategy === "record") {
|
|
1509
|
+
return parseRecordDef(def, refs);
|
|
1510
|
+
}
|
|
1511
|
+
const keys = parseDef(def.keyType._def, {
|
|
1512
|
+
...refs,
|
|
1513
|
+
currentPath: [...refs.currentPath, "items", "items", "0"]
|
|
1514
|
+
}) || parseAnyDef();
|
|
1515
|
+
const values = parseDef(def.valueType._def, {
|
|
1516
|
+
...refs,
|
|
1517
|
+
currentPath: [...refs.currentPath, "items", "items", "1"]
|
|
1518
|
+
}) || parseAnyDef();
|
|
1519
|
+
return {
|
|
1520
|
+
type: "array",
|
|
1521
|
+
maxItems: 125,
|
|
1522
|
+
items: {
|
|
1523
|
+
type: "array",
|
|
1524
|
+
items: [keys, values],
|
|
1525
|
+
minItems: 2,
|
|
1526
|
+
maxItems: 2
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1529
|
+
}
|
|
1530
|
+
function parseNativeEnumDef(def) {
|
|
1531
|
+
const object = def.values;
|
|
1532
|
+
const actualKeys = Object.keys(def.values).filter((key) => {
|
|
1533
|
+
return typeof object[object[key]] !== "number";
|
|
1534
|
+
});
|
|
1535
|
+
const actualValues = actualKeys.map((key) => object[key]);
|
|
1536
|
+
const parsedTypes = Array.from(
|
|
1537
|
+
new Set(actualValues.map((values) => typeof values))
|
|
1538
|
+
);
|
|
1539
|
+
return {
|
|
1540
|
+
type: parsedTypes.length === 1 ? parsedTypes[0] === "string" ? "string" : "number" : ["string", "number"],
|
|
1541
|
+
enum: actualValues
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
function parseNeverDef() {
|
|
1545
|
+
return { not: parseAnyDef() };
|
|
1546
|
+
}
|
|
1547
|
+
function parseNullDef() {
|
|
1548
|
+
return {
|
|
1549
|
+
type: "null"
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
var primitiveMappings = {
|
|
1553
|
+
ZodString: "string",
|
|
1554
|
+
ZodNumber: "number",
|
|
1555
|
+
ZodBigInt: "integer",
|
|
1556
|
+
ZodBoolean: "boolean",
|
|
1557
|
+
ZodNull: "null"
|
|
1558
|
+
};
|
|
1559
|
+
function parseUnionDef(def, refs) {
|
|
1560
|
+
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
|
|
1561
|
+
if (options.every(
|
|
1562
|
+
(x) => x._def.typeName in primitiveMappings && (!x._def.checks || !x._def.checks.length)
|
|
1563
|
+
)) {
|
|
1564
|
+
const types = options.reduce((types2, x) => {
|
|
1565
|
+
const type = primitiveMappings[x._def.typeName];
|
|
1566
|
+
return type && !types2.includes(type) ? [...types2, type] : types2;
|
|
1567
|
+
}, []);
|
|
1568
|
+
return {
|
|
1569
|
+
type: types.length > 1 ? types : types[0]
|
|
1570
|
+
};
|
|
1571
|
+
} else if (options.every((x) => x._def.typeName === "ZodLiteral" && !x.description)) {
|
|
1572
|
+
const types = options.reduce(
|
|
1573
|
+
(acc, x) => {
|
|
1574
|
+
const type = typeof x._def.value;
|
|
1575
|
+
switch (type) {
|
|
1576
|
+
case "string":
|
|
1577
|
+
case "number":
|
|
1578
|
+
case "boolean":
|
|
1579
|
+
return [...acc, type];
|
|
1580
|
+
case "bigint":
|
|
1581
|
+
return [...acc, "integer"];
|
|
1582
|
+
case "object":
|
|
1583
|
+
if (x._def.value === null) return [...acc, "null"];
|
|
1584
|
+
case "symbol":
|
|
1585
|
+
case "undefined":
|
|
1586
|
+
case "function":
|
|
1587
|
+
default:
|
|
1588
|
+
return acc;
|
|
1589
|
+
}
|
|
1590
|
+
},
|
|
1591
|
+
[]
|
|
1592
|
+
);
|
|
1593
|
+
if (types.length === options.length) {
|
|
1594
|
+
const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);
|
|
1595
|
+
return {
|
|
1596
|
+
type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
|
|
1597
|
+
enum: options.reduce(
|
|
1598
|
+
(acc, x) => {
|
|
1599
|
+
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];
|
|
1600
|
+
},
|
|
1601
|
+
[]
|
|
1602
|
+
)
|
|
1603
|
+
};
|
|
1604
|
+
}
|
|
1605
|
+
} else if (options.every((x) => x._def.typeName === "ZodEnum")) {
|
|
1606
|
+
return {
|
|
1607
|
+
type: "string",
|
|
1608
|
+
enum: options.reduce(
|
|
1609
|
+
(acc, x) => [
|
|
1610
|
+
...acc,
|
|
1611
|
+
...x._def.values.filter((x2) => !acc.includes(x2))
|
|
1612
|
+
],
|
|
1613
|
+
[]
|
|
1614
|
+
)
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
return asAnyOf(def, refs);
|
|
1618
|
+
}
|
|
1619
|
+
var asAnyOf = (def, refs) => {
|
|
1620
|
+
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options).map(
|
|
1621
|
+
(x, i) => parseDef(x._def, {
|
|
1622
|
+
...refs,
|
|
1623
|
+
currentPath: [...refs.currentPath, "anyOf", `${i}`]
|
|
1624
|
+
})
|
|
1625
|
+
).filter(
|
|
1626
|
+
(x) => !!x && (!refs.strictUnions || typeof x === "object" && Object.keys(x).length > 0)
|
|
1627
|
+
);
|
|
1628
|
+
return anyOf.length ? { anyOf } : void 0;
|
|
1629
|
+
};
|
|
1630
|
+
function parseNullableDef(def, refs) {
|
|
1631
|
+
if (["ZodString", "ZodNumber", "ZodBigInt", "ZodBoolean", "ZodNull"].includes(
|
|
1632
|
+
def.innerType._def.typeName
|
|
1633
|
+
) && (!def.innerType._def.checks || !def.innerType._def.checks.length)) {
|
|
1634
|
+
return {
|
|
1635
|
+
type: [
|
|
1636
|
+
primitiveMappings[def.innerType._def.typeName],
|
|
1637
|
+
"null"
|
|
1638
|
+
]
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
const base = parseDef(def.innerType._def, {
|
|
1642
|
+
...refs,
|
|
1643
|
+
currentPath: [...refs.currentPath, "anyOf", "0"]
|
|
1644
|
+
});
|
|
1645
|
+
return base && { anyOf: [base, { type: "null" }] };
|
|
1646
|
+
}
|
|
1647
|
+
function parseNumberDef(def) {
|
|
1648
|
+
const res = {
|
|
1649
|
+
type: "number"
|
|
1650
|
+
};
|
|
1651
|
+
if (!def.checks) return res;
|
|
1652
|
+
for (const check of def.checks) {
|
|
1653
|
+
switch (check.kind) {
|
|
1654
|
+
case "int":
|
|
1655
|
+
res.type = "integer";
|
|
1656
|
+
break;
|
|
1657
|
+
case "min":
|
|
1658
|
+
if (check.inclusive) {
|
|
1659
|
+
res.minimum = check.value;
|
|
1660
|
+
} else {
|
|
1661
|
+
res.exclusiveMinimum = check.value;
|
|
1662
|
+
}
|
|
1663
|
+
break;
|
|
1664
|
+
case "max":
|
|
1665
|
+
if (check.inclusive) {
|
|
1666
|
+
res.maximum = check.value;
|
|
1667
|
+
} else {
|
|
1668
|
+
res.exclusiveMaximum = check.value;
|
|
1669
|
+
}
|
|
1670
|
+
break;
|
|
1671
|
+
case "multipleOf":
|
|
1672
|
+
res.multipleOf = check.value;
|
|
1673
|
+
break;
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
return res;
|
|
1677
|
+
}
|
|
1678
|
+
function parseObjectDef(def, refs) {
|
|
1679
|
+
const result = {
|
|
1680
|
+
type: "object",
|
|
1681
|
+
properties: {}
|
|
1682
|
+
};
|
|
1683
|
+
const required = [];
|
|
1684
|
+
const shape = def.shape();
|
|
1685
|
+
for (const propName in shape) {
|
|
1686
|
+
let propDef = shape[propName];
|
|
1687
|
+
if (propDef === void 0 || propDef._def === void 0) {
|
|
1688
|
+
continue;
|
|
1689
|
+
}
|
|
1690
|
+
const propOptional = safeIsOptional(propDef);
|
|
1691
|
+
const parsedDef = parseDef(propDef._def, {
|
|
1692
|
+
...refs,
|
|
1693
|
+
currentPath: [...refs.currentPath, "properties", propName],
|
|
1694
|
+
propertyPath: [...refs.currentPath, "properties", propName]
|
|
1695
|
+
});
|
|
1696
|
+
if (parsedDef === void 0) {
|
|
1697
|
+
continue;
|
|
1698
|
+
}
|
|
1699
|
+
result.properties[propName] = parsedDef;
|
|
1700
|
+
if (!propOptional) {
|
|
1701
|
+
required.push(propName);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
if (required.length) {
|
|
1705
|
+
result.required = required;
|
|
1706
|
+
}
|
|
1707
|
+
const additionalProperties = decideAdditionalProperties(def, refs);
|
|
1708
|
+
if (additionalProperties !== void 0) {
|
|
1709
|
+
result.additionalProperties = additionalProperties;
|
|
1710
|
+
}
|
|
1711
|
+
return result;
|
|
1712
|
+
}
|
|
1713
|
+
function decideAdditionalProperties(def, refs) {
|
|
1714
|
+
if (def.catchall._def.typeName !== "ZodNever") {
|
|
1715
|
+
return parseDef(def.catchall._def, {
|
|
1716
|
+
...refs,
|
|
1717
|
+
currentPath: [...refs.currentPath, "additionalProperties"]
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
switch (def.unknownKeys) {
|
|
1721
|
+
case "passthrough":
|
|
1722
|
+
return refs.allowedAdditionalProperties;
|
|
1723
|
+
case "strict":
|
|
1724
|
+
return refs.rejectedAdditionalProperties;
|
|
1725
|
+
case "strip":
|
|
1726
|
+
return refs.removeAdditionalStrategy === "strict" ? refs.allowedAdditionalProperties : refs.rejectedAdditionalProperties;
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
function safeIsOptional(schema) {
|
|
1730
|
+
try {
|
|
1731
|
+
return schema.isOptional();
|
|
1732
|
+
} catch (e) {
|
|
1733
|
+
return true;
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
var parseOptionalDef = (def, refs) => {
|
|
1737
|
+
var _a22;
|
|
1738
|
+
if (refs.currentPath.toString() === ((_a22 = refs.propertyPath) == null ? void 0 : _a22.toString())) {
|
|
1739
|
+
return parseDef(def.innerType._def, refs);
|
|
1740
|
+
}
|
|
1741
|
+
const innerSchema = parseDef(def.innerType._def, {
|
|
1742
|
+
...refs,
|
|
1743
|
+
currentPath: [...refs.currentPath, "anyOf", "1"]
|
|
1744
|
+
});
|
|
1745
|
+
return innerSchema ? { anyOf: [{ not: parseAnyDef() }, innerSchema] } : parseAnyDef();
|
|
1746
|
+
};
|
|
1747
|
+
var parsePipelineDef = (def, refs) => {
|
|
1748
|
+
if (refs.pipeStrategy === "input") {
|
|
1749
|
+
return parseDef(def.in._def, refs);
|
|
1750
|
+
} else if (refs.pipeStrategy === "output") {
|
|
1751
|
+
return parseDef(def.out._def, refs);
|
|
1752
|
+
}
|
|
1753
|
+
const a = parseDef(def.in._def, {
|
|
1754
|
+
...refs,
|
|
1755
|
+
currentPath: [...refs.currentPath, "allOf", "0"]
|
|
1756
|
+
});
|
|
1757
|
+
const b = parseDef(def.out._def, {
|
|
1758
|
+
...refs,
|
|
1759
|
+
currentPath: [...refs.currentPath, "allOf", a ? "1" : "0"]
|
|
1760
|
+
});
|
|
1761
|
+
return {
|
|
1762
|
+
allOf: [a, b].filter((x) => x !== void 0)
|
|
1763
|
+
};
|
|
1764
|
+
};
|
|
1765
|
+
function parsePromiseDef(def, refs) {
|
|
1766
|
+
return parseDef(def.type._def, refs);
|
|
1767
|
+
}
|
|
1768
|
+
function parseSetDef(def, refs) {
|
|
1769
|
+
const items = parseDef(def.valueType._def, {
|
|
1770
|
+
...refs,
|
|
1771
|
+
currentPath: [...refs.currentPath, "items"]
|
|
1772
|
+
});
|
|
1773
|
+
const schema = {
|
|
1774
|
+
type: "array",
|
|
1775
|
+
uniqueItems: true,
|
|
1776
|
+
items
|
|
1777
|
+
};
|
|
1778
|
+
if (def.minSize) {
|
|
1779
|
+
schema.minItems = def.minSize.value;
|
|
1780
|
+
}
|
|
1781
|
+
if (def.maxSize) {
|
|
1782
|
+
schema.maxItems = def.maxSize.value;
|
|
1783
|
+
}
|
|
1784
|
+
return schema;
|
|
1785
|
+
}
|
|
1786
|
+
function parseTupleDef(def, refs) {
|
|
1787
|
+
if (def.rest) {
|
|
1788
|
+
return {
|
|
1789
|
+
type: "array",
|
|
1790
|
+
minItems: def.items.length,
|
|
1791
|
+
items: def.items.map(
|
|
1792
|
+
(x, i) => parseDef(x._def, {
|
|
1793
|
+
...refs,
|
|
1794
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1795
|
+
})
|
|
1796
|
+
).reduce(
|
|
1797
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1798
|
+
[]
|
|
1799
|
+
),
|
|
1800
|
+
additionalItems: parseDef(def.rest._def, {
|
|
1801
|
+
...refs,
|
|
1802
|
+
currentPath: [...refs.currentPath, "additionalItems"]
|
|
1803
|
+
})
|
|
1804
|
+
};
|
|
1805
|
+
} else {
|
|
1806
|
+
return {
|
|
1807
|
+
type: "array",
|
|
1808
|
+
minItems: def.items.length,
|
|
1809
|
+
maxItems: def.items.length,
|
|
1810
|
+
items: def.items.map(
|
|
1811
|
+
(x, i) => parseDef(x._def, {
|
|
1812
|
+
...refs,
|
|
1813
|
+
currentPath: [...refs.currentPath, "items", `${i}`]
|
|
1814
|
+
})
|
|
1815
|
+
).reduce(
|
|
1816
|
+
(acc, x) => x === void 0 ? acc : [...acc, x],
|
|
1817
|
+
[]
|
|
1818
|
+
)
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
function parseUndefinedDef() {
|
|
1823
|
+
return {
|
|
1824
|
+
not: parseAnyDef()
|
|
1825
|
+
};
|
|
1826
|
+
}
|
|
1827
|
+
function parseUnknownDef() {
|
|
1828
|
+
return parseAnyDef();
|
|
1829
|
+
}
|
|
1830
|
+
var parseReadonlyDef = (def, refs) => {
|
|
1831
|
+
return parseDef(def.innerType._def, refs);
|
|
1832
|
+
};
|
|
1833
|
+
var selectParser = (def, typeName, refs) => {
|
|
1834
|
+
switch (typeName) {
|
|
1835
|
+
case ZodFirstPartyTypeKind3.ZodString:
|
|
1836
|
+
return parseStringDef(def, refs);
|
|
1837
|
+
case ZodFirstPartyTypeKind3.ZodNumber:
|
|
1838
|
+
return parseNumberDef(def);
|
|
1839
|
+
case ZodFirstPartyTypeKind3.ZodObject:
|
|
1840
|
+
return parseObjectDef(def, refs);
|
|
1841
|
+
case ZodFirstPartyTypeKind3.ZodBigInt:
|
|
1842
|
+
return parseBigintDef(def);
|
|
1843
|
+
case ZodFirstPartyTypeKind3.ZodBoolean:
|
|
1844
|
+
return parseBooleanDef();
|
|
1845
|
+
case ZodFirstPartyTypeKind3.ZodDate:
|
|
1846
|
+
return parseDateDef(def, refs);
|
|
1847
|
+
case ZodFirstPartyTypeKind3.ZodUndefined:
|
|
1848
|
+
return parseUndefinedDef();
|
|
1849
|
+
case ZodFirstPartyTypeKind3.ZodNull:
|
|
1850
|
+
return parseNullDef();
|
|
1851
|
+
case ZodFirstPartyTypeKind3.ZodArray:
|
|
1852
|
+
return parseArrayDef(def, refs);
|
|
1853
|
+
case ZodFirstPartyTypeKind3.ZodUnion:
|
|
1854
|
+
case ZodFirstPartyTypeKind3.ZodDiscriminatedUnion:
|
|
1855
|
+
return parseUnionDef(def, refs);
|
|
1856
|
+
case ZodFirstPartyTypeKind3.ZodIntersection:
|
|
1857
|
+
return parseIntersectionDef(def, refs);
|
|
1858
|
+
case ZodFirstPartyTypeKind3.ZodTuple:
|
|
1859
|
+
return parseTupleDef(def, refs);
|
|
1860
|
+
case ZodFirstPartyTypeKind3.ZodRecord:
|
|
1861
|
+
return parseRecordDef(def, refs);
|
|
1862
|
+
case ZodFirstPartyTypeKind3.ZodLiteral:
|
|
1863
|
+
return parseLiteralDef(def);
|
|
1864
|
+
case ZodFirstPartyTypeKind3.ZodEnum:
|
|
1865
|
+
return parseEnumDef(def);
|
|
1866
|
+
case ZodFirstPartyTypeKind3.ZodNativeEnum:
|
|
1867
|
+
return parseNativeEnumDef(def);
|
|
1868
|
+
case ZodFirstPartyTypeKind3.ZodNullable:
|
|
1869
|
+
return parseNullableDef(def, refs);
|
|
1870
|
+
case ZodFirstPartyTypeKind3.ZodOptional:
|
|
1871
|
+
return parseOptionalDef(def, refs);
|
|
1872
|
+
case ZodFirstPartyTypeKind3.ZodMap:
|
|
1873
|
+
return parseMapDef(def, refs);
|
|
1874
|
+
case ZodFirstPartyTypeKind3.ZodSet:
|
|
1875
|
+
return parseSetDef(def, refs);
|
|
1876
|
+
case ZodFirstPartyTypeKind3.ZodLazy:
|
|
1877
|
+
return () => def.getter()._def;
|
|
1878
|
+
case ZodFirstPartyTypeKind3.ZodPromise:
|
|
1879
|
+
return parsePromiseDef(def, refs);
|
|
1880
|
+
case ZodFirstPartyTypeKind3.ZodNaN:
|
|
1881
|
+
case ZodFirstPartyTypeKind3.ZodNever:
|
|
1882
|
+
return parseNeverDef();
|
|
1883
|
+
case ZodFirstPartyTypeKind3.ZodEffects:
|
|
1884
|
+
return parseEffectsDef(def, refs);
|
|
1885
|
+
case ZodFirstPartyTypeKind3.ZodAny:
|
|
1886
|
+
return parseAnyDef();
|
|
1887
|
+
case ZodFirstPartyTypeKind3.ZodUnknown:
|
|
1888
|
+
return parseUnknownDef();
|
|
1889
|
+
case ZodFirstPartyTypeKind3.ZodDefault:
|
|
1890
|
+
return parseDefaultDef(def, refs);
|
|
1891
|
+
case ZodFirstPartyTypeKind3.ZodBranded:
|
|
1892
|
+
return parseBrandedDef(def, refs);
|
|
1893
|
+
case ZodFirstPartyTypeKind3.ZodReadonly:
|
|
1894
|
+
return parseReadonlyDef(def, refs);
|
|
1895
|
+
case ZodFirstPartyTypeKind3.ZodCatch:
|
|
1896
|
+
return parseCatchDef(def, refs);
|
|
1897
|
+
case ZodFirstPartyTypeKind3.ZodPipeline:
|
|
1898
|
+
return parsePipelineDef(def, refs);
|
|
1899
|
+
case ZodFirstPartyTypeKind3.ZodFunction:
|
|
1900
|
+
case ZodFirstPartyTypeKind3.ZodVoid:
|
|
1901
|
+
case ZodFirstPartyTypeKind3.ZodSymbol:
|
|
1902
|
+
return void 0;
|
|
1903
|
+
default:
|
|
1904
|
+
return /* @__PURE__ */ ((_) => void 0)(typeName);
|
|
1905
|
+
}
|
|
1906
|
+
};
|
|
1907
|
+
var getRelativePath = (pathA, pathB) => {
|
|
1908
|
+
let i = 0;
|
|
1909
|
+
for (; i < pathA.length && i < pathB.length; i++) {
|
|
1910
|
+
if (pathA[i] !== pathB[i]) break;
|
|
1911
|
+
}
|
|
1912
|
+
return [(pathA.length - i).toString(), ...pathB.slice(i)].join("/");
|
|
1913
|
+
};
|
|
1914
|
+
function parseDef(def, refs, forceResolution = false) {
|
|
1915
|
+
var _a22;
|
|
1916
|
+
const seenItem = refs.seen.get(def);
|
|
1917
|
+
if (refs.override) {
|
|
1918
|
+
const overrideResult = (_a22 = refs.override) == null ? void 0 : _a22.call(
|
|
1919
|
+
refs,
|
|
1920
|
+
def,
|
|
1921
|
+
refs,
|
|
1922
|
+
seenItem,
|
|
1923
|
+
forceResolution
|
|
1924
|
+
);
|
|
1925
|
+
if (overrideResult !== ignoreOverride) {
|
|
1926
|
+
return overrideResult;
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
if (seenItem && !forceResolution) {
|
|
1930
|
+
const seenSchema = get$ref(seenItem, refs);
|
|
1931
|
+
if (seenSchema !== void 0) {
|
|
1932
|
+
return seenSchema;
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
const newItem = { def, path: refs.currentPath, jsonSchema: void 0 };
|
|
1936
|
+
refs.seen.set(def, newItem);
|
|
1937
|
+
const jsonSchemaOrGetter = selectParser(def, def.typeName, refs);
|
|
1938
|
+
const jsonSchema2 = typeof jsonSchemaOrGetter === "function" ? parseDef(jsonSchemaOrGetter(), refs) : jsonSchemaOrGetter;
|
|
1939
|
+
if (jsonSchema2) {
|
|
1940
|
+
addMeta(def, refs, jsonSchema2);
|
|
1941
|
+
}
|
|
1942
|
+
if (refs.postProcess) {
|
|
1943
|
+
const postProcessResult = refs.postProcess(jsonSchema2, def, refs);
|
|
1944
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1945
|
+
return postProcessResult;
|
|
1946
|
+
}
|
|
1947
|
+
newItem.jsonSchema = jsonSchema2;
|
|
1948
|
+
return jsonSchema2;
|
|
600
1949
|
}
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
1950
|
+
var get$ref = (item, refs) => {
|
|
1951
|
+
switch (refs.$refStrategy) {
|
|
1952
|
+
case "root":
|
|
1953
|
+
return { $ref: item.path.join("/") };
|
|
1954
|
+
case "relative":
|
|
1955
|
+
return { $ref: getRelativePath(refs.currentPath, item.path) };
|
|
1956
|
+
case "none":
|
|
1957
|
+
case "seen": {
|
|
1958
|
+
if (item.path.length < refs.currentPath.length && item.path.every((value, index) => refs.currentPath[index] === value)) {
|
|
1959
|
+
console.warn(
|
|
1960
|
+
`Recursive reference detected at ${refs.currentPath.join(
|
|
1961
|
+
"/"
|
|
1962
|
+
)}! Defaulting to any`
|
|
1963
|
+
);
|
|
1964
|
+
return parseAnyDef();
|
|
1965
|
+
}
|
|
1966
|
+
return refs.$refStrategy === "seen" ? parseAnyDef() : void 0;
|
|
1967
|
+
}
|
|
607
1968
|
}
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
1969
|
+
};
|
|
1970
|
+
var addMeta = (def, refs, jsonSchema2) => {
|
|
1971
|
+
if (def.description) {
|
|
1972
|
+
jsonSchema2.description = def.description;
|
|
1973
|
+
}
|
|
1974
|
+
return jsonSchema2;
|
|
1975
|
+
};
|
|
1976
|
+
var getRefs = (options) => {
|
|
1977
|
+
const _options = getDefaultOptions(options);
|
|
1978
|
+
const currentPath = _options.name !== void 0 ? [..._options.basePath, _options.definitionPath, _options.name] : _options.basePath;
|
|
1979
|
+
return {
|
|
1980
|
+
..._options,
|
|
1981
|
+
currentPath,
|
|
1982
|
+
propertyPath: void 0,
|
|
1983
|
+
seen: new Map(
|
|
1984
|
+
Object.entries(_options.definitions).map(([name22, def]) => [
|
|
1985
|
+
def._def,
|
|
1986
|
+
{
|
|
1987
|
+
def: def._def,
|
|
1988
|
+
path: [..._options.basePath, _options.definitionPath, name22],
|
|
1989
|
+
// Resolution of references will be forced even though seen, so it's ok that the schema is undefined here for now.
|
|
1990
|
+
jsonSchema: void 0
|
|
1991
|
+
}
|
|
1992
|
+
])
|
|
1993
|
+
)
|
|
1994
|
+
};
|
|
1995
|
+
};
|
|
1996
|
+
var zod3ToJsonSchema = (schema, options) => {
|
|
1997
|
+
var _a22;
|
|
1998
|
+
const refs = getRefs(options);
|
|
1999
|
+
let definitions = typeof options === "object" && options.definitions ? Object.entries(options.definitions).reduce(
|
|
2000
|
+
(acc, [name32, schema2]) => {
|
|
2001
|
+
var _a32;
|
|
2002
|
+
return {
|
|
2003
|
+
...acc,
|
|
2004
|
+
[name32]: (_a32 = parseDef(
|
|
2005
|
+
schema2._def,
|
|
2006
|
+
{
|
|
2007
|
+
...refs,
|
|
2008
|
+
currentPath: [...refs.basePath, refs.definitionPath, name32]
|
|
2009
|
+
},
|
|
2010
|
+
true
|
|
2011
|
+
)) != null ? _a32 : parseAnyDef()
|
|
2012
|
+
};
|
|
2013
|
+
},
|
|
2014
|
+
{}
|
|
2015
|
+
) : void 0;
|
|
2016
|
+
const name22 = typeof options === "string" ? options : (options == null ? void 0 : options.nameStrategy) === "title" ? void 0 : options == null ? void 0 : options.name;
|
|
2017
|
+
const main = (_a22 = parseDef(
|
|
2018
|
+
schema._def,
|
|
2019
|
+
name22 === void 0 ? refs : {
|
|
2020
|
+
...refs,
|
|
2021
|
+
currentPath: [...refs.basePath, refs.definitionPath, name22]
|
|
2022
|
+
},
|
|
2023
|
+
false
|
|
2024
|
+
)) != null ? _a22 : parseAnyDef();
|
|
2025
|
+
const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
|
|
2026
|
+
if (title !== void 0) {
|
|
2027
|
+
main.title = title;
|
|
612
2028
|
}
|
|
2029
|
+
const combined = name22 === void 0 ? definitions ? {
|
|
2030
|
+
...main,
|
|
2031
|
+
[refs.definitionPath]: definitions
|
|
2032
|
+
} : main : {
|
|
2033
|
+
$ref: [
|
|
2034
|
+
...refs.$refStrategy === "relative" ? [] : refs.basePath,
|
|
2035
|
+
refs.definitionPath,
|
|
2036
|
+
name22
|
|
2037
|
+
].join("/"),
|
|
2038
|
+
[refs.definitionPath]: {
|
|
2039
|
+
...definitions,
|
|
2040
|
+
[name22]: main
|
|
2041
|
+
}
|
|
2042
|
+
};
|
|
2043
|
+
combined.$schema = "http://json-schema.org/draft-07/schema#";
|
|
2044
|
+
return combined;
|
|
2045
|
+
};
|
|
2046
|
+
var schemaSymbol = /* @__PURE__ */ Symbol.for("vercel.ai.schema");
|
|
2047
|
+
function jsonSchema(jsonSchema2, {
|
|
2048
|
+
validate
|
|
2049
|
+
} = {}) {
|
|
2050
|
+
return {
|
|
2051
|
+
[schemaSymbol]: true,
|
|
2052
|
+
_type: void 0,
|
|
2053
|
+
// should never be used directly
|
|
2054
|
+
get jsonSchema() {
|
|
2055
|
+
if (typeof jsonSchema2 === "function") {
|
|
2056
|
+
jsonSchema2 = jsonSchema2();
|
|
2057
|
+
}
|
|
2058
|
+
return jsonSchema2;
|
|
2059
|
+
},
|
|
2060
|
+
validate
|
|
2061
|
+
};
|
|
613
2062
|
}
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
return { [validatorSymbol]: true, validate };
|
|
2063
|
+
function isSchema(value) {
|
|
2064
|
+
return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
|
|
617
2065
|
}
|
|
618
|
-
function
|
|
619
|
-
return
|
|
2066
|
+
function asSchema(schema) {
|
|
2067
|
+
return schema == null ? jsonSchema({ properties: {}, additionalProperties: false }) : isSchema(schema) ? schema : "~standard" in schema ? schema["~standard"].vendor === "zod" ? zodSchema(schema) : standardSchema(schema) : schema();
|
|
620
2068
|
}
|
|
621
|
-
function
|
|
622
|
-
return
|
|
2069
|
+
function standardSchema(standardSchema2) {
|
|
2070
|
+
return jsonSchema(
|
|
2071
|
+
() => addAdditionalPropertiesToJsonSchema(
|
|
2072
|
+
standardSchema2["~standard"].jsonSchema.input({
|
|
2073
|
+
target: "draft-07"
|
|
2074
|
+
})
|
|
2075
|
+
),
|
|
2076
|
+
{
|
|
2077
|
+
validate: async (value) => {
|
|
2078
|
+
const result = await standardSchema2["~standard"].validate(value);
|
|
2079
|
+
return "value" in result ? { success: true, value: result.value } : {
|
|
2080
|
+
success: false,
|
|
2081
|
+
error: new TypeValidationError({
|
|
2082
|
+
value,
|
|
2083
|
+
cause: result.issues
|
|
2084
|
+
})
|
|
2085
|
+
};
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
);
|
|
623
2089
|
}
|
|
624
|
-
function
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
2090
|
+
function zod3Schema(zodSchema2, options) {
|
|
2091
|
+
var _a22;
|
|
2092
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
2093
|
+
return jsonSchema(
|
|
2094
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
2095
|
+
() => zod3ToJsonSchema(zodSchema2, {
|
|
2096
|
+
$refStrategy: useReferences ? "root" : "none"
|
|
2097
|
+
}),
|
|
2098
|
+
{
|
|
2099
|
+
validate: async (value) => {
|
|
2100
|
+
const result = await zodSchema2.safeParseAsync(value);
|
|
2101
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
);
|
|
2105
|
+
}
|
|
2106
|
+
function zod4Schema(zodSchema2, options) {
|
|
2107
|
+
var _a22;
|
|
2108
|
+
const useReferences = (_a22 = options == null ? void 0 : options.useReferences) != null ? _a22 : false;
|
|
2109
|
+
return jsonSchema(
|
|
2110
|
+
// defer json schema creation to avoid unnecessary computation when only validation is needed
|
|
2111
|
+
() => addAdditionalPropertiesToJsonSchema(
|
|
2112
|
+
z4.toJSONSchema(zodSchema2, {
|
|
2113
|
+
target: "draft-7",
|
|
2114
|
+
io: "input",
|
|
2115
|
+
reused: useReferences ? "ref" : "inline"
|
|
632
2116
|
})
|
|
633
|
-
|
|
634
|
-
|
|
2117
|
+
),
|
|
2118
|
+
{
|
|
2119
|
+
validate: async (value) => {
|
|
2120
|
+
const result = await z4.safeParseAsync(zodSchema2, value);
|
|
2121
|
+
return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
);
|
|
2125
|
+
}
|
|
2126
|
+
function isZod4Schema(zodSchema2) {
|
|
2127
|
+
return "_zod" in zodSchema2;
|
|
2128
|
+
}
|
|
2129
|
+
function zodSchema(zodSchema2, options) {
|
|
2130
|
+
if (isZod4Schema(zodSchema2)) {
|
|
2131
|
+
return zod4Schema(zodSchema2, options);
|
|
2132
|
+
} else {
|
|
2133
|
+
return zod3Schema(zodSchema2, options);
|
|
2134
|
+
}
|
|
635
2135
|
}
|
|
636
2136
|
async function validateTypes({
|
|
637
2137
|
value,
|
|
638
|
-
schema
|
|
2138
|
+
schema,
|
|
2139
|
+
context
|
|
639
2140
|
}) {
|
|
640
|
-
const result = await safeValidateTypes({ value, schema });
|
|
2141
|
+
const result = await safeValidateTypes({ value, schema, context });
|
|
641
2142
|
if (!result.success) {
|
|
642
|
-
throw TypeValidationError.wrap({ value, cause: result.error });
|
|
2143
|
+
throw TypeValidationError.wrap({ value, cause: result.error, context });
|
|
643
2144
|
}
|
|
644
2145
|
return result.value;
|
|
645
2146
|
}
|
|
646
2147
|
async function safeValidateTypes({
|
|
647
2148
|
value,
|
|
648
|
-
schema
|
|
2149
|
+
schema,
|
|
2150
|
+
context
|
|
649
2151
|
}) {
|
|
650
|
-
const
|
|
2152
|
+
const actualSchema = asSchema(schema);
|
|
651
2153
|
try {
|
|
652
|
-
if (
|
|
2154
|
+
if (actualSchema.validate == null) {
|
|
653
2155
|
return { success: true, value, rawValue: value };
|
|
654
2156
|
}
|
|
655
|
-
const result = await
|
|
2157
|
+
const result = await actualSchema.validate(value);
|
|
656
2158
|
if (result.success) {
|
|
657
2159
|
return { success: true, value: result.value, rawValue: value };
|
|
658
2160
|
}
|
|
659
2161
|
return {
|
|
660
2162
|
success: false,
|
|
661
|
-
error: TypeValidationError.wrap({ value, cause: result.error }),
|
|
2163
|
+
error: TypeValidationError.wrap({ value, cause: result.error, context }),
|
|
662
2164
|
rawValue: value
|
|
663
2165
|
};
|
|
664
2166
|
} catch (error) {
|
|
665
2167
|
return {
|
|
666
2168
|
success: false,
|
|
667
|
-
error: TypeValidationError.wrap({ value, cause: error }),
|
|
2169
|
+
error: TypeValidationError.wrap({ value, cause: error, context }),
|
|
668
2170
|
rawValue: value
|
|
669
2171
|
};
|
|
670
2172
|
}
|
|
@@ -756,7 +2258,7 @@ var postJsonToApi = async ({
|
|
|
756
2258
|
failedResponseHandler,
|
|
757
2259
|
successfulResponseHandler,
|
|
758
2260
|
abortSignal,
|
|
759
|
-
fetch
|
|
2261
|
+
fetch: fetch2
|
|
760
2262
|
}) => postToApi({
|
|
761
2263
|
url,
|
|
762
2264
|
headers: {
|
|
@@ -770,7 +2272,27 @@ var postJsonToApi = async ({
|
|
|
770
2272
|
failedResponseHandler,
|
|
771
2273
|
successfulResponseHandler,
|
|
772
2274
|
abortSignal,
|
|
773
|
-
fetch
|
|
2275
|
+
fetch: fetch2
|
|
2276
|
+
});
|
|
2277
|
+
var postFormDataToApi = async ({
|
|
2278
|
+
url,
|
|
2279
|
+
headers,
|
|
2280
|
+
formData,
|
|
2281
|
+
failedResponseHandler,
|
|
2282
|
+
successfulResponseHandler,
|
|
2283
|
+
abortSignal,
|
|
2284
|
+
fetch: fetch2
|
|
2285
|
+
}) => postToApi({
|
|
2286
|
+
url,
|
|
2287
|
+
headers,
|
|
2288
|
+
body: {
|
|
2289
|
+
content: formData,
|
|
2290
|
+
values: Object.fromEntries(formData.entries())
|
|
2291
|
+
},
|
|
2292
|
+
failedResponseHandler,
|
|
2293
|
+
successfulResponseHandler,
|
|
2294
|
+
abortSignal,
|
|
2295
|
+
fetch: fetch2
|
|
774
2296
|
});
|
|
775
2297
|
var postToApi = async ({
|
|
776
2298
|
url,
|
|
@@ -779,10 +2301,10 @@ var postToApi = async ({
|
|
|
779
2301
|
successfulResponseHandler,
|
|
780
2302
|
failedResponseHandler,
|
|
781
2303
|
abortSignal,
|
|
782
|
-
fetch = getOriginalFetch2()
|
|
2304
|
+
fetch: fetch2 = getOriginalFetch2()
|
|
783
2305
|
}) => {
|
|
784
2306
|
try {
|
|
785
|
-
const response = await
|
|
2307
|
+
const response = await fetch2(url, {
|
|
786
2308
|
method: "POST",
|
|
787
2309
|
headers: withUserAgentSuffix(
|
|
788
2310
|
headers,
|
|
@@ -932,25 +2454,11 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
|
|
|
932
2454
|
rawValue: parsedResult.rawValue
|
|
933
2455
|
};
|
|
934
2456
|
};
|
|
935
|
-
var ALPHA_NUMERIC = new Set(
|
|
936
|
-
"ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvxyz0123456789"
|
|
937
|
-
);
|
|
938
|
-
var { btoa, atob } = globalThis;
|
|
939
|
-
function convertUint8ArrayToBase64(array) {
|
|
940
|
-
let latin1string = "";
|
|
941
|
-
for (let i = 0; i < array.length; i++) {
|
|
942
|
-
latin1string += String.fromCodePoint(array[i]);
|
|
943
|
-
}
|
|
944
|
-
return btoa(latin1string);
|
|
945
|
-
}
|
|
946
|
-
function convertToBase64(value) {
|
|
947
|
-
return value instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
|
|
948
|
-
}
|
|
949
2457
|
function withoutTrailingSlash(url) {
|
|
950
2458
|
return url == null ? void 0 : url.replace(/\/$/, "");
|
|
951
2459
|
}
|
|
952
2460
|
|
|
953
|
-
// node_modules/.pnpm/@ai-sdk+openai-compatible@
|
|
2461
|
+
// node_modules/.pnpm/@ai-sdk+openai-compatible@2.0.41_zod@3.25.76/node_modules/@ai-sdk/openai-compatible/dist/index.mjs
|
|
954
2462
|
import { z as z3 } from "zod/v4";
|
|
955
2463
|
import { z } from "zod/v4";
|
|
956
2464
|
import { z as z2 } from "zod/v4";
|
|
@@ -959,11 +2467,85 @@ import { z as z42 } from "zod/v4";
|
|
|
959
2467
|
import { z as z7 } from "zod/v4";
|
|
960
2468
|
import { z as z6 } from "zod/v4";
|
|
961
2469
|
import { z as z8 } from "zod/v4";
|
|
2470
|
+
function toCamelCase(str) {
|
|
2471
|
+
return str.replace(/[_-]([a-z])/g, (g) => g[1].toUpperCase());
|
|
2472
|
+
}
|
|
2473
|
+
function resolveProviderOptionsKey(rawName, providerOptions) {
|
|
2474
|
+
const camelName = toCamelCase(rawName);
|
|
2475
|
+
if (camelName !== rawName && (providerOptions == null ? void 0 : providerOptions[camelName]) != null) {
|
|
2476
|
+
return camelName;
|
|
2477
|
+
}
|
|
2478
|
+
return rawName;
|
|
2479
|
+
}
|
|
2480
|
+
var openaiCompatibleErrorDataSchema = z.object({
|
|
2481
|
+
error: z.object({
|
|
2482
|
+
message: z.string(),
|
|
2483
|
+
// The additional information below is handled loosely to support
|
|
2484
|
+
// OpenAI-compatible providers that have slightly different error
|
|
2485
|
+
// responses:
|
|
2486
|
+
type: z.string().nullish(),
|
|
2487
|
+
param: z.any().nullish(),
|
|
2488
|
+
code: z.union([z.string(), z.number()]).nullish()
|
|
2489
|
+
})
|
|
2490
|
+
});
|
|
2491
|
+
var defaultOpenAICompatibleErrorStructure = {
|
|
2492
|
+
errorSchema: openaiCompatibleErrorDataSchema,
|
|
2493
|
+
errorToMessage: (data) => data.error.message
|
|
2494
|
+
};
|
|
2495
|
+
function convertOpenAICompatibleChatUsage(usage) {
|
|
2496
|
+
var _a16, _b16, _c, _d, _e, _f;
|
|
2497
|
+
if (usage == null) {
|
|
2498
|
+
return {
|
|
2499
|
+
inputTokens: {
|
|
2500
|
+
total: void 0,
|
|
2501
|
+
noCache: void 0,
|
|
2502
|
+
cacheRead: void 0,
|
|
2503
|
+
cacheWrite: void 0
|
|
2504
|
+
},
|
|
2505
|
+
outputTokens: {
|
|
2506
|
+
total: void 0,
|
|
2507
|
+
text: void 0,
|
|
2508
|
+
reasoning: void 0
|
|
2509
|
+
},
|
|
2510
|
+
raw: void 0
|
|
2511
|
+
};
|
|
2512
|
+
}
|
|
2513
|
+
const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
|
|
2514
|
+
const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
|
|
2515
|
+
const cacheReadTokens = (_d = (_c = usage.prompt_tokens_details) == null ? void 0 : _c.cached_tokens) != null ? _d : 0;
|
|
2516
|
+
const reasoningTokens = (_f = (_e = usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : 0;
|
|
2517
|
+
return {
|
|
2518
|
+
inputTokens: {
|
|
2519
|
+
total: promptTokens,
|
|
2520
|
+
noCache: promptTokens - cacheReadTokens,
|
|
2521
|
+
cacheRead: cacheReadTokens,
|
|
2522
|
+
cacheWrite: void 0
|
|
2523
|
+
},
|
|
2524
|
+
outputTokens: {
|
|
2525
|
+
total: completionTokens,
|
|
2526
|
+
text: completionTokens - reasoningTokens,
|
|
2527
|
+
reasoning: reasoningTokens
|
|
2528
|
+
},
|
|
2529
|
+
raw: usage
|
|
2530
|
+
};
|
|
2531
|
+
}
|
|
962
2532
|
function getOpenAIMetadata(message) {
|
|
963
|
-
var
|
|
964
|
-
return (
|
|
2533
|
+
var _a16, _b16;
|
|
2534
|
+
return (_b16 = (_a16 = message == null ? void 0 : message.providerOptions) == null ? void 0 : _a16.openaiCompatible) != null ? _b16 : {};
|
|
2535
|
+
}
|
|
2536
|
+
function getAudioFormat(mediaType) {
|
|
2537
|
+
switch (mediaType) {
|
|
2538
|
+
case "audio/wav":
|
|
2539
|
+
return "wav";
|
|
2540
|
+
case "audio/mp3":
|
|
2541
|
+
case "audio/mpeg":
|
|
2542
|
+
return "mp3";
|
|
2543
|
+
default:
|
|
2544
|
+
return null;
|
|
2545
|
+
}
|
|
965
2546
|
}
|
|
966
2547
|
function convertToOpenAICompatibleChatMessages(prompt) {
|
|
2548
|
+
var _a16, _b16, _c;
|
|
967
2549
|
const messages = [];
|
|
968
2550
|
for (const { role, content, ...message } of prompt) {
|
|
969
2551
|
const metadata = getOpenAIMetadata({ ...message });
|
|
@@ -984,6 +2566,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
984
2566
|
messages.push({
|
|
985
2567
|
role: "user",
|
|
986
2568
|
content: content.map((part) => {
|
|
2569
|
+
var _a22;
|
|
987
2570
|
const partMetadata = getOpenAIMetadata(part);
|
|
988
2571
|
switch (part.type) {
|
|
989
2572
|
case "text": {
|
|
@@ -999,11 +2582,56 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
999
2582
|
},
|
|
1000
2583
|
...partMetadata
|
|
1001
2584
|
};
|
|
1002
|
-
} else {
|
|
1003
|
-
throw new UnsupportedFunctionalityError({
|
|
1004
|
-
functionality: `file part media type ${part.mediaType}`
|
|
1005
|
-
});
|
|
1006
2585
|
}
|
|
2586
|
+
if (part.mediaType.startsWith("audio/")) {
|
|
2587
|
+
if (part.data instanceof URL) {
|
|
2588
|
+
throw new UnsupportedFunctionalityError({
|
|
2589
|
+
functionality: "audio file parts with URLs"
|
|
2590
|
+
});
|
|
2591
|
+
}
|
|
2592
|
+
const format = getAudioFormat(part.mediaType);
|
|
2593
|
+
if (format === null) {
|
|
2594
|
+
throw new UnsupportedFunctionalityError({
|
|
2595
|
+
functionality: `audio media type ${part.mediaType}`
|
|
2596
|
+
});
|
|
2597
|
+
}
|
|
2598
|
+
return {
|
|
2599
|
+
type: "input_audio",
|
|
2600
|
+
input_audio: {
|
|
2601
|
+
data: convertToBase64(part.data),
|
|
2602
|
+
format
|
|
2603
|
+
},
|
|
2604
|
+
...partMetadata
|
|
2605
|
+
};
|
|
2606
|
+
}
|
|
2607
|
+
if (part.mediaType === "application/pdf") {
|
|
2608
|
+
if (part.data instanceof URL) {
|
|
2609
|
+
throw new UnsupportedFunctionalityError({
|
|
2610
|
+
functionality: "PDF file parts with URLs"
|
|
2611
|
+
});
|
|
2612
|
+
}
|
|
2613
|
+
return {
|
|
2614
|
+
type: "file",
|
|
2615
|
+
file: {
|
|
2616
|
+
filename: (_a22 = part.filename) != null ? _a22 : "document.pdf",
|
|
2617
|
+
file_data: `data:application/pdf;base64,${convertToBase64(part.data)}`
|
|
2618
|
+
},
|
|
2619
|
+
...partMetadata
|
|
2620
|
+
};
|
|
2621
|
+
}
|
|
2622
|
+
if (part.mediaType.startsWith("text/")) {
|
|
2623
|
+
const textContent = part.data instanceof URL ? part.data.toString() : typeof part.data === "string" ? new TextDecoder().decode(
|
|
2624
|
+
convertBase64ToUint8Array(part.data)
|
|
2625
|
+
) : new TextDecoder().decode(part.data);
|
|
2626
|
+
return {
|
|
2627
|
+
type: "text",
|
|
2628
|
+
text: textContent,
|
|
2629
|
+
...partMetadata
|
|
2630
|
+
};
|
|
2631
|
+
}
|
|
2632
|
+
throw new UnsupportedFunctionalityError({
|
|
2633
|
+
functionality: `file part media type ${part.mediaType}`
|
|
2634
|
+
});
|
|
1007
2635
|
}
|
|
1008
2636
|
}
|
|
1009
2637
|
}),
|
|
@@ -1013,6 +2641,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1013
2641
|
}
|
|
1014
2642
|
case "assistant": {
|
|
1015
2643
|
let text = "";
|
|
2644
|
+
let reasoning = "";
|
|
1016
2645
|
const toolCalls = [];
|
|
1017
2646
|
for (const part of content) {
|
|
1018
2647
|
const partMetadata = getOpenAIMetadata(part);
|
|
@@ -1021,7 +2650,12 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1021
2650
|
text += part.text;
|
|
1022
2651
|
break;
|
|
1023
2652
|
}
|
|
2653
|
+
case "reasoning": {
|
|
2654
|
+
reasoning += part.text;
|
|
2655
|
+
break;
|
|
2656
|
+
}
|
|
1024
2657
|
case "tool-call": {
|
|
2658
|
+
const thoughtSignature = (_b16 = (_a16 = part.providerOptions) == null ? void 0 : _a16.google) == null ? void 0 : _b16.thoughtSignature;
|
|
1025
2659
|
toolCalls.push({
|
|
1026
2660
|
id: part.toolCallId,
|
|
1027
2661
|
type: "function",
|
|
@@ -1029,7 +2663,15 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1029
2663
|
name: part.toolName,
|
|
1030
2664
|
arguments: JSON.stringify(part.input)
|
|
1031
2665
|
},
|
|
1032
|
-
...partMetadata
|
|
2666
|
+
...partMetadata,
|
|
2667
|
+
// Include extra_content for Google Gemini thought signatures
|
|
2668
|
+
...thoughtSignature ? {
|
|
2669
|
+
extra_content: {
|
|
2670
|
+
google: {
|
|
2671
|
+
thought_signature: String(thoughtSignature)
|
|
2672
|
+
}
|
|
2673
|
+
}
|
|
2674
|
+
} : {}
|
|
1033
2675
|
});
|
|
1034
2676
|
break;
|
|
1035
2677
|
}
|
|
@@ -1038,6 +2680,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1038
2680
|
messages.push({
|
|
1039
2681
|
role: "assistant",
|
|
1040
2682
|
content: text,
|
|
2683
|
+
...reasoning.length > 0 ? { reasoning_content: reasoning } : {},
|
|
1041
2684
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
1042
2685
|
...metadata
|
|
1043
2686
|
});
|
|
@@ -1045,6 +2688,9 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1045
2688
|
}
|
|
1046
2689
|
case "tool": {
|
|
1047
2690
|
for (const toolResponse of content) {
|
|
2691
|
+
if (toolResponse.type === "tool-approval-response") {
|
|
2692
|
+
continue;
|
|
2693
|
+
}
|
|
1048
2694
|
const output = toolResponse.output;
|
|
1049
2695
|
let contentValue;
|
|
1050
2696
|
switch (output.type) {
|
|
@@ -1052,6 +2698,9 @@ function convertToOpenAICompatibleChatMessages(prompt) {
|
|
|
1052
2698
|
case "error-text":
|
|
1053
2699
|
contentValue = output.value;
|
|
1054
2700
|
break;
|
|
2701
|
+
case "execution-denied":
|
|
2702
|
+
contentValue = (_c = output.reason) != null ? _c : "Tool execution denied.";
|
|
2703
|
+
break;
|
|
1055
2704
|
case "content":
|
|
1056
2705
|
case "json":
|
|
1057
2706
|
case "error-json":
|
|
@@ -1099,39 +2748,32 @@ function mapOpenAICompatibleFinishReason(finishReason) {
|
|
|
1099
2748
|
case "tool_calls":
|
|
1100
2749
|
return "tool-calls";
|
|
1101
2750
|
default:
|
|
1102
|
-
return "
|
|
2751
|
+
return "other";
|
|
1103
2752
|
}
|
|
1104
2753
|
}
|
|
1105
|
-
var
|
|
2754
|
+
var openaiCompatibleLanguageModelChatOptions = z2.object({
|
|
1106
2755
|
/**
|
|
1107
2756
|
* A unique identifier representing your end-user, which can help the provider to
|
|
1108
2757
|
* monitor and detect abuse.
|
|
1109
2758
|
*/
|
|
1110
|
-
user:
|
|
2759
|
+
user: z2.string().optional(),
|
|
1111
2760
|
/**
|
|
1112
2761
|
* Reasoning effort for reasoning models. Defaults to `medium`.
|
|
1113
2762
|
*/
|
|
1114
|
-
reasoningEffort:
|
|
2763
|
+
reasoningEffort: z2.string().optional(),
|
|
1115
2764
|
/**
|
|
1116
2765
|
* Controls the verbosity of the generated text. Defaults to `medium`.
|
|
1117
2766
|
*/
|
|
1118
|
-
textVerbosity:
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
param: z2.any().nullish(),
|
|
1128
|
-
code: z2.union([z2.string(), z2.number()]).nullish()
|
|
1129
|
-
})
|
|
2767
|
+
textVerbosity: z2.string().optional(),
|
|
2768
|
+
/**
|
|
2769
|
+
* Whether to use strict JSON schema validation.
|
|
2770
|
+
* When true, the model uses constrained decoding to guarantee schema compliance.
|
|
2771
|
+
* Only used when the provider supports structured outputs and a schema is provided.
|
|
2772
|
+
*
|
|
2773
|
+
* @default true
|
|
2774
|
+
*/
|
|
2775
|
+
strictJsonSchema: z2.boolean().optional()
|
|
1130
2776
|
});
|
|
1131
|
-
var defaultOpenAICompatibleErrorStructure = {
|
|
1132
|
-
errorSchema: openaiCompatibleErrorDataSchema,
|
|
1133
|
-
errorToMessage: (data) => data.error.message
|
|
1134
|
-
};
|
|
1135
2777
|
function prepareTools({
|
|
1136
2778
|
tools,
|
|
1137
2779
|
toolChoice
|
|
@@ -1143,15 +2785,19 @@ function prepareTools({
|
|
|
1143
2785
|
}
|
|
1144
2786
|
const openaiCompatTools = [];
|
|
1145
2787
|
for (const tool of tools) {
|
|
1146
|
-
if (tool.type === "provider
|
|
1147
|
-
toolWarnings.push({
|
|
2788
|
+
if (tool.type === "provider") {
|
|
2789
|
+
toolWarnings.push({
|
|
2790
|
+
type: "unsupported",
|
|
2791
|
+
feature: `provider-defined tool ${tool.id}`
|
|
2792
|
+
});
|
|
1148
2793
|
} else {
|
|
1149
2794
|
openaiCompatTools.push({
|
|
1150
2795
|
type: "function",
|
|
1151
2796
|
function: {
|
|
1152
2797
|
name: tool.name,
|
|
1153
2798
|
description: tool.description,
|
|
1154
|
-
parameters: tool.inputSchema
|
|
2799
|
+
parameters: tool.inputSchema,
|
|
2800
|
+
...tool.strict != null ? { strict: tool.strict } : {}
|
|
1155
2801
|
}
|
|
1156
2802
|
});
|
|
1157
2803
|
}
|
|
@@ -1185,16 +2831,16 @@ function prepareTools({
|
|
|
1185
2831
|
var OpenAICompatibleChatLanguageModel = class {
|
|
1186
2832
|
// type inferred via constructor
|
|
1187
2833
|
constructor(modelId, config) {
|
|
1188
|
-
this.specificationVersion = "
|
|
1189
|
-
var
|
|
2834
|
+
this.specificationVersion = "v3";
|
|
2835
|
+
var _a16, _b16;
|
|
1190
2836
|
this.modelId = modelId;
|
|
1191
2837
|
this.config = config;
|
|
1192
|
-
const errorStructure = (
|
|
2838
|
+
const errorStructure = (_a16 = config.errorStructure) != null ? _a16 : defaultOpenAICompatibleErrorStructure;
|
|
1193
2839
|
this.chunkSchema = createOpenAICompatibleChatChunkSchema(
|
|
1194
2840
|
errorStructure.errorSchema
|
|
1195
2841
|
);
|
|
1196
2842
|
this.failedResponseHandler = createJsonErrorResponseHandler(errorStructure);
|
|
1197
|
-
this.supportsStructuredOutputs = (
|
|
2843
|
+
this.supportsStructuredOutputs = (_b16 = config.supportsStructuredOutputs) != null ? _b16 : false;
|
|
1198
2844
|
}
|
|
1199
2845
|
get provider() {
|
|
1200
2846
|
return this.config.provider;
|
|
@@ -1203,8 +2849,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1203
2849
|
return this.config.provider.split(".")[0].trim();
|
|
1204
2850
|
}
|
|
1205
2851
|
get supportedUrls() {
|
|
1206
|
-
var
|
|
1207
|
-
return (_c = (
|
|
2852
|
+
var _a16, _b16, _c;
|
|
2853
|
+
return (_c = (_b16 = (_a16 = this.config).supportedUrls) == null ? void 0 : _b16.call(_a16)) != null ? _c : {};
|
|
2854
|
+
}
|
|
2855
|
+
transformRequestBody(args) {
|
|
2856
|
+
var _a16, _b16, _c;
|
|
2857
|
+
return (_c = (_b16 = (_a16 = this.config).transformRequestBody) == null ? void 0 : _b16.call(_a16, args)) != null ? _c : args;
|
|
1208
2858
|
}
|
|
1209
2859
|
async getArgs({
|
|
1210
2860
|
prompt,
|
|
@@ -1221,27 +2871,45 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1221
2871
|
toolChoice,
|
|
1222
2872
|
tools
|
|
1223
2873
|
}) {
|
|
1224
|
-
var
|
|
2874
|
+
var _a16, _b16, _c, _d, _e;
|
|
1225
2875
|
const warnings = [];
|
|
2876
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
2877
|
+
provider: "openai-compatible",
|
|
2878
|
+
providerOptions,
|
|
2879
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2880
|
+
});
|
|
2881
|
+
if (deprecatedOptions != null) {
|
|
2882
|
+
warnings.push({
|
|
2883
|
+
type: "other",
|
|
2884
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
2885
|
+
});
|
|
2886
|
+
}
|
|
1226
2887
|
const compatibleOptions = Object.assign(
|
|
1227
|
-
|
|
1228
|
-
|
|
2888
|
+
deprecatedOptions != null ? deprecatedOptions : {},
|
|
2889
|
+
(_a16 = await parseProviderOptions({
|
|
2890
|
+
provider: "openaiCompatible",
|
|
1229
2891
|
providerOptions,
|
|
1230
|
-
schema:
|
|
1231
|
-
})) != null ?
|
|
1232
|
-
(
|
|
2892
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2893
|
+
})) != null ? _a16 : {},
|
|
2894
|
+
(_b16 = await parseProviderOptions({
|
|
1233
2895
|
provider: this.providerOptionsName,
|
|
1234
2896
|
providerOptions,
|
|
1235
|
-
schema:
|
|
1236
|
-
})) != null ?
|
|
2897
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2898
|
+
})) != null ? _b16 : {},
|
|
2899
|
+
(_c = await parseProviderOptions({
|
|
2900
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
2901
|
+
providerOptions,
|
|
2902
|
+
schema: openaiCompatibleLanguageModelChatOptions
|
|
2903
|
+
})) != null ? _c : {}
|
|
1237
2904
|
);
|
|
2905
|
+
const strictJsonSchema = (_d = compatibleOptions == null ? void 0 : compatibleOptions.strictJsonSchema) != null ? _d : true;
|
|
1238
2906
|
if (topK != null) {
|
|
1239
|
-
warnings.push({ type: "unsupported
|
|
2907
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1240
2908
|
}
|
|
1241
2909
|
if ((responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !this.supportsStructuredOutputs) {
|
|
1242
2910
|
warnings.push({
|
|
1243
|
-
type: "unsupported
|
|
1244
|
-
|
|
2911
|
+
type: "unsupported",
|
|
2912
|
+
feature: "responseFormat",
|
|
1245
2913
|
details: "JSON response format schema is only supported with structuredOutputs"
|
|
1246
2914
|
});
|
|
1247
2915
|
}
|
|
@@ -1253,7 +2921,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1253
2921
|
tools,
|
|
1254
2922
|
toolChoice
|
|
1255
2923
|
});
|
|
2924
|
+
const metadataKey = resolveProviderOptionsKey(
|
|
2925
|
+
this.providerOptionsName,
|
|
2926
|
+
providerOptions
|
|
2927
|
+
);
|
|
1256
2928
|
return {
|
|
2929
|
+
metadataKey,
|
|
1257
2930
|
args: {
|
|
1258
2931
|
// model id:
|
|
1259
2932
|
model: this.modelId,
|
|
@@ -1269,17 +2942,21 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1269
2942
|
type: "json_schema",
|
|
1270
2943
|
json_schema: {
|
|
1271
2944
|
schema: responseFormat.schema,
|
|
1272
|
-
|
|
2945
|
+
strict: strictJsonSchema,
|
|
2946
|
+
name: (_e = responseFormat.name) != null ? _e : "response",
|
|
1273
2947
|
description: responseFormat.description
|
|
1274
2948
|
}
|
|
1275
2949
|
} : { type: "json_object" } : void 0,
|
|
1276
2950
|
stop: stopSequences,
|
|
1277
2951
|
seed,
|
|
1278
2952
|
...Object.fromEntries(
|
|
1279
|
-
Object.entries(
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
2953
|
+
Object.entries({
|
|
2954
|
+
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
2955
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)]
|
|
2956
|
+
}).filter(
|
|
2957
|
+
([key]) => !Object.keys(
|
|
2958
|
+
openaiCompatibleLanguageModelChatOptions.shape
|
|
2959
|
+
).includes(key)
|
|
1283
2960
|
)
|
|
1284
2961
|
),
|
|
1285
2962
|
reasoning_effort: compatibleOptions.reasoningEffort,
|
|
@@ -1294,9 +2971,10 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1294
2971
|
};
|
|
1295
2972
|
}
|
|
1296
2973
|
async doGenerate(options) {
|
|
1297
|
-
var
|
|
1298
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
1299
|
-
const
|
|
2974
|
+
var _a16, _b16, _c, _d, _e, _f, _g, _h;
|
|
2975
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
2976
|
+
const transformedBody = this.transformRequestBody(args);
|
|
2977
|
+
const body = JSON.stringify(transformedBody);
|
|
1300
2978
|
const {
|
|
1301
2979
|
responseHeaders,
|
|
1302
2980
|
value: responseBody,
|
|
@@ -1307,7 +2985,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1307
2985
|
modelId: this.modelId
|
|
1308
2986
|
}),
|
|
1309
2987
|
headers: combineHeaders(this.config.headers(), options.headers),
|
|
1310
|
-
body:
|
|
2988
|
+
body: transformedBody,
|
|
1311
2989
|
failedResponseHandler: this.failedResponseHandler,
|
|
1312
2990
|
successfulResponseHandler: createJsonResponseHandler(
|
|
1313
2991
|
OpenAICompatibleChatResponseSchema
|
|
@@ -1321,7 +2999,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1321
2999
|
if (text != null && text.length > 0) {
|
|
1322
3000
|
content.push({ type: "text", text });
|
|
1323
3001
|
}
|
|
1324
|
-
const reasoning = (
|
|
3002
|
+
const reasoning = (_a16 = choice.message.reasoning_content) != null ? _a16 : choice.message.reasoning;
|
|
1325
3003
|
if (reasoning != null && reasoning.length > 0) {
|
|
1326
3004
|
content.push({
|
|
1327
3005
|
type: "reasoning",
|
|
@@ -1330,37 +3008,40 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1330
3008
|
}
|
|
1331
3009
|
if (choice.message.tool_calls != null) {
|
|
1332
3010
|
for (const toolCall of choice.message.tool_calls) {
|
|
3011
|
+
const thoughtSignature = (_c = (_b16 = toolCall.extra_content) == null ? void 0 : _b16.google) == null ? void 0 : _c.thought_signature;
|
|
1333
3012
|
content.push({
|
|
1334
3013
|
type: "tool-call",
|
|
1335
|
-
toolCallId: (
|
|
3014
|
+
toolCallId: (_d = toolCall.id) != null ? _d : generateId(),
|
|
1336
3015
|
toolName: toolCall.function.name,
|
|
1337
|
-
input: toolCall.function.arguments
|
|
3016
|
+
input: toolCall.function.arguments,
|
|
3017
|
+
...thoughtSignature ? {
|
|
3018
|
+
providerMetadata: {
|
|
3019
|
+
[metadataKey]: { thoughtSignature }
|
|
3020
|
+
}
|
|
3021
|
+
} : {}
|
|
1338
3022
|
});
|
|
1339
3023
|
}
|
|
1340
3024
|
}
|
|
1341
3025
|
const providerMetadata = {
|
|
1342
|
-
[
|
|
1343
|
-
...await ((
|
|
3026
|
+
[metadataKey]: {},
|
|
3027
|
+
...await ((_f = (_e = this.config.metadataExtractor) == null ? void 0 : _e.extractMetadata) == null ? void 0 : _f.call(_e, {
|
|
1344
3028
|
parsedBody: rawResponse
|
|
1345
3029
|
}))
|
|
1346
3030
|
};
|
|
1347
|
-
const completionTokenDetails = (
|
|
3031
|
+
const completionTokenDetails = (_g = responseBody.usage) == null ? void 0 : _g.completion_tokens_details;
|
|
1348
3032
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens) != null) {
|
|
1349
|
-
providerMetadata[
|
|
3033
|
+
providerMetadata[metadataKey].acceptedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.accepted_prediction_tokens;
|
|
1350
3034
|
}
|
|
1351
3035
|
if ((completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens) != null) {
|
|
1352
|
-
providerMetadata[
|
|
3036
|
+
providerMetadata[metadataKey].rejectedPredictionTokens = completionTokenDetails == null ? void 0 : completionTokenDetails.rejected_prediction_tokens;
|
|
1353
3037
|
}
|
|
1354
3038
|
return {
|
|
1355
3039
|
content,
|
|
1356
|
-
finishReason:
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
outputTokens: (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens) != null ? _i : void 0,
|
|
1360
|
-
totalTokens: (_k = (_j = responseBody.usage) == null ? void 0 : _j.total_tokens) != null ? _k : void 0,
|
|
1361
|
-
reasoningTokens: (_n = (_m = (_l = responseBody.usage) == null ? void 0 : _l.completion_tokens_details) == null ? void 0 : _m.reasoning_tokens) != null ? _n : void 0,
|
|
1362
|
-
cachedInputTokens: (_q = (_p = (_o = responseBody.usage) == null ? void 0 : _o.prompt_tokens_details) == null ? void 0 : _p.cached_tokens) != null ? _q : void 0
|
|
3040
|
+
finishReason: {
|
|
3041
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
3042
|
+
raw: (_h = choice.finish_reason) != null ? _h : void 0
|
|
1363
3043
|
},
|
|
3044
|
+
usage: convertOpenAICompatibleChatUsage(responseBody.usage),
|
|
1364
3045
|
providerMetadata,
|
|
1365
3046
|
request: { body },
|
|
1366
3047
|
response: {
|
|
@@ -1372,15 +3053,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1372
3053
|
};
|
|
1373
3054
|
}
|
|
1374
3055
|
async doStream(options) {
|
|
1375
|
-
var
|
|
1376
|
-
const { args, warnings } = await this.getArgs({ ...options });
|
|
1377
|
-
const body = {
|
|
3056
|
+
var _a16;
|
|
3057
|
+
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
3058
|
+
const body = this.transformRequestBody({
|
|
1378
3059
|
...args,
|
|
1379
3060
|
stream: true,
|
|
1380
3061
|
// only include stream_options when in strict compatibility mode:
|
|
1381
3062
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
1382
|
-
};
|
|
1383
|
-
const metadataExtractor = (
|
|
3063
|
+
});
|
|
3064
|
+
const metadataExtractor = (_a16 = this.config.metadataExtractor) == null ? void 0 : _a16.createStreamExtractor();
|
|
1384
3065
|
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1385
3066
|
url: this.config.url({
|
|
1386
3067
|
path: "/chat/completions",
|
|
@@ -1396,22 +3077,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1396
3077
|
fetch: this.config.fetch
|
|
1397
3078
|
});
|
|
1398
3079
|
const toolCalls = [];
|
|
1399
|
-
let finishReason =
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
completionTokensDetails: {
|
|
1403
|
-
reasoningTokens: void 0,
|
|
1404
|
-
acceptedPredictionTokens: void 0,
|
|
1405
|
-
rejectedPredictionTokens: void 0
|
|
1406
|
-
},
|
|
1407
|
-
promptTokens: void 0,
|
|
1408
|
-
promptTokensDetails: {
|
|
1409
|
-
cachedTokens: void 0
|
|
1410
|
-
},
|
|
1411
|
-
totalTokens: void 0
|
|
3080
|
+
let finishReason = {
|
|
3081
|
+
unified: "other",
|
|
3082
|
+
raw: void 0
|
|
1412
3083
|
};
|
|
3084
|
+
let usage = void 0;
|
|
1413
3085
|
let isFirstChunk = true;
|
|
1414
|
-
const providerOptionsName =
|
|
3086
|
+
const providerOptionsName = metadataKey;
|
|
1415
3087
|
let isActiveReasoning = false;
|
|
1416
3088
|
let isActiveText = false;
|
|
1417
3089
|
return {
|
|
@@ -1420,24 +3092,26 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1420
3092
|
start(controller) {
|
|
1421
3093
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1422
3094
|
},
|
|
1423
|
-
// TODO we lost type safety on Chunk, most likely due to the error schema. MUST FIX
|
|
1424
3095
|
transform(chunk, controller) {
|
|
1425
|
-
var _a22,
|
|
3096
|
+
var _a22, _b16, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r;
|
|
1426
3097
|
if (options.includeRawChunks) {
|
|
1427
3098
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1428
3099
|
}
|
|
1429
3100
|
if (!chunk.success) {
|
|
1430
|
-
finishReason = "error";
|
|
3101
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
1431
3102
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
1432
3103
|
return;
|
|
1433
3104
|
}
|
|
1434
|
-
const value = chunk.value;
|
|
1435
3105
|
metadataExtractor == null ? void 0 : metadataExtractor.processChunk(chunk.rawValue);
|
|
1436
|
-
if ("error" in value) {
|
|
1437
|
-
finishReason = "error";
|
|
1438
|
-
controller.enqueue({
|
|
3106
|
+
if ("error" in chunk.value) {
|
|
3107
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
3108
|
+
controller.enqueue({
|
|
3109
|
+
type: "error",
|
|
3110
|
+
error: chunk.value.error.message
|
|
3111
|
+
});
|
|
1439
3112
|
return;
|
|
1440
3113
|
}
|
|
3114
|
+
const value = chunk.value;
|
|
1441
3115
|
if (isFirstChunk) {
|
|
1442
3116
|
isFirstChunk = false;
|
|
1443
3117
|
controller.enqueue({
|
|
@@ -1446,40 +3120,20 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1446
3120
|
});
|
|
1447
3121
|
}
|
|
1448
3122
|
if (value.usage != null) {
|
|
1449
|
-
|
|
1450
|
-
prompt_tokens,
|
|
1451
|
-
completion_tokens,
|
|
1452
|
-
total_tokens,
|
|
1453
|
-
prompt_tokens_details,
|
|
1454
|
-
completion_tokens_details
|
|
1455
|
-
} = value.usage;
|
|
1456
|
-
usage.promptTokens = prompt_tokens != null ? prompt_tokens : void 0;
|
|
1457
|
-
usage.completionTokens = completion_tokens != null ? completion_tokens : void 0;
|
|
1458
|
-
usage.totalTokens = total_tokens != null ? total_tokens : void 0;
|
|
1459
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens) != null) {
|
|
1460
|
-
usage.completionTokensDetails.reasoningTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.reasoning_tokens;
|
|
1461
|
-
}
|
|
1462
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens) != null) {
|
|
1463
|
-
usage.completionTokensDetails.acceptedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.accepted_prediction_tokens;
|
|
1464
|
-
}
|
|
1465
|
-
if ((completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens) != null) {
|
|
1466
|
-
usage.completionTokensDetails.rejectedPredictionTokens = completion_tokens_details == null ? void 0 : completion_tokens_details.rejected_prediction_tokens;
|
|
1467
|
-
}
|
|
1468
|
-
if ((prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens) != null) {
|
|
1469
|
-
usage.promptTokensDetails.cachedTokens = prompt_tokens_details == null ? void 0 : prompt_tokens_details.cached_tokens;
|
|
1470
|
-
}
|
|
3123
|
+
usage = value.usage;
|
|
1471
3124
|
}
|
|
1472
3125
|
const choice = value.choices[0];
|
|
1473
3126
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
1474
|
-
finishReason =
|
|
1475
|
-
choice.finish_reason
|
|
1476
|
-
|
|
3127
|
+
finishReason = {
|
|
3128
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
3129
|
+
raw: (_a22 = choice.finish_reason) != null ? _a22 : void 0
|
|
3130
|
+
};
|
|
1477
3131
|
}
|
|
1478
3132
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
1479
3133
|
return;
|
|
1480
3134
|
}
|
|
1481
3135
|
const delta = choice.delta;
|
|
1482
|
-
const reasoningContent = (
|
|
3136
|
+
const reasoningContent = (_b16 = delta.reasoning_content) != null ? _b16 : delta.reasoning;
|
|
1483
3137
|
if (reasoningContent) {
|
|
1484
3138
|
if (!isActiveReasoning) {
|
|
1485
3139
|
controller.enqueue({
|
|
@@ -1495,6 +3149,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1495
3149
|
});
|
|
1496
3150
|
}
|
|
1497
3151
|
if (delta.content) {
|
|
3152
|
+
if (isActiveReasoning) {
|
|
3153
|
+
controller.enqueue({
|
|
3154
|
+
type: "reasoning-end",
|
|
3155
|
+
id: "reasoning-0"
|
|
3156
|
+
});
|
|
3157
|
+
isActiveReasoning = false;
|
|
3158
|
+
}
|
|
1498
3159
|
if (!isActiveText) {
|
|
1499
3160
|
controller.enqueue({ type: "text-start", id: "txt-0" });
|
|
1500
3161
|
isActiveText = true;
|
|
@@ -1506,8 +3167,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1506
3167
|
});
|
|
1507
3168
|
}
|
|
1508
3169
|
if (delta.tool_calls != null) {
|
|
3170
|
+
if (isActiveReasoning) {
|
|
3171
|
+
controller.enqueue({
|
|
3172
|
+
type: "reasoning-end",
|
|
3173
|
+
id: "reasoning-0"
|
|
3174
|
+
});
|
|
3175
|
+
isActiveReasoning = false;
|
|
3176
|
+
}
|
|
1509
3177
|
for (const toolCallDelta of delta.tool_calls) {
|
|
1510
|
-
const index = toolCallDelta.index;
|
|
3178
|
+
const index = (_c = toolCallDelta.index) != null ? _c : toolCalls.length;
|
|
1511
3179
|
if (toolCalls[index] == null) {
|
|
1512
3180
|
if (toolCallDelta.id == null) {
|
|
1513
3181
|
throw new InvalidResponseDataError({
|
|
@@ -1515,7 +3183,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1515
3183
|
message: `Expected 'id' to be a string.`
|
|
1516
3184
|
});
|
|
1517
3185
|
}
|
|
1518
|
-
if (((
|
|
3186
|
+
if (((_d = toolCallDelta.function) == null ? void 0 : _d.name) == null) {
|
|
1519
3187
|
throw new InvalidResponseDataError({
|
|
1520
3188
|
data: toolCallDelta,
|
|
1521
3189
|
message: `Expected 'function.name' to be a string.`
|
|
@@ -1531,12 +3199,13 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1531
3199
|
type: "function",
|
|
1532
3200
|
function: {
|
|
1533
3201
|
name: toolCallDelta.function.name,
|
|
1534
|
-
arguments: (
|
|
3202
|
+
arguments: (_e = toolCallDelta.function.arguments) != null ? _e : ""
|
|
1535
3203
|
},
|
|
1536
|
-
hasFinished: false
|
|
3204
|
+
hasFinished: false,
|
|
3205
|
+
thoughtSignature: (_h = (_g = (_f = toolCallDelta.extra_content) == null ? void 0 : _f.google) == null ? void 0 : _g.thought_signature) != null ? _h : void 0
|
|
1537
3206
|
};
|
|
1538
3207
|
const toolCall2 = toolCalls[index];
|
|
1539
|
-
if (((
|
|
3208
|
+
if (((_i = toolCall2.function) == null ? void 0 : _i.name) != null && ((_j = toolCall2.function) == null ? void 0 : _j.arguments) != null) {
|
|
1540
3209
|
if (toolCall2.function.arguments.length > 0) {
|
|
1541
3210
|
controller.enqueue({
|
|
1542
3211
|
type: "tool-input-delta",
|
|
@@ -1551,9 +3220,16 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1551
3220
|
});
|
|
1552
3221
|
controller.enqueue({
|
|
1553
3222
|
type: "tool-call",
|
|
1554
|
-
toolCallId: (
|
|
3223
|
+
toolCallId: (_k = toolCall2.id) != null ? _k : generateId(),
|
|
1555
3224
|
toolName: toolCall2.function.name,
|
|
1556
|
-
input: toolCall2.function.arguments
|
|
3225
|
+
input: toolCall2.function.arguments,
|
|
3226
|
+
...toolCall2.thoughtSignature ? {
|
|
3227
|
+
providerMetadata: {
|
|
3228
|
+
[providerOptionsName]: {
|
|
3229
|
+
thoughtSignature: toolCall2.thoughtSignature
|
|
3230
|
+
}
|
|
3231
|
+
}
|
|
3232
|
+
} : {}
|
|
1557
3233
|
});
|
|
1558
3234
|
toolCall2.hasFinished = true;
|
|
1559
3235
|
}
|
|
@@ -1564,24 +3240,31 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1564
3240
|
if (toolCall.hasFinished) {
|
|
1565
3241
|
continue;
|
|
1566
3242
|
}
|
|
1567
|
-
if (((
|
|
1568
|
-
toolCall.function.arguments += (
|
|
3243
|
+
if (((_l = toolCallDelta.function) == null ? void 0 : _l.arguments) != null) {
|
|
3244
|
+
toolCall.function.arguments += (_n = (_m = toolCallDelta.function) == null ? void 0 : _m.arguments) != null ? _n : "";
|
|
1569
3245
|
}
|
|
1570
3246
|
controller.enqueue({
|
|
1571
3247
|
type: "tool-input-delta",
|
|
1572
3248
|
id: toolCall.id,
|
|
1573
|
-
delta: (
|
|
3249
|
+
delta: (_o = toolCallDelta.function.arguments) != null ? _o : ""
|
|
1574
3250
|
});
|
|
1575
|
-
if (((
|
|
3251
|
+
if (((_p = toolCall.function) == null ? void 0 : _p.name) != null && ((_q = toolCall.function) == null ? void 0 : _q.arguments) != null && isParsableJson(toolCall.function.arguments)) {
|
|
1576
3252
|
controller.enqueue({
|
|
1577
3253
|
type: "tool-input-end",
|
|
1578
3254
|
id: toolCall.id
|
|
1579
3255
|
});
|
|
1580
3256
|
controller.enqueue({
|
|
1581
3257
|
type: "tool-call",
|
|
1582
|
-
toolCallId: (
|
|
3258
|
+
toolCallId: (_r = toolCall.id) != null ? _r : generateId(),
|
|
1583
3259
|
toolName: toolCall.function.name,
|
|
1584
|
-
input: toolCall.function.arguments
|
|
3260
|
+
input: toolCall.function.arguments,
|
|
3261
|
+
...toolCall.thoughtSignature ? {
|
|
3262
|
+
providerMetadata: {
|
|
3263
|
+
[providerOptionsName]: {
|
|
3264
|
+
thoughtSignature: toolCall.thoughtSignature
|
|
3265
|
+
}
|
|
3266
|
+
}
|
|
3267
|
+
} : {}
|
|
1585
3268
|
});
|
|
1586
3269
|
toolCall.hasFinished = true;
|
|
1587
3270
|
}
|
|
@@ -1589,7 +3272,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1589
3272
|
}
|
|
1590
3273
|
},
|
|
1591
3274
|
flush(controller) {
|
|
1592
|
-
var _a22,
|
|
3275
|
+
var _a22, _b16, _c, _d, _e;
|
|
1593
3276
|
if (isActiveReasoning) {
|
|
1594
3277
|
controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
|
|
1595
3278
|
}
|
|
@@ -1607,29 +3290,30 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1607
3290
|
type: "tool-call",
|
|
1608
3291
|
toolCallId: (_a22 = toolCall.id) != null ? _a22 : generateId(),
|
|
1609
3292
|
toolName: toolCall.function.name,
|
|
1610
|
-
input: toolCall.function.arguments
|
|
3293
|
+
input: toolCall.function.arguments,
|
|
3294
|
+
...toolCall.thoughtSignature ? {
|
|
3295
|
+
providerMetadata: {
|
|
3296
|
+
[providerOptionsName]: {
|
|
3297
|
+
thoughtSignature: toolCall.thoughtSignature
|
|
3298
|
+
}
|
|
3299
|
+
}
|
|
3300
|
+
} : {}
|
|
1611
3301
|
});
|
|
1612
3302
|
}
|
|
1613
3303
|
const providerMetadata = {
|
|
1614
3304
|
[providerOptionsName]: {},
|
|
1615
3305
|
...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
|
|
1616
3306
|
};
|
|
1617
|
-
if (usage.
|
|
1618
|
-
providerMetadata[providerOptionsName].acceptedPredictionTokens = usage.
|
|
3307
|
+
if (((_b16 = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _b16.accepted_prediction_tokens) != null) {
|
|
3308
|
+
providerMetadata[providerOptionsName].acceptedPredictionTokens = (_c = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _c.accepted_prediction_tokens;
|
|
1619
3309
|
}
|
|
1620
|
-
if (usage.
|
|
1621
|
-
providerMetadata[providerOptionsName].rejectedPredictionTokens = usage.
|
|
3310
|
+
if (((_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.rejected_prediction_tokens) != null) {
|
|
3311
|
+
providerMetadata[providerOptionsName].rejectedPredictionTokens = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.rejected_prediction_tokens;
|
|
1622
3312
|
}
|
|
1623
3313
|
controller.enqueue({
|
|
1624
3314
|
type: "finish",
|
|
1625
3315
|
finishReason,
|
|
1626
|
-
usage:
|
|
1627
|
-
inputTokens: (_b15 = usage.promptTokens) != null ? _b15 : void 0,
|
|
1628
|
-
outputTokens: (_c = usage.completionTokens) != null ? _c : void 0,
|
|
1629
|
-
totalTokens: (_d = usage.totalTokens) != null ? _d : void 0,
|
|
1630
|
-
reasoningTokens: (_e = usage.completionTokensDetails.reasoningTokens) != null ? _e : void 0,
|
|
1631
|
-
cachedInputTokens: (_f = usage.promptTokensDetails.cachedTokens) != null ? _f : void 0
|
|
1632
|
-
},
|
|
3316
|
+
usage: convertOpenAICompatibleChatUsage(usage),
|
|
1633
3317
|
providerMetadata
|
|
1634
3318
|
});
|
|
1635
3319
|
}
|
|
@@ -1640,7 +3324,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
1640
3324
|
};
|
|
1641
3325
|
}
|
|
1642
3326
|
};
|
|
1643
|
-
var openaiCompatibleTokenUsageSchema = z3.
|
|
3327
|
+
var openaiCompatibleTokenUsageSchema = z3.looseObject({
|
|
1644
3328
|
prompt_tokens: z3.number().nullish(),
|
|
1645
3329
|
completion_tokens: z3.number().nullish(),
|
|
1646
3330
|
total_tokens: z3.number().nullish(),
|
|
@@ -1653,7 +3337,7 @@ var openaiCompatibleTokenUsageSchema = z3.object({
|
|
|
1653
3337
|
rejected_prediction_tokens: z3.number().nullish()
|
|
1654
3338
|
}).nullish()
|
|
1655
3339
|
}).nullish();
|
|
1656
|
-
var OpenAICompatibleChatResponseSchema = z3.
|
|
3340
|
+
var OpenAICompatibleChatResponseSchema = z3.looseObject({
|
|
1657
3341
|
id: z3.string().nullish(),
|
|
1658
3342
|
created: z3.number().nullish(),
|
|
1659
3343
|
model: z3.string().nullish(),
|
|
@@ -1670,7 +3354,13 @@ var OpenAICompatibleChatResponseSchema = z3.object({
|
|
|
1670
3354
|
function: z3.object({
|
|
1671
3355
|
name: z3.string(),
|
|
1672
3356
|
arguments: z3.string()
|
|
1673
|
-
})
|
|
3357
|
+
}),
|
|
3358
|
+
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
3359
|
+
extra_content: z3.object({
|
|
3360
|
+
google: z3.object({
|
|
3361
|
+
thought_signature: z3.string().nullish()
|
|
3362
|
+
}).nullish()
|
|
3363
|
+
}).nullish()
|
|
1674
3364
|
})
|
|
1675
3365
|
).nullish()
|
|
1676
3366
|
}),
|
|
@@ -1679,38 +3369,78 @@ var OpenAICompatibleChatResponseSchema = z3.object({
|
|
|
1679
3369
|
),
|
|
1680
3370
|
usage: openaiCompatibleTokenUsageSchema
|
|
1681
3371
|
});
|
|
1682
|
-
var
|
|
1683
|
-
z3.
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
z3.object({
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
z3.
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
3372
|
+
var chunkBaseSchema = z3.looseObject({
|
|
3373
|
+
id: z3.string().nullish(),
|
|
3374
|
+
created: z3.number().nullish(),
|
|
3375
|
+
model: z3.string().nullish(),
|
|
3376
|
+
choices: z3.array(
|
|
3377
|
+
z3.object({
|
|
3378
|
+
delta: z3.object({
|
|
3379
|
+
role: z3.enum(["assistant"]).nullish(),
|
|
3380
|
+
content: z3.string().nullish(),
|
|
3381
|
+
// Most openai-compatible models set `reasoning_content`, but some
|
|
3382
|
+
// providers serving `gpt-oss` set `reasoning`. See #7866
|
|
3383
|
+
reasoning_content: z3.string().nullish(),
|
|
3384
|
+
reasoning: z3.string().nullish(),
|
|
3385
|
+
tool_calls: z3.array(
|
|
3386
|
+
z3.object({
|
|
3387
|
+
index: z3.number().nullish(),
|
|
3388
|
+
//google does not send index
|
|
3389
|
+
id: z3.string().nullish(),
|
|
3390
|
+
function: z3.object({
|
|
3391
|
+
name: z3.string().nullish(),
|
|
3392
|
+
arguments: z3.string().nullish()
|
|
3393
|
+
}),
|
|
3394
|
+
// Support for Google Gemini thought signatures via OpenAI compatibility
|
|
3395
|
+
extra_content: z3.object({
|
|
3396
|
+
google: z3.object({
|
|
3397
|
+
thought_signature: z3.string().nullish()
|
|
3398
|
+
}).nullish()
|
|
3399
|
+
}).nullish()
|
|
3400
|
+
})
|
|
3401
|
+
).nullish()
|
|
3402
|
+
}).nullish(),
|
|
3403
|
+
finish_reason: z3.string().nullish()
|
|
3404
|
+
})
|
|
3405
|
+
),
|
|
3406
|
+
usage: openaiCompatibleTokenUsageSchema
|
|
3407
|
+
});
|
|
3408
|
+
var createOpenAICompatibleChatChunkSchema = (errorSchema) => z3.union([chunkBaseSchema, errorSchema]);
|
|
3409
|
+
function convertOpenAICompatibleCompletionUsage(usage) {
|
|
3410
|
+
var _a16, _b16;
|
|
3411
|
+
if (usage == null) {
|
|
3412
|
+
return {
|
|
3413
|
+
inputTokens: {
|
|
3414
|
+
total: void 0,
|
|
3415
|
+
noCache: void 0,
|
|
3416
|
+
cacheRead: void 0,
|
|
3417
|
+
cacheWrite: void 0
|
|
3418
|
+
},
|
|
3419
|
+
outputTokens: {
|
|
3420
|
+
total: void 0,
|
|
3421
|
+
text: void 0,
|
|
3422
|
+
reasoning: void 0
|
|
3423
|
+
},
|
|
3424
|
+
raw: void 0
|
|
3425
|
+
};
|
|
3426
|
+
}
|
|
3427
|
+
const promptTokens = (_a16 = usage.prompt_tokens) != null ? _a16 : 0;
|
|
3428
|
+
const completionTokens = (_b16 = usage.completion_tokens) != null ? _b16 : 0;
|
|
3429
|
+
return {
|
|
3430
|
+
inputTokens: {
|
|
3431
|
+
total: promptTokens,
|
|
3432
|
+
noCache: promptTokens,
|
|
3433
|
+
cacheRead: void 0,
|
|
3434
|
+
cacheWrite: void 0
|
|
3435
|
+
},
|
|
3436
|
+
outputTokens: {
|
|
3437
|
+
total: completionTokens,
|
|
3438
|
+
text: completionTokens,
|
|
3439
|
+
reasoning: void 0
|
|
3440
|
+
},
|
|
3441
|
+
raw: usage
|
|
3442
|
+
};
|
|
3443
|
+
}
|
|
1714
3444
|
function convertToOpenAICompatibleCompletionPrompt({
|
|
1715
3445
|
prompt,
|
|
1716
3446
|
user = "user",
|
|
@@ -1806,10 +3536,10 @@ function mapOpenAICompatibleFinishReason2(finishReason) {
|
|
|
1806
3536
|
case "tool_calls":
|
|
1807
3537
|
return "tool-calls";
|
|
1808
3538
|
default:
|
|
1809
|
-
return "
|
|
3539
|
+
return "other";
|
|
1810
3540
|
}
|
|
1811
3541
|
}
|
|
1812
|
-
var
|
|
3542
|
+
var openaiCompatibleLanguageModelCompletionOptions = z42.object({
|
|
1813
3543
|
/**
|
|
1814
3544
|
* Echo back the prompt in addition to the completion.
|
|
1815
3545
|
*/
|
|
@@ -1834,11 +3564,11 @@ var openaiCompatibleCompletionProviderOptions = z42.object({
|
|
|
1834
3564
|
var OpenAICompatibleCompletionLanguageModel = class {
|
|
1835
3565
|
// type inferred via constructor
|
|
1836
3566
|
constructor(modelId, config) {
|
|
1837
|
-
this.specificationVersion = "
|
|
1838
|
-
var
|
|
3567
|
+
this.specificationVersion = "v3";
|
|
3568
|
+
var _a16;
|
|
1839
3569
|
this.modelId = modelId;
|
|
1840
3570
|
this.config = config;
|
|
1841
|
-
const errorStructure = (
|
|
3571
|
+
const errorStructure = (_a16 = config.errorStructure) != null ? _a16 : defaultOpenAICompatibleErrorStructure;
|
|
1842
3572
|
this.chunkSchema = createOpenAICompatibleCompletionChunkSchema(
|
|
1843
3573
|
errorStructure.errorSchema
|
|
1844
3574
|
);
|
|
@@ -1851,8 +3581,8 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1851
3581
|
return this.config.provider.split(".")[0].trim();
|
|
1852
3582
|
}
|
|
1853
3583
|
get supportedUrls() {
|
|
1854
|
-
var
|
|
1855
|
-
return (_c = (
|
|
3584
|
+
var _a16, _b16, _c;
|
|
3585
|
+
return (_c = (_b16 = (_a16 = this.config).supportedUrls) == null ? void 0 : _b16.call(_a16)) != null ? _c : {};
|
|
1856
3586
|
}
|
|
1857
3587
|
async getArgs({
|
|
1858
3588
|
prompt,
|
|
@@ -1869,26 +3599,33 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1869
3599
|
tools,
|
|
1870
3600
|
toolChoice
|
|
1871
3601
|
}) {
|
|
1872
|
-
var
|
|
3602
|
+
var _a16, _b16;
|
|
1873
3603
|
const warnings = [];
|
|
1874
|
-
const completionOptions = (
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
3604
|
+
const completionOptions = Object.assign(
|
|
3605
|
+
(_a16 = await parseProviderOptions({
|
|
3606
|
+
provider: this.providerOptionsName,
|
|
3607
|
+
providerOptions,
|
|
3608
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
3609
|
+
})) != null ? _a16 : {},
|
|
3610
|
+
(_b16 = await parseProviderOptions({
|
|
3611
|
+
provider: toCamelCase(this.providerOptionsName),
|
|
3612
|
+
providerOptions,
|
|
3613
|
+
schema: openaiCompatibleLanguageModelCompletionOptions
|
|
3614
|
+
})) != null ? _b16 : {}
|
|
3615
|
+
);
|
|
1879
3616
|
if (topK != null) {
|
|
1880
|
-
warnings.push({ type: "unsupported
|
|
3617
|
+
warnings.push({ type: "unsupported", feature: "topK" });
|
|
1881
3618
|
}
|
|
1882
3619
|
if (tools == null ? void 0 : tools.length) {
|
|
1883
|
-
warnings.push({ type: "unsupported
|
|
3620
|
+
warnings.push({ type: "unsupported", feature: "tools" });
|
|
1884
3621
|
}
|
|
1885
3622
|
if (toolChoice != null) {
|
|
1886
|
-
warnings.push({ type: "unsupported
|
|
3623
|
+
warnings.push({ type: "unsupported", feature: "toolChoice" });
|
|
1887
3624
|
}
|
|
1888
3625
|
if (responseFormat != null && responseFormat.type !== "text") {
|
|
1889
3626
|
warnings.push({
|
|
1890
|
-
type: "unsupported
|
|
1891
|
-
|
|
3627
|
+
type: "unsupported",
|
|
3628
|
+
feature: "responseFormat",
|
|
1892
3629
|
details: "JSON response format is not supported."
|
|
1893
3630
|
});
|
|
1894
3631
|
}
|
|
@@ -1911,6 +3648,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1911
3648
|
presence_penalty: presencePenalty,
|
|
1912
3649
|
seed,
|
|
1913
3650
|
...providerOptions == null ? void 0 : providerOptions[this.providerOptionsName],
|
|
3651
|
+
...providerOptions == null ? void 0 : providerOptions[toCamelCase(this.providerOptionsName)],
|
|
1914
3652
|
// prompt:
|
|
1915
3653
|
prompt: completionPrompt,
|
|
1916
3654
|
// stop sequences:
|
|
@@ -1920,7 +3658,6 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1920
3658
|
};
|
|
1921
3659
|
}
|
|
1922
3660
|
async doGenerate(options) {
|
|
1923
|
-
var _a15, _b15, _c, _d, _e, _f;
|
|
1924
3661
|
const { args, warnings } = await this.getArgs(options);
|
|
1925
3662
|
const {
|
|
1926
3663
|
responseHeaders,
|
|
@@ -1947,12 +3684,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1947
3684
|
}
|
|
1948
3685
|
return {
|
|
1949
3686
|
content,
|
|
1950
|
-
usage:
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
3687
|
+
usage: convertOpenAICompatibleCompletionUsage(response.usage),
|
|
3688
|
+
finishReason: {
|
|
3689
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
3690
|
+
raw: choice.finish_reason
|
|
1954
3691
|
},
|
|
1955
|
-
finishReason: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
1956
3692
|
request: { body: args },
|
|
1957
3693
|
response: {
|
|
1958
3694
|
...getResponseMetadata2(response),
|
|
@@ -1984,12 +3720,11 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1984
3720
|
abortSignal: options.abortSignal,
|
|
1985
3721
|
fetch: this.config.fetch
|
|
1986
3722
|
});
|
|
1987
|
-
let finishReason =
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
outputTokens: void 0,
|
|
1991
|
-
totalTokens: void 0
|
|
3723
|
+
let finishReason = {
|
|
3724
|
+
unified: "other",
|
|
3725
|
+
raw: void 0
|
|
1992
3726
|
};
|
|
3727
|
+
let usage = void 0;
|
|
1993
3728
|
let isFirstChunk = true;
|
|
1994
3729
|
return {
|
|
1995
3730
|
stream: response.pipeThrough(
|
|
@@ -1998,18 +3733,18 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
1998
3733
|
controller.enqueue({ type: "stream-start", warnings });
|
|
1999
3734
|
},
|
|
2000
3735
|
transform(chunk, controller) {
|
|
2001
|
-
var
|
|
3736
|
+
var _a16;
|
|
2002
3737
|
if (options.includeRawChunks) {
|
|
2003
3738
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
2004
3739
|
}
|
|
2005
3740
|
if (!chunk.success) {
|
|
2006
|
-
finishReason = "error";
|
|
3741
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2007
3742
|
controller.enqueue({ type: "error", error: chunk.error });
|
|
2008
3743
|
return;
|
|
2009
3744
|
}
|
|
2010
3745
|
const value = chunk.value;
|
|
2011
3746
|
if ("error" in value) {
|
|
2012
|
-
finishReason = "error";
|
|
3747
|
+
finishReason = { unified: "error", raw: void 0 };
|
|
2013
3748
|
controller.enqueue({ type: "error", error: value.error });
|
|
2014
3749
|
return;
|
|
2015
3750
|
}
|
|
@@ -2025,15 +3760,14 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2025
3760
|
});
|
|
2026
3761
|
}
|
|
2027
3762
|
if (value.usage != null) {
|
|
2028
|
-
usage
|
|
2029
|
-
usage.outputTokens = (_b15 = value.usage.completion_tokens) != null ? _b15 : void 0;
|
|
2030
|
-
usage.totalTokens = (_c = value.usage.total_tokens) != null ? _c : void 0;
|
|
3763
|
+
usage = value.usage;
|
|
2031
3764
|
}
|
|
2032
3765
|
const choice = value.choices[0];
|
|
2033
3766
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
2034
|
-
finishReason =
|
|
2035
|
-
choice.finish_reason
|
|
2036
|
-
|
|
3767
|
+
finishReason = {
|
|
3768
|
+
unified: mapOpenAICompatibleFinishReason2(choice.finish_reason),
|
|
3769
|
+
raw: (_a16 = choice.finish_reason) != null ? _a16 : void 0
|
|
3770
|
+
};
|
|
2037
3771
|
}
|
|
2038
3772
|
if ((choice == null ? void 0 : choice.text) != null) {
|
|
2039
3773
|
controller.enqueue({
|
|
@@ -2050,7 +3784,7 @@ var OpenAICompatibleCompletionLanguageModel = class {
|
|
|
2050
3784
|
controller.enqueue({
|
|
2051
3785
|
type: "finish",
|
|
2052
3786
|
finishReason,
|
|
2053
|
-
usage
|
|
3787
|
+
usage: convertOpenAICompatibleCompletionUsage(usage)
|
|
2054
3788
|
});
|
|
2055
3789
|
}
|
|
2056
3790
|
})
|
|
@@ -2093,7 +3827,7 @@ var createOpenAICompatibleCompletionChunkSchema = (errorSchema) => z5.union([
|
|
|
2093
3827
|
}),
|
|
2094
3828
|
errorSchema
|
|
2095
3829
|
]);
|
|
2096
|
-
var
|
|
3830
|
+
var openaiCompatibleEmbeddingModelOptions = z6.object({
|
|
2097
3831
|
/**
|
|
2098
3832
|
* The number of dimensions the resulting output embeddings should have.
|
|
2099
3833
|
* Only supported in text-embedding-3 and later models.
|
|
@@ -2107,7 +3841,7 @@ var openaiCompatibleEmbeddingProviderOptions = z6.object({
|
|
|
2107
3841
|
});
|
|
2108
3842
|
var OpenAICompatibleEmbeddingModel = class {
|
|
2109
3843
|
constructor(modelId, config) {
|
|
2110
|
-
this.specificationVersion = "
|
|
3844
|
+
this.specificationVersion = "v3";
|
|
2111
3845
|
this.modelId = modelId;
|
|
2112
3846
|
this.config = config;
|
|
2113
3847
|
}
|
|
@@ -2115,12 +3849,12 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2115
3849
|
return this.config.provider;
|
|
2116
3850
|
}
|
|
2117
3851
|
get maxEmbeddingsPerCall() {
|
|
2118
|
-
var
|
|
2119
|
-
return (
|
|
3852
|
+
var _a16;
|
|
3853
|
+
return (_a16 = this.config.maxEmbeddingsPerCall) != null ? _a16 : 2048;
|
|
2120
3854
|
}
|
|
2121
3855
|
get supportsParallelCalls() {
|
|
2122
|
-
var
|
|
2123
|
-
return (
|
|
3856
|
+
var _a16;
|
|
3857
|
+
return (_a16 = this.config.supportsParallelCalls) != null ? _a16 : true;
|
|
2124
3858
|
}
|
|
2125
3859
|
get providerOptionsName() {
|
|
2126
3860
|
return this.config.provider.split(".")[0].trim();
|
|
@@ -2131,18 +3865,31 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2131
3865
|
abortSignal,
|
|
2132
3866
|
providerOptions
|
|
2133
3867
|
}) {
|
|
2134
|
-
var
|
|
3868
|
+
var _a16, _b16, _c;
|
|
3869
|
+
const warnings = [];
|
|
3870
|
+
const deprecatedOptions = await parseProviderOptions({
|
|
3871
|
+
provider: "openai-compatible",
|
|
3872
|
+
providerOptions,
|
|
3873
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3874
|
+
});
|
|
3875
|
+
if (deprecatedOptions != null) {
|
|
3876
|
+
warnings.push({
|
|
3877
|
+
type: "other",
|
|
3878
|
+
message: `The 'openai-compatible' key in providerOptions is deprecated. Use 'openaiCompatible' instead.`
|
|
3879
|
+
});
|
|
3880
|
+
}
|
|
2135
3881
|
const compatibleOptions = Object.assign(
|
|
2136
|
-
|
|
2137
|
-
|
|
3882
|
+
deprecatedOptions != null ? deprecatedOptions : {},
|
|
3883
|
+
(_a16 = await parseProviderOptions({
|
|
3884
|
+
provider: "openaiCompatible",
|
|
2138
3885
|
providerOptions,
|
|
2139
|
-
schema:
|
|
2140
|
-
})) != null ?
|
|
2141
|
-
(
|
|
3886
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3887
|
+
})) != null ? _a16 : {},
|
|
3888
|
+
(_b16 = await parseProviderOptions({
|
|
2142
3889
|
provider: this.providerOptionsName,
|
|
2143
3890
|
providerOptions,
|
|
2144
|
-
schema:
|
|
2145
|
-
})) != null ?
|
|
3891
|
+
schema: openaiCompatibleEmbeddingModelOptions
|
|
3892
|
+
})) != null ? _b16 : {}
|
|
2146
3893
|
);
|
|
2147
3894
|
if (values.length > this.maxEmbeddingsPerCall) {
|
|
2148
3895
|
throw new TooManyEmbeddingValuesForCallError({
|
|
@@ -2179,6 +3926,7 @@ var OpenAICompatibleEmbeddingModel = class {
|
|
|
2179
3926
|
fetch: this.config.fetch
|
|
2180
3927
|
});
|
|
2181
3928
|
return {
|
|
3929
|
+
warnings,
|
|
2182
3930
|
embeddings: response.data.map((item) => item.embedding),
|
|
2183
3931
|
usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
|
|
2184
3932
|
providerMetadata: response.providerMetadata,
|
|
@@ -2195,12 +3943,25 @@ var OpenAICompatibleImageModel = class {
|
|
|
2195
3943
|
constructor(modelId, config) {
|
|
2196
3944
|
this.modelId = modelId;
|
|
2197
3945
|
this.config = config;
|
|
2198
|
-
this.specificationVersion = "
|
|
3946
|
+
this.specificationVersion = "v3";
|
|
2199
3947
|
this.maxImagesPerCall = 10;
|
|
2200
3948
|
}
|
|
2201
3949
|
get provider() {
|
|
2202
3950
|
return this.config.provider;
|
|
2203
3951
|
}
|
|
3952
|
+
/**
|
|
3953
|
+
* The provider options key used to extract provider-specific options.
|
|
3954
|
+
*/
|
|
3955
|
+
get providerOptionsKey() {
|
|
3956
|
+
return this.config.provider.split(".")[0].trim();
|
|
3957
|
+
}
|
|
3958
|
+
// TODO: deprecate non-camelCase keys and remove in future major version
|
|
3959
|
+
getArgs(providerOptions) {
|
|
3960
|
+
return {
|
|
3961
|
+
...providerOptions[this.providerOptionsKey],
|
|
3962
|
+
...providerOptions[toCamelCase(this.providerOptionsKey)]
|
|
3963
|
+
};
|
|
3964
|
+
}
|
|
2204
3965
|
async doGenerate({
|
|
2205
3966
|
prompt,
|
|
2206
3967
|
n,
|
|
@@ -2209,21 +3970,59 @@ var OpenAICompatibleImageModel = class {
|
|
|
2209
3970
|
seed,
|
|
2210
3971
|
providerOptions,
|
|
2211
3972
|
headers,
|
|
2212
|
-
abortSignal
|
|
3973
|
+
abortSignal,
|
|
3974
|
+
files,
|
|
3975
|
+
mask
|
|
2213
3976
|
}) {
|
|
2214
|
-
var
|
|
3977
|
+
var _a16, _b16, _c, _d, _e;
|
|
2215
3978
|
const warnings = [];
|
|
2216
3979
|
if (aspectRatio != null) {
|
|
2217
3980
|
warnings.push({
|
|
2218
|
-
type: "unsupported
|
|
2219
|
-
|
|
3981
|
+
type: "unsupported",
|
|
3982
|
+
feature: "aspectRatio",
|
|
2220
3983
|
details: "This model does not support aspect ratio. Use `size` instead."
|
|
2221
3984
|
});
|
|
2222
3985
|
}
|
|
2223
3986
|
if (seed != null) {
|
|
2224
|
-
warnings.push({ type: "unsupported
|
|
3987
|
+
warnings.push({ type: "unsupported", feature: "seed" });
|
|
3988
|
+
}
|
|
3989
|
+
const currentDate = (_c = (_b16 = (_a16 = this.config._internal) == null ? void 0 : _a16.currentDate) == null ? void 0 : _b16.call(_a16)) != null ? _c : /* @__PURE__ */ new Date();
|
|
3990
|
+
const args = this.getArgs(providerOptions);
|
|
3991
|
+
if (files != null && files.length > 0) {
|
|
3992
|
+
const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
|
|
3993
|
+
url: this.config.url({
|
|
3994
|
+
path: "/images/edits",
|
|
3995
|
+
modelId: this.modelId
|
|
3996
|
+
}),
|
|
3997
|
+
headers: combineHeaders(this.config.headers(), headers),
|
|
3998
|
+
formData: convertToFormData({
|
|
3999
|
+
model: this.modelId,
|
|
4000
|
+
prompt,
|
|
4001
|
+
image: await Promise.all(files.map((file) => fileToBlob(file))),
|
|
4002
|
+
mask: mask != null ? await fileToBlob(mask) : void 0,
|
|
4003
|
+
n,
|
|
4004
|
+
size,
|
|
4005
|
+
...args
|
|
4006
|
+
}),
|
|
4007
|
+
failedResponseHandler: createJsonErrorResponseHandler(
|
|
4008
|
+
(_d = this.config.errorStructure) != null ? _d : defaultOpenAICompatibleErrorStructure
|
|
4009
|
+
),
|
|
4010
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
4011
|
+
openaiCompatibleImageResponseSchema
|
|
4012
|
+
),
|
|
4013
|
+
abortSignal,
|
|
4014
|
+
fetch: this.config.fetch
|
|
4015
|
+
});
|
|
4016
|
+
return {
|
|
4017
|
+
images: response2.data.map((item) => item.b64_json),
|
|
4018
|
+
warnings,
|
|
4019
|
+
response: {
|
|
4020
|
+
timestamp: currentDate,
|
|
4021
|
+
modelId: this.modelId,
|
|
4022
|
+
headers: responseHeaders2
|
|
4023
|
+
}
|
|
4024
|
+
};
|
|
2225
4025
|
}
|
|
2226
|
-
const currentDate = (_c = (_b15 = (_a15 = this.config._internal) == null ? void 0 : _a15.currentDate) == null ? void 0 : _b15.call(_a15)) != null ? _c : /* @__PURE__ */ new Date();
|
|
2227
4026
|
const { value: response, responseHeaders } = await postJsonToApi({
|
|
2228
4027
|
url: this.config.url({
|
|
2229
4028
|
path: "/images/generations",
|
|
@@ -2235,7 +4034,7 @@ var OpenAICompatibleImageModel = class {
|
|
|
2235
4034
|
prompt,
|
|
2236
4035
|
n,
|
|
2237
4036
|
size,
|
|
2238
|
-
...
|
|
4037
|
+
...args,
|
|
2239
4038
|
response_format: "b64_json"
|
|
2240
4039
|
},
|
|
2241
4040
|
failedResponseHandler: createJsonErrorResponseHandler(
|
|
@@ -2261,7 +4060,14 @@ var OpenAICompatibleImageModel = class {
|
|
|
2261
4060
|
var openaiCompatibleImageResponseSchema = z8.object({
|
|
2262
4061
|
data: z8.array(z8.object({ b64_json: z8.string() }))
|
|
2263
4062
|
});
|
|
2264
|
-
|
|
4063
|
+
async function fileToBlob(file) {
|
|
4064
|
+
if (file.type === "url") {
|
|
4065
|
+
return downloadBlob(file.url);
|
|
4066
|
+
}
|
|
4067
|
+
const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array(file.data);
|
|
4068
|
+
return new Blob([data], { type: file.mediaType });
|
|
4069
|
+
}
|
|
4070
|
+
var VERSION2 = true ? "2.0.41" : "0.0.0-test";
|
|
2265
4071
|
function createOpenAICompatible(options) {
|
|
2266
4072
|
const baseURL = withoutTrailingSlash(options.baseURL);
|
|
2267
4073
|
const providerName = options.name;
|
|
@@ -2286,7 +4092,9 @@ function createOpenAICompatible(options) {
|
|
|
2286
4092
|
const createChatModel = (modelId) => new OpenAICompatibleChatLanguageModel(modelId, {
|
|
2287
4093
|
...getCommonModelConfig("chat"),
|
|
2288
4094
|
includeUsage: options.includeUsage,
|
|
2289
|
-
supportsStructuredOutputs: options.supportsStructuredOutputs
|
|
4095
|
+
supportsStructuredOutputs: options.supportsStructuredOutputs,
|
|
4096
|
+
transformRequestBody: options.transformRequestBody,
|
|
4097
|
+
metadataExtractor: options.metadataExtractor
|
|
2290
4098
|
});
|
|
2291
4099
|
const createCompletionModel = (modelId) => new OpenAICompatibleCompletionLanguageModel(modelId, {
|
|
2292
4100
|
...getCommonModelConfig("completion"),
|
|
@@ -2297,9 +4105,11 @@ function createOpenAICompatible(options) {
|
|
|
2297
4105
|
});
|
|
2298
4106
|
const createImageModel = (modelId) => new OpenAICompatibleImageModel(modelId, getCommonModelConfig("image"));
|
|
2299
4107
|
const provider = (modelId) => createLanguageModel(modelId);
|
|
4108
|
+
provider.specificationVersion = "v3";
|
|
2300
4109
|
provider.languageModel = createLanguageModel;
|
|
2301
4110
|
provider.chatModel = createChatModel;
|
|
2302
4111
|
provider.completionModel = createCompletionModel;
|
|
4112
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
2303
4113
|
provider.textEmbeddingModel = createEmbeddingModel;
|
|
2304
4114
|
provider.imageModel = createImageModel;
|
|
2305
4115
|
return provider;
|