whale-memory-core 0.11.1 → 0.11.2
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/index.d.ts +2 -1
- package/lib/self-experience.js +13 -16
- package/lib/unfinished-events.js +39 -18
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -4,4 +4,5 @@ export * as waking from './lib/waking.js'
|
|
|
4
4
|
export * as selfPrompt from './lib/self-prompt.js'
|
|
5
5
|
export * as selfSettings from './lib/self-settings.js'
|
|
6
6
|
export * as evidencePolicy from './lib/evidence-policy.js'
|
|
7
|
-
export * as evidenceFromExperience from './lib/evidence-from-experience.js'
|
|
7
|
+
export * as evidenceFromExperience from './lib/evidence-from-experience.js'
|
|
8
|
+
export * as unfinishedEvents from './lib/unfinished-events.js'
|
package/lib/self-experience.js
CHANGED
|
@@ -246,12 +246,10 @@ export function setUnfinishedState(dir, { id, choice, note = '' } = {}) {
|
|
|
246
246
|
const row = rowsById(dir, target.id)
|
|
247
247
|
if (!row || !row.then || !row.then.unfinished) return { ok: false, error: '这条经历没有「还没弄完」的内容,不需要结案。' }
|
|
248
248
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (
|
|
252
|
-
|
|
253
|
-
const event = unfinishedEvents.append(dir, { id: target.id, choice, note: tooLong('note', note, 200) })
|
|
254
|
-
return { ok: true, id: target.id, at: event.at, choice, noop: false }
|
|
249
|
+
// 重读、判断、追加都在同一把锁里:两个进程同时提交同一选择,只有一个会写
|
|
250
|
+
const decided = unfinishedEvents.decide(dir, { id: target.id, choice, note: tooLong('note', note, 200) })
|
|
251
|
+
if (decided.noop) return { ok: true, id: target.id, at: decided.row ? decided.row.at : '', choice, noop: true }
|
|
252
|
+
return { ok: true, id: target.id, at: decided.row.at, choice, noop: false }
|
|
255
253
|
} catch (e) {
|
|
256
254
|
return { ok: false, error: String(e && e.message ? e.message : e) }
|
|
257
255
|
}
|
|
@@ -367,20 +365,19 @@ export function renderTwoPart(entry, { now = new Date() } = {}) {
|
|
|
367
365
|
const shown = hits.length + 1
|
|
368
366
|
if (later.length > shown) lines.push('- (后来一共回头想过 ' + later.length + ' 次,这里没列出的 ' + (later.length - shown) + ' 次可以用 read 看)')
|
|
369
367
|
} else {
|
|
370
|
-
lines.push('-
|
|
368
|
+
lines.push('- 还没有文字后记')
|
|
371
369
|
}
|
|
372
370
|
const st = entry.unfinishedState
|
|
373
371
|
if (st && t.unfinished) {
|
|
374
372
|
const when2 = st.day || String(st.at || '').slice(0, 10)
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
373
|
+
const history = st.history || []
|
|
374
|
+
// 只看最近一次决定之前的那一次,不用 includes 猜
|
|
375
|
+
const prev = history.length >= 2 ? history[history.length - 2] : ''
|
|
376
|
+
let phrase = ''
|
|
377
|
+
if (st.choice === 'done') phrase = prev === 'shelved' ? '放下过,后来想通了' : '后来想通了'
|
|
378
|
+
else if (st.choice === 'shelved') phrase = prev === 'done' ? '想通过一次,后来又决定先放下' : '先放下了,还没想通'
|
|
379
|
+
else if (st.choice === 'reopen') phrase = prev === 'done' ? '想通过一次,后来又决定接着想' : '放下过,后来又决定接着想'
|
|
380
|
+
if (phrase) lines.push('- 当时没弄完的这件事:' + phrase + ':' + t.unfinished + '(' + when2 + ')')
|
|
384
381
|
}
|
|
385
382
|
lines.push('', '以上是那天留下的全部;没留下的,我不记得。')
|
|
386
383
|
return lines.join('\n')
|
package/lib/unfinished-events.js
CHANGED
|
@@ -11,34 +11,55 @@ export function eventsFile(memDir) {
|
|
|
11
11
|
return path.join(memDir, FILE)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
16
|
-
if (!CHOICES.has(choice)) throw new Error('choice 只能是 done / shelved / reopen')
|
|
14
|
+
function makeRow({ id, choice, note }) {
|
|
17
15
|
const now = new Date()
|
|
18
|
-
|
|
16
|
+
return {
|
|
19
17
|
id: String(id),
|
|
20
18
|
at: now.toISOString(),
|
|
21
19
|
day: now.toLocaleDateString('sv-SE'),
|
|
22
20
|
choice,
|
|
23
21
|
note: String(note || '').slice(0, 200)
|
|
24
22
|
}
|
|
23
|
+
}
|
|
25
24
|
|
|
25
|
+
// 调用前必须已经持有锁;append 和 decide 共用这一层,避免同一次选择被锁外读到两次
|
|
26
|
+
function appendUnlocked(memDir, row) {
|
|
27
|
+
const file = eventsFile(memDir)
|
|
28
|
+
let prefix = ''
|
|
29
|
+
try {
|
|
30
|
+
const st = fs.statSync(file)
|
|
31
|
+
if (st.size) {
|
|
32
|
+
const fd = fs.openSync(file, 'r')
|
|
33
|
+
const buf = Buffer.alloc(1)
|
|
34
|
+
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
35
|
+
fs.closeSync(fd)
|
|
36
|
+
if (buf[0] !== 10) prefix = '\n'
|
|
37
|
+
}
|
|
38
|
+
} catch { }
|
|
39
|
+
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
40
|
+
return row
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function append(memDir, { id, choice, note = '' } = {}, opts = {}) {
|
|
44
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
45
|
+
if (!CHOICES.has(choice)) throw new Error('choice 只能是 done / shelved / reopen')
|
|
46
|
+
const row = makeRow({ id, choice, note })
|
|
47
|
+
fs.mkdirSync(memDir, { recursive: true })
|
|
48
|
+
return withFileLock(eventsFile(memDir) + '.lock', () => appendUnlocked(memDir, row), opts)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 在同一个锁里「重读最近状态 → 判断 → 追加」:
|
|
52
|
+
// 两个进程同时提交同一选择,第二个会看到第一个的结果并返回 noop,不会写出第二条
|
|
53
|
+
export function decide(memDir, { id, choice, note = '' } = {}, opts = {}) {
|
|
54
|
+
if (!String(id || '').trim()) throw new Error('id 不能为空')
|
|
55
|
+
if (!CHOICES.has(choice)) throw new Error('choice 只能是 done / shelved / reopen')
|
|
26
56
|
fs.mkdirSync(memDir, { recursive: true })
|
|
27
57
|
return withFileLock(eventsFile(memDir) + '.lock', () => {
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const fd = fs.openSync(file, 'r')
|
|
34
|
-
const buf = Buffer.alloc(1)
|
|
35
|
-
fs.readSync(fd, buf, 0, 1, st.size - 1)
|
|
36
|
-
fs.closeSync(fd)
|
|
37
|
-
if (buf[0] !== 10) prefix = '\n'
|
|
38
|
-
}
|
|
39
|
-
} catch { }
|
|
40
|
-
fs.appendFileSync(file, prefix + JSON.stringify(row) + '\n', 'utf8')
|
|
41
|
-
return row
|
|
58
|
+
const rows = readAll(memDir).rows.filter((r) => r.id === String(id))
|
|
59
|
+
const last = rows[rows.length - 1] || null
|
|
60
|
+
if (last && last.choice === choice) return { row: last, noop: true }
|
|
61
|
+
if (choice === 'reopen' && !last) return { row: null, noop: true }
|
|
62
|
+
return { row: appendUnlocked(memDir, makeRow({ id, choice, note })), noop: false }
|
|
42
63
|
}, opts)
|
|
43
64
|
}
|
|
44
65
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.2",
|
|
4
4
|
"description": "Self-layer autobiographical memory core: then / later, append-only. Vault, evidence and consolidation live in the QQ Agent plugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|