openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
112 lines (111 loc) • 3.04 kB
JavaScript
import { I as toJSONSchema } from "./json-schema-processors-CIhP-F53.mjs";
import { n as validateJsonSchemaValue, t as parseJsonSchemaIssuePath } from "./schema-validator-NvoOmaWr.mjs";
//#region src/plugins/config-schema.ts
function error(e) {
return {
success: false,
error: { issues: [{
path: [],
message: e
}] }
};
}
function cloneIssue(e) {
return {
path: e.path.filter((e) => {
const t = typeof e;
return t === "string" || t === "number";
}),
message: e.message
};
}
function safeParseRuntimeSchema(e, t) {
const n = e.safeParse(t);
if (n.success) return {
success: true,
data: n.data
};
return {
success: false,
error: { issues: n.error.issues.map(cloneIssue) }
};
}
function normalizeJsonSchema(e) {
if (Array.isArray(e)) return e.map((e) => normalizeJsonSchema(e));
if (!e || typeof e !== "object") return e;
const t = { ...e };
delete t.$schema;
for (const [e, n] of Object.entries(t)) t[e] = normalizeJsonSchema(n);
const n = t.propertyNames;
if (n && typeof n === "object" && !Array.isArray(n) && n.type === "string") delete t.propertyNames;
if (Array.isArray(t.required) && t.required.length === 0) delete t.required;
return t;
}
function safeParseJsonSchema(e, t, n) {
const r = validateJsonSchemaValue({
schema: e,
cacheKey: t,
value: n,
applyDefaults: true
});
if (r.ok) return {
success: true,
data: r.value
};
return {
success: false,
error: { issues: r.errors.map((e) => ({
path: parseJsonSchemaIssuePath(e.path),
message: e.message
})) }
};
}
/** Build a plugin config schema from JSON Schema with runtime validation/default support. */
function buildJsonPluginConfigSchema(e, t) {
return {
safeParse: t?.safeParse ?? ((n) => safeParseJsonSchema(e, t?.cacheKey ?? "plugin-config-schema:json", n)),
...t?.uiHints ? { uiHints: t.uiHints } : {},
jsonSchema: normalizeJsonSchema(e)
};
}
/** Build a plugin config schema from Zod, exporting JSON Schema when the Zod runtime supports it. */
function buildPluginConfigSchema(e, t) {
const n = t?.safeParse ?? ((t) => safeParseRuntimeSchema(e, t));
const r = "_zod" in e;
return {
safeParse: n,
...t?.uiHints ? { uiHints: t.uiHints } : {},
jsonSchema: r ? normalizeJsonSchema(toJSONSchema(e, {
target: "draft-07",
io: "input",
unrepresentable: "any"
})) : {
type: "object",
additionalProperties: true
}
};
}
/** Return a schema for plugins that intentionally accept no config keys. */
function emptyPluginConfigSchema() {
return {
safeParse(e) {
if (e === void 0) return {
success: true,
data: void 0
};
if (!e || typeof e !== "object" || Array.isArray(e)) return error("expected config object");
if (Object.keys(e).length > 0) return error("config must be empty");
return {
success: true,
data: e
};
},
jsonSchema: {
type: "object",
additionalProperties: false,
properties: {}
}
};
}
//#endregion
export { buildPluginConfigSchema as n, emptyPluginConfigSchema as r, buildJsonPluginConfigSchema as t };