opencode-memoir 2.0.1 → 2.0.3

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
@@ -21,7 +21,7 @@ OpenCode downloads and resolves the package from npm automatically; no manual in
21
21
  ## Quick start
22
22
 
23
23
  1. Install the plugin (see above)
24
- 2. Start coding — the agent can use `memoir_recall`, `memoir_remember`, `memoir_get` MCP tools automatically
24
+ 2. Start coding — the agent can use `memoir_memoir_recall`, `memoir_memoir_remember`, `memoir_memoir_get` MCP tools automatically
25
25
 
26
26
  ## Store configuration
27
27
 
@@ -58,7 +58,7 @@ All optional:
58
58
 
59
59
  ## How it works
60
60
 
61
- Instead of wrapping the `memoir` CLI and re-implementing tools in TypeScript, this plugin registers `memoir-mcp` as a dynamic MCP server. OpenCode starts the server via `uvx --from memoir-ai[mcp] memoir-mcp`, and all memoir tools (`memoir_recall`, `memoir_remember`, `memoir_get`, etc.) are available natively to the LLM.
61
+ Instead of wrapping the `memoir` CLI and re-implementing tools in TypeScript, this plugin registers `memoir-mcp` as a dynamic MCP server. OpenCode starts the server via `uvx --from memoir-ai[mcp] memoir-mcp`, and all memoir tools (`memoir_memoir_recall`, `memoir_memoir_remember`, `memoir_memoir_get`, etc.) are available natively to the LLM.
62
62
 
63
63
  ## Development
64
64
 
package/dist/index.js CHANGED
@@ -98,7 +98,7 @@ async function autoMatchMemoirBranch(store, sessionID) {
98
98
  const data = JSON.parse(branchResult);
99
99
  const branches = data?.branches ?? [];
100
100
  if (!branches.includes(codeBranch)) {
101
- await callMemoir(["branch", codeBranch, "--from", "main"], store);
101
+ await callMemoir(["branch", codeBranch], store);
102
102
  }
103
103
  await callMemoir(["checkout", codeBranch], store);
104
104
  setCachedBranch(sessionID, codeBranch);
@@ -142,7 +142,7 @@ Memory rules:
142
142
  - Do not use preferences.* paths unless the user explicitly stated a preference.
143
143
  - If a fact is your interpretation, do not save it; report it as uncertain instead.
144
144
 
145
- Then call memoir_remember with replace=true for durable onboarding facts. Use namespace codebase:onboard in git repositories and project:onboard outside git.`
145
+ Then call memoir_memoir_remember with replace=true for durable onboarding facts. Use namespace codebase:onboard in git repositories and project:onboard outside git.`
146
146
  };
147
147
  const mcp = config.mcp ?? {};
148
148
  mcp.memoir = {
@@ -176,13 +176,13 @@ Then call memoir_remember with replace=true for durable onboarding facts. Use na
176
176
  if (input.sessionID && !sessionsWithStartupHint.has(sid)) {
177
177
  sessionsWithStartupHint.add(sid);
178
178
  output.system?.unshift(
179
- '[memoir] You have Memoir MCP tools (memoir_recall, memoir_remember, memoir_get, memoir_summarize, memoir_status, memoir_branches, memoir_checkout).\nSAVE (memoir_remember) after: task completion, user stating preferences/constraints, discovering project facts. RECALL (memoir_recall \u2192 memoir_get) at session start for prior context, when user asks about prior work.\nNamespaces: "default" (user context), "codebase:onboard" (project facts in git repos). Taxonomy paths: preferences.*, project.*, codebase.*, decisions.*, learning.*.'
179
+ '[memoir] You have Memoir MCP tools (memoir_memoir_recall, memoir_memoir_remember, memoir_memoir_get, memoir_memoir_summarize, memoir_memoir_status, memoir_memoir_branches, memoir_memoir_checkout).\nSAVE (memoir_memoir_remember) after: task completion, user stating preferences/constraints, discovering project facts. RECALL (memoir_memoir_recall \u2192 memoir_memoir_get) at session start for prior context, when user asks about prior work.\nNamespaces: "default" (user context), "codebase:onboard" (project facts in git repos). Taxonomy paths: preferences.*, project.*, codebase.*, decisions.*, learning.*.'
180
180
  );
181
181
  }
182
182
  const count = sessionMsgCount.get(sid) ?? 0;
183
183
  if (shouldRemind(count)) {
184
184
  output.system?.push(
185
- "\n[memoir] Reminder: Use memoir_remember to save context, memoir_recall to retrieve prior context."
185
+ "\n[memoir] Reminder: Use memoir_memoir_remember to save context, memoir_memoir_recall to retrieve prior context."
186
186
  );
187
187
  }
188
188
  } catch (e) {
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/debug.ts", "../src/memory-saver.ts", "../src/store.ts", "../src/index.ts"],
4
- "sourcesContent": ["/**\n * Debug logger writing to stderr. Prefixes with [memoir] for easy filtering.\n * Only writes when MEMOIR_DEBUG=1 is set.\n */\nexport function debugLog(...args: unknown[]): void {\n if (process.env.MEMOIR_DEBUG !== \"1\") return;\n const ts = new Date().toISOString();\n process.stderr.write(`[memoir ${ts}] ${args.map((a) => String(a)).join(\" \")}\\n`);\n}\n", "/** Per-session message count for periodic reminders. */\nexport const sessionMsgCount = new Map<string, number>();\n\nconst REMINDER_INTERVAL = Math.max(0, parseInt(process.env.MEMOIR_REMINDER_INTERVAL ?? \"5\", 10));\n\nexport function incrementMsgCount(sessionID: string): number {\n const count = (sessionMsgCount.get(sessionID) ?? 0) + 1;\n sessionMsgCount.set(sessionID, count);\n return count;\n}\n\nexport function shouldRemind(count: number): boolean {\n return REMINDER_INTERVAL > 0 && count > 1 && count % REMINDER_INTERVAL === 0;\n}\n\nexport function pruneAll(): void {\n sessionMsgCount.clear();\n}\n", "import { execFile, execFileSync } from \"node:child_process\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport { promisify } from \"node:util\";\nimport { debugLog } from \"./debug.js\";\n\nconst execFileAsync = promisify(execFile);\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n/** Resolve memoir store path.\n * 1. Plugin `store` option override\n * 2. MEMOIR_STORE env var\n * 3. Git root \u2192 `~/.memoir/<slug>` (all worktrees share one store)\n * 4. Resolved cwd \u2192 `~/.memoir/<slug>` for non-git dirs\n */\nexport function deriveStorePath(cwd = process.cwd()): string {\n if (pluginStoreOverride) return pluginStoreOverride;\n const envStore = process.env.MEMOIR_STORE;\n if (envStore) return envStore;\n\n let projectDir: string;\n try {\n const gitRoot = execFileSync(\"git\", [\"rev-parse\", \"--show-toplevel\"], {\n cwd,\n encoding: \"utf8\",\n timeout: 3_000,\n }).trim();\n projectDir = gitRoot;\n } catch {\n projectDir = resolve(cwd);\n }\n\n const slug = projectDir.replace(/[/.]/g, \"-\");\n return join(homedir(), \".memoir\", slug);\n}\n\nexport let pluginStoreOverride: string | undefined;\n\nexport function setPluginStoreOverride(store: string | undefined): void {\n pluginStoreOverride = store;\n}\n\n/** Get current git branch (empty string if not in a git repo). */\nexport function currentGitBranch(cwd = process.cwd()): string {\n try {\n return execFileSync(\"git\", [\"branch\", \"--show-current\"], {\n cwd,\n encoding: \"utf8\",\n timeout: 3_000,\n }).trim();\n } catch {\n return \"\";\n }\n}\n\nconst branchCache = new Map<string, string>();\n\nexport function getCachedBranch(sessionID: string): string {\n return branchCache.get(sessionID) ?? \"\";\n}\n\nexport function setCachedBranch(sessionID: string, branch: string): void {\n branchCache.set(sessionID, branch);\n}\n\n/** Call the memoir CLI via uvx. */\nexport async function callMemoir(args: string[], store: string): Promise<string | null> {\n try {\n const { stdout } = (await execFileAsync(\n \"uvx\",\n [\"--from\", \"memoir-ai\", \"memoir\", \"-s\", store, ...args],\n {\n encoding: \"utf8\",\n timeout: 15_000,\n maxBuffer: 1_024 * 1_024,\n },\n )) as { stdout: string };\n return stdout.trim();\n } catch (e) {\n debugLog(\"callMemoir failed:\", errorMessage(e));\n return null;\n }\n}\n\nexport async function autoMatchMemoirBranch(store: string, sessionID: string): Promise<void> {\n const codeBranch = currentGitBranch();\n if (!codeBranch) return;\n\n const cached = getCachedBranch(sessionID);\n if (cached === codeBranch) return;\n\n // Check if branch exists in memoir\n const branchResult = await callMemoir([\"--json\", \"branch\"], store);\n if (branchResult === null) return;\n\n try {\n const data = JSON.parse(branchResult);\n const branches: string[] = data?.branches ?? [];\n if (!branches.includes(codeBranch)) {\n await callMemoir([\"branch\", codeBranch, \"--from\", \"main\"], store);\n }\n await callMemoir([\"checkout\", codeBranch], store);\n setCachedBranch(sessionID, codeBranch);\n } catch (e) {\n debugLog(\"autoMatchMemoirBranch: parsing failed:\", errorMessage(e));\n }\n}\n\nexport function pruneBranchCache(): void {\n branchCache.clear();\n}\n", "import type { Config, Plugin, PluginModule } from \"@opencode-ai/plugin\";\nimport { debugLog } from \"./debug.js\";\nimport { incrementMsgCount, pruneAll, sessionMsgCount, shouldRemind } from \"./memory-saver.js\";\nimport {\n autoMatchMemoirBranch,\n callMemoir,\n deriveStorePath,\n pruneBranchCache,\n setPluginStoreOverride,\n} from \"./store.js\";\n\nconst sessionsWithStartupHint = new Set<string>();\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\nconst MemoirOpenCode: Plugin = async (_input, rawOptions) => {\n const opts = (rawOptions ?? {}) as { store?: string };\n if (opts.store) setPluginStoreOverride(opts.store);\n\n const storePath = deriveStorePath();\n\n const mcpCommand = [\"uvx\", \"--from\", \"memoir-ai[mcp]\", \"memoir-mcp\"];\n if (storePath) {\n mcpCommand.push(\"--store\", storePath);\n }\n\n const hooks: Record<string, unknown> = {\n name: \"memoir\",\n\n config: async (config: Config): Promise<void> => {\n config.command = config.command ?? {};\n config.command[\"memoir:onboard\"] = {\n description: \"Populate or refresh Memoir onboarding for this project\",\n template: `Populate or refresh Memoir onboarding for the current project.\n\nWorkflow:\n- First obtain a project file tree to understand structure.\n- Start studying from project documentation.\n- Continue only based on what the tree and documentation show.\n\nMemory rules:\n- Record only verified facts from files/docs/code or explicit user statements.\n- Do not write inferred user thoughts, intentions, preferences, or opinions.\n- Do not use preferences.* paths unless the user explicitly stated a preference.\n- If a fact is your interpretation, do not save it; report it as uncertain instead.\n\nThen call memoir_remember with replace=true for durable onboarding facts. Use namespace codebase:onboard in git repositories and project:onboard outside git.`,\n };\n\n // Register memoir MCP server\n const mcp = config.mcp ?? {};\n mcp.memoir = {\n type: \"local\" as const,\n command: mcpCommand,\n environment: storePath ? { MEMOIR_STORE: storePath } : undefined,\n };\n config.mcp = mcp;\n },\n\n \"shell.env\": async (\n _input: unknown,\n output: { env: Record<string, string> },\n ): Promise<void> => {\n try {\n if (storePath) {\n output.env.MEMOIR_STORE = storePath;\n }\n } catch (e) {\n debugLog(\"shell.env: failed:\", errorMessage(e));\n }\n },\n\n \"chat.message\": async (\n input: { sessionID?: string },\n _output: { parts?: Array<{ type: string; text: string }> },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n incrementMsgCount(sid);\n\n await autoMatchMemoirBranch(storePath, sid);\n } catch (e) {\n debugLog(\"chat.message: failed:\", errorMessage(e));\n }\n },\n\n \"experimental.chat.system.transform\": async (\n input: { sessionID?: string },\n output: { system?: string[] },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n\n if (input.sessionID && !sessionsWithStartupHint.has(sid)) {\n sessionsWithStartupHint.add(sid);\n output.system?.unshift(\n '[memoir] You have Memoir MCP tools (memoir_recall, memoir_remember, memoir_get, memoir_summarize, memoir_status, memoir_branches, memoir_checkout).\\nSAVE (memoir_remember) after: task completion, user stating preferences/constraints, discovering project facts. RECALL (memoir_recall \\u2192 memoir_get) at session start for prior context, when user asks about prior work.\\nNamespaces: \"default\" (user context), \"codebase:onboard\" (project facts in git repos). Taxonomy paths: preferences.*, project.*, codebase.*, decisions.*, learning.*.',\n );\n }\n\n const count = sessionMsgCount.get(sid) ?? 0;\n if (shouldRemind(count)) {\n output.system?.push(\n \"\\n[memoir] Reminder: Use memoir_remember to save context, memoir_recall to retrieve prior context.\",\n );\n }\n } catch (e) {\n debugLog(\"system.transform: failed:\", errorMessage(e));\n }\n },\n\n dispose: async (): Promise<void> => {\n try {\n if (process.env.MEMOIR_AUTO_SAVE === \"1\") {\n for (const [sid, count] of sessionMsgCount) {\n await callMemoir(\n [\n \"remember\",\n `session.${sid}`,\n \"--message\",\n `Session ended \u2014 ${count} user messages exchanged`,\n ],\n storePath,\n );\n }\n }\n } catch (e) {\n debugLog(\"dispose: save failed:\", errorMessage(e));\n }\n sessionsWithStartupHint.clear();\n pruneBranchCache();\n pruneAll();\n },\n };\n\n return hooks;\n};\n\nconst plugin: PluginModule = {\n id: \"opencode-memoir\",\n server: MemoirOpenCode,\n};\n\nexport default plugin;\n"],
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esContent": ["/**\n * Debug logger writing to stderr. Prefixes with [memoir] for easy filtering.\n * Only writes when MEMOIR_DEBUG=1 is set.\n */\nexport function debugLog(...args: unknown[]): void {\n if (process.env.MEMOIR_DEBUG !== \"1\") return;\n const ts = new Date().toISOString();\n process.stderr.write(`[memoir ${ts}] ${args.map((a) => String(a)).join(\" \")}\\n`);\n}\n", "/** Per-session message count for periodic reminders. */\nexport const sessionMsgCount = new Map<string, number>();\n\nconst REMINDER_INTERVAL = Math.max(0, parseInt(process.env.MEMOIR_REMINDER_INTERVAL ?? \"5\", 10));\n\nexport function incrementMsgCount(sessionID: string): number {\n const count = (sessionMsgCount.get(sessionID) ?? 0) + 1;\n sessionMsgCount.set(sessionID, count);\n return count;\n}\n\nexport function shouldRemind(count: number): boolean {\n return REMINDER_INTERVAL > 0 && count > 1 && count % REMINDER_INTERVAL === 0;\n}\n\nexport function pruneAll(): void {\n sessionMsgCount.clear();\n}\n", "import { execFile, execFileSync } from \"node:child_process\";\nimport { homedir } from \"node:os\";\nimport { join, resolve } from \"node:path\";\nimport { promisify } from \"node:util\";\nimport { debugLog } from \"./debug.js\";\n\nconst execFileAsync = promisify(execFile);\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\n/** Resolve memoir store path.\n * 1. Plugin `store` option override\n * 2. MEMOIR_STORE env var\n * 3. Git root \u2192 `~/.memoir/<slug>` (all worktrees share one store)\n * 4. Resolved cwd \u2192 `~/.memoir/<slug>` for non-git dirs\n */\nexport function deriveStorePath(cwd = process.cwd()): string {\n if (pluginStoreOverride) return pluginStoreOverride;\n const envStore = process.env.MEMOIR_STORE;\n if (envStore) return envStore;\n\n let projectDir: string;\n try {\n const gitRoot = execFileSync(\"git\", [\"rev-parse\", \"--show-toplevel\"], {\n cwd,\n encoding: \"utf8\",\n timeout: 3_000,\n }).trim();\n projectDir = gitRoot;\n } catch {\n projectDir = resolve(cwd);\n }\n\n const slug = projectDir.replace(/[/.]/g, \"-\");\n return join(homedir(), \".memoir\", slug);\n}\n\nexport let pluginStoreOverride: string | undefined;\n\nexport function setPluginStoreOverride(store: string | undefined): void {\n pluginStoreOverride = store;\n}\n\n/** Get current git branch (empty string if not in a git repo). */\nexport function currentGitBranch(cwd = process.cwd()): string {\n try {\n return execFileSync(\"git\", [\"branch\", \"--show-current\"], {\n cwd,\n encoding: \"utf8\",\n timeout: 3_000,\n }).trim();\n } catch {\n return \"\";\n }\n}\n\nconst branchCache = new Map<string, string>();\n\nexport function getCachedBranch(sessionID: string): string {\n return branchCache.get(sessionID) ?? \"\";\n}\n\nexport function setCachedBranch(sessionID: string, branch: string): void {\n branchCache.set(sessionID, branch);\n}\n\n/** Call the memoir CLI via uvx. */\nexport async function callMemoir(args: string[], store: string): Promise<string | null> {\n try {\n const { stdout } = (await execFileAsync(\n \"uvx\",\n [\"--from\", \"memoir-ai\", \"memoir\", \"-s\", store, ...args],\n {\n encoding: \"utf8\",\n timeout: 15_000,\n maxBuffer: 1_024 * 1_024,\n },\n )) as { stdout: string };\n return stdout.trim();\n } catch (e) {\n debugLog(\"callMemoir failed:\", errorMessage(e));\n return null;\n }\n}\n\nexport async function autoMatchMemoirBranch(store: string, sessionID: string): Promise<void> {\n const codeBranch = currentGitBranch();\n if (!codeBranch) return;\n\n const cached = getCachedBranch(sessionID);\n if (cached === codeBranch) return;\n\n // Check if branch exists in memoir\n const branchResult = await callMemoir([\"--json\", \"branch\"], store);\n if (branchResult === null) return;\n\n try {\n const data = JSON.parse(branchResult);\n const branches: string[] = data?.branches ?? [];\n if (!branches.includes(codeBranch)) {\n await callMemoir([\"branch\", codeBranch], store);\n }\n await callMemoir([\"checkout\", codeBranch], store);\n setCachedBranch(sessionID, codeBranch);\n } catch (e) {\n debugLog(\"autoMatchMemoirBranch: parsing failed:\", errorMessage(e));\n }\n}\n\nexport function pruneBranchCache(): void {\n branchCache.clear();\n}\n", "import type { Config, Plugin, PluginModule } from \"@opencode-ai/plugin\";\nimport { debugLog } from \"./debug.js\";\nimport { incrementMsgCount, pruneAll, sessionMsgCount, shouldRemind } from \"./memory-saver.js\";\nimport {\n autoMatchMemoirBranch,\n callMemoir,\n deriveStorePath,\n pruneBranchCache,\n setPluginStoreOverride,\n} from \"./store.js\";\n\nconst sessionsWithStartupHint = new Set<string>();\n\nfunction errorMessage(e: unknown): string {\n return e instanceof Error ? e.message : String(e);\n}\n\nconst MemoirOpenCode: Plugin = async (_input, rawOptions) => {\n const opts = (rawOptions ?? {}) as { store?: string };\n if (opts.store) setPluginStoreOverride(opts.store);\n\n const storePath = deriveStorePath();\n\n const mcpCommand = [\"uvx\", \"--from\", \"memoir-ai[mcp]\", \"memoir-mcp\"];\n if (storePath) {\n mcpCommand.push(\"--store\", storePath);\n }\n\n const hooks: Record<string, unknown> = {\n name: \"memoir\",\n\n config: async (config: Config): Promise<void> => {\n config.command = config.command ?? {};\n config.command[\"memoir:onboard\"] = {\n description: \"Populate or refresh Memoir onboarding for this project\",\n template: `Populate or refresh Memoir onboarding for the current project.\n\nWorkflow:\n- First obtain a project file tree to understand structure.\n- Start studying from project documentation.\n- Continue only based on what the tree and documentation show.\n\nMemory rules:\n- Record only verified facts from files/docs/code or explicit user statements.\n- Do not write inferred user thoughts, intentions, preferences, or opinions.\n- Do not use preferences.* paths unless the user explicitly stated a preference.\n- If a fact is your interpretation, do not save it; report it as uncertain instead.\n\nThen call memoir_memoir_remember with replace=true for durable onboarding facts. Use namespace codebase:onboard in git repositories and project:onboard outside git.`,\n };\n\n // Register memoir MCP server\n const mcp = config.mcp ?? {};\n mcp.memoir = {\n type: \"local\" as const,\n command: mcpCommand,\n environment: storePath ? { MEMOIR_STORE: storePath } : undefined,\n };\n config.mcp = mcp;\n },\n\n \"shell.env\": async (\n _input: unknown,\n output: { env: Record<string, string> },\n ): Promise<void> => {\n try {\n if (storePath) {\n output.env.MEMOIR_STORE = storePath;\n }\n } catch (e) {\n debugLog(\"shell.env: failed:\", errorMessage(e));\n }\n },\n\n \"chat.message\": async (\n input: { sessionID?: string },\n _output: { parts?: Array<{ type: string; text: string }> },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n incrementMsgCount(sid);\n\n await autoMatchMemoirBranch(storePath, sid);\n } catch (e) {\n debugLog(\"chat.message: failed:\", errorMessage(e));\n }\n },\n\n \"experimental.chat.system.transform\": async (\n input: { sessionID?: string },\n output: { system?: string[] },\n ): Promise<void> => {\n try {\n const sid = input.sessionID ?? \"default\";\n\n if (input.sessionID && !sessionsWithStartupHint.has(sid)) {\n sessionsWithStartupHint.add(sid);\n output.system?.unshift(\n '[memoir] You have Memoir MCP tools (memoir_memoir_recall, memoir_memoir_remember, memoir_memoir_get, memoir_memoir_summarize, memoir_memoir_status, memoir_memoir_branches, memoir_memoir_checkout).\\nSAVE (memoir_memoir_remember) after: task completion, user stating preferences/constraints, discovering project facts. RECALL (memoir_memoir_recall \\u2192 memoir_memoir_get) at session start for prior context, when user asks about prior work.\\nNamespaces: \"default\" (user context), \"codebase:onboard\" (project facts in git repos). Taxonomy paths: preferences.*, project.*, codebase.*, decisions.*, learning.*.',\n );\n }\n\n const count = sessionMsgCount.get(sid) ?? 0;\n if (shouldRemind(count)) {\n output.system?.push(\n \"\\n[memoir] Reminder: Use memoir_memoir_remember to save context, memoir_memoir_recall to retrieve prior context.\",\n );\n }\n } catch (e) {\n debugLog(\"system.transform: failed:\", errorMessage(e));\n }\n },\n\n dispose: async (): Promise<void> => {\n try {\n if (process.env.MEMOIR_AUTO_SAVE === \"1\") {\n for (const [sid, count] of sessionMsgCount) {\n await callMemoir(\n [\n \"remember\",\n `session.${sid}`,\n \"--message\",\n `Session ended \u2014 ${count} user messages exchanged`,\n ],\n storePath,\n );\n }\n }\n } catch (e) {\n debugLog(\"dispose: save failed:\", errorMessage(e));\n }\n sessionsWithStartupHint.clear();\n pruneBranchCache();\n pruneAll();\n },\n };\n\n return hooks;\n};\n\nconst plugin: PluginModule = {\n id: \"opencode-memoir\",\n server: MemoirOpenCode,\n};\n\nexport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": ["errorMessage", "_input"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-memoir",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "Memoir memory plugin for OpenCode — Git-versioned, taxonomy-structured AI memory.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",