diff --git a/apps/jingrow/frontend/src/shared/api/agents.ts b/apps/jingrow/frontend/src/shared/api/agents.ts index 6c06a42..3740294 100644 --- a/apps/jingrow/frontend/src/shared/api/agents.ts +++ b/apps/jingrow/frontend/src/shared/api/agents.ts @@ -27,43 +27,6 @@ function get_jingrow_api_headers() { } } -// 获取AI Agent列表 -export const getAgentsApi = async (): Promise => { - try { - const url = `/api/action/jingrow.ai.utils.jlocal.get_local_ai_agents_list` - - const requestData = { - filters: [], - fields: ['name', 'agent_name', 'status', 'enabled', 'trigger_mode', 'trigger_time', 'progress', 'ai_repeat', 'creation', 'modified', 'modified_by'], - order_by: 'modified desc', - limit_start: 0, - limit_page_length: 20 - } - - const response = await axios.post( - url, - requestData, - { - headers: get_jingrow_api_headers(), - withCredentials: true - } - ) - - const message = response.data.message - if (message?.error) { - const errorMsg = typeof message.error === 'object' ? JSON.stringify(message.error) : message.error - throw new Error(errorMsg) - } - - // 根据你提供的API响应,数据直接在message字段中,不是message.data - const agents = Array.isArray(message) ? message : (message?.data || []) - return agents - } catch (error: any) { - console.error("Error in getAgentsApi:", error) - throw new Error(error.response?.data?.message || error.message || '获取AI Agent列表失败') - } -} - // 获取单个AI Agent详情 export const getAgentDetail = async (name: string): Promise => { try { @@ -79,39 +42,6 @@ export const getAgentDetail = async (name: string): Promise => { } } -// 执行AI Agent -export const executeAgentApi = async (name: string): Promise<{ success: boolean; message?: string }> => { - try { - const response = await axios.post( - `/api/action/jingrow.ai.utils.jlocal.execute_local_ai_agent`, - { - name: name - }, - { - headers: get_jingrow_api_headers(), - withCredentials: true - } - ) - - const message = response.data.message - if (message?.error) { - const errorMsg = typeof message.error === 'object' ? JSON.stringify(message.error) : message.error - throw new Error(errorMsg) - } - - return { - success: message?.success || true, - message: message?.message || '执行成功' - } - } catch (error: any) { - console.error("Error in executeAgentApi:", error) - return { - success: false, - message: error.response?.data?.message || error.message || '执行失败' - } - } -} - // 更新AI Agent export const updateAgentApi = async (name: string, data: Partial): Promise => { try { diff --git a/apps/jingrow/frontend/src/shared/stores/agent.ts b/apps/jingrow/frontend/src/shared/stores/agent.ts index e92f671..a4eaf96 100644 --- a/apps/jingrow/frontend/src/shared/stores/agent.ts +++ b/apps/jingrow/frontend/src/shared/stores/agent.ts @@ -1,122 +1,42 @@ import { defineStore } from 'pinia' -import { ref, computed } from 'vue' -import { getAgentsApi, getAgentDetail, executeAgentApi, updateAgentApi, type AIAgent } from '../api/agents' +import { ref } from 'vue' +import { getAgentDetail, updateAgentApi, type AIAgent } from '../api/agents' export const useAgentStore = defineStore('agent', () => { - const agents = ref([]) const currentAgent = ref(null) - const loading = ref(false) - const error = ref(null) - - // 计算属性 - const enabledAgents = computed(() => agents.value.filter((agent: any) => agent.enabled)) - const activeAgents = computed(() => agents.value.filter((agent: any) => agent.status === '进行中')) - const completedAgents = computed(() => agents.value.filter((agent: any) => agent.status === '已完成')) - - // 获取AI Agent列表 - const fetchAgents = async () => { - loading.value = true - error.value = null - try { - agents.value = await getAgentsApi() - } catch (err: any) { - error.value = err.message || '获取AI Agent列表失败' - console.error('获取AI Agent列表失败:', err) - } finally { - loading.value = false - } - } // 获取单个AI Agent const fetchAgent = async (name: string) => { - loading.value = true - error.value = null try { const agentData = await getAgentDetail(name) currentAgent.value = agentData } catch (err: any) { - error.value = err.message || '获取AI Agent详情失败' console.error('获取AI Agent详情失败:', err) - } finally { - loading.value = false + throw err } } - // 执行AI Agent - const executeAgent = async (name: string) => { - loading.value = true - error.value = null - try { - const result = await executeAgentApi(name) - if (result.success) { - // 执行成功后刷新列表 - await fetchAgents() - return { success: true, message: result.message } - } else { - throw new Error(result.message || '执行失败') - } - } catch (err: any) { - error.value = err.message || '执行AI Agent失败' - console.error('执行AI Agent失败:', err) - return { success: false, error: err.message } - } finally { - loading.value = false - } - } - - // 根据状态获取Agent数量 - const getAgentCountByStatus = (status: string) => { - return agents.value.filter((agent: any) => agent.status === status).length - } - - // 根据触发模式获取Agent数量 - const getAgentCountByTriggerMode = (triggerMode: string) => { - return agents.value.filter((agent: any) => agent.trigger_mode === triggerMode).length - } - // 更新AI Agent const updateAgent = async (name: string, data: Partial) => { - loading.value = true - error.value = null try { await updateAgentApi(name, data) // 更新成功后,手动更新本地状态 if (currentAgent.value && currentAgent.value.name === name) { currentAgent.value = { ...currentAgent.value, ...data } } - // 更新列表中的agent - const index = agents.value.findIndex((agent: any) => agent.name === name) - if (index !== -1) { - agents.value[index] = { ...agents.value[index], ...data } - } return { success: true } } catch (err: any) { - error.value = err.message || '更新AI Agent失败' console.error('更新AI Agent失败:', err) throw err - } finally { - loading.value = false } } return { // 状态 - agents, currentAgent, - loading, - error, - - // 计算属性 - enabledAgents, - activeAgents, - completedAgents, // 方法 - fetchAgents, fetchAgent, - executeAgent, - updateAgent, - getAgentCountByStatus, - getAgentCountByTriggerMode + updateAgent } })