import jwt from 'jsonwebtoken'; import dotenv from 'dotenv'; dotenv.config(); const BASE = 'http://localhost:4000'; const ADMIN = '6a00cefe524bebd27037a968'; const token = jwt.sign({ id: ADMIN }, process.env.CRM_JWT_SECRET, { expiresIn: '30m' }); const res = await fetch(`${BASE}/api/agent/stream`, { method: 'POST', headers: { 'content-type': 'application/json', authorization: `Bearer ${token}` }, body: JSON.stringify({ message: 'Show me the lead temperature split and how many follow-ups are overdue.', session_id: 'stream-' + Math.random().toString(36).slice(2, 8), }), }); console.log('status', res.status, res.headers.get('content-type')); const reader = res.body.getReader(); const dec = new TextDecoder(); let buf = ''; let events = 0; for (;;) { const { value, done } = await reader.read(); if (done) break; buf += dec.decode(value, { stream: true }); const frames = buf.split('\n\n'); buf = frames.pop() ?? ''; for (const f of frames) { const line = f.split('\n').find((l) => l.startsWith('data: ')); if (!line) { if (f.startsWith(':')) console.log(' · heartbeat'); continue; } const ev = JSON.parse(line.slice(6)); events++; if (ev.type === 'step') console.log(` → step: ${ev.label}${ev.detail ? ` — ${ev.detail.slice(0, 70)}` : ''}`); else if (ev.type === 'blocks' || ev.type === 'result') { const kinds = (ev.blocks || []).map((b) => b.type).join(', '); if (ev.blocks?.length) console.log(` ✔ ${ev.type}: [${kinds}]`); } else console.log(` · ${ev.type}`); } } console.log(`\ntotal SSE events: ${events}`); process.exit(0);