File size: 5,243 Bytes
98277cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import axios from 'axios'
import type { ApiError } from './api'
import { apiGetWrapped, apiPostWrapped, http, unwrapApiResponse } from './api'

export type TaskStatus =
  | 'queued'
  | 'running'
  | 'retrying'
  | 'fallback_running'
  | 'waiting_rpa'
  | 'rpa_running'
  | 'rpa_imported'
  | 'rpa_failed'
  | 'risk_paused'
  | 'succeeded'
  | 'failed'

export type TaskError = {
  kind?: string
  message?: string
  [key: string]: unknown
}

export type TaskRecord = {
  id: string
  status: TaskStatus
  task_type: string
  target: string
  payload: Record<string, unknown>
  engine?: string | null
  callback?: unknown
  created: number
  started?: number | null
  finished?: number | null
  retry_count: number
  error?: TaskError | null
}

export type TaskListResponse = {
  tasks: TaskRecord[]
  total: number
  limit: number
  offset: number
}

export type TaskStatusResponse = {
  task: TaskRecord
}

export type TaskCreateRequest = {
  task_type: string
  target?: string | null
  engine?: string | null
  payload?: Record<string, unknown>
}

export type TaskCreateResponse = {
  task: TaskRecord
}

export type TaskResultResponse = {
  task_id: string
  status: TaskStatus
  raw: unknown | null
  normalized: unknown | null
  meta: Record<string, unknown>
}

export type ListTasksParams = {
  limit: number
  offset: number
  status?: string[]
  task_type?: string[]
  engine?: string[]
  error_kind?: string[]
  sort?: string
}

function normalizePath(path: string) {
  return path.replace(/^\/+/, '')
}

function toApiError(error: unknown): ApiError {
  if (axios.isAxiosError(error)) {
    if (!error.response) {
      return { status: 0, message: error.message }
    }
    const status = error.response?.status ?? 0
    const body = error.response?.data
    const wrapped = unwrapApiResponse<unknown>(body)
    const message =
      wrapped.ok || wrapped.error === '响应缺少 code/msg 字段'
        ? error.message
        : wrapped.error
    return { status, message, data: body }
  }

  return {
    status: 0,
    message: error instanceof Error ? error.message : String(error),
  }
}

function join(values?: string[]) {
  const parts = (values || []).map((v) => String(v).trim()).filter((v) => v !== '')
  return parts.length ? parts.join(',') : undefined
}

export async function listTasks(params: ListTasksParams) {
  const search = new URLSearchParams()
  search.set('limit', String(params.limit))
  search.set('offset', String(params.offset))
  const status = join(params.status)
  const taskType = join(params.task_type)
  const engine = join(params.engine)
  const errorKind = join(params.error_kind)
  const sort = String(params.sort || '').trim()

  if (status) search.set('status', status)
  if (taskType) search.set('task_type', taskType)
  if (engine) search.set('engine', engine)
  if (errorKind) search.set('error_kind', errorKind)
  if (sort) search.set('sort', sort)

  return apiGetWrapped<TaskListResponse>(`tasks?${search.toString()}`)
}

export async function createTask(payload: TaskCreateRequest) {
  const res = await apiPostWrapped<TaskCreateResponse, TaskCreateRequest>('tasks', payload)
  return res.task
}

export async function retryTask(taskId: string) {
  const res = await apiPostWrapped<TaskStatusResponse, {}>(`tasks/${encodeURIComponent(taskId)}/retry`, {})
  return res.task
}

export async function cancelTask(taskId: string) {
  const res = await apiPostWrapped<TaskStatusResponse, {}>(`tasks/${encodeURIComponent(taskId)}/cancel`, {})
  return res.task
}

export async function markTaskRpa(taskId: string) {
  const res = await apiPostWrapped<TaskStatusResponse, {}>(`tasks/${encodeURIComponent(taskId)}/mark-rpa`, {})
  return res.task
}

export async function getTask(taskId: string) {
  const res = await apiGetWrapped<TaskStatusResponse>(`tasks/${encodeURIComponent(taskId)}`)
  return res.task
}

export async function getTaskResult(taskId: string, signal?: AbortSignal) {
  try {
    const res = await http.get<unknown>(normalizePath(`tasks/${encodeURIComponent(taskId)}/result`), {
      validateStatus: () => true,
      signal,
    })
    if (res.status === 409) return { ready: false as const, body: res.data }
    if (res.status !== 200) {
      const wrapped = unwrapApiResponse<unknown>(res.data)
      const message =
        wrapped.ok || wrapped.error === '响应缺少 code/msg 字段'
          ? `HTTP ${res.status}`
          : wrapped.error
      const apiError: ApiError = { status: res.status, message, data: res.data }
      throw apiError
    }

    const wrapped = unwrapApiResponse<TaskResultResponse>(res.data)
    if (!wrapped.ok) {
      const apiError: ApiError = { status: res.status, message: wrapped.error, data: res.data }
      throw apiError
    }

    return { ready: true as const, data: wrapped.data ?? null }
  } catch (e) {
    if (axios.isAxiosError(e) && e.code === 'ERR_CANCELED') {
      throw new DOMException('Aborted', 'AbortError')
    }
    if (
      !!e &&
      typeof e === 'object' &&
      'status' in e &&
      typeof (e as { status: unknown }).status === 'number' &&
      'message' in e &&
      typeof (e as { message: unknown }).message === 'string'
    ) {
      throw e as ApiError
    }
    throw toApiError(e)
  }
}