whale-memory-core 0.21.1 → 0.23.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 +2 -0
- package/index.js +2 -0
- package/lib/action-source.d.ts +33 -0
- package/lib/action-source.js +121 -0
- package/lib/chapter-events.d.ts +4 -0
- package/lib/chapter-events.js +9 -2
- package/lib/conversation-privacy.js +3 -2
- package/lib/event-log.js +10 -1
- package/lib/later-events.d.ts +5 -0
- package/lib/later-events.js +21 -3
- package/lib/link-events.d.ts +4 -0
- package/lib/link-events.js +9 -2
- package/lib/recall-events.d.ts +5 -0
- package/lib/recall-events.js +23 -4
- package/lib/scope-key.js +1 -1
- package/lib/seal-events.d.ts +4 -0
- package/lib/seal-events.js +11 -2
- package/lib/self-actions.d.ts +71 -0
- package/lib/self-actions.js +192 -0
- package/lib/self-experience.d.ts +7 -2
- package/lib/self-experience.js +65 -14
- package/lib/sqlite-store.js +289 -0
- package/lib/storage/backend.js +39 -0
- package/lib/storage/file-backend.js +119 -0
- package/lib/storage/sqlite-backend.js +206 -0
- package/lib/unfinished-events.d.ts +4 -0
- package/lib/unfinished-events.js +21 -3
- package/lib/unsaid.js +5 -1
- package/package.json +13 -2
package/index.d.ts
CHANGED
|
@@ -24,3 +24,5 @@ export * as factStore from './lib/fact-store.js'
|
|
|
24
24
|
export * as factJudge from './lib/fact-judge.js'
|
|
25
25
|
export * as changed from './lib/changed.js'
|
|
26
26
|
export * as injection from './lib/injection.js'
|
|
27
|
+
export * as selfActions from './lib/self-actions.js'
|
|
28
|
+
export * as actionSource from './lib/action-source.js'
|
package/index.js
CHANGED
|
@@ -25,4 +25,6 @@ export * as factCard from './lib/fact-card.js'
|
|
|
25
25
|
export * as factStore from './lib/fact-store.js'
|
|
26
26
|
export * as factJudge from './lib/fact-judge.js'
|
|
27
27
|
export * as changed from './lib/changed.js'
|
|
28
|
+
export * as selfActions from './lib/self-actions.js'
|
|
29
|
+
export * as actionSource from './lib/action-source.js'
|
|
28
30
|
export * as injection from './lib/injection.js'
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare const SOURCES: readonly ['asked', 'self', 'unknown']
|
|
2
|
+
export declare const TRIGGER_KINDS: readonly string[]
|
|
3
|
+
|
|
4
|
+
export type ActionSource = 'asked' | 'self' | 'unknown'
|
|
5
|
+
|
|
6
|
+
export interface TriggerDescriptor {
|
|
7
|
+
/** 触发种类:user-request / user-mention / system / none */
|
|
8
|
+
kind: 'user-request' | 'user-mention' | 'system' | 'none'
|
|
9
|
+
/** kind=user-request 时可指定:用户要求的动作名 */
|
|
10
|
+
action?: string
|
|
11
|
+
/** kind=user-request 时可指定:用户要用的工具名 */
|
|
12
|
+
tool?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ClassifySourceInput {
|
|
16
|
+
/** 这一条动作的名字,例如 draft_save / project_done */
|
|
17
|
+
action?: string
|
|
18
|
+
/** 这一轮调用的工具名,例如 self_project_draft */
|
|
19
|
+
tool?: string
|
|
20
|
+
/**
|
|
21
|
+
* 这一轮的外部触发,必须是结构化描述:
|
|
22
|
+
* - null:这一轮没有外部输入(唯一能得到 self 的入口)
|
|
23
|
+
* - {kind:'user-request', action?, tool?}:用户要求
|
|
24
|
+
* - {kind:'user-mention'}:提到但不是要求
|
|
25
|
+
* - {kind:'system'}:定时器 / 空闲窗口 / 提醒(不是 self)
|
|
26
|
+
* - 其它(含字符串):unknown——自由文本不作为证据
|
|
27
|
+
*/
|
|
28
|
+
trigger?: TriggerDescriptor | null
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare function classifySource(input?: ClassifySourceInput | null): ActionSource
|
|
32
|
+
export declare function noExternalInput(): null
|
|
33
|
+
export declare function describeSource(source: string, opts?: { system?: boolean }): string
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// 发起来源:只认结构化证据,不从自然语言里猜。
|
|
2
|
+
//
|
|
3
|
+
// 这条是踩过坑才定下来的:上一版试图从一句中文里抽出"谁要的",结果每加一条规则就多一个反例——
|
|
4
|
+
// 「初稿看起来还行」被判成请求,「先别存稿」被判成请求,「帮我看看我删掉的初稿」把"看"和"删"混成一件。
|
|
5
|
+
// 中文口语没有可依赖的边界,关键词匹配在"判不出就记 unknown"这条原则下只会越改越假。
|
|
6
|
+
//
|
|
7
|
+
// 所以这一版换掉整个思路:**调用方给结构化的触发描述,库只做受约束的判定。**
|
|
8
|
+
// · 自由文本一律不当证据:传字符串 → unknown。
|
|
9
|
+
// · self 只有一条入口:调用方显式传 trigger: null(这一轮没有外部输入)。
|
|
10
|
+
// · 定时器、空闲窗口、提醒这类"系统触发"永远不是 self——它们不算她的主动性。
|
|
11
|
+
// · 判不出就 unknown;库不提供任何"猜"的开关。
|
|
12
|
+
|
|
13
|
+
export const SOURCES = Object.freeze(['asked', 'self', 'unknown'])
|
|
14
|
+
|
|
15
|
+
// 触发种类。调用方必须在自己的回合模型里判出来,再交给库。
|
|
16
|
+
export const TRIGGER_KINDS = Object.freeze([
|
|
17
|
+
'user-request', // 用户明确要求做这件事(可带动作名/工具名做精确匹配)
|
|
18
|
+
'user-mention', // 用户提到了这件事,但不是要求
|
|
19
|
+
'system', // 定时器 / 空闲窗口 / 提醒等系统触发
|
|
20
|
+
'none' // 这一轮确实没有外部输入
|
|
21
|
+
])
|
|
22
|
+
|
|
23
|
+
const has = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key)
|
|
24
|
+
const isPlainObject = (v) => Boolean(v) && typeof v === 'object' && !Array.isArray(v)
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 判一条动作的发起来源。永不抛错;任何拿不准的输入都返回 unknown。
|
|
28
|
+
*
|
|
29
|
+
* @param {object|null} [input]
|
|
30
|
+
* @param {string} [input.action] 动作名(draft_save / project_done …)
|
|
31
|
+
* @param {string} [input.tool] 这一轮调用的工具名
|
|
32
|
+
* @param {null|{kind: string, action?: string, tool?: string}} [input.trigger]
|
|
33
|
+
* · null —— 这一轮没有外部输入(唯一能得到 self 的入口)
|
|
34
|
+
* · {kind:'user-request',...} —— 用户要求;若给了 action/tool,必须与本次一致
|
|
35
|
+
* · {kind:'user-mention'} —— 提到但不是要求 → unknown
|
|
36
|
+
* · {kind:'system'} —— 定时器/空闲窗口/提醒 → unknown(不是 self)
|
|
37
|
+
* · undefined / 字符串 / 其它 —— unknown
|
|
38
|
+
* @returns {'asked'|'self'|'unknown'}
|
|
39
|
+
*/
|
|
40
|
+
export function classifySource(input) {
|
|
41
|
+
// 整个函数体包在一层 try 里:属性 getter、被撤销的 Proxy、任何奇怪的宿主对象,
|
|
42
|
+
// 都不能让调用点因为记账失败而挂掉。
|
|
43
|
+
try {
|
|
44
|
+
return classifyInner(input)
|
|
45
|
+
} catch {
|
|
46
|
+
return 'unknown'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function classifyInner(input) {
|
|
51
|
+
const o = isPlainObject(input) || (input && typeof input === 'object') ? input : {}
|
|
52
|
+
|
|
53
|
+
const trigger = o.trigger
|
|
54
|
+
|
|
55
|
+
// ① 唯一的 self 入口:调用方显式声明"这一轮没有外部输入"。
|
|
56
|
+
if (trigger === null) return 'self'
|
|
57
|
+
|
|
58
|
+
// ② 取不到值、或者给了自由文本:不当证据。
|
|
59
|
+
if (!isPlainObject(trigger)) return 'unknown'
|
|
60
|
+
|
|
61
|
+
const rawKind = trigger.kind
|
|
62
|
+
if (typeof rawKind !== 'string') return 'unknown'
|
|
63
|
+
const kind = rawKind.trim()
|
|
64
|
+
|
|
65
|
+
// ③ 显式写 none 等同于"没有外部输入",但只有在 kind 明确时才认。
|
|
66
|
+
if (kind === 'none') return 'self'
|
|
67
|
+
|
|
68
|
+
// ④ 系统触发(定时器/空闲/提醒):不算她自己发起,也不算被用户要求。
|
|
69
|
+
if (kind === 'system') return 'unknown'
|
|
70
|
+
|
|
71
|
+
if (kind !== 'user-request') return 'unknown' // user-mention 与未知种类
|
|
72
|
+
|
|
73
|
+
// ⑤ 用户要求:标了动作/工具就必须对得上。
|
|
74
|
+
// 注意"标了但标坏"(空串、null、非字符串)不等于"没标"——那是调用方的错误,按 unknown 处理。
|
|
75
|
+
// 本次动作/工具一侧:给了就必须是字符串,否则无从核对,按 unknown(调用方的错误不放过)。
|
|
76
|
+
if (o.action !== undefined && typeof o.action !== 'string') return 'unknown'
|
|
77
|
+
if (o.tool !== undefined && typeof o.tool !== 'string') return 'unknown'
|
|
78
|
+
const action = typeof o.action === 'string' ? o.action.trim() : ''
|
|
79
|
+
const tool = typeof o.tool === 'string' ? o.tool.trim() : ''
|
|
80
|
+
|
|
81
|
+
// 触发一侧:读到就核对。用 !== undefined 而不是自有属性判断——
|
|
82
|
+
// 原型链上的 action/tool 同样算"标了",不能靠继承绕过去。
|
|
83
|
+
if (trigger.action !== undefined) {
|
|
84
|
+
const want = trigger.action
|
|
85
|
+
if (typeof want !== 'string' || !want.trim() || want.trim() !== action) return 'unknown'
|
|
86
|
+
}
|
|
87
|
+
if (trigger.tool !== undefined) {
|
|
88
|
+
const want = trigger.tool
|
|
89
|
+
if (typeof want !== 'string' || !want.trim() || want.trim() !== tool) return 'unknown'
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return 'asked'
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* 给宿主用的便捷构造:明确表示"这一轮没有外部输入"。
|
|
97
|
+
* 用函数而不是常量,避免调用点误把对象当可变的共享状态。
|
|
98
|
+
*/
|
|
99
|
+
export function noExternalInput() {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 人类可读的一句话,用于日志与报表;不参与判定。
|
|
105
|
+
* @param {'asked'|'self'|'unknown'} source
|
|
106
|
+
* @param {{system?: boolean}} [opts]
|
|
107
|
+
*/
|
|
108
|
+
export function describeSource(source, opts = {}) {
|
|
109
|
+
try {
|
|
110
|
+
const s = typeof source === 'string' ? source.trim() : ''
|
|
111
|
+
if (s === 'asked') return '用户要求'
|
|
112
|
+
if (s === 'self') return '自己发起(这一轮没有外部输入)'
|
|
113
|
+
if (s === 'unknown') {
|
|
114
|
+
const system = isPlainObject(opts) && opts.system === true
|
|
115
|
+
return system ? '系统触发(定时/空闲/提醒),不计入自己发起' : '判不出来源'
|
|
116
|
+
}
|
|
117
|
+
return '未知来源值'
|
|
118
|
+
} catch {
|
|
119
|
+
return '未知来源值'
|
|
120
|
+
}
|
|
121
|
+
}
|
package/lib/chapter-events.d.ts
CHANGED
|
@@ -29,6 +29,10 @@ export interface Chapter {
|
|
|
29
29
|
export function eventsFile(memDir: string): string
|
|
30
30
|
export function readAll(memDir: string): { rows: ChapterEvent[]; bad: number }
|
|
31
31
|
export function chapters(memDir: string, opts?: { until?: number | null }): Chapter[]
|
|
32
|
+
/** 同 chapters,但输入是已经读出的行(不碰文件) */
|
|
33
|
+
export function chapterRows(rows: ChapterEvent[], opts?: { until?: number | null }): Chapter[]
|
|
34
|
+
/** 一行算不算合格的章节事件(文件与 SQLite 同一条判据) */
|
|
35
|
+
export function isValid(row: unknown): boolean
|
|
32
36
|
/** 这一天落在哪些章节里(章节可以重叠) */
|
|
33
37
|
export function chaptersOn(list: Chapter[], day: string): Chapter[]
|
|
34
38
|
/** from 默认今天;同名同起点重复提交是 noop */
|
package/lib/chapter-events.js
CHANGED
|
@@ -6,7 +6,9 @@ import { createLog, stamp } from './event-log.js'
|
|
|
6
6
|
import { before, parseAsOf } from './as-of.js'
|
|
7
7
|
|
|
8
8
|
export const ACTIONS = new Set(['open', 'rename', 'close'])
|
|
9
|
-
|
|
9
|
+
// 一行算不算合格的章节事件。文件和 SQLite 两个容器用同一条判据,坏行的口径才一致
|
|
10
|
+
export const isValid = (r) => Boolean(r && r.id && ACTIONS.has(r.action))
|
|
11
|
+
const log = createLog('chapter-events.jsonl', { valid: isValid })
|
|
10
12
|
export const eventsFile = log.eventsFile
|
|
11
13
|
export const readAll = log.readAll
|
|
12
14
|
export const internals = { FILE: log.FILE }
|
|
@@ -30,8 +32,13 @@ function checkDay(v, label) {
|
|
|
30
32
|
|
|
31
33
|
// 重放事件,得到每个章节现在(或截至 until)的样子
|
|
32
34
|
export function chapters(memDir, { until = null } = {}) {
|
|
35
|
+
return chapterRows(readAll(memDir).rows, { until })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 同上,但输入是已经读出来的行(不碰文件):SQLite 后端走这里
|
|
39
|
+
export function chapterRows(rows, { until = null } = {}) {
|
|
33
40
|
const map = new Map()
|
|
34
|
-
for (const r of
|
|
41
|
+
for (const r of rows) {
|
|
35
42
|
if (!before(r, until)) continue
|
|
36
43
|
if (r.action === 'open') map.set(r.id, { id: r.id, name: r.name, names: [r.name], from: r.from, to: r.to || '', note: r.note || '', openedAt: r.at })
|
|
37
44
|
const c = map.get(r.id)
|
|
@@ -78,8 +78,9 @@ export function createAudience({ ownerId = '', isActive = () => true } = {}) {
|
|
|
78
78
|
if (source.base === dest.base) {
|
|
79
79
|
// 同一个房间内:只有完全同一套槽位才算"这里本来就看得到"
|
|
80
80
|
allowed = source.key === dest.key;
|
|
81
|
-
} else if (!(dest.group &&
|
|
82
|
-
//
|
|
81
|
+
} else if (!(dest.group && (source.private || source.local))) {
|
|
82
|
+
// 跨房间:任何私聊(主人的、别人的)和本机主人都不流向群;
|
|
83
|
+
// 别人的私聊要进群必须由宿主显式 opt-in(自定义 isActive 也绕不过这一层)。
|
|
83
84
|
allowed = isActive(source.key, source.base);
|
|
84
85
|
}
|
|
85
86
|
}
|
package/lib/event-log.js
CHANGED
|
@@ -10,7 +10,16 @@ export function createLog(fileName, { valid = (r) => Boolean(r) } = {}) {
|
|
|
10
10
|
|
|
11
11
|
function readAll(memDir) {
|
|
12
12
|
let raw = ''
|
|
13
|
-
try {
|
|
13
|
+
try {
|
|
14
|
+
raw = fs.readFileSync(eventsFile(memDir), 'utf8')
|
|
15
|
+
} catch (error) {
|
|
16
|
+
// 只有"还没有这个文件"能当空;权限、是目录、坏盘必须报出来,不能当成 0 条。
|
|
17
|
+
if (error && error.code === 'ENOENT') return { rows: [], bad: 0 }
|
|
18
|
+
const wrapped = new Error('事件日志读不出来:' + path.basename(eventsFile(memDir)) + ':' + String(error && error.message ? error.message : error))
|
|
19
|
+
wrapped.code = 'EVENTS_UNREADABLE'
|
|
20
|
+
wrapped.cause = error
|
|
21
|
+
throw wrapped
|
|
22
|
+
}
|
|
14
23
|
const rows = []
|
|
15
24
|
let bad = 0
|
|
16
25
|
for (const line of raw.split('\n')) {
|
package/lib/later-events.d.ts
CHANGED
|
@@ -17,6 +17,11 @@ export function readAll(memDir: string): { rows: LaterEvent[]; bad: number }
|
|
|
17
17
|
/** until:只要这一刻(毫秒时间戳)之前(含)的后记 */
|
|
18
18
|
export function groupByExp(memDir: string, opts?: { until?: number | null }): Map<string, LaterEvent[]>
|
|
19
19
|
|
|
20
|
+
/** 同 groupByExp,但输入是已经读出的行(不碰文件) */
|
|
21
|
+
export function groupRows(rows: LaterEvent[], opts?: { until?: number | null }): Map<string, LaterEvent[]>
|
|
22
|
+
/** 一行算不算合格的后记事件(文件与 SQLite 同一条判据) */
|
|
23
|
+
export function isValid(row: unknown): boolean
|
|
24
|
+
|
|
20
25
|
export function list(memDir: string, opts?: { id?: string; limit?: number }): LaterEvent[]
|
|
21
26
|
|
|
22
27
|
export const internals: { FILE: string }
|
package/lib/later-events.js
CHANGED
|
@@ -46,23 +46,41 @@ export function append(memDir, { id, thought, reason = '', feeling = [] } = {},
|
|
|
46
46
|
|
|
47
47
|
export function readAll(memDir) {
|
|
48
48
|
let raw = ''
|
|
49
|
-
try {
|
|
49
|
+
try {
|
|
50
|
+
raw = fs.readFileSync(eventsFile(memDir), 'utf8')
|
|
51
|
+
} catch (error) {
|
|
52
|
+
// 只有"还没有这个文件"能当空;权限、是目录、坏盘必须报出来,不能当成 0 条。
|
|
53
|
+
if (error && error.code === 'ENOENT') return { rows: [], bad: 0 }
|
|
54
|
+
const wrapped = new Error('事件日志读不出来:later-events.jsonl:' + String(error && error.message ? error.message : error))
|
|
55
|
+
wrapped.code = 'EVENTS_UNREADABLE'
|
|
56
|
+
wrapped.cause = error
|
|
57
|
+
throw wrapped
|
|
58
|
+
}
|
|
50
59
|
const rows = []
|
|
51
60
|
let bad = 0
|
|
52
61
|
for (const line of raw.split('\n')) {
|
|
53
62
|
if (!line.trim()) continue
|
|
54
63
|
try {
|
|
55
64
|
const r = JSON.parse(line)
|
|
56
|
-
if (r
|
|
65
|
+
if (isValid(r)) rows.push(r)
|
|
57
66
|
else bad += 1
|
|
58
67
|
} catch { bad += 1 }
|
|
59
68
|
}
|
|
60
69
|
return { rows, bad }
|
|
61
70
|
}
|
|
62
71
|
|
|
72
|
+
// 一行算不算合格的后记事件。文件和 SQLite 两个容器用同一条判据,坏行的口径才一致
|
|
73
|
+
export function isValid(r) {
|
|
74
|
+
return Boolean(r && r.id && r.thought)
|
|
75
|
+
}
|
|
76
|
+
|
|
63
77
|
// until:只要这一刻之前(含)写下的后记,「翻回那天的自己」用
|
|
64
78
|
export function groupByExp(memDir, { until = null } = {}) {
|
|
65
|
-
|
|
79
|
+
return groupRows(readAll(memDir).rows, { until })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 同上,但输入是已经读出来的行(不碰文件):SQLite 后端从库里读出事件后走这里
|
|
83
|
+
export function groupRows(rows, { until = null } = {}) {
|
|
66
84
|
const map = new Map()
|
|
67
85
|
for (const r of rows) {
|
|
68
86
|
if (!before(r, until)) continue
|
package/lib/link-events.d.ts
CHANGED
|
@@ -12,4 +12,8 @@ export function readAll(memDir: string): { rows: LinkEvent[]; bad: number }
|
|
|
12
12
|
export function link(memDir: string, opts: { from: string; to: string; reason: string }, lockOpts?: Record<string, unknown>): { row: LinkEvent | null; noop: boolean }
|
|
13
13
|
/** 每条经历连着哪些(两头都有);until 用于翻回那天 */
|
|
14
14
|
export function linksByExp(memDir: string, opts?: { until?: number | null }): Map<string, Array<{ other: string; reason: string; at: string; day: string }>>
|
|
15
|
+
/** 同 linksByExp,但输入是已经读出的行(不碰文件) */
|
|
16
|
+
export function linksRows(rows: LinkEvent[], opts?: { until?: number | null }): Map<string, Array<{ other: string; reason: string; at: string; day: string }>>
|
|
17
|
+
/** 一行算不算合格的联想事件(文件与 SQLite 同一条判据) */
|
|
18
|
+
export function isValid(row: unknown): boolean
|
|
15
19
|
export const internals: { FILE: string }
|
package/lib/link-events.js
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
import { createLog, stamp } from './event-log.js'
|
|
5
5
|
import { before } from './as-of.js'
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// 一行算不算合格的联想事件。文件和 SQLite 两个容器用同一条判据,坏行的口径才一致
|
|
8
|
+
export const isValid = (r) => Boolean(r && r.from && r.to && r.reason)
|
|
9
|
+
const log = createLog('link-events.jsonl', { valid: isValid })
|
|
8
10
|
export const eventsFile = log.eventsFile
|
|
9
11
|
export const readAll = log.readAll
|
|
10
12
|
export const internals = { FILE: log.FILE }
|
|
@@ -22,9 +24,14 @@ export function link(memDir, { from, to, reason }, opts = {}) {
|
|
|
22
24
|
|
|
23
25
|
// 每条经历连着哪些:不分方向,两头都能看到;until 用于翻回那天
|
|
24
26
|
export function linksByExp(memDir, { until = null } = {}) {
|
|
27
|
+
return linksRows(readAll(memDir).rows, { until })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 同上,但输入是已经读出来的行(不碰文件):SQLite 后端走这里
|
|
31
|
+
export function linksRows(rows, { until = null } = {}) {
|
|
25
32
|
const map = new Map()
|
|
26
33
|
const push = (id, other, r) => map.set(id, [...(map.get(id) || []), { other, reason: r.reason, at: r.at, day: r.day }])
|
|
27
|
-
for (const r of
|
|
34
|
+
for (const r of rows) {
|
|
28
35
|
if (!before(r, until)) continue
|
|
29
36
|
push(r.from, r.to, r)
|
|
30
37
|
push(r.to, r.from, r)
|
package/lib/recall-events.d.ts
CHANGED
|
@@ -20,6 +20,11 @@ export function readAll(memDir: string): { rows: RecallEvent[]; bad: number }
|
|
|
20
20
|
/** until:只数这一刻(毫秒时间戳)之前(含)的想起 */
|
|
21
21
|
export function countByExp(memDir: string, opts?: { until?: number | null }): Map<string, { count: number; lastAt: string }>
|
|
22
22
|
|
|
23
|
+
/** 同 countByExp,但输入是已经读出的行(不碰文件、不走缓存) */
|
|
24
|
+
export function countRows(rows: RecallEvent[], opts?: { until?: number | null }): Map<string, { count: number; lastAt: string }>
|
|
25
|
+
/** 一行算不算合格的想起事件(文件与 SQLite 同一条判据) */
|
|
26
|
+
export function isValid(row: unknown): boolean
|
|
27
|
+
|
|
23
28
|
export function list(memDir: string, opts?: { id?: string; limit?: number }): RecallEvent[]
|
|
24
29
|
|
|
25
30
|
export const internals: { FILE: string }
|
package/lib/recall-events.js
CHANGED
|
@@ -49,20 +49,34 @@ export function append(memDir, { id, query = '', chatKey = '', backfilled = fals
|
|
|
49
49
|
|
|
50
50
|
export function readAll(memDir) {
|
|
51
51
|
let raw = ''
|
|
52
|
-
try {
|
|
52
|
+
try {
|
|
53
|
+
raw = fs.readFileSync(eventsFile(memDir), 'utf8')
|
|
54
|
+
} catch (error) {
|
|
55
|
+
// 只有"还没有这个文件"能当空;权限、是目录、坏盘必须报出来,不能当成 0 条。
|
|
56
|
+
if (error && error.code === 'ENOENT') return { rows: [], bad: 0 }
|
|
57
|
+
const wrapped = new Error('事件日志读不出来:recall-events.jsonl:' + String(error && error.message ? error.message : error))
|
|
58
|
+
wrapped.code = 'EVENTS_UNREADABLE'
|
|
59
|
+
wrapped.cause = error
|
|
60
|
+
throw wrapped
|
|
61
|
+
}
|
|
53
62
|
const rows = []
|
|
54
63
|
let bad = 0
|
|
55
64
|
for (const line of raw.split('\n')) {
|
|
56
65
|
if (!line.trim()) continue
|
|
57
66
|
try {
|
|
58
67
|
const r = JSON.parse(line)
|
|
59
|
-
if (r
|
|
68
|
+
if (isValid(r)) rows.push(r)
|
|
60
69
|
else bad += 1
|
|
61
70
|
} catch { bad += 1 }
|
|
62
71
|
}
|
|
63
72
|
return { rows, bad }
|
|
64
73
|
}
|
|
65
74
|
|
|
75
|
+
// 一行算不算合格的想起事件。文件和 SQLite 两个容器用同一条判据,坏行的口径才一致
|
|
76
|
+
export function isValid(r) {
|
|
77
|
+
return Boolean(r && r.id)
|
|
78
|
+
}
|
|
79
|
+
|
|
66
80
|
// 进程内缓存:同一进程里连续 recall 不再重复解析整个事件文件
|
|
67
81
|
const memo = new Map()
|
|
68
82
|
|
|
@@ -74,7 +88,13 @@ export function countByExp(memDir, { until = null } = {}) {
|
|
|
74
88
|
const hit = until === null ? memo.get(memDir) : null
|
|
75
89
|
if (hit && hit.stamp === stamp) return hit.map
|
|
76
90
|
|
|
77
|
-
const
|
|
91
|
+
const map = countRows(readAll(memDir).rows, { until })
|
|
92
|
+
if (until === null) memo.set(memDir, { stamp, map })
|
|
93
|
+
return map
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 同上,但输入是已经读出来的行(不碰文件、不走缓存):SQLite 后端走这里
|
|
97
|
+
export function countRows(rows, { until = null } = {}) {
|
|
78
98
|
const map = new Map()
|
|
79
99
|
for (const r of rows) {
|
|
80
100
|
if (!before(r, until)) continue
|
|
@@ -83,7 +103,6 @@ export function countByExp(memDir, { until = null } = {}) {
|
|
|
83
103
|
if (String(r.at || '') > cur.lastAt) cur.lastAt = String(r.at || '')
|
|
84
104
|
map.set(r.id, cur)
|
|
85
105
|
}
|
|
86
|
-
if (until === null) memo.set(memDir, { stamp, map })
|
|
87
106
|
return map
|
|
88
107
|
}
|
|
89
108
|
|
package/lib/scope-key.js
CHANGED
|
@@ -76,7 +76,7 @@ export function scopeKeysFor(chatKey) {
|
|
|
76
76
|
export function recordInScope(recordChatKey, queryChatKey) {
|
|
77
77
|
const record = String(recordChatKey || '').trim();
|
|
78
78
|
const query = String(queryChatKey || '').trim();
|
|
79
|
-
if (!query) return
|
|
79
|
+
if (!query) return !record; // 空查询只匹配没有会话键的记录,不再当“全局”
|
|
80
80
|
if (!record) return false;
|
|
81
81
|
return scopeKeysFor(query).includes(record);
|
|
82
82
|
}
|
package/lib/seal-events.d.ts
CHANGED
|
@@ -22,4 +22,8 @@ export function decide(memDir: string, opts: { id: string; action: SealAction; n
|
|
|
22
22
|
export function readAll(memDir: string): { rows: SealEvent[]; bad: number }
|
|
23
23
|
/** 现在(或截至 until)仍封存着的经历;strict 时坏行会抛 SEAL_EVENTS_CORRUPT */
|
|
24
24
|
export function sealedByExp(memDir: string, opts?: { until?: number | null; strict?: boolean }): Map<string, SealedInfo>
|
|
25
|
+
/** 同 sealedByExp,但输入是已经读出的 { rows, bad }(不碰文件);strict 时坏行同样抛 */
|
|
26
|
+
export function sealedFrom(read: { rows: SealEvent[]; bad: number }, opts?: { until?: number | null; strict?: boolean }): Map<string, SealedInfo>
|
|
27
|
+
/** 一行算不算合格的封存事件(文件与 SQLite 同一条判据) */
|
|
28
|
+
export function isValid(row: unknown): boolean
|
|
25
29
|
export const internals: { FILE: string }
|
package/lib/seal-events.js
CHANGED
|
@@ -72,18 +72,27 @@ export function readAll(memDir) {
|
|
|
72
72
|
if (!line.trim()) continue
|
|
73
73
|
try {
|
|
74
74
|
const r = JSON.parse(line)
|
|
75
|
-
if (
|
|
75
|
+
if (isValid(r)) rows.push(r)
|
|
76
76
|
else bad += 1
|
|
77
77
|
} catch { bad += 1 }
|
|
78
78
|
}
|
|
79
79
|
return { rows, bad }
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
// 一行算不算合格的封存事件。文件和 SQLite 两个容器用同一条判据,坏行的口径才一致
|
|
83
|
+
export function isValid(r) {
|
|
84
|
+
return Boolean(r && r.id && ACTIONS.has(r.action))
|
|
85
|
+
}
|
|
86
|
+
|
|
82
87
|
// 每个经历 id 现在是否封存着:只收最后一次是 seal 的;until 用于翻回那天(那时还没封存就不算)
|
|
83
88
|
// strict:展示路径用。日志里有坏行时"某条经历唯一一次封存"可能正好坏掉,
|
|
84
89
|
// 这时不能默认可见——把"状态未知"抛给调用方(fail closed)。
|
|
85
90
|
export function sealedByExp(memDir, { until = null, strict = false } = {}) {
|
|
86
|
-
|
|
91
|
+
return sealedFrom(readAll(memDir), { until, strict })
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 同上,但输入是已经读出来的 { rows, bad }(不碰文件):SQLite 后端走这里,坏行同样 fail closed
|
|
95
|
+
export function sealedFrom({ rows, bad }, { until = null, strict = false } = {}) {
|
|
87
96
|
if (strict && bad > 0) {
|
|
88
97
|
const error = new Error('封存日志有 ' + bad + ' 行无法解析,封存状态不确定')
|
|
89
98
|
error.code = 'SEAL_EVENTS_CORRUPT'
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export declare const FILE_NAME: string
|
|
2
|
+
export declare const DEFAULT_MAX_LINES: number
|
|
3
|
+
export declare const SOURCES: readonly string[]
|
|
4
|
+
|
|
5
|
+
export interface SelfActionSource {
|
|
6
|
+
asked: number
|
|
7
|
+
self: number
|
|
8
|
+
unknown: number
|
|
9
|
+
[key: string]: number
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SelfActionRow {
|
|
13
|
+
v: 1
|
|
14
|
+
at: number
|
|
15
|
+
day: string
|
|
16
|
+
action: string
|
|
17
|
+
source: 'asked' | 'self' | 'unknown'
|
|
18
|
+
outcome: string
|
|
19
|
+
itemRef?: string
|
|
20
|
+
triggerLen?: number
|
|
21
|
+
triggerDigest?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SelfActionOk { ok: true; [key: string]: unknown }
|
|
25
|
+
export interface SelfActionFailure {
|
|
26
|
+
ok: false
|
|
27
|
+
code: 'invalid_input' | 'limit' | 'busy' | 'storage_error'
|
|
28
|
+
[key: string]: unknown
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SelfActionLogOptions {
|
|
32
|
+
dir: string
|
|
33
|
+
actions?: string[]
|
|
34
|
+
maxLines?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface RecordSelfActionInput {
|
|
38
|
+
action: string
|
|
39
|
+
source?: 'asked' | 'self' | 'unknown'
|
|
40
|
+
outcome?: string
|
|
41
|
+
itemRef?: string
|
|
42
|
+
triggerLen?: number
|
|
43
|
+
triggerDigest?: string
|
|
44
|
+
at?: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface SelfActionLog {
|
|
48
|
+
record(input: RecordSelfActionInput): SelfActionOk | SelfActionFailure
|
|
49
|
+
read(input?: {
|
|
50
|
+
since?: number
|
|
51
|
+
until?: number
|
|
52
|
+
action?: string
|
|
53
|
+
source?: string
|
|
54
|
+
outcome?: string
|
|
55
|
+
limit?: number
|
|
56
|
+
}): { ok: true; total: number; rows: SelfActionRow[]; corrupt: number } | SelfActionFailure
|
|
57
|
+
summarize(input?: { now?: number; days?: number }): {
|
|
58
|
+
ok: true
|
|
59
|
+
windowDays: number
|
|
60
|
+
total: number
|
|
61
|
+
bySource: SelfActionSource
|
|
62
|
+
byOutcome: Record<string, number>
|
|
63
|
+
byAction: Record<string, number>
|
|
64
|
+
daily: Array<{ day: string; asked: number; self: number; unknown: number }>
|
|
65
|
+
corrupt: number
|
|
66
|
+
} | SelfActionFailure
|
|
67
|
+
file: string
|
|
68
|
+
maxLines: number
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export declare function createSelfActionLog(options: SelfActionLogOptions): SelfActionLog
|