Spaces:
Runtime error
Runtime error
File size: 1,180 Bytes
b040061 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import { useCallback } from 'react';
const BACKEND_URL = import.meta.env.VITE_BACKEND_URL || (import.meta.env.DEV ? 'http://localhost:8000' : '');
export default function useOperatorSSE() {
const sendChat = useCallback(async (message) => {
const res = await fetch(`${BACKEND_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message }),
});
const reader = res.body.getReader();
const decoder = new TextDecoder();
let replyText = '';
let buffer = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (line.startsWith('data:') || line.startsWith('data: ')) {
try {
const data = JSON.parse(line.replace(/^data:\s*/, ''));
if (data.type === 'chat_reply') {
replyText = data.text;
}
} catch { /* skip */ }
}
}
}
return replyText;
}, []);
return { sendChat };
}
|