| 1 | import type { ConfigEditorConfigDto, ConfigEditorConfigVo } from './config-editor.types' |
| 2 | import { getAccountListApi } from '@/api/accounts/account.api' |
| 3 | import http from '@/utils/request' |
| 4 | import { ConfigEditorServiceTarget } from './config-editor.types' |
| 5 | |
| 6 | function getConfigEditorRoute(target: ConfigEditorServiceTarget, path = '') { |
| 7 | const baseRoute = target === ConfigEditorServiceTarget.Ai ? 'ai/config' : 'config' |
| 8 | return path ? `${baseRoute}/${path}` : baseRoute |
| 9 | } |
| 10 | |
| 11 | export function getConfigEditorConfigApi(target = ConfigEditorServiceTarget.Server, silent = true) { |
| 12 | return http.get<ConfigEditorConfigVo>(getConfigEditorRoute(target), undefined, silent) |
| 13 | } |
| 14 | |
| 15 | export function validateConfigEditorConfigApi(data: ConfigEditorConfigDto, target = ConfigEditorServiceTarget.Server, silent = true) { |
| 16 | return http.post<void>(getConfigEditorRoute(target, 'validate'), data, silent) |
| 17 | } |
| 18 | |
| 19 | export function saveConfigEditorConfigApi(data: ConfigEditorConfigDto, target = ConfigEditorServiceTarget.Server, silent = true) { |
| 20 | return http.put<void>(getConfigEditorRoute(target), data, silent) |
| 21 | } |
| 22 | |
| 23 | export function restartConfigEditorServiceApi(target = ConfigEditorServiceTarget.Server, silent = true) { |
| 24 | return http.post<void>(getConfigEditorRoute(target, 'restart'), undefined, silent) |
| 25 | } |
| 26 | |
| 27 | export async function checkConfigEditorConfigReadyApi(target = ConfigEditorServiceTarget.Server, timeoutMs = 2500) { |
| 28 | const timeout = new Promise<false>(resolve => setTimeout(() => resolve(false), timeoutMs)) |
| 29 | |
| 30 | if (target === ConfigEditorServiceTarget.Server) { |
| 31 | return Promise.race([ |
| 32 | getAccountListApi(true) |
| 33 | .then(response => !!response && response.code === 0) |
| 34 | .catch(() => false), |
| 35 | timeout, |
| 36 | ]) |
| 37 | } |
| 38 | |
| 39 | const readiness = getConfigEditorConfigApi(target, true) |
| 40 | .then(response => !!response && response.code === 0) |
| 41 | .catch(() => false) |
| 42 | |
| 43 | return Promise.race([readiness, timeout]) |
| 44 | } |
| 45 |