@cubicecho/agent-docs 0.0.0-stage → 0.1.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +52 -2
  3. package/_agents/docs/01-install.md +53 -0
  4. package/_agents/docs/checks.md +15 -0
  5. package/_agents/docs/ci.md +30 -0
  6. package/_agents/docs/cli.md +39 -0
  7. package/_agents/docs/common-mistakes.md +49 -0
  8. package/_agents/docs/config.md +25 -0
  9. package/dist/bin.d.ts +7 -0
  10. package/dist/bin.js +11 -0
  11. package/dist/check/check-agent-docs.d.ts +22 -0
  12. package/dist/check/check-agent-docs.js +254 -0
  13. package/dist/cli/run.d.ts +24 -0
  14. package/dist/cli/run.js +131 -0
  15. package/dist/consts.d.ts +16 -0
  16. package/dist/consts.js +16 -0
  17. package/dist/core/config.d.ts +31 -0
  18. package/dist/core/config.js +69 -0
  19. package/dist/core/errors.d.ts +17 -0
  20. package/dist/core/errors.js +21 -0
  21. package/dist/core/files.d.ts +28 -0
  22. package/dist/core/files.js +67 -0
  23. package/dist/core/json.d.ts +26 -0
  24. package/dist/core/json.js +44 -0
  25. package/dist/core/package-manifest.d.ts +36 -0
  26. package/dist/core/package-manifest.js +45 -0
  27. package/dist/defaults.d.ts +26 -0
  28. package/dist/defaults.js +14 -0
  29. package/dist/exports/entry-points.d.ts +30 -0
  30. package/dist/exports/entry-points.js +119 -0
  31. package/dist/exports/export-index.d.ts +44 -0
  32. package/dist/exports/export-index.js +233 -0
  33. package/dist/exports/module-source.d.ts +74 -0
  34. package/dist/exports/module-source.js +161 -0
  35. package/dist/exports/scan.d.ts +29 -0
  36. package/dist/exports/scan.js +156 -0
  37. package/dist/exports/summary.d.ts +36 -0
  38. package/dist/exports/summary.js +84 -0
  39. package/dist/index.d.ts +16 -0
  40. package/dist/index.js +16 -0
  41. package/dist/llms/identity.d.ts +21 -0
  42. package/dist/llms/identity.js +67 -0
  43. package/dist/llms/plan.d.ts +53 -0
  44. package/dist/llms/plan.js +318 -0
  45. package/dist/llms/sync.d.ts +16 -0
  46. package/dist/llms/sync.js +36 -0
  47. package/dist/markdown/anchors.d.ts +35 -0
  48. package/dist/markdown/anchors.js +66 -0
  49. package/dist/markdown/page.d.ts +20 -0
  50. package/dist/markdown/page.js +57 -0
  51. package/llms.txt +42 -0
  52. package/package.json +65 -4
@@ -0,0 +1,156 @@
1
+ import { AgentDocsError } from '../core/errors.js';
2
+ const DOC_OPEN = '/**';
3
+ const COMMENT_CLOSE = '*/';
4
+ const QUOTES = new Set(['"', "'", '`']);
5
+ /** A `/` after one of these (or at the start) opens a regex literal rather than dividing. */
6
+ const BEFORE_REGEX = /[(,=:[!&|?{};+\-*%<>~^]$|(?:^|[^\w$])(?:return|typeof|case|of|in)$/;
7
+ const MARGIN = /^\s*\* ?/;
8
+ /** How many characters before a `/` are enough to see the keyword or operator in front of it. */
9
+ const REGEX_LOOKBEHIND = 8;
10
+ /**
11
+ * Finds the comments in a TypeScript file.
12
+ *
13
+ * @param file - The file's path, for messages.
14
+ * @param source - The file's text.
15
+ * @returns The source with comments blanked, and its doc comments.
16
+ * @throws {@link AgentDocsError} If a block comment is never closed.
17
+ *
18
+ * @remarks
19
+ * Strings, template literals and regex literals are stepped over, so a `//` in a URL or a `/*` in a pattern is
20
+ * not taken for a comment.
21
+ */
22
+ export function scanSource(file, source) {
23
+ const parts = [];
24
+ const docBlocks = [];
25
+ let copiedTo = 0;
26
+ let i = 0;
27
+ while (i < source.length) {
28
+ const char = source[i];
29
+ const next = source[i + 1];
30
+ if (QUOTES.has(char)) {
31
+ i = skipQuoted(source, i);
32
+ continue;
33
+ }
34
+ const isLineComment = char === '/' && next === '/';
35
+ const isBlockComment = char === '/' && next === '*';
36
+ if (isLineComment || isBlockComment) {
37
+ const end = isLineComment ? lineEnd(source, i) : blockEnd(file, source, i);
38
+ const isDoc = isBlockComment && source.startsWith(DOC_OPEN, i) && end - i > DOC_OPEN.length + 1;
39
+ if (isDoc) {
40
+ docBlocks.push({ start: i, end, body: docBody(source.slice(i + DOC_OPEN.length, end - COMMENT_CLOSE.length)) });
41
+ }
42
+ parts.push(source.slice(copiedTo, i), source.slice(i, end).replace(/[^\n]/g, ' '));
43
+ copiedTo = end;
44
+ i = end;
45
+ continue;
46
+ }
47
+ const opensRegex = char === '/' && BEFORE_REGEX.test(source.slice(Math.max(0, i - REGEX_LOOKBEHIND), i).trimEnd());
48
+ if (opensRegex) {
49
+ i = skipRegex(source, i);
50
+ continue;
51
+ }
52
+ i += 1;
53
+ }
54
+ parts.push(source.slice(copiedTo));
55
+ return { code: parts.join(''), docBlocks };
56
+ }
57
+ /**
58
+ * Offset of the end of the line a position is on.
59
+ *
60
+ * @param source - The text.
61
+ * @param from - A position on the line.
62
+ * @returns The offset of its newline, or the text's length on the last line.
63
+ */
64
+ function lineEnd(source, from) {
65
+ const newline = source.indexOf('\n', from);
66
+ return newline === -1 ? source.length : newline;
67
+ }
68
+ /**
69
+ * Offset just past the block comment that opens at a position.
70
+ *
71
+ * @param file - The file's path, for messages.
72
+ * @param source - The text.
73
+ * @param from - Offset of the opening `/*`.
74
+ * @returns Offset just past its `*\/`.
75
+ * @throws {@link AgentDocsError} If the comment is never closed.
76
+ */
77
+ function blockEnd(file, source, from) {
78
+ const close = source.indexOf(COMMENT_CLOSE, from + 2);
79
+ if (close === -1) {
80
+ const line = source.slice(0, from).split('\n').length;
81
+ throw new AgentDocsError([`${file}:${line} opens a comment that never closes: add the closing "*/".`]);
82
+ }
83
+ return close + COMMENT_CLOSE.length;
84
+ }
85
+ /**
86
+ * Offset just past the string or template literal that opens at a position.
87
+ *
88
+ * @param source - The text.
89
+ * @param from - Offset of the opening quote.
90
+ * @returns Offset just past the closing quote; a plain string also ends at its line's end.
91
+ */
92
+ function skipQuoted(source, from) {
93
+ const quote = source[from];
94
+ let i = from + 1;
95
+ while (i < source.length) {
96
+ const char = source[i];
97
+ if (char === '\\') {
98
+ i += 2;
99
+ continue;
100
+ }
101
+ if (char === quote) {
102
+ return i + 1;
103
+ }
104
+ const isUnterminatedLine = char === '\n' && quote !== '`';
105
+ if (isUnterminatedLine) {
106
+ return i;
107
+ }
108
+ i += 1;
109
+ }
110
+ return i;
111
+ }
112
+ /**
113
+ * Offset just past the regex literal that opens at a position.
114
+ *
115
+ * @param source - The text.
116
+ * @param from - Offset of the opening `/`.
117
+ * @returns Offset just past the closing `/`, or the line's end when there is none.
118
+ */
119
+ function skipRegex(source, from) {
120
+ let inClass = false;
121
+ for (let i = from + 1; i < source.length; i++) {
122
+ const char = source[i];
123
+ if (char === '\\') {
124
+ i += 1;
125
+ }
126
+ else if (char === '\n') {
127
+ return i;
128
+ }
129
+ else if (char === '[') {
130
+ inClass = true;
131
+ }
132
+ else if (char === ']') {
133
+ inClass = false;
134
+ }
135
+ else if (char === '/' && inClass === false) {
136
+ return i + 1;
137
+ }
138
+ }
139
+ return source.length;
140
+ }
141
+ /**
142
+ * The lines of a doc comment's inside, `*` margin stripped.
143
+ *
144
+ * @param inner - The text between `/**` and `*\/`.
145
+ * @returns The lines, with the empty first and last lines of the block form dropped.
146
+ */
147
+ function docBody(inner) {
148
+ const lines = inner.split('\n').map((line, index) => (index === 0 ? line : line.replace(MARGIN, '')).trimEnd());
149
+ while (lines.length > 0 && lines[0].trim() === '') {
150
+ lines.shift();
151
+ }
152
+ while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
153
+ lines.pop();
154
+ }
155
+ return lines;
156
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * The paragraphs of a doc comment ahead of its first tag, each on one line.
3
+ *
4
+ * @param body - The comment's lines, `*` margin stripped.
5
+ * @returns The paragraphs, blank-line separated in the comment.
6
+ *
7
+ * @remarks
8
+ * Tags, and the `@remarks` prose under them, are not the summary's; a block written without the blank line above
9
+ * its tags would otherwise run its summary on into them.
10
+ */
11
+ export declare function paragraphs(body: readonly string[]): string[];
12
+ /**
13
+ * Reduces TSDoc and markdown links to their text.
14
+ *
15
+ * @param text - One paragraph of a doc comment.
16
+ * @returns `{@link X | label}` as `label`, `{@link X}` as `X`, and `[text](url)` as `text`.
17
+ */
18
+ export declare function plainText(text: string): string;
19
+ /**
20
+ * The first sentence of a paragraph.
21
+ *
22
+ * @param text - One paragraph, on one line.
23
+ * @returns Its text up to the first sentence end, or all of it when it has none.
24
+ *
25
+ * @remarks
26
+ * A period inside a code span, or after an abbreviation such as `e.g.` or `i.e.` (also after a bracket, as in
27
+ * `(e.g. a host)`), does not end the sentence.
28
+ */
29
+ export declare function firstSentence(text: string): string;
30
+ /**
31
+ * The one-line summary a doc comment gives the index.
32
+ *
33
+ * @param body - The comment's lines, `*` margin stripped.
34
+ * @returns The first sentence of its first paragraph, links reduced to text; empty when it has no prose.
35
+ */
36
+ export declare function summarize(body: readonly string[]): string;
@@ -0,0 +1,84 @@
1
+ /** A period, `!` or `?` (and any closing quote or bracket) followed by whitespace or the end: a sentence end. */
2
+ const SENTENCE_END = /[.!?]['")\]]*(?=\s|$)/g;
3
+ /** Text ending in one of these abbreviations has not ended a sentence, whatever comes before it: `(e.g.`. */
4
+ const ABBREVIATION = /(?:^|[^\p{L}])(?:e\.g|i\.e|cf|vs|viz|approx|incl|resp)\.$/iu;
5
+ /** `etc.` ends a sentence only when a capital letter opens the next. */
6
+ const ETC = /(?:^|[^\p{L}])etc\.$/iu;
7
+ const CAPITAL_NEXT = /^\s+\p{Lu}/u;
8
+ const BACKTICK = /`/g;
9
+ const TSDOC_LINK = /\{@link(?:code|plain)?\s+([^}|]+?)(?:\s*\|\s*([^}]+?))?\s*\}/g;
10
+ const MARKDOWN_LINK = /\[([^\]]+)\]\([^)]*\)/g;
11
+ /**
12
+ * The paragraphs of a doc comment ahead of its first tag, each on one line.
13
+ *
14
+ * @param body - The comment's lines, `*` margin stripped.
15
+ * @returns The paragraphs, blank-line separated in the comment.
16
+ *
17
+ * @remarks
18
+ * Tags, and the `@remarks` prose under them, are not the summary's; a block written without the blank line above
19
+ * its tags would otherwise run its summary on into them.
20
+ */
21
+ export function paragraphs(body) {
22
+ const out = [];
23
+ let current = [];
24
+ for (const line of body) {
25
+ const trimmed = line.trim();
26
+ if (trimmed.startsWith('@')) {
27
+ break;
28
+ }
29
+ if (trimmed === '') {
30
+ out.push(current.join(' '));
31
+ current = [];
32
+ }
33
+ else {
34
+ current.push(trimmed);
35
+ }
36
+ }
37
+ out.push(current.join(' '));
38
+ return out.filter((entry) => entry !== '');
39
+ }
40
+ /**
41
+ * Reduces TSDoc and markdown links to their text.
42
+ *
43
+ * @param text - One paragraph of a doc comment.
44
+ * @returns `{@link X | label}` as `label`, `{@link X}` as `X`, and `[text](url)` as `text`.
45
+ */
46
+ export function plainText(text) {
47
+ return text
48
+ .replace(TSDOC_LINK, (_whole, target, label) => label ?? target)
49
+ .replace(MARKDOWN_LINK, '$1');
50
+ }
51
+ /**
52
+ * The first sentence of a paragraph.
53
+ *
54
+ * @param text - One paragraph, on one line.
55
+ * @returns Its text up to the first sentence end, or all of it when it has none.
56
+ *
57
+ * @remarks
58
+ * A period inside a code span, or after an abbreviation such as `e.g.` or `i.e.` (also after a bracket, as in
59
+ * `(e.g. a host)`), does not end the sentence.
60
+ */
61
+ export function firstSentence(text) {
62
+ for (const match of text.matchAll(SENTENCE_END)) {
63
+ const head = text.slice(0, match.index + match[0].length);
64
+ const tail = text.slice(head.length);
65
+ const backticks = head.match(BACKTICK)?.length ?? 0;
66
+ const isInCode = backticks % 2 === 1;
67
+ const isAbbreviation = ABBREVIATION.test(head) || (ETC.test(head) && CAPITAL_NEXT.test(tail) === false);
68
+ if (isInCode || isAbbreviation) {
69
+ continue;
70
+ }
71
+ return head;
72
+ }
73
+ return text;
74
+ }
75
+ /**
76
+ * The one-line summary a doc comment gives the index.
77
+ *
78
+ * @param body - The comment's lines, `*` margin stripped.
79
+ * @returns The first sentence of its first paragraph, links reduced to text; empty when it has no prose.
80
+ */
81
+ export function summarize(body) {
82
+ const [first = ''] = paragraphs(body);
83
+ return firstSentence(plainText(first));
84
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Generates and checks the agent docs a package ships.
3
+ *
4
+ * `planLlms` works out llms.txt from the `_agents/docs/` pages and, for TypeScript, the doc comments behind each
5
+ * package.json `exports` entry; `writeLlms` writes it and `findStaleFiles` compares it with the committed copy.
6
+ * `checkAgentDocs` checks the README, AGENTS.md, links, page sizes and package contents against the standard. The
7
+ * `agent-docs` command runs the same functions.
8
+ */
9
+ export { type CheckResult, checkAgentDocs } from './check/check-agent-docs.ts';
10
+ export { type CliOutput, ExitCode, runCli } from './cli/run.ts';
11
+ export { type LlmsConfig, loadLlmsConfig } from './core/config.ts';
12
+ export { AgentDocsError } from './core/errors.ts';
13
+ export { LAYOUT_DEFAULTS, type LayoutSettings, LIMIT_DEFAULTS, type LimitSettings } from './defaults.ts';
14
+ export { firstSentence, summarize } from './exports/summary.ts';
15
+ export { ExportPlacement, type GeneratedFile, type LlmsOptions, type LlmsPlan, planLlms } from './llms/plan.ts';
16
+ export { findStaleFiles, writeLlms } from './llms/sync.ts';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Generates and checks the agent docs a package ships.
3
+ *
4
+ * `planLlms` works out llms.txt from the `_agents/docs/` pages and, for TypeScript, the doc comments behind each
5
+ * package.json `exports` entry; `writeLlms` writes it and `findStaleFiles` compares it with the committed copy.
6
+ * `checkAgentDocs` checks the README, AGENTS.md, links, page sizes and package contents against the standard. The
7
+ * `agent-docs` command runs the same functions.
8
+ */
9
+ export { checkAgentDocs } from './check/check-agent-docs.js';
10
+ export { ExitCode, runCli } from './cli/run.js';
11
+ export { loadLlmsConfig } from './core/config.js';
12
+ export { AgentDocsError } from './core/errors.js';
13
+ export { LAYOUT_DEFAULTS, LIMIT_DEFAULTS } from './defaults.js';
14
+ export { firstSentence, summarize } from './exports/summary.js';
15
+ export { ExportPlacement, planLlms } from './llms/plan.js';
16
+ export { findStaleFiles, writeLlms } from './llms/sync.js';
@@ -0,0 +1,21 @@
1
+ import type { LlmsConfig } from '../core/config.ts';
2
+ /** The llms.txt title and summary. */
3
+ export interface PackageIdentity {
4
+ /** The `# H1`. */
5
+ title: string;
6
+ /** The `>` line, on one line. */
7
+ summary: string;
8
+ }
9
+ /**
10
+ * The package's title and one-line summary, from the first manifest that has them.
11
+ *
12
+ * @param root - Package root.
13
+ * @param config - `_agents/llms.json`, whose `title` and `summary` win.
14
+ * @returns The title and summary.
15
+ * @throws {@link AgentDocsError} If no manifest or config gives one of them.
16
+ *
17
+ * @remarks
18
+ * Manifests are read in this order: package.json, haxelib.json, pyproject.toml (`[project]`), and Home
19
+ * Assistant's manifest.json.
20
+ */
21
+ export declare function packageIdentity(root: string, config: LlmsConfig): PackageIdentity;
@@ -0,0 +1,67 @@
1
+ import { existsSync, readFileSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ import { AgentDocsError } from '../core/errors.js';
4
+ import { isJsonObject, readJsonFile, stringField } from '../core/json.js';
5
+ /**
6
+ * The package's title and one-line summary, from the first manifest that has them.
7
+ *
8
+ * @param root - Package root.
9
+ * @param config - `_agents/llms.json`, whose `title` and `summary` win.
10
+ * @returns The title and summary.
11
+ * @throws {@link AgentDocsError} If no manifest or config gives one of them.
12
+ *
13
+ * @remarks
14
+ * Manifests are read in this order: package.json, haxelib.json, pyproject.toml (`[project]`), and Home
15
+ * Assistant's manifest.json.
16
+ */
17
+ export function packageIdentity(root, config) {
18
+ const manifests = [
19
+ jsonManifest(root, 'package.json'),
20
+ jsonManifest(root, 'haxelib.json'),
21
+ pyprojectManifest(root),
22
+ jsonManifest(root, 'manifest.json'),
23
+ ].filter((manifest) => manifest !== undefined);
24
+ const title = config.title ?? manifests.find((m) => m.name)?.name;
25
+ const summary = config.summary ?? manifests.find((m) => m.description)?.description;
26
+ const problems = [];
27
+ if (!title) {
28
+ problems.push('no title: add "name" to a manifest, or "title" to _agents/llms.json.');
29
+ }
30
+ if (!summary) {
31
+ problems.push('no summary: add "description" to a manifest, or "summary" to _agents/llms.json.');
32
+ }
33
+ if (!title || !summary) {
34
+ throw new AgentDocsError(problems);
35
+ }
36
+ return { title, summary: summary.replace(/\s+/g, ' ').trim() };
37
+ }
38
+ /**
39
+ * The name and description of a JSON manifest.
40
+ *
41
+ * @param root - Package root.
42
+ * @param path - Manifest file name.
43
+ * @returns Its fields, or undefined when the file is absent or not an object.
44
+ */
45
+ function jsonManifest(root, path) {
46
+ const parsed = readJsonFile(root, path);
47
+ if (isJsonObject(parsed) === false) {
48
+ return undefined;
49
+ }
50
+ return { name: stringField(parsed, 'name'), description: stringField(parsed, 'description') };
51
+ }
52
+ /**
53
+ * The `name` and `description` of pyproject.toml's `[project]` table.
54
+ *
55
+ * @param root - Package root.
56
+ * @returns Its fields, or undefined when there is no pyproject.toml.
57
+ */
58
+ function pyprojectManifest(root) {
59
+ const full = join(root, 'pyproject.toml');
60
+ if (existsSync(full) === false) {
61
+ return undefined;
62
+ }
63
+ const text = readFileSync(full, 'utf8');
64
+ const table = text.split(/^\[project\]\s*$/m)[1]?.split(/^\[/m)[0] ?? '';
65
+ const field = (key) => table.match(new RegExp(`^${key}\\s*=\\s*"([^"]*)"`, 'm'))?.[1];
66
+ return { name: field('name'), description: field('description') };
67
+ }
@@ -0,0 +1,53 @@
1
+ import { type LimitSettings } from '../defaults.ts';
2
+ /** What {@link planLlms} reads. */
3
+ export interface LlmsOptions {
4
+ /** Package root: where package.json and llms.txt are. */
5
+ root: string;
6
+ /** Adds a `Base URL:` line, for a copy served from a site. */
7
+ baseUrl?: string;
8
+ /** Limits to change, for a test or an unusual repo. */
9
+ limits?: Partial<LimitSettings>;
10
+ }
11
+ /** One file the plan writes. */
12
+ export interface GeneratedFile {
13
+ /** Path relative to the package root, with `/` separators. */
14
+ path: string;
15
+ /** The whole file. */
16
+ content: string;
17
+ }
18
+ /** Where the export index went. */
19
+ export declare const ExportPlacement: {
20
+ /** The package has no TypeScript source, or `"exports": false` turned the index off. */
21
+ readonly None: 'none';
22
+ /** One `##` section per entry point, inside llms.txt. */
23
+ readonly Inline: 'inline';
24
+ /** One generated page per entry point under the docs folder, which llms.txt links like any other page. */
25
+ readonly Pages: 'pages';
26
+ };
27
+ export type ExportPlacement = (typeof ExportPlacement)[keyof typeof ExportPlacement];
28
+ /** Everything one run of `llms` writes, before anything is written. */
29
+ export interface LlmsPlan {
30
+ /** llms.txt first, then any generated export pages. */
31
+ files: GeneratedFile[];
32
+ /** Export pages a previous run generated that this one no longer does. */
33
+ obsolete: string[];
34
+ /** Where the export index went. */
35
+ placement: ExportPlacement;
36
+ /** Exports that have no doc comment, as `name (import path)`. */
37
+ undocumented: string[];
38
+ }
39
+ /**
40
+ * Works out llms.txt, and the export pages when there are any, without writing anything.
41
+ *
42
+ * @param options - The package root, and an optional base URL and limits.
43
+ * @returns The files to write and the ones to delete.
44
+ * @throws {@link AgentDocsError} With one line per problem: a page with no title or summary, a missing manifest
45
+ * field, an export that can't be traced, or a result over the size limit.
46
+ *
47
+ * @remarks
48
+ * Each page in the docs folder becomes one link, under `## Docs` or a section named after its subfolder. When the
49
+ * package has TypeScript in `src/`, each package.json `exports` entry adds a `## Exports: <import path>` section.
50
+ * If that would take llms.txt over `llmsLimitKb`, the sections become generated pages in `<docsDir>/exports/`
51
+ * instead, and llms.txt links them.
52
+ */
53
+ export declare function planLlms(options: LlmsOptions): LlmsPlan;