nebula-memory-mcp 1.1.0 → 1.2.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.
- package/mcp-server.mjs +36 -1
- package/memory/store.cjs +66 -0
- package/package.json +1 -1
package/mcp-server.mjs
CHANGED
|
@@ -42,7 +42,7 @@ const DATA_DIR = path.resolve(
|
|
|
42
42
|
|
|
43
43
|
const SERVER_INFO = {
|
|
44
44
|
name: 'agent-memory',
|
|
45
|
-
version: '1.
|
|
45
|
+
version: '1.2.0',
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// MCP 协议版本(当前规范)
|
|
@@ -151,6 +151,14 @@ const TOOLS = [
|
|
|
151
151
|
required: ['id'],
|
|
152
152
|
},
|
|
153
153
|
},
|
|
154
|
+
{
|
|
155
|
+
name: 'compact',
|
|
156
|
+
description: '手动压缩记忆库文件:清理追加历史,磁盘显著变小、下次启动明显变快。实例保持可用,不影响读写。可选 namespace。',
|
|
157
|
+
inputSchema: {
|
|
158
|
+
type: 'object',
|
|
159
|
+
properties: { namespace: { type: 'string', description: '目标记忆库(可选)。不填=默认单库。' } },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
154
162
|
]
|
|
155
163
|
|
|
156
164
|
// ── 工具执行 ─────────────────────────────────────────────────────────────
|
|
@@ -186,6 +194,16 @@ async function callTool(name, args, defaultMem) {
|
|
|
186
194
|
const r = await mem.forget(String(args.id || ''))
|
|
187
195
|
return { content: [{ type: 'text', text: JSON.stringify(r) }] }
|
|
188
196
|
}
|
|
197
|
+
case 'compact': {
|
|
198
|
+
const r = await mem.compact()
|
|
199
|
+
const summary = {
|
|
200
|
+
ok: r.ok,
|
|
201
|
+
durationMs: r.durationMs,
|
|
202
|
+
nodeLines: `${r.before.nodeLines} -> ${r.after.nodeLines}`,
|
|
203
|
+
nodesFileMB: `${r.before.nodesMB} -> ${r.after.nodesMB}`,
|
|
204
|
+
}
|
|
205
|
+
return { content: [{ type: 'text', text: JSON.stringify(summary) }] }
|
|
206
|
+
}
|
|
189
207
|
default:
|
|
190
208
|
throw new Error(`unknown tool: ${name}`)
|
|
191
209
|
}
|
|
@@ -193,6 +211,23 @@ async function callTool(name, args, defaultMem) {
|
|
|
193
211
|
|
|
194
212
|
// ── 主流程 ───────────────────────────────────────────────────────────────
|
|
195
213
|
async function main() {
|
|
214
|
+
// CLI 子命令:node mcp-server.mjs compact [数据目录]
|
|
215
|
+
// ⚠️ 必须在插件服务停止后使用(压缩重写 JSONL,与服务双写会冲突)。
|
|
216
|
+
if (process.argv[2] === 'compact') {
|
|
217
|
+
const dir = path.resolve(
|
|
218
|
+
process.argv[3] ||
|
|
219
|
+
process.env.NEBULA_DATA_DIR ||
|
|
220
|
+
process.env.MEMORY_NEBULA_DATA_DIR ||
|
|
221
|
+
path.join(os.homedir(), '.agent-memory')
|
|
222
|
+
)
|
|
223
|
+
const mem = new NebulaMemory({ dir, log: () => {} })
|
|
224
|
+
await mem.init()
|
|
225
|
+
const r = await mem.compact()
|
|
226
|
+
await mem.close()
|
|
227
|
+
console.log(JSON.stringify(r, null, 2))
|
|
228
|
+
return
|
|
229
|
+
}
|
|
230
|
+
|
|
196
231
|
// 默认单库实例(不传 namespace 时用),保持与旧版完全一致的行为。
|
|
197
232
|
const defaultMem = new NebulaMemory({ dir: DATA_DIR, log: () => {} })
|
|
198
233
|
await defaultMem.init()
|
package/memory/store.cjs
CHANGED
|
@@ -87,6 +87,8 @@ const DEFAULTS = {
|
|
|
87
87
|
observeConsolidateEvery: 20, // observe() 每 N 轮触发一次巩固
|
|
88
88
|
observeConsolidateDays: 1, // 或距上次巩固超过 A 天
|
|
89
89
|
tickConsolidateDays: 1, // tick() 里距上次巩固超过 A 天才真正巩固
|
|
90
|
+
autoCompactRatio: 2.5, // 追加行数 > 存活×该比例 → 自动调度压缩(0 = 关闭自动压缩)
|
|
91
|
+
autoCompactDelayMs: 5000, // 跨过阈值后延迟这么多毫秒再压(防抖:写突发结束才动手)
|
|
90
92
|
};
|
|
91
93
|
|
|
92
94
|
/** 打分权重(1 = 原位;调大可放大该因子对排序的影响) */
|
|
@@ -446,6 +448,7 @@ class NebulaMemory {
|
|
|
446
448
|
}
|
|
447
449
|
this._ready = true;
|
|
448
450
|
if (this._indexDirty) this._writeIndexSnapshot();
|
|
451
|
+
this._scheduleAutoCompact(); // 重启加载后若追加历史已膨胀,排一次自动压缩
|
|
449
452
|
this._log(
|
|
450
453
|
'info',
|
|
451
454
|
`ready dir=${this.dir} nodes=${this.nodes.size} edges=${this.edgeMap.size} badLines=${this._badLines}`
|
|
@@ -467,6 +470,7 @@ class NebulaMemory {
|
|
|
467
470
|
/** 关闭:落 pending 更新 → 必要时压缩 → 落索引快照。可重复调用。 */
|
|
468
471
|
async close() {
|
|
469
472
|
if (!this._ready) return;
|
|
473
|
+
if (this._autoCompactTimer) { clearTimeout(this._autoCompactTimer); this._autoCompactTimer = null; }
|
|
470
474
|
this._flushPending();
|
|
471
475
|
this._maybeCompact();
|
|
472
476
|
if (this._traceLines > num(this.opts.traceMax, 200)) this._compactTrace();
|
|
@@ -476,6 +480,67 @@ class NebulaMemory {
|
|
|
476
480
|
this._closed = true;
|
|
477
481
|
}
|
|
478
482
|
|
|
483
|
+
/**
|
|
484
|
+
* 手动压缩(实例保持可用):落 pending → 强制重写 nodes/edges jsonl → 落索引快照。
|
|
485
|
+
* 只序列化内存里的存活集,规模再大也是亚秒级;收益是磁盘文件和下一次冷启动。
|
|
486
|
+
* @returns {Promise<{ok:boolean, durationMs:number, before:Object, after:Object, dir:string}>}
|
|
487
|
+
*/
|
|
488
|
+
async compact() {
|
|
489
|
+
this._assertReady();
|
|
490
|
+
const t = Date.now();
|
|
491
|
+
const sizeOf = (f) => { try { return fs.statSync(f).size; } catch { return 0; } };
|
|
492
|
+
const before = {
|
|
493
|
+
nodeLines: this._nodeLines,
|
|
494
|
+
edgeLines: this._edgeLines,
|
|
495
|
+
nodesMB: +(sizeOf(this.fileNodes) / 1048576).toFixed(1),
|
|
496
|
+
edgesMB: +(sizeOf(this.fileEdges) / 1048576).toFixed(1),
|
|
497
|
+
alive: { nodes: this.nodes.size, edges: this.edgeMap.size },
|
|
498
|
+
};
|
|
499
|
+
this._flushPending();
|
|
500
|
+
this._compactNodesNow = true;
|
|
501
|
+
this._maybeCompact();
|
|
502
|
+
this._compactNodesNow = false;
|
|
503
|
+
if (this._traceLines > num(this.opts.traceMax, 200)) this._compactTrace();
|
|
504
|
+
this._writeIndexSnapshot();
|
|
505
|
+
const after = {
|
|
506
|
+
nodeLines: this._nodeLines,
|
|
507
|
+
edgeLines: this._edgeLines,
|
|
508
|
+
nodesMB: +(sizeOf(this.fileNodes) / 1048576).toFixed(1),
|
|
509
|
+
edgesMB: +(sizeOf(this.fileEdges) / 1048576).toFixed(1),
|
|
510
|
+
alive: { nodes: this.nodes.size, edges: this.edgeMap.size },
|
|
511
|
+
};
|
|
512
|
+
const out = { ok: true, durationMs: Date.now() - t, before, after, dir: this.dir };
|
|
513
|
+
this._log('info', `compact: nodeLines ${before.nodeLines} -> ${after.nodeLines}, ${before.nodesMB}MB -> ${after.nodesMB}MB in ${out.durationMs}ms`);
|
|
514
|
+
return out;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* 自动压缩调度:追加行数超过 存活×autoCompactRatio 时,延迟 autoCompactDelayMs 执行。
|
|
519
|
+
* 压缩只重写存活集(亚秒级),不阻塞当前请求;阈值防抖保证一次写突发只压一次。
|
|
520
|
+
*/
|
|
521
|
+
_scheduleAutoCompact() {
|
|
522
|
+
if (this._autoCompactTimer || this._closed) return;
|
|
523
|
+
const ratio = num(this.opts.autoCompactRatio, 2.5);
|
|
524
|
+
if (ratio <= 0) return; // 0 = 关闭
|
|
525
|
+
if (this._nodeLines <= Math.max(200, this.nodes.size * ratio)) return;
|
|
526
|
+
this._autoCompactTimer = setTimeout(() => {
|
|
527
|
+
this._autoCompactTimer = null;
|
|
528
|
+
if (this._closed || !this._ready) return;
|
|
529
|
+
try {
|
|
530
|
+
this._flushPending();
|
|
531
|
+
this._compactNodesNow = true;
|
|
532
|
+
this._maybeCompact();
|
|
533
|
+
this._compactNodesNow = false;
|
|
534
|
+
if (this._traceLines > num(this.opts.traceMax, 200)) this._compactTrace();
|
|
535
|
+
this._writeIndexSnapshot();
|
|
536
|
+
this._log('info', `auto-compact done: nodeLines=${this._nodeLines} nodes=${this.nodes.size}`);
|
|
537
|
+
} catch (e) {
|
|
538
|
+
this._log('warn', `auto-compact failed: ${e.message}`);
|
|
539
|
+
}
|
|
540
|
+
}, Math.max(0, num(this.opts.autoCompactDelayMs, 5000)));
|
|
541
|
+
this._autoCompactTimer.unref?.();
|
|
542
|
+
}
|
|
543
|
+
|
|
479
544
|
// -- 写入 -----------------------------------------------------------------
|
|
480
545
|
|
|
481
546
|
/**
|
|
@@ -1530,6 +1595,7 @@ class NebulaMemory {
|
|
|
1530
1595
|
this._raw.set(n.id, rec);
|
|
1531
1596
|
this._nodeLines++;
|
|
1532
1597
|
this._writesSinceIndex++;
|
|
1598
|
+
this._scheduleAutoCompact();
|
|
1533
1599
|
}
|
|
1534
1600
|
|
|
1535
1601
|
_addEdge(a, b, weight, meta = {}) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nebula-memory-mcp",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"mcpName": "io.github.yb-zhai/nebula-memory",
|
|
5
5
|
"description": "Zero-dependency local Agent memory MCP Server (stdio JSON-RPC). Works offline, no API keys, no database. Exposes remember/recall/stats/forget to Claude Desktop, Cursor, ChatGPT and any MCP client.",
|
|
6
6
|
"type": "module",
|