beans-memory 0.0.0-stage → 0.1.4
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/LICENSE +21 -0
- package/README.id.md +354 -0
- package/README.md +353 -2
- package/docs/wordmark.svg +36 -0
- package/examples/claude-code.mcp.json +8 -0
- package/examples/codex.config.toml +8 -0
- package/examples/gemini.settings.json +10 -0
- package/examples/memory-instructions.md +19 -0
- package/package.json +45 -4
- package/skills/beans-memory/SKILL.md +56 -0
- package/src/cli.ts +183 -0
- package/src/db.ts +136 -0
- package/src/import-claude-mem-db.ts +125 -0
- package/src/import-claude-mem-extras.ts +162 -0
- package/src/import-claude-mem.ts +137 -0
- package/src/mcp.ts +230 -0
- package/src/migrate-legacy-db.ts +72 -0
- package/src/plugin.ts +335 -0
- package/src/redact-prompts.ts +27 -0
- package/src/redact.ts +26 -0
- package/src/rehash.ts +45 -0
- package/src/repo.ts +245 -0
- package/src/time.ts +18 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { openDb } from "./db";
|
|
4
|
+
import { addObservation, stats } from "./repo";
|
|
5
|
+
|
|
6
|
+
type Row = {
|
|
7
|
+
id?: number;
|
|
8
|
+
project?: string;
|
|
9
|
+
type?: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
subtitle?: string;
|
|
12
|
+
narrative?: string;
|
|
13
|
+
facts?: string;
|
|
14
|
+
concepts?: string;
|
|
15
|
+
files_modified?: string;
|
|
16
|
+
files_read?: string;
|
|
17
|
+
created_at?: string;
|
|
18
|
+
source?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const args = process.argv.slice(2);
|
|
22
|
+
function arg(name: string, fallback: string): string {
|
|
23
|
+
const index = args.indexOf(`--${name}`);
|
|
24
|
+
const value = args[index + 1];
|
|
25
|
+
return index >= 0 && value ? value : fallback;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const jsonlPath = arg("jsonl", "");
|
|
29
|
+
const dbPath = arg("db", "");
|
|
30
|
+
const project = arg("project", "");
|
|
31
|
+
const includeDiscovery = args.includes("--include-discovery");
|
|
32
|
+
const dryRun = args.includes("--dry-run");
|
|
33
|
+
|
|
34
|
+
if (!jsonlPath || !dbPath || !project) {
|
|
35
|
+
console.error(
|
|
36
|
+
"Usage: bun import-claude-mem.ts --jsonl <observations.jsonl> --db <memory.db> --project <name> [--include-discovery] [--dry-run]",
|
|
37
|
+
);
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const KEEP_TYPES = new Set([
|
|
42
|
+
"decision",
|
|
43
|
+
"bugfix",
|
|
44
|
+
"feature",
|
|
45
|
+
"change",
|
|
46
|
+
"refactor",
|
|
47
|
+
"security_note",
|
|
48
|
+
"sensitive",
|
|
49
|
+
"test-coverage",
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
function parseArray(value: string | undefined): string[] {
|
|
53
|
+
if (!value) return [];
|
|
54
|
+
try {
|
|
55
|
+
const parsed = JSON.parse(value);
|
|
56
|
+
return Array.isArray(parsed) ? parsed.map(String) : [];
|
|
57
|
+
} catch {
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const lines = readFileSync(jsonlPath, "utf8").split("\n").filter(Boolean);
|
|
63
|
+
|
|
64
|
+
const db = openDb(dbPath);
|
|
65
|
+
let inserted = 0;
|
|
66
|
+
let skippedType = 0;
|
|
67
|
+
let skippedDup = 0;
|
|
68
|
+
let failed = 0;
|
|
69
|
+
|
|
70
|
+
for (const line of lines) {
|
|
71
|
+
let row: Row;
|
|
72
|
+
try {
|
|
73
|
+
row = JSON.parse(line);
|
|
74
|
+
} catch {
|
|
75
|
+
failed += 1;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const type = (row.type ?? "note").toLowerCase();
|
|
80
|
+
if (type === "discovery" && !includeDiscovery) {
|
|
81
|
+
skippedType += 1;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (!KEEP_TYPES.has(type) && type !== "discovery") {
|
|
85
|
+
skippedType += 1;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const facts = parseArray(row.facts);
|
|
90
|
+
const body = [row.narrative ?? "", facts.length > 0 ? facts.map((fact) => `- ${fact}`).join("\n") : ""]
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join("\n\n")
|
|
93
|
+
.trim();
|
|
94
|
+
|
|
95
|
+
if (dryRun) {
|
|
96
|
+
inserted += 1;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const result = addObservation(db, {
|
|
102
|
+
project,
|
|
103
|
+
type,
|
|
104
|
+
title: row.title ?? row.subtitle ?? "Tanpa judul",
|
|
105
|
+
body,
|
|
106
|
+
files: parseArray(row.files_modified),
|
|
107
|
+
concepts: parseArray(row.concepts),
|
|
108
|
+
source: "import",
|
|
109
|
+
createdAt: row.created_at,
|
|
110
|
+
});
|
|
111
|
+
if (result.inserted) inserted += 1;
|
|
112
|
+
else skippedDup += 1;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
failed += 1;
|
|
115
|
+
console.error("gagal:", error instanceof Error ? error.message : error);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
console.log(
|
|
120
|
+
JSON.stringify(
|
|
121
|
+
{
|
|
122
|
+
project,
|
|
123
|
+
db: dbPath,
|
|
124
|
+
totalLines: lines.length,
|
|
125
|
+
inserted,
|
|
126
|
+
skippedType,
|
|
127
|
+
skippedDup,
|
|
128
|
+
failed,
|
|
129
|
+
dryRun,
|
|
130
|
+
stats: dryRun ? null : stats(db, project),
|
|
131
|
+
},
|
|
132
|
+
null,
|
|
133
|
+
2,
|
|
134
|
+
),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
db.close();
|
package/src/mcp.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { openDb, resolveDbPath } from "./db";
|
|
4
|
+
import { localDate, localStamp } from "./time";
|
|
5
|
+
import {
|
|
6
|
+
addObservation,
|
|
7
|
+
getObservations,
|
|
8
|
+
recentObservations,
|
|
9
|
+
searchObservations,
|
|
10
|
+
stats,
|
|
11
|
+
} from "./repo";
|
|
12
|
+
|
|
13
|
+
const db = openDb(resolveDbPath());
|
|
14
|
+
const project = process.env.MEMORY_PROJECT || basename(process.cwd());
|
|
15
|
+
|
|
16
|
+
const TOOLS = [
|
|
17
|
+
{
|
|
18
|
+
name: "memory_search",
|
|
19
|
+
description:
|
|
20
|
+
"Cari catatan memory project ini. Mengembalikan index ringkas (id, tanggal, tipe, judul). Pakai memory_get untuk isi lengkap.",
|
|
21
|
+
inputSchema: {
|
|
22
|
+
type: "object",
|
|
23
|
+
properties: {
|
|
24
|
+
query: { type: "string", description: "Kata kunci pencarian." },
|
|
25
|
+
limit: { type: "number", description: "Jumlah hasil, default 10, maksimal 50." },
|
|
26
|
+
type: { type: "string", description: "Filter tipe, misalnya decision atau bugfix." },
|
|
27
|
+
},
|
|
28
|
+
required: ["query"],
|
|
29
|
+
additionalProperties: false,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: "memory_get",
|
|
34
|
+
description: "Ambil isi lengkap catatan memory berdasarkan id hasil memory_search.",
|
|
35
|
+
inputSchema: {
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: {
|
|
38
|
+
ids: { type: "array", items: { type: "number" }, description: "Daftar id catatan." },
|
|
39
|
+
},
|
|
40
|
+
required: ["ids"],
|
|
41
|
+
additionalProperties: false,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "memory_note",
|
|
46
|
+
description:
|
|
47
|
+
"Simpan catatan permanen untuk project ini: keputusan, bugfix, aturan bisnis, atau temuan wiring.",
|
|
48
|
+
inputSchema: {
|
|
49
|
+
type: "object",
|
|
50
|
+
properties: {
|
|
51
|
+
title: { type: "string" },
|
|
52
|
+
body: { type: "string" },
|
|
53
|
+
type: {
|
|
54
|
+
type: "string",
|
|
55
|
+
enum: ["decision", "bugfix", "feature", "refactor", "security_note", "note"],
|
|
56
|
+
},
|
|
57
|
+
files: { type: "array", items: { type: "string" } },
|
|
58
|
+
concepts: { type: "array", items: { type: "string" } },
|
|
59
|
+
},
|
|
60
|
+
required: ["title", "body"],
|
|
61
|
+
additionalProperties: false,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "memory_stats",
|
|
66
|
+
description: "Ringkasan isi memory project ini.",
|
|
67
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
68
|
+
},
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
function text(value: string): { content: Array<{ type: "text"; text: string }> } {
|
|
72
|
+
return { content: [{ type: "text", text: value }] };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function callTool(name: string, args: any): string {
|
|
76
|
+
if (name === "memory_search") {
|
|
77
|
+
const rows = searchObservations(db, {
|
|
78
|
+
project,
|
|
79
|
+
query: String(args?.query ?? ""),
|
|
80
|
+
limit: Number(args?.limit ?? 10),
|
|
81
|
+
type: typeof args?.type === "string" ? args.type : undefined,
|
|
82
|
+
});
|
|
83
|
+
if (rows.length === 0) return "Tidak ada catatan yang cocok.";
|
|
84
|
+
return rows
|
|
85
|
+
.map((row) => {
|
|
86
|
+
const snip = row.snip ? ` | ${row.snip.replace(/\s+/g, " ").slice(0, 160)}` : "";
|
|
87
|
+
return `#${row.id} · ${localDate(row.created_at)} · ${row.type} · ${row.title}${snip}`;
|
|
88
|
+
})
|
|
89
|
+
.join("\n");
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (name === "memory_get") {
|
|
93
|
+
const ids = Array.isArray(args?.ids)
|
|
94
|
+
? args.ids.map((id: unknown) => Number(id)).filter(Number.isFinite)
|
|
95
|
+
: [];
|
|
96
|
+
const rows = getObservations(db, ids);
|
|
97
|
+
if (rows.length === 0) return "Catatan tidak ditemukan.";
|
|
98
|
+
return rows
|
|
99
|
+
.map((row) =>
|
|
100
|
+
[
|
|
101
|
+
`# ${row.title} (#${row.id})`,
|
|
102
|
+
`Tanggal: ${localStamp(row.created_at)} · Tipe: ${row.type}`,
|
|
103
|
+
row.files.length > 0 ? `File: ${row.files.join(", ")}` : "",
|
|
104
|
+
"",
|
|
105
|
+
row.body,
|
|
106
|
+
]
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.join("\n"),
|
|
109
|
+
)
|
|
110
|
+
.join("\n\n---\n\n");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (name === "memory_note") {
|
|
114
|
+
const result = addObservation(db, {
|
|
115
|
+
project,
|
|
116
|
+
title: String(args?.title ?? ""),
|
|
117
|
+
body: String(args?.body ?? ""),
|
|
118
|
+
type: typeof args?.type === "string" ? args.type : "note",
|
|
119
|
+
files: Array.isArray(args?.files) ? args.files.map(String) : [],
|
|
120
|
+
concepts: Array.isArray(args?.concepts) ? args.concepts.map(String) : [],
|
|
121
|
+
source: "agent",
|
|
122
|
+
});
|
|
123
|
+
return result.inserted
|
|
124
|
+
? `Catatan tersimpan sebagai #${result.id}.`
|
|
125
|
+
: `Catatan identik sudah ada sebagai #${result.id}.`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (name === "memory_stats") {
|
|
129
|
+
const value = stats(db, project);
|
|
130
|
+
return `Project ${project}: ${value.observations} catatan, ${value.prompts} prompt, ${value.sessions} sesi. Catatan terakhir: ${value.lastObservation ? localStamp(value.lastObservation) : "belum ada"}.`;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
throw new Error(`Tool tidak dikenal: ${name}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const SUPPORTED_PROTOCOLS = ["2025-06-18", "2025-03-26", "2024-11-05"];
|
|
137
|
+
|
|
138
|
+
function send(message: unknown): void {
|
|
139
|
+
process.stdout.write(`${JSON.stringify(message)}\n`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function reply(id: unknown, result: unknown): void {
|
|
143
|
+
send({ jsonrpc: "2.0", id, result });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function fail(id: unknown, code: number, message: string): void {
|
|
147
|
+
send({ jsonrpc: "2.0", id, error: { code, message } });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const rl = createInterface({ input: process.stdin });
|
|
151
|
+
rl.on("line", (line) => {
|
|
152
|
+
const trimmed = line.trim();
|
|
153
|
+
if (!trimmed) return;
|
|
154
|
+
|
|
155
|
+
let message: any;
|
|
156
|
+
try {
|
|
157
|
+
message = JSON.parse(trimmed);
|
|
158
|
+
} catch {
|
|
159
|
+
fail(null, -32700, "Parse error");
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const { id, method, params } = message ?? {};
|
|
164
|
+
const hasId = Object.prototype.hasOwnProperty.call(message ?? {}, "id");
|
|
165
|
+
const isNotification = !hasId;
|
|
166
|
+
|
|
167
|
+
if (typeof method !== "string") {
|
|
168
|
+
if (hasId) fail(id ?? null, -32600, "Invalid Request");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
if (method === "initialize") {
|
|
174
|
+
const requested = params?.protocolVersion;
|
|
175
|
+
const protocolVersion =
|
|
176
|
+
typeof requested === "string" && SUPPORTED_PROTOCOLS.includes(requested)
|
|
177
|
+
? requested
|
|
178
|
+
: SUPPORTED_PROTOCOLS[0];
|
|
179
|
+
reply(id, {
|
|
180
|
+
protocolVersion,
|
|
181
|
+
capabilities: { tools: {} },
|
|
182
|
+
serverInfo: { name: "beans-memory", version: "0.1.0" },
|
|
183
|
+
});
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (method === "notifications/initialized" || method === "initialized") return;
|
|
188
|
+
if (method === "ping") {
|
|
189
|
+
if (!isNotification) reply(id, {});
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (method === "tools/list") {
|
|
194
|
+
if (!isNotification) reply(id, { tools: TOOLS });
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (method === "tools/call") {
|
|
199
|
+
if (isNotification) return;
|
|
200
|
+
const name = String(params?.name ?? "");
|
|
201
|
+
try {
|
|
202
|
+
reply(id, text(callTool(name, params?.arguments)));
|
|
203
|
+
} catch (error) {
|
|
204
|
+
reply(id, {
|
|
205
|
+
content: [
|
|
206
|
+
{ type: "text", text: error instanceof Error ? error.message : String(error) },
|
|
207
|
+
],
|
|
208
|
+
isError: true,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (!isNotification) fail(id, -32601, `Method not found: ${method}`);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
if (!isNotification) fail(id, -32603, error instanceof Error ? error.message : String(error));
|
|
217
|
+
}
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
function shutdown(): void {
|
|
221
|
+
try {
|
|
222
|
+
db.close();
|
|
223
|
+
} catch {
|
|
224
|
+
// ignore
|
|
225
|
+
}
|
|
226
|
+
process.exit(0);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
process.on("SIGINT", shutdown);
|
|
230
|
+
process.on("SIGTERM", shutdown);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { openDb, resolveDbPath } from "./db";
|
|
3
|
+
import { addObservation } from "./repo";
|
|
4
|
+
|
|
5
|
+
const args = process.argv.slice(2);
|
|
6
|
+
function arg(name: string, fallback: string): string {
|
|
7
|
+
const index = args.indexOf(`--${name}`);
|
|
8
|
+
const value = args[index + 1];
|
|
9
|
+
return index >= 0 && value ? value : fallback;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const fromPath = arg("from", "");
|
|
13
|
+
const dbPath = arg("db", resolveDbPath());
|
|
14
|
+
|
|
15
|
+
if (!fromPath) {
|
|
16
|
+
console.error("Usage: bun migrate-legacy-db.ts --from <old/memory.db> [--db <new/memory.db>]");
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const source = new Database(fromPath, { readonly: true });
|
|
21
|
+
source.run("PRAGMA busy_timeout = 5000");
|
|
22
|
+
const target = openDb(dbPath);
|
|
23
|
+
|
|
24
|
+
const rows = source
|
|
25
|
+
.query<
|
|
26
|
+
{
|
|
27
|
+
project: string;
|
|
28
|
+
type: string;
|
|
29
|
+
title: string;
|
|
30
|
+
body: string | null;
|
|
31
|
+
files: string;
|
|
32
|
+
concepts: string;
|
|
33
|
+
source: string;
|
|
34
|
+
created_at: string;
|
|
35
|
+
},
|
|
36
|
+
[]
|
|
37
|
+
>(
|
|
38
|
+
`SELECT project, type, title, coalesce(body, '') AS body, files, concepts, source, created_at
|
|
39
|
+
FROM observations ORDER BY created_at`,
|
|
40
|
+
)
|
|
41
|
+
.all();
|
|
42
|
+
|
|
43
|
+
function parseArray(value: string): string[] {
|
|
44
|
+
try {
|
|
45
|
+
const parsed = JSON.parse(value);
|
|
46
|
+
return Array.isArray(parsed) ? parsed.map(String) : [];
|
|
47
|
+
} catch {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let inserted = 0;
|
|
53
|
+
let dup = 0;
|
|
54
|
+
for (const row of rows) {
|
|
55
|
+
const result = addObservation(target, {
|
|
56
|
+
project: row.project,
|
|
57
|
+
type: row.type,
|
|
58
|
+
title: row.title,
|
|
59
|
+
body: row.body ?? "",
|
|
60
|
+
files: parseArray(row.files),
|
|
61
|
+
concepts: parseArray(row.concepts),
|
|
62
|
+
source: row.source === "agent" ? "agent" : "import",
|
|
63
|
+
createdAt: row.created_at,
|
|
64
|
+
});
|
|
65
|
+
if (result.inserted) inserted += 1;
|
|
66
|
+
else dup += 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
source.close();
|
|
70
|
+
target.close();
|
|
71
|
+
|
|
72
|
+
console.log(JSON.stringify({ from: fromPath, to: dbPath, rows: rows.length, inserted, dup }, null, 2));
|