mr-memory 3.9.0 → 3.10.0

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.
@@ -0,0 +1,511 @@
1
+ /**
2
+ * Agent-driven historical import: the transport half (OpenClaw lane).
3
+ *
4
+ * Division of labor (locked): the SERVER owns the extraction prompt
5
+ * (GET /v1/memory/imports/instructions, versioned like reflection_contract);
6
+ * the HOST AGENT executes that prompt locally and writes a JSONL extract; THIS
7
+ * MODULE owns transport only: local validation, deterministic identity math
8
+ * (byte-for-byte with workers/src/services/import-identity.ts), create and
9
+ * approve, idempotent batch upload with resume, and the receipt. The agent
10
+ * never touches HTTP for upload and this module never parses history.
11
+ *
12
+ * The Memory Key never appears in the prompt or the extract file; it rides
13
+ * only in the Authorization header.
14
+ */
15
+
16
+ import { createHash } from "node:crypto";
17
+ import { readFileSync } from "node:fs";
18
+ import readline from "node:readline";
19
+ import {
20
+ importInstructions,
21
+ importCreate,
22
+ importApprove,
23
+ importBatch,
24
+ importStatus,
25
+ importReceipt,
26
+ importDismiss,
27
+ } from "./memoryrouter-api.js";
28
+
29
+ export const PLATFORM = "openclaw";
30
+ export const PARSER_VERSION = 1;
31
+ export const CHUNK_VERSION = 1;
32
+ const BATCH_SIZE = 2000;
33
+ const MAX_CONTENT_CHARS = 1000000;
34
+ const MAX_UPLOAD_ATTEMPTS = 5;
35
+ const INITIAL_BACKOFF_MS = 2000;
36
+ const MAX_BACKOFF_MS = 30000;
37
+
38
+ // ── identity math (mirrors the server byte-for-byte) ─────────────────────────
39
+
40
+ function sha256hex(input: string): string {
41
+ return createHash("sha256").update(input, "utf8").digest("hex");
42
+ }
43
+
44
+ function sortKeys(value: unknown): unknown {
45
+ if (Array.isArray(value)) return value.map(sortKeys);
46
+ if (value && typeof value === "object") {
47
+ const out: Record<string, unknown> = {};
48
+ for (const key of Object.keys(value as Record<string, unknown>).sort()) {
49
+ out[key] = sortKeys((value as Record<string, unknown>)[key]);
50
+ }
51
+ return out;
52
+ }
53
+ return value;
54
+ }
55
+
56
+ export function canonicalJson(value: unknown): string {
57
+ return JSON.stringify(sortKeys(value));
58
+ }
59
+
60
+ export function normalizeText(input: string): string {
61
+ return input.normalize("NFC").replace(/\r\n?/g, "\n").trim();
62
+ }
63
+
64
+ export function vaultFingerprint(memoryKey: string): string {
65
+ return `mrvault_${sha256hex(memoryKey).slice(0, 16)}`;
66
+ }
67
+
68
+ function unescapeIdPart(s: string): string {
69
+ return s.replace(/%3A/g, ":").replace(/%25/g, "%");
70
+ }
71
+
72
+ export function parseExternalId(id: string): { platform: string; conversationId: string; messageId: string; parserVersion: number } | null {
73
+ const parts = id.split(":");
74
+ if (parts.length !== 4) return null;
75
+ const [platform, conv, msg, ver] = parts;
76
+ if (!/^v\d+$/.test(ver)) return null;
77
+ return {
78
+ platform,
79
+ conversationId: unescapeIdPart(conv),
80
+ messageId: unescapeIdPart(msg),
81
+ parserVersion: parseInt(ver.slice(1), 10),
82
+ };
83
+ }
84
+
85
+ export function recordHash(fields: { conversationId: string; messageId: string; role: string; text: string; timestamp: number }): string {
86
+ return sha256hex(
87
+ canonicalJson({
88
+ conversationId: fields.conversationId,
89
+ messageId: fields.messageId,
90
+ role: fields.role,
91
+ text: normalizeText(fields.text),
92
+ timestamp: fields.timestamp,
93
+ v: PARSER_VERSION,
94
+ }),
95
+ ).slice(0, 32);
96
+ }
97
+
98
+ export function conversationContentHash(fields: {
99
+ conversationId: string;
100
+ canonicalPolicy: string;
101
+ canonicalLeaf: string | null;
102
+ records: Array<{ externalId: string; recordHash: string; role: string; timestamp: number }>;
103
+ }): string {
104
+ return sha256hex(canonicalJson(fields)).slice(0, 32);
105
+ }
106
+
107
+ export function archiveContentId(conversationContentHashes: string[]): string {
108
+ const canonical = [...conversationContentHashes].sort().join("\n");
109
+ return `arc_${sha256hex(`v${PARSER_VERSION}\n${canonical}`).slice(0, 24)}`;
110
+ }
111
+
112
+ export function deriveImportId(archiveId: string, fingerprint: string): string {
113
+ return `imp_${sha256hex(`${archiveId}|${fingerprint}|p${PARSER_VERSION}|c${CHUNK_VERSION}`).slice(0, 24)}`;
114
+ }
115
+
116
+ export function previewDigest(preview: unknown): string {
117
+ return `pvd_${sha256hex(canonicalJson(preview)).slice(0, 32)}`;
118
+ }
119
+
120
+ // ── extract validation ───────────────────────────────────────────────────────
121
+
122
+ export interface ExtractRecord {
123
+ externalId: string;
124
+ conversationId: string;
125
+ messageId: string;
126
+ content: string;
127
+ role: "user" | "assistant";
128
+ timestamp: number;
129
+ recordSha256?: string;
130
+ }
131
+
132
+ export function readExtract(filePath: string, platform: string = PLATFORM): { records?: ExtractRecord[]; error?: string } {
133
+ let raw: string;
134
+ try {
135
+ raw = readFileSync(filePath, "utf8");
136
+ } catch (err) {
137
+ return { error: `could not read ${filePath}: ${err instanceof Error ? err.message : String(err)}` };
138
+ }
139
+ const records: ExtractRecord[] = [];
140
+ const seen = new Set<string>();
141
+ const lines = raw.split("\n");
142
+ for (let i = 0; i < lines.length; i++) {
143
+ const line = lines[i].trim();
144
+ if (!line) continue;
145
+ const lineNo = i + 1;
146
+ let p: Record<string, unknown>;
147
+ try {
148
+ p = JSON.parse(line) as Record<string, unknown>;
149
+ } catch {
150
+ return { error: `line ${lineNo}: not valid JSON` };
151
+ }
152
+ if (!p || typeof p !== "object" || Array.isArray(p)) return { error: `line ${lineNo}: record is not an object` };
153
+ const extras = Object.keys(p).filter((k) => !["external_id", "content", "timestamp", "role"].includes(k));
154
+ if (extras.length) return { error: `line ${lineNo}: unexpected fields: ${extras.join(", ")}` };
155
+ if (typeof p.external_id !== "string" || !p.external_id) return { error: `line ${lineNo}: external_id must be a string` };
156
+ const parsed = parseExternalId(p.external_id);
157
+ if (!parsed) return { error: `line ${lineNo}: malformed external_id (expected ${platform}:<conversation>:<message>:v${PARSER_VERSION})` };
158
+ if (parsed.platform !== platform) return { error: `line ${lineNo}: external_id platform must be ${platform}, got ${parsed.platform}` };
159
+ if (parsed.parserVersion !== PARSER_VERSION) return { error: `line ${lineNo}: external_id version must be v${PARSER_VERSION}` };
160
+ if (typeof p.content !== "string" || p.content.length === 0) return { error: `line ${lineNo}: content must be a non-empty string` };
161
+ if (p.content.length > MAX_CONTENT_CHARS) return { error: `line ${lineNo}: content exceeds ${MAX_CONTENT_CHARS} chars` };
162
+ if (typeof p.timestamp !== "number" || !Number.isFinite(p.timestamp) || p.timestamp <= 0) {
163
+ return { error: `line ${lineNo}: timestamp must be a positive epoch-milliseconds number` };
164
+ }
165
+ if (p.timestamp < 1000000000000) return { error: `line ${lineNo}: timestamp looks like seconds, not milliseconds (${p.timestamp})` };
166
+ if (p.role !== "user" && p.role !== "assistant") return { error: `line ${lineNo}: role must be "user" or "assistant"` };
167
+ if (seen.has(p.external_id)) return { error: `line ${lineNo}: duplicate external_id ${p.external_id}` };
168
+ seen.add(p.external_id);
169
+ records.push({
170
+ externalId: p.external_id,
171
+ conversationId: parsed.conversationId,
172
+ messageId: parsed.messageId,
173
+ content: p.content,
174
+ role: p.role,
175
+ timestamp: p.timestamp,
176
+ });
177
+ }
178
+ if (records.length === 0) return { error: "the extract file contains no records" };
179
+ return { records };
180
+ }
181
+
182
+ // ── identity + preview ───────────────────────────────────────────────────────
183
+
184
+ export function computeIdentity(records: ExtractRecord[], memoryKey: string, platform: string = PLATFORM) {
185
+ const byConversation = new Map<string, ExtractRecord[]>();
186
+ for (const r of records) {
187
+ r.recordSha256 = recordHash({
188
+ conversationId: r.conversationId,
189
+ messageId: r.messageId,
190
+ role: r.role,
191
+ text: r.content,
192
+ timestamp: r.timestamp,
193
+ });
194
+ if (!byConversation.has(r.conversationId)) byConversation.set(r.conversationId, []);
195
+ byConversation.get(r.conversationId)!.push(r);
196
+ }
197
+ const convHashes: string[] = [];
198
+ for (const [conversationId, convRecords] of byConversation) {
199
+ convHashes.push(conversationContentHash({
200
+ conversationId,
201
+ canonicalPolicy: `agent-extract-${platform}`,
202
+ canonicalLeaf: null,
203
+ records: convRecords.map((r) => ({
204
+ externalId: r.externalId,
205
+ recordHash: r.recordSha256!,
206
+ role: r.role,
207
+ timestamp: r.timestamp,
208
+ })),
209
+ }));
210
+ }
211
+ const archiveId = archiveContentId(convHashes);
212
+ const fingerprint = vaultFingerprint(memoryKey);
213
+ const importId = deriveImportId(archiveId, fingerprint);
214
+ return { archiveId, fingerprint, importId, conversations: byConversation.size };
215
+ }
216
+
217
+ export function estimateTokens(records: ExtractRecord[]): number {
218
+ let total = 0;
219
+ for (const r of records) total += Math.ceil(r.content.length / 4);
220
+ return total;
221
+ }
222
+
223
+ export function buildPreviewSummary(records: ExtractRecord[]) {
224
+ let earliest = Number.POSITIVE_INFINITY;
225
+ let latest = 0;
226
+ let user = 0;
227
+ for (const r of records) {
228
+ if (r.role === "user") user++;
229
+ if (r.timestamp < earliest) earliest = r.timestamp;
230
+ if (r.timestamp > latest) latest = r.timestamp;
231
+ }
232
+ return {
233
+ records: records.length,
234
+ userRecords: user,
235
+ assistantRecords: records.length - user,
236
+ earliestMs: Number.isFinite(earliest) ? earliest : 0,
237
+ latestMs: latest,
238
+ estimatedTokens: estimateTokens(records),
239
+ };
240
+ }
241
+
242
+ export function toWireRecord(r: ExtractRecord, platform: string = PLATFORM): string {
243
+ return JSON.stringify({
244
+ external_id: r.externalId,
245
+ record_sha256: r.recordSha256,
246
+ content: r.content,
247
+ role: r.role,
248
+ timestamp: r.timestamp,
249
+ source_platform: platform,
250
+ parser_version: PARSER_VERSION,
251
+ chunk_version: CHUNK_VERSION,
252
+ });
253
+ }
254
+
255
+ // ── flows ────────────────────────────────────────────────────────────────────
256
+
257
+ type Fetchers = {
258
+ importInstructions: typeof importInstructions;
259
+ importCreate: typeof importCreate;
260
+ importApprove: typeof importApprove;
261
+ importBatch: typeof importBatch;
262
+ importStatus: typeof importStatus;
263
+ importReceipt: typeof importReceipt;
264
+ importDismiss: typeof importDismiss;
265
+ };
266
+
267
+ const defaultFetchers: Fetchers = {
268
+ importInstructions,
269
+ importCreate,
270
+ importApprove,
271
+ importBatch,
272
+ importStatus,
273
+ importReceipt,
274
+ importDismiss,
275
+ };
276
+
277
+ export interface ImportOptions {
278
+ endpoint: string;
279
+ key: string;
280
+ platform?: string;
281
+ file?: string;
282
+ yes?: boolean;
283
+ nonInteractive?: boolean;
284
+ batchSize?: number;
285
+ log?: (...args: unknown[]) => void;
286
+ fetchers?: Partial<Fetchers>;
287
+ }
288
+
289
+ function fmtDate(ms: number): string {
290
+ if (!Number.isFinite(ms) || ms <= 0) return "unknown";
291
+ return new Date(ms).toISOString().slice(0, 10);
292
+ }
293
+
294
+ function fmtTokens(n: number): string {
295
+ if (n >= 1000000) return `${(n / 1000000).toFixed(1)}M`;
296
+ if (n >= 1000) return `${(n / 1000).toFixed(1)}K`;
297
+ return String(n);
298
+ }
299
+
300
+ async function jsonOf(res: Response): Promise<Record<string, any>> {
301
+ try {
302
+ return (await res.json()) as Record<string, any>;
303
+ } catch {
304
+ return {};
305
+ }
306
+ }
307
+
308
+ function errorDetail(status: number, body: Record<string, unknown>): string {
309
+ const parts = [body.error, body.message].filter(Boolean);
310
+ return parts.length ? parts.join(": ") : `HTTP ${status}`;
311
+ }
312
+
313
+ function sleep(ms: number): Promise<void> {
314
+ return new Promise((resolve) => setTimeout(resolve, ms));
315
+ }
316
+
317
+ function ask(question: string): Promise<string> {
318
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
319
+ return new Promise((resolve) => rl.question(question, (answer) => {
320
+ rl.close();
321
+ resolve(answer.trim().toLowerCase());
322
+ }));
323
+ }
324
+
325
+ /** Fetch and print the server-side extraction prompt for the host agent. */
326
+ export async function runInstructions(options: ImportOptions): Promise<{ version: number }> {
327
+ const f = { ...defaultFetchers, ...(options.fetchers || {}) };
328
+ const log = options.log || ((...args: unknown[]) => console.log(...args));
329
+ const res = await f.importInstructions(options.endpoint, options.key, options.platform || PLATFORM);
330
+ const body = await jsonOf(res);
331
+ if (!res.ok) throw new Error(`could not fetch import instructions (${errorDetail(res.status, body)})`);
332
+ if (!body.prompt) throw new Error("instructions response carried no prompt");
333
+ log(body.prompt);
334
+ return { version: Number(body.import_prompt_version) || 0 };
335
+ }
336
+
337
+ /** Call the dismiss endpoint so the server-side nudge stops permanently. */
338
+ export async function runDismiss(options: ImportOptions): Promise<void> {
339
+ const f = { ...defaultFetchers, ...(options.fetchers || {}) };
340
+ const log = options.log || ((...args: unknown[]) => console.log(...args));
341
+ const res = await f.importDismiss(options.endpoint, options.key, options.platform || PLATFORM);
342
+ const body = await jsonOf(res);
343
+ if (!res.ok) throw new Error(`dismiss failed (${errorDetail(res.status, body)})`);
344
+ log("Import suggestion dismissed permanently for this vault.");
345
+ }
346
+
347
+ /** The transport: validate, create, quote, approve, upload, receipt. */
348
+ export async function runImportFile(options: ImportOptions): Promise<{
349
+ aborted?: boolean;
350
+ importId?: string;
351
+ totals?: Record<string, number>;
352
+ complete?: boolean;
353
+ receipt?: unknown;
354
+ }> {
355
+ const f = { ...defaultFetchers, ...(options.fetchers || {}) };
356
+ const log = options.log || ((...args: unknown[]) => console.log(...args));
357
+ const platform = options.platform || PLATFORM;
358
+ if (!options.file) throw new Error("--file <path> is required");
359
+
360
+ const parsed = readExtract(options.file, platform);
361
+ if (parsed.error) throw new Error(`invalid extract: ${parsed.error}`);
362
+ const records = parsed.records!;
363
+ const identity = computeIdentity(records, options.key, platform);
364
+ const preview = buildPreviewSummary(records);
365
+
366
+ const clientPreviewDigest = previewDigest({
367
+ importId: identity.importId,
368
+ archiveId: identity.archiveId,
369
+ fingerprint: identity.fingerprint,
370
+ parserVersion: PARSER_VERSION,
371
+ chunkVersion: CHUNK_VERSION,
372
+ expectedRecords: preview.records,
373
+ expectedChunks: preview.records,
374
+ estimatedTokens: preview.estimatedTokens,
375
+ selection: null,
376
+ categories: [],
377
+ });
378
+ const createRes = await f.importCreate(options.endpoint, options.key, {
379
+ import_id: identity.importId,
380
+ archive_content_id: identity.archiveId,
381
+ archive_sha256: null,
382
+ parser_version: PARSER_VERSION,
383
+ chunk_version: CHUNK_VERSION,
384
+ source_platform: platform,
385
+ vault_fingerprint: identity.fingerprint,
386
+ preview_digest: clientPreviewDigest,
387
+ expected_records: preview.records,
388
+ expected_chunks: preview.records,
389
+ estimated_tokens: preview.estimatedTokens,
390
+ selection: null,
391
+ categories: [],
392
+ });
393
+ const created = await jsonOf(createRes);
394
+ if (!createRes.ok) throw new Error(`import preflight failed (${errorDetail(createRes.status, created)})`);
395
+ if (created.preview_digest && created.preview_digest !== clientPreviewDigest) {
396
+ throw new Error("client/server preview digest mismatch (fail closed)");
397
+ }
398
+ const quote = (created.cost_quote || {}) as Record<string, any>;
399
+
400
+ log("");
401
+ log("Import preview");
402
+ log("----------------------------");
403
+ log(`Platform: ${platform}`);
404
+ log(`Destination vault: ${identity.fingerprint} (opaque fingerprint)`);
405
+ log(`Conversations: ${identity.conversations}`);
406
+ log(`Records: ${preview.records} (user ${preview.userRecords}, assistant ${preview.assistantRecords})`);
407
+ log(`Date range: ${fmtDate(preview.earliestMs)} to ${fmtDate(preview.latestMs)}`);
408
+ log(`Estimated tokens: ${fmtTokens(preview.estimatedTokens)}`);
409
+ if (quote.available) {
410
+ log(`Estimated cost: $${((quote.estimated_cost_cents || 0) / 100).toFixed(2)} (rate $${quote.rate_per_million_usd}/1M paid tokens, free remaining ${fmtTokens(quote.free_tokens_remaining || 0)})`);
411
+ } else {
412
+ log(`Estimated cost: unavailable (${quote.reason || "unknown"}); the server will refuse approval (fail closed)`);
413
+ }
414
+ log(`Import id: ${identity.importId}`);
415
+ log("");
416
+ log("Re-running this command is always safe: committed batches replay for free and duplicates are never re-billed.");
417
+ log("");
418
+
419
+ if (!options.yes) {
420
+ if (options.nonInteractive || !process.stdin.isTTY) {
421
+ throw new Error("approval required: re-run with --yes to confirm the upload");
422
+ }
423
+ const answer = await ask("Proceed with the upload? [y/N]: ");
424
+ if (answer !== "y" && answer !== "yes") {
425
+ log("Aborted. Nothing was uploaded.");
426
+ return { aborted: true };
427
+ }
428
+ }
429
+
430
+ const approveRes = await f.importApprove(options.endpoint, options.key, identity.importId, String(created.approval_digest));
431
+ if (!approveRes.ok) {
432
+ const body = await jsonOf(approveRes);
433
+ // A completed import cannot be re-approved; that is the fully-settled
434
+ // resume case. Print the receipt instead of failing.
435
+ if (body.error === "import_completed") {
436
+ const receiptRes = await f.importReceipt(options.endpoint, options.key, identity.importId);
437
+ let receipt: unknown = null;
438
+ if (receiptRes.ok) {
439
+ const receiptBody = await jsonOf(receiptRes);
440
+ receipt = receiptBody.receipt ?? null;
441
+ }
442
+ log("This exact extract was already fully imported into this vault. Nothing to upload.");
443
+ if (receipt) {
444
+ log("Receipt:");
445
+ log(JSON.stringify(receipt, null, 2));
446
+ }
447
+ return { importId: identity.importId, complete: true, receipt };
448
+ }
449
+ throw new Error(`approval refused (${errorDetail(approveRes.status, body)})`);
450
+ }
451
+
452
+ const batches: ExtractRecord[][] = [];
453
+ const size = options.batchSize || BATCH_SIZE;
454
+ for (let i = 0; i < records.length; i += size) batches.push(records.slice(i, i + size));
455
+ const totals = { stored: 0, duplicate: 0, skipped: 0, failed: 0, conflict: 0 };
456
+ log(`Uploading ${records.length} record(s) in ${batches.length} batch(es)...`);
457
+ for (let i = 0; i < batches.length; i++) {
458
+ const jsonl = batches[i].map((r) => toWireRecord(r, platform)).join("\n");
459
+ const idempotencyKey = `${identity.importId}:b${i}`;
460
+ let res: Response | null = null;
461
+ let backoff = INITIAL_BACKOFF_MS;
462
+ for (let attempt = 1; attempt <= MAX_UPLOAD_ATTEMPTS; attempt++) {
463
+ try {
464
+ res = await f.importBatch(options.endpoint, options.key, identity.importId, i, jsonl, idempotencyKey);
465
+ } catch (err) {
466
+ res = null;
467
+ if (attempt === MAX_UPLOAD_ATTEMPTS) {
468
+ throw new Error(`batch ${i} failed after ${MAX_UPLOAD_ATTEMPTS} attempts: ${err instanceof Error ? err.message : String(err)}`);
469
+ }
470
+ }
471
+ if (res && (res.ok || (res.status < 500 && res.status !== 429))) break;
472
+ if (attempt === MAX_UPLOAD_ATTEMPTS) break;
473
+ await sleep(backoff);
474
+ backoff = Math.min(backoff * 2, MAX_BACKOFF_MS);
475
+ }
476
+ if (!res) throw new Error(`batch ${i} received no response`);
477
+ const body = await jsonOf(res);
478
+ if (res.status === 402) {
479
+ throw new Error("payment required: out of tokens/credits. Add credits at https://app.memoryrouter.ai and re-run to resume.");
480
+ }
481
+ if (!res.ok) throw new Error(`batch ${i} failed (${errorDetail(res.status, body)}). Re-run to resume; committed batches replay for free.`);
482
+ const s = (body.summary || {}) as Record<string, number>;
483
+ totals.stored += s.stored || 0;
484
+ totals.duplicate += s.duplicate || 0;
485
+ totals.skipped += s.skipped || 0;
486
+ totals.failed += s.failed || 0;
487
+ totals.conflict += s.conflict || 0;
488
+ log(` batch ${i + 1}/${batches.length}${body.replayed ? " (replayed, free)" : ""}: stored ${s.stored || 0}, duplicate ${s.duplicate || 0}, failed ${s.failed || 0}`);
489
+ }
490
+
491
+ const statusRes = await f.importStatus(options.endpoint, options.key, identity.importId);
492
+ const statusBody = await jsonOf(statusRes);
493
+ const complete = Boolean(statusRes.ok && statusBody.complete);
494
+ let receipt: unknown = null;
495
+ if (complete) {
496
+ const receiptRes = await f.importReceipt(options.endpoint, options.key, identity.importId);
497
+ if (receiptRes.ok) {
498
+ const receiptBody = await jsonOf(receiptRes);
499
+ receipt = receiptBody.receipt ?? null;
500
+ }
501
+ }
502
+ log("");
503
+ log(`Done. stored=${totals.stored} duplicate=${totals.duplicate} skipped=${totals.skipped} failed=${totals.failed} conflict=${totals.conflict}`);
504
+ if (receipt) {
505
+ log("Receipt:");
506
+ log(JSON.stringify(receipt, null, 2));
507
+ } else if (!complete) {
508
+ log("The import is not fully settled yet; re-run this command to resume/reconcile.");
509
+ }
510
+ return { importId: identity.importId, totals, complete, receipt };
511
+ }
package/index.js CHANGED
@@ -55,7 +55,6 @@ function stripOldMemory(text) {
55
55
  function jsonToolResult(payload) {
56
56
  return { content: [{ type: "text", text: JSON.stringify(payload, null, 2) }], details: payload };
57
57
  }
58
- const NUDGE_INTERVAL_MS = 24 * 60 * 60 * 1000; // at most one nudge per 24h
59
58
  function buildDebtNudgeLine(reflection) {
60
59
  if (!reflection || !reflection.suggestion)
61
60
  return null;
@@ -63,7 +62,12 @@ function buildDebtNudgeLine(reflection) {
63
62
  const tier2Tokens = reflection.unconsolidated?.tier2?.est_tokens ?? 0;
64
63
  const totalTokens = rawTokens + tier2Tokens;
65
64
  const kTokens = Math.max(1, Math.round(totalTokens / 1000));
66
- return `[MemoryRouter] Consolidation available: ${kTokens}k unconsolidated tokens. Use the memory_reflect tool when convenient.`;
65
+ return `[MemoryRouter] Consolidation suggested: ${kTokens}k unconsolidated tokens. Use the memory_reflect tool to consolidate them now.`;
66
+ }
67
+ function buildImportNudgeLine(importState) {
68
+ if (!importState || !importState.suggested)
69
+ return null;
70
+ return `[MemoryRouter] Historical import suggested: this machine has local conversation history not yet in your vault. Run: openclaw mr import --instructions`;
67
71
  }
68
72
  // ── memory_get helpers (copied from OpenClaw source — pure filesystem, no embeddings)
69
73
  function isFileMissingError(err) {
@@ -311,37 +315,6 @@ async function writeOpenClawConfigFile(config) {
311
315
  await writeFile(configPath, `${JSON.stringify(config, null, 2)}
312
316
  `, "utf8");
313
317
  }
314
- // Nudge rate-limit state lives beside the OpenClaw config so it survives restarts
315
- // without churning the main config file.
316
- function nudgeStatePath() {
317
- return join(dirname(openClawConfigPath()), "mr-memory-state.json");
318
- }
319
- async function consumeNudgeBudget() {
320
- const now = Date.now();
321
- let state = {};
322
- try {
323
- state = JSON.parse(await readFile(nudgeStatePath(), "utf8"));
324
- }
325
- catch { /* missing or unreadable state: treat as never nudged */ }
326
- if (typeof state.lastNudgeAt === "number" && now - state.lastNudgeAt < NUDGE_INTERVAL_MS) {
327
- return false;
328
- }
329
- try {
330
- await writeFile(nudgeStatePath(), `${JSON.stringify({ ...state, lastNudgeAt: now }, null, 2)}\n`, "utf8");
331
- }
332
- catch { /* state write failure must never break prepare */ }
333
- return true;
334
- }
335
- /** One-line, rate-limited debt nudge for prepare injections. Returns null when
336
- * there is no suggestion or the 24h budget is spent. */
337
- async function maybeDebtNudge(reflection) {
338
- const line = buildDebtNudgeLine(reflection);
339
- if (!line)
340
- return null;
341
- if (!(await consumeNudgeBudget()))
342
- return null;
343
- return line;
344
- }
345
318
  // ──────────────────────────────────────────────────────
346
319
  // Full-replace: disable OpenClaw's native memory search so MemoryRouter
347
320
  // owns the memory_search tool (no name conflict, no native embeddings 429).
@@ -773,9 +746,12 @@ If setting up MemoryRouter for another agent:
773
746
  if (elapsed > 2000 && data.metrics)
774
747
  log(`memoryrouter: prepare metrics ${JSON.stringify(data.metrics)}`);
775
748
  let contextText = data.context;
776
- const nudge = await maybeDebtNudge(data.reflection);
749
+ const nudge = buildDebtNudgeLine(data.reflection);
777
750
  if (nudge)
778
751
  contextText = `${contextText}\n\n${nudge}`;
752
+ const importNudge = buildImportNudgeLine(data.import);
753
+ if (importNudge)
754
+ contextText = `${contextText}\n\n${importNudge}`;
779
755
  const wrapped = wrapForInjection(contextText);
780
756
  return { appendSystemContext: wrapped };
781
757
  }
@@ -882,9 +858,12 @@ If setting up MemoryRouter for another agent:
882
858
  if (elapsed > 2000 && data.metrics)
883
859
  log(`memoryrouter: prepare metrics ${JSON.stringify(data.metrics)}`);
884
860
  let contextText = data.context;
885
- const nudge = await maybeDebtNudge(data.reflection);
861
+ const nudge = buildDebtNudgeLine(data.reflection);
886
862
  if (nudge)
887
863
  contextText = `${contextText}\n\n${nudge}`;
864
+ const importNudge = buildImportNudgeLine(data.import);
865
+ if (importNudge)
866
+ contextText = `${contextText}\n\n${importNudge}`;
888
867
  const wrapped = wrapForInjection(contextText);
889
868
  return { appendSystemContext: wrapped };
890
869
  }
@@ -1597,6 +1576,44 @@ If setting up MemoryRouter for another agent:
1597
1576
  console.error(`Failed: ${err instanceof Error ? err.message : String(err)}`);
1598
1577
  }
1599
1578
  });
1579
+ mr.command("import")
1580
+ .description("Import historical OpenClaw conversations into your vault (agent-driven extraction + CLI transport)")
1581
+ .option("--instructions", "Fetch and print the server's extraction prompt for the host agent to execute")
1582
+ .option("--file <path>", "Upload a JSONL extract: preview, price quote, explicit approval, idempotent batches, resume")
1583
+ .option("--dismiss", "Permanently silence the import suggestion for this vault")
1584
+ .option("--yes", "Skip the interactive approval prompt (required for non-interactive --file runs)")
1585
+ .option("--key <key>", "Override memory key (for subagent vaults)")
1586
+ .action(async (opts) => {
1587
+ const effectiveKey = opts.key || process.env.MEMORYROUTER_API_KEY || memoryKey;
1588
+ if (!effectiveKey) {
1589
+ console.error("Not configured. Run: openclaw mr <key> or pass --key <key>");
1590
+ return;
1591
+ }
1592
+ const modes = [Boolean(opts.instructions), opts.file !== undefined, Boolean(opts.dismiss)].filter(Boolean).length;
1593
+ if (modes > 1) {
1594
+ console.error("Choose one of --instructions, --file <path>, or --dismiss");
1595
+ return;
1596
+ }
1597
+ try {
1598
+ const transport = await import("./import-transport.js");
1599
+ if (opts.dismiss) {
1600
+ await transport.runDismiss({ endpoint, key: effectiveKey });
1601
+ return;
1602
+ }
1603
+ if (opts.file) {
1604
+ await transport.runImportFile({ endpoint, key: effectiveKey, file: opts.file, yes: Boolean(opts.yes) });
1605
+ return;
1606
+ }
1607
+ await transport.runInstructions({ endpoint, key: effectiveKey });
1608
+ console.log("");
1609
+ console.log("Execute the prompt above with your agent, then upload the extract:");
1610
+ console.log(" openclaw mr import --file <path>");
1611
+ }
1612
+ catch (err) {
1613
+ console.error(`Import failed: ${err instanceof Error ? err.message : String(err)}`);
1614
+ process.exitCode = 1;
1615
+ }
1616
+ });
1600
1617
  mr.command("reflect")
1601
1618
  .description("Show reflection debt status (consolidation runs via the agent's memory_reflect tool)")
1602
1619
  .option("--key <key>", "Override memory key (for subagent vaults)")