@@ -5,6 +5,10 @@ import {
55 type MemoryDreamingPhaseName ,
66 type MemoryDreamingStorageConfig ,
77} from "openclaw/plugin-sdk/memory-core-host-status" ;
8+ import {
9+ replaceManagedMarkdownBlock ,
10+ withTrailingNewline ,
11+ } from "openclaw/plugin-sdk/memory-host-markdown" ;
812
913const DAILY_PHASE_HEADINGS : Record < Exclude < MemoryDreamingPhaseName , "deep" > , string > = {
1014 light : "## Light Sleep" ,
@@ -27,36 +31,6 @@ function resolvePhaseMarkers(phase: Exclude<MemoryDreamingPhaseName, "deep">): {
2731 } ;
2832}
2933
30- function withTrailingNewline ( content : string ) : string {
31- return content . endsWith ( "\n" ) ? content : `${ content } \n` ;
32- }
33-
34- function replaceManagedBlock ( params : {
35- original : string ;
36- heading : string ;
37- startMarker : string ;
38- endMarker : string ;
39- body : string ;
40- } ) : string {
41- const managedBlock = `${ params . heading } \n${ params . startMarker } \n${ params . body } \n${ params . endMarker } ` ;
42- const existingPattern = new RegExp (
43- `${ escapeRegex ( params . heading ) } \\n${ escapeRegex ( params . startMarker ) } [\\s\\S]*?${ escapeRegex ( params . endMarker ) } ` ,
44- "m" ,
45- ) ;
46- if ( existingPattern . test ( params . original ) ) {
47- return params . original . replace ( existingPattern , managedBlock ) ;
48- }
49- const trimmed = params . original . trimEnd ( ) ;
50- if ( trimmed . length === 0 ) {
51- return `${ managedBlock } \n` ;
52- }
53- return `${ trimmed } \n\n${ managedBlock } \n` ;
54- }
55-
56- function escapeRegex ( value : string ) : string {
57- return value . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
58- }
59-
6034function resolveDailyMemoryPath ( workspaceDir : string , epochMs : number , timezone ?: string ) : string {
6135 const isoDay = formatMemoryDreamingDay ( epochMs , timezone ) ;
6236 return path . join ( workspaceDir , "memory" , `${ isoDay } .md` ) ;
@@ -103,7 +77,7 @@ export async function writeDailyDreamingPhaseBlock(params: {
10377 throw err ;
10478 } ) ;
10579 const markers = resolvePhaseMarkers ( params . phase ) ;
106- const updated = replaceManagedBlock ( {
80+ const updated = replaceManagedMarkdownBlock ( {
10781 original,
10882 heading : DAILY_PHASE_HEADINGS [ params . phase ] ,
10983 startMarker : markers . start ,
0 commit comments