Hamed744 commited on
Commit
aafa71e
·
verified ·
1 Parent(s): ebf4c54

Update static/js/api.js

Browse files
Files changed (1) hide show
  1. static/js/api.js +18 -14
static/js/api.js CHANGED
@@ -1,3 +1,5 @@
 
 
1
  // static/js/api.js
2
 
3
  import * as state from './state.js';
@@ -192,8 +194,6 @@ export async function processAndUploadFile(file) {
192
  }
193
  }
194
 
195
- // NOTE: This function now depends on the refactored UI modules.
196
- // It will not work correctly without them. I am assuming they are available.
197
  export async function streamResponse(modelBubbleOuterDivElement, conversationHistory) {
198
  // Dynamically import the UI modules needed for this function.
199
  const chatUI = await import('./ui/chat.js');
@@ -264,25 +264,30 @@ export async function streamResponse(modelBubbleOuterDivElement, conversationHis
264
  for (const event of events) {
265
  if (event.startsWith('data: ')) {
266
  const dataString = event.substring(6);
267
- if (dataString.trim() === '[DONE]') {
268
  break;
269
  }
270
  try {
271
  const jsonData = JSON.parse(dataString);
272
- const textChunk = jsonData.choices?.[0]?.delta?.content;
273
-
274
- if (textChunk) {
275
- fullBotResponse += textChunk;
 
 
 
 
 
276
 
277
  if (activeTool) {
278
  chatUI.streamFinalText(fullBotResponse, modelBubbleOuterDivElement);
279
  } else {
280
  chatUI.streamFreeWsChunk(modelBubbleOuterDivElement, fullBotResponse);
281
  }
282
- }
283
- else if (jsonData.type === 'error') {
284
  throw new Error(jsonData.message || 'خطای ناشناخته از سرور');
285
  }
 
286
 
287
  } catch (e) {
288
  // console.warn('خطا در پردازش قطعه JSON:', dataString, e);
@@ -332,14 +337,13 @@ export async function streamResponse(modelBubbleOuterDivElement, conversationHis
332
  if (tempMsgIndex > -1) {
333
  if(!modelBubbleOuterDivElement.querySelector('.message-content')?.innerText.includes('متوقف شد')) {
334
  activeChat.messages.splice(tempMsgIndex, 1);
335
- // The following line causes an error because `dom` is not defined here.
336
- // This highlights the issues with splitting UI logic without careful refactoring.
337
- // const tempElement = document.getElementById('chat-window').querySelector(`.message-entry[data-index="${tempMsgIndex}"]`);
338
- // if (tempElement) tempElement.remove();
339
  }
340
  }
341
  }
342
  state.saveSessions();
343
  chatUI.resetState();
344
  }
345
- }
 
 
1
+ // --- START OF FILE api.js ---
2
+
3
  // static/js/api.js
4
 
5
  import * as state from './state.js';
 
194
  }
195
  }
196
 
 
 
197
  export async function streamResponse(modelBubbleOuterDivElement, conversationHistory) {
198
  // Dynamically import the UI modules needed for this function.
199
  const chatUI = await import('./ui/chat.js');
 
264
  for (const event of events) {
265
  if (event.startsWith('data: ')) {
266
  const dataString = event.substring(6);
267
+ if (dataString === '[DONE]') { // Check for DONE signal
268
  break;
269
  }
270
  try {
271
  const jsonData = JSON.parse(dataString);
272
+
273
+ // --- START: MODIFIED LOGIC ---
274
+ // این بخش برای پردازش فرمت پاسخ دریافتی از اسپیس HF جدید است
275
+ if ((jsonData.type === 'tool-log' || jsonData.type === 'tool-progress') && activeTool) {
276
+ if (activeTool === 'deep-think') toolUI.updateDeepThinkPanel(jsonData, modelBubbleOuterDivElement);
277
+ if (activeTool === 'reasoning') toolUI.updateReasoningPanel(jsonData, modelBubbleOuterDivElement);
278
+ } else if (jsonData.type === 'text-delta') {
279
+ const chunk = jsonData.delta;
280
+ fullBotResponse += chunk;
281
 
282
  if (activeTool) {
283
  chatUI.streamFinalText(fullBotResponse, modelBubbleOuterDivElement);
284
  } else {
285
  chatUI.streamFreeWsChunk(modelBubbleOuterDivElement, fullBotResponse);
286
  }
287
+ } else if (jsonData.type === 'error') {
 
288
  throw new Error(jsonData.message || 'خطای ناشناخته از سرور');
289
  }
290
+ // --- END: MODIFIED LOGIC ---
291
 
292
  } catch (e) {
293
  // console.warn('خطا در پردازش قطعه JSON:', dataString, e);
 
337
  if (tempMsgIndex > -1) {
338
  if(!modelBubbleOuterDivElement.querySelector('.message-content')?.innerText.includes('متوقف شد')) {
339
  activeChat.messages.splice(tempMsgIndex, 1);
340
+ const tempElement = document.getElementById('chat-window').querySelector(`.message-entry[data-index="${tempMsgIndex}"]`);
341
+ if (tempElement) tempElement.remove();
 
 
342
  }
343
  }
344
  }
345
  state.saveSessions();
346
  chatUI.resetState();
347
  }
348
+ }
349
+ // --- END OF FILE api.js ---