cortico-world-memory-core 0.2.0 → 0.3.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/CHANGELOG.md +24 -0
- package/ENV_PROMPT.md +1 -1
- package/README.md +7 -4
- package/describe.json +4 -3
- package/package.json +72 -70
- package/src/config.ts +19 -4
- package/src/recall.ts +6 -2
- package/src/site.ts +1 -0
- package/src/store.ts +211 -16
- package/src/tools.ts +7 -2
- package/src/world.ts +151 -31
- package/webui/app.js +141 -44
- package/webui/style.css +7 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.1
|
|
4
|
+
|
|
5
|
+
- 简介末尾加 QQ 交流群 745879632(中英两版)。
|
|
6
|
+
|
|
7
|
+
## 0.3.0
|
|
8
|
+
|
|
9
|
+
- **图谱补上版本链**:被 `supersedes` 接替的旧卡会补进图(压暗、带标签),按「旧 → 新」画虚线箭头,图例注明「虚线箭头=被新版本接替」。之前这张图只有标签共现和「同一天」两种弱边,看不出记忆是怎么演进的——而版本链恰恰是这个插件最该被看见的关系。
|
|
10
|
+
- **搜索加防抖**:输入 150ms 后才重绘,不再每敲一个字就把原文 1000+ 条、表格几十行整体重建。
|
|
11
|
+
- **原文页分页**:先渲染最近 200 条,底部「还有 N 条 · 继续加载」;日期分组在只加载了一部分时显示「52 / 126 条」,不会让人以为那天只有这么几句。
|
|
12
|
+
- **「彻底删除」移出详情栏**:它原来紧挨「退休」,误触一次就不可逆。现在只能在维护面板 → 回收站里删,详情栏只留「退休 / 恢复」。
|
|
13
|
+
- 演示数据补了两组版本链,市场截图能体现新的图谱边。
|
|
14
|
+
|
|
15
|
+
## 0.2.1
|
|
16
|
+
|
|
17
|
+
- **修掉「对话原文一直是 0 行」**:默认 `captureSenders` 白名单一个真实 senderKey 都匹配不上(实际是 `private:1940008084`、`desktop-pet.chat`,旧正则写的是 `qq.` / `desktop-pet.message`)。新默认认 QQ 私聊、桌宠对话/语音、星邮私聊,群聊仍然不收;读到旧默认值会自动换成新的,不用手改设置。
|
|
18
|
+
- **原文过期不再直接删**:超过 `rawRetentionDays` 的行按天挪进 `data/memory-core/archive/YYYY-MM-DD.jsonl`,新增 `rawArchiveDays`(默认 90 天,0 = 永久)。模型用 `mcore_recall` 的 `mode=archive` 能翻回更早的对话。
|
|
19
|
+
- **归档不重复**:过期的新行直接进归档、不再留在 `turns.jsonl`;启动时按已归档的游标去重,重启不会把同一行写第二遍。
|
|
20
|
+
- **历史补录**:新增 `captureBackfill`,打开后重启一次会把水位倒回开头重扫宿主还留着的消息,补完自动停用;中途被打断会从断点接着跑。
|
|
21
|
+
- **补录分页修正**:宿主 `range` 的 `limit` 是「从区间末尾取」,之前拿它当分页,`from=0` 时直接返回全部历史的最后 400 条、水位一次跳到末尾,前面的补录全被跳过。改成分窗扫(`toCursor` 逐段推进),旧水位按版本号自动作废重来——真实数据从 55 行补到 1327 行。
|
|
22
|
+
- **诊断可见**:`state.json` 记录最近一次捕获的体检结果(扫了多少条、留下几条、被白名单挡掉的 key 样例),原文页没有数据时直接把样例显示出来。
|
|
23
|
+
- **召回打分**:标签单独加权(同一关键词命中标签比正文里顺带提到更靠前);`uses` 惩罚封顶 8 次,常用的重要记忆不会越用越沉。
|
|
24
|
+
- **疑似作废**加了老化候选:90 天没被召回、权重低于 0.45、未完成的承诺除外——仍然只是候选,要人点一下才退休。
|
|
25
|
+
- 提示词跟着加了一句 `mode=archive` 的用法(697 → 720 字符)。
|
|
26
|
+
|
|
3
27
|
## 0.2.0
|
|
4
28
|
|
|
5
29
|
- 补上扩展市场要的自描述与元数据:`describe.json`(工具、事件、环境提示词原文)、图标、四张演示截图,`cortico.locales` 中英简介。
|
package/ENV_PROMPT.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
|
|
12
12
|
写法:一条一件事,40–80 字,写清楚谁、什么时候、怎么了;不要写「对方说了一些事」这种空话。同一件事不要写两遍,记不清就先查。`weight` 是这事在你心里的分量,0.3 琐事 / 0.5 一般 / 0.8 重要。
|
|
13
13
|
|
|
14
|
-
查法:`mcore_recall` 不传 `query` 就是最近几条;传 `query` 搜内容,最近 7
|
|
14
|
+
查法:`mcore_recall` 不传 `query` 就是最近几条;传 `query` 搜内容,最近 7 天的对话原文和工作区里的笔记文件也会一起搜,更早的加 `mode=archive` 翻归档;`mode=open` 看没做完的承诺和今天的安排。
|
|
15
15
|
什么时候查:对方提到某个人、某件事、某个日期、旧约定,或者问「你还记得吗」——先查再答,不要硬猜。
|
|
16
16
|
|
|
17
17
|
你自己也有状态:`mcore_state` 管精力、心情、正在做什么、在哪、睡没睡;`mcore_plan` 管今天的安排。这两样和记忆分开放,不要写进 `mcore_write`。
|
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ pnpm add cortico-world-memory-core
|
|
|
30
30
|
| 工具 | 作用 |
|
|
31
31
|
|---|---|
|
|
32
32
|
| `mcore_write` | 写一条记忆。`kind` 五选一,可带 `weight` / `tags` / `dueAt`;传 `closes` 表示「这条替代了某条旧的」 |
|
|
33
|
-
| `mcore_recall` | 查记忆。不传 `query` 就是最近几条;传了就按内容搜,同时搜最近 7 天的对话原文和工作区笔记;`mode=open`
|
|
33
|
+
| `mcore_recall` | 查记忆。不传 `query` 就是最近几条;传了就按内容搜,同时搜最近 7 天的对话原文和工作区笔记;`mode=open` 看没做完的承诺和今天的安排;`mode=archive` 翻更早的归档原文 |
|
|
34
34
|
| `mcore_state` | 角色自己的精力 / 心情 / 正在做什么 / 在哪 / 睡没睡 |
|
|
35
35
|
| `mcore_plan` | 今天的日程(增、勾、清空) |
|
|
36
36
|
|
|
@@ -53,7 +53,7 @@ pnpm add cortico-world-memory-core
|
|
|
53
53
|
| 图谱 | 力导向图:节点大小=权重、颜色=类型、连线=共同标签或同一天;滚轮缩放、拖动、双击适应 |
|
|
54
54
|
| 时间线 | 按天分组,带权重条与截止时间 |
|
|
55
55
|
| 列表 | 可排序表格(类型 / 内容 / 权重 / 来源 / 标签 / 记于 / 使用次数),权重带微条 |
|
|
56
|
-
| 原文 | 最近 7
|
|
56
|
+
| 原文 | 最近 7 天的对话原文,搜索高亮;更早的进 `archive/`,模型可用 `mode=archive` 翻回去 |
|
|
57
57
|
| 文件 | 工作区里被检索的 md / txt(只读) |
|
|
58
58
|
| 维护 | 重复 / 疑似作废 / 垃圾 / 回收站,四项都先预览再执行 |
|
|
59
59
|
|
|
@@ -82,9 +82,11 @@ pnpm add cortico-world-memory-core
|
|
|
82
82
|
|---|---|---|
|
|
83
83
|
| `memoryEnabled` | `true` | 记忆总开关 |
|
|
84
84
|
| `captureTurns` | `true` | 存对方最近的对话原文,供「搜最近聊过什么」 |
|
|
85
|
-
| `captureSenders` | `^(
|
|
85
|
+
| `captureSenders` | `^(desktop-pet\.(chat\|message\|answer\|voice)\|private:\|qq\.private\.\|starpost\.private\.)` | 哪些来源算对话(正则匹配 senderKey)。真实 key 长这样:`private:1940008084`、`desktop-pet.chat`;群聊不算 |
|
|
86
86
|
| `captureMinLength` | `4` | 短于这个字数的消息不进原文库 |
|
|
87
|
-
| `rawRetentionDays` | `7` |
|
|
87
|
+
| `rawRetentionDays` | `7` | 热数据天数,超过就按天挪进 `archive/`(不再直接删) |
|
|
88
|
+
| `rawArchiveDays` | `90` | 归档保留天数,`0` = 永久 |
|
|
89
|
+
| `captureBackfill` | `false` | 打开后重启一次:把水位倒回开头重扫历史消息,补录完自动停用 |
|
|
88
90
|
| `recallLimit` | `5` | 一次召回给几条 |
|
|
89
91
|
| `recallMaxChars` | `600` | 召回结果总字数上限 |
|
|
90
92
|
| `proactiveEnabled` | `true` | 主动开口 |
|
|
@@ -100,6 +102,7 @@ pnpm add cortico-world-memory-core
|
|
|
100
102
|
data/memory-core/
|
|
101
103
|
├─ cards.jsonl 卡片(一行一张,可以直接看)
|
|
102
104
|
├─ turns.jsonl 对方最近的对话原文(按 captureSenders 过滤)
|
|
105
|
+
├─ archive/ 过期的原文按天归档(YYYY-MM-DD.jsonl),可搜可删
|
|
103
106
|
├─ plans.json 今天的日程
|
|
104
107
|
├─ state.json 精力/心情/活动/位置/主动计数
|
|
105
108
|
└─ imported.json 旧数据导入记录(删掉它会在下次启动重新导入)
|
package/describe.json
CHANGED
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
},
|
|
57
57
|
{
|
|
58
58
|
"name": "mcore_recall",
|
|
59
|
-
"description": "查记忆。不传 query 就是最近几条;传 query 按内容搜,最近 7 天的对话原文和工作区的笔记文件也会一起搜;mode=open
|
|
59
|
+
"description": "查记忆。不传 query 就是最近几条;传 query 按内容搜,最近 7 天的对话原文和工作区的笔记文件也会一起搜;mode=open 看还没做完的承诺与今天的安排;mode=archive 翻归档里的旧对话(要带 query)。对方提到人、事、日期、旧约定,或问「你还记得吗」时先查再答。",
|
|
60
60
|
"parameters": {
|
|
61
61
|
"type": "object",
|
|
62
62
|
"additionalProperties": false,
|
|
@@ -81,7 +81,8 @@
|
|
|
81
81
|
"enum": [
|
|
82
82
|
"search",
|
|
83
83
|
"recent",
|
|
84
|
-
"open"
|
|
84
|
+
"open",
|
|
85
|
+
"archive"
|
|
85
86
|
],
|
|
86
87
|
"description": "默认 search;没给 query 按 recent 处理"
|
|
87
88
|
},
|
|
@@ -257,7 +258,7 @@
|
|
|
257
258
|
"key": "worlds.memory-core.envPrompt",
|
|
258
259
|
"title": "记忆核心 · 环境提示词",
|
|
259
260
|
"role": "envPrompt",
|
|
260
|
-
"content": "【记忆核心】\n\n你现在只有一套记忆:`mcore_write` 写进去,`mcore_recall` 查出来。旧的几套记忆已经并进这里,不要再提「另一个记忆系统」,也不要问对方「要记在哪」。\n\n写什么:\n- `fact` 关于对方的稳定事实(名字、作息、忌讳、正在忙的事)。\n- `self` 关于你自己的(喜欢什么、怕什么、决定过什么)。\n- `episode` 发生过的一件事,带上当时的感觉。\n- `promise` 答应过的事,写 `dueAt`;做完或改了传 `closes`(查出来的行首 8 位就是 id)。\n- `note` 过几天就没用的临时信息。\n\n写法:一条一件事,40–80 字,写清楚谁、什么时候、怎么了;不要写「对方说了一些事」这种空话。同一件事不要写两遍,记不清就先查。`weight` 是这事在你心里的分量,0.3 琐事 / 0.5 一般 / 0.8 重要。\n\n查法:`mcore_recall` 不传 `query` 就是最近几条;传 `query` 搜内容,最近 7
|
|
261
|
+
"content": "【记忆核心】\n\n你现在只有一套记忆:`mcore_write` 写进去,`mcore_recall` 查出来。旧的几套记忆已经并进这里,不要再提「另一个记忆系统」,也不要问对方「要记在哪」。\n\n写什么:\n- `fact` 关于对方的稳定事实(名字、作息、忌讳、正在忙的事)。\n- `self` 关于你自己的(喜欢什么、怕什么、决定过什么)。\n- `episode` 发生过的一件事,带上当时的感觉。\n- `promise` 答应过的事,写 `dueAt`;做完或改了传 `closes`(查出来的行首 8 位就是 id)。\n- `note` 过几天就没用的临时信息。\n\n写法:一条一件事,40–80 字,写清楚谁、什么时候、怎么了;不要写「对方说了一些事」这种空话。同一件事不要写两遍,记不清就先查。`weight` 是这事在你心里的分量,0.3 琐事 / 0.5 一般 / 0.8 重要。\n\n查法:`mcore_recall` 不传 `query` 就是最近几条;传 `query` 搜内容,最近 7 天的对话原文和工作区里的笔记文件也会一起搜,更早的加 `mode=archive` 翻归档;`mode=open` 看没做完的承诺和今天的安排。\n什么时候查:对方提到某个人、某件事、某个日期、旧约定,或者问「你还记得吗」——先查再答,不要硬猜。\n\n你自己也有状态:`mcore_state` 管精力、心情、正在做什么、在哪、睡没睡;`mcore_plan` 管今天的安排。这两样和记忆分开放,不要写进 `mcore_write`。\n\n分寸:记忆是你记得的东西,不是拿来念的清单。可以自然地说「我记得你说过……」;不确定的就直接问,别编。"
|
|
261
262
|
}
|
|
262
263
|
]
|
|
263
264
|
}
|
package/package.json
CHANGED
|
@@ -1,70 +1,72 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "cortico-world-memory-core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Cortico World 长期记忆:一张卡片表 + 4 个工具 + 一段静态提示词,附状态、日程、主动开口与本地记忆地图。",
|
|
6
|
-
"keywords": [
|
|
7
|
-
"cortico-world",
|
|
8
|
-
"cortico",
|
|
9
|
-
"coopanion",
|
|
10
|
-
"memory",
|
|
11
|
-
"companion"
|
|
12
|
-
],
|
|
13
|
-
"author": "lutsing",
|
|
14
|
-
"license": "MIT",
|
|
15
|
-
"repository": {
|
|
16
|
-
"type": "git",
|
|
17
|
-
"url": "git+https://github.com/liqin1018/cortico-world-memory-core.git"
|
|
18
|
-
},
|
|
19
|
-
"homepage": "https://github.com/liqin1018/cortico-world-memory-core#readme",
|
|
20
|
-
"bugs": {
|
|
21
|
-
"url": "https://github.com/liqin1018/cortico-world-memory-core/issues"
|
|
22
|
-
},
|
|
23
|
-
"engines": {
|
|
24
|
-
"node": ">=22"
|
|
25
|
-
},
|
|
26
|
-
"main": "./src/index.ts",
|
|
27
|
-
"cortico": {
|
|
28
|
-
"kind": "world",
|
|
29
|
-
"api": 6,
|
|
30
|
-
"displayName": "记忆核心",
|
|
31
|
-
"locales": {
|
|
32
|
-
"zh": {
|
|
33
|
-
"displayName": "记忆核心",
|
|
34
|
-
"description": "一张卡片表、四个工具、一段
|
|
35
|
-
},
|
|
36
|
-
"en": {
|
|
37
|
-
"displayName": "Memory Core",
|
|
38
|
-
"description": "One card table, four tools and a
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
"tags": [
|
|
42
|
-
"memory",
|
|
43
|
-
"companion",
|
|
44
|
-
"internal-state",
|
|
45
|
-
"experimental"
|
|
46
|
-
],
|
|
47
|
-
"describe": "./describe.json",
|
|
48
|
-
"icon": "./icon.svg",
|
|
49
|
-
"screenshots": [
|
|
50
|
-
"assets/screenshots/graph.png",
|
|
51
|
-
"assets/screenshots/table.png",
|
|
52
|
-
"assets/screenshots/timeline.png",
|
|
53
|
-
"assets/screenshots/maintenance.png"
|
|
54
|
-
]
|
|
55
|
-
},
|
|
56
|
-
"files": [
|
|
57
|
-
"src",
|
|
58
|
-
"webui",
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "cortico-world-memory-core",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Cortico World 长期记忆:一张卡片表 + 4 个工具 + 一段静态提示词,附状态、日程、主动开口与本地记忆地图。",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"cortico-world",
|
|
8
|
+
"cortico",
|
|
9
|
+
"coopanion",
|
|
10
|
+
"memory",
|
|
11
|
+
"companion"
|
|
12
|
+
],
|
|
13
|
+
"author": "lutsing",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/liqin1018/cortico-world-memory-core.git"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/liqin1018/cortico-world-memory-core#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/liqin1018/cortico-world-memory-core/issues"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22"
|
|
25
|
+
},
|
|
26
|
+
"main": "./src/index.ts",
|
|
27
|
+
"cortico": {
|
|
28
|
+
"kind": "world",
|
|
29
|
+
"api": 6,
|
|
30
|
+
"displayName": "记忆核心",
|
|
31
|
+
"locales": {
|
|
32
|
+
"zh": {
|
|
33
|
+
"displayName": "记忆核心",
|
|
34
|
+
"description": "一张卡片表、四个工具、一段 720 字的静态提示词。改口不覆盖:旧卡退休、新卡记下接替了谁,召回时不会再同时看到作废的和改后的那条。附带状态、日程、主动开口,以及一个本地记忆地图(图谱 / 时间线 / 维护面板)。有问题或想聊两句:QQ 群 745879632。"
|
|
35
|
+
},
|
|
36
|
+
"en": {
|
|
37
|
+
"displayName": "Memory Core",
|
|
38
|
+
"description": "One card table, four tools and a 720-character static prompt. Corrections don't overwrite: the old card is retired and the new one records what it supersedes, so a retracted plan never comes back alongside the revised one. Ships mood/energy state, today's plan, speaking first, and a local memory map with graph, timeline and a maintenance panel. Questions or just want to chat: QQ group 745879632."
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"tags": [
|
|
42
|
+
"memory",
|
|
43
|
+
"companion",
|
|
44
|
+
"internal-state",
|
|
45
|
+
"experimental"
|
|
46
|
+
],
|
|
47
|
+
"describe": "./describe.json",
|
|
48
|
+
"icon": "./icon.svg",
|
|
49
|
+
"screenshots": [
|
|
50
|
+
"assets/screenshots/graph.png",
|
|
51
|
+
"assets/screenshots/table.png",
|
|
52
|
+
"assets/screenshots/timeline.png",
|
|
53
|
+
"assets/screenshots/maintenance.png"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"src",
|
|
58
|
+
"webui/app.js",
|
|
59
|
+
"webui/index.html",
|
|
60
|
+
"webui/style.css",
|
|
61
|
+
"assets",
|
|
62
|
+
"ENV_PROMPT.md",
|
|
63
|
+
"describe.json",
|
|
64
|
+
"icon.svg",
|
|
65
|
+
"CHANGELOG.md",
|
|
66
|
+
"README.md",
|
|
67
|
+
"LICENSE"
|
|
68
|
+
],
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -34,8 +34,12 @@ export interface MemoryCoreConfig {
|
|
|
34
34
|
captureSenders: string;
|
|
35
35
|
/** 短于这么多字的对话原文不存(过滤表情、单字、口令回执)。 */
|
|
36
36
|
captureMinLength: number;
|
|
37
|
-
/**
|
|
37
|
+
/** 对话原文保留天数(热数据,超过就按天归档)。 */
|
|
38
38
|
rawRetentionDays: number;
|
|
39
|
+
/** 归档保留天数(0 = 永久)。 */
|
|
40
|
+
rawArchiveDays: number;
|
|
41
|
+
/** 启动后把历史事件重扫一遍补录(只做一次)。 */
|
|
42
|
+
captureBackfill: boolean;
|
|
39
43
|
/** 一次召回最多给几条。 */
|
|
40
44
|
recallLimit: number;
|
|
41
45
|
/** 召回结果的总字数上限。 */
|
|
@@ -78,9 +82,13 @@ export const MEMORY_CORE_DEFAULTS: MemoryCoreConfig = {
|
|
|
78
82
|
sleepEnd: '08:00',
|
|
79
83
|
memoryEnabled: true,
|
|
80
84
|
captureTurns: true,
|
|
81
|
-
|
|
85
|
+
// 真实 senderKey 长这样:private:1940008084(QQ 私聊)、desktop-pet.chat(桌宠对话)。
|
|
86
|
+
// 老默认值一个都匹配不上,导致原文库一直是 0 行 —— 改这个 regex 前先看一遍 data/runs/*/events.jsonl。
|
|
87
|
+
captureSenders: '^(desktop-pet\\.(chat|message|answer|voice)|private:|qq\\.private\\.|starpost\\.private\\.)',
|
|
82
88
|
captureMinLength: 4,
|
|
83
89
|
rawRetentionDays: 7,
|
|
90
|
+
rawArchiveDays: 90,
|
|
91
|
+
captureBackfill: false,
|
|
84
92
|
recallLimit: 5,
|
|
85
93
|
recallMaxChars: 600,
|
|
86
94
|
contextEveryMin: 0,
|
|
@@ -170,11 +178,18 @@ export const MEMORY_CORE_CONFIG_GROUP: ConfigGroup = {
|
|
|
170
178
|
'worlds.memory-core.captureSenders': {
|
|
171
179
|
type: 'string',
|
|
172
180
|
title: '算作对话的来源',
|
|
173
|
-
description: '正则匹配事件的 senderKey
|
|
181
|
+
description: '正则匹配事件的 senderKey。默认认 QQ 私聊(private:数字)、桌宠对话(desktop-pet.chat / voice)、星邮私聊;群聊不算。填错了一个字都录不到,改完看「对话原文」页有没有进新行。',
|
|
174
182
|
'x-hot': true,
|
|
175
183
|
},
|
|
176
184
|
'worlds.memory-core.captureMinLength': num('原文最短几个字', '短于这个长度的消息不进原文库(表情、单字、口令回执照样过滤)。', 1, 20, '字'),
|
|
177
|
-
'worlds.memory-core.rawRetentionDays': num('
|
|
185
|
+
'worlds.memory-core.rawRetentionDays': num('原文热数据天数', '超过这么多天的对话会挪进 archive/ 按天归档,不再直接删。', 1, 90, '天'),
|
|
186
|
+
'worlds.memory-core.rawArchiveDays': num('归档保留天数', '归档按天存,超过这么多天的文件在启动时删掉。0 = 永久保留(占盘,自己看着办)。', 0, 3650, '天'),
|
|
187
|
+
'worlds.memory-core.captureBackfill': {
|
|
188
|
+
type: 'boolean',
|
|
189
|
+
title: '重扫历史补录',
|
|
190
|
+
description: '打开后重启一次:把水位倒回开头,把宿主还留着的历史消息重扫一遍。只做一次,做完自动关掉自己。',
|
|
191
|
+
'x-hot': false,
|
|
192
|
+
},
|
|
178
193
|
'worlds.memory-core.recallLimit': num('一次召回给几条', '默认 5 条。给多了挤占上下文。', 1, 12, '条'),
|
|
179
194
|
'worlds.memory-core.recallMaxChars': num('召回结果字数上限', '超过就截断,默认 600 字。', 120, 2000, '字'),
|
|
180
195
|
'worlds.memory-core.contextEveryMin': num('状态投递间隔', '每隔这么多分钟把「我在做什么」推到上下文里。0 = 不推(默认,最省);推一次约 60 token,但每轮都在历史里。', 0, 240, '分钟'),
|
package/src/recall.ts
CHANGED
|
@@ -81,8 +81,12 @@ export function rankCards(cards: readonly Card[], opts: RankOptions): Card[] {
|
|
|
81
81
|
return pool.slice(0, opts.limit);
|
|
82
82
|
}
|
|
83
83
|
const scored = pool.map((card) => {
|
|
84
|
-
const lexical = overlap(queryTokens,
|
|
85
|
-
|
|
84
|
+
const lexical = overlap(queryTokens, card.text);
|
|
85
|
+
// 标签单独算一次:对方说「生日」时,打了 #生日 的卡要比正文里顺嘴提到的排得靠前
|
|
86
|
+
const tagHit = card.tags.length ? overlap(queryTokens, card.tags.join(' ')) : 0;
|
|
87
|
+
// 同一张卡被反复召回只做有限的边际压制(封顶 8 次),别让常用的重要记忆越用越沉
|
|
88
|
+
const used = Math.min(card.uses || 0, 8) * 0.03;
|
|
89
|
+
const score = lexical * 2 + tagHit * 1.2 + card.weight * 0.5 + freshness(card.createdAt, now) - used;
|
|
86
90
|
return { card, score };
|
|
87
91
|
});
|
|
88
92
|
scored.sort((a, b) => b.score - a.score);
|
package/src/site.ts
CHANGED
package/src/store.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* 卡片表、对话原文、日程与状态的文件层。
|
|
3
3
|
* 只碰本插件自己的数据目录;四套旧记忆由 importer.ts 只读导入,不改原文件。
|
|
4
4
|
*/
|
|
5
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync } from 'node:fs';
|
|
5
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, unlinkSync, writeFileSync, appendFileSync } from 'node:fs';
|
|
6
6
|
import { randomUUID } from 'node:crypto';
|
|
7
7
|
import { join } from 'node:path';
|
|
8
8
|
|
|
@@ -68,6 +68,22 @@ export interface CoreState {
|
|
|
68
68
|
proactiveCount?: number;
|
|
69
69
|
lastNightlyDay?: string;
|
|
70
70
|
rawWatermark?: number;
|
|
71
|
+
/** 水位是按哪一版捕获逻辑推进的。旧版用了错的 range 语义,水位不可信,见 CAPTURE_WATERMARK_VERSION。 */
|
|
72
|
+
watermarkVersion?: number;
|
|
73
|
+
/** 历史原文回放只做一次,做完记下时间。 */
|
|
74
|
+
backfillDoneAt?: string;
|
|
75
|
+
/** 回放开始时间:只在开始那一刻倒水位,中断后下次接着跑,不重复从头扫。 */
|
|
76
|
+
backfillStartedAt?: string;
|
|
77
|
+
/** 上一次捕获的体检结果:扫了多少、留下多少、被白名单挡掉的 key 样例。 */
|
|
78
|
+
captureDebug?: {
|
|
79
|
+
at: string;
|
|
80
|
+
scanned: number;
|
|
81
|
+
kept: number;
|
|
82
|
+
keys: string[];
|
|
83
|
+
/** 回放没跑完时,本轮从哪扫到哪,方便看进度。 */
|
|
84
|
+
backfillFrom?: number;
|
|
85
|
+
backfillTo?: number;
|
|
86
|
+
};
|
|
71
87
|
}
|
|
72
88
|
|
|
73
89
|
export function ensureDir(dir: string): void {
|
|
@@ -396,12 +412,31 @@ export class CardStore {
|
|
|
396
412
|
return groups.sort((x, y) => y.score - x.score);
|
|
397
413
|
}
|
|
398
414
|
|
|
399
|
-
/**
|
|
400
|
-
|
|
415
|
+
/**
|
|
416
|
+
* 疑似作废:一类是文案里自己写了「取消了 / 改到 / 作废」,
|
|
417
|
+
* 另一类是放旧了没人再碰——90 天没被召回、从没带过权重、权重还低。
|
|
418
|
+
* 两类都只是候选,仍然要人点一下才退休。
|
|
419
|
+
*/
|
|
420
|
+
stale(limit = 50, now = Date.now()): Card[] {
|
|
401
421
|
const re = /取消|黄了|作废|不去了|改成|改到|算了|不用了/;
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
422
|
+
const hit: Card[] = [];
|
|
423
|
+
const aged: Card[] = [];
|
|
424
|
+
for (const c of this.active()) {
|
|
425
|
+
if (re.test(c.text)) {
|
|
426
|
+
hit.push(c);
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
const born = Date.parse(c.createdAt);
|
|
430
|
+
if (!Number.isFinite(born)) continue;
|
|
431
|
+
const last = Date.parse(c.lastUsedAt ?? '') || born;
|
|
432
|
+
const quietDays = (now - last) / 86_400_000;
|
|
433
|
+
if (quietDays < 90) continue;
|
|
434
|
+
if ((c.uses || 0) > 1) continue;
|
|
435
|
+
if (c.weight > 0.45) continue;
|
|
436
|
+
if (c.kind === 'promise' && !c.done) continue;
|
|
437
|
+
aged.push(c);
|
|
438
|
+
}
|
|
439
|
+
return [...hit.sort((a, b) => b.createdAt.localeCompare(a.createdAt)), ...aged]
|
|
405
440
|
.slice(0, limit);
|
|
406
441
|
}
|
|
407
442
|
|
|
@@ -492,16 +527,31 @@ export class CardStore {
|
|
|
492
527
|
}
|
|
493
528
|
}
|
|
494
529
|
|
|
530
|
+
const DAY_FMT = new Intl.DateTimeFormat('sv-SE', { year: 'numeric', month: '2-digit', day: '2-digit' });
|
|
531
|
+
|
|
532
|
+
/** 事件属于哪一天(本地时区),归档文件按这个命名。 */
|
|
533
|
+
function dayKeyOf(iso: string | undefined): string {
|
|
534
|
+
const at = Date.parse(iso ?? '');
|
|
535
|
+
if (!Number.isFinite(at)) return '';
|
|
536
|
+
return DAY_FMT.format(new Date(at));
|
|
537
|
+
}
|
|
538
|
+
|
|
495
539
|
export class RawLog {
|
|
496
540
|
private entries: RawEntry[] = [];
|
|
497
541
|
private readonly file: string;
|
|
542
|
+
private readonly archiveDir: string;
|
|
498
543
|
private readonly retentionDays: number;
|
|
544
|
+
private readonly archiveDays: number;
|
|
499
545
|
|
|
500
|
-
constructor(dir: string, retentionDays: number) {
|
|
546
|
+
constructor(dir: string, retentionDays: number, archiveDays = 90) {
|
|
501
547
|
this.retentionDays = Math.max(1, Number(retentionDays) || 7);
|
|
548
|
+
this.archiveDays = Math.max(0, Number(archiveDays) || 0);
|
|
502
549
|
ensureDir(dir);
|
|
503
550
|
this.file = join(dir, 'turns.jsonl');
|
|
551
|
+
this.archiveDir = join(dir, 'archive');
|
|
552
|
+
ensureDir(this.archiveDir);
|
|
504
553
|
this.load();
|
|
554
|
+
this.purgeArchive();
|
|
505
555
|
}
|
|
506
556
|
|
|
507
557
|
private load(): void {
|
|
@@ -509,23 +559,140 @@ export class RawLog {
|
|
|
509
559
|
if (!existsSync(this.file)) return;
|
|
510
560
|
const cutoff = Date.now() - this.retentionDays * 86_400_000;
|
|
511
561
|
const kept: RawEntry[] = [];
|
|
562
|
+
const expired: RawEntry[] = [];
|
|
512
563
|
for (const line of readFileSync(this.file, 'utf8').split('\n')) {
|
|
513
564
|
if (!line.trim()) continue;
|
|
514
565
|
try {
|
|
515
566
|
const e = JSON.parse(line) as RawEntry;
|
|
516
567
|
if (!e || typeof e.text !== 'string') continue;
|
|
517
|
-
|
|
568
|
+
const at = Date.parse(e.ts);
|
|
569
|
+
if (!Number.isFinite(at)) continue;
|
|
570
|
+
(at >= cutoff ? kept : expired).push(e);
|
|
518
571
|
} catch {
|
|
519
572
|
// 跳过坏行
|
|
520
573
|
}
|
|
521
574
|
}
|
|
522
575
|
this.entries = kept;
|
|
576
|
+
// 文件可能是「老版本写的最后几行 + 补录回来的开头几行」拼起来的,按 cursor 归一,
|
|
577
|
+
// 不然原文页会按数组顺序显示成 8:25 → 10:32 → 8:24 这种乱序
|
|
578
|
+
this.sortEntries();
|
|
579
|
+
// 过期的挪进按天归档,不再直接抹掉:要翻旧日子用 mode=archive
|
|
580
|
+
// 传已归档游标:启动时文件里可能还留着上次归档过的行(老版本写的),别再写一遍
|
|
581
|
+
if (expired.length) this.archive(expired, this.archivedCursors());
|
|
523
582
|
this.rewrite();
|
|
524
583
|
} catch {
|
|
525
584
|
this.entries = [];
|
|
526
585
|
}
|
|
527
586
|
}
|
|
528
587
|
|
|
588
|
+
/** 归档里已经有过的游标。只在启动时算一次,避免重启后把同一行反复写进归档。 */
|
|
589
|
+
private archivedCursors(): Set<number> {
|
|
590
|
+
const set = new Set<number>();
|
|
591
|
+
for (const { file } of this.archiveFiles()) {
|
|
592
|
+
try {
|
|
593
|
+
for (const line of readFileSync(file, 'utf8').split('\n')) {
|
|
594
|
+
if (!line.trim()) continue;
|
|
595
|
+
try {
|
|
596
|
+
const e = JSON.parse(line) as RawEntry;
|
|
597
|
+
if (typeof e.cursor === 'number') set.add(e.cursor);
|
|
598
|
+
} catch {
|
|
599
|
+
// 跳过坏行
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
} catch {
|
|
603
|
+
// 读不出就当作没归档过
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return set;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** 按天追加到 archive/YYYY-MM-DD.jsonl,只写不读,读的时候再按需扫。 */
|
|
610
|
+
private archive(items: readonly RawEntry[], seen?: Set<number>): void {
|
|
611
|
+
const buckets = new Map<string, string[]>();
|
|
612
|
+
for (const e of items) {
|
|
613
|
+
if (seen?.has(e.cursor)) continue;
|
|
614
|
+
const day = dayKeyOf(e.ts);
|
|
615
|
+
if (!day) continue;
|
|
616
|
+
const line = JSON.stringify(e);
|
|
617
|
+
const list = buckets.get(day);
|
|
618
|
+
if (list) list.push(line);
|
|
619
|
+
else buckets.set(day, [line]);
|
|
620
|
+
}
|
|
621
|
+
for (const [day, lines] of buckets) {
|
|
622
|
+
try {
|
|
623
|
+
appendFileSync(join(this.archiveDir, `${day}.jsonl`), `${lines.join('\n')}\n`, 'utf8');
|
|
624
|
+
} catch {
|
|
625
|
+
// 归档失败不影响热数据
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
private archiveFiles(desc = false): Array<{ day: string; file: string }> {
|
|
631
|
+
try {
|
|
632
|
+
const names = readdirSync(this.archiveDir)
|
|
633
|
+
.filter((n) => /^\d{4}-\d{2}-\d{2}\.jsonl$/.test(n))
|
|
634
|
+
.sort();
|
|
635
|
+
if (desc) names.reverse();
|
|
636
|
+
return names.map((n) => ({ day: n.slice(0, 10), file: join(this.archiveDir, n) }));
|
|
637
|
+
} catch {
|
|
638
|
+
return [];
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/** 归档保留天数,0 = 永久。 */
|
|
643
|
+
private purgeArchive(): void {
|
|
644
|
+
if (!this.archiveDays) return;
|
|
645
|
+
const cutoff = dayKeyOf(new Date(Date.now() - this.archiveDays * 86_400_000).toISOString());
|
|
646
|
+
for (const { day, file } of this.archiveFiles()) {
|
|
647
|
+
if (day >= cutoff) continue;
|
|
648
|
+
try {
|
|
649
|
+
unlinkSync(file);
|
|
650
|
+
} catch {
|
|
651
|
+
// ignore
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
/** 归档概况,给界面显示。 */
|
|
657
|
+
stats(): { days: number; bytes: number; oldest: string } {
|
|
658
|
+
const files = this.archiveFiles();
|
|
659
|
+
let bytes = 0;
|
|
660
|
+
for (const f of files) {
|
|
661
|
+
try {
|
|
662
|
+
bytes += statSync(f.file).size;
|
|
663
|
+
} catch {
|
|
664
|
+
// ignore
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return { days: files.length, bytes, oldest: files[0]?.day ?? '' };
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** 翻归档:默认最近 30 天,最多 8000 行。只给 mode=archive 用,不进常规召回。 */
|
|
671
|
+
readArchive(days = 30, maxEntries = 8000): RawEntry[] {
|
|
672
|
+
const span = Math.max(1, Math.min(365, Number(days) || 30));
|
|
673
|
+
const fromDay = dayKeyOf(new Date(Date.now() - span * 86_400_000).toISOString());
|
|
674
|
+
const out: RawEntry[] = [];
|
|
675
|
+
for (const { day, file } of this.archiveFiles(true)) {
|
|
676
|
+
if (out.length >= maxEntries) break;
|
|
677
|
+
if (day < fromDay) continue;
|
|
678
|
+
try {
|
|
679
|
+
for (const line of readFileSync(file, 'utf8').split('\n')) {
|
|
680
|
+
if (!line.trim()) continue;
|
|
681
|
+
try {
|
|
682
|
+
const e = JSON.parse(line) as RawEntry;
|
|
683
|
+
if (e && typeof e.text === 'string') out.push(e);
|
|
684
|
+
} catch {
|
|
685
|
+
// 跳过坏行
|
|
686
|
+
}
|
|
687
|
+
if (out.length >= maxEntries) break;
|
|
688
|
+
}
|
|
689
|
+
} catch {
|
|
690
|
+
// ignore
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return out.sort((a, b) => a.cursor - b.cursor);
|
|
694
|
+
}
|
|
695
|
+
|
|
529
696
|
private rewrite(): void {
|
|
530
697
|
try {
|
|
531
698
|
writeFileSync(this.file, this.entries.map((e) => JSON.stringify(e)).join('\n') + '\n', 'utf8');
|
|
@@ -551,19 +718,47 @@ export class RawLog {
|
|
|
551
718
|
this.rewrite();
|
|
552
719
|
}
|
|
553
720
|
|
|
721
|
+
/** cursor 是宿主分配的单调节,拿它排最可靠;ts 在补录数据里不一定跟 cursor 同序。 */
|
|
722
|
+
private sortEntries(): void {
|
|
723
|
+
this.entries.sort((a, b) => a.cursor - b.cursor);
|
|
724
|
+
}
|
|
725
|
+
|
|
554
726
|
add(items: RawEntry[]): number {
|
|
555
727
|
if (!items.length) return 0;
|
|
556
|
-
|
|
728
|
+
// 回放时一次几千行,用 Set 判重,别拿 some() 平方级扫
|
|
729
|
+
const have = new Set(this.entries.map((e) => e.cursor));
|
|
730
|
+
const fresh = items.filter((item) => !have.has(item.cursor));
|
|
557
731
|
if (!fresh.length) return 0;
|
|
558
732
|
this.entries.push(...fresh);
|
|
559
733
|
const cutoff = Date.now() - this.retentionDays * 86_400_000;
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
734
|
+
const keep: RawEntry[] = [];
|
|
735
|
+
const expired: RawEntry[] = [];
|
|
736
|
+
for (const e of this.entries) {
|
|
737
|
+
const at = Date.parse(e.ts);
|
|
738
|
+
if (!Number.isFinite(at) || at < cutoff) expired.push(e);
|
|
739
|
+
else keep.push(e);
|
|
740
|
+
}
|
|
741
|
+
// 超过 4000 行的老行也归档,不丢
|
|
742
|
+
if (keep.length > 4000) {
|
|
743
|
+
const over = keep.length - 4000;
|
|
744
|
+
expired.push(...keep.slice(0, over));
|
|
745
|
+
this.entries = keep.slice(over);
|
|
746
|
+
} else {
|
|
747
|
+
this.entries = keep;
|
|
748
|
+
}
|
|
749
|
+
if (expired.length) this.archive(expired);
|
|
750
|
+
this.sortEntries();
|
|
751
|
+
// 只把「还在热数据里」的新行写文件。过期的新行已经进归档了,留在 turns.jsonl 里
|
|
752
|
+
// 会让下次启动再归档一遍(回放时会重复写)。
|
|
753
|
+
const hot = new Set(this.entries.map((e) => e.cursor));
|
|
754
|
+
const writable = fresh.filter((e) => hot.has(e.cursor));
|
|
755
|
+
if (writable.length) {
|
|
756
|
+
try {
|
|
757
|
+
appendFileSync(this.file, writable.map((e) => JSON.stringify(e)).join('\n') + '\n', 'utf8');
|
|
758
|
+
} catch {
|
|
759
|
+
// ignore
|
|
760
|
+
}
|
|
566
761
|
}
|
|
567
|
-
return
|
|
762
|
+
return writable.length;
|
|
568
763
|
}
|
|
569
764
|
}
|
package/src/tools.ts
CHANGED
|
@@ -47,12 +47,17 @@ export const TOOL_SPECS: ToolSpec[] = [
|
|
|
47
47
|
name: 'mcore_recall',
|
|
48
48
|
description:
|
|
49
49
|
'查记忆。不传 query 就是最近几条;传 query 按内容搜,最近 7 天的对话原文和工作区的笔记文件也会一起搜;'
|
|
50
|
-
+ 'mode=open
|
|
50
|
+
+ 'mode=open 看还没做完的承诺与今天的安排;mode=archive 翻归档里的旧对话(要带 query)。'
|
|
51
|
+
+ '对方提到人、事、日期、旧约定,或问「你还记得吗」时先查再答。',
|
|
51
52
|
parameters: obj(
|
|
52
53
|
{
|
|
53
54
|
query: { type: 'string', description: '要查的内容,几个关键词就够' },
|
|
54
55
|
kind: { type: 'string', enum: ['fact', 'self', 'episode', 'promise', 'note'], description: '只看某一类' },
|
|
55
|
-
mode: {
|
|
56
|
+
mode: {
|
|
57
|
+
type: 'string',
|
|
58
|
+
enum: ['search', 'recent', 'open', 'archive'],
|
|
59
|
+
description: '默认 search;没给 query 按 recent 处理',
|
|
60
|
+
},
|
|
56
61
|
days: { type: 'integer', minimum: 1, maximum: 3650, description: '只看最近这么多天' },
|
|
57
62
|
limit: { type: 'integer', minimum: 1, maximum: 12, description: '最多几条,默认 5' },
|
|
58
63
|
},
|
package/src/world.ts
CHANGED
|
@@ -13,7 +13,7 @@ import { join } from 'node:path';
|
|
|
13
13
|
import { randomUUID } from 'node:crypto';
|
|
14
14
|
import type { ToolDef, World, WorldHost, WorldWebUiDecl } from 'cortico/core/types.ts';
|
|
15
15
|
import type { WorldContext } from 'cortico/world.ts';
|
|
16
|
-
import { MEMORY_CORE_CONFIG_GROUP, type MemoryCoreConfig } from './config.ts';
|
|
16
|
+
import { MEMORY_CORE_CONFIG_GROUP, MEMORY_CORE_DEFAULTS, type MemoryCoreConfig } from './config.ts';
|
|
17
17
|
import { TOOL_SPECS } from './tools.ts';
|
|
18
18
|
import {
|
|
19
19
|
CARD_KINDS,
|
|
@@ -38,6 +38,16 @@ const ENV_PROMPT_FILE = fileURLToPath(new URL('../ENV_PROMPT.md', import.meta.ur
|
|
|
38
38
|
const WEB_DIR = fileURLToPath(new URL('../webui/', import.meta.url));
|
|
39
39
|
const TICK_MS = 60_000;
|
|
40
40
|
|
|
41
|
+
/**
|
|
42
|
+
* 捕获水位的可信版本号。
|
|
43
|
+
* v1 用 `range({fromCursor, limit})` 分页,但宿主的 limit 是「从区间末尾取」,
|
|
44
|
+
* 一次就把水位推到历史末尾、前面的全跳过。升到 v2 后旧水位作废,自动重放一遍。
|
|
45
|
+
*/
|
|
46
|
+
const CAPTURE_WATERMARK_VERSION = 2;
|
|
47
|
+
/** 一次 range 的窗口宽度。cursor 是全局连续的,按窗口边界推进,窗口内没有匹配也能往前走。
|
|
48
|
+
* 取大一点:宿主每次 range 都要遍历一遍分片,窗口太碎会让回放变成几百次全表扫描。 */
|
|
49
|
+
const CAPTURE_BATCH = 2_000;
|
|
50
|
+
|
|
41
51
|
function clip(text: string, limit: number): string {
|
|
42
52
|
const s = String(text ?? '').replace(/\s+/g, ' ').trim();
|
|
43
53
|
return s.length > limit ? `${s.slice(0, limit)}…` : s;
|
|
@@ -53,22 +63,46 @@ function str(value: unknown, limit = 200): string {
|
|
|
53
63
|
return typeof value === 'string' ? clip(value, limit) : '';
|
|
54
64
|
}
|
|
55
65
|
|
|
56
|
-
/**
|
|
66
|
+
/**
|
|
67
|
+
* 事件正文外层常裹着东西,剥干净的才是对方说的话:
|
|
68
|
+
* - `[星邮消息] {"text":"..."}` —— JSON 壳;
|
|
69
|
+
* - `[11-03 周二 18:04] [typed] 月:在吗` —— 宿主加的抬头(时间 / 输入方式 / 说话人);
|
|
70
|
+
* - `【QQ私聊 22:37 月(QQ:1940008084)】:#-479828398 有点` —— QQ 那层壳 + 消息号;
|
|
71
|
+
* - `[23:34] [answer] 月回答「…」:选了第 1 项「…」` —— 选项卡的答案,只留选了什么。
|
|
72
|
+
*/
|
|
57
73
|
function extractUserText(raw: string): string {
|
|
58
|
-
|
|
59
|
-
const m = /^\[[^\]]{0,12}消息\]\s*(\{[\s\S]*\})$/.exec(text
|
|
74
|
+
let text = String(raw ?? '').trim();
|
|
75
|
+
const m = /^\[[^\]]{0,12}消息\]\s*(\{[\s\S]*\})$/.exec(text);
|
|
60
76
|
if (m) {
|
|
61
77
|
try {
|
|
62
78
|
const obj = JSON.parse(m[1]) as Record<string, unknown>;
|
|
63
79
|
for (const key of ['text', 'message', 'content', 'msg']) {
|
|
64
80
|
const v = obj[key];
|
|
65
|
-
if (typeof v === 'string' && v.trim())
|
|
81
|
+
if (typeof v === 'string' && v.trim()) {
|
|
82
|
+
text = v.trim();
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
66
85
|
}
|
|
67
86
|
} catch {
|
|
68
87
|
// 不是 JSON 就按原样
|
|
69
88
|
}
|
|
70
89
|
}
|
|
71
|
-
|
|
90
|
+
// 宿主给每层壳的顺序不固定(【】、#消息号、[时间] [输入方式]、说话人:),
|
|
91
|
+
// 一轮一轮剥到不再变化为止;名字那一步排除数字开头,免得把「18:30 开会」吃掉。
|
|
92
|
+
let out = text;
|
|
93
|
+
for (let i = 0; i < 4; i += 1) {
|
|
94
|
+
const next = out
|
|
95
|
+
.replace(/^【[^】]{0,40}】[::]?\s*/, '')
|
|
96
|
+
.replace(/^#-?\d+\s*/, '')
|
|
97
|
+
.replace(/^\[[^\]]{0,24}\]\s*/, '')
|
|
98
|
+
.replace(/^(?![\[\d])[^\s::\d]{1,12}[::]\s*/, '');
|
|
99
|
+
if (next === out) break;
|
|
100
|
+
out = next;
|
|
101
|
+
}
|
|
102
|
+
return out
|
|
103
|
+
.replace(/^[^「」]{0,6}回答「[^」]{0,80}」[::]\s*/, '')
|
|
104
|
+
.replace(/^自己写了[::]\s*/, '')
|
|
105
|
+
.trim();
|
|
72
106
|
}
|
|
73
107
|
|
|
74
108
|
export class MemoryCoreWorld implements World {
|
|
@@ -96,7 +130,11 @@ export class MemoryCoreWorld implements World {
|
|
|
96
130
|
this.dataDir = join(base, 'memory-core');
|
|
97
131
|
ensureDir(this.dataDir);
|
|
98
132
|
this.store = new CardStore(this.dataDir);
|
|
99
|
-
this.raw = new RawLog(
|
|
133
|
+
this.raw = new RawLog(
|
|
134
|
+
this.dataDir,
|
|
135
|
+
Math.max(1, Number(ctx.cfg?.rawRetentionDays ?? 7)),
|
|
136
|
+
Math.max(0, Number(ctx.cfg?.rawArchiveDays ?? 90)),
|
|
137
|
+
);
|
|
100
138
|
this.state = createInitialState(new Date(), this.tz(), this.cfg());
|
|
101
139
|
this.load();
|
|
102
140
|
}
|
|
@@ -166,7 +204,11 @@ export class MemoryCoreWorld implements World {
|
|
|
166
204
|
{
|
|
167
205
|
label: '对话原文',
|
|
168
206
|
state: cfg.captureTurns ? 'online' : 'offline',
|
|
169
|
-
hint: cfg.captureTurns
|
|
207
|
+
hint: cfg.captureTurns
|
|
208
|
+
? `${this.raw.size} 行,近 ${cfg.rawRetentionDays} 天${
|
|
209
|
+
this.raw.stats().days ? ` · 归档 ${this.raw.stats().days} 天` : ''
|
|
210
|
+
}`
|
|
211
|
+
: '已关闭',
|
|
170
212
|
},
|
|
171
213
|
],
|
|
172
214
|
badges: [
|
|
@@ -333,6 +375,7 @@ export class MemoryCoreWorld implements World {
|
|
|
333
375
|
scopes: this.store.scopes(),
|
|
334
376
|
},
|
|
335
377
|
docs: { enabled: Boolean(cfg.workspaceScanEnabled), count: docs.length, dirs: cfg.workspaceScanEnabled ? expandDirs(cfg.workspaceDirs, this.ctx.botDir) : [] },
|
|
378
|
+
archive: this.raw.stats(),
|
|
336
379
|
site: { port: origin ? Number(new URL(origin).port) : 0, origin },
|
|
337
380
|
};
|
|
338
381
|
}
|
|
@@ -557,6 +600,16 @@ export class MemoryCoreWorld implements World {
|
|
|
557
600
|
return lines.length ? lines.join('\n') : '没有未完成的事。';
|
|
558
601
|
}
|
|
559
602
|
|
|
603
|
+
if (mode === 'archive') {
|
|
604
|
+
if (!query) return '翻归档要带 query,给几个关键词。';
|
|
605
|
+
const span = num(args.days, 30, 1, 3650);
|
|
606
|
+
const hits = rankRaw(this.raw.readArchive(span), query, Math.max(1, Math.min(limit, 8)));
|
|
607
|
+
if (!hits.length) return `归档里没找到「${clip(query, 20)}」。`;
|
|
608
|
+
const out: string[] = ['【归档原文】'];
|
|
609
|
+
for (const hit of hits) out.push(`· [${formatLocal(hit.ts, tz).slice(0, 10)}] ${clip(hit.text, 110)}`);
|
|
610
|
+
return out.join('\n').slice(0, maxChars + 40);
|
|
611
|
+
}
|
|
612
|
+
|
|
560
613
|
const cards = rankCards(this.store.active(), {
|
|
561
614
|
query: mode === 'search' ? query : undefined,
|
|
562
615
|
kind: str(args.kind, 20) || undefined,
|
|
@@ -684,7 +737,10 @@ export class MemoryCoreWorld implements World {
|
|
|
684
737
|
try {
|
|
685
738
|
const now = new Date();
|
|
686
739
|
advance(this.state, now, this.tz(), this.cfg());
|
|
687
|
-
this.capture(
|
|
740
|
+
this.capture(
|
|
741
|
+
Boolean(this.cfg().captureBackfill)
|
|
742
|
+
&& (Number(this.state.watermarkVersion ?? 0) < CAPTURE_WATERMARK_VERSION || !this.state.backfillDoneAt),
|
|
743
|
+
);
|
|
688
744
|
await this.maybeContext(now);
|
|
689
745
|
await this.maybeRemind(now);
|
|
690
746
|
await this.maybeProactive(now);
|
|
@@ -701,38 +757,102 @@ export class MemoryCoreWorld implements World {
|
|
|
701
757
|
}
|
|
702
758
|
}
|
|
703
759
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
760
|
+
/**
|
|
761
|
+
* 白名单正则。老版本默认值 `^(starpost\.private\.|qq\.|desktop-pet\.message)` 一个真实 key 都匹配不上
|
|
762
|
+
* (实际是 `private:1940008084`、`desktop-pet.chat` 这种),读到这个旧默认就换成新的,免得用户
|
|
763
|
+
* 明明没动过设置却一直录不到东西。
|
|
764
|
+
*/
|
|
765
|
+
private senderRe(cfg: MemoryCoreConfig): RegExp {
|
|
766
|
+
const raw = String(cfg.captureSenders ?? '').trim();
|
|
767
|
+
const legacy = '^(starpost\\.private\\.|qq\\.|desktop-pet\\.message)';
|
|
768
|
+
const pattern = !raw || raw === legacy ? MEMORY_CORE_DEFAULTS.captureSenders : raw;
|
|
707
769
|
try {
|
|
708
|
-
|
|
770
|
+
return new RegExp(pattern);
|
|
709
771
|
} catch {
|
|
710
|
-
|
|
772
|
+
return new RegExp(MEMORY_CORE_DEFAULTS.captureSenders);
|
|
711
773
|
}
|
|
712
|
-
|
|
713
|
-
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* 从宿主的 external 事件流里捞对方说的话。
|
|
778
|
+
*
|
|
779
|
+
* 宿主 `range` 的语义有两处容易踩:`limit` 是**从区间末尾取**,不是从头取;
|
|
780
|
+
* 只有不传 fromCursor 时才走「倒着凑最近 N 条」的快路径。所以拿
|
|
781
|
+
* `{fromCursor, limit}` 当分页会一次跳到历史末尾——v1 就是这么把 2.5 万条
|
|
782
|
+
* 外对话全跳过的。这里改用 toCursor 开窗,一窗一窗往前推。
|
|
783
|
+
*/
|
|
784
|
+
private capture(backfill = false): void {
|
|
785
|
+
const cfg = this.cfg();
|
|
786
|
+
const host = this.host;
|
|
787
|
+
if (!cfg.captureTurns || !host?.store) return;
|
|
788
|
+
const re = this.senderRe(cfg);
|
|
789
|
+
// 旧版水位是按错的 limit 语义推出来的,不可信:重放一遍
|
|
790
|
+
if (backfill && Number(this.state.watermarkVersion ?? 0) < CAPTURE_WATERMARK_VERSION) {
|
|
791
|
+
this.state.rawWatermark = 0;
|
|
792
|
+
this.state.watermarkVersion = CAPTURE_WATERMARK_VERSION;
|
|
793
|
+
this.state.backfillStartedAt = new Date().toISOString();
|
|
794
|
+
this.state.backfillDoneAt = undefined;
|
|
795
|
+
}
|
|
796
|
+
// 区间上界:只取最后一条,读一个分片就够
|
|
797
|
+
let head = 0;
|
|
714
798
|
try {
|
|
715
|
-
|
|
799
|
+
const last = host.store.range({ limit: 1 });
|
|
800
|
+
head = last.length ? last[0].cursor : 0;
|
|
716
801
|
} catch {
|
|
717
802
|
return;
|
|
718
803
|
}
|
|
719
|
-
|
|
720
|
-
|
|
804
|
+
let from = Number(this.state.rawWatermark ?? 0) || 0;
|
|
805
|
+
if (from > head) from = 0; // 宿主换过数据目录,cursor 从头开始
|
|
806
|
+
const minLen = Math.max(1, Number(cfg.captureMinLength) || 4);
|
|
721
807
|
const items: RawEntry[] = [];
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
808
|
+
const keys = new Set<string>();
|
|
809
|
+
let scanned = 0;
|
|
810
|
+
// 实时每拍最多追 4 窗(2000 条)防积压;回放一次最多 400 窗(20 万条)
|
|
811
|
+
const budget = backfill ? 400 : 4;
|
|
812
|
+
let rounds = 0;
|
|
813
|
+
let drained = from >= head;
|
|
814
|
+
while (!drained && rounds < budget) {
|
|
815
|
+
rounds += 1;
|
|
816
|
+
const to = Math.min(from + CAPTURE_BATCH, head);
|
|
817
|
+
let events: readonly { cursor: number; ts: string; senderKey?: string; text: string }[] = [];
|
|
818
|
+
try {
|
|
819
|
+
events = host.store.range({ fromCursor: from + 1, toCursor: to, origin: 'external' });
|
|
820
|
+
} catch {
|
|
821
|
+
break;
|
|
822
|
+
}
|
|
823
|
+
scanned += events.length;
|
|
824
|
+
for (const e of events) {
|
|
825
|
+
const key = e.senderKey ?? '';
|
|
826
|
+
if (!re.test(key)) {
|
|
827
|
+
// 记几个被挡掉的 key,出问题时一眼能看出是白名单还是正文的问题
|
|
828
|
+
if (key && keys.size < 6) keys.add(key);
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
const text = clip(extractUserText(e.text), 200);
|
|
832
|
+
// 单字、纯表情、`[取消]` 这类系统回执、图片 blob 描述,都不进原文库
|
|
833
|
+
if (!text || text.length < minLen) continue;
|
|
834
|
+
if (/\[blob\b/i.test(text)) continue;
|
|
835
|
+
if (/^\[(表情包|CQ:|图片)/.test(text)) continue;
|
|
836
|
+
if (/^[\p{P}\p{S}\s]+$/u.test(text)) continue;
|
|
837
|
+
if (/^\[[^\]]{0,12}\]$/.test(text.trim())) continue;
|
|
838
|
+
items.push({ cursor: e.cursor, ts: e.ts, sender: key.slice(0, 40), text });
|
|
839
|
+
}
|
|
840
|
+
// 按窗口边界推进,不按「最后一个匹配到的 cursor」:窗口里可能一条 external 都没有
|
|
841
|
+
from = to;
|
|
842
|
+
if (from >= head) drained = true;
|
|
733
843
|
}
|
|
734
844
|
this.raw.add(items);
|
|
735
|
-
|
|
845
|
+
this.state.rawWatermark = from;
|
|
846
|
+
if (backfill && drained) this.state.backfillDoneAt = new Date().toISOString();
|
|
847
|
+
if (scanned || items.length) {
|
|
848
|
+
this.state.captureDebug = {
|
|
849
|
+
at: new Date().toISOString(),
|
|
850
|
+
scanned,
|
|
851
|
+
kept: items.length,
|
|
852
|
+
keys: items.length ? [] : [...keys],
|
|
853
|
+
...(backfill && !drained ? { backfillFrom: from, backfillTo: head } : {}),
|
|
854
|
+
};
|
|
855
|
+
}
|
|
736
856
|
}
|
|
737
857
|
|
|
738
858
|
private countToday(): number {
|
package/webui/app.js
CHANGED
|
@@ -10,6 +10,9 @@ const KINDS = [
|
|
|
10
10
|
{ id: 'note', label: '临时', css: '--k-note' },
|
|
11
11
|
];
|
|
12
12
|
|
|
13
|
+
/** 原文页一次渲染多少条。7 天窗口实测稳定在 1000 条上下,全量拼 DOM 会卡。 */
|
|
14
|
+
const TURNS_PAGE = 200;
|
|
15
|
+
|
|
13
16
|
const S = {
|
|
14
17
|
snap: null,
|
|
15
18
|
view: 'graph',
|
|
@@ -30,6 +33,7 @@ const S = {
|
|
|
30
33
|
status: 'active', // active | all | retired
|
|
31
34
|
scope: null,
|
|
32
35
|
maintenance: null,
|
|
36
|
+
turnsLimit: TURNS_PAGE,
|
|
33
37
|
};
|
|
34
38
|
|
|
35
39
|
const el = {
|
|
@@ -312,7 +316,7 @@ function renderMetrics() {
|
|
|
312
316
|
{ label: '今天新增', value: st.todayNew, unit: '条' },
|
|
313
317
|
{ label: '近 7 天', value: st.weekNew, unit: '条' },
|
|
314
318
|
{ label: '平均权重', value: st.avgWeight.toFixed(2), unit: '' },
|
|
315
|
-
{ label: '对话原文', value: (S.snap.turns ?? []).length, unit: '行' },
|
|
319
|
+
{ label: '对话原文', value: (S.snap.turns ?? []).length, unit: S.snap.archive?.days ? `行 · 归档 ${S.snap.archive.days} 天` : '行' },
|
|
316
320
|
{ label: '工作区文件', value: S.snap.docs?.count ?? 0, unit: '个', tone: S.snap.docs?.enabled ? '' : '' },
|
|
317
321
|
];
|
|
318
322
|
el.metrics.innerHTML =
|
|
@@ -655,6 +659,29 @@ class Graph {
|
|
|
655
659
|
links.push({ a: group[0], b: group[i], w: 1, tag });
|
|
656
660
|
}
|
|
657
661
|
}
|
|
662
|
+
// 版本链:新卡 supersedes 旧卡。这条边比标签共现更有信息量,所以优先级更高——
|
|
663
|
+
// 两卡之间如果已经有标签边,就把它升级成版本链,不再画第二条重合的线。
|
|
664
|
+
// 方向按时间流:旧 → 新,箭头指向新卡。
|
|
665
|
+
const pairKey = (x, y) => (x.id < y.id ? `${x.id}|${y.id}` : `${y.id}|${x.id}`);
|
|
666
|
+
const seen = new Map(links.map((l) => [pairKey(l.a, l.b), l]));
|
|
667
|
+
for (const n of nodes) {
|
|
668
|
+
for (const oldId of n.card.supersedes ?? []) {
|
|
669
|
+
const target = index.get(oldId);
|
|
670
|
+
if (!target) continue;
|
|
671
|
+
const key = pairKey(target, n);
|
|
672
|
+
const exist = seen.get(key);
|
|
673
|
+
if (exist) {
|
|
674
|
+
exist.kind = 'supersede';
|
|
675
|
+
exist.w = 1.2;
|
|
676
|
+
exist.a = target;
|
|
677
|
+
exist.b = n;
|
|
678
|
+
} else {
|
|
679
|
+
const l = { a: target, b: n, w: 1.2, tag: null, kind: 'supersede' };
|
|
680
|
+
links.push(l);
|
|
681
|
+
seen.set(key, l);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
658
685
|
// 没有标签连线的节点,按同一天挂到最近的一个节点上,避免孤岛
|
|
659
686
|
const linked = new Set(links.flatMap((l) => [l.a.id, l.b.id]));
|
|
660
687
|
for (const n of nodes) {
|
|
@@ -798,35 +825,64 @@ class Graph {
|
|
|
798
825
|
if (l.b.id === this.hover) hoverNeighbors.add(l.a.id);
|
|
799
826
|
}
|
|
800
827
|
}
|
|
828
|
+
// 版本链两端的节点一定要带标签,否则「被取代的是什么」看不出来
|
|
829
|
+
const chainIds = new Set();
|
|
830
|
+
for (const l of this.links) {
|
|
831
|
+
if (l.kind !== 'supersede') continue;
|
|
832
|
+
chainIds.add(l.a.id);
|
|
833
|
+
chainIds.add(l.b.id);
|
|
834
|
+
}
|
|
801
835
|
|
|
802
|
-
const
|
|
836
|
+
const P = palette();
|
|
837
|
+
const ink = P.ink;
|
|
838
|
+
const line = P.line;
|
|
803
839
|
for (const l of this.links) {
|
|
804
840
|
const dim = this.hover && l.a.id !== this.hover && l.b.id !== this.hover;
|
|
805
|
-
|
|
806
|
-
ctx.
|
|
807
|
-
ctx.
|
|
841
|
+
const sup = l.kind === 'supersede';
|
|
842
|
+
ctx.strokeStyle = sup ? ink : line;
|
|
843
|
+
ctx.globalAlpha = dim ? 0.07 : sup ? 0.45 : 0.16 + l.w * 0.22;
|
|
844
|
+
ctx.lineWidth = (sup ? 1.5 : 0.8 + l.w * 1.1) / k;
|
|
845
|
+
ctx.setLineDash(sup ? [4 / k, 3 / k] : []);
|
|
808
846
|
ctx.beginPath();
|
|
809
847
|
ctx.moveTo(l.a.x, l.a.y);
|
|
810
848
|
ctx.lineTo(l.b.x, l.b.y);
|
|
811
849
|
ctx.stroke();
|
|
850
|
+
if (sup) {
|
|
851
|
+
// 箭头指向新卡:这条线读作「旧的那条演进成了它」
|
|
852
|
+
const ang = Math.atan2(l.b.y - l.a.y, l.b.x - l.a.x);
|
|
853
|
+
const r = l.b.r + 3 / k;
|
|
854
|
+
const tipX = l.b.x - Math.cos(ang) * r;
|
|
855
|
+
const tipY = l.b.y - Math.sin(ang) * r;
|
|
856
|
+
const size = 5 / k;
|
|
857
|
+
ctx.setLineDash([]);
|
|
858
|
+
ctx.beginPath();
|
|
859
|
+
ctx.moveTo(tipX, tipY);
|
|
860
|
+
ctx.lineTo(tipX - Math.cos(ang - 0.42) * size, tipY - Math.sin(ang - 0.42) * size);
|
|
861
|
+
ctx.lineTo(tipX - Math.cos(ang + 0.42) * size, tipY - Math.sin(ang + 0.42) * size);
|
|
862
|
+
ctx.closePath();
|
|
863
|
+
ctx.fillStyle = ink;
|
|
864
|
+
ctx.fill();
|
|
865
|
+
}
|
|
812
866
|
}
|
|
867
|
+
ctx.setLineDash([]);
|
|
813
868
|
ctx.globalAlpha = 1;
|
|
814
869
|
|
|
815
|
-
const P = palette();
|
|
816
|
-
const ink = P.ink;
|
|
817
870
|
for (const n of this.nodes) {
|
|
818
871
|
const c = P.kinds[n.card.kind] ?? ink;
|
|
819
872
|
const isSel = n.card.id === S.selected;
|
|
820
873
|
const isHover = n.card.id === this.hover;
|
|
821
874
|
const dim = this.hover && !isHover && !hoverNeighbors.has(n.card.id) && !isSel;
|
|
822
|
-
|
|
875
|
+
// 被版本链补进来的旧卡已经退休,压暗一档,别让人以为它还在用
|
|
876
|
+
const retired = n.card.status === 'retired';
|
|
877
|
+
const baseAlpha = dim ? 0.28 : retired ? 0.5 : 1;
|
|
878
|
+
ctx.globalAlpha = baseAlpha;
|
|
823
879
|
if (isSel || isHover) {
|
|
824
880
|
ctx.beginPath();
|
|
825
881
|
ctx.arc(n.x, n.y, n.r + 5 / k, 0, Math.PI * 2);
|
|
826
882
|
ctx.fillStyle = c;
|
|
827
883
|
ctx.globalAlpha = 0.16;
|
|
828
884
|
ctx.fill();
|
|
829
|
-
ctx.globalAlpha =
|
|
885
|
+
ctx.globalAlpha = baseAlpha;
|
|
830
886
|
}
|
|
831
887
|
ctx.beginPath();
|
|
832
888
|
ctx.arc(n.x, n.y, n.r, 0, Math.PI * 2);
|
|
@@ -838,9 +894,10 @@ class Graph {
|
|
|
838
894
|
ctx.stroke();
|
|
839
895
|
}
|
|
840
896
|
const showLabel =
|
|
841
|
-
isSel || isHover
|
|
897
|
+
isSel || isHover
|
|
898
|
+
|| (S.showLabels && (chainIds.has(n.card.id) || k > 1.32 || this.nodes.length <= 16));
|
|
842
899
|
if (showLabel) {
|
|
843
|
-
ctx.globalAlpha = dim ? 0.3 : 0.95;
|
|
900
|
+
ctx.globalAlpha = dim ? 0.3 : retired ? 0.55 : 0.95;
|
|
844
901
|
const size = Math.max(9.5, 11.5 / k);
|
|
845
902
|
ctx.font = `${size}px -apple-system, "PingFang SC", sans-serif`;
|
|
846
903
|
ctx.textAlign = 'center';
|
|
@@ -865,8 +922,10 @@ function renderGraph() {
|
|
|
865
922
|
if (!graph) graph = new Graph(canvas);
|
|
866
923
|
graph.onSelect = (id) => selectCard(id);
|
|
867
924
|
graph.showLabels = S.showLabels;
|
|
868
|
-
const
|
|
869
|
-
|
|
925
|
+
const base = filtered();
|
|
926
|
+
// 版本链的另一头通常是已退休的旧卡,默认筛选看不到——补进图里,边才画得出来
|
|
927
|
+
const list = withSuperseded(base);
|
|
928
|
+
$('#graph-empty').hidden = base.length > 0;
|
|
870
929
|
graph.draw();
|
|
871
930
|
// 结构变化才重排;只改选中不重排
|
|
872
931
|
const signature = list.map((c) => c.id).join(',');
|
|
@@ -884,9 +943,28 @@ function renderLegend() {
|
|
|
884
943
|
const box = $('#graph-legend');
|
|
885
944
|
const counts = {};
|
|
886
945
|
for (const c of filtered()) counts[c.kind] = (counts[c.kind] || 0) + 1;
|
|
946
|
+
const hasChain = withSuperseded(filtered()).some((c) => (c.supersedes ?? []).length);
|
|
887
947
|
box.innerHTML =
|
|
888
948
|
'<span style="color:var(--ink-dim)">大小=权重</span>' +
|
|
889
|
-
KINDS.filter((k) => counts[k.id]).map((k) => `<span><i style="background:var(${k.css})"></i>${k.label} ${counts[k.id]}</span>`).join('')
|
|
949
|
+
KINDS.filter((k) => counts[k.id]).map((k) => `<span><i style="background:var(${k.css})"></i>${k.label} ${counts[k.id]}</span>`).join('') +
|
|
950
|
+
(hasChain ? '<span style="color:var(--ink-dim)">虚线箭头=被新版本接替</span>' : '');
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
/** 把被在用卡 supersedes 掉的旧卡补进图。它们默认已退休、不在筛选结果里,
|
|
954
|
+
* 但版本链恰恰是这个视图最该看得见的关系。 */
|
|
955
|
+
function withSuperseded(list) {
|
|
956
|
+
const ids = new Set(list.map((c) => c.id));
|
|
957
|
+
const extra = [];
|
|
958
|
+
for (const c of list) {
|
|
959
|
+
for (const oldId of c.supersedes ?? []) {
|
|
960
|
+
if (ids.has(oldId)) continue;
|
|
961
|
+
const old = cards().find((x) => x.id === oldId);
|
|
962
|
+
if (!old) continue;
|
|
963
|
+
ids.add(oldId);
|
|
964
|
+
extra.push(old);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
return extra.length ? [...list, ...extra] : list;
|
|
890
968
|
}
|
|
891
969
|
|
|
892
970
|
$('#graph-fit').addEventListener('click', () => graph?.fit());
|
|
@@ -1037,25 +1115,40 @@ function renderTable() {
|
|
|
1037
1115
|
function renderTurns() {
|
|
1038
1116
|
const host = $('#view-turns');
|
|
1039
1117
|
const toks = tokens(S.query);
|
|
1040
|
-
const
|
|
1041
|
-
if (!
|
|
1118
|
+
const matched = (S.snap?.turns ?? []).filter((t) => matches(t.text, toks)).slice().reverse();
|
|
1119
|
+
if (!matched.length) {
|
|
1120
|
+
const dbg = S.snap?.state?.captureDebug;
|
|
1121
|
+
let hint = '在插件设置里确认「存对话原文」是开的,聊几句就会出现在这里。';
|
|
1122
|
+
if (dbg && dbg.scanned && !dbg.kept) {
|
|
1123
|
+
const keys = (dbg.keys ?? []).map((k) => esc(k)).join('、') || '无';
|
|
1124
|
+
hint = `上次扫了 ${dbg.scanned} 条外部事件,一条都没留下——被「算作对话的来源」白名单挡掉了(样例:${keys}),把这几个 key 加进设置里的正则就行。`;
|
|
1125
|
+
}
|
|
1042
1126
|
host.innerHTML = `<div class="turns-wrap"><div class="empty"><p>${S.query ? '没有匹配的对话。' : '还没有存下对话原文。'}</p><p class="dim">${
|
|
1043
|
-
S.query ? '' :
|
|
1127
|
+
S.query ? '' : hint
|
|
1044
1128
|
}</p></div></div>`;
|
|
1045
1129
|
return;
|
|
1046
1130
|
}
|
|
1131
|
+
// 全量拼 DOM 在 1000 条时会明显卡:先渲染最近 200 条,要更多再点。
|
|
1132
|
+
// 分组按匹配总数算,组头显示「已显示/该天总数」,不让人误以为那天只有这么几句。
|
|
1047
1133
|
const groups = new Map();
|
|
1048
|
-
for (const t of
|
|
1134
|
+
for (const t of matched) {
|
|
1049
1135
|
const key = dayKey(t.ts);
|
|
1050
1136
|
if (!groups.has(key)) groups.set(key, []);
|
|
1051
1137
|
groups.get(key).push(t);
|
|
1052
1138
|
}
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1139
|
+
const limit = Math.max(TURNS_PAGE, Math.min(S.turnsLimit ?? TURNS_PAGE, matched.length));
|
|
1140
|
+
let remain = limit;
|
|
1141
|
+
const parts = [];
|
|
1142
|
+
for (const [key, items] of groups) {
|
|
1143
|
+
if (remain <= 0) break;
|
|
1144
|
+
const take = items.slice(0, remain);
|
|
1145
|
+
remain -= take.length;
|
|
1146
|
+
parts.push(`
|
|
1056
1147
|
<div class="day-group">
|
|
1057
|
-
<div class="day-head"><b>${dayLabel(key)}</b><span>${
|
|
1058
|
-
|
|
1148
|
+
<div class="day-head"><b>${dayLabel(key)}</b><span>${
|
|
1149
|
+
take.length === items.length ? `${items.length} 条` : `${take.length} / ${items.length} 条`
|
|
1150
|
+
}</span></div>
|
|
1151
|
+
${take
|
|
1059
1152
|
.map(
|
|
1060
1153
|
(t) => `<div class="turn">
|
|
1061
1154
|
<div><time>${fmtTime(t.ts).slice(6)}</time><div class="who">${esc(shortSender(t.sender))}</div></div>
|
|
@@ -1063,15 +1156,24 @@ function renderTurns() {
|
|
|
1063
1156
|
</div>`,
|
|
1064
1157
|
)
|
|
1065
1158
|
.join('')}
|
|
1066
|
-
</div
|
|
1067
|
-
|
|
1068
|
-
|
|
1159
|
+
</div>`);
|
|
1160
|
+
}
|
|
1161
|
+
const more =
|
|
1162
|
+
matched.length > limit
|
|
1163
|
+
? `<button class="btn turns-more" id="turns-more">还有 ${matched.length - limit} 条 · 继续加载</button>`
|
|
1164
|
+
: '';
|
|
1165
|
+
host.innerHTML = `<div class="turns-wrap">${parts.join('')}${more}</div>`;
|
|
1166
|
+
$('#turns-more')?.addEventListener('click', () => {
|
|
1167
|
+
S.turnsLimit = limit + TURNS_PAGE;
|
|
1168
|
+
renderTurns();
|
|
1169
|
+
});
|
|
1069
1170
|
}
|
|
1070
1171
|
|
|
1071
1172
|
function shortSender(key) {
|
|
1072
|
-
const
|
|
1073
|
-
const base =
|
|
1074
|
-
|
|
1173
|
+
const raw = String(key ?? '');
|
|
1174
|
+
const base = raw.split(/[.:]/)[0];
|
|
1175
|
+
const map = { starpost: '星邮', qq: 'QQ', private: 'QQ 私聊', 'desktop-pet': '桌宠' };
|
|
1176
|
+
return map[base] ?? (raw || '—').slice(0, 12);
|
|
1075
1177
|
}
|
|
1076
1178
|
|
|
1077
1179
|
/* ── 工作区文件 ─────────────────────────────────────── */
|
|
@@ -1192,7 +1294,6 @@ function renderInspector() {
|
|
|
1192
1294
|
? `<button class="btn tiny" id="btn-restore">恢复这条</button>`
|
|
1193
1295
|
: `<button class="btn tiny" id="btn-retire">退休</button>`
|
|
1194
1296
|
}
|
|
1195
|
-
<button class="btn tiny danger" id="btn-delete">彻底删除</button>
|
|
1196
1297
|
</div>
|
|
1197
1298
|
${lineageHtml(card)}
|
|
1198
1299
|
${
|
|
@@ -1229,18 +1330,7 @@ function renderInspector() {
|
|
|
1229
1330
|
toast('复制失败,浏览器不给剪贴板权限', true);
|
|
1230
1331
|
}
|
|
1231
1332
|
});
|
|
1232
|
-
|
|
1233
|
-
if (!confirm(`彻底删掉这条?删了就找不回来了。\n\n${card.text}`)) return;
|
|
1234
|
-
try {
|
|
1235
|
-
await api('/api/purge', { method: 'POST', body: JSON.stringify({ ids: [card.id] }) });
|
|
1236
|
-
S.selected = null;
|
|
1237
|
-
setInspCollapsed(true);
|
|
1238
|
-
toast('已彻底删除');
|
|
1239
|
-
await load();
|
|
1240
|
-
} catch (err) {
|
|
1241
|
-
toast(`删除失败:${err.message}`, true);
|
|
1242
|
-
}
|
|
1243
|
-
});
|
|
1333
|
+
// 彻底删除只在维护面板 → 回收站里给:详情栏紧挨「退休」,误触一次就没了
|
|
1244
1334
|
const retireBtn = $('#btn-retire');
|
|
1245
1335
|
if (retireBtn) {
|
|
1246
1336
|
retireBtn.addEventListener('click', async () => {
|
|
@@ -1589,15 +1679,22 @@ document.addEventListener('keydown', (event) => {
|
|
|
1589
1679
|
}
|
|
1590
1680
|
});
|
|
1591
1681
|
|
|
1682
|
+
// 逐字 render 会把原文 1000+ 条、表格几十行整体重建,打字快了肉眼可见地卡。
|
|
1683
|
+
// 输入框状态立刻更新(清除按钮要跟手),重绘压到 150ms 后。
|
|
1684
|
+
let searchTimer = 0;
|
|
1592
1685
|
el.search.addEventListener('input', () => {
|
|
1593
1686
|
S.query = el.search.value;
|
|
1594
1687
|
S.searchClear.hidden = !S.query;
|
|
1595
|
-
|
|
1688
|
+
S.turnsLimit = TURNS_PAGE;
|
|
1689
|
+
clearTimeout(searchTimer);
|
|
1690
|
+
searchTimer = setTimeout(() => render(), 150);
|
|
1596
1691
|
});
|
|
1597
1692
|
el.searchClear.addEventListener('click', () => {
|
|
1598
1693
|
S.query = '';
|
|
1599
1694
|
el.search.value = '';
|
|
1600
1695
|
S.searchClear.hidden = true;
|
|
1696
|
+
S.turnsLimit = TURNS_PAGE;
|
|
1697
|
+
clearTimeout(searchTimer);
|
|
1601
1698
|
render();
|
|
1602
1699
|
});
|
|
1603
1700
|
|
|
@@ -1691,7 +1788,7 @@ function renderMaintenance(tab) {
|
|
|
1691
1788
|
const list = rows(m.stale);
|
|
1692
1789
|
content = list.length
|
|
1693
1790
|
? list.map((c) => maintRow(c, `<button class="btn tiny" data-retire="${c.id}">退休</button>`)).join('')
|
|
1694
|
-
: '<p class="maint-empty">没有疑似作废的记忆。文案里带「取消了 / 改到 /
|
|
1791
|
+
: '<p class="maint-empty">没有疑似作废的记忆。文案里带「取消了 / 改到 / 作废」,或者 90 天没被召回、权重又低的,会出现在这里。</p>';
|
|
1695
1792
|
if (list.length) bulk = `<button class="btn tiny" data-bulk="stale">全部退休(${list.length})</button>`;
|
|
1696
1793
|
} else if (tab === 'junk') {
|
|
1697
1794
|
const list = rows(m.junk);
|
package/webui/style.css
CHANGED
|
@@ -419,6 +419,13 @@ td .tag-list { max-height: 40px; overflow: hidden; }
|
|
|
419
419
|
|
|
420
420
|
/* 原文 / 文件 */
|
|
421
421
|
.turns-wrap, .docs-wrap { height: 100%; overflow-y: auto; padding: 14px 18px 40px; }
|
|
422
|
+
.turns-more {
|
|
423
|
+
display: block; width: 100%; margin: 6px 0 18px; padding: 9px;
|
|
424
|
+
font: inherit; font-size: 12px; color: var(--ink-dim);
|
|
425
|
+
background: transparent; border: 1px dashed var(--line-strong);
|
|
426
|
+
border-radius: 8px; cursor: pointer;
|
|
427
|
+
}
|
|
428
|
+
.turns-more:hover { color: var(--ink); border-color: var(--ink-dim); }
|
|
422
429
|
.turn { display: grid; grid-template-columns: 84px minmax(0, 1fr); gap: 10px; padding: 5px 0; border-bottom: 1px dashed color-mix(in srgb, var(--line) 70%, transparent); }
|
|
423
430
|
.turn time { font-size: 11px; color: var(--ink-dim); font-family: var(--mono); padding-top: 1px; }
|
|
424
431
|
.turn .who { font-size: 10.5px; color: var(--ink-dim); }
|