import { EvidencePayload } from './types'; // Very forgiving parser: expects the assistant to include a JSON block delimited by ```json ... ``` // If no fenced block found, tries to JSON.parse the entire string. export function tryParseEvidence(text: string): EvidencePayload | null { try { const fenceStart = text.indexOf('```json'); const fenceEnd = text.indexOf('```', fenceStart + 7); const raw = fenceStart >= 0 && fenceEnd > fenceStart ? text.slice(fenceStart + 7, fenceEnd).trim() : text.trim(); const obj = JSON.parse(raw); if (obj && obj.type === 'evidence' && Array.isArray(obj.claims)) { return obj as EvidencePayload; } } catch {} return null; }