Spaces:
Sleeping
Sleeping
Update src/App.js
Browse files- src/App.js +20 -15
src/App.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
// src/App.js
|
| 2 |
|
| 3 |
import React, { useState } from "react";
|
| 4 |
-
import { sendChat, login } from "./api";
|
| 5 |
import "./App.css";
|
| 6 |
|
| 7 |
import ReactMarkdown from "react-markdown";
|
|
@@ -56,41 +56,46 @@ function App() {
|
|
| 56 |
async function handleSend(e) {
|
| 57 |
if (e) e.preventDefault();
|
| 58 |
if (!input.trim() || loading || !token) return;
|
| 59 |
-
|
| 60 |
const userMsg = { role: "user", text: input.trim() };
|
| 61 |
const newMessages = [...messages, userMsg];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
setMessages(newMessages);
|
| 63 |
setInput("");
|
| 64 |
setError(null);
|
| 65 |
setLoading(true);
|
| 66 |
-
|
| 67 |
try {
|
| 68 |
const historyForBackend = newMessages.map((m) => ({
|
| 69 |
role: m.role,
|
| 70 |
content: m.text,
|
| 71 |
}));
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
userMsg.text,
|
| 75 |
strictMode,
|
| 76 |
historyForBackend,
|
| 77 |
-
token
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
);
|
| 79 |
-
|
| 80 |
-
const assistantMsg = {
|
| 81 |
-
role: "assistant",
|
| 82 |
-
text: response.reply,
|
| 83 |
-
debug: response.prolog_query_info,
|
| 84 |
-
};
|
| 85 |
-
|
| 86 |
-
setMessages([...newMessages, assistantMsg]);
|
| 87 |
} catch (err) {
|
| 88 |
-
|
|
|
|
| 89 |
} finally {
|
| 90 |
setLoading(false);
|
| 91 |
}
|
| 92 |
}
|
| 93 |
|
|
|
|
| 94 |
// ============================================================
|
| 95 |
// LOGIN SCREEN (shown if no token)
|
| 96 |
// ============================================================
|
|
|
|
| 1 |
// src/App.js
|
| 2 |
|
| 3 |
import React, { useState } from "react";
|
| 4 |
+
import { sendChat, login, streamChat } from "./api";
|
| 5 |
import "./App.css";
|
| 6 |
|
| 7 |
import ReactMarkdown from "react-markdown";
|
|
|
|
| 56 |
async function handleSend(e) {
|
| 57 |
if (e) e.preventDefault();
|
| 58 |
if (!input.trim() || loading || !token) return;
|
| 59 |
+
|
| 60 |
const userMsg = { role: "user", text: input.trim() };
|
| 61 |
const newMessages = [...messages, userMsg];
|
| 62 |
+
|
| 63 |
+
// Create empty assistant bubble for streaming
|
| 64 |
+
const assistantMsg = { role: "assistant", text: "" };
|
| 65 |
+
newMessages.push(assistantMsg);
|
| 66 |
+
|
| 67 |
setMessages(newMessages);
|
| 68 |
setInput("");
|
| 69 |
setError(null);
|
| 70 |
setLoading(true);
|
| 71 |
+
|
| 72 |
try {
|
| 73 |
const historyForBackend = newMessages.map((m) => ({
|
| 74 |
role: m.role,
|
| 75 |
content: m.text,
|
| 76 |
}));
|
| 77 |
+
|
| 78 |
+
// STREAM HERE ⬇⬇⬇
|
| 79 |
+
await streamChat(
|
| 80 |
userMsg.text,
|
| 81 |
strictMode,
|
| 82 |
historyForBackend,
|
| 83 |
+
token,
|
| 84 |
+
(chunk) => {
|
| 85 |
+
assistantMsg.text += chunk; // append chunk
|
| 86 |
+
setMessages([...newMessages]); // re-render UI
|
| 87 |
+
}
|
| 88 |
);
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
} catch (err) {
|
| 91 |
+
assistantMsg.text += `\n[Error: ${err.message}]`;
|
| 92 |
+
setMessages([...newMessages]);
|
| 93 |
} finally {
|
| 94 |
setLoading(false);
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
+
|
| 99 |
// ============================================================
|
| 100 |
// LOGIN SCREEN (shown if no token)
|
| 101 |
// ============================================================
|