-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathllm-plugin.ts
More file actions
298 lines (230 loc) · 9.23 KB
/
Copy pathllm-plugin.ts
File metadata and controls
298 lines (230 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* eslint-disable require-jsdoc */
import {WebexPlugin} from '@webex/webex-core';
import LLMChannel, {config} from './llm';
import {DATA_CHANNEL_WITH_JWT_TOKEN, LLM_DEFAULT_SESSION} from './constants';
import {DataChannelTokenType} from './llm.types';
/**
* LLMPlugin — registered as `webex.internal.llm`.
*
* Maintains a Map<sessionId, LLMChannel> so multiple simultaneous LLM
* connections (e.g. default session + practice session) can coexist.
* All existing callers continue to work unchanged via the session-keyed API.
*
* sessionId values are the LLM_DEFAULT_SESSION / LLM_PRACTICE_SESSION constants,
* which match the DataChannelTokenType enum values so token keys and session keys
* are the same namespace.
*/
export class LLMPlugin extends (WebexPlugin as any) {
namespace = 'llm';
private sessions = new Map<string, LLMChannel>();
private getOrCreateSession(sessionId: string): LLMChannel {
let channel = this.sessions.get(sessionId);
if (!channel) {
// @ts-ignore — WebexPlugin children require {parent: this.webex}
channel = new LLMChannel({parent: this.webex});
// Forward all events emitted by the channel up through the plugin so that
// callers doing llm.on('event:relay.event', ...) or llm.on('online', ...)
// receive events from whichever session channel emits them.
// Non-default sessions emit events with :<sessionId> suffix so that
// practice-session consumers can subscribe to session-specific names
// like `event:relay.event:llm-practice-session`.
channel.on('all', (eventName: string, ...args: any[]) => {
if (sessionId === LLM_DEFAULT_SESSION) {
this.trigger(eventName, ...args);
} else {
this.trigger(`${eventName}:${sessionId}`, ...args);
}
});
this.sessions.set(sessionId, channel);
}
return channel;
}
private getSession(sessionId: string): LLMChannel | undefined {
return this.sessions.get(sessionId);
}
// Before webex.internal.llm was LLChannel instance which extended Mercury so it was directly available
get hasEverConnected(): boolean {
for (const ch of this.sessions.values()) {
if (ch.hasEverConnected) return true;
}
return false;
}
public registerAndConnect(
locusUrl: string,
datachannelUrl: string,
datachannelToken?: string,
sessionId: string = LLM_DEFAULT_SESSION
): Promise<void> {
const channel = this.getOrCreateSession(sessionId);
return channel.registerAndConnect(locusUrl, datachannelUrl, datachannelToken);
}
public disconnectLLM(
options: {code: number; reason: string},
// eslint-disable-next-line default-param-last
sessionId: string = LLM_DEFAULT_SESSION,
ownerMeetingId?: string
): Promise<boolean | void> {
const channel = this.getSession(sessionId);
if (!channel) return Promise.resolve();
const {isOwner} = this.resolveSessionOwnership(ownerMeetingId, sessionId);
if (!isOwner) {
this.logger.info(`llm#disconnectLLM --> skipping, not owner of session ${sessionId}`);
return Promise.resolve(false);
}
return channel.disconnect(options).then(() => {
this.sessions.delete(sessionId);
return true;
});
}
public disconnectAllLLM(options?: {code: number; reason: string}): Promise<void> {
const promises = Array.from(this.sessions.entries()).map(([sessionId, channel]) =>
channel.disconnect(options).then(() => this.sessions.delete(sessionId))
);
return Promise.all(promises).then(() => undefined);
}
public isConnected(sessionId: string = LLM_DEFAULT_SESSION): boolean {
const session = this.getSession(sessionId);
const connected = session?.isConnected() ?? false;
return connected;
}
public getBinding(sessionId: string = LLM_DEFAULT_SESSION): string | undefined {
return this.getSession(sessionId)?.getBinding();
}
public getSocket(sessionId: string = LLM_DEFAULT_SESSION): any {
return this.getSession(sessionId)?.socket;
}
// Backwards-compatibility: callers that access llm.socket directly
// (e.g. voicea's getPublishTransport) get the default session socket.
get socket(): any {
return this.getSocket(LLM_DEFAULT_SESSION);
}
public getLocusUrl(sessionId: string = LLM_DEFAULT_SESSION): string | undefined {
return this.getSession(sessionId)?.getLocusUrl();
}
public getDatachannelUrl(sessionId: string = LLM_DEFAULT_SESSION): string | undefined {
return this.getSession(sessionId)?.getDatachannelUrl();
}
// tokenKey IS the sessionId (DataChannelTokenType enum values equal LLM_*_SESSION constants)
public getDatachannelToken(
// eslint-disable-next-line default-param-last
tokenKey: string = LLM_DEFAULT_SESSION,
ownerMeetingId?: string
): string | undefined {
const channel = this.getSession(tokenKey);
if (!channel) return undefined;
const {isOwner, currentOwner} = this.resolveSessionOwnership(ownerMeetingId, tokenKey);
if (!isOwner) {
this.logger.info(
`llm#getDatachannelToken --> skip read for session ${tokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
);
return undefined;
}
return channel.getDatachannelToken();
}
public setDatachannelToken(
datachannelToken: string,
ownerMeetingId?: string,
tokenKey: string = LLM_DEFAULT_SESSION
): void {
const channel = this.getOrCreateSession(tokenKey);
const {isOwner, currentOwner} = this.resolveSessionOwnership(ownerMeetingId, tokenKey);
if (!isOwner) {
this.logger.info(
`llm#setDatachannelToken --> skip write for session ${tokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
);
return;
}
channel.setDatachannelToken(datachannelToken);
}
public clearDatachannelToken(tokenKey: string, ownerMeetingId: string): void {
const channel = this.getSession(tokenKey);
if (!channel) return;
const {isOwner, currentOwner} = this.resolveSessionOwnership(ownerMeetingId, tokenKey);
if (!isOwner) {
this.logger.info(
`llm#clearDatachannelToken --> skip clear for session ${tokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
);
return;
}
channel.clearDatachannelToken();
}
public setRefreshHandler(
handler: () => Promise<{
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
}>,
ownerMeetingId?: string,
sessionId: string = LLM_DEFAULT_SESSION
): void {
const channel = this.getOrCreateSession(sessionId);
const {isOwner, currentOwner} = this.resolveSessionOwnership(ownerMeetingId, sessionId);
if (!isOwner) {
this.logger.info(
`llm#setRefreshHandler --> skip write for session ${sessionId}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
);
return;
}
channel.setRefreshHandler(handler);
}
public refreshDataChannelToken(sessionId: string = LLM_DEFAULT_SESSION): Promise<any> {
const channel = this.getSession(sessionId);
if (!channel) {
this.logger.warn(`llm#refreshDataChannelToken --> no channel for session ${sessionId}`);
return Promise.resolve(null);
}
return channel.refreshDataChannelToken();
}
public setOwnerMeetingId(
ownerMeetingId: string | undefined,
sessionId: string = LLM_DEFAULT_SESSION
): void {
const channel = this.getSession(sessionId);
if (channel) channel.ownerMeetingId = ownerMeetingId;
}
public getOwnerMeetingId(sessionId: string = LLM_DEFAULT_SESSION): string | undefined {
return this.getSession(sessionId)?.ownerMeetingId;
}
public resolveSessionOwnership(
ownerMeetingId?: string,
sessionId: string = LLM_DEFAULT_SESSION
): {currentOwner: string | undefined; isOwner: boolean} {
const currentOwner = this.getOwnerMeetingId(sessionId);
const isOwner = !currentOwner || !ownerMeetingId || currentOwner === ownerMeetingId;
return {currentOwner, isOwner};
}
public getConnectionByDatachannelUrl(url: string): LLMChannel | undefined {
for (const channel of this.sessions.values()) {
const datachannelUrl = channel.getDatachannelUrl();
if (datachannelUrl && LLMChannel.matchesDatachannelRequestUrl(url, datachannelUrl)) {
return channel;
}
}
return undefined;
}
public getLocusUrlByDatachannelUrl(requestUrl: string): string | undefined {
for (const channel of this.sessions.values()) {
const datachannelUrl = channel.getDatachannelUrl();
if (datachannelUrl && LLMChannel.matchesDatachannelRequestUrl(requestUrl, datachannelUrl)) {
return channel.getLocusUrl();
}
}
return undefined;
}
public getSessionIdByDatachannelUrl(requestUrl: string): string | undefined {
for (const [sessionId, channel] of this.sessions.entries()) {
const datachannelUrl = channel.getDatachannelUrl();
if (datachannelUrl && LLMChannel.matchesDatachannelRequestUrl(requestUrl, datachannelUrl)) {
return sessionId;
}
}
return undefined;
}
public isDataChannelTokenEnabled(): Promise<boolean> {
// @ts-ignore
return this.webex.internal.feature.getFeature('developer', DATA_CHANNEL_WITH_JWT_TOKEN);
}
public getAllConnections(): Map<string, LLMChannel> {
return new Map(this.sessions);
}
}
export {config};
export default LLMPlugin;