spagestic commited on
Commit
fd9dc38
·
1 Parent(s): 79db033

fix: messages shown in the chat UI

Browse files
Files changed (3) hide show
  1. app.py +2 -1
  2. assets/app.js +9 -2
  3. assets/gradio_api.js +4 -1
app.py CHANGED
@@ -109,7 +109,8 @@ def api_chat(
109
  history: list[dict],
110
  globe_state: dict | None,
111
  hf_token: gr.OAuthToken | None,
112
- ):
 
113
  yield from server_api.stream_chat(message, history, globe_state, hf_token)
114
 
115
 
 
109
  history: list[dict],
110
  globe_state: dict | None,
111
  hf_token: gr.OAuthToken | None,
112
+ ) -> dict[str, Any]:
113
+ """Stream chat updates. Return type enables Gradio SSE output registration."""
114
  yield from server_api.stream_chat(message, history, globe_state, hf_token)
115
 
116
 
assets/app.js CHANGED
@@ -559,15 +559,22 @@ async function loadChoices() {
559
  }
560
 
561
  async function runChat(message) {
 
 
 
 
562
  await gradioStream(
563
  "/chat",
564
  {
565
  message,
566
- history: state.history,
567
  globe_state: state.globeState,
568
  },
569
  async (chunk) => {
570
- if (chunk.history) {
 
 
 
571
  state.history = chunk.history;
572
  }
573
  if (chunk.globe_state) {
 
559
  }
560
 
561
  async function runChat(message) {
562
+ const priorHistory = state.history;
563
+ state.history = [...priorHistory, { role: "user", content: message }];
564
+ await renderMessages();
565
+
566
  await gradioStream(
567
  "/chat",
568
  {
569
  message,
570
+ history: priorHistory,
571
  globe_state: state.globeState,
572
  },
573
  async (chunk) => {
574
+ if (!chunk || typeof chunk !== "object" || Array.isArray(chunk)) {
575
+ return;
576
+ }
577
+ if (Array.isArray(chunk.history)) {
578
  state.history = chunk.history;
579
  }
580
  if (chunk.globe_state) {
assets/gradio_api.js CHANGED
@@ -56,7 +56,10 @@ export async function gradioStream(apiName, payload = {}, onChunk) {
56
  typeof parsed === "string" ? parsed : JSON.stringify(parsed);
57
  return;
58
  }
59
- onChunk(unwrapStreamData(parsed));
 
 
 
60
  } catch {
61
  // Ignore malformed SSE chunks and keep the last valid payload.
62
  }
 
56
  typeof parsed === "string" ? parsed : JSON.stringify(parsed);
57
  return;
58
  }
59
+ const chunk = unwrapStreamData(parsed);
60
+ if (chunk !== null && chunk !== undefined) {
61
+ onChunk(chunk);
62
+ }
63
  } catch {
64
  // Ignore malformed SSE chunks and keep the last valid payload.
65
  }