muralipala1504 commited on
Commit
c62ad6f
·
1 Parent(s): a39f5c0

Fix app.js: align frontend with backend (/run-agent + prompt)

Browse files
Files changed (1) hide show
  1. app.js +51 -43
app.js CHANGED
@@ -1,50 +1,58 @@
1
- document.getElementById("chat-form").addEventListener("submit", async (e) => {
2
- e.preventDefault();
3
- const input = document.getElementById("chat-input");
4
- const output = document.getElementById("chat-output");
5
- const userMessage = input.value.trim();
6
-
7
- if (!userMessage) return;
8
-
9
- // Show user message
10
- const userDiv = document.createElement("div");
11
- userDiv.className = "message";
12
- userDiv.innerHTML = `<strong>User:</strong><br>${userMessage}`;
13
- output.appendChild(userDiv);
14
-
15
- // Clear input
16
- input.value = "";
17
-
18
- // Fetch response from backend
19
- try {
20
- const resp = await fetch("/chat", {
21
- method: "POST",
22
- headers: { "Content-Type": "application/json" },
23
- body: JSON.stringify({ message: userMessage })
24
- });
25
-
26
- const data = await resp.json();
27
-
28
- const botDiv = document.createElement("div");
29
- botDiv.className = "message";
30
-
31
- if (data.response) {
32
- // Wrap output into Prism-compatible code block
33
- botDiv.innerHTML = `<strong>Deepshell:</strong><br>
34
- <pre><code class="language-bash">${data.response}</code></pre>`;
 
 
 
 
 
 
35
  } else {
36
- botDiv.innerHTML = `<strong>Error:</strong><br>Server error: ${data.error || "Unknown error"}`;
37
  }
38
 
39
- output.appendChild(botDiv);
40
  output.scrollTop = output.scrollHeight;
 
41
 
42
- // Highlight with Prism and attach Copy buttons
43
- Prism.highlightAll();
44
- } catch (err) {
45
- const botDiv = document.createElement("div");
46
- botDiv.className = "message";
47
- botDiv.innerHTML = `<strong>Error:</strong><br>Could not connect to server`;
48
- output.appendChild(botDiv);
 
49
  }
50
  });
 
1
+ document.addEventListener('DOMContentLoaded', () => {
2
+ const form = document.getElementById('chat-form');
3
+ const input = document.getElementById('chat-input');
4
+ const output = document.getElementById('chat-output');
5
+
6
+ form.addEventListener('submit', async (e) => {
7
+ e.preventDefault();
8
+ const command = input.value.trim();
9
+ if (!command) return;
10
+
11
+ appendMessage('User', command);
12
+ input.value = '';
13
+ try {
14
+ const response = await fetch('/run-agent', {
15
+ method: 'POST',
16
+ headers: { 'Content-Type': 'application/json' },
17
+ body: JSON.stringify({ prompt: command }), // ✅ backend expects `prompt`
18
+ });
19
+ if (!response.ok) {
20
+ appendMessage('Error', `Server error: ${response.statusText}`);
21
+ return;
22
+ }
23
+ const data = await response.json();
24
+ appendMessage('Deepshell', data.output || 'No response');
25
+ Prism.highlightAll(); // Highlight newly added code blocks
26
+ } catch (err) {
27
+ appendMessage('Error', `Network error: ${err.message}`);
28
+ }
29
+ });
30
+
31
+ function appendMessage(sender, message) {
32
+ const msgDiv = document.createElement('div');
33
+ msgDiv.classList.add('message');
34
+
35
+ // Detect code block markdown ```lang ... ```
36
+ const codeBlockMatch = message.match(/```(\w+)?\n([\s\S]*?)```/);
37
+ if (codeBlockMatch) {
38
+ const lang = codeBlockMatch[1] || 'bash';
39
+ const code = codeBlockMatch[2];
40
+ msgDiv.innerHTML = `<strong>${sender}:</strong><pre><code class="language-${lang}">${escapeHtml(code)}</code></pre>`;
41
  } else {
42
+ msgDiv.innerHTML = `<strong>${sender}:</strong> <pre>${escapeHtml(message)}</pre>`;
43
  }
44
 
45
+ output.appendChild(msgDiv);
46
  output.scrollTop = output.scrollHeight;
47
+ }
48
 
49
+ function escapeHtml(text) {
50
+ return text.replace(/[&<>"']/g, (m) => ({
51
+ '&': '&amp;',
52
+ '<': '&lt;',
53
+ '>': '&gt;',
54
+ '"': '&quot;',
55
+ "'": '&#39;',
56
+ })[m]);
57
  }
58
  });