|
| 1 | +import React from 'react'; |
| 2 | +import { callAiModel, type ChatMessage } from '@services/AiService'; |
| 3 | + |
| 4 | +export const AiChatDemo: React.FC = () => { |
| 5 | + const [apiUrl, setApiUrl] = React.useState(''); |
| 6 | + const [apiKey, setApiKey] = React.useState(''); |
| 7 | + const [model, setModel] = React.useState('gpt-4o-mini'); |
| 8 | + const [messages, setMessages] = React.useState<ChatMessage[]>([{ role: 'system', content: 'You are a helpful assistant.' }]); |
| 9 | + const [input, setInput] = React.useState(''); |
| 10 | + const [loading, setLoading] = React.useState(false); |
| 11 | + const abortRef = React.useRef<AbortController | null>(null); |
| 12 | + |
| 13 | + React.useEffect(() => { |
| 14 | + const url = import.meta.env.VITE_AI_API_URL || ''; |
| 15 | + setApiUrl(url); |
| 16 | + }, []); |
| 17 | + |
| 18 | + const onSend = async () => { |
| 19 | + if (!apiUrl || !apiKey || !input.trim() || loading) return; |
| 20 | + const nextMessages = [...messages, { role: 'user', content: input.trim() } as ChatMessage]; |
| 21 | + setMessages(nextMessages); |
| 22 | + setInput(''); |
| 23 | + setLoading(true); |
| 24 | + const controller = new AbortController(); |
| 25 | + abortRef.current = controller; |
| 26 | + const assistantDraft: ChatMessage = { role: 'assistant', content: '' }; |
| 27 | + setMessages(prev => [...prev, assistantDraft]); |
| 28 | + try { |
| 29 | + await callAiModel({ |
| 30 | + apiUrl, |
| 31 | + apiKey, |
| 32 | + model, |
| 33 | + messages: nextMessages, |
| 34 | + signal: controller.signal, |
| 35 | + onChunk: (m) => { |
| 36 | + assistantDraft.content = m.content; |
| 37 | + setMessages(prev => { |
| 38 | + const copy = [...prev]; |
| 39 | + copy[copy.length - 1] = { role: 'assistant', content: assistantDraft.content }; |
| 40 | + return copy; |
| 41 | + }); |
| 42 | + } |
| 43 | + }); |
| 44 | + } catch (e) { |
| 45 | + // noop |
| 46 | + } finally { |
| 47 | + setLoading(false); |
| 48 | + abortRef.current = null; |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const onAbort = () => { |
| 53 | + abortRef.current?.abort(); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="bg-white border rounded-lg p-4 sm:p-6 space-y-4"> |
| 58 | + <h2 className="text-base sm:text-lg font-semibold text-gray-900">AI 接口演示</h2> |
| 59 | + <div className="grid grid-cols-1 sm:grid-cols-3 gap-3"> |
| 60 | + <input className="px-3 py-2 border rounded col-span-1 sm:col-span-1" placeholder="API URL" |
| 61 | + value={apiUrl} onChange={(e) => setApiUrl(e.target.value)} /> |
| 62 | + <input className="px-3 py-2 border rounded col-span-1 sm:col-span-1" placeholder="API Key" |
| 63 | + type="password" value={apiKey} onChange={(e) => setApiKey(e.target.value)} /> |
| 64 | + <input className="px-3 py-2 border rounded col-span-1 sm:col-span-1" placeholder="Model" |
| 65 | + value={model} onChange={(e) => setModel(e.target.value)} /> |
| 66 | + </div> |
| 67 | + <div className="h-64 border rounded p-3 overflow-auto bg-gray-50"> |
| 68 | + {messages.map((m, i) => ( |
| 69 | + <div key={i} className="mb-2"> |
| 70 | + <div className="text-xs text-gray-500">{m.role}</div> |
| 71 | + <div className="whitespace-pre-wrap text-sm">{m.content}</div> |
| 72 | + </div> |
| 73 | + ))} |
| 74 | + </div> |
| 75 | + <div className="flex gap-2"> |
| 76 | + <input className="flex-1 px-3 py-2 border rounded" placeholder="输入消息" |
| 77 | + value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => { if (e.key === 'Enter') onSend(); }} /> |
| 78 | + <button onClick={onSend} disabled={loading} className="bg-blue-600 text-white px-4 py-2 rounded disabled:opacity-50">发送</button> |
| 79 | + <button onClick={onAbort} disabled={!loading} className="bg-gray-200 px-4 py-2 rounded disabled:opacity-50">中止</button> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +}; |
| 84 | + |
| 85 | + |
0 commit comments