@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.
Files changed (157) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +10 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/resolvers/LiveKitSipResolver.d.ts +21 -0
  6. package/dist/resolvers/LiveKitSipResolver.d.ts.map +1 -0
  7. package/dist/resolvers/LiveKitSipResolver.js +87 -0
  8. package/dist/resolvers/LiveKitSipResolver.js.map +1 -0
  9. package/dist/resolvers/RingCentralTelephonyResolver.d.ts.map +1 -1
  10. package/dist/resolvers/RingCentralTelephonyResolver.js +4 -1
  11. package/dist/resolvers/RingCentralTelephonyResolver.js.map +1 -1
  12. package/dist/resolvers/TelephonyResolver.d.ts.map +1 -1
  13. package/dist/resolvers/TelephonyResolver.js +4 -1
  14. package/dist/resolvers/TelephonyResolver.js.map +1 -1
  15. package/dist/resolvers/VonageTelephonyResolver.d.ts.map +1 -1
  16. package/dist/resolvers/VonageTelephonyResolver.js +4 -1
  17. package/dist/resolvers/VonageTelephonyResolver.js.map +1 -1
  18. package/dist/resolvers/index.d.ts +1 -0
  19. package/dist/resolvers/index.d.ts.map +1 -1
  20. package/dist/resolvers/index.js +1 -0
  21. package/dist/resolvers/index.js.map +1 -1
  22. package/dist/server-extensions/LiveKitSipExtension.d.ts +14 -0
  23. package/dist/server-extensions/LiveKitSipExtension.d.ts.map +1 -0
  24. package/dist/server-extensions/LiveKitSipExtension.js +93 -0
  25. package/dist/server-extensions/LiveKitSipExtension.js.map +1 -0
  26. package/dist/server-extensions/RingCentralTelephonyExtension.d.ts.map +1 -1
  27. package/dist/server-extensions/RingCentralTelephonyExtension.js +8 -2
  28. package/dist/server-extensions/RingCentralTelephonyExtension.js.map +1 -1
  29. package/dist/server-extensions/TwilioTelephonyExtension.d.ts.map +1 -1
  30. package/dist/server-extensions/TwilioTelephonyExtension.js +10 -4
  31. package/dist/server-extensions/TwilioTelephonyExtension.js.map +1 -1
  32. package/dist/server-extensions/VonageTelephonyExtension.d.ts.map +1 -1
  33. package/dist/server-extensions/VonageTelephonyExtension.js +8 -4
  34. package/dist/server-extensions/VonageTelephonyExtension.js.map +1 -1
  35. package/dist/telephony/LiveKitSipRouter.d.ts +26 -0
  36. package/dist/telephony/LiveKitSipRouter.d.ts.map +1 -0
  37. package/dist/telephony/LiveKitSipRouter.js +62 -0
  38. package/dist/telephony/LiveKitSipRouter.js.map +1 -0
  39. package/dist/telephony/LiveKitSipTelephonyService.d.ts +130 -0
  40. package/dist/telephony/LiveKitSipTelephonyService.d.ts.map +1 -0
  41. package/dist/telephony/LiveKitSipTelephonyService.js +453 -0
  42. package/dist/telephony/LiveKitSipTelephonyService.js.map +1 -0
  43. package/dist/telephony/RingCentralTelephonyService.d.ts +57 -8
  44. package/dist/telephony/RingCentralTelephonyService.d.ts.map +1 -1
  45. package/dist/telephony/RingCentralTelephonyService.js +137 -77
  46. package/dist/telephony/RingCentralTelephonyService.js.map +1 -1
  47. package/dist/telephony/TwilioTelephonyRouter.d.ts +36 -6
  48. package/dist/telephony/TwilioTelephonyRouter.d.ts.map +1 -1
  49. package/dist/telephony/TwilioTelephonyRouter.js +150 -54
  50. package/dist/telephony/TwilioTelephonyRouter.js.map +1 -1
  51. package/dist/telephony/TwilioTelephonyService.d.ts +64 -16
  52. package/dist/telephony/TwilioTelephonyService.d.ts.map +1 -1
  53. package/dist/telephony/TwilioTelephonyService.js +151 -77
  54. package/dist/telephony/TwilioTelephonyService.js.map +1 -1
  55. package/dist/telephony/VonageTelephonyRouter.d.ts +29 -1
  56. package/dist/telephony/VonageTelephonyRouter.d.ts.map +1 -1
  57. package/dist/telephony/VonageTelephonyRouter.js +84 -72
  58. package/dist/telephony/VonageTelephonyRouter.js.map +1 -1
  59. package/dist/telephony/VonageTelephonyService.d.ts +66 -8
  60. package/dist/telephony/VonageTelephonyService.d.ts.map +1 -1
  61. package/dist/telephony/VonageTelephonyService.js +152 -67
  62. package/dist/telephony/VonageTelephonyService.js.map +1 -1
  63. package/dist/telephony/agentIdentityLookup.d.ts +30 -0
  64. package/dist/telephony/agentIdentityLookup.d.ts.map +1 -0
  65. package/dist/telephony/agentIdentityLookup.js +129 -0
  66. package/dist/telephony/agentIdentityLookup.js.map +1 -0
  67. package/dist/telephony/callEndObserver.d.ts +47 -0
  68. package/dist/telephony/callEndObserver.d.ts.map +1 -0
  69. package/dist/telephony/callEndObserver.js +95 -0
  70. package/dist/telephony/callEndObserver.js.map +1 -0
  71. package/dist/telephony/callLifecycleTracker.d.ts +83 -0
  72. package/dist/telephony/callLifecycleTracker.d.ts.map +1 -0
  73. package/dist/telephony/callLifecycleTracker.js +155 -0
  74. package/dist/telephony/callLifecycleTracker.js.map +1 -0
  75. package/dist/telephony/callerIdentity.d.ts +65 -0
  76. package/dist/telephony/callerIdentity.d.ts.map +1 -0
  77. package/dist/telephony/callerIdentity.js +72 -0
  78. package/dist/telephony/callerIdentity.js.map +1 -0
  79. package/dist/telephony/handoffDestinations.d.ts +57 -0
  80. package/dist/telephony/handoffDestinations.d.ts.map +1 -0
  81. package/dist/telephony/handoffDestinations.js +78 -0
  82. package/dist/telephony/handoffDestinations.js.map +1 -0
  83. package/dist/telephony/index.d.ts +21 -0
  84. package/dist/telephony/index.d.ts.map +1 -1
  85. package/dist/telephony/index.js +23 -0
  86. package/dist/telephony/index.js.map +1 -1
  87. package/dist/telephony/interactionLifecycle.d.ts +100 -0
  88. package/dist/telephony/interactionLifecycle.d.ts.map +1 -0
  89. package/dist/telephony/interactionLifecycle.js +338 -0
  90. package/dist/telephony/interactionLifecycle.js.map +1 -0
  91. package/dist/telephony/livekit-sip-runtime.d.ts +12 -0
  92. package/dist/telephony/livekit-sip-runtime.d.ts.map +1 -0
  93. package/dist/telephony/livekit-sip-runtime.js +21 -0
  94. package/dist/telephony/livekit-sip-runtime.js.map +1 -0
  95. package/dist/telephony/mediaSocketAuth.d.ts +77 -0
  96. package/dist/telephony/mediaSocketAuth.d.ts.map +1 -0
  97. package/dist/telephony/mediaSocketAuth.js +136 -0
  98. package/dist/telephony/mediaSocketAuth.js.map +1 -0
  99. package/dist/telephony/numberPoolSelector.d.ts +62 -0
  100. package/dist/telephony/numberPoolSelector.d.ts.map +1 -0
  101. package/dist/telephony/numberPoolSelector.js +201 -0
  102. package/dist/telephony/numberPoolSelector.js.map +1 -0
  103. package/dist/telephony/outboundCallPolicy.d.ts +226 -0
  104. package/dist/telephony/outboundCallPolicy.d.ts.map +1 -0
  105. package/dist/telephony/outboundCallPolicy.js +364 -0
  106. package/dist/telephony/outboundCallPolicy.js.map +1 -0
  107. package/dist/telephony/roomCallSession.d.ts +136 -0
  108. package/dist/telephony/roomCallSession.d.ts.map +1 -0
  109. package/dist/telephony/roomCallSession.js +359 -0
  110. package/dist/telephony/roomCallSession.js.map +1 -0
  111. package/dist/telephony/roomCallTools.d.ts +99 -0
  112. package/dist/telephony/roomCallTools.d.ts.map +1 -0
  113. package/dist/telephony/roomCallTools.js +255 -0
  114. package/dist/telephony/roomCallTools.js.map +1 -0
  115. package/dist/telephony/runAsIdentity.d.ts +62 -0
  116. package/dist/telephony/runAsIdentity.d.ts.map +1 -0
  117. package/dist/telephony/runAsIdentity.js +89 -0
  118. package/dist/telephony/runAsIdentity.js.map +1 -0
  119. package/dist/telephony/sipTrunkCarrier.d.ts +100 -0
  120. package/dist/telephony/sipTrunkCarrier.d.ts.map +1 -0
  121. package/dist/telephony/sipTrunkCarrier.js +118 -0
  122. package/dist/telephony/sipTrunkCarrier.js.map +1 -0
  123. package/dist/telephony/teamsAcsMediaRegistry.d.ts.map +1 -1
  124. package/dist/telephony/teamsAcsMediaRegistry.js +3 -2
  125. package/dist/telephony/teamsAcsMediaRegistry.js.map +1 -1
  126. package/dist/telephony/telephonyCallSession.d.ts +130 -0
  127. package/dist/telephony/telephonyCallSession.d.ts.map +1 -0
  128. package/dist/telephony/telephonyCallSession.js +315 -0
  129. package/dist/telephony/telephonyCallSession.js.map +1 -0
  130. package/dist/telephony/telephonyCallTools.d.ts +112 -0
  131. package/dist/telephony/telephonyCallTools.d.ts.map +1 -0
  132. package/dist/telephony/telephonyCallTools.js +269 -0
  133. package/dist/telephony/telephonyCallTools.js.map +1 -0
  134. package/dist/telephony/telephonyCapacity.d.ts +63 -0
  135. package/dist/telephony/telephonyCapacity.d.ts.map +1 -0
  136. package/dist/telephony/telephonyCapacity.js +94 -0
  137. package/dist/telephony/telephonyCapacity.js.map +1 -0
  138. package/dist/telephony/telephonySettings.d.ts +40 -0
  139. package/dist/telephony/telephonySettings.d.ts.map +1 -0
  140. package/dist/telephony/telephonySettings.js +69 -0
  141. package/dist/telephony/telephonySettings.js.map +1 -0
  142. package/dist/telephony/twilioMediaRegistry.d.ts +83 -11
  143. package/dist/telephony/twilioMediaRegistry.d.ts.map +1 -1
  144. package/dist/telephony/twilioMediaRegistry.js +145 -22
  145. package/dist/telephony/twilioMediaRegistry.js.map +1 -1
  146. package/dist/telephony/vonageMediaRegistry.d.ts +96 -2
  147. package/dist/telephony/vonageMediaRegistry.d.ts.map +1 -1
  148. package/dist/telephony/vonageMediaRegistry.js +227 -26
  149. package/dist/telephony/vonageMediaRegistry.js.map +1 -1
  150. package/dist/telephony/webhookParams.d.ts +21 -0
  151. package/dist/telephony/webhookParams.d.ts.map +1 -0
  152. package/dist/telephony/webhookParams.js +45 -0
  153. package/dist/telephony/webhookParams.js.map +1 -0
  154. package/dist/types.d.ts +90 -7
  155. package/dist/types.d.ts.map +1 -1
  156. package/dist/types.js.map +1 -1
  157. package/package.json +16 -13
@@ -0,0 +1,136 @@
1
+ /**
2
+ * @fileoverview Starts ONE agent's session in a LiveKit room for a call (a phone call that arrived through SIP, a call the
3
+ * server placed, a web visitor, or an agent taking a conversation over from another).
4
+ *
5
+ * It is the room-call twin of {@link TelephonyCallSessionStarter}: the same guarantees, over the LiveKit room path. The
6
+ * agent is placed in the room through the room coordinator (so it joins the way every other agent does, with the same
7
+ * roster and turn-taking), with what a call needs on top: the phone/room framing and handoff tools, a transcript that lands
8
+ * in the call's own conversation, recovery from a dropped model session, and cleanup on every path.
9
+ *
10
+ * What the starter guarantees:
11
+ * - the capacity slot, the agent-session row and the model session are released/closed on EVERY path, a failed start
12
+ * included, never leaked;
13
+ * - the **co-agent** is resolved through the shared chain and the dialled agent is the TARGET it voices, never collapsed;
14
+ * - the transcript is written to the call's own conversation, and an agent taking over writes to the SAME conversation, so a
15
+ * transfer does not split the record of the call;
16
+ * - when an agent's session ends for any reason, a handoff still waiting on it is withdrawn (unless the engine itself is
17
+ * taking the agent out, which it marks as leaving);
18
+ * - a talk-over drops queued progress narration but leaves delegated work running (the caller cancels it explicitly).
19
+ *
20
+ * @module @memberjunction/telephony-adapters
21
+ */
22
+ import { IMetadataProvider, UserInfo } from '@memberjunction/core';
23
+ import { ResolveRealtimeCoAgentID } from '@memberjunction/ai-agents';
24
+ import type { BridgeDisconnectReason } from '@memberjunction/ai-bridge-base';
25
+ import type { LiveKitAgentRoomCoordinator, RoomHandoffDeps, RoomHandoffEngine, StartedRoomAgent, StartRoomAgentRequest } from '@memberjunction/livekit-room-server';
26
+ import type { IAgentSessionManager } from '../sessionManager.js';
27
+ import { type ICallerIdentityResolver } from './callerIdentity.js';
28
+ import type { CallCapacityLease, ICallCapacity } from './telephonyCapacity.js';
29
+ import type { TransferTarget } from './outboundCallPolicy.js';
30
+ import { FindAgentNameByID, type HandoffDestinationDeps } from './handoffDestinations.js';
31
+ import { InteractionLifecycleService } from './interactionLifecycle.js';
32
+ /** The part of the room coordinator the starter drives (a `Pick` so tests inject a fake). */
33
+ export type RoomCallCoordinator = Pick<LiveKitAgentRoomCoordinator, 'StartAgentRoomSession' | 'StopAgentRoomSession'>;
34
+ /** The part of the handoff engine the starter drives. */
35
+ export type RoomCallHandoffEngine = Pick<RoomHandoffEngine, 'RequestHandoff' | 'AgentReadyToLeave' | 'CancelRoom' | 'Deps'> & {
36
+ Configure?: (deps: Partial<RoomHandoffDeps>) => void;
37
+ };
38
+ /** The collaborators the starter uses. */
39
+ export interface RoomCallStarterDeps {
40
+ Coordinator: RoomCallCoordinator;
41
+ SessionManager: IAgentSessionManager;
42
+ /** Resolves the co-agent for a target agent (defaults to the shared chain). */
43
+ CoAgentResolver: typeof ResolveRealtimeCoAgentID;
44
+ /** Resolves who an inbound phone caller is. */
45
+ CallerResolver: ICallerIdentityResolver;
46
+ Engine: RoomCallHandoffEngine;
47
+ /** The validated transfer directory. */
48
+ Targets: readonly TransferTarget[];
49
+ /** What destination resolution reads (the outbound policy, user and agent lookups). */
50
+ Destinations: HandoffDestinationDeps;
51
+ /** The concurrent-call gate an agent taking over a phone call takes a slot from. */
52
+ Capacity: ICallCapacity;
53
+ /** The display name of an agent by id (defaults to the AI engine's agent cache). */
54
+ AgentNameResolver?: typeof FindAgentNameByID;
55
+ /** Manages Interaction, InteractionEvent, and InteractionLink rows. */
56
+ InteractionLifecycle?: InteractionLifecycleService;
57
+ /** Estimated carrier cost per minute for computing CostEstimate on Interaction. */
58
+ CostPerMinute?: number;
59
+ }
60
+ /** One agent session to start in a room. */
61
+ export interface RoomCallStartArgs {
62
+ RoomName: string;
63
+ /** The agent the caller talks to (the persona). The voice front-end that speaks as it is resolved by the starter. */
64
+ Agent: {
65
+ AgentID: string;
66
+ Name?: string;
67
+ };
68
+ /** `phone` for a call that went through SIP; `web` for a person in the room from a browser. */
69
+ Channel: 'phone' | 'web';
70
+ Direction: 'Inbound' | 'Outbound';
71
+ /** The phone party's number: the caller (inbound) or the number being dialled (outbound). */
72
+ RemoteNumber?: string;
73
+ /** The number that was dialled (inbound), for the caller-identity hook. */
74
+ DialedNumber?: string;
75
+ /** The capacity slot this agent already holds; the starter owns releasing it. */
76
+ Lease?: CallCapacityLease | null;
77
+ ContextUser: UserInfo;
78
+ MetadataProvider: IMetadataProvider;
79
+ /** Who is on the other end, in words a person can use. Defaults to a masked number (phone) or `Web visitor`. */
80
+ CallerLabel?: string;
81
+ /** Set when this agent takes a conversation over from another. */
82
+ Takeover?: {
83
+ PreviousAgentName: string;
84
+ Brief: string;
85
+ };
86
+ /** Reuse an existing conversation (a taking-over agent continues the call's record). */
87
+ ConversationID?: string;
88
+ RealtimeModelID?: string;
89
+ RealtimeVoice?: string;
90
+ /** Hangs up the phone leg(s) of the call. Called when the agent ends the call; absent for a web room. */
91
+ HangUp?: () => Promise<void>;
92
+ /** Called once when this agent's session has ended, for the caller's own bookkeeping. */
93
+ OnEnded?: (reason: BridgeDisconnectReason) => void;
94
+ }
95
+ /** What a started agent session is known by. */
96
+ export interface StartedRoomCall {
97
+ SessionBridgeID: string;
98
+ AgentSessionID: string;
99
+ ConversationID?: string;
100
+ }
101
+ /** Starts agent sessions in LiveKit rooms. One instance per server. */
102
+ export declare class RoomCallSessionStarter {
103
+ private readonly deps;
104
+ private readonly rooms;
105
+ constructor(deps: RoomCallStarterDeps);
106
+ /**
107
+ * Starts the agent's session in the room. On ANY failure the lease is released and the agent-session row is closed
108
+ * before the error is rethrown; on success the lease is released when the session ends.
109
+ */
110
+ Start(args: RoomCallStartArgs): Promise<StartedRoomCall>;
111
+ /**
112
+ * Puts an agent into a room for the handoff engine: an agent taking a conversation over, or the first agent of a web
113
+ * room. The engine's `AgentStarter` seam. An agent joining a PHONE call holds its own capacity slot (it is a live model
114
+ * session); a web room is not a phone call and takes none.
115
+ */
116
+ StartRoomAgent(request: StartRoomAgentRequest): Promise<StartedRoomAgent>;
117
+ private startWithSession;
118
+ /** The tool executor; it reaches the agent's live room context lazily because the bridge exists only after the agent starts. */
119
+ private buildExecutor;
120
+ /** What the handoff engine needs to act on this agent's call. */
121
+ private agentContext;
122
+ private stopAgent;
123
+ private buildFraming;
124
+ /** Installs the room-call executor on a model session's runtime (the first one, and any re-opened after a drop). */
125
+ private bindLocalTools;
126
+ /** The caller talked over the agent: queued progress narration is stale. Delegated work is left running. */
127
+ private cancelPendingNarration;
128
+ /** End-of-session bookkeeping: free the slot, close the row, withdraw a handoff that was waiting on this agent. Never throws. */
129
+ private onSessionEnded;
130
+ private rememberRoom;
131
+ private forgetAgent;
132
+ /** Who is on the other end, in words a person can use (a masked number, never the full number). */
133
+ private callerLabel;
134
+ private resolveAgentName;
135
+ }
136
+ //# sourceMappingURL=roomCallSession.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roomCallSession.d.ts","sourceRoot":"","sources":["../../src/telephony/roomCallSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,iBAAiB,EAAuB,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAExF,OAAO,EAA+D,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAClI,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAC7E,OAAO,KAAK,EAAE,2BAA2B,EAAE,eAAe,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAC;AACpK,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAA4C,KAAK,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC7G,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AAC/E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,iBAAiB,EAA6B,KAAK,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAKrH,OAAO,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AAExE,6FAA6F;AAC7F,MAAM,MAAM,mBAAmB,GAAG,IAAI,CAAC,2BAA2B,EAAE,uBAAuB,GAAG,sBAAsB,CAAC,CAAC;AAEtH,yDAAyD;AACzD,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,iBAAiB,EAAE,gBAAgB,GAAG,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C,WAAW;YAWL,gBAAgB;CAYjC"}
@@ -0,0 +1,359 @@
1
+ /**
2
+ * @fileoverview Starts ONE agent's session in a LiveKit room for a call (a phone call that arrived through SIP, a call the
3
+ * server placed, a web visitor, or an agent taking a conversation over from another).
4
+ *
5
+ * It is the room-call twin of {@link TelephonyCallSessionStarter}: the same guarantees, over the LiveKit room path. The
6
+ * agent is placed in the room through the room coordinator (so it joins the way every other agent does, with the same
7
+ * roster and turn-taking), with what a call needs on top: the phone/room framing and handoff tools, a transcript that lands
8
+ * in the call's own conversation, recovery from a dropped model session, and cleanup on every path.
9
+ *
10
+ * What the starter guarantees:
11
+ * - the capacity slot, the agent-session row and the model session are released/closed on EVERY path, a failed start
12
+ * included, never leaked;
13
+ * - the **co-agent** is resolved through the shared chain and the dialled agent is the TARGET it voices, never collapsed;
14
+ * - the transcript is written to the call's own conversation, and an agent taking over writes to the SAME conversation, so a
15
+ * transfer does not split the record of the call;
16
+ * - when an agent's session ends for any reason, a handoff still waiting on it is withdrawn (unless the engine itself is
17
+ * taking the agent out, which it marks as leaving);
18
+ * - a talk-over drops queued progress narration but leaves delegated work running (the caller cancels it explicitly).
19
+ *
20
+ * @module @memberjunction/telephony-adapters
21
+ */
22
+ import { LogError, LogStatus } from '@memberjunction/core';
23
+ import { CreateBridgeSessionTranscriptSink, GetBridgeRealtimeRuntime } from '@memberjunction/ai-agents';
24
+ import { ResolveCallerSafely } from './callerIdentity.js';
25
+ import { FindAgentNameByID, ResolveHandoffDestination } from './handoffDestinations.js';
26
+ import { BuildRoomCallFraming, BuildRoomCallTools, ComputeHandoffCapabilities, RoomCallToolExecutor, SupportedRoomTargets } from './roomCallTools.js';
27
+ import { CloseAgentSessionRow } from './telephonyCallSession.js';
28
+ import { MaskNumber } from './outboundCallPolicy.js';
29
+ import { FindPhoneNumber } from './agentIdentityLookup.js';
30
+ import { InteractionLifecycleService } from './interactionLifecycle.js';
31
+ /** Starts agent sessions in LiveKit rooms. One instance per server. */
32
+ export class RoomCallSessionStarter {
33
+ constructor(deps) {
34
+ this.deps = deps;
35
+ this.rooms = new Map();
36
+ if (typeof this.deps.Engine.Configure === 'function') {
37
+ this.deps.Engine.Configure({
38
+ Observer: {
39
+ OnHandoffEvent: (event) => {
40
+ const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
41
+ const destinations = this.deps.Destinations;
42
+ const contextUser = event.ContextUser ?? destinations.User;
43
+ const provider = event.Provider ?? destinations.Provider;
44
+ if (contextUser && provider) {
45
+ void lifecycle.RecordRoomEvent(event.RoomName, event.EventType, contextUser, provider, event.ActorUserID, event.ActorAgentID, event.Details);
46
+ }
47
+ },
48
+ },
49
+ });
50
+ }
51
+ }
52
+ /**
53
+ * Starts the agent's session in the room. On ANY failure the lease is released and the agent-session row is closed
54
+ * before the error is rethrown; on success the lease is released when the session ends.
55
+ */
56
+ async Start(args) {
57
+ const ref = {};
58
+ let agentSessionID;
59
+ let createdInteractionID;
60
+ const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
61
+ try {
62
+ const coAgentID = await this.deps.CoAgentResolver(args.Agent.AgentID, undefined, args.ContextUser, args.MetadataProvider);
63
+ const agentName = await this.resolveAgentName(args);
64
+ const context = this.rooms.get(roomKey(args.RoomName));
65
+ const session = await this.deps.SessionManager.CreateSession({ agentID: coAgentID, userID: args.ContextUser.ID, conversationID: args.ConversationID ?? context?.ConversationID }, args.ContextUser, args.MetadataProvider);
66
+ agentSessionID = session.ID;
67
+ let interactionID = context?.InteractionID;
68
+ if (!interactionID) {
69
+ const phone = args.Channel === 'phone'
70
+ ? await FindPhoneNumber(args.DialedNumber ?? args.RemoteNumber ?? '', undefined, args.ContextUser)
71
+ : null;
72
+ const interaction = await lifecycle.CreateInteraction({
73
+ Channel: args.Channel === 'phone' ? 'Phone' : 'Web',
74
+ Direction: args.Direction,
75
+ PhoneNumberID: phone?.ID ?? null,
76
+ RemoteAddress: args.RemoteNumber ?? args.CallerLabel ?? null,
77
+ ExternalID: args.RoomName,
78
+ AgentSessionID: session.ID,
79
+ RoomName: args.RoomName,
80
+ Status: 'Active',
81
+ StartedAt: new Date(),
82
+ ContextUser: args.ContextUser,
83
+ MetadataProvider: args.MetadataProvider,
84
+ });
85
+ interactionID = interaction?.ID;
86
+ createdInteractionID = interaction?.ID;
87
+ }
88
+ else if (args.Takeover) {
89
+ await lifecycle.RecordEvent({
90
+ InteractionID: interactionID,
91
+ EventType: 'Transferred',
92
+ Details: { PreviousAgentName: args.Takeover.PreviousAgentName, Brief: args.Takeover.Brief },
93
+ ContextUser: args.ContextUser,
94
+ MetadataProvider: args.MetadataProvider,
95
+ });
96
+ }
97
+ return await this.startWithSession(args, coAgentID, agentName, session.ID, session.ConversationID ?? undefined, ref, interactionID);
98
+ }
99
+ catch (e) {
100
+ args.Lease?.Release();
101
+ if (agentSessionID) {
102
+ await CloseAgentSessionRow(agentSessionID, 'Error', args.ContextUser, args.MetadataProvider);
103
+ }
104
+ if (createdInteractionID) {
105
+ await lifecycle.CloseInteraction({
106
+ InteractionID: createdInteractionID,
107
+ EndReason: 'Error',
108
+ ContextUser: args.ContextUser,
109
+ MetadataProvider: args.MetadataProvider,
110
+ });
111
+ }
112
+ await closeQuietly(ref.Session);
113
+ throw e;
114
+ }
115
+ }
116
+ /**
117
+ * Puts an agent into a room for the handoff engine: an agent taking a conversation over, or the first agent of a web
118
+ * room. The engine's `AgentStarter` seam. An agent joining a PHONE call holds its own capacity slot (it is a live model
119
+ * session); a web room is not a phone call and takes none.
120
+ */
121
+ async StartRoomAgent(request) {
122
+ const context = this.rooms.get(roomKey(request.RoomName));
123
+ const channel = context?.Channel ?? 'web';
124
+ const lease = channel === 'phone' ? this.deps.Capacity.TryAcquire() : null;
125
+ if (channel === 'phone' && !lease) {
126
+ throw new Error('All agent lines are busy right now.');
127
+ }
128
+ const started = await this.Start({
129
+ RoomName: request.RoomName,
130
+ Agent: { AgentID: request.AgentID, Name: request.AgentName },
131
+ Channel: channel,
132
+ Direction: context?.Direction ?? 'Inbound',
133
+ RemoteNumber: context?.RemoteNumber,
134
+ Lease: lease,
135
+ ContextUser: request.ContextUser,
136
+ MetadataProvider: request.Provider,
137
+ CallerLabel: request.CallerLabel,
138
+ Takeover: request.Brief ? { PreviousAgentName: request.PreviousAgentName ?? 'the previous agent', Brief: request.Brief } : undefined,
139
+ ConversationID: context?.ConversationID,
140
+ RealtimeModelID: request.RealtimeModelID,
141
+ RealtimeVoice: request.RealtimeVoice,
142
+ HangUp: context?.HangUp,
143
+ });
144
+ return { SessionBridgeID: started.SessionBridgeID };
145
+ }
146
+ async startWithSession(args, coAgentID, agentName, agentSessionID, conversationID, ref, interactionID) {
147
+ const supported = SupportedRoomTargets(this.deps.Targets, ComputeHandoffCapabilities(this.deps.Engine.Deps));
148
+ const callerLabel = this.callerLabel(args);
149
+ const executor = this.buildExecutor(args, agentName, callerLabel, supported, ref);
150
+ const framing = await this.buildFraming(args, supported, interactionID);
151
+ const started = await this.deps.Coordinator.StartAgentRoomSession({
152
+ AgentSessionID: agentSessionID,
153
+ RoomName: args.RoomName,
154
+ AgentID: coAgentID,
155
+ AgentName: agentName,
156
+ TargetAgentID: args.Agent.AgentID,
157
+ RealtimeModelID: args.RealtimeModelID,
158
+ RealtimeVoice: args.RealtimeVoice,
159
+ // A call is a conversation the agent always answers, not a "passive" meeting seat.
160
+ TurnMode: 'Active',
161
+ ContextUser: args.ContextUser,
162
+ MetadataProvider: args.MetadataProvider,
163
+ Host: {
164
+ HostTools: BuildRoomCallTools(supported),
165
+ HostFraming: framing,
166
+ ConversationID: conversationID,
167
+ OnModelSession: (session) => this.bindLocalTools(session, executor, ref),
168
+ TranscriptSink: conversationID
169
+ ? CreateBridgeSessionTranscriptSink({ ConversationID: conversationID, AgentSessionID: agentSessionID, AgentID: args.Agent.AgentID })
170
+ : undefined,
171
+ // Same host policy as the carrier path: a talk-over drops queued narration but does not abort delegated work.
172
+ OnBargeIn: () => this.cancelPendingNarration(ref),
173
+ RecoverModelSession: true,
174
+ OnSessionEnded: (reason) => this.onSessionEnded(args, agentSessionID, reason),
175
+ JoinMethod: args.Channel === 'phone' && args.Direction === 'Inbound' ? 'InboundRoute' : 'OnDemand',
176
+ Direction: args.Direction,
177
+ },
178
+ });
179
+ ref.BridgeID = started.SessionBridgeID;
180
+ this.rememberRoom(args, callerLabel, conversationID, interactionID);
181
+ LogStatus(`[Telephony] room call session ${started.SessionBridgeID} started in ${args.RoomName} (agent session ${agentSessionID}).`);
182
+ return { SessionBridgeID: started.SessionBridgeID, AgentSessionID: agentSessionID, ConversationID: conversationID };
183
+ }
184
+ /** The tool executor; it reaches the agent's live room context lazily because the bridge exists only after the agent starts. */
185
+ buildExecutor(args, agentName, callerLabel, supported, ref) {
186
+ return new RoomCallToolExecutor({
187
+ Targets: supported,
188
+ Engine: this.deps.Engine,
189
+ Agent: () => (ref.BridgeID ? this.agentContext(args, agentName, callerLabel, ref) : undefined),
190
+ ResolveDestination: (target) => ResolveHandoffDestination(target, { User: args.ContextUser, Provider: args.MetadataProvider }, this.deps.Destinations),
191
+ CancelPendingWork: () => (ref.Session ? GetBridgeRealtimeRuntime(ref.Session)?.CancelInFlightDelegations() ?? 0 : 0),
192
+ EndCall: async (reason) => {
193
+ LogStatus(`[Telephony] agent ended room call ${args.RoomName}: ${reason}`);
194
+ await this.stopAgent(args, ref);
195
+ await args.HangUp?.();
196
+ },
197
+ });
198
+ }
199
+ /** What the handoff engine needs to act on this agent's call. */
200
+ agentContext(args, agentName, callerLabel, ref) {
201
+ return {
202
+ RoomName: args.RoomName,
203
+ AgentName: agentName,
204
+ CallerLabel: callerLabel,
205
+ ContextUser: args.ContextUser,
206
+ Provider: args.MetadataProvider,
207
+ LeaveRoom: () => this.stopAgent(args, ref),
208
+ NotifyModel: (note) => ref.Session?.SendContextNote?.(note),
209
+ };
210
+ }
211
+ async stopAgent(args, ref) {
212
+ if (ref.BridgeID) {
213
+ await this.deps.Coordinator.StopAgentRoomSession(ref.BridgeID, 'Explicit', args.ContextUser, args.MetadataProvider);
214
+ }
215
+ }
216
+ async buildFraming(args, supported, interactionID) {
217
+ const caller = args.Channel === 'phone' && args.Direction === 'Inbound' && args.RemoteNumber
218
+ ? await ResolveCallerSafely(this.deps.CallerResolver, args.RemoteNumber, args.DialedNumber ?? '', args.ContextUser)
219
+ : undefined;
220
+ if (caller && interactionID) {
221
+ const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
222
+ if (caller.LinkedRecord) {
223
+ void lifecycle.CreateLink({
224
+ InteractionID: interactionID,
225
+ EntityID: caller.LinkedRecord.EntityID,
226
+ RecordID: caller.LinkedRecord.RecordID,
227
+ Role: 'Caller',
228
+ ContextUser: args.ContextUser,
229
+ MetadataProvider: args.MetadataProvider,
230
+ });
231
+ }
232
+ else if (caller.PersonRecordID && caller.PersonEntityID) {
233
+ void lifecycle.CreateLink({
234
+ InteractionID: interactionID,
235
+ EntityID: caller.PersonEntityID,
236
+ RecordID: caller.PersonRecordID,
237
+ Role: 'Caller',
238
+ ContextUser: args.ContextUser,
239
+ MetadataProvider: args.MetadataProvider,
240
+ });
241
+ }
242
+ }
243
+ return BuildRoomCallFraming({
244
+ Channel: args.Channel,
245
+ Direction: args.Direction,
246
+ RemoteNumber: args.RemoteNumber,
247
+ Caller: caller,
248
+ SupportedTargets: supported,
249
+ Takeover: args.Takeover,
250
+ });
251
+ }
252
+ /** Installs the room-call executor on a model session's runtime (the first one, and any re-opened after a drop). */
253
+ bindLocalTools(session, executor, ref) {
254
+ ref.Session = session;
255
+ const runtime = GetBridgeRealtimeRuntime(session);
256
+ if (!runtime) {
257
+ LogError('[Telephony] the model session has no bridge runtime; transfer_call / finish_handoff / end_call will not work on this call.');
258
+ return;
259
+ }
260
+ runtime.SetLocalToolHandler(executor);
261
+ }
262
+ /** The caller talked over the agent: queued progress narration is stale. Delegated work is left running. */
263
+ cancelPendingNarration(ref) {
264
+ if (ref.Session) {
265
+ GetBridgeRealtimeRuntime(ref.Session)?.CancelPendingNarration();
266
+ }
267
+ }
268
+ /** End-of-session bookkeeping: free the slot, close the row, withdraw a handoff that was waiting on this agent. Never throws. */
269
+ async onSessionEnded(args, agentSessionID, reason) {
270
+ args.Lease?.Release();
271
+ const key = roomKey(args.RoomName);
272
+ const context = this.rooms.get(key);
273
+ const isLastAgent = !context || context.LiveAgents <= 1;
274
+ const interactionID = context?.InteractionID;
275
+ this.forgetAgent(args.RoomName);
276
+ this.deps.Engine.CancelRoom(args.RoomName);
277
+ await CloseAgentSessionRow(agentSessionID, reason, args.ContextUser, args.MetadataProvider);
278
+ if (isLastAgent && interactionID) {
279
+ const lifecycle = this.deps.InteractionLifecycle ?? InteractionLifecycleService.Instance;
280
+ await lifecycle.CloseInteraction({
281
+ InteractionID: interactionID,
282
+ EndReason: reason,
283
+ CostPerMinute: this.deps.CostPerMinute,
284
+ ContextUser: args.ContextUser,
285
+ MetadataProvider: args.MetadataProvider,
286
+ });
287
+ }
288
+ try {
289
+ args.OnEnded?.(reason);
290
+ }
291
+ catch (e) {
292
+ LogError(`[Telephony] a room call's end hook failed: ${e instanceof Error ? e.message : String(e)}`);
293
+ }
294
+ }
295
+ rememberRoom(args, callerLabel, conversationID, interactionID) {
296
+ const key = roomKey(args.RoomName);
297
+ const existing = this.rooms.get(key);
298
+ if (existing) {
299
+ existing.LiveAgents++;
300
+ if (!existing.InteractionID && interactionID) {
301
+ existing.InteractionID = interactionID;
302
+ }
303
+ return;
304
+ }
305
+ this.rooms.set(key, {
306
+ Channel: args.Channel,
307
+ Direction: args.Direction,
308
+ RemoteNumber: args.RemoteNumber,
309
+ ConversationID: conversationID,
310
+ CallerLabel: callerLabel,
311
+ HangUp: args.HangUp,
312
+ InteractionID: interactionID,
313
+ LiveAgents: 1,
314
+ });
315
+ }
316
+ forgetAgent(roomName) {
317
+ const key = roomKey(roomName);
318
+ const context = this.rooms.get(key);
319
+ if (context && --context.LiveAgents <= 0) {
320
+ this.rooms.delete(key);
321
+ }
322
+ }
323
+ /** Who is on the other end, in words a person can use (a masked number, never the full number). */
324
+ callerLabel(args) {
325
+ const existing = this.rooms.get(roomKey(args.RoomName));
326
+ if (existing) {
327
+ return existing.CallerLabel;
328
+ }
329
+ if (args.CallerLabel) {
330
+ return args.CallerLabel;
331
+ }
332
+ return args.Channel === 'phone' && args.RemoteNumber ? `Phone caller ${MaskNumber(args.RemoteNumber)}` : 'Web visitor';
333
+ }
334
+ async resolveAgentName(args) {
335
+ if (args.Agent.Name) {
336
+ return args.Agent.Name;
337
+ }
338
+ try {
339
+ const find = this.deps.AgentNameResolver ?? FindAgentNameByID;
340
+ return (await find(args.Agent.AgentID, args.ContextUser, args.MetadataProvider)) ?? 'Agent';
341
+ }
342
+ catch (e) {
343
+ LogError(`[Telephony] could not resolve the agent's display name; using a generic one: ${e instanceof Error ? e.message : String(e)}`);
344
+ return 'Agent';
345
+ }
346
+ }
347
+ }
348
+ function roomKey(roomName) {
349
+ return roomName.trim().toLowerCase();
350
+ }
351
+ async function closeQuietly(session) {
352
+ try {
353
+ await session?.Close();
354
+ }
355
+ catch (e) {
356
+ LogError(`[Telephony] closing the model session after a failed start failed: ${e instanceof Error ? e.message : String(e)}`);
357
+ }
358
+ }
359
+ //# sourceMappingURL=roomCallSession.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roomCallSession.js","sourceRoot":"","sources":["../../src/telephony/roomCallSession.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAqB,QAAQ,EAAE,SAAS,EAAY,MAAM,sBAAsB,CAAC;AAExF,OAAO,EAAE,iCAAiC,EAAE,wBAAwB,EAA4B,MAAM,2BAA2B,CAAC;AAIlI,OAAO,EAAE,mBAAmB,EAAqD,MAAM,qBAAqB,CAAC;AAG7G,OAAO,EAAE,iBAAiB,EAAE,yBAAyB,EAA+B,MAAM,0BAA0B,CAAC;AACrH,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,0BAA0B,EAAE,oBAAoB,E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AAI,OAAO,CAAC;QAChG,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACT,QAAQ,CAAC,gFAAgF,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;YACvI,OAAO,OAAO,CAAC;QACnB,CAAC;IACL,CAAC;CACJ;AAED,SAAS,OAAO,CAAC,QAAgB;IAC7B,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;AACzC,CAAC;AAED,KAAK,UAAU,YAAY,CAAC,OAAqC;IAC7D,IAAI,CAAC;QACD,MAAM,OAAO,EAAE,KAAK,EAAE,CAAC;IAC3B,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACT,QAAQ,CAAC,sEAAsE,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IACjI,CAAC;AACL,CAAC"}
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @fileoverview What an agent in a LiveKit ROOM call is told, and the tools it can use to bring someone else in.
3
+ *
4
+ * A carrier call can only be transferred to a phone number: the carrier moves the call and the agent is gone. A room call
5
+ * is different. The caller is a participant in a room the agent is also in, so "transfer" means *bring someone into the
6
+ * room*: a person at an Explorer console (who may accept or decline), a phone number the room dials, or another AI agent.
7
+ * The agent leaves only once the new party is actually there, and the caller never changes rooms.
8
+ *
9
+ * Tools offered (the host executes them; arguments come from the model, so they are validated here):
10
+ * - `transfer_call`: names an entry of the operator's transfer directory plus a `mode` (`warm` or `blind`) and a short
11
+ * `summary` of the conversation. Never a number, an email or an agent id: the directory is what keeps a caller from
12
+ * steering the agent to an arbitrary destination.
13
+ * - `finish_handoff`: called after the agent has introduced the new party, so it can leave.
14
+ * - `cancel_pending_work` and `end_call`: the same as on a carrier call.
15
+ *
16
+ * The model is told what happens next through `[handoff]` messages the engine sends (the person accepted, declined, did
17
+ * not answer, has joined), so the instructions here describe those and what to do on each.
18
+ *
19
+ * @module @memberjunction/telephony-adapters
20
+ */
21
+ import type { RealtimeToolCall, RealtimeToolDefinition } from '@memberjunction/ai';
22
+ import type { BridgeLocalToolHandler } from '@memberjunction/ai-agents';
23
+ import { type RoomHandoffAgentContext, type RoomHandoffDeps, type RoomHandoffEngine } from '@memberjunction/livekit-room-server';
24
+ import type { CallerIdentity } from './callerIdentity.js';
25
+ import { type TransferTarget } from './outboundCallPolicy.js';
26
+ import type { ResolvedDestination } from './handoffDestinations.js';
27
+ /** Which kinds of destination this server can actually bring into a room (each needs a different collaborator). */
28
+ export interface RoomHandoffCapabilities {
29
+ /** A phone number can be dialed into the room (needs a LiveKit outbound trunk). */
30
+ Number: boolean;
31
+ /** A person can be offered the conversation and watched into the room. */
32
+ User: boolean;
33
+ /** Another AI agent can be started in the room. */
34
+ Agent: boolean;
35
+ }
36
+ /** Works out what the handoff engine can do from the collaborators it has been given. */
37
+ export declare function ComputeHandoffCapabilities(deps: Readonly<RoomHandoffDeps>): RoomHandoffCapabilities;
38
+ /** The directory entries this server can act on. An entry whose kind is unsupported is not offered to the model. */
39
+ export declare function SupportedRoomTargets(targets: readonly TransferTarget[], capabilities: RoomHandoffCapabilities): TransferTarget[];
40
+ /**
41
+ * The tools to offer in a room call. `end_call` and `cancel_pending_work` always; `transfer_call` and `finish_handoff` only
42
+ * when there is at least one directory entry the server can act on.
43
+ */
44
+ export declare function BuildRoomCallTools(supportedTargets: readonly TransferTarget[]): RealtimeToolDefinition[];
45
+ /** What the framing needs to know about the call. */
46
+ export interface RoomCallFramingInput {
47
+ /** `phone` for a call that arrived through (or was placed through) SIP; `web` for a person who joined the room from a browser. */
48
+ Channel: 'phone' | 'web';
49
+ Direction: 'Inbound' | 'Outbound';
50
+ /** The other party's number (phone calls). */
51
+ RemoteNumber?: string;
52
+ /** What the host knows about an inbound phone caller. */
53
+ Caller?: CallerIdentity;
54
+ /** The directory entries the agent can hand over to (supported ones only). */
55
+ SupportedTargets: readonly TransferTarget[];
56
+ /** Set when this agent is taking a conversation over from another. */
57
+ Takeover?: {
58
+ PreviousAgentName: string;
59
+ Brief: string;
60
+ };
61
+ }
62
+ /**
63
+ * The instructions that make the model behave correctly in a room call: phone etiquette (or web-room etiquette), who is on
64
+ * the other end, how to hand over and what the `[handoff]` messages mean, and, for an agent that is taking over, what it has
65
+ * been told so far. Appended to the agent's own prompt.
66
+ */
67
+ export declare function BuildRoomCallFraming(input: RoomCallFramingInput): string;
68
+ /** What the executor needs. */
69
+ export interface RoomCallToolExecutorDeps {
70
+ /** The directory entries the agent can hand over to (supported ones only). */
71
+ Targets: readonly TransferTarget[];
72
+ /** The handoff engine (a `Pick` so tests inject a fake). */
73
+ Engine: Pick<RoomHandoffEngine, 'RequestHandoff' | 'AgentReadyToLeave'>;
74
+ /** The agent's room context. Resolved lazily: the bridge session exists only after the agent has started. */
75
+ Agent: () => RoomHandoffAgentContext | undefined;
76
+ /** Resolves a directory entry to a destination, checking everything that can change while the server runs. */
77
+ ResolveDestination: (target: TransferTarget) => Promise<ResolvedDestination>;
78
+ /** Aborts the delegated work in flight; returns how many runs were aborted. */
79
+ CancelPendingWork: () => number;
80
+ /** Ends the conversation (stops this agent and hangs up the phone leg, if any). */
81
+ EndCall: (reason: string) => Promise<void>;
82
+ /** Override for the goodbye settle delay (tests). */
83
+ SettleMs?: number;
84
+ }
85
+ /** Runs the room-call tools against the live room. */
86
+ export declare class RoomCallToolExecutor implements BridgeLocalToolHandler {
87
+ private readonly deps;
88
+ private ending;
89
+ constructor(deps: RoomCallToolExecutorDeps);
90
+ /** Whether `toolName` is one of the room-call tools. */
91
+ Handles(toolName: string): boolean;
92
+ /** Runs one tool call and returns the JSON the model is handed back. Never throws. */
93
+ Execute(call: RealtimeToolCall): Promise<string>;
94
+ private transfer;
95
+ private finishHandoff;
96
+ private cancelPendingWork;
97
+ private endCall;
98
+ }
99
+ //# sour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