File size: 6,615 Bytes
ce1ab17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//
// SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
// SPDX-License-Identifier: Apache-2.0
//

// Prism.
Prism.plugins.autoloader.languages_path = 'https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/';

// WebSocket.
const socket = new WebSocket((window.location.protocol==="https:"?"wss":"ws")+"//"+window.location.host);

// UI elements.
const chatArea = document.getElementById('chatArea');
const chatBox = document.getElementById('chatBox');
const initialContent = document.getElementById('initialContent');
const form = document.getElementById('footerForm');
const input = document.getElementById('userInput');
const btn = document.getElementById('sendBtn');
const stopBtn = document.getElementById('stopBtn');
const promptItems = document.querySelectorAll('.prompt-item');
const mainHeader = document.getElementById('mainHeader');
const chatHeader = document.getElementById('chatHeader');
const homeBtn = document.getElementById('homeBtn');
const clearBtn = document.getElementById('clearBtn');

// Track state.
let streamMsg = null;
let conversationHistory = [];
let currentAssistantText = "";

// Render markdown content.
function renderMarkdown(el) {
  const raw = el.dataset.text || "";
  const html = marked.parse(raw, {
    gfm: true,
    breaks: true,
    smartLists: true,
    smartypants: false,
    headerIds: false
  });
  el.innerHTML = '<div class="md-content">' + html + '</div>';
  const wrapper = el.querySelector('.md-content');

  // Wrap tables.
  const tables = wrapper.querySelectorAll('table');
  tables.forEach(t => {
    if (t.parentNode && t.parentNode.classList && t.parentNode.classList.contains('table-wrapper')) return;
    const div = document.createElement('div');
    div.className = 'table-wrapper';
    t.parentNode.insertBefore(div, t);
    div.appendChild(t);
  });

  // Style horizontal rules.
  const hrs = wrapper.querySelectorAll('hr');
  hrs.forEach(h => {
    if (!h.classList.contains('styled-hr')) {
      h.classList.add('styled-hr');
    }
  });

  // Highlight code.
  Prism.highlightAllUnder(wrapper);
}

// Chat view.
function enterChatView() {
  mainHeader.style.display = 'none';
  chatHeader.style.display = 'flex';
  chatHeader.setAttribute('aria-hidden', 'false');
  chatBox.style.display = 'flex';
  initialContent.style.display = 'none';
}

// Home view.
function leaveChatView() {
  mainHeader.style.display = 'flex';
  chatHeader.style.display = 'none';
  chatHeader.setAttribute('aria-hidden', 'true');
  chatBox.style.display = 'none';
  initialContent.style.display = 'flex';
}

// Chat bubble.
function addMsg(who, text) {
  const div = document.createElement('div');
  div.className = 'bubble ' + (who==='user' ? 'bubble-user' : 'bubble-assist');
  div.dataset.text = text;
  renderMarkdown(div);
  chatBox.appendChild(div);
  chatBox.style.display='flex';
  chatBox.scrollTop = chatBox.scrollHeight;
  return div;
}

// Clear all chat.
function clearAllMessages() {
  try { socket.send(JSON.stringify({type:'stop'})); } catch {}
  conversationHistory = [];
  currentAssistantText = "";
  if(streamMsg) {
    const loadingEl = streamMsg.querySelector('.loading');
    if(loadingEl) loadingEl.remove();
    streamMsg = null;
  }
  chatBox.innerHTML = "";
  input.value = "";
  btn.disabled = true;
  stopBtn.style.display = 'none';
  btn.style.display = 'inline-flex';
  enterChatView();
}

// Wait for socket ready.
function sendWhenReady(msgFn) {
  if (socket.readyState === WebSocket.OPEN) {
    msgFn();
  } else {
    socket.addEventListener('open', function handler() {
      msgFn();
      socket.removeEventListener('open', handler);
    });
  }
}

// Prompts.
promptItems.forEach(p => {
  p.addEventListener('click', () => {
    input.value = p.dataset.prompt;
    sendWhenReady(submitMessage);
  });
});

// Send user message.
async function submitMessage() {
  const message = input.value.trim();
  if(!message) return;
  enterChatView();
  addMsg('user', message);
  conversationHistory.push({role: 'user', content: message});
  streamMsg = addMsg('assistant', '');
  const loadingEl = document.createElement('span');
  loadingEl.className = 'loading';
  streamMsg.appendChild(loadingEl);
  stopBtn.style.display = 'inline-flex';
  btn.style.display = 'none';
  input.value='';
  btn.disabled = true;
  try {
    socket.send(JSON.stringify({type:'ask', message, history: conversationHistory}));
  } catch (error) {
    if(streamMsg){ 
      const loadingEl = streamMsg.querySelector('.loading');
      if(loadingEl) loadingEl.remove();
      streamMsg.dataset.text = error.message || 'An error occurred during the request.';
      renderMarkdown(streamMsg);
      streamMsg.dataset.done='1'; 
      streamMsg=null; 
    }
    btn.style.display = 'inline-flex';
    stopBtn.style.display = 'none';
  }
}

// Submit.
form.addEventListener('submit', e => {
  e.preventDefault();
  submitMessage();
});

// Stop.
stopBtn.addEventListener('click', () => {
  stopBtn.style.pointerEvents = 'none';
  try { socket.send(JSON.stringify({type:'stop'})); } catch {}
});

// Home.
homeBtn.addEventListener('click', () => {
  leaveChatView();
});

// Clear messages.
clearBtn.addEventListener('click', () => {
  clearAllMessages();
});

// Socket messages.
socket.onmessage = (e) => {
  const data = JSON.parse(e.data);
  if(data.type==='chunk') {
    if(streamMsg) {
      const loadingEl = streamMsg.querySelector('.loading');
      if(loadingEl) loadingEl.remove();
      streamMsg.dataset.text += data.chunk;
      currentAssistantText = streamMsg.dataset.text ||"";
      renderMarkdown(streamMsg);
      chatBox.scrollTop = chatBox.scrollHeight;
    }
  } else if(data.type==='end' || data.type==='error') {
    if(streamMsg){
      const text = streamMsg.dataset.text || "";
      const loadingEl = streamMsg.querySelector('.loading');
      if(loadingEl) loadingEl.remove();
      streamMsg.dataset.done='1';
      if(data.type==='error') {
        streamMsg.dataset.text = data.error || 'An error occurred during the request.';
        renderMarkdown(streamMsg);
      } else {
        conversationHistory.push({role: 'assistant', content: text});
      }
      streamMsg = null;
    }
    btn.style.display = 'inline-flex';
    stopBtn.style.display = 'none';
    stopBtn.style.pointerEvents = 'auto';
  }
};

// Enable send button only if input has text.
input.addEventListener('input', () => {
  btn.disabled= input.value.trim() === '';
});

// Animations.
document.addEventListener('DOMContentLoaded', function() {
  AOS.init({
    duration: 800,
    easing: 'ease-out-cubic',
    once: true,
    offset: 50
  });
});