File size: 10,223 Bytes
e7586f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d20163
 
 
 
 
e7586f8
 
 
3d20163
e7586f8
3d20163
e7586f8
 
 
3d20163
 
 
 
 
 
 
 
 
 
 
e7586f8
 
 
3d20163
e7586f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client';

import React, { useState, useRef, useEffect } from 'react';
import { User, ChatMessage as ChatMessageType } from '@/lib/types';
import { api } from '@/lib/api';
import ChatMessage from './ChatMessage';
import { Send, Loader2, LogOut } from 'lucide-react';
import { ROLE_COLORS, COLLECTION_ICONS } from '@/lib/constants';

interface ChatInterfaceProps {
  user: User;
  onLogout: () => void;
  onAdminPanel: () => void;
}

export default function ChatInterface({ user, onLogout, onAdminPanel }: ChatInterfaceProps) {
  const [messages, setMessages] = useState<ChatMessageType[]>([]);
  const [input, setInput] = useState('');
  const [loading, setLoading] = useState(false);
  const [collections, setCollections] = useState<string[]>([]);
  const messagesEndRef = useRef<HTMLDivElement>(null);

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

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

  const loadCollections = async () => {
    try {
      const cols = await api.getCollections();
      setCollections(cols.map((c) => c.name));
    } catch (err) {
      console.error('Failed to load collections:', err);
    }
  };

  const handleSendMessage = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!input.trim()) return;

    // Add user message
    const userMessage: ChatMessageType = {
      id: Date.now().toString(),
      type: 'user',
      content: input,
      timestamp: new Date(),
    };

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

    try {
      const response = await api.chat({
        user_role: user.role,
        query: input,
        user_id: user.username,
      });

      // Guard against empty/blank responses from the backend
      const answerText = response.answer?.trim()
        ? response.answer
        : "I wasn't able to generate a response for your question. Please try rephrasing or ask a different question.";

      const assistantMessage: ChatMessageType = {
        id: (Date.now() + 1).toString(),
        type: 'assistant',
        content: answerText,
        timestamp: new Date(),
        response: { ...response, answer: answerText },
      };

      setMessages((prev) => [...prev, assistantMessage]);
    } catch (error: unknown) {
      // Extract a helpful message from the error if possible
      let errorText = 'Sorry, I encountered an error processing your query. Please try again in a moment.';
      if (error && typeof error === 'object' && 'response' in error) {
        const axiosError = error as { response?: { data?: { error?: string; detail?: string } } };
        const serverMsg = axiosError.response?.data?.detail || axiosError.response?.data?.error;
        if (serverMsg) {
          errorText = `Sorry, something went wrong: ${serverMsg}`;
        }
      }

      const errorMessage: ChatMessageType = {
        id: (Date.now() + 1).toString(),
        type: 'assistant',
        content: errorText,
        timestamp: new Date(),
      };
      setMessages((prev) => [...prev, errorMessage]);
      console.error('Chat error:', error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <div className="h-screen flex flex-col bg-gray-100">
      {/* Header */}
      <div className="bg-white border-b border-gray-200 px-6 py-4 flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold text-gray-900">FinBot</h1>
          <p className="text-sm text-gray-600">Advanced RAG with RBAC Enforcement</p>
        </div>
        <div className="flex items-center gap-4">
          <button
            onClick={onAdminPanel}
            className="px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-800 rounded-lg font-semibold transition-colors text-sm"
          >
            Admin Panel
          </button>
          <button
            onClick={onLogout}
            className="px-4 py-2 bg-red-500 hover:bg-red-600 text-white rounded-lg font-semibold transition-colors flex items-center gap-2"
          >
            <LogOut size={18} />
            Logout
          </button>
        </div>
      </div>

      <div className="flex flex-1 gap-4 p-4 min-h-0">
        {/* Sidebar */}
        <div className="w-64 bg-white rounded-lg border border-gray-200 p-4 flex flex-col gap-4 overflow-y-auto">
          {/* User Profile */}
          <div className="border-b pb-4">
            <h3 className="text-sm font-bold text-gray-700 mb-3">Your Profile</h3>
            <div className="space-y-2">
              <div>
                <p className="text-xs text-gray-600">Name</p>
                <p className="font-semibold text-gray-900">{user.name}</p>
              </div>
              <div>
                <p className="text-xs text-gray-600">Username</p>
                <p className="font-mono text-sm text-gray-700">@{user.username}</p>
              </div>
              <div>
                <p className="text-xs text-gray-600">Department</p>
                <p className="font-semibold text-gray-900">{user.department}</p>
              </div>
              <div>
                <span className={`inline-block px-3 py-1 rounded-full text-xs font-bold ${ROLE_COLORS[user.role]}`}>
                  {user.role}
                </span>
              </div>
            </div>
          </div>

          {/* Access Control */}
          <div className="border-b pb-4">
            <h3 className="text-sm font-bold text-gray-700 mb-3 flex items-center gap-2">
              πŸ” Your Access
            </h3>
            <div className="space-y-2">
              {(user.accessible_collections ?? []).map((collection) => (
                <div
                  key={collection}
                  className="flex items-center gap-2 bg-green-50 border border-green-200 rounded p-2"
                >
                  <span className="text-lg">{COLLECTION_ICONS[collection] || 'πŸ“'}</span>
                  <span className="text-sm font-semibold text-green-800">{collection}</span>
                </div>
              ))}
            </div>
            <div className="mt-3 p-2 bg-blue-50 border border-blue-200 rounded text-xs text-blue-700">
              <p className="font-semibold mb-1">Restricted Collections:</p>
              <ul className="space-y-1">
                {collections
                  .filter((c) => !(user.accessible_collections ?? []).includes(c))
                  .map((c) => (
                    <li key={c} className="flex items-center gap-1.5">
                      <span className="text-xs font-bold">🚫</span>
                      {c}
                    </li>
                  ))}
              </ul>
            </div>
          </div>

          {/* System Info */}
          <div className="text-xs text-gray-600 bg-gray-50 p-3 rounded border border-gray-200">
            <p className="font-bold mb-2">System Info</p>
            <ul className="space-y-1">
              <li>
                <strong>Backend:</strong> Running
              </li>
              <li>
                <strong>Collections:</strong> {collections.length}
              </li>
              <li>
                <strong>RBAC:</strong> Enforced
              </li>
            </ul>
          </div>
        </div>

        {/* Chat Area */}
        <div className="flex-1 bg-white rounded-lg border border-gray-200 flex flex-col min-h-0">
          {/* Messages */}
          <div className="flex-1 overflow-y-auto p-6 space-y-4">
            {messages.length === 0 && (
              <div className="h-full flex items-center justify-center">
                <div className="text-center">
                  <div className="text-6xl mb-4">πŸ’¬</div>
                  <p className="text-2xl font-bold text-gray-800 mb-2">Start a Conversation</p>
                  <p className="text-gray-600 max-w-sm">
                                Ask FinBot any questions about your company&apos;s business data. RBAC ensures you only see
                                information you&apos;re authorized to access.
                  </p>
                </div>
              </div>
            )}
            {messages.map((msg) => (
              <ChatMessage
                key={msg.id}
                type={msg.type}
                content={msg.content}
                timestamp={msg.timestamp}
                response={msg.response}
              />
            ))}
            {loading && (
              <div className="flex justify-start">
                <div className="bg-gray-100 rounded-2xl p-4 flex items-center gap-3">
                  <Loader2 className="animate-spin text-primary-600" size={20} />
                  <span className="text-gray-600">FinBot is thinking...</span>
                </div>
              </div>
            )}
            <div ref={messagesEndRef} />
          </div>

          {/* Input */}
          <div className="border-t border-gray-200 p-4">
            <form onSubmit={handleSendMessage} className="flex gap-3">
              <input
                type="text"
                value={input}
                onChange={(e) => setInput(e.target.value)}
                placeholder="Ask a question about your company..."
                disabled={loading}
                className="flex-1 px-4 py-3 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 disabled:opacity-50"
              />
              <button
                type="submit"
                disabled={loading || !input.trim()}
                className="px-6 py-3 bg-primary-600 hover:bg-primary-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded-lg font-semibold flex items-center gap-2 transition-colors"
              >
                {loading ? <Loader2 size={18} className="animate-spin" /> : <Send size={18} />}
                Send
              </button>
            </form>
            <p className="text-xs text-gray-500 mt-2">
              πŸ’‘ Tip: Try asking about different collections or testing RBAC by asking about restricted content.
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}