UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

66 lines (65 loc) • 3.93 kB
import { u as isStringArray } from "./record-coerce-CVPuDqcc.mjs"; import { i as withEngineStoreAdmission } from "./admission-scope-DmxaPNhm.mjs"; import { a as executeSqliteQueryTakeFirstSync, s as getNodeSqliteKysely } from "./kysely-sync-YJo2lmGV.mjs"; import { n as isArtifactPreservingStateRead } from "./artifact-preserving-state-reads-BfhmBr-V.mjs"; import { r as resolveDatabasePath } from "./openclaw-state-db.paths-R3ArRCcz.mjs"; import { l as runOpenClawStateWriteTransaction, r as openOpenClawStateDatabase } from "./openclaw-state-db-ARhwdPJg.mjs"; import { o as recordLegacyMigrationRun } from "./state-migrations.receipts-CXYpigJ-.mjs"; import { s as withDevicePairingLock } from "./device-pairing-store-readonly-CIEEZowO.mjs"; import { D as readDevicePairingStoreStateFromDatabase, c as resolveNodePairingGeneration, t as clearNodePairingGenerationState, w as persistDevicePairingStoreState } from "./device-pairing-identity-BqPJxbIo.mjs"; import "./device-pairing-D--Fzrek.mjs"; import { n as NODE_DESKTOP_STREAM_COMMAND } from "./node-desktop-stream-BZM2AiRA.mjs"; import { randomUUID } from "node:crypto"; //#region src/infra/device-pairing-node-desktop-migration.ts const MIGRATION_ID = "node-desktop-stream-pairing-defaults-v1"; /** Also applies to old file-backed surfaces imported after the SQLite migration completed. */ function preserveLegacyDesktopStreamOptOut(e, t, n) { const r = t.gateway?.nodes?.commands; if ([r?.allow, r?.deny].some((e) => e?.some((e) => e.trim() === "desktop.stream"))) return false; const i = e.nodeSurface; if (!Array.isArray(i?.commands) || !i.commands.some((e) => typeof e === "string" && e.trim() === "desktop.stream")) return false; if (!isStringArray(i.commands) || !Number.isSafeInteger(i.createdAtMs) || !Number.isSafeInteger(i.approvedAtMs) || !Number.isSafeInteger(i.approvedAtMs + 1)) throw new Error(`Cannot migrate malformed desktop approval for node ${e.deviceId}`); const a = resolveNodePairingGeneration(e); i.commands = i.commands.filter((e) => e.trim() !== NODE_DESKTOP_STREAM_COMMAND); i.approvedAtMs = Math.max(n, i.approvedAtMs + 1); clearNodePairingGenerationState(e, a); if (e.pendingNodeSurface?.commands?.some((e) => e.trim() === "desktop.stream")) { e.pendingNodeSurface.silent = false; e.pendingNodeSurface.revision = randomUUID(); } return true; } /** Commit narrowed approvals and the once-per-database receipt together before node admission. */ async function migrateLegacyDesktopStreamOptOuts(e, t) { return await withDevicePairingLock(async () => withEngineStoreAdmission(() => { const n = t ? { env: { ...process.env, OPENCLAW_STATE_DIR: t } } : {}; if (isArtifactPreservingStateRead("agent", resolveDatabasePath(n))) throw new Error("Programming error: shared-state write during artifact-preserving inspection."); const r = openOpenClawStateDatabase(n); const i = executeSqliteQueryTakeFirstSync(r.db, getNodeSqliteKysely(r.db).selectFrom("migration_runs").select("status").where("id", "=", MIGRATION_ID)); if (i?.status === "completed") return 0; if (i) throw new Error("Desktop approval migration receipt is incomplete; existing approvals remain unchanged."); return runOpenClawStateWriteTransaction(({ db: n }) => { const r = Date.now(); const i = readDevicePairingStoreStateFromDatabase(n); let a = 0; for (const t of Object.values(i.pairedByDeviceId)) if (preserveLegacyDesktopStreamOptOut(t, e, r)) a += 1; if (a > 0) persistDevicePairingStoreState(i, t, "paired"); recordLegacyMigrationRun(n, { runId: MIGRATION_ID, startedAt: r, finishedAt: r, status: "completed", reportJson: JSON.stringify({ retiredDesktopApprovals: a }) }); return a; }, { ...n, database: r }, { operationLabel: "preserve legacy desktop stream opt-outs" }); })); } //#endregion export { preserveLegacyDesktopStreamOptOut as n, migrateLegacyDesktopStreamOptOuts as t };