File size: 11,797 Bytes
b88ce1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useState, useEffect, useRef } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import { Send, Trash2, RefreshCw, Bot, User, Key, Settings2 } from 'lucide-react';
import { useAuth } from '../context/AuthContext';
import { cn } from '../lib/utils';

export default function Test() {
    const { token: adminToken } = useAuth();
    // Initialize state from localStorage
    const [messages, setMessages] = useState(() => {
        const saved = localStorage.getItem('test_messages');
        return saved ? JSON.parse(saved) : [];
    });
    const [input, setInput] = useState('');
    const [models, setModels] = useState([]);
    const [selectedModel, setSelectedModel] = useState(() => localStorage.getItem('test_selected_model') || '');
    const [apiKey, setApiKey] = useState(() => localStorage.getItem('test_api_key') || '');

    // Persist API key
    useEffect(() => {
        localStorage.setItem('test_api_key', apiKey);
    }, [apiKey]);

    // Persist messages
    useEffect(() => {
        localStorage.setItem('test_messages', JSON.stringify(messages));
    }, [messages]);

    // Persist selected model
    useEffect(() => {
        if (selectedModel) {
            localStorage.setItem('test_selected_model', selectedModel);
        }
    }, [selectedModel]);
    const [isLoading, setIsLoading] = useState(false);
    const [isStreaming, setIsStreaming] = useState(false);
    const messagesEndRef = useRef(null);

    const fetchModels = async () => {
        try {
            const headers = {};
            if (apiKey) headers['Authorization'] = `Bearer ${apiKey}`;
            else if (adminToken) headers['Authorization'] = `Bearer ${adminToken}`;

            const res = await fetch('/v1/models', {
                headers: {
                    'Authorization': `Bearer ${apiKey || 'sk-test'}`
                }
            });

            if (res.ok) {
                const data = await res.json();
                setModels(data.data || []);
                if (data.data?.length > 0 && !selectedModel) setSelectedModel(data.data[0].id);
            }
        } catch (error) {
            console.error('Failed to fetch models', error);
        }
    };

    useEffect(() => {
        fetchModels();
    }, []);

    useEffect(() => {
        messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
    }, [messages]);

    const handleSend = async () => {
        if (!input.trim() || !selectedModel) return;

        const userMsg = { role: 'user', content: input };
        setMessages(prev => [...prev, userMsg]);
        setInput('');
        setIsLoading(true);
        setIsStreaming(true);

        const assistantMsgId = Date.now();
        setMessages(prev => [...prev, { role: 'assistant', content: '', id: assistantMsgId }]);

        try {
            const response = await fetch('/v1/chat/completions', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Authorization': `Bearer ${apiKey || 'sk-test'}`
                },
                body: JSON.stringify({
                    model: selectedModel,
                    messages: [...messages, userMsg].map(m => ({ role: m.role, content: m.content })),
                    stream: true
                })
            });

            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }

            const reader = response.body.getReader();
            const decoder = new TextDecoder();
            let assistantContent = '';

            while (true) {
                const { done, value } = await reader.read();
                if (done) break;

                const chunk = decoder.decode(value);
                const lines = chunk.split('\n');

                for (const line of lines) {
                    if (line.startsWith('data: ') && line !== 'data: [DONE]') {
                        try {
                            const data = JSON.parse(line.slice(6));
                            const content = data.choices[0]?.delta?.content || '';
                            assistantContent += content;

                            setMessages(prev => prev.map(msg =>
                                msg.id === assistantMsgId ? { ...msg, content: assistantContent } : msg
                            ));
                        } catch (e) {
                            console.error('Error parsing chunk', e);
                        }
                    }
                }
            }
        } catch (error) {
            setMessages(prev => prev.map(msg =>
                msg.id === assistantMsgId ? { ...msg, content: `Error: ${error.message}` } : msg
            ));
        } finally {
            setIsLoading(false);
            setIsStreaming(false);
        }
    };

    return (
        <div className="h-[calc(100vh-8rem)] flex flex-col lg:flex-row gap-6">
            {/* Chat Area */}
            <div className="flex-1 flex flex-col bg-white rounded-xl border border-zinc-200 shadow-sm overflow-hidden">
                <div className="flex-1 overflow-y-auto p-6 space-y-6 bg-zinc-50/30">
                    {messages.length === 0 && (
                        <div className="h-full flex flex-col items-center justify-center text-zinc-400">
                            <Bot className="w-12 h-12 mb-4 opacity-20" />
                            <p className="text-sm font-medium">开始一段新的对话...</p>
                        </div>
                    )}
                    {messages.map((msg, idx) => (
                        <motion.div
                            key={idx}
                            initial={{ opacity: 0, y: 10 }}
                            animate={{ opacity: 1, y: 0 }}
                            className={cn(
                                "flex gap-4 max-w-3xl",
                                msg.role === 'user' ? "ml-auto flex-row-reverse" : ""
                            )}
                        >
                            <div className={cn(
                                "w-8 h-8 rounded-full flex items-center justify-center shrink-0 border",
                                msg.role === 'user'
                                    ? "bg-zinc-900 text-white border-zinc-900"
                                    : "bg-white text-zinc-600 border-zinc-200"
                            )}>
                                {msg.role === 'user' ? <User className="w-4 h-4" /> : <Bot className="w-4 h-4" />}
                            </div>
                            <div className={cn(
                                "p-4 rounded-2xl text-sm leading-relaxed shadow-sm border",
                                msg.role === 'user'
                                    ? "bg-zinc-900 text-white border-zinc-900 rounded-tr-none"
                                    : "bg-white border-zinc-200 text-zinc-800 rounded-tl-none"
                            )}>
                                <div className="whitespace-pre-wrap">{msg.content}</div>
                            </div>
                        </motion.div>
                    ))}
                    <div ref={messagesEndRef} />
                </div>

                <div className="p-4 bg-white border-t border-zinc-200">
                    <div className="flex gap-3">
                        <input
                            type="text"
                            value={input}
                            onChange={(e) => setInput(e.target.value)}
                            onKeyDown={(e) => e.key === 'Enter' && !e.shiftKey && handleSend()}
                            placeholder="输入消息..."
                            className="flex-1 px-4 py-3 bg-zinc-50 border border-zinc-200 rounded-xl focus:ring-2 focus:ring-zinc-900/5 focus:border-zinc-900 outline-none transition-all text-sm placeholder:text-zinc-400"
                        />
                        <button
                            onClick={handleSend}
                            disabled={isLoading || !input.trim()}
                            className="px-6 py-3 bg-zinc-900 hover:bg-zinc-800 text-white font-medium rounded-xl transition-colors disabled:opacity-50 shadow-sm flex items-center gap-2 text-sm"
                        >
                            <Send className="w-4 h-4" />
                            发送
                        </button>
                    </div>
                </div>
            </div>

            {/* Settings Sidebar */}
            <div className="w-full lg:w-80 flex flex-col gap-6">
                <div className="bg-white rounded-xl border border-zinc-200 p-6 shadow-sm space-y-6">
                    <h3 className="font-semibold text-zinc-900 flex items-center gap-2 text-base">
                        <Settings2 className="w-5 h-5" />
                        配置
                    </h3>

                    <div className="space-y-5">
                        <div>
                            <label className="block text-xs font-medium text-zinc-500 mb-1.5">模型</label>
                            <div className="flex gap-2">
                                <select
                                    value={selectedModel}
                                    onChange={(e) => setSelectedModel(e.target.value)}
                                    className="flex-1 px-3 py-2 bg-zinc-50 border border-zinc-200 rounded-lg text-sm focus:border-zinc-900 outline-none transition-all"
                                >
                                    {models.length === 0 && <option>加载中...</option>}
                                    {models.map(m => (
                                        <option key={m.id} value={m.id}>{m.id}</option>
                                    ))}
                                </select>
                                <button
                                    onClick={fetchModels}
                                    className="p-2 text-zinc-500 hover:text-zinc-900 hover:bg-zinc-100 rounded-lg transition-colors"
                                >
                                    <RefreshCw className="w-4 h-4" />
                                </button>
                            </div>
                        </div>

                        <div>
                            <label className="block text-xs font-medium text-zinc-500 mb-1.5">API Key</label>
                            <div className="relative">
                                <Key className="absolute left-3 top-2.5 w-4 h-4 text-zinc-400" />
                                <input
                                    type="password"
                                    value={apiKey}
                                    onChange={(e) => setApiKey(e.target.value)}
                                    placeholder="sk-..."
                                    className="w-full pl-9 pr-3 py-2 bg-zinc-50 border border-zinc-200 rounded-lg text-sm focus:border-zinc-900 outline-none transition-all placeholder:text-zinc-400"
                                />
                            </div>
                        </div>

                        <button
                            onClick={() => setMessages([])}
                            className="w-full py-2.5 flex items-center justify-center gap-2 text-red-600 hover:bg-red-50 border border-red-100 hover:border-red-200 rounded-lg transition-colors text-sm font-medium"
                        >
                            <Trash2 className="w-4 h-4" />
                            清空对话
                        </button>
                    </div>
                </div>
            </div>
        </div>
    );
}