whale-memory-core 0.6.1 → 0.8.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/index.d.ts +7 -0
- package/index.js +3 -1
- package/lib/evidence-from-experience.d.ts +7 -0
- package/lib/evidence-from-experience.js +41 -0
- package/lib/evidence-policy.d.ts +34 -0
- package/lib/evidence-policy.js +113 -0
- package/lib/file-lock.d.ts +1 -0
- package/lib/file-lock.js +7 -2
- package/lib/later-events.d.ts +19 -0
- package/lib/recall-events.d.ts +24 -0
- package/lib/recall-events.js +10 -0
- package/lib/self-experience.d.ts +76 -0
- package/lib/self-experience.js +6 -4
- package/lib/self-memory.d.ts +18 -0
- package/lib/self-prompt.d.ts +40 -0
- package/lib/self-prompt.js +2 -27
- package/lib/self-settings.d.ts +12 -0
- package/lib/waking.d.ts +3 -0
- package/package.json +15 -11
package/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * as selfExperience from './lib/self-experience.js'
|
|
2
|
+
export * as selfMemory from './lib/self-memory.js'
|
|
3
|
+
export * as waking from './lib/waking.js'
|
|
4
|
+
export * as selfPrompt from './lib/self-prompt.js'
|
|
5
|
+
export * as selfSettings from './lib/self-settings.js'
|
|
6
|
+
export * as evidencePolicy from './lib/evidence-policy.js'
|
|
7
|
+
export * as evidenceFromExperience from './lib/evidence-from-experience.js'
|
package/index.js
CHANGED
|
@@ -4,4 +4,6 @@ export * as selfExperience from './lib/self-experience.js'
|
|
|
4
4
|
export * as selfMemory from './lib/self-memory.js'
|
|
5
5
|
export * as waking from './lib/waking.js'
|
|
6
6
|
export * as selfPrompt from './lib/self-prompt.js'
|
|
7
|
-
export * as selfSettings from './lib/self-settings.js'
|
|
7
|
+
export * as selfSettings from './lib/self-settings.js'
|
|
8
|
+
export * as evidencePolicy from './lib/evidence-policy.js'
|
|
9
|
+
export * as evidenceFromExperience from './lib/evidence-from-experience.js'
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Fact, Verdict } from './evidence-policy.js'
|
|
2
|
+
import type { Experience } from './self-experience.js'
|
|
3
|
+
|
|
4
|
+
export function moodFacts(experiences: Experience[], opts?: { subject?: string }): Fact[]
|
|
5
|
+
export function quoteFacts(experiences: Experience[], opts?: { subject?: string }): Fact[]
|
|
6
|
+
export function factsFromExperiences(experiences: Experience[], opts?: { subject?: string }): Fact[]
|
|
7
|
+
export function ask(experiences: Experience[], opts: { slot: string; at?: string; subject?: string }): Verdict
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// 把「她的经历」桥到证据层:只抽有原文锚的东西,不推断
|
|
2
|
+
// 情绪快照 → mood fact;原文锚 → quote:<source> fact
|
|
3
|
+
import { query } from './evidence-policy.js'
|
|
4
|
+
|
|
5
|
+
const moodOf = (r) => {
|
|
6
|
+
const state = r.then && r.then.state
|
|
7
|
+
if (!state || typeof state !== 'object') return ''
|
|
8
|
+
return String(state.mood || '')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function moodFacts(experiences, { subject = 'self' } = {}) {
|
|
12
|
+
const facts = []
|
|
13
|
+
for (const r of experiences || []) {
|
|
14
|
+
const mood = moodOf(r)
|
|
15
|
+
if (!mood) continue
|
|
16
|
+
const at = (r.then.state && r.then.state.at) || r.at
|
|
17
|
+
if (!at) continue
|
|
18
|
+
facts.push({ id: 'mood:' + r.id, subject, slot: 'mood', value: mood, at, source: 'then.state' })
|
|
19
|
+
}
|
|
20
|
+
return facts
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function quoteFacts(experiences, { subject = 'self' } = {}) {
|
|
24
|
+
const facts = []
|
|
25
|
+
for (const r of experiences || []) {
|
|
26
|
+
const q = r.then && r.then.quote
|
|
27
|
+
if (!q || !q.text) continue
|
|
28
|
+
const slot = 'quote:' + (q.source || 'unknown')
|
|
29
|
+
facts.push({ id: 'quote:' + r.id, subject, slot, value: q.text, at: r.at, source: q.source || 'then.quote' })
|
|
30
|
+
}
|
|
31
|
+
return facts
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function factsFromExperiences(experiences, opts = {}) {
|
|
35
|
+
return [...moodFacts(experiences, opts), ...quoteFacts(experiences, opts)]
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 问她某个时点是什么心情/读到过什么:没有就返回 unknown,不猜
|
|
39
|
+
export function ask(experiences, { slot, at, subject = 'self' } = {}) {
|
|
40
|
+
return query(factsFromExperiences(experiences, { subject }), { subject, slot, at })
|
|
41
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface Fact {
|
|
2
|
+
id: string
|
|
3
|
+
subject: string
|
|
4
|
+
slot?: string
|
|
5
|
+
value?: unknown
|
|
6
|
+
at: string
|
|
7
|
+
source?: string
|
|
8
|
+
supersedes?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface NormalizedFact extends Required<Omit<Fact, 'slot' | 'value' | 'source' | 'supersedes'>> {
|
|
12
|
+
slot: string
|
|
13
|
+
value: unknown
|
|
14
|
+
source: string
|
|
15
|
+
supersedes: string
|
|
16
|
+
t: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type Verdict =
|
|
20
|
+
| { status: 'known'; value: unknown; fact: NormalizedFact; evidence: NormalizedFact[]; reason: string }
|
|
21
|
+
| { status: 'unknown'; reason: string; evidence: NormalizedFact[] }
|
|
22
|
+
|
|
23
|
+
export function normalizeFacts(facts?: Fact[]): NormalizedFact[]
|
|
24
|
+
export function asOf(facts: Fact[], at: string): NormalizedFact[]
|
|
25
|
+
export function supersessionMap(facts?: Fact[]): Map<string, NormalizedFact>
|
|
26
|
+
export function currentFact(facts: Fact[], opts?: { subject: string; slot?: string; at?: string }): NormalizedFact | null
|
|
27
|
+
export function query(facts: Fact[], opts: { subject: string; slot?: string; at?: string }): Verdict
|
|
28
|
+
export function hasSuccessor(facts: Fact[], id: string, at?: string): {
|
|
29
|
+
ok: boolean
|
|
30
|
+
reason?: string
|
|
31
|
+
hasSuccessor?: boolean
|
|
32
|
+
successors?: NormalizedFact[]
|
|
33
|
+
}
|
|
34
|
+
export function render(verdict: Verdict | null): string
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// 证据策略(开源子集):只做判定,不做存储
|
|
2
|
+
// 一条 fact:{ id, subject, slot, value, at, source?, supersedes? }
|
|
3
|
+
// 规则:as-of、被取代、后继缺席;没有记录就返回「不知道」,不推断
|
|
4
|
+
|
|
5
|
+
const ts = (v) => {
|
|
6
|
+
const n = Date.parse(String(v || ''))
|
|
7
|
+
return Number.isFinite(n) ? n : null
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function normalize(f) {
|
|
11
|
+
if (!f || !f.id || !f.subject) return null
|
|
12
|
+
const t = ts(f.at)
|
|
13
|
+
if (t === null) return null
|
|
14
|
+
return {
|
|
15
|
+
id: String(f.id),
|
|
16
|
+
subject: String(f.subject),
|
|
17
|
+
slot: String(f.slot || 'default'),
|
|
18
|
+
value: f.value === undefined ? null : f.value,
|
|
19
|
+
at: new Date(t).toISOString(),
|
|
20
|
+
t,
|
|
21
|
+
source: f.source ? String(f.source) : '',
|
|
22
|
+
supersedes: f.supersedes ? String(f.supersedes) : ''
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function normalizeFacts(facts = []) {
|
|
27
|
+
return facts.map(normalize).filter(Boolean).sort((a, b) => a.t - b.t || a.id.localeCompare(b.id))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 某个时间点之前成立的全部 fact(含被后来者取代的旧值,方便看历史)
|
|
31
|
+
export function asOf(facts, at) {
|
|
32
|
+
const limit = ts(at)
|
|
33
|
+
if (limit === null) return []
|
|
34
|
+
return normalizeFacts(facts).filter((f) => f.t <= limit)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// id → 取代它的新 fact(只看给定的集合)
|
|
38
|
+
export function supersessionMap(facts = []) {
|
|
39
|
+
const map = new Map()
|
|
40
|
+
for (const f of normalizeFacts(facts)) {
|
|
41
|
+
if (f.supersedes) map.set(f.supersedes, f)
|
|
42
|
+
}
|
|
43
|
+
return map
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 截至 at 仍然有效的那一条:没被更晚的 fact 取代
|
|
47
|
+
export function currentFact(facts, { subject, slot = 'default', at = new Date().toISOString() } = {}) {
|
|
48
|
+
const pool = asOf(facts, at).filter((f) => f.subject === subject && f.slot === slot)
|
|
49
|
+
if (!pool.length) return null
|
|
50
|
+
const superseded = new Set(supersessionMap(pool).keys())
|
|
51
|
+
const alive = pool.filter((f) => !superseded.has(f.id))
|
|
52
|
+
return alive.length ? alive[alive.length - 1] : null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 主查询:有就给出值 + 证据链;没有就明说没有;不问「可能是什么」
|
|
56
|
+
export function query(facts, { subject, slot = 'default', at = new Date().toISOString() } = {}) {
|
|
57
|
+
const all = normalizeFacts(facts)
|
|
58
|
+
const atT = ts(at)
|
|
59
|
+
if (atT === null) return { status: 'unknown', reason: 'as-of 时间不合法' }
|
|
60
|
+
|
|
61
|
+
const pool = all.filter((f) => f.subject === subject && f.slot === slot && f.t <= atT)
|
|
62
|
+
if (!pool.length) {
|
|
63
|
+
return { status: 'unknown', reason: '截至该时间没有记录', evidence: [] }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 只看截至 as-of 已成立的取代关系:未来才会发生的取代不算数
|
|
67
|
+
const bySupersede = supersessionMap(pool)
|
|
68
|
+
const alive = pool.filter((f) => !bySupersede.has(f.id))
|
|
69
|
+
if (!alive.length) {
|
|
70
|
+
return { status: 'unknown', reason: '该时间点的记录都已被后续取代', evidence: pool }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const latest = alive[alive.length - 1]
|
|
74
|
+
const chain = []
|
|
75
|
+
let cur = latest
|
|
76
|
+
const seen = new Set()
|
|
77
|
+
while (cur && !seen.has(cur.id)) {
|
|
78
|
+
chain.push(cur)
|
|
79
|
+
seen.add(cur.id)
|
|
80
|
+
cur = cur.supersedes ? pool.find((f) => f.id === cur.supersedes) || null : null
|
|
81
|
+
}
|
|
82
|
+
chain.reverse()
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
status: 'known',
|
|
86
|
+
value: latest.value,
|
|
87
|
+
fact: latest,
|
|
88
|
+
evidence: chain,
|
|
89
|
+
reason: chain.length > 1 ? '有后续记录,旧值被取代' : '只有一条记录,未发现后续'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 后继缺席:在 at 之后有没有更新的记录?没有就明说没有,不补
|
|
94
|
+
export function hasSuccessor(facts, id, at = new Date().toISOString()) {
|
|
95
|
+
const all = normalizeFacts(facts)
|
|
96
|
+
const base = all.find((f) => f.id === id)
|
|
97
|
+
if (!base) return { ok: false, reason: '找不到这条记录' }
|
|
98
|
+
const atT = ts(at)
|
|
99
|
+
if (atT === null) return { ok: false, reason: 'as-of 时间不合法' }
|
|
100
|
+
const later = all.filter((f) => f.subject === base.subject && f.slot === base.slot && f.t > base.t && f.t <= atT)
|
|
101
|
+
return { ok: true, hasSuccessor: later.length > 0, successors: later }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// 渲染成一句人话:知道就说值,不知道就说不知道
|
|
105
|
+
export function render(verdict) {
|
|
106
|
+
if (!verdict || verdict.status !== 'known') {
|
|
107
|
+
return '没有记录:' + (verdict && verdict.reason ? verdict.reason : '查不到')
|
|
108
|
+
}
|
|
109
|
+
const f = verdict.fact
|
|
110
|
+
const when = f.at.slice(0, 10)
|
|
111
|
+
const src = f.source ? '(' + f.source + ')' : ''
|
|
112
|
+
return String(f.value) + ' · ' + when + src
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function withFileLock<T>(lockPath: string, fn: () => T, opts?: { timeoutMs?: number }): T
|
package/lib/file-lock.js
CHANGED
|
@@ -18,9 +18,14 @@ export function withFileLock(lockPath, fn, { timeoutMs = 3000 } = {}) {
|
|
|
18
18
|
if (Date.now() - st.mtimeMs > 10000) { fs.rmSync(lockPath, { recursive: true, force: true }); continue }
|
|
19
19
|
} catch { /* 锁刚被释放 */ }
|
|
20
20
|
if (Date.now() - start > timeoutMs) throw new Error('文件被另一个进程锁着,稍后再试。')
|
|
21
|
-
|
|
22
|
-
while (Date.now() - t < 20) { /* 短等待 */ }
|
|
21
|
+
sleep(20)
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
try { return fn() } finally { try { fs.rmSync(lockPath, { recursive: true, force: true }) } catch { } }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// 阻塞式小睡,不空转 CPU;Node 主线程允许 Atomics.wait
|
|
28
|
+
const sleepBuf = new Int32Array(new SharedArrayBuffer(4))
|
|
29
|
+
function sleep(ms) {
|
|
30
|
+
try { Atomics.wait(sleepBuf, 0, 0, ms) } catch { const t = Date.now(); while (Date.now() - t < ms) { } }
|
|
26
31
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface LaterEvent {
|
|
2
|
+
id: string
|
|
3
|
+
at: string
|
|
4
|
+
day: string
|
|
5
|
+
thought: string
|
|
6
|
+
reason: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function eventsFile(memDir: string): string
|
|
10
|
+
|
|
11
|
+
export function append(memDir: string, event: { id: string; thought: string; reason?: string }, opts?: { timeoutMs?: number }): LaterEvent
|
|
12
|
+
|
|
13
|
+
export function readAll(memDir: string): { rows: LaterEvent[]; bad: number }
|
|
14
|
+
|
|
15
|
+
export function groupByExp(memDir: string): Map<string, LaterEvent[]>
|
|
16
|
+
|
|
17
|
+
export function list(memDir: string, opts?: { id?: string; limit?: number }): LaterEvent[]
|
|
18
|
+
|
|
19
|
+
export const internals: { FILE: string }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface RecallEvent {
|
|
2
|
+
id: string
|
|
3
|
+
at: string
|
|
4
|
+
query: string
|
|
5
|
+
chatKey: string
|
|
6
|
+
backfilled?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function eventsFile(memDir: string): string
|
|
10
|
+
|
|
11
|
+
export function append(memDir: string, event: {
|
|
12
|
+
id: string
|
|
13
|
+
query?: string
|
|
14
|
+
chatKey?: string
|
|
15
|
+
backfilled?: boolean
|
|
16
|
+
}, opts?: { timeoutMs?: number }): RecallEvent
|
|
17
|
+
|
|
18
|
+
export function readAll(memDir: string): { rows: RecallEvent[]; bad: number }
|
|
19
|
+
|
|
20
|
+
export function countByExp(memDir: string): Map<string, { count: number; lastAt: string }>
|
|
21
|
+
|
|
22
|
+
export function list(memDir: string, opts?: { id?: string; limit?: number }): RecallEvent[]
|
|
23
|
+
|
|
24
|
+
export const internals: { FILE: string }
|
package/lib/recall-events.js
CHANGED
|
@@ -62,7 +62,16 @@ export function readAll(memDir) {
|
|
|
62
62
|
return { rows, bad }
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
+
// 进程内缓存:同一进程里连续 recall 不再重复解析整个事件文件
|
|
66
|
+
const memo = new Map()
|
|
67
|
+
|
|
65
68
|
export function countByExp(memDir) {
|
|
69
|
+
const file = eventsFile(memDir)
|
|
70
|
+
let stamp = ''
|
|
71
|
+
try { const st = fs.statSync(file); stamp = st.mtimeMs + ':' + st.size } catch { }
|
|
72
|
+
const hit = memo.get(memDir)
|
|
73
|
+
if (hit && hit.stamp === stamp) return hit.map
|
|
74
|
+
|
|
66
75
|
const { rows } = readAll(memDir)
|
|
67
76
|
const map = new Map()
|
|
68
77
|
for (const r of rows) {
|
|
@@ -71,6 +80,7 @@ export function countByExp(memDir) {
|
|
|
71
80
|
if (String(r.at || '') > cur.lastAt) cur.lastAt = String(r.at || '')
|
|
72
81
|
map.set(r.id, cur)
|
|
73
82
|
}
|
|
83
|
+
memo.set(memDir, { stamp, map })
|
|
74
84
|
return map
|
|
75
85
|
}
|
|
76
86
|
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
export type Weight = 'important' | 'normal' | 'minor'
|
|
2
|
+
|
|
3
|
+
export interface Quote {
|
|
4
|
+
text: string
|
|
5
|
+
source?: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface LaterEntry {
|
|
9
|
+
at: string
|
|
10
|
+
day?: string
|
|
11
|
+
thought: string
|
|
12
|
+
reason?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Experience {
|
|
16
|
+
id: string
|
|
17
|
+
at: string
|
|
18
|
+
day: string
|
|
19
|
+
author?: string
|
|
20
|
+
source?: string
|
|
21
|
+
kind: string
|
|
22
|
+
weight: Weight
|
|
23
|
+
then: {
|
|
24
|
+
text: string
|
|
25
|
+
scene?: string
|
|
26
|
+
feeling?: string[]
|
|
27
|
+
thought?: string
|
|
28
|
+
unfinished?: string
|
|
29
|
+
quote?: Quote | null
|
|
30
|
+
state?: Record<string, unknown> | null
|
|
31
|
+
}
|
|
32
|
+
later: LaterEntry[]
|
|
33
|
+
recalls?: number
|
|
34
|
+
lastRecalledAt?: string
|
|
35
|
+
status?: string
|
|
36
|
+
legacy?: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const LIMITS: Record<string, number>
|
|
40
|
+
|
|
41
|
+
export function loadAll(dir: string): Experience[]
|
|
42
|
+
|
|
43
|
+
export function addExperience(dir: string, opts: {
|
|
44
|
+
text: string
|
|
45
|
+
scene?: string
|
|
46
|
+
feeling?: string[]
|
|
47
|
+
thought?: string
|
|
48
|
+
unfinished?: string
|
|
49
|
+
quote?: Quote | null
|
|
50
|
+
weight?: Weight
|
|
51
|
+
kind?: string
|
|
52
|
+
state?: Record<string, unknown> | null
|
|
53
|
+
source?: string
|
|
54
|
+
}): Experience
|
|
55
|
+
|
|
56
|
+
export function reviseExperience(dir: string, opts: { id: string; thought: string; reason?: string }): {
|
|
57
|
+
ok: boolean
|
|
58
|
+
id?: string
|
|
59
|
+
at?: string
|
|
60
|
+
error?: string
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function markRecalled(dir: string, id: string): boolean
|
|
64
|
+
|
|
65
|
+
export function currentView(dir: string, id: string): Experience | null
|
|
66
|
+
|
|
67
|
+
export function recall(dir: string, query: string, opts?: {
|
|
68
|
+
limit?: number
|
|
69
|
+
minOverlap?: number
|
|
70
|
+
mark?: boolean
|
|
71
|
+
minRatio?: number
|
|
72
|
+
}): Experience[]
|
|
73
|
+
|
|
74
|
+
export function renderTwoPart(entry: Experience, opts?: { now?: Date; laterCount?: number }): string
|
|
75
|
+
|
|
76
|
+
export const internals: { WEIGHTS: Set<Weight>; FILE: string }
|
package/lib/self-experience.js
CHANGED
|
@@ -207,17 +207,19 @@ export function recall(dir, query, { limit = 3, minOverlap = 1, mark = false, mi
|
|
|
207
207
|
? Math.max(minOverlap, Math.ceil(bigrams.size * minRatio))
|
|
208
208
|
: minOverlap
|
|
209
209
|
|
|
210
|
-
const
|
|
210
|
+
const measure = (r) => {
|
|
211
211
|
const s = (r.then.text + ' ' + r.then.thought + ' ' + (r.then.feeling || []).join(' '))
|
|
212
212
|
const t = s.replace(/[\s\p{P}\p{S}]+/gu, '')
|
|
213
213
|
let n = 0
|
|
214
214
|
for (const g of bigrams) if (t.includes(g)) n++
|
|
215
|
-
|
|
215
|
+
// 记录自身的 bigram 规模:长记录天然更容易碰巧命中,排序时按 √长度 归一化
|
|
216
|
+
const size = Math.max(1, t.length - 1)
|
|
217
|
+
return { n, size }
|
|
216
218
|
}
|
|
217
219
|
const weightOf = (r) => (r.weight === 'important' ? 3 : r.weight === 'minor' ? 0.5 : 1)
|
|
218
220
|
const picked = rows.map((r) => {
|
|
219
|
-
const n =
|
|
220
|
-
return { r, n, s: n * weightOf(r) }
|
|
221
|
+
const { n, size } = measure(r)
|
|
222
|
+
return { r, n, s: (n / Math.sqrt(size)) * weightOf(r) }
|
|
221
223
|
})
|
|
222
224
|
.filter((x) => (q ? x.n >= required : true))
|
|
223
225
|
.sort((a, b) => b.s - a.s || String(b.r.at).localeCompare(String(a.r.at)))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface MemoryItem {
|
|
2
|
+
id: string
|
|
3
|
+
at: string
|
|
4
|
+
kind: string
|
|
5
|
+
text: string
|
|
6
|
+
thought: string
|
|
7
|
+
origin: 'own-time' | 'diary'
|
|
8
|
+
author: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function loadExperiences(dir: string): MemoryItem[]
|
|
12
|
+
export function loadDiaries(dir: string): MemoryItem[]
|
|
13
|
+
export function recall(dir: string, query: string, opts?: {
|
|
14
|
+
includeDiary?: boolean
|
|
15
|
+
limit?: number
|
|
16
|
+
minOverlap?: number
|
|
17
|
+
}): MemoryItem[]
|
|
18
|
+
export function renderBlock(items: MemoryItem[], opts?: { title?: string; maxChars?: number }): string
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface PromptHistoryEntry {
|
|
2
|
+
at: string
|
|
3
|
+
chars: number
|
|
4
|
+
hash?: string
|
|
5
|
+
reason: string
|
|
6
|
+
snapshot: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function load(dataDir: string): string
|
|
10
|
+
|
|
11
|
+
export function save(dataDir: string, text: string, reason?: string, opts?: { timeoutMs?: number }): {
|
|
12
|
+
ok: boolean
|
|
13
|
+
noop?: boolean
|
|
14
|
+
version?: string
|
|
15
|
+
chars?: number
|
|
16
|
+
hash?: string
|
|
17
|
+
error?: string
|
|
18
|
+
reason?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function history(dataDir: string, limit?: number): PromptHistoryEntry[]
|
|
22
|
+
|
|
23
|
+
export function revert(dataDir: string, version: string, reason?: string): ReturnType<typeof save>
|
|
24
|
+
|
|
25
|
+
export function render(basePrompt: string, dataDir: string, opts?: { marker?: string }): {
|
|
26
|
+
text: string
|
|
27
|
+
appended: boolean
|
|
28
|
+
truncated: boolean
|
|
29
|
+
note?: string
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function verify(dataDir: string): {
|
|
33
|
+
ok: boolean
|
|
34
|
+
activeHash?: string
|
|
35
|
+
versionHash?: string
|
|
36
|
+
version?: string
|
|
37
|
+
note?: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const internals: { MAX_CHARS: number; MAX_APPEND_TOTAL: number; VERSIONS: string; ACTIVE: string; sha1: (s: string) => string }
|
package/lib/self-prompt.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import fs from 'node:fs'
|
|
4
4
|
import path from 'node:path'
|
|
5
5
|
import crypto from 'node:crypto'
|
|
6
|
+
import { withFileLock } from './file-lock.js'
|
|
6
7
|
|
|
7
8
|
const DIR = 'prompt-overlay'
|
|
8
9
|
const ACTIVE = 'prompt-overlay.md'
|
|
@@ -32,32 +33,6 @@ function atomicWrite(file, body) {
|
|
|
32
33
|
fs.renameSync(tmp, file)
|
|
33
34
|
}
|
|
34
35
|
|
|
35
|
-
// 单写者锁:另一个进程正在写时等一会儿;超过 timeout 报错,不静默覆盖
|
|
36
|
-
// Windows 上并发 mkdir/rm 同一路径会短暂返回 EPERM/ENOENT,当成“忙”重试
|
|
37
|
-
const BUSY = new Set(['EEXIST', 'EPERM', 'ENOENT', 'EBUSY', 'EACCES'])
|
|
38
|
-
|
|
39
|
-
function withLock(dir, fn, { timeoutMs = 2000 } = {}) {
|
|
40
|
-
const lock = path.join(dir, '.lock')
|
|
41
|
-
const start = Date.now()
|
|
42
|
-
for (;;) {
|
|
43
|
-
try {
|
|
44
|
-
fs.mkdirSync(dir, { recursive: true })
|
|
45
|
-
fs.mkdirSync(lock)
|
|
46
|
-
break
|
|
47
|
-
} catch (e) {
|
|
48
|
-
if (!BUSY.has(e.code)) throw e
|
|
49
|
-
try {
|
|
50
|
-
const st = fs.statSync(lock)
|
|
51
|
-
if (Date.now() - st.mtimeMs > 10000) { fs.rmSync(lock, { recursive: true, force: true }); continue }
|
|
52
|
-
} catch { /* 锁刚被释放 */ }
|
|
53
|
-
if (Date.now() - start > timeoutMs) throw new Error('提示词文件被另一个进程锁着,稍后再试。')
|
|
54
|
-
const t = Date.now()
|
|
55
|
-
while (Date.now() - t < 25) { /* 短等待 */ }
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
try { return fn() } finally { try { fs.rmSync(lock, { recursive: true, force: true }) } catch { } }
|
|
59
|
-
}
|
|
60
|
-
|
|
61
36
|
export function load(dataDir) {
|
|
62
37
|
try {
|
|
63
38
|
return clean(fs.readFileSync(path.join(base(dataDir), ACTIVE), 'utf8'))
|
|
@@ -74,7 +49,7 @@ export function save(dataDir, text, reason = '', opts = {}) {
|
|
|
74
49
|
|
|
75
50
|
const d = base(dataDir)
|
|
76
51
|
try {
|
|
77
|
-
return
|
|
52
|
+
return withFileLock(path.join(d, '.lock'), () => {
|
|
78
53
|
const baseStamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 23)
|
|
79
54
|
let stamp = baseStamp
|
|
80
55
|
let n = 1
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export const DEFAULTS: {
|
|
2
|
+
ownTime: string
|
|
3
|
+
voiceSoftLimitPerDay: number | null
|
|
4
|
+
quietHours: string | null
|
|
5
|
+
readingPerDay: number
|
|
6
|
+
note: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type SelfSettings = typeof DEFAULTS & Record<string, unknown> & { updatedAt?: string }
|
|
10
|
+
|
|
11
|
+
export function load(dataDir: string): SelfSettings
|
|
12
|
+
export function save(dataDir: string, patch: Record<string, unknown>, reason?: string): SelfSettings
|
package/lib/waking.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export function buildWakingBlock(dataDir: string, chatKey: string, opts?: { now?: Date }): string
|
|
2
|
+
export function consumeWaking(dataDir: string, chatKey: string, opts?: { now?: Date }): boolean
|
|
3
|
+
export function leaveNote(dataDir: string, text: string): { at: string; day: string; text: string }
|
package/package.json
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
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",
|
|
7
|
+
"types": "./index.d.ts",
|
|
7
8
|
"exports": {
|
|
8
|
-
".": "./index.js",
|
|
9
|
-
"./self-experience": "./lib/self-experience.js",
|
|
10
|
-
"./self-memory": "./lib/self-memory.js",
|
|
11
|
-
"./waking": "./lib/waking.js",
|
|
12
|
-
"./self-prompt": "./lib/self-prompt.js",
|
|
13
|
-
"./self-settings": "./lib/self-settings.js",
|
|
14
|
-
"./recall-events": "./lib/recall-events.js",
|
|
15
|
-
"./later-events": "./lib/later-events.js",
|
|
16
|
-
"./file-lock": "./lib/file-lock.js"
|
|
9
|
+
".": { "types": "./index.d.ts", "default": "./index.js" },
|
|
10
|
+
"./self-experience": { "types": "./lib/self-experience.d.ts", "default": "./lib/self-experience.js" },
|
|
11
|
+
"./self-memory": { "types": "./lib/self-memory.d.ts", "default": "./lib/self-memory.js" },
|
|
12
|
+
"./waking": { "types": "./lib/waking.d.ts", "default": "./lib/waking.js" },
|
|
13
|
+
"./self-prompt": { "types": "./lib/self-prompt.d.ts", "default": "./lib/self-prompt.js" },
|
|
14
|
+
"./self-settings": { "types": "./lib/self-settings.d.ts", "default": "./lib/self-settings.js" },
|
|
15
|
+
"./recall-events": { "types": "./lib/recall-events.d.ts", "default": "./lib/recall-events.js" },
|
|
16
|
+
"./later-events": { "types": "./lib/later-events.d.ts", "default": "./lib/later-events.js" },
|
|
17
|
+
"./file-lock": { "types": "./lib/file-lock.d.ts", "default": "./lib/file-lock.js" },
|
|
18
|
+
"./evidence-policy": { "types": "./lib/evidence-policy.d.ts", "default": "./lib/evidence-policy.js" },
|
|
19
|
+
"./evidence-from-experience": { "types": "./lib/evidence-from-experience.d.ts", "default": "./lib/evidence-from-experience.js" }
|
|
17
20
|
},
|
|
18
21
|
"files": [
|
|
19
22
|
"index.js",
|
|
23
|
+
"index.d.ts",
|
|
20
24
|
"lib",
|
|
21
25
|
"demo.mjs",
|
|
22
26
|
"README.md"
|
|
@@ -41,4 +45,4 @@
|
|
|
41
45
|
"directory": "packages/core"
|
|
42
46
|
},
|
|
43
47
|
"sideEffects": false
|
|
44
|
-
}
|
|
48
|
+
}
|