Files
rohitg00__agentmemory/test/openclaw-plugin.test.ts
Rohit Ghumare 48b11e8351 fix(openclaw): claim plugins.slots.memory via registerMemoryCapability (#310)
* fix(openclaw): claim plugins.slots.memory via registerMemoryCapability

The OpenClaw integration declared kind: "memory" in its manifest and
wired before_agent_start / agent_end hooks, but never called
api.registerMemoryCapability(...). On OpenClaw 2026.4.9+ that
left plugins.slots.memory = "agentmemory" reporting unavailable
even though the hooks worked end-to-end and the REST API was
healthy.

Per OpenClaw's plugin SDK declarations
(plugin-sdk/src/plugins/types.d.ts), api.registerMemoryCapability
takes a MemoryPluginCapability and is the canonical way to occupy
the memory slot. All capability fields are optional. The
promptBuilder field has signature:
  (params: { availableTools: Set<string>; citationsMode? }) => string[]

This commit:

- Calls api.registerMemoryCapability({ promptBuilder }) at register
  time when the host exposes it. promptBuilder returns three lines
  describing agentmemory as the active memory provider, including
  the configured base_url so the agent knows where recall is
  coming from.
- Guards on `typeof api.registerMemoryCapability === "function"`
  so older OpenClaw builds (pre-capability API) still load via
  the existing hook-only path. Net behaviour on older hosts is
  unchanged.
- Does NOT register a runtime adapter. OpenClaw's current
  MemoryRuntimeBackendConfig type is exactly { backend: "builtin" }
  or { backend: "qmd"; qmd?: { command?: string } }; both are
  in-process backends and don't fit an external REST service.
  The hook-driven recall + capture flow is the working
  integration path; runtime registration is documented as a
  follow-up gated on an upstream OpenClaw change to add an
  "external" backend variant.

README troubleshooting block adds a specific entry for the
"unavailable slot" symptom, pointing at the registerMemoryCapability
fix. The README also documents the runtime-adapter scope decision
so users running deep host integrations aren't surprised.

3 new tests in test/openclaw-plugin.test.ts assert:
1. registerMemoryCapability is called with a promptBuilder when
   the host supports it.
2. Plugin still registers hooks and does not throw on older hosts
   that don't expose registerMemoryCapability.
3. promptBuilder respects the configured base_url.

871 / 871 tests pass.

Verified against the openclaw npm package surface:
- api.registerMemoryCapability is declared at
  plugin-sdk/src/plugins/types.d.ts:2028
- MemoryPluginCapability is declared at
  plugin-sdk/src/plugins/memory-state.d.ts
- promptBuilder signature matches MemoryPromptSectionBuilder
- No import from openclaw/extensions/* needed (and that path is
  not in the package's exports map anyway)

This is a fresh implementation. The closed PR #302 attempted the
same end goal but imported memoryRuntime from a non-existent
path (openclaw/extensions/memory-core/runtime-api) and would have
failed with ERR_PACKAGE_PATH_NOT_EXPORTED at load time.

* fix(openclaw): accept params arg in promptBuilder to match MemoryPromptSectionBuilder

Reviewer flagged that promptBuilder was declared as () => [...] which
doesn't match the MemoryPluginCapability contract verified from
openclaw@2026.5.7 (plugin-sdk/src/plugins/memory-state.d.ts):

  type MemoryPromptSectionBuilder = (params: {
    availableTools: Set<string>;
    citationsMode?: MemoryCitationsMode;
  }) => string[];

JavaScript would tolerate the zero-arg form at runtime (extra args
are ignored), but the signature was misleading and would fail a
TypeScript port. Updated to (_params) => [...] with a comment
explaining we accept-but-ignore. Underscore-prefix conveys
intentional non-use to lint/readers.

Validation: 3/3 openclaw tests still pass (they invoke
promptBuilder({ availableTools: new Set() }) — both old and new
signatures accept that call).
2026-05-12 23:39:24 +01:00

63 lines
2.7 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
type Capability = {
promptBuilder?: (params: {
availableTools: Set<string>;
}) => string[] | undefined;
};
type RegisterFn = (capability: Capability) => void;
interface FakeApi {
registerMemoryCapability: RegisterFn;
on: ReturnType<typeof vi.fn>;
pluginConfig: Record<string, unknown>;
logger: { warn: ReturnType<typeof vi.fn> };
}
function makeApi(overrides: Partial<FakeApi> = {}): FakeApi {
return {
registerMemoryCapability: vi.fn(),
on: vi.fn(),
pluginConfig: { base_url: "http://localhost:3111" },
logger: { warn: vi.fn() },
...overrides,
};
}
describe("openclaw plugin — memory capability registration (closes #286 follow-up)", () => {
it("calls api.registerMemoryCapability with a promptBuilder when the host supports it", async () => {
const mod = await import("../integrations/openclaw/plugin.mjs");
const plugin = (mod as unknown as { default: { register(api: FakeApi): void } }).default;
const api = makeApi();
plugin.register(api);
expect(api.registerMemoryCapability).toHaveBeenCalledTimes(1);
const capability = (api.registerMemoryCapability as ReturnType<typeof vi.fn>).mock.calls[0][0] as Capability;
expect(typeof capability.promptBuilder).toBe("function");
const lines = capability.promptBuilder?.({ availableTools: new Set() });
expect(Array.isArray(lines)).toBe(true);
expect((lines as string[]).join(" ")).toMatch(/agentmemory/i);
});
it("still registers hooks and tolerates older OpenClaw builds without registerMemoryCapability", async () => {
const mod = await import("../integrations/openclaw/plugin.mjs");
const plugin = (mod as unknown as { default: { register(api: FakeApi): void } }).default;
const api = makeApi({ registerMemoryCapability: undefined as unknown as RegisterFn });
expect(() => plugin.register(api)).not.toThrow();
expect(api.on).toHaveBeenCalled();
const events = (api.on as ReturnType<typeof vi.fn>).mock.calls.map((c) => c[0]);
expect(events).toContain("before_agent_start");
expect(events).toContain("agent_end");
});
it("promptBuilder returns lines that mention the configured base_url", async () => {
const mod = await import("../integrations/openclaw/plugin.mjs");
const plugin = (mod as unknown as { default: { register(api: FakeApi): void } }).default;
const api = makeApi({ pluginConfig: { base_url: "http://memory.internal:9999" } });
plugin.register(api);
const capability = (api.registerMemoryCapability as ReturnType<typeof vi.fn>).mock.calls[0][0] as Capability;
const lines = capability.promptBuilder?.({ availableTools: new Set() }) ?? [];
expect(lines.join("\n")).toMatch(/memory\.internal:9999/);
});
});