Spaces:
Configuration error
Configuration error
File size: 8,591 Bytes
aa15bce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
'use client';
import { useCallback, useEffect, useState } from 'react';
import SettingsModal, { useSettings } from '@/components/SettingsModal';
import { ChatHeader } from '@/components/chat/ChatHeader';
import { ChatInput } from '@/components/chat/ChatInput';
import { ChatMessages } from '@/components/chat/ChatMessages';
import { ErrorBanner } from '@/components/chat/ErrorBanner';
import { useAutoScroll } from '@/components/chat/useAutoScroll';
import type { ChatBubble } from '@/components/chat/types';
const POLL_INTERVAL_MS = 1500;
const formatEscapeCharacters = (text: string): string => {
return text
.replace(/\\n/g, '\n')
.replace(/\\t/g, '\t')
.replace(/\\r/g, '\r')
.replace(/\\\\/g, '\\');
};
const isRenderableMessage = (entry: any) =>
typeof entry?.role === 'string' &&
typeof entry?.content === 'string' &&
entry.content.trim().length > 0;
const toBubbles = (payload: any): ChatBubble[] => {
if (!Array.isArray(payload?.messages)) return [];
return payload.messages
.filter(isRenderableMessage)
.map((message: any, index: number) => ({
id: `history-${index}`,
role: message.role,
text: formatEscapeCharacters(message.content),
}));
};
export default function Page() {
const { settings, setSettings } = useSettings();
const [open, setOpen] = useState(false);
const [input, setInput] = useState('');
const [messages, setMessages] = useState<ChatBubble[]>([]);
const [error, setError] = useState<string | null>(null);
const [isWaitingForResponse, setIsWaitingForResponse] = useState(false);
const { scrollContainerRef, handleScroll } = useAutoScroll({
items: messages,
isWaiting: isWaitingForResponse,
});
const openSettings = useCallback(() => setOpen(true), [setOpen]);
const closeSettings = useCallback(() => setOpen(false), [setOpen]);
const loadHistory = useCallback(async () => {
try {
const res = await fetch('/api/chat/history', { cache: 'no-store' });
if (!res.ok) return;
const data = await res.json();
setMessages(toBubbles(data));
} catch (err: any) {
if (err?.name === 'AbortError') return;
console.error('Failed to load chat history', err);
}
}, []);
useEffect(() => {
void loadHistory();
}, [loadHistory]);
// Detect and store browser timezone on first load
useEffect(() => {
const detectAndStoreTimezone = async () => {
// Only run if timezone not already stored
if (settings.timezone) return;
try {
const browserTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
// Send to server
const response = await fetch('/api/timezone', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ timezone: browserTimezone }),
});
if (response.ok) {
// Update local settings
setSettings({ ...settings, timezone: browserTimezone });
}
} catch (error) {
// Fail silently - timezone detection is not critical
console.debug('Timezone detection failed:', error);
}
};
void detectAndStoreTimezone();
}, [settings, setSettings]);
useEffect(() => {
const intervalId = window.setInterval(() => {
void loadHistory();
}, POLL_INTERVAL_MS);
return () => window.clearInterval(intervalId);
}, [loadHistory]);
const canSubmit = input.trim().length > 0;
const inputPlaceholder = 'Type a message…';
const sendMessage = useCallback(
async (text: string) => {
const trimmed = text.trim();
if (!trimmed) return;
setError(null);
setIsWaitingForResponse(true);
// Optimistically add the user message immediately
const userMessage: ChatBubble = {
id: `user-${Date.now()}`,
role: 'user',
text: formatEscapeCharacters(trimmed),
};
setMessages(prev => {
const newMessages = [...prev, userMessage];
return newMessages;
});
try {
const res = await fetch('/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
messages: [{ role: 'user', content: trimmed }],
}),
});
if (!(res.ok || res.status === 202)) {
const detail = await res.text();
throw new Error(detail || `Request failed (${res.status})`);
}
} catch (err: any) {
console.error('Failed to send message', err);
setError(err?.message || 'Failed to send message');
// Remove the optimistic message on error
setMessages(prev => prev.filter(msg => msg.id !== userMessage.id));
setIsWaitingForResponse(false);
throw err instanceof Error ? err : new Error('Failed to send message');
} finally {
// Poll until we get the assistant's response
let pollAttempts = 0;
const maxPollAttempts = 30; // Max 30 attempts (30 seconds)
const pollForAssistantResponse = async () => {
pollAttempts++;
try {
const res = await fetch('/api/chat/history', { cache: 'no-store' });
if (res.ok) {
const data = await res.json();
const currentMessages = toBubbles(data);
// Check if the last message is from assistant and contains our user message
const lastMessage = currentMessages[currentMessages.length - 1];
const hasUserMessage = currentMessages.some(msg => msg.text === trimmed && msg.role === 'user');
const hasAssistantResponse = lastMessage?.role === 'assistant' && hasUserMessage;
if (hasAssistantResponse) {
// We got the assistant response, update messages and stop loading
setMessages(currentMessages);
setIsWaitingForResponse(false);
return;
}
}
} catch (err) {
console.error('Error polling for response:', err);
}
// Continue polling if we haven't exceeded max attempts
if (pollAttempts < maxPollAttempts) {
setTimeout(pollForAssistantResponse, 1000); // Poll every second
} else {
// Timeout - stop loading and update messages anyway
setIsWaitingForResponse(false);
await loadHistory();
}
};
// Start polling after a brief delay
setTimeout(pollForAssistantResponse, 1000);
}
},
[loadHistory],
);
const handleClearHistory = useCallback(async () => {
try {
const res = await fetch('/api/chat/history', { method: 'DELETE' });
if (!res.ok) {
console.error('Failed to clear chat history', res.statusText);
return;
}
setMessages([]);
} catch (err) {
console.error('Failed to clear chat history', err);
}
}, [setMessages]);
const triggerClearHistory = useCallback(() => {
void handleClearHistory();
}, [handleClearHistory]);
const handleSubmit = useCallback(async () => {
if (!canSubmit) return;
const value = input;
setInput('');
try {
await sendMessage(value);
} catch {
setInput(value);
}
}, [canSubmit, input, sendMessage, setInput]);
const handleInputChange = useCallback((value: string) => {
setInput(value);
}, [setInput]);
const clearError = useCallback(() => setError(null), [setError]);
return (
<main className="chat-bg min-h-screen p-4 sm:p-6">
<div className="chat-wrap flex flex-col">
<ChatHeader onOpenSettings={openSettings} onClearHistory={triggerClearHistory} />
<div className="card flex-1 overflow-hidden">
<ChatMessages
messages={messages}
isWaitingForResponse={isWaitingForResponse}
scrollContainerRef={scrollContainerRef}
onScroll={handleScroll}
/>
<div className="border-t border-gray-200 p-3">
{error && <ErrorBanner message={error} onDismiss={clearError} />}
<ChatInput
value={input}
canSubmit={canSubmit}
placeholder={inputPlaceholder}
onChange={handleInputChange}
onSubmit={handleSubmit}
/>
</div>
</div>
<SettingsModal open={open} onClose={closeSettings} settings={settings} onSave={setSettings} />
</div>
</main>
);
}
|