dsh-plain-memory 1.0.1 → 1.1.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/LICENSE +21 -21
- package/README.en.md +76 -72
- package/README.md +155 -147
- package/cards.js +69 -69
- package/cordis.patch.yml +5 -5
- package/git.js +119 -119
- package/index.js +149 -20
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/memory.js +417 -396
- package/package.json +49 -48
- package/prompts/daily-consolidate.md +29 -28
- package/template/README.md +40 -39
- package/template/self.md +15 -15
package/index.js
CHANGED
|
@@ -17,7 +17,7 @@ import { join, resolve } from 'node:path'
|
|
|
17
17
|
import { fileURLToPath } from 'node:url'
|
|
18
18
|
import {
|
|
19
19
|
appendEntry, buildIndex, entryId, estimateTokens, findSecret, formatEntry, readRepo,
|
|
20
|
-
recallAll, slug, splitCues, summarize, today, tokenize,
|
|
20
|
+
recallAll, slug, splitCues, summarize, today, tokenize, MIN_MATCHED_TERMS,
|
|
21
21
|
} from './memory.js'
|
|
22
22
|
import { branchOf, commit, isDirty, isRepo, pull, pushBackground } from './git.js'
|
|
23
23
|
import { ensureState, readCards, writeCard } from './cards.js'
|
|
@@ -50,10 +50,11 @@ export function normalizeConfig(config = {}) {
|
|
|
50
50
|
maxRecall: Math.min(5, positive(config.maxRecall, 3)),
|
|
51
51
|
minScore: positive(config.minScore, 0.3),
|
|
52
52
|
cardScore: positive(config.cardScore, 0.3),
|
|
53
|
-
// 默认 1(一个线索即可);调成 2 会明显更安静,但实测会把跨领域召回从 5/5 压到 2/5
|
|
54
53
|
// 多窗口状态卡默认关闭(实验性):只写卡、不推送时,/state 里不会多出任何注入
|
|
55
54
|
cards: config.cards === true,
|
|
56
|
-
|
|
55
|
+
// 默认 2:至少两个独立线索才算想起来(与 memory.js 的 MIN_MATCHED_TERMS 一致)。
|
|
56
|
+
// 默认 1 等于「碰上一个词就注入」,生产日志里大部分噪声从这里来。
|
|
57
|
+
minCues: positive(config.minCues, MIN_MATCHED_TERMS),
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
|
|
@@ -90,17 +91,34 @@ function agentsFile() {
|
|
|
90
91
|
export function renderSelfBlock(repo) {
|
|
91
92
|
const entries = readRepo(repo).filter((e) => e.layer === 'self')
|
|
92
93
|
const lines = [SELF_BEGIN, '## 用户长期偏好(由 dsh-plain-memory 维护,按此执行)']
|
|
93
|
-
for (const e of entries)
|
|
94
|
-
const text = (e.body || e.title).replace(/\s+/g, ' ').trim()
|
|
95
|
-
const line = `- ${text}`
|
|
96
|
-
if (estimateTokens([...lines, line, SELF_END].join('\n')) > SELF_TOKEN_BUDGET) break
|
|
97
|
-
lines.push(line)
|
|
98
|
-
}
|
|
94
|
+
for (const e of entries) lines.push(selfLine(e))
|
|
99
95
|
if (lines.length === 2) return ''
|
|
100
96
|
lines.push(SELF_END)
|
|
101
97
|
return lines.join('\n')
|
|
102
98
|
}
|
|
103
99
|
|
|
100
|
+
function selfLine(entry) {
|
|
101
|
+
return `- ${(entry.body || entry.title).replace(/\s+/g, ' ').trim()}`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 自我层的预算只在「写入时」把关:显示时一条都不丢,写满了就拒绝新条目并让人合并规则。 */
|
|
105
|
+
export function selfTokens(repo, extra) {
|
|
106
|
+
const entries = readRepo(repo).filter((e) => e.layer === 'self')
|
|
107
|
+
const all = extra ? [...entries, extra] : entries
|
|
108
|
+
return estimateTokens(all.map(selfLine).join('\n'))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function assertSelfBudget(repo, entry) {
|
|
112
|
+
const tokens = selfTokens(repo, entry)
|
|
113
|
+
if (tokens <= SELF_TOKEN_BUDGET) return
|
|
114
|
+
const file = join(repo, 'self.md')
|
|
115
|
+
throw new Error(
|
|
116
|
+
`自我层已超预算(约 ${tokens}/${SELF_TOKEN_BUDGET} token),写入被拒。`
|
|
117
|
+
+ `规则文件:${file}。自我层每条都必须生效、不能在显示时截断,所以这里不接收新条目——`
|
|
118
|
+
+ '请用文件工具把相近的规则合并成一条后重试。',
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
|
|
104
122
|
/** 只改托管块,保留 AGENTS.md 里用户自己的内容。 */
|
|
105
123
|
export function syncSelfBlock(repo) {
|
|
106
124
|
const file = agentsFile()
|
|
@@ -134,6 +152,26 @@ export function removeSelfBlock() {
|
|
|
134
152
|
writeFileSync(file, next ? `${next}\n` : '', 'utf8')
|
|
135
153
|
}
|
|
136
154
|
|
|
155
|
+
/** 只有「明确截止日期」才自动填 expires;观察日期(某天实测)不算,否则会把有效记忆整条作废。 */
|
|
156
|
+
const DEADLINE_WORDS = /(截止|到期|deadline|最晚|交付|交稿|提交日|提交截止)/i
|
|
157
|
+
const DATE_RE = /\b(20\d{2})-(\d{2})-(\d{2})\b/
|
|
158
|
+
|
|
159
|
+
export function detectExpiry(text) {
|
|
160
|
+
const s = String(text || '')
|
|
161
|
+
if (!DATE_RE.test(s) || !DEADLINE_WORDS.test(s)) return ''
|
|
162
|
+
DATE_RE.lastIndex = 0
|
|
163
|
+
const m = DATE_RE.exec(s)
|
|
164
|
+
DATE_RE.lastIndex = 0
|
|
165
|
+
return m ? m[0] : ''
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function normalizeExpiry(value) {
|
|
169
|
+
const s = String(value || '').trim()
|
|
170
|
+
if (!s) return ''
|
|
171
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) throw new Error('expires 必须是 YYYY-MM-DD')
|
|
172
|
+
return s
|
|
173
|
+
}
|
|
174
|
+
|
|
137
175
|
function sessionKey(agent) {
|
|
138
176
|
return String(agent?.session?.id ?? '')
|
|
139
177
|
}
|
|
@@ -225,7 +263,7 @@ export function apply(ctx, config = {}) {
|
|
|
225
263
|
id, agent, cwd, line: '',
|
|
226
264
|
files: new Set(), objective: '', conclusion: '',
|
|
227
265
|
index: null, ready: false, pulled: false, isGit: false,
|
|
228
|
-
writes: 0, cardAt: 0, seenCards: new Set(), seenMem: new Set(), lastGoal: '',
|
|
266
|
+
writes: 0, cardAt: 0, seenCards: new Set(), seenMem: new Set(), pendingUsed: [], lastGoal: '',
|
|
229
267
|
preSteps: 0, injected: 0, warned: new Set(), flushing: false, pendingFlush: false,
|
|
230
268
|
}
|
|
231
269
|
sessions.set(id, st)
|
|
@@ -309,17 +347,45 @@ export function apply(ctx, config = {}) {
|
|
|
309
347
|
return makeUserMessage(text, name, 'recall')
|
|
310
348
|
}
|
|
311
349
|
|
|
350
|
+
/** 本会话历史里注入过的内容;宿主不提供事件流时返回 null(退回进程内的 seenMem)。 */
|
|
351
|
+
function recalledHistory(st) {
|
|
352
|
+
const session = st.agent?.session
|
|
353
|
+
if (!session || typeof session.eventAt !== 'function' || typeof session.seq !== 'number') return null
|
|
354
|
+
const parts = []
|
|
355
|
+
const from = Math.max(0, session.seq - 400)
|
|
356
|
+
for (let i = from; i < session.seq; i += 1) {
|
|
357
|
+
let event
|
|
358
|
+
try { event = session.eventAt(i) } catch { continue }
|
|
359
|
+
if (event?.type !== 'user/message') continue
|
|
360
|
+
const data = event.data || {}
|
|
361
|
+
const text = (data.content || []).map((b) => b?.text || '').join('\n')
|
|
362
|
+
// source.kind 在部分宿主会被归一,认内容标记更稳
|
|
363
|
+
if (data.source?.kind !== name && !text.includes(HEADER.trim().split('\n')[0])) continue
|
|
364
|
+
parts.push(text)
|
|
365
|
+
}
|
|
366
|
+
return parts.join('\n')
|
|
367
|
+
}
|
|
368
|
+
|
|
312
369
|
async function recallLines(st, text) {
|
|
370
|
+
// 自动注入不按句切开(切句会让三个字的一段碰到一个词就拿高分),概念桥与关联跳转只留给主动查询
|
|
371
|
+
const hits = recallAll(st.index, text, opts.maxRecall, opts.minScore, opts.minCues, { split: false, hop: false })
|
|
372
|
+
// 去重不存状态:直接看标题是否已在本会话历史里出现过,进程重启也不会重复注入
|
|
373
|
+
const history = recalledHistory(st)
|
|
374
|
+
const kept = []
|
|
375
|
+
const titles = new Set()
|
|
313
376
|
const lines = []
|
|
314
|
-
const hits = recallAll(st.index, text, opts.maxRecall, opts.minScore, opts.minCues)
|
|
315
|
-
.filter((h) => !st.seenMem.has(h.entry.id))
|
|
316
377
|
for (const hit of hits) {
|
|
317
378
|
const e = hit.entry
|
|
379
|
+
if (titles.has(e.title)) continue
|
|
380
|
+
const dup = history === null ? st.seenMem.has(e.id) : history.includes(e.title)
|
|
381
|
+
if (dup) continue
|
|
382
|
+
titles.add(e.title)
|
|
383
|
+
if (history === null) st.seenMem.add(e.id)
|
|
384
|
+
kept.push(hit)
|
|
318
385
|
const label = hit.hop ? '关联' : e.layer === 'self' ? '自我' : e.layer === 'episodic' ? '情景' : '语义'
|
|
319
386
|
lines.push(`[${label}] ${e.title}(${e.date || '无日期'},会话 ${shortId(e.session)}):${summarize(e, 140)}`)
|
|
320
|
-
st.seenMem.add(e.id)
|
|
321
387
|
}
|
|
322
|
-
return { lines, hits }
|
|
388
|
+
return { lines, hits: kept }
|
|
323
389
|
}
|
|
324
390
|
|
|
325
391
|
/** 会话里最后一条真人消息;判断「用户是不是亲口说了记住」。 */
|
|
@@ -340,24 +406,78 @@ export function apply(ctx, config = {}) {
|
|
|
340
406
|
return /记住|记下|记住这|以后都|以后不要|以后别|别再/.test(lastUserText(agent))
|
|
341
407
|
}
|
|
342
408
|
|
|
343
|
-
/**
|
|
409
|
+
/**
|
|
410
|
+
* 观察日志:每次注入记一行(`hits` 是命中的词,`relevant` 留空由人补)。
|
|
411
|
+
* 「有没有用上」由下一轮或回合结束另起一行补 `used`:模型回答里出现该条正文的 ≥2 个特征词才算用上,
|
|
412
|
+
* 证据不足就记 null,不猜。两行都只记聚合事实,不记正文内容。
|
|
413
|
+
*/
|
|
414
|
+
const FEATURE_MAX = 24
|
|
415
|
+
function featuresOf(entry) {
|
|
416
|
+
return [...new Set(tokenize(`${entry.title} ${entry.body}`))].filter((t) => !t.startsWith('+')).slice(0, FEATURE_MAX)
|
|
417
|
+
}
|
|
418
|
+
|
|
344
419
|
function logRecall(st, text, hits, cardCount) {
|
|
345
420
|
try {
|
|
421
|
+
const at = new Date().toISOString()
|
|
346
422
|
const line = JSON.stringify({
|
|
347
|
-
at
|
|
423
|
+
at,
|
|
348
424
|
session: shortId(st.id),
|
|
349
|
-
q: text.replace(/\s+/g, ' ').slice(0,
|
|
425
|
+
q: text.replace(/\s+/g, ' ').slice(0, 600),
|
|
350
426
|
ids: hits.map((h) => h.entry.id),
|
|
351
427
|
titles: hits.map((h) => h.entry.title),
|
|
352
428
|
scores: hits.map((h) => Number(h.score.toFixed(3))),
|
|
429
|
+
hits: hits.map((h) => h.terms || []),
|
|
353
430
|
hop: hits.filter((h) => h.hop).length,
|
|
354
431
|
cards: cardCount,
|
|
355
432
|
relevant: null,
|
|
356
433
|
})
|
|
357
434
|
appendFileSync(join(ensureState(opts.repoPath), 'recall.log'), `${line}\n`, 'utf8')
|
|
435
|
+
if (hits.length) {
|
|
436
|
+
st.pendingUsed.push({
|
|
437
|
+
at,
|
|
438
|
+
session: shortId(st.id),
|
|
439
|
+
ids: hits.map((h) => h.entry.id),
|
|
440
|
+
features: hits.map((h) => featuresOf(h.entry)),
|
|
441
|
+
fromSeq: Number(st.agent?.session?.seq ?? 0),
|
|
442
|
+
})
|
|
443
|
+
if (st.pendingUsed.length > 3) st.pendingUsed.shift()
|
|
444
|
+
}
|
|
358
445
|
} catch (error) { log('recall log failed', String(error)) }
|
|
359
446
|
}
|
|
360
447
|
|
|
448
|
+
/** 注入之后产生的模型回答文本;没有事件流就返回 null。 */
|
|
449
|
+
function assistantTextAfter(st, fromSeq) {
|
|
450
|
+
const session = st.agent?.session
|
|
451
|
+
if (!session || typeof session.eventAt !== 'function' || typeof session.seq !== 'number') return null
|
|
452
|
+
const parts = []
|
|
453
|
+
for (let i = Math.max(0, fromSeq); i < session.seq; i += 1) {
|
|
454
|
+
let event
|
|
455
|
+
try { event = session.eventAt(i) } catch { continue }
|
|
456
|
+
if (!String(event?.type || '').startsWith('assistant')) continue
|
|
457
|
+
for (const b of event.data?.content || []) if (b?.type === 'text') parts.push(b.text)
|
|
458
|
+
}
|
|
459
|
+
return parts.join('\n')
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function finalizeUsed(st) {
|
|
463
|
+
if (!st.pendingUsed.length) return
|
|
464
|
+
const pending = st.pendingUsed
|
|
465
|
+
st.pendingUsed = []
|
|
466
|
+
for (const p of pending) {
|
|
467
|
+
const text = assistantTextAfter(st, p.fromSeq)
|
|
468
|
+
const used = text === null || !text.trim()
|
|
469
|
+
? p.ids.map(() => null)
|
|
470
|
+
: p.features.map((f) => f.filter((t) => text.includes(t)).length >= 2)
|
|
471
|
+
try {
|
|
472
|
+
appendFileSync(
|
|
473
|
+
join(ensureState(opts.repoPath), 'recall.log'),
|
|
474
|
+
`${JSON.stringify({ at: p.at, session: p.session, ids: p.ids, used, via: text === null ? 'no-events' : 'events' })}\n`,
|
|
475
|
+
'utf8',
|
|
476
|
+
)
|
|
477
|
+
} catch { /* 日志失败不影响主流程 */ }
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
361
481
|
/**
|
|
362
482
|
* 状态卡按「会过期的记忆」匹配:用和记忆召回同一套线索匹配(用户消息 vs 卡的目标/结论/改动文件名),
|
|
363
483
|
* 过阈值才注入;同分支、同文件只是加分项。超过 24 小时没更新的卡直接失忆。
|
|
@@ -484,6 +604,7 @@ export function apply(ctx, config = {}) {
|
|
|
484
604
|
st.preSteps += 1
|
|
485
605
|
st.lastGoal = text.split('\n').find((l) => l.trim())?.trim().slice(0, 60) || ''
|
|
486
606
|
auditInjection(st)
|
|
607
|
+
finalizeUsed(st)
|
|
487
608
|
touchCard(st)
|
|
488
609
|
const recalled = await recallLines(st, text)
|
|
489
610
|
const cards = opts.cards ? cardLines(st, text) : []
|
|
@@ -518,6 +639,7 @@ export function apply(ctx, config = {}) {
|
|
|
518
639
|
const st = sessions.get(sessionKey(agent))
|
|
519
640
|
if (!st) { trace(`turn-stopping unknown session=${shortId(sessionKey(agent))}`); return }
|
|
520
641
|
trace(`turn-stopping session=${shortId(st.id)} writes=${st.writes} preSteps=${st.preSteps}`)
|
|
642
|
+
finalizeUsed(st)
|
|
521
643
|
if (st.preSteps === 0) {
|
|
522
644
|
warnOnce(st, 'hook-not-fired', '会话有回合结束,但 agent/pre-step 一次都没触发;记忆注入未生效')
|
|
523
645
|
}
|
|
@@ -561,6 +683,7 @@ export function apply(ctx, config = {}) {
|
|
|
561
683
|
body: { type: 'string', description: '记忆正文,不超过 600 字' },
|
|
562
684
|
cues: { type: 'string', description: '线索词,逗号分隔,最多 12 个' },
|
|
563
685
|
related: { type: 'string', description: '关联条目 id,逗号分隔' },
|
|
686
|
+
expires: { type: 'string', description: '可选,YYYY-MM-DD。这条记忆到期后不再被自动召回;只有带截止日期的事才填' },
|
|
564
687
|
},
|
|
565
688
|
},
|
|
566
689
|
output: textOutput,
|
|
@@ -579,12 +702,15 @@ export function apply(ctx, config = {}) {
|
|
|
579
702
|
const layer = args.layer
|
|
580
703
|
const date = today()
|
|
581
704
|
const title = args.title.trim()
|
|
705
|
+
const expires = normalizeExpiry(args.expires) || detectExpiry(`${title}\n${body}`)
|
|
582
706
|
const entry = {
|
|
583
707
|
id: entryId(layer, title, date), layer, title, date, session: st.id,
|
|
584
708
|
cues: splitCues(args.cues || '').slice(0, 12),
|
|
585
709
|
related: splitCues(args.related || ''),
|
|
586
710
|
body,
|
|
711
|
+
expires,
|
|
587
712
|
}
|
|
713
|
+
if (layer === 'self') assertSelfBudget(opts.repoPath, entry)
|
|
588
714
|
const rel = appendEntry(opts.repoPath, entry)
|
|
589
715
|
st.index = buildIndex(readRepo(opts.repoPath).filter((e) => e.layer !== 'self'))
|
|
590
716
|
st.writes += 1
|
|
@@ -614,10 +740,12 @@ export function apply(ctx, config = {}) {
|
|
|
614
740
|
const file = join(dir, 'pending', `${String(args?.id || '').replace(/[^\w.-]/g, '')}.json`)
|
|
615
741
|
if (!existsSync(file)) throw new Error(`找不到提案 ${args?.id}`)
|
|
616
742
|
const p = JSON.parse(readFileSync(file, 'utf8'))
|
|
617
|
-
|
|
743
|
+
const entry = {
|
|
618
744
|
id: entryId('self', p.title, p.date), layer: 'self', title: p.title, date: p.date,
|
|
619
745
|
session: p.session, cues: p.cues, related: [], body: p.body,
|
|
620
|
-
}
|
|
746
|
+
}
|
|
747
|
+
assertSelfBudget(opts.repoPath, entry)
|
|
748
|
+
appendEntry(opts.repoPath, entry)
|
|
621
749
|
rmSync(file, { force: true })
|
|
622
750
|
st.index = buildIndex(readRepo(opts.repoPath).filter((e) => e.layer !== 'self'))
|
|
623
751
|
st.writes += 1
|
|
@@ -665,7 +793,7 @@ export function apply(ctx, config = {}) {
|
|
|
665
793
|
async execute(args, exec) {
|
|
666
794
|
if (typeof args?.query !== 'string' || !args.query.trim()) throw new Error('query 不能为空')
|
|
667
795
|
const st = await ensureSession(exec.agent)
|
|
668
|
-
const hits = recallAll(st.index, args.query, 5, 0.2, 1)
|
|
796
|
+
const hits = recallAll(st.index, args.query, 5, 0.2, 1, { split: true, hop: true })
|
|
669
797
|
if (hits.length === 0) return '记忆仓里没有匹配的条目。'
|
|
670
798
|
return hits.map((h) => `[${h.entry.layer}] ${h.entry.title}(${h.entry.date || '无日期'})\n${h.entry.body}`).join('\n\n')
|
|
671
799
|
},
|
|
@@ -684,3 +812,4 @@ export function apply(ctx, config = {}) {
|
|
|
684
812
|
}
|
|
685
813
|
|
|
686
814
|
export { formatEntry, slug, splitCues }
|
|
815
|
+
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
{
|
|
2
|
-
"meta": {
|
|
3
|
-
"title": "Plain Memory",
|
|
4
|
-
"description": "Markdown memory in your own Git repo, with per-window state cards and local cue-based recall."
|
|
5
|
-
}
|
|
6
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Plain Memory",
|
|
4
|
+
"description": "Markdown memory in your own Git repo, with per-window state cards and local cue-based recall."
|
|
5
|
+
}
|
|
6
|
+
}
|
package/locale/zh.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
{
|
|
2
|
-
"meta": {
|
|
3
|
-
"title": "原味记忆",
|
|
4
|
-
"description": "记忆是纯 Markdown,存在你自己的 Git 仓库;多窗口状态卡;线索词本地召回。"
|
|
5
|
-
}
|
|
6
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "原味记忆",
|
|
4
|
+
"description": "记忆是纯 Markdown,存在你自己的 Git 仓库;多窗口状态卡;线索词本地召回。"
|
|
5
|
+
}
|
|
6
|
+
}
|