/** * Chat API Utilities * Handles session management and file uploads */ export async function createNewSession( firstMessage: string | undefined, API_BASE_URL: string, getAuthHeaders: () => Record, setCurrentSessionId: (id: string) => void, setMessages: (messages: any[]) => void, loadChatHistory: () => void ): Promise { // Generate title from first message if provided let title = "New Chat" if (firstMessage) { const content = firstMessage.trim() if (content.length <= 50) { title = content } else { title = content.substring(0, 50).split(' ').slice(0, -1).join(' ') + "..." } } try { const headers: Record = { "Content-Type": "application/json", ...getAuthHeaders() } const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions`, { method: "POST", headers, body: JSON.stringify({ title }) }) if (response.ok) { const session = await response.json() setCurrentSessionId(session.id) setMessages([]) loadChatHistory() return session.id } } catch (error) { console.error("Failed to create session:", error) } return null } export async function loadSession( sessionId: string, API_BASE_URL: string, getAuthHeaders: () => Record, setMessages: (messages: any[]) => void, setCurrentSessionId: (id: string) => void, setShowHistory: (show: boolean) => void ): Promise { try { const headers: Record = { "Content-Type": "application/json", ...getAuthHeaders() } const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions/${sessionId}/messages`, { headers }) if (response.ok) { const sessionMessages = await response.json() setMessages(sessionMessages) setCurrentSessionId(sessionId) setShowHistory(false) } } catch (error) { console.error("Failed to load session:", error) } } export async function uploadFiles( sessionId: string, files: File[], API_BASE_URL: string, getAuthHeaders: () => Record, setUploadingFiles: (uploading: boolean) => void ): Promise { const attachmentIds: string[] = [] setUploadingFiles(true) try { for (const file of files) { const formData = new FormData() formData.append("file", file) const headers: Record = { ...getAuthHeaders() } const response = await fetch(`${API_BASE_URL}/api/v1/chat/sessions/${sessionId}/attachments`, { method: "POST", headers, body: formData, }) if (response.ok) { const data = await response.json() attachmentIds.push(data.attachment.id) } else { const error = await response.json() throw new Error(error.detail || "Failed to upload file") } } } finally { setUploadingFiles(false) } return attachmentIds }