UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

161 lines (160 loc) • 5.33 kB
import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { jm as MAX_MEMORY_MIGRATION_ITEMS } from "./src-BX8OAe1J.mjs"; import { v as summarizeMigrationItems } from "./migration-C73lp9ea.mjs"; import { r as bindMemoryMigrationPlanSources } from "./memory-migration-source-DgY-BqQY.mjs"; import { t as withPluginMigrationProviders } from "./migration-provider-runtime-DwgVXsrn.mjs"; import { r as createMigrationLogger, t as buildMigrationContext } from "./context-BwDZNbEs.mjs"; import { t as runMigrationApply } from "./apply-DOyylIhP.mjs"; import { randomUUID } from "node:crypto"; //#region src/commands/migrate/memory-import.ts /** Canonical memory-only migration planning and apply policy for embedded surfaces. */ const MEMORY_ITEM_KIND = "memory"; const silentRuntime = { log() {}, error() {}, exit(e) { throw new Error(`migration exited with ${e}`); } }; async function withMemoryMigrationProviders(e, t, n, r) { return await withPluginMigrationProviders({ cfg: e, onCleanupError: n, onForeignOwner: r }, async (e) => await t(e.filter((e) => e.supportedItemKinds?.includes(MEMORY_ITEM_KIND)))); } /** * Plan memory-only offers with the selected state owner. `discoveryFailed` is set when a * running Gateway could not be asked for plans; discovery is advisory, so callers continue. */ async function withMemoryMigrationOffers(e, t, n) { return await withMemoryMigrationProviders(e.config, async (n) => { const r = createMigrationLogger(e.runtime); const i = []; for (const t of n) try { const { detection: n, plan: r } = await planProviderMemoryImport({ provider: t, config: e.config, agentId: e.agentId, overwrite: false }); const a = r.items.filter((e) => e.status === "planned").map((e) => e.id); if (n?.found === false || a.length === 0) continue; i.push({ providerId: t.id, label: t.label, source: n?.source ?? r.source, target: r.target, plannedIds: a, conflicts: r.items.filter((e) => e.status === "conflict").length, apply: () => applyProviderMemoryImport({ provider: t, config: e.config, agentId: e.agentId, itemIds: a, overwrite: false, preflightPlan: r }) }); } catch (e) { r.debug?.(`Memory migration provider ${t.id} planning failed: ${formatErrorMessage(e)}`); } return await t(i); }, n, async (n) => { let r; try { r = await n("migrations.memory.plan", { agentId: e.agentId, overwrite: false }); } catch (n) { createMigrationLogger(e.runtime).debug?.(`Memory migration planning through the Gateway failed: ${formatErrorMessage(n)}`); return await t([], true); } const i = []; for (const t of r.providers) { const a = t.items.filter((e) => e.status === "planned").map((e) => e.id); const o = t.planFingerprint; if (!t.found || !o || a.length === 0) continue; i.push({ providerId: t.providerId, label: t.label, source: t.source ?? "", target: t.target ?? r.workspace, plannedIds: a, conflicts: t.items.filter((e) => e.status === "conflict").length, apply: () => n("migrations.memory.apply", { agentId: e.agentId, providerId: t.providerId, planFingerprint: o, itemIds: a, overwrite: false, idempotencyKey: randomUUID() }) }); } return await t(i); }); } function shapeMemoryOnlyPlan(e) { const t = e.items.filter((e) => e.kind === MEMORY_ITEM_KIND); if (t.length > 2e3) throw new Error(`memory import found ${t.length} items; the maximum is ${MAX_MEMORY_MIGRATION_ITEMS}. Narrow or split the source memory before importing.`); const n = /* @__PURE__ */ new Set(); for (const e of t) { if (n.has(e.id)) throw new Error(`duplicate memory migration item id "${e.id}"`); n.add(e.id); } const r = t.find((e) => (e.status === "planned" || e.status === "conflict") && e.action !== "copy"); if (r) throw new Error(`memory import only supports copy actions; ${r.id} uses ${r.action}`); return { ...e, items: t, summary: summarizeMigrationItems(t) }; } async function planProviderMemoryImport(e) { const t = buildMigrationContext({ runtime: e.runtime ?? silentRuntime, configOverride: e.config, targetAgentId: e.agentId, itemKinds: [MEMORY_ITEM_KIND], overwrite: e.overwrite, json: true }); const n = await e.provider.detect?.(t); if (n && !n.found) return { detection: n, plan: { providerId: e.provider.id, source: n.source ?? "", summary: summarizeMigrationItems([]), items: [] } }; return { detection: n, plan: await bindMemoryMigrationPlanSources(shapeMemoryOnlyPlan(await e.provider.plan(t)), { includeConflicts: e.overwrite === true }) }; } async function applyProviderMemoryImport(e) { return await runMigrationApply({ runtime: e.runtime ?? silentRuntime, providerId: e.provider.id, provider: e.provider, onApplyCompleted: e.onApplyCompleted, opts: { yes: true, json: true, configOverride: e.config, targetAgentId: e.agentId, itemKinds: [MEMORY_ITEM_KIND], itemIds: e.itemIds, overwrite: e.overwrite, preflightPlan: e.preflightPlan, preferHomeBackupOutput: true, allowPartialResult: true } }); } //#endregion export { withMemoryMigrationProviders as i, planProviderMemoryImport as n, withMemoryMigrationOffers as r, applyProviderMemoryImport as t };