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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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// src/components/InferencePanel.tsx
import React, { useState, useEffect, useRef } from 'react';
import axios from 'axios';

const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';

// Define message types
interface Message {
  id: string;
  sender: 'user' | 'ai';
  text: string;
  domain?: string;
  confidence?: number;
  modelUsed?: string;
  sourceType?: string;
  thinkingProcess?: string;
  thinkingSteps?: string[];
  isEditing?: boolean;
  editedText?: string;
}

interface InferencePanelProps {
  activeDomain: string;
  currentSessionId?: string;
}

const InferencePanel: React.FC<InferencePanelProps> = ({ activeDomain }) => {
  const [sessionId] = useState(`ws-session-${Date.now()}-${Math.random()}`);
  const [socket, setSocket] = useState<WebSocket | null>(null);
  const [isConnected, setIsConnected] = useState(false);
  const [messages, setMessages] = useState<Message[]>([]);
  const [inputValue, setInputValue] = useState('');
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const [ragMode, setRagMode] = useState<'direct' | 'rag'>('rag');
  const [currentThinkingSteps, setCurrentThinkingSteps] = useState<string[]>([]);
  const [availableDomains, setAvailableDomains] = useState<any[]>([]);
  const [editingMessageId, setEditingMessageId] = useState<string | null>(null);
  const [editingText, setEditingText] = useState<string>('');

  const messagesEndRef = useRef<null | HTMLDivElement>(null);

  // Load available domains
  useEffect(() => {
    const fetchDomains = async () => {
      try {
        const response = await axios.get(`${API_URL}/api/config/domains`);
        setAvailableDomains(response.data);
      } catch (error) {
        console.error('Failed to fetch domains:', error);
      }
    };
    fetchDomains();
  }, []);

  // Effect to manage WebSocket connection
  useEffect(() => {
    const wsUrl = `ws://localhost:8000/api/questions/ws/${sessionId}`;
    const ws = new WebSocket(wsUrl);

    ws.onopen = () => {
      console.log('WebSocket connected');
      setIsConnected(true);
      setError(null);
    };

    ws.onmessage = (event) => {
      const data = JSON.parse(event.data);
      handleSocketMessage(data);
    };

    ws.onclose = () => {
      console.log('WebSocket disconnected');
      setIsConnected(false);
    };

    ws.onerror = (error) => {
      console.error('WebSocket error:', error);
      setIsConnected(false);
      setIsLoading(false);
      setError('Could not connect to the inference server.');
    };

    setSocket(ws);

    // Cleanup on unmount
    return () => {
      ws.close();
    };
  }, [sessionId]);

  // Effect to scroll to the bottom of messages
  useEffect(() => {
    messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
  }, [messages, currentThinkingSteps]);


  const handleSocketMessage = (data: any) => {
    switch (data.type) {
      case 'processing':
        setCurrentThinkingSteps(prev => [...prev, 'Processing...']);
        break;
      
      case 'thinking':
        // Update current thinking steps for real-time display
        setCurrentThinkingSteps(prev => [...prev, data.step]);
        break;

      case 'token':
        setMessages(prev => {
          const lastMessage = prev[prev.length - 1];
          if (lastMessage && lastMessage.sender === 'ai' && !lastMessage.sourceType) { // Ensure it's the current AI response
            // Append token to the last AI message
            const updatedMessages = [...prev];
            updatedMessages[prev.length - 1] = { ...lastMessage, text: lastMessage.text + data.content };
            return updatedMessages;
          } else {
            // Start a new AI message (should be rare, but good for robustness)
            return [...prev, {
              id: `ai-${Date.now()}-${Math.random()}`,
              sender: 'ai',
              text: data.content || '',
              domain: activeDomain
            }];
          }
        });
        break;
      
      case 'meta': // Metadata sent before or during streaming
        setMessages(prev => {
          const lastMessage = prev[prev.length - 1];
          if (lastMessage && lastMessage.sender === 'ai') {
            const updatedMessages = [...prev];
            updatedMessages[prev.length - 1] = { 
              ...lastMessage, 
              confidence: data.confidence,
              modelUsed: data.model_used,
              sourceType: data.source_type,
              thinkingProcess: data.thinking_process // Changed from data.thinking
            };
            return updatedMessages;
          }
          return prev;
        });
        break;

      case 'response': // Final complete answer
        setIsLoading(false);
        setCurrentThinkingSteps([]); // Clear real-time thinking steps
        setMessages(prev => {
          const lastMessage = prev[prev.length - 1];
          if (lastMessage && lastMessage.sender === 'ai') {
             const updatedMessages = [...prev];
             // Ensure final metadata is set
             updatedMessages[prev.length - 1] = {
               ...lastMessage,
               text: data.response, // Final complete response
               confidence: data.confidence || lastMessage.confidence,
               modelUsed: data.model_used || lastMessage.modelUsed,
               sourceType: data.source_type || lastMessage.sourceType,
               thinkingProcess: data.thinking_process || lastMessage.thinkingProcess, // Changed from data.thinking
               thinkingSteps: currentThinkingSteps // Attach all thinking steps to the final message
             };
             return updatedMessages;
          }
          // If no streaming tokens received, create a new message
          return [...prev, {
            id: `ai-${Date.now()}-${Math.random()}`,
            sender: 'ai',
            text: data.response,
            domain: activeDomain,
            confidence: data.confidence,
            modelUsed: data.model_used,
            sourceType: data.source_type,
            thinkingProcess: data.thinking_process, // Changed from data.thinking
            thinkingSteps: currentThinkingSteps
          }];
        });
        break;

      case 'error':
        setIsLoading(false);
        setCurrentThinkingSteps([]);
        setError(`WebSocket Error: ${data.error}`); // Set error state for prominent display
        break;
    }
  };

  const handleDomainChange = (messageId: string, newDomain: string) => {
    setMessages(prev => prev.map(msg =>
      msg.id === messageId ? { ...msg, domain: newDomain } : msg
    ));
  };

  const handleStartEdit = (msg: Message) => {
    setEditingMessageId(msg.id);
    setEditingText(msg.text);
  };

  const handleSaveEdit = (msgId: string) => {
    setMessages(prev => prev.map(msg =>
      msg.id === msgId ? { ...msg, text: editingText } : msg
    ));
    setEditingMessageId(null);
  };

  const handleCancelEdit = () => {
    setEditingMessageId(null);
    setEditingText('');
  };

  const handleSendMessage = () => {
    if (inputValue.trim() && socket && isConnected && !isLoading) {
      // Add user message to chat
      const newMessage: Message = {
        id: `msg-${Date.now()}-${Math.random()}`,
        sender: 'user',
        text: inputValue,
        domain: activeDomain
      };
      setMessages(prev => [...prev, newMessage]);

      // Send question to WebSocket server
      socket.send(JSON.stringify({
        type: 'question',
        question: inputValue,
        stream: true, // Always stream
        domain_id: activeDomain, // Use the active domain from props
        rag_mode: ragMode // Add the selected RAG mode
      }));

      setIsLoading(true);
      setInputValue('');
      setCurrentThinkingSteps([]); // Clear previous thinking steps display
    }
  };

  const getSourceTypeColor = (sourceType?: string) => {
    switch (sourceType) {
      case 'db_augmented': return 'bg-blue-600';
      case 'ai_internal_weights': return 'bg-purple-600';
      case 'web_search': return 'bg-yellow-600';
      default: return 'bg-gray-600';
    }
  };

  return (
    <div className="chat-container">
      <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', paddingBottom: '0.5rem' }}>
        <h3 style={{ margin: 0, color: '#00aaff' }}>Inference</h3>
        <span
          className={`connection-status ${isConnected ? 'connected' : 'disconnected'}`}
          style={{ fontSize: '0.75rem', padding: '0.2rem 0.5rem', borderRadius: '10px' }}
        >
          {isConnected ? '● Connected' : '○ Disconnected'}
        </span>
      </div>
      {error && (
        <div style={{ color: 'red', marginBottom: '1rem', padding: '0.5rem', backgroundColor: 'rgba(255,0,0,0.1)', borderRadius: '5px' }}>
          {error}
        </div>
      )}
      <div className="messages">
        {messages.map((msg) => (
          <div key={msg.id} className={`message ${msg.sender}`}>
            {editingMessageId === msg.id ? (
              <div>
                <textarea
                  value={editingText}
                  onChange={(e) => setEditingText(e.target.value)}
                  style={{
                    width: '100%',
                    minHeight: '100px',
                    padding: '8px',
                    backgroundColor: '#2a2a2a',
                    border: '1px solid #444',
                    borderRadius: '4px',
                    color: '#fff',
                    fontSize: '14px'
                  }}
                />
                <div style={{ marginTop: '8px', display: 'flex', gap: '8px' }}>
                  <button onClick={() => handleSaveEdit(msg.id)} style={{ padding: '4px 8px', fontSize: '12px' }}>保存</button>
                  <button onClick={handleCancelEdit} style={{ padding: '4px 8px', fontSize: '12px' }}>キャンセル</button>
                </div>
              </div>
            ) : (
              <div>
                {msg.text}
                {msg.sender === 'ai' && (
                  <div style={{ marginTop: '8px', display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' }}>
                    {msg.domain && availableDomains.length > 0 && (
                      <>
                        <span style={{ fontSize: '12px', color: '#888' }}>Domain:</span>
                        <select
                          value={msg.domain}
                          onChange={(e) => handleDomainChange(msg.id, e.target.value)}
                          style={{
                            padding: '4px 8px',
                            fontSize: '12px',
                            backgroundColor: '#2a2a2a',
                            border: '1px solid #444',
                            borderRadius: '4px',
                            color: '#fff'
                          }}
                        >
                          {availableDomains.map(domain => (
                            <option key={domain.domain_id} value={domain.domain_id}>
                              {domain.name}
                            </option>
                          ))}
                        </select>
                      </>
                    )}
                    <button
                      onClick={() => handleStartEdit(msg)}
                      style={{
                        padding: '4px 8px',
                        fontSize: '12px',
                        backgroundColor: '#444',
                        border: 'none',
                        borderRadius: '4px',
                        color: '#fff',
                        cursor: 'pointer'
                      }}
                    >
                      編集
                    </button>
                  </div>
                )}
                {msg.sender === 'ai' && msg.confidence !== undefined && (
                  <div className="response-meta text-xs mt-2 pt-2 border-t border-gray-700 flex flex-wrap gap-2">
                    {msg.confidence !== undefined && (
                      <span className="confidence-badge bg-green-600">
                        Confidence: {(msg.confidence * 100).toFixed(1)}%
                      </span>
                    )}
                    {msg.modelUsed && (
                      <span className="memory-badge bg-gray-600">
                        Model: {msg.modelUsed}
                      </span>
                    )}
                    {msg.sourceType && (
                      <span className={`memory-badge ${getSourceTypeColor(msg.sourceType)}`}>
                        Source: {msg.sourceType.replace('_', ' ')}
                      </span>
                    )}
                  </div>
                )}
                {msg.sender === 'ai' && msg.thinkingSteps && msg.thinkingSteps.length > 0 && (
                  <div className="thinking-steps mt-2 pt-2 border-t border-gray-700">
                    <div className="thinking-header text-xs text-gray-400">Thinking Process:</div>
                    {msg.thinkingSteps.map((step, stepIndex) => (
                      <div key={stepIndex} className="thinking-step text-xs text-gray-500 pl-4 border-l-2 border-blue-500 my-1">
                        {step}
                      </div>
                    ))}
                  </div>
                )}
              </div>
            )}
          </div>
        ))}
        {/* Real-time thinking steps display */}
        {isLoading && currentThinkingSteps.length > 0 && (
          <div className="thinking-steps" style={{ background: '#2a2a2a', border: '1px solid #444', borderRadius: '6px', padding: '1rem', marginBottom: '1rem' }}>
            <div className="thinking-header" style={{ color: '#888', fontSize: '0.85rem', marginBottom: '0.5rem' }}>Thinking... <span className="loading-dots"><span>.</span><span>.</span><span>.</span></span></div>
            {currentThinkingSteps.map((step, i) => (
              <div key={i} className="thinking-step" style={{ color: '#aaa', fontSize: '0.85rem', padding: '0.25rem 0', paddingLeft: '1rem', borderLeft: '2px solid #007acc', marginBottom: '0.25rem' }}>{step}</div>
            ))}
          </div>
        )}
        <div ref={messagesEndRef} />
      </div>

      <div className="rag-mode-selector" style={{ display: 'flex', justifyContent: 'center', alignItems: 'center', marginBottom: '0.5rem', gap: '0.5rem' }}>
        <span style={{ fontSize: '0.8rem', color: '#ccc' }}>Query Mode:</span>
        <div style={{ display: 'flex', border: '1px solid #555', borderRadius: '15px', padding: '2px' }}>
          <button 
            onClick={() => setRagMode('direct')}
            className={`rag-mode-btn ${ragMode === 'direct' ? 'active' : ''}`}
            disabled={isLoading}
          >
            Direct DB
          </button>
          <button 
            onClick={() => setRagMode('rag')}
            className={`rag-mode-btn ${ragMode === 'rag' ? 'active' : ''}`}
            disabled={isLoading}
          >
            RAG Mode
          </button>
        </div>
      </div>

      <div className="input-area">
        <input
          type="text"
          placeholder="Ask NullAI anything..."
          value={inputValue}
          onChange={(e) => setInputValue(e.target.value)}
          onKeyPress={(e) => e.key === 'Enter' && handleSendMessage()}
          disabled={isLoading || !isConnected}
        />
        <button onClick={handleSendMessage} disabled={isLoading || !isConnected}>
          {!isConnected ? 'Connect.' : (isLoading ? 'Generating...' : 'Send')}
        </button>
      </div>
    </div>
  );
};

export default InferencePanel;