Files
odysseus/static/js/emailReplyStream.js
T
pewdiepie-archdaemon 2e8413a54a Preserve preview harness, editor, email and task improvements
Snapshot current maintainer-preview application changes and regression fixtures for integration into lab. Excludes local runtime data, evaluation outputs and source backups. Focused Python regression selection: 140 passed; full suite not certified.
2026-10-01 01:34:26 +00:00

31 lines
1.2 KiB
JavaScript

export async function readEmailReplyResponse(response, onReply) {
if (!response.ok) throw new Error(`AI reply service returned HTTP ${response.status}`);
if (!response.headers.get('content-type')?.includes('text/event-stream')) return response.json();
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
try {
while (true) {
const { value, done } = await reader.read();
buffer += decoder.decode(value || new Uint8Array(), { stream: !done });
let boundary;
while ((boundary = buffer.indexOf('\n\n')) !== -1) {
const frame = buffer.slice(0, boundary);
buffer = buffer.slice(boundary + 2);
for (const line of frame.split('\n')) {
if (!line.startsWith('data: ')) continue;
const event = JSON.parse(line.slice(6));
if (event.type === 'reply' && onReply(event.text) === false) {
throw new Error('Draft was edited or closed; AI insertion stopped');
}
if (event.type === 'result') return event;
}
}
if (done) throw new Error('AI reply connection ended before completion');
}
} finally {
await reader.cancel();
reader.releaseLock();
}
}