-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
141 lines (124 loc) · 5.89 KB
/
script.js
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
document.addEventListener('DOMContentLoaded', () => {
const messagesContainer = document.querySelector('.messages-container');
const messageInput = document.querySelector('#chatInput');
const sendButton = document.querySelector('.send-button');
// Initialize Qt web channel
new QWebChannel(qt.webChannelTransport, function(channel) {
window.terminal = channel.objects.terminal;
});
// Store the current connection to disconnect it later
let currentConnection = null;
function addChatMessage(text, sender) {
const messageElement = document.createElement('div');
messageElement.classList.add('message', sender);
messageElement.textContent = text;
messagesContainer.appendChild(messageElement);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
function addExecutionLog(command) {
const logElement = document.createElement('div');
logElement.classList.add('message', 'execution-log');
logElement.innerHTML = `
<div class="log-header">
<div class="terminal-icon">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<rect x="2" y="4" width="20" height="16" rx="2" />
<path d="M6 8l4 4-4 4"/>
<line x1="12" y1="16" x2="18" y2="16"/>
</svg>
</div>
<span class="terminal-name">Terminal AI</span>
</div>
<div class="log-content">
<div class="timeline">
<div class="timeline-item">
<div class="timeline-icon active">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"/>
<path d="M12 8v4l3 3"/>
</svg>
</div>
<div class="timeline-content">
<div class="timeline-title">Received Command</div>
<code>${command}</code>
</div>
</div>
<div class="timeline-item">
<div class="timeline-icon active">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M5 12h14"/>
<path d="M12 5l7 7-7 7"/>
</svg>
</div>
<div class="timeline-content">
<div class="timeline-title">Executing</div>
<div class="timeline-text">Processing command in terminal</div>
</div>
</div>
<div class="timeline-item">
<div class="timeline-icon success">
<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M20 6L9 17l-5-5"/>
</svg>
</div>
<div class="timeline-content">
<div class="timeline-title success">Completed</div>
<div class="timeline-text">Command executed successfully</div>
</div>
</div>
</div>
</div>
`;
messagesContainer.appendChild(logElement);
messagesContainer.scrollTop = messagesContainer.scrollHeight;
}
async function handleChatSubmit(e) {
e.preventDefault();
const message = messageInput.value.trim();
if (message) {
addChatMessage(message, 'user');
messageInput.value = '';
messageInput.disabled = true;
addChatMessage("Processing...", 'assistant');
await new Promise(resolve => {
// Disconnect previous connection if exists
if (currentConnection) {
window.terminal.response_ready.disconnect(currentConnection);
}
// Create new connection handler
currentConnection = (command, speech) => {
// Find and remove the "Processing..." message
const processingMessage = Array.from(messagesContainer.getElementsByClassName('message')).find(
el => el.textContent === "Processing..."
);
if (processingMessage) {
messagesContainer.removeChild(processingMessage);
}
addChatMessage(speech, 'assistant');
if (command) {
addExecutionLog(command);
window.terminal.send_command(command);
}
resolve();
};
// Connect new handler
window.terminal.response_ready.connect(currentConnection);
// Process message
window.terminal.process_chat_message(message);
});
messageInput.disabled = false;
}
}
// Event listeners
sendButton.addEventListener('click', handleChatSubmit);
messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') handleChatSubmit(e);
});
// Add reset button functionality
const resetButton = document.querySelector('.reset-button');
resetButton.addEventListener('click', () => {
if (window.terminal) {
window.terminal.reset_terminal();
}
});
});