41 lines
1 KiB
JavaScript
41 lines
1 KiB
JavaScript
|
|
/**
|
||
|
|
* iai-mcp session-init plugin for OpenCode.
|
||
|
|
*
|
||
|
|
* Sets the system prompt via chat.message hook's UserMessage.system field.
|
||
|
|
* This reaches the main agent and triggers memory_session_context on every session.
|
||
|
|
*/
|
||
|
|
|
||
|
|
export const IaiMcpSessionInit = async ({
|
||
|
|
client,
|
||
|
|
project,
|
||
|
|
directory,
|
||
|
|
worktree,
|
||
|
|
experimental_workspace,
|
||
|
|
serverUrl,
|
||
|
|
$,
|
||
|
|
}) => {
|
||
|
|
const injectedSessions = new Set();
|
||
|
|
|
||
|
|
return {
|
||
|
|
event: async ({ event }) => {
|
||
|
|
if (event.type !== "session.updated") return;
|
||
|
|
const sid = event.properties?.info?.id;
|
||
|
|
if (!sid || injectedSessions.has(sid)) return;
|
||
|
|
injectedSessions.add(sid);
|
||
|
|
},
|
||
|
|
|
||
|
|
"chat.message": async (input, output) => {
|
||
|
|
const sid = input.sessionID;
|
||
|
|
if (!sid || !injectedSessions.has(sid)) return;
|
||
|
|
if (injectedSessions.has(`done-${sid}`)) return;
|
||
|
|
|
||
|
|
if (output.message) {
|
||
|
|
output.message.system =
|
||
|
|
"MANDATORY: Call `memory_session_context` (no arguments) BEFORE answering any user query.";
|
||
|
|
}
|
||
|
|
|
||
|
|
injectedSessions.add(`done-${sid}`);
|
||
|
|
},
|
||
|
|
};
|
||
|
|
};
|