diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index 2f46052ee..4e89baf98 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -160,9 +160,32 @@ const memosLocalPlugin = { configSchema: pluginConfigSchema, register(api: OpenClawPluginApi) { - api.registerMemoryCapability({ - promptBuilder: buildMemoryPromptSection, - }); + // OpenClaw 2026.3.31 split the legacy `registerMemoryCapability` facade + // into three focused registration methods. We prefer the new API when + // the host exposes it; otherwise we fall back to the legacy + // capability registration so older hosts still load. Either method + // being missing is non-fatal — the plugin must remain load-safe. + // + // See: https://github.com/MemTensor/MemOS/issues/1559 + const hostApi = api as OpenClawPluginApi & { + registerMemoryPromptSection?: (builder: typeof buildMemoryPromptSection) => void; + registerMemoryFlushPlan?: (resolver: unknown) => void; + registerMemoryRuntime?: (runtime: unknown) => void; + }; + + if (typeof hostApi.registerMemoryPromptSection === "function") { + hostApi.registerMemoryPromptSection(buildMemoryPromptSection); + } else if (typeof hostApi.registerMemoryCapability === "function") { + hostApi.registerMemoryCapability({ + promptBuilder: buildMemoryPromptSection, + }); + } else { + hostApi.logger?.warn?.( + "memos-local: host SDK exposes neither registerMemoryPromptSection " + + "nor registerMemoryCapability — memory prompt section will not be " + + "installed. Plugin continues without a system prompt prelude.", + ); + } const moduleDir = path.dirname(fileURLToPath(import.meta.url)); const localRequire = createRequire(import.meta.url); diff --git a/apps/memos-local-openclaw/tests/issue-1559-memory-api-migration.test.ts b/apps/memos-local-openclaw/tests/issue-1559-memory-api-migration.test.ts new file mode 100644 index 000000000..915ef7a91 --- /dev/null +++ b/apps/memos-local-openclaw/tests/issue-1559-memory-api-migration.test.ts @@ -0,0 +1,82 @@ +import { describe, it, expect, vi } from "vitest"; +import memosLocalPlugin from "../index"; + +/** + * Regression test for GitHub issue #1559. + * + * OpenClaw 2026.3.31 removed `api.registerMemoryCapability` and replaced it + * with three focused methods. The plugin must: + * 1. Prefer `api.registerMemoryPromptSection(builder)` when the host + * exposes it (new SDK). + * 2. Fall back to `api.registerMemoryCapability({ promptBuilder })` when + * only the legacy method exists (older SDK). + * 3. Never crash when neither method is exposed. + */ + +function makeBaseApi(extra: Record) { + return { + pluginConfig: {}, + config: {}, + resolvePath: (input: string) => + input === "~/.openclaw" ? "/tmp/memos-local-openclaw-1559" : input, + logger: { + debug: () => {}, + info: () => {}, + warn: () => {}, + error: () => {}, + }, + registerTool: () => {}, + registerService: () => {}, + on: () => {}, + ...extra, + } as any; +} + +describe("OpenClaw 2026.3.31 memory-API migration (issue #1559)", () => { + it("calls registerMemoryPromptSection when the new host SDK exposes it", () => { + const registerMemoryPromptSection = vi.fn(); + const registerMemoryCapability = vi.fn(); + const api = makeBaseApi({ + registerMemoryPromptSection, + registerMemoryCapability, // both present: new API must win + }); + + expect(() => memosLocalPlugin.register(api)).not.toThrow(); + + expect(registerMemoryPromptSection).toHaveBeenCalledTimes(1); + const builderArg = registerMemoryPromptSection.mock.calls[0][0]; + expect(typeof builderArg).toBe("function"); + // Builder must return a string[] (prompt section lines) + const out = builderArg({ + availableTools: new Set(["memory_search", "memory_get"]), + }); + expect(Array.isArray(out)).toBe(true); + expect(out.some((line: string) => /memory/i.test(line))).toBe(true); + + // When the new API is wired, the legacy capability call MUST NOT also be + // invoked — otherwise the host would receive a duplicate registration. + expect(registerMemoryCapability).not.toHaveBeenCalled(); + }); + + it("falls back to registerMemoryCapability on hosts without the new API", () => { + const registerMemoryCapability = vi.fn(); + const api = makeBaseApi({ + registerMemoryCapability, // legacy-only host (pre-2026.3.31) + }); + + expect(() => memosLocalPlugin.register(api)).not.toThrow(); + + expect(registerMemoryCapability).toHaveBeenCalledTimes(1); + const capabilityArg = registerMemoryCapability.mock.calls[0][0]; + expect(capabilityArg).toBeDefined(); + expect(typeof capabilityArg.promptBuilder).toBe("function"); + }); + + it("does not crash when neither memory-registration API is present", () => { + const api = makeBaseApi({}); + // The host that triggered #1559 had registerMemoryCapability undefined; + // a future host could conceivably drop both methods. Plugin must remain + // load-safe and surface a warning rather than throwing. + expect(() => memosLocalPlugin.register(api)).not.toThrow(); + }); +});