import { Document } from '@/types' import { estimateTokens, extractSiteName } from './parser' /** * Generates the llms-full.md consolidated document with table of contents */ export function generateFullDocument( documents: Document[], rawContent: string, sourceUrl: string ): Document { const siteName = extractSiteName(sourceUrl) const title = `${siteName.charAt(0).toUpperCase() + siteName.slice(1)} Documentation` // Build table of contents const tocLines = documents.map((doc, index) => { const anchor = doc.title.toLowerCase().replace(/[^\w\s-]/g, '').replace(/\s+/g, '-') return `${index + 1}. [${doc.title}](#${anchor})` }) const toc = tocLines.length > 0 ? `## Table of Contents\n\n${tocLines.join('\n')}\n\n---\n\n` : '' // Build full content const header = `# ${title}\n\n> Source: ${sourceUrl}\n\n${toc}` // Combine all document contents const bodyParts = documents.map(doc => doc.content) const body = bodyParts.join('\n\n---\n\n') const fullContent = `${header}${body}` return { filename: 'llms-full.md', title, content: fullContent, tokens: estimateTokens(fullContent) } } /** * Generates the AGENT-GUIDE.md file with instructions for AI assistants */ export function generateAgentGuide( documents: Document[], sourceUrl: string, siteName: string ): Document { const formattedSiteName = siteName.charAt(0).toUpperCase() + siteName.slice(1) const extractionDate = new Date().toISOString().split('T')[0] // Build file list with descriptions const fileList = documents.map(doc => { const tokenInfo = `(${doc.tokens.toLocaleString()} tokens)` return `- **${doc.filename}** - ${doc.title} ${tokenInfo}` }).join('\n') // Determine topics from document titles const topics = documents.map(d => d.title).slice(0, 5).join(', ') const topicSummary = documents.length > 5 ? `${topics}, and more` : topics const content = `# Agent Guide for ${formattedSiteName} Documentation This package contains documentation extracted from ${formattedSiteName}'s official documentation. ## Source Information - **Original URL**: ${sourceUrl} - **Extraction Date**: ${extractionDate} - **Total Documents**: ${documents.length} - **Total Tokens**: ${documents.reduce((sum, d) => sum + d.tokens, 0).toLocaleString()} ## Included Files ### Main Files - **llms-full.md** - Complete documentation compiled into a single file with table of contents - **AGENT-GUIDE.md** - This file, containing usage instructions ### Individual Documentation Files ${fileList} ## How to Use This Documentation As an AI assistant, you can use these files to: 1. **Answer questions** about ${formattedSiteName} features, APIs, and functionality 2. **Provide code examples** from the official documentation 3. **Explain concepts** and best practices as documented by ${formattedSiteName} 4. **Troubleshoot issues** by referencing relevant documentation sections 5. **Guide users** through setup, configuration, and implementation ## Recommended Reading Order 1. Start with **llms-full.md** for complete context and overview 2. Reference individual files when focusing on specific topics: ${documents.slice(0, 5).map((d, i) => ` ${i + 1}. ${d.filename} - for ${d.title}`).join('\n')} ## Topics Covered This documentation covers: ${topicSummary} ## Important Notes - This documentation was automatically extracted and may not include all content from the original source - For the most up-to-date information, refer to the original documentation at ${sourceUrl} - Code examples should be verified against the current API version ## Source Reference Original documentation: ${sourceUrl} --- *Generated by llm.energy - Documentation Extraction Tool* ` return { filename: 'AGENT-GUIDE.md', title: 'Agent Guide', content, tokens: estimateTokens(content) } } /** * Main generation function: creates all output documents from parsed content */ export function generateAllDocuments( documents: Document[], rawContent: string, sourceUrl: string ): { documents: Document[] fullDocument: Document agentGuide: Document } { const siteName = extractSiteName(sourceUrl) const fullDocument = generateFullDocument(documents, rawContent, sourceUrl) const agentGuide = generateAgentGuide(documents, sourceUrl, siteName) return { documents, fullDocument, agentGuide } }