// Agent状态组件 const AgentStatusComponent = { template: '#agent-status-component', data() { return { messages: [], userInput: '', isProcessing: false, currentMode: '', sessionId: '', tasks: [], currentTask: null, pollingInterval: null }; }, mounted() { this.sessionId = 'session_' + Date.now(); this.currentMode = t('standby'); this.loadTasks(); this.startPolling(); }, beforeUnmount() { this.stopPolling(); }, methods: { async sendMessage() { if (!this.userInput.trim() || this.isProcessing) return; const message = this.userInput.trim(); this.userInput = ''; this.isProcessing = true; // 添加用户消息 this.addMessage('user', message); try { // 调用Agent执行API const response = await fetch('/api/agent/execute', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message, session_id: this.sessionId, model: 'kimi' }) }); const data = await response.json(); if (data.success) { this.currentTask = data.task_id; this.addMessage('system', t('msg-task-submitted') + data.task_id); this.loadTasks(); } else { this.addMessage('error', data.message || t('msg-submit-failed')); } } catch (error) { this.addMessage('error', t('msg-request-failed') + error.message); } this.isProcessing = false; }, async loadTasks() { try { const response = await fetch('/api/agent/tasks'); const data = await response.json(); if (data.success) { this.tasks = data.tasks; } } catch (error) { console.error(t('msg-load-task-failed'), error); } }, async checkTaskStatus(taskId) { try { const response = await fetch(`/api/agent/status/${taskId}`); const data = await response.json(); if (data.success && data.task) { const task = data.task; // 更新任务状态 const taskIndex = this.tasks.findIndex(t => t.task_id === taskId); if (taskIndex >= 0) { this.tasks[taskIndex] = { ...this.tasks[taskIndex], ...task }; } // 如果任务完成,显示结果 if (task.status === 'completed' && task.result) { this.addMessage('assistant', task.result.response || t('msg-task-completed')); // 显示执行结果摘要 if (task.result.execution_result) { const execResult = task.result.execution_result; if (execResult.status === 'success') { const manifest = execResult.manifest || {}; const metrics = manifest.metrics || {}; this.addMessage('system', t('msg-analysis-complete') + (metrics.final_members || 0) + t('msg-gene-members')); } } } else if (task.status === 'failed') { this.addMessage('error', task.result?.response || t('msg-task-failed')); } } } catch (error) { console.error(t('msg-check-task-status-failed'), error); } }, selectTask(task) { this.currentTask = task.task_id; this.checkTaskStatus(task.task_id); }, startPolling() { this.pollingInterval = setInterval(() => { if (this.currentTask) { this.checkTaskStatus(this.currentTask); } this.loadTasks(); }, 3000); // 每3秒轮询一次 }, stopPolling() { if (this.pollingInterval) { clearInterval(this.pollingInterval); } }, addMessage(role, content) { this.messages.push({ role: role, content: content, timestamp: new Date().toISOString() }); // 滚动到底部 setTimeout(() => { const container = this.$refs.messageContainer; if (container) { container.scrollTop = container.scrollHeight; } }, 100); }, getRoleLabel(role) { const keys = { 'user': 'user-role', 'assistant': 'agent-role', 'system': 'system-role', 'error': 'error-role', 'execution': 'execution-role', 'planning': 'planning-role', 'diagnosis': 'diagnosis-role' }; return keys[role] ? t(keys[role]) : role; }, getStatusIcon(status) { const icons = { 'pending': '⏳', 'running': '🔄', 'completed': '✅', 'failed': '❌', 'cancelled': '🚫' }; return icons[status] || '❓'; }, formatMessage(content) { // 简单的Markdown格式化 return content .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/\n/g, '
'); }, formatTime(timestamp) { if (!timestamp) return ''; const date = new Date(timestamp); return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' }); }, t(key) { return window.t ? window.t(key) : key; } } };