iai-mcp-opencode/deploy/opencode/iai-mcp-session-init.js

38 lines
938 B
JavaScript

/**
* iai-mcp session-init plugin for OpenCode.
*
* Sends SDK prompt on session.updated to inject the system instruction.
* The model calls memory_session_context on every new session.
*/
export const IaiMcpSessionInit = async ({
client,
project,
directory,
worktree,
experimental_workspace,
serverUrl,
$,
}) => {
const doneSessions = new Set();
return {
event: async ({ event }) => {
if (event.type !== "session.updated") return;
const sid = event.properties?.info?.id;
if (!sid || doneSessions.has(sid)) return;
doneSessions.add(sid);
try {
await client.session.prompt({
path: { id: sid },
body: {
system: "MANDATORY: Call `memory_session_context` (no arguments) BEFORE answering any user query.",
parts: [{ type: "text", text: "INIT" }],
noReply: false,
},
});
} catch {}
},
};
};