All files / web/src/api workflows.ts

49.49% Statements 49/99
61.11% Branches 11/18
87.5% Functions 7/8
49.49% Lines 49/99

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 1121x 1x         1x   1x 3x     3x 1x 1x   1x 2x       2x 2x 2x 2x 1x 1x   1x 2x     2x 1x 1x   1x 1x       1x 1x   1x 1x       1x 1x   1x 2x             2x 2x 2x 2x 1x 1x   1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x   1x                                                    
import { apiClient } from './config'
import { isTauri, invokeCommand } from './utils'
import type { Workflow } from '@/types/generated/Workflow'
import type { ExecutionContext } from '@/types/generated/ExecutionContext'
import type { ExecutionSummary } from '@/types/generated/ExecutionSummary'
import type { ExecutionHistoryPage } from '@/types/generated/ExecutionHistoryPage'
import { API_ENDPOINTS } from '@/constants'
 
export const listWorkflows = async (): Promise<Workflow[]> => {
  if (isTauri()) {
    return invokeCommand<Workflow[]>('list_workflows')
  }
  const response = await apiClient.get<Workflow[]>(API_ENDPOINTS.WORKFLOW.LIST)
  return response.data
}
 
export const createWorkflow = async (workflow: Workflow): Promise<{ id: string }> => {
  if (isTauri()) {
    const result = await invokeCommand<Workflow>('create_workflow', { workflow })
    return { id: result.id }
  }
  const response = await apiClient.post<{ id: string }>(
    API_ENDPOINTS.WORKFLOW.CREATE,
    workflow
  )
  return response.data
}
 
export const getWorkflow = async (id: string): Promise<Workflow> => {
  if (isTauri()) {
    return invokeCommand<Workflow>('get_workflow', { id })
  }
  const response = await apiClient.get<Workflow>(API_ENDPOINTS.WORKFLOW.GET(id))
  return response.data
}
 
export const updateWorkflow = async (id: string, workflow: Workflow): Promise<void> => {
  if (isTauri()) {
    await invokeCommand('update_workflow', { id, workflow })
    return
  }
  await apiClient.put<void>(API_ENDPOINTS.WORKFLOW.UPDATE(id), workflow)
}
 
export const deleteWorkflow = async (id: string): Promise<void> => {
  if (isTauri()) {
    await invokeCommand('delete_workflow', { id })
    return
  }
  await apiClient.delete<void>(API_ENDPOINTS.WORKFLOW.DELETE(id))
}
 
export const executeInline = async (workflow: Workflow): Promise<ExecutionContext> => {
  if (isTauri()) {
    const { id } = await createWorkflow(workflow)
    return invokeCommand<ExecutionContext>('execute_workflow_sync', {
      workflow_id: id,
      input: {}
    })
  }
  const response = await apiClient.post<ExecutionContext>(
    API_ENDPOINTS.EXECUTION.INLINE_RUN,
    workflow
  )
  return response.data
}
 
export const submitWorkflow = async (
  id: string,
  initialVariables?: any
): Promise<{ execution_id: string }> => {
  if (isTauri()) {
    const taskId = await invokeCommand<string>('submit_workflow', {
      workflow_id: id,
      input: initialVariables || {}
    })
    return { execution_id: taskId }
  }
  const response = await apiClient.post<{ execution_id: string; workflow_id: string }>(
    API_ENDPOINTS.EXECUTION.SUBMIT(id),
    { initial_variables: initialVariables }
  )
  return { execution_id: response.data.execution_id }
}
 
export const listWorkflowExecutions = async (
  id: string,
  page = 1,
  pageSize = 20
): Promise<ExecutionHistoryPage> => {
  if (isTauri()) {
    const items = await invokeCommand<ExecutionSummary[]>('list_workflow_executions', {
      workflow_id: id,
      limit: pageSize
    })
    const total = items.length
    const resolvedPageSize = total > 0 ? Math.min(total, pageSize) : pageSize
    return {
      items,
      total,
      page: 1,
      page_size: resolvedPageSize,
      total_pages: total > 0 ? 1 : 0
    }
  }
  const response = await apiClient.get<ExecutionHistoryPage>(
    API_ENDPOINTS.EXECUTION.HISTORY(id),
    { params: { page, page_size: pageSize } }
  )
  return response.data
}