@tokenroll/llmdoc 3.0.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.html +554 -0
- package/dist/bin/llmdoc.js +24 -0
- package/dist/cli.js +261 -0
- package/dist/commands/context.js +78 -0
- package/dist/commands/delta.js +78 -0
- package/dist/commands/fingerprint.js +38 -0
- package/dist/commands/hook.js +84 -0
- package/dist/commands/index.js +53 -0
- package/dist/commands/mv.js +175 -0
- package/dist/commands/new.js +59 -0
- package/dist/commands/prune.js +171 -0
- package/dist/commands/search.js +44 -0
- package/dist/commands/serve.js +0 -0
- package/dist/commands/show.js +47 -0
- package/dist/commands/status.js +46 -0
- package/dist/commands/tree.js +96 -0
- package/dist/commands/upgrade.js +124 -0
- package/dist/commands/validate.js +30 -0
- package/dist/lib/constants.js +8 -0
- package/dist/lib/doc-shape.js +50 -0
- package/dist/lib/errors.js +8 -0
- package/dist/lib/format.js +24 -0
- package/dist/lib/fs.js +81 -0
- package/dist/lib/git.js +142 -0
- package/dist/lib/markdown.js +99 -0
- package/dist/lib/output-schema.js +56 -0
- package/dist/lib/package-root.js +16 -0
- package/dist/lib/pagination.js +65 -0
- package/dist/lib/rewrite.js +110 -0
- package/dist/lib/schema.js +63 -0
- package/dist/lib/search.js +157 -0
- package/dist/lib/state.js +251 -0
- package/dist/lib/workspace.js +421 -0
- package/dist/types.js +1 -0
- package/package.json +50 -0
- package/schemas/doc-frontmatter.schema.json +48 -0
- package/schemas/meta.schema.json +65 -0
- package/schemas/output.schema.json +908 -0
- package/templates/doc.mdx +7 -0
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>llmdoc viewer</title>
|
|
7
|
+
<script src="/assets/marked.js"></script>
|
|
8
|
+
<style>
|
|
9
|
+
:root {
|
|
10
|
+
--bg: #f4f5f7; --panel: #ffffff; --border: #e5e8ec; --border-strong: #d6dade;
|
|
11
|
+
--text: #171c26; --muted: #667085; --faint: #98a2b3;
|
|
12
|
+
--accent: #4c5fd5; --accent-soft: #eef0fc;
|
|
13
|
+
--fresh: #aab2bd; --impacted: #e8890c; --dirty: #d6453d; --review: #c9a227;
|
|
14
|
+
--shadow: 0 1px 2px rgba(16,24,40,.05), 0 4px 14px rgba(16,24,40,.06);
|
|
15
|
+
--radius: 12px;
|
|
16
|
+
}
|
|
17
|
+
* { box-sizing: border-box; }
|
|
18
|
+
html, body { height: 100%; }
|
|
19
|
+
body {
|
|
20
|
+
margin: 0; color: var(--text); background: var(--bg);
|
|
21
|
+
font: 13px/1.6 -apple-system, "SF Pro Text", "Segoe UI", "PingFang SC", "Noto Sans CJK SC", sans-serif;
|
|
22
|
+
display: flex; flex-direction: column; overflow: hidden;
|
|
23
|
+
-webkit-font-smoothing: antialiased;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* ---------- header ---------- */
|
|
27
|
+
header {
|
|
28
|
+
display: flex; align-items: center; gap: 10px; padding: 10px 18px;
|
|
29
|
+
background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap; z-index: 3;
|
|
30
|
+
}
|
|
31
|
+
header h1 { font-size: 14.5px; margin: 0 8px 0 0; font-weight: 700; letter-spacing: -.01em; }
|
|
32
|
+
header h1 .repo { color: var(--muted); font-weight: 450; }
|
|
33
|
+
.chip {
|
|
34
|
+
padding: 3px 10px; border-radius: 999px; border: 1px solid var(--border);
|
|
35
|
+
background: #fafbfc; font-size: 11.5px; color: var(--muted); white-space: nowrap;
|
|
36
|
+
}
|
|
37
|
+
.chip.ok { color: #147a3d; border-color: #bfe4cb; background: #f0faf3; }
|
|
38
|
+
.chip.bad { color: var(--dirty); border-color: #f0c7c4; background: #fdf2f1; }
|
|
39
|
+
.chip.warn { color: #b26a05; border-color: #f0dcb4; background: #fdf8ec; }
|
|
40
|
+
.chip.btn { cursor: pointer; user-select: none; }
|
|
41
|
+
.chip.btn:hover { border-color: var(--border-strong); color: var(--text); }
|
|
42
|
+
.toggle { display: flex; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; margin-left: auto; }
|
|
43
|
+
.toggle button {
|
|
44
|
+
border: 0; background: var(--panel); color: var(--muted); font: inherit; font-size: 12px;
|
|
45
|
+
padding: 5px 14px; cursor: pointer;
|
|
46
|
+
}
|
|
47
|
+
.toggle button.on { background: var(--accent); color: #fff; font-weight: 600; }
|
|
48
|
+
.toggle button:not(.on):hover { background: var(--accent-soft); color: var(--accent); }
|
|
49
|
+
|
|
50
|
+
/* ---------- layout ---------- */
|
|
51
|
+
main { flex: 1; display: grid; grid-template-columns: 272px 1fr 408px; min-height: 0; }
|
|
52
|
+
|
|
53
|
+
/* ---------- sidebar ---------- */
|
|
54
|
+
#sidebar { background: var(--panel); border-right: 1px solid var(--border); overflow-y: auto; padding: 12px; }
|
|
55
|
+
#sidebar input {
|
|
56
|
+
width: 100%; padding: 7px 11px; border: 1px solid var(--border); border-radius: 9px;
|
|
57
|
+
font: inherit; font-size: 12.5px; margin-bottom: 8px; outline: none; background: #fafbfc;
|
|
58
|
+
}
|
|
59
|
+
#sidebar input:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px rgba(76,95,213,.12); }
|
|
60
|
+
.group-label {
|
|
61
|
+
font-size: 10.5px; text-transform: uppercase; letter-spacing: .07em; color: var(--faint);
|
|
62
|
+
margin: 14px 6px 5px; display: flex; align-items: center; gap: 7px; font-weight: 600; cursor: pointer;
|
|
63
|
+
}
|
|
64
|
+
.group-label:hover { color: var(--muted); }
|
|
65
|
+
.swatch { width: 10px; height: 10px; border-radius: 3.5px; display: inline-block; }
|
|
66
|
+
.doc-item {
|
|
67
|
+
display: flex; align-items: center; gap: 8px; padding: 5.5px 9px; border-radius: 8px;
|
|
68
|
+
cursor: pointer; transition: background .1s;
|
|
69
|
+
}
|
|
70
|
+
.doc-item:hover { background: #f2f4f7; }
|
|
71
|
+
.doc-item.active { background: var(--accent-soft); }
|
|
72
|
+
.doc-item.active .name { color: var(--accent); font-weight: 600; }
|
|
73
|
+
.doc-item .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; box-shadow: 0 0 0 2.5px color-mix(in srgb, currentColor 18%, transparent); }
|
|
74
|
+
.doc-item .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 12.5px; }
|
|
75
|
+
.doc-item .kind { margin-left: auto; font-size: 10px; color: var(--faint); flex: none; }
|
|
76
|
+
|
|
77
|
+
/* ---------- graph ---------- */
|
|
78
|
+
#graph-wrap { position: relative; min-width: 0; background:
|
|
79
|
+
radial-gradient(circle at 1px 1px, #dfe3e8 1px, transparent 0) 0 0/26px 26px; }
|
|
80
|
+
#graph { width: 100%; height: 100%; display: block; cursor: grab; }
|
|
81
|
+
#graph:active { cursor: grabbing; }
|
|
82
|
+
#legend {
|
|
83
|
+
position: absolute; left: 14px; bottom: 14px; background: color-mix(in srgb, var(--panel) 92%, transparent);
|
|
84
|
+
backdrop-filter: blur(6px); border: 1px solid var(--border); border-radius: 10px;
|
|
85
|
+
padding: 8px 14px; font-size: 11px; color: var(--muted); display: flex; gap: 15px; box-shadow: var(--shadow);
|
|
86
|
+
}
|
|
87
|
+
#legend i { display: inline-block; width: 18px; border-top: 2px solid #9aa4b2; vertical-align: middle; margin-right: 5px; border-radius: 2px; }
|
|
88
|
+
#legend i.related { border-top-style: dashed; }
|
|
89
|
+
#legend i.link { border-top-width: 1px; border-top-color: #c8cfd8; }
|
|
90
|
+
#graph-hint {
|
|
91
|
+
position: absolute; top: 14px; left: 14px; font-size: 11.5px; color: var(--muted);
|
|
92
|
+
background: color-mix(in srgb, var(--panel) 92%, transparent); backdrop-filter: blur(6px);
|
|
93
|
+
border: 1px solid var(--border); border-radius: 10px; padding: 6px 12px; box-shadow: var(--shadow);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* svg */
|
|
97
|
+
.edge-path { fill: none; transition: opacity .15s; }
|
|
98
|
+
.node { transition: opacity .15s; }
|
|
99
|
+
.node circle, .node .topic-shape { cursor: pointer; transition: filter .12s; }
|
|
100
|
+
.node:hover circle, .node:hover .topic-shape { filter: brightness(1.06) saturate(1.1); }
|
|
101
|
+
.node text { fill: var(--text); pointer-events: none; }
|
|
102
|
+
.node .sub { fill: var(--muted); }
|
|
103
|
+
.node.dim, .edge-path.dim { opacity: .12; }
|
|
104
|
+
|
|
105
|
+
/* ---------- detail ---------- */
|
|
106
|
+
#detail { background: var(--panel); border-left: 1px solid var(--border); overflow-y: auto; }
|
|
107
|
+
#detail .inner { padding: 18px 20px 32px; }
|
|
108
|
+
#detail .placeholder { color: var(--faint); margin-top: 48px; text-align: center; font-size: 12.5px; }
|
|
109
|
+
#detail h2 { font-size: 15px; margin: 0 0 3px; word-break: break-all; letter-spacing: -.01em; }
|
|
110
|
+
#detail .desc { color: var(--muted); font-size: 12.5px; }
|
|
111
|
+
#detail .meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 6px; }
|
|
112
|
+
.rel-block { font-size: 12.5px; margin: 3px 0; }
|
|
113
|
+
.rel-link { color: var(--accent); cursor: pointer; text-decoration: none; }
|
|
114
|
+
.rel-link:hover { text-decoration: underline; }
|
|
115
|
+
.code-path {
|
|
116
|
+
font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11px;
|
|
117
|
+
background: #f6f7f9; border: 1px solid var(--border); border-radius: 6px; padding: 2px 7px;
|
|
118
|
+
}
|
|
119
|
+
.coderef {
|
|
120
|
+
font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: #6d3fc0;
|
|
121
|
+
background: #f6f1fd; border: 1px solid #e4d6f8; border-radius: 6px; padding: 1px 7px;
|
|
122
|
+
}
|
|
123
|
+
#doc-body { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 14px; font-size: 13.5px; }
|
|
124
|
+
#doc-body h1 { font-size: 17px; } #doc-body h2 { font-size: 14.5px; margin-top: 22px; } #doc-body h3 { font-size: 13.5px; }
|
|
125
|
+
#doc-body pre { background: #f6f7f9; border: 1px solid var(--border); padding: 11px 13px; border-radius: 9px; overflow-x: auto; font-size: 12px; }
|
|
126
|
+
#doc-body code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; background: #f3f4f6; padding: 1px 5px; border-radius: 5px; }
|
|
127
|
+
#doc-body pre code { background: none; padding: 0; }
|
|
128
|
+
#doc-body table { border-collapse: collapse; font-size: 12.5px; }
|
|
129
|
+
#doc-body td, #doc-body th { border: 1px solid var(--border); padding: 5px 9px; }
|
|
130
|
+
#doc-body a { color: var(--accent); }
|
|
131
|
+
#doc-body blockquote { margin: 0; padding: 2px 14px; border-left: 3px solid var(--border-strong); color: var(--muted); }
|
|
132
|
+
|
|
133
|
+
/* topic detail card list */
|
|
134
|
+
.topic-doc-card {
|
|
135
|
+
border: 1px solid var(--border); border-radius: 10px; padding: 10px 13px; margin: 8px 0;
|
|
136
|
+
cursor: pointer; transition: border-color .12s, box-shadow .12s;
|
|
137
|
+
}
|
|
138
|
+
.topic-doc-card:hover { border-color: var(--accent); box-shadow: var(--shadow); }
|
|
139
|
+
.topic-doc-card .t { font-weight: 600; font-size: 12.5px; display: flex; gap: 8px; align-items: center; }
|
|
140
|
+
.topic-doc-card .d { color: var(--muted); font-size: 12px; margin-top: 3px; }
|
|
141
|
+
</style>
|
|
142
|
+
</head>
|
|
143
|
+
<body>
|
|
144
|
+
<header>
|
|
145
|
+
<h1>llmdoc <span class="repo" id="repo"></span></h1>
|
|
146
|
+
<span class="chip" id="stat-docs"></span>
|
|
147
|
+
<span class="chip" id="stat-baseline"></span>
|
|
148
|
+
<span class="chip" id="stat-validate"></span>
|
|
149
|
+
<span class="chip" id="stat-delta"></span>
|
|
150
|
+
<span class="chip btn" id="refresh">↻ 刷新</span>
|
|
151
|
+
<div class="toggle">
|
|
152
|
+
<button id="mode-topics">Topics 拓扑</button>
|
|
153
|
+
<button id="mode-docs">Documents 全图</button>
|
|
154
|
+
</div>
|
|
155
|
+
</header>
|
|
156
|
+
<main>
|
|
157
|
+
<nav id="sidebar">
|
|
158
|
+
<input id="search" type="search" placeholder="过滤文档 (路径 / 描述)" />
|
|
159
|
+
<div id="doc-list"></div>
|
|
160
|
+
</nav>
|
|
161
|
+
<section id="graph-wrap">
|
|
162
|
+
<svg id="graph"></svg>
|
|
163
|
+
<div id="graph-hint"></div>
|
|
164
|
+
<div id="legend"></div>
|
|
165
|
+
</section>
|
|
166
|
+
<aside id="detail"><div class="inner"><div class="placeholder">选择一个 topic 或文档查看详情</div></div></aside>
|
|
167
|
+
</main>
|
|
168
|
+
<script>
|
|
169
|
+
"use strict";
|
|
170
|
+
const PALETTE = ["#4c5fd5", "#0e8f6f", "#c26a12", "#7a4bc9", "#0d84ab", "#b13d72", "#5f8016", "#96591f"];
|
|
171
|
+
const STATUS_COLOR = { fresh: "#aab2bd", impacted: "#e8890c", dirty: "#d6453d", "needs-review": "#c9a227" };
|
|
172
|
+
const STATUS_RANK = { fresh: 0, "needs-review": 1, impacted: 2, dirty: 3 };
|
|
173
|
+
let state = null, selected = null, mode = "topics", topicColor = new Map(), sim = null;
|
|
174
|
+
|
|
175
|
+
async function loadState() {
|
|
176
|
+
state = await (await fetch("/api/state")).json();
|
|
177
|
+
topicColor = new Map();
|
|
178
|
+
[...new Set(state.nodes.map((n) => n.topic).filter(Boolean))].sort()
|
|
179
|
+
.forEach((topic, i) => topicColor.set(topic, PALETTE[i % PALETTE.length]));
|
|
180
|
+
renderHeader(); renderSidebar(); renderGraph();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function colorOf(topic) { return topic ? topicColor.get(topic) : "#3c4657"; }
|
|
184
|
+
|
|
185
|
+
function renderHeader() {
|
|
186
|
+
document.getElementById("repo").textContent = "/ " + state.repository;
|
|
187
|
+
document.getElementById("stat-docs").textContent =
|
|
188
|
+
state.nodes.length + " docs · ~" + state.growth.currentTotalEstimatedTokens + " tokens";
|
|
189
|
+
const behind = state.baseline.behindHead;
|
|
190
|
+
const baselineEl = document.getElementById("stat-baseline");
|
|
191
|
+
baselineEl.textContent = state.baseline.revision
|
|
192
|
+
? "baseline " + state.baseline.revision.slice(0, 7) + (behind ? " · " + behind + " behind" : " · 最新")
|
|
193
|
+
: "baseline 缺失";
|
|
194
|
+
baselineEl.className = "chip " + (behind ? "warn" : state.baseline.revision ? "ok" : "bad");
|
|
195
|
+
const v = state.validate;
|
|
196
|
+
const validateEl = document.getElementById("stat-validate");
|
|
197
|
+
validateEl.textContent = v.ok ? "validate ok" + (v.warnings ? " · " + v.warnings + " warn" : "") : "validate " + v.errors + " errors";
|
|
198
|
+
validateEl.className = "chip " + (v.ok ? "ok" : "bad");
|
|
199
|
+
const stale = state.nodes.filter((n) => n.status !== "fresh").length;
|
|
200
|
+
const deltaEl = document.getElementById("stat-delta");
|
|
201
|
+
deltaEl.textContent = stale ? stale + " 份待同步 · 建议 " + state.delta.suggestedMode : "知识面新鲜";
|
|
202
|
+
deltaEl.className = "chip " + (stale ? "warn" : "ok");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/* ---------- sidebar ---------- */
|
|
206
|
+
function renderSidebar() {
|
|
207
|
+
const query = document.getElementById("search").value.trim().toLowerCase();
|
|
208
|
+
const match = (n) => !query || n.path.toLowerCase().includes(query) || n.description.toLowerCase().includes(query);
|
|
209
|
+
const container = document.getElementById("doc-list");
|
|
210
|
+
container.innerHTML = "";
|
|
211
|
+
const groups = new Map([["", state.nodes.filter((n) => !n.topic && match(n))]]);
|
|
212
|
+
for (const node of state.nodes) if (node.topic && match(node)) {
|
|
213
|
+
if (!groups.has(node.topic)) groups.set(node.topic, []);
|
|
214
|
+
groups.get(node.topic).push(node);
|
|
215
|
+
}
|
|
216
|
+
for (const [topic, nodes] of groups) {
|
|
217
|
+
if (!nodes.length) continue;
|
|
218
|
+
const label = document.createElement("div");
|
|
219
|
+
label.className = "group-label";
|
|
220
|
+
label.innerHTML = topic
|
|
221
|
+
? '<span class="swatch" style="background:' + colorOf(topic) + '"></span>' + topic + "/"
|
|
222
|
+
: "root";
|
|
223
|
+
if (topic) label.onclick = () => selectTopic(topic);
|
|
224
|
+
container.append(label);
|
|
225
|
+
for (const node of nodes) {
|
|
226
|
+
const item = document.createElement("div");
|
|
227
|
+
item.className = "doc-item" + (selected === node.path ? " active" : "");
|
|
228
|
+
item.innerHTML =
|
|
229
|
+
'<span class="dot" style="background:' + STATUS_COLOR[node.status] + ';color:' + STATUS_COLOR[node.status] + '"></span>' +
|
|
230
|
+
'<span class="name" title="' + node.path + '">' + node.path.split("/").pop().replace(/\.mdx$/, "") + "</span>" +
|
|
231
|
+
'<span class="kind">' + node.kind + "</span>";
|
|
232
|
+
item.onclick = () => selectDoc(node.path);
|
|
233
|
+
container.append(item);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* ---------- shared force simulation ---------- */
|
|
239
|
+
function simulate(nodes, edges, width, height, spread) {
|
|
240
|
+
const byId = new Map(nodes.map((n) => [n.id, n]));
|
|
241
|
+
nodes.forEach((n, i) => {
|
|
242
|
+
const angle = (i / nodes.length) * Math.PI * 2;
|
|
243
|
+
n.x = width / 2 + Math.cos(angle) * spread * 0.55 + Math.cos(i * 5.7) * 30;
|
|
244
|
+
n.y = height / 2 + Math.sin(angle) * spread * 0.55 + Math.sin(i * 5.7) * 30;
|
|
245
|
+
n.vx = 0; n.vy = 0;
|
|
246
|
+
});
|
|
247
|
+
const repulse = spread * spread * 3.2;
|
|
248
|
+
for (let step = 0; step < 320; step++) {
|
|
249
|
+
for (let i = 0; i < nodes.length; i++) for (let j = i + 1; j < nodes.length; j++) {
|
|
250
|
+
const a = nodes[i], b = nodes[j];
|
|
251
|
+
let dx = a.x - b.x, dy = a.y - b.y, d2 = dx * dx + dy * dy || 1, d = Math.sqrt(d2);
|
|
252
|
+
const force = Math.min(14, repulse / d2);
|
|
253
|
+
dx /= d; dy /= d; a.vx += dx * force; a.vy += dy * force; b.vx -= dx * force; b.vy -= dy * force;
|
|
254
|
+
}
|
|
255
|
+
for (const e of edges) {
|
|
256
|
+
const a = byId.get(e.from), b = byId.get(e.to);
|
|
257
|
+
if (!a || !b) continue;
|
|
258
|
+
const dx = b.x - a.x, dy = b.y - a.y, d = Math.sqrt(dx * dx + dy * dy) || 1;
|
|
259
|
+
const pull = (d - (a.r + b.r + spread * 0.55)) * 0.014;
|
|
260
|
+
a.vx += (dx / d) * pull; a.vy += (dy / d) * pull; b.vx -= (dx / d) * pull; b.vy -= (dy / d) * pull;
|
|
261
|
+
}
|
|
262
|
+
for (const n of nodes) {
|
|
263
|
+
if (n.cluster) { n.vx += (n.cluster.x - n.x) * 0.014; n.vy += (n.cluster.y - n.y) * 0.014; }
|
|
264
|
+
n.vx += (width / 2 - n.x) * 0.004; n.vy += (height / 2 - n.y) * 0.004;
|
|
265
|
+
n.x += n.vx *= 0.8; n.y += n.vy *= 0.8;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* ---------- graph rendering ---------- */
|
|
271
|
+
function renderGraph() {
|
|
272
|
+
const svg = document.getElementById("graph");
|
|
273
|
+
const { width, height } = svg.getBoundingClientRect();
|
|
274
|
+
const NS = "http://www.w3.org/2000/svg";
|
|
275
|
+
svg.innerHTML = "";
|
|
276
|
+
const defs = document.createElementNS(NS, "defs");
|
|
277
|
+
defs.innerHTML =
|
|
278
|
+
'<marker id="arrow" viewBox="0 0 8 8" refX="7.5" refY="4" markerWidth="5.5" markerHeight="5.5" orient="auto">' +
|
|
279
|
+
'<path d="M0 .8 L8 4 L0 7.2 z" fill="#9aa4b2"/></marker>' +
|
|
280
|
+
'<filter id="soft" x="-40%" y="-40%" width="180%" height="180%">' +
|
|
281
|
+
'<feDropShadow dx="0" dy="1.5" stdDeviation="2.5" flood-color="#101828" flood-opacity="0.18"/></filter>';
|
|
282
|
+
const root = document.createElementNS(NS, "g");
|
|
283
|
+
svg.append(defs, root);
|
|
284
|
+
|
|
285
|
+
const graph = mode === "topics" ? buildTopicGraph(width, height) : buildDocGraph(width, height);
|
|
286
|
+
document.getElementById("graph-hint").textContent = mode === "topics"
|
|
287
|
+
? "Topic 拓扑 · 边粗细 = 跨域引用数 · 点击下钻"
|
|
288
|
+
: "文档全图 · 点击节点查看详情 · 拖拽 / 滚轮缩放";
|
|
289
|
+
document.getElementById("legend").innerHTML = (mode === "topics"
|
|
290
|
+
? '<span><i></i>跨域引用</span>'
|
|
291
|
+
: '<span><i></i>requires</span><span><i class="related"></i>related</span><span><i class="link"></i>link</span>') +
|
|
292
|
+
'<span><b style="color:var(--dirty)">●</b> dirty</span><span><b style="color:var(--impacted)">●</b> impacted</span><span><b style="color:var(--review)">●</b> review</span>';
|
|
293
|
+
|
|
294
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
295
|
+
const edgeEls = graph.edges.map((e) => {
|
|
296
|
+
const pathEl = document.createElementNS(NS, "path");
|
|
297
|
+
pathEl.setAttribute("class", "edge-path");
|
|
298
|
+
pathEl.setAttribute("stroke", e.style === "link" ? "#c8cfd8" : "#9aa4b2");
|
|
299
|
+
pathEl.setAttribute("stroke-width", e.width ?? (e.style === "link" ? 1 : 1.7));
|
|
300
|
+
pathEl.setAttribute("stroke-linecap", "round");
|
|
301
|
+
pathEl.setAttribute("stroke-opacity", e.style === "link" ? 0.75 : 0.85);
|
|
302
|
+
if (e.style === "related") pathEl.setAttribute("stroke-dasharray", "5 5");
|
|
303
|
+
if (e.style === "requires") pathEl.setAttribute("marker-end", "url(#arrow)");
|
|
304
|
+
root.append(pathEl);
|
|
305
|
+
return { e, pathEl };
|
|
306
|
+
});
|
|
307
|
+
const nodeEls = graph.nodes.map((n) => {
|
|
308
|
+
const g = document.createElementNS(NS, "g");
|
|
309
|
+
g.setAttribute("class", "node");
|
|
310
|
+
const circle = document.createElementNS(NS, "circle");
|
|
311
|
+
circle.setAttribute("r", n.r);
|
|
312
|
+
circle.setAttribute("fill", n.fill);
|
|
313
|
+
circle.setAttribute("fill-opacity", n.isTopic ? "0.92" : "0.88");
|
|
314
|
+
circle.setAttribute("stroke", n.status === "fresh" ? "#ffffff" : STATUS_COLOR[n.status]);
|
|
315
|
+
circle.setAttribute("stroke-width", n.status === "fresh" ? 2 : 3.5);
|
|
316
|
+
circle.setAttribute("filter", "url(#soft)");
|
|
317
|
+
if (n.isTopic) circle.setAttribute("class", "topic-shape");
|
|
318
|
+
g.append(circle);
|
|
319
|
+
if (n.isTopic) {
|
|
320
|
+
const title = textEl(NS, n.label, 0, 4, 12.5, 700, "#ffffff");
|
|
321
|
+
const sub = textEl(NS, n.subLabel, 0, n.r + 15, 10.5, 500, null);
|
|
322
|
+
sub.setAttribute("class", "sub");
|
|
323
|
+
g.append(title, sub);
|
|
324
|
+
} else {
|
|
325
|
+
const label = textEl(NS, n.label, 0, n.r + 13, 10, 500, null);
|
|
326
|
+
g.append(label);
|
|
327
|
+
}
|
|
328
|
+
const tip = document.createElementNS(NS, "title");
|
|
329
|
+
tip.textContent = n.tooltip;
|
|
330
|
+
g.append(tip);
|
|
331
|
+
g.addEventListener("click", (event) => { event.stopPropagation(); n.onClick(); });
|
|
332
|
+
enableDrag(g, n);
|
|
333
|
+
root.append(g);
|
|
334
|
+
return { n, g };
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
function position() {
|
|
338
|
+
for (const { e, pathEl } of edgeEls) {
|
|
339
|
+
const a = byId.get(e.from), b = byId.get(e.to);
|
|
340
|
+
const dx = b.x - a.x, dy = b.y - a.y, d = Math.sqrt(dx * dx + dy * dy) || 1;
|
|
341
|
+
const ax = a.x + (dx / d) * a.r, ay = a.y + (dy / d) * a.r;
|
|
342
|
+
const bx = b.x - (dx / d) * (b.r + (e.style === "requires" ? 4 : 0)), by = b.y - (dy / d) * (b.r + (e.style === "requires" ? 4 : 0));
|
|
343
|
+
const mx = (ax + bx) / 2 - dy / d * d * 0.09, my = (ay + by) / 2 + dx / d * d * 0.09;
|
|
344
|
+
pathEl.setAttribute("d", `M ${ax} ${ay} Q ${mx} ${my} ${bx} ${by}`);
|
|
345
|
+
}
|
|
346
|
+
for (const { n, g } of nodeEls) g.setAttribute("transform", `translate(${n.x},${n.y})`);
|
|
347
|
+
}
|
|
348
|
+
const view = { x: 0, y: 0, k: 1 };
|
|
349
|
+
const applyView = () => root.setAttribute("transform", `translate(${view.x},${view.y}) scale(${view.k})`);
|
|
350
|
+
svg.addEventListener("wheel", (event) => {
|
|
351
|
+
event.preventDefault();
|
|
352
|
+
const factor = event.deltaY < 0 ? 1.12 : 0.9;
|
|
353
|
+
const next = Math.min(4, Math.max(0.25, view.k * factor));
|
|
354
|
+
view.x = event.offsetX - ((event.offsetX - view.x) / view.k) * next;
|
|
355
|
+
view.y = event.offsetY - ((event.offsetY - view.y) / view.k) * next;
|
|
356
|
+
view.k = next; applyView();
|
|
357
|
+
}, { passive: false });
|
|
358
|
+
let pan = null;
|
|
359
|
+
svg.addEventListener("mousedown", (event) => { if (event.target === svg) pan = { x: event.clientX - view.x, y: event.clientY - view.y }; });
|
|
360
|
+
window.addEventListener("mousemove", (event) => { if (pan) { view.x = event.clientX - pan.x; view.y = event.clientY - pan.y; applyView(); } });
|
|
361
|
+
window.addEventListener("mouseup", () => { pan = null; });
|
|
362
|
+
svg.addEventListener("click", () => highlight(null));
|
|
363
|
+
|
|
364
|
+
sim = { nodes: graph.nodes, edges: graph.edges, nodeEls, edgeEls, position, view };
|
|
365
|
+
position();
|
|
366
|
+
highlight(mode === "docs" ? selected : null);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function textEl(NS, content, x, y, size, weight, fill) {
|
|
370
|
+
const t = document.createElementNS(NS, "text");
|
|
371
|
+
t.setAttribute("text-anchor", "middle");
|
|
372
|
+
t.setAttribute("x", x); t.setAttribute("y", y);
|
|
373
|
+
t.setAttribute("font-size", size); t.setAttribute("font-weight", weight);
|
|
374
|
+
if (fill) t.style.fill = fill;
|
|
375
|
+
t.textContent = content;
|
|
376
|
+
return t;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function worstStatus(nodes) {
|
|
380
|
+
return nodes.reduce((worst, n) => (STATUS_RANK[n.status] > STATUS_RANK[worst] ? n.status : worst), "fresh");
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/* Topics 拓扑:topic 聚合节点 + root singletons,边 = 跨域文档关系聚合 */
|
|
384
|
+
function buildTopicGraph(width, height) {
|
|
385
|
+
const topics = [...new Set(state.nodes.map((n) => n.topic).filter(Boolean))].sort();
|
|
386
|
+
const nodes = [];
|
|
387
|
+
for (const topic of topics) {
|
|
388
|
+
const docs = state.nodes.filter((n) => n.topic === topic);
|
|
389
|
+
const tokens = docs.reduce((sum, n) => sum + n.estimatedTokens, 0);
|
|
390
|
+
nodes.push({
|
|
391
|
+
id: "topic:" + topic, isTopic: true, label: topic, subLabel: docs.length + " docs · ~" + tokens + " tk",
|
|
392
|
+
r: 26 + Math.min(26, Math.sqrt(tokens)), fill: colorOf(topic), status: worstStatus(docs),
|
|
393
|
+
tooltip: topic + "/\n" + docs.map((d) => "· " + d.path.split("/").pop()).join("\n"),
|
|
394
|
+
onClick: () => selectTopic(topic)
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
for (const n of state.nodes.filter((n) => !n.topic)) {
|
|
398
|
+
nodes.push({
|
|
399
|
+
id: "doc:" + n.path, isTopic: false, label: n.path.replace(/\.mdx$/, ""),
|
|
400
|
+
r: 10 + Math.min(9, Math.sqrt(n.estimatedTokens)), fill: colorOf(null), status: n.status,
|
|
401
|
+
tooltip: n.path + "\n" + n.description, onClick: () => selectDoc(n.path)
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
const groupOf = (path) => {
|
|
405
|
+
const node = state.nodes.find((n) => n.path === path);
|
|
406
|
+
return node ? (node.topic ? "topic:" + node.topic : "doc:" + path) : null;
|
|
407
|
+
};
|
|
408
|
+
const agg = new Map();
|
|
409
|
+
for (const e of state.edges) {
|
|
410
|
+
const a = groupOf(e.from), b = groupOf(e.to);
|
|
411
|
+
if (!a || !b || a === b) continue;
|
|
412
|
+
const key = [a, b].sort().join("→");
|
|
413
|
+
agg.set(key, (agg.get(key) ?? { from: a, to: b, count: 0 }));
|
|
414
|
+
agg.get(key).count += 1;
|
|
415
|
+
}
|
|
416
|
+
const edges = [...agg.values()].map((e) => ({ from: e.from, to: e.to, style: "aggregate", width: 1.4 + Math.min(6, e.count * 1.1) }));
|
|
417
|
+
simulate(nodes, edges, width, height, Math.min(width, height) / 3.1);
|
|
418
|
+
return { nodes, edges };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/* Documents 全图 */
|
|
422
|
+
function buildDocGraph(width, height) {
|
|
423
|
+
const topics = [...new Set(state.nodes.map((n) => n.topic || "·root"))];
|
|
424
|
+
const centers = new Map(topics.map((topic, i) => {
|
|
425
|
+
const angle = (i / topics.length) * Math.PI * 2;
|
|
426
|
+
return [topic, { x: width / 2 + Math.cos(angle) * width / 4.6, y: height / 2 + Math.sin(angle) * height / 4.6 }];
|
|
427
|
+
}));
|
|
428
|
+
const nodes = state.nodes.map((n) => ({
|
|
429
|
+
id: n.path, isTopic: false,
|
|
430
|
+
label: n.path.split("/").pop().replace(/\.mdx$/, ""),
|
|
431
|
+
r: 8 + Math.min(11, Math.sqrt(n.estimatedTokens)), fill: colorOf(n.topic), status: n.status,
|
|
432
|
+
cluster: centers.get(n.topic || "·root"),
|
|
433
|
+
tooltip: n.path + "\n" + n.description, onClick: () => selectDoc(n.path)
|
|
434
|
+
}));
|
|
435
|
+
const edges = state.edges.map((e) => ({ from: e.from, to: e.to, style: e.type }));
|
|
436
|
+
simulate(nodes, edges, width, height, Math.min(width, height) / 5.2);
|
|
437
|
+
return { nodes, edges };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function enableDrag(g, node) {
|
|
441
|
+
g.addEventListener("mousedown", (event) => {
|
|
442
|
+
event.stopPropagation();
|
|
443
|
+
const start = { x: event.clientX, y: event.clientY, nx: node.x, ny: node.y };
|
|
444
|
+
const move = (e2) => { node.x = start.nx + (e2.clientX - start.x) / sim.view.k; node.y = start.ny + (e2.clientY - start.y) / sim.view.k; sim.position(); };
|
|
445
|
+
const up = () => { window.removeEventListener("mousemove", move); window.removeEventListener("mouseup", up); };
|
|
446
|
+
window.addEventListener("mousemove", move); window.addEventListener("mouseup", up);
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
function highlight(id) {
|
|
451
|
+
if (!sim) return;
|
|
452
|
+
const neighbors = new Set();
|
|
453
|
+
if (id) {
|
|
454
|
+
neighbors.add(id);
|
|
455
|
+
for (const e of sim.edges) { if (e.from === id) neighbors.add(e.to); if (e.to === id) neighbors.add(e.from); }
|
|
456
|
+
}
|
|
457
|
+
for (const { n, g } of sim.nodeEls) g.setAttribute("class", "node" + (id && !neighbors.has(n.id) ? " dim" : ""));
|
|
458
|
+
for (const { e, pathEl } of sim.edgeEls) {
|
|
459
|
+
const cls = "edge-path" + (id && e.from !== id && e.to !== id ? " dim" : "");
|
|
460
|
+
pathEl.setAttribute("class", cls);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/* ---------- detail panel ---------- */
|
|
465
|
+
function setMode(next) {
|
|
466
|
+
mode = next;
|
|
467
|
+
document.getElementById("mode-topics").className = mode === "topics" ? "on" : "";
|
|
468
|
+
document.getElementById("mode-docs").className = mode === "docs" ? "on" : "";
|
|
469
|
+
if (state) renderGraph();
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function selectTopic(topic) {
|
|
473
|
+
selected = null;
|
|
474
|
+
renderSidebar();
|
|
475
|
+
if (mode === "topics") highlight("topic:" + topic);
|
|
476
|
+
const docs = state.nodes.filter((n) => n.topic === topic);
|
|
477
|
+
const detail = document.querySelector("#detail .inner");
|
|
478
|
+
detail.innerHTML =
|
|
479
|
+
'<h2><span class="swatch" style="background:' + colorOf(topic) + ';width:12px;height:12px;border-radius:4px;display:inline-block;margin-right:7px"></span>' + topic + "/</h2>" +
|
|
480
|
+
'<div class="desc">' + docs.length + " docs · ~" + docs.reduce((s, n) => s + n.estimatedTokens, 0) + " tokens · " +
|
|
481
|
+
"状态最差 " + worstStatus(docs) + "</div>" +
|
|
482
|
+
docs.map((n) =>
|
|
483
|
+
'<div class="topic-doc-card" data-path="' + n.path + '">' +
|
|
484
|
+
'<div class="t"><span class="dot" style="width:7px;height:7px;border-radius:50%;background:' + STATUS_COLOR[n.status] + '"></span>' +
|
|
485
|
+
n.path.split("/").pop().replace(/\.mdx$/, "") + '<span style="margin-left:auto;color:var(--faint);font-size:10px;font-weight:500">' + n.kind + "</span></div>" +
|
|
486
|
+
'<div class="d">' + escapeHtml(n.description) + "</div>" +
|
|
487
|
+
"</div>"
|
|
488
|
+
).join("");
|
|
489
|
+
detail.querySelectorAll(".topic-doc-card").forEach((card) => { card.onclick = () => selectDoc(card.dataset.path); });
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
async function selectDoc(path) {
|
|
493
|
+
selected = path;
|
|
494
|
+
renderSidebar();
|
|
495
|
+
if (mode === "docs") highlight(path); else { setMode("docs"); highlight(path); }
|
|
496
|
+
const detail = document.querySelector("#detail .inner");
|
|
497
|
+
const response = await fetch("/api/doc?path=" + encodeURIComponent(path));
|
|
498
|
+
if (!response.ok) { detail.innerHTML = '<div class="placeholder">加载失败</div>'; return; }
|
|
499
|
+
const doc = await response.json();
|
|
500
|
+
const node = state.nodes.find((n) => n.path === path) || {};
|
|
501
|
+
const relations = doc.frontmatter.relations || {};
|
|
502
|
+
const relHtml = (list, label) => (list && list.length)
|
|
503
|
+
? '<div class="rel-block"><b>' + label + ":</b> " + list.map((t) => '<a class="rel-link" data-path="' + t + '">' + t + "</a>").join(" · ") + "</div>"
|
|
504
|
+
: "";
|
|
505
|
+
const body = (doc.body || "").replace(/<CodeRef\s+([^>]*?)\/>/g, (m, attrs) => {
|
|
506
|
+
const pathAttr = /path="([^"]*)"/.exec(attrs), symbol = /symbol="([^"]*)"/.exec(attrs);
|
|
507
|
+
return '<span class="coderef">⌁ ' + (pathAttr ? pathAttr[1] : "") + (symbol ? " · " + symbol[1] : "") + "</span>";
|
|
508
|
+
});
|
|
509
|
+
detail.innerHTML =
|
|
510
|
+
"<h2>" + path + "</h2>" +
|
|
511
|
+
'<div class="desc">' + escapeHtml(doc.frontmatter.description || "") + "</div>" +
|
|
512
|
+
'<div class="meta-row">' +
|
|
513
|
+
'<span class="chip">' + (doc.frontmatter.kind || "?") + "</span>" +
|
|
514
|
+
'<span class="chip" style="color:' + STATUS_COLOR[node.status || "fresh"] + '">' + (node.status || "") + "</span>" +
|
|
515
|
+
'<span class="chip">~' + doc.estimatedTokens + " tokens · " + doc.lineCount + " lines</span>" +
|
|
516
|
+
"</div>" +
|
|
517
|
+
relHtml(relations.requires, "requires") + relHtml(relations.related, "related") +
|
|
518
|
+
((doc.frontmatter.code && doc.frontmatter.code.paths && doc.frontmatter.code.paths.length)
|
|
519
|
+
? '<div class="meta-row">' + doc.frontmatter.code.paths.map((p) => '<span class="code-path">' + escapeHtml(p) + "</span>").join("") + "</div>"
|
|
520
|
+
: "") +
|
|
521
|
+
'<div id="doc-body">' + window.marked.parse(body) + "</div>";
|
|
522
|
+
detail.querySelectorAll(".rel-link").forEach((a) => { a.onclick = () => selectDoc(a.dataset.path); });
|
|
523
|
+
detail.querySelectorAll("#doc-body a[href]").forEach((a) => {
|
|
524
|
+
const href = a.getAttribute("href");
|
|
525
|
+
if (/^https?:|^mailto:|^#/.test(href)) { a.target = "_blank"; return; }
|
|
526
|
+
a.onclick = (event) => {
|
|
527
|
+
event.preventDefault();
|
|
528
|
+
const resolved = normalizePath(path.split("/").slice(0, -1).join("/") + "/" + href.split(/[?#]/)[0]);
|
|
529
|
+
if (state.nodes.some((n) => n.path === resolved)) selectDoc(resolved);
|
|
530
|
+
};
|
|
531
|
+
});
|
|
532
|
+
document.getElementById("detail").scrollTop = 0;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
function normalizePath(p) {
|
|
536
|
+
const parts = [];
|
|
537
|
+
for (const part of p.split("/")) {
|
|
538
|
+
if (!part || part === ".") continue;
|
|
539
|
+
if (part === "..") parts.pop(); else parts.push(part);
|
|
540
|
+
}
|
|
541
|
+
return parts.join("/");
|
|
542
|
+
}
|
|
543
|
+
function escapeHtml(text) { const div = document.createElement("div"); div.textContent = text; return div.innerHTML; }
|
|
544
|
+
|
|
545
|
+
document.getElementById("search").addEventListener("input", renderSidebar);
|
|
546
|
+
document.getElementById("refresh").addEventListener("click", loadState);
|
|
547
|
+
document.getElementById("mode-topics").addEventListener("click", () => setMode("topics"));
|
|
548
|
+
document.getElementById("mode-docs").addEventListener("click", () => setMode("docs"));
|
|
549
|
+
window.addEventListener("resize", () => state && renderGraph());
|
|
550
|
+
setMode("topics");
|
|
551
|
+
loadState();
|
|
552
|
+
</script>
|
|
553
|
+
</body>
|
|
554
|
+
</html>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { runCli } from "../cli.js";
|
|
3
|
+
try {
|
|
4
|
+
const stdin = process.argv[2] === "hook" ? await readStdin() : "";
|
|
5
|
+
const result = await runCli(process.argv.slice(2), process.cwd(), stdin);
|
|
6
|
+
if (result.stdout) {
|
|
7
|
+
process.stdout.write(`${result.stdout}\n`);
|
|
8
|
+
}
|
|
9
|
+
process.exit(result.exitCode);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
process.stderr.write(`llmdoc: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
13
|
+
process.exit(70);
|
|
14
|
+
}
|
|
15
|
+
async function readStdin() {
|
|
16
|
+
if (process.stdin.isTTY) {
|
|
17
|
+
return "";
|
|
18
|
+
}
|
|
19
|
+
const chunks = [];
|
|
20
|
+
for await (const chunk of process.stdin) {
|
|
21
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
22
|
+
}
|
|
23
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
24
|
+
}
|