File size: 6,705 Bytes
91994bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import React, { useState, useEffect, useRef } from 'react';
import axios from 'axios';

interface ChatInterfaceProps {
    onCommand: (cmd: { task: string; type: '3d' | 'pcb' }) => void;
}

const ChatInterface: React.FC<ChatInterfaceProps> = ({ onCommand: _onCommand }) => {
    const [messages, setMessages] = useState([
        { role: 'assistant', content: 'Welcome to QuLab Infinite. I am ECH0, your R&D Navigator. What are we building today?' }
    ]);
    const [input, setInput] = useState('');
    const [isLoading, setIsLoading] = useState(false);
    const endRef = useRef<HTMLDivElement>(null);

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

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

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

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

        try {
            const res = await axios.post('http://localhost:8000/chat', { message: newMsg.content });
            const data = res.data;
            const assistantMsg = { role: 'assistant', content: data.response };

            setMessages(prev => [...prev, assistantMsg]);

            if (data.action_result) {
                // If ECH0 performed a simulation, we can log it or show it.
                // For now, we print it as a system message.
                const sysMsg = {
                    role: 'system',
                    content: `[SIMULATION OUTPUT]: ${JSON.stringify(data.action_result.products)} produced. ${data.action_result.observations[0]}`
                };
                setMessages(prev => [...prev, sysMsg]);

                // If it was a PCB/3D task (legacy check), we could still trigger onCommand
                // But generally ECH0Service handles universal lab now.
                // We'll leave the prop just in case we want to force UI switches, 
                // but for now relying on the chat response is cleaner.
            }

        } catch (error) {
            console.error("Chat Error", error);
            setMessages(prev => [...prev, { role: 'assistant', content: "Communication Application Error. Is the backend or Ollama online?" }]);
        } finally {
            setIsLoading(false);
        }
    };

    const handleKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === 'Enter') handleSend();
    };

    return (
        <div className="glass-panel" style={{
            width: '90%',
            maxWidth: '800px',
            height: '80vh',
            display: 'flex',
            flexDirection: 'column',
            overflow: 'hidden'
        }}>
            <div style={{
                padding: '1.5rem',
                background: 'rgba(0, 240, 255, 0.05)',
                borderBottom: '1px solid var(--glass-border)',
                display: 'flex',
                alignItems: 'center',
                gap: '1rem'
            }}>
                <div style={{
                    width: '40px', height: '40px', borderRadius: '50%',
                    background: 'var(--accent-cyan)',
                    boxShadow: '0 0 15px var(--accent-cyan)',
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                    fontWeight: 'bold', fontSize: '1.2rem', color: '#000'
                }}>
                    AI
                </div>
                <div>
                    <h2 style={{ fontSize: '1.2rem' }}>ECH0 COMMAND</h2>
                    <span style={{ fontSize: '0.8rem', color: isLoading ? 'var(--accent-purple)' : 'var(--accent-green)' }}>
                        ● {isLoading ? 'THINKING...' : 'ONLINE'}
                    </span>
                </div>
            </div>

            <div style={{ flexGrow: 1, padding: '1.5rem', overflowY: 'auto', display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
                {messages.map((msg, i) => (
                    <div key={i} style={{
                        display: 'flex',
                        justifyContent: msg.role === 'user' ? 'flex-end' : 'flex-start'
                    }}>
                        <div style={{
                            maxWidth: '70%',
                            padding: '1rem',
                            borderRadius: '12px',
                            background: msg.role === 'user' ? 'linear-gradient(135deg, var(--accent-purple), #8800ff)' : (msg.role === 'system' ? 'rgba(255,100,0,0.2)' : 'var(--bg-tertiary)'),
                            border: msg.role === 'assistant' ? '1px solid var(--glass-border)' : (msg.role === 'system' ? '1px dashed var(--accent-cyan)' : 'none'),
                            color: msg.role === 'system' ? 'var(--accent-cyan)' : 'white',
                            boxShadow: '0 4px 10px rgba(0,0,0,0.2)',
                            fontFamily: msg.role === 'system' ? 'var(--font-mono)' : 'inherit'
                        }}>
                            {msg.content}
                        </div>
                    </div>
                ))}
                <div ref={endRef} />
            </div>

            <div style={{ padding: '1.5rem', background: 'rgba(0,0,0,0.2)' }}>
                <div style={{
                    display: 'flex',
                    background: 'var(--bg-secondary)',
                    border: '1px solid var(--glass-border)',
                    borderRadius: '8px',
                    padding: '0.5rem'
                }}>
                    <input
                        type="text"
                        value={input}
                        onChange={(e) => setInput(e.target.value)}
                        onKeyDown={handleKeyDown}
                        placeholder="Type a command (e.g., 'Combine Sodium and Water')..."
                        disabled={isLoading}
                        style={{
                            flexGrow: 1,
                            background: 'transparent',
                            border: 'none',
                            color: 'white',
                            padding: '0.5rem',
                            outline: 'none',
                            fontFamily: 'var(--font-main)',
                            fontSize: '1rem',
                            opacity: isLoading ? 0.5 : 1
                        }}
                    />
                    <button onClick={handleSend} style={{ width: 'auto', padding: '0 1.5rem' }} disabled={isLoading}>SEND</button>
                </div>
            </div>
        </div>
    );
};


export default ChatInterface;