incognitolm commited on
Commit ·
8270b87
1
Parent(s): fb24a1d
Update chatStream.js
Browse files- server/chatStream.js +14 -0
server/chatStream.js
CHANGED
|
@@ -192,6 +192,20 @@ export async function websocketChatStream(body, headers, onToken, abortSignal) {
|
|
| 192 |
if (payload.error) {
|
| 193 |
console.error("[WS PAYLOAD ERROR]", payload.error);
|
| 194 |
if (onToken) onToken(`[ERROR] ${payload.error}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
return;
|
| 196 |
}
|
| 197 |
|
|
|
|
| 192 |
if (payload.error) {
|
| 193 |
console.error("[WS PAYLOAD ERROR]", payload.error);
|
| 194 |
if (onToken) onToken(`[ERROR] ${payload.error}`);
|
| 195 |
+
|
| 196 |
+
// Mark as finished to end the stream
|
| 197 |
+
if (!finished) {
|
| 198 |
+
finished = true;
|
| 199 |
+
const toolCalls = [...toolCallBuffer.values()].map((t) => ({
|
| 200 |
+
id: t.id || `call_${crypto.randomUUID()}`,
|
| 201 |
+
type: "function",
|
| 202 |
+
function: { name: t.name, arguments: t.arguments },
|
| 203 |
+
}));
|
| 204 |
+
ws.removeListener("message", messageHandler);
|
| 205 |
+
ws.removeListener("error", errorHandler);
|
| 206 |
+
if (abortSignal) abortSignal.removeEventListener("abort", abortHandler);
|
| 207 |
+
resolve({ assistantText, toolCalls });
|
| 208 |
+
}
|
| 209 |
return;
|
| 210 |
}
|
| 211 |
|