Skip to content

Commit df63069

Browse files
committed
feat(dsh-go): add native memory panel for the dsh-go mobile app
dsh-go renders a Flutter-native UI and never mounts the Web client, so the memory library had no panel on phones — only the memory_* tools. This adds a panel that ships inside the package: package.json declares a dshGo manifest and src/index.js mounts the panel from ctx.inject(["dshGo"]), so hosts without the dsh-go bridge (desktop, headless) never load src/dsh-go/** at all. The panel reads and writes through the route handlers src/api.js already registers: host.js resolves them with ctx.get("webServer").match(pathname) and calls the handler in-process with a fake req/res pair. No second data path, no extra port, no direct SQLite access — auth policy, status codes and payload shapes stay exactly what the HTTP API returns. Layout follows the Web panel: three native tabs (memories / entities / settings), with settings splitting into "settings | status" because App tabs are 80dp wide minimum and a 297dp panel only fits three of them. Feature toggles are laid out inline under "memory engine" instead of hiding in a sub-page, and every caption reuses the zh i18n strings of the Web panel instead of inventing new prose. dsh-go feeds are push-based — data() runs once per panel instance and refresh only repaints — so every action re-loads and pushes a fresh payload to all open feeds (throttled at 4/s). Verification: view checker clean across 18 panel states (2-6 KB, far below the 120-node / 32 KiB budget); end-to-end against the real dsh-go bridge 27/27 (three tabs, settings/status switch, five sub-pages, filters, search, entity detail, conflict queue, create -> detail -> delete, unknown action 409); node --test green apart from the pre-existing runtime-loader failures on this machine; check-sync reports src/ and lib/ identical.
1 parent 2a5e72b commit df63069

26 files changed

Lines changed: 5534 additions & 0 deletions

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dsh-mneme 是 DSH 宿主的记忆插件(蒸馏 / 注入 / 检索 / 巩固 / sc
5454
| 检索辅助 | `src/search/{bm25,adaptive}.js`、`src/vector-index.js`、`src/embedding.js`、`src/local-embedder.js`、`src/reranker.js` | BM25 / 自适应阈值 / 向量索引 / 嵌入 / 重排 |
5555
| 热度 | `src/heat.js` | 纯函数遗忘曲线(opt-in) |
5656
| 复用统计 | `src/recall-stats.js` | recall_runs 只读聚合(#217,Top-N 召回 + 僵尸率) |
57+
| dsh-go 原生面板 | `src/dsh-go/`(`panel.js` 状态机 / `host.js` 进程内派发 / `views/*`) | 手机上(dsh-go App)的记忆库面板:三个原生页签(记忆/实体/设置),设置内再分「设置 / 状态」。取数与写回走 `src/api.js` 的同一批路由 handler,不新增口径([docs/DSH-GO.md](dsh-mneme/docs/DSH-GO.md)) |
5758
| 工作流 | `src/pipeline.js` + `.mneme/pipelines/*.json`(项目级) | M.O. 读侧:两层来源(项目层优先、包内 `pipelines/` 为全局兜底)→ `mneme.pipeline/v1` 投影([docs/WORKFLOWS.md](dsh-mneme/docs/WORKFLOWS.md)) |
5859
| 绕行与候选 | `src/candidates.js`(+ `<memoryDir>/candidates/<项目键>/`) | M.O. 的出口与草稿层:绕行的分档判定(R0/R1 记账先走、R2/R3 停下问人 + 机械兜底清单 + 闸门保护)、候选的归并/归档/升格(进仓库要人批) |
5960
| 冷启动 | `src/bootstrap.js` | 从仓库文件反向构建初始记忆(POST /bootstrap) |

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
## [Unreleased]
44

5+
- **dsh-go 原生面板:手机上也能看、能改记忆库了(面板从「独立插件」搬进本包)**:dsh-go 是 Flutter 原生界面,Web 面板在那里看不到,所以之前只能靠 11 个 `memory_*` 工具间接操作。这一版把面板做成本包自带的一块:`package.json` 声明 `dshGo` 清单,`src/index.js` 在 `ctx.inject(["dshGo"], …)` 里挂载,桌面 / headless 宿主没有 dsh-go 桥时整块不加载。
6+
7+
- **不新开口径**:取数与写回都走 `src/api.js` 已经注册的同一批路由 handler——`src/dsh-go/host.js` 用 `ctx.get("webServer").match(pathname)` 拿到 handler,伪造 req/res 在进程内调用。这样 `apiToken` 策略、错误码、字段全部照旧;也不另开端口、不直连 SQLite。
8+
- **组织照搬电脑端**:顶层三个原生页签(记忆 / 实体 / 设置),设置页内两格开关分「设置 / 状态」。App 的页签条每格有 80dp 下限,四个页签在手机上会折行,所以状态并入设置页而不是硬塞第四个页签。
9+
- **设置页功能开关不再收进子页面**:运行模式 + 分组开关直接铺在「记忆引擎」下,改动即时写回 `/features`(`flagsMeta` 里标出「已改 N 项 · 待重启生效 M 项」)。
10+
- **文案复用 Web 面板的 zh i18n**:画像 / 规则 / 人格 / 命令 / 向量 / 外部访问的说明文字取 `memory.settings.*Hint`,冲突裁决提示取 `conflictQueue.applyHint`;自造的解释性小字全部删掉——面板只说事实,不复述实现。
11+
- **推送式 feed**:dsh-go 的 `data()` 每个面板实例只跑一次,`refresh` 效果只重画视图。所以每个动作结尾都由面板自己把新 payload 推给所有打开的 feed(节流 4/s),否则点完按钮界面仍是旧数据。
12+
- **不读外部访问令牌**:`GET /external-api` 会顺手生成 Bearer token 并落库,面板只做开关 / 端口 / 监听三项。
13+
- **验证**:新增 `test/dsh-go-panel.test.js`,锁视图预算(19 种面板状态,1.7–7.4 KB、≤54 节点,上限 120 节点 / 32 KiB)、三个页签、功能开关内联与文案回归;`check-sync` 一致。
514
- **记忆库从「侧栏 footer 挤一个入口 + 居中浮层」改成宿主原生面板位:侧栏一颗图标进 `sidebar.panellist`,正文落 `main` 整页(参照 dsh-baleen,用户 2026-11-04 提的)**:此前唯一的完整入口是 `sidebar.footer.action`,点开是一个 `createPortal` 到 `document.body` 的居中浮层——它不是宿主的面板,占着屏幕中央却不参与宿主的布局与导航状态,Esc/焦点环/背板点击都得自己实现一遍,而截图里它看起来又很像「一个新窗口」。宿主早就给了成对的座位:`sidebar.panellist`(侧栏面板列表,list 槽)与 `main`(正文区,keyed 槽),侧栏一行的点击由宿主 `PanelRow` 自己调 `layout.selectPanel(id)`,插件只交一颗图标和一片内容。**main 的 `key` 必须与侧栏的 `id` 逐字相同**(两者都取 `dsh-mneme`)——宿主按 `options.key` 在 keyed 槽里找 entry,对不上会抛 `layout.selectPanel: main panel "…" is not registered`;这条不是防御性写作,是宿主 `layout.js` 里真会抛的那一行。
615

716
- **模板块只交「图标」和「页面」,不自己造按钮、不做关闭**:宿主 `PanelRow` 已经渲染了 `button` + `aria-label` + `Tooltip` + `aria-current` + active 高亮,插件再套一层按钮就是两颗可聚焦元素(Tab 停两次、读屏报两次),而 `onClick` 还得自己去 `selectPanel`——那是宿主自己的活。回到会话也不由我们提供:宿主所有「打开会话」的路径都走 `selectPanel(null)`(`workspace.js` 的 `openSession` / `openWorkspaceSession` 都是 `replaceMain(…, "reveal")`),baleen 的面板同样没有返回按钮。`main` 槽只给出 `min-width:0; overflow:hidden` 的容器,注册者必须自己铺满 `100%` 并自管滚动,所以外层新增 `.mneme-panel`。

‎dsh-mneme/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ dsh web
137137
138138
官方设置面板 → 「记忆库设置」→「记忆」标签:按类型浏览、全文搜索;启用向量搜索后可用「语义」切换做向量召回。
139139

140+
**手机端的原生面板(dsh-go)**:本包同时声明 `dshGo` 清单,装在有 [dsh-go](https://github.com/ccch1mneyyy/dsh-Go) 桥的宿主上时,「记忆库」以原生面板出现(对话详情「插件」页签 / 全部功能)。记忆库按宿主独立存放:手机与电脑上的 mneme 各有一份 `memory.db`,**两者之间没有在线同步**;目前只能靠分享包 `mneme.share/v1`(`dsh-mneme share` / `receive`)手动迁移,是一次性交接,不是实时同步。三个原生页签(记忆 / 实体 / 设置),设置页内再分「设置 / 状态」,功能开关直接铺在设置页上、改完即时生效。面板与 Web 面板共用同一批后端路由(进程内派发,不新开端口),区别只在呈现层;桌面 / headless 宿主没有这个服务,面板不加载。见 [`docs/DSH-GO.md`](docs/DSH-GO.md)。
141+
140142
### 用户设置(画像 / 规则)与自定义指令 ⚙️
141143

142144
官方设置面板 → 「记忆库设置」标签:

‎dsh-mneme/docs/DSH-GO.md‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# dsh-go 原生面板
2+
3+
> 客户端仓库:[ccch1mneyyy/dsh-Go](https://github.com/ccch1mneyyy/dsh-Go)
4+
5+
> dsh-mneme 一直是「Web 面板 + 11 个记忆工具 + 自动注入」。手机 App **[dsh-go](https://github.com/ccch1mneyyy/dsh-Go)**(DSH 的 Android 客户端:Flutter 原生壳,手机本机跑引擎,也能遥控电脑)是 Flutter 原生界面,看不到 Web 面板——这个域就是给它的原生面板:与电脑端同一套功能划分(记忆 / 实体 / 设置 / 状态)、同一批后端路由。
6+
>
7+
> 记忆库是**按宿主独立存放**的:面板读写的是它所在那台宿主的 `memory.db`,与另一台设备上的库不互通。目前没有在线同步,只能通过分享包 `mneme.share/v1` 手动迁移,且不是实时同步。
8+
9+
## 职责边界
10+
11+
- **只做展示与转发**:取数、写回都走 `src/api.js` 已注册的 HTTP 路由 handler(进程内派发),不新增路由、不直连 SQLite、不写第二套口径。
12+
- **不碰记忆本体**:面板不参与蒸馏、注入、巩固、冲突判定;它只是这些能力的一个操作面(和 Web 面板同级)。
13+
- **可选能力**:桌面 / headless 宿主没有 [dsh-go](https://github.com/ccch1mneyyy/dsh-Go) 桥,这个域整块不加载(`ctx.inject(["dshGo"], …)` 的回调不会被调度)。
14+
- **不重复 Web 面板**:`lib/client.js`(浏览器 bundle)照旧是 Web 面板的唯一实现;两边共享的是**后端**,不是视图代码。
15+
16+
## 对外接口
17+
18+
### 1. 包清单
19+
20+
`package.json` 的 `dshGo` 字段([dsh-go](https://github.com/ccch1mneyyy/dsh-Go) 桥扫描已启用插件的 package.json,见 `dsh-go-bridge/lib/native/manifest.js`):
21+
22+
```json
23+
"dshGo": { "v": 1, "title": {...}, "icon": "book", "contributes": { "panels": [{ "id": "mneme", ... }] } }
24+
```
25+
26+
面板的 `pkg` 由桥按「注册调用所在 fiber 所属的 loader entry」反查,所以**不需要独立包**:在 mneme 自己的插件上下文里调 `ctx.dshGo.panel("mneme", …)`,归谁一目了然。
27+
28+
### 2. 挂载点
29+
30+
`src/index.js` 在 webServer 块内、`createApi` 之后:
31+
32+
```js
33+
ctx.inject(["dshGo"], (dshCtx) => {
34+
mountDshGoPanel(dshCtx);
35+
});
36+
```
37+
38+
### 3. 视图协议(v2)
39+
40+
`buildView(state)` 返回 `{v:2, type:"stack", accent:"app", children:[…]}`,顶层是 App 原生 `tabs`:记忆 / 实体 / 设置。设置页顶部两格 `segmented`(设置 | 状态)——电脑端是四个页签,但 App 的页签条每格有 80dp 下限,四格在手机上会折行,所以放三格。
41+
42+
## 内部文件
43+
44+
| 文件 | 一句话 |
45+
|---|---|
46+
| `src/dsh-go/panel.js` | 状态机、取数、动作(`mountDshGoPanel`)。动作跑完自己把新数据推给所有打开的 feed |
47+
| `src/dsh-go/host.js` | 进程内派发:`ctx.get("webServer").match(path)` 拿到 mneme 自己的 handler,伪造 req/res 调用 |
48+
| `src/dsh-go/text.js` | 类型标签、开关分组、时间与 scope 格式化 |
49+
| `src/dsh-go/views/root.js` | `buildView`:顶层页签组合 |
50+
| `src/dsh-go/views/{common,memory,entity,settings,status}.js` | 各分区视图;`common.js` 是小组件构造器 |
51+
52+
文案取自 Web 面板的 zh i18n(`lib/client.js` 里的 `memory.*` 键),面板不再自造解释性文案。
53+
54+
## 已知坑
55+
56+
- **feed 是推送式的**:`data()` 每个面板实例只跑一次,`refresh` 效果只让 App 重画视图、不会重跑 `data()`。所以每个动作结尾都要 `broadcast()`(向所有 emit 推新 payload),否则点了按钮界面还是旧数据。推送节流 4/s。
57+
- **不能走 HTTP**:[dsh-go](https://github.com/ccch1mneyyy/dsh-Go) 桥的路由守卫(`installFallbackGuard`)对没有合法签名的请求直接 401(`x-dsh-go-guard: reject`),浏览器 cookie 是宿主签的、进程内也拿不到。进程内 `webServer.match()` 绕开这一层,且复用同一批 handler。
58+
- **`/external-api` 的 GET 有副作用**:会顺手生成 Bearer token 并落库。面板只做开关 / 端口 / 监听,不读令牌。
59+
- **`/update` 不接受 `type`**:编辑已有记忆时类型只读展示,新建才给选择器。
60+
- **`/create` 不接受 scope**:归属 Agent / 工作区两个输入只在编辑已有记忆时出现。
61+
- **节点预算**:面板 120 节点 / 8 层 / 32 KiB,`data` 32 KiB。列表行只下发 `summary`(`content` 截 120 字),全文留在详情取,否则条目一多就超预算。
62+
- **不要用 `metric.caption` 放单位**:`caption` 固定渲染在数值下方一行;v2 的 metric 整串数值同一字号,所以单位拼进 value(`57 条`),App 侧只有自带模型卡才会拆小字单位。

‎dsh-mneme/lib/dsh-go/host.js‎

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// In-process call into the mneme HTTP API.
2+
//
3+
// The panel runs inside the engine process, exactly where mneme registered its
4+
// routes. Going through real HTTP would hit the dsh-go bridge guard (401 unless
5+
// the request carries the process launch token, which is never written to disk),
6+
// so we dispatch the registered route handler directly and never leave the
7+
// process. Everything stays on the local web server table: no sockets, no auth.
8+
import { EventEmitter } from "node:events";
9+
10+
const PREFIX = "/api/dsh-mneme";
11+
const DEFAULT_TIMEOUT = 5000;
12+
13+
function makeRequest(url, method, body) {
14+
const req = new EventEmitter();
15+
req.url = url;
16+
req.method = method;
17+
req.headers = {
18+
host: "127.0.0.1",
19+
"content-type": "application/json",
20+
"content-length": String(body === undefined ? 0 : Buffer.byteLength(body)),
21+
};
22+
req.socket = { remoteAddress: "127.0.0.1", encrypted: false };
23+
req.resume = () => {};
24+
req.pause = () => {};
25+
req.setEncoding = () => {};
26+
// The handler attaches its body listeners synchronously when invoked, so the
27+
// body is only released afterwards by flushRequest(); emitting during
28+
// construction would fire 'data'/'end' with no listener attached and the
29+
// handler's readBody() would wait forever.
30+
req.flush = () => {
31+
queueMicrotask(() => {
32+
if (body !== undefined) req.emit("data", Buffer.from(body));
33+
req.emit("end");
34+
});
35+
};
36+
return req;
37+
}
38+
39+
function makeResponse() {
40+
const chunks = [];
41+
let settled;
42+
const done = new Promise((resolve) => {
43+
settled = resolve;
44+
});
45+
const headers = {};
46+
const res = {
47+
statusCode: 200,
48+
headersSent: false,
49+
done,
50+
writeHead(status, extra) {
51+
res.statusCode = status;
52+
res.headersSent = true;
53+
if (extra !== undefined && extra !== null) {
54+
for (const key of Object.keys(extra)) headers[key.toLowerCase()] = extra[key];
55+
}
56+
return res;
57+
},
58+
setHeader(key, value) {
59+
headers[key.toLowerCase()] = value;
60+
return res;
61+
},
62+
getHeader(key) {
63+
return headers[key.toLowerCase()];
64+
},
65+
removeHeader(key) {
66+
delete headers[key.toLowerCase()];
67+
},
68+
write(chunk) {
69+
if (chunk !== undefined && chunk !== null && typeof chunk !== "function") {
70+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
71+
}
72+
return true;
73+
},
74+
end(chunk) {
75+
if (chunk !== undefined && chunk !== null && typeof chunk !== "function") {
76+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)));
77+
}
78+
settled();
79+
return res;
80+
},
81+
flushHeaders() {},
82+
};
83+
res.text = () => Buffer.concat(chunks).toString("utf8");
84+
res.bytes = () => Buffer.concat(chunks);
85+
return res;
86+
}
87+
88+
function timeout(ms) {
89+
return new Promise((_, reject) => {
90+
const timer = setTimeout(() => reject(new Error("mneme 接口超时")), ms);
91+
timer.unref?.();
92+
});
93+
}
94+
95+
/**
96+
* Resolve the mneme route handler and run it in-process.
97+
* @param {object} ctx - the plugin context (must expose get()).
98+
*/
99+
export function createHostCall(ctx) {
100+
return async function callMneme(path, options = {}) {
101+
const method = (options.method ?? "GET").toUpperCase();
102+
const body = options.body === undefined ? undefined : JSON.stringify(options.body);
103+
const url = new URL(PREFIX + path, "http://127.0.0.1");
104+
const webServer = ctx.get?.("webServer") ?? ctx.reflect?.get?.("webServer");
105+
if (webServer === undefined || typeof webServer.match !== "function") {
106+
throw new Error("宿主 webServer 不可用");
107+
}
108+
const route = webServer.match(url.pathname);
109+
if (route === undefined || route === null) throw new Error(`mneme 未注册 ${url.pathname}`);
110+
const req = makeRequest(url.pathname + url.search, method, body);
111+
const res = makeResponse();
112+
let run;
113+
try {
114+
run = Promise.resolve(route.handler(req, res));
115+
} finally {
116+
req.flush();
117+
}
118+
await Promise.race([run, res.done, timeout(options.timeoutMs ?? DEFAULT_TIMEOUT)]);
119+
const text = res.text();
120+
if (res.statusCode >= 400) {
121+
const detail = text.length > 200 ? `${text.slice(0, 200)}…` : text;
122+
const error = new Error(`mneme ${method} ${path} → ${res.statusCode} ${detail}`);
123+
error.status = res.statusCode;
124+
error.body = text;
125+
throw error;
126+
}
127+
if (options.raw === true) return text;
128+
if (text === "") return {};
129+
try {
130+
return JSON.parse(text);
131+
} catch {
132+
return { text };
133+
}
134+
};
135+
}

0 commit comments

Comments
 (0)