@slashml/memory 1.0.11 → 1.0.12
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/bin/cli.js +704 -2
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -10,7 +10,7 @@ const readline = require("readline");
|
|
|
10
10
|
|
|
11
11
|
const DEFAULT_BASE_URL = "https://api.slashml.ai";
|
|
12
12
|
const DEFAULT_CONSOLE_URL = "https://app.slashml.ai";
|
|
13
|
-
const PACKAGE_VERSION = "1.0.
|
|
13
|
+
const PACKAGE_VERSION = "1.0.12";
|
|
14
14
|
|
|
15
15
|
function printHelp() {
|
|
16
16
|
console.log(`@slashml/memory v${PACKAGE_VERSION} -- Official CLI & MCP Stdio Server for / ml (SlashML · https://slashml.ai)
|
|
@@ -43,7 +43,14 @@ Options:
|
|
|
43
43
|
--once Run a single watch-sandbox transcript scan pass and exit
|
|
44
44
|
--transcript-dir <dir> Additional directory to scan for .jsonl agent transcripts
|
|
45
45
|
status, --status Show ambient memory ROI telemetry (tokens processed, % noise filtered, rules assimilated, amnesia intercepted)
|
|
46
|
-
--
|
|
46
|
+
migrate, --migrate Migrate memories from Mem0, Zep, Supermemory, Honcho, Hindsight, Letta, or a JSON/JSONL/MD file
|
|
47
|
+
--from <provider> Source provider for migrate: mem0 | zep | supermemory | honcho | hindsight | letta | file
|
|
48
|
+
--source-key <key> Source provider API key (or MEM0_API_KEY / ZEP_API_KEY / SUPERMEMORY_API_KEY / HONCHO_API_KEY / LETTA_API_KEY)
|
|
49
|
+
--source-url <url> Optional custom base URL for self-hosted source provider
|
|
50
|
+
--file <path> Import memories from a local JSON, JSONL, CSV, or Markdown export file
|
|
51
|
+
import-local, --import-local Import local coding-agent memories (~/.claude/projects/*/memory/*.md, CLAUDE.md, .cursorrules, .windsurfrules, .cursor/rules)
|
|
52
|
+
--dry-run Preview discovered memories during migrate or import-local without writing to / ml
|
|
53
|
+
--json Format status/sync/migrate output as JSON
|
|
47
54
|
--signup <work-email> Request a 6-digit OTP to provision a / ml free API key (requires work/custom domain @company.com; for @gmail.com / @hotmail.com sign in with Google/GitHub at https://app.slashml.ai)
|
|
48
55
|
--verify <work-email> <code> Verify 6-digit code and print the new SLASHML_API_KEY to stdout`);
|
|
49
56
|
}
|
|
@@ -195,6 +202,13 @@ function parseCliArgs(argv) {
|
|
|
195
202
|
let intervalMs = 1500;
|
|
196
203
|
let transcriptDir = process.env.SLASHML_TRANSCRIPT_DIR || null;
|
|
197
204
|
let forgetId = null;
|
|
205
|
+
let isMigrate = false;
|
|
206
|
+
let migrateFrom = null;
|
|
207
|
+
let sourceKey = null;
|
|
208
|
+
let sourceUrl = null;
|
|
209
|
+
let importFile = null;
|
|
210
|
+
let isImportLocal = false;
|
|
211
|
+
let isDryRun = false;
|
|
198
212
|
|
|
199
213
|
for (let i = 0; i < argv.length; i++) {
|
|
200
214
|
const arg = argv[i];
|
|
@@ -214,6 +228,22 @@ function parseCliArgs(argv) {
|
|
|
214
228
|
isSyncRules = true;
|
|
215
229
|
} else if (arg === "--watch-sandbox" || arg === "watch-sandbox") {
|
|
216
230
|
isWatchSandbox = true;
|
|
231
|
+
} else if (arg === "migrate" || arg === "--migrate") {
|
|
232
|
+
isMigrate = true;
|
|
233
|
+
} else if (arg === "--from" && i + 1 < argv.length) {
|
|
234
|
+
migrateFrom = argv[++i].trim().toLowerCase();
|
|
235
|
+
isMigrate = true;
|
|
236
|
+
} else if ((arg === "--source-key" || arg === "--from-key") && i + 1 < argv.length) {
|
|
237
|
+
sourceKey = argv[++i].trim();
|
|
238
|
+
} else if ((arg === "--source-url" || arg === "--from-url") && i + 1 < argv.length) {
|
|
239
|
+
sourceUrl = argv[++i].trim().replace(/\/+$/, "");
|
|
240
|
+
} else if (arg === "--file" && i + 1 < argv.length) {
|
|
241
|
+
importFile = argv[++i];
|
|
242
|
+
isMigrate = true;
|
|
243
|
+
} else if (arg === "import-local" || arg === "--import-local") {
|
|
244
|
+
isImportLocal = true;
|
|
245
|
+
} else if (arg === "--dry-run") {
|
|
246
|
+
isDryRun = true;
|
|
217
247
|
} else if ((arg === "--forget" || arg === "forget") && i + 1 < argv.length) {
|
|
218
248
|
forgetId = argv[++i];
|
|
219
249
|
} else if (arg === "--daemon") {
|
|
@@ -291,6 +321,13 @@ function parseCliArgs(argv) {
|
|
|
291
321
|
intervalMs,
|
|
292
322
|
transcriptDir,
|
|
293
323
|
forgetId,
|
|
324
|
+
isMigrate,
|
|
325
|
+
migrateFrom,
|
|
326
|
+
sourceKey,
|
|
327
|
+
sourceUrl,
|
|
328
|
+
importFile,
|
|
329
|
+
isImportLocal,
|
|
330
|
+
isDryRun,
|
|
294
331
|
};
|
|
295
332
|
}
|
|
296
333
|
|
|
@@ -2637,6 +2674,671 @@ function drainPendingSandboxIntervention() {
|
|
|
2637
2674
|
return null;
|
|
2638
2675
|
}
|
|
2639
2676
|
|
|
2677
|
+
function makeMigrationDocId(prefix, text, userId = "default") {
|
|
2678
|
+
const crypto = require("crypto");
|
|
2679
|
+
const digest = crypto
|
|
2680
|
+
.createHash("sha1")
|
|
2681
|
+
.update(`${prefix}:${userId}:${String(text || "").trim().toLowerCase()}`)
|
|
2682
|
+
.digest("hex")
|
|
2683
|
+
.slice(0, 12);
|
|
2684
|
+
return `mig_${prefix}_${digest}`;
|
|
2685
|
+
}
|
|
2686
|
+
|
|
2687
|
+
function stripSlashmlInjectedBlocks(rawMarkdown) {
|
|
2688
|
+
return String(rawMarkdown || "")
|
|
2689
|
+
.replace(/<!--\s*(?:SLASHML|BLINK)-MEMORY-RULE:START\s*-->[\s\S]*?<!--\s*(?:SLASHML|BLINK)-MEMORY-RULE:END\s*-->/gi, "")
|
|
2690
|
+
.replace(/<!--\s*SLASHML-AMBIENT-MEMORY:START\s*-->[\s\S]*?<!--\s*SLASHML-AMBIENT-MEMORY:END\s*-->/gi, "")
|
|
2691
|
+
.replace(/^---[\r\n]+[\s\S]*?[\r\n]+---[\r\n]*/m, "")
|
|
2692
|
+
.trim();
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
function extractEntriesFromMarkdown(rawMarkdown, defaultUserId, baseMetadata = {}) {
|
|
2696
|
+
const cleaned = stripSlashmlInjectedBlocks(rawMarkdown);
|
|
2697
|
+
if (!cleaned) return [];
|
|
2698
|
+
const entries = [];
|
|
2699
|
+
const lines = cleaned.split(/\r?\n/);
|
|
2700
|
+
let currentHeading = "";
|
|
2701
|
+
let paragraphBuf = [];
|
|
2702
|
+
|
|
2703
|
+
function flushParagraph() {
|
|
2704
|
+
if (paragraphBuf.length === 0) return;
|
|
2705
|
+
const text = paragraphBuf.join(" ").replace(/\s+/g, " ").trim();
|
|
2706
|
+
paragraphBuf = [];
|
|
2707
|
+
if (text.length >= 12) {
|
|
2708
|
+
const fullText = currentHeading ? `[${currentHeading}] ${text}` : text;
|
|
2709
|
+
entries.push({
|
|
2710
|
+
text: fullText,
|
|
2711
|
+
user_id: defaultUserId,
|
|
2712
|
+
metadata: { ...baseMetadata, ...(currentHeading ? { section: currentHeading } : {}) },
|
|
2713
|
+
});
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
|
|
2717
|
+
for (const rawLine of lines) {
|
|
2718
|
+
const line = rawLine.trim();
|
|
2719
|
+
if (!line) {
|
|
2720
|
+
flushParagraph();
|
|
2721
|
+
continue;
|
|
2722
|
+
}
|
|
2723
|
+
const headingMatch = line.match(/^#{1,4}\s+(.+)$/);
|
|
2724
|
+
if (headingMatch) {
|
|
2725
|
+
flushParagraph();
|
|
2726
|
+
currentHeading = headingMatch[1].trim();
|
|
2727
|
+
continue;
|
|
2728
|
+
}
|
|
2729
|
+
const bulletMatch = line.match(/^(?:[-*+]|\d+\.)\s+(.+)$/);
|
|
2730
|
+
if (bulletMatch) {
|
|
2731
|
+
flushParagraph();
|
|
2732
|
+
const itemText = bulletMatch[1].replace(/\s+/g, " ").trim();
|
|
2733
|
+
if (itemText.length >= 8) {
|
|
2734
|
+
const fullText = currentHeading ? `[${currentHeading}] ${itemText}` : itemText;
|
|
2735
|
+
entries.push({
|
|
2736
|
+
text: fullText,
|
|
2737
|
+
user_id: defaultUserId,
|
|
2738
|
+
metadata: { ...baseMetadata, ...(currentHeading ? { section: currentHeading } : {}) },
|
|
2739
|
+
});
|
|
2740
|
+
}
|
|
2741
|
+
continue;
|
|
2742
|
+
}
|
|
2743
|
+
paragraphBuf.push(line);
|
|
2744
|
+
}
|
|
2745
|
+
flushParagraph();
|
|
2746
|
+
return entries;
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
function normalizeImportedRecord(item, provider, defaultUserId, extraMeta = {}) {
|
|
2750
|
+
if (!item) return null;
|
|
2751
|
+
if (typeof item === "string") {
|
|
2752
|
+
const t = item.trim();
|
|
2753
|
+
if (!t) return null;
|
|
2754
|
+
return {
|
|
2755
|
+
id: makeMigrationDocId(provider, t, defaultUserId),
|
|
2756
|
+
text: t,
|
|
2757
|
+
user_id: defaultUserId,
|
|
2758
|
+
metadata: { source_provider: provider, ...extraMeta },
|
|
2759
|
+
};
|
|
2760
|
+
}
|
|
2761
|
+
if (typeof item !== "object") return null;
|
|
2762
|
+
const text = String(
|
|
2763
|
+
item.memory ||
|
|
2764
|
+
item.text ||
|
|
2765
|
+
item.fact ||
|
|
2766
|
+
item.content ||
|
|
2767
|
+
item.summary ||
|
|
2768
|
+
item.value ||
|
|
2769
|
+
(Array.isArray(item.messages)
|
|
2770
|
+
? item.messages
|
|
2771
|
+
.map((m) => (typeof m === "string" ? m : `${m.role || "user"}: ${m.content || m.text || ""}`))
|
|
2772
|
+
.join("\n")
|
|
2773
|
+
: "") ||
|
|
2774
|
+
""
|
|
2775
|
+
).trim();
|
|
2776
|
+
if (!text) return null;
|
|
2777
|
+
const resolvedUser = String(
|
|
2778
|
+
item.user_id ||
|
|
2779
|
+
item.userId ||
|
|
2780
|
+
item.agent_id ||
|
|
2781
|
+
item.agentId ||
|
|
2782
|
+
(item.metadata && (item.metadata.user_id || item.metadata.agent_id)) ||
|
|
2783
|
+
defaultUserId ||
|
|
2784
|
+
"default"
|
|
2785
|
+
).trim();
|
|
2786
|
+
const baseMeta = item.metadata && typeof item.metadata === "object" ? item.metadata : {};
|
|
2787
|
+
const categories = Array.isArray(item.categories) ? item.categories : undefined;
|
|
2788
|
+
return {
|
|
2789
|
+
id: String(item.id || item.memory_id || item.uuid || makeMigrationDocId(provider, text, resolvedUser)),
|
|
2790
|
+
text,
|
|
2791
|
+
user_id: resolvedUser,
|
|
2792
|
+
metadata: {
|
|
2793
|
+
...baseMeta,
|
|
2794
|
+
...extraMeta,
|
|
2795
|
+
source_provider: provider,
|
|
2796
|
+
user_id: resolvedUser,
|
|
2797
|
+
...(item.agent_id ? { agent_id: item.agent_id } : {}),
|
|
2798
|
+
...(item.app_id ? { app_id: item.app_id } : {}),
|
|
2799
|
+
...(item.run_id || item.session_id ? { run_id: item.run_id || item.session_id } : {}),
|
|
2800
|
+
...(categories ? { categories } : {}),
|
|
2801
|
+
...(item.created_at ? { source_created_at: item.created_at } : {}),
|
|
2802
|
+
},
|
|
2803
|
+
};
|
|
2804
|
+
}
|
|
2805
|
+
|
|
2806
|
+
function parseMemoriesFromFileContent(rawContent, filePath, defaultUserId) {
|
|
2807
|
+
const ext = path.extname(filePath || "").toLowerCase();
|
|
2808
|
+
const trimmed = String(rawContent || "").trim();
|
|
2809
|
+
if (!trimmed) return [];
|
|
2810
|
+
|
|
2811
|
+
if (ext === ".md" || ext === ".mdc" || ext === ".txt" || filePath.endsWith("rules")) {
|
|
2812
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) {
|
|
2813
|
+
return extractEntriesFromMarkdown(trimmed, defaultUserId, {
|
|
2814
|
+
source_file: path.basename(filePath),
|
|
2815
|
+
}).map((e) => normalizeImportedRecord(e, "file", defaultUserId, e.metadata));
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
|
|
2819
|
+
// Try JSON
|
|
2820
|
+
try {
|
|
2821
|
+
const parsedJson = JSON.parse(trimmed);
|
|
2822
|
+
const list = Array.isArray(parsedJson)
|
|
2823
|
+
? parsedJson
|
|
2824
|
+
: Array.isArray(parsedJson.results)
|
|
2825
|
+
? parsedJson.results
|
|
2826
|
+
: Array.isArray(parsedJson.memories)
|
|
2827
|
+
? parsedJson.memories
|
|
2828
|
+
: Array.isArray(parsedJson.documents)
|
|
2829
|
+
? parsedJson.documents
|
|
2830
|
+
: Array.isArray(parsedJson.items)
|
|
2831
|
+
? parsedJson.items
|
|
2832
|
+
: Array.isArray(parsedJson.facts)
|
|
2833
|
+
? parsedJson.facts
|
|
2834
|
+
: parsedJson.vault && Array.isArray(parsedJson.vault.documents)
|
|
2835
|
+
? parsedJson.vault.documents
|
|
2836
|
+
: [parsedJson];
|
|
2837
|
+
return list
|
|
2838
|
+
.map((item) => normalizeImportedRecord(item, "file", defaultUserId, { source_file: path.basename(filePath) }))
|
|
2839
|
+
.filter(Boolean);
|
|
2840
|
+
} catch {
|
|
2841
|
+
// Try JSONL
|
|
2842
|
+
const lines = trimmed.split(/\r?\n/);
|
|
2843
|
+
const jsonlItems = [];
|
|
2844
|
+
let isJsonl = false;
|
|
2845
|
+
for (const l of lines) {
|
|
2846
|
+
const s = l.trim();
|
|
2847
|
+
if (!s) continue;
|
|
2848
|
+
if (s.startsWith("{")) {
|
|
2849
|
+
try {
|
|
2850
|
+
const obj = JSON.parse(s);
|
|
2851
|
+
isJsonl = true;
|
|
2852
|
+
const norm = normalizeImportedRecord(obj, "file", defaultUserId, {
|
|
2853
|
+
source_file: path.basename(filePath),
|
|
2854
|
+
});
|
|
2855
|
+
if (norm) jsonlItems.push(norm);
|
|
2856
|
+
} catch {
|
|
2857
|
+
// ignore
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
}
|
|
2861
|
+
if (isJsonl && jsonlItems.length > 0) {
|
|
2862
|
+
return jsonlItems;
|
|
2863
|
+
}
|
|
2864
|
+
}
|
|
2865
|
+
|
|
2866
|
+
return extractEntriesFromMarkdown(trimmed, defaultUserId, {
|
|
2867
|
+
source_file: path.basename(filePath),
|
|
2868
|
+
})
|
|
2869
|
+
.map((e) => normalizeImportedRecord(e, "file", defaultUserId, e.metadata))
|
|
2870
|
+
.filter(Boolean);
|
|
2871
|
+
}
|
|
2872
|
+
|
|
2873
|
+
async function fetchProviderMemories(provider, sourceKey, sourceUrl, defaultUserId) {
|
|
2874
|
+
const normProvider = String(provider || "").trim().toLowerCase();
|
|
2875
|
+
const collected = [];
|
|
2876
|
+
|
|
2877
|
+
if (normProvider === "mem0") {
|
|
2878
|
+
const base = (sourceUrl || "https://api.mem0.ai").replace(/\/+$/, "");
|
|
2879
|
+
const headers = {
|
|
2880
|
+
Authorization: sourceKey.startsWith("Token ") || sourceKey.startsWith("Bearer ") ? sourceKey : `Token ${sourceKey}`,
|
|
2881
|
+
"Content-Type": "application/json",
|
|
2882
|
+
Accept: "application/json",
|
|
2883
|
+
};
|
|
2884
|
+
for (let page = 1; page <= 50; page++) {
|
|
2885
|
+
const qs = new URLSearchParams({ page: String(page), page_size: "100" });
|
|
2886
|
+
if (defaultUserId && defaultUserId !== "default") {
|
|
2887
|
+
qs.set("user_id", defaultUserId);
|
|
2888
|
+
}
|
|
2889
|
+
const res = await fetch(`${base}/v1/memories/?${qs.toString()}`, { headers });
|
|
2890
|
+
if (!res.ok) {
|
|
2891
|
+
if (page === 1) {
|
|
2892
|
+
const v2Res = await fetch(`${base}/v2/memories/`, {
|
|
2893
|
+
method: "POST",
|
|
2894
|
+
headers,
|
|
2895
|
+
body: JSON.stringify({
|
|
2896
|
+
filters: defaultUserId && defaultUserId !== "default" ? { user_id: defaultUserId } : {},
|
|
2897
|
+
page: 1,
|
|
2898
|
+
page_size: 200,
|
|
2899
|
+
}),
|
|
2900
|
+
});
|
|
2901
|
+
if (!v2Res.ok) {
|
|
2902
|
+
const errTxt = await res.text().catch(() => "");
|
|
2903
|
+
throw new Error(`Mem0 API returned HTTP ${res.status}: ${errTxt.slice(0, 200)}`);
|
|
2904
|
+
}
|
|
2905
|
+
const v2Data = await v2Res.json();
|
|
2906
|
+
const v2Items = Array.isArray(v2Data) ? v2Data : v2Data.results || v2Data.memories || [];
|
|
2907
|
+
for (const it of v2Items) {
|
|
2908
|
+
const n = normalizeImportedRecord(it, "mem0", defaultUserId);
|
|
2909
|
+
if (n) collected.push(n);
|
|
2910
|
+
}
|
|
2911
|
+
}
|
|
2912
|
+
break;
|
|
2913
|
+
}
|
|
2914
|
+
const data = await res.json();
|
|
2915
|
+
const items = Array.isArray(data) ? data : data.results || data.memories || [];
|
|
2916
|
+
if (items.length === 0) break;
|
|
2917
|
+
for (const it of items) {
|
|
2918
|
+
const n = normalizeImportedRecord(it, "mem0", defaultUserId);
|
|
2919
|
+
if (n) collected.push(n);
|
|
2920
|
+
}
|
|
2921
|
+
if (items.length < 100 || (!Array.isArray(data) && !data.next)) break;
|
|
2922
|
+
}
|
|
2923
|
+
return collected;
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2926
|
+
if (normProvider === "zep") {
|
|
2927
|
+
const base = (sourceUrl || "https://api.getzep.com").replace(/\/+$/, "");
|
|
2928
|
+
const authHeader = sourceKey.startsWith("z_") ? `Api-Key ${sourceKey}` : `Bearer ${sourceKey}`;
|
|
2929
|
+
const headers = { Authorization: authHeader, Accept: "application/json" };
|
|
2930
|
+
|
|
2931
|
+
if (defaultUserId && defaultUserId !== "default") {
|
|
2932
|
+
const uRes = await fetch(`${base}/api/v2/users/${encodeURIComponent(defaultUserId)}/facts`, { headers });
|
|
2933
|
+
if (uRes.ok) {
|
|
2934
|
+
const uData = await uRes.json();
|
|
2935
|
+
const facts = Array.isArray(uData) ? uData : uData.facts || uData.results || [];
|
|
2936
|
+
for (const f of facts) {
|
|
2937
|
+
const n = normalizeImportedRecord(f, "zep", defaultUserId);
|
|
2938
|
+
if (n) collected.push(n);
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
|
|
2943
|
+
const sRes = await fetch(`${base}/api/v2/sessions-ordered?pageSize=100&pageNumber=1`, { headers });
|
|
2944
|
+
if (!sRes.ok && collected.length === 0) {
|
|
2945
|
+
const errTxt = await sRes.text().catch(() => "");
|
|
2946
|
+
throw new Error(`Zep API returned HTTP ${sRes.status}: ${errTxt.slice(0, 200)}`);
|
|
2947
|
+
}
|
|
2948
|
+
if (sRes.ok) {
|
|
2949
|
+
const sData = await sRes.json();
|
|
2950
|
+
const sessions = Array.isArray(sData) ? sData : sData.sessions || sData.results || [];
|
|
2951
|
+
for (const sess of sessions.slice(0, 50)) {
|
|
2952
|
+
const sid = sess.session_id || sess.sessionId || sess.id;
|
|
2953
|
+
if (!sid) continue;
|
|
2954
|
+
const mRes = await fetch(`${base}/api/v2/sessions/${encodeURIComponent(sid)}/memory`, { headers });
|
|
2955
|
+
if (!mRes.ok) continue;
|
|
2956
|
+
const mData = await mRes.json();
|
|
2957
|
+
const facts = Array.isArray(mData.facts)
|
|
2958
|
+
? mData.facts
|
|
2959
|
+
: Array.isArray(mData.relevant_facts)
|
|
2960
|
+
? mData.relevant_facts
|
|
2961
|
+
: [];
|
|
2962
|
+
for (const f of facts) {
|
|
2963
|
+
const n = normalizeImportedRecord(f, "zep", sess.user_id || defaultUserId, { session_id: sid });
|
|
2964
|
+
if (n) collected.push(n);
|
|
2965
|
+
}
|
|
2966
|
+
if (mData.summary && mData.summary.content) {
|
|
2967
|
+
const n = normalizeImportedRecord(
|
|
2968
|
+
{ text: mData.summary.content, user_id: sess.user_id || defaultUserId },
|
|
2969
|
+
"zep",
|
|
2970
|
+
sess.user_id || defaultUserId,
|
|
2971
|
+
{ session_id: sid, kind: "session_summary" }
|
|
2972
|
+
);
|
|
2973
|
+
if (n) collected.push(n);
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
}
|
|
2977
|
+
return collected;
|
|
2978
|
+
}
|
|
2979
|
+
|
|
2980
|
+
if (normProvider === "supermemory") {
|
|
2981
|
+
const base = (sourceUrl || "https://api.supermemory.ai").replace(/\/+$/, "");
|
|
2982
|
+
const headers = {
|
|
2983
|
+
Authorization: sourceKey.startsWith("Bearer ") ? sourceKey : `Bearer ${sourceKey}`,
|
|
2984
|
+
"Content-Type": "application/json",
|
|
2985
|
+
Accept: "application/json",
|
|
2986
|
+
};
|
|
2987
|
+
let res = await fetch(`${base}/v3/documents/list`, {
|
|
2988
|
+
method: "POST",
|
|
2989
|
+
headers,
|
|
2990
|
+
body: JSON.stringify({ page: 1, limit: 200 }),
|
|
2991
|
+
});
|
|
2992
|
+
if (!res.ok) {
|
|
2993
|
+
res = await fetch(`${base}/v3/memories`, { headers });
|
|
2994
|
+
}
|
|
2995
|
+
if (!res.ok) {
|
|
2996
|
+
const errTxt = await res.text().catch(() => "");
|
|
2997
|
+
throw new Error(`Supermemory API returned HTTP ${res.status}: ${errTxt.slice(0, 200)}`);
|
|
2998
|
+
}
|
|
2999
|
+
const data = await res.json();
|
|
3000
|
+
const items = Array.isArray(data) ? data : data.memories || data.documents || data.results || [];
|
|
3001
|
+
for (const it of items) {
|
|
3002
|
+
const n = normalizeImportedRecord(it, "supermemory", defaultUserId);
|
|
3003
|
+
if (n) collected.push(n);
|
|
3004
|
+
}
|
|
3005
|
+
return collected;
|
|
3006
|
+
}
|
|
3007
|
+
|
|
3008
|
+
if (normProvider === "honcho") {
|
|
3009
|
+
const base = (sourceUrl || "https://api.honcho.dev").replace(/\/+$/, "");
|
|
3010
|
+
const headers = {
|
|
3011
|
+
Authorization: sourceKey.startsWith("Bearer ") ? sourceKey : `Bearer ${sourceKey}`,
|
|
3012
|
+
Accept: "application/json",
|
|
3013
|
+
};
|
|
3014
|
+
const appsRes = await fetch(`${base}/v1/apps`, { headers });
|
|
3015
|
+
if (!appsRes.ok) {
|
|
3016
|
+
const errTxt = await appsRes.text().catch(() => "");
|
|
3017
|
+
throw new Error(`Honcho API returned HTTP ${appsRes.status}: ${errTxt.slice(0, 200)}`);
|
|
3018
|
+
}
|
|
3019
|
+
const appsData = await appsRes.json();
|
|
3020
|
+
const apps = Array.isArray(appsData) ? appsData : appsData.apps || appsData.items || [];
|
|
3021
|
+
for (const app of apps.slice(0, 10)) {
|
|
3022
|
+
const appId = app.id || app.app_id;
|
|
3023
|
+
if (!appId) continue;
|
|
3024
|
+
const uRes = await fetch(`${base}/v1/apps/${encodeURIComponent(appId)}/users?size=100`, { headers });
|
|
3025
|
+
if (!uRes.ok) continue;
|
|
3026
|
+
const uData = await uRes.json();
|
|
3027
|
+
const users = Array.isArray(uData) ? uData : uData.items || uData.users || [];
|
|
3028
|
+
for (const u of users.slice(0, 50)) {
|
|
3029
|
+
const uid = u.id || u.user_id;
|
|
3030
|
+
if (!uid) continue;
|
|
3031
|
+
const mmRes = await fetch(
|
|
3032
|
+
`${base}/v1/apps/${encodeURIComponent(appId)}/users/${encodeURIComponent(uid)}/metamessages?size=100`,
|
|
3033
|
+
{ headers }
|
|
3034
|
+
);
|
|
3035
|
+
if (!mmRes.ok) continue;
|
|
3036
|
+
const mmData = await mmRes.json();
|
|
3037
|
+
const mms = Array.isArray(mmData) ? mmData : mmData.items || mmData.metamessages || [];
|
|
3038
|
+
for (const mm of mms) {
|
|
3039
|
+
const n = normalizeImportedRecord(mm, "honcho", u.name || uid, { app_id: appId });
|
|
3040
|
+
if (n) collected.push(n);
|
|
3041
|
+
}
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
return collected;
|
|
3045
|
+
}
|
|
3046
|
+
|
|
3047
|
+
if (normProvider === "hindsight") {
|
|
3048
|
+
const base = (sourceUrl || "https://api.hindsight.vectorize.io").replace(/\/+$/, "");
|
|
3049
|
+
const headers = {
|
|
3050
|
+
Authorization: sourceKey.startsWith("Bearer ") ? sourceKey : `Bearer ${sourceKey}`,
|
|
3051
|
+
Accept: "application/json",
|
|
3052
|
+
};
|
|
3053
|
+
const res = await fetch(`${base}/v1/memories`, { headers });
|
|
3054
|
+
if (!res.ok) {
|
|
3055
|
+
const errTxt = await res.text().catch(() => "");
|
|
3056
|
+
throw new Error(`Hindsight API returned HTTP ${res.status}: ${errTxt.slice(0, 200)}`);
|
|
3057
|
+
}
|
|
3058
|
+
const data = await res.json();
|
|
3059
|
+
const items = Array.isArray(data) ? data : data.memories || data.results || data.items || [];
|
|
3060
|
+
for (const it of items) {
|
|
3061
|
+
const n = normalizeImportedRecord(it, "hindsight", defaultUserId);
|
|
3062
|
+
if (n) collected.push(n);
|
|
3063
|
+
}
|
|
3064
|
+
return collected;
|
|
3065
|
+
}
|
|
3066
|
+
|
|
3067
|
+
if (normProvider === "letta" || normProvider === "memgpt") {
|
|
3068
|
+
const base = (sourceUrl || "https://api.letta.com").replace(/\/+$/, "");
|
|
3069
|
+
const headers = {
|
|
3070
|
+
Authorization: sourceKey.startsWith("Bearer ") ? sourceKey : `Bearer ${sourceKey}`,
|
|
3071
|
+
Accept: "application/json",
|
|
3072
|
+
};
|
|
3073
|
+
const aRes = await fetch(`${base}/v1/agents/`, { headers });
|
|
3074
|
+
if (!aRes.ok) {
|
|
3075
|
+
const errTxt = await aRes.text().catch(() => "");
|
|
3076
|
+
throw new Error(`Letta API returned HTTP ${aRes.status}: ${errTxt.slice(0, 200)}`);
|
|
3077
|
+
}
|
|
3078
|
+
const agents = await aRes.json();
|
|
3079
|
+
const agentList = Array.isArray(agents) ? agents : agents.agents || [];
|
|
3080
|
+
for (const ag of agentList.slice(0, 30)) {
|
|
3081
|
+
const aid = ag.id;
|
|
3082
|
+
if (!aid) continue;
|
|
3083
|
+
const pRes = await fetch(`${base}/v1/agents/${encodeURIComponent(aid)}/archival-memory?limit=100`, {
|
|
3084
|
+
headers,
|
|
3085
|
+
});
|
|
3086
|
+
if (!pRes.ok) continue;
|
|
3087
|
+
const passages = await pRes.json();
|
|
3088
|
+
const pList = Array.isArray(passages) ? passages : passages.passages || passages.results || [];
|
|
3089
|
+
for (const p of pList) {
|
|
3090
|
+
const n = normalizeImportedRecord(p, "letta", ag.name || defaultUserId, { agent_id: aid });
|
|
3091
|
+
if (n) collected.push(n);
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3094
|
+
return collected;
|
|
3095
|
+
}
|
|
3096
|
+
|
|
3097
|
+
throw new Error(
|
|
3098
|
+
`Unsupported provider "${provider}". Supported providers: mem0, zep, supermemory, honcho, hindsight, letta, file.`
|
|
3099
|
+
);
|
|
3100
|
+
}
|
|
3101
|
+
|
|
3102
|
+
async function batchIngestMigratedRecords(opts, records) {
|
|
3103
|
+
const seenIds = new Set();
|
|
3104
|
+
const deduped = [];
|
|
3105
|
+
for (const r of records) {
|
|
3106
|
+
if (!r || !r.text) continue;
|
|
3107
|
+
const id = r.id || makeMigrationDocId("import", r.text, r.user_id || opts.userId);
|
|
3108
|
+
if (seenIds.has(id)) continue;
|
|
3109
|
+
seenIds.add(id);
|
|
3110
|
+
deduped.push({ ...r, id });
|
|
3111
|
+
}
|
|
3112
|
+
|
|
3113
|
+
if (opts.isDryRun || deduped.length === 0) {
|
|
3114
|
+
return { total_discovered: deduped.length, ingested_count: 0, dry_run: Boolean(opts.isDryRun), sample: deduped.slice(0, 5) };
|
|
3115
|
+
}
|
|
3116
|
+
|
|
3117
|
+
if (!opts.apiKey) {
|
|
3118
|
+
throw new Error(
|
|
3119
|
+
"Missing SLASHML_API_KEY. Set SLASHML_API_KEY or pass --api-key <sml_live_...> (or sign in at https://app.slashml.ai)."
|
|
3120
|
+
);
|
|
3121
|
+
}
|
|
3122
|
+
|
|
3123
|
+
let ingestedCount = 0;
|
|
3124
|
+
const batchSize = 50;
|
|
3125
|
+
for (let i = 0; i < deduped.length; i += batchSize) {
|
|
3126
|
+
const chunk = deduped.slice(i, i + batchSize);
|
|
3127
|
+
const documents = chunk.map((r) => ({
|
|
3128
|
+
id: r.id,
|
|
3129
|
+
text: r.text,
|
|
3130
|
+
metadata: {
|
|
3131
|
+
...(r.metadata || {}),
|
|
3132
|
+
user_id: r.user_id || opts.userId || "default",
|
|
3133
|
+
},
|
|
3134
|
+
}));
|
|
3135
|
+
await apiRequest(opts, "POST", "/v1/memory/ingest", { documents });
|
|
3136
|
+
ingestedCount += chunk.length;
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
return {
|
|
3140
|
+
total_discovered: deduped.length,
|
|
3141
|
+
ingested_count: ingestedCount,
|
|
3142
|
+
dry_run: false,
|
|
3143
|
+
sample: deduped.slice(0, 5),
|
|
3144
|
+
};
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
async function runMigrateCommand(opts) {
|
|
3148
|
+
const provider = (opts.migrateFrom || (opts.importFile ? "file" : "mem0")).toLowerCase();
|
|
3149
|
+
let records = [];
|
|
3150
|
+
|
|
3151
|
+
if (opts.importFile || provider === "file") {
|
|
3152
|
+
if (!opts.importFile) {
|
|
3153
|
+
throw new Error("Missing --file <path> for file migration. Example: npx -y @slashml/memory migrate --file ./memories.json");
|
|
3154
|
+
}
|
|
3155
|
+
const resolvedPath = path.resolve(opts.importFile);
|
|
3156
|
+
if (!fs.existsSync(resolvedPath)) {
|
|
3157
|
+
throw new Error(`File not found: ${resolvedPath}`);
|
|
3158
|
+
}
|
|
3159
|
+
const content = fs.readFileSync(resolvedPath, "utf8");
|
|
3160
|
+
records = parseMemoriesFromFileContent(content, resolvedPath, opts.userId || "default");
|
|
3161
|
+
} else {
|
|
3162
|
+
const envKeyMap = {
|
|
3163
|
+
mem0: process.env.MEM0_API_KEY,
|
|
3164
|
+
zep: process.env.ZEP_API_KEY,
|
|
3165
|
+
supermemory: process.env.SUPERMEMORY_API_KEY,
|
|
3166
|
+
honcho: process.env.HONCHO_API_KEY,
|
|
3167
|
+
hindsight: process.env.HINDSIGHT_API_KEY,
|
|
3168
|
+
letta: process.env.LETTA_API_KEY,
|
|
3169
|
+
memgpt: process.env.LETTA_API_KEY,
|
|
3170
|
+
};
|
|
3171
|
+
const resolvedSourceKey = opts.sourceKey || envKeyMap[provider];
|
|
3172
|
+
if (!resolvedSourceKey) {
|
|
3173
|
+
throw new Error(
|
|
3174
|
+
`Missing source API key for "${provider}". Pass --source-key <key> or set ${provider.toUpperCase()}_API_KEY.`
|
|
3175
|
+
);
|
|
3176
|
+
}
|
|
3177
|
+
records = await fetchProviderMemories(provider, resolvedSourceKey, opts.sourceUrl, opts.userId || "default");
|
|
3178
|
+
}
|
|
3179
|
+
|
|
3180
|
+
const res = await batchIngestMigratedRecords(opts, records);
|
|
3181
|
+
const payload = {
|
|
3182
|
+
status: opts.isDryRun ? "dry_run" : "migrated",
|
|
3183
|
+
source: opts.importFile ? `file:${opts.importFile}` : provider,
|
|
3184
|
+
user_id: opts.userId || "default",
|
|
3185
|
+
...res,
|
|
3186
|
+
};
|
|
3187
|
+
|
|
3188
|
+
if (opts.jsonOutput) {
|
|
3189
|
+
process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
|
|
3190
|
+
} else {
|
|
3191
|
+
console.log(
|
|
3192
|
+
`[/ ml] ${opts.isDryRun ? "[DRY RUN] Discovered" : "Migrated"} ${res.ingested_count || res.total_discovered} memories from ${payload.source} into / ml (user_id=${payload.user_id}).`
|
|
3193
|
+
);
|
|
3194
|
+
}
|
|
3195
|
+
}
|
|
3196
|
+
|
|
3197
|
+
function discoverLocalAgentMemoryFiles(workspaceRoot) {
|
|
3198
|
+
const home = os.homedir();
|
|
3199
|
+
const cwd = workspaceRoot ? path.resolve(workspaceRoot) : process.cwd();
|
|
3200
|
+
const candidates = [];
|
|
3201
|
+
|
|
3202
|
+
function addIfFile(p, kind) {
|
|
3203
|
+
try {
|
|
3204
|
+
if (fs.existsSync(p) && fs.statSync(p).isFile()) {
|
|
3205
|
+
candidates.push({ path: p, kind });
|
|
3206
|
+
}
|
|
3207
|
+
} catch {
|
|
3208
|
+
// ignore
|
|
3209
|
+
}
|
|
3210
|
+
}
|
|
3211
|
+
|
|
3212
|
+
// 1. Claude Code global & per-project auto-memory (~/.claude/CLAUDE.md, ~/.claude/projects/*/memory/*.md)
|
|
3213
|
+
addIfFile(path.join(home, ".claude", "CLAUDE.md"), "claude_global");
|
|
3214
|
+
const claudeProjectsDir = path.join(home, ".claude", "projects");
|
|
3215
|
+
if (fs.existsSync(claudeProjectsDir)) {
|
|
3216
|
+
try {
|
|
3217
|
+
for (const projEntry of fs.readdirSync(claudeProjectsDir, { withFileTypes: true })) {
|
|
3218
|
+
if (!projEntry.isDirectory()) continue;
|
|
3219
|
+
const projMemoryDir = path.join(claudeProjectsDir, projEntry.name, "memory");
|
|
3220
|
+
if (fs.existsSync(projMemoryDir)) {
|
|
3221
|
+
for (const memFile of fs.readdirSync(projMemoryDir, { withFileTypes: true })) {
|
|
3222
|
+
if (memFile.isFile() && memFile.name.endsWith(".md") && memFile.name.toLowerCase() !== "memory.md") {
|
|
3223
|
+
addIfFile(path.join(projMemoryDir, memFile.name), `claude_project:${projEntry.name}`);
|
|
3224
|
+
} else if (memFile.isFile() && memFile.name.toLowerCase() === "memory.md") {
|
|
3225
|
+
addIfFile(path.join(projMemoryDir, memFile.name), `claude_project_index:${projEntry.name}`);
|
|
3226
|
+
}
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
}
|
|
3230
|
+
} catch {
|
|
3231
|
+
// ignore
|
|
3232
|
+
}
|
|
3233
|
+
}
|
|
3234
|
+
|
|
3235
|
+
// 2. Workspace-level agent rule & memory files
|
|
3236
|
+
for (const rel of [
|
|
3237
|
+
"CLAUDE.md",
|
|
3238
|
+
"AGENTS.md",
|
|
3239
|
+
".cursorrules",
|
|
3240
|
+
".windsurfrules",
|
|
3241
|
+
".clinerules",
|
|
3242
|
+
".github/copilot-instructions.md",
|
|
3243
|
+
]) {
|
|
3244
|
+
addIfFile(path.join(cwd, rel), "workspace_rules");
|
|
3245
|
+
}
|
|
3246
|
+
|
|
3247
|
+
// 3. Cursor .cursor/rules/*.{mdc,md}
|
|
3248
|
+
const cursorRulesDir = path.join(cwd, ".cursor", "rules");
|
|
3249
|
+
if (fs.existsSync(cursorRulesDir)) {
|
|
3250
|
+
try {
|
|
3251
|
+
for (const e of fs.readdirSync(cursorRulesDir, { withFileTypes: true })) {
|
|
3252
|
+
if (e.isFile() && (e.name.endsWith(".mdc") || e.name.endsWith(".md"))) {
|
|
3253
|
+
addIfFile(path.join(cursorRulesDir, e.name), "cursor_rule");
|
|
3254
|
+
}
|
|
3255
|
+
}
|
|
3256
|
+
} catch {
|
|
3257
|
+
// ignore
|
|
3258
|
+
}
|
|
3259
|
+
}
|
|
3260
|
+
|
|
3261
|
+
// 4. Local OpenMemory / Mem0 JSON dumps (~/.openmemory, ~/.mem0)
|
|
3262
|
+
for (const localDir of [path.join(home, ".openmemory"), path.join(home, ".mem0")]) {
|
|
3263
|
+
if (fs.existsSync(localDir)) {
|
|
3264
|
+
try {
|
|
3265
|
+
for (const e of fs.readdirSync(localDir, { withFileTypes: true })) {
|
|
3266
|
+
if (e.isFile() && (e.name.endsWith(".json") || e.name.endsWith(".jsonl"))) {
|
|
3267
|
+
addIfFile(path.join(localDir, e.name), "local_memory_dump");
|
|
3268
|
+
}
|
|
3269
|
+
}
|
|
3270
|
+
} catch {
|
|
3271
|
+
// ignore
|
|
3272
|
+
}
|
|
3273
|
+
}
|
|
3274
|
+
}
|
|
3275
|
+
|
|
3276
|
+
return candidates;
|
|
3277
|
+
}
|
|
3278
|
+
|
|
3279
|
+
async function runImportLocalCommand(opts) {
|
|
3280
|
+
const workspaceDir = opts.repo ? path.resolve(opts.repo) : process.cwd();
|
|
3281
|
+
const projectId = path.basename(workspaceDir.replace(/[\\/]+$/, "")) || "default";
|
|
3282
|
+
const discoveredFiles = discoverLocalAgentMemoryFiles(workspaceDir);
|
|
3283
|
+
const allRecords = [];
|
|
3284
|
+
|
|
3285
|
+
for (const item of discoveredFiles) {
|
|
3286
|
+
try {
|
|
3287
|
+
const raw = fs.readFileSync(item.path, "utf8");
|
|
3288
|
+
const parsedRecords = parseMemoriesFromFileContent(raw, item.path, opts.userId || "default").map((r) => ({
|
|
3289
|
+
...r,
|
|
3290
|
+
id: makeMigrationDocId("local", r.text, opts.userId || "default"),
|
|
3291
|
+
metadata: {
|
|
3292
|
+
...(r.metadata || {}),
|
|
3293
|
+
source_kind: item.kind,
|
|
3294
|
+
source_file: path.basename(item.path),
|
|
3295
|
+
project_id: projectId,
|
|
3296
|
+
},
|
|
3297
|
+
}));
|
|
3298
|
+
allRecords.push(...parsedRecords);
|
|
3299
|
+
} catch {
|
|
3300
|
+
// ignore unreadable file
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
|
|
3304
|
+
const res = await batchIngestMigratedRecords(opts, allRecords);
|
|
3305
|
+
const payload = {
|
|
3306
|
+
status: opts.isDryRun ? "dry_run" : "imported_local",
|
|
3307
|
+
scanned_files: discoveredFiles.map((f) => f.path),
|
|
3308
|
+
project_id: projectId,
|
|
3309
|
+
user_id: opts.userId || "default",
|
|
3310
|
+
...res,
|
|
3311
|
+
};
|
|
3312
|
+
|
|
3313
|
+
if (opts.jsonOutput) {
|
|
3314
|
+
process.stdout.write(JSON.stringify(payload, null, 2) + "\n");
|
|
3315
|
+
} else {
|
|
3316
|
+
console.log(
|
|
3317
|
+
`[/ ml] ${opts.isDryRun ? "[DRY RUN] Discovered" : "Imported"} ${res.ingested_count || res.total_discovered} local coding-agent memories across ${discoveredFiles.length} file(s) into / ml (project=${projectId}, user_id=${payload.user_id}).`
|
|
3318
|
+
);
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
|
|
3322
|
+
if (parsed.isMigrate) {
|
|
3323
|
+
runMigrateCommand(parsed)
|
|
3324
|
+
.then(() => process.exit(0))
|
|
3325
|
+
.catch((err) => {
|
|
3326
|
+
console.error(`Error: ${err.message || err}`);
|
|
3327
|
+
process.exit(1);
|
|
3328
|
+
});
|
|
3329
|
+
return;
|
|
3330
|
+
}
|
|
3331
|
+
|
|
3332
|
+
if (parsed.isImportLocal) {
|
|
3333
|
+
runImportLocalCommand(parsed)
|
|
3334
|
+
.then(() => process.exit(0))
|
|
3335
|
+
.catch((err) => {
|
|
3336
|
+
console.error(`Error: ${err.message || err}`);
|
|
3337
|
+
process.exit(1);
|
|
3338
|
+
});
|
|
3339
|
+
return;
|
|
3340
|
+
}
|
|
3341
|
+
|
|
2640
3342
|
if (parsed.isInstallHooks) {
|
|
2641
3343
|
installAmbientHooks(parsed.apiKey);
|
|
2642
3344
|
process.exit(0);
|