whale-memory-core 0.7.0 → 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/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 +76 -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 +15 -12
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 @@
|
|
|
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,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 }
|
|
@@ -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,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",
|
|
17
|
-
"./evidence-policy": "./lib/evidence-policy.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" }
|
|
18
20
|
},
|
|
19
21
|
"files": [
|
|
20
22
|
"index.js",
|
|
23
|
+
"index.d.ts",
|
|
21
24
|
"lib",
|
|
22
25
|
"demo.mjs",
|
|
23
26
|
"README.md"
|
|
@@ -42,4 +45,4 @@
|
|
|
42
45
|
"directory": "packages/core"
|
|
43
46
|
},
|
|
44
47
|
"sideEffects": false
|
|
45
|
-
}
|
|
48
|
+
}
|