beans-memory 0.0.0-stage → 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/LICENSE +21 -0
- package/README.id.md +400 -0
- package/README.md +399 -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 +46 -4
- package/skills/beans-memory/SKILL.md +52 -0
- package/src/cli.ts +203 -0
- package/src/db.ts +138 -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/install.ts +376 -0
- package/src/mcp.ts +236 -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
package/src/cli.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { basename } from "node:path";
|
|
3
|
+
import { openDb, resolveDbPath } from "./db";
|
|
4
|
+
import { install, TARGETS } from "./install";
|
|
5
|
+
import {
|
|
6
|
+
addObservation,
|
|
7
|
+
getObservations,
|
|
8
|
+
recentObservations,
|
|
9
|
+
searchObservations,
|
|
10
|
+
stats,
|
|
11
|
+
} from "./repo";
|
|
12
|
+
import { localDate, localStamp } from "./time";
|
|
13
|
+
|
|
14
|
+
type Parsed = {
|
|
15
|
+
positional: string[];
|
|
16
|
+
flags: Record<string, string | boolean>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function parse(argv: string[]): Parsed {
|
|
20
|
+
const positional: string[] = [];
|
|
21
|
+
const flags: Record<string, string | boolean> = {};
|
|
22
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
23
|
+
const token = argv[index];
|
|
24
|
+
if (token === undefined) {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (!token.startsWith("--")) {
|
|
28
|
+
positional.push(token);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const [rawKey, inline] = token.slice(2).split("=", 2);
|
|
32
|
+
const key = (rawKey ?? "").trim();
|
|
33
|
+
const next = argv[index + 1];
|
|
34
|
+
if (inline !== undefined) {
|
|
35
|
+
flags[key] = inline;
|
|
36
|
+
} else if (next !== undefined && !next.startsWith("--")) {
|
|
37
|
+
flags[key] = next;
|
|
38
|
+
index += 1;
|
|
39
|
+
} else {
|
|
40
|
+
flags[key] = true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { positional, flags };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function list(value: string | boolean | undefined): string[] {
|
|
47
|
+
if (typeof value !== "string" || !value.trim()) return [];
|
|
48
|
+
return value
|
|
49
|
+
.split(",")
|
|
50
|
+
.map((item) => item.trim())
|
|
51
|
+
.filter(Boolean);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function usage(): never {
|
|
55
|
+
console.error(
|
|
56
|
+
[
|
|
57
|
+
"Usage:",
|
|
58
|
+
" beans-memory search <query> [--limit=N] [--type=T] [--json]",
|
|
59
|
+
" beans-memory get <id...> [--json]",
|
|
60
|
+
" beans-memory note --title <title> (--body <text> | --stdin) [--type=T] [--files a,b] [--concepts x,y]",
|
|
61
|
+
" beans-memory recent [--limit=N] [--json]",
|
|
62
|
+
" beans-memory stats [--json]",
|
|
63
|
+
" beans-memory projects [--json]",
|
|
64
|
+
" beans-memory install [--opencode] [--claude] [--codex] [--gemini] [--hermes] [--all] [--dry-run]",
|
|
65
|
+
"",
|
|
66
|
+
"Global: --db <path>, --project <name>",
|
|
67
|
+
"Env: MEMORY_DB, MEMORY_PROJECT",
|
|
68
|
+
].join("\n"),
|
|
69
|
+
);
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function readStdin(): Promise<string> {
|
|
74
|
+
const chunks: Buffer[] = [];
|
|
75
|
+
for await (const chunk of Bun.stdin.stream()) {
|
|
76
|
+
chunks.push(Buffer.from(chunk));
|
|
77
|
+
}
|
|
78
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { positional, flags } = parse(process.argv.slice(2));
|
|
82
|
+
const command = positional.shift();
|
|
83
|
+
|
|
84
|
+
if (!command) usage();
|
|
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
|
+
|
|
104
|
+
const dbPath = typeof flags.db === "string" ? flags.db : resolveDbPath();
|
|
105
|
+
const project =
|
|
106
|
+
(typeof flags.project === "string" ? flags.project : "") ||
|
|
107
|
+
process.env.MEMORY_PROJECT ||
|
|
108
|
+
basename(process.cwd());
|
|
109
|
+
const json = flags.json === true;
|
|
110
|
+
const limit = Number(typeof flags.limit === "string" ? flags.limit : 10);
|
|
111
|
+
|
|
112
|
+
const db = openDb(dbPath);
|
|
113
|
+
|
|
114
|
+
function print(value: unknown): void {
|
|
115
|
+
console.log(JSON.stringify(value, null, 2));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
try {
|
|
119
|
+
if (command === "search") {
|
|
120
|
+
const query = positional.join(" ").trim();
|
|
121
|
+
if (!query) usage();
|
|
122
|
+
const rows = searchObservations(db, {
|
|
123
|
+
project,
|
|
124
|
+
query,
|
|
125
|
+
limit,
|
|
126
|
+
type: typeof flags.type === "string" ? flags.type : undefined,
|
|
127
|
+
});
|
|
128
|
+
if (json) print(rows);
|
|
129
|
+
else if (rows.length === 0) console.log("Tidak ada catatan yang cocok.");
|
|
130
|
+
else
|
|
131
|
+
for (const row of rows) {
|
|
132
|
+
const snippet = row.snip ? ` | ${row.snip.replace(/\s+/g, " ").slice(0, 160)}` : "";
|
|
133
|
+
console.log(`#${row.id} · ${localDate(row.created_at)} · ${row.type} · ${row.title}${snippet}`);
|
|
134
|
+
}
|
|
135
|
+
} else if (command === "get") {
|
|
136
|
+
const ids = positional.map((id) => Number(id)).filter(Number.isFinite);
|
|
137
|
+
if (ids.length === 0) usage();
|
|
138
|
+
const rows = getObservations(db, ids);
|
|
139
|
+
if (json) print(rows);
|
|
140
|
+
else if (rows.length === 0) console.log("Catatan tidak ditemukan.");
|
|
141
|
+
else
|
|
142
|
+
for (const row of rows) {
|
|
143
|
+
console.log(`# ${row.title} (#${row.id})`);
|
|
144
|
+
console.log(`Tanggal: ${localStamp(row.created_at)} · Tipe: ${row.type}`);
|
|
145
|
+
if (row.files.length > 0) console.log(`File: ${row.files.join(", ")}`);
|
|
146
|
+
console.log("");
|
|
147
|
+
console.log(row.body);
|
|
148
|
+
console.log("");
|
|
149
|
+
}
|
|
150
|
+
} else if (command === "note") {
|
|
151
|
+
const title = typeof flags.title === "string" ? flags.title : "";
|
|
152
|
+
if (!title) usage();
|
|
153
|
+
const body = flags.stdin === true ? await readStdin() : typeof flags.body === "string" ? flags.body : "";
|
|
154
|
+
const result = addObservation(db, {
|
|
155
|
+
project,
|
|
156
|
+
title,
|
|
157
|
+
body,
|
|
158
|
+
type: typeof flags.type === "string" ? flags.type : "note",
|
|
159
|
+
files: list(flags.files),
|
|
160
|
+
concepts: list(flags.concepts),
|
|
161
|
+
source: "agent",
|
|
162
|
+
});
|
|
163
|
+
if (json) print(result);
|
|
164
|
+
else
|
|
165
|
+
console.log(
|
|
166
|
+
result.inserted
|
|
167
|
+
? `Catatan tersimpan sebagai #${result.id}.`
|
|
168
|
+
: `Catatan identik sudah ada sebagai #${result.id}.`,
|
|
169
|
+
);
|
|
170
|
+
} else if (command === "recent") {
|
|
171
|
+
const rows = recentObservations(db, project, limit);
|
|
172
|
+
if (json) print(rows);
|
|
173
|
+
else if (rows.length === 0) console.log("Belum ada catatan.");
|
|
174
|
+
else
|
|
175
|
+
for (const row of rows) {
|
|
176
|
+
console.log(`#${row.id} · ${localDate(row.created_at)} · ${row.type} · ${row.title}`);
|
|
177
|
+
}
|
|
178
|
+
} else if (command === "stats") {
|
|
179
|
+
const value = stats(db, project);
|
|
180
|
+
if (json) print(value);
|
|
181
|
+
else
|
|
182
|
+
console.log(
|
|
183
|
+
`Project ${project}: ${value.observations} catatan, ${value.prompts} prompt, ${value.sessions} sesi. Catatan terakhir: ${
|
|
184
|
+
value.lastObservation ? localStamp(value.lastObservation) : "belum ada"
|
|
185
|
+
}.`,
|
|
186
|
+
);
|
|
187
|
+
} else if (command === "projects") {
|
|
188
|
+
const rows = db
|
|
189
|
+
.query<{ project: string; n: number }, []>(
|
|
190
|
+
"SELECT project, count(*) AS n FROM observations GROUP BY project ORDER BY n DESC",
|
|
191
|
+
)
|
|
192
|
+
.all();
|
|
193
|
+
if (json) print(rows);
|
|
194
|
+
else for (const row of rows) console.log(`${row.project}\t${row.n}`);
|
|
195
|
+
} else {
|
|
196
|
+
usage();
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
200
|
+
process.exitCode = 1;
|
|
201
|
+
} finally {
|
|
202
|
+
db.close();
|
|
203
|
+
}
|
package/src/db.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { chmodSync, existsSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
|
+
|
|
6
|
+
export function resolveDbPath(_directory: string = process.cwd(), home: string = homedir()): string {
|
|
7
|
+
const override = process.env.MEMORY_DB;
|
|
8
|
+
if (override) {
|
|
9
|
+
mkdirSync(dirname(override), { recursive: true, mode: 0o700 });
|
|
10
|
+
return resolve(override);
|
|
11
|
+
}
|
|
12
|
+
const folder = join(home, ".local", "share", "beans-memory");
|
|
13
|
+
mkdirSync(folder, { recursive: true, mode: 0o700 });
|
|
14
|
+
const current = join(folder, "memory.db");
|
|
15
|
+
const legacy = join(home, ".local", "share", "agent-memory", "memory.db");
|
|
16
|
+
if (!existsSync(current) && existsSync(legacy)) {
|
|
17
|
+
try {
|
|
18
|
+
const old = new Database(legacy, { readonly: true });
|
|
19
|
+
old.run("VACUUM INTO ?", [current]);
|
|
20
|
+
old.close();
|
|
21
|
+
} catch {
|
|
22
|
+
// leave the legacy database untouched if the copy fails
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function secure(path: string): void {
|
|
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;
|
|
32
|
+
try {
|
|
33
|
+
chmodSync(path, 0o600);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw error instanceof Error ? error : new Error(String(error));
|
|
36
|
+
}
|
|
37
|
+
for (const file of [`${path}-wal`, `${path}-shm`]) {
|
|
38
|
+
try {
|
|
39
|
+
chmodSync(file, 0o600);
|
|
40
|
+
} catch {
|
|
41
|
+
// sidecar may not exist yet
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function openDb(path: string = resolveDbPath()): Database {
|
|
47
|
+
const db = new Database(path, { create: true });
|
|
48
|
+
secure(path);
|
|
49
|
+
db.run("PRAGMA journal_mode = WAL");
|
|
50
|
+
db.run("PRAGMA busy_timeout = 5000");
|
|
51
|
+
db.run("PRAGMA foreign_keys = ON");
|
|
52
|
+
migrate(db);
|
|
53
|
+
secure(path);
|
|
54
|
+
return db;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function migrate(db: Database): void {
|
|
58
|
+
db.run(`
|
|
59
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
60
|
+
id TEXT PRIMARY KEY,
|
|
61
|
+
project TEXT NOT NULL,
|
|
62
|
+
directory TEXT,
|
|
63
|
+
title TEXT,
|
|
64
|
+
files TEXT NOT NULL DEFAULT '[]',
|
|
65
|
+
started_at TEXT NOT NULL,
|
|
66
|
+
ended_at TEXT
|
|
67
|
+
)
|
|
68
|
+
`);
|
|
69
|
+
|
|
70
|
+
db.run(`
|
|
71
|
+
CREATE TABLE IF NOT EXISTS prompts (
|
|
72
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
73
|
+
session_id TEXT NOT NULL,
|
|
74
|
+
project TEXT NOT NULL,
|
|
75
|
+
body TEXT NOT NULL,
|
|
76
|
+
created_at TEXT NOT NULL,
|
|
77
|
+
created_at_epoch INTEGER NOT NULL
|
|
78
|
+
)
|
|
79
|
+
`);
|
|
80
|
+
|
|
81
|
+
db.run(`
|
|
82
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
83
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
84
|
+
session_id TEXT,
|
|
85
|
+
project TEXT NOT NULL,
|
|
86
|
+
type TEXT NOT NULL DEFAULT 'note',
|
|
87
|
+
title TEXT NOT NULL,
|
|
88
|
+
body TEXT,
|
|
89
|
+
files TEXT NOT NULL DEFAULT '[]',
|
|
90
|
+
concepts TEXT NOT NULL DEFAULT '[]',
|
|
91
|
+
source TEXT NOT NULL DEFAULT 'agent',
|
|
92
|
+
content_hash TEXT NOT NULL UNIQUE,
|
|
93
|
+
created_at TEXT NOT NULL,
|
|
94
|
+
created_at_epoch INTEGER NOT NULL
|
|
95
|
+
)
|
|
96
|
+
`);
|
|
97
|
+
|
|
98
|
+
db.run(`
|
|
99
|
+
CREATE VIRTUAL TABLE IF NOT EXISTS observations_fts USING fts5(
|
|
100
|
+
title,
|
|
101
|
+
body,
|
|
102
|
+
files,
|
|
103
|
+
content='observations',
|
|
104
|
+
content_rowid='id',
|
|
105
|
+
tokenize='unicode61'
|
|
106
|
+
)
|
|
107
|
+
`);
|
|
108
|
+
|
|
109
|
+
db.run(`
|
|
110
|
+
CREATE TRIGGER IF NOT EXISTS observations_ai AFTER INSERT ON observations BEGIN
|
|
111
|
+
INSERT INTO observations_fts(rowid, title, body, files)
|
|
112
|
+
VALUES (new.id, new.title, coalesce(new.body, ''), new.files);
|
|
113
|
+
END
|
|
114
|
+
`);
|
|
115
|
+
|
|
116
|
+
db.run(`
|
|
117
|
+
CREATE TRIGGER IF NOT EXISTS observations_ad AFTER DELETE ON observations BEGIN
|
|
118
|
+
INSERT INTO observations_fts(observations_fts, rowid, title, body, files)
|
|
119
|
+
VALUES ('delete', old.id, old.title, coalesce(old.body, ''), old.files);
|
|
120
|
+
END
|
|
121
|
+
`);
|
|
122
|
+
|
|
123
|
+
db.run(`
|
|
124
|
+
CREATE TRIGGER IF NOT EXISTS observations_au AFTER UPDATE ON observations BEGIN
|
|
125
|
+
INSERT INTO observations_fts(observations_fts, rowid, title, body, files)
|
|
126
|
+
VALUES ('delete', old.id, old.title, coalesce(old.body, ''), old.files);
|
|
127
|
+
INSERT INTO observations_fts(rowid, title, body, files)
|
|
128
|
+
VALUES (new.id, new.title, coalesce(new.body, ''), new.files);
|
|
129
|
+
END
|
|
130
|
+
`);
|
|
131
|
+
|
|
132
|
+
db.run(
|
|
133
|
+
"CREATE INDEX IF NOT EXISTS idx_observations_project_date ON observations(project, created_at_epoch DESC)",
|
|
134
|
+
);
|
|
135
|
+
db.run(
|
|
136
|
+
"CREATE INDEX IF NOT EXISTS idx_prompts_project_date ON prompts(project, created_at_epoch DESC)",
|
|
137
|
+
);
|
|
138
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { openDb, resolveDbPath } from "./db";
|
|
5
|
+
import { addObservation, stats } from "./repo";
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
function arg(name: string, fallback: string): string {
|
|
9
|
+
const index = args.indexOf(`--${name}`);
|
|
10
|
+
const value = args[index + 1];
|
|
11
|
+
return index >= 0 && value ? value : fallback;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const sourcePath = arg("source", join(homedir(), ".claude-mem", "claude-mem.db"));
|
|
15
|
+
const dbPath = arg("db", resolveDbPath());
|
|
16
|
+
const onlyProject = arg("project", "");
|
|
17
|
+
const includeDiscovery = args.includes("--include-discovery");
|
|
18
|
+
const dryRun = args.includes("--dry-run");
|
|
19
|
+
|
|
20
|
+
const KEEP_TYPES = new Set([
|
|
21
|
+
"decision",
|
|
22
|
+
"bugfix",
|
|
23
|
+
"feature",
|
|
24
|
+
"change",
|
|
25
|
+
"refactor",
|
|
26
|
+
"security_note",
|
|
27
|
+
"sensitive",
|
|
28
|
+
"test-coverage",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
function normalizeProject(project: string): string {
|
|
32
|
+
const trimmed = project.trim();
|
|
33
|
+
if (trimmed.startsWith("sinarmas-api/")) return "sinarmas-api";
|
|
34
|
+
return trimmed;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parseArray(value: string | null): string[] {
|
|
38
|
+
if (!value) return [];
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(value);
|
|
41
|
+
return Array.isArray(parsed) ? parsed.map(String) : [];
|
|
42
|
+
} catch {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const source = new Database(sourcePath, { readonly: true });
|
|
48
|
+
source.run("PRAGMA busy_timeout = 5000");
|
|
49
|
+
|
|
50
|
+
const rows = source
|
|
51
|
+
.query<
|
|
52
|
+
{
|
|
53
|
+
project: string;
|
|
54
|
+
type: string;
|
|
55
|
+
title: string | null;
|
|
56
|
+
subtitle: string | null;
|
|
57
|
+
narrative: string | null;
|
|
58
|
+
facts: string | null;
|
|
59
|
+
concepts: string | null;
|
|
60
|
+
files_modified: string | null;
|
|
61
|
+
created_at: string;
|
|
62
|
+
},
|
|
63
|
+
[]
|
|
64
|
+
>(
|
|
65
|
+
`SELECT project, type, title, subtitle, narrative, facts, concepts, files_modified, created_at
|
|
66
|
+
FROM observations ORDER BY created_at`,
|
|
67
|
+
)
|
|
68
|
+
.all();
|
|
69
|
+
|
|
70
|
+
const db = dryRun ? null : openDb(dbPath);
|
|
71
|
+
const perProject = new Map<string, { inserted: number; dup: number; skipped: number }>();
|
|
72
|
+
|
|
73
|
+
for (const row of rows) {
|
|
74
|
+
const project = normalizeProject(row.project);
|
|
75
|
+
if (onlyProject && project !== onlyProject) continue;
|
|
76
|
+
|
|
77
|
+
const entry = perProject.get(project) ?? { inserted: 0, dup: 0, skipped: 0 };
|
|
78
|
+
perProject.set(project, entry);
|
|
79
|
+
|
|
80
|
+
const type = (row.type ?? "note").toLowerCase();
|
|
81
|
+
if (!KEEP_TYPES.has(type) && !(includeDiscovery && type === "discovery")) {
|
|
82
|
+
entry.skipped += 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const facts = parseArray(row.facts);
|
|
87
|
+
const body = [
|
|
88
|
+
row.narrative ?? "",
|
|
89
|
+
facts.length > 0 ? facts.map((fact) => `- ${fact}`).join("\n") : "",
|
|
90
|
+
]
|
|
91
|
+
.filter(Boolean)
|
|
92
|
+
.join("\n\n")
|
|
93
|
+
.trim();
|
|
94
|
+
|
|
95
|
+
if (dryRun || !db) {
|
|
96
|
+
entry.inserted += 1;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const result = addObservation(db, {
|
|
101
|
+
project,
|
|
102
|
+
type,
|
|
103
|
+
title: row.title ?? row.subtitle ?? "Tanpa judul",
|
|
104
|
+
body,
|
|
105
|
+
files: parseArray(row.files_modified),
|
|
106
|
+
concepts: parseArray(row.concepts),
|
|
107
|
+
source: "import",
|
|
108
|
+
createdAt: row.created_at,
|
|
109
|
+
});
|
|
110
|
+
if (result.inserted) entry.inserted += 1;
|
|
111
|
+
else entry.dup += 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
source.close();
|
|
115
|
+
|
|
116
|
+
const report: Record<string, unknown> = { db: dbPath, dryRun, totalRows: rows.length, projects: {} };
|
|
117
|
+
for (const [project, value] of [...perProject.entries()].sort((a, b) => b[1].inserted - a[1].inserted)) {
|
|
118
|
+
(report.projects as Record<string, unknown>)[project] = {
|
|
119
|
+
...value,
|
|
120
|
+
stats: db ? stats(db, project) : null,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
console.log(JSON.stringify(report, null, 2));
|
|
125
|
+
if (db) db.close();
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Database } from "bun:sqlite";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { openDb, resolveDbPath } from "./db";
|
|
5
|
+
import { addObservation, stats } from "./repo";
|
|
6
|
+
|
|
7
|
+
const args = process.argv.slice(2);
|
|
8
|
+
function arg(name: string, fallback: string): string {
|
|
9
|
+
const index = args.indexOf(`--${name}`);
|
|
10
|
+
const value = args[index + 1];
|
|
11
|
+
return index >= 0 && value ? value : fallback;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const sourcePath = arg("source", join(homedir(), ".claude-mem", "claude-mem.db"));
|
|
15
|
+
const dbPath = arg("db", resolveDbPath());
|
|
16
|
+
const dryRun = args.includes("--dry-run");
|
|
17
|
+
|
|
18
|
+
function normalizeProject(project: string): string {
|
|
19
|
+
const trimmed = project.trim();
|
|
20
|
+
if (trimmed.startsWith("sinarmas-api/")) return "sinarmas-api";
|
|
21
|
+
return trimmed;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function clip(value: string | null, limit: number): string {
|
|
25
|
+
const text = (value ?? "").trim();
|
|
26
|
+
if (!text) return "";
|
|
27
|
+
return text.length > limit ? `${text.slice(0, limit)}…` : text;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const source = new Database(sourcePath, { readonly: true });
|
|
31
|
+
source.run("PRAGMA busy_timeout = 5000");
|
|
32
|
+
const db = dryRun ? null : openDb(dbPath);
|
|
33
|
+
|
|
34
|
+
const summaries = source
|
|
35
|
+
.query<
|
|
36
|
+
{
|
|
37
|
+
project: string;
|
|
38
|
+
request: string | null;
|
|
39
|
+
investigated: string | null;
|
|
40
|
+
learned: string | null;
|
|
41
|
+
completed: string | null;
|
|
42
|
+
next_steps: string | null;
|
|
43
|
+
created_at: string;
|
|
44
|
+
},
|
|
45
|
+
[]
|
|
46
|
+
>(
|
|
47
|
+
`SELECT project, request, investigated, learned, completed, next_steps, created_at
|
|
48
|
+
FROM session_summaries ORDER BY created_at`,
|
|
49
|
+
)
|
|
50
|
+
.all();
|
|
51
|
+
|
|
52
|
+
let summaryInserted = 0;
|
|
53
|
+
let summaryDup = 0;
|
|
54
|
+
|
|
55
|
+
for (const row of summaries) {
|
|
56
|
+
const project = normalizeProject(row.project);
|
|
57
|
+
const title = clip(row.request, 140) || "Ringkasan sesi";
|
|
58
|
+
const body = [
|
|
59
|
+
clip(row.request, 1200) ? `Request: ${clip(row.request, 1200)}` : "",
|
|
60
|
+
clip(row.investigated, 1200) ? `Investigasi: ${clip(row.investigated, 1200)}` : "",
|
|
61
|
+
clip(row.learned, 1500) ? `Dipelajari: ${clip(row.learned, 1500)}` : "",
|
|
62
|
+
clip(row.completed, 1200) ? `Selesai: ${clip(row.completed, 1200)}` : "",
|
|
63
|
+
clip(row.next_steps, 600) ? `Berikutnya: ${clip(row.next_steps, 600)}` : "",
|
|
64
|
+
]
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.join("\n\n");
|
|
67
|
+
|
|
68
|
+
if (!body) continue;
|
|
69
|
+
if (dryRun || !db) {
|
|
70
|
+
summaryInserted += 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const result = addObservation(db, {
|
|
75
|
+
project,
|
|
76
|
+
type: "session_summary",
|
|
77
|
+
title,
|
|
78
|
+
body,
|
|
79
|
+
source: "import_summary",
|
|
80
|
+
createdAt: row.created_at,
|
|
81
|
+
});
|
|
82
|
+
if (result.inserted) summaryInserted += 1;
|
|
83
|
+
else summaryDup += 1;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const prompts = source
|
|
87
|
+
.query<
|
|
88
|
+
{
|
|
89
|
+
project: string;
|
|
90
|
+
session_id: string;
|
|
91
|
+
body: string;
|
|
92
|
+
created_at: string;
|
|
93
|
+
created_at_epoch: number;
|
|
94
|
+
},
|
|
95
|
+
[]
|
|
96
|
+
>(
|
|
97
|
+
`SELECT coalesce(s.project, 'unknown') AS project,
|
|
98
|
+
p.content_session_id AS session_id,
|
|
99
|
+
p.prompt_text AS body,
|
|
100
|
+
p.created_at,
|
|
101
|
+
p.created_at_epoch
|
|
102
|
+
FROM user_prompts p
|
|
103
|
+
LEFT JOIN sdk_sessions s ON s.id = p.session_db_id
|
|
104
|
+
ORDER BY p.created_at_epoch`,
|
|
105
|
+
)
|
|
106
|
+
.all();
|
|
107
|
+
|
|
108
|
+
let promptInserted = 0;
|
|
109
|
+
let promptDup = 0;
|
|
110
|
+
|
|
111
|
+
for (const row of prompts) {
|
|
112
|
+
const project = normalizeProject(row.project);
|
|
113
|
+
const body = row.body.trim().slice(0, 8000);
|
|
114
|
+
if (!body) continue;
|
|
115
|
+
|
|
116
|
+
if (dryRun || !db) {
|
|
117
|
+
promptInserted += 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const exists = db
|
|
122
|
+
.query<{ id: number }, [string, number, string]>(
|
|
123
|
+
"SELECT id FROM prompts WHERE session_id = ? AND created_at_epoch = ? AND body = ? LIMIT 1",
|
|
124
|
+
)
|
|
125
|
+
.get(row.session_id, row.created_at_epoch, body);
|
|
126
|
+
if (exists) {
|
|
127
|
+
promptDup += 1;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
db.run(
|
|
132
|
+
"INSERT INTO prompts (session_id, project, body, created_at, created_at_epoch) VALUES (?, ?, ?, ?, ?)",
|
|
133
|
+
[row.session_id, project, body, row.created_at, row.created_at_epoch],
|
|
134
|
+
);
|
|
135
|
+
promptInserted += 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
source.close();
|
|
139
|
+
|
|
140
|
+
const projectStats: Record<string, unknown> = {};
|
|
141
|
+
if (db) {
|
|
142
|
+
const projects = db
|
|
143
|
+
.query<{ project: string }, []>("SELECT DISTINCT project FROM observations ORDER BY project")
|
|
144
|
+
.all();
|
|
145
|
+
for (const { project } of projects) projectStats[project] = stats(db, project);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
console.log(
|
|
149
|
+
JSON.stringify(
|
|
150
|
+
{
|
|
151
|
+
db: dbPath,
|
|
152
|
+
dryRun,
|
|
153
|
+
summaries: { total: summaries.length, inserted: summaryInserted, dup: summaryDup },
|
|
154
|
+
prompts: { total: prompts.length, inserted: promptInserted, dup: promptDup },
|
|
155
|
+
projects: projectStats,
|
|
156
|
+
},
|
|
157
|
+
null,
|
|
158
|
+
2,
|
|
159
|
+
),
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
if (db) db.close();
|