@memberjunction/telephony-adapters 6.2.0-edge.2 → 6.2.0-edge.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/resolvers/LiveKitSipResolver.d.ts +21 -0
- package/dist/resolvers/LiveKitSipResolver.d.ts.map +1 -0
- package/dist/resolvers/LiveKitSipResolver.js +87 -0
- package/dist/resolvers/LiveKitSipResolver.js.map +1 -0
- package/dist/resolvers/RingCentralTelephonyResolver.d.ts.map +1 -1
- package/dist/resolvers/RingCentralTelephonyResolver.js +4 -1
- package/dist/resolvers/RingCentralTelephonyResolver.js.map +1 -1
- package/dist/resolvers/TelephonyResolver.d.ts.map +1 -1
- package/dist/resolvers/TelephonyResolver.js +4 -1
- package/dist/resolvers/TelephonyResolver.js.map +1 -1
- package/dist/resolvers/VonageTelephonyResolver.d.ts.map +1 -1
- package/dist/resolvers/VonageTelephonyResolver.js +4 -1
- package/dist/resolvers/VonageTelephonyResolver.js.map +1 -1
- package/dist/resolvers/index.d.ts +1 -0
- package/dist/resolvers/index.d.ts.map +1 -1
- package/dist/resolvers/index.js +1 -0
- package/dist/resolvers/index.js.map +1 -1
- package/dist/server-extensions/LiveKitSipExtension.d.ts +14 -0
- package/dist/server-extensions/LiveKitSipExtension.d.ts.map +1 -0
- package/dist/server-extensions/LiveKitSipExtension.js +93 -0
- package/dist/server-extensions/LiveKitSipExtension.js.map +1 -0
- package/dist/server-extensions/RingCentralTelephonyExtension.d.ts.map +1 -1
- package/dist/server-extensions/RingCentralTelephonyExtension.js +8 -2
- package/dist/server-extensions/RingCentralTelephonyExtension.js.map +1 -1
- package/dist/server-extensions/TwilioTelephonyExtension.d.ts.map +1 -1
- package/dist/server-extensions/TwilioTelephonyExtension.js +10 -4
- package/dist/server-extensions/TwilioTelephonyExtension.js.map +1 -1
- package/dist/server-extensions/VonageTelephonyExtension.d.ts.map +1 -1
- package/dist/server-extensions/VonageTelephonyExtension.js +8 -4
- package/dist/server-extensions/VonageTelephonyExtension.js.map +1 -1
- package/dist/telephony/LiveKitSipRouter.d.ts +26 -0
- package/dist/telephony/LiveKitSipRouter.d.ts.map +1 -0
- package/dist/telephony/LiveKitSipRouter.js +62 -0
- package/dist/telephony/LiveKitSipRouter.js.map +1 -0
- package/dist/telephony/LiveKitSipTelephonyService.d.ts +140 -0
- package/dist/telephony/LiveKitSipTelephonyService.d.ts.map +1 -0
- package/dist/telephony/LiveKitSipTelephonyService.js +501 -0
- package/dist/telephony/LiveKitSipTelephonyService.js.map +1 -0
- package/dist/telephony/RingCentralTelephonyService.d.ts +57 -8
- package/dist/telephony/RingCentralTelephonyService.d.ts.map +1 -1
- package/dist/telephony/RingCentralTelephonyService.js +137 -77
- package/dist/telephony/RingCentralTelephonyService.js.map +1 -1
- package/dist/telephony/TeamsMeetingsService.d.ts +5 -2
- package/dist/telephony/TeamsMeetingsService.d.ts.map +1 -1
- package/dist/telephony/TeamsMeetingsService.js +9 -2
- package/dist/telephony/TeamsMeetingsService.js.map +1 -1
- package/dist/telephony/TwilioTelephonyRouter.d.ts +36 -6
- package/dist/telephony/TwilioTelephonyRouter.d.ts.map +1 -1
- package/dist/telephony/TwilioTelephonyRouter.js +150 -54
- package/dist/telephony/TwilioTelephonyRouter.js.map +1 -1
- package/dist/telephony/TwilioTelephonyService.d.ts +64 -16
- package/dist/telephony/TwilioTelephonyService.d.ts.map +1 -1
- package/dist/telephony/TwilioTelephonyService.js +151 -77
- package/dist/telephony/TwilioTelephonyService.js.map +1 -1
- package/dist/telephony/VonageTelephonyRouter.d.ts +29 -1
- package/dist/telephony/VonageTelephonyRouter.d.ts.map +1 -1
- package/dist/telephony/VonageTelephonyRouter.js +84 -72
- package/dist/telephony/VonageTelephonyRouter.js.map +1 -1
- package/dist/telephony/VonageTelephonyService.d.ts +66 -8
- package/dist/telephony/VonageTelephonyService.d.ts.map +1 -1
- package/dist/telephony/VonageTelephonyService.js +152 -67
- package/dist/telephony/VonageTelephonyService.js.map +1 -1
- package/dist/telephony/agentIdentityLookup.d.ts +30 -0
- package/dist/telephony/agentIdentityLookup.d.ts.map +1 -0
- package/dist/telephony/agentIdentityLookup.js +129 -0
- package/dist/telephony/agentIdentityLookup.js.map +1 -0
- package/dist/telephony/callEndObserver.d.ts +47 -0
- package/dist/telephony/callEndObserver.d.ts.map +1 -0
- package/dist/telephony/callEndObserver.js +95 -0
- package/dist/telephony/callEndObserver.js.map +1 -0
- package/dist/telephony/callLifecycleTracker.d.ts +83 -0
- package/dist/telephony/callLifecycleTracker.d.ts.map +1 -0
- package/dist/telephony/callLifecycleTracker.js +155 -0
- package/dist/telephony/callLifecycleTracker.js.map +1 -0
- package/dist/telephony/callerIdentity.d.ts +65 -0
- package/dist/telephony/callerIdentity.d.ts.map +1 -0
- package/dist/telephony/callerIdentity.js +72 -0
- package/dist/telephony/callerIdentity.js.map +1 -0
- package/dist/telephony/handoffDestinations.d.ts +57 -0
- package/dist/telephony/handoffDestinations.d.ts.map +1 -0
- package/dist/telephony/handoffDestinations.js +78 -0
- package/dist/telephony/handoffDestinations.js.map +1 -0
- package/dist/telephony/index.d.ts +21 -0
- package/dist/telephony/index.d.ts.map +1 -1
- package/dist/telephony/index.js +23 -0
- package/dist/telephony/index.js.map +1 -1
- package/dist/telephony/interactionLifecycle.d.ts +101 -0
- package/dist/telephony/interactionLifecycle.d.ts.map +1 -0
- package/dist/telephony/interactionLifecycle.js +340 -0
- package/dist/telephony/interactionLifecycle.js.map +1 -0
- package/dist/telephony/livekit-sip-runtime.d.ts +55 -0
- package/dist/telephony/livekit-sip-runtime.d.ts.map +1 -0
- package/dist/telephony/livekit-sip-runtime.js +36 -0
- package/dist/telephony/livekit-sip-runtime.js.map +1 -0
- package/dist/telephony/mediaSocketAuth.d.ts +77 -0
- package/dist/telephony/mediaSocketAuth.d.ts.map +1 -0
- package/dist/telephony/mediaSocketAuth.js +136 -0
- package/dist/telephony/mediaSocketAuth.js.map +1 -0
- package/dist/telephony/numberPoolSelector.d.ts +62 -0
- package/dist/telephony/numberPoolSelector.d.ts.map +1 -0
- package/dist/telephony/numberPoolSelector.js +201 -0
- package/dist/telephony/numberPoolSelector.js.map +1 -0
- package/dist/telephony/outboundCallPolicy.d.ts +226 -0
- package/dist/telephony/outboundCallPolicy.d.ts.map +1 -0
- package/dist/telephony/outboundCallPolicy.js +364 -0
- package/dist/telephony/outboundCallPolicy.js.map +1 -0
- package/dist/telephony/roomCallSession.d.ts +136 -0
- package/dist/telephony/roomCallSession.d.ts.map +1 -0
- package/dist/telephony/roomCallSession.js +361 -0
- package/dist/telephony/roomCallSession.js.map +1 -0
- package/dist/telephony/roomCallTools.d.ts +99 -0
- package/dist/telephony/roomCallTools.d.ts.map +1 -0
- package/dist/telephony/roomCallTools.js +255 -0
- package/dist/telephony/roomCallTools.js.map +1 -0
- package/dist/telephony/runAsIdentity.d.ts +62 -0
- package/dist/telephony/runAsIdentity.d.ts.map +1 -0
- package/dist/telephony/runAsIdentity.js +89 -0
- package/dist/telephony/runAsIdentity.js.map +1 -0
- package/dist/telephony/sipTrunkCarrier.d.ts +100 -0
- package/dist/telephony/sipTrunkCarrier.d.ts.map +1 -0
- package/dist/telephony/sipTrunkCarrier.js +118 -0
- package/dist/telephony/sipTrunkCarrier.js.map +1 -0
- package/dist/telephony/teamsAcsMediaRegistry.d.ts.map +1 -1
- package/dist/telephony/teamsAcsMediaRegistry.js +3 -2
- package/dist/telephony/teamsAcsMediaRegistry.js.map +1 -1
- package/dist/telephony/telephonyCallSession.d.ts +135 -0
- package/dist/telephony/telephonyCallSession.d.ts.map +1 -0
- package/dist/telephony/telephonyCallSession.js +331 -0
- package/dist/telephony/telephonyCallSession.js.map +1 -0
- package/dist/telephony/telephonyCallTools.d.ts +112 -0
- package/dist/telephony/telephonyCallTools.d.ts.map +1 -0
- package/dist/telephony/telephonyCallTools.js +269 -0
- package/dist/telephony/telephonyCallTools.js.map +1 -0
- package/dist/telephony/telephonyCapacity.d.ts +63 -0
- package/dist/telephony/telephonyCapacity.d.ts.map +1 -0
- package/dist/telephony/telephonyCapacity.js +94 -0
- package/dist/telephony/telephonyCapacity.js.map +1 -0
- package/dist/telephony/telephonySettings.d.ts +40 -0
- package/dist/telephony/telephonySettings.d.ts.map +1 -0
- package/dist/telephony/telephonySettings.js +69 -0
- package/dist/telephony/telephonySettings.js.map +1 -0
- package/dist/telephony/twilioMediaRegistry.d.ts +83 -11
- package/dist/telephony/twilioMediaRegistry.d.ts.map +1 -1
- package/dist/telephony/twilioMediaRegistry.js +145 -22
- package/dist/telephony/twilioMediaRegistry.js.map +1 -1
- package/dist/telephony/vonageMediaRegistry.d.ts +96 -2
- package/dist/telephony/vonageMediaRegistry.d.ts.map +1 -1
- package/dist/telephony/vonageMediaRegistry.js +227 -26
- package/dist/telephony/vonageMediaRegistry.js.map +1 -1
- package/dist/telephony/webhookParams.d.ts +21 -0
- package/dist/telephony/webhookParams.d.ts.map +1 -0
- package/dist/telephony/webhookParams.js +45 -0
- package/dist/telephony/webhookParams.js.map +1 -0
- package/dist/types.d.ts +90 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +16 -13
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The narrow interface a SIP-trunk carrier has to satisfy to carry phone calls to and from LiveKit SIP.
|
|
3
|
+
*
|
|
4
|
+
* In the room-based call path the carrier is only a pipe: it terminates the phone network and speaks SIP to LiveKit. It
|
|
5
|
+
* does not run the agent, route the call or transfer it; MJ and LiveKit do that. So the carrier interface is small, and
|
|
6
|
+
* it is about *configuration* rather than calls: what the carrier's trunk must be set to so it points at LiveKit, whether
|
|
7
|
+
* the settings MJ was given look right, and whether that looks healthy.
|
|
8
|
+
*
|
|
9
|
+
* Twilio Elastic SIP Trunking is the first implementation. It validates and describes configuration only; it makes no
|
|
10
|
+
* Twilio REST calls, so it needs no Twilio credentials and cannot change anything on the Twilio account. Another carrier
|
|
11
|
+
* (Telnyx, Bandwidth, a SIP provider) is another implementation of {@link ISipTrunkCarrier}.
|
|
12
|
+
*
|
|
13
|
+
* **Vendor details here are not live-verified.** The Twilio hostname shapes and the settings an Elastic SIP Trunk needs
|
|
14
|
+
* are written from Twilio's and LiveKit's documentation; confirm them against a real trunk (see
|
|
15
|
+
* `plans/realtime/bridges-and-widget/LIVE-CALL-CHECKLIST.md`).
|
|
16
|
+
*
|
|
17
|
+
* @module @memberjunction/telephony-adapters
|
|
18
|
+
*/
|
|
19
|
+
import { LogError } from '@memberjunction/core';
|
|
20
|
+
import { IsValidE164 } from './outboundCallPolicy.js';
|
|
21
|
+
const TWILIO_TERMINATION_HOST = /^[a-z0-9-]+\.pstn(\.[a-z0-9-]+)?\.twilio\.com$/i;
|
|
22
|
+
const SIP_URI = /^sips?:[^\s]+$/i;
|
|
23
|
+
/** A Twilio Elastic SIP Trunk, validated from configuration alone. */
|
|
24
|
+
export class TwilioElasticSipTrunkCarrier {
|
|
25
|
+
constructor(settings) {
|
|
26
|
+
this.settings = settings;
|
|
27
|
+
this.Name = 'twilio-elastic-sip';
|
|
28
|
+
}
|
|
29
|
+
DescribeTrunk() {
|
|
30
|
+
return {
|
|
31
|
+
Carrier: 'Twilio Elastic SIP Trunking',
|
|
32
|
+
Steps: [
|
|
33
|
+
'In Twilio, create an Elastic SIP Trunk (Voice > Manage > Elastic SIP Trunking).',
|
|
34
|
+
`Origination: add an Origination URI that points at your LiveKit SIP endpoint${this.settings.originationUri ? ` (${this.settings.originationUri})` : ''}; calls from the phone network are sent there.`,
|
|
35
|
+
'Phone numbers: associate each number the deployment answers with the trunk.',
|
|
36
|
+
'Termination: set a Termination SIP URI (<name>.pstn.twilio.com) and authenticate LiveKit with a credential list or an IP access control list; only needed to dial out.',
|
|
37
|
+
'In LiveKit, create an inbound trunk for the same numbers and an outbound trunk that uses the Termination URI and those credentials.',
|
|
38
|
+
],
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
ValidateConfig(context) {
|
|
42
|
+
const problems = [];
|
|
43
|
+
const warnings = [];
|
|
44
|
+
this.checkOrigination(problems);
|
|
45
|
+
this.checkNumbers(context, problems, warnings);
|
|
46
|
+
if (context.DialOut) {
|
|
47
|
+
this.checkTermination(problems, warnings);
|
|
48
|
+
}
|
|
49
|
+
return { Valid: problems.length === 0, Problems: problems, Warnings: warnings };
|
|
50
|
+
}
|
|
51
|
+
async CheckHealth(context) {
|
|
52
|
+
const check = this.ValidateConfig(context);
|
|
53
|
+
return {
|
|
54
|
+
Healthy: check.Valid,
|
|
55
|
+
Detail: check.Valid
|
|
56
|
+
? 'The trunk settings look valid. This is a configuration check only; Twilio was not contacted.'
|
|
57
|
+
: `The trunk settings have problems: ${check.Problems.join(' ')} (configuration check only; Twilio was not contacted).`,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async ListNumbers() {
|
|
61
|
+
return (this.settings.numbers ?? []).map((n) => ({ Number: n }));
|
|
62
|
+
}
|
|
63
|
+
checkOrigination(problems) {
|
|
64
|
+
const uri = this.settings.originationUri?.trim();
|
|
65
|
+
if (!uri) {
|
|
66
|
+
problems.push('originationUri is not set: Twilio needs the LiveKit SIP URI it should send inbound calls to.');
|
|
67
|
+
}
|
|
68
|
+
else if (!SIP_URI.test(uri)) {
|
|
69
|
+
problems.push(`originationUri '${uri}' is not a SIP URI (expected sip:… or sips:…).`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
checkNumbers(context, problems, warnings) {
|
|
73
|
+
const declared = this.settings.numbers ?? [];
|
|
74
|
+
for (const number of declared) {
|
|
75
|
+
if (!IsValidE164(number)) {
|
|
76
|
+
problems.push(`The trunk number '${number}' is not E.164 (for example +14155550123).`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (declared.length === 0) {
|
|
80
|
+
warnings.push('The trunk lists no numbers, so the check cannot confirm the deployment\'s numbers are on it.');
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
for (const number of context.Numbers) {
|
|
84
|
+
if (!declared.includes(number)) {
|
|
85
|
+
warnings.push(`The deployment answers ${number}, which is not on the trunk's number list.`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
checkTermination(problems, warnings) {
|
|
90
|
+
const host = this.settings.terminationUri?.trim();
|
|
91
|
+
if (!host) {
|
|
92
|
+
problems.push('terminationUri is not set, but the deployment dials out: Twilio needs a Termination SIP URI.');
|
|
93
|
+
}
|
|
94
|
+
else if (!TWILIO_TERMINATION_HOST.test(host)) {
|
|
95
|
+
problems.push(`terminationUri '${host}' does not look like a Twilio termination URI (<name>.pstn.twilio.com).`);
|
|
96
|
+
}
|
|
97
|
+
if (!this.settings.terminationAuth) {
|
|
98
|
+
warnings.push('terminationAuth is not set: Twilio only accepts outbound calls from LiveKit when it authenticates with a credential list or an IP access control list.');
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Builds the carrier for the settings, or `undefined` when none are given or the type is unknown (logged: a typo in the
|
|
104
|
+
* carrier type must be visible, but a missing carrier block is allowed because the check is optional).
|
|
105
|
+
*/
|
|
106
|
+
export function CreateSipTrunkCarrier(settings) {
|
|
107
|
+
if (!settings) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
switch (settings.type) {
|
|
111
|
+
case 'twilio-elastic-sip':
|
|
112
|
+
return new TwilioElasticSipTrunkCarrier(settings);
|
|
113
|
+
default:
|
|
114
|
+
LogError(`[Telephony][LiveKitSip] unknown carrier type '${settings.type ?? ''}'; skipping the carrier check.`);
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=sipTrunkCarrier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sipTrunkCarrier.js","sourceRoot":"","sources":["../../src/teleph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AcsMediaRegistry.d.ts","sourceRoot":"","sources":["../../src/telephony/teamsAcsMediaRegistry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"teamsAcsMediaRegistry.d.ts","sourceRoot":"","sources":["../../src/telephony/teamsAcsMediaRegistry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAE3F;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,mGAAmG;IACnG,cAAc,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI,CAAC;IACvC,2EAA2E;IAC3E,KAAK,IAAI,IAAI,CAAC;CACjB;AAUD;;GAEG;AACH,qBAAa,qBAAsB,YAAW,aAAa;aAG3B,UAAU;IAFtC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAuC;gBAEpC,UAAU,SAAQ;IAE9C,sGAAsG;IAC/F,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAMlC,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,WAAW,GAAG,IAAI;IAS5C,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,KAAK,EAAE,oBAAoB,KAAK,IAAI,GAAG,IAAI;IAI7E,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,KAAK,IAAI,GAAG,IAAI;IAM5F,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,kBAAkB,GAAG,IAAI;IASpE,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,oBAAoB,GAAG,IAAI;IAUlE,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI;IAU/E,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAa7B,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO;IAIvC,OAAO,CAAC,aAAa;CAQxB"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @module @memberjunction/telephony-adapters
|
|
5
5
|
*/
|
|
6
|
+
import { LogError } from '@memberjunction/core';
|
|
6
7
|
/**
|
|
7
8
|
* Coordinates ACS application-hosted-media transports with the bridge bindings, keyed by Graph call id.
|
|
8
9
|
*/
|
|
@@ -66,8 +67,8 @@ export class TeamsAcsMediaRegistry {
|
|
|
66
67
|
try {
|
|
67
68
|
channel.transport?.close();
|
|
68
69
|
}
|
|
69
|
-
catch {
|
|
70
|
-
|
|
70
|
+
catch (err) {
|
|
71
|
+
LogError(`[TeamsAcsMediaRegistry] Error closing transport for call ${callId}`, undefined, err);
|
|
71
72
|
}
|
|
72
73
|
this.channels.delete(callId);
|
|
73
74
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"teamsAcsMediaRegistry.js","sourceRoot":"","sources":["../../src/telephony/teamsAcsMediaRegistry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;
|
|
1
|
+
{"version":3,"file":"teamsAcsMediaRegistry.js","sourceRoot":"","sources":["../../src/telephony/teamsAcsMediaRegistry.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAqBhD;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAG9B,YAA4B,aAAa,KAAK;QAAlB,eAAU,GAAV,UAAU,CAAQ;QAF7B,aAAQ,GAAG,IAAI,GAAG,EAA4B,CAAC;IAEf,CAAC;IAElD,sGAAsG;IAC/F,YAAY,CAAC,MAAc;QAC9B,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;IAC/B,CAAC;IAED,iFAAiF;IAE1E,IAAI,CAAC,MAAc,EAAE,GAAgB;QACxC,MAAM,OAAO,GAA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
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Starts ONE phone call's agent session, the same way for every carrier.
|
|
3
|
+
*
|
|
4
|
+
* Twilio, Vonage and RingCentral differ in how a call reaches MJ and how its media is carried, but once a call is
|
|
5
|
+
* admitted the work is identical: pick the co-agent that voices the dialled agent, create the agent session, open
|
|
6
|
+
* the realtime model session with the phone framing and call-control tools, start the bridge session on the bridge
|
|
7
|
+
* engine, and wire what happens next — barge-in, recovery from a dropped model session, the transcript, and
|
|
8
|
+
* cleanup. That used to be copied into each carrier's service (and had drifted); it lives here once.
|
|
9
|
+
*
|
|
10
|
+
* What the helper guarantees:
|
|
11
|
+
* - the **co-agent** is resolved through the shared chain (the browser path's), and the dialled agent is the
|
|
12
|
+
* TARGET it voices — the two are never collapsed into one id;
|
|
13
|
+
* - the call-capacity lease and the agent-session row are released/closed on EVERY path — a failed start, a
|
|
14
|
+
* normal hang-up, a model loss — never leaked;
|
|
15
|
+
* - the transcript lands in the call's OWN conversation, with the dialled agent attributed;
|
|
16
|
+
* - a talk-over drops queued spoken progress but leaves delegated work running (the caller cancels it explicitly);
|
|
17
|
+
* - a model session lost mid-call is re-opened once with the conversation so far (the engine owns the retry cap).
|
|
18
|
+
*
|
|
19
|
+
* @module @memberjunction/telephony-adapters
|
|
20
|
+
*/
|
|
21
|
+
import { IMetadataProvider, UserInfo } from '@memberjunction/core';
|
|
22
|
+
import type { MJAIBridgeAgentIdentityEntity, MJAIBridgeProviderEntity } from '@memberjunction/core-entities';
|
|
23
|
+
import type { AIBridgeEngine } from '@memberjunction/ai-bridge-server';
|
|
24
|
+
import { CreateBridgeRealtimeSession, ResolveRealtimeCoAgentID } from '@memberjunction/ai-agents';
|
|
25
|
+
import { type BridgeDisconnectReason, type BridgeNativeSdkBinding } from '@memberjunction/ai-bridge-base';
|
|
26
|
+
import type { IAgentSessionManager } from '../sessionManager.js';
|
|
27
|
+
import { type ICallerIdentityResolver } from './callerIdentity.js';
|
|
28
|
+
import type { CallCapacityLease } from './telephonyCapacity.js';
|
|
29
|
+
import { type OutboundAuthorization, type OutboundCallPolicy, type OutboundCallRequest, type OutboundGuardDeps, type TransferTarget } from './outboundCallPolicy.js';
|
|
30
|
+
import { InteractionLifecycleService } from './interactionLifecycle.js';
|
|
31
|
+
/** The part of the bridge engine the starter drives (a `Pick` so tests inject a fake). */
|
|
32
|
+
export type TelephonyStartEngine = Pick<AIBridgeEngine, 'Config' | 'StartBridgeSession' | 'StopBridgeSession'>;
|
|
33
|
+
/** The collaborators the starter uses (production defaults are wired by each carrier service). */
|
|
34
|
+
export interface TelephonyCallStarterDeps {
|
|
35
|
+
Engine: TelephonyStartEngine;
|
|
36
|
+
SessionFactory: typeof CreateBridgeRealtimeSession;
|
|
37
|
+
SessionManager: IAgentSessionManager;
|
|
38
|
+
/** Resolves the co-agent for a target agent (defaults to the shared chain). */
|
|
39
|
+
CoAgentResolver: typeof ResolveRealtimeCoAgentID;
|
|
40
|
+
/** Resolves who an inbound caller is. */
|
|
41
|
+
CallerResolver: ICallerIdentityResolver;
|
|
42
|
+
/** The outbound policy a transfer destination is checked against. */
|
|
43
|
+
OutboundPolicy: OutboundCallPolicy;
|
|
44
|
+
/** The validated transfer directory (`telephony.transferTargets`); empty means the agent cannot transfer. */
|
|
45
|
+
TransferTargets?: readonly TransferTarget[];
|
|
46
|
+
/** Manages Interaction, InteractionEvent, and InteractionLink rows. */
|
|
47
|
+
InteractionLifecycle?: InteractionLifecycleService;
|
|
48
|
+
/** Estimated carrier cost per minute for computing CostEstimate on Interaction. */
|
|
49
|
+
CostPerMinute?: number;
|
|
50
|
+
}
|
|
51
|
+
/** One call to start. */
|
|
52
|
+
export interface TelephonyCallStartArgs {
|
|
53
|
+
/** Resolves the seeded carrier provider row (called after the engine's metadata is loaded; may throw when it is missing). */
|
|
54
|
+
ResolveProvider: () => MJAIBridgeProviderEntity;
|
|
55
|
+
/** The agent identity (the DID the agent answers on, and the agent it pins). */
|
|
56
|
+
Identity: Pick<MJAIBridgeAgentIdentityEntity, 'AgentID' | 'IdentityValue'>;
|
|
57
|
+
Direction: 'Inbound' | 'Outbound';
|
|
58
|
+
/** The other party's number: the caller (inbound) or the number being dialled (outbound). */
|
|
59
|
+
RemoteNumber: string;
|
|
60
|
+
/** The driver configuration — build it with {@link BuildTelephonyCallConfiguration}. */
|
|
61
|
+
Configuration: Record<string, unknown>;
|
|
62
|
+
/** Binds the carrier's real SDK onto the driver. */
|
|
63
|
+
BindSdk: BridgeNativeSdkBinding;
|
|
64
|
+
ContextUser: UserInfo;
|
|
65
|
+
MetadataProvider: IMetadataProvider;
|
|
66
|
+
/** The capacity slot the call already holds; the starter owns releasing it. */
|
|
67
|
+
Lease: CallCapacityLease;
|
|
68
|
+
}
|
|
69
|
+
/** The identifiers a carrier service needs once the call's session is up. */
|
|
70
|
+
export interface StartedTelephonyCall {
|
|
71
|
+
/** The carrier's call id (the bridge's external connection id). */
|
|
72
|
+
RoomKey?: string;
|
|
73
|
+
SessionBridgeID: string;
|
|
74
|
+
}
|
|
75
|
+
/** The inputs of the per-call driver configuration. */
|
|
76
|
+
export interface CallConfigurationInput {
|
|
77
|
+
Direction: 'Inbound' | 'Outbound';
|
|
78
|
+
/** Outbound only: the agent's caller-id the call originates from. */
|
|
79
|
+
AgentNumber?: string;
|
|
80
|
+
/** Inbound only: the caller's number. */
|
|
81
|
+
CallerNumber?: string;
|
|
82
|
+
/** Inbound only: the carrier's id for the call being answered. */
|
|
83
|
+
InboundCallId?: string;
|
|
84
|
+
/** Carrier-specific extra keys (e.g. RingCentral's negotiated carrier sample rate). */
|
|
85
|
+
Extra?: Record<string, unknown>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Builds the bridge `Configuration` the telephony driver reads. For an inbound call the roster needs the agent's
|
|
89
|
+
* own number and the caller's number kept apart: the driver's `Address` is the agent's DID, and the caller's
|
|
90
|
+
* number rides in {@link CALLER_NUMBER_CONFIG_KEY}.
|
|
91
|
+
*/
|
|
92
|
+
export declare function BuildTelephonyCallConfiguration(input: CallConfigurationInput): Record<string, unknown>;
|
|
93
|
+
/**
|
|
94
|
+
* Runs the outbound gate for a call that already holds a capacity slot, giving the slot back when the gate
|
|
95
|
+
* refuses or throws — a refused call must not keep a line busy.
|
|
96
|
+
*/
|
|
97
|
+
export declare function AuthorizeOutboundCallOrRelease(request: OutboundCallRequest, guard: OutboundGuardDeps, lease: CallCapacityLease): Promise<OutboundAuthorization>;
|
|
98
|
+
/** Starts phone calls' agent sessions. One instance per carrier service. */
|
|
99
|
+
export declare class TelephonyCallSessionStarter {
|
|
100
|
+
private readonly deps;
|
|
101
|
+
constructor(deps: TelephonyCallStarterDeps);
|
|
102
|
+
/**
|
|
103
|
+
* Starts the call's agent session. On ANY failure the lease is released, the agent-session row closed and the
|
|
104
|
+
* model session closed before the error is rethrown, its co-agent run finalized as failed with the start's error;
|
|
105
|
+
* on success {@link TelephonyCallStartArgs.Lease} is released when the session ends.
|
|
106
|
+
*/
|
|
107
|
+
Start(args: TelephonyCallStartArgs): Promise<StartedTelephonyCall>;
|
|
108
|
+
private startWithSession;
|
|
109
|
+
/** What the agent is told about the call: phone etiquette, who is on the line, and the tools it has. */
|
|
110
|
+
private buildFraming;
|
|
111
|
+
/** The call-control tool executor; it reaches the call's bridge lazily because that exists only after start. */
|
|
112
|
+
private buildExecutor;
|
|
113
|
+
/** Installs the call-control executor on a model session's runtime so its tool calls reach the call. */
|
|
114
|
+
private bindLocalTools;
|
|
115
|
+
/** The caller talked over the agent: queued progress narration is stale. Delegated work is left running. */
|
|
116
|
+
private cancelPendingNarration;
|
|
117
|
+
/** The caller explicitly asked to cancel (`cancel_pending_work`): abort the delegated runs in flight. */
|
|
118
|
+
private cancelInFlightDelegations;
|
|
119
|
+
/** End-of-call bookkeeping: free the capacity slot, close the agent-session row, and close the interaction. Never throws. */
|
|
120
|
+
private onSessionEnded;
|
|
121
|
+
/** Moves the agent-session row to `Closed`. A row that is already closed (or missing) is left alone. */
|
|
122
|
+
private closeAgentSession;
|
|
123
|
+
/**
|
|
124
|
+
* Closes the model session of a call whose start failed (when one was opened), after finalizing its co-agent run as
|
|
125
|
+
* failed with the start's error: the close alone would finalize that run as completed. Neither step throws; a failure
|
|
126
|
+
* is logged, so the caller still sees why the start failed.
|
|
127
|
+
*/
|
|
128
|
+
private closeFailedStartSession;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Moves an agent-session row to `Closed`, recording why. A row that is already closed (or missing) is left alone. Never
|
|
132
|
+
* throws: closing is bookkeeping at the end of a call and must not turn a clean hang-up into an error.
|
|
133
|
+
*/
|
|
134
|
+
export declare function CloseAgentSessionRow(agentSessionID: string, reason: BridgeDisconnectReason, user: UserInfo, provider: IMetadataProvider): Promise<void>;
|
|
135
|
+
//# sourceMappingURL=telephonyCallSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"telephonyCallSession.d.ts","sourceRoot":"","sources":["../../src/telephony/telephonyCallSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,iBAAiB,EAAuB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAExF,OAAO,KAAK,EAA0B,6BAA6B,EAAE,wBAAwB,EAAuB,MAAM,+BAA+B,CAAC;AAC1J,OAAO,KAAK,EAAE,cAAc,EAA6D,MAAM,kCAAkC,CAAC;AAClI,OAAO,EAAE,2BAA2B,EAA+D,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAC/J,OAAO,EAKH,KAAK,sBAAsB,EAC3B,KAAK,sBAAsB,EAC9B,MAAM,gCAAgC,CAAC;AACxC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAAuB,KAAK,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AACxF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAE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iB;IAI/B;;;;OAIG;YACW,uBAAuB;CAexC;AAED;;;GAGG;AACH,wBAAsB,oBAAoB,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,sBAAsB,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAe7J"}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Starts ONE phone call's agent session, the same way for every carrier.
|
|
3
|
+
*
|
|
4
|
+
* Twilio, Vonage and RingCentral differ in how a call reaches MJ and how its media is carried, but once a call is
|
|
5
|
+
* admitted the work is identical: pick the co-agent that voices the dialled agent, create the agent session, open
|
|
6
|
+
* the realtime model session with the phone framing and call-control tools, start the bridge session on the bridge
|
|
7
|
+
* engine, and wire what happens next — barge-in, recovery from a dropped model session, the transcript, and
|
|
8
|
+
* cleanup. That used to be copied into each carrier's service (and had drifted); it lives here once.
|
|
9
|
+
*
|
|
10
|
+
* What the helper guarantees:
|
|
11
|
+
* - the **co-agent** is resolved through the shared chain (the browser path's), and the dialled agent is the
|
|
12
|
+
* TARGET it voices — the two are never collapsed into one id;
|
|
13
|
+
* - the call-capacity lease and the agent-session row are released/closed on EVERY path — a failed start, a
|
|
14
|
+
* normal hang-up, a model loss — never leaked;
|
|
15
|
+
* - the transcript lands in the call's OWN conversation, with the dialled agent attributed;
|
|
16
|
+
* - a talk-over drops queued spoken progress but leaves delegated work running (the caller cancels it explicitly);
|
|
17
|
+
* - a model session lost mid-call is re-opened once with the conversation so far (the engine owns the retry cap).
|
|
18
|
+
*
|
|
19
|
+
* @module @memberjunction/telephony-adapters
|
|
20
|
+
*/
|
|
21
|
+
import { LogError, LogStatus } from '@memberjunction/core';
|
|
22
|
+
import { CreateBridgeSessionTranscriptSink, GetBridgeRealtimeRuntime } from '@memberjunction/ai-agents';
|
|
23
|
+
import { CALLER_NUMBER_CONFIG_KEY, DIRECTION_CONFIG_KEY, FROM_NUMBER_CONFIG_KEY, INBOUND_CALL_ID_CONFIG_KEY, } from '@memberjunction/ai-bridge-base';
|
|
24
|
+
import { ResolveCallerSafely } from './callerIdentity.js';
|
|
25
|
+
import { AuthorizeOutboundCall, } from './outboundCallPolicy.js';
|
|
26
|
+
import { BuildPhoneFraming, BuildTelephonyTools, TelephonyCallToolExecutor } from './telephonyCallTools.js';
|
|
27
|
+
import { FindPhoneNumber } from './agentIdentityLookup.js';
|
|
28
|
+
import { InteractionLifecycleService } from './interactionLifecycle.js';
|
|
29
|
+
const AGENT_SESSION_ENTITY = 'MJ: AI Agent Sessions';
|
|
30
|
+
/**
|
|
31
|
+
* Builds the bridge `Configuration` the telephony driver reads. For an inbound call the roster needs the agent's
|
|
32
|
+
* own number and the caller's number kept apart: the driver's `Address` is the agent's DID, and the caller's
|
|
33
|
+
* number rides in {@link CALLER_NUMBER_CONFIG_KEY}.
|
|
34
|
+
*/
|
|
35
|
+
export function BuildTelephonyCallConfiguration(input) {
|
|
36
|
+
const config = { ...(input.Extra ?? {}), [DIRECTION_CONFIG_KEY]: input.Direction };
|
|
37
|
+
if (input.AgentNumber) {
|
|
38
|
+
config[FROM_NUMBER_CONFIG_KEY] = input.AgentNumber;
|
|
39
|
+
}
|
|
40
|
+
if (input.CallerNumber) {
|
|
41
|
+
config[CALLER_NUMBER_CONFIG_KEY] = input.CallerNumber;
|
|
42
|
+
}
|
|
43
|
+
if (input.InboundCallId) {
|
|
44
|
+
config[INBOUND_CALL_ID_CONFIG_KEY] = input.InboundCallId;
|
|
45
|
+
}
|
|
46
|
+
return config;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Runs the outbound gate for a call that already holds a capacity slot, giving the slot back when the gate
|
|
50
|
+
* refuses or throws — a refused call must not keep a line busy.
|
|
51
|
+
*/
|
|
52
|
+
export async function AuthorizeOutboundCallOrRelease(request, guard, lease) {
|
|
53
|
+
try {
|
|
54
|
+
const verdict = await AuthorizeOutboundCall(request, guard);
|
|
55
|
+
if (!verdict.Allowed) {
|
|
56
|
+
lease.Release();
|
|
57
|
+
}
|
|
58
|
+
return verdict;
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
lease.Release();
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Starts phone calls' agent sessions. One instance per carrier service. */
|
|
66
|
+
export class TelephonyCallSessionStarter {
|
|
67
|
+
constructor(deps) {
|
|
68
|
+
this.deps = deps;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Starts the call's agent session. On ANY failure the lease is released, the agent-session row closed and the
|
|
72
|
+
* model session closed before the error is rethrown, its co-agent run finalized as failed with the start's error;
|
|
73
|
+
* on success {@link TelephonyCallStartArgs.Lease} is released when the session ends.
|
|
74
|
+
*/
|
|
75
|
+
async Start(args) {
|
|
76
|
+
let agentSession;
|
|
77
|
+
let interaction = null;
|
|
78
|
+
const ref = {};
|
|
79
|
+
const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
|
|
80
|
+
try {
|
|
81
|
+
await this.deps.Engine.Config(false, args.ContextUser, args.MetadataProvider);
|
|
82
|
+
const carrier = args.ResolveProvider();
|
|
83
|
+
const coAgentID = await this.deps.CoAgentResolver(args.Identity.AgentID, undefined, args.ContextUser, args.MetadataProvider);
|
|
84
|
+
agentSession = await this.deps.SessionManager.CreateSession({ agentID: coAgentID, userID: args.ContextUser.ID }, args.ContextUser, args.MetadataProvider);
|
|
85
|
+
const phone = await FindPhoneNumber(args.Direction === 'Inbound' ? args.Identity.IdentityValue : (args.Configuration[FROM_NUMBER_CONFIG_KEY] ?? ''), carrier.ID, args.ContextUser, args.MetadataProvider);
|
|
86
|
+
interaction = await lifecycle.CreateInteraction({
|
|
87
|
+
Channel: 'Phone',
|
|
88
|
+
Direction: args.Direction,
|
|
89
|
+
PhoneNumberID: phone?.ID ?? null,
|
|
90
|
+
RemoteAddress: args.RemoteNumber,
|
|
91
|
+
ExternalID: args.Configuration[INBOUND_CALL_ID_CONFIG_KEY] ?? null,
|
|
92
|
+
AgentSessionID: agentSession.ID,
|
|
93
|
+
RoomName: args.RemoteNumber,
|
|
94
|
+
Status: 'Active',
|
|
95
|
+
StartedAt: new Date(),
|
|
96
|
+
ContextUser: args.ContextUser,
|
|
97
|
+
MetadataProvider: args.MetadataProvider,
|
|
98
|
+
});
|
|
99
|
+
return await this.startWithSession(args, carrier, coAgentID, agentSession, ref, interaction?.ID);
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
args.Lease.Release();
|
|
103
|
+
await this.closeFailedStartSession(ref.Initial, e);
|
|
104
|
+
if (agentSession) {
|
|
105
|
+
await this.closeAgentSession(agentSession.ID, 'Error', args.ContextUser, args.MetadataProvider);
|
|
106
|
+
}
|
|
107
|
+
if (interaction) {
|
|
108
|
+
await lifecycle.CloseInteraction({
|
|
109
|
+
InteractionID: interaction.ID,
|
|
110
|
+
EndReason: 'Error',
|
|
111
|
+
ContextUser: args.ContextUser,
|
|
112
|
+
MetadataProvider: args.MetadataProvider,
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async startWithSession(args, carrier, coAgentID, agentSession, ref, interactionID) {
|
|
119
|
+
const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
|
|
120
|
+
const features = readToolFeatures(carrier);
|
|
121
|
+
const executor = this.buildExecutor(args, features, ref);
|
|
122
|
+
const framing = await this.buildFraming(args, features, interactionID);
|
|
123
|
+
const tools = BuildTelephonyTools(features, this.deps.TransferTargets ?? []);
|
|
124
|
+
const openModelSession = async (priorTranscript) => {
|
|
125
|
+
const session = await this.deps.SessionFactory({
|
|
126
|
+
// The caller sees no video: the session asks the model for no avatar and reports `phone`.
|
|
127
|
+
PhoneCall: true,
|
|
128
|
+
AgentID: coAgentID,
|
|
129
|
+
TargetAgentID: args.Identity.AgentID,
|
|
130
|
+
ContextUser: args.ContextUser,
|
|
131
|
+
MetadataProvider: args.MetadataProvider,
|
|
132
|
+
AgentSessionID: agentSession.ID,
|
|
133
|
+
ConversationID: agentSession.ConversationID ?? undefined,
|
|
134
|
+
RoomName: args.RemoteNumber,
|
|
135
|
+
HostTools: tools,
|
|
136
|
+
HostFraming: framing,
|
|
137
|
+
PriorTranscript: priorTranscript,
|
|
138
|
+
});
|
|
139
|
+
this.bindLocalTools(session, executor);
|
|
140
|
+
return session;
|
|
141
|
+
};
|
|
142
|
+
ref.Initial = await openModelSession();
|
|
143
|
+
const active = await this.deps.Engine.StartBridgeSession({
|
|
144
|
+
AgentSessionID: agentSession.ID,
|
|
145
|
+
AgentID: coAgentID,
|
|
146
|
+
TargetAgentID: args.Identity.AgentID,
|
|
147
|
+
Provider: carrier,
|
|
148
|
+
RealtimeSession: ref.Initial,
|
|
149
|
+
// The agent's own number, always: for an inbound call the caller's number rides in the configuration.
|
|
150
|
+
Address: args.Direction === 'Inbound' ? args.Identity.IdentityValue : args.RemoteNumber,
|
|
151
|
+
Direction: args.Direction,
|
|
152
|
+
// A call is a 1:1 conversation the agent always answers: not a "passive" meeting seat.
|
|
153
|
+
JoinMethod: args.Direction === 'Inbound' ? 'InboundRoute' : 'OnDemand',
|
|
154
|
+
TurnMode: 'Active',
|
|
155
|
+
Configuration: args.Configuration,
|
|
156
|
+
BindSdk: args.BindSdk,
|
|
157
|
+
ContextUser: args.ContextUser,
|
|
158
|
+
MetadataProvider: args.MetadataProvider,
|
|
159
|
+
TranscriptSink: agentSession.ConversationID
|
|
160
|
+
? CreateBridgeSessionTranscriptSink({ ConversationID: agentSession.ConversationID, AgentSessionID: agentSession.ID, AgentID: args.Identity.AgentID })
|
|
161
|
+
: undefined,
|
|
162
|
+
// HOST POLICY (deliberate, same as the browser): barge-in drops queued progress narration but does NOT
|
|
163
|
+
// abort delegated runs — the caller keeps talking while work runs, and full-duplex models make
|
|
164
|
+
// backchannels ("mm-hm") common, so cancel-on-speech would kill exactly the jobs they asked for. See
|
|
165
|
+
// packages/RealtimeRuntime/src/session/RealtimeSessionRuntime.ts ~L1956-1963. The phone has no per-job
|
|
166
|
+
// cancel button, so explicit cancellation is the `cancel_pending_work` tool.
|
|
167
|
+
OnBargeIn: () => this.cancelPendingNarration(ref),
|
|
168
|
+
RecoverRealtimeSession: (request) => openModelSession(request.PriorTranscript),
|
|
169
|
+
OnSessionEnded: (reason) => this.onSessionEnded(args, agentSession.ID, reason, interactionID),
|
|
170
|
+
});
|
|
171
|
+
ref.Active = active;
|
|
172
|
+
if (active.RoomKey && interactionID) {
|
|
173
|
+
await lifecycle.UpdateExternalID(interactionID, active.RoomKey, args.ContextUser, args.MetadataProvider);
|
|
174
|
+
}
|
|
175
|
+
LogStatus(`[Telephony] ${args.Direction} call session ${active.SessionBridgeID} started (agent session ${agentSession.ID}).`);
|
|
176
|
+
return { RoomKey: active.RoomKey, SessionBridgeID: active.SessionBridgeID };
|
|
177
|
+
}
|
|
178
|
+
/** What the agent is told about the call: phone etiquette, who is on the line, and the tools it has. */
|
|
179
|
+
async buildFraming(args, features, interactionID) {
|
|
180
|
+
const caller = args.Direction === 'Inbound'
|
|
181
|
+
? await ResolveCallerSafely(this.deps.CallerResolver, args.RemoteNumber, args.Identity.IdentityValue, args.ContextUser)
|
|
182
|
+
: undefined;
|
|
183
|
+
if (caller && interactionID) {
|
|
184
|
+
const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
|
|
185
|
+
if (caller.LinkedRecord) {
|
|
186
|
+
void lifecycle.CreateLink({
|
|
187
|
+
InteractionID: interactionID,
|
|
188
|
+
EntityID: caller.LinkedRecord.EntityID,
|
|
189
|
+
RecordID: caller.LinkedRecord.RecordID,
|
|
190
|
+
Role: 'Caller',
|
|
191
|
+
ContextUser: args.ContextUser,
|
|
192
|
+
MetadataProvider: args.MetadataProvider,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
else if (caller.PersonRecordID && caller.PersonEntityID) {
|
|
196
|
+
void lifecycle.CreateLink({
|
|
197
|
+
InteractionID: interactionID,
|
|
198
|
+
EntityID: caller.PersonEntityID,
|
|
199
|
+
RecordID: caller.PersonRecordID,
|
|
200
|
+
Role: 'Caller',
|
|
201
|
+
ContextUser: args.ContextUser,
|
|
202
|
+
MetadataProvider: args.MetadataProvider,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return BuildPhoneFraming({
|
|
207
|
+
Direction: args.Direction,
|
|
208
|
+
RemoteNumber: args.RemoteNumber,
|
|
209
|
+
Caller: caller,
|
|
210
|
+
Features: features,
|
|
211
|
+
TransferTargets: this.deps.TransferTargets ?? [],
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/** The call-control tool executor; it reaches the call's bridge lazily because that exists only after start. */
|
|
215
|
+
buildExecutor(args, features, ref) {
|
|
216
|
+
return new TelephonyCallToolExecutor({
|
|
217
|
+
Controls: () => ref.Active?.Bridge,
|
|
218
|
+
Policy: this.deps.OutboundPolicy,
|
|
219
|
+
Features: features,
|
|
220
|
+
TransferTargets: this.deps.TransferTargets ?? [],
|
|
221
|
+
CancelPendingWork: () => this.cancelInFlightDelegations(ref),
|
|
222
|
+
EndCall: async (reason) => {
|
|
223
|
+
if (ref.Active) {
|
|
224
|
+
LogStatus(`[Telephony] agent ended call ${ref.Active.SessionBridgeID}: ${reason}`);
|
|
225
|
+
await this.deps.Engine.StopBridgeSession(ref.Active.SessionBridgeID, 'Explicit', args.ContextUser, args.MetadataProvider);
|
|
226
|
+
}
|
|
227
|
+
},
|
|
228
|
+
NotifyModel: (note) => ref.Active?.RealtimeSession.SendContextNote?.(note),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
/** Installs the call-control executor on a model session's runtime so its tool calls reach the call. */
|
|
232
|
+
bindLocalTools(session, executor) {
|
|
233
|
+
const runtime = GetBridgeRealtimeRuntime(session);
|
|
234
|
+
if (!runtime) {
|
|
235
|
+
LogError('[Telephony] the model session has no bridge runtime; transfer_call / send_dtmf / end_call will not work on this call.');
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
runtime.SetLocalToolHandler(executor);
|
|
239
|
+
}
|
|
240
|
+
/** The caller talked over the agent: queued progress narration is stale. Delegated work is left running. */
|
|
241
|
+
cancelPendingNarration(ref) {
|
|
242
|
+
const session = ref.Active?.RealtimeSession ?? ref.Initial;
|
|
243
|
+
if (session) {
|
|
244
|
+
GetBridgeRealtimeRuntime(session)?.CancelPendingNarration();
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** The caller explicitly asked to cancel (`cancel_pending_work`): abort the delegated runs in flight. */
|
|
248
|
+
cancelInFlightDelegations(ref) {
|
|
249
|
+
const session = ref.Active?.RealtimeSession ?? ref.Initial;
|
|
250
|
+
return session ? GetBridgeRealtimeRuntime(session)?.CancelInFlightDelegations() ?? 0 : 0;
|
|
251
|
+
}
|
|
252
|
+
/** End-of-call bookkeeping: free the capacity slot, close the agent-session row, and close the interaction. Never throws. */
|
|
253
|
+
async onSessionEnded(args, agentSessionID, reason, interactionID) {
|
|
254
|
+
args.Lease.Release();
|
|
255
|
+
await this.closeAgentSession(agentSessionID, reason, args.ContextUser, args.MetadataProvider);
|
|
256
|
+
if (interactionID) {
|
|
257
|
+
const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
|
|
258
|
+
await lifecycle.CloseInteraction({
|
|
259
|
+
InteractionID: interactionID,
|
|
260
|
+
EndReason: reason,
|
|
261
|
+
CostPerMinute: this.deps.CostPerMinute,
|
|
262
|
+
ContextUser: args.ContextUser,
|
|
263
|
+
MetadataProvider: args.MetadataProvider,
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/** Moves the agent-session row to `Closed`. A row that is already closed (or missing) is left alone. */
|
|
268
|
+
async closeAgentSession(agentSessionID, reason, user, provider) {
|
|
269
|
+
await CloseAgentSessionRow(agentSessionID, reason, user, provider);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Closes the model session of a call whose start failed (when one was opened), after finalizing its co-agent run as
|
|
273
|
+
* failed with the start's error: the close alone would finalize that run as completed. Neither step throws; a failure
|
|
274
|
+
* is logged, so the caller still sees why the start failed.
|
|
275
|
+
*/
|
|
276
|
+
async closeFailedStartSession(session, startError) {
|
|
277
|
+
if (!session) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
try {
|
|
281
|
+
await GetBridgeRealtimeRuntime(session)?.Finalize(false, startError instanceof Error ? startError.message : String(startError));
|
|
282
|
+
}
|
|
283
|
+
catch (e) {
|
|
284
|
+
LogError(`[Telephony] recording a failed start on the call's agent run failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
await session.Close();
|
|
288
|
+
}
|
|
289
|
+
catch (e) {
|
|
290
|
+
LogError(`[Telephony] closing the model session after a failed start failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Moves an agent-session row to `Closed`, recording why. A row that is already closed (or missing) is left alone. Never
|
|
296
|
+
* throws: closing is bookkeeping at the end of a call and must not turn a clean hang-up into an error.
|
|
297
|
+
*/
|
|
298
|
+
export async function CloseAgentSessionRow(agentSessionID, reason, user, provider) {
|
|
299
|
+
try {
|
|
300
|
+
const row = await provider.GetEntityObject(AGENT_SESSION_ENTITY, user);
|
|
301
|
+
if (!(await row.Load(agentSessionID)) || row.Status === 'Closed') {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
row.Status = 'Closed';
|
|
305
|
+
row.ClosedAt = new Date();
|
|
306
|
+
row.CloseReason = mapCloseReason(reason);
|
|
307
|
+
if (!(await row.Save())) {
|
|
308
|
+
LogError(`[Telephony] could not close agent session ${agentSessionID}: ${row.LatestResult?.CompleteMessage ?? 'unknown error'}`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
catch (e) {
|
|
312
|
+
LogError(`[Telephony] closing agent session ${agentSessionID} failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/** Reads which call-control features the provider declares; anything undeclared is unsupported. */
|
|
316
|
+
function readToolFeatures(provider) {
|
|
317
|
+
const features = provider.SupportedFeaturesObject;
|
|
318
|
+
return { CallTransfer: features?.CallTransfer === true, DTMF: features?.DTMF === true };
|
|
319
|
+
}
|
|
320
|
+
/** Maps the bridge's disconnect reason onto the agent-session row's narrower close-reason set. */
|
|
321
|
+
function mapCloseReason(reason) {
|
|
322
|
+
switch (reason) {
|
|
323
|
+
case 'Error':
|
|
324
|
+
case 'Janitor':
|
|
325
|
+
case 'Shutdown':
|
|
326
|
+
return reason;
|
|
327
|
+
default:
|
|
328
|
+
return 'Explicit';
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
//# sourceMappingURL=telephonyCallSession.js.map
|