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
|
@@ -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/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
|
+
}
|