Skip to content
Closed
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
22 changes: 19 additions & 3 deletions plugin/opencode/agentmemory-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async function post(path: string, body: Record<string, unknown>, timeoutMs = 500
}
}

/** POST JSON to the agentmemory daemon and parse the JSON response. Returns null on failure. */
async function postJson(path: string, body: Record<string, unknown>): Promise<unknown | null> {
try {
const res = await fetch(`${API}/agentmemory${path}`, {
Expand Down Expand Up @@ -167,8 +168,16 @@ function extractErrorMessage(err: unknown): string {
return String(err ?? "");
}

/**
* Agentmemory capture plugin for OpenCode.
*
* Captures session events (session.created, tool.use, etc.) and forwards them
* to the agentmemory daemon for persistent memory storage. The plugin determines
* the project path from the workspace context at init time, with per-session
* override from the `session.created` event's directory field.
*/
export const AgentmemoryCapturePlugin: Plugin = async (ctx) => {
projectPath = ctx.worktree || ctx.project?.id || process.cwd();
projectPath = ctx.worktree || ctx.project?.worktree || ctx.directory || process.cwd();

return {
event: async ({ event }) => {
Expand All @@ -188,13 +197,20 @@ export const AgentmemoryCapturePlugin: Plugin = async (ctx) => {
// and another `session.created` event during the await could
// rebind it, causing context to be cached against the wrong key.
const sessionId = activeSessionId;
// Use session-specific directory if available and valid;
// fall back to the init-time projectPath otherwise.
// Runtime validation (typeof + length) avoids sending invalid
// values to the /session/start backend.
const sessionProject = (typeof info?.directory === 'string' && info.directory.length > 0)
? info.directory
: projectPath;
const startResult = await postJson("/session/start", {
sessionId,
title: info?.title ?? null,
parentID: info?.parentID ?? null,
version: info?.version ?? null,
project: projectPath,
cwd: projectPath,
project: sessionProject,
cwd: sessionProject,
});
// cache the context returned at session/start so the
// chat.system.transform hook injects it without a second fetch.
Expand Down