Files
sionrui/frontend/app/web-gold/src/api/common.js
2026-01-18 15:27:43 +08:00

73 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { fetchEventSource } from '@microsoft/fetch-event-source'
import tokenManager from '@gold/utils/token-manager'
import BaiLianService from './bailian'
import { API_BASE } from '@gold/config/api'
// 百炼API基础路径
const TIKHUB_BASE = API_BASE.TIKHUB_APP || API_BASE.TIKHUB || ''
// 应用层通用服务
export const CommonService = {
/**
* 视频转字符(音频转文字)
*/
videoToCharacters(data) {
return BaiLianService.videoToCharacters(data)
},
/**
* 调用工作流
*/
callWorkflow(data) {
return BaiLianService.callWorkflow(data)
},
/**
* 流式调用工作流SSE
*/
async callWorkflowStream(options) {
const {
data,
ctrl,
onMessage,
onError,
onClose
} = options || {}
const authHeader = tokenManager.getAuthHeader()
let retryCount = 0
const maxRetries = 0
return fetchEventSource(`${TIKHUB_BASE}/callWorkflow`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
...(authHeader ? { Authorization: authHeader } : {})
},
openWhenHidden: true,
body: JSON.stringify(data),
onmessage: onMessage,
onerror: (err) => {
retryCount++
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