import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DOCS_DIR = path.join(__dirname, "..", "src", "docs");
const DOC_SOURCES = [
{
provider: "openai",
urls: [
"https://platform.openai.com/docs/api-reference/chat",
"https://platform.openai.com/docs/api-reference/embeddings",
"https://platform.openai.com/docs/api-reference/models",
],
},
{
provider: "anthropic",
urls: [
"https://docs.anthropic.com/en/api/messages",
"https://docs.anthropic.com/en/api/build-a-client",
],
},
{
provider: "groq",
urls: [
"https://console.groq.com/docs/text-chat",
"https://console.groq.com/docs/api-reference",
],
},
{
provider: "deepseek",
urls: [
"https://api-docs.deepseek.com/quick_start/pricing",
"https://api-docs.deepseek.com/guides/function_calling",
],
},
{
provider: "gemini",
urls: [
"https://ai.google.dev/gemini-api/docs/text-generation",
"https://ai.google.dev/api/generate-content",
],
},
];
async function fetchPage(url) {
try {
const res = await fetch(url, {
headers: {
"User-Agent": "Mozilla/5.0 (compatible; APIDocsUpdater/1.0)",
},
});
if (!res.ok) {
console.warn(` ā Failed to fetch ${url}: HTTP ${res.status}`);
return null;
}
return await res.text();
} catch (e) {
console.warn(` ā Error fetching ${url}: ${e.message}`);
return null;
}
}
function extractTextFromHtml(html) {
const withoutScripts = html.replace(/<script[\s\S]*?<\/script>/gi, "");
const withoutStyles = withoutScripts.replace(/<style[\s\S]*?<\/style>/gi, "");
const withoutComments = withoutStyles.replace(/<!--[\s\S]*?-->/g, "");
const withBreaks = withoutComments
.replace(/<(br|p|div|li|tr|h[1-6])[\s>]/gi, "\n")
.replace(/<\/(p|div|li|tr|h[1-6])>/gi, "\n");
const withoutTags = withBreaks.replace(/<[^>]*>/g, "");
const decoded = withoutTags
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/ /g, " ");
return decoded.replace(/[ \t]+/g, " ").replace(/\n{3,}/g, "\n\n").trim();
}
async function updateProvider(provider) {
console.log(`\nš¦ Updating ${provider.provider}...`);
const allContent = [];
for (const url of provider.urls) {
console.log(` Fetching ${url}...`);
const html = await fetchPage(url);
if (html) {
const text = extractTextFromHtml(html);
allContent.push({ url, text: text.substring(0, 10000) });
}
}
if (allContent.length === 0) {
console.log(` ā No content fetched for ${provider.provider}`);
return;
}
const output = {
provider: provider.provider,
updated: new Date().toISOString(),
sources: allContent,
};
const outputPath = path.join(DOCS_DIR, `${provider.provider}-raw.json`);
fs.writeFileSync(outputPath, JSON.stringify(output, null, 2));
console.log(` ā
Saved to ${outputPath}`);
}
async function main() {
console.log("š API Documentation Updater");
console.log("============================");
if (!fs.existsSync(DOCS_DIR)) {
fs.mkdirSync(DOCS_DIR, { recursive: true });
}
for (const provider of DOC_SOURCES) {
await updateProvider(provider);
}
console.log("\nā
Update complete!");
console.log("Raw documentation saved to src/docs/*.json");
console.log("Review and manually integrate into TypeScript doc files.");
}
main().catch(console.error);