workflow.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type { Fetcher } from 'swr'
  2. import { get, post } from './base'
  3. import type { CommonResponse } from '@/models/common'
  4. import type {
  5. ChatRunHistoryResponse,
  6. ConversationVariableResponse,
  7. FetchWorkflowDraftResponse,
  8. NodesDefaultConfigsResponse,
  9. WorkflowRunHistoryResponse,
  10. } from '@/types/workflow'
  11. import type { BlockEnum } from '@/app/components/workflow/types'
  12. export const fetchWorkflowDraft = (url: string) => {
  13. return get(url, {}, { silent: true }) as Promise<FetchWorkflowDraftResponse>
  14. }
  15. export const syncWorkflowDraft = ({ url, params }: { url: string; params: Pick<FetchWorkflowDraftResponse, 'graph' | 'features' | 'environment_variables' | 'conversation_variables'> }) => {
  16. return post<CommonResponse & { updated_at: number; hash: string }>(url, { body: params }, { silent: true })
  17. }
  18. export const fetchNodesDefaultConfigs: Fetcher<NodesDefaultConfigsResponse, string> = (url) => {
  19. return get<NodesDefaultConfigsResponse>(url)
  20. }
  21. export const fetchWorkflowRunHistory: Fetcher<WorkflowRunHistoryResponse, string> = (url) => {
  22. return get<WorkflowRunHistoryResponse>(url)
  23. }
  24. export const fetchChatRunHistory: Fetcher<ChatRunHistoryResponse, string> = (url) => {
  25. return get<ChatRunHistoryResponse>(url)
  26. }
  27. export const singleNodeRun = (appId: string, nodeId: string, params: object) => {
  28. return post(`apps/${appId}/workflows/draft/nodes/${nodeId}/run`, { body: params })
  29. }
  30. export const getIterationSingleNodeRunUrl = (isChatFlow: boolean, appId: string, nodeId: string) => {
  31. return `apps/${appId}/${isChatFlow ? 'advanced-chat/' : ''}workflows/draft/iteration/nodes/${nodeId}/run`
  32. }
  33. export const publishWorkflow = (url: string) => {
  34. return post<CommonResponse & { created_at: number }>(url)
  35. }
  36. export const fetchPublishedWorkflow: Fetcher<FetchWorkflowDraftResponse, string> = (url) => {
  37. return get<FetchWorkflowDraftResponse>(url)
  38. }
  39. export const stopWorkflowRun = (url: string) => {
  40. return post<CommonResponse>(url)
  41. }
  42. export const fetchNodeDefault = (appId: string, blockType: BlockEnum, query = {}) => {
  43. return get(`apps/${appId}/workflows/default-workflow-block-configs/${blockType}`, {
  44. params: { q: JSON.stringify(query) },
  45. })
  46. }
  47. // TODO: archived
  48. export const updateWorkflowDraftFromDSL = (appId: string, data: string) => {
  49. return post<FetchWorkflowDraftResponse>(`apps/${appId}/workflows/draft/import`, { body: { data } })
  50. }
  51. export const fetchCurrentValueOfConversationVariable: Fetcher<ConversationVariableResponse, { url: string; params: { conversation_id: string } }> = ({ url, params }) => {
  52. return get<ConversationVariableResponse>(url, { params })
  53. }