UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

231 lines (230 loc) • 7.83 kB
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs"; import { i as isMissingPathError } from "./errno-DFcoqIT9.mjs"; import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs"; import "./paths-DAcAg5aV.mjs"; import "./errors-BKzCeTYb.mjs"; import { s as resolveOpenClawStateSqlitePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { r as decodeUtf8Input } from "./utf8-input-BHczGT8v.mjs"; import { i as createPluginStateKeyedStore } from "./plugin-state-store-C2xZ8cMl.mjs"; import { t as archiveLegacyStateSource } from "./doctor-state-migration-fs-BDJXiDF-.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region src/commands/doctor-retired-phone-control.ts const PHONE_CONTROL_PLUGIN_ID = "phone-control"; const ARM_STATE_NAMESPACE = "armed"; const RETIRED_ARM_GROUPS = /* @__PURE__ */ new Set([ "camera", "screen", "computer", "mobile-ui", "writes", "all" ]); const RETIRED_PHONE_CONTROL_SEEDED_DENY_COMMANDS = [ "camera.snap", "camera.clip", "screen.record", "computer.act", "mobile.ui.observe", "mobile.ui.act", "contacts.add", "calendar.add", "reminders.add", "sms.send", "sms.search", "health.summary" ]; function resolveLegacyArmStatePath(e) { return path.join(resolveStateDir(e), "plugins", PHONE_CONTROL_PLUGIN_ID, "armed.json"); } async function inspectStatePath(e, t) { try { return (await fs.stat(e)).isFile() ? { status: "file" } : { status: "unsafe", warning: `${t} at ${e} is not a regular file.` }; } catch (n) { if (isMissingPathError(n)) return { status: "missing" }; return { status: "unsafe", warning: `Could not inspect ${t} at ${e}: ${String(n)}` }; } } function isNonBlankStringArray(e) { return Array.isArray(e) && e.every((e) => typeof e === "string" && e.trim() !== ""); } function isTimestamp(e) { return Number.isSafeInteger(e) && Number(e) >= 0; } function isRetiredArmState(e) { if (!isRecord(e) || !isTimestamp(e.armedAtMs)) return false; if (e.expiresAtMs !== null && !isTimestamp(e.expiresAtMs)) return false; if (e.version === 1) return isNonBlankStringArray(e.removedFromDeny); if (e.version !== 2 && e.version !== 3) return false; if (typeof e.group !== "string" || !RETIRED_ARM_GROUPS.has(e.group) || !isNonBlankStringArray(e.armedCommands) || !isNonBlankStringArray(e.addedToAllow) || !isNonBlankStringArray(e.removedFromDeny)) return false; if (e.version === 2) return true; return typeof e.generation === "string" && e.generation.trim() !== "" && (e.phase === "preparing" || e.phase === "active") && isNonBlankStringArray(e.persistentAllows); } function readStringArrayField(e, t) { if (!isRecord(e)) return []; const n = e[t]; return Array.isArray(n) ? n.filter((e) => typeof e === "string" && e.trim() !== "") : []; } function openRetiredArmStateStore(e) { return createPluginStateKeyedStore(PHONE_CONTROL_PLUGIN_ID, { namespace: ARM_STATE_NAMESPACE, maxEntries: 1, overflowPolicy: "reject-new", env: e }); } async function readRetiredArmStates(e) { const t = resolveLegacyArmStatePath(e); const n = resolveOpenClawStateSqlitePath(e); const [r, i] = await Promise.all([inspectStatePath(t, "retired Phone Control lease state"), inspectStatePath(n, "OpenClaw state database")]); const a = [r, i].flatMap((e) => e.status === "unsafe" ? [e.warning] : []); const o = a.length > 0; let s; let c = false; if (r.status === "file") try { s = JSON.parse(decodeUtf8Input(await fs.readFile(t), t)); c = isRetiredArmState(s); if (!c) a.push(`Retired Phone Control lease state at ${t} is malformed.`); } catch (e) { a.push(`Could not read retired Phone Control lease state at ${t}: ${String(e)}`); } let l = false; let u = []; if (i.status === "file") try { u = await openRetiredArmStateStore(e).entries(); if (u.length > 1) { l = true; a.push("Retired Phone Control lease journal contains multiple records."); } else if (u.length === 1 && !isRetiredArmState(u[0]?.value)) { l = true; a.push("Retired Phone Control lease journal contains a malformed record."); } } catch (e) { l = true; a.push(`Could not read retired Phone Control lease journal: ${String(e)}`); } const d = u.length === 1 && !l; const f = !o && !l && (d || r.status !== "file" || c); return { states: f ? d ? u.map((e) => e.value) : c ? [s] : [] : [], cleanupPending: o || r.status !== "missing" || u.length > 0 || l, cleanupSafe: f, warnings: a }; } function isExactSeededDenyList(e) { const t = [...e].toSorted(); const n = [...RETIRED_PHONE_CONTROL_SEEDED_DENY_COMMANDS].toSorted(); return t.length === n.length && t.every((e, t) => e === n[t]); } function withCommandLists(e, t) { const n = { ...e.gateway?.nodes?.commands }; for (const e of ["allow", "deny"]) if (t[e]?.length) n[e] = t[e]; else delete n[e]; const r = { ...e.gateway?.nodes }; delete r.commands; return { ...e, gateway: { ...e.gateway, nodes: { ...r, ...Object.keys(n).length > 0 ? { commands: n } : {} } } }; } /** Plans canonical config cleanup while retaining the journal until the config write succeeds. */ async function prepareRetiredPhoneControlCleanup(e) { const t = await readRetiredArmStates(e.env ?? process.env); const n = []; const r = { config: e.cfg, configChanges: n, cleanupPending: t.cleanupPending, cleanupSafe: t.cleanupSafe, warnings: t.warnings }; if (!t.cleanupSafe) return r; const i = new Set(t.states.flatMap((e) => readStringArrayField(e, "addedToAllow"))); const a = t.states.flatMap((e) => readStringArrayField(e, "removedFromDeny")); const o = e.cfg.gateway?.nodes?.commands?.allow; const s = e.cfg.gateway?.nodes?.commands?.deny; const c = new Set(s); const l = [...s ?? [], ...[...new Set(a)].filter((e) => !c.has(e))]; const u = s !== void 0 && isExactSeededDenyList(l); const d = u ? new Set(a) : void 0; const f = o?.filter((e) => !i.has(e.trim()) && !d?.has(e.trim())); const p = u ? l.filter((e) => f?.includes(e)) : l; const m = Boolean(o && f?.length !== o.length); const h = l.length !== (s?.length ?? 0); if (!m && !h && !u) return r; if (m) n.push("Removed stale Phone Control lease-only command allow entries."); if (h && !u) n.push("Restored command deny entries removed by Phone Control leases."); if (u) n.push("Removed the retired Phone Control setup deny seed."); return { ...r, config: withCommandLists(e.cfg, { allow: f, deny: p }) }; } /** Drops SQLite journal rows and archives the legacy file after config persistence. */ async function finalizeRetiredPhoneControlCleanup(e) { const t = e.env ?? process.env; const n = []; const r = []; const i = resolveLegacyArmStatePath(t); const a = await inspectStatePath(i, "retired Phone Control lease state"); if (a.status === "unsafe") { r.push(a.warning); return { changes: n, warnings: r }; } if (a.status === "file") { await archiveLegacyStateSource({ filePath: i, label: "retired Phone Control lease state", changes: n, warnings: r }); const e = await inspectStatePath(i, "retired Phone Control lease state"); if (e.status === "unsafe") r.push(e.warning); if (e.status !== "missing") return { changes: n, warnings: r }; } const o = await inspectStatePath(resolveOpenClawStateSqlitePath(t), "OpenClaw state database"); if (o.status === "unsafe") { r.push(o.warning); return { changes: n, warnings: r }; } if (o.status === "file") try { const e = openRetiredArmStateStore(t); if ((await e.entries()).length > 0) { await e.clear(); n.push("Dropped the retired Phone Control lease journal."); } } catch (e) { r.push(`Failed to drop the retired Phone Control lease journal: ${String(e)}`); } return { changes: n, warnings: r }; } //#endregion export { finalizeRetiredPhoneControlCleanup, prepareRetiredPhoneControlCleanup };