@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.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/_agents/docs/01-install.md +53 -0
- package/_agents/docs/checks.md +15 -0
- package/_agents/docs/ci.md +30 -0
- package/_agents/docs/cli.md +39 -0
- package/_agents/docs/common-mistakes.md +49 -0
- package/_agents/docs/config.md +25 -0
- package/dist/bin.d.ts +7 -0
- package/dist/bin.js +11 -0
- package/dist/check/check-agent-docs.d.ts +22 -0
- package/dist/check/check-agent-docs.js +254 -0
- package/dist/cli/run.d.ts +24 -0
- package/dist/cli/run.js +131 -0
- package/dist/consts.d.ts +16 -0
- package/dist/consts.js +16 -0
- package/dist/core/config.d.ts +31 -0
- package/dist/core/config.js +69 -0
- package/dist/core/errors.d.ts +17 -0
- package/dist/core/errors.js +21 -0
- package/dist/core/files.d.ts +28 -0
- package/dist/core/files.js +67 -0
- package/dist/core/json.d.ts +26 -0
- package/dist/core/json.js +44 -0
- package/dist/core/package-manifest.d.ts +36 -0
- package/dist/core/package-manifest.js +45 -0
- package/dist/defaults.d.ts +26 -0
- package/dist/defaults.js +14 -0
- package/dist/exports/entry-points.d.ts +30 -0
- package/dist/exports/entry-points.js +119 -0
- package/dist/exports/export-index.d.ts +44 -0
- package/dist/exports/export-index.js +233 -0
- package/dist/exports/module-source.d.ts +74 -0
- package/dist/exports/module-source.js +161 -0
- package/dist/exports/scan.d.ts +29 -0
- package/dist/exports/scan.js +156 -0
- package/dist/exports/summary.d.ts +36 -0
- package/dist/exports/summary.js +84 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/llms/identity.d.ts +21 -0
- package/dist/llms/identity.js +67 -0
- package/dist/llms/plan.d.ts +53 -0
- package/dist/llms/plan.js +318 -0
- package/dist/llms/sync.d.ts +16 -0
- package/dist/llms/sync.js +36 -0
- package/dist/markdown/anchors.d.ts +35 -0
- package/dist/markdown/anchors.js +66 -0
- package/dist/markdown/page.d.ts +20 -0
- package/dist/markdown/page.js +57 -0
- package/llms.txt +42 -0
- package/package.json +65 -4
package/dist/cli/run.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { checkAgentDocs } from '../check/check-agent-docs.js';
|
|
2
|
+
import { LLMS_FILE } from '../consts.js';
|
|
3
|
+
import { AgentDocsError } from '../core/errors.js';
|
|
4
|
+
import { LIMIT_DEFAULTS } from '../defaults.js';
|
|
5
|
+
import { ExportPlacement, planLlms } from '../llms/plan.js';
|
|
6
|
+
import { findStaleFiles, writeLlms } from '../llms/sync.js';
|
|
7
|
+
/** The process exit codes the CLI returns. */
|
|
8
|
+
export const ExitCode = {
|
|
9
|
+
/** Everything passed or was written. */
|
|
10
|
+
Ok: 0,
|
|
11
|
+
/** At least one problem, or a usage error. */
|
|
12
|
+
Failed: 1,
|
|
13
|
+
};
|
|
14
|
+
const USAGE = [
|
|
15
|
+
'Usage: agent-docs <command> [options]',
|
|
16
|
+
'',
|
|
17
|
+
'Commands:',
|
|
18
|
+
' llms Write llms.txt (and any generated export pages) in the current folder.',
|
|
19
|
+
' llms --check Exit 1 if llms.txt or an export page is missing or stale. For CI.',
|
|
20
|
+
' llms --stdout Print what llms would write, and write nothing.',
|
|
21
|
+
' llms --base-url <url> Add a "Base URL:" line, for a copy served from a site.',
|
|
22
|
+
' check Check the package against the agent docs standard. Changes nothing.',
|
|
23
|
+
' help Print this message.',
|
|
24
|
+
].join('\n');
|
|
25
|
+
/**
|
|
26
|
+
* Runs one CLI command against a package.
|
|
27
|
+
*
|
|
28
|
+
* @param argv - The arguments after the program name.
|
|
29
|
+
* @param cwd - The package root.
|
|
30
|
+
* @param output - Where to print.
|
|
31
|
+
* @returns The exit code: 1 with one line per problem, 0 otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export function runCli(argv, cwd, output) {
|
|
34
|
+
const [command, ...rest] = argv;
|
|
35
|
+
try {
|
|
36
|
+
if (command === 'llms') {
|
|
37
|
+
return runLlms(rest, cwd, output);
|
|
38
|
+
}
|
|
39
|
+
if (command === 'check') {
|
|
40
|
+
return runCheck(rest, cwd, output);
|
|
41
|
+
}
|
|
42
|
+
const isHelp = command === 'help' || command === '--help' || command === '-h';
|
|
43
|
+
if (isHelp) {
|
|
44
|
+
output.out(USAGE);
|
|
45
|
+
return ExitCode.Ok;
|
|
46
|
+
}
|
|
47
|
+
output.err(command === undefined ? 'agent-docs: name a command.' : `agent-docs: unknown command "${command}".`);
|
|
48
|
+
output.err(USAGE);
|
|
49
|
+
return ExitCode.Failed;
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
if (error instanceof AgentDocsError) {
|
|
53
|
+
const prefix = command === 'llms' ? 'agent-docs llms' : 'agent-docs';
|
|
54
|
+
for (const problem of error.problems) {
|
|
55
|
+
output.err(`${prefix}: ${problem}`);
|
|
56
|
+
}
|
|
57
|
+
return ExitCode.Failed;
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* `agent-docs llms`: writes, checks or prints llms.txt.
|
|
64
|
+
*
|
|
65
|
+
* @param args - The options after `llms`.
|
|
66
|
+
* @param cwd - The package root.
|
|
67
|
+
* @param output - Where to print.
|
|
68
|
+
* @returns The exit code.
|
|
69
|
+
* @throws {@link AgentDocsError} If an option is wrong or the docs can't be indexed.
|
|
70
|
+
*/
|
|
71
|
+
function runLlms(args, cwd, output) {
|
|
72
|
+
const known = new Set(['--check', '--stdout', '--base-url']);
|
|
73
|
+
const baseUrlIndex = args.indexOf('--base-url');
|
|
74
|
+
const baseUrl = baseUrlIndex === -1 ? undefined : args[baseUrlIndex + 1];
|
|
75
|
+
const isBaseUrlValue = (index) => baseUrlIndex !== -1 && index === baseUrlIndex + 1;
|
|
76
|
+
const unknown = args.filter((arg, index) => known.has(arg) === false && isBaseUrlValue(index) === false);
|
|
77
|
+
const problems = unknown.map((arg) => `unknown option "${arg}": see "agent-docs help".`);
|
|
78
|
+
const lacksBaseUrl = baseUrlIndex !== -1 && (baseUrl === undefined || baseUrl.startsWith('--'));
|
|
79
|
+
if (lacksBaseUrl) {
|
|
80
|
+
problems.push('--base-url needs a URL after it.');
|
|
81
|
+
}
|
|
82
|
+
if (problems.length > 0) {
|
|
83
|
+
throw new AgentDocsError(problems);
|
|
84
|
+
}
|
|
85
|
+
const plan = planLlms({ root: cwd, baseUrl });
|
|
86
|
+
if (args.includes('--stdout')) {
|
|
87
|
+
for (const file of plan.files) {
|
|
88
|
+
output.out(plan.files.length > 1 ? `==> ${file.path} <==\n${file.content}` : file.content);
|
|
89
|
+
}
|
|
90
|
+
return ExitCode.Ok;
|
|
91
|
+
}
|
|
92
|
+
const pageCount = plan.files.length - 1;
|
|
93
|
+
const pagesNote = plan.placement === ExportPlacement.Pages ? ` and ${pageCount} export pages` : '';
|
|
94
|
+
if (args.includes('--check')) {
|
|
95
|
+
const stale = findStaleFiles(cwd, plan);
|
|
96
|
+
if (stale.length > 0) {
|
|
97
|
+
throw new AgentDocsError(stale);
|
|
98
|
+
}
|
|
99
|
+
output.out(`agent-docs llms: ${LLMS_FILE}${pagesNote} up to date.`);
|
|
100
|
+
return ExitCode.Ok;
|
|
101
|
+
}
|
|
102
|
+
writeLlms(cwd, plan);
|
|
103
|
+
output.out(`agent-docs llms: wrote ${LLMS_FILE}${pagesNote}.`);
|
|
104
|
+
if (plan.placement === ExportPlacement.Pages) {
|
|
105
|
+
output.out(`agent-docs llms: the export index is over ${LIMIT_DEFAULTS.llmsLimitKb} KB, so it went to its own pages.`);
|
|
106
|
+
}
|
|
107
|
+
if (plan.undocumented.length > 0) {
|
|
108
|
+
output.out(`agent-docs llms: no doc comment on ${plan.undocumented.join(', ')}.`);
|
|
109
|
+
}
|
|
110
|
+
return ExitCode.Ok;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `agent-docs check`: checks the package against the standard.
|
|
114
|
+
*
|
|
115
|
+
* @param args - The options after `check`; there are none.
|
|
116
|
+
* @param cwd - The package root.
|
|
117
|
+
* @param output - Where to print.
|
|
118
|
+
* @returns The exit code.
|
|
119
|
+
* @throws {@link AgentDocsError} If an option is given, or with one line per problem.
|
|
120
|
+
*/
|
|
121
|
+
function runCheck(args, cwd, output) {
|
|
122
|
+
if (args.length > 0) {
|
|
123
|
+
throw new AgentDocsError([`check takes no options, got "${args.join(' ')}".`]);
|
|
124
|
+
}
|
|
125
|
+
const { problems, pageCount } = checkAgentDocs(cwd);
|
|
126
|
+
if (problems.length > 0) {
|
|
127
|
+
throw new AgentDocsError(problems);
|
|
128
|
+
}
|
|
129
|
+
output.out(`agent-docs: ${pageCount} pages, each within ${LIMIT_DEFAULTS.pageLimitKb} KB; every link resolves.`);
|
|
130
|
+
return ExitCode.Ok;
|
|
131
|
+
}
|
package/dist/consts.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Bytes in one kilobyte, as the size limits count them. */
|
|
2
|
+
export declare const BYTES_PER_KB = 1024;
|
|
3
|
+
/** This package's name, as messages tell a reader what to run. */
|
|
4
|
+
export declare const PACKAGE_NAME = "@cubicecho/agent-docs";
|
|
5
|
+
/** The command that regenerates llms.txt, quoted in every "stale" message. */
|
|
6
|
+
export declare const LLMS_COMMAND = "npx @cubicecho/agent-docs llms";
|
|
7
|
+
/** The generated index, relative to the package root. */
|
|
8
|
+
export declare const LLMS_FILE = "llms.txt";
|
|
9
|
+
/** The optional overrides file both commands read, relative to the package root. */
|
|
10
|
+
export declare const CONFIG_FILE = "_agents/llms.json";
|
|
11
|
+
/** Text the README's hidden pointer comment must contain. */
|
|
12
|
+
export declare const POINTER_TEXT = "AI agents:";
|
|
13
|
+
/** The whole pointer line, as a failing check suggests it. */
|
|
14
|
+
export declare const POINTER_LINE = "<!-- AI agents: read llms.txt and _agents/docs/ for the full, version-matched API. -->";
|
|
15
|
+
/** What every page this package generates starts with, so a rerun can tell its own pages from hand-written ones. */
|
|
16
|
+
export declare const GENERATED_MARKER = "<!-- Generated by @cubicecho/agent-docs";
|
package/dist/consts.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Bytes in one kilobyte, as the size limits count them. */
|
|
2
|
+
export const BYTES_PER_KB = 1024;
|
|
3
|
+
/** This package's name, as messages tell a reader what to run. */
|
|
4
|
+
export const PACKAGE_NAME = '@cubicecho/agent-docs';
|
|
5
|
+
/** The command that regenerates llms.txt, quoted in every "stale" message. */
|
|
6
|
+
export const LLMS_COMMAND = 'npx @cubicecho/agent-docs llms';
|
|
7
|
+
/** The generated index, relative to the package root. */
|
|
8
|
+
export const LLMS_FILE = 'llms.txt';
|
|
9
|
+
/** The optional overrides file both commands read, relative to the package root. */
|
|
10
|
+
export const CONFIG_FILE = '_agents/llms.json';
|
|
11
|
+
/** Text the README's hidden pointer comment must contain. */
|
|
12
|
+
export const POINTER_TEXT = 'AI agents:';
|
|
13
|
+
/** The whole pointer line, as a failing check suggests it. */
|
|
14
|
+
export const POINTER_LINE = '<!-- AI agents: read llms.txt and _agents/docs/ for the full, version-matched API. -->';
|
|
15
|
+
/** What every page this package generates starts with, so a rerun can tell its own pages from hand-written ones. */
|
|
16
|
+
export const GENERATED_MARKER = '<!-- Generated by @cubicecho/agent-docs';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** `_agents/llms.json`, the overrides both commands share, with its defaults filled in. */
|
|
2
|
+
export interface LlmsConfig {
|
|
3
|
+
/** Replaces the manifest's name as the llms.txt title. */
|
|
4
|
+
title?: string;
|
|
5
|
+
/** Replaces the manifest's description as the llms.txt summary. */
|
|
6
|
+
summary?: string;
|
|
7
|
+
/** Heading text per docs subfolder name, replacing the one made from the folder name. */
|
|
8
|
+
sections: Record<string, string>;
|
|
9
|
+
/** Markdown appended after the generated sections. */
|
|
10
|
+
extra?: string;
|
|
11
|
+
/**
|
|
12
|
+
* The pages' folder, relative to the package root, with `/` separators.
|
|
13
|
+
*
|
|
14
|
+
* @defaultValue `LAYOUT_DEFAULTS.docsDir`
|
|
15
|
+
*/
|
|
16
|
+
docsDir: string;
|
|
17
|
+
/**
|
|
18
|
+
* Whether llms.txt indexes the package's exports.
|
|
19
|
+
*
|
|
20
|
+
* @defaultValue `true`
|
|
21
|
+
*/
|
|
22
|
+
exports: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Reads `_agents/llms.json`, or the defaults when it is absent.
|
|
26
|
+
*
|
|
27
|
+
* @param root - Package root.
|
|
28
|
+
* @returns The config, every field checked.
|
|
29
|
+
* @throws {@link AgentDocsError} If the file is not JSON or a field has the wrong type.
|
|
30
|
+
*/
|
|
31
|
+
export declare function loadLlmsConfig(root: string): LlmsConfig;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CONFIG_FILE } from '../consts.js';
|
|
2
|
+
import { LAYOUT_DEFAULTS } from '../defaults.js';
|
|
3
|
+
import { AgentDocsError } from './errors.js';
|
|
4
|
+
import { isJsonObject, readJsonFile } from './json.js';
|
|
5
|
+
/**
|
|
6
|
+
* Reads `_agents/llms.json`, or the defaults when it is absent.
|
|
7
|
+
*
|
|
8
|
+
* @param root - Package root.
|
|
9
|
+
* @returns The config, every field checked.
|
|
10
|
+
* @throws {@link AgentDocsError} If the file is not JSON or a field has the wrong type.
|
|
11
|
+
*/
|
|
12
|
+
export function loadLlmsConfig(root) {
|
|
13
|
+
const parsed = readJsonFile(root, CONFIG_FILE) ?? {};
|
|
14
|
+
if (isJsonObject(parsed) === false) {
|
|
15
|
+
throw new AgentDocsError([`${CONFIG_FILE} must hold a JSON object.`]);
|
|
16
|
+
}
|
|
17
|
+
const problems = [];
|
|
18
|
+
const text = (key) => {
|
|
19
|
+
const value = parsed[key];
|
|
20
|
+
if (value === undefined || typeof value === 'string') {
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
problems.push(`${CONFIG_FILE} "${key}" must be a string.`);
|
|
24
|
+
return undefined;
|
|
25
|
+
};
|
|
26
|
+
const config = {
|
|
27
|
+
title: text('title'),
|
|
28
|
+
summary: text('summary'),
|
|
29
|
+
extra: text('extra'),
|
|
30
|
+
docsDir: (text('docsDir') ?? LAYOUT_DEFAULTS.docsDir).replace(/^\.\//, '').replace(/\/$/, ''),
|
|
31
|
+
sections: sectionNames(parsed, problems),
|
|
32
|
+
exports: parsed.exports !== false,
|
|
33
|
+
};
|
|
34
|
+
const hasBadExports = parsed.exports !== undefined && typeof parsed.exports !== 'boolean';
|
|
35
|
+
if (hasBadExports) {
|
|
36
|
+
problems.push(`${CONFIG_FILE} "exports" must be true or false.`);
|
|
37
|
+
}
|
|
38
|
+
if (problems.length > 0) {
|
|
39
|
+
throw new AgentDocsError(problems);
|
|
40
|
+
}
|
|
41
|
+
return config;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The `sections` map of the config, keeping only string headings.
|
|
45
|
+
*
|
|
46
|
+
* @param parsed - Parsed llms.json.
|
|
47
|
+
* @param problems - Receives a line for each entry that is not a string; appended to in place.
|
|
48
|
+
* @returns Heading text per folder name.
|
|
49
|
+
*/
|
|
50
|
+
function sectionNames(parsed, problems) {
|
|
51
|
+
const { sections } = parsed;
|
|
52
|
+
if (sections === undefined) {
|
|
53
|
+
return {};
|
|
54
|
+
}
|
|
55
|
+
if (isJsonObject(sections) === false) {
|
|
56
|
+
problems.push(`${CONFIG_FILE} "sections" must map folder names to heading text.`);
|
|
57
|
+
return {};
|
|
58
|
+
}
|
|
59
|
+
const named = {};
|
|
60
|
+
for (const [folder, heading] of Object.entries(sections)) {
|
|
61
|
+
if (typeof heading === 'string') {
|
|
62
|
+
named[folder] = heading;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
problems.push(`${CONFIG_FILE} "sections.${folder}" must be a string.`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return named;
|
|
69
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The docs or the package can't be read or generated as they stand.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Each entry in `problems` is one line that names the file and says what to change, so the CLI prints them as
|
|
6
|
+
* they are.
|
|
7
|
+
*/
|
|
8
|
+
export declare class AgentDocsError extends Error {
|
|
9
|
+
/** One line per problem, each naming the file and the fix. */
|
|
10
|
+
readonly problems: readonly string[];
|
|
11
|
+
/**
|
|
12
|
+
* Collects the problems into one error.
|
|
13
|
+
*
|
|
14
|
+
* @param problems - One line per problem; at least one.
|
|
15
|
+
*/
|
|
16
|
+
constructor(problems: readonly string[]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The docs or the package can't be read or generated as they stand.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* Each entry in `problems` is one line that names the file and says what to change, so the CLI prints them as
|
|
6
|
+
* they are.
|
|
7
|
+
*/
|
|
8
|
+
export class AgentDocsError extends Error {
|
|
9
|
+
/** One line per problem, each naming the file and the fix. */
|
|
10
|
+
problems;
|
|
11
|
+
/**
|
|
12
|
+
* Collects the problems into one error.
|
|
13
|
+
*
|
|
14
|
+
* @param problems - One line per problem; at least one.
|
|
15
|
+
*/
|
|
16
|
+
constructor(problems) {
|
|
17
|
+
super(problems.join('\n'));
|
|
18
|
+
this.name = 'AgentDocsError';
|
|
19
|
+
this.problems = problems;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every `.md` file under a folder, in the order llms.txt lists them.
|
|
3
|
+
*
|
|
4
|
+
* @param root - Package root.
|
|
5
|
+
* @param dir - Folder relative to the root, with `/` separators.
|
|
6
|
+
* @returns Paths relative to the root, with `/` separators; empty when the folder is absent.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Each folder's entries are sorted by name and a subfolder's files sit where its name sorts, so a `01-` prefix
|
|
10
|
+
* sets a page's place.
|
|
11
|
+
*/
|
|
12
|
+
export declare function markdownFiles(root: string, dir: string): string[];
|
|
13
|
+
/**
|
|
14
|
+
* Orders two `/`-separated paths the way {@link markdownFiles} walks them.
|
|
15
|
+
*
|
|
16
|
+
* @param a - First path.
|
|
17
|
+
* @param b - Second path.
|
|
18
|
+
* @returns Negative when `a` comes first, positive when `b` does, 0 when equal.
|
|
19
|
+
*/
|
|
20
|
+
export declare function compareWalkOrder(a: string, b: string): number;
|
|
21
|
+
/**
|
|
22
|
+
* Whether any `.ts` file sits anywhere under a folder.
|
|
23
|
+
*
|
|
24
|
+
* @param root - Package root.
|
|
25
|
+
* @param dir - Folder relative to the root.
|
|
26
|
+
* @returns false when the folder is absent.
|
|
27
|
+
*/
|
|
28
|
+
export declare function hasTypeScript(root: string, dir: string): boolean;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* Every `.md` file under a folder, in the order llms.txt lists them.
|
|
5
|
+
*
|
|
6
|
+
* @param root - Package root.
|
|
7
|
+
* @param dir - Folder relative to the root, with `/` separators.
|
|
8
|
+
* @returns Paths relative to the root, with `/` separators; empty when the folder is absent.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Each folder's entries are sorted by name and a subfolder's files sit where its name sorts, so a `01-` prefix
|
|
12
|
+
* sets a page's place.
|
|
13
|
+
*/
|
|
14
|
+
export function markdownFiles(root, dir) {
|
|
15
|
+
const full = join(root, dir);
|
|
16
|
+
if (existsSync(full) === false) {
|
|
17
|
+
return [];
|
|
18
|
+
}
|
|
19
|
+
const found = [];
|
|
20
|
+
for (const entry of readdirSync(full).sort()) {
|
|
21
|
+
const path = `${dir}/${entry}`;
|
|
22
|
+
if (statSync(join(root, path)).isDirectory()) {
|
|
23
|
+
found.push(...markdownFiles(root, path));
|
|
24
|
+
}
|
|
25
|
+
else if (entry.endsWith('.md')) {
|
|
26
|
+
found.push(path);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Orders two `/`-separated paths the way {@link markdownFiles} walks them.
|
|
33
|
+
*
|
|
34
|
+
* @param a - First path.
|
|
35
|
+
* @param b - Second path.
|
|
36
|
+
* @returns Negative when `a` comes first, positive when `b` does, 0 when equal.
|
|
37
|
+
*/
|
|
38
|
+
export function compareWalkOrder(a, b) {
|
|
39
|
+
const left = a.split('/');
|
|
40
|
+
const right = b.split('/');
|
|
41
|
+
for (let i = 0; i < Math.min(left.length, right.length); i++) {
|
|
42
|
+
if (left[i] !== right[i]) {
|
|
43
|
+
return left[i] < right[i] ? -1 : 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return left.length - right.length;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Whether any `.ts` file sits anywhere under a folder.
|
|
50
|
+
*
|
|
51
|
+
* @param root - Package root.
|
|
52
|
+
* @param dir - Folder relative to the root.
|
|
53
|
+
* @returns false when the folder is absent.
|
|
54
|
+
*/
|
|
55
|
+
export function hasTypeScript(root, dir) {
|
|
56
|
+
const full = join(root, dir);
|
|
57
|
+
if (existsSync(full) === false) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
return readdirSync(full).some((entry) => {
|
|
61
|
+
const path = `${dir}/${entry}`;
|
|
62
|
+
if (statSync(join(root, path)).isDirectory()) {
|
|
63
|
+
return entry !== 'node_modules' && hasTypeScript(root, path);
|
|
64
|
+
}
|
|
65
|
+
return /\.(ts|tsx|mts)$/.test(entry) && entry.endsWith('.d.ts') === false;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** A parsed JSON object whose fields are not yet narrowed. */
|
|
2
|
+
export type JsonObject = Record<string, unknown>;
|
|
3
|
+
/**
|
|
4
|
+
* Tells a JSON object from an array, null or a scalar.
|
|
5
|
+
*
|
|
6
|
+
* @param value - Any parsed JSON value.
|
|
7
|
+
* @returns true when `value` is a plain object.
|
|
8
|
+
*/
|
|
9
|
+
export declare function isJsonObject(value: unknown): value is JsonObject;
|
|
10
|
+
/**
|
|
11
|
+
* Reads and parses a JSON file under the package root.
|
|
12
|
+
*
|
|
13
|
+
* @param root - Package root.
|
|
14
|
+
* @param path - File path relative to the root, with `/` separators.
|
|
15
|
+
* @returns The parsed value, or undefined when the file is absent.
|
|
16
|
+
* @throws {@link AgentDocsError} If the file is not valid JSON.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readJsonFile(root: string, path: string): unknown;
|
|
19
|
+
/**
|
|
20
|
+
* A string field of an object, if it holds one.
|
|
21
|
+
*
|
|
22
|
+
* @param object - Parsed JSON object.
|
|
23
|
+
* @param key - Field name.
|
|
24
|
+
* @returns The value, or undefined when the field is missing or not a string.
|
|
25
|
+
*/
|
|
26
|
+
export declare function stringField(object: JsonObject, key: string): string | undefined;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { AgentDocsError } from './errors.js';
|
|
4
|
+
/**
|
|
5
|
+
* Tells a JSON object from an array, null or a scalar.
|
|
6
|
+
*
|
|
7
|
+
* @param value - Any parsed JSON value.
|
|
8
|
+
* @returns true when `value` is a plain object.
|
|
9
|
+
*/
|
|
10
|
+
export function isJsonObject(value) {
|
|
11
|
+
return typeof value === 'object' && value !== null && Array.isArray(value) === false;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Reads and parses a JSON file under the package root.
|
|
15
|
+
*
|
|
16
|
+
* @param root - Package root.
|
|
17
|
+
* @param path - File path relative to the root, with `/` separators.
|
|
18
|
+
* @returns The parsed value, or undefined when the file is absent.
|
|
19
|
+
* @throws {@link AgentDocsError} If the file is not valid JSON.
|
|
20
|
+
*/
|
|
21
|
+
export function readJsonFile(root, path) {
|
|
22
|
+
const full = join(root, path);
|
|
23
|
+
if (existsSync(full) === false) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(readFileSync(full, 'utf8'));
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
31
|
+
throw new AgentDocsError([`${path} is not valid JSON: ${reason}. Fix the syntax.`]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A string field of an object, if it holds one.
|
|
36
|
+
*
|
|
37
|
+
* @param object - Parsed JSON object.
|
|
38
|
+
* @param key - Field name.
|
|
39
|
+
* @returns The value, or undefined when the field is missing or not a string.
|
|
40
|
+
*/
|
|
41
|
+
export function stringField(object, key) {
|
|
42
|
+
const value = object[key];
|
|
43
|
+
return typeof value === 'string' ? value : undefined;
|
|
44
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** A peer dependency as llms.txt states it. */
|
|
2
|
+
export interface PeerDependency {
|
|
3
|
+
/** Package name. */
|
|
4
|
+
name: string;
|
|
5
|
+
/** Version range. */
|
|
6
|
+
range: string;
|
|
7
|
+
/** Whether `peerDependenciesMeta` marks it optional. */
|
|
8
|
+
isOptional: boolean;
|
|
9
|
+
}
|
|
10
|
+
/** The parts of package.json both commands read. */
|
|
11
|
+
export interface PackageManifest {
|
|
12
|
+
/** `name`, when it is a string. */
|
|
13
|
+
name?: string;
|
|
14
|
+
/** `description`, when it is a string. */
|
|
15
|
+
description?: string;
|
|
16
|
+
/** `private: true`: the package is never published. */
|
|
17
|
+
isPrivate: boolean;
|
|
18
|
+
/** A private package with `workspaces`: a monorepo root that publishes nothing itself. */
|
|
19
|
+
isWorkspaceRoot: boolean;
|
|
20
|
+
/** `files`, when it is a list; undefined means npm's default of everything. */
|
|
21
|
+
files?: string[];
|
|
22
|
+
/** `exports` as written; undefined when absent. */
|
|
23
|
+
exports: unknown;
|
|
24
|
+
/** `engines.node`. */
|
|
25
|
+
nodeRange?: string;
|
|
26
|
+
/** `peerDependencies`, in the order written. */
|
|
27
|
+
peers: PeerDependency[];
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Reads package.json at the package root.
|
|
31
|
+
*
|
|
32
|
+
* @param root - Package root.
|
|
33
|
+
* @returns The manifest, or undefined when there is no package.json.
|
|
34
|
+
* @throws {@link AgentDocsError} If package.json is not valid JSON.
|
|
35
|
+
*/
|
|
36
|
+
export declare function readPackageManifest(root: string): PackageManifest | undefined;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { isJsonObject, readJsonFile, stringField } from './json.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reads package.json at the package root.
|
|
4
|
+
*
|
|
5
|
+
* @param root - Package root.
|
|
6
|
+
* @returns The manifest, or undefined when there is no package.json.
|
|
7
|
+
* @throws {@link AgentDocsError} If package.json is not valid JSON.
|
|
8
|
+
*/
|
|
9
|
+
export function readPackageManifest(root) {
|
|
10
|
+
const parsed = readJsonFile(root, 'package.json');
|
|
11
|
+
if (isJsonObject(parsed) === false) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const isPrivate = parsed.private === true;
|
|
15
|
+
const files = Array.isArray(parsed.files) ? parsed.files.filter((entry) => typeof entry === 'string') : undefined;
|
|
16
|
+
const engines = isJsonObject(parsed.engines) ? parsed.engines : {};
|
|
17
|
+
return {
|
|
18
|
+
name: stringField(parsed, 'name'),
|
|
19
|
+
description: stringField(parsed, 'description'),
|
|
20
|
+
isPrivate,
|
|
21
|
+
isWorkspaceRoot: isPrivate && Boolean(parsed.workspaces),
|
|
22
|
+
files,
|
|
23
|
+
exports: parsed.exports,
|
|
24
|
+
nodeRange: stringField(engines, 'node'),
|
|
25
|
+
peers: peerDependencies(parsed),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The peer dependencies a manifest declares.
|
|
30
|
+
*
|
|
31
|
+
* @param manifest - Parsed package.json.
|
|
32
|
+
* @returns One entry per string-valued `peerDependencies` field.
|
|
33
|
+
*/
|
|
34
|
+
function peerDependencies(manifest) {
|
|
35
|
+
const peers = isJsonObject(manifest.peerDependencies) ? manifest.peerDependencies : {};
|
|
36
|
+
const meta = isJsonObject(manifest.peerDependenciesMeta) ? manifest.peerDependenciesMeta : {};
|
|
37
|
+
return Object.entries(peers).flatMap(([name, range]) => {
|
|
38
|
+
if (typeof range !== 'string') {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const entryMeta = meta[name];
|
|
42
|
+
const isOptional = isJsonObject(entryMeta) && entryMeta.optional === true;
|
|
43
|
+
return [{ name, range, isOptional }];
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** The size limits both commands hold the docs to. */
|
|
2
|
+
export interface LimitSettings {
|
|
3
|
+
/** Largest agent doc page, in KB: skills servers and file tools refuse or truncate larger reads. */
|
|
4
|
+
pageLimitKb: number;
|
|
5
|
+
/** Line count past which a page must open with a contents list linking each `##` heading. */
|
|
6
|
+
contentsAfterLines: number;
|
|
7
|
+
/** Largest llms.txt, in KB, before the export index moves out of it into generated pages. */
|
|
8
|
+
llmsLimitKb: number;
|
|
9
|
+
}
|
|
10
|
+
/** The limits the agent docs standard sets. */
|
|
11
|
+
export declare const LIMIT_DEFAULTS: Readonly<LimitSettings>;
|
|
12
|
+
/** Where the docs and the source they index are found. */
|
|
13
|
+
export interface LayoutSettings {
|
|
14
|
+
/** The agent-facing pages, relative to the package root; `docsDir` in `_agents/llms.json` replaces it. */
|
|
15
|
+
docsDir: string;
|
|
16
|
+
/** Heading of the llms.txt section that lists the pages sitting directly in the docs folder. */
|
|
17
|
+
topLevelSection: string;
|
|
18
|
+
/** Folder under the docs folder that receives the export index when llms.txt would be too large. */
|
|
19
|
+
exportsFolder: string;
|
|
20
|
+
/** Build output that `exports` targets point into. */
|
|
21
|
+
outDir: string;
|
|
22
|
+
/** Source folder an `exports` target maps back to. */
|
|
23
|
+
sourceDir: string;
|
|
24
|
+
}
|
|
25
|
+
/** The layout the agent docs standard and the cubicecho build use. */
|
|
26
|
+
export declare const LAYOUT_DEFAULTS: Readonly<LayoutSettings>;
|
package/dist/defaults.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** The limits the agent docs standard sets. */
|
|
2
|
+
export const LIMIT_DEFAULTS = Object.freeze({
|
|
3
|
+
pageLimitKb: 25,
|
|
4
|
+
contentsAfterLines: 100,
|
|
5
|
+
llmsLimitKb: 25,
|
|
6
|
+
});
|
|
7
|
+
/** The layout the agent docs standard and the cubicecho build use. */
|
|
8
|
+
export const LAYOUT_DEFAULTS = Object.freeze({
|
|
9
|
+
docsDir: '_agents/docs',
|
|
10
|
+
topLevelSection: 'Docs',
|
|
11
|
+
exportsFolder: 'exports',
|
|
12
|
+
outDir: 'dist',
|
|
13
|
+
sourceDir: 'src',
|
|
14
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** One package.json `exports` entry, traced to the source file it is built from. */
|
|
2
|
+
export interface EntryPoint {
|
|
3
|
+
/** The subpath as written in `exports`: `.` or `./mdns`. */
|
|
4
|
+
subpath: string;
|
|
5
|
+
/** What a consumer imports: the package name plus the subpath. */
|
|
6
|
+
importPath: string;
|
|
7
|
+
/** The entry's source file, relative to the package root, with `/` separators. */
|
|
8
|
+
source: string;
|
|
9
|
+
}
|
|
10
|
+
/** The entry points found, and the entries that point at no source. */
|
|
11
|
+
export interface EntryPointScan {
|
|
12
|
+
/** Entries traced to a source file, in `exports` order. */
|
|
13
|
+
entries: EntryPoint[];
|
|
14
|
+
/** One line per entry whose target has no source file. */
|
|
15
|
+
problems: string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Maps each package.json `exports` entry back to its source file.
|
|
19
|
+
*
|
|
20
|
+
* @param root - Package root.
|
|
21
|
+
* @param packageName - The package's name, for the import paths.
|
|
22
|
+
* @param exportsField - package.json `exports` as written; undefined when absent.
|
|
23
|
+
* @returns The entries, and a problem for each that points into the build at a file `src/` lacks.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* A `types` or `import` target in `dist/` maps to the same path in `src/` with a `.ts` extension. Without
|
|
27
|
+
* `exports`, `src/index.ts` is the one entry when it exists. Wildcard subpaths and non-code targets (JSON, CSS)
|
|
28
|
+
* are left out, since they name no module to read.
|
|
29
|
+
*/
|
|
30
|
+
export declare function findEntryPoints(root: string, packageName: string, exportsField: unknown): EntryPointScan;
|