File size: 7,775 Bytes
49e53ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client';

import { useState, useRef, useEffect } from 'react';
import { Send, Bot, User, Sparkles, Loader2, X, Maximize2, Minimize2 } from 'lucide-react';

interface Message {
  id: string;
  role: 'user' | 'assistant';
  content: string;
  timestamp: Date;
}

interface AIChatPanelProps {
  onClose?: () => void;
  isExpanded?: boolean;
  onToggleExpand?: () => void;
}

export default function AIChatPanel({ onClose, isExpanded, onToggleExpand }: AIChatPanelProps) {
  const [messages, setMessages] = useState<Message[]>([
    {
      id: '1',
      role: 'assistant',
      content: "Hello! I'm the QuantumShield AI Assistant. I can help you analyze transactions, understand fraud patterns, and explain how our quantum-enhanced detection works. What would you like to know?",
      timestamp: new Date()
    }
  ]);
  const [input, setInput] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const messagesEndRef = useRef<HTMLDivElement>(null);

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

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

  const sendMessage = async () => {
    if (!input.trim() || isLoading) return;

    const userMessage: Message = {
      id: Date.now().toString(),
      role: 'user',
      content: input,
      timestamp: new Date()
    };

    setMessages(prev => [...prev, userMessage]);
    setInput('');
    setIsLoading(true);

    try {
      const response = await fetch('http://localhost:8000/api/chat', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          message: input,
          conversation_history: messages.map(m => ({
            role: m.role,
            content: m.content
          }))
        })
      });

      if (!response.ok) throw new Error('Chat request failed');

      const data = await response.json();
      
      const assistantMessage: Message = {
        id: (Date.now() + 1).toString(),
        role: 'assistant',
        content: data.response,
        timestamp: new Date()
      };

      setMessages(prev => [...prev, assistantMessage]);
    } catch (error) {
      const errorMessage: Message = {
        id: (Date.now() + 1).toString(),
        role: 'assistant',
        content: "I'm having trouble connecting to the server. Please make sure the backend is running on port 8000.",
        timestamp: new Date()
      };
      setMessages(prev => [...prev, errorMessage]);
    } finally {
      setIsLoading(false);
    }
  };

  const handleKeyPress = (e: React.KeyboardEvent) => {
    if (e.key === 'Enter' && !e.shiftKey) {
      e.preventDefault();
      sendMessage();
    }
  };

  const suggestedQuestions = [
    "How does quantum detection work?",
    "What are VQC, QAOA, and QNN?",
    "Explain the fraud scoring system"
  ];

  return (
    <div className="card-dark rounded-3xl p-6 card-hover h-full flex flex-col">

      {/* Header */}

      <div className="flex items-center justify-between mb-4">

        <div className="flex items-center gap-3">

          <div className="w-10 h-10 rounded-xl bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center">

            <Sparkles className="w-5 h-5 text-white" />

          </div>

          <div>

            <h3 className="text-xl font-bold text-white">AI ASSISTANT</h3>

            <p className="text-xs text-white/60">Quantum Analysis Expert</p>

          </div>

        </div>

        <div className="flex items-center gap-2">

          {onToggleExpand && (

            <button 

              onClick={onToggleExpand}

              className="p-2 text-white/60 hover:text-white hover:bg-white/10 rounded-lg transition-colors"

            >

              {isExpanded ? <Minimize2 className="w-4 h-4" /> : <Maximize2 className="w-4 h-4" />}

            </button>

          )}

          {onClose && (

            <button 

              onClick={onClose}

              className="p-2 text-white/60 hover:text-white hover:bg-white/10 rounded-lg transition-colors"

            >

              <X className="w-4 h-4" />

            </button>

          )}

        </div>

      </div>



      {/* Messages */}

      <div className="flex-1 overflow-y-auto space-y-4 mb-4 min-h-[200px] max-h-[400px] scrollbar-thin pr-2">

        {messages.map((message) => (

          <div

            key={message.id}

            className={`flex gap-3 ${message.role === 'user' ? 'flex-row-reverse' : ''}`}

          >

            <div className={`w-8 h-8 rounded-lg flex items-center justify-center flex-shrink-0 ${

              message.role === 'user' 

                ? 'bg-blue-500' 

                : 'bg-gradient-to-br from-purple-500 to-pink-500'

            }`}>

              {message.role === 'user' ? (

                <User className="w-4 h-4 text-white" />

              ) : (

                <Bot className="w-4 h-4 text-white" />

              )}

            </div>

            <div className={`max-w-[80%] rounded-2xl px-4 py-3 ${

              message.role === 'user'

                ? 'bg-blue-500 text-white'

                : 'bg-white/10 text-white'

            }`}>

              <p className="text-sm whitespace-pre-wrap">{message.content}</p>

              <p className={`text-xs mt-1 ${message.role === 'user' ? 'text-blue-200' : 'text-white/40'}`}>

                {message.timestamp.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}

              </p>

            </div>

          </div>

        ))}

        

        {isLoading && (

          <div className="flex gap-3">

            <div className="w-8 h-8 rounded-lg bg-gradient-to-br from-purple-500 to-pink-500 flex items-center justify-center">

              <Bot className="w-4 h-4 text-white" />

            </div>

            <div className="bg-white/10 rounded-2xl px-4 py-3">

              <div className="flex items-center gap-2">

                <Loader2 className="w-4 h-4 text-white animate-spin" />

                <span className="text-sm text-white/60">Analyzing...</span>

              </div>

            </div>

          </div>

        )}

        

        <div ref={messagesEndRef} />

      </div>



      {/* Suggested Questions */}

      {messages.length <= 2 && (

        <div className="flex flex-wrap gap-2 mb-4">

          {suggestedQuestions.map((question, index) => (

            <button

              key={index}

              onClick={() => setInput(question)}

              className="text-xs px-3 py-1.5 bg-white/10 hover:bg-white/20 text-white/80 rounded-full transition-colors"

            >

              {question}

            </button>

          ))}

        </div>

      )}



      {/* Input */}

      <div className="flex gap-2">

        <input

          type="text"

          value={input}

          onChange={(e) => setInput(e.target.value)}

          onKeyPress={handleKeyPress}

          placeholder="Ask about fraud detection..."

          className="flex-1 bg-white/10 border-0 rounded-xl px-4 py-3 text-white placeholder-white/40 focus:ring-2 focus:ring-blue-500 focus:outline-none"

          disabled={isLoading}

        />

        <button

          onClick={sendMessage}

          disabled={!input.trim() || isLoading}

          className="px-4 py-3 bg-gradient-to-r from-blue-500 to-purple-600 rounded-xl text-white font-medium hover:opacity-90 transition-opacity disabled:opacity-50 disabled:cursor-not-allowed"

        >

          <Send className="w-5 h-5" />

        </button>

      </div>

    </div>
  );
}