@mojoaar/opencode-mimir 0.23.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -44,6 +44,11 @@ one the MCP header can use.
44
44
  |---|---|
45
45
  | `mimir_digest` | Returns what this project already knows: paths, titles, and a summary of the newest memories. |
46
46
  | `mimir_remember` | Stores one durable fact at a path like `deploy/rollback`. Writing the same path again replaces it. |
47
+ | `mimir_search` | Hybrid search over this project's memories. |
48
+ | `mimir_learn` | Hands a transcript over; the project's own model writes at most three memories from it. |
49
+
50
+ When OpenCode compacts the conversation it also hands those messages to Mimir,
51
+ which distils at most three memories from them.
47
52
 
48
53
  `tools` decides whether they are added: `auto` (default) leaves them out when an
49
54
  MCP server already points at the same `/mcp` endpoint, `always` adds them
@@ -62,10 +67,10 @@ unreachable, slow, or unconfigured - a session never waits on memory.
62
67
 
63
68
  ## Import an existing project
64
69
 
65
- Until the package is published, run the CLI from a checkout:
70
+ The package ships the importer as its `mimir` binary:
66
71
 
67
72
  ```sh
68
- node packages/opencode-plugin/dist/bin.js ingest .
73
+ npx @mojoaar/opencode-mimir ingest .
69
74
  ```
70
75
 
71
76
  Walks the folder and loads its **documentation and configuration** - READMEs,
package/dist/index.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  import { Plugin } from '@opencode/plugin';
2
2
  export declare const DIGEST_HEADER = "## What this project already knows (Mimir)\n\n";
3
3
  export declare const NUDGE_HEADER = "## Memory check (Mimir)\n\n";
4
+ export declare const MAX_TRANSCRIPT = 32000;
5
+ /** A compaction of nothing is not worth a model call. */
6
+ export declare const MIN_TRANSCRIPT = 500;
7
+ /** Message text is shaped differently by each provider, so read what we can and skip the rest. */
8
+ export declare function transcript(messages: unknown[]): string;
4
9
  /**
5
10
  * One injection point and two tools. The hook is what puts the project's
6
11
  * digest in front of the model before the first turn, so a session starts
package/dist/index.js CHANGED
@@ -14,6 +14,39 @@ const NUDGE_EVERY = 20;
14
14
  const NUDGE = 'If this session settled a decision, a convention or a gotcha that is not in Mimir yet, ' +
15
15
  'call memory_write now at one clear path, or hand the conversation over with mimir_learn, ' +
16
16
  'before the detail is compacted away.';
17
+ export const MAX_TRANSCRIPT = 32_000;
18
+ /** A compaction of nothing is not worth a model call. */
19
+ export const MIN_TRANSCRIPT = 500;
20
+ const LEARN_HINT = 'This session was just compacted. Keep what was decided, what broke and any convention, and skip what Mimir already has.';
21
+ /** Message text is shaped differently by each provider, so read what we can and skip the rest. */
22
+ export function transcript(messages) {
23
+ const lines = [];
24
+ for (const message of messages) {
25
+ if (typeof message !== 'object' || message === null)
26
+ continue;
27
+ const record = message;
28
+ const role = typeof record.role === 'string' ? record.role : 'message';
29
+ const body = textOf(record.content) || textOf(record.parts);
30
+ if (body)
31
+ lines.push(`${role}: ${body}`);
32
+ }
33
+ return lines.join('\n\n').slice(-MAX_TRANSCRIPT);
34
+ }
35
+ function textOf(value) {
36
+ if (typeof value === 'string')
37
+ return value.trim();
38
+ if (!Array.isArray(value))
39
+ return '';
40
+ return value
41
+ .map((part) => {
42
+ if (typeof part === 'string')
43
+ return part;
44
+ const text = part?.text;
45
+ return typeof text === 'string' ? text : '';
46
+ })
47
+ .join('\n')
48
+ .trim();
49
+ }
17
50
  /**
18
51
  * One injection point and two tools. The hook is what puts the project's
19
52
  * digest in front of the model before the first turn, so a session starts
@@ -46,6 +79,8 @@ export default Plugin.define({
46
79
  return;
47
80
  event.system.push({ type: 'text', text: `${DIGEST_HEADER}${text}` });
48
81
  });
82
+ // The reminder rides the message count. A shrink means a compaction just
83
+ // happened, which is the worst moment to lose a detail, so it nudges early.
49
84
  const counts = new Map();
50
85
  const nudge = await ctx.session.hook('context', async (event) => {
51
86
  const state = counts.get(event.sessionID) ?? { turns: 0, nudgedAt: 0 };
@@ -55,11 +90,31 @@ export default Plugin.define({
55
90
  if (due)
56
91
  event.system.push({ type: 'text', text: `${NUDGE_HEADER}${NUDGE}` });
57
92
  });
93
+ // OpenCode fires this for its own compaction and hands over the messages
94
+ // being replaced. A compressed view swapped in by another plugin never
95
+ // reaches it, which is why the message count is not a signal.
96
+ const compacted = new Set();
97
+ const compaction = await ctx.session.hook('compaction', async (event) => {
98
+ // One compaction can ask twice. The second call must not write again.
99
+ if (compacted.has(event.sessionID))
100
+ return;
101
+ const text = transcript(event.messages ?? []);
102
+ if (text.length < MIN_TRANSCRIPT)
103
+ return;
104
+ compacted.add(event.sessionID);
105
+ const sent = await callTool('memory_learn', { repo: repo ?? undefined, transcript: text, hint: LEARN_HINT }, { ...config, timeoutMs: 4000 }).catch(() => null);
106
+ // Nothing is written to event.system here: this request's prompt becomes
107
+ // the summary that replaces the conversation. A failure gives the session
108
+ // its one chance back, so an outage does not use the compaction up.
109
+ if (!sent?.ok)
110
+ compacted.delete(event.sessionID);
111
+ });
58
112
  const tools = await registerTools(ctx, config, repo);
59
113
  return async () => {
60
114
  await tools.dispose();
61
115
  await session.dispose();
62
116
  await nudge.dispose();
117
+ await compaction.dispose();
63
118
  };
64
119
  },
65
120
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojoaar/opencode-mimir",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "Mimir long-term memory for OpenCode sessions",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
package/src/index.ts CHANGED
@@ -18,6 +18,38 @@ const NUDGE =
18
18
  'call memory_write now at one clear path, or hand the conversation over with mimir_learn, ' +
19
19
  'before the detail is compacted away.'
20
20
 
21
+ export const MAX_TRANSCRIPT = 32_000
22
+ /** A compaction of nothing is not worth a model call. */
23
+ export const MIN_TRANSCRIPT = 500
24
+ const LEARN_HINT =
25
+ 'This session was just compacted. Keep what was decided, what broke and any convention, and skip what Mimir already has.'
26
+
27
+ /** Message text is shaped differently by each provider, so read what we can and skip the rest. */
28
+ export function transcript(messages: unknown[]): string {
29
+ const lines: string[] = []
30
+ for (const message of messages) {
31
+ if (typeof message !== 'object' || message === null) continue
32
+ const record = message as { role?: unknown; content?: unknown; parts?: unknown }
33
+ const role = typeof record.role === 'string' ? record.role : 'message'
34
+ const body = textOf(record.content) || textOf(record.parts)
35
+ if (body) lines.push(`${role}: ${body}`)
36
+ }
37
+ return lines.join('\n\n').slice(-MAX_TRANSCRIPT)
38
+ }
39
+
40
+ function textOf(value: unknown): string {
41
+ if (typeof value === 'string') return value.trim()
42
+ if (!Array.isArray(value)) return ''
43
+ return value
44
+ .map((part) => {
45
+ if (typeof part === 'string') return part
46
+ const text = (part as { text?: unknown })?.text
47
+ return typeof text === 'string' ? text : ''
48
+ })
49
+ .join('\n')
50
+ .trim()
51
+ }
52
+
21
53
  /**
22
54
  * One injection point and two tools. The hook is what puts the project's
23
55
  * digest in front of the model before the first turn, so a session starts
@@ -56,21 +88,48 @@ export default Plugin.define({
56
88
  event.system.push({ type: 'text', text: `${DIGEST_HEADER}${text}` })
57
89
  })
58
90
 
91
+ // The reminder rides the message count. A shrink means a compaction just
92
+ // happened, which is the worst moment to lose a detail, so it nudges early.
59
93
  const counts = new Map<string, { turns: number; nudgedAt: number }>()
60
94
  const nudge = await ctx.session.hook('context', async (event) => {
61
95
  const state = counts.get(event.sessionID) ?? { turns: 0, nudgedAt: 0 }
62
96
  const turns = event.messages?.length ?? 0
63
97
  const due = turns < state.turns || turns - state.nudgedAt >= NUDGE_EVERY
64
98
  counts.set(event.sessionID, { turns, nudgedAt: due ? turns : state.nudgedAt })
99
+
65
100
  if (due) event.system.push({ type: 'text', text: `${NUDGE_HEADER}${NUDGE}` })
66
101
  })
67
102
 
103
+ // OpenCode fires this for its own compaction and hands over the messages
104
+ // being replaced. A compressed view swapped in by another plugin never
105
+ // reaches it, which is why the message count is not a signal.
106
+ const compacted = new Set<string>()
107
+ const compaction = await ctx.session.hook('compaction', async (event) => {
108
+ // One compaction can ask twice. The second call must not write again.
109
+ if (compacted.has(event.sessionID)) return
110
+ const text = transcript(event.messages ?? [])
111
+ if (text.length < MIN_TRANSCRIPT) return
112
+ compacted.add(event.sessionID)
113
+
114
+ const sent = await callTool(
115
+ 'memory_learn',
116
+ { repo: repo ?? undefined, transcript: text, hint: LEARN_HINT },
117
+ { ...config, timeoutMs: 4000 },
118
+ ).catch(() => null)
119
+
120
+ // Nothing is written to event.system here: this request's prompt becomes
121
+ // the summary that replaces the conversation. A failure gives the session
122
+ // its one chance back, so an outage does not use the compaction up.
123
+ if (!sent?.ok) compacted.delete(event.sessionID)
124
+ })
125
+
68
126
  const tools = await registerTools(ctx, config, repo)
69
127
 
70
128
  return async () => {
71
129
  await tools.dispose()
72
130
  await session.dispose()
73
131
  await nudge.dispose()
132
+ await compaction.dispose()
74
133
  }
75
134
  },
76
135
  })