52323610e3
Verifiziert: useDeleteNotification und useAgentSkills haben NULL Komponenten-Importeure (nur Definitionsdateien). Die echten Komponenten nutzen andere Hooks (useNotifications, useMarkNotificationRead, useUnreadNotificationCount; useAgentTools/useAgentToolsFull). Nach AGENTS.md 0.2 keine Backend-Shims fuer tote Calls: beide Hooks entfernt, ungenutztes apiDelete-Import in notifications.ts bereinigt. Damit sind alle 12 API-Braeche aus dem D5-Triage abgeschlossen: ai/sessions x5 (3e5f13f), policies x4 (86c96f0), mail x4 (86c96f0), notifications DELETE (hier), agents/skills (hier). tsc exit=0.
322 lines
9.0 KiB
TypeScript
322 lines
9.0 KiB
TypeScript
/**
|
|
* React Query hooks for Automation & Agents API.
|
|
* Follows the pattern from plugins.ts.
|
|
*/
|
|
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { apiGet, apiPost, apiPatch, apiDelete } from './client';
|
|
import type {
|
|
AutomationDefinition,
|
|
AutomationRun,
|
|
AutomationVersion,
|
|
AgentDefinition,
|
|
AgentRun,
|
|
AgentVersion,
|
|
AgentTool,
|
|
AutomationSettings,
|
|
MiniAppDef,
|
|
AgentToolInfo,
|
|
AgentRunFull,
|
|
} from '@/types/automation';
|
|
|
|
// ── Automation Hooks ──
|
|
|
|
export function useAutomations() {
|
|
return useQuery({
|
|
queryKey: ['automations'],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AutomationDefinition[] | { items: AutomationDefinition[] }>('/automation');
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAutomation(id: string) {
|
|
return useQuery({
|
|
queryKey: ['automation', id],
|
|
queryFn: () => apiGet<AutomationDefinition>(`/automation/${id}`),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useCreateAutomation() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: Partial<AutomationDefinition>) => apiPost<AutomationDefinition>('/automation', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automations'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateAutomation() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: Partial<AutomationDefinition> }) =>
|
|
apiPatch<AutomationDefinition>(`/automation/${id}`, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automations'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAutomation() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiDelete(`/automation/${id}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automations'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useExecuteAutomation() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiPost(`/automation/${id}/execute`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automationRuns'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDryRunAutomation() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiPost(`/automation/${id}/dry-run`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automationRuns'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAutomationRuns(automationId: string) {
|
|
return useQuery({
|
|
queryKey: ['automationRuns', automationId],
|
|
queryFn: () => apiGet<AutomationRun[]>(`/automation/${automationId}/runs`),
|
|
enabled: !!automationId,
|
|
});
|
|
}
|
|
|
|
export function useAutomationVersions(automationId: string) {
|
|
return useQuery({
|
|
queryKey: ['automationVersions', automationId],
|
|
queryFn: () => apiGet<AutomationVersion[]>(`/automation/${automationId}/versions`),
|
|
enabled: !!automationId,
|
|
});
|
|
}
|
|
|
|
export function useRestoreAutomationVersion() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, versionId }: { id: string; versionId: string }) =>
|
|
apiPost(`/automation/${id}/versions/${versionId}/restore`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automations'] });
|
|
queryClient.invalidateQueries({ queryKey: ['automationVersions'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAutomationSettings() {
|
|
return useQuery({
|
|
queryKey: ['automationSettings'],
|
|
queryFn: () => apiGet<AutomationSettings>('/automation/settings'),
|
|
});
|
|
}
|
|
|
|
export function useUpdateAutomationSettings() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: Partial<AutomationSettings>) =>
|
|
apiPatch<AutomationSettings>('/automation/settings', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['automationSettings'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Agent Hooks ──
|
|
|
|
export function useAgents() {
|
|
return useQuery({
|
|
queryKey: ['agents'],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AgentDefinition[] | { items: AgentDefinition[] }>('/agents');
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAgent(id: string) {
|
|
return useQuery({
|
|
queryKey: ['agent', id],
|
|
queryFn: () => apiGet<AgentDefinition>(`/agents/${id}`),
|
|
enabled: !!id,
|
|
});
|
|
}
|
|
|
|
export function useCreateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: Partial<AgentDefinition>) => apiPost<AgentDefinition>('/agents', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agents'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useUpdateAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, data }: { id: string; data: Partial<AgentDefinition> }) =>
|
|
apiPatch<AgentDefinition>(`/agents/${id}`, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agents'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiDelete(`/agents/${id}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agents'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useExecuteAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiPost(`/agents/${id}/execute`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agentRuns'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTestRunAgent() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: string) => apiPost(`/agents/${id}/test-run`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agentRuns'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAgentRuns(agentId: string) {
|
|
return useQuery({
|
|
queryKey: ['agentRuns', agentId],
|
|
queryFn: () => apiGet<AgentRun[]>(`/agents/${agentId}/runs`),
|
|
enabled: !!agentId,
|
|
});
|
|
}
|
|
|
|
export function useAgentVersions(agentId: string) {
|
|
return useQuery({
|
|
queryKey: ['agentVersions', agentId],
|
|
queryFn: () => apiGet<AgentVersion[]>(`/agents/${agentId}/versions`),
|
|
enabled: !!agentId,
|
|
});
|
|
}
|
|
|
|
export function useRestoreAgentVersion() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, versionId }: { id: string; versionId: string }) =>
|
|
apiPost(`/agents/${id}/versions/${versionId}/restore`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agents'] });
|
|
queryClient.invalidateQueries({ queryKey: ['agentVersions'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAgentTools() {
|
|
return useQuery({
|
|
queryKey: ['agentTools'],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AgentTool[] | { items: AgentTool[] }>('/agents/tools');
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Agent Editor / Chat / Log / Monitor Hooks ──
|
|
|
|
export function useAgentToolsFull() {
|
|
return useQuery({
|
|
queryKey: ['agentToolsFull'],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AgentToolInfo[] | { items: AgentToolInfo[] }>('/agents/tools');
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useAgentRunsFull(agentId: string) {
|
|
return useQuery({
|
|
queryKey: ['agentRunsFull', agentId],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AgentRunFull[] | { items: AgentRunFull[] }>(`/agents/${agentId}/runs`);
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
enabled: !!agentId,
|
|
});
|
|
}
|
|
|
|
export function useRecentAgentRuns(limit = 20) {
|
|
return useQuery({
|
|
queryKey: ['recentAgentRuns', limit],
|
|
queryFn: async () => {
|
|
const res = await apiGet<AgentRunFull[] | { items: AgentRunFull[] }>(`/agents/runs/recent?limit=${limit}`);
|
|
return Array.isArray(res) ? res : res.items ?? [];
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Agent Communication Hooks ──
|
|
|
|
export function useSendAgentMessage() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, toAgentName, message }: { id: string; toAgentName: string; message: string }) =>
|
|
apiPost(`/agents/${id}/send-message`, { to_agent_name: toAgentName, message }),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['agentRuns'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── MiniApp Hooks ──
|
|
|
|
export function useMiniApps() {
|
|
return useQuery({
|
|
queryKey: ['miniapps'],
|
|
queryFn: () => apiGet<MiniAppDef[]>('/automation/miniapps'),
|
|
});
|
|
}
|
|
|
|
export function useCreateMiniApp() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: Partial<MiniAppDef>) => apiPost<MiniAppDef>('/automation/miniapps', data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['miniapps'] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteMiniApp() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (appId: string) => apiDelete(`/automation/miniapps/${appId}`),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ['miniapps'] });
|
|
},
|
|
});
|
|
}
|