WebashalarForML commited on
Commit
ff85bff
·
verified ·
1 Parent(s): 7a6087c

Update static/frontend.html

Browse files
Files changed (1) hide show
  1. static/frontend.html +74 -228
static/frontend.html CHANGED
@@ -5,149 +5,109 @@
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Code Assistant Agent</title>
7
  <style>
8
- body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 20px; background-color: #f0f2f5; color: #333; }
9
- .container { max-width: 850px; margin: auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1); }
10
- h1 { color: #1f7cff; text-align: center; margin-bottom: 25px; font-weight: 600; }
11
- #chat-window {
12
- height: 400px;
13
- overflow-y: scroll;
14
- border: 1px solid #ddd;
15
- padding: 15px;
16
- border-radius: 8px;
17
- margin-bottom: 20px;
18
- background-color: #e9eff4;
19
- }
20
-
21
- /* NEW STRUCTURE FOR CHAT BUBBLES */
22
- .message-row {
23
- display: flex;
24
- margin-bottom: 15px;
25
- }
26
- .message {
27
- padding: 10px 15px;
28
- border-radius: 18px;
29
- max-width: 75%;
30
- position: relative;
31
- word-wrap: break-word;
32
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
33
- }
34
- .user {
35
- background-color: #1f7cff; /* Bright blue for user */
36
- color: white;
37
- margin-left: auto; /* Pushes to the right */
38
- border-bottom-right-radius: 4px; /* Sharper corner on user's side */
39
- }
40
- .assistant {
41
- background-color: #ffffff;
42
- border: 1px solid #c9d6de;
43
- color: #333;
44
- margin-right: auto; /* Keeps it on the left */
45
- border-bottom-left-radius: 4px;
46
- text-align: left;
47
- }
48
-
49
- #input-container { display: flex; gap: 10px; margin-bottom: 20px; }
50
- #user-input { flex-grow: 1; padding: 12px; border: 1px solid #ccc; border-radius: 6px; }
51
- #send-button { padding: 10px 20px; background-color: #1f7cff; color: white; border: none; border-radius: 6px; cursor: pointer; transition: background-color 0.3s; }
52
- #send-button:hover { background-color: #165bb0; }
53
-
54
- #state-info { padding: 15px; background-color: #e6f0ff; border-left: 5px solid #1f7cff; border-radius: 4px; font-size: 0.9em; margin-bottom: 25px; display: flex; justify-content: space-between; align-items: center; }
55
-
56
- pre {
57
- background-color: #272822;
58
- color: #f8f8f2;
59
- padding: 10px;
60
- border-radius: 4px;
61
- overflow-x: auto;
62
- margin-top: 10px;
63
- font-family: 'Consolas', 'Monaco', monospace;
64
- font-size: 0.9em;
65
- }
66
-
67
- .tag-area {
68
- margin-top: 10px;
69
- padding-top: 5px;
70
- border-top: 1px dashed #e0e0e0;
71
- font-size: 0.8em;
72
- display: flex;
73
- align-items: center;
74
- flex-wrap: wrap;
75
- }
76
- .tag-area input[type="text"] {
77
- flex-grow: 1;
78
- padding: 4px;
79
- border: 1px solid #ccc;
80
- border-radius: 4px;
81
- margin-right: 5px;
82
- min-width: 150px;
83
- }
84
 
85
- .tag-chip { display: inline-block; background-color: #6c757d; color: white; padding: 3px 10px; border-radius: 16px; font-size: 0.75em; margin-right: 5px; margin-bottom: 5px; }
86
- .bookmark-btn { background-color: #28a745; color: white; border: none; padding: 6px 12px; border-radius: 4px; cursor: pointer; transition: background-color 0.3s; font-size: 0.8em; }
87
- .bookmark-btn:hover { background-color: #1e7e34; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- /* Bookmarked Replies Styling */
90
- h2 { border-bottom: 2px solid #ddd; padding-bottom: 5px; margin-top: 30px; }
91
- #tag-search { padding: 10px; border: 1px solid #ccc; border-radius: 4px; width: 100%; margin-bottom: 15px; }
92
- .tagged-replies-list { list-style: none; padding: 0; }
93
- .tagged-replies-list li { border: 1px solid #f0f0f0; background: #fafafa; padding: 15px; border-radius: 6px; margin-bottom: 10px; }
94
- .tagged-replies-list li p { margin: 0 0 5px 0; }
95
- </style>
 
 
 
 
 
 
 
 
 
 
 
96
  </head>
97
  <body>
98
  <div class="container">
99
  <h1>Code Assistant Agent 🤖</h1>
100
 
101
  <div id="state-info">
102
- Current Language: <span id="current-language">Python</span>
103
- | Total Bookmarked Replies: <span id="tagged-count">0</span>
 
 
 
 
 
 
 
 
 
 
 
104
  </div>
105
 
106
- <div id="chat-window">
107
- </div>
108
 
109
  <div id="input-container">
110
  <input type="text" id="user-input" placeholder="Ask about code, concepts, or bugs..." onkeypress="if(event.key === 'Enter') sendMessage()">
111
  <button id="send-button" onclick="sendMessage()">Send</button>
112
  </div>
113
-
114
- <h2>Bookmarked Replies</h2>
115
- <input type="text" id="tag-search" placeholder="Search by Tag (e.g., Python, Debugging)" oninput="filterReplies()">
116
- <ul id="tagged-replies-display" class="tagged-replies-list">
117
- </ul>
118
  </div>
119
 
120
  <script>
121
- // Store the full conversation history (Crucial for context)
122
  let chatHistory = [];
123
-
124
- // Initial state structure matching the backend
125
  let assistantState = {
126
  conversationSummary: "",
127
- language: "Python",
128
- taggedReplies: []
129
  };
130
  const apiUrl = '/chat';
131
- const tagUrl = '/tag_reply';
132
 
133
  document.addEventListener('DOMContentLoaded', () => {
134
- updateStateInfo();
135
- // Initial greeting (display only, not pushed to chatHistory)
136
  appendMessage({ role: 'assistant', content: "Hello! I'm your Code Assistant. What programming language are you working in today?" });
137
  });
138
 
139
- // --- Core Chat Logic ---
140
-
141
  async function sendMessage() {
142
  const inputElement = document.getElementById('user-input');
143
  const userMessage = inputElement.value.trim();
144
-
145
  if (!userMessage) return;
146
 
147
- // 1. Capture and add the user message to history
148
  const userMsgObj = { role: 'user', content: userMessage };
149
  chatHistory.push(userMsgObj);
150
-
151
  appendMessage(userMsgObj);
152
  inputElement.value = '';
153
 
@@ -156,38 +116,22 @@
156
  method: 'POST',
157
  headers: { 'Content-Type': 'application/json' },
158
  body: JSON.stringify({
159
- // CRUCIAL FIX: Send the complete chatHistory array
160
  chat_history: chatHistory,
161
  assistant_state: assistantState
162
  })
163
  });
164
 
165
  if (!response.ok) {
166
- // Remove the user message from history if the request failed
167
  chatHistory.pop();
168
  throw new Error(`HTTP error! status: ${response.status}`);
169
  }
170
 
171
  const data = await response.json();
172
-
173
- // 2. Update state
174
  assistantState = data.updated_state;
175
 
176
- // 3. Capture and add the assistant reply to history
177
- const assistantMsgObj = {
178
- role: 'assistant',
179
- content: data.assistant_reply
180
- };
181
- chatHistory.push(assistantMsgObj); // <-- Add assistant's reply to history
182
-
183
- // 4. Display the reply
184
- appendMessage({
185
- role: 'assistant',
186
- content: data.assistant_reply,
187
- suggestedTags: data.suggested_tags
188
- });
189
-
190
- updateStateInfo();
191
 
192
  } catch (error) {
193
  console.error('Chat failed:', error);
@@ -195,8 +139,6 @@
195
  }
196
  }
197
 
198
- // --- Display and State Management ---
199
-
200
  function appendMessage(msg) {
201
  const chatWindow = document.getElementById('chat-window');
202
  const messageRow = document.createElement('div');
@@ -206,8 +148,6 @@
206
  msgDiv.className = `message ${msg.role}`;
207
 
208
  let content = msg.content;
209
-
210
- // Basic Markdown for code blocks in the frontend
211
  content = content.replace(/```(\w+)?\n([\s\S]*?)\n```/g, (match, lang, code) => {
212
  const language = lang || 'code';
213
  return `<pre><code class="language-${language}">${code.trim()}</code></pre>`;
@@ -216,106 +156,12 @@
216
  msgDiv.innerHTML = content;
217
  messageRow.appendChild(msgDiv);
218
  chatWindow.appendChild(messageRow);
219
-
220
-
221
- // Add Tagging UI for assistant replies
222
- if (msg.role === 'assistant') {
223
- const tagArea = document.createElement('div');
224
- tagArea.className = 'tag-area';
225
- tagArea.innerHTML = 'Tags: ';
226
-
227
- const tagInput = document.createElement('input');
228
- tagInput.type = 'text';
229
- tagInput.placeholder = 'Add your tags (comma-separated)';
230
- tagInput.id = 'tag-input-' + Date.now();
231
- tagInput.style.marginRight = '10px';
232
- tagInput.value = (msg.suggestedTags || []).join(', '); // Pre-populate with suggested tags
233
-
234
- const bookmarkBtn = document.createElement('button');
235
- bookmarkBtn.className = 'bookmark-btn';
236
- bookmarkBtn.textContent = 'Save/Bookmark';
237
- bookmarkBtn.onclick = () => saveReply(msg.content, tagInput.value, bookmarkBtn);
238
-
239
- tagArea.appendChild(tagInput);
240
- tagArea.appendChild(bookmarkBtn);
241
- msgDiv.appendChild(tagArea);
242
- }
243
-
244
  chatWindow.scrollTop = chatWindow.scrollHeight;
245
  }
246
 
247
- function updateStateInfo() {
248
- document.getElementById('current-language').textContent = assistantState.language;
249
- document.getElementById('tagged-count').textContent = assistantState.taggedReplies.length;
250
- filterReplies(); // Refresh the bookmark list
251
- }
252
-
253
- // --- Bookmarking and Tagging Logic ---
254
-
255
- async function saveReply(replyContent, tagsString, buttonElement) {
256
- const tags = tagsString.split(',').map(tag => tag.trim()).filter(tag => tag.length > 0);
257
-
258
- if (tags.length === 0) {
259
- alert("Please enter at least one tag.");
260
- return;
261
- }
262
-
263
- try {
264
- const response = await fetch(tagUrl, {
265
- method: 'POST',
266
- headers: { 'Content-Type': 'application/json' },
267
- body: JSON.stringify({
268
- reply: replyContent,
269
- tags: tags,
270
- assistant_state: assistantState
271
- })
272
- });
273
-
274
- if (!response.ok) {
275
- throw new Error(`HTTP error! status: ${response.status}`);
276
- }
277
-
278
- const data = await response.json();
279
- assistantState = data.updated_state; // Update state with the new tagged reply
280
-
281
- alert(data.message);
282
- buttonElement.textContent = 'Saved!';
283
- buttonElement.disabled = true;
284
- updateStateInfo();
285
-
286
- } catch (error) {
287
- console.error('Tagging failed:', error);
288
- alert(`Failed to save reply: ${error.message}`);
289
- }
290
- }
291
-
292
- function filterReplies() {
293
- const query = document.getElementById('tag-search').value.toLowerCase();
294
- const displayList = document.getElementById('tagged-replies-display');
295
- displayList.innerHTML = '';
296
-
297
- const filtered = assistantState.taggedReplies.filter(item => {
298
- if (!query) return true;
299
- return item.tags.some(tag => tag.toLowerCase().includes(query));
300
- });
301
-
302
- if (filtered.length === 0 && query) {
303
- displayList.innerHTML = '<li>No bookmarked replies match your search tag.</li>';
304
- return;
305
- }
306
-
307
- filtered.forEach(item => {
308
- const li = document.createElement('li');
309
- let tagChips = item.tags.map(tag => `<span class="tag-chip">${tag}</span>`).join('');
310
-
311
- // Show a truncated reply and tags
312
- li.innerHTML = `
313
- <p style="font-weight: bold;">${item.reply.substring(0, 150)}...</p>
314
- <p style="font-size: 0.9em;">${tagChips}</p>
315
- `;
316
- displayList.appendChild(li);
317
- });
318
  }
319
  </script>
320
  </body>
321
- </html>
 
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Code Assistant Agent</title>
7
  <style>
8
+ body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 20px; background-color: #f0f2f5; color: #333; }
9
+ .container { max-width: 850px; margin: auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1); }
10
+ h1 { color: #1f7cff; text-align: center; margin-bottom: 25px; font-weight: 600; }
11
+ #chat-window {
12
+ height: 400px;
13
+ overflow-y: scroll;
14
+ border: 1px solid #ddd;
15
+ padding: 15px;
16
+ border-radius: 8px;
17
+ margin-bottom: 20px;
18
+ background-color: #e9eff4;
19
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ .message-row { display: flex; margin-bottom: 15px; }
22
+ .message {
23
+ padding: 10px 15px;
24
+ border-radius: 18px;
25
+ max-width: 75%;
26
+ position: relative;
27
+ word-wrap: break-word;
28
+ box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
29
+ }
30
+ .user {
31
+ background-color: #1f7cff;
32
+ color: white;
33
+ margin-left: auto;
34
+ border-bottom-right-radius: 4px;
35
+ }
36
+ .assistant {
37
+ background-color: #ffffff;
38
+ border: 1px solid #c9d6de;
39
+ color: #333;
40
+ margin-right: auto;
41
+ border-bottom-left-radius: 4px;
42
+ text-align: left;
43
+ }
44
 
45
+ #input-container { display: flex; gap: 10px; margin-bottom: 20px; }
46
+ #user-input { flex-grow: 1; padding: 12px; border: 1px solid #ccc; border-radius: 6px; }
47
+ #send-button { padding: 10px 20px; background-color: #1f7cff; color: white; border: none; border-radius: 6px; cursor: pointer; transition: background-color 0.3s; }
48
+ #send-button:hover { background-color: #165bb0; }
49
+
50
+ #state-info { padding: 15px; background-color: #e6f0ff; border-left: 5px solid #1f7cff; border-radius: 4px; font-size: 0.9em; margin-bottom: 25px; display: flex; justify-content: space-between; align-items: center; }
51
+
52
+ pre {
53
+ background-color: #272822;
54
+ color: #f8f8f2;
55
+ padding: 10px;
56
+ border-radius: 4px;
57
+ overflow-x: auto;
58
+ margin-top: 10px;
59
+ font-family: 'Consolas', 'Monaco', monospace;
60
+ font-size: 0.9em;
61
+ }
62
+ </style>
63
  </head>
64
  <body>
65
  <div class="container">
66
  <h1>Code Assistant Agent 🤖</h1>
67
 
68
  <div id="state-info">
69
+ Current Language:
70
+ <select id="current-language" onchange="updateLanguage()">
71
+ <option value="Python">Python</option>
72
+ <option value="JavaScript">JavaScript</option>
73
+ <option value="Java">Java</option>
74
+ <option value="C++">C++</option>
75
+ <option value="C#">C#</option>
76
+ <option value="Go">Go</option>
77
+ <option value="Ruby">Ruby</option>
78
+ <option value="PHP">PHP</option>
79
+ <option value="TypeScript">TypeScript</option>
80
+ <option value="Swift">Swift</option>
81
+ </select>
82
  </div>
83
 
84
+ <div id="chat-window"></div>
 
85
 
86
  <div id="input-container">
87
  <input type="text" id="user-input" placeholder="Ask about code, concepts, or bugs..." onkeypress="if(event.key === 'Enter') sendMessage()">
88
  <button id="send-button" onclick="sendMessage()">Send</button>
89
  </div>
 
 
 
 
 
90
  </div>
91
 
92
  <script>
 
93
  let chatHistory = [];
 
 
94
  let assistantState = {
95
  conversationSummary: "",
96
+ language: "Python"
 
97
  };
98
  const apiUrl = '/chat';
 
99
 
100
  document.addEventListener('DOMContentLoaded', () => {
 
 
101
  appendMessage({ role: 'assistant', content: "Hello! I'm your Code Assistant. What programming language are you working in today?" });
102
  });
103
 
 
 
104
  async function sendMessage() {
105
  const inputElement = document.getElementById('user-input');
106
  const userMessage = inputElement.value.trim();
 
107
  if (!userMessage) return;
108
 
 
109
  const userMsgObj = { role: 'user', content: userMessage };
110
  chatHistory.push(userMsgObj);
 
111
  appendMessage(userMsgObj);
112
  inputElement.value = '';
113
 
 
116
  method: 'POST',
117
  headers: { 'Content-Type': 'application/json' },
118
  body: JSON.stringify({
 
119
  chat_history: chatHistory,
120
  assistant_state: assistantState
121
  })
122
  });
123
 
124
  if (!response.ok) {
 
125
  chatHistory.pop();
126
  throw new Error(`HTTP error! status: ${response.status}`);
127
  }
128
 
129
  const data = await response.json();
 
 
130
  assistantState = data.updated_state;
131
 
132
+ const assistantMsgObj = { role: 'assistant', content: data.assistant_reply };
133
+ chatHistory.push(assistantMsgObj);
134
+ appendMessage(assistantMsgObj);
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  } catch (error) {
137
  console.error('Chat failed:', error);
 
139
  }
140
  }
141
 
 
 
142
  function appendMessage(msg) {
143
  const chatWindow = document.getElementById('chat-window');
144
  const messageRow = document.createElement('div');
 
148
  msgDiv.className = `message ${msg.role}`;
149
 
150
  let content = msg.content;
 
 
151
  content = content.replace(/```(\w+)?\n([\s\S]*?)\n```/g, (match, lang, code) => {
152
  const language = lang || 'code';
153
  return `<pre><code class="language-${language}">${code.trim()}</code></pre>`;
 
156
  msgDiv.innerHTML = content;
157
  messageRow.appendChild(msgDiv);
158
  chatWindow.appendChild(messageRow);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  chatWindow.scrollTop = chatWindow.scrollHeight;
160
  }
161
 
162
+ function updateLanguage() {
163
+ assistantState.language = document.getElementById('current-language').value;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  }
165
  </script>
166
  </body>
167
+ </html>