2025-11-13 01:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 应用层 API 服务
|
|
|
|
|
|
* 封装应用特定的 API 调用,使用 mono 级别的服务
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
2025-11-25 00:58:51 +08:00
|
|
|
|
// 直接使用实例(最简单、最可靠)
|
|
|
|
|
|
import tokenManager from '@gold/utils/token-manager'
|
2025-11-13 01:06:28 +08:00
|
|
|
|
import { TikHubService } from '@gold/api/services'
|
2025-11-12 22:45:29 +08:00
|
|
|
|
import { API_BASE } from '@gold/config/api'
|
|
|
|
|
|
|
2025-11-13 01:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* TikHub API 基础路径
|
|
|
|
|
|
*/
|
|
|
|
|
|
const TIKHUB_BASE = API_BASE.TIKHUB_APP || API_BASE.TIKHUB || ''
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
2025-11-13 01:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 应用层通用服务
|
|
|
|
|
|
*/
|
2025-11-10 00:59:40 +08:00
|
|
|
|
export const CommonService = {
|
2025-11-13 01:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 视频转字符(音频转文字)
|
|
|
|
|
|
* 直接使用 mono 级别的 TikHub 服务
|
|
|
|
|
|
*/
|
2025-11-10 00:59:40 +08:00
|
|
|
|
videoToCharacters(data) {
|
2025-11-13 01:06:28 +08:00
|
|
|
|
return TikHubService.videoToCharacters(data)
|
2025-11-10 00:59:40 +08:00
|
|
|
|
},
|
2025-11-13 01:06:28 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 调用工作流
|
|
|
|
|
|
*/
|
2025-11-10 00:59:40 +08:00
|
|
|
|
callWorkflow(data) {
|
2025-11-13 01:06:28 +08:00
|
|
|
|
return TikHubService.callWorkflow(data)
|
2025-11-10 00:59:40 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
2025-11-13 01:06:28 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 流式调用工作流(SSE)
|
|
|
|
|
|
*/
|
|
|
|
|
|
async callWorkflowStream(options) {
|
2025-11-10 00:59:40 +08:00
|
|
|
|
const {
|
|
|
|
|
|
data,
|
|
|
|
|
|
ctrl,
|
|
|
|
|
|
onMessage,
|
|
|
|
|
|
onError,
|
|
|
|
|
|
onClose
|
|
|
|
|
|
} = options || {}
|
2025-11-25 00:58:51 +08:00
|
|
|
|
|
|
|
|
|
|
const authHeader = tokenManager.getAuthHeader()
|
|
|
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
|
let retryCount = 0
|
2025-11-13 01:06:28 +08:00
|
|
|
|
const maxRetries = 0
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
2025-11-13 01:06:28 +08:00
|
|
|
|
return fetchEventSource(`${TIKHUB_BASE}/callWorkflow`, {
|
2025-11-10 00:59:40 +08:00
|
|
|
|
method: 'post',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...(authHeader ? { Authorization: authHeader } : {})
|
|
|
|
|
|
},
|
|
|
|
|
|
openWhenHidden: true,
|
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
|
onmessage: onMessage,
|
|
|
|
|
|
onerror: (err) => {
|
|
|
|
|
|
retryCount++
|
2025-12-28 00:19:17 +08:00
|
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
|
if (typeof onError === 'function') {
|
|
|
|
|
|
onError(err)
|
|
|
|
|
|
}
|
2025-12-28 00:19:17 +08:00
|
|
|
|
|
2025-11-10 00:59:40 +08:00
|
|
|
|
if (retryCount > maxRetries) {
|
2025-11-13 01:06:28 +08:00
|
|
|
|
throw err
|
2025-11-10 00:59:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onclose: () => {
|
|
|
|
|
|
if (typeof onClose === 'function') {
|
|
|
|
|
|
onClose()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
signal: ctrl ? ctrl.signal : undefined
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default CommonService
|
|
|
|
|
|
|
|
|
|
|
|
|