Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions examples/openclaw-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import {
clampScore,
postProcessMemories,
formatMemoryLines,
pickMemoriesForInjection,
} from "./memory-ranking.js";
import { withTimeout } from "./process-manager.js";
import {
Expand Down Expand Up @@ -1090,25 +1090,32 @@ const contextEnginePlugin = {
};
}

const memories = postProcessMemories(result.memories ?? [], {
limit,
const leafOnly = (result.memories ?? []).filter((m) => !m.level || m.level === 2);
const processed = postProcessMemories(leafOnly, {
limit: requestLimit,
scoreThreshold,
});
const memories = pickMemoriesForInjection(processed, limit, query);
if (memories.length === 0) {
return {
content: [{ type: "text", text: "No relevant OpenViking memories found." }],
details: { count: 0, total: result.total ?? 0, scoreThreshold },
};
}
const memoryLines = await buildMemoryLines(
memories,
(uri) => recallClient.read(uri, session.agentId),
{ recallPreferAbstract: false },
);
return {
content: [
{
type: "text",
text: `Found ${memories.length} memories:\n\n${formatMemoryLines(memories)}`,
text: `Found ${memoryLines.length} memories:\n\n${memoryLines.join("\n")}`,
},
],
details: {
count: memories.length,
count: memoryLines.length,
memories,
total: result.total ?? memories.length,
scoreThreshold,
Expand Down
74 changes: 73 additions & 1 deletion examples/openclaw-plugin/tests/ut/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ afterEach(() => {
vi.unstubAllGlobals();
});

function setupPlugin(clientOverrides?: Record<string, unknown>) {
function setupPlugin(
clientOverrides?: Record<string, unknown>,
pluginConfigOverrides?: Record<string, unknown>,
) {
const tools = new Map<string, ToolDef>();
const factoryTools = new Map<string, (ctx: Record<string, unknown>) => ToolDef>();
const commands = new Map<string, CommandDef>();
Expand Down Expand Up @@ -88,6 +91,7 @@ function setupPlugin(clientOverrides?: Record<string, unknown>) {
baseUrl: "http://127.0.0.1:1933",
autoCapture: false,
autoRecall: false,
...pluginConfigOverrides,
},
logger: {
info: vi.fn(),
Expand Down Expand Up @@ -174,6 +178,74 @@ describe("Tool: memory_recall (registration)", () => {
expect(props).toHaveProperty("scoreThreshold");
expect(props).toHaveProperty("targetUri");
});

it("fills L2 content and filters explicit recall results like auto-recall", async () => {
const fetchMock = vi.fn(async (url: string, init?: RequestInit) => {
const requestUrl = new URL(url);
if (requestUrl.pathname === "/api/v1/system/status") {
return okResponse({ user: "default" });
}

if (requestUrl.pathname === "/api/v1/search/find") {
const body = JSON.parse(String(init?.body ?? "{}"));
const targetUri = String(body.target_uri ?? "");
const memories =
targetUri.includes("user")
? [
makeMemory({
uri: "viking://user/default/memories/high",
abstract: "Abstract only text",
score: 0.92,
}),
makeMemory({
uri: "viking://user/default/memories/low",
abstract: "Low score text",
score: 0.05,
}),
]
: [];
return okResponse({ memories, total: memories.length });
}

if (requestUrl.pathname === "/api/v1/content/read") {
expect(requestUrl.searchParams.get("uri")).toBe("viking://user/default/memories/high");
return okResponse("Full L2 content from read");
}

return okResponse({});
});
vi.stubGlobal("fetch", fetchMock);

const { factoryTools, api } = setupPlugin(undefined, {
recallLimit: 1,
recallPreferAbstract: true,
recallScoreThreshold: 0.2,
});
contextEnginePlugin.register(api as any);
const factory = factoryTools.get("memory_recall");
expect(factory).toBeDefined();

const tool = factory!({ sessionId: "test-session", agentId: "main" });
const result = await tool.execute("tc-memory-recall", {
query: "backend preference",
limit: 1,
scoreThreshold: 0.2,
}) as ToolResult;

expect(result.content[0]!.text).toContain("Full L2 content from read");
expect(result.content[0]!.text).not.toContain("Abstract only text");
expect(result.content[0]!.text).not.toContain("Low score text");

const findCalls = fetchMock.mock.calls.filter(([calledUrl]) =>
String(calledUrl).includes("/api/v1/search/find")
);
expect(findCalls).toHaveLength(2);
for (const [, init] of findCalls) {
const body = JSON.parse(String((init as RequestInit).body));
expect(body.limit).toBe(20);
expect(body.score_threshold).toBe(0);
}
});
});

describe("Tool: memory_store (behavioral)", () => {
Expand Down