import { useQuery } from '@tanstack/react-query'; import { api } from '../client'; import { config } from '../../config'; import { useAuthStore } from '../../auth/auth-store'; export function useAgents(status?: string, group?: string) { return useQuery({ queryKey: ['agents', status, group], queryFn: async () => { const { data, error } = await api.GET('/agents', { params: { query: { ...(status ? { status } : {}), ...(group ? { group } : {}) } }, }); if (error) throw new Error('Failed to load agents'); return data!; }, refetchInterval: 10_000, }); } export function useAgentEvents(appId?: string, agentId?: string, limit = 50) { return useQuery({ queryKey: ['agents', 'events', appId, agentId, limit], queryFn: async () => { const token = useAuthStore.getState().accessToken; const params = new URLSearchParams(); if (appId) params.set('appId', appId); if (agentId) params.set('agentId', agentId); params.set('limit', String(limit)); const res = await fetch(`${config.apiBaseUrl}/agents/events-log?${params}`, { headers: { Authorization: `Bearer ${token}`, 'X-Cameleer-Protocol-Version': '1', }, }); if (!res.ok) throw new Error('Failed to load agent events'); return res.json(); }, refetchInterval: 15_000, }); }