@memberjunction/telephony-adapters 6.2.0-edge.2 → 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,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Per-call media-socket authentication shared by the Twilio and Vonage media registries.
|
|
3
|
+
*
|
|
4
|
+
* The carrier media websockets (`WSS /telephony/twilio/media`, `WSS /telephony/vonage/media`) are public:
|
|
5
|
+
* a carrier cannot present an MJ JWT, and the voice-webhook signature only covers the HTTP request that
|
|
6
|
+
* precedes the socket. Without a second factor, anyone who can reach the server could open a socket, claim
|
|
7
|
+
* any call id, and either listen to a live call or inject audio into it.
|
|
8
|
+
*
|
|
9
|
+
* The second factor is a **per-call secret token**. MJ mints it when it accepts an inbound call or places an
|
|
10
|
+
* outbound one, embeds it in the TwiML / NCCO the carrier will execute (so only the carrier ever sees it),
|
|
11
|
+
* and records it here as an *expected call*. The socket must present the same token to attach. A call that
|
|
12
|
+
* was never registered, a wrong token, or a call whose socket is already attached are all refused — a socket
|
|
13
|
+
* never replaces another and never creates state for a call MJ did not register.
|
|
14
|
+
*
|
|
15
|
+
* Expectations carry a TTL so a call whose media socket never connects does not leave an entry (and its
|
|
16
|
+
* channel) behind forever.
|
|
17
|
+
*
|
|
18
|
+
* @module @memberjunction/telephony-adapters
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* How long an expected call may wait for its media socket to connect before the expectation is dropped.
|
|
22
|
+
* Inbound sockets connect within seconds of the answer response; outbound sockets only connect once the
|
|
23
|
+
* callee answers, so this must outlast the carrier's ring time (Twilio's default ring timeout is 60 s).
|
|
24
|
+
*/
|
|
25
|
+
export declare const DEFAULT_EXPECTATION_TTL_MS = 90000;
|
|
26
|
+
/** Why a media socket was refused. */
|
|
27
|
+
export type SocketAuthFailure = 'unknown-call' | 'bad-token' | 'already-attached';
|
|
28
|
+
/** The verdict of {@link ExpectedCallStore.Verify}. */
|
|
29
|
+
export type SocketAuthResult = {
|
|
30
|
+
Ok: true;
|
|
31
|
+
} | {
|
|
32
|
+
Ok: false;
|
|
33
|
+
Reason: SocketAuthFailure;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The set of calls MJ is expecting a media socket for, keyed by the carrier's call identifier (or, for a
|
|
37
|
+
* Vonage outbound call whose UUID is not yet known, a correlation id). Pure bookkeeping — no sockets, no I/O.
|
|
38
|
+
*/
|
|
39
|
+
export declare class ExpectedCallStore {
|
|
40
|
+
private readonly onExpired;
|
|
41
|
+
private readonly defaultTtlMs;
|
|
42
|
+
private readonly entries;
|
|
43
|
+
/**
|
|
44
|
+
* @param onExpired Invoked (once) when a call's TTL elapses with no socket ever attached. The entry has
|
|
45
|
+
* already been removed by then.
|
|
46
|
+
* @param defaultTtlMs TTL applied when {@link Expect} is not given one explicitly.
|
|
47
|
+
*/
|
|
48
|
+
constructor(onExpired: (key: string) => void, defaultTtlMs?: number);
|
|
49
|
+
/**
|
|
50
|
+
* Registers (or re-registers) a call's token and starts its connect TTL. Re-registering the same key
|
|
51
|
+
* replaces the earlier expectation and restarts the timer.
|
|
52
|
+
*
|
|
53
|
+
* @param key The carrier call id (or correlation id).
|
|
54
|
+
* @param token The per-call secret the socket must present.
|
|
55
|
+
* @param ttlMs Optional TTL override in milliseconds.
|
|
56
|
+
*/
|
|
57
|
+
Expect(key: string, token: string, ttlMs?: number): void;
|
|
58
|
+
/**
|
|
59
|
+
* Decides whether a connecting socket may attach to the call. The order matters: an unknown call is
|
|
60
|
+
* reported before the token is examined, and a wrong token before "already attached", so a probe never
|
|
61
|
+
* learns whether a given call currently has a live socket.
|
|
62
|
+
*/
|
|
63
|
+
Verify(key: string, token: string | undefined): SocketAuthResult;
|
|
64
|
+
/** Records that a socket attached; the connect TTL no longer applies. */
|
|
65
|
+
MarkAttached(key: string): void;
|
|
66
|
+
/** Moves an expectation to a new key (Vonage: correlation id → call UUID), keeping its token and timer. */
|
|
67
|
+
Rekey(fromKey: string, toKey: string): void;
|
|
68
|
+
/** Removes a call's expectation and cancels its TTL timer. Safe for unknown keys. */
|
|
69
|
+
Forget(key: string): void;
|
|
70
|
+
/** Whether an expectation (attached or not) exists for the key. */
|
|
71
|
+
Has(key: string): boolean;
|
|
72
|
+
/** Cancels every outstanding TTL timer and clears the store (registry/service shutdown). */
|
|
73
|
+
Clear(): void;
|
|
74
|
+
/** Starts the connect-TTL timer: a never-attached expectation expires, is removed, and is reported once. */
|
|
75
|
+
private startTtlTimer;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=mediaSocketAuth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediaSocketAuth.d.ts","sourceRoot":"","sources":["../../src/telephony/mediaSocketAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH;;;;GAIG;AACH,eAAO,MAAM,0BAA0B,QAAS,CAAC;AAEjD,sCAAsC;AACtC,MAAM,MAAM,iBAAiB,GAAG,cAAc,GAAG,WAAW,GAAG,kBAAkB,CAAC;AAElF,uDAAuD;AACvD,MAAM,MAAM,gBAAgB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,iBAAiB,CAAA;CAAE,CAAC;AASvF;;;GAGG;AACH,qBAAa,iBAAiB;IAStB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,YAAY;IATjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsC;IAE9D;;;;OAIG;gBAEkB,SAAS,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,EAChC,YAAY,GAAE,MAAmC;IAGtE;;;;;;;OAOG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,GAAE,MAA0B,GAAG,IAAI;IAOlF;;;;OAIG;IACI,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,gBAAgB;IAcvE,yEAAyE;IAClE,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAYtC,2GAA2G;IACpG,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IAelD,qFAAqF;IAC9E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAWhC,mEAAmE;IAC5D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIhC,4FAA4F;IACrF,KAAK,IAAI,IAAI;IASpB,4GAA4G;IAC5G,OAAO,CAAC,aAAa;CASxB"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Per-call media-socket authentication shared by the Twilio and Vonage media registries.
|
|
3
|
+
*
|
|
4
|
+
* The carrier media websockets (`WSS /telephony/twilio/media`, `WSS /telephony/vonage/media`) are public:
|
|
5
|
+
* a carrier cannot present an MJ JWT, and the voice-webhook signature only covers the HTTP request that
|
|
6
|
+
* precedes the socket. Without a second factor, anyone who can reach the server could open a socket, claim
|
|
7
|
+
* any call id, and either listen to a live call or inject audio into it.
|
|
8
|
+
*
|
|
9
|
+
* The second factor is a **per-call secret token**. MJ mints it when it accepts an inbound call or places an
|
|
10
|
+
* outbound one, embeds it in the TwiML / NCCO the carrier will execute (so only the carrier ever sees it),
|
|
11
|
+
* and records it here as an *expected call*. The socket must present the same token to attach. A call that
|
|
12
|
+
* was never registered, a wrong token, or a call whose socket is already attached are all refused — a socket
|
|
13
|
+
* never replaces another and never creates state for a call MJ did not register.
|
|
14
|
+
*
|
|
15
|
+
* Expectations carry a TTL so a call whose media socket never connects does not leave an entry (and its
|
|
16
|
+
* channel) behind forever.
|
|
17
|
+
*
|
|
18
|
+
* @module @memberjunction/telephony-adapters
|
|
19
|
+
*/
|
|
20
|
+
import { MediaTokensEqual } from '@memberjunction/ai-bridge-base';
|
|
21
|
+
/**
|
|
22
|
+
* How long an expected call may wait for its media socket to connect before the expectation is dropped.
|
|
23
|
+
* Inbound sockets connect within seconds of the answer response; outbound sockets only connect once the
|
|
24
|
+
* callee answers, so this must outlast the carrier's ring time (Twilio's default ring timeout is 60 s).
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULT_EXPECTATION_TTL_MS = 90_000;
|
|
27
|
+
/**
|
|
28
|
+
* The set of calls MJ is expecting a media socket for, keyed by the carrier's call identifier (or, for a
|
|
29
|
+
* Vonage outbound call whose UUID is not yet known, a correlation id). Pure bookkeeping — no sockets, no I/O.
|
|
30
|
+
*/
|
|
31
|
+
export class ExpectedCallStore {
|
|
32
|
+
/**
|
|
33
|
+
* @param onExpired Invoked (once) when a call's TTL elapses with no socket ever attached. The entry has
|
|
34
|
+
* already been removed by then.
|
|
35
|
+
* @param defaultTtlMs TTL applied when {@link Expect} is not given one explicitly.
|
|
36
|
+
*/
|
|
37
|
+
constructor(onExpired, defaultTtlMs = DEFAULT_EXPECTATION_TTL_MS) {
|
|
38
|
+
this.onExpired = onExpired;
|
|
39
|
+
this.defaultTtlMs = defaultTtlMs;
|
|
40
|
+
this.entries = new Map();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Registers (or re-registers) a call's token and starts its connect TTL. Re-registering the same key
|
|
44
|
+
* replaces the earlier expectation and restarts the timer.
|
|
45
|
+
*
|
|
46
|
+
* @param key The carrier call id (or correlation id).
|
|
47
|
+
* @param token The per-call secret the socket must present.
|
|
48
|
+
* @param ttlMs Optional TTL override in milliseconds.
|
|
49
|
+
*/
|
|
50
|
+
Expect(key, token, ttlMs = this.defaultTtlMs) {
|
|
51
|
+
this.Forget(key);
|
|
52
|
+
const entry = { Token: token, Attached: false };
|
|
53
|
+
this.startTtlTimer(key, entry, ttlMs);
|
|
54
|
+
this.entries.set(key, entry);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Decides whether a connecting socket may attach to the call. The order matters: an unknown call is
|
|
58
|
+
* reported before the token is examined, and a wrong token before "already attached", so a probe never
|
|
59
|
+
* learns whether a given call currently has a live socket.
|
|
60
|
+
*/
|
|
61
|
+
Verify(key, token) {
|
|
62
|
+
const entry = this.entries.get(key);
|
|
63
|
+
if (!entry) {
|
|
64
|
+
return { Ok: false, Reason: 'unknown-call' };
|
|
65
|
+
}
|
|
66
|
+
if (!MediaTokensEqual(entry.Token, token)) {
|
|
67
|
+
return { Ok: false, Reason: 'bad-token' };
|
|
68
|
+
}
|
|
69
|
+
if (entry.Attached) {
|
|
70
|
+
return { Ok: false, Reason: 'already-attached' };
|
|
71
|
+
}
|
|
72
|
+
return { Ok: true };
|
|
73
|
+
}
|
|
74
|
+
/** Records that a socket attached; the connect TTL no longer applies. */
|
|
75
|
+
MarkAttached(key) {
|
|
76
|
+
const entry = this.entries.get(key);
|
|
77
|
+
if (!entry) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
entry.Attached = true;
|
|
81
|
+
if (entry.Timer) {
|
|
82
|
+
clearTimeout(entry.Timer);
|
|
83
|
+
entry.Timer = undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Moves an expectation to a new key (Vonage: correlation id → call UUID), keeping its token and timer. */
|
|
87
|
+
Rekey(fromKey, toKey) {
|
|
88
|
+
const entry = this.entries.get(fromKey);
|
|
89
|
+
if (!entry || fromKey === toKey) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
this.entries.delete(fromKey);
|
|
93
|
+
this.entries.set(toKey, entry);
|
|
94
|
+
if (entry.Timer) {
|
|
95
|
+
// The timer closes over the ORIGINAL key, so it must be restarted on the new one. A rekey happens
|
|
96
|
+
// milliseconds after Expect, so restarting with the default TTL is indistinguishable from "time left".
|
|
97
|
+
clearTimeout(entry.Timer);
|
|
98
|
+
this.startTtlTimer(toKey, entry, this.defaultTtlMs);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Removes a call's expectation and cancels its TTL timer. Safe for unknown keys. */
|
|
102
|
+
Forget(key) {
|
|
103
|
+
const entry = this.entries.get(key);
|
|
104
|
+
if (!entry) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (entry.Timer) {
|
|
108
|
+
clearTimeout(entry.Timer);
|
|
109
|
+
}
|
|
110
|
+
this.entries.delete(key);
|
|
111
|
+
}
|
|
112
|
+
/** Whether an expectation (attached or not) exists for the key. */
|
|
113
|
+
Has(key) {
|
|
114
|
+
return this.entries.has(key);
|
|
115
|
+
}
|
|
116
|
+
/** Cancels every outstanding TTL timer and clears the store (registry/service shutdown). */
|
|
117
|
+
Clear() {
|
|
118
|
+
for (const entry of this.entries.values()) {
|
|
119
|
+
if (entry.Timer) {
|
|
120
|
+
clearTimeout(entry.Timer);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
this.entries.clear();
|
|
124
|
+
}
|
|
125
|
+
/** Starts the connect-TTL timer: a never-attached expectation expires, is removed, and is reported once. */
|
|
126
|
+
startTtlTimer(key, entry, ttlMs) {
|
|
127
|
+
entry.Timer = setTimeout(() => {
|
|
128
|
+
// Only a never-attached expectation can expire (MarkAttached clears the timer).
|
|
129
|
+
this.entries.delete(key);
|
|
130
|
+
this.onExpired(key);
|
|
131
|
+
}, ttlMs);
|
|
132
|
+
// Never keep the process alive solely for a connect timeout (Node-only; guarded for other hosts).
|
|
133
|
+
entry.Timer.unref?.();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=mediaSocketAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mediaSocketAuth.js","sourceRoot":"","sources":["../../src/telephony/mediaSocketAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,gCAAgC,CAAC;AAElE;;;;GAIG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,MAAM,CAAC;AAejD;;;GAGG;AACH,MAAM,OAAO,iBAAiB;IAG1B;;;;OAIG;IACH,YACqB,SAAgC,EAChC,eAAuB,0BAA0B;QADjD,cAAS,GAAT,SAAS,CAAuB;QAChC,iBAAY,GAAZ,YAAY,CAAqC;QATrD,YAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;IAU3D,CAAC;IAEJ;;;;;;;OAOG;IACI,MAAM,CAAC,GAAW,EAAE,KAAa,EAAE,QAAgB,IAAI,CAAC,YAAY;QACvE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACjB,MAAM,KAAK,GAAoB,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;QACjE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;QACtC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,GAAW,EAAE,KAAyB;QAChD,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CA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C;QACV,kGAAkG;QACjG,KAAK,CAAC,KAAgC,CAAC,KAAK,EAAE,EAAE,CAAC;IACtD,CAAC;CACJ"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Outbound caller-ID number pool selector.
|
|
3
|
+
*
|
|
4
|
+
* Implements selection over `MJ: Number Pools` and `MJ: Phone Numbers` using:
|
|
5
|
+
* - RoundRobin: evenly rotates through active numbers in the pool;
|
|
6
|
+
* - LocalPresence: prefers numbers matching the area code / country prefix of the callee (ToNumber),
|
|
7
|
+
* falling back to RoundRobin if no match;
|
|
8
|
+
* - Random: picks randomly among qualifying pool numbers.
|
|
9
|
+
*
|
|
10
|
+
* Enforces:
|
|
11
|
+
* - PhoneNumber.Status = 'Active' (Inactive or Porting numbers are skipped);
|
|
12
|
+
* - MaxConcurrentPerNumber ceiling: counts active `MJ: Interactions` for each candidate number,
|
|
13
|
+
* skipping numbers that have reached or exceeded the ceiling.
|
|
14
|
+
*
|
|
15
|
+
* @module @memberjunction/telephony-adapters
|
|
16
|
+
*/
|
|
17
|
+
import { IMetadataProvider, UserInfo } from '@memberjunction/core';
|
|
18
|
+
import { BaseSingleton } from '@memberjunction/global';
|
|
19
|
+
import type { MJNumberPoolEntity, MJPhoneNumberEntity } from '@memberjunction/core-entities';
|
|
20
|
+
export interface NumberPoolSelectionRequest {
|
|
21
|
+
/** The pool to select a number from. */
|
|
22
|
+
NumberPoolID: string;
|
|
23
|
+
/** The callee's number (used for LocalPresence matching). */
|
|
24
|
+
ToNumber?: string;
|
|
25
|
+
/** Telephony carrier provider ID to optionally filter numbers by carrier. */
|
|
26
|
+
ProviderID?: string;
|
|
27
|
+
ContextUser: UserInfo;
|
|
28
|
+
MetadataProvider?: IMetadataProvider;
|
|
29
|
+
}
|
|
30
|
+
export interface SelectedNumberPoolResult {
|
|
31
|
+
PhoneNumber: MJPhoneNumberEntity;
|
|
32
|
+
Number: string;
|
|
33
|
+
PhoneNumberID: string;
|
|
34
|
+
SelectionRule: MJNumberPoolEntity['SelectionRule'];
|
|
35
|
+
}
|
|
36
|
+
/** Selects numbers from outbound number pools according to configured policies. */
|
|
37
|
+
export declare class NumberPoolSelector extends BaseSingleton<NumberPoolSelector> {
|
|
38
|
+
constructor();
|
|
39
|
+
private readonly roundRobinPointers;
|
|
40
|
+
static get Instance(): NumberPoolSelector;
|
|
41
|
+
/** Resets in-memory round robin pointers (primarily for deterministic unit tests). */
|
|
42
|
+
ResetPointers(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Selects an active phone number from the given pool, honouring concurrency limits
|
|
45
|
+
* and the pool's selection rule.
|
|
46
|
+
*
|
|
47
|
+
* @returns The selected phone number, or `null` if no qualifying number is available.
|
|
48
|
+
*/
|
|
49
|
+
SelectNumber(request: NumberPoolSelectionRequest): Promise<SelectedNumberPoolResult | null>;
|
|
50
|
+
private loadPool;
|
|
51
|
+
private loadActiveNumbers;
|
|
52
|
+
private filterByConcurrencyCap;
|
|
53
|
+
private applySelectionRule;
|
|
54
|
+
private pickRoundRobin;
|
|
55
|
+
/**
|
|
56
|
+
* Matches candidate numbers against `toNumber` for local presence.
|
|
57
|
+
* Checks area code match (first 5 chars for NANP +1NXX, e.g. +1415),
|
|
58
|
+
* then country prefix (e.g. +1).
|
|
59
|
+
*/
|
|
60
|
+
private matchLocalPresence;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=numberPoolSelector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numberPoolSelector.d.ts","sourceRoot":"","sources":["../../src/telephony/numberPoolSelector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,iBAAiB,EAAqB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACtF,OAAO,EAAE,aAAa,EAAmB,MAAM,wBAAwB,CAAC;AACxE,OAAO,KAAK,EAAE,kBAAkB,EAAE,mBAAmB,EAAuB,MAAM,+BAA+B,CAAC;AAMlH,MAAM,WAAW,0BAA0B;IACvC,wCAAwC;IACxC,YAAY,EAAE,MAAM,CAAC;IACrB,6DAA6D;IAC7D,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6EAA6E;IAC7E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,QAAQ,CAAC;IACtB,gBAAgB,CAAC,EAAE,iBAAiB,CAAC;CACxC;AAED,MAAM,WAAW,wBAAwB;IACrC,WAAW,EAAE,mBAAmB,CAAC;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,kBAAkB,CAAC,eAAe,CAAC,CAAC;CACtD;AAED,mFAAmF;AACnF,qBAAa,kBAAmB,SAAQ,aAAa,CAAC,kBAAkB,CAAC;;IAKrE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAA6B;IAEhE,WAAkB,QAAQ,IAAI,kBAAkB,CAE/C;IAED,sFAAsF;IAC/E,aAAa,IAAI,IAAI;IAI5B;;;;;OAKG;IACU,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAAC,wBAAwB,GAAG,IAAI,CAAC;YAiC1F,QAAQ;YAsBR,iBAAiB;YAsBjB,sBAAsB;IA8CpC,OAAO,CAAC,kBAAkB;IA8B1B,OAAO,CAAC,cAAc;IAOtB;;;;OAIG;IACH,OAAO,CAAC,kBAAkB;CA6B7B"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Outbound caller-ID number pool selector.
|
|
3
|
+
*
|
|
4
|
+
* Implements selection over `MJ: Number Pools` and `MJ: Phone Numbers` using:
|
|
5
|
+
* - RoundRobin: evenly rotates through active numbers in the pool;
|
|
6
|
+
* - LocalPresence: prefers numbers matching the area code / country prefix of the callee (ToNumber),
|
|
7
|
+
* falling back to RoundRobin if no match;
|
|
8
|
+
* - Random: picks randomly among qualifying pool numbers.
|
|
9
|
+
*
|
|
10
|
+
* Enforces:
|
|
11
|
+
* - PhoneNumber.Status = 'Active' (Inactive or Porting numbers are skipped);
|
|
12
|
+
* - MaxConcurrentPerNumber ceiling: counts active `MJ: Interactions` for each candidate number,
|
|
13
|
+
* skipping numbers that have reached or exceeded the ceiling.
|
|
14
|
+
*
|
|
15
|
+
* @module @memberjunction/telephony-adapters
|
|
16
|
+
*/
|
|
17
|
+
import { LogError, RunView } from '@memberjunction/core';
|
|
18
|
+
import { BaseSingleton, EscapeSQLString } from '@memberjunction/global';
|
|
19
|
+
const NUMBER_POOL_ENTITY = 'MJ: Number Pools';
|
|
20
|
+
const PHONE_NUMBER_ENTITY = 'MJ: Phone Numbers';
|
|
21
|
+
const INTERACTION_ENTITY = 'MJ: Interactions';
|
|
22
|
+
/** Selects numbers from outbound number pools according to configured policies. */
|
|
23
|
+
export class NumberPoolSelector extends BaseSingleton {
|
|
24
|
+
constructor() {
|
|
25
|
+
super();
|
|
26
|
+
this.roundRobinPointers = new Map();
|
|
27
|
+
}
|
|
28
|
+
static get Instance() {
|
|
29
|
+
return NumberPoolSelector.getInstance();
|
|
30
|
+
}
|
|
31
|
+
/** Resets in-memory round robin pointers (primarily for deterministic unit tests). */
|
|
32
|
+
ResetPointers() {
|
|
33
|
+
this.roundRobinPointers.clear();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Selects an active phone number from the given pool, honouring concurrency limits
|
|
37
|
+
* and the pool's selection rule.
|
|
38
|
+
*
|
|
39
|
+
* @returns The selected phone number, or `null` if no qualifying number is available.
|
|
40
|
+
*/
|
|
41
|
+
async SelectNumber(request) {
|
|
42
|
+
const pool = await this.loadPool(request.NumberPoolID, request.ContextUser, request.MetadataProvider);
|
|
43
|
+
if (!pool) {
|
|
44
|
+
LogError(`[NumberPoolSelector] pool '${request.NumberPoolID}' not found.`);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
const candidateNumbers = await this.loadActiveNumbers(request.NumberPoolID, request.ProviderID, request.ContextUser, request.MetadataProvider);
|
|
48
|
+
if (!candidateNumbers.length) {
|
|
49
|
+
LogError(`[NumberPoolSelector] no active phone numbers found in pool '${pool.Name}' (${pool.ID}).`);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
// Filter numbers by concurrency cap if MaxConcurrentPerNumber is set
|
|
53
|
+
const qualifyingNumbers = await this.filterByConcurrencyCap(pool, candidateNumbers, request.ContextUser, request.MetadataProvider);
|
|
54
|
+
if (!qualifyingNumbers.length) {
|
|
55
|
+
LogError(`[NumberPoolSelector] all active numbers in pool '${pool.Name}' are at capacity (cap: ${pool.MaxConcurrentPerNumber}).`);
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
const chosen = this.applySelectionRule(pool, qualifyingNumbers, request.ToNumber);
|
|
59
|
+
if (!chosen) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
PhoneNumber: chosen,
|
|
64
|
+
Number: chosen.Number,
|
|
65
|
+
PhoneNumberID: chosen.ID,
|
|
66
|
+
SelectionRule: pool.SelectionRule,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async loadPool(poolId, user, provider) {
|
|
70
|
+
try {
|
|
71
|
+
const runView = provider ? RunView.FromMetadataProvider(provider) : new RunView();
|
|
72
|
+
if (!runView.ProviderToUse && typeof runView.RunView !== 'function') {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const result = await runView.RunView({
|
|
76
|
+
EntityName: NUMBER_POOL_ENTITY,
|
|
77
|
+
ExtraFilter: `ID='${EscapeSQLString(poolId)}'`,
|
|
78
|
+
MaxRows: 1,
|
|
79
|
+
ResultType: 'entity_object',
|
|
80
|
+
}, user);
|
|
81
|
+
return result.Success && result.Results?.length ? result.Results[0] : null;
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
LogError(`[NumberPoolSelector] error loading pool: ${e instanceof Error ? e.message : String(e)}`);
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async loadActiveNumbers(poolId, providerId, user, provider) {
|
|
89
|
+
try {
|
|
90
|
+
const runView = provider ? RunView.FromMetadataProvider(provider) : new RunView();
|
|
91
|
+
if (!runView.ProviderToUse && typeof runView.RunView !== 'function') {
|
|
92
|
+
return [];
|
|
93
|
+
}
|
|
94
|
+
const providerFilter = providerId ? ` AND ProviderID='${EscapeSQLString(providerId)}'` : '';
|
|
95
|
+
const result = await runView.RunView({
|
|
96
|
+
EntityName: PHONE_NUMBER_ENTITY,
|
|
97
|
+
ExtraFilter: `NumberPoolID='${EscapeSQLString(poolId)}' AND Status='Active'${providerFilter}`,
|
|
98
|
+
ResultType: 'entity_object',
|
|
99
|
+
}, user);
|
|
100
|
+
return result.Success && result.Results ? result.Results : [];
|
|
101
|
+
}
|
|
102
|
+
catch (e) {
|
|
103
|
+
LogError(`[NumberPoolSelector] error loading active numbers: ${e instanceof Error ? e.message : String(e)}`);
|
|
104
|
+
return [];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async filterByConcurrencyCap(pool, candidates, user, provider) {
|
|
108
|
+
const maxConcurrent = pool.MaxConcurrentPerNumber;
|
|
109
|
+
if (maxConcurrent == null || maxConcurrent <= 0) {
|
|
110
|
+
return candidates;
|
|
111
|
+
}
|
|
112
|
+
const candidateIds = candidates.map((c) => `'${EscapeSQLString(c.ID)}'`).join(',');
|
|
113
|
+
const activeCounts = new Map();
|
|
114
|
+
try {
|
|
115
|
+
const runView = provider ? RunView.FromMetadataProvider(provider) : new RunView();
|
|
116
|
+
if (!runView.ProviderToUse && typeof runView.RunView !== 'function') {
|
|
117
|
+
return candidates;
|
|
118
|
+
}
|
|
119
|
+
const result = await runView.RunView({
|
|
120
|
+
EntityName: INTERACTION_ENTITY,
|
|
121
|
+
ExtraFilter: `Status='Active' AND PhoneNumberID IN (${candidateIds})`,
|
|
122
|
+
ResultType: 'entity_object',
|
|
123
|
+
}, user);
|
|
124
|
+
if (result.Success && result.Results) {
|
|
125
|
+
for (const interaction of result.Results) {
|
|
126
|
+
if (interaction.PhoneNumberID) {
|
|
127
|
+
const current = activeCounts.get(interaction.PhoneNumberID) ?? 0;
|
|
128
|
+
activeCounts.set(interaction.PhoneNumberID, current + 1);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (e) {
|
|
134
|
+
LogError(`[NumberPoolSelector] error checking concurrency cap: ${e instanceof Error ? e.message : String(e)}`);
|
|
135
|
+
}
|
|
136
|
+
return candidates.filter((candidate) => {
|
|
137
|
+
const count = activeCounts.get(candidate.ID) ?? 0;
|
|
138
|
+
return count < maxConcurrent;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
applySelectionRule(pool, candidates, toNumber) {
|
|
142
|
+
if (!candidates.length) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
switch (pool.SelectionRule) {
|
|
146
|
+
case 'Random':
|
|
147
|
+
return candidates[Math.floor(Math.random() * candidates.length)];
|
|
148
|
+
case 'LocalPresence': {
|
|
149
|
+
if (toNumber) {
|
|
150
|
+
const matched = this.matchLocalPresence(candidates, toNumber);
|
|
151
|
+
if (matched.length > 0) {
|
|
152
|
+
return this.pickRoundRobin(pool.ID, matched);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Fall back to round robin across all candidates
|
|
156
|
+
return this.pickRoundRobin(pool.ID, candidates);
|
|
157
|
+
}
|
|
158
|
+
case 'RoundRobin':
|
|
159
|
+
default:
|
|
160
|
+
return this.pickRoundRobin(pool.ID, candidates);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
pickRoundRobin(poolId, list) {
|
|
164
|
+
const current = this.roundRobinPointers.get(poolId) ?? 0;
|
|
165
|
+
const index = current % list.length;
|
|
166
|
+
this.roundRobinPointers.set(poolId, index + 1);
|
|
167
|
+
return list[index];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Matches candidate numbers against `toNumber` for local presence.
|
|
171
|
+
* Checks area code match (first 5 chars for NANP +1NXX, e.g. +1415),
|
|
172
|
+
* then country prefix (e.g. +1).
|
|
173
|
+
*/
|
|
174
|
+
matchLocalPresence(candidates, toNumber) {
|
|
175
|
+
const normalizedTo = toNumber.trim();
|
|
176
|
+
if (!normalizedTo.startsWith('+')) {
|
|
177
|
+
return [];
|
|
178
|
+
}
|
|
179
|
+
// Try matching area code (+1 followed by 3-digit area code = 5 chars, or other 5-char prefix)
|
|
180
|
+
if (normalizedTo.length >= 5) {
|
|
181
|
+
const areaCodePrefix = normalizedTo.slice(0, 5);
|
|
182
|
+
const areaMatches = candidates.filter((c) => (c.Number ?? '').startsWith(areaCodePrefix));
|
|
183
|
+
if (areaMatches.length > 0) {
|
|
184
|
+
return areaMatches;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
// Try 4-char prefix
|
|
188
|
+
if (normalizedTo.length >= 4) {
|
|
189
|
+
const prefix4 = normalizedTo.slice(0, 4);
|
|
190
|
+
const matches4 = candidates.filter((c) => (c.Number ?? '').startsWith(prefix4));
|
|
191
|
+
if (matches4.length > 0) {
|
|
192
|
+
return matches4;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Try country code prefix (e.g. +1 or +44)
|
|
196
|
+
const countryPrefix = normalizedTo.startsWith('+1') ? '+1' : normalizedTo.slice(0, 3);
|
|
197
|
+
const countryMatches = candidates.filter((c) => (c.Number ?? '').startsWith(countryPrefix));
|
|
198
|
+
return countryMatches;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=numberPoolSelector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numberPoolSelector.js","sourceRoot":"","sources":["../../src/telephony/numberPoolSelector.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAqB,QAAQ,EAAE,OAAO,EAAY,MAAM,sBAAsB,CAAC;AACt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