UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

196 lines (195 loc) • 5.92 kB
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; import { c as isSecretRef } from "./ref-contract-b8L-9DrQ.mjs"; import "./types.secrets-BmNLLnHp.mjs"; import { t as VERSION } from "./version-GlxUMdxI.mjs"; import { r as shouldWarnOnTouchedVersion } from "./version-DkjdPwZ3.mjs"; import { n as formatConfigIssueLines } from "./issue-format-WaYQX-7p.mjs"; import { t as isSensitiveConfigPath } from "./sensitive-paths-Bso24MeS.mjs"; import path from "node:path"; import JSON5 from "json5"; //#region src/config/issue-location.ts function skipTrivia(e, t) { while (t.pos < e.length) { const n = e[t.pos]; if (/\s/u.test(n ?? "")) { t.pos++; continue; } if (n === "/" && e[t.pos + 1] === "/") { t.pos += 2; while (t.pos < e.length && !/[\n\r\u2028\u2029]/u.test(e[t.pos] ?? "")) t.pos++; continue; } if (n === "/" && e[t.pos + 1] === "*") { const n = e.indexOf("*/", t.pos + 2); t.pos = n === -1 ? e.length : n + 2; continue; } return; } } function scanQuoted(e, t) { const n = e[t.pos++]; while (t.pos < e.length) { const r = e[t.pos++]; if (r === "\\") t.pos++; else if (r === n) return; } } function consume(e, t, n) { skipTrivia(e, t); if (e[t.pos] !== n) return false; t.pos++; return true; } function readObjectKey(e, t) { skipTrivia(e, t); const n = t.pos; const r = e[t.pos]; if (r === "\"" || r === "'") scanQuoted(e, t); else while (t.pos < e.length && !/[\s:]/u.test(e[t.pos] ?? "") && !(e[t.pos] === "/" && /[/*]/u.test(e[t.pos + 1] ?? ""))) t.pos++; if (t.pos === n) return null; const i = e.slice(n, t.pos); try { if (r === "\"" || r === "'") { const e = JSON5.parse(i); return typeof e === "string" ? e : null; } const e = JSON5.parse(`{${i}:null}`); return Object.keys(e)[0] ?? null; } catch { return null; } } function skipValue(e, t) { skipTrivia(e, t); const n = e[t.pos]; if (n === "\"" || n === "'") { scanQuoted(e, t); return; } if (n === "{" || n === "[") { const r = n === "{" ? "}" : "]"; t.pos++; while (t.pos < e.length) { skipTrivia(e, t); if (e[t.pos] === r) { t.pos++; return; } if (n === "{" && (readObjectKey(e, t) === null || !consume(e, t, ":"))) return; skipValue(e, t); skipTrivia(e, t); if (e[t.pos] === ",") t.pos++; } return; } while (t.pos < e.length && !/[,}\]\s]/u.test(e[t.pos] ?? "") && !(e[t.pos] === "/" && /[/*]/u.test(e[t.pos + 1] ?? ""))) t.pos++; } function locateValueOffset(e, t, n, r) { const i = n[r]; const a = r === n.length - 1; if (typeof i === "number") { if (!consume(e, t, "[")) return; for (let o = 0; t.pos < e.length; o++) { skipTrivia(e, t); if (e[t.pos] === "]") return; if (o === i) return a ? t.pos : locateValueOffset(e, t, n, r + 1); skipValue(e, t); if (!consume(e, t, ",")) return; } return; } if (!consume(e, t, "{")) return; let o; while (t.pos < e.length) { skipTrivia(e, t); if (e[t.pos] === "}") return o; const s = readObjectKey(e, t); if (s === null || !consume(e, t, ":")) return; skipTrivia(e, t); const c = t.pos; if (s === i) o = a ? c : locateValueOffset(e, { pos: c }, n, r + 1); t.pos = c; skipValue(e, t); skipTrivia(e, t); if (e[t.pos] === ",") { t.pos++; continue; } return o; } return o; } function lineAtOffset(e, t) { let n = 1; for (let r = 0; r < t; r++) { const t = e[r]; if (t === "\r") { n++; if (e[r + 1] === "\n") r++; } else if (t === "\n" || t === "\u2028" || t === "\u2029") n++; } return n; } function resolveConfigValueAtPath(e, t) { let n = e; for (const e of t) { if (typeof e === "number") { if (!Array.isArray(n) || e >= n.length) return; n = n[e]; continue; } if (!n || typeof n !== "object" || Array.isArray(n)) return; n = n[e]; } return n; } function stringifyReceivedValue(e) { if (e === void 0) return null; if (typeof e === "number" && (!Number.isFinite(e) || Object.is(e, -0))) return Object.is(e, -0) ? "-0" : String(e); try { const t = JSON.stringify(e); if (t === void 0) return null; return t.length > 160 ? `${truncateUtf16Safe(t, 157)}...` : t; } catch { return null; } } function appendReceivedValueHint(e, t, n, r) { if (n === void 0 || isSecretRef(n) || isSensitiveConfigPath(t) || r[0] === "channels" || r[0] === "plugins" && r[1] === "entries" && r[3] === "config" || typeof n === "object" && n !== null || e.toLowerCase().includes("got:") || /\breceived\b/i.test(e)) return e; const i = stringifyReceivedValue(n); return i ? `${e}, got: ${i}` : e; } function resolveConfigIssueLineInRaw(e, t) { if (t.length === 0 || e.trim().length === 0) return; const n = locateValueOffset(e, { pos: 0 }, t, 0); return n === void 0 ? void 0 : lineAtOffset(e, n); } /** Render invalid config issues with source locations, received values, and version-skew advice. */ function renderConfigValidationIssueLines(e, t = "-") { const n = typeof e.raw === "string" ? e.raw : null; const r = typeof e.path === "string" && e.path.trim() ? path.basename(e.path) : "openclaw.json"; const i = e.issues.map((t) => { const i = t.pathSegments; if (!i || i.length === 0) return t; const a = resolveConfigValueAtPath(e.parsed, i); const o = resolveConfigValueAtPath(e.sourceConfig, i); const s = n === null ? void 0 : resolveConfigIssueLineInRaw(n, i); const c = s !== void 0 && Object.is(a, o) ? appendReceivedValueHint(t.message, t.path, o, i) : t.message; return { ...t, pathSegments: i, message: c, ...s === void 0 ? {} : { line: s, sourceFile: r } }; }); const a = formatConfigIssueLines(i, t, { normalizeRoot: true }); const o = e.sourceConfig.meta?.lastTouchedVersion; return shouldWarnOnTouchedVersion(VERSION, o) ? [...a, `Config was last written by OpenClaw ${o}, but you are running ${VERSION} — upgrade or re-run setup.`] : a; } //#endregion export { renderConfigValidationIssueLines as t };