Spaces:
Sleeping
Sleeping
optimized to reduce latency
Browse files- web/src/App.tsx +58 -68
web/src/App.tsx
CHANGED
|
@@ -1065,6 +1065,16 @@ function App() {
|
|
| 1065 |
let pendingReasoningFlush = '';
|
| 1066 |
let rafId: number | null = null;
|
| 1067 |
let firstChunkSeen = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1068 |
|
| 1069 |
const flushChunks = () => {
|
| 1070 |
rafId = null;
|
|
@@ -1073,27 +1083,24 @@ function App() {
|
|
| 1073 |
pendingThinkingFlush = '';
|
| 1074 |
pendingReasoningFlush = '';
|
| 1075 |
if (!tc && !rc) return;
|
| 1076 |
-
|
| 1077 |
-
|
| 1078 |
-
|
| 1079 |
-
|
| 1080 |
-
|
| 1081 |
-
|
| 1082 |
-
|
| 1083 |
-
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
next.thinkingContent = (msg.thinkingContent || '') + tc;
|
| 1087 |
-
next.thinkingExpanded = true;
|
| 1088 |
-
}
|
| 1089 |
-
}
|
| 1090 |
-
if (rc) {
|
| 1091 |
-
next.reasoningContent = (msg.reasoningContent || '') + rc;
|
| 1092 |
-
next.reasoningExpanded = true;
|
| 1093 |
}
|
| 1094 |
-
|
| 1095 |
-
|
| 1096 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1097 |
};
|
| 1098 |
|
| 1099 |
const scheduleFlush = () => {
|
|
@@ -1165,46 +1172,34 @@ function App() {
|
|
| 1165 |
toolsUsed = true;
|
| 1166 |
finalBuffer = '';
|
| 1167 |
setStatusMessage(`Generating ${data.name}...`);
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
|
| 1174 |
-
|
| 1175 |
-
|
| 1176 |
-
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
},
|
| 1184 |
-
],
|
| 1185 |
-
}
|
| 1186 |
-
: msg
|
| 1187 |
-
)
|
| 1188 |
-
);
|
| 1189 |
} else if (event === 'tool_executing') {
|
| 1190 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|
| 1191 |
flushChunks();
|
| 1192 |
// Tool is now executing - update status
|
| 1193 |
setStatusMessage(`Executing ${data.name}...`);
|
| 1194 |
-
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
|
| 1198 |
-
|
| 1199 |
-
|
| 1200 |
-
|
| 1201 |
-
|
| 1202 |
-
: tc
|
| 1203 |
-
),
|
| 1204 |
-
}
|
| 1205 |
-
: msg
|
| 1206 |
-
)
|
| 1207 |
-
);
|
| 1208 |
} else if (event === 'tool_complete') {
|
| 1209 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|
| 1210 |
flushChunks();
|
|
@@ -1266,19 +1261,14 @@ function App() {
|
|
| 1266 |
hasError = output.startsWith('Error:') || output.includes('\nError:');
|
| 1267 |
}
|
| 1268 |
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
-
|
| 1273 |
-
...
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
|
| 1277 |
-
: tc
|
| 1278 |
-
),
|
| 1279 |
-
};
|
| 1280 |
-
})
|
| 1281 |
-
);
|
| 1282 |
} else if (event === 'done') {
|
| 1283 |
// Flush any remaining buffered chunks synchronously before routing
|
| 1284 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|
|
|
|
| 1065 |
let pendingReasoningFlush = '';
|
| 1066 |
let rafId: number | null = null;
|
| 1067 |
let firstChunkSeen = false;
|
| 1068 |
+
const updateAssistantMessage = (updater: (msg: Message) => Message) => {
|
| 1069 |
+
setMessages((prev) => {
|
| 1070 |
+
let idx = prev.length - 1;
|
| 1071 |
+
while (idx >= 0 && prev[idx].id !== assistantId) idx -= 1;
|
| 1072 |
+
if (idx < 0) return prev;
|
| 1073 |
+
const next = prev.slice();
|
| 1074 |
+
next[idx] = updater(prev[idx]);
|
| 1075 |
+
return next;
|
| 1076 |
+
});
|
| 1077 |
+
};
|
| 1078 |
|
| 1079 |
const flushChunks = () => {
|
| 1080 |
rafId = null;
|
|
|
|
| 1083 |
pendingThinkingFlush = '';
|
| 1084 |
pendingReasoningFlush = '';
|
| 1085 |
if (!tc && !rc) return;
|
| 1086 |
+
updateAssistantMessage((msg) => {
|
| 1087 |
+
const next: typeof msg = { ...msg, isStreaming: true };
|
| 1088 |
+
if (tc) {
|
| 1089 |
+
if (isR1) {
|
| 1090 |
+
// R1: content chunks are the final answer, not thinking — route
|
| 1091 |
+
// directly to content so the Thinking panel never shows for R1.
|
| 1092 |
+
next.content = (msg.content || '') + tc;
|
| 1093 |
+
} else {
|
| 1094 |
+
next.thinkingContent = (msg.thinkingContent || '') + tc;
|
| 1095 |
+
next.thinkingExpanded = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1096 |
}
|
| 1097 |
+
}
|
| 1098 |
+
if (rc) {
|
| 1099 |
+
next.reasoningContent = (msg.reasoningContent || '') + rc;
|
| 1100 |
+
next.reasoningExpanded = true;
|
| 1101 |
+
}
|
| 1102 |
+
return next;
|
| 1103 |
+
});
|
| 1104 |
};
|
| 1105 |
|
| 1106 |
const scheduleFlush = () => {
|
|
|
|
| 1172 |
toolsUsed = true;
|
| 1173 |
finalBuffer = '';
|
| 1174 |
setStatusMessage(`Generating ${data.name}...`);
|
| 1175 |
+
updateAssistantMessage((msg) => ({
|
| 1176 |
+
...msg,
|
| 1177 |
+
isStreaming: true,
|
| 1178 |
+
toolCalls: [
|
| 1179 |
+
...(msg.toolCalls || []),
|
| 1180 |
+
{
|
| 1181 |
+
id: data.id,
|
| 1182 |
+
name: data.name,
|
| 1183 |
+
input: data.input,
|
| 1184 |
+
output: '',
|
| 1185 |
+
isGenerating: true,
|
| 1186 |
+
isExecuting: false,
|
| 1187 |
+
},
|
| 1188 |
+
],
|
| 1189 |
+
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1190 |
} else if (event === 'tool_executing') {
|
| 1191 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|
| 1192 |
flushChunks();
|
| 1193 |
// Tool is now executing - update status
|
| 1194 |
setStatusMessage(`Executing ${data.name}...`);
|
| 1195 |
+
updateAssistantMessage((msg) => ({
|
| 1196 |
+
...msg,
|
| 1197 |
+
toolCalls: (msg.toolCalls || []).map((tc) =>
|
| 1198 |
+
tc.id === data.id
|
| 1199 |
+
? { ...tc, isGenerating: false, isExecuting: true }
|
| 1200 |
+
: tc
|
| 1201 |
+
),
|
| 1202 |
+
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1203 |
} else if (event === 'tool_complete') {
|
| 1204 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|
| 1205 |
flushChunks();
|
|
|
|
| 1261 |
hasError = output.startsWith('Error:') || output.includes('\nError:');
|
| 1262 |
}
|
| 1263 |
|
| 1264 |
+
updateAssistantMessage((msg) => ({
|
| 1265 |
+
...msg,
|
| 1266 |
+
toolCalls: (msg.toolCalls || []).map((tc) =>
|
| 1267 |
+
tc.id === data.id
|
| 1268 |
+
? { ...tc, output: data.output, error: hasError, isGenerating: false, isExecuting: false }
|
| 1269 |
+
: tc
|
| 1270 |
+
),
|
| 1271 |
+
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1272 |
} else if (event === 'done') {
|
| 1273 |
// Flush any remaining buffered chunks synchronously before routing
|
| 1274 |
if (rafId !== null) { cancelAnimationFrame(rafId); rafId = null; }
|