beans-memory 0.1.4 → 0.1.5

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.id.md CHANGED
@@ -8,11 +8,12 @@
8
8
 
9
9
  <p align="center">
10
10
  <a href="https://github.com/firasmpratama/beans-memory/tags"><img src="https://img.shields.io/github/v/tag/firasmpratama/beans-memory" alt="Tag terbaru"></a>
11
+ <a href="https://www.npmjs.com/package/beans-memory"><img src="https://img.shields.io/npm/v/beans-memory" alt="Versi npm"></a>
11
12
  <a href="LICENSE"><img src="https://img.shields.io/github/license/firasmpratama/beans-memory" alt="Lisensi: MIT"></a>
12
13
  <a href="https://github.com/firasmpratama/beans-memory/stargazers"><img src="https://img.shields.io/github/stars/firasmpratama/beans-memory" alt="GitHub stars"></a>
13
14
  <img src="https://img.shields.io/github/last-commit/firasmpratama/beans-memory" alt="Commit terakhir">
14
15
  <img src="https://img.shields.io/badge/Bun-%3E%3D1.2-000000?logo=bun&logoColor=white" alt="Bun 1.2+">
15
- <img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux-lightgrey" alt="Platform">
16
+ <img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey" alt="Platform">
16
17
  </p>
17
18
 
18
19
  <p align="center">
@@ -21,6 +22,29 @@
21
22
 
22
23
  Agent bisa menyimpan catatan saat bekerja, mencarinya lagi nanti, dan menerima index kecil di awal setiap sesi.
23
24
 
25
+ ## Install
26
+
27
+ Butuh Bun 1.2+ (https://bun.sh). macOS, Linux, dan Windows didukung.
28
+
29
+ Dari npm:
30
+
31
+ ```bash
32
+ bun add -g beans-memory
33
+ beans-memory install
34
+ ```
35
+
36
+ `install` menulis loader plugin OpenCode, menyalin skill bersama untuk OpenCode dan Claude Code, lalu mengonfigurasi MCP untuk klien yang terdeteksi: Codex, Gemini CLI, dan Hermes. Restart OpenCode setelah instalasi pertama.
37
+
38
+ Pilih target secara eksplisit kalau perlu:
39
+
40
+ ```bash
41
+ beans-memory install --opencode --claude --codex --gemini --hermes
42
+ beans-memory install --all
43
+ beans-memory install --dry-run
44
+ ```
45
+
46
+ Config yang sudah ada tidak pernah ditimpa sembarangan. Kalau config target sudah mendefinisikan `beans-memory` tanpa blok terkelola, installer membiarkannya dan menampilkan snippet untuk ditempel manual. Untuk menjalankan dari source, lihat Mulai cepat dari source di bawah.
47
+
24
48
  ## Cara kerja
25
49
 
26
50
  ```
@@ -41,10 +65,12 @@ OpenCode (runtime Bun)
41
65
 
42
66
  - OpenCode 2
43
67
  - Bun 1.2+ untuk CLI, server MCP, importer, dan test. Tidak perlu untuk plugin saja: plugin berjalan di Bun bawaan OpenCode.
44
- - `curl -fsSL https://bun.sh/install | bash`
45
- - macOS atau Linux. Windows secara teori bisa, belum diuji.
68
+ - `curl -fsSL https://bun.sh/install | bash` di macOS/Linux, atau `powershell -c "irm bun.sh/install.ps1 | iex"` di Windows.
69
+ - macOS, Linux, atau Windows.
46
70
 
47
- ## Mulai cepat
71
+ ## Mulai cepat dari source
72
+
73
+ Pakai ini kalau ingin menjalankan kode dari disk. Untuk instalasi biasa, pakai npm di atas.
48
74
 
49
75
  ```bash
50
76
  git clone https://github.com/firasmpratama/beans-memory ~/tools/beans-memory
@@ -52,6 +78,8 @@ cd ~/tools/beans-memory
52
78
  ./install.sh
53
79
  ```
54
80
 
81
+ `install.sh` berjalan di macOS dan Linux. Di Windows, jalankan `bun src/cli.ts install --opencode` dari repo.
82
+
55
83
  Restart OpenCode, lalu minta agent memanggil `memory_stats`. Di instalasi baru hasilnya nol catatan.
56
84
 
57
85
  Script memasang loader plugin, membuat launcher CLI `beans-memory` kalau Bun tersedia, dan menyalin skill. Script tidak mengubah `opencode.json` dan tidak menyalakan service.
@@ -138,6 +166,12 @@ Dengan panduan di `AGENTS.md`, agent sebaiknya menulis catatan singkat: apa yang
138
166
 
139
167
  ## Server MCP (opsional)
140
168
 
169
+ Setelah `bun add -g beans-memory`, server juga tersedia sebagai `beans-memory-mcp` di PATH. Untuk klien yang menerima array command:
170
+
171
+ ```jsonc
172
+ { "command": ["beans-memory-mcp"] }
173
+ ```
174
+
141
175
  Untuk klien yang tidak bisa memuat plugin:
142
176
 
143
177
  ```jsonc
@@ -174,7 +208,15 @@ Contoh Claude Desktop:
174
208
 
175
209
  ## CLI dan skill (opsional)
176
210
 
177
- Store yang sama bisa dipakai tanpa registrasi tool:
211
+ Store yang sama bisa dipakai tanpa registrasi tool. Install CLI global dari npm, lalu daftarkan plugin dan skill sekali:
212
+
213
+ ```bash
214
+ bun add -g beans-memory
215
+ beans-memory install
216
+ beans-memory stats
217
+ ```
218
+
219
+ Atau jalankan langsung dari repo:
178
220
 
179
221
  ```bash
180
222
  bun src/cli.ts search "query" --limit 5
@@ -206,7 +248,7 @@ Kalau ingin skill sebagai satu-satunya jalur, set `MEMORY_TOOLS=off` sebelum Ope
206
248
 
207
249
  Server MCP bisa dipakai klien apa pun yang mendukung stdio MCP. Semua agent di mesin yang sama berbagi satu database, jadi catatan dari Claude Code terlihat di Codex, Gemini CLI, dan OpenCode.
208
250
 
209
- File siap pakai ada di `examples/`. Ganti `/ABSOLUTE/PATH/TO/` dengan path sebenarnya.
251
+ `beans-memory install --all` menulis config ini otomatis kalau key belum ada atau blok terkelola sudah ada. File siap pakai di `examples/` untuk setup manual; ganti `/ABSOLUTE/PATH/TO/` dengan path sebenarnya.
210
252
 
211
253
  ### Claude Code
212
254
 
@@ -246,6 +288,8 @@ Lokasi default:
246
288
  ~/.local/share/beans-memory/memory.db
247
289
  ```
248
290
 
291
+ Di Windows path yang sama menjadi `%USERPROFILE%\.local\share\beans-memory\memory.db`.
292
+
249
293
  Ubah dengan `MEMORY_DB=/path/ke/memory.db`. Semua project memakai satu file; kolom `project` yang memisahkan. Nama project diambil dari basename working directory, jadi `~/work/api` menjadi project `api`.
250
294
 
251
295
  Backup, dengan OpenCode tertutup:
@@ -294,6 +338,7 @@ bun src/migrate-legacy-db.ts --from /lama/memory.db
294
338
  - Catatan disimpan apa adanya. Tidak ada redaksi rahasia pada catatan. Jangan simpan token, password, atau isi `.env` ke dalam catatan.
295
339
  - Prompt yang ditangkap otomatis diredaksi untuk pola rahasia umum: API key OpenAI/GitHub/AWS/Google, webhook Slack/Discord, dan pola `password=` / `token:`. Redaksi ini tidak lengkap, jadi tetap jangan menempelkan rahasia ke chat.
296
340
  - Database bisa dibaca proses apa pun yang berjalan sebagai user kamu. Perlakukan seperti file catatan, bukan vault.
341
+ - Permission file diset `0600` di macOS dan Linux. Di Windows database mengikuti ACL profil user.
297
342
  - Laporkan kerentanan lewat [pelaporan privat GitHub](https://github.com/firasmpratama/beans-memory/security/advisories/new), bukan issue publik. Lihat [SECURITY.md](.github/SECURITY.md) untuk isi laporan.
298
343
 
299
344
  ## Troubleshooting
@@ -332,6 +377,7 @@ src/repo.ts query: sesi, prompt, observasi, pencarian
332
377
  src/plugin.ts plugin OpenCode: hook dan tool
333
378
  src/mcp.ts server MCP stdio
334
379
  src/cli.ts CLI untuk pemakaian lewat skill
380
+ src/install.ts installer multi-agent: opencode, claude, codex, gemini, hermes
335
381
  src/time.ts format UTC ke waktu lokal
336
382
  src/redact.ts redaksi rahasia untuk prompt yang ditangkap
337
383
  src/rehash.ts migrasi hash sekali jalan
package/README.md CHANGED
@@ -8,11 +8,12 @@
8
8
 
9
9
  <p align="center">
10
10
  <a href="https://github.com/firasmpratama/beans-memory/tags"><img src="https://img.shields.io/github/v/tag/firasmpratama/beans-memory" alt="Latest tag"></a>
11
+ <a href="https://www.npmjs.com/package/beans-memory"><img src="https://img.shields.io/npm/v/beans-memory" alt="npm version"></a>
11
12
  <a href="LICENSE"><img src="https://img.shields.io/github/license/firasmpratama/beans-memory" alt="License: MIT"></a>
12
13
  <a href="https://github.com/firasmpratama/beans-memory/stargazers"><img src="https://img.shields.io/github/stars/firasmpratama/beans-memory" alt="GitHub stars"></a>
13
14
  <img src="https://img.shields.io/github/last-commit/firasmpratama/beans-memory" alt="Last commit">
14
15
  <img src="https://img.shields.io/badge/Bun-%3E%3D1.2-000000?logo=bun&logoColor=white" alt="Bun 1.2+">
15
- <img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux-lightgrey" alt="Platforms">
16
+ <img src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey" alt="Platforms">
16
17
  </p>
17
18
 
18
19
  <p align="center">
@@ -21,6 +22,29 @@
21
22
 
22
23
  The agent can save notes while it works, search them later, and gets a small index injected at the start of each session.
23
24
 
25
+ ## Install
26
+
27
+ Requires Bun 1.2+ (https://bun.sh). macOS, Linux, and Windows are supported.
28
+
29
+ From npm:
30
+
31
+ ```bash
32
+ bun add -g beans-memory
33
+ beans-memory install
34
+ ```
35
+
36
+ `install` writes the OpenCode plugin loader, copies the shared skill for OpenCode and Claude Code, and configures MCP for the clients it detects: Codex, Gemini CLI, and Hermes. Restart OpenCode after the first install.
37
+
38
+ Pick targets explicitly when needed:
39
+
40
+ ```bash
41
+ beans-memory install --opencode --claude --codex --gemini --hermes
42
+ beans-memory install --all
43
+ beans-memory install --dry-run
44
+ ```
45
+
46
+ Existing configs are never replaced blindly. If a target config already defines `beans-memory` without the managed block, the installer leaves it untouched and prints the snippet to add by hand. To run from source instead, see Quick start from source below.
47
+
24
48
  ## How it works
25
49
 
26
50
  ```
@@ -41,10 +65,12 @@ OpenCode (Bun runtime)
41
65
 
42
66
  - OpenCode 2
43
67
  - Bun 1.2+ for the CLI, MCP server, importers, and tests. Not needed for the plugin alone: it runs inside OpenCode's bundled Bun.
44
- - `curl -fsSL https://bun.sh/install | bash`
45
- - macOS or Linux. Windows works in theory but is untested.
68
+ - `curl -fsSL https://bun.sh/install | bash` on macOS/Linux, or `powershell -c "irm bun.sh/install.ps1 | iex"` on Windows.
69
+ - macOS, Linux, or Windows.
46
70
 
47
- ## Quick start
71
+ ## Quick start from source
72
+
73
+ Use this when you want to run the code from disk. For a normal install, use npm above.
48
74
 
49
75
  ```bash
50
76
  git clone https://github.com/firasmpratama/beans-memory ~/tools/beans-memory
@@ -52,6 +78,8 @@ cd ~/tools/beans-memory
52
78
  ./install.sh
53
79
  ```
54
80
 
81
+ `install.sh` runs on macOS and Linux. On Windows, run `bun src/cli.ts install --opencode` from the repository instead.
82
+
55
83
  Then restart OpenCode and ask the agent to call `memory_stats`. On a fresh install it returns zero notes.
56
84
 
57
85
  The installer writes the plugin loader, creates the `beans-memory` CLI launcher when Bun is available, and installs the skill. It does not edit `opencode.json` and does not start a service.
@@ -138,6 +166,12 @@ Guided by `AGENTS.md`, the agent should keep notes short: what changed, why, and
138
166
 
139
167
  ## MCP server (optional)
140
168
 
169
+ After `bun add -g beans-memory`, the server is also available as `beans-memory-mcp` on PATH. For clients that accept a command array:
170
+
171
+ ```jsonc
172
+ { "command": ["beans-memory-mcp"] }
173
+ ```
174
+
141
175
  For clients that cannot load the plugin:
142
176
 
143
177
  ```jsonc
@@ -174,7 +208,15 @@ Claude Desktop example:
174
208
 
175
209
  ## CLI and skill (optional)
176
210
 
177
- The same store is available without any tool registration:
211
+ The same store is available without any tool registration. Install the CLI globally from npm, then register the plugin and skill once:
212
+
213
+ ```bash
214
+ bun add -g beans-memory
215
+ beans-memory install
216
+ beans-memory stats
217
+ ```
218
+
219
+ Or run it from the repository:
178
220
 
179
221
  ```bash
180
222
  bun src/cli.ts search "query" --limit 5
@@ -206,7 +248,7 @@ When the skill is the only path you want, set `MEMORY_TOOLS=off` before starting
206
248
 
207
249
  The MCP server works with any client that speaks stdio MCP. Every agent on the same machine shares one database, so a note saved from Claude Code is visible in Codex, Gemini CLI, and OpenCode.
208
250
 
209
- Ready-to-copy files live in `examples/`. Replace `/ABSOLUTE/PATH/TO/` with real paths first.
251
+ `beans-memory install --all` writes these configs automatically when the key is absent or when the managed block exists. The ready-to-copy files in `examples/` are for manual setup; replace `/ABSOLUTE/PATH/TO/` with real paths first.
210
252
 
211
253
  ### Claude Code
212
254
 
@@ -246,6 +288,8 @@ Default location:
246
288
  ~/.local/share/beans-memory/memory.db
247
289
  ```
248
290
 
291
+ On Windows the same path resolves to `%USERPROFILE%\.local\share\beans-memory\memory.db`.
292
+
249
293
  Override with `MEMORY_DB=/path/to/memory.db`. All projects share one file; the `project` column separates them. The project name comes from the basename of the working directory, so `~/work/api` is project `api`.
250
294
 
251
295
  Backup, with OpenCode closed:
@@ -294,6 +338,7 @@ bun src/migrate-legacy-db.ts --from /old/memory.db
294
338
  - Notes are stored as written. There is no secret redaction for notes. Do not save tokens, passwords, or `.env` contents into a note.
295
339
  - Prompts captured automatically are redacted for common secret patterns: OpenAI/GitHub/AWS/Google API keys, Slack/Discord webhooks, and `password=` / `token:` style assignments. Redaction is best effort, not a guarantee, so still avoid pasting secrets into chat.
296
340
  - The database is readable by any process running as your user. Treat it like a notes file, not a vault.
341
+ - File permissions are set to `0600` on macOS and Linux. On Windows the database inherits the ACL of the user profile.
297
342
  - Report vulnerabilities through [GitHub private vulnerability reporting](https://github.com/firasmpratama/beans-memory/security/advisories/new), not a public issue. See [SECURITY.md](.github/SECURITY.md) for what to include.
298
343
 
299
344
  ## Troubleshooting
@@ -332,6 +377,7 @@ src/repo.ts queries: sessions, prompts, observations, search
332
377
  src/plugin.ts OpenCode plugin: hooks and tools
333
378
  src/mcp.ts MCP stdio server
334
379
  src/cli.ts CLI for skill-based use
380
+ src/install.ts multi-agent installer: opencode, claude, codex, gemini, hermes
335
381
  src/time.ts UTC to local formatting
336
382
  src/redact.ts secret redaction for captured prompts
337
383
  src/rehash.ts one-time hash migration
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "beans-memory",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Local persistent memory for OpenCode and other coding agents: SQLite + FTS5 plugin, MCP server, and CLI",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -13,7 +13,8 @@
13
13
  "url": "https://github.com/firasmpratama/beans-memory/issues"
14
14
  },
15
15
  "bin": {
16
- "beans-memory": "./src/cli.ts"
16
+ "beans-memory": "./src/cli.ts",
17
+ "beans-memory-mcp": "./src/mcp.ts"
17
18
  },
18
19
  "files": [
19
20
  "src",
@@ -9,11 +9,7 @@ metadata:
9
9
 
10
10
  Memory lokal berbasis SQLite. Tidak ada cloud, tidak ada akun.
11
11
 
12
- CLI: `beans-memory` (launcher di `~/.local/bin/beans-memory`). Kalau launcher tidak tersedia, jalankan langsung:
13
-
14
- ```bash
15
- bun "/Volumes/Beans Ext/projects/ai/beans-memory/src/cli.ts" <command>
16
- ```
12
+ CLI: `beans-memory`. Kalau perintah tidak tersedia, pakai `bunx beans-memory` (paket npm) atau `bun src/cli.ts` dari repo.
17
13
 
18
14
  ## Sebelum mulai
19
15
 
package/src/cli.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env bun
2
2
  import { basename } from "node:path";
3
3
  import { openDb, resolveDbPath } from "./db";
4
+ import { install, TARGETS } from "./install";
4
5
  import {
5
6
  addObservation,
6
7
  getObservations,
@@ -60,6 +61,7 @@ function usage(): never {
60
61
  " beans-memory recent [--limit=N] [--json]",
61
62
  " beans-memory stats [--json]",
62
63
  " beans-memory projects [--json]",
64
+ " beans-memory install [--opencode] [--claude] [--codex] [--gemini] [--hermes] [--all] [--dry-run]",
63
65
  "",
64
66
  "Global: --db <path>, --project <name>",
65
67
  "Env: MEMORY_DB, MEMORY_PROJECT",
@@ -81,6 +83,24 @@ const command = positional.shift();
81
83
 
82
84
  if (!command) usage();
83
85
 
86
+ if (command === "install") {
87
+ const selectedTargets = TARGETS.filter((target) => flags[target] === true);
88
+ const targets = flags.all === true ? [...TARGETS] : selectedTargets;
89
+ try {
90
+ const reports = install({
91
+ targets: targets.length > 0 ? targets : undefined,
92
+ dryRun: flags["dry-run"] === true,
93
+ });
94
+ for (const report of reports) {
95
+ console.log(`${report.target}: ${report.status} · ${report.detail}`);
96
+ }
97
+ } catch (error) {
98
+ console.error(error instanceof Error ? error.message : String(error));
99
+ process.exitCode = 1;
100
+ }
101
+ process.exit();
102
+ }
103
+
84
104
  const dbPath = typeof flags.db === "string" ? flags.db : resolveDbPath();
85
105
  const project =
86
106
  (typeof flags.project === "string" ? flags.project : "") ||
package/src/db.ts CHANGED
@@ -27,6 +27,8 @@ export function resolveDbPath(_directory: string = process.cwd(), home: string =
27
27
 
28
28
  function secure(path: string): void {
29
29
  if (path === ":memory:" || path.startsWith("file::memory:")) return;
30
+ // POSIX mode bits do not apply on Windows; ACLs are inherited from the user profile.
31
+ if (process.platform === "win32") return;
30
32
  try {
31
33
  chmodSync(path, 0o600);
32
34
  } catch (error) {
package/src/install.ts ADDED
@@ -0,0 +1,376 @@
1
+ import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { dirname, join } from "node:path";
4
+
5
+ export const TARGETS = ["opencode", "claude", "codex", "gemini", "hermes"] as const;
6
+
7
+ export type InstallTarget = (typeof TARGETS)[number];
8
+
9
+ export type InstallStatus = "installed" | "skipped" | "manual";
10
+
11
+ export type InstallReport = {
12
+ target: InstallTarget;
13
+ status: InstallStatus;
14
+ detail: string;
15
+ };
16
+
17
+ export type InstallOptions = {
18
+ home?: string;
19
+ targets?: InstallTarget[];
20
+ dryRun?: boolean;
21
+ packageRoot?: string;
22
+ bunPath?: string;
23
+ };
24
+
25
+ const BLOCK_BEGIN = "# beans-memory begin";
26
+ const BLOCK_END = "# beans-memory end";
27
+
28
+ function packageRootFromModule(): string {
29
+ return join(import.meta.dir, "..");
30
+ }
31
+
32
+ function ensureDirectory(path: string, dryRun: boolean): void {
33
+ if (dryRun || existsSync(path)) return;
34
+ mkdirSync(path, { recursive: true });
35
+ }
36
+
37
+ function writeText(path: string, content: string, dryRun: boolean): boolean {
38
+ if (existsSync(path) && readFileSync(path, "utf8") === content) return false;
39
+ if (dryRun) return true;
40
+ ensureDirectory(dirname(path), false);
41
+ writeFileSync(path, content);
42
+ return true;
43
+ }
44
+
45
+ function writeConfig(path: string, source: string | null, next: string, dryRun: boolean): boolean {
46
+ if (source !== null && source === next) return false;
47
+ if (dryRun) return true;
48
+ if (source !== null && existsSync(path)) {
49
+ const backupPath = `${path}.beans-memory.bak`;
50
+ if (!existsSync(backupPath)) {
51
+ copyFileSync(path, backupPath);
52
+ }
53
+ }
54
+ ensureDirectory(dirname(path), false);
55
+ writeFileSync(path, next);
56
+ return true;
57
+ }
58
+
59
+ function skillSource(packageRoot: string): string {
60
+ const path = join(packageRoot, "skills", "beans-memory", "SKILL.md");
61
+ if (!existsSync(path)) throw new Error(`Skill tidak ditemukan: ${path}`);
62
+ return readFileSync(path, "utf8");
63
+ }
64
+
65
+ function installSkill(home: string, packageRoot: string, dryRun: boolean): string {
66
+ const path = join(home, ".claude", "skills", "beans-memory", "SKILL.md");
67
+ writeText(path, skillSource(packageRoot), dryRun);
68
+ return path;
69
+ }
70
+
71
+ function installHermesSkill(home: string, packageRoot: string, dryRun: boolean): string {
72
+ const path = join(home, ".hermes", "skills", "beans-memory", "SKILL.md");
73
+ writeText(path, skillSource(packageRoot), dryRun);
74
+ return path;
75
+ }
76
+
77
+ function loaderContent(entry: string): string {
78
+ return `// Managed by beans-memory install. Re-run the command if the package moves.
79
+ import { pathToFileURL } from "node:url";
80
+
81
+ const ENTRY = ${JSON.stringify(entry)};
82
+
83
+ const noop = {
84
+ id: "memory",
85
+ async setup() {
86
+ return () => {};
87
+ },
88
+ };
89
+
90
+ let plugin: unknown = noop;
91
+
92
+ try {
93
+ const loaded = await import(pathToFileURL(ENTRY).href);
94
+ if (loaded?.default?.setup) plugin = loaded.default;
95
+ } catch (error) {
96
+ console.error("[memory] plugin not loaded:", error);
97
+ }
98
+
99
+ export default plugin;
100
+ `;
101
+ }
102
+
103
+ function findExistingLoader(
104
+ pluginsDirectory: string,
105
+ ): { path: string; managed: boolean } | null {
106
+ if (!existsSync(pluginsDirectory)) return null;
107
+ const candidates: string[] = [];
108
+ for (const entry of readdirSync(pluginsDirectory, { withFileTypes: true })) {
109
+ if (entry.isDirectory()) {
110
+ candidates.push(join(pluginsDirectory, entry.name, "index.ts"));
111
+ } else if (entry.isFile() && entry.name.endsWith(".ts")) {
112
+ candidates.push(join(pluginsDirectory, entry.name));
113
+ }
114
+ }
115
+ const pattern = /(beans|agent)-memory[\\/]+src[\\/]+plugin\.ts/;
116
+ for (const candidate of candidates) {
117
+ if (!existsSync(candidate)) continue;
118
+ const content = readFileSync(candidate, "utf8");
119
+ if (pattern.test(content)) {
120
+ return { path: candidate, managed: content.includes("Managed by beans-memory") };
121
+ }
122
+ }
123
+ return null;
124
+ }
125
+
126
+ function mcpSnippet(bunPath: string, mcpPath: string): string {
127
+ return JSON.stringify(
128
+ {
129
+ mcpServers: {
130
+ "beans-memory": {
131
+ command: bunPath,
132
+ args: [mcpPath],
133
+ },
134
+ },
135
+ },
136
+ null,
137
+ 2,
138
+ );
139
+ }
140
+
141
+ function replaceBlock(source: string, block: string): string | null {
142
+ const begin = source.indexOf(BLOCK_BEGIN);
143
+ const end = source.indexOf(BLOCK_END);
144
+ if (begin < 0 || end < begin) return null;
145
+ return `${source.slice(0, begin)}${block}${source.slice(end + BLOCK_END.length)}`;
146
+ }
147
+
148
+ function appendBlock(source: string, block: string): string {
149
+ const trimmed = source.trimEnd();
150
+ return trimmed.length === 0 ? `${block}\n` : `${trimmed}\n\n${block}\n`;
151
+ }
152
+
153
+ function installOpencode(
154
+ home: string,
155
+ packageRoot: string,
156
+ dryRun: boolean,
157
+ ): InstallReport {
158
+ const pluginsDirectory = join(home, ".config", "opencode", "plugins");
159
+ const existing = findExistingLoader(pluginsDirectory);
160
+ const loaderPath = existing?.path ?? join(pluginsDirectory, "beans-memory", "index.ts");
161
+ const skillPath = installSkill(home, packageRoot, dryRun);
162
+
163
+ if (existing && !existing.managed) {
164
+ return {
165
+ target: "opencode",
166
+ status: "manual",
167
+ detail: `loader kustom terdeteksi dan tidak diubah: ${loaderPath}; skill: ${skillPath}`,
168
+ };
169
+ }
170
+
171
+ const entry = join(packageRoot, "src", "plugin.ts");
172
+ const changed = writeText(loaderPath, loaderContent(entry), dryRun);
173
+ return {
174
+ target: "opencode",
175
+ status: "installed",
176
+ detail: `${changed ? "loader ditulis" : "loader sudah benar"}: ${loaderPath}; skill: ${skillPath}`,
177
+ };
178
+ }
179
+
180
+ function shellQuote(value: string): string {
181
+ return `'${value.replace(/'/g, `'\\''`)}'`;
182
+ }
183
+
184
+ function installClaude(home: string, packageRoot: string, bunPath: string, dryRun: boolean): InstallReport {
185
+ const skillPath = installSkill(home, packageRoot, dryRun);
186
+ const mcpPath = join(packageRoot, "src", "mcp.ts");
187
+ const command = `claude mcp add --scope user --transport stdio beans_memory -- ${shellQuote(bunPath)} ${shellQuote(mcpPath)}`;
188
+ return {
189
+ target: "claude",
190
+ status: "manual",
191
+ detail: `skill: ${skillPath}; MCP manual: ${command}`,
192
+ };
193
+ }
194
+
195
+ function installCodex(home: string, packageRoot: string, bunPath: string, dryRun: boolean): InstallReport {
196
+ const configPath = join(home, ".codex", "config.toml");
197
+ const mcpPath = join(packageRoot, "src", "mcp.ts");
198
+ const block = [
199
+ BLOCK_BEGIN,
200
+ "[mcp_servers.beans_memory]",
201
+ `command = ${JSON.stringify(bunPath)}`,
202
+ `args = [${JSON.stringify(mcpPath)}]`,
203
+ "startup_timeout_sec = 20",
204
+ "tool_timeout_sec = 60",
205
+ BLOCK_END,
206
+ ].join("\n");
207
+
208
+ if (!existsSync(configPath)) {
209
+ writeText(configPath, `${block}\n`, dryRun);
210
+ return { target: "codex", status: "installed", detail: `config ditulis: ${configPath}` };
211
+ }
212
+
213
+ const source = readFileSync(configPath, "utf8");
214
+ const replaced = replaceBlock(source, block);
215
+ if (replaced !== null) {
216
+ writeConfig(configPath, source, replaced, dryRun);
217
+ return { target: "codex", status: "installed", detail: `config diperbarui: ${configPath}` };
218
+ }
219
+ if (/^[ \t]*\[mcp_servers\s*\.\s*["']?beans_memory["']?(?:\s*\.|\s*\])/m.test(source)) {
220
+ return {
221
+ target: "codex",
222
+ status: "skipped",
223
+ detail: `mcp_servers.beans_memory sudah ada di ${configPath}; tidak diubah`,
224
+ };
225
+ }
226
+ writeConfig(configPath, source, appendBlock(source, block), dryRun);
227
+ return { target: "codex", status: "installed", detail: `config ditambahkan: ${configPath}` };
228
+ }
229
+
230
+ function installGemini(home: string, packageRoot: string, bunPath: string, dryRun: boolean): InstallReport {
231
+ const configPath = join(home, ".gemini", "settings.json");
232
+ const mcpPath = join(packageRoot, "src", "mcp.ts");
233
+ const server = {
234
+ command: bunPath,
235
+ args: [mcpPath],
236
+ timeout: 30000,
237
+ trust: false,
238
+ };
239
+ const snippet = mcpSnippet(bunPath, mcpPath);
240
+
241
+ if (!existsSync(configPath)) {
242
+ writeText(configPath, `${JSON.stringify({ mcpServers: { "beans-memory": server } }, null, 2)}\n`, dryRun);
243
+ return { target: "gemini", status: "installed", detail: `config ditulis: ${configPath}` };
244
+ }
245
+
246
+ let parsedRaw: unknown;
247
+ try {
248
+ parsedRaw = JSON.parse(readFileSync(configPath, "utf8"));
249
+ } catch {
250
+ return {
251
+ target: "gemini",
252
+ status: "manual",
253
+ detail: `JSON tidak bisa dibaca, tidak diubah. Tambahkan manual:\n${snippet}`,
254
+ };
255
+ }
256
+
257
+ if (parsedRaw === null || typeof parsedRaw !== "object" || Array.isArray(parsedRaw)) {
258
+ return {
259
+ target: "gemini",
260
+ status: "manual",
261
+ detail: `Format settings tidak dikenali, tidak diubah. Tambahkan manual:\n${snippet}`,
262
+ };
263
+ }
264
+
265
+ const parsed = parsedRaw as Record<string, unknown>;
266
+ const existingServers = parsed.mcpServers;
267
+ if (
268
+ existingServers !== undefined &&
269
+ (existingServers === null || typeof existingServers !== "object" || Array.isArray(existingServers))
270
+ ) {
271
+ return {
272
+ target: "gemini",
273
+ status: "manual",
274
+ detail: `mcpServers bukan objek, tidak diubah. Tambahkan manual:\n${snippet}`,
275
+ };
276
+ }
277
+
278
+ const servers = existingServers ? (existingServers as Record<string, unknown>) : {};
279
+ if (Object.hasOwn(servers, "beans-memory")) {
280
+ return {
281
+ target: "gemini",
282
+ status: "skipped",
283
+ detail: `mcpServers.beans-memory sudah ada di ${configPath}; tidak diubah`,
284
+ };
285
+ }
286
+
287
+ servers["beans-memory"] = server;
288
+ parsed.mcpServers = servers;
289
+ writeConfig(configPath, readFileSync(configPath, "utf8"), `${JSON.stringify(parsed, null, 2)}\n`, dryRun);
290
+ return { target: "gemini", status: "installed", detail: `config diperbarui: ${configPath}` };
291
+ }
292
+
293
+ function installHermes(home: string, packageRoot: string, bunPath: string, dryRun: boolean): InstallReport {
294
+ const skillPath = installHermesSkill(home, packageRoot, dryRun);
295
+ const configPath = join(home, ".hermes", "config.yaml");
296
+ const mcpPath = join(packageRoot, "src", "mcp.ts");
297
+ const block = [
298
+ BLOCK_BEGIN,
299
+ "mcp_servers:",
300
+ " beans-memory:",
301
+ ` command: ${JSON.stringify(bunPath)}`,
302
+ " args:",
303
+ ` - ${JSON.stringify(mcpPath)}`,
304
+ BLOCK_END,
305
+ ].join("\n");
306
+
307
+ if (!existsSync(configPath)) {
308
+ writeText(configPath, `${block}\n`, dryRun);
309
+ return {
310
+ target: "hermes",
311
+ status: "installed",
312
+ detail: `skill: ${skillPath}; config ditulis: ${configPath}`,
313
+ };
314
+ }
315
+
316
+ const source = readFileSync(configPath, "utf8");
317
+ const replaced = replaceBlock(source, block);
318
+ if (replaced !== null) {
319
+ writeConfig(configPath, source, replaced, dryRun);
320
+ return {
321
+ target: "hermes",
322
+ status: "installed",
323
+ detail: `skill: ${skillPath}; config diperbarui: ${configPath}`,
324
+ };
325
+ }
326
+ if (/^[ \t]*["']?mcp_servers["']?\s*:/m.test(source)) {
327
+ return {
328
+ target: "hermes",
329
+ status: "manual",
330
+ detail: `skill: ${skillPath}; mcp_servers sudah ada di ${configPath}. Tambahkan manual:\n${block}`,
331
+ };
332
+ }
333
+
334
+ writeConfig(configPath, source, appendBlock(source, block), dryRun);
335
+ return {
336
+ target: "hermes",
337
+ status: "installed",
338
+ detail: `skill: ${skillPath}; config ditambahkan: ${configPath}`,
339
+ };
340
+ }
341
+
342
+ function detectTargets(home: string): InstallTarget[] {
343
+ const detected: InstallTarget[] = [];
344
+ if (existsSync(join(home, ".config", "opencode"))) detected.push("opencode");
345
+ if (existsSync(join(home, ".claude"))) detected.push("claude");
346
+ if (existsSync(join(home, ".codex"))) detected.push("codex");
347
+ if (existsSync(join(home, ".gemini"))) detected.push("gemini");
348
+ if (existsSync(join(home, ".hermes"))) detected.push("hermes");
349
+ return detected;
350
+ }
351
+
352
+ export function install(options: InstallOptions = {}): InstallReport[] {
353
+ const home = options.home ?? homedir();
354
+ const packageRoot = options.packageRoot ?? packageRootFromModule();
355
+ const bunPath = options.bunPath ?? process.execPath;
356
+ const dryRun = options.dryRun ?? false;
357
+ const visibleTargets = options.targets ?? detectTargets(home);
358
+
359
+ if (visibleTargets.length === 0) {
360
+ throw new Error(
361
+ "Tidak ada agent yang terdeteksi. Pilih target: --opencode, --claude, --codex, --gemini, --hermes, atau --all.",
362
+ );
363
+ }
364
+
365
+ const reports: InstallReport[] = [];
366
+ for (const target of visibleTargets) {
367
+ if (target === "opencode") reports.push(installOpencode(home, packageRoot, dryRun));
368
+ if (target === "claude") reports.push(installClaude(home, packageRoot, bunPath, dryRun));
369
+ if (target === "codex") reports.push(installCodex(home, packageRoot, bunPath, dryRun));
370
+ if (target === "gemini") reports.push(installGemini(home, packageRoot, bunPath, dryRun));
371
+ if (target === "hermes") reports.push(installHermes(home, packageRoot, bunPath, dryRun));
372
+ }
373
+ return dryRun
374
+ ? reports.map((report) => ({ ...report, detail: `[dry-run] ${report.detail}` }))
375
+ : reports;
376
+ }
package/src/mcp.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { basename } from "node:path";
1
+ #!/usr/bin/env bun
2
+ import { basename, join } from "node:path";
2
3
  import { createInterface } from "node:readline";
3
4
  import { openDb, resolveDbPath } from "./db";
4
5
  import { localDate, localStamp } from "./time";
@@ -10,6 +11,11 @@ import {
10
11
  stats,
11
12
  } from "./repo";
12
13
 
14
+ const packageJson = (await Bun.file(join(import.meta.dir, "..", "package.json")).json()) as {
15
+ version?: string;
16
+ };
17
+ const VERSION = packageJson.version ?? "0.0.0";
18
+
13
19
  const db = openDb(resolveDbPath());
14
20
  const project = process.env.MEMORY_PROJECT || basename(process.cwd());
15
21
 
@@ -179,7 +185,7 @@ rl.on("line", (line) => {
179
185
  reply(id, {
180
186
  protocolVersion,
181
187
  capabilities: { tools: {} },
182
- serverInfo: { name: "beans-memory", version: "0.1.0" },
188
+ serverInfo: { name: "beans-memory", version: VERSION },
183
189
  });
184
190
  return;
185
191
  }