1- import { createRequire } from "node:module"
2- import { dirname , resolve } from "node:path"
3- import { pathToFileURL } from "node:url"
41import type { AgentMessage } from "@mariozechner/pi-agent-core"
5- import type {
6- AssembleResult ,
7- BootstrapResult ,
8- CompactResult ,
9- ContextEngine ,
10- } from "openclaw/plugin-sdk"
2+ import { delegateCompactionToRuntime } from "openclaw/plugin-sdk/core"
113import type { BmClient } from "../bm-client.ts"
124import type { BasicMemoryConfig } from "../config.ts"
135import { selectCaptureTurn } from "../hooks/capture.ts"
146import { loadRecallState } from "../hooks/recall.ts"
157import { log } from "../logger.ts"
168
17- const require = createRequire ( import . meta. url )
189export const MAX_ASSEMBLE_RECALL_CHARS = 1200
1910const TRUNCATED_RECALL_SUFFIX = "\n\n[Basic Memory recall truncated]"
2011const SUBAGENT_HANDOFF_FOLDER = "agent/subagents"
2112const MAX_SUBAGENT_RECALL_CHARS = 800
2213
14+ type AssembleResult = {
15+ messages : AgentMessage [ ]
16+ estimatedTokens : number
17+ systemPromptAddition ?: string
18+ }
19+
20+ type BootstrapResult = {
21+ bootstrapped : boolean
22+ importedMessages ?: number
23+ reason ?: string
24+ }
25+
26+ type CompactResult = {
27+ ok : boolean
28+ compacted : boolean
29+ reason ?: string
30+ result ?: {
31+ summary ?: string
32+ firstKeptEntryId ?: string
33+ tokensBefore : number
34+ tokensAfter ?: number
35+ details ?: unknown
36+ sessionId ?: string
37+ sessionFile ?: string
38+ }
39+ }
40+
41+ interface ContextEngine {
42+ readonly info : {
43+ id : string
44+ name : string
45+ version ?: string
46+ ownsCompaction ?: boolean
47+ }
48+ }
49+
2350interface SessionMemoryState {
2451 recallContext : string
2552}
@@ -104,36 +131,6 @@ function buildSubagentCompletionUpdate(params: {
104131 ] . join ( "\n" )
105132}
106133
107- type LegacyContextEngineModule = {
108- LegacyContextEngine : new ( ) => {
109- compact ( params : {
110- sessionId : string
111- sessionFile : string
112- tokenBudget ?: number
113- force ?: boolean
114- currentTokenCount ?: number
115- compactionTarget ?: "budget" | "threshold"
116- customInstructions ?: string
117- runtimeContext ?: Record < string , unknown >
118- } ) : Promise < CompactResult >
119- }
120- }
121-
122- async function loadLegacyContextEngine ( ) : Promise <
123- LegacyContextEngineModule [ "LegacyContextEngine" ]
124- > {
125- const pluginSdkPath = require . resolve ( "openclaw/plugin-sdk" )
126- const legacyPath = resolve (
127- dirname ( pluginSdkPath ) ,
128- "context-engine" ,
129- "legacy.js" ,
130- )
131- const module = ( await import (
132- pathToFileURL ( legacyPath ) . href
133- ) ) as LegacyContextEngineModule
134- return module . LegacyContextEngine
135- }
136-
137134export class BasicMemoryContextEngine implements ContextEngine {
138135 readonly info = {
139136 id : "openclaw-basic-memory" ,
@@ -144,9 +141,6 @@ export class BasicMemoryContextEngine implements ContextEngine {
144141
145142 private readonly sessionState = new Map < string , SessionMemoryState > ( )
146143 private readonly subagentState = new Map < string , SubagentHandoffState > ( )
147- private legacyContextEnginePromise : Promise <
148- InstanceType < LegacyContextEngineModule [ "LegacyContextEngine" ] >
149- > | null = null
150144
151145 constructor (
152146 private readonly client : BmClient ,
@@ -243,8 +237,7 @@ export class BasicMemoryContextEngine implements ContextEngine {
243237 customInstructions ?: string
244238 runtimeContext ?: Record < string , unknown >
245239 } ) : Promise < CompactResult > {
246- const legacy = await this . getLegacyContextEngine ( )
247- return legacy . compact ( params )
240+ return delegateCompactionToRuntime ( params )
248241 }
249242
250243 async prepareSubagentSpawn ( params : {
@@ -315,16 +308,4 @@ export class BasicMemoryContextEngine implements ContextEngine {
315308 this . sessionState . clear ( )
316309 this . subagentState . clear ( )
317310 }
318-
319- private async getLegacyContextEngine ( ) : Promise <
320- InstanceType < LegacyContextEngineModule [ "LegacyContextEngine" ] >
321- > {
322- if ( ! this . legacyContextEnginePromise ) {
323- this . legacyContextEnginePromise = loadLegacyContextEngine ( ) . then (
324- ( LegacyContextEngine ) => new LegacyContextEngine ( ) ,
325- )
326- }
327-
328- return this . legacyContextEnginePromise
329- }
330311}
0 commit comments