orchestration fixes
This commit is contained in:
@@ -31,6 +31,7 @@ export async function sendMessage(sessionId, message, model) {
|
||||
// onChunk(text) called for each token
|
||||
// onDone({ model, tokenCount }) called when stream closes
|
||||
// returns an abort function — call it to cancel mid-stream
|
||||
/*
|
||||
export function streamMessage(sessionId, message, model, { onChunk, onDone, onError }) {
|
||||
const controller = new AbortController();
|
||||
|
||||
@@ -80,7 +81,63 @@ export function streamMessage(sessionId, message, model, { onChunk, onDone, onEr
|
||||
|
||||
return () => controller.abort();
|
||||
}
|
||||
*/
|
||||
export function streamMessage(sessionId, message, model, { onChunk, onDone, onError }) {
|
||||
const controller = new AbortController();
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const res = await fetch(`${BASE_URL}/chat/stream`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ sessionId, message, model }),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
if (!res.ok) throw new Error(`Stream request failed: ${res.status}`);
|
||||
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = '';
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
const events = buffer.split('\n\n');
|
||||
buffer = events.pop() || '';
|
||||
|
||||
for (const event of events) {
|
||||
const lines = event.split('\n');
|
||||
const dataLines = lines
|
||||
.filter(line => line.startsWith('data: '))
|
||||
.map(line => line.slice(6));
|
||||
|
||||
if (dataLines.length === 0) continue;
|
||||
|
||||
const raw = dataLines.join('\n').trim();
|
||||
if (raw === '[DONE]') continue;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(raw);
|
||||
|
||||
if (data.text) onChunk(data.text);
|
||||
if (data.done) onDone({ model: data.model ?? model, tokenCount: data.tokenCount ?? 0 });
|
||||
if (data.error) onError(new Error(data.error));
|
||||
} catch (err) {
|
||||
console.error('[chat-client] Failed to parse SSE event:', raw, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
if (err.name !== 'AbortError') onError(err);
|
||||
}
|
||||
})();
|
||||
|
||||
return () => controller.abort();
|
||||
}
|
||||
export async function fetchModels() {
|
||||
const res = await fetch(`${BASE_URL}/models`);
|
||||
if(!res.ok) throw new Error(`Failted to fetch models: ${res.status}`);
|
||||
|
||||
Reference in New Issue
Block a user