From 19008ca485355bea295de40e22c7ef6c24e3a71b Mon Sep 17 00:00:00 2001 From: Hoan HL Date: Thu, 27 Nov 2025 15:27:29 +0700 Subject: [PATCH] wip --- lib/tasks/gather.js | 2 +- lib/tasks/stt-task.js | 7 ++++--- lib/tasks/transcribe.js | 24 +++++++++++++++++++++++- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/tasks/gather.js b/lib/tasks/gather.js index 6ccfda36..bc10c2d2 100644 --- a/lib/tasks/gather.js +++ b/lib/tasks/gather.js @@ -1161,7 +1161,7 @@ class TaskGather extends SttTask { } async _startFallback(cs, ep, evt) { - if (this.canFallback) { + if (this.canFallback()) { this._stopTranscribing(ep); try { this.logger.debug('gather:_startFallback'); diff --git a/lib/tasks/stt-task.js b/lib/tasks/stt-task.js index f02d9776..06cd4992 100644 --- a/lib/tasks/stt-task.js +++ b/lib/tasks/stt-task.js @@ -171,7 +171,7 @@ class SttTask extends Task { try { this.sttCredentials = await this._initSpeechCredentials(this.cs, this.vendor, this.label); } catch (error) { - if (this.canFallback) { + if (this.canFallback()) { this.notifyError( { msg: 'ASR error', details:`Invalid vendor ${this.vendor}, Error: ${error}`, @@ -340,11 +340,12 @@ class SttTask extends Task { return credentials; } - get canFallback() { + canFallback() { return this.fallbackVendor && this.isHandledByPrimaryProvider && !this.cs.hasFallbackAsr; } - // gather does not need to provide ep, but transcribe does as it might has 2 legs + // ep is optional for gather or any verb that have single ep, + // but transcribe does need as it might has 2 eps async _initFallback(ep) { assert(this.fallbackVendor, 'fallback failed without fallbackVendor configuration'); this.logger.info(`Failed to use primary STT provider, fallback to ${this.fallbackVendor}`); diff --git a/lib/tasks/transcribe.js b/lib/tasks/transcribe.js index 2ea6d199..e636d8c4 100644 --- a/lib/tasks/transcribe.js +++ b/lib/tasks/transcribe.js @@ -70,6 +70,9 @@ class TaskTranscribe extends SttTask { this._bufferedTranscripts = [ [], [] ]; // for channel 1 and 2 this.bugname_prefix = 'transcribe_'; this.paused = false; + // fallback flags + this.isHandledByPrimaryProviderForEp1 = true; + this.isHandledByPrimaryProviderForEp2 = true; } get name() { return TaskName.Transcribe; } @@ -776,7 +779,7 @@ class TaskTranscribe extends SttTask { } async _startFallback(cs, _ep, evt) { - if (this.canFallback) { + if (this.canFallback(_ep)) { _ep.stopTranscription({ vendor: this.vendor, bugname: this.bugname, @@ -895,6 +898,25 @@ class TaskTranscribe extends SttTask { if (this._asrTimer) clearTimeout(this._asrTimer); this._asrTimer = null; } + + // We need to keep track the fallback is happened for each endpoint + // override the canFallback and _initFallback methods to make sure that + // we only fallback once per endpoint + canFallback(ep) { + const isHandledByPrimaryProvider = ep === this.ep ? + this.isHandledByPrimaryProviderForEp1 : ep === this.ep2 ? + this.isHandledByPrimaryProviderForEp2 : false; + return this.fallbackVendor && isHandledByPrimaryProvider && !this.cs.hasFallbackAsr; + } + + _initFallback(ep) { + if (ep === this.ep) { + this.isHandledByPrimaryProviderForEp1 = false; + } else if (ep === this.ep2) { + this.isHandledByPrimaryProviderForEp2 = false; + } + return super._initFallback(ep); + } } module.exports = TaskTranscribe;