|
|
@@ -8,6 +8,8 @@ import type {
|
|
|
SttEventMap,
|
|
|
ILanguageItem,
|
|
|
} from '../types'
|
|
|
+import { SUB_BOT_UID, PUB_BOT_UID, EXPERIENCE_DURATION } from '../utils/stt-constants'
|
|
|
+import { generateBotTokens, generateAuthorization } from '../utils/token-utils'
|
|
|
|
|
|
interface ApiSTTStartOptions {
|
|
|
uid: string | number
|
|
|
@@ -50,7 +52,6 @@ export class SttManagerAdapter extends AGEventEmitter<SttEventMap> implements IS
|
|
|
private _appId: string = ''
|
|
|
private _certificate: string = ''
|
|
|
private _gatewayAddress = 'https://api.agora.io'
|
|
|
- private _baseUrl = 'https://service.agora.io/toolbox-overseas'
|
|
|
|
|
|
constructor(rtmManager: any, appId: string, certificate: string) {
|
|
|
super()
|
|
|
@@ -146,7 +147,7 @@ export class SttManagerAdapter extends AGEventEmitter<SttEventMap> implements IS
|
|
|
taskId,
|
|
|
token,
|
|
|
startTime: Date.now(),
|
|
|
- duration: 3600000, // 1小时
|
|
|
+ duration: EXPERIENCE_DURATION,
|
|
|
}),
|
|
|
])
|
|
|
}
|
|
|
@@ -375,7 +376,7 @@ export class SttManagerAdapter extends AGEventEmitter<SttEventMap> implements IS
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
- Authorization: await this._genBasicAuthorization({ uid, channel }),
|
|
|
+ Authorization: await this._genAuthorization({ uid, channel }),
|
|
|
},
|
|
|
body: JSON.stringify(data),
|
|
|
})
|
|
|
@@ -394,16 +395,36 @@ export class SttManagerAdapter extends AGEventEmitter<SttEventMap> implements IS
|
|
|
const { channel, languages, token, uid } = options
|
|
|
const url = `${this._gatewayAddress}/v1/projects/${this._appId}/rtsc/speech-to-text/tasks?builderToken=${token}`
|
|
|
|
|
|
+ // 为bot生成token
|
|
|
+ let subBotToken: string | null = null
|
|
|
+ let pubBotToken: string | null = null
|
|
|
+
|
|
|
+ if (this._certificate) {
|
|
|
+ const botTokens = await generateBotTokens({
|
|
|
+ appId: this._appId,
|
|
|
+ appCertificate: this._certificate,
|
|
|
+ channel,
|
|
|
+ })
|
|
|
+ subBotToken = botTokens.subBotToken
|
|
|
+ pubBotToken = botTokens.pubBotToken
|
|
|
+ }
|
|
|
+
|
|
|
const body: any = {
|
|
|
languages: languages.map((item) => item.source),
|
|
|
maxIdleTime: 60,
|
|
|
rtcConfig: {
|
|
|
channelName: channel,
|
|
|
- subBotUid: '1000',
|
|
|
- pubBotUid: '2000',
|
|
|
+ subBotUid: SUB_BOT_UID,
|
|
|
+ pubBotUid: PUB_BOT_UID,
|
|
|
},
|
|
|
}
|
|
|
|
|
|
+ // 如果成功生成了bot token,添加到配置中
|
|
|
+ if (subBotToken && pubBotToken) {
|
|
|
+ body.rtcConfig.subBotToken = subBotToken
|
|
|
+ body.rtcConfig.pubBotToken = pubBotToken
|
|
|
+ }
|
|
|
+
|
|
|
if (languages.find((item) => item.target?.length)) {
|
|
|
body.translateConfig = {
|
|
|
forceTranslateInterval: 2,
|
|
|
@@ -496,42 +517,12 @@ export class SttManagerAdapter extends AGEventEmitter<SttEventMap> implements IS
|
|
|
uid: string | number
|
|
|
channel: string
|
|
|
}): Promise<string> {
|
|
|
- // 使用基础的Agora token(与主应用保持一致)
|
|
|
- return await this._genBasicAuthorization(config)
|
|
|
- }
|
|
|
-
|
|
|
- private async _genBasicAuthorization(config: {
|
|
|
- uid: string | number
|
|
|
- channel: string
|
|
|
- }): Promise<string> {
|
|
|
- // 使用基础的Agora token生成逻辑
|
|
|
- const { uid, channel } = config
|
|
|
- const url = `${this._baseUrl}/v2/token/generate`
|
|
|
-
|
|
|
- const data = {
|
|
|
+ // 使用工具函数生成Authorization头(带缓存机制)
|
|
|
+ return await generateAuthorization({
|
|
|
appId: this._appId,
|
|
|
appCertificate: this._certificate,
|
|
|
- channelName: channel,
|
|
|
- expire: 7200,
|
|
|
- src: 'web',
|
|
|
- types: [1, 2],
|
|
|
- uid: uid.toString(),
|
|
|
- }
|
|
|
-
|
|
|
- const response = await fetch(url, {
|
|
|
- method: 'POST',
|
|
|
- headers: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- },
|
|
|
- body: JSON.stringify(data),
|
|
|
+ uid: config.uid,
|
|
|
+ channel: config.channel,
|
|
|
})
|
|
|
-
|
|
|
- if (response.ok) {
|
|
|
- const result = await response.json()
|
|
|
- const token = result?.data?.token || ''
|
|
|
- return `agora token="${token}"`
|
|
|
- }
|
|
|
-
|
|
|
- return ''
|
|
|
}
|
|
|
}
|