UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

264 lines (263 loc) • 9.53 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { t as isBlockedObjectKey } from "./prototype-keys-CLGi5xDj.mjs"; //#region src/plugin-sdk/migration.ts /** Shared migration failure reason when an item lacks required paths. */ const MIGRATION_REASON_MISSING_SOURCE_OR_TARGET = "missing source or target"; /** Shared migration conflict reason when a target already exists. */ const MIGRATION_REASON_TARGET_EXISTS = "target exists"; /** Creates a migration item, defaulting new provider output to the planned state. */ function createMigrationItem(e) { return { ...e, status: e.status ?? "planned" }; } /** Marks a planned item as blocked by an existing target value. */ function markMigrationItemConflict(e, t) { return { ...e, status: "conflict", reason: t }; } /** Marks an item as failed during detection or apply. */ function markMigrationItemError(e, t) { return { ...e, status: "error", reason: t }; } /** Marks an item as intentionally skipped, usually for manual follow-up. */ function markMigrationItemSkipped(e, t) { return { ...e, status: "skipped", reason: t }; } /** Counts migration item statuses for provider plans, apply results, and CLI reports. */ function summarizeMigrationItems(e) { return { total: e.length, planned: e.filter((e) => e.status === "planned").length, migrated: e.filter((e) => e.status === "migrated").length, skipped: e.filter((e) => e.status === "skipped").length, conflicts: e.filter((e) => e.status === "conflict").length, errors: e.filter((e) => e.status === "error").length, sensitive: e.filter((e) => e.sensitive).length }; } const REDACTED_MIGRATION_VALUE = "[redacted]"; const SECRET_KEY_MARKERS = [ "accesstoken", "apikey", "authorization", "bearertoken", "clientsecret", "cookie", "credential", "password", "privatekey", "refreshtoken", "secret" ]; const SECRET_VALUE_PATTERNS = [ /\bBearer\s+[A-Za-z0-9._~+/=-]+/gu, /\bsk-[A-Za-z0-9_-]{8,}\b/gu, /\bgh[pousr]_[A-Za-z0-9_]{16,}\b/gu, /\bxox[abprs]-[A-Za-z0-9-]{8,}\b/gu, /\bAIza[0-9A-Za-z_-]{12,}\b/gu ]; function normalizeSecretKey(e) { return e.toLowerCase().replaceAll(/[^a-z0-9]/gu, ""); } function isSecretKey(e) { const t = normalizeSecretKey(e); if (t === "token" || t.endsWith("token")) return true; if (t === "auth" || t === "authorization") return true; return SECRET_KEY_MARKERS.some((e) => t.includes(e)); } var MigrationConfigPatchConflictError = class extends Error { constructor(e) { super(e); this.reason = e; this.name = "MigrationConfigPatchConflictError"; } }; const MIGRATION_REASON_UNSAFE_CONFIG_PATCH_PATH = "unsafe config patch path"; function isSafeMigrationConfigPath(e) { return e.length > 0 && e.every((e) => e.length > 0 && !isBlockedObjectKey(e)); } function cloneMigrationConfigValue(e) { if (Array.isArray(e)) return e.map((e) => cloneMigrationConfigValue(e)); if (!isRecord(e)) return structuredClone(e); const t = {}; for (const [n, r] of Object.entries(e)) if (!isBlockedObjectKey(n)) t[n] = cloneMigrationConfigValue(r); return t; } /** Reads a nested config value, returning undefined when a parent is not an object. */ function readMigrationConfigPath(e, t) { if (!isSafeMigrationConfigPath(t)) return; let n = e; for (const e of t) { if (!isRecord(n)) return; n = n[e]; } return n; } /** Deep-merges object patches and replaces scalar/array values with a cloned target value. */ function mergeMigrationConfigValue(e, t) { if (!isRecord(e) || !isRecord(t)) return cloneMigrationConfigValue(t); const n = { ...e }; for (const [e, r] of Object.entries(t)) { if (isBlockedObjectKey(e)) continue; n[e] = mergeMigrationConfigValue(n[e], r); } return n; } /** Writes a config patch path in-place, creating missing object parents as needed. */ function writeMigrationConfigPath(e, t, n) { if (!isSafeMigrationConfigPath(t)) throw new Error(MIGRATION_REASON_UNSAFE_CONFIG_PATCH_PATH); let r = e; for (const e of t.slice(0, -1)) { const t = r[e]; if (!isRecord(t)) r[e] = {}; r = r[e]; } const i = t.at(-1); if (!i) throw new Error(MIGRATION_REASON_UNSAFE_CONFIG_PATCH_PATH); r[i] = mergeMigrationConfigValue(r[i], n); } /** Checks whether a config patch would overwrite existing leaf keys without `--overwrite`. */ function hasMigrationConfigPatchConflict(e, t, n) { if (!isRecord(n)) return readMigrationConfigPath(e, t) !== void 0; const r = readMigrationConfigPath(e, t); if (!isRecord(r)) return false; return Object.keys(n).some((e) => !isBlockedObjectKey(e) && Object.hasOwn(r, e) && r[e] !== void 0); } /** Builds a planned or conflicting config-merge migration item. */ function createMigrationConfigPatchItem(e) { return createMigrationItem({ id: e.id, kind: "config", action: "merge", source: e.source, target: e.target, status: e.conflict ? "conflict" : "planned", reason: e.conflict ? e.reason ?? "target exists" : void 0, message: e.message, sensitive: e.sensitive, details: { ...e.details, path: e.path, value: e.value } }); } /** Builds a skipped item that records user-facing manual migration guidance. */ function createMigrationManualItem(e) { return createMigrationItem({ id: e.id, kind: "manual", action: "manual", source: e.source, status: "skipped", message: e.message, reason: e.recommendation }); } /** Reads config patch metadata from an item produced by `createMigrationConfigPatchItem`. */ function readMigrationConfigPatchDetails(e) { const t = e.details?.path; if (!Array.isArray(t) || !t.every((e) => typeof e === "string")) return; return { path: t, value: e.details?.value }; } /** Resolves the host-owned config mutation target for migration apply. */ function resolveMigrationConfigRuntime(e) { return e.configRuntime ?? e.runtime?.config; } /** Applies one planned config patch through the runtime config writer and returns its final status. */ async function applyMigrationConfigPatchItem(e, t) { if (t.status !== "planned") return t; const n = readMigrationConfigPatchDetails(t); if (!n) return markMigrationItemError(t, "missing config patch"); if (!isSafeMigrationConfigPath(n.path)) return markMigrationItemError(t, MIGRATION_REASON_UNSAFE_CONFIG_PATCH_PATH); const r = resolveMigrationConfigRuntime(e); if (!r?.current || !r.mutateConfigFile) return markMigrationItemError(t, "config runtime unavailable"); try { const i = r.current(); if (!e.overwrite && hasMigrationConfigPatchConflict(i, n.path, n.value)) return markMigrationItemConflict(t, MIGRATION_REASON_TARGET_EXISTS); await r.mutateConfigFile({ base: "runtime", afterWrite: { mode: "auto" }, mutate(t) { if (!e.overwrite && hasMigrationConfigPatchConflict(t, n.path, n.value)) throw new MigrationConfigPatchConflictError(MIGRATION_REASON_TARGET_EXISTS); writeMigrationConfigPath(t, n.path, n.value); } }); return { ...t, status: "migrated" }; } catch (e) { if (e instanceof MigrationConfigPatchConflictError) return markMigrationItemConflict(t, e.reason); return markMigrationItemError(t, e instanceof Error ? e.message : String(e)); } } /** Manual items never mutate state; applying one preserves the skipped/manual status. */ function applyMigrationManualItem(e) { return markMigrationItemSkipped(e, e.reason ?? "manual follow-up required"); } function isSecretReferenceLike(e) { if (!isRecord(e)) return false; return e.source === "env" && typeof e.id === "string" && (e.provider === void 0 || typeof e.provider === "string"); } function redactString(e) { let t = e; for (const e of SECRET_VALUE_PATTERNS) t = t.replace(e, REDACTED_MIGRATION_VALUE); return t; } function redactMigrationValueInternal(e, t) { if (typeof e === "string") return redactString(e); if (!e || typeof e !== "object") return e; if (t.has(e)) return REDACTED_MIGRATION_VALUE; t.add(e); if (Array.isArray(e)) try { return e.map((e) => redactMigrationValueInternal(e, t)); } finally { t.delete(e); } const n = e; const r = {}; const i = n.sensitive === true && isRecord(n.details) && Object.hasOwn(n.details, "value"); for (const [e, a] of Object.entries(n)) { if (e === "details" && i && isRecord(a)) { const n = redactMigrationValueInternal(a, t); r[e] = isRecord(n) ? { ...n, value: REDACTED_MIGRATION_VALUE } : REDACTED_MIGRATION_VALUE; continue; } if (isSecretKey(e) && !isSecretReferenceLike(a)) { r[e] = REDACTED_MIGRATION_VALUE; continue; } r[e] = redactMigrationValueInternal(a, t); } return r; } /** Redacts likely secret values while preserving SecretRef-like objects for operator context. */ function redactMigrationValue(e) { return redactMigrationValueInternal(e, /* @__PURE__ */ new WeakSet()); } /** Redacts sensitive fields from a full migration plan before report/output serialization. */ function redactMigrationPlan(e) { return redactMigrationValue(e); } //#endregion export { resolveMigrationConfigRuntime as _, createMigrationConfigPatchItem as a, hasMigrationConfigPatchConflict as c, markMigrationItemSkipped as d, mergeMigrationConfigValue as f, redactMigrationValue as g, redactMigrationPlan as h, applyMigrationManualItem as i, markMigrationItemConflict as l, readMigrationConfigPath as m, MIGRATION_REASON_TARGET_EXISTS as n, createMigrationItem as o, readMigrationConfigPatchDetails as p, applyMigrationConfigPatchItem as r, createMigrationManualItem as s, MIGRATION_REASON_MISSING_SOURCE_OR_TARGET as t, markMigrationItemError as u, summarizeMigrationItems as v, writeMigrationConfigPath as y };