Spaces:
Sleeping
Sleeping
| import { create } from "zustand"; | |
| import { api } from "@/lib/api"; | |
| import type { | |
| AgentProfile, | |
| AgentCreateRequest, | |
| AgentUpdateRequest, | |
| AgentTemplate, | |
| ToolInfo, | |
| } from "@/types/agent"; | |
| interface AgentStore { | |
| agents: AgentProfile[]; | |
| templates: AgentTemplate[]; | |
| tools: ToolInfo[]; | |
| loading: boolean; | |
| error: string | null; | |
| fetchAgents: () => Promise<void>; | |
| fetchTemplates: () => Promise<void>; | |
| fetchTools: () => Promise<void>; | |
| createAgent: (data: AgentCreateRequest) => Promise<AgentProfile>; | |
| updateAgent: (id: string, data: AgentUpdateRequest) => Promise<void>; | |
| deleteAgent: (id: string) => Promise<void>; | |
| getAgent: (id: string) => AgentProfile | undefined; | |
| } | |
| export const useAgentStore = create<AgentStore>((set, get) => ({ | |
| agents: [], | |
| templates: [], | |
| tools: [], | |
| loading: false, | |
| error: null, | |
| fetchAgents: async () => { | |
| set({ loading: true, error: null }); | |
| try { | |
| const agents = await api.get<AgentProfile[]>("/agents"); | |
| set({ agents, loading: false }); | |
| } catch (e) { | |
| set({ error: String(e), loading: false }); | |
| } | |
| }, | |
| fetchTemplates: async () => { | |
| try { | |
| const templates = await api.get<AgentTemplate[]>("/agents/templates"); | |
| set({ templates }); | |
| } catch (e) { | |
| set({ error: String(e) }); | |
| } | |
| }, | |
| fetchTools: async () => { | |
| try { | |
| const tools = await api.get<ToolInfo[]>("/tools"); | |
| set({ tools }); | |
| } catch (e) { | |
| set({ error: String(e) }); | |
| } | |
| }, | |
| createAgent: async (data) => { | |
| const agent = await api.post<AgentProfile>("/agents", data); | |
| set((s) => ({ agents: [...s.agents, agent] })); | |
| return agent; | |
| }, | |
| updateAgent: async (id, data) => { | |
| const agent = await api.put<AgentProfile>(`/agents/${id}`, data); | |
| set((s) => ({ | |
| agents: s.agents.map((a) => (a.agent_id === id ? agent : a)), | |
| })); | |
| }, | |
| deleteAgent: async (id) => { | |
| await api.delete(`/agents/${id}`); | |
| set((s) => ({ agents: s.agents.filter((a) => a.agent_id !== id) })); | |
| }, | |
| getAgent: (id) => get().agents.find((a) => a.agent_id === id), | |
| })); | |