@kenkaiiii/gg-ai 5.57.0 → 5.57.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +145 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +145 -16
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -505,6 +505,108 @@ function normalizeRootForAnthropic(schema) {
|
|
|
505
505
|
return out;
|
|
506
506
|
}
|
|
507
507
|
|
|
508
|
+
// src/utils/strict-tool-schema.ts
|
|
509
|
+
var UnsupportedStrictSchemaError = class extends Error {
|
|
510
|
+
};
|
|
511
|
+
var UNSUPPORTED_KEYS = [
|
|
512
|
+
"$ref",
|
|
513
|
+
"$defs",
|
|
514
|
+
"definitions",
|
|
515
|
+
"allOf",
|
|
516
|
+
"oneOf",
|
|
517
|
+
"patternProperties",
|
|
518
|
+
"dependentSchemas",
|
|
519
|
+
"dependencies",
|
|
520
|
+
"unevaluatedProperties",
|
|
521
|
+
"propertyNames",
|
|
522
|
+
"contains",
|
|
523
|
+
"prefixItems",
|
|
524
|
+
"not",
|
|
525
|
+
"if",
|
|
526
|
+
"then",
|
|
527
|
+
"else"
|
|
528
|
+
];
|
|
529
|
+
function isSchemaObject(value) {
|
|
530
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
531
|
+
}
|
|
532
|
+
function isStructured(value) {
|
|
533
|
+
if (!isSchemaObject(value)) return false;
|
|
534
|
+
const types = typeof value.type === "string" ? [value.type] : Array.isArray(value.type) ? value.type : [];
|
|
535
|
+
return types.includes("object") || types.includes("array") || value.properties !== void 0 || value.items !== void 0;
|
|
536
|
+
}
|
|
537
|
+
function allowsNull(schema) {
|
|
538
|
+
if (!isSchemaObject(schema)) return false;
|
|
539
|
+
if (schema.type === "null" || Array.isArray(schema.type) && schema.type.includes("null"))
|
|
540
|
+
return true;
|
|
541
|
+
if (schema.const === null || Array.isArray(schema.enum) && schema.enum.includes(null))
|
|
542
|
+
return true;
|
|
543
|
+
return Array.isArray(schema.anyOf) && schema.anyOf.some((variant) => allowsNull(variant));
|
|
544
|
+
}
|
|
545
|
+
function makeNodeStrict(schema) {
|
|
546
|
+
if (!isSchemaObject(schema)) {
|
|
547
|
+
throw new UnsupportedStrictSchemaError("boolean schemas are unsupported");
|
|
548
|
+
}
|
|
549
|
+
for (const key of UNSUPPORTED_KEYS) {
|
|
550
|
+
if (schema[key] !== void 0) {
|
|
551
|
+
throw new UnsupportedStrictSchemaError(`${key} schemas are unsupported`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (schema.anyOf !== void 0) {
|
|
555
|
+
if (!Array.isArray(schema.anyOf) || schema.anyOf.length === 0) {
|
|
556
|
+
throw new UnsupportedStrictSchemaError("anyOf must contain at least one schema");
|
|
557
|
+
}
|
|
558
|
+
for (const variant of schema.anyOf) {
|
|
559
|
+
if (isStructured(variant)) {
|
|
560
|
+
throw new UnsupportedStrictSchemaError("object and array unions are unsupported");
|
|
561
|
+
}
|
|
562
|
+
makeNodeStrict(variant);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (schema.items !== void 0) {
|
|
566
|
+
if (Array.isArray(schema.items)) {
|
|
567
|
+
throw new UnsupportedStrictSchemaError("tuple schemas are unsupported");
|
|
568
|
+
}
|
|
569
|
+
makeNodeStrict(schema.items);
|
|
570
|
+
}
|
|
571
|
+
const isObjectSchema = schema.type === "object";
|
|
572
|
+
if (schema.properties !== void 0 && !isObjectSchema) {
|
|
573
|
+
throw new UnsupportedStrictSchemaError("properties require type object");
|
|
574
|
+
}
|
|
575
|
+
if (!isObjectSchema) return;
|
|
576
|
+
if (schema.additionalProperties !== void 0 && schema.additionalProperties !== false) {
|
|
577
|
+
throw new UnsupportedStrictSchemaError(
|
|
578
|
+
"schema-valued or true additionalProperties is unsupported"
|
|
579
|
+
);
|
|
580
|
+
}
|
|
581
|
+
if (schema.properties !== void 0 && !isSchemaObject(schema.properties)) {
|
|
582
|
+
throw new UnsupportedStrictSchemaError("object properties must be a schema map");
|
|
583
|
+
}
|
|
584
|
+
const properties = schema.properties ?? {};
|
|
585
|
+
const required = new Set(Array.isArray(schema.required) ? schema.required : []);
|
|
586
|
+
for (const [key, property] of Object.entries(properties)) {
|
|
587
|
+
makeNodeStrict(property);
|
|
588
|
+
if (!required.has(key) && !allowsNull(property)) {
|
|
589
|
+
properties[key] = { anyOf: [property, { type: "null" }] };
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
schema.required = Object.keys(properties);
|
|
593
|
+
schema.additionalProperties = false;
|
|
594
|
+
}
|
|
595
|
+
function supportsStrictToolSampling(provider) {
|
|
596
|
+
return provider === "openai";
|
|
597
|
+
}
|
|
598
|
+
function makeStrictToolSchema(schema) {
|
|
599
|
+
const cloned = structuredClone(schema);
|
|
600
|
+
if (!isSchemaObject(cloned)) {
|
|
601
|
+
throw new UnsupportedStrictSchemaError("root schema must be an object");
|
|
602
|
+
}
|
|
603
|
+
makeNodeStrict(cloned);
|
|
604
|
+
if (cloned.type !== "object") {
|
|
605
|
+
throw new UnsupportedStrictSchemaError("root schema must have type object");
|
|
606
|
+
}
|
|
607
|
+
return cloned;
|
|
608
|
+
}
|
|
609
|
+
|
|
508
610
|
// src/providers/reasoning-field.ts
|
|
509
611
|
var REASONING_FIELD_ALIASES = [
|
|
510
612
|
"reasoning_content",
|
|
@@ -1089,15 +1191,28 @@ function toOpenAIMessages(messages, options) {
|
|
|
1089
1191
|
}
|
|
1090
1192
|
return out;
|
|
1091
1193
|
}
|
|
1092
|
-
function toOpenAITools(tools) {
|
|
1093
|
-
return tools.map((tool) =>
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1194
|
+
function toOpenAITools(tools, opts) {
|
|
1195
|
+
return tools.map((tool) => {
|
|
1196
|
+
let parameters = resolveToolSchema(tool);
|
|
1197
|
+
let strict;
|
|
1198
|
+
if (opts?.strict) {
|
|
1199
|
+
try {
|
|
1200
|
+
parameters = makeStrictToolSchema(parameters);
|
|
1201
|
+
strict = true;
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
if (!(error instanceof UnsupportedStrictSchemaError)) throw error;
|
|
1204
|
+
}
|
|
1099
1205
|
}
|
|
1100
|
-
|
|
1206
|
+
return {
|
|
1207
|
+
type: "function",
|
|
1208
|
+
function: {
|
|
1209
|
+
name: tool.name,
|
|
1210
|
+
description: tool.description,
|
|
1211
|
+
parameters,
|
|
1212
|
+
...strict ? { strict } : {}
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
});
|
|
1101
1216
|
}
|
|
1102
1217
|
function toOpenAIToolChoice(choice) {
|
|
1103
1218
|
if (choice === "auto") return "auto";
|
|
@@ -1954,7 +2069,11 @@ async function* runStream2(options) {
|
|
|
1954
2069
|
...options.topP != null && !hasFixedKimiSampling ? { top_p: options.topP } : {},
|
|
1955
2070
|
...options.stop ? { stop: options.stop } : {},
|
|
1956
2071
|
...options.thinking && !usesThinkingParam && !isKimiK3 && !isKimiK27 && !isLocal ? { reasoning_effort: toOpenAIReasoningEffort(options.thinking, options.model) } : {},
|
|
1957
|
-
...options.tools?.length ? {
|
|
2072
|
+
...options.tools?.length ? {
|
|
2073
|
+
tools: toOpenAITools(options.tools, {
|
|
2074
|
+
strict: supportsStrictToolSampling(options.provider)
|
|
2075
|
+
})
|
|
2076
|
+
} : {},
|
|
1958
2077
|
...options.toolChoice && options.tools?.length ? { tool_choice: toOpenAIToolChoice(options.toolChoice) } : {},
|
|
1959
2078
|
...useStreaming ? { stream_options: { include_usage: true } } : {}
|
|
1960
2079
|
};
|
|
@@ -2909,13 +3028,23 @@ function toCodexInput(messages, options) {
|
|
|
2909
3028
|
return { system, input };
|
|
2910
3029
|
}
|
|
2911
3030
|
function toCodexTools(tools) {
|
|
2912
|
-
return tools.map((tool) =>
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
3031
|
+
return tools.map((tool) => {
|
|
3032
|
+
let parameters = resolveToolSchema(tool);
|
|
3033
|
+
let strict = null;
|
|
3034
|
+
try {
|
|
3035
|
+
parameters = makeStrictToolSchema(parameters);
|
|
3036
|
+
strict = true;
|
|
3037
|
+
} catch (error) {
|
|
3038
|
+
if (!(error instanceof UnsupportedStrictSchemaError)) throw error;
|
|
3039
|
+
}
|
|
3040
|
+
return {
|
|
3041
|
+
type: "function",
|
|
3042
|
+
name: tool.name,
|
|
3043
|
+
description: tool.description,
|
|
3044
|
+
parameters,
|
|
3045
|
+
strict
|
|
3046
|
+
};
|
|
3047
|
+
});
|
|
2919
3048
|
}
|
|
2920
3049
|
function parseCodexErrorBody(text, statusCode) {
|
|
2921
3050
|
if (!text) return {};
|