68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
import http from '@/api/http'
|
||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||
import { getAuthHeader } from '@/utils/token-manager'
|
||
|
||
// 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io
|
||
const SERVER_BASE = '/webApi/admin-api/ai/tikHup'
|
||
|
||
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
|
||
|
||
|