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.2KB

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