UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

283 lines (282 loc) • 11.5 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import "./paths-DAcAg5aV.mjs"; import { f as withCommandProcessScope } from "./exec-spawn-DMKoejF-.mjs"; import { t as beginLifecycleWriteCustody } from "./lifecycle-write-custody-B171kU9T.mjs"; import { t as exitCliAfterOutput } from "./one-shot-exit-DkbGSQD5.mjs"; import { t as runWithLocalStateOwner } from "./local-state-owner-DTl7OHpC.mjs"; import { r as withProgress } from "./progress-DZHhZ1Fk.mjs"; import { d as markMigrationItemSkipped, v as summarizeMigrationItems } from "./migration-C73lp9ea.mjs"; import { t as backupCreateCommand } from "./backup-NcKHuQX_.mjs"; import { n as buildMigrationReportDir, t as buildMigrationContext } from "./context-BwDZNbEs.mjs"; import { n as assertApplySucceeded, o as writeApplyResult, r as assertConflictFreePlan, t as MIGRATION_CONFLICT_REASON_PHRASES } from "./output-Czm_nIwq.mjs"; import { t as buildMigrationProviderOptions } from "./providers-HJWmMMBT.mjs"; import { t as applyMigrationItemSelection } from "./item-selection-CrW1-Zuf.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region src/commands/migrate/selection.ts const MIGRATION_NOT_SELECTED_REASON = "not selected for migration"; const MIGRATION_SELECTION_ACCEPT = "__openclaw_migrate_accept_recommended__"; const MIGRATION_SELECTION_TOGGLE_ALL_ON = "__openclaw_migrate_toggle_all_on__"; const MIGRATION_SELECTION_TOGGLE_ALL_OFF = "__openclaw_migrate_toggle_all_off__"; function formatSelectionRefList(e) { return e.length === 0 ? "none" : e.map((e) => `"${e}"`).join(", "); } function getSelectableMigrationItems(e, t) { return e.items.filter((e) => e.kind === t && e.action === (t === "skill" ? "copy" : "install") && (e.status === "planned" || e.status === "conflict")); } function formatMigrationSelectionLabel(e, t) { const n = `${t}:`; return normalizeOptionalString(e.details?.[`${t}Name`]) ?? (e.id.startsWith(n) ? e.id.slice(n.length) : e.id); } function getDefaultMigrationSelectionValues(e) { return e.filter((e) => e.status === "planned").map((e) => e.id); } function formatMigrationSelectionHint(e, t) { if (e.status !== "conflict") return; const n = normalizeOptionalString(e.details?.[t === "skill" ? "sourceLabel" : "marketplaceName"]); const r = e.reason ? MIGRATION_CONFLICT_REASON_PHRASES[e.reason] ?? e.reason : "conflict"; return n ? `${n}${t === "plugin" ? " plugin" : ""} ${r}` : r; } /** Keep selected copies/installs and their corresponding config writes together. */ function applyMigrationSelectedItemIds(e, t, n) { const r = getSelectableMigrationItems(e, n); const i = new Set(r.map((e) => e.id)); const a = new Set(r.filter((e) => t.has(e.id)).map((e) => n === "plugin" ? normalizeOptionalString(e.details?.configKey) : normalizeOptionalString(e.details?.skillName) ?? (e.source ? path.basename(e.source) : void 0)).filter((e) => e !== void 0)); const o = e.items.map((e) => { if (n === "plugin") { const t = applyCodexPluginConfigSelection(e, a); if (t) return t; } else { const t = e.kind === "config" ? e.details?.path : void 0; if (Array.isArray(t) && t.length === 3 && t[0] === "skills" && t[1] === "entries" && !a.has(t[2]) && (e.status === "planned" || e.status === "conflict")) return markMigrationItemSkipped(e, MIGRATION_NOT_SELECTED_REASON); } return i.has(e.id) && !t.has(e.id) ? markMigrationItemSkipped(e, MIGRATION_NOT_SELECTED_REASON) : e; }); return { ...e, items: o, summary: summarizeMigrationItems(o) }; } function applyMigrationSelections(e, t) { let n = e; for (const e of ["skill", "plugin"]) { const r = t[`${e}s`]; if (r === void 0) continue; const i = getSelectableMigrationItems(n, e); const a = /* @__PURE__ */ new Map(); for (const t of i) { const n = `${e}:`; const r = t.id.startsWith(n) ? t.id.slice(n.length) : void 0; const i = t.source ? path.basename(t.source) : void 0; const o = t.target ? path.basename(t.target) : void 0; for (const n of [ t.id, r, normalizeOptionalString(t.details?.[`${e}Name`]), ...e === "plugin" ? [normalizeOptionalString(t.details?.configKey)] : [], i, o ].filter((e) => typeof e === "string" && e.trim().length > 0)) { const e = normalizeOptionalLowercaseString(n); if (!e) continue; const r = a.get(e) ?? /* @__PURE__ */ new Set(); r.add(t.id); a.set(e, r); } } const o = /* @__PURE__ */ new Set(); const s = []; const c = []; for (const e of r) { const t = normalizeOptionalLowercaseString(e); if (!t) continue; const n = a.get(t); if (!n) { s.push(e); continue; } if (n.size > 1) { c.push(e); continue; } const [r] = n; if (r) o.add(r); } if (s.length > 0 || c.length > 0) { const t = i.map((t) => formatMigrationSelectionLabel(t, e)).toSorted((e, t) => e.localeCompare(t)); const n = e === "skill" ? "Skill" : "Plugin"; const r = []; if (s.length > 0) r.push(`No migratable ${e} matched ${formatSelectionRefList(s)}.`); if (c.length > 0) r.push(`${n} selection ${formatSelectionRefList(c)} was ambiguous.`); r.push(`Available ${e}s: ${t.length > 0 ? t.join(", ") : "none"}.`); throw new Error(r.join(" ")); } n = applyMigrationSelectedItemIds(n, o, e); } return applyMigrationItemSelection(n, t.itemIds); } function applyCodexPluginConfigSelection(e, t) { if (e.kind !== "config" || e.action !== "merge") return; const n = e.details?.value; if (!isRecord(n)) return; const r = n.config; if (!isRecord(r)) return; const i = r.codexPlugins; if (!isRecord(i) || !isRecord(i.plugins)) return; const a = Object.fromEntries(Object.entries(i.plugins).filter(([e]) => t.has(e))); if (Object.keys(a).length === 0) return markMigrationItemSkipped(e, MIGRATION_NOT_SELECTED_REASON); return { ...e, details: { ...e.details, value: { ...n, config: { ...r, codexPlugins: { ...i, plugins: a } } } } }; } function resolveInteractiveMigrationSelection(e, t) { const n = new Set(e.map((e) => e.id)); const r = new Set(t.filter((e) => n.has(e))); return r.size === 0 && !t.includes("__openclaw_migrate_toggle_all_off__") && t.includes("__openclaw_migrate_toggle_all_on__") ? n : r; } function selectedMigrationItemValues(e) { return e.filter((e) => e !== "__openclaw_migrate_toggle_all_on__" && e !== "__openclaw_migrate_toggle_all_off__"); } function resolveMigrationSelectionBulkToggleValues(e, t) { if (e === "__openclaw_migrate_toggle_all_on__") return [MIGRATION_SELECTION_TOGGLE_ALL_ON, ...t]; if (e === "__openclaw_migrate_toggle_all_off__") return [MIGRATION_SELECTION_TOGGLE_ALL_OFF]; } function reconcileInteractiveMigrationSkillToggleValues(e, t, n) { const r = resolveMigrationSelectionBulkToggleValues(t, n); if (r !== void 0) return r; if (t !== void 0 && n.includes(t)) return selectedMigrationItemValues(e); return e.filter((t) => t !== "__openclaw_migrate_toggle_all_on__" || !e.includes("__openclaw_migrate_toggle_all_off__")); } function reconcileInteractiveMigrationEnterValues(e, t, n, r = {}) { const i = resolveMigrationSelectionBulkToggleValues(t, n); if (i !== void 0) return i; if (t !== void 0 && n.includes(t)) { const n = selectedMigrationItemValues(e); if (r.preserveDeselectedActivatedValue && !e.includes(t)) return n; return uniqueStrings([...n, t]); } return [...e]; } function reconcileInteractiveMigrationShortcutValues(e, t, n, r) { const i = e.filter((e) => n.includes(e)); if (r === "a" && i.length === n.length) return [MIGRATION_SELECTION_TOGGLE_ALL_OFF]; const a = t.filter((e) => n.includes(e)); if (a.length === n.length) return [MIGRATION_SELECTION_TOGGLE_ALL_ON, ...n]; if (a.length === 0) return [MIGRATION_SELECTION_TOGGLE_ALL_OFF]; return a; } //#endregion //#region src/commands/migrate/apply.ts /** Creates a verified pre-migration backup, treating absent local state as empty. */ async function createPreMigrationBackup(e, t) { try { return (await backupCreateCommand({ log() {}, error() {}, exit(e) { throw new Error(`backup exited with ${e}`); } }, { output: e, preferHomeOutput: t, verify: true })).archivePath; } catch (e) { const t = e instanceof Error ? e.message : String(e); if (t.includes("No local OpenClaw state was found to back up") || t.includes("No OpenClaw config file was found to back up")) return; throw e; } } async function runMigrationApply(e) { return await runWithLocalStateOwner({ method: "migrate.apply", params: {}, target: e.providerId, onForeignOwner: "refuse", runLocal: async ({ assertCurrent: t }) => { const applyMigration = async (n) => { t(); const createContext = (t = {}) => buildMigrationContext({ ...e.opts, providerOptions: buildMigrationProviderOptions(e.opts, e.providerId), runtime: e.runtime, ...t }); const r = (e.opts.preflightPlan ? 0 : 1) + (e.opts.noBackup ? 0 : 1) + 1; let i = 0; const tick = () => { i += 1; n?.setPercent(i / r * 100); }; if (!e.opts.preflightPlan) n?.setLabel("Preparing migration plan…"); const a = e.opts.preflightPlan ?? await e.provider.plan(createContext()); if (!e.opts.preflightPlan) tick(); t(); const o = applyMigrationSelections(a, e.opts); assertConflictFreePlan(o, e.providerId); const s = resolveStateDir(); const c = buildMigrationReportDir(e.providerId, s); const l = beginLifecycleWriteCustody("migration"); let u; try { if (!e.opts.noBackup) n?.setLabel("Preparing migration backup…"); const r = e.opts.noBackup ? void 0 : await createPreMigrationBackup(e.opts.backupOutput, e.opts.preferHomeBackupOutput); if (!e.opts.noBackup) tick(); t(); await fs.mkdir(c, { recursive: true }); t(); const i = createContext({ backupPath: r, reportDir: c }); n?.setLabel("Applying migration…"); const a = await withCommandProcessScope(async () => { t(); const n = await e.provider.apply(i, o); e.onApplyCompleted?.(); return n; }); tick(); return { ...a, backupPath: a.backupPath ?? r, reportDir: a.reportDir ?? c }; } catch (e) { u = e; throw e; } finally { l(u); } }; const n = e.opts.json ? await applyMigration() : await withProgress({ label: `Applying ${e.providerId} migration…` }, applyMigration); writeApplyResult(e.runtime, e.opts, n); if (!e.opts.allowPartialResult) try { assertApplySucceeded(n); } catch (t) { if (e.opts.json) exitCliAfterOutput(e.runtime, 1); throw t; } return n; } }); } //#endregion export { applyMigrationSelectedItemIds as a, formatMigrationSelectionLabel as c, reconcileInteractiveMigrationEnterValues as d, reconcileInteractiveMigrationShortcutValues as f, MIGRATION_SELECTION_TOGGLE_ALL_ON as i, getDefaultMigrationSelectionValues as l, resolveInteractiveMigrationSelection as m, MIGRATION_SELECTION_ACCEPT as n, applyMigrationSelections as o, reconcileInteractiveMigrationSkillToggleValues as p, MIGRATION_SELECTION_TOGGLE_ALL_OFF as r, formatMigrationSelectionHint as s, runMigrationApply as t, getSelectableMigrationItems as u };