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 6.4KB

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