@tokenroll/llmdoc 3.5.1 → 3.5.3
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 +243 -0
- package/assets/viewer-detail.js +223 -0
- package/assets/viewer-graph.js +471 -0
- package/assets/viewer-model.js +127 -0
- package/assets/viewer.css +343 -0
- package/assets/viewer.html +51 -547
- package/dist/cli.js +4 -3
- package/dist/commands/context.js +14 -1
- package/dist/commands/init-state.js +11 -4
- package/dist/commands/new.js +10 -4
- package/dist/commands/search.js +6 -2
- package/dist/commands/serve.js +0 -0
- package/dist/commands/status.js +3 -19
- package/dist/lib/fs.js +14 -0
- package/dist/lib/git.js +8 -2
- package/dist/lib/repository-health.js +26 -0
- package/dist/lib/search.js +131 -21
- package/dist/lib/viewer-http.js +107 -0
- package/dist/lib/viewer-state.js +131 -0
- package/dist/lib/workspace.js +40 -1
- package/package.json +1 -1
- package/schemas/output.schema.json +24 -1
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { resolveDocLink } from "./markdown.js";
|
|
3
|
+
import { readRepositoryRevisionHealth } from "./repository-health.js";
|
|
4
|
+
import { analyzeDelta, computeGrowthState } from "./state.js";
|
|
5
|
+
import { loadWorkspace, validateWorkspace } from "./workspace.js";
|
|
6
|
+
const EDGE_PRIORITY = {
|
|
7
|
+
requires: 3,
|
|
8
|
+
related: 2,
|
|
9
|
+
link: 1
|
|
10
|
+
};
|
|
11
|
+
export function loadViewerState(rootDir, generatedAt = new Date().toISOString()) {
|
|
12
|
+
const workspace = loadWorkspace(rootDir);
|
|
13
|
+
const delta = analyzeDelta(workspace);
|
|
14
|
+
return projectViewerState({
|
|
15
|
+
workspace,
|
|
16
|
+
delta,
|
|
17
|
+
growth: computeGrowthState(workspace),
|
|
18
|
+
issues: validateWorkspace(workspace),
|
|
19
|
+
revisionHealth: readViewerRevisionHealth(workspace, delta),
|
|
20
|
+
generatedAt
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 把已读取的工作区事实投影成稳定、可序列化的 Viewer DTO。
|
|
25
|
+
* 此函数不读文件、不调用 git,便于独立验证状态优先级和 API 契约。
|
|
26
|
+
*/
|
|
27
|
+
export function projectViewerState(input) {
|
|
28
|
+
const { workspace, delta, growth, issues, revisionHealth, generatedAt } = input;
|
|
29
|
+
const impactedPaths = new Set(delta.impacts.map((impact) => impact.document.llmdocPath));
|
|
30
|
+
const dirtyPaths = new Set(delta.dirtyDocuments.map((document) => document.llmdocPath));
|
|
31
|
+
const needsReviewPaths = new Set(delta.needsReview.map((document) => document.llmdocPath));
|
|
32
|
+
const nodes = workspace.documents
|
|
33
|
+
.map((document) => {
|
|
34
|
+
const documentPath = document.llmdocPath;
|
|
35
|
+
return {
|
|
36
|
+
path: documentPath,
|
|
37
|
+
topic: document.topic,
|
|
38
|
+
title: document.title,
|
|
39
|
+
kind: normalizeDocumentKind(document.frontmatter.kind),
|
|
40
|
+
description: typeof document.frontmatter.description === "string" ? document.frontmatter.description : "",
|
|
41
|
+
estimatedTokens: document.estimatedTokens,
|
|
42
|
+
lineCount: document.lineCount,
|
|
43
|
+
codePaths: stringList(document.frontmatter.code?.paths),
|
|
44
|
+
status: dirtyPaths.has(documentPath)
|
|
45
|
+
? "dirty"
|
|
46
|
+
: impactedPaths.has(documentPath)
|
|
47
|
+
? "impacted"
|
|
48
|
+
: needsReviewPaths.has(documentPath)
|
|
49
|
+
? "needs-review"
|
|
50
|
+
: "fresh"
|
|
51
|
+
};
|
|
52
|
+
})
|
|
53
|
+
.sort((left, right) => compareText(left.path, right.path));
|
|
54
|
+
return {
|
|
55
|
+
repository: path.basename(workspace.rootDir),
|
|
56
|
+
generatedAt,
|
|
57
|
+
baseline: {
|
|
58
|
+
revision: workspace.meta?.baseline.revision ?? null,
|
|
59
|
+
headRevision: delta.git.headRevision,
|
|
60
|
+
behindHead: delta.git.baselineBehindHead,
|
|
61
|
+
relevantBehindHead: revisionHealth.relevantCommitsBehindHead,
|
|
62
|
+
metadataOnlyBehind: revisionHealth.metadataOnlyBehind,
|
|
63
|
+
degradedReason: delta.git.degradedReason
|
|
64
|
+
},
|
|
65
|
+
growth,
|
|
66
|
+
validate: {
|
|
67
|
+
ok: issues.every((issue) => issue.severity !== "error"),
|
|
68
|
+
errors: issues.filter((issue) => issue.severity === "error").length,
|
|
69
|
+
warnings: issues.filter((issue) => issue.severity === "warning").length,
|
|
70
|
+
issues: issues.map((issue) => ({ ...issue }))
|
|
71
|
+
},
|
|
72
|
+
delta: {
|
|
73
|
+
suggestedMode: delta.suggestedMode,
|
|
74
|
+
reasons: [...delta.reasons],
|
|
75
|
+
unmappedCommittedPaths: [...delta.unmappedCommittedPaths],
|
|
76
|
+
unmappedDirtyPaths: [...delta.unmappedDirtyPaths]
|
|
77
|
+
},
|
|
78
|
+
nodes,
|
|
79
|
+
edges: buildViewerEdges(workspace.documents, workspace.documentsByLlmdocPath)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 构建有向关系图。同一有向文档对只保留最强关系,反向关系保留为独立边。
|
|
84
|
+
* Map 替换与最终排序让结果不依赖扫描和 frontmatter 中的声明顺序。
|
|
85
|
+
*/
|
|
86
|
+
export function buildViewerEdges(documents, knownDocuments) {
|
|
87
|
+
const edgesByDirection = new Map();
|
|
88
|
+
const addEdge = (from, to, type) => {
|
|
89
|
+
if (from === to || !knownDocuments.has(to)) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
const key = `${from}\u0000${to}`;
|
|
93
|
+
const current = edgesByDirection.get(key);
|
|
94
|
+
if (!current || EDGE_PRIORITY[type] > EDGE_PRIORITY[current.type]) {
|
|
95
|
+
edgesByDirection.set(key, { from, to, type });
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
for (const document of documents) {
|
|
99
|
+
const from = document.llmdocPath;
|
|
100
|
+
for (const target of stringList(document.frontmatter.relations?.requires)) {
|
|
101
|
+
addEdge(from, target, "requires");
|
|
102
|
+
}
|
|
103
|
+
for (const target of stringList(document.frontmatter.relations?.related)) {
|
|
104
|
+
addEdge(from, target, "related");
|
|
105
|
+
}
|
|
106
|
+
for (const target of document.links) {
|
|
107
|
+
addEdge(from, resolveDocLink(from, target), "link");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return [...edgesByDirection.values()].sort((left, right) => compareText(left.from, right.from) || compareText(left.to, right.to) || EDGE_PRIORITY[right.type] - EDGE_PRIORITY[left.type]);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* 与 status 命令一致,只把实际触碰 implementation surface 的 commit 计入知识落后。
|
|
114
|
+
* 纯 llmdoc/meta.json follow-up 会保留原始 behindHead,但 relevant 为 0。
|
|
115
|
+
*/
|
|
116
|
+
export function readViewerRevisionHealth(workspace, delta) {
|
|
117
|
+
const health = readRepositoryRevisionHealth(workspace.rootDir, workspace.meta?.baseline.revision ?? null, delta.git);
|
|
118
|
+
return {
|
|
119
|
+
relevantCommitsBehindHead: health.relevantCommitsBehindHead,
|
|
120
|
+
metadataOnlyBehind: health.metadataOnlyBehind
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function compareText(left, right) {
|
|
124
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
125
|
+
}
|
|
126
|
+
function normalizeDocumentKind(value) {
|
|
127
|
+
return value === "architecture" || value === "guide" || value === "reference" ? value : "unknown";
|
|
128
|
+
}
|
|
129
|
+
function stringList(value) {
|
|
130
|
+
return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
|
|
131
|
+
}
|
package/dist/lib/workspace.js
CHANGED
|
@@ -7,6 +7,7 @@ import { estimateTokens, extractCodeRefs, extractLinks, extractTitle, resolveDoc
|
|
|
7
7
|
import { assertDocKindMatchesShape, parseDocTargetShape } from "./doc-shape.js";
|
|
8
8
|
import { normalizeRepoRelativePath, repoPath, resolveInsideRoot } from "./fs.js";
|
|
9
9
|
import { gitCommitExists, isShallowRepository } from "./git.js";
|
|
10
|
+
import { matchesCodePathPattern } from "./search.js";
|
|
10
11
|
import { validateDocFrontmatter, validateMeta } from "./schema.js";
|
|
11
12
|
export function loadWorkspace(rootDir) {
|
|
12
13
|
const configuredLlmdocDir = path.join(rootDir, "llmdoc");
|
|
@@ -150,7 +151,7 @@ export function validateWorkspace(workspace) {
|
|
|
150
151
|
severity: "error",
|
|
151
152
|
code: "meta.missing",
|
|
152
153
|
path: "llmdoc/meta.json",
|
|
153
|
-
message: "缺少 llmdoc/meta.json
|
|
154
|
+
message: "缺少 llmdoc/meta.json。请运行 `npx -y @tokenroll/llmdoc init-state` 建立台账(若仓库尚无提交,请先创建首次 Git 提交)。"
|
|
154
155
|
});
|
|
155
156
|
}
|
|
156
157
|
for (const document of workspace.documents) {
|
|
@@ -297,6 +298,15 @@ export function validateWorkspace(workspace) {
|
|
|
297
298
|
message: `code.paths 指向不存在的路径: ${codePath}`
|
|
298
299
|
});
|
|
299
300
|
}
|
|
301
|
+
else if (normalized.isGlob &&
|
|
302
|
+
!globMatchesExistingFile(workspace.rootDir, normalized.absolutePath, normalized.normalized)) {
|
|
303
|
+
issues.push({
|
|
304
|
+
severity: "error",
|
|
305
|
+
code: "code.paths.unmatched",
|
|
306
|
+
path: document.repoPath,
|
|
307
|
+
message: `code.paths glob 未命中任何现有文件: ${codePath}`
|
|
308
|
+
});
|
|
309
|
+
}
|
|
300
310
|
}
|
|
301
311
|
if (document.lineCount > DOC_LINE_WARNING_LIMIT) {
|
|
302
312
|
issues.push({
|
|
@@ -432,3 +442,32 @@ function validateCodePathPattern(rootDir, input) {
|
|
|
432
442
|
return { ok: false, message: error.message };
|
|
433
443
|
}
|
|
434
444
|
}
|
|
445
|
+
function globMatchesExistingFile(rootDir, staticPrefixPath, pattern) {
|
|
446
|
+
const pending = [staticPrefixPath];
|
|
447
|
+
while (pending.length > 0) {
|
|
448
|
+
const currentPath = pending.pop();
|
|
449
|
+
const stat = fs.lstatSync(currentPath);
|
|
450
|
+
if (stat.isSymbolicLink()) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (stat.isFile()) {
|
|
454
|
+
if (matchesCodePathPattern(pattern, repoPath(rootDir, currentPath))) {
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
if (!stat.isDirectory()) {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
for (const entry of fs.readdirSync(currentPath, { withFileTypes: true })) {
|
|
463
|
+
if (entry.isSymbolicLink()) {
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (entry.isDirectory() && [".git", ".llmdoc-tmp", "node_modules"].includes(entry.name)) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
pending.push(path.join(currentPath, entry.name));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return false;
|
|
473
|
+
}
|
package/package.json
CHANGED
|
@@ -374,6 +374,7 @@
|
|
|
374
374
|
"additionalProperties": false,
|
|
375
375
|
"required": [
|
|
376
376
|
"query",
|
|
377
|
+
"searchMode",
|
|
377
378
|
"results",
|
|
378
379
|
"pagination"
|
|
379
380
|
],
|
|
@@ -382,6 +383,13 @@
|
|
|
382
383
|
"type": "string",
|
|
383
384
|
"minLength": 1
|
|
384
385
|
},
|
|
386
|
+
"searchMode": {
|
|
387
|
+
"type": "string",
|
|
388
|
+
"enum": [
|
|
389
|
+
"lexical",
|
|
390
|
+
"cjk-bigram-fallback"
|
|
391
|
+
]
|
|
392
|
+
},
|
|
385
393
|
"results": {
|
|
386
394
|
"type": "array",
|
|
387
395
|
"items": {
|
|
@@ -425,6 +433,7 @@
|
|
|
425
433
|
"required": [
|
|
426
434
|
"impacted",
|
|
427
435
|
"prerequisites",
|
|
436
|
+
"unmappedFiles",
|
|
428
437
|
"pagination"
|
|
429
438
|
],
|
|
430
439
|
"properties": {
|
|
@@ -440,6 +449,13 @@
|
|
|
440
449
|
"$ref": "#/$defs/docRef"
|
|
441
450
|
}
|
|
442
451
|
},
|
|
452
|
+
"unmappedFiles": {
|
|
453
|
+
"type": "array",
|
|
454
|
+
"items": {
|
|
455
|
+
"type": "string",
|
|
456
|
+
"minLength": 1
|
|
457
|
+
}
|
|
458
|
+
},
|
|
443
459
|
"pagination": {
|
|
444
460
|
"$ref": "#/$defs/pagination"
|
|
445
461
|
}
|
|
@@ -851,12 +867,19 @@
|
|
|
851
867
|
"type": "object",
|
|
852
868
|
"additionalProperties": false,
|
|
853
869
|
"required": [
|
|
854
|
-
"created"
|
|
870
|
+
"created",
|
|
871
|
+
"next"
|
|
855
872
|
],
|
|
856
873
|
"properties": {
|
|
857
874
|
"created": {
|
|
858
875
|
"type": "string",
|
|
859
876
|
"minLength": 1
|
|
877
|
+
},
|
|
878
|
+
"next": {
|
|
879
|
+
"type": [
|
|
880
|
+
"string",
|
|
881
|
+
"null"
|
|
882
|
+
]
|
|
860
883
|
}
|
|
861
884
|
}
|
|
862
885
|
},
|