docspack 0.4.0 → 1.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/dist/agent.d.ts +48 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +243 -0
- package/dist/agent.js.map +1 -0
- package/dist/artifact.d.ts +32 -0
- package/dist/artifact.d.ts.map +1 -0
- package/dist/artifact.js +78 -0
- package/dist/artifact.js.map +1 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +17 -0
- package/dist/build.js.map +1 -1
- package/dist/changed.d.ts +31 -0
- package/dist/changed.d.ts.map +1 -0
- package/dist/changed.js +71 -0
- package/dist/changed.js.map +1 -0
- package/dist/cli.js +131 -10
- package/dist/cli.js.map +1 -1
- package/dist/coverage.d.ts +35 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +64 -0
- package/dist/coverage.js.map +1 -0
- package/dist/db.d.ts +38 -2
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +109 -6
- package/dist/db.js.map +1 -1
- package/dist/discovery.d.ts +14 -0
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +31 -6
- package/dist/discovery.js.map +1 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +44 -0
- package/dist/doctor.js.map +1 -1
- package/dist/help.d.ts.map +1 -1
- package/dist/help.js +65 -4
- package/dist/help.js.map +1 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +3 -0
- package/dist/mcp.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +1 -0
- package/dist/preview.js.map +1 -1
- package/dist/search.d.ts +14 -1
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +108 -21
- package/dist/search.js.map +1 -1
- package/dist/surface.d.ts +45 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +208 -0
- package/dist/surface.js.map +1 -0
- package/dist/sync.d.ts +8 -1
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +50 -1
- package/dist/sync.js.map +1 -1
- package/dist/verify.d.ts +9 -0
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +1 -1
- package/dist/verify.js.map +1 -1
- package/package.json +1 -1
- package/src/agent.ts +308 -0
- package/src/artifact.ts +110 -0
- package/src/build.ts +19 -0
- package/src/changed.ts +99 -0
- package/src/cli.ts +167 -10
- package/src/coverage.ts +96 -0
- package/src/db.ts +168 -7
- package/src/discovery.ts +40 -5
- package/src/doctor.ts +52 -0
- package/src/help.ts +65 -4
- package/src/index.ts +30 -0
- package/src/mcp.ts +3 -0
- package/src/preview.ts +1 -0
- package/src/search.ts +148 -22
- package/src/surface.ts +265 -0
- package/src/sync.ts +66 -2
- package/src/verify.ts +1 -1
package/src/agent.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
|
+
import { DocspackError } from "./errors.js";
|
|
5
|
+
import { AGENTS_SNIPPET, FEEDBACK_SNIPPET } from "./snippet.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wiring docspack into the agent tooling a project already uses.
|
|
9
|
+
*
|
|
10
|
+
* The documented setup is "paste two lines into AGENTS.md", which is manual, easy to forget, and
|
|
11
|
+
* drifts: a line pasted once says whatever docspack did at the time. This writes the same thing
|
|
12
|
+
* from the code that defines it, into a marked block that can be rewritten in place.
|
|
13
|
+
*/
|
|
14
|
+
export type SurfaceKind = "instructions" | "skill" | "hook" | "mcp";
|
|
15
|
+
|
|
16
|
+
export interface AgentFile {
|
|
17
|
+
/** Path relative to the project root. */
|
|
18
|
+
readonly path: string;
|
|
19
|
+
readonly kind: SurfaceKind;
|
|
20
|
+
readonly contents: string;
|
|
21
|
+
readonly status: "create" | "update" | "unchanged";
|
|
22
|
+
/** Why this file is in the plan, for the line the CLI prints. */
|
|
23
|
+
readonly reason: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AgentPlan {
|
|
27
|
+
readonly files: readonly AgentFile[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface AgentOptions {
|
|
31
|
+
readonly cwd: string;
|
|
32
|
+
/**
|
|
33
|
+
* The three surfaces beyond the instruction block. Left unset, each is inferred from what is
|
|
34
|
+
* already on disk, so `docspack agent check` needs no flags to say whether what a project
|
|
35
|
+
* chose is still current — which is the only form of the check CI can run.
|
|
36
|
+
*/
|
|
37
|
+
readonly feedback?: boolean;
|
|
38
|
+
/** Write a SessionStart hook that keeps the index in step with the lockfile. */
|
|
39
|
+
readonly hooks?: boolean;
|
|
40
|
+
/** Write the MCP server into `.mcp.json`. */
|
|
41
|
+
readonly mcp?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const START = "<!-- docspack:start -->";
|
|
45
|
+
const END = "<!-- docspack:end -->";
|
|
46
|
+
|
|
47
|
+
/** Files an agent reads as standing instructions, in the order they are checked. */
|
|
48
|
+
const INSTRUCTION_FILES = ["AGENTS.md", "CLAUDE.md"] as const;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What to write, and where. Nothing is written for a tool the project does not use: the presence
|
|
52
|
+
* of `.claude/` is what says a Claude Code user works here, and a project with neither an
|
|
53
|
+
* AGENTS.md nor a CLAUDE.md gets AGENTS.md, which is the cross-tool convention.
|
|
54
|
+
*/
|
|
55
|
+
export async function planAgentSetup(options: AgentOptions): Promise<AgentPlan> {
|
|
56
|
+
const root = resolve(options.cwd);
|
|
57
|
+
if (!existsSync(join(root, "package.json"))) {
|
|
58
|
+
throw new DocspackError(`No package.json found in ${root}`, {
|
|
59
|
+
hint: "Run docspack from a project directory, or pass --cwd <dir>.",
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const existing = INSTRUCTION_FILES.filter((name) => existsSync(join(root, name)));
|
|
64
|
+
const feedback = options.feedback ?? (await wantsFeedback(root, existing));
|
|
65
|
+
const files: AgentFile[] = [];
|
|
66
|
+
const block = instructionBlock(feedback);
|
|
67
|
+
|
|
68
|
+
const targets = existing.length > 0 ? existing : ["AGENTS.md"];
|
|
69
|
+
for (const name of targets) {
|
|
70
|
+
const before = await read(join(root, name));
|
|
71
|
+
const after = withBlock(before ?? "", block);
|
|
72
|
+
files.push({
|
|
73
|
+
path: name,
|
|
74
|
+
kind: "instructions",
|
|
75
|
+
contents: after,
|
|
76
|
+
status: before === undefined ? "create" : after === before ? "unchanged" : "update",
|
|
77
|
+
reason:
|
|
78
|
+
before === undefined
|
|
79
|
+
? "the cross-tool convention for standing agent instructions"
|
|
80
|
+
: "already read by the agents working here",
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (existsSync(join(root, ".claude"))) {
|
|
85
|
+
const path = join(".claude", "skills", "docspack", "SKILL.md");
|
|
86
|
+
const before = await read(join(root, path));
|
|
87
|
+
const contents = skill(feedback);
|
|
88
|
+
files.push({
|
|
89
|
+
path,
|
|
90
|
+
kind: "skill",
|
|
91
|
+
contents,
|
|
92
|
+
status: before === undefined ? "create" : before === contents ? "unchanged" : "update",
|
|
93
|
+
// A skill's body is loaded when it is used; instructions are loaded every session. Both
|
|
94
|
+
// are written, because only one of them survives a session that never asks a question.
|
|
95
|
+
reason: "loaded on demand, so it costs nothing in a session that asks no questions",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const settings = await read(join(root, ".claude", "settings.json"));
|
|
100
|
+
const wantsHook = options.hooks ?? (settings ?? "").includes("docspack sync");
|
|
101
|
+
if (wantsHook) files.push(await hook(root));
|
|
102
|
+
|
|
103
|
+
const mcpConfig = await read(join(root, ".mcp.json"));
|
|
104
|
+
const wantsMcp = options.mcp ?? /"docspack"\s*:/.test(mcpConfig ?? "");
|
|
105
|
+
if (wantsMcp) files.push(await mcpEntry(root));
|
|
106
|
+
|
|
107
|
+
return { files };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Writes a plan. A file that already carries the current text is left alone. */
|
|
111
|
+
export async function applyAgentSetup(
|
|
112
|
+
options: AgentOptions,
|
|
113
|
+
plan: AgentPlan,
|
|
114
|
+
): Promise<readonly AgentFile[]> {
|
|
115
|
+
const root = resolve(options.cwd);
|
|
116
|
+
const written: AgentFile[] = [];
|
|
117
|
+
|
|
118
|
+
for (const file of plan.files) {
|
|
119
|
+
if (file.status === "unchanged") continue;
|
|
120
|
+
const target = join(root, file.path);
|
|
121
|
+
await mkdir(dirname(target), { recursive: true });
|
|
122
|
+
await writeFile(target, file.contents, "utf8");
|
|
123
|
+
written.push(file);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return written;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Replaces the marked block, or appends one. Everything outside the markers is left exactly as
|
|
131
|
+
* it was: these files are written by people, and a tool that rewrites them wholesale is a tool
|
|
132
|
+
* nobody runs twice.
|
|
133
|
+
*/
|
|
134
|
+
export function withBlock(document: string, block: string): string {
|
|
135
|
+
const start = document.indexOf(START);
|
|
136
|
+
const end = document.indexOf(END);
|
|
137
|
+
|
|
138
|
+
if (start !== -1 && end > start) {
|
|
139
|
+
return `${document.slice(0, start)}${block}${document.slice(end + END.length)}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const body = document.trimEnd();
|
|
143
|
+
return body.length === 0 ? `${block}\n` : `${body}\n\n${block}\n`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Whether a project already asked for the feedback half, read from what it has on disk. */
|
|
147
|
+
async function wantsFeedback(root: string, files: readonly string[]): Promise<boolean> {
|
|
148
|
+
for (const name of files) {
|
|
149
|
+
if ((await read(join(root, name)))?.includes("docspack feedback add") === true) return true;
|
|
150
|
+
}
|
|
151
|
+
const skillFile = await read(join(root, ".claude", "skills", "docspack", "SKILL.md"));
|
|
152
|
+
return skillFile?.includes("docspack feedback add") === true;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function instructionBlock(feedback: boolean): string {
|
|
156
|
+
const lines = [
|
|
157
|
+
START,
|
|
158
|
+
"## Documentation for this project's dependencies",
|
|
159
|
+
"",
|
|
160
|
+
...AGENTS_SNIPPET,
|
|
161
|
+
...(feedback ? ["", ...FEEDBACK_SNIPPET] : []),
|
|
162
|
+
"",
|
|
163
|
+
"<!-- Written by `docspack agent install`. Re-run it to update this block. -->",
|
|
164
|
+
END,
|
|
165
|
+
];
|
|
166
|
+
return lines.join("\n");
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The Claude Code skill. Its description is what decides whether the skill is reached for, and
|
|
171
|
+
* the body is loaded only once it is — so the description names the situations rather than the
|
|
172
|
+
* tool, and the body carries what the two-line instruction cannot afford to.
|
|
173
|
+
*/
|
|
174
|
+
function skill(feedback: boolean): string {
|
|
175
|
+
return [
|
|
176
|
+
"---",
|
|
177
|
+
"name: docspack",
|
|
178
|
+
"description: >-",
|
|
179
|
+
" Answer questions about this project's installed dependencies from local, version-matched",
|
|
180
|
+
" documentation instead of memory. Use before writing or reviewing code that calls a",
|
|
181
|
+
" dependency, when an API's shape is uncertain, when an import or a signature may have",
|
|
182
|
+
" changed between versions, or when a name is not in any documentation at all.",
|
|
183
|
+
"allowed-tools: Bash",
|
|
184
|
+
"---",
|
|
185
|
+
"",
|
|
186
|
+
"# Documentation for installed dependencies",
|
|
187
|
+
"",
|
|
188
|
+
'Run `docspack ask "<question>"` for anything about a dependency of this project. It answers',
|
|
189
|
+
"from the versions in `node_modules`, offline, in about 100ms, and bounded to 3,000 tokens.",
|
|
190
|
+
"",
|
|
191
|
+
"Prefer it over recalling an API from training data. The installed version is usually newer",
|
|
192
|
+
"than the training data and the answer states which release it came from.",
|
|
193
|
+
"",
|
|
194
|
+
"## What the answer can tell you",
|
|
195
|
+
"",
|
|
196
|
+
"- A passage from a documentation package, headed by its chunk id and the version it documents.",
|
|
197
|
+
"- A declaration read from the installed build, when the question names something the",
|
|
198
|
+
" documentation never mentions. It carries the import to write and the signature as installed.",
|
|
199
|
+
"- A note saying the name is exported but documented nowhere. That is a real answer: it means",
|
|
200
|
+
" no prose exists, not that the search failed.",
|
|
201
|
+
"",
|
|
202
|
+
"## Other commands",
|
|
203
|
+
"",
|
|
204
|
+
"- `docspack changed <library>` — what a library's exports gained and lost between two",
|
|
205
|
+
" installed versions. Use it when an upgrade broke something, or before writing code against",
|
|
206
|
+
" a version newer than what you remember.",
|
|
207
|
+
"- `docspack list` — which packages are indexed here.",
|
|
208
|
+
"- `docspack sync` — index them. Run it if `ask` reports something installed but not indexed.",
|
|
209
|
+
...(feedback
|
|
210
|
+
? [
|
|
211
|
+
"",
|
|
212
|
+
"## Recording a problem",
|
|
213
|
+
"",
|
|
214
|
+
"If the documentation contradicts the installed package, record it:",
|
|
215
|
+
"",
|
|
216
|
+
"```bash",
|
|
217
|
+
'docspack feedback add --chunk <id> --kind <drift|incorrect|missing> --evidence "<claim>"',
|
|
218
|
+
"```",
|
|
219
|
+
"",
|
|
220
|
+
"Only claims that can be shown false. `incorrect` and `missing` also need `--expected`,",
|
|
221
|
+
"`--actual` and `--repro`, so record those only after running something. It appends to a",
|
|
222
|
+
"local file for a human to review and sends nothing.",
|
|
223
|
+
]
|
|
224
|
+
: []),
|
|
225
|
+
"",
|
|
226
|
+
].join("\n");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* A SessionStart hook, so the index follows the lockfile without anyone remembering to sync.
|
|
231
|
+
*
|
|
232
|
+
* `--quiet` keeps it silent: a hook that prints on every session start spends context on a
|
|
233
|
+
* message saying nothing changed.
|
|
234
|
+
*/
|
|
235
|
+
async function hook(root: string): Promise<AgentFile> {
|
|
236
|
+
const path = join(".claude", "settings.json");
|
|
237
|
+
const before = await read(join(root, path));
|
|
238
|
+
const settings = parseJson(before, path);
|
|
239
|
+
// Silent and never failing: a session must not break, or print, because an index is stale.
|
|
240
|
+
const command = "npx --no-install docspack sync --quiet >/dev/null 2>&1 || true";
|
|
241
|
+
|
|
242
|
+
const hooks = (settings.hooks ?? {}) as Record<string, unknown>;
|
|
243
|
+
const sessionStart = Array.isArray(hooks.SessionStart) ? [...hooks.SessionStart] : [];
|
|
244
|
+
const already = JSON.stringify(sessionStart).includes("docspack sync");
|
|
245
|
+
if (!already) sessionStart.push({ hooks: [{ type: "command", command }] });
|
|
246
|
+
|
|
247
|
+
const contents = `${JSON.stringify(
|
|
248
|
+
{ ...settings, hooks: { ...hooks, SessionStart: sessionStart } },
|
|
249
|
+
null,
|
|
250
|
+
2,
|
|
251
|
+
)}\n`;
|
|
252
|
+
|
|
253
|
+
return {
|
|
254
|
+
path,
|
|
255
|
+
kind: "hook",
|
|
256
|
+
contents,
|
|
257
|
+
status: before === undefined ? "create" : before === contents ? "unchanged" : "update",
|
|
258
|
+
reason: "keeps the index in step with the lockfile without anyone remembering to sync",
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async function mcpEntry(root: string): Promise<AgentFile> {
|
|
263
|
+
const path = ".mcp.json";
|
|
264
|
+
const before = await read(join(root, path));
|
|
265
|
+
const config = parseJson(before, path);
|
|
266
|
+
const servers = (config.mcpServers ?? {}) as Record<string, unknown>;
|
|
267
|
+
const contents = `${JSON.stringify(
|
|
268
|
+
{
|
|
269
|
+
...config,
|
|
270
|
+
mcpServers: {
|
|
271
|
+
...servers,
|
|
272
|
+
docspack: { command: "npx", args: ["-y", "docspack", "mcp"] },
|
|
273
|
+
},
|
|
274
|
+
},
|
|
275
|
+
null,
|
|
276
|
+
2,
|
|
277
|
+
)}\n`;
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
path,
|
|
281
|
+
kind: "mcp",
|
|
282
|
+
contents,
|
|
283
|
+
status: before === undefined ? "create" : before === contents ? "unchanged" : "update",
|
|
284
|
+
reason: "a declared tool, for clients that would rather have one than a shell command",
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function parseJson(raw: string | undefined, path: string): Record<string, unknown> {
|
|
289
|
+
if (raw === undefined || raw.trim().length === 0) return {};
|
|
290
|
+
try {
|
|
291
|
+
const parsed: unknown = JSON.parse(raw);
|
|
292
|
+
if (typeof parsed !== "object" || parsed === null) throw new Error("not an object");
|
|
293
|
+
return parsed as Record<string, unknown>;
|
|
294
|
+
} catch (error) {
|
|
295
|
+
throw new DocspackError(`${path} is not valid JSON`, {
|
|
296
|
+
hint: "Fix it, or move it aside and run this again.",
|
|
297
|
+
cause: error,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async function read(path: string): Promise<string | undefined> {
|
|
303
|
+
try {
|
|
304
|
+
return await readFile(path, "utf8");
|
|
305
|
+
} catch {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
}
|
package/src/artifact.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type { IndexedChunk } from "./db.js";
|
|
4
|
+
import { chunkId, estimateTokens, packageId } from "./spec.js";
|
|
5
|
+
import { declarationsIn, type ExportedSymbol, readPublicSurface } from "./surface.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A package derived from an installed library rather than published by its author.
|
|
9
|
+
*
|
|
10
|
+
* Half of a well-documented library's exported names are mentioned nowhere in its own
|
|
11
|
+
* documentation (see `docs/design/beyond-retrieval.md`), and almost all of them are declared in
|
|
12
|
+
* the type declarations sitting in `node_modules`. Those declarations are the answer a
|
|
13
|
+
* documentation site cannot serve, because the site is not the artifact.
|
|
14
|
+
*/
|
|
15
|
+
export interface ArtifactPackage {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly version: string;
|
|
19
|
+
readonly chunks: readonly IndexedChunk[];
|
|
20
|
+
/**
|
|
21
|
+
* Every exported name, mapped to the chunk carrying its declaration — or to an empty string
|
|
22
|
+
* when the package exports the name without declaring it anywhere this can read.
|
|
23
|
+
*
|
|
24
|
+
* Recording those too is what keeps `changed` honest: a name re-exported from a file with no
|
|
25
|
+
* declaration in it would otherwise look like a name the release removed.
|
|
26
|
+
*/
|
|
27
|
+
readonly symbols: ReadonlyMap<string, string>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Beyond this a package is generated machinery, and indexing it would drown the prose. */
|
|
31
|
+
const MAX_SYMBOLS = 2000;
|
|
32
|
+
|
|
33
|
+
/** How much of a declaration is carried. Long enough for a signature, short enough to rank. */
|
|
34
|
+
const DECLARATION_LIMIT = 400;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Derives chunks from an installed library: one per exported name, carrying the import a caller
|
|
38
|
+
* writes and the declaration as the installed build states it.
|
|
39
|
+
*
|
|
40
|
+
* Returns nothing when the package ships no type declarations. That is the common case across a
|
|
41
|
+
* dependency tree and is not a problem worth reporting.
|
|
42
|
+
*/
|
|
43
|
+
export async function readArtifact(dir: string): Promise<ArtifactPackage | undefined> {
|
|
44
|
+
const surface = await readPublicSurface(dir);
|
|
45
|
+
if (surface === undefined || surface.name.length === 0 || surface.version.length === 0) {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const id = packageId(surface.name, surface.version);
|
|
50
|
+
const declared = new Map<string, Map<string, string>>();
|
|
51
|
+
const chunks: IndexedChunk[] = [];
|
|
52
|
+
const symbols = new Map<string, string>();
|
|
53
|
+
|
|
54
|
+
for (const symbol of surface.symbols.values()) {
|
|
55
|
+
symbols.set(symbol.name, "");
|
|
56
|
+
if (chunks.length >= MAX_SYMBOLS) continue;
|
|
57
|
+
|
|
58
|
+
let declarations = declared.get(symbol.file);
|
|
59
|
+
if (declarations === undefined) {
|
|
60
|
+
try {
|
|
61
|
+
declarations = declarationsIn(
|
|
62
|
+
await readFile(join(dir, symbol.file), "utf8"),
|
|
63
|
+
DECLARATION_LIMIT,
|
|
64
|
+
);
|
|
65
|
+
} catch {
|
|
66
|
+
declarations = new Map();
|
|
67
|
+
}
|
|
68
|
+
declared.set(symbol.file, declarations);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const declaration = declarations.get(symbol.name);
|
|
72
|
+
// A name that only appears in an `export { … }` list has no declaration to show. The import
|
|
73
|
+
// line alone would say nothing the name did not already say, so it is left out.
|
|
74
|
+
if (declaration === undefined) continue;
|
|
75
|
+
|
|
76
|
+
const content = render(symbol, declaration);
|
|
77
|
+
const chunk = chunkId(id, symbol.name);
|
|
78
|
+
chunks.push({
|
|
79
|
+
chunkId: chunk,
|
|
80
|
+
filePath: symbol.file,
|
|
81
|
+
tokens: estimateTokens(content),
|
|
82
|
+
content,
|
|
83
|
+
// The name is the tag, and tags are weighted 3× against prose in the ranking.
|
|
84
|
+
tags: [symbol.name, symbol.from],
|
|
85
|
+
});
|
|
86
|
+
symbols.set(symbol.name, chunk);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (symbols.size === 0) return undefined;
|
|
90
|
+
return { id, name: surface.name, version: surface.version, chunks, symbols };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* What an agent receives. The import line is first because it is the part a caller gets wrong
|
|
95
|
+
* most often — a name that exists at a subpath nobody guessed is the same failure as a name that
|
|
96
|
+
* does not exist.
|
|
97
|
+
*/
|
|
98
|
+
function render(symbol: ExportedSymbol, declaration: string): string {
|
|
99
|
+
return [
|
|
100
|
+
`# ${symbol.name}`,
|
|
101
|
+
"",
|
|
102
|
+
`Declared by \`${symbol.from}\`. This is the installed build's own declaration, not prose.`,
|
|
103
|
+
"",
|
|
104
|
+
"```ts",
|
|
105
|
+
`import { ${symbol.name} } from "${symbol.from}";`,
|
|
106
|
+
"",
|
|
107
|
+
declaration.trim(),
|
|
108
|
+
"```",
|
|
109
|
+
].join("\n");
|
|
110
|
+
}
|
package/src/build.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
import type { Dirent } from "node:fs";
|
|
2
3
|
import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
3
4
|
import { join, relative, sep } from "node:path";
|
|
@@ -281,6 +282,13 @@ async function collectFromSource(
|
|
|
281
282
|
|
|
282
283
|
const documents: SourceDocument[] = [];
|
|
283
284
|
const links = fetchableLinks(parsed).slice(0, options.pages ?? 50);
|
|
285
|
+
// Two links can name the same document. `fetchableLinks` already drops a repeated URL and a
|
|
286
|
+
// repeated fragment, but a site whose index links anchors as a query — `/v4?id=codecs` — gets
|
|
287
|
+
// one entry per anchor, and each one fetches the whole page again. Left alone that packages
|
|
288
|
+
// the same text dozens of times: an index mostly made of duplicates, which is slower to search
|
|
289
|
+
// and ranks worse, because copies of one page crowd out the rest of the corpus.
|
|
290
|
+
const seen = new Set<string>();
|
|
291
|
+
let duplicates = 0;
|
|
284
292
|
|
|
285
293
|
for (const link of links) {
|
|
286
294
|
options.onProgress?.(`fetching ${link.url}`);
|
|
@@ -293,6 +301,12 @@ async function collectFromSource(
|
|
|
293
301
|
warnings.push(`skipped ${link.url}: document was empty`);
|
|
294
302
|
continue;
|
|
295
303
|
}
|
|
304
|
+
const fingerprint = createHash("sha256").update(text).digest("hex");
|
|
305
|
+
if (seen.has(fingerprint)) {
|
|
306
|
+
duplicates += 1;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
seen.add(fingerprint);
|
|
296
310
|
documents.push({
|
|
297
311
|
title: link.title.length > 0 ? link.title : (firstHeading(text) ?? link.url),
|
|
298
312
|
origin: link.url,
|
|
@@ -306,6 +320,11 @@ async function collectFromSource(
|
|
|
306
320
|
}
|
|
307
321
|
}
|
|
308
322
|
|
|
323
|
+
if (duplicates > 0) {
|
|
324
|
+
warnings.push(
|
|
325
|
+
`skipped ${duplicates} of ${links.length} linked documents whose text repeated one already packaged`,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
309
328
|
if (fetchableLinks(parsed).length > links.length) {
|
|
310
329
|
warnings.push(
|
|
311
330
|
`packaged ${links.length} of ${fetchableLinks(parsed).length} linked documents (raise with --pages)`,
|
package/src/changed.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { identifiers } from "./coverage.js";
|
|
2
|
+
import type { Store } from "./db.js";
|
|
3
|
+
import { discoverLibraries, discoverPackages } from "./discovery.js";
|
|
4
|
+
import { DocspackError } from "./errors.js";
|
|
5
|
+
import { packageId } from "./spec.js";
|
|
6
|
+
|
|
7
|
+
export interface SurfaceChange {
|
|
8
|
+
readonly library: string;
|
|
9
|
+
readonly from: string;
|
|
10
|
+
readonly to: string;
|
|
11
|
+
/** Names the newer release exports that the older one did not. */
|
|
12
|
+
readonly added: readonly string[];
|
|
13
|
+
/** Names the older release exported that the newer one does not. */
|
|
14
|
+
readonly removed: readonly string[];
|
|
15
|
+
/**
|
|
16
|
+
* Added names that no documentation package installed here mentions. These are the ones a
|
|
17
|
+
* model cannot know: too new for its training data, and absent from what the vendor published.
|
|
18
|
+
*/
|
|
19
|
+
readonly undocumented: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ChangedOptions {
|
|
23
|
+
readonly cwd: string;
|
|
24
|
+
readonly store: Store;
|
|
25
|
+
/** Library name, optionally with the version to compare against: `hono` or `hono@4.0.0`. */
|
|
26
|
+
readonly library: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* What changed in a library's exported surface between two versions in the store.
|
|
31
|
+
*
|
|
32
|
+
* Both versions come from the local index, never the network: the store is global, so a version
|
|
33
|
+
* indexed for another project on this machine is already here. Measured across two libraries,
|
|
34
|
+
* upgrades are overwhelmingly additive — hono 3.12.12 to 4.13.3 removed three names and added
|
|
35
|
+
* 243 — so the useful report is what exists now that an older release did not have.
|
|
36
|
+
*/
|
|
37
|
+
export async function changedSurface(options: ChangedOptions): Promise<SurfaceChange> {
|
|
38
|
+
const [name, requested] = split(options.library);
|
|
39
|
+
|
|
40
|
+
const installed = (await discoverLibraries(options.cwd)).find((library) => library.name === name);
|
|
41
|
+
const indexed = options.store.versionsOf(name).filter((pkg) => pkg.kind === "artifact");
|
|
42
|
+
if (indexed.length === 0) {
|
|
43
|
+
throw new DocspackError(`No version of ${name} is indexed`, {
|
|
44
|
+
hint:
|
|
45
|
+
installed === undefined
|
|
46
|
+
? `${name} is not a dependency of this project.`
|
|
47
|
+
: "Run `docspack sync` to index the installed version, then ask again.",
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const to = installed?.version ?? indexed[indexed.length - 1]?.version;
|
|
52
|
+
if (to === undefined) throw new DocspackError(`No version of ${name} is indexed`);
|
|
53
|
+
|
|
54
|
+
const others = indexed.map((pkg) => pkg.version).filter((version) => version !== to);
|
|
55
|
+
const from = requested ?? others[others.length - 1];
|
|
56
|
+
if (from === undefined) {
|
|
57
|
+
throw new DocspackError(`Only ${name}@${to} is indexed, so there is nothing to compare`, {
|
|
58
|
+
hint: "Index another version by running `docspack sync` in a project that installs it. The store is shared across projects on this machine.",
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
if (!options.store.hasPackage(packageId(name, from))) {
|
|
62
|
+
throw new DocspackError(`${name}@${from} is not in the store`, {
|
|
63
|
+
hint: `Indexed versions: ${indexed.map((pkg) => pkg.version).join(", ")}`,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const before = new Set(options.store.symbolsOf(packageId(name, from)));
|
|
68
|
+
const after = new Set(options.store.symbolsOf(packageId(name, to)));
|
|
69
|
+
const added = [...after].filter((symbol) => !before.has(symbol)).sort();
|
|
70
|
+
const removed = [...before].filter((symbol) => !after.has(symbol)).sort();
|
|
71
|
+
|
|
72
|
+
const documented = await documentedNames(options);
|
|
73
|
+
return {
|
|
74
|
+
library: name,
|
|
75
|
+
from,
|
|
76
|
+
to,
|
|
77
|
+
added,
|
|
78
|
+
removed,
|
|
79
|
+
undocumented: added.filter((symbol) => !documented.has(symbol)),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Every identifier any documentation package installed here mentions. */
|
|
84
|
+
async function documentedNames(options: ChangedOptions): Promise<Set<string>> {
|
|
85
|
+
const names = new Set<string>();
|
|
86
|
+
for (const pkg of (await discoverPackages(options.cwd)).packages) {
|
|
87
|
+
for (const chunk of options.store.chunksOf(pkg.id)) {
|
|
88
|
+
for (const identifier of identifiers(chunk.content)) names.add(identifier);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return names;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `hono@4.0.0` into its parts, keeping a scope's leading `@` out of it. */
|
|
95
|
+
function split(spec: string): [name: string, version: string | undefined] {
|
|
96
|
+
const at = spec.lastIndexOf("@");
|
|
97
|
+
if (at <= 0) return [spec, undefined];
|
|
98
|
+
return [spec.slice(0, at), spec.slice(at + 1)];
|
|
99
|
+
}
|