import React, { useState, useRef, useEffect } from 'react'; import { PlusOutlined, CopyOutlined, LikeOutlined, DislikeOutlined, ArrowLeftOutlined, PaperClipOutlined, PhoneOutlined, } from '@ant-design/icons'; import { Tooltip, message } from 'antd'; import ReactMarkdown from 'react-markdown'; import { useChatStore, Message } from '../store/chatStore'; import { sendChatMessage } from '../api'; import '../styles/index.scss'; import aiIcon from '@/assets/public/aiIcon.png'; export const ChatInterface: React.FC = () => { const { currentSessionId, sessions, selectedAppId, addMessage, addSession, updateCurrentSession, loading, setLoading, } = useChatStore(); const [inputValue, setInputValue] = useState(''); const [activeFeatures, setActiveFeatures] = useState>(new Set()); const [abortController, setAbortController] = useState(null); const messagesEndRef = useRef(null); const inputRef = useRef(null); const currentSession = sessions.find((s) => s.id === currentSessionId); const messages = currentSession?.messages || []; useEffect(() => { messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' }); }, [messages]); const handleSendMessage = async () => { if (!inputValue.trim() || loading) return; const userMessage: Message = { id: Date.now().toString(), role: 'user', content: inputValue.trim(), createTime: new Date().toISOString(), }; let sessionId = currentSessionId; if (!sessionId) { const newSession = { id: Date.now().toString(), topic: inputValue.trim().slice(0, 20), appId: selectedAppId || undefined, messages: [userMessage], createTime: new Date().toISOString(), }; addSession(newSession); sessionId = newSession.id; setInputValue(''); return; } addMessage(sessionId, userMessage); const userText = inputValue.trim(); setInputValue(''); setLoading(true); const controller = new AbortController(); setAbortController(controller); try { const assistantMessageId = (Date.now() + 1).toString(); addMessage(sessionId, { id: assistantMessageId, role: 'assistant', content: '', createTime: new Date().toISOString(), }); const response = await sendChatMessage({ message: userText, appId: currentSession?.appId || selectedAppId || undefined, sessionId, }); updateCurrentSession((session) => { const lastMsgIndex = session.messages.length - 1; if (lastMsgIndex >= 0 && session.messages[lastMsgIndex].id === assistantMessageId) { session.messages[lastMsgIndex].content = response || '抱歉,我暂时无法回答这个问题。'; } }); } catch (error: any) { if (error.name === 'AbortError') { message.info('请求已取消'); } else { message.error('发送失败,请重试'); } } finally { setLoading(false); setAbortController(null); } }; const handleCancel = () => { abortController?.abort(); setLoading(false); }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSendMessage(); } }; const handleCopy = (content: string) => { navigator.clipboard.writeText(content); message.success('复制成功'); }; const toggleFeature = (feature: string) => { setActiveFeatures((prev) => { const next = new Set(prev); if (next.has(feature)) { next.delete(feature); } else { next.add(feature); } return next; }); }; const handleSuggestionClick = (text: string) => { setInputValue(text); inputRef.current?.focus(); }; const handleBack = () => { window.close(); setTimeout(() => { window.location.href = '/appCenter'; }, 100); }; const features = [ { key: 'deep-think', label: '深度思考/思维链', icon: '🔬' }, { key: 'search', label: '联网搜索', icon: '🌐' }, ]; const suggestions = [ '专业知识', '职能管理', '项目级应用', ]; return (
{/* Back Button */} {/* Chat Messages */}
{messages.length === 0 ? ( ) : ( <> {messages.map((msg) => (
{msg.role === 'assistant' ? '🤖' : '👤'}
{msg.content} {msg.role === 'assistant' && (
)}
))} {loading && (
🤖
)}
)}
{/* Footer */}
内容由 AI 生成,请仔细甄别 | 上海建科工程咨询有限公司
); }; // Welcome Screen Component interface WelcomeScreenProps { features: Array<{ key: string; label: string; icon: string; isNew?: boolean }>; activeFeatures: Set; toggleFeature: (feature: string) => void; suggestions: string[]; onSuggestionClick: (text: string) => void; inputValue: string; setInputValue: (value: string) => void; onSend: () => void; loading: boolean; inputRef: React.RefObject; } const WelcomeScreen: React.FC = ({ features, activeFeatures, toggleFeature, suggestions, onSuggestionClick, inputValue, setInputValue, onSend, loading, inputRef, }) => { return (
{/* Logo */}
AI Icon
建科小智
,智能问答
{/* Input Container */}
setInputValue(e.currentTarget.textContent || '')} onKeyDown={(e) => { if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); onSend(); } }} suppressContentEditableWarning data-placeholder="请输入你的问题或需求" /> {/* Input Actions - 包含功能按钮和发送按钮 */}
{/* 附件按钮 */} {/* 功能按钮 */} {features.map((feature) => ( ))}
{/* Suggestion Tags */}
我猜你在找...
{suggestions.map((text, index) => (
onSuggestionClick(text)}> {text}
))}
); };