fraudTech commited on
Commit
e837790
·
1 Parent(s): 2eedef0

optimized to reduce latency

Browse files
Files changed (1) hide show
  1. 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
- setMessages((prev) =>
1077
- prev.map((msg) => {
1078
- if (msg.id !== assistantId) return msg;
1079
- const next: typeof msg = { ...msg, isStreaming: true };
1080
- if (tc) {
1081
- if (isR1) {
1082
- // R1: content chunks are the final answer, not thinking — route
1083
- // directly to content so the Thinking panel never shows for R1.
1084
- next.content = (msg.content || '') + tc;
1085
- } else {
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
- return next;
1095
- })
1096
- );
 
 
 
 
1097
  };
1098
 
1099
  const scheduleFlush = () => {
@@ -1165,46 +1172,34 @@ function App() {
1165
  toolsUsed = true;
1166
  finalBuffer = '';
1167
  setStatusMessage(`Generating ${data.name}...`);
1168
- setMessages((prev) =>
1169
- prev.map((msg) =>
1170
- msg.id === assistantId
1171
- ? {
1172
- ...msg,
1173
- isStreaming: true,
1174
- toolCalls: [
1175
- ...(msg.toolCalls || []),
1176
- {
1177
- id: data.id,
1178
- name: data.name,
1179
- input: data.input,
1180
- output: '',
1181
- isGenerating: true,
1182
- isExecuting: false,
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
- setMessages((prev) =>
1195
- prev.map((msg) =>
1196
- msg.id === assistantId
1197
- ? {
1198
- ...msg,
1199
- toolCalls: (msg.toolCalls || []).map((tc) =>
1200
- tc.id === data.id
1201
- ? { ...tc, isGenerating: false, isExecuting: true }
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
- setMessages((prev) =>
1270
- prev.map((msg) => {
1271
- if (msg.id !== assistantId) return msg;
1272
- return {
1273
- ...msg,
1274
- toolCalls: (msg.toolCalls || []).map((tc) =>
1275
- tc.id === data.id
1276
- ? { ...tc, output: data.output, error: hasError, isGenerating: false, isExecuting: false }
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; }