dsh-rosemary-memory 0.0.0-stage → 0.3.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/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +191 -2
- package/client.js +397 -0
- package/docs/detail.md +39 -0
- package/docs/detail.zh.md +48 -0
- package/dsh.bundle.patch.yml +15 -0
- package/icon.svg +12 -0
- package/index.js +563 -0
- package/lib/digest.js +268 -0
- package/lib/recall.js +156 -0
- package/lib/store.js +101 -0
- package/lib/transcript.js +131 -0
- package/lib/units.js +213 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +62 -4
- package/scripts/smoke.mjs +37 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# 更新日志
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
- **改名:`dsh-memory` → `dsh-rosemary-memory`**(rosemary 是迷迭香:记忆的香料)。包名、bundle 名、客户端模块 id、导出名全部跟着改;`/dsh-memory/*` 路由前缀保留为别名,旧书签和旧客户端 bundle 不会 404
|
|
6
|
+
- 环境变量:新增 `DSH_ROSEMARY_MEMORY_DIR`,旧的 `DSH_MEMORY_DIR` 继续识别
|
|
7
|
+
- 发布到 npm:补 `repository` / `homepage` / `bugs` / `publishConfig`,去掉 `private`,包内 18 个文件、32.8 kB(`data/` 永不打包)
|
|
8
|
+
- `dsh.bundle.patch.yml` 不再写死 `storeDir`,默认 `<插件目录>/data`;要换位置就加 `config: { storeDir: ... }`
|
|
9
|
+
- 修 `lib/units.js` 注释里一处乱码(`鈥?` → `—`)
|
|
10
|
+
|
|
11
|
+
## 0.2.0
|
|
12
|
+
|
|
13
|
+
- 记忆改成**侧边栏整页**:左栏「🧠 记忆」→ 加权记忆列表 + 阈值设置,输入框旁的按钮已移除
|
|
14
|
+
- 新增 `lib/units.js`:把相似 note 聚成加权单元,达到阈值的升级为「加权压缩条目」(playbook),展开可看被压缩的原文
|
|
15
|
+
- 新增路由:`GET /dsh-rosemary-memory/units`、`POST /dsh-rosemary-memory/compress`、`GET|POST /dsh-rosemary-memory/settings`
|
|
16
|
+
- 阈值可调:再压缩阈值(条数 / 权重下限 / 相似度)写 `data/state.json`;bili 上下文阈值写 `~/.config/billion-context/billion-context.json` 的 `compress` 段,保留其余键
|
|
17
|
+
- 记忆库默认落在**插件目录** `data/`,可用 `DSH_ROSEMARY_MEMORY_DIR` 或 config `storeDir` 覆盖
|
|
18
|
+
- 修复:`memory_recall` / `memory_brief` 不再把当前目录当硬过滤条件(跨项目召回会被悄悄滤空)
|
|
19
|
+
- 修复:`/digest` 什么都不传时只归纳**最新会话**,不再扫全机器
|
|
20
|
+
|
|
21
|
+
## 0.1.0
|
|
22
|
+
|
|
23
|
+
- 首个版本:读 DSH 会话存档(逐帧 zstd + JSONL)→ 确定性归纳成 run / note
|
|
24
|
+
- 四个宿主工具 `memory_digest` / `memory_recall` / `memory_remember` / `memory_brief`
|
|
25
|
+
- 确定性召回打分(精确 / 三元组 / 时间 / 频次 / 跨会话信任 / 人工权重)
|
|
26
|
+
- 全部本地 JSONL,不跑模型、不上服务器
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 HZY
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,192 @@
|
|
|
1
|
-
#
|
|
1
|
+
# dsh-rosemary-memory
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**可审计的跨会话记忆** —— DeepSeek Harness(DSH)插件。
|
|
4
|
+
|
|
5
|
+
> 名字里的 rosemary 是「迷迭香」:记忆的香料。记住该记的,也敢忘记该忘的。
|
|
6
|
+
|
|
7
|
+
过去的会话被归纳成一条条 run / note,再按权重压成可展开的记忆单元;下个会话开头把它注入,或者随时按报错串、命令、文件名召回。**不跑模型、不上服务器、不改写原始会话记录**——同一份输入永远给出同一份结果,每一步打分都能解释。
|
|
8
|
+
|
|
9
|
+
> Auditable cross-session memory for DeepSeek Harness. Deterministic digest, weighted compression, no model calls, no server, all local.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## 它解决什么
|
|
14
|
+
|
|
15
|
+
DSH 的上下文插件(如 [billion-context](https://github.com/ranxianglei/billion-context))在**会话内部**压缩:折叠出的摘要省下了 token,但会话一关,摘要也跟着消失。三个月前那次部署踩的坑,今天的会话完全不知道。
|
|
16
|
+
|
|
17
|
+
dsh-rosemary-memory 在会话之外再存一层记忆:
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
L0 原始消息 ──► L1 会话内折叠摘要(bili 的活)
|
|
21
|
+
│
|
|
22
|
+
└──► L2 run(一回合一条)──► L3 note(结论/根因/修法)
|
|
23
|
+
│
|
|
24
|
+
└──► 加权单元(相似 note 合并,权重可解释)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 为什么不走向量
|
|
28
|
+
|
|
29
|
+
这是**刻意的取舍**,不是没做完。
|
|
30
|
+
|
|
31
|
+
| 向量擅长 | 工作流记忆需要 |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| 同义改写("登录报错"≈"认证失败") | 精确符号:`UnknownHostException`、`session-8c0b7bb4`、`lib/units.js:103` |
|
|
34
|
+
| 模糊主题相似 | 能解释"为什么这条被召回" |
|
|
35
|
+
| 一次性建索引 | 换模型不必全量重算 |
|
|
36
|
+
|
|
37
|
+
向量会把 `UnknownHostException` 和"网络错误"映射到同一个邻域,而工作流记忆最需要区分的就是这两者。所以这里用**精确匹配优先 + 字符三元组兜底**的确定性打分;真要加向量,它也只能是精确命中为 0 时的兜底层,且必须把"语义相近"写进命中原由里。
|
|
38
|
+
|
|
39
|
+
## 加权压缩(页面上的记忆列表)
|
|
40
|
+
|
|
41
|
+
原始 note 一条条读没有意义——同一条教训会在三个会话里各记一次。插件把它们折成 **unit**:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
权重 = 频次(log, 6 次封顶)×0.35
|
|
45
|
+
+ 跨会话数(4 个封顶)×0.25
|
|
46
|
+
+ 人工权重×0.2
|
|
47
|
+
+ 新鲜度(45 天半衰)×0.2
|
|
48
|
+
|
|
49
|
+
相似度 = token 交集 ÷ 较短一方 # 含于关系:短句不会被长段落淹没
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
聚类里 note 数 ≥ `compactThreshold` 且合并权重 ≥ `compactWeight` 时,升级为**加权压缩条目(playbook)**:排在列表前面,展开能看到被它压缩掉的原文,**来源 note 一条不删**。
|
|
53
|
+
|
|
54
|
+
这是和"黑箱摘要"最大的区别:丢掉了什么,页面上明明白白。
|
|
55
|
+
|
|
56
|
+
## 和已有项目的关系
|
|
57
|
+
|
|
58
|
+
| | dsh-rosemary-memory | [claude-mem](https://github.com/thedotmack/claude-mem) | [Basic Memory](https://basicmemory.com) |
|
|
59
|
+
| --- | --- | --- | --- |
|
|
60
|
+
| 采集 | 解析会话原档,**零模型调用** | hooks + SDK 生成语义摘要 | 模型通过 MCP 工具写 Markdown |
|
|
61
|
+
| 压缩 | 相似度聚类 + 可解释权重 + 阈值可调 | 摘要 | 人工/模型整理 |
|
|
62
|
+
| 检索 | 确定性打分(精确优先) | 混合检索 | BM25 + 向量 |
|
|
63
|
+
| 依赖 | 零依赖、单进程、JSONL | Node worker + SDK | Python MCP server |
|
|
64
|
+
| 受众 | DSH 专用(工具 + 路由 + 槽位) | 15+ 客户端 | 任意 MCP 客户端 |
|
|
65
|
+
|
|
66
|
+
结论先摆在这:**这类工具不稀缺,稀缺的是"可审计"**。如果你要的是开箱自动、抓到就存,claude-mem 更成熟;如果你要的是"我知道它为什么记住、为什么忘记、什么时候压",这个插件是冲着这个来的。
|
|
67
|
+
|
|
68
|
+
## 安装
|
|
69
|
+
|
|
70
|
+
### npm(推荐)
|
|
71
|
+
|
|
72
|
+
DSH 用 pnpm 管 profile,所以在这台机器上装插件 = 在自己的 profile 目录里加一个依赖。直接指向 npm 上的包:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
cd ~/.dsh/profiles/<profile>
|
|
76
|
+
npm install dsh-rosemary-memory # 或 pnpm add dsh-rosemary-memory
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
然后在同一个 `package.json` 里把包名登记进 bundles 列表:
|
|
80
|
+
|
|
81
|
+
```jsonc
|
|
82
|
+
{
|
|
83
|
+
"dependencies": { "dsh-rosemary-memory": "^0.3.0" },
|
|
84
|
+
"dsh": { "profile": { "bundles": ["...", "dsh-rosemary-memory"] } }
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
重载 DSH(或 `plugin_manager set_bundle enabled=false` → `true`)后,左侧栏出现「🧠 记忆」。
|
|
89
|
+
|
|
90
|
+
### 本地目录(改代码时用这个)
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
plugin_manager install_bundle target: "D:/path/to/dsh-rosemary-memory"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
或者手工 link 进 profile:
|
|
97
|
+
|
|
98
|
+
```jsonc
|
|
99
|
+
{
|
|
100
|
+
"dependencies": { "dsh-rosemary-memory": "link:D:/path/to/dsh-rosemary-memory" },
|
|
101
|
+
"dsh": { "profile": { "bundles": ["...", "dsh-rosemary-memory"] } }
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### 从源码装
|
|
106
|
+
|
|
107
|
+
```powershell
|
|
108
|
+
git clone https://github.com/HZYqwq/dsh-rosemary-memory
|
|
109
|
+
cd dsh-rosemary-memory
|
|
110
|
+
python -X utf8 tools/make_repo.py --check # 只检查,不动 git
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
包本身零依赖、无构建步骤,`node --check` 通过就能跑。
|
|
114
|
+
|
|
115
|
+
## 用法
|
|
116
|
+
|
|
117
|
+
**侧栏整页**:记忆列表(加权单元 + 权重条 + 出现次数 + 跨会话数 + 展开被压缩原文)|阈值页(再压缩阈值、bili 上下文阈值)|页头「归纳新回合」「重新压缩」。
|
|
118
|
+
|
|
119
|
+
**四个宿主工具**:
|
|
120
|
+
|
|
121
|
+
| 工具 | 作用 |
|
|
122
|
+
| --- | --- |
|
|
123
|
+
| `memory_digest` | 归纳(默认 dry-run,`dryRun:false` 才写) |
|
|
124
|
+
| `memory_recall` | 跨会话检索,返回评分与命中原由 |
|
|
125
|
+
| `memory_remember` | 显式写一条长期记忆(模型可主动调用的唯一写入口) |
|
|
126
|
+
| `memory_brief` | 可注入稳定前缀的精简摘要(默认 1200 token) |
|
|
127
|
+
|
|
128
|
+
**HTTP**(同源,手机端也能用):
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
GET /dsh-rosemary-memory/status GET /dsh-rosemary-memory/brief?budget=1200
|
|
132
|
+
GET /dsh-rosemary-memory/recall?q= GET /dsh-rosemary-memory/notes?limit=50
|
|
133
|
+
POST /dsh-rosemary-memory/digest POST /dsh-rosemary-memory/remember
|
|
134
|
+
GET /dsh-rosemary-memory/units?q= POST /dsh-rosemary-memory/compress
|
|
135
|
+
GET|POST /dsh-rosemary-memory/settings
|
|
136
|
+
|
|
137
|
+
旧的 `/dsh-memory/*` 前缀继续可用(别名)。
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
默认作用域是故意收窄的:`digest` 什么都不给时只归纳**最新会话**(按钮点一下不会扫全机器);`recall` 不给 project 时搜**全部项目**;`brief` 优先当前目录,没有则回退全库。
|
|
141
|
+
|
|
142
|
+
## 阈值
|
|
143
|
+
|
|
144
|
+
| 阈值 | 默认 | 作用 |
|
|
145
|
+
| --- | --- | --- |
|
|
146
|
+
| `compactThreshold` | 2 | 多少条相似 note 才升级为压缩条目 |
|
|
147
|
+
| `compactWeight` | 0.4 | 合并后权重下限 |
|
|
148
|
+
| `similarity` | 0.55 | 相似度门槛,越高越保守 |
|
|
149
|
+
| `nudgeGrowthTokens` | 50000 | bili:新增多少 token 后考虑折叠 |
|
|
150
|
+
| `maxContextLimit` | 75% | bili:窗口用到多少比例时每轮催促折叠 |
|
|
151
|
+
|
|
152
|
+
前三个在 `data/state.json`;后两个写进 `~/.config/billion-context/billion-context.json` 的 `compress` 段(保留其他键)。bili 逐请求读取,立即生效。
|
|
153
|
+
|
|
154
|
+
## 数据
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
<插件目录>/data/
|
|
158
|
+
runs.jsonl 每回合一条
|
|
159
|
+
notes.jsonl 每条结论
|
|
160
|
+
units.json 重新压缩的快照
|
|
161
|
+
state.json 归纳时间、阈值
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
全部 append-only、可 grep、可手改。位置可用 `DSH_ROSEMARY_MEMORY_DIR`(或旧名 `DSH_MEMORY_DIR`)、或 config `storeDir` 覆盖。
|
|
165
|
+
|
|
166
|
+
**数据永远不会进仓库**:`.gitignore` 已经排除 `data/`,CI 也会在检测到 `data/` 被提交时直接失败。
|
|
167
|
+
|
|
168
|
+
## 已知边界
|
|
169
|
+
|
|
170
|
+
诚实标注,别指望它做这些:
|
|
171
|
+
|
|
172
|
+
- **没有向量检索**(理由见上)。换个说法("登不进去" vs "认证失败")大概率召回不到。
|
|
173
|
+
- **没有冲突消解**。两条互相矛盾的结论会都保留、互标 `conflicts` 并扣分,不会自动裁决谁对。
|
|
174
|
+
- **没有基准评测**。不像 mem0 有 LoCoMo 之类的分数,这里没有。
|
|
175
|
+
- **只支持 DSH**。工具、路由、槽位都是 DSH 的插件 API,换个 agent 要重写适配层。
|
|
176
|
+
- **界面中文优先**,英文只有 `locale/en.json` 的元数据。
|
|
177
|
+
- 归纳质量取决于启发式规则(问句不算结论、markdown 表格行丢弃等),会漏也会误收。
|
|
178
|
+
|
|
179
|
+
## 开发
|
|
180
|
+
|
|
181
|
+
```powershell
|
|
182
|
+
node --check index.js
|
|
183
|
+
node --check client.js
|
|
184
|
+
Get-ChildItem lib/*.js | ForEach-Object { node --check $_.FullName }
|
|
185
|
+
node scripts/smoke.mjs # 离线跑通 digest + recall + brief
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
改 `index.js` 或 `lib/*.js` 后必须重载插件——ESM 模块缓存不会自己失效。
|
|
189
|
+
|
|
190
|
+
## License
|
|
191
|
+
|
|
192
|
+
MIT
|
package/client.js
ADDED
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
// dsh-rosemary-memory — browser entry.
|
|
2
|
+
//
|
|
3
|
+
// One page, reachable from the left sidebar, in two halves:
|
|
4
|
+
//
|
|
5
|
+
// * 记忆 — the compressed view: notes folded into weighted units, playbooks
|
|
6
|
+
// first, each row expandable to the notes it was built from;
|
|
7
|
+
// * 阈值 — the two thresholds that decide when this page grows a new playbook
|
|
8
|
+
// (memory) and when billion-context starts folding the conversation.
|
|
9
|
+
//
|
|
10
|
+
// Everything is a same-origin fetch to /dsh-rosemary-memory/*, so it keeps working when
|
|
11
|
+
// the UI is opened from a phone through the remote tunnel.
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: 'dsh-rosemary-memory',
|
|
14
|
+
factory: (require) => {
|
|
15
|
+
const React = require('react');
|
|
16
|
+
const h = React.createElement;
|
|
17
|
+
|
|
18
|
+
const ROUTE = '/dsh-rosemary-memory';
|
|
19
|
+
const C = {
|
|
20
|
+
text: 'var(--dsw-alias-label-primary, #18202c)',
|
|
21
|
+
muted: 'var(--dsw-alias-label-secondary, #667085)',
|
|
22
|
+
line: 'var(--dsw-alias-border-l2, #e7e9ef)',
|
|
23
|
+
soft: 'var(--dsw-alias-bg-layer-2, #f7f8fa)',
|
|
24
|
+
bg: 'var(--dsw-alias-bg-layer-1, #fff)',
|
|
25
|
+
accent: 'var(--dsh-rosemary-memory-accent, #4f6bdb)',
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
async function api(pathname, init) {
|
|
29
|
+
const res = await fetch(`${ROUTE}${pathname}`, init);
|
|
30
|
+
const data = await res.json().catch(() => ({}));
|
|
31
|
+
if (!res.ok) throw new Error(data.error || `${res.status} ${res.statusText}`);
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function relTime(at) {
|
|
36
|
+
if (!at) return '';
|
|
37
|
+
const diff = Date.now() - at;
|
|
38
|
+
const min = Math.round(diff / 60000);
|
|
39
|
+
if (min < 1) return '刚刚';
|
|
40
|
+
if (min < 60) return `${min} 分钟前`;
|
|
41
|
+
const hours = Math.round(min / 60);
|
|
42
|
+
if (hours < 24) return `${hours} 小时前`;
|
|
43
|
+
const days = Math.round(hours / 24);
|
|
44
|
+
return days < 30 ? `${days} 天前` : `${Math.round(days / 30)} 个月前`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function Bar({ value }) {
|
|
48
|
+
return h(
|
|
49
|
+
'span',
|
|
50
|
+
{ style: { display: 'inline-block', width: 54, height: 6, borderRadius: 3, background: C.line, overflow: 'hidden', verticalAlign: 'middle' } },
|
|
51
|
+
h('span', {
|
|
52
|
+
style: { display: 'block', width: `${Math.round(Math.max(0.04, Math.min(1, value)) * 100)}%`, height: '100%', background: C.accent },
|
|
53
|
+
}),
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function Badge({ kind }) {
|
|
58
|
+
const label = kind === 'playbook' ? '加权压缩' : kind === 'fix' ? '修复' : kind === 'decision' ? '决策' : '记忆';
|
|
59
|
+
return h(
|
|
60
|
+
'span',
|
|
61
|
+
{
|
|
62
|
+
style: {
|
|
63
|
+
fontSize: 11,
|
|
64
|
+
padding: '1px 7px',
|
|
65
|
+
borderRadius: 999,
|
|
66
|
+
border: `1px solid ${kind === 'playbook' ? C.accent : C.line}`,
|
|
67
|
+
color: kind === 'playbook' ? C.accent : C.muted,
|
|
68
|
+
whiteSpace: 'nowrap',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
label,
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function UnitRow({ unit, open, onToggle }) {
|
|
76
|
+
return h(
|
|
77
|
+
'div',
|
|
78
|
+
{ style: { borderTop: `1px solid ${C.line}`, padding: '14px 0' } },
|
|
79
|
+
h(
|
|
80
|
+
'div',
|
|
81
|
+
{ style: { display: 'flex', gap: 10, alignItems: 'baseline' } },
|
|
82
|
+
h('div', { style: { flex: 1, minWidth: 0 } }, h('div', { style: { fontWeight: 600, color: C.text, fontSize: 14.5, lineHeight: 1.5 } }, unit.title)),
|
|
83
|
+
h('div', { style: { display: 'flex', gap: 8, alignItems: 'center', color: C.muted, fontSize: 12, whiteSpace: 'nowrap' } },
|
|
84
|
+
h(Bar, { value: unit.weight }),
|
|
85
|
+
h('span', null, unit.weight.toFixed(2)),
|
|
86
|
+
h(Badge, { kind: unit.kind }),
|
|
87
|
+
),
|
|
88
|
+
),
|
|
89
|
+
h(
|
|
90
|
+
'div',
|
|
91
|
+
{ style: { marginTop: 6, color: C.muted, fontSize: 12.5, display: 'flex', gap: 14, flexWrap: 'wrap' } },
|
|
92
|
+
h('span', null, `出现 ${unit.timesSeen} 次`),
|
|
93
|
+
h('span', null, `跨 ${unit.sessionsSeen} 个会话`),
|
|
94
|
+
unit.members > 1 ? h('span', null, `合并 ${unit.members} 条`) : null,
|
|
95
|
+
h('span', null, relTime(unit.lastSeenAt)),
|
|
96
|
+
unit.folded.length
|
|
97
|
+
? h('a', { href: '#', onClick: (event) => { event.preventDefault(); onToggle(); }, style: { color: C.accent, textDecoration: 'none' } },
|
|
98
|
+
open ? '收起原文' : `看被压缩的 ${unit.folded.length} 条`)
|
|
99
|
+
: null,
|
|
100
|
+
),
|
|
101
|
+
open
|
|
102
|
+
? h(
|
|
103
|
+
'div',
|
|
104
|
+
{ style: { marginTop: 8, background: C.soft, borderRadius: 8, padding: '10px 12px', fontSize: 12.5, color: C.muted, lineHeight: 1.65 } },
|
|
105
|
+
h('div', { style: { color: C.text, marginBottom: 6 } }, unit.text),
|
|
106
|
+
unit.folded.map((line, index) => h('div', { key: index, style: { paddingTop: 4, borderTop: index ? `1px dashed ${C.line}` : 'none' } }, line)),
|
|
107
|
+
)
|
|
108
|
+
: null,
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function Field({ label, hint, value, onChange, suffix }) {
|
|
113
|
+
return h(
|
|
114
|
+
'label',
|
|
115
|
+
{ style: { display: 'block', marginBottom: 14 } },
|
|
116
|
+
h('div', { style: { fontSize: 12.5, color: C.text, marginBottom: 4 } }, label),
|
|
117
|
+
h(
|
|
118
|
+
'div',
|
|
119
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 8 } },
|
|
120
|
+
h('input', {
|
|
121
|
+
value,
|
|
122
|
+
onChange: (event) => onChange(event.target.value),
|
|
123
|
+
style: { flex: 1, padding: '6px 9px', borderRadius: 7, border: `1px solid ${C.line}`, background: C.bg, color: C.text, fontSize: 13, fontFamily: 'inherit' },
|
|
124
|
+
}),
|
|
125
|
+
suffix ? h('span', { style: { color: C.muted, fontSize: 12 } }, suffix) : null,
|
|
126
|
+
),
|
|
127
|
+
hint ? h('div', { style: { marginTop: 4, color: C.muted, fontSize: 11.5, lineHeight: 1.5 } }, hint) : null,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function MemoryPage() {
|
|
132
|
+
const [tab, setTab] = React.useState('units');
|
|
133
|
+
const [units, setUnits] = React.useState([]);
|
|
134
|
+
const [stats, setStats] = React.useState({});
|
|
135
|
+
const [q, setQ] = React.useState('');
|
|
136
|
+
const [openId, setOpenId] = React.useState('');
|
|
137
|
+
const [busy, setBusy] = React.useState('');
|
|
138
|
+
const [error, setError] = React.useState('');
|
|
139
|
+
const [notice, setNotice] = React.useState('');
|
|
140
|
+
const [memory, setMemory] = React.useState({ compactThreshold: 2, compactWeight: 0.4, similarity: 0.55 });
|
|
141
|
+
const [context, setContext] = React.useState({ nudgeGrowthTokens: 50000, maxContextLimit: '75%' });
|
|
142
|
+
|
|
143
|
+
const load = React.useCallback(async (needle) => {
|
|
144
|
+
setError('');
|
|
145
|
+
try {
|
|
146
|
+
const data = await api(`/units?q=${encodeURIComponent(needle || '')}&limit=120`);
|
|
147
|
+
setUnits(data.units || []);
|
|
148
|
+
setStats(data.stats || {});
|
|
149
|
+
} catch (err) {
|
|
150
|
+
setError(String(err.message || err));
|
|
151
|
+
}
|
|
152
|
+
}, []);
|
|
153
|
+
|
|
154
|
+
const loadSettings = React.useCallback(async () => {
|
|
155
|
+
try {
|
|
156
|
+
const data = await api('/settings');
|
|
157
|
+
setMemory(data.memory || {});
|
|
158
|
+
setContext(data.context || {});
|
|
159
|
+
} catch (err) {
|
|
160
|
+
setError(String(err.message || err));
|
|
161
|
+
}
|
|
162
|
+
}, []);
|
|
163
|
+
|
|
164
|
+
React.useEffect(() => {
|
|
165
|
+
load('');
|
|
166
|
+
loadSettings();
|
|
167
|
+
}, [load, loadSettings]);
|
|
168
|
+
|
|
169
|
+
// Debounced search: the store is small, but typing should not fire a
|
|
170
|
+
// request per keystroke.
|
|
171
|
+
React.useEffect(() => {
|
|
172
|
+
const timer = setTimeout(() => load(q), 180);
|
|
173
|
+
return () => clearTimeout(timer);
|
|
174
|
+
}, [q, load]);
|
|
175
|
+
|
|
176
|
+
const recompress = async () => {
|
|
177
|
+
setBusy('compress');
|
|
178
|
+
setNotice('');
|
|
179
|
+
try {
|
|
180
|
+
const data = await api('/compress', { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}' });
|
|
181
|
+
setNotice(`已重新压缩:${data.stats.units} 个单元,${data.stats.playbooks} 个加权压缩条目`);
|
|
182
|
+
await load(q);
|
|
183
|
+
} catch (err) {
|
|
184
|
+
setError(String(err.message || err));
|
|
185
|
+
} finally {
|
|
186
|
+
setBusy('');
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const digest = async () => {
|
|
191
|
+
setBusy('digest');
|
|
192
|
+
setNotice('');
|
|
193
|
+
try {
|
|
194
|
+
const data = await api('/digest', {
|
|
195
|
+
method: 'POST',
|
|
196
|
+
headers: { 'content-type': 'application/json' },
|
|
197
|
+
body: JSON.stringify({ dryRun: false, all: true }),
|
|
198
|
+
});
|
|
199
|
+
setNotice(`归纳完成:${data.sessions} 个会话,新增 ${data.runsAdded} run / ${data.notesAdded} note`);
|
|
200
|
+
await load(q);
|
|
201
|
+
} catch (err) {
|
|
202
|
+
setError(String(err.message || err));
|
|
203
|
+
} finally {
|
|
204
|
+
setBusy('');
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const save = async () => {
|
|
209
|
+
setBusy('save');
|
|
210
|
+
setNotice('');
|
|
211
|
+
setError('');
|
|
212
|
+
try {
|
|
213
|
+
const data = await api('/settings', {
|
|
214
|
+
method: 'POST',
|
|
215
|
+
headers: { 'content-type': 'application/json' },
|
|
216
|
+
body: JSON.stringify({
|
|
217
|
+
memory: {
|
|
218
|
+
compactThreshold: Number(memory.compactThreshold),
|
|
219
|
+
compactWeight: Number(memory.compactWeight),
|
|
220
|
+
similarity: Number(memory.similarity),
|
|
221
|
+
},
|
|
222
|
+
context: {
|
|
223
|
+
nudgeGrowthTokens: Number(context.nudgeGrowthTokens),
|
|
224
|
+
maxContextLimit: String(context.maxContextLimit),
|
|
225
|
+
},
|
|
226
|
+
}),
|
|
227
|
+
});
|
|
228
|
+
setMemory(data.memory);
|
|
229
|
+
setContext(data.context);
|
|
230
|
+
setNotice('阈值已保存(长上下文阈值由 billion-context 逐请求读取,无需重启)');
|
|
231
|
+
await load(q);
|
|
232
|
+
} catch (err) {
|
|
233
|
+
setError(String(err.message || err));
|
|
234
|
+
} finally {
|
|
235
|
+
setBusy('');
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const tabStyle = (active) => ({
|
|
240
|
+
padding: '7px 14px',
|
|
241
|
+
borderRadius: 8,
|
|
242
|
+
border: `1px solid ${active ? C.accent : C.line}`,
|
|
243
|
+
background: active ? 'color-mix(in srgb, ' + C.accent + ' 8%, transparent)' : C.bg,
|
|
244
|
+
color: active ? C.accent : C.text,
|
|
245
|
+
cursor: 'pointer',
|
|
246
|
+
fontSize: 13,
|
|
247
|
+
fontFamily: 'inherit',
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
return h(
|
|
251
|
+
'div',
|
|
252
|
+
{
|
|
253
|
+
className: 'dshm',
|
|
254
|
+
// The slot hands the page a fixed-height box: this element is the scroll
|
|
255
|
+
// container, and the inner div keeps the reading column centered.
|
|
256
|
+
style: {
|
|
257
|
+
boxSizing: 'border-box',
|
|
258
|
+
width: '100%',
|
|
259
|
+
height: '100%',
|
|
260
|
+
maxHeight: '100%',
|
|
261
|
+
overflowY: 'auto',
|
|
262
|
+
overflowX: 'hidden',
|
|
263
|
+
WebkitOverflowScrolling: 'touch',
|
|
264
|
+
padding: '28px 34px 60px',
|
|
265
|
+
color: C.text,
|
|
266
|
+
font: '14px/1.6 var(--dsh-font-family-content, system-ui)',
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
h(
|
|
270
|
+
'div',
|
|
271
|
+
{ style: { maxWidth: 1000, margin: '0 auto' } },
|
|
272
|
+
h(
|
|
273
|
+
'div',
|
|
274
|
+
{ style: { display: 'flex', alignItems: 'center', gap: 12, marginBottom: 4 } },
|
|
275
|
+
h('h1', { style: { fontSize: 20, margin: 0, fontWeight: 700 } }, '跨会话记忆'),
|
|
276
|
+
h('span', { style: { color: C.muted, fontSize: 12.5 } },
|
|
277
|
+
`${stats.units || 0} 个单元 · ${stats.playbooks || 0} 个加权压缩 · ${stats.notes || 0} 条原始 note`),
|
|
278
|
+
),
|
|
279
|
+
h('div', { style: { color: C.muted, fontSize: 12.5, marginBottom: 18 } },
|
|
280
|
+
'把过去的回合归纳成 run / note,再把相似的 note 加权合并成一条:出现越多、跨会话越多,权重越高。'),
|
|
281
|
+
|
|
282
|
+
h('div', { style: { display: 'flex', gap: 8, marginBottom: 18 } },
|
|
283
|
+
h('button', { style: tabStyle(tab === 'units'), onClick: () => setTab('units') }, '记忆'),
|
|
284
|
+
h('button', { style: tabStyle(tab === 'settings'), onClick: () => setTab('settings') }, '阈值'),
|
|
285
|
+
h('div', { style: { flex: 1 } }),
|
|
286
|
+
h('button', {
|
|
287
|
+
onClick: digest,
|
|
288
|
+
disabled: busy === 'digest',
|
|
289
|
+
style: { padding: '7px 14px', borderRadius: 8, border: `1px solid ${C.line}`, background: C.bg, color: C.text, cursor: 'pointer', fontSize: 13, fontFamily: 'inherit' },
|
|
290
|
+
}, busy === 'digest' ? '归纳中…' : '归纳新回合'),
|
|
291
|
+
h('button', {
|
|
292
|
+
onClick: recompress,
|
|
293
|
+
disabled: busy === 'compress',
|
|
294
|
+
style: { padding: '7px 14px', borderRadius: 8, border: `1px solid ${C.accent}`, background: C.accent, color: '#fff', cursor: 'pointer', fontSize: 13, fontFamily: 'inherit' },
|
|
295
|
+
}, busy === 'compress' ? '压缩中…' : '重新压缩'),
|
|
296
|
+
),
|
|
297
|
+
|
|
298
|
+
notice ? h('div', { style: { marginBottom: 14, fontSize: 12.5, color: '#1a7f4b' } }, notice) : null,
|
|
299
|
+
error ? h('div', { style: { marginBottom: 14, fontSize: 12.5, color: '#c0392b' } }, error) : null,
|
|
300
|
+
|
|
301
|
+
tab === 'units'
|
|
302
|
+
? h(
|
|
303
|
+
'div',
|
|
304
|
+
null,
|
|
305
|
+
h('input', {
|
|
306
|
+
value: q,
|
|
307
|
+
onChange: (event) => setQ(event.target.value),
|
|
308
|
+
placeholder: '搜索记忆:报错、命令、文件名、结论…',
|
|
309
|
+
style: { width: '100%', boxSizing: 'border-box', padding: '9px 12px', borderRadius: 9, border: `1px solid ${C.line}`, background: C.bg, color: C.text, fontSize: 13.5, fontFamily: 'inherit', marginBottom: 8 },
|
|
310
|
+
}),
|
|
311
|
+
units.length === 0
|
|
312
|
+
? h('div', { style: { padding: '36px 0', color: C.muted, fontSize: 13 } }, '没有匹配的记忆。先点「归纳新回合」,或者换个关键词。')
|
|
313
|
+
: units.map((unit) =>
|
|
314
|
+
h(UnitRow, {
|
|
315
|
+
key: unit.id,
|
|
316
|
+
unit,
|
|
317
|
+
open: openId === unit.id,
|
|
318
|
+
onToggle: () => setOpenId(openId === unit.id ? '' : unit.id),
|
|
319
|
+
})),
|
|
320
|
+
)
|
|
321
|
+
: h(
|
|
322
|
+
'div',
|
|
323
|
+
{ style: { display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(320px, 1fr))', gap: 22 } },
|
|
324
|
+
h(
|
|
325
|
+
'section',
|
|
326
|
+
{ style: { border: `1px solid ${C.line}`, borderRadius: 12, padding: '18px 20px' } },
|
|
327
|
+
h('div', { style: { fontWeight: 600, marginBottom: 12 } }, '再压缩阈值'),
|
|
328
|
+
h(Field, {
|
|
329
|
+
label: '合并多少条才升级为压缩条目',
|
|
330
|
+
value: memory.compactThreshold,
|
|
331
|
+
onChange: (v) => setMemory({ ...memory, compactThreshold: v }),
|
|
332
|
+
suffix: '条',
|
|
333
|
+
hint: '相似度达到下一条阈值的 note 数量;1 表示只要相近就合并。',
|
|
334
|
+
}),
|
|
335
|
+
h(Field, {
|
|
336
|
+
label: '合并后权重下限',
|
|
337
|
+
value: memory.compactWeight,
|
|
338
|
+
onChange: (v) => setMemory({ ...memory, compactWeight: v }),
|
|
339
|
+
suffix: '0–1',
|
|
340
|
+
hint: '权重 = 频次×0.35 + 跨会话×0.25 + 人工×0.2 + 新鲜度×0.2。低于此值的组合仍按普通记忆显示。',
|
|
341
|
+
}),
|
|
342
|
+
h(Field, {
|
|
343
|
+
label: '相似度阈值',
|
|
344
|
+
value: memory.similarity,
|
|
345
|
+
onChange: (v) => setMemory({ ...memory, similarity: v }),
|
|
346
|
+
suffix: '0.2–0.9',
|
|
347
|
+
hint: '两条 note 的 token 重叠占比(含于较短一方)。越高越保守,越低越爱合并。',
|
|
348
|
+
}),
|
|
349
|
+
),
|
|
350
|
+
h(
|
|
351
|
+
'section',
|
|
352
|
+
{ style: { border: `1px solid ${C.line}`, borderRadius: 12, padding: '18px 20px' } },
|
|
353
|
+
h('div', { style: { fontWeight: 600, marginBottom: 12 } }, '上下文压缩阈值(billion-context)'),
|
|
354
|
+
h(Field, {
|
|
355
|
+
label: '增长触发阈值',
|
|
356
|
+
value: context.nudgeGrowthTokens,
|
|
357
|
+
onChange: (v) => setContext({ ...context, nudgeGrowthTokens: v }),
|
|
358
|
+
suffix: 'tokens',
|
|
359
|
+
hint: '一次对话新增多少内容后开始考虑折叠。调小=更早折叠(更省、更早丢细节)。',
|
|
360
|
+
}),
|
|
361
|
+
h(Field, {
|
|
362
|
+
label: '窗口压力阈值',
|
|
363
|
+
value: context.maxContextLimit,
|
|
364
|
+
onChange: (v) => setContext({ ...context, maxContextLimit: v }),
|
|
365
|
+
suffix: '窗口占比',
|
|
366
|
+
hint: '如 60%:上下文用到窗口的 60% 就每轮催促折叠,直到降下来。95% 是紧急层。',
|
|
367
|
+
}),
|
|
368
|
+
h('div', { style: { color: C.muted, fontSize: 11.5, marginTop: 4, wordBreak: 'break-all' } }, `写入 ${context.path || ''}`),
|
|
369
|
+
),
|
|
370
|
+
h('div', { style: { gridColumn: '1 / -1' } },
|
|
371
|
+
h('button', {
|
|
372
|
+
onClick: save,
|
|
373
|
+
disabled: busy === 'save',
|
|
374
|
+
style: { padding: '9px 20px', borderRadius: 9, border: `1px solid ${C.accent}`, background: C.accent, color: '#fff', cursor: 'pointer', fontSize: 13.5, fontFamily: 'inherit' },
|
|
375
|
+
}, busy === 'save' ? '保存中…' : '保存阈值')),
|
|
376
|
+
),
|
|
377
|
+
),
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function MemoryIcon({ size }) {
|
|
382
|
+
return h('span', { style: { fontSize: (size || 18) - 2, lineHeight: 1, display: 'inline-block' } }, '🧠');
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
return {
|
|
386
|
+
inject: ['slots'],
|
|
387
|
+
apply(ctx) {
|
|
388
|
+
// The page itself: `main` + a `sidebar.panellist` entry keyed by the same
|
|
389
|
+
// id is how a plugin owns a full screen instead of a decoration slot.
|
|
390
|
+
ctx.slots.inject('main', () =>
|
|
391
|
+
ctx.slots.register({ name: 'main', key: 'dsh-rosemary-memory', inject: () => ({}) }, MemoryPage));
|
|
392
|
+
ctx.slots.inject('sidebar.panellist', () =>
|
|
393
|
+
ctx.slots.register({ name: 'sidebar.panellist', id: 'dsh-rosemary-memory', order: 40, label: '记忆' }, MemoryIcon));
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
},
|
|
397
|
+
});
|