Files
sionrui/frontend/app/web-gold/src/api/common.js

73 lines
1.6 KiB
JavaScript
Raw Normal View History

2025-11-10 00:59:40 +08:00
import { fetchEventSource } from '@microsoft/fetch-event-source'
import tokenManager from '@gold/utils/token-manager'
2026-01-18 15:27:43 +08:00
import BaiLianService from './bailian'
2025-11-12 22:45:29 +08:00
import { API_BASE } from '@gold/config/api'
2026-01-18 15:27:43 +08:00
// 百炼API基础路径
2025-11-13 01:06:28 +08:00
const TIKHUB_BASE = API_BASE.TIKHUB_APP || API_BASE.TIKHUB || ''
2025-11-10 00:59:40 +08:00
2026-01-18 15:27:43 +08:00
// 应用层通用服务
2025-11-10 00:59:40 +08:00
export const CommonService = {
2025-11-13 01:06:28 +08:00
/**
* 视频转字符音频转文字
*/
2025-11-10 00:59:40 +08:00
videoToCharacters(data) {
2026-01-18 15:27:43 +08:00
return BaiLianService.videoToCharacters(data)
2025-11-10 00:59:40 +08:00
},
2025-11-13 01:06:28 +08:00
/**
* 调用工作流
*/
2025-11-10 00:59:40 +08:00
callWorkflow(data) {
2026-01-18 15:27:43 +08:00
return BaiLianService.callWorkflow(data)
2025-11-10 00:59:40 +08:00
},
2026-01-18 15:27:43 +08:00
2025-11-13 01:06:28 +08:00
/**
* 流式调用工作流SSE
*/
async callWorkflowStream(options) {
2025-11-10 00:59:40 +08:00
const {
data,
ctrl,
onMessage,
onError,
onClose
} = options || {}
const authHeader = tokenManager.getAuthHeader()
2025-11-10 00:59:40 +08:00
let retryCount = 0
2025-11-13 01:06:28 +08:00
const maxRetries = 0
2026-01-18 15:27:43 +08:00
2025-11-13 01:06:28 +08:00
return fetchEventSource(`${TIKHUB_BASE}/callWorkflow`, {
2025-11-10 00:59:40 +08:00
method: 'post',
headers: {
'Content-Type': 'application/json',
...(authHeader ? { Authorization: authHeader } : {})
},
openWhenHidden: true,
body: JSON.stringify(data),
onmessage: onMessage,
onerror: (err) => {
retryCount++
2025-11-10 00:59:40 +08:00
if (typeof onError === 'function') {
onError(err)
}
2025-11-10 00:59:40 +08:00
if (retryCount > maxRetries) {
2025-11-13 01:06:28 +08:00
throw err
2025-11-10 00:59:40 +08:00
}
},
onclose: () => {
if (typeof onClose === 'function') {
onClose()
}
},
signal: ctrl ? ctrl.signal : undefined
})
}
}
export default CommonService