Repository navigation
Expand file tree
/
Copy pathgenerate-tool-docs.mjs
More file actions
65 lines (59 loc) · 3.25 KB
/
Copy pathgenerate-tool-docs.mjs
File metadata and controls
65 lines (59 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Generates docs/TOOLS.md from the compiled tool registry (the same schemas
// used for tools/list and validation). `--check` fails if the file is stale.
import { readFileSync, writeFileSync } from 'node:fs';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const { listTools, acceptedNames } = require('../dist/tools/registry.js');
const target = new URL('../docs/TOOLS.md', import.meta.url);
function typeOf(schema) {
if (!schema || typeof schema !== 'object') return 'any';
if (schema.enum) return schema.enum.map((v) => `\`${JSON.stringify(v)}\``).join(' \\| ');
if (schema.const !== undefined) return `\`${JSON.stringify(schema.const)}\``;
if (schema.anyOf) return schema.anyOf.map(typeOf).join(' or ');
if (schema.type === 'array') return `${typeOf(schema.items)}[]`;
if (schema.type === 'integer' || schema.type === 'number') {
const range = [schema.minimum ?? schema.exclusiveMinimum, schema.maximum].some((v) => v !== undefined)
? ` (${schema.exclusiveMinimum !== undefined ? `>${schema.exclusiveMinimum}` : schema.minimum ?? '…'}–${schema.maximum ?? '…'})`
: '';
return `${schema.type}${range}`;
}
return schema.type ?? 'any';
}
const escape = (text) => String(text ?? '').replace(/\|/g, '\\|').replace(/\n/g, ' ');
const aliases = new Map();
for (const [alias, canonical] of acceptedNames()) {
if (alias === canonical) continue;
aliases.set(canonical, [...(aliases.get(canonical) ?? []), alias]);
}
let out = '# Tool reference\n\n';
out += '<!-- Generated by `npm run docs:tools` from the tool registry. Do not edit by hand; CI runs `npm run docs:check`. -->\n\n';
out += 'Every tool is read-only and queries public NASA/JPL services. Unknown parameters are rejected. Legacy names are accepted as aliases but not listed by `tools/list`.\n\n';
for (const tool of listTools()) {
out += `## \`${tool.name}\` — ${tool.title}\n\n${tool.description}\n\n`;
const names = aliases.get(tool.name);
if (names?.length) out += `Aliases: ${names.map((n) => `\`${n}\``).join(', ')}\n\n`;
const properties = tool.inputSchema.properties ?? {};
const required = new Set(tool.inputSchema.required ?? []);
const rows = Object.entries(properties);
if (rows.length) {
out += '| Parameter | Type | Required | Default | Description |\n|---|---|---|---|---|\n';
for (const [name, schema] of rows) {
out += `| \`${name}\` | ${escape(typeOf(schema))} | ${required.has(name) ? 'yes' : ''} | ${schema.default !== undefined ? `\`${escape(JSON.stringify(schema.default))}\`` : ''} | ${escape(schema.description)} |\n`;
}
out += '\n';
} else {
out += 'No parameters.\n\n';
}
if (tool.outputSchema) out += `Returns \`structuredContent\` matching its advertised \`outputSchema\` (fields: ${Object.keys(tool.outputSchema.properties ?? {}).map((k) => `\`${k}\``).join(', ')}).\n\n`;
}
if (process.argv.includes('--check')) {
const current = readFileSync(target, 'utf8').replace(/\r\n/g, '\n');
if (current !== out) {
console.error('docs/TOOLS.md is out of date with the tool registry. Run `npm run docs:tools` and commit the result.');
process.exit(1);
}
console.log('docs/TOOLS.md matches the tool registry.');
} else {
writeFileSync(target, out);
console.log(`wrote ${target.pathname}`);
}