2025-11-10 00:59:40 +08:00
|
|
|
|
import http from '@/api/http'
|
|
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
|
|
|
|
|
import { getAuthHeader } from '@/utils/token-manager'
|
2025-11-11 23:51:17 +08:00
|
|
|
|
import { API_BASE } from '@/api/config'
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io
|
2025-11-11 23:51:17 +08:00
|
|
|
|
const SERVER_BASE = API_BASE.TIKHUB
|
2025-11-10 00:59:40 +08:00
|
|
|
|
|
|
|
|
|
|
export const CommonService = {
|
|
|
|
|
|
videoToCharacters(data) {
|
|
|
|
|
|
return http.post(`${SERVER_BASE}/videoToCharacters2`, data)
|
|
|
|
|
|
},
|
|
|
|
|
|
callWorkflow(data) {
|
|
|
|
|
|
return http.post(`${SERVER_BASE}/callWorkflow`, data)
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 流式调用 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
|
|
|
|
|
|
|
|
|
|
|
|
|