feat(memory-claudemem): implement lifecycle hooks
Add two hooks: - after_tool_call: fire-and-forget observation to claude-mem (skips memory_* tools to prevent recursion) - before_agent_start: context injection from memory search (wraps in <claude-mem-context> tags) Both hooks have error handling and graceful degradation. Part of claude-mem integration (Phase 4/7). Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
967800c7e3
commit
d9f23cb000
@ -17,7 +17,50 @@ const claudeMemPlugin = {
|
|||||||
`memory-claudemem: plugin registered (worker: ${cfg.workerUrl})`,
|
`memory-claudemem: plugin registered (worker: ${cfg.workerUrl})`,
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Phase 4 - Hook registration
|
// Hook: after_tool_call → Observe tool calls (fire-and-forget)
|
||||||
|
api.on("after_tool_call", async (event, ctx) => {
|
||||||
|
// Skip memory tools to prevent recursion
|
||||||
|
if (event.toolName.startsWith("memory_")) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Fire-and-forget: don't await, let it run in parallel
|
||||||
|
// Use sessionKey as the session identifier (may be undefined)
|
||||||
|
client.observe(
|
||||||
|
ctx.sessionKey ?? "unknown",
|
||||||
|
event.toolName,
|
||||||
|
event.params,
|
||||||
|
event.result,
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
api.logger.warn?.(`memory-claudemem: observation failed: ${err}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Hook: before_agent_start → Context injection from memory search
|
||||||
|
api.on("before_agent_start", async (event) => {
|
||||||
|
// Skip if prompt is empty or too short
|
||||||
|
if (!event.prompt || event.prompt.length < 5) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const results = await client.search(event.prompt, 5);
|
||||||
|
if (results.length === 0) return;
|
||||||
|
|
||||||
|
const memoryContext = results
|
||||||
|
.map((r) => `- [#${r.id}] ${r.title}: ${r.snippet}`)
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
api.logger.info?.(
|
||||||
|
`memory-claudemem: injecting ${results.length} memories into context`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
prependContext: `<claude-mem-context>\nThe following memories may be relevant:\n${memoryContext}\n</claude-mem-context>`,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
api.logger.warn?.(`memory-claudemem: context injection failed: ${err}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// TODO: Phase 5 - Tool registration
|
// TODO: Phase 5 - Tool registration
|
||||||
// TODO: Phase 6 - CLI registration
|
// TODO: Phase 6 - CLI registration
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user