73 lines
1.6 KiB
JavaScript
73 lines
1.6 KiB
JavaScript
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
|