docspack 0.2.0 → 0.4.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/README.md +9 -3
- package/dist/build.d.ts +21 -0
- package/dist/build.d.ts.map +1 -1
- package/dist/build.js +194 -31
- package/dist/build.js.map +1 -1
- package/dist/cli.js +74 -131
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +7 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +20 -1
- package/dist/config.js.map +1 -1
- package/dist/db.d.ts +8 -4
- package/dist/db.d.ts.map +1 -1
- package/dist/db.js +13 -7
- package/dist/db.js.map +1 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +47 -4
- package/dist/doctor.js.map +1 -1
- package/dist/document.d.ts +2 -0
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +7 -3
- package/dist/document.js.map +1 -1
- package/dist/eval.d.ts +52 -0
- package/dist/eval.d.ts.map +1 -0
- package/dist/eval.js +101 -0
- package/dist/eval.js.map +1 -0
- package/dist/help.d.ts +31 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +342 -0
- package/dist/help.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +4 -2
- package/dist/preview.js.map +1 -1
- package/dist/search.d.ts +7 -0
- package/dist/search.d.ts.map +1 -1
- package/dist/search.js +33 -3
- package/dist/search.js.map +1 -1
- package/dist/spec.d.ts +27 -1
- package/dist/spec.d.ts.map +1 -1
- package/dist/spec.js +53 -5
- package/dist/spec.js.map +1 -1
- package/dist/stopwords.d.ts +14 -0
- package/dist/stopwords.d.ts.map +1 -0
- package/dist/stopwords.js +121 -0
- package/dist/stopwords.js.map +1 -0
- package/dist/sync.js +1 -1
- package/dist/sync.js.map +1 -1
- package/dist/verify.d.ts +8 -2
- package/dist/verify.d.ts.map +1 -1
- package/dist/verify.js +84 -23
- package/dist/verify.js.map +1 -1
- package/package.json +1 -1
- package/src/build.ts +261 -33
- package/src/cli.ts +91 -138
- package/src/config.ts +35 -5
- package/src/db.ts +13 -7
- package/src/doctor.ts +49 -5
- package/src/document.ts +13 -4
- package/src/eval.ts +149 -0
- package/src/help.ts +382 -0
- package/src/index.ts +20 -0
- package/src/preview.ts +4 -1
- package/src/search.ts +41 -3
- package/src/spec.ts +84 -6
- package/src/stopwords.ts +120 -0
- package/src/sync.ts +1 -1
- package/src/verify.ts +108 -28
package/src/eval.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { DocspackError } from "./errors.js";
|
|
3
|
+
import { previewPackage } from "./preview.js";
|
|
4
|
+
import { DEFAULT_LIMIT, DEFAULT_MAX_TOKENS } from "./search.js";
|
|
5
|
+
|
|
6
|
+
/** One question, and the chunk ids that would be a correct answer to it. */
|
|
7
|
+
export interface EvalCase {
|
|
8
|
+
readonly query: string;
|
|
9
|
+
/** Bare chunk ids, as they appear in the manifest. Any one of them counts as a hit. */
|
|
10
|
+
readonly expect: readonly string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface EvalCaseResult extends EvalCase {
|
|
14
|
+
/** Chunk ids returned, in rank order. */
|
|
15
|
+
readonly returned: readonly string[];
|
|
16
|
+
/** 1-based rank of the first expected chunk, or 0 when none of them came back. */
|
|
17
|
+
readonly rank: number;
|
|
18
|
+
readonly tokens: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EvalReport {
|
|
22
|
+
readonly cases: readonly EvalCaseResult[];
|
|
23
|
+
/** Cases where an expected chunk came back at all, within `limit`. */
|
|
24
|
+
readonly hits: number;
|
|
25
|
+
/** Cases where an expected chunk came back first. */
|
|
26
|
+
readonly top1: number;
|
|
27
|
+
readonly total: number;
|
|
28
|
+
/** Mean tokens per answer — what tuning the chunk budget trades retrieval against. */
|
|
29
|
+
readonly averageTokens: number;
|
|
30
|
+
readonly limit: number;
|
|
31
|
+
readonly ok: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface EvalOptions {
|
|
35
|
+
readonly dir: string;
|
|
36
|
+
readonly cases: readonly EvalCase[];
|
|
37
|
+
readonly limit?: number;
|
|
38
|
+
readonly maxTokens?: number;
|
|
39
|
+
/** Percentage of cases that must return an expected chunk. Below it, `ok` is false. */
|
|
40
|
+
readonly minHitRate?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Answers a set of questions from a package on disk and reports how often the right chunk came
|
|
45
|
+
* back.
|
|
46
|
+
*
|
|
47
|
+
* `doctor` cannot see this. Every chunk can be well-formed, well-tagged and the right size while
|
|
48
|
+
* the package still answers the wrong question, and the highest-leverage authoring decision —
|
|
49
|
+
* the chunk budget — trades two things off against each other in a way only measurement settles:
|
|
50
|
+
* bm25 normalizes for length, so retrieval prefers small chunks, while an answer capped at three
|
|
51
|
+
* of them prefers large ones.
|
|
52
|
+
*/
|
|
53
|
+
export async function evaluatePackage(options: EvalOptions): Promise<EvalReport> {
|
|
54
|
+
if (options.cases.length === 0) {
|
|
55
|
+
throw new DocspackError("The evaluation set is empty", {
|
|
56
|
+
hint: "Add at least one { query, expect } entry.",
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const limit = options.limit ?? DEFAULT_LIMIT;
|
|
61
|
+
const cases: EvalCaseResult[] = [];
|
|
62
|
+
|
|
63
|
+
for (const entry of options.cases) {
|
|
64
|
+
const result = await previewPackage({
|
|
65
|
+
dir: options.dir,
|
|
66
|
+
query: entry.query,
|
|
67
|
+
limit,
|
|
68
|
+
maxTokens: options.maxTokens ?? DEFAULT_MAX_TOKENS,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// A hit is compared on the bare id: an eval set written by the author must not have to
|
|
72
|
+
// restate the package name and version it is already sitting inside.
|
|
73
|
+
const returned = result.hits.map((hit) => bareChunkId(hit.chunkId));
|
|
74
|
+
const expected = new Set(entry.expect);
|
|
75
|
+
const rank = returned.findIndex((id) => expected.has(id)) + 1;
|
|
76
|
+
|
|
77
|
+
cases.push({ ...entry, returned, rank, tokens: result.tokens });
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const hits = cases.filter((entry) => entry.rank > 0).length;
|
|
81
|
+
const total = cases.length;
|
|
82
|
+
const rate = (hits / total) * 100;
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
cases,
|
|
86
|
+
hits,
|
|
87
|
+
top1: cases.filter((entry) => entry.rank === 1).length,
|
|
88
|
+
total,
|
|
89
|
+
averageTokens: Math.round(cases.reduce((sum, entry) => sum + entry.tokens, 0) / total),
|
|
90
|
+
limit,
|
|
91
|
+
ok: options.minHitRate === undefined || rate >= options.minHitRate,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** `@acme/docspack@1.4.0/api-auth` -> `api-auth`. */
|
|
96
|
+
function bareChunkId(id: string): string {
|
|
97
|
+
const slash = id.lastIndexOf("/");
|
|
98
|
+
return slash === -1 ? id : id.slice(slash + 1);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Reads an evaluation set: a JSON array of `{ query, expect }`, where `expect` is a chunk id or
|
|
103
|
+
* an array of them. Malformed entries are an error rather than a skip — a gate that silently
|
|
104
|
+
* evaluates nine of ten questions reports a hit rate that is not about the set it was given.
|
|
105
|
+
*/
|
|
106
|
+
export async function readEvalSet(file: string): Promise<readonly EvalCase[]> {
|
|
107
|
+
let raw: string;
|
|
108
|
+
try {
|
|
109
|
+
raw = await readFile(file, "utf8");
|
|
110
|
+
} catch {
|
|
111
|
+
throw new DocspackError(`Could not read the evaluation set ${file}`, {
|
|
112
|
+
hint: 'It is a JSON array of { "query": "…", "expect": "chunk-id" }.',
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let parsed: unknown;
|
|
117
|
+
try {
|
|
118
|
+
parsed = JSON.parse(raw);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new DocspackError(`${file} is not valid JSON`, { cause: error });
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return parseEvalSet(parsed, file);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function parseEvalSet(value: unknown, where: string): readonly EvalCase[] {
|
|
127
|
+
const fail = (message: string): never => {
|
|
128
|
+
throw new DocspackError(`${where}: ${message}`, {
|
|
129
|
+
hint: 'Each entry is { "query": "how do I …", "expect": "chunk-id" }.',
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
if (!Array.isArray(value)) fail("expected a JSON array of evaluation cases");
|
|
134
|
+
|
|
135
|
+
return (value as unknown[]).map((entry, index): EvalCase => {
|
|
136
|
+
if (typeof entry !== "object" || entry === null) return fail(`case #${index} is not an object`);
|
|
137
|
+
const record = entry as { query?: unknown; expect?: unknown };
|
|
138
|
+
|
|
139
|
+
if (typeof record.query !== "string" || record.query.trim().length === 0) {
|
|
140
|
+
return fail(`case #${index} needs a non-empty "query"`);
|
|
141
|
+
}
|
|
142
|
+
const expect = Array.isArray(record.expect) ? record.expect : [record.expect];
|
|
143
|
+
if (expect.length === 0 || expect.some((id) => typeof id !== "string" || id.length === 0)) {
|
|
144
|
+
return fail(`case "${record.query}" needs "expect": a chunk id, or an array of them`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { query: record.query, expect: expect as string[] };
|
|
148
|
+
});
|
|
149
|
+
}
|
package/src/help.ts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import { AGENTS_SNIPPET, FEEDBACK_SNIPPET } from "./snippet.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The help text, per command.
|
|
5
|
+
*
|
|
6
|
+
* `docspack <command> --help` printed the global help, so no flag was discoverable from the CLI
|
|
7
|
+
* at all — `doctor --pedantic`, `ask --max-tokens` and `init --mirror` were findable only by
|
|
8
|
+
* reading the source. That matters more than usual for a tool whose main user is an agent that
|
|
9
|
+
* will not open a browser, and it is why the global page now points at these rather than trying
|
|
10
|
+
* to list every flag of every command in one block.
|
|
11
|
+
*/
|
|
12
|
+
export interface CommandHelp {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** The line this command shows in the global help and on the site. */
|
|
15
|
+
readonly summary: string;
|
|
16
|
+
readonly group: "core" | "authoring";
|
|
17
|
+
readonly usage: string;
|
|
18
|
+
/** Flags this command reads, beyond the global ones. */
|
|
19
|
+
readonly options: readonly (readonly [string, string])[];
|
|
20
|
+
readonly detail?: readonly string[];
|
|
21
|
+
readonly examples: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const LIMIT = 3;
|
|
25
|
+
const MAX_TOKENS = 3000;
|
|
26
|
+
|
|
27
|
+
const QUERY_OPTIONS: readonly (readonly [string, string])[] = [
|
|
28
|
+
["-p, --package <s>", "only packages whose name contains this text"],
|
|
29
|
+
["--limit <n>", `maximum chunks to return (default ${LIMIT})`],
|
|
30
|
+
["--max-tokens <n>", `token ceiling for the result set (default ${MAX_TOKENS})`],
|
|
31
|
+
["--all", "search the whole store, not just this project"],
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
export const COMMANDS: readonly CommandHelp[] = [
|
|
35
|
+
{
|
|
36
|
+
name: "sync",
|
|
37
|
+
summary: "Index the docs packages this project depends on",
|
|
38
|
+
group: "core",
|
|
39
|
+
usage: "docspack sync [options]",
|
|
40
|
+
options: [["--force", "re-index packages already in the store"]],
|
|
41
|
+
detail: [
|
|
42
|
+
"Reads node_modules and indexes every @vendor/docspack and @docspack-community/<name>",
|
|
43
|
+
"package this project declares. Makes no network requests.",
|
|
44
|
+
],
|
|
45
|
+
examples: ["docspack sync", "docspack sync --force"],
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "ask",
|
|
49
|
+
summary: "Answer from the local index — the command to give an agent",
|
|
50
|
+
group: "core",
|
|
51
|
+
usage: 'docspack ask "<question>"',
|
|
52
|
+
options: QUERY_OPTIONS,
|
|
53
|
+
detail: [
|
|
54
|
+
"Answers from the installed versions, in the Markdown an agent should read. Exits 0 when",
|
|
55
|
+
"chunks were returned, 3 when a docs package is installed but not indexed, and 4 when",
|
|
56
|
+
"everything installed is indexed and nothing matched.",
|
|
57
|
+
],
|
|
58
|
+
examples: [
|
|
59
|
+
'docspack ask "how do I verify a webhook signature"',
|
|
60
|
+
'docspack ask "webhook signature" --package stripe --limit 5',
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: "search",
|
|
65
|
+
summary: "Same index, formatted for a human reading the terminal",
|
|
66
|
+
group: "core",
|
|
67
|
+
usage: "docspack search <query>",
|
|
68
|
+
options: QUERY_OPTIONS,
|
|
69
|
+
detail: ["The same index and ranking as `ask`, printed as a list of hits with a preview."],
|
|
70
|
+
examples: ["docspack search webhook signature"],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "list",
|
|
74
|
+
summary: "Show this project's docs packages and their index state",
|
|
75
|
+
group: "core",
|
|
76
|
+
usage: "docspack list",
|
|
77
|
+
options: [],
|
|
78
|
+
examples: ["docspack list", "docspack list --json"],
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: "verify",
|
|
82
|
+
summary: "Check the docs still describe the code you installed",
|
|
83
|
+
group: "core",
|
|
84
|
+
usage: "docspack verify [options]",
|
|
85
|
+
options: [["--package-dir <d>", "verify one package directory instead of what is installed"]],
|
|
86
|
+
detail: [
|
|
87
|
+
"Compares the identifiers the chunks name against what the documented libraries declare,",
|
|
88
|
+
"read from their .d.ts files. Nothing you depend on is imported or executed. The libraries",
|
|
89
|
+
'come from the manifest\'s "documents", or from the docspack key when the manifest has none.',
|
|
90
|
+
"Exits 1 when anything was reported.",
|
|
91
|
+
],
|
|
92
|
+
examples: ["docspack verify", "docspack verify --json"],
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: "feedback",
|
|
96
|
+
summary: "Record documentation problems: add, list, submit, remove",
|
|
97
|
+
group: "core",
|
|
98
|
+
usage: "docspack feedback <add|list|submit|remove> [options]",
|
|
99
|
+
options: [
|
|
100
|
+
["--chunk <id>", "add: the chunk the problem is in"],
|
|
101
|
+
["--kind <k>", "add: drift, incorrect or missing"],
|
|
102
|
+
["--evidence <text>", "add: the claim, in one line"],
|
|
103
|
+
["--expected <text>", "add: what the documentation led you to expect"],
|
|
104
|
+
["--actual <text>", "add: what happened instead"],
|
|
105
|
+
["--repro <code>", "add: code that demonstrates it"],
|
|
106
|
+
["-p, --package <s>", "submit: only findings from packages matching this text"],
|
|
107
|
+
["--all", "remove: every recorded finding"],
|
|
108
|
+
],
|
|
109
|
+
detail: [
|
|
110
|
+
"Writes to .docspack/feedback.jsonl in this project. Nothing is transmitted: docspack",
|
|
111
|
+
"contains no code that can send a report anywhere.",
|
|
112
|
+
"",
|
|
113
|
+
"`drift` must name the identifier that drifted. `incorrect` and `missing` must carry",
|
|
114
|
+
"--expected, --actual and --repro, so a claim that cannot show its work cannot be",
|
|
115
|
+
"recorded. `submit` prints a prefilled GitHub issue URL for a human to open and file.",
|
|
116
|
+
],
|
|
117
|
+
examples: [
|
|
118
|
+
'docspack feedback add --chunk @acme/docspack@1.4.0/api-auth --kind drift --evidence "client.setKey is not exported; setApiKey is"',
|
|
119
|
+
"docspack feedback list",
|
|
120
|
+
"docspack feedback submit",
|
|
121
|
+
"docspack feedback remove <fingerprint>",
|
|
122
|
+
],
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "mcp",
|
|
126
|
+
summary: "Serve the index over MCP instead, as a long-lived process",
|
|
127
|
+
group: "core",
|
|
128
|
+
usage: "docspack mcp",
|
|
129
|
+
options: [],
|
|
130
|
+
detail: [
|
|
131
|
+
"Serves the same index over the Model Context Protocol, for clients that prefer a tool",
|
|
132
|
+
"definition to a shell command. `query_local_docs` returns exactly what `ask` prints.",
|
|
133
|
+
"stdout is the protocol channel, so nothing else is written to it.",
|
|
134
|
+
],
|
|
135
|
+
examples: ["docspack mcp"],
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
name: "sources",
|
|
139
|
+
summary: "List curated sources that `docspack build` can fetch",
|
|
140
|
+
group: "core",
|
|
141
|
+
usage: "docspack sources",
|
|
142
|
+
options: [],
|
|
143
|
+
examples: ["docspack sources", "docspack build hono --name @docspack-community/hono"],
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: "init",
|
|
147
|
+
summary: "Scaffold a documentation package, then build and check it",
|
|
148
|
+
group: "authoring",
|
|
149
|
+
usage: "docspack init [name] [options]",
|
|
150
|
+
options: [
|
|
151
|
+
["-y, --yes", "skip the prompts and use flags plus detected defaults"],
|
|
152
|
+
["--dry-run", "print the file tree and write nothing"],
|
|
153
|
+
["--mirror <id|url>", "bootstrap from a published llms.txt"],
|
|
154
|
+
["--community", "scaffold under @docspack-community"],
|
|
155
|
+
["--template <t>", "full (default) or minimal"],
|
|
156
|
+
["--mode <m>", "standalone or in-repo"],
|
|
157
|
+
["--no-workflow", "skip the release workflow"],
|
|
158
|
+
["--no-build", "scaffold only"],
|
|
159
|
+
["--name <name>", "package name, e.g. @acme/docspack"],
|
|
160
|
+
["--pkg-version <v>", "package version"],
|
|
161
|
+
["--from <dir>", "directory of Markdown to package"],
|
|
162
|
+
["--openapi <file>", "OpenAPI JSON to package"],
|
|
163
|
+
["--out <dir>", "where to scaffold (default: ./docspack)"],
|
|
164
|
+
["--force", "overwrite files that already exist"],
|
|
165
|
+
],
|
|
166
|
+
detail: [
|
|
167
|
+
"Reads the surrounding project first — name, version, a docs directory, an OpenAPI",
|
|
168
|
+
"document, the git remote, the license — and proposes a package built from what it found.",
|
|
169
|
+
],
|
|
170
|
+
examples: [
|
|
171
|
+
"docspack init",
|
|
172
|
+
"docspack init --name @acme/docspack --from ./docs --yes",
|
|
173
|
+
"docspack init --mirror hono --community --yes",
|
|
174
|
+
],
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
name: "build",
|
|
178
|
+
summary: "Generate the .llms/ payload for publishing",
|
|
179
|
+
group: "authoring",
|
|
180
|
+
usage: "docspack build [source] [options]",
|
|
181
|
+
options: [
|
|
182
|
+
["--from <dir>", "directory of Markdown to package"],
|
|
183
|
+
["--openapi <file>", "OpenAPI JSON to package, one chunk per operation"],
|
|
184
|
+
["--name <name>", "package name, e.g. @acme/docspack"],
|
|
185
|
+
["--pkg-version <v>", "package version"],
|
|
186
|
+
["--out <dir>", "output directory (default: the current directory)"],
|
|
187
|
+
["--pages <n>", "maximum documents to fetch from a remote source"],
|
|
188
|
+
["--max-chunk-tokens <n>", "split a section above this size (default 800)"],
|
|
189
|
+
["--min-chunk-tokens <n>", "pack adjacent sections up to this size before splitting"],
|
|
190
|
+
["--documents <p>", "library this package documents, repeatable: acme@1.4.0"],
|
|
191
|
+
],
|
|
192
|
+
detail: [
|
|
193
|
+
"Splits each document at ## headings, then ###, then paragraphs, until every chunk fits",
|
|
194
|
+
"the budget. --min-chunk-tokens packs the other way first, merging adjacent sections up to",
|
|
195
|
+
"the budget: use it for generated reference, where a heading is a field name and one chunk",
|
|
196
|
+
"per heading is hundreds of chunks too small to answer anything.",
|
|
197
|
+
"",
|
|
198
|
+
"With no flags, settings are read from the docspack key of the package.json in --out.",
|
|
199
|
+
],
|
|
200
|
+
examples: [
|
|
201
|
+
"docspack build",
|
|
202
|
+
"docspack build --from ./docs --name @acme/docspack --pkg-version 1.4.0",
|
|
203
|
+
"docspack build --from ./reference --min-chunk-tokens 400 --max-chunk-tokens 900",
|
|
204
|
+
"docspack build --openapi ./openapi.json",
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
name: "doctor",
|
|
209
|
+
summary: "Check a package the way the indexer and a reviewer would",
|
|
210
|
+
group: "authoring",
|
|
211
|
+
usage: "docspack doctor [options]",
|
|
212
|
+
options: [
|
|
213
|
+
["--strict", "treat structural warnings as failures"],
|
|
214
|
+
["--pedantic", "--strict, and fail on prose style as well"],
|
|
215
|
+
["--package-dir <d>", "the package to inspect (default: .)"],
|
|
216
|
+
],
|
|
217
|
+
detail: [
|
|
218
|
+
"Errors are packages that will not index. Warnings are packages that will index and",
|
|
219
|
+
"retrieve badly. Prose style is a note unless --pedantic, because writing by hand is not a",
|
|
220
|
+
"reason to block a first publish. Exits 1 when the package is not ready to publish.",
|
|
221
|
+
],
|
|
222
|
+
examples: ["docspack doctor", "docspack doctor --strict", "docspack doctor --json"],
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
name: "preview",
|
|
226
|
+
summary: "Answer a query from the local package, as an agent would",
|
|
227
|
+
group: "authoring",
|
|
228
|
+
usage: "docspack preview <query> [options]",
|
|
229
|
+
options: [
|
|
230
|
+
["--package-dir <d>", "the package to read (default: .)"],
|
|
231
|
+
["--limit <n>", `maximum chunks to return (default ${LIMIT})`],
|
|
232
|
+
["--max-tokens <n>", `token ceiling for the result set (default ${MAX_TOKENS})`],
|
|
233
|
+
],
|
|
234
|
+
detail: [
|
|
235
|
+
"Indexes the package in memory and answers through the same ranking and token budget an",
|
|
236
|
+
"agent gets. Nothing is published, installed, or written to the global store.",
|
|
237
|
+
],
|
|
238
|
+
examples: ['docspack preview "how do I authenticate"'],
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
name: "eval",
|
|
242
|
+
summary: "Measure retrieval against a set of questions",
|
|
243
|
+
group: "authoring",
|
|
244
|
+
usage: "docspack eval <queries.json> [options]",
|
|
245
|
+
options: [
|
|
246
|
+
["--package-dir <d>", "the package to read (default: .)"],
|
|
247
|
+
["--limit <n>", `chunks per answer, the window a hit must fall in (default ${LIMIT})`],
|
|
248
|
+
["--max-tokens <n>", `token ceiling for each answer (default ${MAX_TOKENS})`],
|
|
249
|
+
["--min-hit-rate <n>", "exit 1 below this percentage of questions answered"],
|
|
250
|
+
],
|
|
251
|
+
detail: [
|
|
252
|
+
"The evaluation set is a JSON array of questions and the chunk ids that would answer them:",
|
|
253
|
+
"",
|
|
254
|
+
' [{ "query": "how do I verify a webhook signature", "expect": "webhooks-signing" },',
|
|
255
|
+
' { "query": "rate limits", "expect": ["rate-limits", "errors"] }]',
|
|
256
|
+
"",
|
|
257
|
+
"Reports hit rate, top-1 rate and mean answer size — the numbers that settle the chunk",
|
|
258
|
+
"budget, which no structural check can see. With --min-hit-rate it gates a publish on",
|
|
259
|
+
"retrieval quality the way `doctor --strict` gates it on structure.",
|
|
260
|
+
],
|
|
261
|
+
examples: [
|
|
262
|
+
"docspack eval ./eval/queries.json",
|
|
263
|
+
"docspack eval ./eval/queries.json --min-hit-rate 90",
|
|
264
|
+
"docspack eval ./eval/queries.json --limit 1 --json",
|
|
265
|
+
],
|
|
266
|
+
},
|
|
267
|
+
];
|
|
268
|
+
|
|
269
|
+
const GLOBAL_OPTIONS: readonly (readonly [string, string])[] = [
|
|
270
|
+
["--store <path>", "Use a different index"],
|
|
271
|
+
["--cwd <dir>", "Run in a different directory"],
|
|
272
|
+
["--json", "Machine-readable output"],
|
|
273
|
+
["-q, --quiet", "Only print errors"],
|
|
274
|
+
["-h, --help", "Show this help, or a command's help"],
|
|
275
|
+
["-v, --version", "Show the version"],
|
|
276
|
+
];
|
|
277
|
+
|
|
278
|
+
export function findCommandHelp(name: string): CommandHelp | undefined {
|
|
279
|
+
return COMMANDS.find((command) => command.name === name);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const WIDTH = 21;
|
|
283
|
+
|
|
284
|
+
function row([flag, description]: readonly [string, string]): string {
|
|
285
|
+
return ` ${flag.padEnd(WIDTH)} ${description}`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** The page one command prints for `docspack <command> --help`. */
|
|
289
|
+
export function commandHelpText(command: CommandHelp): string {
|
|
290
|
+
const blocks: string[] = [
|
|
291
|
+
`docspack ${command.name} — ${command.summary}`,
|
|
292
|
+
"",
|
|
293
|
+
"Usage",
|
|
294
|
+
` ${command.usage}`,
|
|
295
|
+
];
|
|
296
|
+
|
|
297
|
+
if (command.detail !== undefined) {
|
|
298
|
+
blocks.push("", ...command.detail.map((line) => (line.length === 0 ? "" : ` ${line}`)));
|
|
299
|
+
}
|
|
300
|
+
if (command.options.length > 0) {
|
|
301
|
+
blocks.push("", "Options", ...command.options.map(row));
|
|
302
|
+
}
|
|
303
|
+
blocks.push(
|
|
304
|
+
"",
|
|
305
|
+
"Global options",
|
|
306
|
+
...GLOBAL_OPTIONS.map(row),
|
|
307
|
+
"",
|
|
308
|
+
"Examples",
|
|
309
|
+
...command.examples.map((example) => ` ${example}`),
|
|
310
|
+
"",
|
|
311
|
+
);
|
|
312
|
+
return blocks.join("\n");
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The global page. It lists the commands and the options that apply to all of them, and points
|
|
317
|
+
* at the per-command pages for the rest — one list of every flag of every command was what made
|
|
318
|
+
* the flags unfindable in the first place.
|
|
319
|
+
*/
|
|
320
|
+
export function globalHelpText(storePath: string, notIndexed: number, noMatch: number): string {
|
|
321
|
+
const group = (which: CommandHelp["group"]): string =>
|
|
322
|
+
COMMANDS.filter((command) => command.group === which)
|
|
323
|
+
.map((command) => row([command.name, command.summary]))
|
|
324
|
+
.join("\n");
|
|
325
|
+
|
|
326
|
+
return `docspack — local, version-locked documentation for AI agents
|
|
327
|
+
|
|
328
|
+
Usage
|
|
329
|
+
docspack <command> [options]
|
|
330
|
+
docspack <command> --help every flag that command takes
|
|
331
|
+
|
|
332
|
+
Commands
|
|
333
|
+
${group("core")}
|
|
334
|
+
|
|
335
|
+
Authoring
|
|
336
|
+
${group("authoring")}
|
|
337
|
+
|
|
338
|
+
How it works
|
|
339
|
+
Documentation ships as npm packages named @vendor/docspack or
|
|
340
|
+
@docspack-community/<name>. Add them to package.json, run \`docspack sync\`, and
|
|
341
|
+
every chunk is indexed into a shared SQLite database with FTS5. Agents query
|
|
342
|
+
that index locally: no network, no scraping, always the installed version.
|
|
343
|
+
|
|
344
|
+
Giving an agent access
|
|
345
|
+
Any agent with a shell can run \`docspack ask\`, so one line in AGENTS.md or
|
|
346
|
+
CLAUDE.md is the whole setup — no server, no per-agent configuration, and
|
|
347
|
+
nothing resident when nobody is asking:
|
|
348
|
+
|
|
349
|
+
${AGENTS_SNIPPET.map((line) => ` ${line}`).join("\n")}
|
|
350
|
+
|
|
351
|
+
Add the block below as well if you want the agent to record documentation
|
|
352
|
+
problems it runs into. It writes to a local file; nothing is sent:
|
|
353
|
+
|
|
354
|
+
${FEEDBACK_SNIPPET.map((line) => ` ${line}`).join("\n")}
|
|
355
|
+
|
|
356
|
+
\`docspack mcp\` serves the same index over the Model Context Protocol for
|
|
357
|
+
clients that prefer a tool definition. Both return identical text.
|
|
358
|
+
|
|
359
|
+
Global options
|
|
360
|
+
${GLOBAL_OPTIONS.map(row).join("\n")}
|
|
361
|
+
|
|
362
|
+
The default index is ${storePath}.
|
|
363
|
+
|
|
364
|
+
Exit codes
|
|
365
|
+
\`ask\` and \`search\` answer with an exit code a script can branch on:
|
|
366
|
+
|
|
367
|
+
0 chunks were returned
|
|
368
|
+
${notIndexed} nothing matched, and a docs package is installed but not indexed —
|
|
369
|
+
run \`docspack sync\`
|
|
370
|
+
${noMatch} nothing matched, and everything installed is already indexed
|
|
371
|
+
1 the command failed; 2 the command was used wrongly
|
|
372
|
+
|
|
373
|
+
Examples
|
|
374
|
+
npx docspack sync
|
|
375
|
+
npx docspack ask "how do I verify a webhook signature"
|
|
376
|
+
npx docspack init
|
|
377
|
+
npx docspack doctor --strict
|
|
378
|
+
npx docspack preview "how do I authenticate"
|
|
379
|
+
npx docspack eval ./eval/queries.json --min-hit-rate 90
|
|
380
|
+
npx docspack feedback submit
|
|
381
|
+
`;
|
|
382
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -32,6 +32,15 @@ export {
|
|
|
32
32
|
type Severity,
|
|
33
33
|
} from "./doctor.js";
|
|
34
34
|
export { DocspackError } from "./errors.js";
|
|
35
|
+
export {
|
|
36
|
+
type EvalCase,
|
|
37
|
+
type EvalCaseResult,
|
|
38
|
+
type EvalOptions,
|
|
39
|
+
type EvalReport,
|
|
40
|
+
evaluatePackage,
|
|
41
|
+
parseEvalSet,
|
|
42
|
+
readEvalSet,
|
|
43
|
+
} from "./eval.js";
|
|
35
44
|
export { type ExportSurface, readExportSurface } from "./exports.js";
|
|
36
45
|
export {
|
|
37
46
|
type AddOptions,
|
|
@@ -46,6 +55,13 @@ export {
|
|
|
46
55
|
readFeedback,
|
|
47
56
|
removeFindings,
|
|
48
57
|
} from "./feedback.js";
|
|
58
|
+
export {
|
|
59
|
+
COMMANDS,
|
|
60
|
+
type CommandHelp,
|
|
61
|
+
commandHelpText,
|
|
62
|
+
findCommandHelp,
|
|
63
|
+
globalHelpText,
|
|
64
|
+
} from "./help.js";
|
|
49
65
|
export { type FetchLike, HttpClient } from "./http.js";
|
|
50
66
|
export { type Detected, detectProject, type PackageManager } from "./init/detect.js";
|
|
51
67
|
export {
|
|
@@ -92,7 +108,9 @@ export {
|
|
|
92
108
|
CHUNKS_DIR,
|
|
93
109
|
type ChunkSpec,
|
|
94
110
|
chunkId,
|
|
111
|
+
type DocumentedLibrary,
|
|
95
112
|
estimateTokens,
|
|
113
|
+
formatDocumentedLibrary,
|
|
96
114
|
isCommunityPackage,
|
|
97
115
|
isDocsPackage,
|
|
98
116
|
isVendorPackage,
|
|
@@ -100,12 +118,14 @@ export {
|
|
|
100
118
|
MANIFEST_FILE,
|
|
101
119
|
type PackageManifest,
|
|
102
120
|
packageId,
|
|
121
|
+
parseDocumentedLibrary,
|
|
103
122
|
parseManifest,
|
|
104
123
|
resolveChunkFile,
|
|
105
124
|
SCHEMA_URL,
|
|
106
125
|
SPEC_URL,
|
|
107
126
|
serializeManifest,
|
|
108
127
|
} from "./spec.js";
|
|
128
|
+
export { STOPWORDS } from "./stopwords.js";
|
|
109
129
|
export {
|
|
110
130
|
type BlockedFinding,
|
|
111
131
|
type BlockedReason,
|
package/src/preview.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { DEFAULT_LIMIT, DEFAULT_MAX_TOKENS, type QueryHit } from "./search.js";
|
|
|
6
6
|
import {
|
|
7
7
|
chunkId,
|
|
8
8
|
estimateTokens,
|
|
9
|
+
formatDocumentedLibrary,
|
|
9
10
|
isCommunityPackage,
|
|
10
11
|
LLMS_DIR,
|
|
11
12
|
MANIFEST_FILE,
|
|
@@ -45,6 +46,7 @@ export async function previewPackage(options: PreviewOptions): Promise<PreviewRe
|
|
|
45
46
|
|
|
46
47
|
const manifest = parseManifest(JSON.parse(raw), `${LLMS_DIR}/${MANIFEST_FILE}`);
|
|
47
48
|
const id = packageId(manifest.name, manifest.version);
|
|
49
|
+
const documents = (manifest.documents ?? []).map(formatDocumentedLibrary);
|
|
48
50
|
|
|
49
51
|
const chunks: IndexedChunk[] = [];
|
|
50
52
|
for (const chunk of manifest.chunks) {
|
|
@@ -58,7 +60,7 @@ export async function previewPackage(options: PreviewOptions): Promise<PreviewRe
|
|
|
58
60
|
chunks.push({
|
|
59
61
|
chunkId: chunkId(id, chunk.id),
|
|
60
62
|
filePath: chunk.file,
|
|
61
|
-
tokens: chunk.tokens
|
|
63
|
+
tokens: chunk.tokens ?? estimateTokens(contents),
|
|
62
64
|
content: contents,
|
|
63
65
|
tags: [...chunk.tags, ...chunk.entities],
|
|
64
66
|
});
|
|
@@ -84,6 +86,7 @@ export async function previewPackage(options: PreviewOptions): Promise<PreviewRe
|
|
|
84
86
|
name: manifest.name,
|
|
85
87
|
version: manifest.version,
|
|
86
88
|
trusted: !isCommunityPackage(manifest.name),
|
|
89
|
+
...(documents.length === 0 ? {} : { documents }),
|
|
87
90
|
}),
|
|
88
91
|
);
|
|
89
92
|
|