/** * AGENTS.md Injector * Safely merges generated indexes into AGENTS.md while preserving user content */ import { readFile, writeFile } from 'fs/promises'; import { existsSync } from 'fs'; const BEGIN_MARKER = ''; const END_MARKER = ''; /** * Inject indexes into AGENTS.md, preserving user content */ export async function injectAgentsMd(outPath: string, indexes: string[]): Promise { let existingContent = ''; // Read existing file if it exists if (existsSync(outPath)) { existingContent = await readFile(outPath, 'utf-8'); } // Generate managed section const managedSection = generateManagedSection(indexes); // Merge with existing content const newContent = mergeContent(existingContent, managedSection); // Write back await writeFile(outPath, newContent, 'utf-8'); } /** * Generate the managed section with all indexes */ function generateManagedSection(indexes: string[]): string { const lines = [ BEGIN_MARKER, '', '', '## Framework Documentation Indexes', '', ]; for (const index of indexes) { lines.push(index); lines.push(''); } lines.push(END_MARKER); return lines.join('\n'); } /** * Merge managed section with existing content */ function mergeContent(existing: string, managedSection: string): string { // If no existing content, just return managed section with a header if (!existing.trim()) { return `# Project Guidelines\n\n${managedSection}\n`; } // Check if managed section already exists const beginIndex = existing.indexOf(BEGIN_MARKER); const endIndex = existing.indexOf(END_MARKER); if (beginIndex !== -1 && endIndex !== -1) { // Replace existing managed section const before = existing.slice(0, beginIndex); const after = existing.slice(endIndex + END_MARKER.length); return `${before}${managedSection}${after}`; } // Append managed section to end return `${existing.trimEnd()}\n\n${managedSection}\n`; } /** * Remove managed section from AGENTS.md */ export async function removeManagedSection(outPath: string): Promise { if (!existsSync(outPath)) return; const content = await readFile(outPath, 'utf-8'); const beginIndex = content.indexOf(BEGIN_MARKER); const endIndex = content.indexOf(END_MARKER); if (beginIndex !== -1 && endIndex !== -1) { const before = content.slice(0, beginIndex); const after = content.slice(endIndex + END_MARKER.length); const newContent = (before + after).trim(); if (newContent) { await writeFile(outPath, newContent + '\n', 'utf-8'); } } } /** * Check if AGENTS.md has a managed section */ export async function hasManagedSection(outPath: string): Promise { if (!existsSync(outPath)) return false; const content = await readFile(outPath, 'utf-8'); return content.includes(BEGIN_MARKER) && content.includes(END_MARKER); } /** * Get just the user content (excluding managed section) */ export async function getUserContent(outPath: string): Promise { if (!existsSync(outPath)) return ''; const content = await readFile(outPath, 'utf-8'); const beginIndex = content.indexOf(BEGIN_MARKER); const endIndex = content.indexOf(END_MARKER); if (beginIndex !== -1 && endIndex !== -1) { const before = content.slice(0, beginIndex); const after = content.slice(endIndex + END_MARKER.length); return (before + after).trim(); } return content; }