fix: back to text format with flushHeaders+heartbeat

This commit is contained in:
aedes
2026-04-20 17:38:40 +03:00
parent 3c3812bc18
commit 2019bf216f

View File

@@ -44,29 +44,13 @@ app.post('/api/chat', (req, res) => {
// heartbeat to keep connection alive during long generation // heartbeat to keep connection alive during long generation
const heartbeat = setInterval(() => res.write(': ping\n\n'), 15000); const heartbeat = setInterval(() => res.write(': ping\n\n'), 15000);
const proc = spawn(CLAUDE_BIN, ['-p', text, '--model', modelId, '--output-format', 'stream-json', '--verbose'], { const proc = spawn(CLAUDE_BIN, ['-p', text, '--model', modelId, '--output-format', 'text'], {
env, env,
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
}); });
let buf = '';
proc.stdout.on('data', (chunk) => { proc.stdout.on('data', (chunk) => {
buf += chunk.toString(); res.write(`data: ${JSON.stringify({ text: chunk.toString() })}\n\n`);
const lines = buf.split('\n');
buf = lines.pop();
for (const line of lines) {
if (!line.trim()) continue;
try {
const obj = JSON.parse(line);
if (obj.type === 'assistant' && obj.message?.content) {
for (const block of obj.message.content) {
if (block.type === 'text') res.write(`data: ${JSON.stringify({ text: block.text })}\n\n`);
}
} else if (obj.type === 'result' && obj.is_error) {
res.write(`data: ${JSON.stringify({ error: obj.result || 'error' })}\n\n`);
}
} catch (e) {}
}
}); });
let stderrBuf = ''; let stderrBuf = '';