Add iai-mcp-session-init plugin for automatic memory loading in OpenCode
This commit is contained in:
parent
ebe7ebf17a
commit
31a69186a3
2 changed files with 68 additions and 7 deletions
35
README.md
35
README.md
|
|
@ -133,15 +133,21 @@ Use the absolute path. `~` and `$HOME` won't expand here.
|
|||
|
||||
For Claude Desktop (untested), edit `~/Library/Application Support/Claude/claude_desktop_config.json`.
|
||||
|
||||
### Install the OpenCode plugin
|
||||
### Install the OpenCode plugins
|
||||
|
||||
This is the OpenCode equivalent of the Claude Code Stop hook. It listens for idle sessions and writes a deferred-capture JSONL file for the daemon to drain.
|
||||
**Capture plugin** — listens for idle sessions and writes a deferred-capture JSONL file for the daemon to drain.
|
||||
|
||||
```bash
|
||||
mkdir -p ~/.config/opencode/plugins
|
||||
cp deploy/opencode/iai-mcp-capture.js ~/.config/opencode/plugins/
|
||||
```
|
||||
|
||||
**Session-init plugin** — automatically loads session background memory on every new session. The model calls `memory_session_context` before responding, so it has your identity, topic cluster, and rich club context from turn one.
|
||||
|
||||
```bash
|
||||
cp deploy/opencode/iai-mcp-session-init.js ~/.config/opencode/plugins/
|
||||
```
|
||||
|
||||
Make sure `@opencode-ai/plugin` is installed:
|
||||
|
||||
```bash
|
||||
|
|
@ -149,12 +155,27 @@ cd ~/.config/opencode
|
|||
npm install @opencode-ai/plugin
|
||||
```
|
||||
|
||||
### Connect OpenCode
|
||||
|
||||
Add the MCP server to `~/.config/opencode/config.json`:
|
||||
Register both plugins in `~/.config/opencode/config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugin": [
|
||||
"~/.config/opencode/plugins/iai-mcp-capture.js",
|
||||
"~/.config/opencode/plugins/iai-mcp-session-init.js"
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### Connect OpenCode
|
||||
|
||||
Add the MCP server and plugins to `~/.config/opencode/config.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"plugin": [
|
||||
"~/.config/opencode/plugins/iai-mcp-capture.js",
|
||||
"~/.config/opencode/plugins/iai-mcp-session-init.js"
|
||||
],
|
||||
"mcp": {
|
||||
"iai-mcp": {
|
||||
"type": "local",
|
||||
|
|
@ -165,7 +186,7 @@ Add the MCP server to `~/.config/opencode/config.json`:
|
|||
}
|
||||
```
|
||||
|
||||
Use the absolute path to `mcp-wrapper/dist/index.js`. The plugin file (`iai-mcp-capture.js`) must already be in `~/.config/opencode/plugins/`.
|
||||
Use the absolute path to `mcp-wrapper/dist/index.js`. Both plugin files must already be in `~/.config/opencode/plugins/`.
|
||||
|
||||
### Verify
|
||||
|
||||
|
|
@ -362,7 +383,7 @@ Limitations worth knowing about:
|
|||
|
||||
Claude Code is the primary host, validated in daily use.
|
||||
|
||||
OpenCode is supported via the `iai-mcp-capture.js` plugin (see Install the OpenCode plugin above) and MCP server config in `~/.config/opencode/config.json`.
|
||||
OpenCode is supported via the `iai-mcp-capture.js` and `iai-mcp-session-init.js` plugins (see Install the OpenCode plugins above) and MCP server config in `~/.config/opencode/config.json`.
|
||||
|
||||
Claude Desktop should work (uses `claude_desktop_config.json` instead of `~/.claude.json`) but hasn't been tested end to end.
|
||||
|
||||
|
|
|
|||
40
deploy/opencode/iai-mcp-session-init.js
Normal file
40
deploy/opencode/iai-mcp-session-init.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
* 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}`);
|
||||
},
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue