@tokenroll/llmdoc 3.5.3 → 3.6.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/assets/viewer-app.js +11 -11
- package/assets/viewer-detail.js +4 -4
- package/assets/viewer-graph.js +3 -3
- package/assets/viewer.html +19 -19
- package/dist/cli.js +68 -68
- package/dist/commands/adopt.js +6 -6
- package/dist/commands/commit.js +6 -6
- package/dist/commands/context.js +1 -1
- package/dist/commands/fingerprint.js +3 -3
- package/dist/commands/hook.js +62 -28
- package/dist/commands/init-state.js +5 -5
- package/dist/commands/mv.js +25 -12
- package/dist/commands/new.js +5 -5
- package/dist/commands/prune.js +13 -3
- package/dist/commands/search.js +1 -1
- package/dist/commands/serve.js +1 -1
- package/dist/commands/show.js +1 -1
- package/dist/commands/status.js +1 -1
- package/dist/commands/tree.js +1 -1
- package/dist/commands/upgrade.js +15 -15
- package/dist/lib/config.js +145 -0
- package/dist/lib/doc-shape.js +5 -5
- package/dist/lib/fs.js +8 -8
- package/dist/lib/git.js +6 -6
- package/dist/lib/markdown.js +5 -5
- package/dist/lib/output-schema.js +3 -3
- package/dist/lib/package-root.js +1 -1
- package/dist/lib/pagination.js +2 -2
- package/dist/lib/schema.js +8 -0
- package/dist/lib/state.js +16 -15
- package/dist/lib/viewer-http.js +2 -2
- package/dist/lib/workspace.js +32 -28
- package/package.json +1 -1
- package/schemas/config.schema.json +34 -0
- package/schemas/output.schema.json +12 -0
package/dist/lib/doc-shape.js
CHANGED
|
@@ -3,15 +3,15 @@ import { CliError } from "./errors.js";
|
|
|
3
3
|
const DOCUMENT_KINDS = ["architecture", "guide", "reference"];
|
|
4
4
|
export function parseDocTargetShape(repoRelativePath) {
|
|
5
5
|
if (!repoRelativePath.startsWith("llmdoc/")) {
|
|
6
|
-
throw new CliError("
|
|
6
|
+
throw new CliError("The target must be under llmdoc/.");
|
|
7
7
|
}
|
|
8
8
|
if (!repoRelativePath.endsWith(".mdx")) {
|
|
9
|
-
throw new CliError("
|
|
9
|
+
throw new CliError("The target must be an .mdx document.");
|
|
10
10
|
}
|
|
11
11
|
const llmdocPath = repoRelativePath.slice("llmdoc/".length);
|
|
12
12
|
const segments = llmdocPath.split("/");
|
|
13
13
|
if (segments.length !== 1 && segments.length !== 2) {
|
|
14
|
-
throw new CliError("V3
|
|
14
|
+
throw new CliError("V3 document paths allow only a root singleton or topic/file depth.");
|
|
15
15
|
}
|
|
16
16
|
const basename = path.posix.basename(llmdocPath);
|
|
17
17
|
const topic = segments.length === 2 ? (segments[0] ?? null) : null;
|
|
@@ -28,7 +28,7 @@ export function parseDocTargetShape(repoRelativePath) {
|
|
|
28
28
|
export function assertDocKindMatchesShape(shape) {
|
|
29
29
|
// V3 不设入口节点:topic 即纯目录,描述由 CLI 从文档 front matter 聚合。
|
|
30
30
|
if (shape.basename === "index.mdx") {
|
|
31
|
-
throw new CliError("V3
|
|
31
|
+
throw new CliError("V3 does not use index.mdx entry nodes. Use a regular name; llmdoc tree derives topic descriptions.");
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
export function validateMoveTargetShape(shape) {
|
|
@@ -46,5 +46,5 @@ export function assertDocumentKind(input) {
|
|
|
46
46
|
if (DOCUMENT_KINDS.includes(input)) {
|
|
47
47
|
return input;
|
|
48
48
|
}
|
|
49
|
-
throw new CliError(
|
|
49
|
+
throw new CliError(`Invalid kind: ${input}. Allowed values: ${DOCUMENT_KINDS.join(", ")}`);
|
|
50
50
|
}
|
package/dist/lib/fs.js
CHANGED
|
@@ -6,7 +6,7 @@ export function findProjectRoot(startDir) {
|
|
|
6
6
|
if (found) {
|
|
7
7
|
return found;
|
|
8
8
|
}
|
|
9
|
-
throw new CliError("
|
|
9
|
+
throw new CliError("No llmdoc/ directory was found. Run this command inside an llmdoc repository.", 2);
|
|
10
10
|
}
|
|
11
11
|
// new 是唯一允许在 llmdoc/ 尚不存在时运行的结构改写命令。
|
|
12
12
|
// 首次创建严格锁定最近 Git 根;无 Git 但已有 llmdoc/ 时保留原有兼容路径。
|
|
@@ -20,7 +20,7 @@ export function findProjectRootForNew(startDir) {
|
|
|
20
20
|
if (gitRoot) {
|
|
21
21
|
return gitRoot;
|
|
22
22
|
}
|
|
23
|
-
throw new CliError("
|
|
23
|
+
throw new CliError("No Git repository was found. Run `git init` before `llmdoc new`.", 2);
|
|
24
24
|
}
|
|
25
25
|
export function findProjectRootOrNull(startDir) {
|
|
26
26
|
const start = path.resolve(startDir);
|
|
@@ -66,14 +66,14 @@ function isDirectory(candidate) {
|
|
|
66
66
|
export function normalizeRepoRelativePath(input) {
|
|
67
67
|
const normalized = input.replaceAll("\\", "/");
|
|
68
68
|
if (!normalized || normalized.startsWith("/") || normalized.includes("\0")) {
|
|
69
|
-
throw new CliError(
|
|
69
|
+
throw new CliError(`Invalid path: ${input}`);
|
|
70
70
|
}
|
|
71
71
|
const parts = normalized.split("/").filter((part) => part !== ".");
|
|
72
72
|
if (parts.length === 0) {
|
|
73
|
-
throw new CliError(
|
|
73
|
+
throw new CliError(`Invalid path: ${input}`);
|
|
74
74
|
}
|
|
75
75
|
if (parts.some((part) => part === ".." || part === "")) {
|
|
76
|
-
throw new CliError(
|
|
76
|
+
throw new CliError(`Path must be a normalized repository-relative path: ${input}`);
|
|
77
77
|
}
|
|
78
78
|
return parts.join("/");
|
|
79
79
|
}
|
|
@@ -84,19 +84,19 @@ export function resolveInsideRoot(rootDir, repoRelativePath, options) {
|
|
|
84
84
|
const nearestExistingAncestor = findNearestExistingAncestor(candidate);
|
|
85
85
|
const ancestorRealPath = fs.realpathSync(nearestExistingAncestor);
|
|
86
86
|
if (!isWithinRoot(rootRealPath, ancestorRealPath)) {
|
|
87
|
-
throw new CliError(
|
|
87
|
+
throw new CliError(`Path escapes the repository directly or through a symlink: ${repoRelativePath}`);
|
|
88
88
|
}
|
|
89
89
|
if (fs.existsSync(candidate)) {
|
|
90
90
|
const candidateRealPath = fs.realpathSync(candidate);
|
|
91
91
|
if (!isWithinRoot(rootRealPath, candidateRealPath)) {
|
|
92
|
-
throw new CliError(
|
|
92
|
+
throw new CliError(`Path escapes the repository directly or through a symlink: ${repoRelativePath}`);
|
|
93
93
|
}
|
|
94
94
|
return candidateRealPath;
|
|
95
95
|
}
|
|
96
96
|
if (options?.allowMissing) {
|
|
97
97
|
return candidate;
|
|
98
98
|
}
|
|
99
|
-
throw new CliError(
|
|
99
|
+
throw new CliError(`Path does not exist: ${repoRelativePath}`);
|
|
100
100
|
}
|
|
101
101
|
export function isWithinRoot(rootRealPath, candidateRealPath) {
|
|
102
102
|
return candidateRealPath === rootRealPath || candidateRealPath.startsWith(`${rootRealPath}${path.sep}`);
|
package/dist/lib/git.js
CHANGED
|
@@ -8,7 +8,7 @@ function runGit(rootDir, args) {
|
|
|
8
8
|
encoding: "utf8"
|
|
9
9
|
});
|
|
10
10
|
if (result.status !== 0) {
|
|
11
|
-
throw new CliError((result.stderr || result.stdout || "
|
|
11
|
+
throw new CliError((result.stderr || result.stdout || "Git command failed").trim());
|
|
12
12
|
}
|
|
13
13
|
return result.stdout.trim();
|
|
14
14
|
}
|
|
@@ -48,7 +48,7 @@ export function readGitState(rootDir, baselineRevision) {
|
|
|
48
48
|
stagedPaths: [],
|
|
49
49
|
unstagedPaths: [],
|
|
50
50
|
untrackedPaths: [],
|
|
51
|
-
degradedReason: "
|
|
51
|
+
degradedReason: "The current directory is not a Git repository; state diagnostics are unavailable."
|
|
52
52
|
};
|
|
53
53
|
}
|
|
54
54
|
const headRevision = runGitSafe(rootDir, ["rev-parse", "--verify", "HEAD"]);
|
|
@@ -65,10 +65,10 @@ export function readGitState(rootDir, baselineRevision) {
|
|
|
65
65
|
: null;
|
|
66
66
|
let degradedReason = null;
|
|
67
67
|
if (!headRevision) {
|
|
68
|
-
degradedReason = isUnbornHead(rootDir) ? "HEAD
|
|
68
|
+
degradedReason = isUnbornHead(rootDir) ? "HEAD has no commit (the current branch is unborn)." : "Unable to resolve the HEAD commit.";
|
|
69
69
|
}
|
|
70
70
|
else if (baselineRevision && !gitCommitExists(rootDir, baselineRevision)) {
|
|
71
|
-
degradedReason = `baseline.revision
|
|
71
|
+
degradedReason = `baseline.revision does not exist in current Git history: ${baselineRevision}`;
|
|
72
72
|
}
|
|
73
73
|
return {
|
|
74
74
|
available: true,
|
|
@@ -116,10 +116,10 @@ export function readCommitsWithChangedPathsSince(rootDir, revision, headRevision
|
|
|
116
116
|
}
|
|
117
117
|
export function canAdvanceRevisions(gitState) {
|
|
118
118
|
if (!gitState.available || !gitState.headRevision) {
|
|
119
|
-
return { ok: false, reason: gitState.degradedReason ?? "
|
|
119
|
+
return { ok: false, reason: gitState.degradedReason ?? "Git state is unavailable." };
|
|
120
120
|
}
|
|
121
121
|
if (gitState.inProgressOperation) {
|
|
122
|
-
return { ok: false, reason: `${gitState.inProgressOperation}
|
|
122
|
+
return { ok: false, reason: `${gitState.inProgressOperation} is in progress; revisions cannot advance.` };
|
|
123
123
|
}
|
|
124
124
|
return { ok: true };
|
|
125
125
|
}
|
package/dist/lib/markdown.js
CHANGED
|
@@ -64,27 +64,27 @@ export function validateCodeRefTags(body) {
|
|
|
64
64
|
const raw = match[0] ?? "";
|
|
65
65
|
const attributes = match[1] ?? "";
|
|
66
66
|
if (!raw.endsWith("/>")) {
|
|
67
|
-
issues.push("CodeRef
|
|
67
|
+
issues.push("CodeRef must use the self-closing form <CodeRef ... />.");
|
|
68
68
|
continue;
|
|
69
69
|
}
|
|
70
70
|
const attrMatches = [...attributes.matchAll(/([A-Za-z]+)="([^"]*)"/g)];
|
|
71
71
|
const consumed = attrMatches.map((attr) => attr[0]).join(" ").trim();
|
|
72
72
|
const normalized = attributes.replace(/\s+/g, " ").trim().replace(/\/$/, "").trim();
|
|
73
73
|
if (normalized && consumed !== normalized) {
|
|
74
|
-
issues.push("CodeRef
|
|
74
|
+
issues.push("CodeRef allows only the double-quoted path attribute and optional symbol attribute.");
|
|
75
75
|
continue;
|
|
76
76
|
}
|
|
77
77
|
const names = attrMatches.map((attr) => attr[1]);
|
|
78
78
|
const duplicates = names.filter((name, index) => names.indexOf(name) !== index);
|
|
79
79
|
if (duplicates.length > 0) {
|
|
80
|
-
issues.push(`CodeRef
|
|
80
|
+
issues.push(`CodeRef contains duplicate attributes: ${[...new Set(duplicates)].join(", ")}`);
|
|
81
81
|
}
|
|
82
82
|
if (!names.includes("path")) {
|
|
83
|
-
issues.push("CodeRef
|
|
83
|
+
issues.push("CodeRef is missing the required path attribute.");
|
|
84
84
|
}
|
|
85
85
|
for (const name of names) {
|
|
86
86
|
if (name !== "path" && name !== "symbol") {
|
|
87
|
-
issues.push(`CodeRef
|
|
87
|
+
issues.push(`CodeRef contains an unknown attribute: ${name}`);
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
}
|
|
@@ -13,7 +13,7 @@ export function assertOutputSchema(name, payload) {
|
|
|
13
13
|
if (ok) {
|
|
14
14
|
return;
|
|
15
15
|
}
|
|
16
|
-
throw new CliError(
|
|
16
|
+
throw new CliError(`Internal output contract error (${name}): ${formatErrors(validator.errors ?? [])}`, 70);
|
|
17
17
|
}
|
|
18
18
|
export function stringifyValidatedOutput(name, payload) {
|
|
19
19
|
assertOutputSchema(name, payload);
|
|
@@ -25,7 +25,7 @@ export function parseAndValidateJsonString(name, input) {
|
|
|
25
25
|
payload = JSON.parse(input);
|
|
26
26
|
}
|
|
27
27
|
catch (error) {
|
|
28
|
-
throw new CliError(
|
|
28
|
+
throw new CliError(`Internal output contract error (${name}): JSON parse failed: ${error.message}`, 70);
|
|
29
29
|
}
|
|
30
30
|
return stringifyValidatedOutput(name, payload);
|
|
31
31
|
}
|
|
@@ -36,7 +36,7 @@ function getValidator(name) {
|
|
|
36
36
|
}
|
|
37
37
|
const defs = schemaDocument.$defs;
|
|
38
38
|
if (!defs?.[name]) {
|
|
39
|
-
throw new CliError(
|
|
39
|
+
throw new CliError(`Internal output contract error: schema ${name} was not found`, 70);
|
|
40
40
|
}
|
|
41
41
|
const validator = ajv.compile({
|
|
42
42
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
package/dist/lib/package-root.js
CHANGED
|
@@ -9,7 +9,7 @@ export function packageRootFromImport(metaUrl) {
|
|
|
9
9
|
}
|
|
10
10
|
const parent = path.dirname(current);
|
|
11
11
|
if (parent === current) {
|
|
12
|
-
throw new Error(
|
|
12
|
+
throw new Error(`Unable to locate the package root above ${metaUrl}.`);
|
|
13
13
|
}
|
|
14
14
|
current = parent;
|
|
15
15
|
}
|
package/dist/lib/pagination.js
CHANGED
|
@@ -6,7 +6,7 @@ export function paginate({ items, estimate, options }) {
|
|
|
6
6
|
const budget = options.budget ?? DEFAULT_BUDGET;
|
|
7
7
|
const offset = decodeCursor(options.cursor);
|
|
8
8
|
if (offset < 0 || offset > items.length) {
|
|
9
|
-
throw new CliError("cursor
|
|
9
|
+
throw new CliError("The cursor is invalid or expired.");
|
|
10
10
|
}
|
|
11
11
|
const selected = [];
|
|
12
12
|
let usedBudget = 0;
|
|
@@ -60,6 +60,6 @@ function decodeCursor(cursor) {
|
|
|
60
60
|
return payload.offset;
|
|
61
61
|
}
|
|
62
62
|
catch {
|
|
63
|
-
throw new CliError("cursor
|
|
63
|
+
throw new CliError("The cursor is invalid or corrupted.");
|
|
64
64
|
}
|
|
65
65
|
}
|
package/dist/lib/schema.js
CHANGED
|
@@ -11,6 +11,7 @@ function readSchema(fileName) {
|
|
|
11
11
|
}
|
|
12
12
|
const docValidator = ajv.compile(readSchema("doc-frontmatter.schema.json"));
|
|
13
13
|
const metaValidator = ajv.compile(readSchema("meta.schema.json"));
|
|
14
|
+
const configValidator = ajv.compile(readSchema("config.schema.json"));
|
|
14
15
|
export function validateDocFrontmatter(input) {
|
|
15
16
|
const ok = docValidator(input);
|
|
16
17
|
if (ok) {
|
|
@@ -33,6 +34,13 @@ export function validateMeta(input) {
|
|
|
33
34
|
}
|
|
34
35
|
return errors;
|
|
35
36
|
}
|
|
37
|
+
export function validateLlmdocConfig(input) {
|
|
38
|
+
const ok = configValidator(input);
|
|
39
|
+
if (ok) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
return (configValidator.errors ?? []).map(formatAjvError);
|
|
43
|
+
}
|
|
36
44
|
function formatAjvError(error) {
|
|
37
45
|
return `${error.instancePath || "/"} ${error.message ?? "invalid"}`;
|
|
38
46
|
}
|
package/dist/lib/state.js
CHANGED
|
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
|
|
2
2
|
import { canAdvanceRevisions, gitCommitExists, readChangedPathsSince, readGitState } from "./git.js";
|
|
3
3
|
import { normalizeRepoRelativePath } from "./fs.js";
|
|
4
4
|
import { matchesCodePathPattern } from "./search.js";
|
|
5
|
+
import { LLMDOC_CONFIG_FILENAME } from "./config.js";
|
|
5
6
|
export function analyzeDelta(workspace, scope) {
|
|
6
7
|
const git = readWorkspaceGitState(workspace);
|
|
7
8
|
const ignorePatterns = loadIgnorePatterns(workspace.rootDir);
|
|
@@ -30,8 +31,8 @@ export function analyzeDelta(workspace, scope) {
|
|
|
30
31
|
if (revisionInvalid) {
|
|
31
32
|
invalidRevisionDocuments.push(document);
|
|
32
33
|
invalidRevisionReasons.push(!validatedRevision
|
|
33
|
-
?
|
|
34
|
-
:
|
|
34
|
+
? `Document ${document.llmdocPath} has no validatedRevision`
|
|
35
|
+
: `Document ${document.llmdocPath} has a validatedRevision that does not exist in current Git history: ${validatedRevision}`);
|
|
35
36
|
}
|
|
36
37
|
const changedCommittedPaths = !revisionInvalid && git.headRevision && validatedRevision !== git.headRevision
|
|
37
38
|
? getChangedPathsForRevision(workspace.rootDir, validatedRevision, git.headRevision, changedPathsByRevision).filter((filePath) => isSurface(filePath) && documentMatchesPath(document, filePath))
|
|
@@ -71,26 +72,24 @@ export function analyzeDelta(workspace, scope) {
|
|
|
71
72
|
const unmappedDirtyPaths = dirtyPaths.filter((filePath) => !workspace.documents.some((document) => documentMatchesPath(document, filePath)));
|
|
72
73
|
const reasons = [];
|
|
73
74
|
if (!git.available || git.degradedReason) {
|
|
74
|
-
reasons.push(git.degradedReason ?? "
|
|
75
|
+
reasons.push(git.degradedReason ?? "Git state is unavailable");
|
|
75
76
|
}
|
|
76
77
|
if (invalidRevisionReasons.length > 0) {
|
|
77
78
|
reasons.push(...invalidRevisionReasons);
|
|
78
79
|
}
|
|
79
80
|
if (unmappedCommittedPaths.length > 0 || unmappedDirtyPaths.length > 0) {
|
|
80
|
-
reasons.push("
|
|
81
|
+
reasons.push("Unmapped code paths exist");
|
|
81
82
|
}
|
|
82
83
|
if (needsReview.length > 0) {
|
|
83
|
-
reasons.push("
|
|
84
|
+
reasons.push("Documents in the reverse requires closure need review");
|
|
84
85
|
}
|
|
85
86
|
if (directImpacts.length > 8) {
|
|
86
|
-
reasons.push("
|
|
87
|
+
reasons.push("Too many documents are impacted; deep mode is recommended");
|
|
87
88
|
}
|
|
88
89
|
if (dirtyDocuments.length > 0) {
|
|
89
|
-
reasons.push("
|
|
90
|
+
reasons.push("Mapped implementation code has uncommitted changes");
|
|
90
91
|
}
|
|
91
|
-
const suggestedMode = reasons.
|
|
92
|
-
? "deep"
|
|
93
|
-
: "light";
|
|
92
|
+
const suggestedMode = reasons.length > 0 ? "deep" : "light";
|
|
94
93
|
return {
|
|
95
94
|
git,
|
|
96
95
|
impacts: directImpacts.sort((left, right) => left.document.llmdocPath.localeCompare(right.document.llmdocPath)),
|
|
@@ -124,7 +123,7 @@ export function parseScope(values, workspace) {
|
|
|
124
123
|
continue;
|
|
125
124
|
}
|
|
126
125
|
if (!workspace.documentsByLlmdocPath.has(normalized)) {
|
|
127
|
-
throw new Error(`
|
|
126
|
+
throw new Error(`Scope did not match any topic or document: ${raw}`);
|
|
128
127
|
}
|
|
129
128
|
documentPaths.add(normalized);
|
|
130
129
|
}
|
|
@@ -153,7 +152,7 @@ export function computeGrowthState(workspace) {
|
|
|
153
152
|
export function assertRevisionAdvancePreconditions(input) {
|
|
154
153
|
const { workspace, llmdocPaths, updateAll } = input;
|
|
155
154
|
if (!workspace.meta) {
|
|
156
|
-
throw new Error("
|
|
155
|
+
throw new Error("meta.json is missing");
|
|
157
156
|
}
|
|
158
157
|
const git = readWorkspaceGitState(workspace);
|
|
159
158
|
const advance = canAdvanceRevisions(git);
|
|
@@ -171,8 +170,8 @@ export function assertRevisionAdvancePreconditions(input) {
|
|
|
171
170
|
});
|
|
172
171
|
if (blocked.length > 0) {
|
|
173
172
|
throw new Error(updateAll
|
|
174
|
-
?
|
|
175
|
-
:
|
|
173
|
+
? `A full fingerprint cannot advance while mapped code for these documents is dirty: ${blocked.join(", ")}`
|
|
174
|
+
: `Cannot advance revisions while mapped code for these documents is dirty: ${blocked.join(", ")}`);
|
|
176
175
|
}
|
|
177
176
|
return { git, targetPaths };
|
|
178
177
|
}
|
|
@@ -257,7 +256,9 @@ export function loadIgnorePatterns(rootDir) {
|
|
|
257
256
|
return patterns;
|
|
258
257
|
}
|
|
259
258
|
export function isImplementationSurfacePath(repoRelativePath, ignorePatterns = []) {
|
|
260
|
-
if (repoRelativePath
|
|
259
|
+
if (repoRelativePath === LLMDOC_CONFIG_FILENAME ||
|
|
260
|
+
repoRelativePath.startsWith("llmdoc/") ||
|
|
261
|
+
repoRelativePath.startsWith(".llmdoc-tmp/")) {
|
|
261
262
|
return false;
|
|
262
263
|
}
|
|
263
264
|
const basename = repoRelativePath.split("/").pop() ?? repoRelativePath;
|
package/dist/lib/viewer-http.js
CHANGED
|
@@ -48,7 +48,7 @@ export function createViewerRequestHandler(rootDir) {
|
|
|
48
48
|
// 仅按扫描产生的 canonical llmdocPath 查表,不接受任意文件系统路径。
|
|
49
49
|
const document = workspace.documentsByLlmdocPath.get(docPath);
|
|
50
50
|
if (!document) {
|
|
51
|
-
sendJson(response, 404, { error:
|
|
51
|
+
sendJson(response, 404, { error: `Document not found: ${docPath}` }, headOnly);
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
54
|
sendJson(response, 200, {
|
|
@@ -73,7 +73,7 @@ function sendStaticAsset(response, assetsRoot, asset, headOnly) {
|
|
|
73
73
|
// fileName 只可能来自上方常量白名单;请求值不会参与路径解析。
|
|
74
74
|
const assetPath = path.join(assetsRoot, asset.fileName);
|
|
75
75
|
if (!fs.existsSync(assetPath)) {
|
|
76
|
-
throw new CliError(`
|
|
76
|
+
throw new CliError(`Viewer asset is missing: ${asset.fileName}`);
|
|
77
77
|
}
|
|
78
78
|
sendBody(response, 200, fs.readFileSync(assetPath), asset.contentType, headOnly);
|
|
79
79
|
}
|
package/dist/lib/workspace.js
CHANGED
|
@@ -9,10 +9,11 @@ import { normalizeRepoRelativePath, repoPath, resolveInsideRoot } from "./fs.js"
|
|
|
9
9
|
import { gitCommitExists, isShallowRepository } from "./git.js";
|
|
10
10
|
import { matchesCodePathPattern } from "./search.js";
|
|
11
11
|
import { validateDocFrontmatter, validateMeta } from "./schema.js";
|
|
12
|
+
import { loadLlmdocConfig } from "./config.js";
|
|
12
13
|
export function loadWorkspace(rootDir) {
|
|
13
14
|
const configuredLlmdocDir = path.join(rootDir, "llmdoc");
|
|
14
15
|
if (!fs.existsSync(configuredLlmdocDir) || !fs.statSync(configuredLlmdocDir).isDirectory()) {
|
|
15
|
-
throw new CliError("
|
|
16
|
+
throw new CliError("The repository does not contain an llmdoc/ directory.", 2);
|
|
16
17
|
}
|
|
17
18
|
resolveInsideRoot(rootDir, "llmdoc");
|
|
18
19
|
const llmdocDir = configuredLlmdocDir;
|
|
@@ -37,6 +38,7 @@ export function loadWorkspace(rootDir) {
|
|
|
37
38
|
rootSingletons.sort((left, right) => left.llmdocPath.localeCompare(right.llmdocPath));
|
|
38
39
|
const metaPath = path.join(llmdocDir, "meta.json");
|
|
39
40
|
const meta = loadMeta(metaPath, preloadIssues);
|
|
41
|
+
const llmdocConfig = loadLlmdocConfig(rootDir, documentsByLlmdocPath);
|
|
40
42
|
return {
|
|
41
43
|
rootDir,
|
|
42
44
|
llmdocDir,
|
|
@@ -46,6 +48,7 @@ export function loadWorkspace(rootDir) {
|
|
|
46
48
|
topics,
|
|
47
49
|
rootSingletons,
|
|
48
50
|
meta,
|
|
51
|
+
llmdocConfig,
|
|
49
52
|
preloadIssues
|
|
50
53
|
};
|
|
51
54
|
}
|
|
@@ -59,7 +62,7 @@ function scanDocuments(rootDir, llmdocDir, preloadIssues) {
|
|
|
59
62
|
severity: "error",
|
|
60
63
|
code: "file.extension.invalid",
|
|
61
64
|
path: repoRelativePath,
|
|
62
|
-
message: "llmdoc/
|
|
65
|
+
message: "llmdoc/ allows only .mdx documents and one root-level meta.json file."
|
|
63
66
|
});
|
|
64
67
|
}
|
|
65
68
|
return;
|
|
@@ -74,7 +77,7 @@ function scanDocuments(rootDir, llmdocDir, preloadIssues) {
|
|
|
74
77
|
severity: "error",
|
|
75
78
|
code: "frontmatter.parse",
|
|
76
79
|
path: repoRelativePath,
|
|
77
|
-
message: `
|
|
80
|
+
message: `Front matter parse failed: ${error.message}`
|
|
78
81
|
});
|
|
79
82
|
return;
|
|
80
83
|
}
|
|
@@ -125,7 +128,7 @@ function loadMeta(metaPath, preloadIssues) {
|
|
|
125
128
|
severity: "error",
|
|
126
129
|
code: "meta.parse",
|
|
127
130
|
path: "llmdoc/meta.json",
|
|
128
|
-
message: `meta.json
|
|
131
|
+
message: `meta.json parse failed: ${error.message}`
|
|
129
132
|
});
|
|
130
133
|
return null;
|
|
131
134
|
}
|
|
@@ -136,7 +139,7 @@ function loadMeta(metaPath, preloadIssues) {
|
|
|
136
139
|
severity: "error",
|
|
137
140
|
code: "meta.invalid",
|
|
138
141
|
path: "llmdoc/meta.json",
|
|
139
|
-
message: `meta.json
|
|
142
|
+
message: `Invalid meta.json: ${error}`
|
|
140
143
|
});
|
|
141
144
|
}
|
|
142
145
|
return null;
|
|
@@ -145,13 +148,14 @@ function loadMeta(metaPath, preloadIssues) {
|
|
|
145
148
|
}
|
|
146
149
|
export function validateWorkspace(workspace) {
|
|
147
150
|
const issues = [...workspace.preloadIssues];
|
|
151
|
+
issues.push(...workspace.llmdocConfig.issues);
|
|
148
152
|
const seenDocPaths = new Set();
|
|
149
153
|
if (!workspace.meta) {
|
|
150
154
|
issues.push({
|
|
151
155
|
severity: "error",
|
|
152
156
|
code: "meta.missing",
|
|
153
157
|
path: "llmdoc/meta.json",
|
|
154
|
-
message: "
|
|
158
|
+
message: "llmdoc/meta.json is missing. Run `npx -y @tokenroll/llmdoc init-state` to create the ledger (create the initial Git commit first if the repository has no commits)."
|
|
155
159
|
});
|
|
156
160
|
}
|
|
157
161
|
for (const document of workspace.documents) {
|
|
@@ -162,7 +166,7 @@ export function validateWorkspace(workspace) {
|
|
|
162
166
|
severity: "error",
|
|
163
167
|
code: "frontmatter.invalid",
|
|
164
168
|
path: document.repoPath,
|
|
165
|
-
message: `front matter
|
|
169
|
+
message: `Invalid front matter: ${error}`
|
|
166
170
|
});
|
|
167
171
|
}
|
|
168
172
|
try {
|
|
@@ -183,7 +187,7 @@ export function validateWorkspace(workspace) {
|
|
|
183
187
|
severity: "error",
|
|
184
188
|
code: "hierarchy.nested",
|
|
185
189
|
path: document.repoPath,
|
|
186
|
-
message: "topic
|
|
190
|
+
message: "A topic cannot contain nested subdirectories."
|
|
187
191
|
});
|
|
188
192
|
}
|
|
189
193
|
const mdxBodyErrors = validateMdxBody(document.body);
|
|
@@ -200,7 +204,7 @@ export function validateWorkspace(workspace) {
|
|
|
200
204
|
severity: "warning",
|
|
201
205
|
code: "link.wikilink",
|
|
202
206
|
path: document.repoPath,
|
|
203
|
-
message: "
|
|
207
|
+
message: "The body contains [[wikilink]] syntax, which llmdoc does not parse. Use a standard relative Markdown link."
|
|
204
208
|
});
|
|
205
209
|
}
|
|
206
210
|
for (const relationPath of document.frontmatter.relations?.requires ?? []) {
|
|
@@ -218,7 +222,7 @@ export function validateWorkspace(workspace) {
|
|
|
218
222
|
severity: "error",
|
|
219
223
|
code: "relations.requires.missing",
|
|
220
224
|
path: document.repoPath,
|
|
221
|
-
message: `requires
|
|
225
|
+
message: `requires points to a missing document: ${relationPath}`
|
|
222
226
|
});
|
|
223
227
|
}
|
|
224
228
|
}
|
|
@@ -237,7 +241,7 @@ export function validateWorkspace(workspace) {
|
|
|
237
241
|
severity: "error",
|
|
238
242
|
code: "relations.related.missing",
|
|
239
243
|
path: document.repoPath,
|
|
240
|
-
message: `related
|
|
244
|
+
message: `related points to a missing document: ${relationPath}`
|
|
241
245
|
});
|
|
242
246
|
}
|
|
243
247
|
}
|
|
@@ -249,7 +253,7 @@ export function validateWorkspace(workspace) {
|
|
|
249
253
|
severity: "error",
|
|
250
254
|
code: "link.invalid-path",
|
|
251
255
|
path: document.repoPath,
|
|
252
|
-
message:
|
|
256
|
+
message: `Invalid body link: ${linkTarget} (${normalizedLink.message})`
|
|
253
257
|
});
|
|
254
258
|
}
|
|
255
259
|
else if (!workspace.documentsByLlmdocPath.has(normalizedLink.value)) {
|
|
@@ -257,7 +261,7 @@ export function validateWorkspace(workspace) {
|
|
|
257
261
|
severity: "error",
|
|
258
262
|
code: "link.missing",
|
|
259
263
|
path: document.repoPath,
|
|
260
|
-
message:
|
|
264
|
+
message: `Body link points to a missing document: ${linkTarget}`
|
|
261
265
|
});
|
|
262
266
|
}
|
|
263
267
|
}
|
|
@@ -276,7 +280,7 @@ export function validateWorkspace(workspace) {
|
|
|
276
280
|
severity: "error",
|
|
277
281
|
code: "coderef.path.missing",
|
|
278
282
|
path: document.repoPath,
|
|
279
|
-
message: `CodeRef path
|
|
283
|
+
message: `CodeRef path does not exist: ${codeRef.path}`
|
|
280
284
|
});
|
|
281
285
|
}
|
|
282
286
|
}
|
|
@@ -295,7 +299,7 @@ export function validateWorkspace(workspace) {
|
|
|
295
299
|
severity: "error",
|
|
296
300
|
code: "code.paths.missing",
|
|
297
301
|
path: document.repoPath,
|
|
298
|
-
message: `code.paths
|
|
302
|
+
message: `code.paths points to a missing path: ${codePath}`
|
|
299
303
|
});
|
|
300
304
|
}
|
|
301
305
|
else if (normalized.isGlob &&
|
|
@@ -304,7 +308,7 @@ export function validateWorkspace(workspace) {
|
|
|
304
308
|
severity: "error",
|
|
305
309
|
code: "code.paths.unmatched",
|
|
306
310
|
path: document.repoPath,
|
|
307
|
-
message: `code.paths glob
|
|
311
|
+
message: `code.paths glob matches no existing files: ${codePath}`
|
|
308
312
|
});
|
|
309
313
|
}
|
|
310
314
|
}
|
|
@@ -313,7 +317,7 @@ export function validateWorkspace(workspace) {
|
|
|
313
317
|
severity: "warning",
|
|
314
318
|
code: "size.line-warning",
|
|
315
319
|
path: document.repoPath,
|
|
316
|
-
message:
|
|
320
|
+
message: `The document has ${document.lineCount} lines, exceeding the recommended limit of ${DOC_LINE_WARNING_LIMIT}.`
|
|
317
321
|
});
|
|
318
322
|
}
|
|
319
323
|
}
|
|
@@ -323,7 +327,7 @@ export function validateWorkspace(workspace) {
|
|
|
323
327
|
severity: "warning",
|
|
324
328
|
code: "topic.empty",
|
|
325
329
|
path: `llmdoc/${topic}`,
|
|
326
|
-
message: "topic
|
|
330
|
+
message: "The topic directory is empty."
|
|
327
331
|
});
|
|
328
332
|
}
|
|
329
333
|
}
|
|
@@ -332,13 +336,13 @@ export function validateWorkspace(workspace) {
|
|
|
332
336
|
// 否则任何 fetch-depth:1 的 CI 都会误报陈旧。
|
|
333
337
|
const shallow = isShallowRepository(workspace.rootDir);
|
|
334
338
|
const revisionSeverity = shallow ? "warning" : "error";
|
|
335
|
-
const shallowHint = shallow ? "(shallow clone
|
|
339
|
+
const shallowHint = shallow ? " (shallow clone; use fetch-depth: 0 or git fetch --unshallow for complete validation)" : "";
|
|
336
340
|
if (!gitCommitExists(workspace.rootDir, workspace.meta.baseline.revision)) {
|
|
337
341
|
issues.push({
|
|
338
342
|
severity: revisionSeverity,
|
|
339
343
|
code: "meta.baseline.revision.missing",
|
|
340
344
|
path: "llmdoc/meta.json",
|
|
341
|
-
message: `baseline.revision
|
|
345
|
+
message: `baseline.revision does not exist in current Git history: ${workspace.meta.baseline.revision}${shallowHint}`
|
|
342
346
|
});
|
|
343
347
|
}
|
|
344
348
|
const metaPaths = Object.keys(workspace.meta.documents).sort();
|
|
@@ -349,7 +353,7 @@ export function validateWorkspace(workspace) {
|
|
|
349
353
|
severity: "error",
|
|
350
354
|
code: "meta.entry.missing",
|
|
351
355
|
path: `llmdoc/${docPath}`,
|
|
352
|
-
message: "meta.json
|
|
356
|
+
message: "meta.json is missing the corresponding documents entry. Use `llmdoc adopt <path>` to register it without rewriting the document."
|
|
353
357
|
});
|
|
354
358
|
}
|
|
355
359
|
}
|
|
@@ -359,7 +363,7 @@ export function validateWorkspace(workspace) {
|
|
|
359
363
|
severity: "error",
|
|
360
364
|
code: "meta.entry.orphaned",
|
|
361
365
|
path: `llmdoc/${metaPath}`,
|
|
362
|
-
message: "meta.json
|
|
366
|
+
message: "meta.json contains an orphan documents entry."
|
|
363
367
|
});
|
|
364
368
|
}
|
|
365
369
|
const revision = workspace.meta.documents[metaPath]?.validatedRevision;
|
|
@@ -368,7 +372,7 @@ export function validateWorkspace(workspace) {
|
|
|
368
372
|
severity: revisionSeverity,
|
|
369
373
|
code: "meta.document.revision.missing",
|
|
370
374
|
path: `llmdoc/${metaPath}`,
|
|
371
|
-
message: `validatedRevision
|
|
375
|
+
message: `validatedRevision does not exist in current Git history: ${revision}${shallowHint}`
|
|
372
376
|
});
|
|
373
377
|
}
|
|
374
378
|
}
|
|
@@ -382,13 +386,13 @@ function validateMdxBody(body) {
|
|
|
382
386
|
const cleanBody = stripMarkdownLiterals(body);
|
|
383
387
|
const issues = [];
|
|
384
388
|
if (/^\s*import\s+/m.test(cleanBody) || /^\s*export\s+/m.test(cleanBody)) {
|
|
385
|
-
issues.push("
|
|
389
|
+
issues.push("Only plain Markdown and self-closing CodeRef tags are allowed; import/export is forbidden.");
|
|
386
390
|
}
|
|
387
391
|
if (/<(?!CodeRef\b)[A-Za-z][^>]*>/m.test(cleanBody) || /<\/[A-Za-z][^>]*>/m.test(cleanBody)) {
|
|
388
|
-
issues.push("
|
|
392
|
+
issues.push("Only self-closing <CodeRef ... /> components are allowed; arbitrary JSX tags are forbidden.");
|
|
389
393
|
}
|
|
390
394
|
if (/\{[^}\n]+\}/m.test(cleanBody)) {
|
|
391
|
-
issues.push("
|
|
395
|
+
issues.push("MDX/JS expressions are forbidden in document bodies.");
|
|
392
396
|
}
|
|
393
397
|
issues.push(...validateCodeRefTags(body));
|
|
394
398
|
return [...new Set(issues)];
|
|
@@ -397,10 +401,10 @@ function validateLlmdocRelativePath(input) {
|
|
|
397
401
|
try {
|
|
398
402
|
const normalized = normalizeRepoRelativePath(input);
|
|
399
403
|
if (!normalized.endsWith(".mdx")) {
|
|
400
|
-
return { ok: false, message:
|
|
404
|
+
return { ok: false, message: `Path must point to an .mdx document: ${input}` };
|
|
401
405
|
}
|
|
402
406
|
if (normalized.startsWith("llmdoc/")) {
|
|
403
|
-
return { ok: false, message:
|
|
407
|
+
return { ok: false, message: `Path must be relative to llmdoc/, not the repository root: ${input}` };
|
|
404
408
|
}
|
|
405
409
|
return { ok: true, value: normalized };
|
|
406
410
|
}
|