@tokenroll/llmdoc 3.1.1 → 3.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/dist/cli.js +73 -43
- package/dist/commands/hook.js +26 -8
- package/dist/lib/fs.js +30 -2
- package/dist/lib/git.js +4 -0
- package/dist/lib/workspace.js +10 -5
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -41,11 +41,39 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
41
41
|
}
|
|
42
42
|
throw error;
|
|
43
43
|
}
|
|
44
|
-
program
|
|
45
|
-
|
|
44
|
+
program
|
|
45
|
+
.name("llmdoc")
|
|
46
|
+
.description("面向 LLM 的项目知识库 CLI:渐进检索 llmdoc/ 文档,维护 revision 台账")
|
|
47
|
+
.version(readPackageVersion(), "--version", "输出 CLI 版本")
|
|
48
|
+
.helpOption("-h, --help", "显示帮助")
|
|
49
|
+
.helpCommand("help [command]", "显示指定命令的帮助")
|
|
50
|
+
.showHelpAfterError("(用 --help 查看用法)");
|
|
51
|
+
program
|
|
52
|
+
.option("--json", "以 JSON 输出(经 schema 校验)")
|
|
53
|
+
.option("--cursor <cursor>", "上次输出截断处的游标,从该处继续")
|
|
54
|
+
.option("--budget <tokens>", "输出 token 预算,超出即截断并返回 cursor", parseInteger)
|
|
55
|
+
.option("--limit <n>", "最多返回条数", parseInteger);
|
|
56
|
+
program.addHelpText("after", [
|
|
57
|
+
"",
|
|
58
|
+
"按用途速查:",
|
|
59
|
+
" 检索(只读) tree → index / search / context → show",
|
|
60
|
+
" 状态诊断 status · delta · validate",
|
|
61
|
+
" 结构改写 new · mv · fingerprint · init-state · commit",
|
|
62
|
+
" 维护诊断 prune · upgrade",
|
|
63
|
+
" 集成 hook · serve",
|
|
64
|
+
"",
|
|
65
|
+
"常用示例:",
|
|
66
|
+
" llmdoc tree --docs 全局地图,展开到文档级",
|
|
67
|
+
" llmdoc search \"重试策略\" --limit 5 词法检索文档",
|
|
68
|
+
" llmdoc context --files src/api/retry.ts 按源码反查应读文档",
|
|
69
|
+
" llmdoc show api-client/retry-policy.mdx 读取正文",
|
|
70
|
+
" llmdoc commit -m \"docs: ...\" 校验并提交 llmdoc 写集",
|
|
71
|
+
"",
|
|
72
|
+
"所有检索命令支持 --json / --budget / --limit;输出被截断时带 --cursor 继续。"
|
|
73
|
+
].join("\n"));
|
|
46
74
|
program
|
|
47
75
|
.command("tree")
|
|
48
|
-
.description("输出 llmdoc 全局地图")
|
|
76
|
+
.description("输出 llmdoc 全局地图(默认停在 topic 层)")
|
|
49
77
|
.option("--docs", "展开到文档级")
|
|
50
78
|
.action((commandOptions) => {
|
|
51
79
|
const rootDir = findProjectRoot(cwd);
|
|
@@ -54,9 +82,9 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
54
82
|
});
|
|
55
83
|
program
|
|
56
84
|
.command("index")
|
|
57
|
-
.description("列出文档元数据索引")
|
|
58
|
-
.option("--topic <topic>")
|
|
59
|
-
.option("--kind <kind>")
|
|
85
|
+
.description("列出文档元数据索引(不读正文即可判断相关性)")
|
|
86
|
+
.option("--topic <topic>", "只列指定 topic 下的文档")
|
|
87
|
+
.option("--kind <kind>", "只列指定类型: architecture | guide | reference")
|
|
60
88
|
.action((commandOptions) => {
|
|
61
89
|
const rootDir = findProjectRoot(cwd);
|
|
62
90
|
output.push(writeOutput("index", runIndex({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
@@ -64,25 +92,25 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
64
92
|
program
|
|
65
93
|
.command("show")
|
|
66
94
|
.description("读取一个或多个文档正文")
|
|
67
|
-
.argument("<path...>")
|
|
95
|
+
.argument("<path...>", "llmdoc/ 下的相对路径,如 api-client/retry-policy.mdx")
|
|
68
96
|
.action((paths) => {
|
|
69
97
|
const rootDir = findProjectRoot(cwd);
|
|
70
98
|
output.push(writeOutput("show", runShow({ ...globalOptions, cwd: rootDir, paths }), globalOptions.json));
|
|
71
99
|
});
|
|
72
100
|
program
|
|
73
101
|
.command("search")
|
|
74
|
-
.description("按词法检索 llmdoc 文档")
|
|
75
|
-
.argument("<query>")
|
|
76
|
-
.option("--topic <topic>")
|
|
77
|
-
.option("--kind <kind>")
|
|
102
|
+
.description("按词法检索 llmdoc 文档(front matter、标题与正文,返回 snippet)")
|
|
103
|
+
.argument("<query>", "检索词")
|
|
104
|
+
.option("--topic <topic>", "限定 topic")
|
|
105
|
+
.option("--kind <kind>", "限定类型: architecture | guide | reference")
|
|
78
106
|
.action((query, commandOptions) => {
|
|
79
107
|
const rootDir = findProjectRoot(cwd);
|
|
80
108
|
output.push(writeOutput("search", runSearch({ ...globalOptions, ...commandOptions, cwd: rootDir, query }), globalOptions.json));
|
|
81
109
|
});
|
|
82
110
|
program
|
|
83
111
|
.command("context")
|
|
84
|
-
.description("按源码文件反查应读文档")
|
|
85
|
-
.requiredOption("--files <files...>")
|
|
112
|
+
.description("按源码文件反查应读文档(含 requires 前置闭包)")
|
|
113
|
+
.requiredOption("--files <files...>", "源码文件路径,可多个")
|
|
86
114
|
.action((commandOptions) => {
|
|
87
115
|
const rootDir = findProjectRoot(cwd);
|
|
88
116
|
output.push(writeOutput("context", runContext({
|
|
@@ -109,17 +137,17 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
109
137
|
});
|
|
110
138
|
program
|
|
111
139
|
.command("delta")
|
|
112
|
-
.description("查看代码变化对应的文档影响面")
|
|
113
|
-
.option("--scope <scope...>")
|
|
140
|
+
.description("查看代码变化对应的文档影响面(决定 update 走 light 还是 deep)")
|
|
141
|
+
.option("--scope <scope...>", "限定参与比对的代码路径")
|
|
114
142
|
.action((commandOptions) => {
|
|
115
143
|
const rootDir = findProjectRoot(cwd);
|
|
116
144
|
output.push(writeOutput("delta", runDelta({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
117
145
|
});
|
|
118
146
|
program
|
|
119
147
|
.command("fingerprint")
|
|
120
|
-
.description("
|
|
121
|
-
.option("--update <path...>")
|
|
122
|
-
.option("--all", "
|
|
148
|
+
.description("把文档 validatedRevision 刷新到当前 HEAD")
|
|
149
|
+
.option("--update <path...>", "只刷新指定文档")
|
|
150
|
+
.option("--all", "刷新全部文档并推进 baseline")
|
|
123
151
|
.action((commandOptions) => {
|
|
124
152
|
const rootDir = findProjectRoot(cwd);
|
|
125
153
|
output.push(writeOutput("fingerprint", runFingerprint({
|
|
@@ -154,9 +182,34 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
154
182
|
noVerify: commandOptions.verify === false
|
|
155
183
|
}), globalOptions.json));
|
|
156
184
|
});
|
|
185
|
+
program
|
|
186
|
+
.command("new")
|
|
187
|
+
.description("在 llmdoc/ 下生成文档脚手架")
|
|
188
|
+
.argument("<path>", "目标相对路径,如 api-client/retry-policy.mdx")
|
|
189
|
+
.requiredOption("--kind <kind>", "文档类型: architecture | guide | reference")
|
|
190
|
+
.option("--description <description>", "front matter 一句话描述")
|
|
191
|
+
.action((targetPath, commandOptions) => {
|
|
192
|
+
const rootDir = findProjectRoot(cwd);
|
|
193
|
+
output.push(writeOutput("new", runNew({
|
|
194
|
+
...globalOptions,
|
|
195
|
+
cwd: rootDir,
|
|
196
|
+
path: targetPath,
|
|
197
|
+
kind: commandOptions.kind,
|
|
198
|
+
description: commandOptions.description
|
|
199
|
+
}), globalOptions.json));
|
|
200
|
+
});
|
|
201
|
+
program
|
|
202
|
+
.command("mv")
|
|
203
|
+
.description("移动/重命名文档或整个 topic,并重写内部引用")
|
|
204
|
+
.argument("<from>", "源路径")
|
|
205
|
+
.argument("<to>", "目标路径")
|
|
206
|
+
.action((from, to) => {
|
|
207
|
+
const rootDir = findProjectRoot(cwd);
|
|
208
|
+
output.push(writeOutput("mv", runMove({ ...globalOptions, cwd: rootDir, from, to }), globalOptions.json));
|
|
209
|
+
});
|
|
157
210
|
program
|
|
158
211
|
.command("prune")
|
|
159
|
-
.description("
|
|
212
|
+
.description("输出只读收敛报告(增长趋势、重复候选、小文档合并候选)")
|
|
160
213
|
.option("--report", "输出只读收敛报告")
|
|
161
214
|
.action((commandOptions) => {
|
|
162
215
|
const rootDir = findProjectRoot(cwd);
|
|
@@ -170,7 +223,7 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
170
223
|
const rootDir = findProjectRootOrNull(cwd) ?? cwd;
|
|
171
224
|
output.push(writeOutput("upgrade", await runUpgrade({ ...globalOptions, ...commandOptions, cwd: rootDir }), globalOptions.json));
|
|
172
225
|
});
|
|
173
|
-
const hookCommand = program.command("hook");
|
|
226
|
+
const hookCommand = program.command("hook").description("供编辑器/Agent hooks 调用的只读信号(异常时 fail-open)");
|
|
174
227
|
hookCommand
|
|
175
228
|
.command("session-start")
|
|
176
229
|
.description("输出 SessionStart 短状态信号")
|
|
@@ -201,29 +254,6 @@ export async function runCli(argv, cwd = process.cwd(), stdin = "") {
|
|
|
201
254
|
const rootDir = findProjectRoot(cwd);
|
|
202
255
|
output.push(await runServe({ cwd: rootDir, port: commandOptions.port }));
|
|
203
256
|
});
|
|
204
|
-
program
|
|
205
|
-
.command("new")
|
|
206
|
-
.argument("<path>")
|
|
207
|
-
.requiredOption("--kind <kind>")
|
|
208
|
-
.option("--description <description>")
|
|
209
|
-
.action((targetPath, commandOptions) => {
|
|
210
|
-
const rootDir = findProjectRoot(cwd);
|
|
211
|
-
output.push(writeOutput("new", runNew({
|
|
212
|
-
...globalOptions,
|
|
213
|
-
cwd: rootDir,
|
|
214
|
-
path: targetPath,
|
|
215
|
-
kind: commandOptions.kind,
|
|
216
|
-
description: commandOptions.description
|
|
217
|
-
}), globalOptions.json));
|
|
218
|
-
});
|
|
219
|
-
program
|
|
220
|
-
.command("mv")
|
|
221
|
-
.argument("<from>")
|
|
222
|
-
.argument("<to>")
|
|
223
|
-
.action((from, to) => {
|
|
224
|
-
const rootDir = findProjectRoot(cwd);
|
|
225
|
-
output.push(writeOutput("mv", runMove({ ...globalOptions, cwd: rootDir, from, to }), globalOptions.json));
|
|
226
|
-
});
|
|
227
257
|
try {
|
|
228
258
|
await program.parseAsync(argv, { from: "user" });
|
|
229
259
|
}
|
package/dist/commands/hook.js
CHANGED
|
@@ -29,26 +29,44 @@ export function runHook(options) {
|
|
|
29
29
|
}
|
|
30
30
|
function runSessionStart(cwd, stdin) {
|
|
31
31
|
const source = inferSource(stdin);
|
|
32
|
+
const lifecycle = source === "compact" ? "compact re-entry" : "cold start";
|
|
32
33
|
try {
|
|
33
34
|
const workspace = loadWorkspace(cwd);
|
|
34
35
|
const delta = analyzeDelta(workspace);
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
|
|
36
|
+
const baseline = workspace.meta?.baseline.revision ? workspace.meta.baseline.revision.slice(0, 7) : "缺失";
|
|
37
|
+
const freshness = delta.git.baselineBehindHead === null ? "落后未知" : delta.git.baselineBehindHead === 0 ? "与 HEAD 同步" : `落后 HEAD ${delta.git.baselineBehindHead} commit`;
|
|
38
|
+
const parts = [`llmdoc ${lifecycle}`, `baseline ${baseline}(${freshness})`];
|
|
39
|
+
if (delta.impacts.length === 0 && delta.needsReview.length === 0) {
|
|
40
|
+
parts.push("文档无待处理影响");
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
parts.push(`受影响 ${delta.impacts.length} 篇, 待复核 ${delta.needsReview.length} 篇 → 建议先看 npx @tokenroll/llmdoc delta`);
|
|
44
|
+
}
|
|
45
|
+
return parts.join("; ");
|
|
39
46
|
}
|
|
40
47
|
catch {
|
|
41
|
-
return
|
|
48
|
+
return `llmdoc ${lifecycle}; 状态读取失败,检索不受影响`;
|
|
42
49
|
}
|
|
43
50
|
}
|
|
44
51
|
function runStop(cwd) {
|
|
45
52
|
try {
|
|
46
53
|
const workspace = loadWorkspace(cwd);
|
|
47
54
|
const delta = analyzeDelta(workspace);
|
|
48
|
-
const
|
|
55
|
+
const unmappedCount = delta.unmappedCommittedPaths.length + delta.unmappedDirtyPaths.length;
|
|
56
|
+
const shouldUpdate = delta.impacts.length > 0 || unmappedCount > 0;
|
|
57
|
+
if (!shouldUpdate) {
|
|
58
|
+
return { continue: true };
|
|
59
|
+
}
|
|
60
|
+
const summary = [
|
|
61
|
+
delta.impacts.length > 0 ? `${delta.impacts.length} 篇文档受代码变更影响` : null,
|
|
62
|
+
unmappedCount > 0 ? `${unmappedCount} 个代码路径未映射到任何文档` : null
|
|
63
|
+
]
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join(", ");
|
|
66
|
+
const reasons = delta.reasons.length > 0 ? `信号: ${delta.reasons.join("; ")}` : "";
|
|
49
67
|
return {
|
|
50
68
|
continue: true,
|
|
51
|
-
|
|
69
|
+
systemMessage: `llmdoc: ${summary},建议运行 /llmdoc:update(先用 npx @tokenroll/llmdoc delta 查看影响面)。${reasons}`
|
|
52
70
|
};
|
|
53
71
|
}
|
|
54
72
|
catch (error) {
|
|
@@ -61,7 +79,7 @@ function runStop(cwd) {
|
|
|
61
79
|
function runCompact() {
|
|
62
80
|
return {
|
|
63
81
|
continue: true,
|
|
64
|
-
systemMessage: "
|
|
82
|
+
systemMessage: "即将 compact:请在 summary 中写入 LLMDOC_STATE(active goal、已读 llmdoc 文档、关键结论与不变量、用户决策、next step、open risks)。恢复后若该状态仍充分,直接继续,不要重放 tree/show。"
|
|
65
83
|
};
|
|
66
84
|
}
|
|
67
85
|
function inferSource(stdin) {
|
package/dist/lib/fs.js
CHANGED
|
@@ -9,9 +9,29 @@ export function findProjectRoot(startDir) {
|
|
|
9
9
|
throw new CliError("未找到 llmdoc/ 目录,请在仓库内运行该命令。", 2);
|
|
10
10
|
}
|
|
11
11
|
export function findProjectRootOrNull(startDir) {
|
|
12
|
-
|
|
12
|
+
const start = path.resolve(startDir);
|
|
13
|
+
const gitRoot = findNearestGitRootOrNull(start);
|
|
14
|
+
if (gitRoot) {
|
|
15
|
+
return isDirectory(path.join(gitRoot, "llmdoc")) ? gitRoot : null;
|
|
16
|
+
}
|
|
17
|
+
// Git 是 llmdoc 有效性与 delta 语义的基础;这里只为仓库初始化前的
|
|
18
|
+
// 临时目录保留兼容回退。存在 Git 边界时绝不能越界命中同级仓库。
|
|
19
|
+
let current = start;
|
|
20
|
+
while (true) {
|
|
21
|
+
if (isDirectory(path.join(current, "llmdoc"))) {
|
|
22
|
+
return current;
|
|
23
|
+
}
|
|
24
|
+
const parent = path.dirname(current);
|
|
25
|
+
if (parent === current) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
current = parent;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function findNearestGitRootOrNull(startDir) {
|
|
32
|
+
let current = startDir;
|
|
13
33
|
while (true) {
|
|
14
|
-
if (fs.existsSync(path.join(current, "
|
|
34
|
+
if (fs.existsSync(path.join(current, ".git"))) {
|
|
15
35
|
return current;
|
|
16
36
|
}
|
|
17
37
|
const parent = path.dirname(current);
|
|
@@ -21,6 +41,14 @@ export function findProjectRootOrNull(startDir) {
|
|
|
21
41
|
current = parent;
|
|
22
42
|
}
|
|
23
43
|
}
|
|
44
|
+
function isDirectory(candidate) {
|
|
45
|
+
try {
|
|
46
|
+
return fs.statSync(candidate).isDirectory();
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
24
52
|
export function normalizeRepoRelativePath(input) {
|
|
25
53
|
const normalized = input.replaceAll("\\", "/");
|
|
26
54
|
if (!normalized || normalized.startsWith("/") || normalized.includes("\0")) {
|
package/dist/lib/git.js
CHANGED
|
@@ -26,6 +26,10 @@ export function gitCommitExists(rootDir, revision) {
|
|
|
26
26
|
});
|
|
27
27
|
return result.status === 0;
|
|
28
28
|
}
|
|
29
|
+
// shallow clone(CI 常态)里历史 commit 不可达,revision 校验需要据此降级而不是误报陈旧。
|
|
30
|
+
export function isShallowRepository(rootDir) {
|
|
31
|
+
return runGitSafe(rootDir, ["rev-parse", "--is-shallow-repository"]) === "true";
|
|
32
|
+
}
|
|
29
33
|
export function readGitState(rootDir, baselineRevision) {
|
|
30
34
|
if (!isGitRepository(rootDir)) {
|
|
31
35
|
return {
|
package/dist/lib/workspace.js
CHANGED
|
@@ -6,7 +6,7 @@ import { CliError } from "./errors.js";
|
|
|
6
6
|
import { estimateTokens, extractCodeRefs, extractLinks, extractTitle, resolveDocLink, stripMarkdownLiterals, validateCodeRefTags } from "./markdown.js";
|
|
7
7
|
import { assertDocKindMatchesShape, parseDocTargetShape } from "./doc-shape.js";
|
|
8
8
|
import { normalizeRepoRelativePath, repoPath, resolveInsideRoot } from "./fs.js";
|
|
9
|
-
import { gitCommitExists } from "./git.js";
|
|
9
|
+
import { gitCommitExists, isShallowRepository } from "./git.js";
|
|
10
10
|
import { validateDocFrontmatter, validateMeta } from "./schema.js";
|
|
11
11
|
export function loadWorkspace(rootDir) {
|
|
12
12
|
const configuredLlmdocDir = path.join(rootDir, "llmdoc");
|
|
@@ -318,12 +318,17 @@ export function validateWorkspace(workspace) {
|
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
320
|
if (workspace.meta) {
|
|
321
|
+
// shallow clone 中历史 commit 天然不可达:revision 缺失降级为 warning,
|
|
322
|
+
// 否则任何 fetch-depth:1 的 CI 都会误报陈旧。
|
|
323
|
+
const shallow = isShallowRepository(workspace.rootDir);
|
|
324
|
+
const revisionSeverity = shallow ? "warning" : "error";
|
|
325
|
+
const shallowHint = shallow ? "(shallow clone;完整校验请用 fetch-depth: 0 或 git fetch --unshallow)" : "";
|
|
321
326
|
if (!gitCommitExists(workspace.rootDir, workspace.meta.baseline.revision)) {
|
|
322
327
|
issues.push({
|
|
323
|
-
severity:
|
|
328
|
+
severity: revisionSeverity,
|
|
324
329
|
code: "meta.baseline.revision.missing",
|
|
325
330
|
path: "llmdoc/meta.json",
|
|
326
|
-
message: `baseline.revision 不存在于当前 git 历史: ${workspace.meta.baseline.revision}`
|
|
331
|
+
message: `baseline.revision 不存在于当前 git 历史: ${workspace.meta.baseline.revision}${shallowHint}`
|
|
327
332
|
});
|
|
328
333
|
}
|
|
329
334
|
const metaPaths = Object.keys(workspace.meta.documents).sort();
|
|
@@ -350,10 +355,10 @@ export function validateWorkspace(workspace) {
|
|
|
350
355
|
const revision = workspace.meta.documents[metaPath]?.validatedRevision;
|
|
351
356
|
if (revision && !gitCommitExists(workspace.rootDir, revision)) {
|
|
352
357
|
issues.push({
|
|
353
|
-
severity:
|
|
358
|
+
severity: revisionSeverity,
|
|
354
359
|
code: "meta.document.revision.missing",
|
|
355
360
|
path: `llmdoc/${metaPath}`,
|
|
356
|
-
message: `validatedRevision 不存在于当前 git 历史: ${revision}`
|
|
361
|
+
message: `validatedRevision 不存在于当前 git 历史: ${revision}${shallowHint}`
|
|
357
362
|
});
|
|
358
363
|
}
|
|
359
364
|
}
|