put waiting too long message back in chat

This commit is contained in:
Cole Goldsmith 2025-11-20 10:51:38 -06:00
parent 8c5efb73a7
commit 0e60b7b6c9

View file

@ -72,6 +72,7 @@ function ChatPage() {
y: number; y: number;
} | null>(null); } | null>(null);
const [uploadedFile, setUploadedFile] = useState<File | null>(null); const [uploadedFile, setUploadedFile] = useState<File | null>(null);
const [waitingTooLong, setWaitingTooLong] = useState(false);
const chatInputRef = useRef<ChatInputHandle>(null); const chatInputRef = useRef<ChatInputHandle>(null);
@ -100,12 +101,13 @@ function ChatPage() {
streamingMessage, streamingMessage,
sendMessage: sendStreamingMessage, sendMessage: sendStreamingMessage,
abortStream, abortStream,
isLoading: isStreamLoading,
} = useChatStreaming({ } = useChatStreaming({
endpoint: apiEndpoint, endpoint: apiEndpoint,
onComplete: (message, responseId) => { onComplete: (message, responseId) => {
setMessages((prev) => [...prev, message]); setMessages((prev) => [...prev, message]);
setLoading(false); setLoading(false);
setWaitingTooLong(false);
if (responseId) { if (responseId) {
cancelNudges(); cancelNudges();
setPreviousResponseIds((prev) => ({ setPreviousResponseIds((prev) => ({
@ -124,6 +126,7 @@ function ChatPage() {
onError: (error) => { onError: (error) => {
console.error("Streaming error:", error); console.error("Streaming error:", error);
setLoading(false); setLoading(false);
setWaitingTooLong(false);
const errorMessage: Message = { const errorMessage: Message = {
role: "assistant", role: "assistant",
content: content:
@ -134,6 +137,23 @@ function ChatPage() {
}, },
}); });
// Show warning if waiting too long (20 seconds)
useEffect(() => {
let timeoutId: NodeJS.Timeout | null = null;
if (isStreamLoading && !streamingMessage) {
timeoutId = setTimeout(() => {
setWaitingTooLong(true);
}, 20000); // 20 seconds
} else {
setWaitingTooLong(false);
}
return () => {
if (timeoutId) clearTimeout(timeoutId);
};
}, [isStreamLoading, streamingMessage]);
const getCursorPosition = (textarea: HTMLTextAreaElement) => { const getCursorPosition = (textarea: HTMLTextAreaElement) => {
// Create a hidden div with the same styles as the textarea // Create a hidden div with the same styles as the textarea
const div = document.createElement("div"); const div = document.createElement("div");
@ -1323,6 +1343,20 @@ function ChatPage() {
isCompleted={false} isCompleted={false}
/> />
)} )}
{/* Waiting too long indicator */}
{waitingTooLong && !streamingMessage && loading && (
<div className="pl-10 space-y-2">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<Loader2 className="h-4 w-4 animate-spin" />
<span>The server is taking longer than expected...</span>
</div>
<p className="text-xs text-muted-foreground">
This may be due to high server load. The request will
timeout after 60 seconds.
</p>
</div>
)}
</> </>
)} )}
{!streamingMessage && ( {!streamingMessage && (