chat agent
This commit is contained in:
parent
b9c6c831c1
commit
b44e4ed6f9
11 changed files with 626 additions and 16 deletions
|
|
@ -16,7 +16,8 @@ const state = {
|
|||
selectedRunId: null,
|
||||
currentView: 'dashboard', // 'dashboard' or 'task'
|
||||
isEditing: false,
|
||||
isAuthenticated: false
|
||||
isAuthenticated: false,
|
||||
chatMessages: []
|
||||
};
|
||||
// DOM elements
|
||||
const loginOverlay = document.getElementById('login-overlay');
|
||||
|
|
@ -53,6 +54,11 @@ const schedulePresets = document.getElementById('schedule-presets');
|
|||
const toggleCustomCronBtn = document.getElementById('toggle-custom-cron');
|
||||
const customCronContainer = document.getElementById('custom-cron-container');
|
||||
const presetBtns = document.querySelectorAll('.btn-preset');
|
||||
const chatMessagesEl = document.getElementById('chat-messages');
|
||||
const chatForm = document.getElementById('chat-form');
|
||||
const chatInput = document.getElementById('chat-input');
|
||||
const clearChatBtn = document.getElementById('clear-chat-btn');
|
||||
const chatSendBtn = document.getElementById('chat-send-btn');
|
||||
|
||||
function updateState(newState) {
|
||||
Object.assign(state, newState);
|
||||
|
|
@ -89,6 +95,7 @@ function renderApp() {
|
|||
|
||||
if (state.currentView === 'dashboard') {
|
||||
fetchRecentRuns();
|
||||
renderChat();
|
||||
} else if (state.selectedTaskId) {
|
||||
const task = state.tasks.find(t => t.id === state.selectedTaskId);
|
||||
if (task) {
|
||||
|
|
@ -354,6 +361,62 @@ function escapeHtml(text) {
|
|||
return div.innerHTML;
|
||||
}
|
||||
|
||||
function renderChat() {
|
||||
if (!chatMessagesEl) return;
|
||||
|
||||
if (state.chatMessages.length === 0) {
|
||||
chatMessagesEl.innerHTML = `
|
||||
<div class="chat-empty-state">
|
||||
<p>Start a conversation with the assistant.</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
chatMessagesEl.innerHTML = state.chatMessages
|
||||
.map(msg => `
|
||||
<div class="chat-message ${msg.role}">
|
||||
${escapeHtml(msg.content)}
|
||||
</div>
|
||||
`)
|
||||
.join('');
|
||||
|
||||
chatMessagesEl.scrollTop = chatMessagesEl.scrollHeight;
|
||||
}
|
||||
|
||||
async function sendChatMessage(text) {
|
||||
const userMessage = { role: 'user', content: text };
|
||||
state.chatMessages.push(userMessage);
|
||||
renderChat();
|
||||
|
||||
chatInput.value = '';
|
||||
chatInput.disabled = true;
|
||||
chatSendBtn.disabled = true;
|
||||
|
||||
try {
|
||||
const response = await fetchWithAuth(`${API_URL}/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ messages: state.chatMessages })
|
||||
});
|
||||
|
||||
if (!response.ok) throw new Error('Chat API failed');
|
||||
|
||||
const result = await response.json();
|
||||
state.chatMessages.push(result.message);
|
||||
renderChat();
|
||||
} catch (error) {
|
||||
console.error('Chat error:', error);
|
||||
showToast('Failed to get chat response.', 'error');
|
||||
state.chatMessages.push({ role: 'assistant', content: 'Sorry, I encountered an error. Please try again.' });
|
||||
renderChat();
|
||||
} finally {
|
||||
chatInput.disabled = false;
|
||||
chatSendBtn.disabled = false;
|
||||
chatInput.focus();
|
||||
}
|
||||
}
|
||||
|
||||
// Event Listeners
|
||||
rerunBtn.addEventListener('click', async () => {
|
||||
if (!state.selectedTaskId) return;
|
||||
|
|
@ -447,6 +510,19 @@ toggleCustomCronBtn.addEventListener('click', () => {
|
|||
customCronContainer.classList.toggle('hidden');
|
||||
});
|
||||
|
||||
chatForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const text = chatInput.value.trim();
|
||||
if (text) {
|
||||
sendChatMessage(text);
|
||||
}
|
||||
});
|
||||
|
||||
clearChatBtn.addEventListener('click', () => {
|
||||
state.chatMessages = [];
|
||||
renderChat();
|
||||
});
|
||||
|
||||
cronInput.addEventListener('input', () => {
|
||||
// If user types manually, update presets active state
|
||||
updateScheduleUI(cronInput.value);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue