dsh-memory-graph-chiyo:帶錨點、會遺忘、夜間整理的記憶圖插件

前言

給 AI agent 做長期記憶時,常見方案要麼只靠向量相似檢索、丟掉「誰說的、原話是什麼」,要麼把對話抽成結構化事實卻無法再對回原句。DeepSeek Harness(DSH)支持以插件擴展能力;若你希望記憶既能召回、又能回溯原文,並且會隨時間衰減而不是隻增不減,可以看下維護者 fushui-menghua 在社區目錄「記憶」分類下的 dsh-memory-graph-chiyo。它把記憶做成圖:強制錨點、四層衰減、夜間 consolidation,數據落在本地 JSONL,運行時僅依賴 Node 內置模塊。

這是什麼

dsh-memory-graph-chiyo(npm 包名同名,當前版本 1.0.1,許可證 MIT)定位爲「可回溯、會遺忘、會睡覺整理的記憶內核」,面向 bridge / DSH 人格類 agent 的長期記憶層。README 將其概括爲抽取式檢索與輕量時序圖譜路線的混合,並強調三件事:錨點強制(fact / plan 必須能指回原話)、四層衰減(instant → short → long → cold)、夜間整理(歸檔重複與噪聲、強化規則與承諾類記憶)。

與常見記憶路線的差異(簡述)

README 用表格對比了向量 RAG、Mem0、Zep/Graphiti、Letta、Cognee 等路徑的短板;本項目在結構化事實與關係邊上做召回,同時要求事實類節點帶 anchors,並內置計劃過期、共享記憶審批、敏感信息寫入前過濾、錨點與原文一致性校驗等機制。具體能力以倉庫 README 與測試(README 標註 67 passing)爲準。

核心機制

錨點、節點類型與 scope

記憶按 scope 隔離,例如 user:alice、agent:chiyo、group:demo。節點 kind 包括 fact(須有錨點)、impression(可無錨點)、plan(帶失效期)。layer 爲 instant / short / long / cold / permanent 等;寫入時相同規範化文本會合並並增加 mentionCount。

衰減與夜間整理

短期層在滿足提及次數與時間條件後可升入長期層;長期不用會沉入冷層。nightly 會推進層與即時記憶、處理 plan 狀態,並做重複/墓碑/極小陳舊項歸檔,以及對規則承諾類內容的強化。可用 cron 配合 CLI 每天執行,例如:

0 3 * * * cd /path/to/app && npx memory-graph nightly --dir ./data --scopes user:alice --apply

召回與關係邊

召回綜合字符 bigram、關鍵詞、重要度與新鮮度,並過濾冷層、歸檔與過期 plan。支持 跨 scope 召回(結果帶 scope)。關係類型含 person、relationship、because、derivedFrom、supports、contradicts 等,可按 hops 沿邊擴展一跳。

共享記憶與合規

共享流程爲 propose → approve → 跨會話可見 → revoke;未批准不可召回。另有路徑、QQ 號、憑據、令牌等敏感內容的寫入前拒收(可逐類放行)。

作爲 DSH 插件安裝與工具

面向 DSH 用戶,官方安裝命令爲:

dsh plugin --profile web add dsh-memory-graph-chiyo

裝上後 DSH 會註冊 5 個工具:memory_write、memory_recall、memory_health、memory_nightly、memory_shared。數據默認目錄爲 ~/.dsh/memory-graph,默認 scope 爲 agent:memory(可在插件配置中修改)。插件入口爲包內 ./plugin.js 的 apply(ctx),與庫共用同一 npm 包。

環境要求: Node ≥ 20;package.json 中 engines.dsh 爲 >=0.1.7-rc.1。

典型用法:庫 API

開發者也可直接安裝庫:

npm i dsh-memory-graph-chiyo

下面步驟演示創建存儲、寫入帶錨點的事實、建關係、召回與夜間整理(摘自 README 快速開始)。

1、創建記憶實例並指定數據目錄:

import { createMemory } from 'dsh-memory-graph-chiyo';

const mem = createMemory({ dir: './memory-data' });

2、寫入 fact(必須帶 anchors):

mem.write('user:alice', {
  nodes: [
    { kind: 'fact', text: '她喜歡下雨天', anchors: [{ quote: '我最喜歡下雨了' }] },
    { kind: 'fact', text: '她怕黑', anchors: [{ quote: '我怕黑' }] },
  ],
});

3、在已有節點上建立 because 關係並召回(hops: 1):

const fear = mem.load('user:alice').find((n) => /怕黑/.test(n.text));
mem.write('user:alice', {
  nodes: [{
    kind: 'fact',
    text: '睡覺要留一盞小燈',
    anchors: [{ quote: '留個燈' }],
    relations: [{ type: 'because', targetId: fear.id }],
  }],
});

const hits = mem.recall('user:alice', { query: '怕黑', hops: 1 });

4、執行夜間整理(apply: true 會寫回狀態):

const report = mem.nightly(['user:alice', 'agent:chiyo'], { apply: true });

存儲佈局爲按 scope 與月份分文件的 JSONL,例如 memory-data/user_alice/2026-11.jsonl;同 id 以最後一條爲準(last-wins)。

橋接層(QQ / 微信人格機器人)

若使用橋接 + DSH 人格機器人,README 提供 createBridgeMemory 適配:每輪前拼裝上下文、每輪後自動抽取記憶,並可調用 nightly。詳見倉庫 docs/BRIDGE.md。

import { createBridgeMemory } from 'dsh-memory-graph-chiyo/bridge';
const bm = createBridgeMemory({ dir: './memory', scope: 'agent:chiyo' });

bm.beforeTurn({ query: 用戶這句話 });
bm.afterTurn({ messages: [用戶, 回覆] });
bm.nightly({ apply: true });

同一橋接對象還可使用情緒、做夢與待辦等擴展 API(bm.mood、bm.dream、bm.tasks)。

命令行體檢與召回

包提供 memory-graph 可執行文件,例如:

npx memory-graph health --dir ./data --scope user:alice
npx memory-graph recall --dir ./data --scope user:alice --query 下雨
npx memory-graph recall-all --dir ./data --scopes user:alice,agent:chiyo --query 下雨
npx memory-graph nightly --dir ./data --scopes user:alice --apply

適用場景與注意

適合: 需要長期對話記憶、又要求 fact/plan 可回溯原話;希望自動遺忘與定時整理;打算接入 DSH 工具面或 Node 服務內嵌記憶層的開發者。

注意: DSH 插件在 當前 dsh 進程權限 下運行,會讀寫本機 ~/.dsh/memory-graph(或你配置的路徑)。安裝前請閱讀 GitHub 源碼與 MIT 許可證,確認敏感過濾策略是否滿足你的合規要求。社區目錄 SkillHub 爲獨立站點,與 DeepSeek / 幻方無官方從屬關係。

小結

dsh-memory-graph-chiyo 把「記得住、忘得掉、每晚自己整理」落到錨點圖、JSONL 與 DSH 五件套工具上;庫與插件同一包,可按項目選 npm 或 dsh plugin --profile web add。更多能力(上下文壓縮帶錨點、星圖可視化、extractAndWrite、assembleContext 等)見 README。

羽毛球分组比赛记分
小程序二维码

欢迎使用《羽毛球分组比赛记分》微信小程序

小夜