UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

23 lines (22 loc) • 1.05 kB
import { computeEntryRev } from "./frontmatter.js"; export async function migrateScope(backend, fromScope, toScope) { if (fromScope === toScope) throw new Error("migrateScope: fromScope and toScope are identical"); const headers = await backend.listHeaders([fromScope]); const entries = await backend.getByIds(headers.map((h) => h.id)); const patches = entries .filter((e) => e.scope === fromScope) .map((e) => { const moved = { ...e, scope: toScope }; moved.rev = computeEntryRev(moved); return { op: "update", id: e.id, entry: moved, baseRev: e.rev }; }); const report = patches.length > 0 ? await backend.applyPatches(patches) : { applied: [], conflicts: [] }; let cursorMoved = false; const cursor = await backend.getConsolidationCursor(fromScope); if (cursor !== undefined) { await backend.setConsolidationCursor(toScope, cursor); cursorMoved = true; } return { moved: report.applied.length, conflicts: report.conflicts, cursorMoved }; }