UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

148 lines (147 loc) • 5.17 kB
import { i as asOptionalObjectRecord } from "./record-coerce-CVPuDqcc.mjs"; import { n as preserveAtPrefixedRelativePath } from "./path-policy-CyYRyWre.mjs"; import { i as normalizeLocalWindowsDrivePath } from "./path-utils-B5H__xjt.mjs"; //#region src/agents/agent-tools.params.ts /** * Shared validation for model-supplied tool parameters. * Converts malformed file-tool arguments into retryable errors and fixes the * specific XML suffix and Office-extension corruption seen in path arguments. */ const RETRY_GUIDANCE_SUFFIX = " Supply correct parameters before retrying."; const XML_ARG_VALUE_SUFFIX_RE = /<\/arg_value>>+$/; const HALLUCINATED_OFFICE_PATH_EXTENSION_RE = /\.(doc|ppt|xls)(?:odex|codex|xodex|xcodex)$/i; const OFFICE_EXTENSION_BY_FAMILY = { doc: ".docx", ppt: ".pptx", xls: ".xlsx" }; function parameterValidationError(e) { return /* @__PURE__ */ new Error(`${e}.${RETRY_GUIDANCE_SUFFIX}`); } function describeReceivedParamValue(e, t = false) { if (e === void 0 || e === null) return; if (typeof e === "string") { if (t || e.trim().length > 0) return; return "<empty-string>"; } if (Array.isArray(e)) return "<array>"; return `<${typeof e}>`; } function formatReceivedParamHint(e, t) { const n = new Set(t.flatMap((e) => e.allowEmpty ? e.keys : [])); const r = []; for (const t of Object.keys(e)) { const i = describeReceivedParamValue(e[t], n.has(t)); if (e[t] === void 0 || e[t] === null) continue; r.push(i ? `${t}=${i}` : t); } return r.length > 0 ? ` (received: ${r.join(", ")})` : ""; } function isValidEditReplacement(e) { if (!e || typeof e !== "object") return false; const t = e; return typeof t.oldText === "string" && t.oldText.trim().length > 0 && typeof t.newText === "string"; } function hasValidEditReplacements(e) { const t = e.edits; return Array.isArray(t) && t.length > 0 && t.every(isValidEditReplacement); } const REQUIRED_PARAM_GROUPS = { read: [{ keys: ["path"], label: "path" }], write: [{ keys: ["path"], label: "path" }, { keys: ["content"], label: "content", allowEmpty: true }], edit: [{ keys: ["path"], label: "path" }, { keys: ["edits"], label: "edits", validator: hasValidEditReplacements }] }; /** Models send `optional: false` for a required read; the read schema accepts only `true`. */ function prepareReadToolParams(e) { const t = asOptionalObjectRecord(e); if (t?.optional !== false) return e; const n = { ...t }; delete n.optional; return n; } /** Canonical daily-memory journals, which a read without `optional` treats as optional. */ function isDailyMemoryReadPath(e) { const t = process.platform === "win32" ? e.replace(/\\/g, "/") : e; return /^(?:\.\/)*memory\/\d{4}-\d{2}-\d{2}\.md$/u.test(t); } function stripMalformedXmlArgValueSuffix(e) { return e.includes("</arg_value>") ? e.replace(XML_ARG_VALUE_SUFFIX_RE, "") : e; } function normalizeFileToolPathParam(e, t, n) { const r = stripMalformedXmlArgValueSuffix(e).replace(HALLUCINATED_OFFICE_PATH_EXTENSION_RE, (e, t) => OFFICE_EXTENSION_BY_FAMILY[t.toLowerCase()] ?? e); const i = t && !n ? normalizeLocalWindowsDrivePath(r) : r; return t ? Promise.resolve(preserveAtPrefixedRelativePath(i, t, n)) : r; } /** Strip malformed XML suffixes from selected string fields without mutating input. */ function stripMalformedXmlArgValueSuffixFromKeys(e, t) { let n; for (const r of t) { const t = e[r]; if (typeof t !== "string") continue; const i = stripMalformedXmlArgValueSuffix(t); if (i !== t) { n ??= { ...e }; n[r] = i; } } return n ?? e; } /** Normalize selected file-tool path fields without mutating input. */ async function normalizeFileToolPathParamsFromKeys(e, t, n, r) { let i; for (const a of t) { const t = e[a]; if (typeof t !== "string") continue; const o = n ? await normalizeFileToolPathParam(t, n, r) : normalizeFileToolPathParam(t); if (o !== t) { i ??= { ...e }; i[a] = o; } } return i ?? e; } function missingRequiredParamLabels(e, t) { return t.filter((t) => !e || !(t.validator?.(e) ?? t.keys.some((n) => { if (!(n in e)) return false; const r = e[n]; return typeof r === "string" && (t.allowEmpty || r.trim().length > 0); }))).map((e) => e.label ?? e.keys.join(" or ")); } function assertRequiredParams(e, t, n) { if (!e) throw parameterValidationError(`Missing parameters for ${n}`); const r = missingRequiredParamLabels(e, t); if (r.length > 0) { const n = r.join(", "); throw parameterValidationError(`Missing required ${r.length === 1 ? "parameter" : "parameters"}: ${n}${formatReceivedParamHint(e, t)}`); } } function wrapToolParamValidation(e, t, n) { return { ...e, execute: async (r, i, a, o) => { const s = asOptionalObjectRecord(i); const c = s ? await normalizeFileToolPathParamsFromKeys(s, ["path"], n) : s; assertRequiredParams(c, t, e.name); return e.execute(r, c ?? i, a, o); } }; } //#endregion export { normalizeFileToolPathParamsFromKeys as a, wrapToolParamValidation as c, normalizeFileToolPathParam as i, assertRequiredParams as n, prepareReadToolParams as o, isDailyMemoryReadPath as r, stripMalformedXmlArgValueSuffixFromKeys as s, REQUIRED_PARAM_GROUPS as t };