2025-11-10 00:59:40 +08:00
|
|
|
|
import http from '@/api/http'
|
|
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
2025-11-12 22:45:29 +08:00
|
|
|
|
import { getAuthHeader } from '@gold/utils/token-manager'
|
|
|
|
|
|
// 使用公共配置和 API 服务创建器
|
|
|
|
|
|
import { API_BASE } from '@gold/config/api'
|
|
|
|
|
|
import { createApiService } from '@gold/api/services'
|
|
|
|
|
|
// 初始化公共 hook 的 API 服务
|
|
|
|
|
|
import { setApiService } from '@gold/hooks/web/useVoiceText'
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io
|
2025-11-12 22:45:29 +08:00
|
|
|
|
// 注意:API_BASE.TIKHUB 不存在,应该使用 TIKHUB_APP
|
|
|
|
|
|
const SERVER_BASE = API_BASE.TIKHUB_APP || API_BASE.TIKHUB || ''
|
|
|
|
|
|
|
|
|
|
|
|
// 创建公共 API 服务实例
|
|
|
|
|
|
const apiService = createApiService({
|
|
|
|
|
|
http,
|
|
|
|
|
|
getAuthHeader,
|
|
|
|
|
|
baseUrl: SERVER_BASE,
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// 设置全局 API 服务(供 useVoiceText hook 使用)
|
|
|
|
|
|
setApiService(apiService)
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
|
|
export const CommonService = {
|
|
|
|
|
|
videoToCharacters(data) {
|
2025-11-12 22:45:29 +08:00
|
|
|
|
return apiService.videoToCharacters(data)
|
2025-11-10 00:59:40 +08:00
|
|
|
|
},
|
|
|
|
|
|
callWorkflow(data) {
|
2025-11-12 22:45:29 +08:00
|
|
|
|
return apiService.callWorkflow(data)
|
2025-11-10 00:59:40 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 流式调用 workflow
|
|
|
|
|
|
callWorkflowStream: async (options) => {
|
|
|
|
|
|
const {
|
|
|
|
|
|
data,
|
|
|
|
|
|
ctrl,
|
|
|
|
|
|
onMessage,
|
|
|
|
|
|
onError,
|
|
|
|
|
|
onClose
|
|
|
|
|
|
} = options || {}
|
|
|
|
|
|
|
|
|
|
|
|
const authHeader = getAuthHeader()
|
|
|
|
|
|
|
|
|
|
|
|
let retryCount = 0
|
|
|
|
|
|
const maxRetries = 0 // 禁用自动重试
|
|
|
|
|
|
|
|
|
|
|
|
return fetchEventSource(`${SERVER_BASE}/callWorkflow`, {
|
|
|
|
|
|
method: 'post',
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
|
...(authHeader ? { Authorization: authHeader } : {})
|
|
|
|
|
|
},
|
|
|
|
|
|
openWhenHidden: true,
|
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
|
onmessage: onMessage,
|
|
|
|
|
|
onerror: (err) => {
|
|
|
|
|
|
retryCount++
|
|
|
|
|
|
console.error('SSE错误,重试次数:', retryCount, err)
|
|
|
|
|
|
|
|
|
|
|
|
// 调用自定义错误处理
|
|
|
|
|
|
if (typeof onError === 'function') {
|
|
|
|
|
|
onError(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 超过最大重试次数,停止重连
|
|
|
|
|
|
if (retryCount > maxRetries) {
|
|
|
|
|
|
throw err // 抛出错误,停止自动重连
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onclose: () => {
|
|
|
|
|
|
// 调用自定义关闭处理
|
|
|
|
|
|
if (typeof onClose === 'function') {
|
|
|
|
|
|
onClose()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
signal: ctrl ? ctrl.signal : undefined
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default CommonService
|
|
|
|
|
|
|
|
|
|
|
|
|