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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 = (url?: string) => {
  103. return useQuery({
  104. enabled: !!url,
  105. queryKey: [...useConversationVarValuesKey, url],
  106. queryFn: async () => {
  107. const { items } = (await get(url || '')) as { items: VarInInspect[] }
  108. return items
  109. },
  110. })
  111. }
  112. export const useInvalidateConversationVarValues = (url: string) => {
  113. return useInvalid([...useConversationVarValuesKey, url])
  114. }
  115. export const useResetConversationVar = (appId: string) => {
  116. return useMutation({
  117. mutationKey: [NAME_SPACE, 'reset conversation var', appId],
  118. mutationFn: async (varId: string) => {
  119. return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`)
  120. },
  121. })
  122. }
  123. export const useResetToLastRunValue = (appId: string) => {
  124. return useMutation({
  125. mutationKey: [NAME_SPACE, 'reset to last run value', appId],
  126. mutationFn: async (varId: string): Promise<{ value: any }> => {
  127. return put(`apps/${appId}/workflows/draft/variables/${varId}/reset`)
  128. },
  129. })
  130. }
  131. export const useSysVarValuesKey = [NAME_SPACE, 'sys-variable']
  132. export const useSysVarValues = (url?: string) => {
  133. return useQuery({
  134. enabled: !!url,
  135. queryKey: [...useSysVarValuesKey, url],
  136. queryFn: async () => {
  137. const { items } = (await get(url || '')) as { items: VarInInspect[] }
  138. return items
  139. },
  140. })
  141. }
  142. export const useInvalidateSysVarValues = (url: string) => {
  143. return useInvalid([...useSysVarValuesKey, url])
  144. }
  145. export const useDeleteAllInspectorVars = (appId: string) => {
  146. return useMutation({
  147. mutationKey: [NAME_SPACE, 'delete all inspector vars', appId],
  148. mutationFn: async () => {
  149. return del(`apps/${appId}/workflows/draft/variables`)
  150. },
  151. })
  152. }
  153. export const useDeleteNodeInspectorVars = (appId: string) => {
  154. return useMutation({
  155. mutationKey: [NAME_SPACE, 'delete node inspector vars', appId],
  156. mutationFn: async (nodeId: string) => {
  157. return del(`apps/${appId}/workflows/draft/nodes/${nodeId}/variables`)
  158. },
  159. })
  160. }
  161. export const useDeleteInspectVar = (appId: string) => {
  162. return useMutation({
  163. mutationKey: [NAME_SPACE, 'delete inspector var', appId],
  164. mutationFn: async (varId: string) => {
  165. return del(`apps/${appId}/workflows/draft/variables/${varId}`)
  166. },
  167. })
  168. }
  169. // edit the name or value of the inspector var
  170. export const useEditInspectorVar = (appId: string) => {
  171. return useMutation({
  172. mutationKey: [NAME_SPACE, 'edit inspector var', appId],
  173. mutationFn: async ({ varId, ...rest }: {
  174. varId: string
  175. name?: string
  176. value?: any
  177. }) => {
  178. return patch(`apps/${appId}/workflows/draft/variables/${varId}`, {
  179. body: rest,
  180. })
  181. },
  182. })
  183. }