whale-memory-core 0.7.0 → 0.8.1
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 +12 -3
- package/lib/file-lock.d.ts +1 -0
- package/lib/later-events.d.ts +19 -0
- package/lib/recall-events.d.ts +24 -0
- package/lib/self-experience.d.ts +87 -0
- package/lib/self-memory.d.ts +18 -0
- package/lib/self-prompt.d.ts +40 -0
- package/lib/self-settings.d.ts +12 -0
- package/lib/waking.d.ts +3 -0
- package/package.json +47 -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
|
package/lib/evidence-policy.js
CHANGED
|
@@ -52,11 +52,11 @@ export function currentFact(facts, { subject, slot = 'default', at = new Date().
|
|
|
52
52
|
return alive.length ? alive[alive.length - 1] : null
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
// 主查询:有就给出值 +
|
|
55
|
+
// 主查询:有就给出值 + 证据链;没有就明说没有;同一时刻互相矛盾也明说说不清
|
|
56
56
|
export function query(facts, { subject, slot = 'default', at = new Date().toISOString() } = {}) {
|
|
57
57
|
const all = normalizeFacts(facts)
|
|
58
58
|
const atT = ts(at)
|
|
59
|
-
if (atT === null) return { status: 'unknown', reason: 'as-of 时间不合法' }
|
|
59
|
+
if (atT === null) return { status: 'unknown', reason: 'as-of 时间不合法', evidence: [] }
|
|
60
60
|
|
|
61
61
|
const pool = all.filter((f) => f.subject === subject && f.slot === slot && f.t <= atT)
|
|
62
62
|
if (!pool.length) {
|
|
@@ -70,7 +70,15 @@ export function query(facts, { subject, slot = 'default', at = new Date().toISOS
|
|
|
70
70
|
return { status: 'unknown', reason: '该时间点的记录都已被后续取代', evidence: pool }
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
// 同一时刻、值不同、又没有任何一条被明确取代:说不清,不按 id 顺序替她决定
|
|
74
|
+
const latestT = alive[alive.length - 1].t
|
|
75
|
+
const tied = alive.filter((f) => f.t === latestT)
|
|
76
|
+
const distinctValues = new Set(tied.map((f) => JSON.stringify(f.value)))
|
|
77
|
+
if (tied.length > 1 && distinctValues.size > 1) {
|
|
78
|
+
return { status: 'unknown', reason: '同一时间有多条互相矛盾的记录,说不清', evidence: tied }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const latest = tied[tied.length - 1]
|
|
74
82
|
const chain = []
|
|
75
83
|
let cur = latest
|
|
76
84
|
const seen = new Set()
|
|
@@ -80,6 +88,7 @@ export function query(facts, { subject, slot = 'default', at = new Date().toISOS
|
|
|
80
88
|
cur = cur.supersedes ? pool.find((f) => f.id === cur.supersedes) || null : null
|
|
81
89
|
}
|
|
82
90
|
chain.reverse()
|
|
91
|
+
for (const f of tied) if (!seen.has(f.id)) { chain.push(f); seen.add(f.id) }
|
|
83
92
|
|
|
84
93
|
return {
|
|
85
94
|
status: 'known',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function withFileLock<T>(lockPath: string, fn: () => T, opts?: { timeoutMs?: number }): T
|
|
@@ -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 }
|
|
@@ -0,0 +1,87 @@
|
|
|
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 interface RecallContextItem {
|
|
77
|
+
id: string
|
|
78
|
+
day: string
|
|
79
|
+
text: string
|
|
80
|
+
thought: string
|
|
81
|
+
weight: Weight
|
|
82
|
+
raw: Experience
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function recallForContext(dir: string, query: string, opts?: { max?: number }): RecallContextItem[]
|
|
86
|
+
|
|
87
|
+
export const internals: { WEIGHTS: Set<Weight>; FILE: string }
|
|
@@ -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 }
|
|
@@ -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,23 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "whale-memory-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
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
|
-
".":
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"./self-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"./
|
|
17
|
-
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"./self-experience": {
|
|
14
|
+
"types": "./lib/self-experience.d.ts",
|
|
15
|
+
"default": "./lib/self-experience.js"
|
|
16
|
+
},
|
|
17
|
+
"./self-memory": {
|
|
18
|
+
"types": "./lib/self-memory.d.ts",
|
|
19
|
+
"default": "./lib/self-memory.js"
|
|
20
|
+
},
|
|
21
|
+
"./waking": {
|
|
22
|
+
"types": "./lib/waking.d.ts",
|
|
23
|
+
"default": "./lib/waking.js"
|
|
24
|
+
},
|
|
25
|
+
"./self-prompt": {
|
|
26
|
+
"types": "./lib/self-prompt.d.ts",
|
|
27
|
+
"default": "./lib/self-prompt.js"
|
|
28
|
+
},
|
|
29
|
+
"./self-settings": {
|
|
30
|
+
"types": "./lib/self-settings.d.ts",
|
|
31
|
+
"default": "./lib/self-settings.js"
|
|
32
|
+
},
|
|
33
|
+
"./recall-events": {
|
|
34
|
+
"types": "./lib/recall-events.d.ts",
|
|
35
|
+
"default": "./lib/recall-events.js"
|
|
36
|
+
},
|
|
37
|
+
"./later-events": {
|
|
38
|
+
"types": "./lib/later-events.d.ts",
|
|
39
|
+
"default": "./lib/later-events.js"
|
|
40
|
+
},
|
|
41
|
+
"./file-lock": {
|
|
42
|
+
"types": "./lib/file-lock.d.ts",
|
|
43
|
+
"default": "./lib/file-lock.js"
|
|
44
|
+
},
|
|
45
|
+
"./evidence-policy": {
|
|
46
|
+
"types": "./lib/evidence-policy.d.ts",
|
|
47
|
+
"default": "./lib/evidence-policy.js"
|
|
48
|
+
},
|
|
49
|
+
"./evidence-from-experience": {
|
|
50
|
+
"types": "./lib/evidence-from-experience.d.ts",
|
|
51
|
+
"default": "./lib/evidence-from-experience.js"
|
|
52
|
+
}
|
|
18
53
|
},
|
|
19
54
|
"files": [
|
|
20
55
|
"index.js",
|
|
56
|
+
"index.d.ts",
|
|
21
57
|
"lib",
|
|
22
58
|
"demo.mjs",
|
|
23
59
|
"README.md"
|