Spaces:
Running
Running
muralipala1504 commited on
Commit ·
c62ad6f
1
Parent(s): a39f5c0
Fix app.js: align frontend with backend (/run-agent + prompt)
Browse files
app.js
CHANGED
|
@@ -1,50 +1,58 @@
|
|
| 1 |
-
document.
|
| 2 |
-
|
| 3 |
-
const input = document.getElementById(
|
| 4 |
-
const output = document.getElementById(
|
| 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 |
} else {
|
| 36 |
-
|
| 37 |
}
|
| 38 |
|
| 39 |
-
output.appendChild(
|
| 40 |
output.scrollTop = output.scrollHeight;
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 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 |
+
'&': '&',
|
| 52 |
+
'<': '<',
|
| 53 |
+
'>': '>',
|
| 54 |
+
'"': '"',
|
| 55 |
+
"'": ''',
|
| 56 |
+
})[m]);
|
| 57 |
}
|
| 58 |
});
|