File size: 9,842 Bytes
6cdce85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
'use client';

import { useState, useRef, useEffect, useCallback } from 'react';
import { Trash2 } from 'lucide-react';
import { Message } from './Message';
import { MessageInput, MessageInputRef } from './MessageInput';
import { QubitIcon } from './QubitIcon';
import { LoadingStatus } from './LoadingStatus';
import { SYSTEM_PROMPT } from '@/config/constants';
import { resizeImageForInference, fetchAndResizeImage } from '@/lib/utils/image';
import { postProcessResponse } from '@/lib/utils/response';
import type { Message as MessageType, DatasetExample } from '@/types';

interface ChatInterfaceProps {
  selectedExample?: DatasetExample | null;
  onExampleUsed?: () => void;
}

export function ChatInterface({ selectedExample, onExampleUsed }: ChatInterfaceProps) {
  const [messages, setMessages] = useState<MessageType[]>([]);
  const [isLoading, setIsLoading] = useState(false);
  const [hasStartedStreaming, setHasStartedStreaming] = useState(false);
  const messagesEndRef = useRef<HTMLDivElement>(null);
  const inputRef = useRef<MessageInputRef>(null);
  const processedExampleRef = useRef<string | null>(null);
  const abortControllerRef = useRef<AbortController | null>(null);

  const scrollToBottom = useCallback(() => {
    messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, []);

  useEffect(() => {
    scrollToBottom();
  }, [messages, scrollToBottom]);

  useEffect(() => {
    if (
      selectedExample &&
      selectedExample.id !== processedExampleRef.current
    ) {
      processedExampleRef.current = selectedExample.id;
      inputRef.current?.setContent(
        selectedExample.question,
        selectedExample.imageUrl
      );
      onExampleUsed?.();
    }
  }, [selectedExample, onExampleUsed]);

  const handleSendMessage = async (
    content: string,
    imageUrl?: string,
    imageBase64?: string
  ) => {
    if (!content.trim() && !imageUrl && !imageBase64) return;

    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
    }
    abortControllerRef.current = new AbortController();

    const userMessage: MessageType = {
      id: crypto.randomUUID(),
      role: 'user',
      content,
      imageUrl,
      imageBase64,
      timestamp: new Date(),
    };

    const assistantMessageId = crypto.randomUUID();
    const loadingMessage: MessageType = {
      id: assistantMessageId,
      role: 'assistant',
      content: '',
      timestamp: new Date(),
      isLoading: true,
    };

    setMessages((prev) => [...prev, userMessage, loadingMessage]);
    setIsLoading(true);
    setHasStartedStreaming(false);

    try {
      let imageData: string | undefined;

      if (imageBase64) {
        try {
          imageData = await resizeImageForInference(`data:image/jpeg;base64,${imageBase64}`);
        } catch (e) {
          console.error('Failed to resize image:', e);
          imageData = imageBase64;
        }
      } else if (imageUrl) {
        try {
          imageData = await fetchAndResizeImage(imageUrl);
        } catch (e) {
          console.error('Failed to fetch and resize image:', e);
        }
      }

      const userContent = imageData
        ? [
          { type: 'text', text: content },
          { type: 'image_url', image_url: { url: `data:image/jpeg;base64,${imageData}` } },
        ]
        : content;

      const response = await fetch('/api/chat', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          messages: [
            { role: 'system', content: SYSTEM_PROMPT },
            ...messages.map((m) => ({
              role: m.role,
              content: m.content,
            })),
            { role: 'user', content: userContent },
          ],
          stream: true,
        }),
        signal: abortControllerRef.current.signal,
      });

      if (!response.ok) {
        const data = await response.json();
        throw new Error(data.error || 'Request failed');
      }

      const reader = response.body?.getReader();
      if (!reader) {
        throw new Error('No response body');
      }

      const decoder = new TextDecoder();
      let buffer = '';
      let fullContent = '';

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

        buffer += decoder.decode(value, { stream: true });
        const lines = buffer.split('\n');
        buffer = lines.pop() || '';

        for (const line of lines) {
          const trimmed = line.trim();
          if (!trimmed || !trimmed.startsWith('data: ')) continue;

          const jsonStr = trimmed.slice(6);
          try {
            const data = JSON.parse(jsonStr);

            if (data.error) {
              throw new Error(data.error);
            }

            if (data.content) {
              // First content received - streaming has started
              if (fullContent === '') {
                setHasStartedStreaming(true);
                setMessages((prev) =>
                  prev.map((m) =>
                    m.id === assistantMessageId ? { ...m, isLoading: false } : m
                  )
                );
              }
              
              fullContent += data.content;
              const processedContent = postProcessResponse(fullContent);
              setMessages((prev) =>
                prev.map((m) =>
                  m.id === assistantMessageId
                    ? { ...m, content: processedContent }
                    : m
                )
              );
            }
          } catch (e) {
            if (e instanceof SyntaxError) continue;
            throw e;
          }
        }
      }

      const finalContent = postProcessResponse(fullContent);
      setMessages((prev) =>
        prev.map((m) =>
          m.id === assistantMessageId
            ? { ...m, content: finalContent }
            : m
        )
      );
    } catch (error) {
      if ((error as Error).name === 'AbortError') {
        return;
      }

      setMessages((prev) =>
        prev.map((m) =>
          m.id === assistantMessageId
            ? {
              ...m,
              content: `Error: ${error instanceof Error ? error.message : 'Failed to get response'}`,
              isLoading: false,
            }
            : m
        )
      );
    } finally {
      setIsLoading(false);
      setHasStartedStreaming(false);
      abortControllerRef.current = null;
    }
  };

  const handleClearChat = () => {
    if (abortControllerRef.current) {
      abortControllerRef.current.abort();
    }
    setMessages([]);
    inputRef.current?.clear();
    processedExampleRef.current = null;
  };

  const handleCopyCode = (code: string) => {
    console.log('Code copied:', code.substring(0, 50) + '...');
  };

  return (
    <div className="flex flex-col h-full">
      {messages.length > 0 && (
        <div className="flex justify-end px-4 pt-2">
          <button
            onClick={handleClearChat}
            className="flex items-center gap-1.5 px-3 py-1.5 text-xs text-zinc-500 
                       hover:text-zinc-300 hover:bg-zinc-800/50 rounded-md transition-colors"
          >
            <Trash2 className="w-3.5 h-3.5" />
            Clear chat
          </button>
        </div>
      )}

      <div className="flex-1 overflow-y-auto p-4 space-y-6">
        {messages.length === 0 ? (
          <div className="flex flex-col items-center justify-center h-full text-center px-4">
            <div className="w-16 h-16 mb-5 rounded-xl bg-zinc-800/80 border border-teal-700/30 flex items-center justify-center">
              <QubitIcon size={32} className="text-teal-400" />
            </div>
            <h2 className="text-xl font-semibold text-zinc-200 mb-2">
              Quantum Assistant
            </h2>
            <p className="text-zinc-500 max-w-md mb-8 text-sm leading-relaxed">
              Ask questions about quantum computing, generate Qiskit code, or upload circuit diagrams for analysis.
            </p>
            <div className="grid grid-cols-1 sm:grid-cols-3 gap-3 w-full max-w-xl">
              {[
                { label: 'Circuits', text: 'Create a Bell state circuit' },
                { label: 'Concepts', text: 'Explain Bloch sphere representation' },
                { label: 'Algorithms', text: 'Implement VQE algorithm' },
              ].map((suggestion, i) => (
                <button
                  key={i}
                  onClick={() => inputRef.current?.setContent(suggestion.text)}
                  className="bg-zinc-800/60 hover:bg-zinc-800 border border-zinc-700/50 hover:border-zinc-600/50 rounded-lg p-4 text-left group transition-all"
                >
                  <span className="text-[10px] font-mono text-teal-500/80 mb-2 block uppercase tracking-wider">
                    {suggestion.label}
                  </span>
                  <span className="text-sm text-zinc-400 group-hover:text-zinc-200 transition-colors">
                    {suggestion.text}
                  </span>
                </button>
              ))}
            </div>
          </div>
        ) : (
          messages.map((message) => (
            <Message
              key={message.id}
              message={message}
              onCopyCode={handleCopyCode}
              loadingStatus={
                message.isLoading ? (
                  <LoadingStatus
                    isLoading={isLoading}
                    hasStartedStreaming={hasStartedStreaming}
                  />
                ) : undefined
              }
            />
          ))
        )}
        <div ref={messagesEndRef} />
      </div>

      <div className="p-4 border-t border-zinc-800/80">
        <MessageInput ref={inputRef} onSend={handleSendMessage} isLoading={isLoading} />
      </div>
    </div>
  );
}