send end of utterance events if using deepgram, interim events are enabled, and utterance_end_ms option is set (#772) (#782)

This commit is contained in:
Dave Horton
2024-06-13 13:18:32 -04:00
committed by GitHub
parent cafe149bdf
commit 76a3aa7f42

View File

@@ -215,7 +215,7 @@ class TaskTranscribe extends SttTask {
this._onVendorConnectFailure.bind(this, cs, ep, channel)); this._onVendorConnectFailure.bind(this, cs, ep, channel));
/* if app sets deepgramOptions.utteranceEndMs they essentially want continuous asr */ /* if app sets deepgramOptions.utteranceEndMs they essentially want continuous asr */
if (opts.DEEPGRAM_SPEECH_UTTERANCE_END_MS) this.isContinuousAsr = true; //if (opts.DEEPGRAM_SPEECH_UTTERANCE_END_MS) this.isContinuousAsr = true;
break; break;
case 'soniox': case 'soniox':
@@ -339,6 +339,12 @@ class TaskTranscribe extends SttTask {
if (this.vendor === 'deepgram' && evt.type === 'UtteranceEnd') { if (this.vendor === 'deepgram' && evt.type === 'UtteranceEnd') {
/* we will only get this when we have set utterance_end_ms */ /* we will only get this when we have set utterance_end_ms */
/* DH: send a speech event when we get UtteranceEnd if they want interim events */
if (this.interim) {
this.logger.debug('Gather:_onTranscription - got UtteranceEnd event from deepgram, sending speech event');
this._resolve(channel, evt);
}
if (bufferedTranscripts.length === 0) { if (bufferedTranscripts.length === 0) {
this.logger.debug('Gather:_onTranscription - got UtteranceEnd event from deepgram but no buffered transcripts'); this.logger.debug('Gather:_onTranscription - got UtteranceEnd event from deepgram but no buffered transcripts');
} }
@@ -448,26 +454,31 @@ class TaskTranscribe extends SttTask {
} }
async _resolve(channel, evt) { async _resolve(channel, evt) {
/* we've got a transcript, so end the otel child span for this channel */ if (evt.is_final) {
if (this.childSpan[channel - 1] && this.childSpan[channel - 1].span) { /* we've got a final transcript, so end the otel child span for this channel */
this.childSpan[channel - 1].span.setAttributes({ if (this.childSpan[channel - 1] && this.childSpan[channel - 1].span) {
channel, this.childSpan[channel - 1].span.setAttributes({
'stt.resolve': 'transcript', channel,
'stt.result': JSON.stringify(evt) 'stt.resolve': 'transcript',
}); 'stt.result': JSON.stringify(evt)
this.childSpan[channel - 1].span.end(); });
this.childSpan[channel - 1].span.end();
}
} }
if (this.transcriptionHook) { if (this.transcriptionHook) {
const b3 = this.getTracingPropagation(); const b3 = this.getTracingPropagation();
const httpHeaders = b3 && {b3}; const httpHeaders = b3 && {b3};
const payload = {
...this.cs.callInfo,
...httpHeaders,
...(evt.alternatives && {speech: evt}),
...(evt.type && {speechEvent: evt})
};
try { try {
const json = await this.cs.requestor.request('verb:hook', this.transcriptionHook, { this.logger.debug({payload}, 'sending transcriptionHook');
...this.cs.callInfo, const json = await this.cs.requestor.request('verb:hook', this.transcriptionHook, payload);
...httpHeaders, this.logger.info({json}, 'completed transcriptionHook');
speech: evt
});
this.logger.info({json}, 'sent transcriptionHook');
if (json && Array.isArray(json) && !this.parentTask) { if (json && Array.isArray(json) && !this.parentTask) {
const makeTask = require('./make_task'); const makeTask = require('./make_task');
const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata)); const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata));
@@ -488,7 +499,7 @@ class TaskTranscribe extends SttTask {
this._clearTimer(); this._clearTimer();
this.notifyTaskDone(); this.notifyTaskDone();
} }
else { else if (evt.is_final) {
/* start another child span for this channel */ /* start another child span for this channel */
const {span, ctx} = this.startChildSpan(`${STT_LISTEN_SPAN_NAME}:${channel}`); const {span, ctx} = this.startChildSpan(`${STT_LISTEN_SPAN_NAME}:${channel}`);
this.childSpan[channel - 1] = {span, ctx}; this.childSpan[channel - 1] = {span, ctx};