fix: back to text format with flushHeaders+heartbeat
This commit is contained in:
20
server.js
20
server.js
@@ -44,29 +44,13 @@ app.post('/api/chat', (req, res) => {
|
||||
// heartbeat to keep connection alive during long generation
|
||||
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,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
});
|
||||
|
||||
let buf = '';
|
||||
proc.stdout.on('data', (chunk) => {
|
||||
buf += chunk.toString();
|
||||
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) {}
|
||||
}
|
||||
res.write(`data: ${JSON.stringify({ text: chunk.toString() })}\n\n`);
|
||||
});
|
||||
|
||||
let stderrBuf = '';
|
||||
|
||||
Reference in New Issue
Block a user