Skip to content

Commit b0c7bac

Browse files
committed
refactor(plugin-sdk): add memory host aliases
1 parent e7407f8 commit b0c7bac

10 files changed

Lines changed: 120 additions & 31 deletions

File tree

docs/plugins/sdk-migration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ Current bundled provider examples:
291291
| `plugin-sdk/memory-core-host-runtime-cli` | Memory host CLI runtime | Memory host CLI runtime helpers |
292292
| `plugin-sdk/memory-core-host-runtime-core` | Memory host core runtime | Memory host core runtime helpers |
293293
| `plugin-sdk/memory-core-host-runtime-files` | Memory host file/runtime helpers | Memory host file/runtime helpers |
294+
| `plugin-sdk/memory-host-core` | Memory host core runtime alias | Vendor-neutral alias for memory host core runtime helpers |
295+
| `plugin-sdk/memory-host-files` | Memory host file/runtime alias | Vendor-neutral alias for memory host file/runtime helpers |
296+
| `plugin-sdk/memory-host-markdown` | Managed markdown helpers | Shared managed-markdown helpers for memory-adjacent plugins |
297+
| `plugin-sdk/memory-host-status` | Memory host status alias | Vendor-neutral alias for memory host status helpers |
294298
| `plugin-sdk/memory-lancedb` | Bundled memory-lancedb helpers | Memory-lancedb helper surface |
295299
| `plugin-sdk/testing` | Test utilities | Test helpers and mocks |
296300
</Accordion>

docs/plugins/sdk-overview.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ explicitly promotes one as public.
257257
| `plugin-sdk/memory-core-host-runtime-cli` | Memory host CLI runtime helpers |
258258
| `plugin-sdk/memory-core-host-runtime-core` | Memory host core runtime helpers |
259259
| `plugin-sdk/memory-core-host-runtime-files` | Memory host file/runtime helpers |
260+
| `plugin-sdk/memory-host-core` | Vendor-neutral alias for memory host core runtime helpers |
261+
| `plugin-sdk/memory-host-files` | Vendor-neutral alias for memory host file/runtime helpers |
262+
| `plugin-sdk/memory-host-markdown` | Shared managed-markdown helpers for memory-adjacent plugins |
263+
| `plugin-sdk/memory-host-status` | Vendor-neutral alias for memory host status helpers |
260264
| `plugin-sdk/memory-lancedb` | Bundled memory-lancedb helper surface |
261265
</Accordion>
262266

extensions/memory-core/src/dreaming-markdown.ts

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

913
const 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-
6034
function 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,

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,22 @@
695695
"types": "./dist/plugin-sdk/memory-core-host-runtime-files.d.ts",
696696
"default": "./dist/plugin-sdk/memory-core-host-runtime-files.js"
697697
},
698+
"./plugin-sdk/memory-host-core": {
699+
"types": "./dist/plugin-sdk/memory-host-core.d.ts",
700+
"default": "./dist/plugin-sdk/memory-host-core.js"
701+
},
702+
"./plugin-sdk/memory-host-files": {
703+
"types": "./dist/plugin-sdk/memory-host-files.d.ts",
704+
"default": "./dist/plugin-sdk/memory-host-files.js"
705+
},
706+
"./plugin-sdk/memory-host-markdown": {
707+
"types": "./dist/plugin-sdk/memory-host-markdown.d.ts",
708+
"default": "./dist/plugin-sdk/memory-host-markdown.js"
709+
},
710+
"./plugin-sdk/memory-host-status": {
711+
"types": "./dist/plugin-sdk/memory-host-status.d.ts",
712+
"default": "./dist/plugin-sdk/memory-host-status.js"
713+
},
698714
"./plugin-sdk/memory-lancedb": {
699715
"types": "./dist/plugin-sdk/memory-lancedb.d.ts",
700716
"default": "./dist/plugin-sdk/memory-lancedb.js"

scripts/lib/plugin-sdk-entrypoints.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@
163163
"memory-core-host-runtime-cli",
164164
"memory-core-host-runtime-core",
165165
"memory-core-host-runtime-files",
166+
"memory-host-core",
167+
"memory-host-files",
168+
"memory-host-markdown",
169+
"memory-host-status",
166170
"memory-lancedb",
167171
"msteams",
168172
"models-provider-runtime",

src/plugin-sdk/memory-host-core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./memory-core-host-runtime-core.js";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./memory-core-host-runtime-files.js";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { describe, expect, it } from "vitest";
2+
import { replaceManagedMarkdownBlock, withTrailingNewline } from "./memory-host-markdown.js";
3+
4+
describe("withTrailingNewline", () => {
5+
it("preserves trailing newlines", () => {
6+
expect(withTrailingNewline("hello\n")).toBe("hello\n");
7+
});
8+
9+
it("adds a trailing newline when missing", () => {
10+
expect(withTrailingNewline("hello")).toBe("hello\n");
11+
});
12+
});
13+
14+
describe("replaceManagedMarkdownBlock", () => {
15+
it("appends a managed block when missing", () => {
16+
expect(
17+
replaceManagedMarkdownBlock({
18+
original: "# Title\n",
19+
heading: "## Generated",
20+
startMarker: "<!-- start -->",
21+
endMarker: "<!-- end -->",
22+
body: "- first",
23+
}),
24+
).toBe("# Title\n\n## Generated\n<!-- start -->\n- first\n<!-- end -->\n");
25+
});
26+
27+
it("replaces an existing managed block in place", () => {
28+
expect(
29+
replaceManagedMarkdownBlock({
30+
original:
31+
"# Title\n\n## Generated\n<!-- start -->\n- old\n<!-- end -->\n\n## Notes\nkept\n",
32+
heading: "## Generated",
33+
startMarker: "<!-- start -->",
34+
endMarker: "<!-- end -->",
35+
body: "- new",
36+
}),
37+
).toBe("# Title\n\n## Generated\n<!-- start -->\n- new\n<!-- end -->\n\n## Notes\nkept\n");
38+
});
39+
40+
it("supports headingless blocks", () => {
41+
expect(
42+
replaceManagedMarkdownBlock({
43+
original: "alpha\n",
44+
startMarker: "<!-- start -->",
45+
endMarker: "<!-- end -->",
46+
body: "beta",
47+
}),
48+
).toBe("alpha\n\n<!-- start -->\nbeta\n<!-- end -->\n");
49+
});
50+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export type ManagedMarkdownBlockParams = {
2+
original: string;
3+
body: string;
4+
startMarker: string;
5+
endMarker: string;
6+
heading?: string;
7+
};
8+
9+
function escapeRegex(value: string): string {
10+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
11+
}
12+
13+
export function withTrailingNewline(content: string): string {
14+
return content.endsWith("\n") ? content : `${content}\n`;
15+
}
16+
17+
export function replaceManagedMarkdownBlock(params: ManagedMarkdownBlockParams): string {
18+
const headingPrefix = params.heading ? `${params.heading}\n` : "";
19+
const managedBlock = `${headingPrefix}${params.startMarker}\n${params.body}\n${params.endMarker}`;
20+
const existingPattern = new RegExp(
21+
`${params.heading ? `${escapeRegex(params.heading)}\\n` : ""}${escapeRegex(params.startMarker)}[\\s\\S]*?${escapeRegex(params.endMarker)}`,
22+
"m",
23+
);
24+
25+
if (existingPattern.test(params.original)) {
26+
return params.original.replace(existingPattern, managedBlock);
27+
}
28+
29+
const trimmed = params.original.trimEnd();
30+
if (trimmed.length === 0) {
31+
return `${managedBlock}\n`;
32+
}
33+
return `${trimmed}\n\n${managedBlock}\n`;
34+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./memory-core-host-status.js";

0 commit comments

Comments
 (0)