import { defineStore } from 'pinia'; import { useUiStore } from './ui'; interface SubAgent { task_id: string; agent_id?: string | number; status?: string; summary?: string; last_tool?: string; conversation_id?: string; } interface SubAgentActivityEntry { id?: string; tool?: string; status?: string; args?: Record; ts?: number; error?: string; } interface SubAgentState { subAgents: SubAgent[]; pollTimer: ReturnType | null; activityTimer: ReturnType | null; activeAgent: SubAgent | null; activityEntries: SubAgentActivityEntry[]; activityLoading: boolean; activityError: string | null; } export const useSubAgentStore = defineStore('subAgent', { state: (): SubAgentState => ({ subAgents: [], pollTimer: null, activityTimer: null, activeAgent: null, activityEntries: [], activityLoading: false, activityError: null }), actions: { async fetchSubAgents() { try { const resp = await fetch('/api/sub_agents'); if (!resp.ok) { throw new Error(await resp.text()); } const data = await resp.json(); if (data.success) { this.subAgents = Array.isArray(data.data) ? data.data : []; } } catch (error) { console.error('获取子智能体列表失败:', error); } }, startPolling() { if (this.pollTimer) { return; } const uiStore = useUiStore(); this.pollTimer = setInterval(() => { if (uiStore.panelMode === 'subAgents') { this.fetchSubAgents(); } }, 5000); }, stopPolling() { if (this.pollTimer) { clearInterval(this.pollTimer); this.pollTimer = null; } }, openSubAgent(agent: SubAgent) { if (!agent || !agent.task_id) { return; } this.activeAgent = agent; this.activityEntries = []; this.activityError = null; this.fetchSubAgentActivity(agent.task_id); this.startActivityPolling(); }, closeSubAgent() { this.stopActivityPolling(); this.activeAgent = null; this.activityEntries = []; this.activityError = null; this.activityLoading = false; }, startActivityPolling() { if (this.activityTimer) { return; } this.activityTimer = setInterval(() => { const taskId = this.activeAgent?.task_id; if (taskId) { this.fetchSubAgentActivity(taskId); } }, 2000); }, stopActivityPolling() { if (this.activityTimer) { clearInterval(this.activityTimer); this.activityTimer = null; } }, async fetchSubAgentActivity(taskId: string) { if (!taskId) return; this.activityLoading = true; try { const resp = await fetch(`/api/sub_agents/${taskId}/activity?limit=200`); if (!resp.ok) { throw new Error(await resp.text()); } const data = await resp.json(); if (data && data.success && data.data) { const entries = Array.isArray(data.data.entries) ? data.data.entries : []; this.activityEntries = entries; } } catch (error: any) { this.activityError = error?.message || String(error); console.error('获取子智能体活动失败:', error); } finally { this.activityLoading = false; } }, stripConversationPrefix(conversationId: string) { if (!conversationId) return ''; return conversationId.startsWith('conv_') ? conversationId.slice(5) : conversationId; }, getBaseUrl() { const override = (window as any).SUB_AGENT_BASE_URL || (window as any).__SUB_AGENT_BASE_URL__; if (override && typeof override === 'string') { return override.replace(/\/$/, ''); } const { protocol, hostname } = window.location; if (hostname && hostname.includes('agent.')) { const mappedHost = hostname.replace('agent.', 'subagent.'); return `${protocol}//${mappedHost}`; } return `${protocol}//${hostname}:8092`; } } });