docs: give GDK its own navigation and developer guides (#13165)
This commit is contained in:
@@ -240,7 +240,8 @@ function renderDocumentation(schemas, meta, gooseVersion = 'Preview') {
|
||||
const output = [
|
||||
'---',
|
||||
'title: goose ACP Reference',
|
||||
'sidebar_label: Reference',
|
||||
'sidebar_label: goose extensions',
|
||||
'displayed_sidebar: gdkSidebar',
|
||||
'sidebar_position: 2',
|
||||
'---',
|
||||
'',
|
||||
|
||||
@@ -46,6 +46,7 @@ async function main() {
|
||||
const sections = [
|
||||
{ name: 'Getting Started', pattern: 'getting-started/*.{md,mdx}' },
|
||||
{ name: 'Guides', pattern: 'guides/**/*.{md,mdx}' },
|
||||
{ name: 'GDK', pattern: 'gdk/**/*.{md,mdx}' },
|
||||
];
|
||||
|
||||
let output = `# goose Documentation Map
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const {
|
||||
LANGUAGES,
|
||||
funcAnchor,
|
||||
itemAnchor,
|
||||
memberAnchor,
|
||||
signature,
|
||||
variantName,
|
||||
} = require("../src/components/GdkApiReference/api");
|
||||
|
||||
const cell = (value) =>
|
||||
String(value).replace(/\|/g, "\\|").replace(/\r?\n/g, "<br />");
|
||||
const heading = (level, anchor, name) =>
|
||||
`<a id="${anchor}"></a>\n\n${"#".repeat(level)} ${name}\n`;
|
||||
|
||||
function parameterTable(rows, language, anchor, kind) {
|
||||
if (rows.length === 0) return "";
|
||||
return [
|
||||
"| Name | Type | Default | Description |",
|
||||
"| --- | --- | --- | --- |",
|
||||
...rows.map((row) => {
|
||||
const name = language.field(row.name);
|
||||
const type = language.type(row.type);
|
||||
const value =
|
||||
row.default === null ? "—" : `\`${language.default(row.default)}\``;
|
||||
return `| <a id="${memberAnchor(anchor, kind, row.name)}"></a>\`${name}\` | \`${cell(type)}\` | ${cell(value)} | ${cell(row.docs || "—")} |`;
|
||||
}),
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function renderFunction(func, language, owner) {
|
||||
const anchor = funcAnchor(func, owner);
|
||||
return [
|
||||
heading(owner ? 4 : 3, anchor, `\`${language.func(func.name)}\``),
|
||||
...(func.docs ? [func.docs, ""] : []),
|
||||
`\`\`\`${language.prism}`,
|
||||
signature(func, language, owner),
|
||||
"```",
|
||||
"",
|
||||
parameterTable(func.params, language, anchor, "param"),
|
||||
...(func.throws
|
||||
? [`Raises \`${language.errorType(func.throws)}\`.`, ""]
|
||||
: []),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function renderMarkdown(doc, language) {
|
||||
const filename = `api-reference-${language.id}.md`;
|
||||
const output = [
|
||||
`# ${language.label} API reference`,
|
||||
"",
|
||||
`Generated from \`${doc.source}\` for \`goose-sdk ${doc.version}\` (documentation series ${doc.docVersion}). Do not edit this file.`,
|
||||
"",
|
||||
`This is the in-process provider and compaction API, not the full Rust agent runtime. Read the [crate overview](/docs/gdk/reference/index.md) for native Rust APIs.`,
|
||||
"",
|
||||
`[Versioned copy](/docs/gdk/sdk/${doc.version}/${filename}). Match the API snapshot to your installed package before writing code.`,
|
||||
"",
|
||||
"## Contents",
|
||||
"",
|
||||
"- [Functions](#functions)",
|
||||
...doc.items.map((item) => {
|
||||
const name =
|
||||
item.kind === "error" ? language.errorType(item.name) : item.name;
|
||||
return `- [${name}](#${itemAnchor(item)})`;
|
||||
}),
|
||||
"",
|
||||
heading(2, "functions", "Functions"),
|
||||
...doc.functions.map((func) => renderFunction(func, language)),
|
||||
];
|
||||
|
||||
for (const item of doc.items) {
|
||||
const anchor = itemAnchor(item);
|
||||
const name =
|
||||
item.kind === "error" ? language.errorType(item.name) : item.name;
|
||||
output.push(
|
||||
heading(2, anchor, `\`${name}\``),
|
||||
`Kind: ${item.kind}.`,
|
||||
"",
|
||||
...(item.docs ? [item.docs, ""] : []),
|
||||
parameterTable(item.fields, language, anchor, "field"),
|
||||
);
|
||||
for (const variant of item.variants) {
|
||||
const variantAnchor = memberAnchor(anchor, "variant", variant.name);
|
||||
output.push(
|
||||
heading(
|
||||
3,
|
||||
variantAnchor,
|
||||
`\`${variantName(item, variant, language)}\``,
|
||||
),
|
||||
parameterTable(variant.fields, language, variantAnchor, "field"),
|
||||
);
|
||||
}
|
||||
output.push(
|
||||
...item.methods.map((method) =>
|
||||
renderFunction(method, language, item.name),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
output
|
||||
.join("\n")
|
||||
.replace(/\n{3,}/g, "\n\n")
|
||||
.trimEnd() + "\n"
|
||||
);
|
||||
}
|
||||
|
||||
function generateReferences(data, outputDir) {
|
||||
for (const language of LANGUAGES.filter((entry) => entry.id !== "rust")) {
|
||||
const filename = `api-reference-${language.id}.md`;
|
||||
for (const [index, doc] of data.versions.entries()) {
|
||||
const content = renderMarkdown(doc, language);
|
||||
const versionDir = path.join(outputDir, doc.version);
|
||||
fs.mkdirSync(versionDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(versionDir, filename), content);
|
||||
if (index === 0)
|
||||
fs.writeFileSync(path.join(outputDir, filename), content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
const data = require("../src/data/gdk-api.json");
|
||||
const outputDir = path.join(__dirname, "../static/docs/gdk/sdk");
|
||||
generateReferences(data, outputDir);
|
||||
console.log(
|
||||
`[generate-gdk-api-markdown] Generated Python and Kotlin references in ${outputDir}`,
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { renderMarkdown, generateReferences };
|
||||
@@ -0,0 +1,149 @@
|
||||
const { test } = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const {
|
||||
generateReferences,
|
||||
renderMarkdown,
|
||||
} = require("./generate-gdk-api-markdown");
|
||||
const { LANGUAGES } = require("../src/components/GdkApiReference/api");
|
||||
const data = require("../src/data/gdk-api.json");
|
||||
|
||||
test("publishes the linked APIs and preserves versioned snapshots", (t) => {
|
||||
const outputDir = fs.mkdtempSync(path.join(os.tmpdir(), "gdk-api-markdown-"));
|
||||
t.after(() => fs.rmSync(outputDir, { recursive: true, force: true }));
|
||||
const previous = { ...data.versions[0], version: "0.0.1", docVersion: "0.0" };
|
||||
const versions = [...data.versions, previous];
|
||||
generateReferences({ versions }, outputDir);
|
||||
|
||||
const llms = fs.readFileSync(
|
||||
path.join(__dirname, "../static/llms.txt"),
|
||||
"utf8",
|
||||
);
|
||||
const links = [
|
||||
...llms.matchAll(
|
||||
/\]\((\/docs\/gdk\/sdk\/api-reference-(?:python|kotlin)\.md)(?:#([^)]*))?\)/g,
|
||||
),
|
||||
];
|
||||
assert.ok(
|
||||
links.length > 0,
|
||||
"llms.txt must advertise the generated references",
|
||||
);
|
||||
for (const [, target, anchor] of links) {
|
||||
const content = fs.readFileSync(
|
||||
path.join(outputDir, path.basename(target)),
|
||||
"utf8",
|
||||
);
|
||||
if (anchor)
|
||||
assert.ok(
|
||||
content.includes(`id="${anchor}"`),
|
||||
`Missing ${target}#${anchor}`,
|
||||
);
|
||||
}
|
||||
|
||||
for (const language of ["python", "kotlin"]) {
|
||||
const filename = `api-reference-${language}.md`;
|
||||
const latest = fs.readFileSync(path.join(outputDir, filename), "utf8");
|
||||
for (const [index, doc] of versions.entries()) {
|
||||
const snapshot = fs.readFileSync(
|
||||
path.join(outputDir, doc.version, filename),
|
||||
"utf8",
|
||||
);
|
||||
assert.ok(snapshot.includes(`goose-sdk ${doc.version}`));
|
||||
if (index === 0) assert.equal(latest, snapshot);
|
||||
else assert.notEqual(latest, snapshot);
|
||||
for (const func of doc.functions) {
|
||||
const anchor = `fn-${func.name.replaceAll("_", "-")}`;
|
||||
assert.ok(
|
||||
snapshot.includes(`id="${anchor}"`),
|
||||
`Missing function ${func.name}`,
|
||||
);
|
||||
}
|
||||
for (const item of doc.items) {
|
||||
assert.ok(snapshot.includes(`id="${item.name.toLowerCase()}"`));
|
||||
for (const method of item.methods) {
|
||||
const anchor = `${item.name}-${method.name}`
|
||||
.replaceAll("_", "-")
|
||||
.toLowerCase();
|
||||
assert.ok(
|
||||
snapshot.includes(`id="${anchor}"`),
|
||||
`Missing method ${item.name}.${method.name}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("renders binding signatures, defaults, error payloads, and field documentation", () => {
|
||||
const python = renderMarkdown(
|
||||
data.versions[0],
|
||||
LANGUAGES.find((entry) => entry.id === "python"),
|
||||
);
|
||||
assert.ok(
|
||||
python.includes(
|
||||
"async def Provider.stream(model: ProviderModelConfig, system: str, messages: list[ProviderMessage], tools: list[ProviderTool]) -> ProviderStream",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
python.includes(
|
||||
"async def ProviderStream.next_chunk() -> StreamChunk | None",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
python.includes(
|
||||
"async def Provider.compact(model_name: str, messages: list[CompactionMessage], templates: CompactionTemplates | None) -> CompactionSummary",
|
||||
),
|
||||
);
|
||||
assert.ok(python.includes("`toolshim` | `bool` | `False`"));
|
||||
assert.ok(
|
||||
python.includes("`request_headers` | `dict[str, str] \\| None` | `None`"),
|
||||
);
|
||||
assert.ok(
|
||||
python.includes(
|
||||
"Per-request HTTP headers attached to the outgoing provider call.",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
python.includes('id="gooseerror-variant-ratelimited-field-retry-after-ms"'),
|
||||
);
|
||||
assert.ok(python.includes("Raises `GooseError`."));
|
||||
assert.ok(!python.includes("<GdkApiReference"));
|
||||
|
||||
const kotlin = renderMarkdown(
|
||||
data.versions[0],
|
||||
LANGUAGES.find((entry) => entry.id === "kotlin"),
|
||||
);
|
||||
assert.ok(
|
||||
kotlin.includes("suspend fun ProviderStream.nextChunk(): StreamChunk?"),
|
||||
);
|
||||
assert.ok(
|
||||
kotlin.includes(
|
||||
"suspend fun Provider.listModels(): List<ProviderModelInfo>",
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
kotlin.includes("`requestHeaders` | `Map<String, String>?` | `null`"),
|
||||
);
|
||||
assert.ok(kotlin.includes("@Throws(GooseException::class)"));
|
||||
assert.ok(kotlin.includes("### `GooseException.RateLimited`"));
|
||||
});
|
||||
|
||||
test("Markdown exports turn static download links into fetchable paths", async (t) => {
|
||||
const siteDir = fs.mkdtempSync(
|
||||
path.join(os.tmpdir(), "gdk-markdown-export-"),
|
||||
);
|
||||
t.after(() => fs.rmSync(siteDir, { recursive: true, force: true }));
|
||||
fs.mkdirSync(path.join(siteDir, "docs"));
|
||||
fs.writeFileSync(
|
||||
path.join(siteDir, "docs/reference.mdx"),
|
||||
'---\ntitle: Reference\n---\nimport Api from "./api";\n\n[Python](pathname:///docs/gdk/sdk/api-reference-python.md)\n',
|
||||
);
|
||||
const plugin = require("../plugins/markdown-export.cjs")({ siteDir });
|
||||
await plugin.postBuild({ outDir: path.join(siteDir, "build") });
|
||||
assert.equal(
|
||||
fs.readFileSync(path.join(siteDir, "build/docs/reference.md"), "utf8"),
|
||||
"[Python](/docs/gdk/sdk/api-reference-python.md)",
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user