import { escapeHtml, sanitizeUrl } from '@/utils/sanitize'; const SECTION_HEADERS = ['SITUATION NOW', 'WHAT THIS MEANS FOR', 'KEY RISKS', 'OUTLOOK', 'WATCH ITEMS']; export interface IntelBriefCitationSource { title?: string; url?: string; } type IntelBriefCitationOptions = | { sources: readonly IntelBriefCitationSource[] } | { count: number; hrefPrefix: string }; /** * Converts structured LLM intel brief text into HTML. * Handles the 5-section format (SITUATION NOW / WHAT THIS MEANS FOR / KEY RISKS / OUTLOOK / WATCH ITEMS). * Falls back gracefully to paragraph rendering for older prose-format responses. * * @param text Raw brief text from LLM * @param citationOpts Optional citation link config for source references like [1], [2] */ export function formatIntelBrief( text: string, citationOpts?: IntelBriefCitationOptions, ): string { const escaped = escapeHtml(text); const lines = escaped.split('\n'); const out: string[] = []; let inSection = false; for (const line of lines) { const trimmed = line.trim(); const isHeader = SECTION_HEADERS.some(h => trimmed.toUpperCase().startsWith(h)); if (isHeader) { if (inSection) out.push(''); out.push(`
${escaped.replace(/\n\n/g, '
').replace(/\n/g, '
')}