@openclaw/ai 0.0.0 → 2026.7.1-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/LICENSE +21 -0
- package/README.md +27 -3
- package/dist/anthropic-B5gZQM5X.mjs +1383 -0
- package/dist/api-registry-BXYnCOIR.d.mts +33 -0
- package/dist/azure-openai-responses-DNoSk8Uy.mjs +141 -0
- package/dist/azure-openai-responses-client-compat-a_O_GVQV.mjs +41 -0
- package/dist/diagnostics-BaTA9eVl.d.mts +25 -0
- package/dist/diagnostics-COpOtRwq.mjs +36 -0
- package/dist/diagnostics.d.mts +2 -0
- package/dist/diagnostics.mjs +2 -0
- package/dist/env-api-keys-CtMlqaQ4.mjs +171 -0
- package/dist/event-stream-0nZeBKl2.d.mts +26 -0
- package/dist/event-stream-ReMmOTzX.mjs +65 -0
- package/dist/event-stream.d.mts +2 -0
- package/dist/event-stream.mjs +2 -0
- package/dist/github-copilot-headers-BsH5cqGj.mjs +48 -0
- package/dist/google-D6sIQ1bL.mjs +55 -0
- package/dist/google-shared-ZPSl2qTi.mjs +548 -0
- package/dist/google-vertex-rDGwkoZK.mjs +111 -0
- package/dist/hash-CHgqbJmD.mjs +16 -0
- package/dist/headers-B_e4-1J0.mjs +9 -0
- package/dist/host-4t713IeR.mjs +37 -0
- package/dist/index-BoTnz8cv.d.mts +74 -0
- package/dist/index.d.mts +69 -0
- package/dist/index.mjs +7 -0
- package/dist/internal/anthropic.d.mts +234 -0
- package/dist/internal/anthropic.mjs +4 -0
- package/dist/internal/openai.d.mts +244 -0
- package/dist/internal/openai.mjs +7 -0
- package/dist/internal/runtime.d.mts +245 -0
- package/dist/internal/runtime.mjs +176 -0
- package/dist/internal/shared.d.mts +48 -0
- package/dist/internal/shared.mjs +3 -0
- package/dist/json-parse-DzNSIQBq.mjs +134 -0
- package/dist/llm-request-activity-CehVkZP-.mjs +35 -0
- package/dist/mistral-CePVNdws.mjs +563 -0
- package/dist/model-utils-DgmOla96.mjs +69 -0
- package/dist/openai-chatgpt-jwt-DhAAzLkj.mjs +39 -0
- package/dist/openai-chatgpt-responses-DVC4Bk_A.mjs +1068 -0
- package/dist/openai-completions-B9QLIq2U.mjs +844 -0
- package/dist/openai-responses-B6LylGxM.mjs +136 -0
- package/dist/openai-responses-shared-sj2YUPYc.mjs +1944 -0
- package/dist/openai-tool-projection-BknoV11q.mjs +195 -0
- package/dist/providers.d.mts +11 -0
- package/dist/providers.mjs +109 -0
- package/dist/reasoning-tag-text-partitioner-axhAdUwg.mjs +394 -0
- package/dist/sanitize-unicode-BZiVbGwK.d.mts +24 -0
- package/dist/sanitize-unicode-DT5o51ur.mjs +26 -0
- package/dist/src-CZ503MYJ.mjs +99 -0
- package/dist/stream-CREqxHgU.mjs +74 -0
- package/dist/stream-first-event-timeout-RjWszj8c.mjs +106 -0
- package/dist/streaming-byte-guard-BrbkbwUu.mjs +46 -0
- package/dist/tool-schema-json-projection-BXtBc_mD.mjs +74 -0
- package/dist/transform-messages-BhGF_fF4.mjs +507 -0
- package/dist/types-BVVgDSdq.d.mts +1 -0
- package/dist/types-DRgdPqaZ.d.mts +587 -0
- package/dist/types.d.mts +6 -0
- package/dist/types.mjs +5 -0
- package/dist/validation-BDMWOr8d.d.mts +9 -0
- package/dist/validation-FrchoOlv.mjs +199 -0
- package/dist/validation.d.mts +2 -0
- package/dist/validation.mjs +2 -0
- package/npm-shrinkwrap.json +645 -0
- package/package.json +74 -2
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { Compile } from "typebox/compile";
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
//#region packages/llm-core/src/validation.ts
|
|
4
|
+
const validatorCache = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
const TYPEBOX_KIND = Symbol.for("TypeBox.Kind");
|
|
6
|
+
/** Maximum string length accepted for schema-gated JSON coercion. */
|
|
7
|
+
const MAX_JSON_COERCE_LENGTH = 64 * 1024;
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return typeof value === "object" && value !== null;
|
|
10
|
+
}
|
|
11
|
+
function isJsonSchemaObject(value) {
|
|
12
|
+
return isRecord(value);
|
|
13
|
+
}
|
|
14
|
+
function hasTypeBoxMetadata(schema) {
|
|
15
|
+
return isRecord(schema) && Object.getOwnPropertySymbols(schema).includes(TYPEBOX_KIND);
|
|
16
|
+
}
|
|
17
|
+
function getSchemaTypes(schema) {
|
|
18
|
+
if (typeof schema.type === "string") return [schema.type];
|
|
19
|
+
if (Array.isArray(schema.type)) return schema.type.filter((type) => typeof type === "string");
|
|
20
|
+
return [];
|
|
21
|
+
}
|
|
22
|
+
function matchesJsonType(value, type) {
|
|
23
|
+
switch (type) {
|
|
24
|
+
case "number": return typeof value === "number";
|
|
25
|
+
case "integer": return typeof value === "number" && Number.isInteger(value);
|
|
26
|
+
case "boolean": return typeof value === "boolean";
|
|
27
|
+
case "string": return typeof value === "string";
|
|
28
|
+
case "null": return value === null;
|
|
29
|
+
case "array": return Array.isArray(value);
|
|
30
|
+
case "object": return isRecord(value) && !Array.isArray(value);
|
|
31
|
+
default: return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isValidatorSchema(value) {
|
|
35
|
+
return isRecord(value);
|
|
36
|
+
}
|
|
37
|
+
const JSON_NUMBER_TOKEN_RE = /^[+-]?(?:(?:\d+\.?\d*)|(?:\.\d+))(?:e[+-]?\d+)?$/iu;
|
|
38
|
+
function parseJsonNumberString(value) {
|
|
39
|
+
const trimmed = value.trim();
|
|
40
|
+
if (!trimmed || !JSON_NUMBER_TOKEN_RE.test(trimmed)) return;
|
|
41
|
+
const parsed = Number(trimmed);
|
|
42
|
+
return Number.isFinite(parsed) ? parsed : void 0;
|
|
43
|
+
}
|
|
44
|
+
function parseJsonIntegerString(value) {
|
|
45
|
+
const parsed = parseJsonNumberString(value);
|
|
46
|
+
return parsed !== void 0 && Number.isSafeInteger(parsed) ? parsed : void 0;
|
|
47
|
+
}
|
|
48
|
+
function getSubSchemaValidator(schema) {
|
|
49
|
+
if (!isValidatorSchema(schema)) return;
|
|
50
|
+
try {
|
|
51
|
+
return getValidator(schema);
|
|
52
|
+
} catch {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function coercePrimitiveByType(value, type) {
|
|
57
|
+
switch (type) {
|
|
58
|
+
case "number":
|
|
59
|
+
if (value === null) return 0;
|
|
60
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
61
|
+
const parsed = parseJsonNumberString(value);
|
|
62
|
+
if (parsed !== void 0) return parsed;
|
|
63
|
+
}
|
|
64
|
+
if (typeof value === "boolean") return value ? 1 : 0;
|
|
65
|
+
return value;
|
|
66
|
+
case "integer":
|
|
67
|
+
if (value === null) return 0;
|
|
68
|
+
if (typeof value === "string" && value.trim() !== "") {
|
|
69
|
+
const parsed = parseJsonIntegerString(value);
|
|
70
|
+
if (parsed !== void 0) return parsed;
|
|
71
|
+
}
|
|
72
|
+
if (typeof value === "boolean") return value ? 1 : 0;
|
|
73
|
+
return value;
|
|
74
|
+
case "boolean":
|
|
75
|
+
if (value === null) return false;
|
|
76
|
+
if (typeof value === "string") {
|
|
77
|
+
if (value === "true") return true;
|
|
78
|
+
if (value === "false") return false;
|
|
79
|
+
}
|
|
80
|
+
if (typeof value === "number") {
|
|
81
|
+
if (value === 1) return true;
|
|
82
|
+
if (value === 0) return false;
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
case "string":
|
|
86
|
+
if (value === null) return "";
|
|
87
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
88
|
+
return value;
|
|
89
|
+
case "array":
|
|
90
|
+
if (typeof value === "string" && value.trim() !== "" && value.length <= MAX_JSON_COERCE_LENGTH) try {
|
|
91
|
+
const parsed = JSON.parse(value);
|
|
92
|
+
if (Array.isArray(parsed)) return parsed;
|
|
93
|
+
} catch {}
|
|
94
|
+
return value;
|
|
95
|
+
case "object":
|
|
96
|
+
if (typeof value === "string" && value.trim() !== "" && value.length <= MAX_JSON_COERCE_LENGTH) try {
|
|
97
|
+
const parsed = JSON.parse(value);
|
|
98
|
+
if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) return parsed;
|
|
99
|
+
} catch {}
|
|
100
|
+
return value;
|
|
101
|
+
case "null":
|
|
102
|
+
if (value === "" || value === 0 || value === false) return null;
|
|
103
|
+
return value;
|
|
104
|
+
default: return value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function applySchemaObjectCoercion(value, schema) {
|
|
108
|
+
const properties = schema.properties;
|
|
109
|
+
const definedKeys = new Set(properties ? Object.keys(properties) : []);
|
|
110
|
+
if (properties) {
|
|
111
|
+
for (const [key, propertySchema] of Object.entries(properties)) if (key in value) value[key] = coerceWithJsonSchema(value[key], propertySchema);
|
|
112
|
+
}
|
|
113
|
+
if (schema.additionalProperties && isJsonSchemaObject(schema.additionalProperties)) {
|
|
114
|
+
for (const [key, propertyValue] of Object.entries(value)) if (!definedKeys.has(key)) value[key] = coerceWithJsonSchema(propertyValue, schema.additionalProperties);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function applySchemaArrayCoercion(value, schema) {
|
|
118
|
+
if (Array.isArray(schema.items)) {
|
|
119
|
+
for (let index = 0; index < value.length; index++) {
|
|
120
|
+
const itemSchema = schema.items[index];
|
|
121
|
+
if (itemSchema) value[index] = coerceWithJsonSchema(value[index], itemSchema);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (isJsonSchemaObject(schema.items)) for (let index = 0; index < value.length; index++) value[index] = coerceWithJsonSchema(value[index], schema.items);
|
|
126
|
+
}
|
|
127
|
+
function coerceWithUnionSchema(value, schemas) {
|
|
128
|
+
if (value === null) {
|
|
129
|
+
for (const schema of schemas) if (getSchemaTypes(schema).includes("null")) {
|
|
130
|
+
const validator = getSubSchemaValidator(schema);
|
|
131
|
+
if (!validator || validator.Check(value)) return value;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
for (const schema of schemas) {
|
|
135
|
+
const coerced = coerceWithJsonSchema(structuredClone(value), schema);
|
|
136
|
+
if (getSubSchemaValidator(schema)?.Check(coerced)) return coerced;
|
|
137
|
+
}
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
function coerceWithJsonSchema(value, schema) {
|
|
141
|
+
let nextValue = value;
|
|
142
|
+
if (Array.isArray(schema.allOf)) for (const nested of schema.allOf) nextValue = coerceWithJsonSchema(nextValue, nested);
|
|
143
|
+
if (Array.isArray(schema.anyOf)) nextValue = coerceWithUnionSchema(nextValue, schema.anyOf);
|
|
144
|
+
if (Array.isArray(schema.oneOf)) nextValue = coerceWithUnionSchema(nextValue, schema.oneOf);
|
|
145
|
+
const schemaTypes = getSchemaTypes(schema);
|
|
146
|
+
const matchesUnionMember = schemaTypes.length > 1 && schemaTypes.some((schemaType) => matchesJsonType(nextValue, schemaType));
|
|
147
|
+
if (schemaTypes.length > 0 && !matchesUnionMember) for (const schemaType of schemaTypes) {
|
|
148
|
+
const candidate = coercePrimitiveByType(nextValue, schemaType);
|
|
149
|
+
if (candidate !== nextValue) {
|
|
150
|
+
nextValue = candidate;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (schemaTypes.includes("object") && isRecord(nextValue) && !Array.isArray(nextValue)) applySchemaObjectCoercion(nextValue, schema);
|
|
155
|
+
if (schemaTypes.includes("array") && Array.isArray(nextValue)) applySchemaArrayCoercion(nextValue, schema);
|
|
156
|
+
return nextValue;
|
|
157
|
+
}
|
|
158
|
+
function getValidator(schema) {
|
|
159
|
+
const key = schema;
|
|
160
|
+
const cached = validatorCache.get(key);
|
|
161
|
+
if (cached) return cached;
|
|
162
|
+
const validator = Compile(schema);
|
|
163
|
+
validatorCache.set(key, validator);
|
|
164
|
+
return validator;
|
|
165
|
+
}
|
|
166
|
+
function formatValidationPath(error) {
|
|
167
|
+
if (error.keyword === "required") {
|
|
168
|
+
const requiredProperty = error.params.requiredProperties?.[0];
|
|
169
|
+
if (requiredProperty) {
|
|
170
|
+
const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
171
|
+
return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return error.instancePath.replace(/^\//, "").replace(/\//g, ".") || "root";
|
|
175
|
+
}
|
|
176
|
+
/** Finds the target tool and validates/coerces a model-emitted tool call. */
|
|
177
|
+
function validateToolCall(tools, toolCall) {
|
|
178
|
+
const tool = tools.find((t) => t.name === toolCall.name);
|
|
179
|
+
if (!tool) throw new Error(`Tool "${toolCall.name}" not found`);
|
|
180
|
+
return validateToolArguments(tool, toolCall);
|
|
181
|
+
}
|
|
182
|
+
/** Validates tool arguments against TypeBox or plain JSON-schema parameters. */
|
|
183
|
+
function validateToolArguments(tool, toolCall) {
|
|
184
|
+
const args = structuredClone(toolCall.arguments);
|
|
185
|
+
Value.Convert(tool.parameters, args);
|
|
186
|
+
const validator = getValidator(tool.parameters);
|
|
187
|
+
if (!hasTypeBoxMetadata(tool.parameters) && isJsonSchemaObject(tool.parameters)) {
|
|
188
|
+
const coerced = coerceWithJsonSchema(args, tool.parameters);
|
|
189
|
+
if (coerced !== args) if (isRecord(args) && isRecord(coerced)) {
|
|
190
|
+
for (const key of Object.keys(args)) delete args[key];
|
|
191
|
+
Object.assign(args, coerced);
|
|
192
|
+
} else return validator.Check(coerced) ? coerced : args;
|
|
193
|
+
}
|
|
194
|
+
if (validator.Check(args)) return args;
|
|
195
|
+
const errors = validator.Errors(args).map((error) => ` - ${formatValidationPath(error)}: ${error.message}`).join("\n") || "Unknown validation error";
|
|
196
|
+
throw new Error(`Validation failed for tool "${toolCall.name}":\n${errors}\n\nReceived arguments:\n${JSON.stringify(toolCall.arguments, null, 2)}`);
|
|
197
|
+
}
|
|
198
|
+
//#endregion
|
|
199
|
+
export { validateToolCall as n, validateToolArguments as t };
|