@memberjunction/telephony-adapters 6.2.0-edge.1 → 6.2.0-edge.3
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 +130 -0
- package/dist/telephony/LiveKitSipTelephonyService.d.ts.map +1 -0
- package/dist/telephony/LiveKitSipTelephonyService.js +453 -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/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 +100 -0
- package/dist/telephony/interactionLifecycle.d.ts.map +1 -0
- package/dist/telephony/interactionLifecycle.js +338 -0
- package/dist/telephony/interactionLifecycle.js.map +1 -0
- package/dist/telephony/livekit-sip-runtime.d.ts +12 -0
- package/dist/telephony/livekit-sip-runtime.d.ts.map +1 -0
- package/dist/telephony/livekit-sip-runtime.js +21 -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 +359 -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 +130 -0
- package/dist/telephony/telephonyCallSession.d.ts.map +1 -0
- package/dist/telephony/telephonyCallSession.js +315 -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,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview A transparent {@link ITelephonyCallSdk} decorator that reports when a call has ended.
|
|
3
|
+
*
|
|
4
|
+
* The bridge engine ends a phone session through exactly two seams of the SDK it was handed: the SDK's own
|
|
5
|
+
* `onCallEnded` callback (the remote party hung up / the carrier dropped it) and `hangup()` (MJ is tearing
|
|
6
|
+
* the session down — explicit stop, status callback, max duration, janitor, shutdown). Wrapping the SDK lets
|
|
7
|
+
* the telephony services learn about **every** end of a call from one place — without taking over the
|
|
8
|
+
* driver's single, "latest handler wins" `onCallEnded` slot (which the engine owns) — so they can release
|
|
9
|
+
* per-call state such as the max-duration timer deterministically.
|
|
10
|
+
*
|
|
11
|
+
* Pure delegation otherwise: every call is forwarded unchanged.
|
|
12
|
+
*
|
|
13
|
+
* @module @memberjunction/telephony-adapters
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Wraps an SDK so `onEnded(callId)` fires when the call ends by either route. `onEnded` may fire more than
|
|
17
|
+
* once for one call (the carrier reports the end, then MJ hangs up); it must be idempotent.
|
|
18
|
+
*/
|
|
19
|
+
export class CallEndObserverSdk {
|
|
20
|
+
constructor(inner, onEnded) {
|
|
21
|
+
this.inner = inner;
|
|
22
|
+
this.onEnded = onEnded;
|
|
23
|
+
/** The call id once known (set by `dial` / `answer`); `hangup` also receives it explicitly. */
|
|
24
|
+
this.callId = null;
|
|
25
|
+
// The call is no longer MJ's once it has been handed to the carrier or said goodbye to, so these report
|
|
26
|
+
// the end just as a hang-up does (releasing the per-call timer).
|
|
27
|
+
const innerDetach = inner.detach?.bind(inner);
|
|
28
|
+
if (innerDetach) {
|
|
29
|
+
this.detach = async (callId) => {
|
|
30
|
+
try {
|
|
31
|
+
await innerDetach(callId);
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
this.onEnded(callId);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const innerGoodbye = inner.playMessageAndHangup?.bind(inner);
|
|
39
|
+
if (innerGoodbye) {
|
|
40
|
+
this.playMessageAndHangup = async (callId, message) => {
|
|
41
|
+
try {
|
|
42
|
+
await innerGoodbye(callId, message);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
this.onEnded(callId);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async dial(toNumber, fromNumber, args) {
|
|
51
|
+
const callId = await this.inner.dial(toNumber, fromNumber, args);
|
|
52
|
+
this.callId = callId;
|
|
53
|
+
return callId;
|
|
54
|
+
}
|
|
55
|
+
async answer(callId) {
|
|
56
|
+
this.callId = callId;
|
|
57
|
+
await this.inner.answer(callId);
|
|
58
|
+
}
|
|
59
|
+
async hangup(callId) {
|
|
60
|
+
try {
|
|
61
|
+
await this.inner.hangup(callId);
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
// Released even when the carrier REST hang-up throws: the session is being torn down regardless.
|
|
65
|
+
this.onEnded(callId);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
sendAudioFrame(pcm) {
|
|
69
|
+
this.inner.sendAudioFrame(pcm);
|
|
70
|
+
}
|
|
71
|
+
onAudioFrame(cb) {
|
|
72
|
+
this.inner.onAudioFrame(cb);
|
|
73
|
+
}
|
|
74
|
+
sendDtmf(digits) {
|
|
75
|
+
return this.inner.sendDtmf(digits);
|
|
76
|
+
}
|
|
77
|
+
onDtmf(cb) {
|
|
78
|
+
this.inner.onDtmf(cb);
|
|
79
|
+
}
|
|
80
|
+
transfer(callId, toNumber) {
|
|
81
|
+
return this.inner.transfer(callId, toNumber);
|
|
82
|
+
}
|
|
83
|
+
onCallEnded(cb) {
|
|
84
|
+
this.inner.onCallEnded(() => {
|
|
85
|
+
if (this.callId) {
|
|
86
|
+
this.onEnded(this.callId);
|
|
87
|
+
}
|
|
88
|
+
cb();
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
flushOutbound() {
|
|
92
|
+
this.inner.flushOutbound?.();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=callEndObserver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callEndObserver.js","sourceRoot":"","sources":["../../src/telephony/callEndObserver.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAIH;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAgB3B,YACqB,KAAwB,EACxB,OAAiC;QADjC,UAAK,GAAL,KAAK,CAAmB;QACxB,YAAO,GAAP,OAAO,CAA0B;QAjBtD,+FAA+F;QACvF,WAAM,GAAkB,IAAI,CAAC;QAkBjC,wGAAwG;QACxG,iEAAiE;QACjE,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;QAC9C,IAAI,WAAW,EAAE,CAAC;YACd,IAAI,CAA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AC;IACL,CAAC;IAEM,cAAc,CAAC,GAAgB;QAClC,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAEM,YAAY,CAAC,EAA8B;QAC9C,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;IAChC,CAAC;IAEM,QAAQ,CAAC,MAAc;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;IAEM,MAAM,CAAC,EAA4B;QACtC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC;IAEM,QAAQ,CAAC,MAAc,EAAE,QAAgB;QAC5C,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACjD,CAAC;IAEM,WAAW,CAAC,EAAc;QAC7B,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,GAAG,EAAE;YACxB,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;gBACd,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YAC9B,CAAC;YACD,EAAE,EAAE,CAAC;QACT,CAAC,CAAC,CAAC;IACP,CAAC;IAEM,aAAa;QAChB,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE,EAAE,CAAC;IACjC,CAAC;CACJ"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Tracks live telephony bridge sessions by carrier call id so they can be ended from outside
|
|
3
|
+
* the media path, and caps how long any one call may last.
|
|
4
|
+
*
|
|
5
|
+
* Three things end a phone session without the media socket cooperating, and all of them need the same
|
|
6
|
+
* plumbing — "find the bridge session for this call id and stop it":
|
|
7
|
+
*
|
|
8
|
+
* - the carrier's **status callback** reports a terminal state (busy / no-answer / failed / canceled /
|
|
9
|
+
* completed) — an unanswered outbound call never opens a media socket, so nothing else would end it;
|
|
10
|
+
* - **answering-machine detection** decides the callee is a machine and the operator wants a hang-up;
|
|
11
|
+
* - the **maximum call length** elapses (`telephony.maxCallSeconds`).
|
|
12
|
+
*
|
|
13
|
+
* The engine only knows sessions by their bridge-row id, and a call id is unknown until the carrier
|
|
14
|
+
* responds, so this tracker is the call-id → session map. It also closes a race: a status callback can
|
|
15
|
+
* arrive while the session is still starting (the answer webhooks no longer wait for it), so a request to
|
|
16
|
+
* end an in-flight call is remembered and honoured the moment the session attaches.
|
|
17
|
+
*
|
|
18
|
+
* Every timer it creates is cleared on every exit path ({@link Release}, {@link Fail}, expiry, a stop
|
|
19
|
+
* request) and is `unref`'d, so it can neither leak nor hold the process open.
|
|
20
|
+
*
|
|
21
|
+
* @module @memberjunction/telephony-adapters
|
|
22
|
+
*/
|
|
23
|
+
import { IMetadataProvider, UserInfo } from '@memberjunction/core';
|
|
24
|
+
import type { BridgeDisconnectReason } from '@memberjunction/ai-bridge-base';
|
|
25
|
+
/** Default maximum length of one phone call, in seconds. */
|
|
26
|
+
export declare const DEFAULT_MAX_CALL_SECONDS = 1800;
|
|
27
|
+
/** The pieces of a live bridge session needed to stop it. */
|
|
28
|
+
export interface TrackedSession {
|
|
29
|
+
/** The `MJ: AI Agent Session Bridges` row id the engine knows the session by. */
|
|
30
|
+
SessionBridgeID: string;
|
|
31
|
+
/** The user the session runs as (the engine writes the teardown rows as them). */
|
|
32
|
+
ContextUser: UserInfo;
|
|
33
|
+
/** The metadata provider the session runs under. */
|
|
34
|
+
Provider: IMetadataProvider;
|
|
35
|
+
}
|
|
36
|
+
/** Stops a bridge session — in production `AIBridgeEngine.StopBridgeSession`. */
|
|
37
|
+
export type StopSessionFn = (session: TrackedSession, reason: BridgeDisconnectReason) => Promise<boolean>;
|
|
38
|
+
/** Why an outside party is asking for a call to end (log text only). */
|
|
39
|
+
export type CallEndRequestReason = 'carrier-status' | 'answering-machine' | 'max-duration' | 'media-connect-timeout';
|
|
40
|
+
/**
|
|
41
|
+
* Maps carrier call ids to their live bridge session, ends calls on request, and enforces the call cap.
|
|
42
|
+
*/
|
|
43
|
+
export declare class CallLifecycleTracker {
|
|
44
|
+
private readonly stopSession;
|
|
45
|
+
private readonly calls;
|
|
46
|
+
private readonly maxCallMs;
|
|
47
|
+
/**
|
|
48
|
+
* @param stopSession How to stop a session (inject a fake in tests).
|
|
49
|
+
* @param maxCallSeconds Maximum call length; a non-positive or non-finite value uses the default.
|
|
50
|
+
*/
|
|
51
|
+
constructor(stopSession: StopSessionFn, maxCallSeconds?: number);
|
|
52
|
+
/**
|
|
53
|
+
* Marks a call as starting (its bridge session is being created) and starts the start-deadline timer.
|
|
54
|
+
* Idempotent. Registering the call BEFORE the session exists is what lets a terminal carrier status that
|
|
55
|
+
* races the session start be remembered rather than dropped.
|
|
56
|
+
*/
|
|
57
|
+
Begin(callKey: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Attaches the live session to its call and arms the max-duration timer. If an end was requested while the
|
|
60
|
+
* session was still starting, the session is stopped immediately instead.
|
|
61
|
+
*/
|
|
62
|
+
Attach(callKey: string, session: TrackedSession): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Asks for a call to end (carrier status, machine detection, connect timeout). A live session is stopped
|
|
65
|
+
* now; a still-starting one is stopped as soon as it attaches; an unknown call is ignored (it already ended).
|
|
66
|
+
*
|
|
67
|
+
* @returns `true` when a live or starting call was found.
|
|
68
|
+
*/
|
|
69
|
+
RequestEnd(callKey: string, why: CallEndRequestReason, stopReason?: BridgeDisconnectReason): Promise<boolean>;
|
|
70
|
+
/** The session ended by some other route (hang-up, remote end): forget the call and cancel its timer. */
|
|
71
|
+
Release(callKey: string): void;
|
|
72
|
+
/** The session failed to start: forget the call and cancel its timer. */
|
|
73
|
+
Fail(callKey: string): void;
|
|
74
|
+
/** Whether a call is currently tracked (any state). */
|
|
75
|
+
IsTracking(callKey: string): boolean;
|
|
76
|
+
/** Cancels every timer and forgets every call (service shutdown). */
|
|
77
|
+
Dispose(): void;
|
|
78
|
+
/** Arms the one-shot timer that ends the call at the cap. Always cleared via {@link Release}. */
|
|
79
|
+
private armMaxDurationTimer;
|
|
80
|
+
/** Stops the session and forgets the call. The timer is cleared first so it cannot fire again. */
|
|
81
|
+
private stopAndForget;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=callLifecycleTracker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callLifecycleTracker.d.ts","sourceRoot":"","sources":["../../src/telephony/callLifecycleTracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,iBAAiB,EAAuB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACxF,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAE7E,4DAA4D;AAC5D,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAQ7C,6DAA6D;AAC7D,MAAM,WAAW,cAAc;IAC3B,iFAAiF;IACjF,eAAe,EAAE,MAAM,CAAC;IACxB,kFAAkF;IAClF,WAAW,EAAE,QAAQ,CAAC;IACtB,oDAAoD;IACpD,QAAQ,EAAE,iBAAiB,CAAC;CAC/B;AAED,iFAAiF;AACjF,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,sBAAsB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE1G,wEAAwE;AACxE,MAAM,MAAM,oBAAoB,GAAG,gBAAgB,GAAG,mBAAmB,GAAG,cAAc,GAAG,uBAAuB,CAAC;AAarH;;GAEG;AACH,qBAAa,oBAAoB;IASzB,OAAO,CAAC,QAAQ,CAAC,WAAW;IARhC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAkC;IACxD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IAEnC;;;OAGG;gBAEkB,WAAW,EAAE,aAAa,EAC3C,cAAc,GAAE,MAAiC;IAKrD;;;;OAIG;IACI,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAgBnC;;;OAGG;IACU,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAgB5E;;;;;OAKG;IACU,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,oBAAoB,EAAE,UAAU,GAAE,sBAAoC,GAAG,OAAO,CAAC,OAAO,CAAC;IAevI,yGAAyG;IAClG,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAQrC,yEAAyE;IAClE,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAIlC,uDAAuD;IAChD,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAI3C,qEAAqE;IAC9D,OAAO,IAAI,IAAI;IAWtB,iGAAiG;IACjG,OAAO,CAAC,mBAAmB;IAU3B,kGAAkG;YACpF,aAAa;CAe9B"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Tracks live telephony bridge sessions by carrier call id so they can be ended from outside
|
|
3
|
+
* the media path, and caps how long any one call may last.
|
|
4
|
+
*
|
|
5
|
+
* Three things end a phone session without the media socket cooperating, and all of them need the same
|
|
6
|
+
* plumbing — "find the bridge session for this call id and stop it":
|
|
7
|
+
*
|
|
8
|
+
* - the carrier's **status callback** reports a terminal state (busy / no-answer / failed / canceled /
|
|
9
|
+
* completed) — an unanswered outbound call never opens a media socket, so nothing else would end it;
|
|
10
|
+
* - **answering-machine detection** decides the callee is a machine and the operator wants a hang-up;
|
|
11
|
+
* - the **maximum call length** elapses (`telephony.maxCallSeconds`).
|
|
12
|
+
*
|
|
13
|
+
* The engine only knows sessions by their bridge-row id, and a call id is unknown until the carrier
|
|
14
|
+
* responds, so this tracker is the call-id → session map. It also closes a race: a status callback can
|
|
15
|
+
* arrive while the session is still starting (the answer webhooks no longer wait for it), so a request to
|
|
16
|
+
* end an in-flight call is remembered and honoured the moment the session attaches.
|
|
17
|
+
*
|
|
18
|
+
* Every timer it creates is cleared on every exit path ({@link Release}, {@link Fail}, expiry, a stop
|
|
19
|
+
* request) and is `unref`'d, so it can neither leak nor hold the process open.
|
|
20
|
+
*
|
|
21
|
+
* @module @memberjunction/telephony-adapters
|
|
22
|
+
*/
|
|
23
|
+
import { LogError, LogStatus } from '@memberjunction/core';
|
|
24
|
+
/** Default maximum length of one phone call, in seconds. */
|
|
25
|
+
export const DEFAULT_MAX_CALL_SECONDS = 1800;
|
|
26
|
+
/**
|
|
27
|
+
* How long a call may sit in the "starting" state (session being created) before the tracker forgets it. A
|
|
28
|
+
* start that never attaches (it failed, or the process lost it) must not leave an entry behind forever.
|
|
29
|
+
*/
|
|
30
|
+
const START_DEADLINE_MS = 60_000;
|
|
31
|
+
/**
|
|
32
|
+
* Maps carrier call ids to their live bridge session, ends calls on request, and enforces the call cap.
|
|
33
|
+
*/
|
|
34
|
+
export class CallLifecycleTracker {
|
|
35
|
+
/**
|
|
36
|
+
* @param stopSession How to stop a session (inject a fake in tests).
|
|
37
|
+
* @param maxCallSeconds Maximum call length; a non-positive or non-finite value uses the default.
|
|
38
|
+
*/
|
|
39
|
+
constructor(stopSession, maxCallSeconds = DEFAULT_MAX_CALL_SECONDS) {
|
|
40
|
+
this.stopSession = stopSession;
|
|
41
|
+
this.calls = new Map();
|
|
42
|
+
this.maxCallMs = (Number.isFinite(maxCallSeconds) && maxCallSeconds > 0 ? maxCallSeconds : DEFAULT_MAX_CALL_SECONDS) * 1000;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Marks a call as starting (its bridge session is being created) and starts the start-deadline timer.
|
|
46
|
+
* Idempotent. Registering the call BEFORE the session exists is what lets a terminal carrier status that
|
|
47
|
+
* races the session start be remembered rather than dropped.
|
|
48
|
+
*/
|
|
49
|
+
Begin(callKey) {
|
|
50
|
+
if (this.calls.has(callKey)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const call = { State: 'starting' };
|
|
54
|
+
call.Timer = setTimeout(() => {
|
|
55
|
+
call.Timer = undefined;
|
|
56
|
+
if (call.State !== 'live') {
|
|
57
|
+
LogError(`[Telephony] call ${callKey} never produced a live session within ${START_DEADLINE_MS / 1000}s; forgetting it.`);
|
|
58
|
+
this.calls.delete(callKey);
|
|
59
|
+
}
|
|
60
|
+
}, START_DEADLINE_MS);
|
|
61
|
+
call.Timer.unref?.();
|
|
62
|
+
this.calls.set(callKey, call);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Attaches the live session to its call and arms the max-duration timer. If an end was requested while the
|
|
66
|
+
* session was still starting, the session is stopped immediately instead.
|
|
67
|
+
*/
|
|
68
|
+
async Attach(callKey, session) {
|
|
69
|
+
const call = this.calls.get(callKey) ?? { State: 'starting' };
|
|
70
|
+
this.calls.set(callKey, call);
|
|
71
|
+
if (call.Timer) {
|
|
72
|
+
clearTimeout(call.Timer); // the start-deadline timer — superseded by the max-duration timer below
|
|
73
|
+
call.Timer = undefined;
|
|
74
|
+
}
|
|
75
|
+
call.Session = session;
|
|
76
|
+
if (call.State === 'end-requested') {
|
|
77
|
+
await this.stopAndForget(callKey, call, call.PendingStopReason ?? 'HostEnded');
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
call.State = 'live';
|
|
81
|
+
this.armMaxDurationTimer(callKey, call);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Asks for a call to end (carrier status, machine detection, connect timeout). A live session is stopped
|
|
85
|
+
* now; a still-starting one is stopped as soon as it attaches; an unknown call is ignored (it already ended).
|
|
86
|
+
*
|
|
87
|
+
* @returns `true` when a live or starting call was found.
|
|
88
|
+
*/
|
|
89
|
+
async RequestEnd(callKey, why, stopReason = 'HostEnded') {
|
|
90
|
+
const call = this.calls.get(callKey);
|
|
91
|
+
if (!call || call.State === 'end-requested') {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
LogStatus(`[Telephony] ending call ${callKey} (${why}).`);
|
|
95
|
+
if (call.State === 'starting') {
|
|
96
|
+
call.State = 'end-requested';
|
|
97
|
+
call.PendingStopReason = stopReason;
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
await this.stopAndForget(callKey, call, stopReason);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
/** The session ended by some other route (hang-up, remote end): forget the call and cancel its timer. */
|
|
104
|
+
Release(callKey) {
|
|
105
|
+
const call = this.calls.get(callKey);
|
|
106
|
+
if (call?.Timer) {
|
|
107
|
+
clearTimeout(call.Timer);
|
|
108
|
+
}
|
|
109
|
+
this.calls.delete(callKey);
|
|
110
|
+
}
|
|
111
|
+
/** The session failed to start: forget the call and cancel its timer. */
|
|
112
|
+
Fail(callKey) {
|
|
113
|
+
this.Release(callKey);
|
|
114
|
+
}
|
|
115
|
+
/** Whether a call is currently tracked (any state). */
|
|
116
|
+
IsTracking(callKey) {
|
|
117
|
+
return this.calls.has(callKey);
|
|
118
|
+
}
|
|
119
|
+
/** Cancels every timer and forgets every call (service shutdown). */
|
|
120
|
+
Dispose() {
|
|
121
|
+
for (const call of this.calls.values()) {
|
|
122
|
+
if (call.Timer) {
|
|
123
|
+
clearTimeout(call.Timer);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
this.calls.clear();
|
|
127
|
+
}
|
|
128
|
+
// ── internals ────────────────────────────────────────────────────────────────
|
|
129
|
+
/** Arms the one-shot timer that ends the call at the cap. Always cleared via {@link Release}. */
|
|
130
|
+
armMaxDurationTimer(callKey, call) {
|
|
131
|
+
call.Timer = setTimeout(() => {
|
|
132
|
+
call.Timer = undefined;
|
|
133
|
+
void this.RequestEnd(callKey, 'max-duration').catch((err) => LogError(`[Telephony] max-duration stop failed for call ${callKey}: ${err instanceof Error ? err.message : String(err)}`));
|
|
134
|
+
}, this.maxCallMs);
|
|
135
|
+
call.Timer.unref?.();
|
|
136
|
+
}
|
|
137
|
+
/** Stops the session and forgets the call. The timer is cleared first so it cannot fire again. */
|
|
138
|
+
async stopAndForget(callKey, call, reason) {
|
|
139
|
+
if (call.Timer) {
|
|
140
|
+
clearTimeout(call.Timer);
|
|
141
|
+
call.Timer = undefined;
|
|
142
|
+
}
|
|
143
|
+
this.calls.delete(callKey);
|
|
144
|
+
if (!call.Session) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
await this.stopSession(call.Session, reason);
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
LogError(`[Telephony] failed to stop the session for call ${callKey}: ${err instanceof Error ? err.message : String(err)}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=callLifecycleTracker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callLifecycleTracker.js","sourceRoot":"","sources":["../../src/telephony/callLifecycleTracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAqB,QAAQ,EAAE,SAAS,EAAY,MAAM,sBAAsB,CAAC;AAGxF,4DAA4D;AAC5D,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAE7C;;;GAGG;AACH,MAAM,iBAAiB,GAAG,MAAM,CAAC;AA6BjC;;GAEG;AACH,MAAM,OAAO,oBAAoB;IAI7B;;;OAGG;IACH,YACqB,WAA0B,EAC3C,iBAAyB,wBAAwB;QADhC,gBAAW,GAAX,WAAW,CAAe;QAR9B,UAAK,GAAG,IAAI,GAAG,EAAuB,CAAC;QAWpD,IAAI,CAAC,SAAS,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAC,IAAI,cAAc,GAAG,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,wBAAwB,CAAC,GAAG,IAAI,CAAC;IAChI,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,OAAe;QACxB,IAAI,IAAI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AAC;YACD,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,QAAQ,CAAC,mDAAmD,OAAO,KAAK,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChI,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The extension point that turns an inbound caller's number into whatever the host knows about them.
|
|
3
|
+
*
|
|
4
|
+
* MemberJunction core knows nothing about people, contacts or CRMs, so it cannot say who is calling. A host that
|
|
5
|
+
* does (a membership system, a CRM) registers its own {@link BaseCallerIdentityResolver} subclass under
|
|
6
|
+
* {@link CALLER_IDENTITY_RESOLVER_KEY} and the telephony services use it instead of the default, which treats
|
|
7
|
+
* every caller as anonymous. The agent is then told the caller's number, and whether anything about them was
|
|
8
|
+
* verified, as call context.
|
|
9
|
+
*
|
|
10
|
+
* **A caller-ID number is not proof of identity** — it is trivially spoofed. The default resolver says so, and a
|
|
11
|
+
* host resolver should only return `Verified: true` when it has verified the caller some other way (a PIN the
|
|
12
|
+
* caller entered, a callback, a signed-in session).
|
|
13
|
+
*
|
|
14
|
+
* @module @memberjunction/telephony-adapters
|
|
15
|
+
*/
|
|
16
|
+
import { UserInfo } from '@memberjunction/core';
|
|
17
|
+
/** The ClassFactory key a host registers its resolver under to replace the default. */
|
|
18
|
+
export declare const CALLER_IDENTITY_RESOLVER_KEY = "TelephonyCallerIdentity";
|
|
19
|
+
/** What the host knows about the party on the other end of an inbound call. */
|
|
20
|
+
export interface CallerIdentity {
|
|
21
|
+
/** A name to address the caller by, when the host recognises the number. */
|
|
22
|
+
DisplayName?: string;
|
|
23
|
+
/** Whether the host VERIFIED who this is (never merely "the number matched a record"). */
|
|
24
|
+
Verified: boolean;
|
|
25
|
+
/** Free-text facts the agent may use (membership status, open cases) — written for the model to read. */
|
|
26
|
+
ContextNotes?: string;
|
|
27
|
+
/** The linked record in the host CRM / database (e.g. Person, Contact) to record on InteractionLink. */
|
|
28
|
+
LinkedRecord?: {
|
|
29
|
+
EntityID: string;
|
|
30
|
+
RecordID: string;
|
|
31
|
+
};
|
|
32
|
+
/** Convenience Person record ID for linking when EntityID is resolved separately. */
|
|
33
|
+
PersonRecordID?: string;
|
|
34
|
+
PersonEntityID?: string;
|
|
35
|
+
}
|
|
36
|
+
/** Resolves an inbound caller's identity. */
|
|
37
|
+
export interface ICallerIdentityResolver {
|
|
38
|
+
/**
|
|
39
|
+
* @param fromNumber The caller's number as the carrier reported it (caller ID — unverified).
|
|
40
|
+
* @param dialedNumber The number the caller dialled (the agent's DID).
|
|
41
|
+
* @param contextUser The user the call runs as.
|
|
42
|
+
*/
|
|
43
|
+
ResolveCaller(fromNumber: string, dialedNumber: string, contextUser: UserInfo): Promise<CallerIdentity>;
|
|
44
|
+
}
|
|
45
|
+
/** Base class host resolvers extend and register under {@link CALLER_IDENTITY_RESOLVER_KEY}. */
|
|
46
|
+
export declare abstract class BaseCallerIdentityResolver implements ICallerIdentityResolver {
|
|
47
|
+
abstract ResolveCaller(fromNumber: string, dialedNumber: string, contextUser: UserInfo): Promise<CallerIdentity>;
|
|
48
|
+
}
|
|
49
|
+
/** The default: nothing is known about the caller and nothing is verified. */
|
|
50
|
+
export declare class AnonymousCallerIdentityResolver extends BaseCallerIdentityResolver {
|
|
51
|
+
ResolveCaller(): Promise<CallerIdentity>;
|
|
52
|
+
}
|
|
53
|
+
/** Tree-shaking anchor: importing this keeps the default resolver registered. */
|
|
54
|
+
export declare function LoadAnonymousCallerIdentityResolver(): void;
|
|
55
|
+
/**
|
|
56
|
+
* Builds the resolver to use: the host's registration (highest priority wins, so a host subclass loaded after
|
|
57
|
+
* this package replaces the default), else the anonymous default.
|
|
58
|
+
*/
|
|
59
|
+
export declare function CreateCallerIdentityResolver(): ICallerIdentityResolver;
|
|
60
|
+
/**
|
|
61
|
+
* Runs a resolver and never lets it fail the call: a throwing host resolver is logged and the caller is treated
|
|
62
|
+
* as anonymous, so a CRM outage cannot stop the phone from being answered.
|
|
63
|
+
*/
|
|
64
|
+
export declare function ResolveCallerSafely(resolver: ICallerIdentityResolver, fromNumber: string, dialedNumber: string, contextUser: UserInfo): Promise<CallerIdentity>;
|
|
65
|
+
//# sourceMappingURL=callerIdentity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callerIdentity.d.ts","sourceRoot":"","sources":["../../src/telephony/callerIdentity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAY,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAG1D,uFAAuF;AACvF,eAAO,MAAM,4BAA4B,4BAA4B,CAAC;AAEtE,+EAA+E;AAC/E,MAAM,WAAW,cAAc;IAC3B,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,0FAA0F;IAC1F,QAAQ,EAAE,OAAO,CAAC;IAClB,yGAAyG;IACzG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,wGAAwG;IACxG,YAAY,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACtD,qFAAqF;IACrF,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,cAAc,CAAC,EAAE,MAAM,CAAC;CAC3B;AAED,6CAA6C;AAC7C,MAAM,WAAW,uBAAuB;IACpC;;;;OAIG;IACH,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;CAC3G;AAED,gGAAgG;AAChG,8BAAsB,0BAA2B,YAAW,uBAAuB;aAC/D,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,GAAG,OAAO,CAAC,cAAc,CAAC;CAC1H;AAED,8EAA8E;AAC9E,qBACa,+BAAgC,SAAQ,0BAA0B;IAC9D,aAAa,IAAI,OAAO,CAAC,cAAc,CAAC;CAGxD;AAED,iFAAiF;AACjF,wBAAgB,mCAAmC,IAAI,IAAI,CAE1D;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,IAAI,uBAAuB,CAStE;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CACrC,QAAQ,EAAE,uBAAuB,EACjC,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,QAAQ,GACtB,OAAO,CAAC,cAAc,CAAC,CASzB"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The extension point that turns an inbound caller's number into whatever the host knows about them.
|
|
3
|
+
*
|
|
4
|
+
* MemberJunction core knows nothing about people, contacts or CRMs, so it cannot say who is calling. A host that
|
|
5
|
+
* does (a membership system, a CRM) registers its own {@link BaseCallerIdentityResolver} subclass under
|
|
6
|
+
* {@link CALLER_IDENTITY_RESOLVER_KEY} and the telephony services use it instead of the default, which treats
|
|
7
|
+
* every caller as anonymous. The agent is then told the caller's number, and whether anything about them was
|
|
8
|
+
* verified, as call context.
|
|
9
|
+
*
|
|
10
|
+
* **A caller-ID number is not proof of identity** — it is trivially spoofed. The default resolver says so, and a
|
|
11
|
+
* host resolver should only return `Verified: true` when it has verified the caller some other way (a PIN the
|
|
12
|
+
* caller entered, a callback, a signed-in session).
|
|
13
|
+
*
|
|
14
|
+
* @module @memberjunction/telephony-adapters
|
|
15
|
+
*/
|
|
16
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
17
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
18
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
19
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
20
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
21
|
+
};
|
|
22
|
+
import { LogError } from '@memberjunction/core';
|
|
23
|
+
import { MJGlobal, RegisterClass } from '@memberjunction/global';
|
|
24
|
+
/** The ClassFactory key a host registers its resolver under to replace the default. */
|
|
25
|
+
export const CALLER_IDENTITY_RESOLVER_KEY = 'TelephonyCallerIdentity';
|
|
26
|
+
/** Base class host resolvers extend and register under {@link CALLER_IDENTITY_RESOLVER_KEY}. */
|
|
27
|
+
export class BaseCallerIdentityResolver {
|
|
28
|
+
}
|
|
29
|
+
/** The default: nothing is known about the caller and nothing is verified. */
|
|
30
|
+
let AnonymousCallerIdentityResolver = class AnonymousCallerIdentityResolver extends BaseCallerIdentityResolver {
|
|
31
|
+
async ResolveCaller() {
|
|
32
|
+
return { Verified: false };
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
AnonymousCallerIdentityResolver = __decorate([
|
|
36
|
+
RegisterClass(BaseCallerIdentityResolver, CALLER_IDENTITY_RESOLVER_KEY)
|
|
37
|
+
], AnonymousCallerIdentityResolver);
|
|
38
|
+
export { AnonymousCallerIdentityResolver };
|
|
39
|
+
/** Tree-shaking anchor: importing this keeps the default resolver registered. */
|
|
40
|
+
export function LoadAnonymousCallerIdentityResolver() {
|
|
41
|
+
// intentionally empty — the import itself is what registers the class
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Builds the resolver to use: the host's registration (highest priority wins, so a host subclass loaded after
|
|
45
|
+
* this package replaces the default), else the anonymous default.
|
|
46
|
+
*/
|
|
47
|
+
export function CreateCallerIdentityResolver() {
|
|
48
|
+
const registration = MJGlobal.Instance.ClassFactory.GetRegistration(BaseCallerIdentityResolver, CALLER_IDENTITY_RESOLVER_KEY);
|
|
49
|
+
if (registration) {
|
|
50
|
+
const instance = MJGlobal.Instance.ClassFactory.CreateInstance(BaseCallerIdentityResolver, CALLER_IDENTITY_RESOLVER_KEY);
|
|
51
|
+
if (instance) {
|
|
52
|
+
return instance;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return new AnonymousCallerIdentityResolver();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Runs a resolver and never lets it fail the call: a throwing host resolver is logged and the caller is treated
|
|
59
|
+
* as anonymous, so a CRM outage cannot stop the phone from being answered.
|
|
60
|
+
*/
|
|
61
|
+
export async function ResolveCallerSafely(resolver, fromNumber, dialedNumber, contextUser) {
|
|
62
|
+
try {
|
|
63
|
+
const identity = await resolver.ResolveCaller(fromNumber, dialedNumber, contextUser);
|
|
64
|
+
// A resolver can only vouch for a caller it actually identified.
|
|
65
|
+
return { ...identity, Verified: identity.Verified === true };
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
LogError(`[Telephony] caller identity resolution failed; treating the caller as anonymous: ${e instanceof Error ? e.message : String(e)}`);
|
|
69
|
+
return { Verified: false };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=callerIdentity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"callerIdentity.js","sourceRoot":"","sources":["../../src/telephony/callerIdentity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;;;;;;;AAEH,OAAO,EAAE,QAAQ,EAAY,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAEjE,uFAAuF;AACvF,MAAM,CAAC,MAAM,4BAA4B,GAAG,yBAAyB,CAAC;AA2BtE,gGAAgG;AAChG,MAAM,OAAgB,0BAA0B;CAE/C;AAED,8EAA8E;AAEvE,IAAM,+BAA+B,GAArC,MAAM,+BAAgC,SAAQ,0BAA0B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AC;IAC/B,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Turns a named entry of the transfer directory into a resolved destination the room handoff engine can act
|
|
3
|
+
* on, at the moment the agent asks to transfer.
|
|
4
|
+
*
|
|
5
|
+
* The directory (`telephony.transferTargets`) holds names, emails and agent names, and is validated when the server starts.
|
|
6
|
+
* What it cannot know until a call is live is whether the person still exists and is active, whether the agent exists and
|
|
7
|
+
* the call's run-as user may run it, and whether a number still passes the outbound policy. Those are checked here, every
|
|
8
|
+
* time, because the answers change while the server runs and because the model (and so an unverified caller) is the one
|
|
9
|
+
* choosing which entry to ask for.
|
|
10
|
+
*
|
|
11
|
+
* Every refusal message is safe to hand back to the model: it says a destination is unavailable without saying why in a
|
|
12
|
+
* way that exposes configuration.
|
|
13
|
+
*
|
|
14
|
+
* @module @memberjunction/telephony-adapters
|
|
15
|
+
*/
|
|
16
|
+
import type { IMetadataProvider, UserInfo } from '@memberjunction/core';
|
|
17
|
+
import type { HandoffDestination } from '@memberjunction/livekit-room-server';
|
|
18
|
+
import { type OutboundCallPolicy, type TransferTarget } from './outboundCallPolicy.js';
|
|
19
|
+
import { type RunAsUserDirectory } from './runAsIdentity.js';
|
|
20
|
+
/** An agent found by name. */
|
|
21
|
+
export interface FoundAgent {
|
|
22
|
+
ID: string;
|
|
23
|
+
Name: string;
|
|
24
|
+
}
|
|
25
|
+
/** What destination resolution reads. All optional: production defaults are used when absent. */
|
|
26
|
+
export interface HandoffDestinationDeps {
|
|
27
|
+
/** The outbound policy a number (or a fallback number) is checked against. */
|
|
28
|
+
Policy: OutboundCallPolicy;
|
|
29
|
+
/** User lookups (defaults to the process-wide user cache). */
|
|
30
|
+
Directory?: RunAsUserDirectory;
|
|
31
|
+
/** Finds an active agent by name (defaults to the AI engine's agent cache). */
|
|
32
|
+
FindAgentByName?: (name: string, user: UserInfo, provider: IMetadataProvider) => Promise<FoundAgent | undefined>;
|
|
33
|
+
/** Whether the user may run the agent (defaults to the agent permission check the browser path applies). */
|
|
34
|
+
CanRunAgent?: (agentId: string, user: UserInfo) => Promise<boolean>;
|
|
35
|
+
}
|
|
36
|
+
/** The outcome of resolving a directory entry. */
|
|
37
|
+
export type ResolvedDestination = {
|
|
38
|
+
Ok: true;
|
|
39
|
+
Destination: HandoffDestination;
|
|
40
|
+
} | {
|
|
41
|
+
Ok: false;
|
|
42
|
+
Error: string;
|
|
43
|
+
};
|
|
44
|
+
/** The context the resolution runs in: the call's run-as user and provider. */
|
|
45
|
+
export interface HandoffResolutionContext {
|
|
46
|
+
User: UserInfo;
|
|
47
|
+
Provider: IMetadataProvider;
|
|
48
|
+
}
|
|
49
|
+
/** Finds an ACTIVE agent by name (case-insensitive) in the AI engine's cache. */
|
|
50
|
+
export declare function FindActiveAgentByName(name: string, user: UserInfo, provider: IMetadataProvider): Promise<FoundAgent | undefined>;
|
|
51
|
+
/** The display name of an agent by id, or `undefined` when it is not in the cache. */
|
|
52
|
+
export declare function FindAgentNameByID(agentID: string, user: UserInfo, provider: IMetadataProvider): Promise<string | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* Resolves one directory entry to a destination, checking everything that can change while the server runs. Never throws.
|
|
55
|
+
*/
|
|
56
|
+
export declare function ResolveHandoffDestination(target: TransferTarget, context: HandoffResolutionContext, deps: HandoffDestinationDeps): Promise<ResolvedDestination>;
|
|
57
|
+
//# sourceMappingURL=handoffDestinations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handoffDestinations.d.ts","sourceRoot":"","sources":["../../src/telephony/handoffDestinations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAE9E,OAAO,EAA6C,KAAK,kBAAkB,EAAE,KAAK,cAAc,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,MAAM,GAAG,SAAS,CAAC,CAIjI;AAED;;GAEG;AACH,wBAAsB,yBAAyB,CAC3C,MAAM,EAAE,cAAc,EACtB,OAAO,EAAE,wBAAwB,EACjC,IAAI,EAAE,sBAAsB,GAC7B,OAAO,CAAC,mBAAmB,CAAC,CAS9B"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Turns a named entry of the transfer directory into a resolved destination the room handoff engine can act
|
|
3
|
+
* on, at the moment the agent asks to transfer.
|
|
4
|
+
*
|
|
5
|
+
* The directory (`telephony.transferTargets`) holds names, emails and agent names, and is validated when the server starts.
|
|
6
|
+
* What it cannot know until a call is live is whether the person still exists and is active, whether the agent exists and
|
|
7
|
+
* the call's run-as user may run it, and whether a number still passes the outbound policy. Those are checked here, every
|
|
8
|
+
* time, because the answers change while the server runs and because the model (and so an unverified caller) is the one
|
|
9
|
+
* choosing which entry to ask for.
|
|
10
|
+
*
|
|
11
|
+
* Every refusal message is safe to hand back to the model: it says a destination is unavailable without saying why in a
|
|
12
|
+
* way that exposes configuration.
|
|
13
|
+
*
|
|
14
|
+
* @module @memberjunction/telephony-adapters
|
|
15
|
+
*/
|
|
16
|
+
import { AIEngineBase } from '@memberjunction/ai-engine-base';
|
|
17
|
+
import { CheckTransferDestination, CanUserRunAgent } from './outboundCallPolicy.js';
|
|
18
|
+
import { UserCacheDirectory } from './runAsIdentity.js';
|
|
19
|
+
/** Finds an ACTIVE agent by name (case-insensitive) in the AI engine's cache. */
|
|
20
|
+
export async function FindActiveAgentByName(name, user, provider) {
|
|
21
|
+
await AIEngineBase.Instance.Config(false, user, provider);
|
|
22
|
+
const wanted = (name ?? '').trim().toLowerCase();
|
|
23
|
+
const agent = AIEngineBase.Instance.Agents.find((a) => a.Status === 'Active' && (a.Name ?? '').trim().toLowerCase() === wanted);
|
|
24
|
+
return agent ? { ID: agent.ID, Name: agent.Name ?? name } : undefined;
|
|
25
|
+
}
|
|
26
|
+
/** The display name of an agent by id, or `undefined` when it is not in the cache. */
|
|
27
|
+
export async function FindAgentNameByID(agentID, user, provider) {
|
|
28
|
+
await AIEngineBase.Instance.Config(false, user, provider);
|
|
29
|
+
const wanted = (agentID ?? '').toLowerCase();
|
|
30
|
+
return AIEngineBase.Instance.Agents.find((a) => a.ID.toLowerCase() === wanted)?.Name ?? undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Resolves one directory entry to a destination, checking everything that can change while the server runs. Never throws.
|
|
34
|
+
*/
|
|
35
|
+
export async function ResolveHandoffDestination(target, context, deps) {
|
|
36
|
+
switch (target.Kind) {
|
|
37
|
+
case 'number':
|
|
38
|
+
return resolveNumber(target.Number, target.Name, deps.Policy);
|
|
39
|
+
case 'user':
|
|
40
|
+
return resolveUser(target, deps);
|
|
41
|
+
case 'agent':
|
|
42
|
+
return resolveAgent(target.AgentName, context, deps);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function resolveNumber(number, displayName, policy) {
|
|
46
|
+
const verdict = CheckTransferDestination(policy, number);
|
|
47
|
+
if (!verdict.Allowed) {
|
|
48
|
+
return { Ok: false, Error: verdict.Reason };
|
|
49
|
+
}
|
|
50
|
+
return { Ok: true, Destination: { Kind: 'number', Number: verdict.Number, DisplayName: displayName } };
|
|
51
|
+
}
|
|
52
|
+
function resolveUser(target, deps) {
|
|
53
|
+
const user = (deps.Directory ?? UserCacheDirectory).FindByEmail(target.UserEmail);
|
|
54
|
+
if (!user || !user.IsActive) {
|
|
55
|
+
return { Ok: false, Error: `${target.Name} is not available.` };
|
|
56
|
+
}
|
|
57
|
+
if (!target.FallbackNumber) {
|
|
58
|
+
return { Ok: true, Destination: { Kind: 'user', UserID: user.ID, DisplayName: target.Name } };
|
|
59
|
+
}
|
|
60
|
+
const fallback = CheckTransferDestination(deps.Policy, target.FallbackNumber);
|
|
61
|
+
if (!fallback.Allowed) {
|
|
62
|
+
return { Ok: false, Error: fallback.Reason };
|
|
63
|
+
}
|
|
64
|
+
return { Ok: true, Destination: { Kind: 'user', UserID: user.ID, DisplayName: target.Name, FallbackNumber: fallback.Number } };
|
|
65
|
+
}
|
|
66
|
+
async function resolveAgent(agentName, context, deps) {
|
|
67
|
+
const find = deps.FindAgentByName ?? FindActiveAgentByName;
|
|
68
|
+
const agent = await find(agentName, context.User, context.Provider);
|
|
69
|
+
if (!agent) {
|
|
70
|
+
return { Ok: false, Error: `${agentName} is not available.` };
|
|
71
|
+
}
|
|
72
|
+
const canRun = deps.CanRunAgent ?? CanUserRunAgent;
|
|
73
|
+
if (!(await canRun(agent.ID, context.User))) {
|
|
74
|
+
return { Ok: false, Error: `${agentName} is not available.` };
|
|
75
|
+
}
|
|
76
|
+
return { Ok: true, Destination: { Kind: 'agent', AgentID: agent.ID, AgentName: agent.Name } };
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=handoffDestinations.js.map
|