openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
175 lines (174 loc) • 5.8 kB
JavaScript
import { I as toJSONSchema } from "./json-schema-processors-CIhP-F53.mjs";
import { Fn as object, Jn as string, Lt as boolean, Nt as array, Pn as number, Un as record, or as union } from "./schemas-Cy6abvsu.mjs";
import { n as ZodIssueCode } from "./compat-76W6bfd0.mjs";
import { O as evaluateDmPolicyAllowFromDependency, o as DmPolicySchema } from "./zod-schema.core-BWFCYYML.mjs";
import { n as validateJsonSchemaValue, t as parseJsonSchemaIssuePath } from "./schema-validator-NvoOmaWr.mjs";
import { a as ToolPolicySchema } from "./zod-schema.agent-runtime-NX0nH15U.mjs";
//#region src/channels/plugins/config-schema.ts
const AllowFromListSchema = array(union([string(), number()])).optional();
/** Validate one policy scope; the channel owns account selection and refinement ordering. */
function refineChannelDmPolicy(e) {
const { channelId: t, value: n, accountId: r, ctx: i } = e;
const a = r === void 0 ? n : n.accounts?.[r];
if (!a) return;
const o = a.dmPolicy ?? n.dmPolicy;
const s = evaluateDmPolicyAllowFromDependency({
policy: o,
allowFrom: a.allowFrom ?? n.allowFrom
});
if (!s) return;
const c = `channels.${t}`;
const l = r === void 0 ? c : `${c}.accounts.*`;
const u = r === void 0 ? "" : ` (or ${c}.allowFrom)`;
const d = s === "open_requires_wildcard" ? "include \"*\"" : "contain at least one sender ID";
i.addIssue({
code: ZodIssueCode.custom,
path: r === void 0 ? ["allowFrom"] : [
"accounts",
r,
"allowFrom"
],
message: `${l}.dmPolicy="${o}" requires ${l}.allowFrom${u} to ${d}`
});
}
/** Canonical per-group/room channel policy shape. */
const ChannelGroupEntrySchema = object({
requireMention: boolean().optional(),
tools: ToolPolicySchema,
toolsBySender: record(string(), ToolPolicySchema).optional(),
skills: array(string()).optional(),
enabled: boolean().optional(),
allowFrom: AllowFromListSchema,
systemPrompt: string().optional()
}).strict();
/** Extend the canonical group/room policy shape with channel-owned fields. */
function buildGroupEntrySchema(e, t) {
const n = new Set(t?.omit ?? []);
const r = Object.fromEntries(Object.entries(ChannelGroupEntrySchema.shape).filter(([e]) => !n.has(e)));
return object({
...r,
...e ?? {}
}).strict();
}
/** Build the common nested DM config block used by channel account schemas. */
function buildNestedDmConfigSchema(e) {
const t = {
enabled: boolean().optional(),
policy: DmPolicySchema.optional(),
allowFrom: AllowFromListSchema
};
return object(e ? {
...t,
...e
} : t).optional();
}
/** Add `accounts` catchall and `defaultAccount` fields to a channel account schema. */
function buildCatchallMultiAccountChannelSchema(e) {
return buildMultiAccountChannelSchema(e, { accountsMode: "catchall" });
}
/** Add the standard accounts/defaultAccount envelope and optional shared account/root refinement. */
function buildMultiAccountChannelSchema(e, t = {}) {
const n = t.refine;
const r = t.accountSchema ?? e;
const i = n ? r.superRefine((e, t) => {
return n(e, t);
}) : r;
const a = t.optionalAccount ? i.optional() : i;
const o = t.accountsMode === "catchall" ? object({}).catchall(a).optional() : record(string(), a).optional();
const s = e.extend({
accounts: o,
defaultAccount: string().optional()
});
return n ? s.superRefine((e, t) => {
return n(e, t);
}) : s;
}
function cloneRuntimeIssue(e) {
const t = e && typeof e === "object" ? e : {};
const n = Array.isArray(t.path) ? t.path.filter((e) => {
const t = typeof e;
return t === "string" || t === "number";
}) : void 0;
return {
...t,
...n ? { path: n } : {}
};
}
/** Build a channel config schema from JSON Schema with runtime validation/default support. */
function buildJsonChannelConfigSchema(e, t) {
return {
schema: e,
...t?.uiHints ? { uiHints: t.uiHints } : {},
runtime: t?.runtime ?? { safeParse(n) {
const r = validateJsonSchemaValue({
schema: e,
cacheKey: t?.cacheKey ?? "channel-config-schema:json",
value: n,
applyDefaults: true
});
return r.ok ? {
success: true,
data: r.value
} : {
success: false,
issues: r.errors.map((e) => ({
path: parseJsonSchemaIssuePath(e.path),
message: e.message
}))
};
} }
};
}
/** Build a channel config schema from Zod, exporting JSON Schema when available. */
function buildChannelConfigSchema(e, t) {
return {
schema: "_zod" in e ? toJSONSchema(e, {
target: "draft-07",
...t?.jsonSchemaMode ? { io: t.jsonSchemaMode } : {},
unrepresentable: "any"
}) : {
type: "object",
additionalProperties: true
},
...t?.uiHints ? { uiHints: t.uiHints } : {},
runtime: { safeParse(t) {
const n = e.safeParse(t);
return n.success ? {
success: true,
data: n.data
} : {
success: false,
issues: n.error.issues.map(cloneRuntimeIssue)
};
} }
};
}
/** Return a channel config schema for channels that intentionally accept no config keys. */
function emptyChannelConfigSchema() {
return {
schema: {
type: "object",
additionalProperties: false,
properties: {}
},
runtime: { safeParse(e) {
if (e === void 0) return {
success: true,
data: void 0
};
const t = !e || typeof e !== "object" || Array.isArray(e) ? "expected config object" : Object.keys(e).length > 0 ? "config must be empty" : void 0;
return t ? {
success: false,
issues: [{
path: [],
message: t
}]
} : {
success: true,
data: e
};
} }
};
}
//#endregion
export { buildGroupEntrySchema as a, buildNestedDmConfigSchema as c, buildChannelConfigSchema as i, emptyChannelConfigSchema as l, ChannelGroupEntrySchema as n, buildJsonChannelConfigSchema as o, buildCatchallMultiAccountChannelSchema as r, buildMultiAccountChannelSchema as s, AllowFromListSchema as t, refineChannelDmPolicy as u };