Spaces:
Sleeping
Sleeping
revert to no thinking status
Browse files- src/App.js +5 -14
- src/api.js +3 -25
src/App.js
CHANGED
|
@@ -77,7 +77,6 @@ function App() {
|
|
| 77 |
return `sid-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
| 78 |
}, []);
|
| 79 |
const [loading, setLoading] = useState(false);
|
| 80 |
-
const [thinkingStatus, setThinkingStatus] = useState(null);
|
| 81 |
const [error, setError] = useState(null);
|
| 82 |
|
| 83 |
// ---------------------------
|
|
@@ -477,7 +476,6 @@ function App() {
|
|
| 477 |
setInput("");
|
| 478 |
setError(null);
|
| 479 |
setLoading(true);
|
| 480 |
-
setThinkingStatus("Preparing request...");
|
| 481 |
|
| 482 |
try {
|
| 483 |
const historyForBackend = newMessages.map((m) => ({
|
|
@@ -495,17 +493,12 @@ function App() {
|
|
| 495 |
if (assistantMsg.thinking && !(assistantMsg.text || "").trim()) {
|
| 496 |
assistantMsg.text = chunk;
|
| 497 |
assistantMsg.thinking = false;
|
| 498 |
-
setThinkingStatus(null);
|
| 499 |
} else {
|
| 500 |
assistantMsg.text += chunk; // append chunk
|
| 501 |
}
|
| 502 |
setMessages([...newMessages]); // re-render UI
|
| 503 |
},
|
| 504 |
-
sessionId
|
| 505 |
-
(status) => {
|
| 506 |
-
// Status callback
|
| 507 |
-
setThinkingStatus(status);
|
| 508 |
-
}
|
| 509 |
);
|
| 510 |
|
| 511 |
// After streaming completes, fetch debug info via non-streaming endpoint
|
|
@@ -537,10 +530,8 @@ function App() {
|
|
| 537 |
assistantMsg.thinking = false;
|
| 538 |
assistantMsg.text += `\n[Error: ${err.message}]`;
|
| 539 |
setMessages([...newMessages]);
|
| 540 |
-
setThinkingStatus(null);
|
| 541 |
} finally {
|
| 542 |
setLoading(false);
|
| 543 |
-
setThinkingStatus(null);
|
| 544 |
}
|
| 545 |
}
|
| 546 |
|
|
@@ -704,7 +695,7 @@ function App() {
|
|
| 704 |
<main className="main">
|
| 705 |
<div className="messages">
|
| 706 |
{messages.map((msg, idx) => (
|
| 707 |
-
<Message key={idx} message={msg}
|
| 708 |
))}
|
| 709 |
</div>
|
| 710 |
|
|
@@ -962,7 +953,7 @@ function App() {
|
|
| 962 |
// MESSAGE COMPONENT
|
| 963 |
// ============================================================
|
| 964 |
|
| 965 |
-
function Message({ message
|
| 966 |
const isUser = message.role === "user";
|
| 967 |
const [showDebug, setShowDebug] = useState(false);
|
| 968 |
const stripMarkdownFences = React.useCallback((text) => {
|
|
@@ -978,10 +969,10 @@ function Message({ message, thinkingStatus }) {
|
|
| 978 |
const displayText = React.useMemo(() => {
|
| 979 |
const raw = typeof message.text === "string" ? message.text : "";
|
| 980 |
if (isThinking) {
|
| 981 |
-
return
|
| 982 |
}
|
| 983 |
return raw;
|
| 984 |
-
}, [message.text, isThinking
|
| 985 |
|
| 986 |
const normalizedText = React.useMemo(
|
| 987 |
() => normalizeLatex(displayText),
|
|
|
|
| 77 |
return `sid-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
| 78 |
}, []);
|
| 79 |
const [loading, setLoading] = useState(false);
|
|
|
|
| 80 |
const [error, setError] = useState(null);
|
| 81 |
|
| 82 |
// ---------------------------
|
|
|
|
| 476 |
setInput("");
|
| 477 |
setError(null);
|
| 478 |
setLoading(true);
|
|
|
|
| 479 |
|
| 480 |
try {
|
| 481 |
const historyForBackend = newMessages.map((m) => ({
|
|
|
|
| 493 |
if (assistantMsg.thinking && !(assistantMsg.text || "").trim()) {
|
| 494 |
assistantMsg.text = chunk;
|
| 495 |
assistantMsg.thinking = false;
|
|
|
|
| 496 |
} else {
|
| 497 |
assistantMsg.text += chunk; // append chunk
|
| 498 |
}
|
| 499 |
setMessages([...newMessages]); // re-render UI
|
| 500 |
},
|
| 501 |
+
sessionId
|
|
|
|
|
|
|
|
|
|
|
|
|
| 502 |
);
|
| 503 |
|
| 504 |
// After streaming completes, fetch debug info via non-streaming endpoint
|
|
|
|
| 530 |
assistantMsg.thinking = false;
|
| 531 |
assistantMsg.text += `\n[Error: ${err.message}]`;
|
| 532 |
setMessages([...newMessages]);
|
|
|
|
| 533 |
} finally {
|
| 534 |
setLoading(false);
|
|
|
|
| 535 |
}
|
| 536 |
}
|
| 537 |
|
|
|
|
| 695 |
<main className="main">
|
| 696 |
<div className="messages">
|
| 697 |
{messages.map((msg, idx) => (
|
| 698 |
+
<Message key={idx} message={msg} />
|
| 699 |
))}
|
| 700 |
</div>
|
| 701 |
|
|
|
|
| 953 |
// MESSAGE COMPONENT
|
| 954 |
// ============================================================
|
| 955 |
|
| 956 |
+
function Message({ message }) {
|
| 957 |
const isUser = message.role === "user";
|
| 958 |
const [showDebug, setShowDebug] = useState(false);
|
| 959 |
const stripMarkdownFences = React.useCallback((text) => {
|
|
|
|
| 969 |
const displayText = React.useMemo(() => {
|
| 970 |
const raw = typeof message.text === "string" ? message.text : "";
|
| 971 |
if (isThinking) {
|
| 972 |
+
return "Thinking...";
|
| 973 |
}
|
| 974 |
return raw;
|
| 975 |
+
}, [message.text, isThinking]);
|
| 976 |
|
| 977 |
const normalizedText = React.useMemo(
|
| 978 |
() => normalizeLatex(displayText),
|
src/api.js
CHANGED
|
@@ -63,7 +63,7 @@ export async function sendChat(message, strictMode = true, history = [], token,
|
|
| 63 |
return json;
|
| 64 |
}
|
| 65 |
|
| 66 |
-
export async function streamChat(message, strictMode, history, token, onToken, sessionId = null
|
| 67 |
const res = await fetch(`${API_BASE}/chat/stream`, {
|
| 68 |
method: "POST",
|
| 69 |
headers: {
|
|
@@ -84,35 +84,13 @@ export async function streamChat(message, strictMode, history, token, onToken, s
|
|
| 84 |
|
| 85 |
const reader = res.body.getReader();
|
| 86 |
const decoder = new TextDecoder();
|
| 87 |
-
let buffer = "";
|
| 88 |
|
| 89 |
while (true) {
|
| 90 |
const { value, done } = await reader.read();
|
| 91 |
if (done) break;
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
// Process complete SSE messages (ending with \n\n)
|
| 96 |
-
const lines = buffer.split("\n");
|
| 97 |
-
buffer = lines.pop() || ""; // Keep incomplete line in buffer
|
| 98 |
-
|
| 99 |
-
for (const line of lines) {
|
| 100 |
-
if (line.startsWith("data: ")) {
|
| 101 |
-
try {
|
| 102 |
-
const jsonData = JSON.parse(line.substring(6));
|
| 103 |
-
|
| 104 |
-
if (jsonData.type === "status" && onStatus) {
|
| 105 |
-
onStatus(jsonData.status);
|
| 106 |
-
} else if (jsonData.type === "chunk") {
|
| 107 |
-
onToken(jsonData.chunk);
|
| 108 |
-
} else if (jsonData.type === "error") {
|
| 109 |
-
throw new Error(jsonData.error || "Streaming error");
|
| 110 |
-
}
|
| 111 |
-
} catch (e) {
|
| 112 |
-
console.error("Failed to parse SSE data:", line, e);
|
| 113 |
-
}
|
| 114 |
-
}
|
| 115 |
-
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
|
|
|
| 63 |
return json;
|
| 64 |
}
|
| 65 |
|
| 66 |
+
export async function streamChat(message, strictMode, history, token, onToken, sessionId = null) {
|
| 67 |
const res = await fetch(`${API_BASE}/chat/stream`, {
|
| 68 |
method: "POST",
|
| 69 |
headers: {
|
|
|
|
| 84 |
|
| 85 |
const reader = res.body.getReader();
|
| 86 |
const decoder = new TextDecoder();
|
|
|
|
| 87 |
|
| 88 |
while (true) {
|
| 89 |
const { value, done } = await reader.read();
|
| 90 |
if (done) break;
|
| 91 |
|
| 92 |
+
const chunk = decoder.decode(value);
|
| 93 |
+
onToken(chunk);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|