mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2026-01-25 02:07:56 +00:00
Compare commits
39 Commits
feat/tts_s
...
feat/fd_19
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
37dd80f0c7 | ||
|
|
2468557aef | ||
|
|
3c3dfa81d3 | ||
|
|
ca8e506d1a | ||
|
|
961c2589ac | ||
|
|
e4ec0025c3 | ||
|
|
ba275ef547 | ||
|
|
83a8cf6d25 | ||
|
|
09220872ae | ||
|
|
fdce05fa40 | ||
|
|
3bd1dd6323 | ||
|
|
54dc172ebd | ||
|
|
e007e0e2d3 | ||
|
|
c5cd488fdf | ||
|
|
57982335e0 | ||
|
|
5cea91e18a | ||
|
|
e396b6aa98 | ||
|
|
9104ebb603 | ||
|
|
1ad0261336 | ||
|
|
7802822773 | ||
|
|
edb4d21ce1 | ||
|
|
8048e9cf88 | ||
|
|
451feafed4 | ||
|
|
7f1543a0f3 | ||
|
|
83955ba972 | ||
|
|
a5fa5fce5b | ||
|
|
cc1751f500 | ||
|
|
1a1f53aede | ||
|
|
1984b6d3ea | ||
|
|
769b66f57e | ||
|
|
98b845f489 | ||
|
|
f92b1dbc97 | ||
|
|
0442144793 | ||
|
|
2de24af169 | ||
|
|
a884880321 | ||
|
|
b307df79d0 | ||
|
|
77bd11dd47 | ||
|
|
46d56fe546 | ||
|
|
30ab281ea2 |
@@ -119,7 +119,7 @@ const ENCRYPTION_SECRET = process.env.ENCRYPTION_SECRET;
|
||||
const HTTP_POOL = process.env.HTTP_POOL && parseInt(process.env.HTTP_POOL);
|
||||
const HTTP_POOLSIZE = parseInt(process.env.HTTP_POOLSIZE, 10) || 10;
|
||||
const HTTP_PIPELINING = parseInt(process.env.HTTP_PIPELINING, 10) || 1;
|
||||
const HTTP_TIMEOUT = 10000;
|
||||
const HTTP_TIMEOUT = parseInt(process.env.JAMBONES_HTTP_TIMEOUT, 10) || 10000;
|
||||
const HTTP_PROXY_IP = process.env.JAMBONES_HTTP_PROXY_IP;
|
||||
const HTTP_PROXY_PORT = process.env.JAMBONES_HTTP_PROXY_PORT;
|
||||
const HTTP_PROXY_PROTOCOL = process.env.JAMBONES_HTTP_PROXY_PROTOCOL || 'http';
|
||||
@@ -139,6 +139,11 @@ const JAMBONES_USE_FREESWITCH_TIMER_FD = process.env.JAMBONES_USE_FREESWITCH_TIM
|
||||
const JAMBONES_DIAL_SBC_FOR_REGISTERED_USER = process.env.JAMBONES_DIAL_SBC_FOR_REGISTERED_USER || false;
|
||||
const JAMBONES_MEDIA_TIMEOUT_MS = process.env.JAMBONES_MEDIA_TIMEOUT_MS || 0;
|
||||
const JAMBONES_MEDIA_HOLD_TIMEOUT_MS = process.env.JAMBONES_MEDIA_HOLD_TIMEOUT_MS || 0;
|
||||
const JAMBONES_WEBHOOK_ERROR_RETURN = parseInt(process.env.JAMBONES_WEBHOOK_ERROR_RETURN, 10) || 480;
|
||||
|
||||
/* say / tts */
|
||||
const JAMBONES_SAY_CHUNK_SIZE = parseInt(process.env.JAMBONES_SAY_CHUNK_SIZE, 10) || 900;
|
||||
|
||||
// jambonz
|
||||
const JAMBONES_TRANSCRIBE_EP_DESTROY_DELAY_MS =
|
||||
process.env.JAMBONES_TRANSCRIBE_EP_DESTROY_DELAY_MS;
|
||||
@@ -231,5 +236,7 @@ module.exports = {
|
||||
JAMBONES_DIAL_SBC_FOR_REGISTERED_USER,
|
||||
JAMBONES_MEDIA_TIMEOUT_MS,
|
||||
JAMBONES_MEDIA_HOLD_TIMEOUT_MS,
|
||||
JAMBONES_SAY_CHUNK_SIZE,
|
||||
JAMBONES_TRANSCRIBE_EP_DESTROY_DELAY_MS,
|
||||
JAMBONES_WEBHOOK_ERROR_RETURN
|
||||
};
|
||||
|
||||
@@ -291,7 +291,7 @@ router.post('/',
|
||||
}, {
|
||||
...(account.enable_debug_log && {level: 'debug'})
|
||||
});
|
||||
app.requestor.logger = app.notifier.logger = sipLogger;
|
||||
app.requestor.logger = app.notifier.logger = restDial.logger = sipLogger;
|
||||
const callInfo = new CallInfo({
|
||||
direction: CallDirection.Outbound,
|
||||
req: inviteReq,
|
||||
|
||||
@@ -12,7 +12,8 @@ const RootSpan = require('./utils/call-tracer');
|
||||
const listTaskNames = require('./utils/summarize-tasks');
|
||||
const {
|
||||
JAMBONES_MYSQL_REFRESH_TTL,
|
||||
JAMBONES_DISABLE_DIRECT_P2P_CALL
|
||||
JAMBONES_DISABLE_DIRECT_P2P_CALL,
|
||||
JAMBONES_WEBHOOK_ERROR_RETURN
|
||||
} = require('./config');
|
||||
const { createJambonzApp } = require('./dynamic-apps');
|
||||
const { decrypt } = require('./utils/encrypt-decrypt');
|
||||
@@ -480,7 +481,7 @@ module.exports = function(srf, logger) {
|
||||
message: `${err?.message}`.trim()
|
||||
}).catch((err) => this.logger.info({err}, 'Error generating alert for parsing application'));
|
||||
logger.info({err}, `Error retrieving or parsing application: ${err?.message}`);
|
||||
res.send(480, {headers: {'X-Reason': err?.message || 'unknown'}});
|
||||
res.send(JAMBONES_WEBHOOK_ERROR_RETURN, {headers: {'X-Reason': err?.message || 'unknown'}});
|
||||
app.requestor.close(WS_CLOSE_CODES.GoingAway);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ class CallInfo {
|
||||
let srf;
|
||||
this.direction = opts.direction;
|
||||
this.traceId = opts.traceId;
|
||||
this.hasRecording = false;
|
||||
this.callTerminationBy = undefined;
|
||||
if (opts.req) {
|
||||
const u = opts.req.getParsedHeader('from');
|
||||
|
||||
@@ -504,7 +504,12 @@ class CallSession extends Emitter {
|
||||
}
|
||||
|
||||
get isTtsStreamEnabled() {
|
||||
return this.backgroundTaskManager.isTaskRunning('ttsStream');
|
||||
// 1st background tts stream
|
||||
return this.backgroundTaskManager.isTaskRunning('ttsStream') ||
|
||||
// 2nd current task streaming tts
|
||||
TaskName.Say === this.currentTask?.name && this.currentTask?.isStreamingTts ||
|
||||
// 3rd nested verb is streaming tts
|
||||
TaskName.Gather === this.currentTask?.name && this.currentTask.sayTask?.isStreamingTts;
|
||||
}
|
||||
|
||||
get isListenEnabled() {
|
||||
@@ -658,6 +663,15 @@ class CallSession extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
// disableTtsCache
|
||||
get disableTtsCache() {
|
||||
return this._disableTtsCache || false;
|
||||
}
|
||||
|
||||
set disableTtsCache(d) {
|
||||
this._disableTtsCache = d;
|
||||
}
|
||||
|
||||
getTsStreamingVendor() {
|
||||
let v;
|
||||
if (this.currentTask?.isStreamingTts) {
|
||||
@@ -742,69 +756,101 @@ class CallSession extends Emitter {
|
||||
return this._fillerNoise;
|
||||
}
|
||||
|
||||
async pauseOrResumeBackgroundListenIfRequired(action, silence = false) {
|
||||
if ((action == 'pauseCallRecording' || action == 'resumeCallRecording') &&
|
||||
this.backgroundTaskManager.isTaskRunning('record')) {
|
||||
this.logger.debug({action, silence}, 'CallSession:pauseOrResumeBackgroundListenIfRequired');
|
||||
const backgroundListenTask = this.backgroundTaskManager.getTask('record');
|
||||
const status = action === 'pauseCallRecording' ? ListenStatus.Pause : ListenStatus.Resume;
|
||||
backgroundListenTask.updateListen(
|
||||
status,
|
||||
silence
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async notifyRecordOptions(opts) {
|
||||
const {action, silence} = opts;
|
||||
const {action, silence = false, type = 'siprec'} = opts;
|
||||
this.logger.debug({opts}, 'CallSession:notifyRecordOptions');
|
||||
|
||||
this.pauseOrResumeBackgroundListenIfRequired(action, silence);
|
||||
|
||||
/* if we have not answered yet, just save the details for later */
|
||||
if (!this.dlg) {
|
||||
if (action === 'startCallRecording') {
|
||||
this.recordOptions = opts;
|
||||
return true;
|
||||
if (type == 'cloud') {
|
||||
switch (action) {
|
||||
case 'pauseCallRecording':
|
||||
if (this.backgroundTaskManager.isTaskRunning('record')) {
|
||||
this.logger.debug({action, silence, type}, 'CallSession:cloudRecording');
|
||||
const backgroundListenTask = this.backgroundTaskManager.getTask('record');
|
||||
backgroundListenTask.updateListen(
|
||||
ListenStatus.Pause,
|
||||
silence
|
||||
);
|
||||
return true;
|
||||
} else { return false; }
|
||||
case 'resumeCallRecording':
|
||||
if (this.backgroundTaskManager.isTaskRunning('record')) {
|
||||
this.logger.debug({action, silence, type}, 'CallSession:cloudRecording');
|
||||
const backgroundListenTask = this.backgroundTaskManager.getTask('record');
|
||||
backgroundListenTask.updateListen(
|
||||
ListenStatus.Resume,
|
||||
silence
|
||||
);
|
||||
return true;
|
||||
} else { return false; }
|
||||
case 'startCallRecording':
|
||||
if (!this.backgroundTaskManager.isTaskRunning('record')) {
|
||||
this.logger.debug({action, silence, type}, 'CallSession:cloudRecording');
|
||||
this.callInfo.hasRecording = true;
|
||||
this.updateCallStatus(Object.assign({}, this.callInfo.toJSON()), this.serviceUrl)
|
||||
.catch((err) => this.logger.error(err, 'redis error'));
|
||||
if (!this.dlg) {
|
||||
// Call not yet answered so set flag to record on status change
|
||||
this.application.record_all_calls = true;
|
||||
} else {
|
||||
this.backgroundTaskManager.newTask('record');
|
||||
}
|
||||
return true;
|
||||
} else { return false; }
|
||||
case 'stopCallRecording':
|
||||
if (this.backgroundTaskManager.isTaskRunning('record')) {
|
||||
this.logger.debug({action, silence, type}, 'CallSession:cloudRecording');
|
||||
this.backgroundTaskManager.stop('record');
|
||||
return true;
|
||||
} else { return false; }
|
||||
}
|
||||
} else {
|
||||
// SIPREC
|
||||
/* if we have not answered yet, just save the details for later */
|
||||
if (!this.dlg) {
|
||||
if (action === 'startCallRecording') {
|
||||
this.recordOptions = opts;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* check validity of request */
|
||||
if (action == 'startCallRecording' && this.recordState !== RecordState.RecordingOff) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: recording is already started, ignoring request');
|
||||
return false;
|
||||
}
|
||||
if (action == 'stopCallRecording' && this.recordState === RecordState.RecordingOff) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: recording is already stopped, ignoring request');
|
||||
return false;
|
||||
}
|
||||
if (action == 'pauseCallRecording' && this.recordState !== RecordState.RecordingOn) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: cannot pause recording, ignoring request ');
|
||||
return false;
|
||||
}
|
||||
if (action == 'resumeCallRecording' && this.recordState !== RecordState.RecordingPaused) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: cannot resume recording, ignoring request ');
|
||||
return false;
|
||||
}
|
||||
/* check validity of request */
|
||||
if (action == 'startCallRecording' && this.recordState !== RecordState.RecordingOff) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: recording is already started, ignoring request');
|
||||
return false;
|
||||
}
|
||||
if (action == 'stopCallRecording' && this.recordState === RecordState.RecordingOff) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: recording is already stopped, ignoring request');
|
||||
return false;
|
||||
}
|
||||
if (action == 'pauseCallRecording' && this.recordState !== RecordState.RecordingOn) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: cannot pause recording, ignoring request ');
|
||||
return false;
|
||||
}
|
||||
if (action == 'resumeCallRecording' && this.recordState !== RecordState.RecordingPaused) {
|
||||
this.logger.info({recordState: this.recordState},
|
||||
'CallSession:notifyRecordOptions: cannot resume recording, ignoring request ');
|
||||
return false;
|
||||
}
|
||||
|
||||
this.recordOptions = opts;
|
||||
this.recordOptions = opts;
|
||||
|
||||
switch (action) {
|
||||
case 'startCallRecording':
|
||||
return await this.startRecording();
|
||||
case 'stopCallRecording':
|
||||
return await this.stopRecording();
|
||||
case 'pauseCallRecording':
|
||||
return await this.pauseRecording();
|
||||
case 'resumeCallRecording':
|
||||
return await this.resumeRecording();
|
||||
default:
|
||||
throw new Error(`invalid record action ${action}`);
|
||||
switch (action) {
|
||||
case 'startCallRecording':
|
||||
return await this.startRecording();
|
||||
case 'stopCallRecording':
|
||||
return await this.stopRecording();
|
||||
case 'pauseCallRecording':
|
||||
return await this.pauseRecording();
|
||||
case 'resumeCallRecording':
|
||||
return await this.resumeRecording();
|
||||
default:
|
||||
throw new Error(`invalid record action ${action}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -918,7 +964,7 @@ class CallSession extends Emitter {
|
||||
this.logger.debug('CallSession:enableBackgroundTtsStream - ttsStream enabled');
|
||||
} else {
|
||||
this.logger.debug(
|
||||
'CallSession:enableBackgroundTtsStream - ignoring request as call does not have required conditions');
|
||||
'CallSession:enableBackgroundTtsStream - ignoring request; conditions not met (probably not using ws api)');
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.info({err, say}, 'CallSession:enableBackgroundTtsStream - Error creating background tts stream task');
|
||||
@@ -932,15 +978,25 @@ class CallSession extends Emitter {
|
||||
}
|
||||
}
|
||||
clearTtsStream() {
|
||||
this.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'user_interruption'})
|
||||
.catch((err) => this.logger.info({err}, 'CallSession:clearTtsStream - Error sending user_interruption'));
|
||||
this.ttsStreamingBuffer?.clear();
|
||||
if (this.isTtsStreamEnabled) {
|
||||
this.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'user_interruption'})
|
||||
.catch((err) => this.logger.info({err}, 'CallSession:clearTtsStream - Error sending user_interruption'));
|
||||
this.ttsStreamingBuffer?.clear();
|
||||
}
|
||||
}
|
||||
|
||||
startTtsStream() {
|
||||
this.ttsStreamingBuffer?.start();
|
||||
}
|
||||
|
||||
stopTtsStream() {
|
||||
if (this.isTtsStreamEnabled) {
|
||||
this.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'stream_closed'})
|
||||
.catch((err) => this.logger.info({err}, 'CallSession:clearTtsStream - Error sending user_interruption'));
|
||||
this.ttsStreamingBuffer?.stop();
|
||||
}
|
||||
}
|
||||
|
||||
async enableBotMode(gather, autoEnable) {
|
||||
try {
|
||||
let task;
|
||||
@@ -964,7 +1020,7 @@ class CallSession extends Emitter {
|
||||
task.sticky = autoEnable;
|
||||
// listen to the bargein-done from background manager
|
||||
this.backgroundTaskManager.on('bargeIn-done', () => {
|
||||
if (this.requestor instanceof WsRequestor) {
|
||||
if (this.appIsUsingWebsockets) {
|
||||
try {
|
||||
this.kill(true);
|
||||
} catch (err) {}
|
||||
@@ -1178,7 +1234,8 @@ class CallSession extends Emitter {
|
||||
speech_credential_sid: credential.speech_credential_sid,
|
||||
client_id: credential.client_id,
|
||||
client_key: credential.client_key,
|
||||
user_id: credential.user_id
|
||||
user_id: credential.user_id,
|
||||
houndify_server_uri: credential.houndify_server_uri
|
||||
};
|
||||
}
|
||||
else if ('deepgramflux' === vendor) {
|
||||
@@ -1230,9 +1287,10 @@ class CallSession extends Emitter {
|
||||
}
|
||||
else {
|
||||
writeAlerts({
|
||||
alert_type: AlertType.STT_NOT_PROVISIONED,
|
||||
alert_type: type === 'tts' ? AlertType.TTS_NOT_PROVISIONED : AlertType.STT_NOT_PROVISIONED,
|
||||
account_sid: this.accountSid,
|
||||
vendor,
|
||||
label,
|
||||
target_sid: this.callSid
|
||||
}).catch((err) => this.logger.error({err}, 'Error writing tts alert'));
|
||||
}
|
||||
@@ -1263,6 +1321,7 @@ class CallSession extends Emitter {
|
||||
this.ttsStreamingBuffer.on(TtsStreamingEvents.Pause, this._onTtsStreamingPause.bind(this));
|
||||
this.ttsStreamingBuffer.on(TtsStreamingEvents.Resume, this._onTtsStreamingResume.bind(this));
|
||||
this.ttsStreamingBuffer.on(TtsStreamingEvents.ConnectFailure, this._onTtsStreamingConnectFailure.bind(this));
|
||||
this.ttsStreamingBuffer.on(TtsStreamingEvents.Connected, this._onTtsStreamingConnected.bind(this));
|
||||
}
|
||||
else {
|
||||
this.logger.info(`CallSession:exec - not a normal call session: ${this.constructor.name}`);
|
||||
@@ -1321,7 +1380,7 @@ class CallSession extends Emitter {
|
||||
}
|
||||
|
||||
if (0 === this.tasks.length &&
|
||||
this.requestor instanceof WsRequestor &&
|
||||
this.appIsUsingWebsockets &&
|
||||
!this.requestor.closedGracefully &&
|
||||
!this.callGone &&
|
||||
!this.isConfirmCallSession
|
||||
@@ -2441,6 +2500,36 @@ Duration=${duration} `
|
||||
}
|
||||
else {
|
||||
this.logger.error(err, `Error attempting to allocate endpoint for for task ${task.name}`);
|
||||
// Check for SipError type (e.g., 488 codec incompatibility)
|
||||
const isSipError = err.name === 'SipError';
|
||||
if (isSipError && err.status) {
|
||||
// Extract Reason header from SIP response if available (e.g., Q.850;cause=88;text="INCOMPATIBLE_DESTINATION")
|
||||
const sipReasonHeader = err.res?.msg?.headers?.reason;
|
||||
this._endpointAllocationError = {
|
||||
status: err.status,
|
||||
reason: err.reason || 'Endpoint Allocation Failed',
|
||||
sipReasonHeader
|
||||
};
|
||||
this.logger.info({endpointAllocationError: this._endpointAllocationError},
|
||||
'Captured SipError for propagation to SBC');
|
||||
|
||||
// Send SIP error response immediately for inbound calls
|
||||
if (this.res && !this.res.finalResponseSent) {
|
||||
this.logger.info(`Sending ${err.status} response to SBC due to SipError`);
|
||||
this.res.send(err.status, {
|
||||
headers: {
|
||||
'X-Reason': `endpoint allocation failure: ${err.reason || 'Endpoint Allocation Failed'}`,
|
||||
...(sipReasonHeader && {'Reason': sipReasonHeader})
|
||||
}
|
||||
});
|
||||
this._notifyCallStatusChange({
|
||||
callStatus: CallStatus.Failed,
|
||||
sipStatus: err.status,
|
||||
sipReason: err.reason || 'Endpoint Allocation Failed'
|
||||
});
|
||||
this._callReleased();
|
||||
}
|
||||
}
|
||||
throw new Error(`${BADPRECONDITIONS}: unable to allocate endpoint`);
|
||||
}
|
||||
}
|
||||
@@ -2547,7 +2636,7 @@ Duration=${duration} `
|
||||
this.backgroundTaskManager.stopAll();
|
||||
this.clearOrRestoreActionHookDelayProcessor().catch((err) => {});
|
||||
|
||||
this.ttsStreamingBuffer?.stop();
|
||||
this.stopTtsStream();
|
||||
|
||||
this.sttLatencyCalculator?.stop();
|
||||
}
|
||||
@@ -2953,8 +3042,7 @@ Duration=${duration} `
|
||||
|
||||
// manage record all call.
|
||||
if (callStatus === CallStatus.InProgress) {
|
||||
if (this.accountInfo.account.record_all_calls ||
|
||||
this.application.record_all_calls) {
|
||||
if (this.accountInfo.account.record_all_calls || this.application.record_all_calls) {
|
||||
this.backgroundTaskManager.newTask('record');
|
||||
}
|
||||
} else if (callStatus == CallStatus.Completed) {
|
||||
@@ -3007,14 +3095,14 @@ Duration=${duration} `
|
||||
*/
|
||||
|
||||
_notifyTaskError(obj) {
|
||||
if (this.requestor instanceof WsRequestor) {
|
||||
if (this.appIsUsingWebsockets) {
|
||||
this.requestor.request('jambonz:error', '/error', obj)
|
||||
.catch((err) => this.logger.debug({err}, 'CallSession:_notifyTaskError - Error sending'));
|
||||
}
|
||||
}
|
||||
|
||||
_notifyTaskStatus(task, evt) {
|
||||
if (this.notifyEvents && this.requestor instanceof WsRequestor) {
|
||||
if (this.notifyEvents && this.appIsUsingWebsockets) {
|
||||
const obj = {...evt, id: task.id, name: task.name};
|
||||
this.requestor.request('verb:status', '/status', obj)
|
||||
.catch((err) => this.logger.debug({err}, 'CallSession:_notifyTaskStatus - Error sending'));
|
||||
@@ -3066,7 +3154,7 @@ Duration=${duration} `
|
||||
}
|
||||
|
||||
_clearTasks(backgroundGather, evt) {
|
||||
if (this.requestor instanceof WsRequestor && !backgroundGather.cleared) {
|
||||
if (this.appIsUsingWebsockets && !backgroundGather.cleared) {
|
||||
this.logger.debug({evt}, 'CallSession:_clearTasks on event from background gather');
|
||||
try {
|
||||
backgroundGather.cleared = true;
|
||||
@@ -3094,10 +3182,12 @@ Duration=${duration} `
|
||||
}
|
||||
}
|
||||
|
||||
_onTtsStreamingEmpty() {
|
||||
this.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'stream_empty'})
|
||||
.catch((err) => this.logger.info({err}, 'CallSession:_onTtsStreamingEmpty - Error sending'));
|
||||
_onTtsStreamingConnected() {
|
||||
this.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'stream_open'})
|
||||
.catch((err) => this.logger.info({err}, 'CallSession:_onTtsStreamingConnected - Error sending'));
|
||||
}
|
||||
|
||||
_onTtsStreamingEmpty() {
|
||||
const task = this.currentTask;
|
||||
if (task && TaskName.Say === task.name) {
|
||||
task.notifyTtsStreamIsEmpty();
|
||||
|
||||
@@ -60,6 +60,19 @@ class InboundCallSession extends CallSession {
|
||||
}
|
||||
});
|
||||
}
|
||||
else if (this._endpointAllocationError) {
|
||||
// Propagate SIP error from endpoint allocation failure back to the client
|
||||
const {status, reason, sipReasonHeader} = this._endpointAllocationError;
|
||||
this.rootSpan.setAttributes({'call.termination': `endpoint allocation SIP error ${status}`});
|
||||
this.logger.info({endpointAllocationError: this._endpointAllocationError},
|
||||
`InboundCallSession:_onTasksDone generating ${status} due to endpoint allocation failure`);
|
||||
this.res.send(status, {
|
||||
headers: {
|
||||
'X-Reason': `endpoint allocation failure: ${reason}`,
|
||||
...(sipReasonHeader && {'Reason': sipReasonHeader})
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.rootSpan.setAttributes({'call.termination': 'tasks completed without answering call'});
|
||||
this.logger.info('InboundCallSession:_onTasksDone auto-generating non-success response to invite');
|
||||
|
||||
@@ -18,7 +18,8 @@ class TaskConfig extends Task {
|
||||
'boostAudioSignal',
|
||||
'vad',
|
||||
'ttsStream',
|
||||
'autoStreamTts'
|
||||
'autoStreamTts',
|
||||
'disableTtsCache'
|
||||
].forEach((k) => this[k] = this.data[k] || {});
|
||||
|
||||
if ('notifyEvents' in this.data) {
|
||||
@@ -88,6 +89,7 @@ class TaskConfig extends Task {
|
||||
get hasReferHook() { return Object.keys(this.data).includes('referHook'); }
|
||||
get hasNotifySttLatency() { return Object.keys(this.data).includes('notifySttLatency'); }
|
||||
get hasTtsStream() { return Object.keys(this.ttsStream).length; }
|
||||
get hasDisableTtsCache() { return Object.keys(this.data).includes('disableTtsCache'); }
|
||||
|
||||
get summary() {
|
||||
const phrase = [];
|
||||
@@ -125,6 +127,7 @@ class TaskConfig extends Task {
|
||||
phrase.push(`${this.ttsStream.enable ? 'enable' : 'disable'} ttsStream`);
|
||||
}
|
||||
if ('autoStreamTts' in this.data) phrase.push(`enable Say.stream value ${this.data.autoStreamTts ? 'on' : 'off'}`);
|
||||
if (this.hasDisableTtsCache) phrase.push(`disableTtsCache ${this.data.disableTtsCache ? 'on' : 'off'}`);
|
||||
return `${this.name}{${phrase.join(',')}}`;
|
||||
}
|
||||
|
||||
@@ -357,6 +360,11 @@ class TaskConfig extends Task {
|
||||
this.logger.info('Config: disabling ttsStream');
|
||||
cs.disableTtsStream();
|
||||
}
|
||||
|
||||
if (this.hasDisableTtsCache) {
|
||||
this.logger.info(`set disableTtsCache = ${this.disableTtsCache}`);
|
||||
cs.disableTtsCache = this.data.disableTtsCache;
|
||||
}
|
||||
}
|
||||
|
||||
async kill(cs) {
|
||||
|
||||
@@ -21,7 +21,7 @@ const {parseUri} = require('drachtio-srf');
|
||||
const {ANCHOR_MEDIA_ALWAYS,
|
||||
JAMBONZ_DIAL_PAI_HEADER,
|
||||
JAMBONES_DIAL_SBC_FOR_REGISTERED_USER} = require('../config');
|
||||
const { isOnhold, isOpusFirst } = require('../utils/sdp-utils');
|
||||
const { isOnhold, isOpusFirst, getLeadingCodec } = require('../utils/sdp-utils');
|
||||
const { normalizeJambones } = require('@jambonz/verb-specifications');
|
||||
const { selectHostPort } = require('../utils/network');
|
||||
const { sleepFor } = require('../utils/helpers');
|
||||
@@ -158,6 +158,7 @@ class TaskDial extends Task {
|
||||
|
||||
get canReleaseMedia() {
|
||||
const keepAnchor = this.data.anchorMedia ||
|
||||
this.isTranscoding ||
|
||||
this.cs.isBackGroundListen ||
|
||||
this.cs.onHoldMusic ||
|
||||
ANCHOR_MEDIA_ALWAYS ||
|
||||
@@ -575,7 +576,7 @@ class TaskDial extends Task {
|
||||
proxy: `sip:${sbcAddress}`,
|
||||
callingNumber: this.callerId || fromUri.user,
|
||||
...(this.callerName && {callingName: this.callerName}),
|
||||
opusFirst: isOpusFirst(this.cs.ep.remote.sdp),
|
||||
opusFirst: isOpusFirst(this.cs.ep.local.sdp),
|
||||
isVideoCall: this.cs.ep.remote.sdp.includes('m=video')
|
||||
};
|
||||
|
||||
@@ -772,6 +773,15 @@ class TaskDial extends Task {
|
||||
}
|
||||
|
||||
async _connectSingleDial(cs, sd) {
|
||||
// start connect with dialed leg, this is the soonest we can identify transcoding
|
||||
if (this.epOther && sd.ep) {
|
||||
const codecA = getLeadingCodec(this.epOther.local.sdp);
|
||||
const codecB = getLeadingCodec(sd.ep.remote.sdp);
|
||||
this.isTranscoding = (codecA !== codecB);
|
||||
if (this.isTranscoding) {
|
||||
this.logger.info(`Dial:_connectSingleDial - transcoding from ${codecA} (A leg) to ${codecB} (B leg)`);
|
||||
}
|
||||
}
|
||||
if (!this.bridged && !this.canReleaseMedia) {
|
||||
this.logger.debug('Dial:_connectSingleDial bridging endpoints');
|
||||
if (this.epOther) {
|
||||
@@ -929,7 +939,6 @@ class TaskDial extends Task {
|
||||
this.logger.info({err}, 'Dial:_selectSingleDial - Error boosting audio signal');
|
||||
}
|
||||
}
|
||||
|
||||
/* if we can release the media back to the SBC, do so now */
|
||||
if (this.canReleaseMedia || this.shouldExitMediaPathEntirely) {
|
||||
setTimeout(this._releaseMedia.bind(this, cs, sd, this.shouldExitMediaPathEntirely), 200);
|
||||
|
||||
@@ -258,7 +258,7 @@ class TaskGather extends SttTask {
|
||||
startDtmfListener();
|
||||
}
|
||||
this._stopVad();
|
||||
if (!this.killed) {
|
||||
if (!this.killed && !this.resolved) {
|
||||
startListening(cs, ep);
|
||||
if (this.input.includes('speech') && this.vendor === 'nuance' && this.listenDuringPrompt) {
|
||||
this.logger.debug('Gather:exec - starting transcription timers after say completes');
|
||||
@@ -270,19 +270,21 @@ class TaskGather extends SttTask {
|
||||
};
|
||||
this.sayTask.span = span;
|
||||
this.sayTask.ctx = ctx;
|
||||
this.sayTask.exec(cs, {ep}) // kicked off, _not_ waiting for it to complete
|
||||
this.sayTask
|
||||
.exec(cs, {ep}) // kicked off, _not_ waiting for it to complete
|
||||
.then(() => {
|
||||
if (this.sayTask.isStreamingTts) return;
|
||||
this.logger.debug('Gather:exec - nested say task completed');
|
||||
span.end();
|
||||
process();
|
||||
return;
|
||||
})
|
||||
.catch((err) => {
|
||||
process();
|
||||
});
|
||||
if (this.sayTask.isStreamingTts && !this.sayTask.closeOnStreamEmpty) {
|
||||
// if streaming tts, we do not wait for it to complete if it is not closing the stream automatically
|
||||
process();
|
||||
} else {
|
||||
this.sayTask.on('playDone', (err) => {
|
||||
span.end();
|
||||
if (err) this.logger.error({err}, 'Gather:exec Error playing tts');
|
||||
process();
|
||||
});
|
||||
}
|
||||
}
|
||||
else if (this.playTask) {
|
||||
@@ -294,7 +296,7 @@ class TaskGather extends SttTask {
|
||||
startDtmfListener();
|
||||
}
|
||||
this._stopVad();
|
||||
if (!this.killed) {
|
||||
if (!this.killed && !this.resolved) {
|
||||
startListening(cs, ep);
|
||||
if (this.input.includes('speech') && this.vendor === 'nuance' && this.listenDuringPrompt) {
|
||||
this.logger.debug('Gather:exec - starting transcription timers after play completes');
|
||||
@@ -306,15 +308,17 @@ class TaskGather extends SttTask {
|
||||
};
|
||||
this.playTask.span = span;
|
||||
this.playTask.ctx = ctx;
|
||||
this.playTask.exec(cs, {ep}) // kicked off, _not_ waiting for it to complete
|
||||
this.playTask
|
||||
.exec(cs, {ep}) // kicked off, _not_ waiting for it to complete
|
||||
.then(() => {
|
||||
this.logger.debug('Gather:exec - nested play task completed');
|
||||
span.end();
|
||||
process();
|
||||
return;
|
||||
})
|
||||
.catch((err) => {
|
||||
process();
|
||||
});
|
||||
this.playTask.on('playDone', (err) => {
|
||||
span.end();
|
||||
if (err) this.logger.error({err}, 'Gather:exec Error playing url');
|
||||
process();
|
||||
});
|
||||
}
|
||||
else {
|
||||
if (this.killed) {
|
||||
@@ -496,6 +500,10 @@ class TaskGather extends SttTask {
|
||||
this.addCustomEventListener(ep, GladiaTranscriptionEvents.ConnectFailure,
|
||||
this._onVendorConnectFailure.bind(this, cs, ep));
|
||||
this.addCustomEventListener(ep, GladiaTranscriptionEvents.Error, this._onVendorError.bind(this, cs, ep));
|
||||
// gladia require unique url for each session
|
||||
const {host, path} = await this.createGladiaLiveSession();
|
||||
opts.GLADIA_SPEECH_HOST = host;
|
||||
opts.GLADIA_SPEECH_PATH = path;
|
||||
break;
|
||||
|
||||
case 'soniox':
|
||||
@@ -877,17 +885,15 @@ class TaskGather extends SttTask {
|
||||
this._fillerNoiseOn = false; // in a race, if we just started audio it may sneak through here
|
||||
this.ep.api('uuid_break', this.ep.uuid)
|
||||
.catch((err) => this.logger.info(err, 'Error killing audio'));
|
||||
cs.clearTtsStream();
|
||||
if (cs.isTtsStreamEnabled) cs.clearTtsStream();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (this.sayTask && !this.sayTask.killed) {
|
||||
this.sayTask.removeAllListeners('playDone');
|
||||
this.sayTask.kill(cs);
|
||||
this.sayTask = null;
|
||||
}
|
||||
if (this.playTask && !this.playTask.killed) {
|
||||
this.playTask.removeAllListeners('playDone');
|
||||
this.playTask.kill(cs);
|
||||
this.playTask = null;
|
||||
}
|
||||
@@ -1159,7 +1165,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');
|
||||
@@ -1316,6 +1322,8 @@ class TaskGather extends SttTask {
|
||||
}
|
||||
|
||||
this.resolved = true;
|
||||
// gather is resolved, prevent any further transcription events while resolve in progress
|
||||
this.removeCustomEventListeners();
|
||||
// If bargin is false and ws application return ack to verb:hook
|
||||
// the gather should not play any audio
|
||||
this._killAudio(this.cs);
|
||||
|
||||
@@ -9,7 +9,7 @@ function escapeString(str) {
|
||||
return str
|
||||
.replace(/\\/g, '\\\\') // Escape backslashes
|
||||
.replace(/"/g, '\\"') // Escape double quotes
|
||||
.replace(/\b/g, '\\b') // Escape backspace
|
||||
.replace(/[\b]/g, '\\b') // Escape backspace (NOTE: [\b] not \b)
|
||||
.replace(/\f/g, '\\f') // Escape formfeed
|
||||
.replace(/\n/g, '\\n') // Escape newlines
|
||||
.replace(/\r/g, '\\r') // Escape carriage returns
|
||||
@@ -36,7 +36,8 @@ class TaskListen extends Task {
|
||||
this.metadata = {};
|
||||
for (const key in this.data.metadata) {
|
||||
if (this.data.metadata.hasOwnProperty(key)) {
|
||||
this.metadata[key] = escapeString(this.data.metadata[key]);
|
||||
const value = this.data.metadata[key];
|
||||
this.metadata[key] = typeof value === 'string' ? escapeString(value) : value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,8 +146,9 @@ class TaskLlmUltravox_S2S extends Task {
|
||||
return data;
|
||||
}
|
||||
|
||||
_unregisterHandlers() {
|
||||
_unregisterHandlers(ep) {
|
||||
this.removeCustomEventListeners();
|
||||
ep.removeAllListeners('dtmf');
|
||||
}
|
||||
|
||||
_registerHandlers(ep) {
|
||||
@@ -155,6 +156,7 @@ class TaskLlmUltravox_S2S extends Task {
|
||||
this.addCustomEventListener(ep, LlmEvents_Ultravox.ConnectFailure, this._onConnectFailure.bind(this, ep));
|
||||
this.addCustomEventListener(ep, LlmEvents_Ultravox.Disconnect, this._onDisconnect.bind(this, ep));
|
||||
this.addCustomEventListener(ep, LlmEvents_Ultravox.ServerEvent, this._onServerEvent.bind(this, ep));
|
||||
ep.on('dtmf', this._onDtmf.bind(this, ep));
|
||||
}
|
||||
|
||||
async _startListening(cs, ep) {
|
||||
@@ -189,7 +191,7 @@ class TaskLlmUltravox_S2S extends Task {
|
||||
/* note: the parent llm verb started the span, which is why this is necessary */
|
||||
await this.parent.performAction(this.results);
|
||||
|
||||
this._unregisterHandlers();
|
||||
this._unregisterHandlers(ep);
|
||||
}
|
||||
|
||||
async kill(cs) {
|
||||
@@ -346,6 +348,18 @@ class TaskLlmUltravox_S2S extends Task {
|
||||
excludeEvents: this.excludeEvents
|
||||
}, 'TaskLlmUltravox_S2S:_populateEvents');
|
||||
}
|
||||
|
||||
_onDtmf(ep, evt) {
|
||||
this.logger.info({evt}, 'TaskLlmUltravox_S2S:_onDtmf - DTMF received');
|
||||
const {dtmf} = evt;
|
||||
const data = {
|
||||
type: 'user_text_message',
|
||||
text: `DTMF received: ${dtmf}`,
|
||||
urgency: 'immediate'
|
||||
};
|
||||
this._api(ep, [ep.uuid, ClientEvent, JSON.stringify(data)])
|
||||
.catch((err) => this.logger.info({err, evt}, 'TaskLlmUltravox_S2S:_onDtmf - Error sending DTMF as text message'));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TaskLlmUltravox_S2S;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const Task = require('./task');
|
||||
const {TaskName} = require('../utils/constants');
|
||||
const WsRequestor = require('../utils/ws-requestor');
|
||||
const URL = require('url');
|
||||
const HttpRequestor = require('../utils/http-requestor');
|
||||
|
||||
/**
|
||||
@@ -10,6 +9,7 @@ const HttpRequestor = require('../utils/http-requestor');
|
||||
class TaskRedirect extends Task {
|
||||
constructor(logger, opts) {
|
||||
super(logger, opts);
|
||||
this.statusHook = opts.statusHook || false;
|
||||
}
|
||||
|
||||
get name() { return TaskName.Redirect; }
|
||||
@@ -33,7 +33,7 @@ class TaskRedirect extends Task {
|
||||
}
|
||||
else {
|
||||
const baseUrl = this.cs.application.requestor.baseUrl;
|
||||
const newUrl = URL.parse(this.actionHook);
|
||||
const newUrl = new URL(this.actionHook);
|
||||
const newBaseUrl = newUrl.protocol + '//' + newUrl.host;
|
||||
if (baseUrl != newBaseUrl) {
|
||||
try {
|
||||
@@ -47,6 +47,30 @@ class TaskRedirect extends Task {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* update the notifier if a new statusHook was provided */
|
||||
if (this.statusHook) {
|
||||
this.logger.info(`TaskRedirect updating statusHook to ${this.statusHook}`);
|
||||
try {
|
||||
const oldNotifier = cs.application.notifier;
|
||||
const isStatusHookAbsolute = cs.notifier?._isAbsoluteUrl(this.statusHook);
|
||||
if (isStatusHookAbsolute) {
|
||||
if (cs.notifier instanceof WsRequestor) {
|
||||
cs.application.notifier = new WsRequestor(this.logger, cs.accountSid, {url: this.statusHook},
|
||||
cs.accountInfo.account.webhook_secret);
|
||||
} else {
|
||||
cs.application.notifier = new HttpRequestor(this.logger, cs.accountSid, {url: this.statusHook},
|
||||
cs.accountInfo.account.webhook_secret);
|
||||
}
|
||||
if (oldNotifier?.close) oldNotifier.close();
|
||||
}
|
||||
/* update the call_status_hook URL that gets passed to the notifier */
|
||||
cs.application.call_status_hook = this.statusHook;
|
||||
} catch (err) {
|
||||
this.logger.info(err, `TaskRedirect error updating statusHook to ${this.statusHook}`);
|
||||
}
|
||||
}
|
||||
|
||||
await this.performAction();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
const assert = require('assert');
|
||||
const TtsTask = require('./tts-task');
|
||||
const {TaskName, TaskPreconditions} = require('../utils/constants');
|
||||
const {JAMBONES_SAY_CHUNK_SIZE} = require('../config');
|
||||
const pollySSMLSplit = require('polly-ssml-split');
|
||||
const { SpeechCredentialError } = require('../utils/error');
|
||||
const { SpeechCredentialError, NonFatalTaskError } = require('../utils/error');
|
||||
const { sleepFor } = require('../utils/helpers');
|
||||
const { NON_FANTAL_ERRORS } = require('../utils/constants.json');
|
||||
|
||||
/**
|
||||
* Discard unmatching responses:
|
||||
@@ -30,7 +32,7 @@ const isMatchingEvent = (logger, filename, playbackId, evt) => {
|
||||
const breakLengthyTextIfNeeded = (logger, text) => {
|
||||
// As The text can be used for tts streaming, we need to break lengthy text into smaller chunks
|
||||
// HIGH_WATER_BUFFER_SIZE defined in tts-streaming-buffer.js
|
||||
const chunkSize = 900;
|
||||
const chunkSize = JAMBONES_SAY_CHUNK_SIZE;
|
||||
const isSSML = text.startsWith('<speak>');
|
||||
const options = {
|
||||
softLimit: 100,
|
||||
@@ -120,13 +122,11 @@ class TaskSay extends TtsTask {
|
||||
}
|
||||
if (this.isStreamingTts) await this.handlingStreaming(cs, obj);
|
||||
else await this.handling(cs, obj);
|
||||
this.emit('playDone');
|
||||
} catch (error) {
|
||||
if (error instanceof SpeechCredentialError) {
|
||||
// if say failed due to speech credentials, alarm is writtern and error notification is sent
|
||||
// finished this say to move to next task.
|
||||
this.logger.info({error}, 'Say failed due to SpeechCredentialError, finished!');
|
||||
this.emit('playDone');
|
||||
return;
|
||||
}
|
||||
throw error;
|
||||
@@ -147,9 +147,6 @@ class TaskSay extends TtsTask {
|
||||
|
||||
await cs.startTtsStream();
|
||||
|
||||
cs.requestor?.request('tts:streaming-event', '/streaming-event', {event_type: 'stream_open'})
|
||||
.catch((err) => this.logger.info({err}, 'TaskSay:handlingStreaming - Error sending'));
|
||||
|
||||
if (this.text.length !== 0) {
|
||||
this.logger.info('TaskSay:handlingStreaming - sending text to TTS stream');
|
||||
for (const t of this.text) {
|
||||
@@ -407,11 +404,19 @@ class TaskSay extends TtsTask {
|
||||
this._playResolve = resolve;
|
||||
this._playReject = reject;
|
||||
});
|
||||
const r = await ep.play(filename);
|
||||
this.logger.debug({r}, 'Say:exec play result');
|
||||
if (r.playbackSeconds == null && r.playbackMilliseconds == null && r.playbackLastOffsetPos == null) {
|
||||
this._playReject(new Error('Playback failed to start'));
|
||||
try {
|
||||
const r = await ep.play(filename);
|
||||
this.logger.debug({r}, 'Say:exec play result');
|
||||
if (r.playbackSeconds == null && r.playbackMilliseconds == null && r.playbackLastOffsetPos == null) {
|
||||
this._playReject(new Error('Playback failed to start'));
|
||||
}
|
||||
} catch (err) {
|
||||
if (NON_FANTAL_ERRORS.includes(err.message)) {
|
||||
throw new NonFatalTaskError(err.message);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
|
||||
try {
|
||||
// wait for playback-stop event received to confirm if the playback is successful
|
||||
await this._playPromise;
|
||||
@@ -449,8 +454,8 @@ class TaskSay extends TtsTask {
|
||||
const {memberId, confName} = cs;
|
||||
this.killPlayToConfMember(this.ep, memberId, confName);
|
||||
} else if (this.isStreamingTts) {
|
||||
this.logger.debug('TaskSay:kill - clearing TTS stream for streaming audio');
|
||||
cs.clearTtsStream();
|
||||
this.logger.debug('TaskSay:kill - stopping TTS stream for streaming audio');
|
||||
cs.stopTtsStream();
|
||||
} else {
|
||||
if (!this.notifiedPlayBackStop) {
|
||||
this.notifyStatus({event: 'stop-playback'});
|
||||
|
||||
@@ -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}`,
|
||||
@@ -203,26 +203,14 @@ class SttTask extends Task {
|
||||
if (cs.hasGlobalSttPunctuation && !this.data.recognizer.punctuation) {
|
||||
this.data.recognizer.punctuation = cs.globalSttPunctuation;
|
||||
}
|
||||
if (this.vendor === 'gladia') {
|
||||
const { api_key, region } = this.sttCredentials;
|
||||
const {url} = await this.createGladiaLiveSession({
|
||||
api_key, region,
|
||||
model: this.data.recognizer.model || 'solaria-1',
|
||||
options: this.data.recognizer.gladiaOptions || {}
|
||||
});
|
||||
const {host, pathname, search} = new URL(url);
|
||||
this.sttCredentials.host = host;
|
||||
this.sttCredentials.path = `${pathname}${search}`;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async createGladiaLiveSession({
|
||||
api_key,
|
||||
region = 'us-west',
|
||||
model = 'solaria-1',
|
||||
options = {},
|
||||
}) {
|
||||
async createGladiaLiveSession() {
|
||||
const { api_key, region = 'us-west' } = this.sttCredentials;
|
||||
const model = this.data.recognizer.model || 'solaria-1';
|
||||
const options = this.data.recognizer.gladiaOptions || {};
|
||||
|
||||
const url = `https://api.gladia.io/v2/live?region=${region}`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
@@ -252,7 +240,9 @@ class SttTask extends Task {
|
||||
|
||||
const data = await response.json();
|
||||
this.logger.debug({url: data.url}, 'Gladia Call registered');
|
||||
return data;
|
||||
|
||||
const {host, pathname, search} = new URL(data.url);
|
||||
return {host, path: `${pathname}${search}`};
|
||||
}
|
||||
|
||||
addCustomEventListener(ep, event, handler) {
|
||||
@@ -260,8 +250,19 @@ class SttTask extends Task {
|
||||
ep.addCustomEventListener(event, handler);
|
||||
}
|
||||
|
||||
removeCustomEventListeners() {
|
||||
this.eventHandlers.forEach((h) => h.ep.removeCustomEventListener(h.event, h.handler));
|
||||
removeCustomEventListeners(ep) {
|
||||
if (ep) {
|
||||
// for specific endpoint
|
||||
this.eventHandlers.filter((h) => h.ep === ep).forEach((h) => {
|
||||
h.ep.removeCustomEventListener(h.event, h.handler);
|
||||
});
|
||||
this.eventHandlers = this.eventHandlers.filter((h) => h.ep !== ep);
|
||||
return;
|
||||
} else {
|
||||
// for all endpoints
|
||||
this.eventHandlers.forEach((h) => h.ep.removeCustomEventListener(h.event, h.handler));
|
||||
this.eventHandlers = [];
|
||||
}
|
||||
}
|
||||
|
||||
async _initSpeechCredentials(cs, vendor, label) {
|
||||
@@ -275,6 +276,7 @@ class SttTask extends Task {
|
||||
account_sid: cs.accountSid,
|
||||
alert_type: AlertType.STT_NOT_PROVISIONED,
|
||||
vendor,
|
||||
label,
|
||||
target_sid: cs.callSid
|
||||
}).catch((err) => this.logger.info({err}, 'Error generating alert for no stt'));
|
||||
// the ASR might have fallback configuration, should not done task here.
|
||||
@@ -329,11 +331,13 @@ class SttTask extends Task {
|
||||
return credentials;
|
||||
}
|
||||
|
||||
get canFallback() {
|
||||
canFallback() {
|
||||
return this.fallbackVendor && this.isHandledByPrimaryProvider && !this.cs.hasFallbackAsr;
|
||||
}
|
||||
|
||||
async _initFallback() {
|
||||
// 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}`);
|
||||
this.isHandledByPrimaryProvider = false;
|
||||
@@ -346,7 +350,7 @@ class SttTask extends Task {
|
||||
this.data.recognizer.label = this.label;
|
||||
this.sttCredentials = await this._initSpeechCredentials(this.cs, this.vendor, this.label);
|
||||
// cleanup previous listener from previous vendor
|
||||
this.removeCustomEventListeners();
|
||||
this.removeCustomEventListeners(ep);
|
||||
}
|
||||
|
||||
async compileHintsForCobalt(ep, hostport, model, token, hints) {
|
||||
@@ -473,6 +477,7 @@ class SttTask extends Task {
|
||||
message: 'STT failure reported by vendor',
|
||||
detail: evt.error,
|
||||
vendor: this.vendor,
|
||||
label: this.label,
|
||||
target_sid: cs.callSid
|
||||
}).catch((err) => this.logger.info({err}, `Error generating alert for ${this.vendor} connection failure`));
|
||||
}
|
||||
@@ -486,6 +491,7 @@ class SttTask extends Task {
|
||||
alert_type: AlertType.STT_FAILURE,
|
||||
message: `Failed connecting to ${this.vendor} speech recognizer: ${reason}`,
|
||||
vendor: this.vendor,
|
||||
label: this.label,
|
||||
target_sid: cs.callSid
|
||||
}).catch((err) => this.logger.info({err}, `Error generating alert for ${this.vendor} connection failure`));
|
||||
}
|
||||
|
||||
@@ -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; }
|
||||
@@ -456,6 +459,14 @@ class TaskTranscribe extends SttTask {
|
||||
else if (this.data.recognizer?.hints?.length > 0) {
|
||||
prompt = this.data.recognizer?.hints.join(', ');
|
||||
}
|
||||
} else if (this.vendor === 'gladia') {
|
||||
// gladia require unique url for each session
|
||||
const {host, path} = await this.createGladiaLiveSession();
|
||||
await ep.set({
|
||||
GLADIA_SPEECH_HOST: host,
|
||||
GLADIA_SPEECH_PATH: path,
|
||||
})
|
||||
.catch((err) => this.logger.info(err, 'Error setting channel variables'));
|
||||
}
|
||||
|
||||
await ep.startTranscription({
|
||||
@@ -776,7 +787,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,
|
||||
@@ -786,7 +797,7 @@ class TaskTranscribe extends SttTask {
|
||||
try {
|
||||
this.notifyError({ msg: 'ASR error',
|
||||
details:`STT Vendor ${this.vendor} error: ${evt.error || evt.reason}`, failover: 'in progress'});
|
||||
await this._initFallback();
|
||||
await this._initFallback(_ep);
|
||||
let channel = 1;
|
||||
if (this.ep !== _ep) {
|
||||
channel = 2;
|
||||
@@ -895,6 +906,41 @@ 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
|
||||
// we want to keep track this on task level instead of endpoint level
|
||||
// because the endpoint instance is used across multiple tasks.
|
||||
canFallback(ep) {
|
||||
let isHandledByPrimaryProvider = this.isHandledByPrimaryProvider;
|
||||
if (ep === this.ep) {
|
||||
isHandledByPrimaryProvider = this.isHandledByPrimaryProviderForEp1;
|
||||
} else if (ep === this.ep2) {
|
||||
isHandledByPrimaryProvider = this.isHandledByPrimaryProviderForEp2;
|
||||
}
|
||||
|
||||
const isOneOfEndpointAlreadyFallenBack = !!this.ep && !!this.ep2 &&
|
||||
this.isHandledByPrimaryProviderForEp1 !== this.isHandledByPrimaryProviderForEp2;
|
||||
|
||||
// fallback is configured
|
||||
return this.fallbackVendor &&
|
||||
// has this endpoint already fallen back
|
||||
isHandledByPrimaryProvider &&
|
||||
// in global level, is there any fallback is already happened
|
||||
// one fallen endpoint will mark cs.hasFallbackAsr to true,
|
||||
// so if one endpoint was fallen, the other endpoint would be able to fallback.
|
||||
(isOneOfEndpointAlreadyFallenBack || !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;
|
||||
|
||||
@@ -41,6 +41,10 @@ class TtsTask extends Task {
|
||||
|
||||
async exec(cs) {
|
||||
super.exec(cs);
|
||||
// update disableTtsCache from call session if not set in task
|
||||
if (this.data.disableTtsCache == null) {
|
||||
this.disableTtsCache = cs.disableTtsCache;
|
||||
}
|
||||
if (cs.synthesizer) {
|
||||
this.options = {...cs.synthesizer.options, ...this.options};
|
||||
this.data.synthesizer = this.data.synthesizer || {};
|
||||
@@ -81,55 +85,67 @@ class TtsTask extends Task {
|
||||
}
|
||||
|
||||
async setTtsStreamingChannelVars(vendor, language, voice, credentials, ep) {
|
||||
const {api_key, model_id, api_uri, custom_tts_streaming_url, auth_token} = credentials;
|
||||
let obj;
|
||||
const {api_key, model_id, api_uri, custom_tts_streaming_url, auth_token, options} = credentials;
|
||||
// api_key, model_id, api_uri, custom_tts_streaming_url, and auth_token are encoded in the credentials
|
||||
// allow them to be overriden via config, using options
|
||||
// give preference to options passed in via config
|
||||
const parsed_options = options ? JSON.parse(options) : {};
|
||||
const local_options = {...parsed_options, ...this.options};
|
||||
const local_voice_settings = {...(parsed_options.voice_settings || {}), ...(this.options.voice_settings || {})};
|
||||
const local_api_key = local_options.api_key ?? api_key;
|
||||
const local_model_id = local_options.model_id ?? model_id;
|
||||
const local_api_uri = local_options.api_uri ?? api_uri;
|
||||
const local_custom_tts_streaming_url = local_options.custom_tts_streaming_url ?? custom_tts_streaming_url;
|
||||
const local_auth_token = local_options.auth_token ?? auth_token;
|
||||
|
||||
this.logger.debug(`setTtsStreamingChannelVars: vendor: ${vendor}, language: ${language}, voice: ${voice}`);
|
||||
|
||||
let obj;
|
||||
|
||||
switch (vendor) {
|
||||
case 'deepgram':
|
||||
obj = {
|
||||
DEEPGRAM_API_KEY: api_key,
|
||||
DEEPGRAM_API_KEY: local_api_key,
|
||||
DEEPGRAM_TTS_STREAMING_MODEL: voice
|
||||
};
|
||||
break;
|
||||
case 'cartesia':
|
||||
obj = {
|
||||
CARTESIA_API_KEY: api_key,
|
||||
CARTESIA_TTS_STREAMING_MODEL_ID: model_id,
|
||||
CARTESIA_API_KEY: local_api_key,
|
||||
CARTESIA_TTS_STREAMING_MODEL_ID: local_model_id,
|
||||
CARTESIA_TTS_STREAMING_VOICE_ID: voice,
|
||||
CARTESIA_TTS_STREAMING_LANGUAGE: language || 'en',
|
||||
};
|
||||
break;
|
||||
case 'elevenlabs':
|
||||
const {stability, similarity_boost, use_speaker_boost, style, speed} = this.options.voice_settings || {};
|
||||
// eslint-disable-next-line max-len
|
||||
const {stability, similarity_boost, use_speaker_boost, style, speed} = local_voice_settings || {};
|
||||
obj = {
|
||||
ELEVENLABS_API_KEY: api_key,
|
||||
...(api_uri && {ELEVENLABS_API_URI: api_uri}),
|
||||
ELEVENLABS_TTS_STREAMING_MODEL_ID: model_id,
|
||||
ELEVENLABS_API_KEY: local_api_key,
|
||||
...(api_uri && {ELEVENLABS_API_URI: local_api_uri}),
|
||||
ELEVENLABS_TTS_STREAMING_MODEL_ID: local_model_id,
|
||||
ELEVENLABS_TTS_STREAMING_VOICE_ID: voice,
|
||||
// 20/12/2024 - only eleven_turbo_v2_5 support multiple language
|
||||
...(['eleven_turbo_v2_5'].includes(model_id) && {ELEVENLABS_TTS_STREAMING_LANGUAGE: language}),
|
||||
...(['eleven_turbo_v2_5'].includes(local_model_id) && {ELEVENLABS_TTS_STREAMING_LANGUAGE: language}),
|
||||
...(stability && {ELEVENLABS_TTS_STREAMING_VOICE_SETTINGS_STABILITY: stability}),
|
||||
...(similarity_boost && {ELEVENLABS_TTS_STREAMING_VOICE_SETTINGS_SIMILARITY_BOOST: similarity_boost}),
|
||||
...(use_speaker_boost && {ELEVENLABS_TTS_STREAMING_VOICE_SETTINGS_USE_SPEAKER_BOOST: use_speaker_boost}),
|
||||
...(style && {ELEVENLABS_TTS_STREAMING_VOICE_SETTINGS_STYLE: style}),
|
||||
// speed has value 0.7 to 1.2, 1.0 is default, make sure we send the value event it's 0
|
||||
...(speed !== null && speed !== undefined && {ELEVENLABS_TTS_STREAMING_VOICE_SETTINGS_SPEED: `${speed}`}),
|
||||
...(this.options.pronunciation_dictionary_locators &&
|
||||
Array.isArray(this.options.pronunciation_dictionary_locators) && {
|
||||
...(local_options.pronunciation_dictionary_locators &&
|
||||
Array.isArray(local_options.pronunciation_dictionary_locators) && {
|
||||
ELEVENLABS_TTS_STREAMING_PRONUNCIATION_DICTIONARY_LOCATORS:
|
||||
JSON.stringify(this.options.pronunciation_dictionary_locators)
|
||||
JSON.stringify(local_options.pronunciation_dictionary_locators)
|
||||
}),
|
||||
};
|
||||
break;
|
||||
case 'rimelabs':
|
||||
const {
|
||||
pauseBetweenBrackets, phonemizeBetweenBrackets, inlineSpeedAlpha, speedAlpha, reduceLatency
|
||||
} = this.options;
|
||||
} = local_options;
|
||||
obj = {
|
||||
RIMELABS_API_KEY: api_key,
|
||||
RIMELABS_TTS_STREAMING_MODEL_ID: model_id,
|
||||
RIMELABS_API_KEY: local_api_key,
|
||||
RIMELABS_TTS_STREAMING_MODEL_ID: local_model_id,
|
||||
RIMELABS_TTS_STREAMING_VOICE_ID: voice,
|
||||
RIMELABS_TTS_STREAMING_LANGUAGE: language || 'en',
|
||||
...(pauseBetweenBrackets && {RIMELABS_TTS_STREAMING_PAUSE_BETWEEN_BRACKETS: pauseBetweenBrackets}),
|
||||
@@ -144,8 +160,8 @@ class TtsTask extends Task {
|
||||
if (vendor.startsWith('custom:')) {
|
||||
const use_tls = custom_tts_streaming_url.startsWith('wss://');
|
||||
obj = {
|
||||
CUSTOM_TTS_STREAMING_HOST: custom_tts_streaming_url.replace(/^(ws|wss):\/\//, ''),
|
||||
CUSTOM_TTS_STREAMING_API_KEY: auth_token,
|
||||
CUSTOM_TTS_STREAMING_HOST: local_custom_tts_streaming_url.replace(/^(ws|wss):\/\//, ''),
|
||||
CUSTOM_TTS_STREAMING_API_KEY: local_auth_token,
|
||||
CUSTOM_TTS_STREAMING_VOICE_ID: voice,
|
||||
CUSTOM_TTS_STREAMING_LANGUAGE: language || 'en',
|
||||
CUSTOM_TTS_STREAMING_USE_TLS: use_tls
|
||||
@@ -258,15 +274,16 @@ class TtsTask extends Task {
|
||||
account_sid,
|
||||
alert_type: AlertType.TTS_NOT_PROVISIONED,
|
||||
vendor,
|
||||
label,
|
||||
target_sid: cs.callSid
|
||||
}).catch((err) => this.logger.info({err}, 'Error generating alert for no tts'));
|
||||
throw new SpeechCredentialError('no provisioned speech credentials for TTS');
|
||||
}
|
||||
|
||||
/* produce an audio segment from the provided text */
|
||||
const generateAudio = async(text) => {
|
||||
if (this.killed) return;
|
||||
if (text.startsWith('silence_stream://')) return text;
|
||||
const generateAudio = async(text, index) => {
|
||||
if (this.killed) return {index, filePath: null};
|
||||
if (text.startsWith('silence_stream://')) return {index, filePath: text};
|
||||
|
||||
/* otel: trace time for tts */
|
||||
if (!preCache && !this._disableTracing) {
|
||||
@@ -295,7 +312,6 @@ class TtsTask extends Task {
|
||||
renderForCaching: preCache
|
||||
});
|
||||
if (!filePath.startsWith('say:')) {
|
||||
this.playbackIds.push(null);
|
||||
this.logger.debug(`Say: file ${filePath}, served from cache ${servedFromCache}`);
|
||||
if (filePath) cs.trackTmpFile(filePath);
|
||||
if (this.otelSpan) {
|
||||
@@ -323,10 +339,11 @@ class TtsTask extends Task {
|
||||
'id': this.id
|
||||
});
|
||||
}
|
||||
return {index, filePath, playbackId: null};
|
||||
}
|
||||
else {
|
||||
this.playbackIds.push(extractPlaybackId(filePath));
|
||||
this.logger.debug({playbackIds: this.playbackIds}, 'Say: a streaming tts api will be used');
|
||||
const playbackId = extractPlaybackId(filePath);
|
||||
this.logger.debug('Say: a streaming tts api will be used');
|
||||
const modifiedPath = filePath.replace('say:{', `say:{session-uuid=${ep.uuid},`);
|
||||
this.notifyStatus({
|
||||
event: 'synthesized-audio',
|
||||
@@ -335,9 +352,8 @@ class TtsTask extends Task {
|
||||
servedFromCache,
|
||||
'id': this.id
|
||||
});
|
||||
return modifiedPath;
|
||||
return {index, filePath: modifiedPath, playbackId};
|
||||
}
|
||||
return filePath;
|
||||
} catch (err) {
|
||||
this.logger.info({err}, 'Error synthesizing tts');
|
||||
if (this.otelSpan) this.otelSpan.end();
|
||||
@@ -345,6 +361,7 @@ class TtsTask extends Task {
|
||||
account_sid: cs.accountSid,
|
||||
alert_type: AlertType.TTS_FAILURE,
|
||||
vendor,
|
||||
label,
|
||||
detail: err.message,
|
||||
target_sid: cs.callSid
|
||||
}).catch((err) => this.logger.info({err}, 'Error generating alert for tts failure'));
|
||||
@@ -352,8 +369,20 @@ class TtsTask extends Task {
|
||||
}
|
||||
};
|
||||
|
||||
const arr = this.text.map((t) => (this._validateURL(t) ? t : generateAudio(t)));
|
||||
return (await Promise.all(arr)).filter((fp) => fp && fp.length);
|
||||
// process all text segments in parallel will cause ordering issue
|
||||
// so we attach index to each promise result and sort them later
|
||||
|
||||
const arr = this.text.map((t, index) => (this._validateURL(t) ?
|
||||
Promise.resolve({index, filePath: t, playbackId: null}) : generateAudio(t, index)));
|
||||
const results = await Promise.all(arr);
|
||||
const sorted = results.sort((a, b) => a.index - b.index);
|
||||
|
||||
return sorted
|
||||
.filter((fp) => fp.filePath && fp.filePath.length)
|
||||
.map((r) => {
|
||||
this.playbackIds.push(r.playbackId);
|
||||
return r.filePath;
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.info(err, 'TaskSay:exec error');
|
||||
throw err;
|
||||
|
||||
@@ -118,6 +118,13 @@ class ActionHookDelayProcessor extends Emitter {
|
||||
this.logger.debug('ActionHookDelayProcessor#_onNoResponseTimer');
|
||||
this._noResponseTimer = null;
|
||||
|
||||
/* check if endpoint is still available (call may have ended) */
|
||||
if (!this.ep) {
|
||||
this.logger.debug('ActionHookDelayProcessor#_onNoResponseTimer: endpoint is null, call may have ended');
|
||||
this._active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the next play or say action */
|
||||
const verb = this.actions[this._retryCount % this.actions.length];
|
||||
|
||||
@@ -129,8 +136,8 @@ class ActionHookDelayProcessor extends Emitter {
|
||||
this._taskInProgress.exec(this.cs, {ep: this.ep}).catch((err) => {
|
||||
this.logger.info(`ActionHookDelayProcessor#_onNoResponseTimer: error playing file: ${err.message}`);
|
||||
this._taskInProgress = null;
|
||||
this.ep.removeAllListeners('playback-start');
|
||||
this.ep.removeAllListeners('playback-stop');
|
||||
this.ep?.removeAllListeners('playback-start');
|
||||
this.ep?.removeAllListeners('playback-stop');
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.info(err, 'ActionHookDelayProcessor#_onNoResponseTimer: error starting action');
|
||||
|
||||
@@ -405,19 +405,21 @@ module.exports = (logger) => {
|
||||
if (ep.amd) {
|
||||
vendor = ep.amd.vendor;
|
||||
ep.amd.stopAllTimers();
|
||||
|
||||
ep.removeListener(GoogleTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(GoogleTranscriptionEvents.EndOfUtterance, ep.amd.EndOfUtteranceHandler);
|
||||
ep.removeListener(AwsTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(AzureTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(AzureTranscriptionEvents.NoSpeechDetected, ep.amd.noSpeechHandler);
|
||||
ep.removeListener(NuanceTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(DeepgramTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(SonioxTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(IbmTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(NvidiaTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(JambonzTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
|
||||
try {
|
||||
ep.removeListener(GoogleTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(GoogleTranscriptionEvents.EndOfUtterance, ep.amd.EndOfUtteranceHandler);
|
||||
ep.removeListener(AwsTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(AzureTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(AzureTranscriptionEvents.NoSpeechDetected, ep.amd.noSpeechHandler);
|
||||
ep.removeListener(NuanceTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(DeepgramTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(SonioxTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(IbmTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(NvidiaTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
ep.removeListener(JambonzTranscriptionEvents.Transcription, ep.amd.transcriptionHandler);
|
||||
} catch (error) {
|
||||
logger.error('Unable to Remove AMD Listener', error);
|
||||
}
|
||||
ep.amd = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -135,26 +135,24 @@ class BackgroundTaskManager extends Emitter {
|
||||
|
||||
// Initiate Record
|
||||
async _initRecord() {
|
||||
if (this.cs.accountInfo.account.record_all_calls || this.cs.application.record_all_calls) {
|
||||
if (!JAMBONZ_RECORD_WS_BASE_URL || !this.cs.accountInfo.account.bucket_credential) {
|
||||
this.logger.error('_initRecord: invalid cfg - missing JAMBONZ_RECORD_WS_BASE_URL or bucket config');
|
||||
return undefined;
|
||||
}
|
||||
const listenOpts = {
|
||||
url: `${JAMBONZ_RECORD_WS_BASE_URL}/record/${this.cs.accountInfo.account.bucket_credential.vendor}`,
|
||||
disableBidirectionalAudio: true,
|
||||
mixType : 'stereo',
|
||||
passDtmf: true
|
||||
};
|
||||
if (JAMBONZ_RECORD_WS_USERNAME && JAMBONZ_RECORD_WS_PASSWORD) {
|
||||
listenOpts.wsAuth = {
|
||||
username: JAMBONZ_RECORD_WS_USERNAME,
|
||||
password: JAMBONZ_RECORD_WS_PASSWORD
|
||||
};
|
||||
}
|
||||
this.logger.debug({listenOpts}, '_initRecord: enabling listen');
|
||||
return await this._initListen({verb: 'listen', ...listenOpts}, 'jambonz-session-record', true, 'record');
|
||||
if (!JAMBONZ_RECORD_WS_BASE_URL || !this.cs.accountInfo.account.bucket_credential) {
|
||||
this.logger.error('_initRecord: invalid cfg - missing JAMBONZ_RECORD_WS_BASE_URL or bucket config');
|
||||
return undefined;
|
||||
}
|
||||
const listenOpts = {
|
||||
url: `${JAMBONZ_RECORD_WS_BASE_URL}/record/${this.cs.accountInfo.account.bucket_credential.vendor}`,
|
||||
disableBidirectionalAudio: true,
|
||||
mixType : 'stereo',
|
||||
passDtmf: true
|
||||
};
|
||||
if (JAMBONZ_RECORD_WS_USERNAME && JAMBONZ_RECORD_WS_PASSWORD) {
|
||||
listenOpts.wsAuth = {
|
||||
username: JAMBONZ_RECORD_WS_USERNAME,
|
||||
password: JAMBONZ_RECORD_WS_PASSWORD
|
||||
};
|
||||
}
|
||||
this.logger.debug({listenOpts}, '_initRecord: enabling listen');
|
||||
return await this._initListen({verb: 'listen', ...listenOpts}, 'jambonz-session-record', true, 'record');
|
||||
}
|
||||
|
||||
// Initiate Transcribe
|
||||
|
||||
@@ -335,7 +335,8 @@
|
||||
"Empty": "tts_streaming::empty",
|
||||
"Pause": "tts_streaming::pause",
|
||||
"Resume": "tts_streaming::resume",
|
||||
"ConnectFailure": "tts_streaming::connect_failed"
|
||||
"ConnectFailure": "tts_streaming::connect_failed",
|
||||
"Connected": "tts_streaming::connected"
|
||||
},
|
||||
"TtsStreamingConnectionStatus": {
|
||||
"NotConnected": "not_connected",
|
||||
@@ -355,5 +356,8 @@
|
||||
"WS_CLOSE_CODES": {
|
||||
"NormalClosure": 1000,
|
||||
"GoingAway": 1001
|
||||
}
|
||||
},
|
||||
"NON_FANTAL_ERRORS": [
|
||||
"File Not Found"
|
||||
]
|
||||
}
|
||||
|
||||
@@ -152,6 +152,7 @@ const speechMapper = (cred) => {
|
||||
obj.client_id = o.client_id;
|
||||
obj.client_key = o.client_key;
|
||||
obj.user_id = o.user_id;
|
||||
obj.houndify_server_uri = o.houndify_server_uri;
|
||||
}
|
||||
else if ('voxist' === obj.vendor) {
|
||||
const o = JSON.parse(decrypt(credential));
|
||||
|
||||
@@ -191,7 +191,7 @@ class HttpRequestor extends BaseRequestor {
|
||||
method,
|
||||
headers: hdrs,
|
||||
...('POST' === method && {body: JSON.stringify(payload)}),
|
||||
timeout: HTTP_TIMEOUT,
|
||||
headersTimeout: HTTP_TIMEOUT,
|
||||
followRedirects: false
|
||||
};
|
||||
|
||||
|
||||
@@ -100,6 +100,30 @@ module.exports = (logger) => {
|
||||
else if (K8S) {
|
||||
lifecycleEmitter.scaleIn = () => process.exit(0);
|
||||
}
|
||||
else {
|
||||
process.on('SIGUSR1', () => {
|
||||
logger.info('received SIGUSR1: begin drying up calls for scale-in');
|
||||
dryUpCalls = true;
|
||||
|
||||
const {srf} = require('../..');
|
||||
const {writeSystemAlerts} = srf.locals;
|
||||
if (writeSystemAlerts) {
|
||||
const {SystemState, FEATURE_SERVER} = require('./constants');
|
||||
writeSystemAlerts({
|
||||
system_component: FEATURE_SERVER,
|
||||
state : SystemState.GracefulShutdownInProgress,
|
||||
fields : {
|
||||
detail: `feature-server with process_id ${process.pid} shutdown in progress`,
|
||||
host: srf.locals?.ipv4
|
||||
}
|
||||
});
|
||||
}
|
||||
pingProxies(srf);
|
||||
|
||||
// Note: in response to SIGUSR1 we start drying up but do not exit when calls reach zero.
|
||||
// This is to allow external scripts that sent the signal to manage the lifecycle.
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function pingProxies(srf) {
|
||||
|
||||
@@ -55,11 +55,28 @@ const extractSdpMedia = (sdp) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getLeadingCodec = (sdp) => {
|
||||
if (!sdp) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = sdpTransform.parse(sdp);
|
||||
const audio = parsed.media?.find((m) => m.type === 'audio');
|
||||
|
||||
if (!audio) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return audio.rtp?.[0]?.codec || null;
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
isOnhold,
|
||||
mergeSdpMedia,
|
||||
extractSdpMedia,
|
||||
isOpusFirst,
|
||||
makeOpusFirst,
|
||||
removeVideoSdp
|
||||
removeVideoSdp,
|
||||
getLeadingCodec
|
||||
};
|
||||
|
||||
@@ -127,7 +127,6 @@ class SttLatencyCalculator extends Emitter {
|
||||
|
||||
calculateLatency() {
|
||||
if (!this.isRunning) {
|
||||
this.logger.debug('Latency calculator is not running, cannot calculate latency, returning default values');
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -920,7 +920,7 @@ module.exports = (logger) => {
|
||||
...(rOpts.initialSpeechTimeoutMs > 0 &&
|
||||
{AZURE_INITIAL_SPEECH_TIMEOUT_MS: rOpts.initialSpeechTimeoutMs}),
|
||||
...(rOpts.requestSnr && {AZURE_REQUEST_SNR: 1}),
|
||||
...(rOpts.audioLogging && {AZURE_AUDIO_LOGGING: 1}),
|
||||
...(azureOptions.audioLogging && {AZURE_AUDIO_LOGGING: 1}),
|
||||
...{AZURE_USE_OUTPUT_FORMAT_DETAILED: 1},
|
||||
...(azureOptions.speechSegmentationSilenceTimeoutMs &&
|
||||
{AZURE_SPEECH_SEGMENTATION_SILENCE_TIMEOUT_MS: azureOptions.speechSegmentationSilenceTimeoutMs}),
|
||||
@@ -1085,13 +1085,6 @@ module.exports = (logger) => {
|
||||
...(keyterms && keyterms.length > 0 && {DEEPGRAMFLUX_SPEECH_KEYTERMS: keyterms.join(',')}),
|
||||
};
|
||||
}
|
||||
else if ('gladia' === vendor) {
|
||||
const {host, path} = sttCredentials;
|
||||
opts = {
|
||||
GLADIA_SPEECH_HOST: host,
|
||||
GLADIA_SPEECH_PATH: path,
|
||||
};
|
||||
}
|
||||
else if ('soniox' === vendor) {
|
||||
const {sonioxOptions = {}} = rOpts;
|
||||
const {storage = {}} = sonioxOptions;
|
||||
@@ -1226,8 +1219,10 @@ module.exports = (logger) => {
|
||||
audioFormat, enableNoiseReduction, enableProfanityFilter, enablePunctuation,
|
||||
enableCapitalization, confidenceThreshold, enableDisfluencyFilter,
|
||||
maxResults, enableWordTimestamps, maxAlternatives, partialTranscriptInterval,
|
||||
sessionTimeout, connectionTimeout, customVocabulary, languageModel
|
||||
sessionTimeout, connectionTimeout, customVocabulary, languageModel,
|
||||
requestInfo, sampleRate
|
||||
} = rOpts.houndifyOptions || {};
|
||||
const audioEndpointUri = audioEndpoint || sttCredentials.houndify_server_uri;
|
||||
|
||||
opts = {
|
||||
...opts,
|
||||
@@ -1263,10 +1258,12 @@ module.exports = (logger) => {
|
||||
...(country && {HOUNDIFY_COUNTRY: country}),
|
||||
...(timeZone && {HOUNDIFY_TIMEZONE: timeZone}),
|
||||
...(domain && {HOUNDIFY_DOMAIN: domain}),
|
||||
...(audioEndpoint && {HOUNDIFY_AUDIO_ENDPOINT: audioEndpoint}),
|
||||
...(audioEndpointUri && {HOUNDIFY_AUDIO_ENDPOINT: audioEndpointUri}),
|
||||
...(customVocabulary && {HOUNDIFY_CUSTOM_VOCABULARY:
|
||||
Array.isArray(customVocabulary) ? customVocabulary.join(',') : customVocabulary}),
|
||||
...(languageModel && {HOUNDIFY_LANGUAGE_MODEL: languageModel}),
|
||||
...(requestInfo && {HOUNDIFY_REQUEST_INFO: JSON.stringify(requestInfo)}),
|
||||
...(sampleRate && {HOUNDIFY_SAMPLING_RATE: sampleRate}),
|
||||
};
|
||||
}
|
||||
else if ('voxist' === vendor) {
|
||||
@@ -1313,6 +1310,9 @@ module.exports = (logger) => {
|
||||
...(openaiOptions.turn_detection.silence_duration_ms && {
|
||||
OPENAI_TURN_DETECTION_SILENCE_DURATION_MS: openaiOptions.turn_detection.silence_duration_ms
|
||||
}),
|
||||
...(openaiOptions.turn_detection.eagerness && {
|
||||
OPENAI_TURN_DETECTION_EAGERNESS: openaiOptions.turn_detection.eagerness
|
||||
})
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ class TtsStreamingBuffer extends Emitter {
|
||||
clearTimeout(this.timer);
|
||||
this.removeCustomEventListeners();
|
||||
if (this.ep) {
|
||||
this._api(this.ep, [this.ep.uuid, 'close'])
|
||||
this._api(this.ep, [this.ep.uuid, 'stop'])
|
||||
.catch((err) =>
|
||||
this.logger.info({ err }, 'TtsStreamingBuffer:stop Error closing TTS streaming')
|
||||
);
|
||||
@@ -163,7 +163,6 @@ class TtsStreamingBuffer extends Emitter {
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.logger.debug('TtsStreamingBuffer:clear');
|
||||
if (this._connectionStatus !== TtsStreamingConnectionStatus.Connected) return;
|
||||
clearTimeout(this.timer);
|
||||
this._api(this.ep, [this.ep.uuid, 'clear']).catch((err) =>
|
||||
@@ -193,10 +192,7 @@ class TtsStreamingBuffer extends Emitter {
|
||||
this.logger.debug('TtsStreamingBuffer:_feedQueue TTS stream is not open or no endpoint available');
|
||||
return;
|
||||
}
|
||||
if (
|
||||
this._connectionStatus === TtsStreamingConnectionStatus.NotConnected ||
|
||||
this._connectionStatus === TtsStreamingConnectionStatus.Failed
|
||||
) {
|
||||
if (this._connectionStatus !== TtsStreamingConnectionStatus.Connected) {
|
||||
this.logger.debug('TtsStreamingBuffer:_feedQueue TTS stream is not connected');
|
||||
return;
|
||||
}
|
||||
@@ -278,6 +274,14 @@ class TtsStreamingBuffer extends Emitter {
|
||||
}
|
||||
const chunk = combinedText.slice(0, chunkEnd);
|
||||
|
||||
// Check if the chunk is only whitespace before processing the queue
|
||||
// If so, wait for more meaningful text
|
||||
if (isWhitespace(chunk)) {
|
||||
this.logger.debug('TtsStreamingBuffer:_feedQueue chunk is only whitespace, waiting for more text');
|
||||
this._setTimerIfNeeded();
|
||||
return;
|
||||
}
|
||||
|
||||
// Now we iterate over the queue items
|
||||
// and deduct their lengths until we've accounted for chunkEnd characters.
|
||||
let remaining = chunkEnd;
|
||||
@@ -301,6 +305,14 @@ class TtsStreamingBuffer extends Emitter {
|
||||
this.bufferedLength -= chunkEnd;
|
||||
|
||||
const modifiedChunk = chunk.replace(/\n\n/g, '\n \n');
|
||||
|
||||
if (isWhitespace(modifiedChunk)) {
|
||||
this.logger.debug('TtsStreamingBuffer:_feedQueue modified chunk is only whitespace, restoring queue');
|
||||
this.queue.unshift({ type: 'text', value: chunk });
|
||||
this.bufferedLength += chunkEnd;
|
||||
this._setTimerIfNeeded();
|
||||
return;
|
||||
}
|
||||
this.logger.debug(`TtsStreamingBuffer:_feedQueue sending chunk to tts: ${modifiedChunk}`);
|
||||
|
||||
try {
|
||||
@@ -349,6 +361,7 @@ class TtsStreamingBuffer extends Emitter {
|
||||
if (this.queue.length > 0) {
|
||||
await this._feedQueue();
|
||||
}
|
||||
this.emit(TtsStreamingEvents.Connected, { vendor });
|
||||
}
|
||||
|
||||
_onConnectFailure(vendor) {
|
||||
@@ -399,6 +412,7 @@ class TtsStreamingBuffer extends Emitter {
|
||||
|
||||
removeCustomEventListeners() {
|
||||
this.eventHandlers.forEach((h) => h.ep.removeCustomEventListener(h.event, h.handler));
|
||||
this.eventHandlers.length = 0;
|
||||
}
|
||||
|
||||
_initHandlers(ep) {
|
||||
@@ -422,7 +436,15 @@ class TtsStreamingBuffer extends Emitter {
|
||||
|
||||
const findSentenceBoundary = (text, limit) => {
|
||||
// Look for punctuation or double newline that signals sentence end.
|
||||
const sentenceEndRegex = /[.!?](?=\s|$)|\n\n/g;
|
||||
// Includes:
|
||||
// - ASCII: . ! ?
|
||||
// - Arabic: ؟ (question mark), ۔ (full stop)
|
||||
// - Japanese: 。 (full stop), !, ? (full-width exclamation/question)
|
||||
//
|
||||
// For languages that use spaces between sentences, we still require
|
||||
// whitespace or end-of-string after the mark. For Japanese (no spaces),
|
||||
// we treat the punctuation itself as a boundary regardless of following char.
|
||||
const sentenceEndRegex = /[.!?؟۔](?=\s|$)|[。!?]|\n\n/g;
|
||||
let lastSentenceBoundary = -1;
|
||||
let match;
|
||||
while ((match = sentenceEndRegex.exec(text)) && match.index < limit) {
|
||||
|
||||
5296
package-lock.json
generated
5296
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@@ -31,10 +31,10 @@
|
||||
"@jambonz/http-health-check": "^0.0.1",
|
||||
"@jambonz/mw-registrar": "^0.2.7",
|
||||
"@jambonz/realtimedb-helpers": "^0.8.15",
|
||||
"@jambonz/speech-utils": "^0.2.25",
|
||||
"@jambonz/speech-utils": "^0.2.26",
|
||||
"@jambonz/stats-collector": "^0.1.10",
|
||||
"@jambonz/time-series": "^0.2.14",
|
||||
"@jambonz/verb-specifications": "^0.0.119",
|
||||
"@jambonz/time-series": "^0.2.15",
|
||||
"@jambonz/verb-specifications": "^0.0.125",
|
||||
"@modelcontextprotocol/sdk": "^1.9.0",
|
||||
"@opentelemetry/api": "^1.8.0",
|
||||
"@opentelemetry/exporter-jaeger": "^1.23.0",
|
||||
@@ -49,12 +49,12 @@
|
||||
"debug": "^4.3.4",
|
||||
"deepcopy": "^2.1.0",
|
||||
"drachtio-fsmrf": "^4.1.2",
|
||||
"drachtio-srf": "^5.0.11",
|
||||
"drachtio-srf": "^5.0.14",
|
||||
"express": "^4.19.2",
|
||||
"express-validator": "^7.0.1",
|
||||
"moment": "^2.30.1",
|
||||
"parse-url": "^9.2.0",
|
||||
"pino": "^8.20.0",
|
||||
"pino": "^10.1.0",
|
||||
"polly-ssml-split": "^0.1.0",
|
||||
"sdp-transform": "^2.15.0",
|
||||
"short-uuid": "^5.1.0",
|
||||
|
||||
@@ -83,7 +83,8 @@ test('invalid jambonz json create alert tests', async(t) => {
|
||||
{account_sid: 'bb845d4b-83a9-4cde-a6e9-50f3743bab3f', page: 1, page_size: 25, days: 7});
|
||||
let checked = false;
|
||||
for (let i = 0; i < data.total; i++) {
|
||||
checked = data.data[i].message === 'malformed jambonz payload: must be array'
|
||||
checked = data.data[i].message === 'malformed jambonz payload: must be array';
|
||||
if (checked) break;
|
||||
}
|
||||
t.ok(checked, 'alert is raised as expected');
|
||||
disconnect();
|
||||
|
||||
Reference in New Issue
Block a user