You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-workflow.ts 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import { del, get, patch, post, put } from './base'
  2. import { useInfiniteQuery, useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
  3. import type {
  4. FetchWorkflowDraftPageParams,
  5. FetchWorkflowDraftPageResponse,
  6. FetchWorkflowDraftResponse,
  7. NodeTracing,
  8. PublishWorkflowParams,
  9. UpdateWorkflowParams,
  10. VarInInspect,
  11. WorkflowConfigResponse,
  12. } from '@/types/workflow'
  13. import type { CommonResponse } from '@/models/common'
  14. import { useInvalid, useReset } from './use-base'
  15. import type { FlowType } from '@/types/common'
  16. import { getFlowPrefix } from './utils'
  17. const NAME_SPACE = 'workflow'
  18. export const useAppWorkflow = (appID: string) => {
  19. return useQuery<FetchWorkflowDraftResponse>({
  20. enabled: !!appID,
  21. queryKey: [NAME_SPACE, 'publish', appID],
  22. queryFn: () => get<FetchWorkflowDraftResponse>(`/apps/${appID}/workflows/publish`),
  23. })
  24. }
  25. export const useInvalidateAppWorkflow = () => {
  26. const queryClient = useQueryClient()
  27. return (appID: string) => {
  28. queryClient.invalidateQueries(
  29. {
  30. queryKey: [NAME_SPACE, 'publish', appID],
  31. })
  32. }
  33. }
  34. export const useWorkflowConfig = <T = WorkflowConfigResponse>(url: string, onSuccess: (v: T) => void) => {
  35. return useQuery({
  36. enabled: !!url,
  37. queryKey: [NAME_SPACE, 'config', url],
  38. staleTime: 0,
  39. queryFn: async () => {
  40. const data = await get<T>(url)
  41. onSuccess(data)
  42. return data
  43. },
  44. })
  45. }
  46. const WorkflowVersionHistoryKey = [NAME_SPACE, 'versionHistory']
  47. export const useWorkflowVersionHistory = (params: FetchWorkflowDraftPageParams) => {
  48. const { url, initialPage, limit, userId, namedOnly } = params
  49. return useInfiniteQuery({
  50. enabled: !!url,
  51. queryKey: [...WorkflowVersionHistoryKey, url, initialPage, limit, userId, namedOnly],
  52. queryFn: ({ pageParam = 1 }) => get<FetchWorkflowDraftPageResponse>(url, {
  53. params: {
  54. page: pageParam,
  55. limit,
  56. user_id: userId || '',
  57. named_only: !!namedOnly,
  58. },
  59. }),
  60. getNextPageParam: lastPage => lastPage.has_more ? lastPage.page + 1 : null,
  61. initialPageParam: initialPage,
  62. })
  63. }
  64. export const useResetWorkflowVersionHistory = () => {
  65. return useReset([...WorkflowVersionHistoryKey])
  66. }
  67. export const useUpdateWorkflow = () => {
  68. return useMutation({
  69. mutationKey: [NAME_SPACE, 'update'],
  70. mutationFn: (params: UpdateWorkflowParams) => patch(params.url, {
  71. body: {
  72. marked_name: params.title,
  73. marked_comment: params.releaseNotes,
  74. },
  75. }),
  76. })
  77. }
  78. export const useDeleteWorkflow = () => {
  79. return useMutation({
  80. mutationKey: [NAME_SPACE, 'delete'],
  81. mutationFn: (url: string) => del(url),
  82. })
  83. }
  84. export const usePublishWorkflow = () => {
  85. return useMutation({
  86. mutationKey: [NAME_SPACE, 'publish'],
  87. mutationFn: (params: PublishWorkflowParams) => post<CommonResponse & { created_at: number }>(params.url, {
  88. body: {
  89. marked_name: params.title,
  90. marked_comment: params.releaseNotes,
  91. },
  92. }),
  93. })
  94. }
  95. const useLastRunKey = [NAME_SPACE, 'last-run']
  96. export const useLastRun = (flowType: FlowType, flowId: string, nodeId: string, enabled: boolean) => {
  97. return useQuery<NodeTracing>({
  98. enabled,
  99. queryKey: [...useLastRunKey, flowType, flowId, nodeId],
  100. queryFn: async () => {
  101. return get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/nodes/${nodeId}/last-run`, {}, {
  102. silent: true,
  103. })
  104. },
  105. retry: 0,
  106. })
  107. }
  108. export const useInvalidLastRun = (flowType: FlowType, flowId: string, nodeId: string) => {
  109. return useInvalid([...useLastRunKey, flowType, flowId, nodeId])
  110. }
  111. // Rerun workflow or change the version of workflow
  112. export const useInvalidAllLastRun = (flowType?: FlowType, flowId?: string) => {
  113. return useInvalid([NAME_SPACE, flowType, 'last-run', flowId])
  114. }
  115. export const useConversationVarValues = (flowType?: FlowType, flowId?: string) => {
  116. return useQuery({
  117. enabled: !!flowId,
  118. queryKey: [NAME_SPACE, flowType, 'conversation var values', flowId],
  119. queryFn: async () => {
  120. const { items } = (await get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/conversation-variables`)) as { items: VarInInspect[] }
  121. return items
  122. },
  123. })
  124. }
  125. export const useInvalidateConversationVarValues = (flowType: FlowType, flowId: string) => {
  126. return useInvalid([NAME_SPACE, flowType, 'conversation var values', flowId])
  127. }
  128. export const useResetConversationVar = (flowType: FlowType, flowId: string) => {
  129. return useMutation({
  130. mutationKey: [NAME_SPACE, flowType, 'reset conversation var', flowId],
  131. mutationFn: async (varId: string) => {
  132. return put(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}/reset`)
  133. },
  134. })
  135. }
  136. export const useResetToLastRunValue = (flowType: FlowType, flowId: string) => {
  137. return useMutation({
  138. mutationKey: [NAME_SPACE, flowType, 'reset to last run value', flowId],
  139. mutationFn: async (varId: string): Promise<{ value: any }> => {
  140. return put(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}/reset`)
  141. },
  142. })
  143. }
  144. export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable']
  145. export const useSysVarValues = (flowType?: FlowType, flowId?: string) => {
  146. return useQuery({
  147. enabled: !!flowId,
  148. queryKey: [NAME_SPACE, flowType, 'sys var values', flowId],
  149. queryFn: async () => {
  150. const { items } = (await get(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/system-variables`)) as { items: VarInInspect[] }
  151. return items
  152. },
  153. })
  154. }
  155. export const useInvalidateSysVarValues = (flowType: FlowType, flowId: string) => {
  156. return useInvalid([NAME_SPACE, flowType, 'sys var values', flowId])
  157. }
  158. export const useDeleteAllInspectorVars = (flowType: FlowType, flowId: string) => {
  159. return useMutation({
  160. mutationKey: [NAME_SPACE, flowType, 'delete all inspector vars', flowId],
  161. mutationFn: async () => {
  162. return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables`)
  163. },
  164. })
  165. }
  166. export const useDeleteNodeInspectorVars = (flowType: FlowType, flowId: string) => {
  167. return useMutation({
  168. mutationKey: [NAME_SPACE, flowType, 'delete node inspector vars', flowId],
  169. mutationFn: async (nodeId: string) => {
  170. return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/nodes/${nodeId}/variables`)
  171. },
  172. })
  173. }
  174. export const useDeleteInspectVar = (flowType: FlowType, flowId: string) => {
  175. return useMutation({
  176. mutationKey: [NAME_SPACE, flowType, 'delete inspector var', flowId],
  177. mutationFn: async (varId: string) => {
  178. return del(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}`)
  179. },
  180. })
  181. }
  182. // edit the name or value of the inspector var
  183. export const useEditInspectorVar = (flowType: FlowType, flowId: string) => {
  184. return useMutation({
  185. mutationKey: [NAME_SPACE, flowType, 'edit inspector var', flowId],
  186. mutationFn: async ({ varId, ...rest }: {
  187. varId: string
  188. name?: string
  189. value?: any
  190. }) => {
  191. return patch(`${getFlowPrefix(flowType)}/${flowId}/workflows/draft/variables/${varId}`, {
  192. body: rest,
  193. })
  194. },
  195. })
  196. }