UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.21 kB
import{t as formatErrorMessage}from"./errors-D37BJTfI.mjs";import{om as MAX_MEMORY_MIGRATION_ITEMS}from"./src-BDZMFmKa.mjs";import{v as summarizeMigrationItems}from"./migration-B8ObiJfa.mjs";import{r as bindMemoryMigrationPlanSources}from"./memory-migration-source-ChYNCcXZ.mjs";import{t as withPluginMigrationProviders}from"./migration-provider-runtime-lHLy-ONV.mjs";import{r as createMigrationLogger,t as buildMigrationContext}from"./context-DIgQQdr4.mjs";import{t as runMigrationApply}from"./apply-Ds3-dMvM.mjs";import{randomUUID}from"node:crypto"; //!src/commands/migrate/memory-import.ts 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))))}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}})}export{withMemoryMigrationProviders as i,planProviderMemoryImport as n,withMemoryMigrationOffers as r,applyProviderMemoryImport as t};