File size: 2,994 Bytes
5da4770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { createMutationHook, createQueryHook } from '@/hooks/use-query';
import { useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { workflowKeys } from './workflow-keys';
import { 
  CreateWorkflowRequest, 
  UpdateWorkflowRequest, 
  ExecuteWorkflowRequest,
  getAgentWorkflows,
  createAgentWorkflow,
  updateAgentWorkflow,
  deleteAgentWorkflow,
  executeWorkflow,
  getWorkflowExecutions
} from './workflow-utils';


export const useAgentWorkflows = (agentId: string) => {
  return createQueryHook(
    workflowKeys.agent(agentId),
    () => getAgentWorkflows(agentId),
    {
      enabled: !!agentId,
      staleTime: 30000,
    }
  )();
};

export const useCreateAgentWorkflow = () => {
  const queryClient = useQueryClient();
  
  return createMutationHook(
    ({ agentId, workflow }: { agentId: string; workflow: CreateWorkflowRequest }) => 
      createAgentWorkflow(agentId, workflow),
    {
      onSuccess: (data, variables) => {
        queryClient.invalidateQueries({ queryKey: workflowKeys.agent(variables.agentId) });
        toast.success('Workflow created successfully');
      },
    }
  )();
};

export const useUpdateAgentWorkflow = () => {
  const queryClient = useQueryClient();
  
  return createMutationHook(
    ({ agentId, workflowId, workflow }: { agentId: string; workflowId: string; workflow: UpdateWorkflowRequest }) => 
      updateAgentWorkflow(agentId, workflowId, workflow),
    {
      onSuccess: (data, variables) => {
        queryClient.invalidateQueries({ queryKey: workflowKeys.agent(variables.agentId) });
        toast.success('Workflow updated successfully');
      },
    }
  )();
};

export const useDeleteAgentWorkflow = () => {
  const queryClient = useQueryClient();
  
  return createMutationHook(
    ({ agentId, workflowId }: { agentId: string; workflowId: string }) => 
      deleteAgentWorkflow(agentId, workflowId),
    {
      onSuccess: (_, variables) => {
        queryClient.invalidateQueries({ queryKey: workflowKeys.agent(variables.agentId) });
        toast.success('Workflow deleted successfully');
      },
    }
  )();
};

export const useExecuteWorkflow = () => {
  const queryClient = useQueryClient();
  return createMutationHook(
    ({ agentId, workflowId, execution }: { agentId: string; workflowId: string; execution: ExecuteWorkflowRequest }) => 
      executeWorkflow(agentId, workflowId, execution),
    {
      onSuccess: (_, variables) => {
        queryClient.invalidateQueries({ queryKey: workflowKeys.executions(variables.agentId, variables.workflowId) });
        toast.success('Workflow execution started');
      },
    }
  )();
};

export const useWorkflowExecutions = (agentId: string, workflowId: string, limit: number = 20) => {
  return createQueryHook(
    workflowKeys.executions(agentId, workflowId),
    () => getWorkflowExecutions(agentId, workflowId, limit),
    {
      enabled: !!agentId && !!workflowId,
      staleTime: 10000, // 10 seconds
    }
  )();
};