Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 58x 58x 58x 58x 58x 58x 17x 17x 58x 58x 25x 8x 8x 58x 58x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 58x 4x 4x 4x 58x 8x 8x 58x 58x 58x 58x 58x 58x 3x 3x 58x 58x 58x 43x 43x 43x 43x 43x 43x 58x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 7x 7x 7x 7x 7x 7x 21x 21x 21x 21x 21x 21x 7x 7x 7x 15x 15x 8x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 58x 58x | import React, { useState, useRef, useEffect } from 'react'; import { MessageList } from './MessageList'; import { MessageInput } from './MessageInput'; import type { ChatSession } from './types'; import './BottomChat.css'; interface BottomChatProps { onSessionUpdate?: (session: ChatSession) => void; } export const BottomChat: React.FC<BottomChatProps> = ({ onSessionUpdate }) => { const [messages, setMessages] = useState<ChatSession['messages']>([]); const [isLoading, setIsLoading] = useState(false); const [sessionId, setSessionId] = useState<string>(''); const [isExpanded, setIsExpanded] = useState(false); const chatContainerRef = useRef<HTMLDivElement>(null); // Generate session ID on mount useEffect(() => { const newSessionId = `session-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; setSessionId(newSessionId); }, []); // Auto scroll to bottom when new messages arrive useEffect(() => { if (chatContainerRef.current) { chatContainerRef.current.scrollTop = chatContainerRef.current.scrollHeight; } }, [messages]); const handleSendMessage = async (content: string) => { if (!content.trim() || isLoading) return; const userMessage = { id: `msg-${Date.now()}`, content, timestamp: new Date(), sender: 'user' as const, type: 'text' as const }; setMessages(prev => [...prev, userMessage]); setIsLoading(true); try { const response = await fetch('/api/ai/chat/', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRFToken': getCsrfToken(), }, body: JSON.stringify({ query: content, session_id: sessionId }) }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); if (data.error) { throw new Error(data.error); } const aiMessage = { id: `msg-${Date.now()}-ai`, content: data.response, timestamp: new Date(), sender: 'ai' as const, type: 'text' as const, metadata: { queryType: data.metadata?.query_type, confidence: data.metadata?.confidence, sources: data.sources || [], processingTime: data.metadata?.processing_time } }; setMessages(prev => [...prev, aiMessage]); // Update session if callback provided if (onSessionUpdate) { onSessionUpdate({ id: sessionId, messages: [...messages, userMessage, aiMessage], createdAt: new Date(), updatedAt: new Date() }); } } catch (err) { console.error('Chat error:', err); // Add error message to chat const errorMessage = { id: `msg-${Date.now()}-error`, content: '抱歉,处理您的问题时出现了问题。请稍后再试。', timestamp: new Date(), sender: 'ai' as const, type: 'error' as const }; setMessages(prev => [...prev, errorMessage]); } finally { setIsLoading(false); } }; const getCsrfToken = (): string => { const token = document.querySelector('[name=csrfmiddlewaretoken]')?.getAttribute('value'); return token || ''; }; const toggleChat = () => { setIsExpanded(!isExpanded); }; const quickPrompts = [ "爷爷的故事", "家族传统", "健康记录" ]; const handleQuickPrompt = (prompt: string) => { handleSendMessage(prompt); }; return ( <div className="bottom-chat"> {/* Collapsed state - floating button */} {!isExpanded && ( <button className="chat-floating-btn" onClick={toggleChat} title="打开家庭知识助手" > 💬 </button> )} {/* Expanded state - chat interface */} {isExpanded && ( <div className="chat-container"> <div className="chat-header"> <h3>💬 家庭知识助手</h3> <button className="chat-close-btn" onClick={toggleChat} title="关闭" > ✕ </button> </div> <div className="chat-body"> {/* Welcome message */} {messages.length === 0 && ( <div className="chat-welcome"> <div className="welcome-icon">🤖</div> <p className="welcome-text"> 询问家庭知识,比如:爷爷的创业故事、妈妈的生日安排... </p> {/* Quick prompts */} <div className="quick-prompts"> {quickPrompts.map((prompt, index) => ( <button key={index} className="quick-prompt-btn" onClick={() => handleQuickPrompt(prompt)} > {prompt} </button> ))} </div> </div> )} {/* Messages area */} <div className="chat-messages" ref={chatContainerRef}> {messages.length > 0 && ( <MessageList messages={messages} isLoading={isLoading} /> )} </div> </div> {/* Input area */} <div className="chat-input-area"> <MessageInput onSendMessage={handleSendMessage} disabled={isLoading} placeholder="输入您的问题..." /> </div> </div> )} </div> ); }; |