Skip to content

Commit d9947ec

Browse files
authored
feat(choro): CLI 命令由服务端推导,五个入口共用(#607) (#627)
fix(choro): 地志提示里的命令改成能直接粘贴就跑的一条(#607)
2 parents 53db5a2 + 38ceaa3 commit d9947ec

15 files changed

Lines changed: 420 additions & 30 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@
142142

143143
## 🎨 面板
144144

145+
- **仓库地志的指引改成「贴一条能直接跑的命令」,面板带复制按钮(#607)**。原本文案是「在仓库目录里跑 `dsh-mneme-choro <仓库路径> --apply`」,两处都是错的:插件装在 profile 依赖里,npm 只为**全局**安装把 shim 放进 PATH,`<profile>/node_modules/.bin` 不在 PATH 上,照抄必然 `command not found`;而参数与 cwd 无关,cd 进仓库裸跑只打印帮助、什么都不做。issue 作者的原话是「完全就是 AI 自己编的」。
146+
147+
- **命令串从插件自身位置推导,不信 PATH 也不信 cwd**:新增 `src/wiki/cli-hint.js`,从调用方的 `import.meta.url` 逐级上溯(上限 6 层)找「同时存在 `bin/dsh-mneme-choro.mjs` 与 `package.json`」的目录,产出 `node "<entry>" <repo> --apply`;Windows 用双引号、POSIX 用单引号并把内嵌单引号转成 `'\''`(安装路径带空格是常态)。找不到入口返回 `null`,调用方退回通用文案——不硬编一个假路径。**刻意不按固定 dirname 次数推导**:`src/` 与 `lib/` 同层、`src/wiki/` 在第二层、`bin/` 在第一层,写死层数只要有一处调用方挪位置就悄悄算错,而算错的表现正是又给一条跑不起来的命令。**也只给 `node <entry>` 一种形态**:`.bin` 的 shim 在 hoisted 与 pnpm isolated 两种 linker 下位置不同,猜错就是再给一条错的——宁可只给一条一定对的。
148+
- **五个入口共用同一份推导**:面板空态(`GET /choro/repos` 新增 `cliCommand` 字段)、`/choro` 命令的「仓库没登记」、`/choro/page` 的 409 hint、三个基于 DSH 工具错误的 choro 工具(`choro_module_map` / `choro_read_page` / `choro_search` 的 repo 解析与 page-missing)、T1 的 `not-registered` 拒绝原因。面板 bundle 拿不到 CLI 路径,所以命令串由服务端算好一起下发;缺字段时面板显示 fallback 文案(`node <安装目录>/node_modules/@modusensus/dsh-mneme/bin/dsh-mneme-choro.mjs …`),不在前端再拼一条可能跑不起来的命令。
149+
- **面板补三件事**:无仓库空态加「复制命令」按钮(`navigator.clipboard`,成功 `announce` 「已复制」1.5s 复位、失败明说「请手动选中」——非安全上下文里 `clipboard` 不存在,静默失败等于点了没反应);`wiki.empty.noRepo.where` 点名开关位置「设置 → 记忆库设置 → 功能开关 → 它什么时候写进去 → 仓库地志(chorography)」并说明要重启;`wiki.state.pageMissing` 从「重跑 CLI」改成「点顶部『重建骨架』即可」(面板里本来就有那个按钮,让人离开面板去跑命令是多余的一步)。
150+
- **验证**:新增 `test/wiki-cli-hint.test.js` 5 例——四个不同深度的真实模块位置必须收敛到同一个入口(写死层数的实现会在这里红)、推导出的入口必须真实存在且包根同时有 `bin/` 与 `package.json`、裁剪树(只有 `bin/` 或只有 `package.json`)返回 `null`、两种平台的引号与单引号转义逐字比对、以及**真的用推导出的入口 spawn 一次 `--help` 并断言退出码 0**(这条 issue 的验收标准就是「用户照着能跑起来」,字符串断言证明不了)。定向 135 例全绿;`npm run sync` 后 `lib/wiki/cli-hint.js` 同步产出、check-sync 96 个文件一致。
145151
- **M.O.(工作流)下架**:`group.mo` 从 `FEATURE_GROUPS` 摘掉,首屏位(`FEATURE_FIRST_SCREEN`)一并收回,首屏可点项预算回到 8。三个旗标(`actionPipelineEnabled` / `actionRunEnabled` / `actionNudgeEnabled`)**仍留在 schema 与设置白名单里、语义零改动**,座位注册逻辑与 `/mo-run` 命令一字未改——只是面板不再渲染那一组。
146152

147153
- **这是一次产品决策的反转,不是放宽**:v0.9.0 是产品主人点名「M.O. 别藏在里面」才把它提进首屏并豁免了 ≤8 的软预算。本轮主人决定先下架,所以 `test/client.test.js` 的首屏锁从三组收紧回两组,注释里把反转理由写清楚——免得下次有人照着旧注释把组加回来。

‎dsh-mneme/lib/api.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { timingSafeEqual, randomBytes } from "node:crypto";
77
import { TYPES } from "./store.js";
88
import { TYPE_FILE, renderMirrorText, renderFileHeader, parseHumanEdits, MIRROR_READONLY_TYPES } from "./mirror.js";
99
import { langOf } from "./lang.js";
10+
// issue #607:面板空态要显示「能直接粘贴就跑」的命令,而面板 bundle 拿不到
11+
// CLI 入口路径(独立 bundle 不能 import src)——所以由这条路由把命令算好发给它。
12+
import { choroCliCommandFrom } from "./wiki/cli-hint.js";
1013
// op 形状谓词单一份(评审 P3:两份皮会让形状修洞漏一边)。
1114
import { isValidFactOps } from "./profile-facts.js";
1215
import { computeHeat } from "./heat.js";
@@ -58,6 +61,10 @@ import {
5861
NUDGE_AUTO_BUDGET_MS
5962
} from "./pipeline-nudge.js";
6063

64+
// 面板空态与 409 hint 共用的 CLI 命令串(#607):本文件在 src/ 与 lib/ 同一层,
65+
// 模块加载时推导一次。推导不出时为 null,两处各自退回通用说法。
66+
const CHORO_CLI = choroCliCommandFrom(import.meta.url, "<repo>");
67+
6168
// headers:少数端点(/export 附件下载)需要追加 Content-Disposition 等响应头。
6269
function sendJson(res, status, payload, headers = {}) {
6370
res.writeHead(status, { "Content-Type": "application/json; charset=utf-8", ...headers });
@@ -3083,7 +3090,9 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
30833090
}
30843091
}
30853092
}
3086-
sendJson(res, 200, { repos, autoRefreshed, autoRefreshError });
3093+
// cliCommand(#607):面板空态直接把它贴出来,用户复制即用。推导不出
3094+
// 入口时为 null,面板据此退回通用说法(不硬编一个假路径)。
3095+
sendJson(res, 200, { repos, autoRefreshed, autoRefreshError, cliCommand: CHORO_CLI });
30873096
}
30883097
});
30893098

@@ -3138,7 +3147,7 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
31383147
slug: page.slug,
31393148
relPath: page.relPath,
31403149
hint: page.error === "page-missing"
3141-
? "run `dsh-mneme-choro <repo> --apply` to rebuild"
3150+
? `run ${CHORO_CLI ? `\`${CHORO_CLI}\`` : "`dsh-mneme-choro <repo> --apply`"} to rebuild`
31423151
: "check the local file and its permissions"
31433152
});
31443153
return;

‎dsh-mneme/lib/choro-command.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import { gitHead } from "./wiki/t0.js";
88
import { renderChoroBlock } from "./wiki/inject.js";
99
import { STR } from "./lang.js";
10+
// #607:提示里的 CLI 命令必须给绝对路径入口(profile 装的插件没有全局 shim),
11+
// 光写 `dsh-mneme-choro` 让用户照抄只会 command not found。推导不出入口时
12+
// 返回 null,文案退回通用说法——宁可少说,也不给一条跑不起来的命令。
13+
import { choroCliCommandFrom } from "./wiki/cli-hint.js";
1014

1115
export const CHORO_COMMAND = "choro";
1216

@@ -46,6 +50,9 @@ function staleness(repo) {
4650
* @param {() => string} [opts.language]
4751
*/
4852
export function createChoroHandler({ choro, config, language = () => "zh" } = {}) {
53+
// 本文件在 `src/` 与 `lib/` 的同一层(bin/ 的父级),推导一次就够,不必每次
54+
// 报错都去碰文件系统。
55+
const cli = choroCliCommandFrom(import.meta.url, language() === "en" ? "<repo path>" : "<仓库路径>");
4956
return (invocation) => {
5057
const lang = language() === "en" ? "en" : "zh";
5158
const S = STR.choroCommandUsage[lang];
@@ -58,7 +65,7 @@ export function createChoroHandler({ choro, config, language = () => "zh" } = {}
5865
const cwd = typeof raw === "string" ? raw.trim() : "";
5966
if (!cwd) return { kind: "error", text: S.noCwd };
6067
const repo = choro ? choro.repoByCwd(cwd) : null;
61-
if (!repo) return { kind: "error", text: S.noRepo(cwd) };
68+
if (!repo) return { kind: "error", text: S.noRepo(cwd, cli) };
6269
const outline = choro.outline(repo.id);
6370
if (!outline || outline.modules.length === 0) {
6471
return { kind: "success", text: S.title(repo.name, 0, dateOnly(repo.updated_at)) + "\n" + S.empty };
@@ -76,7 +83,7 @@ export function createChoroHandler({ choro, config, language = () => "zh" } = {}
7683
if (rest > 0) lines.push(S.truncated(rest));
7784
const head = staleness(repo);
7885
if (head.state === "fresh") lines.push(S.fresh(shortSha(head.head)));
79-
else if (head.state === "stale") lines.push(S.stale(shortSha(head.baseline), shortSha(head.head)));
86+
else if (head.state === "stale") lines.push(S.stale(shortSha(head.baseline), shortSha(head.head), cli));
8087
else lines.push(S.noGit);
8188
lines.push(S.hint);
8289
return { kind: "success", text: lines.filter(Boolean).join("\n") };

‎dsh-mneme/lib/client.js‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,11 @@ window.__ModuleLoader__.load({
327327
// 里、也根本不需要 cd(它收的是路径参数)。文案改成三步:说清这是什么、给出
328328
// 可直接点的生成入口、把 CLI 兜底写成带绝对路径的完整示例。
329329
"wiki.empty.noRepo": "这是 mneme 插件的「仓库地志」:把仓库代码结构铺成可浏览的模块页。还没有登记任何仓库——在下面填入仓库目录的绝对路径,点「生成骨架」就会直接生成并登记。也可以在本机命令行里跑(这个命令不在 PATH 里,要给绝对路径):",
330-
"wiki.empty.disabled": "「仓库地志」未启用:打开 设置 → 记忆(mneme) → 功能 → 仓库地志,打开开关后重启 DSH 生效。",
330+
"wiki.empty.disabled": "chorography(仓库地志)未启用。它属于本插件(记忆库):打开「设置 → 记忆库设置 → 功能开关 → 它什么时候写进去 → 仓库地志(chorography)」,然后重启 DSH 生效。",
331+
"wiki.empty.noRepo.where": "仓库地志属于本插件(记忆库),开关在「设置 → 记忆库设置 → 功能开关 → 它什么时候写进去」里的「仓库地志(chorography)」,打开后要重启 DSH 才生效。",
332+
"wiki.empty.copy": "复制命令",
333+
"wiki.empty.copied": "已复制",
334+
"wiki.empty.copyFailed": "复制失败,请手动选中命令文本。",
331335
"wiki.empty.pickRepo": "已登记多个仓库,选一个:",
332336
"wiki.empty.generate": "生成骨架",
333337
"wiki.empty.generating": "生成中…",
@@ -356,7 +360,7 @@ window.__ModuleLoader__.load({
356360
"wiki.search.placeholder": "在页面里检索…",
357361
"wiki.state.loading": "加载中…",
358362
"wiki.state.pickPage": "从左侧选一个模块页。",
359-
"wiki.state.pageMissing": "索引还在、但磁盘上的页面文件没了:点上面的「重建骨架」重新生成(等价于 node \"<profile>/node_modules/@modusensus/dsh-mneme/bin/dsh-mneme-choro.mjs\" \"<仓库路径>\" --apply)。",
363+
"wiki.state.pageMissing": "索引还在、磁盘上的页面文件缺失。点顶部「重建骨架」即可重新生成(不用离开这个面板)。",
360364
"wiki.state.unavailable": "chorography 查询装配不可用(宿主环境缺件)。",
361365
"wiki.state.error": "加载失败",
362366
"wiki.tree.title": "模块",
@@ -1376,7 +1380,7 @@ window.__ModuleLoader__.load({
13761380
"memory.status.token.op.dream_summarize": "总览摘要",
13771381
"memory.status.token.op.sleep_pattern": "睡眠模式",
13781382
"memory.status.token.op.write_admission": "写入准入",
1379-
"memory.status.error": "加载失败"
1383+
"memory.status.error": "加载失败",
13801384
},
13811385
en: {
13821386
"memory.panel.empty": "No memories yet",
@@ -1390,7 +1394,11 @@ window.__ModuleLoader__.load({
13901394
"wiki.rebuild.modules": "modules",
13911395
"wiki.rebuild.pages": "pages",
13921396
"wiki.empty.noRepo": "This is the mneme plugin's \"Repo Atlas\": it turns a repository's code structure into browsable module pages. No repository is registered yet — put the repository directory's absolute path below and press \"Generate skeleton\" to build and register it. You can also run it from a terminal (the command is not on PATH, so give the absolute path):",
1393-
"wiki.empty.disabled": "Repo Atlas is disabled: open Settings → Memory (mneme) → Features → Repo Atlas, turn it on, then restart DSH to apply.",
1397+
"wiki.empty.disabled": "chorography (Repo Atlas) is disabled. It belongs to this plugin (memory store): enable Settings → Mneme settings → Features → When it gets written → Repo atlas (chorography), then restart DSH.",
1398+
"wiki.empty.noRepo.where": "The repo atlas belongs to this plugin (memory store). The switch lives under Settings → Mneme settings → Features → When it gets written → Repo atlas (chorography); restart DSH after turning it on.",
1399+
"wiki.empty.copy": "Copy command",
1400+
"wiki.empty.copied": "Copied",
1401+
"wiki.empty.copyFailed": "Copy failed — select the command text manually.",
13941402
"wiki.empty.pickRepo": "Multiple repositories registered — pick one:",
13951403
"wiki.empty.generate": "Generate skeleton",
13961404
"wiki.empty.generating": "Generating…",
@@ -1419,7 +1427,7 @@ window.__ModuleLoader__.load({
14191427
"wiki.search.placeholder": "Search pages…",
14201428
"wiki.state.loading": "Loading…",
14211429
"wiki.state.pickPage": "Pick a module page from the tree.",
1422-
"wiki.state.pageMissing": "The index is still here but the page file is gone from disk: press \"Rebuild skeleton\" above to regenerate it (equivalent to node \"<profile>/node_modules/@modusensus/dsh-mneme/bin/dsh-mneme-choro.mjs\" \"<repo-path>\" --apply).",
1430+
"wiki.state.pageMissing": "The index is there but the page file is missing. Use the “Rebuild” button in the top bar to regenerate it — no need to leave this panel.",
14231431
"wiki.state.unavailable": "chorography queries are unavailable (host wiring missing).",
14241432
"wiki.state.error": "Failed to load",
14251433
"wiki.tree.title": "Modules",
@@ -12319,7 +12327,8 @@ window.__ModuleLoader__.load({
1231912327
const [state, setState] = useState({
1232012328
phase: "loading", repos: [], repo: null, modules: [], edges: [], page: null,
1232112329
hits: [], q: "", selected: null, disabled: false, error: null,
12322-
rebuilding: false, rebuildNote: null, path: "", flags: null, t1: null, toast: ""
12330+
rebuilding: false, rebuildNote: null, path: "", flags: null, t1: null, toast: "",
12331+
cliCommand: null, copied: false
1232312332
});
1232412333
// T1 跑完要重读当前仓库的模块树,但轮询的闭包里拿不到最新的 state.repo:
1232512334
// 用 ref 记住加载成功的仓库 id,避免把整棵树塞进 effect 依赖里。
@@ -12357,7 +12366,10 @@ window.__ModuleLoader__.load({
1235712366
if (status === 409) { setState((s) => ({ ...s, phase: "disabled" })); return; }
1235812367
if (status === 503) { setState((s) => ({ ...s, phase: "unavailable" })); return; }
1235912368
const repos = data.repos ?? [];
12360-
if (repos.length === 0) { setState((s) => ({ ...s, phase: "no-repo", repos: [], repo: null })); return; }
12369+
// #607:命令串由服务端算好(面板 bundle 拿不到 CLI 入口路径)。缺字段时
12370+
// 留 null,空态退回 fallback 文案——不在这里拼一条可能跑不起来的命令。
12371+
const cliCommand = typeof data.cliCommand === "string" && data.cliCommand ? data.cliCommand : null;
12372+
if (repos.length === 0) { setState((s) => ({ ...s, phase: "no-repo", repos: [], repo: null, cliCommand })); return; }
1236112373
const ref = repoRef ?? (repos.length === 1 ? repos[0].id : null);
1236212374
if (ref === null) { setState((s) => ({ ...s, phase: "pick-repo", repos, repo: null })); return; }
1236312375
const mod = await readJson(await apiFetch(`/api/dsh-mneme/choro/modules?repo=${encodeURIComponent(ref)}`));
@@ -12424,6 +12436,19 @@ window.__ModuleLoader__.load({
1242412436
}
1242512437
}, [state.repo, state.rebuilding, load, t]);
1242612438

12439+
// 复制命令(#607):issue 作者的原话是「用户怎么可能手动跑起来这个命令」。
12440+
// 光把命令写好不够——长路径手抄一遍就是一次抄错的机会,所以给复制按钮。
12441+
// 失败必须说出来:剪贴板在非安全上下文里不存在,静默失败等于点了没反应。
12442+
const copyCommand = useCallback(() => {
12443+
const text = state.cliCommand;
12444+
if (!text) return;
12445+
const done = () => { setState((s) => ({ ...s, copied: true })); announce(t("wiki.empty.copied")); setTimeout(() => setState((s) => ({ ...s, copied: false })), 1500); };
12446+
const failed = () => announce(t("wiki.empty.copyFailed"));
12447+
const clip = typeof navigator !== "undefined" ? navigator.clipboard : null;
12448+
if (clip && typeof clip.writeText === "function") { clip.writeText(text).then(done, failed); return; }
12449+
failed();
12450+
}, [state.cliCommand, t]);
12451+
1242712452
// #607 的另一半:空态那条路也得能直接生成。这里收的是一个【路径】而不是
1242812453
// 一个已登记的 repo id——新目录第一次进来自报家门用的就是它。body 同时
1242912454
// 带 repo(有的话)与 path:A 线按 §3.2 扩出来的处理取 path、老实现取
@@ -12590,8 +12615,13 @@ window.__ModuleLoader__.load({
1259012615
state.rebuildNote && h("div", { className: "mneme-wikirebuildnote mneme-wikirunwide" }, state.rebuildNote),
1259112616
h("details", { className: "mneme-wikicli" },
1259212617
h("summary", null, t("wiki.empty.cliSummary")),
12593-
h("pre", { className: "mneme-wikipre" }, t("wiki.empty.cliCmd")),
12618+
state.cliCommand
12619+
? h("div", { style: { display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap" } },
12620+
h("code", { className: "mneme-set-token", style: { flex: 1, minWidth: 0, wordBreak: "break-all" } }, state.cliCommand),
12621+
h("button", { className: "mneme-btn", type: "button", onClick: copyCommand }, state.copied ? t("wiki.empty.copied") : t("wiki.empty.copy")))
12622+
: h("pre", { className: "mneme-wikipre" }, t("wiki.empty.cliCmd")),
1259412623
h("p", { className: "mneme-wikiclihint" }, t("wiki.empty.cliHint"))),
12624+
h("p", { className: "mneme-set-hint", style: { marginTop: 10 } }, t("wiki.empty.noRepo.where")),
1259512625
state.toast && h("div", { className: "mneme-toast", role: "status" }, state.toast));
1259612626
}
1259712627
if (state.phase === "pick-repo") {

0 commit comments

Comments
 (0)