export default async function handler(req, res) { res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); if (req.method === 'OPTIONS') { res.status(200).end(); return; } if (req.method !== 'POST') { res.status(405).json({ error: 'Method not allowed' }); return; } const { messages } = req.body || {}; if (!messages || !Array.isArray(messages) || messages.length === 0) { res.status(400).json({ error: 'Missing messages' }); return; } const SYSTEM_PROMPT = `You are the Nevada Notary Encyclopedia, an expert AI reference tool built by Melanie B Notary for working notaries. You specialize exclusively in Nevada notary law, practice, and procedure. Your knowledge covers NRS Chapter 240, NRS Chapter 240A, Remote Online Notarization, notarial acts, acceptable ID, journal requirements, fees under NRS 240.100, prohibited acts, loan signing, seal/stamp rules, certificate wording, and common document types. ALWAYS structure your response using EXACTLY these section headers on their own lines: ## ANSWER ## NEVADA LAW ## EXPLANATION ## SCENARIO ## MISTAKES ## CHECKLIST ## RESOURCES Rules: Always cite exact NRS numbers. Always be specific to Nevada. Never give legal advice.`; res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); try { const response = await fetch('https://api.anthropic.com/v1/messages', { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-api-key': process.env.ANTHROPIC_API_KEY, 'anthropic-version': '2023-06-01', }, body: JSON.stringify({ model: 'claude-sonnet-4-5', max_tokens: 1024, system: SYSTEM_PROMPT, messages: messages, stream: true, }), }); if (!response.ok) { const errText = await response.text(); res.write(`data: ${JSON.stringify({ error: `API error: ${response.status}` })}\n\n`); res.end(); return; } const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value, { stream: true }); const lines = chunk.split('\n'); for (const line of lines) { if (line.startsWith('data: ')) { try { const data = JSON.parse(line.slice(6)); if (data.type === 'content_block_delta' && data.delta?.text) { res.write(`data: ${JSON.stringify({ text: data.delta.text })}\n\n`); } } catch {} } } } res.write(`data: ${JSON.stringify({ done: true })}\n\n`); res.end(); } catch (err) { res.write(`data: ${JSON.stringify({ error: err.message })}\n\n`); res.end(); } }