@webex/internal-plugin-llm 3.12.0-next.67 → 3.12.0-next.68
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/README.md +45 -65
- package/dist/index.js +17 -6
- package/dist/index.js.map +1 -1
- package/dist/llm-plugin.js +199 -0
- package/dist/llm-plugin.js.map +1 -0
- package/dist/llm.js +193 -411
- package/dist/llm.js.map +1 -1
- package/dist/llm.types.js +8 -0
- package/dist/llm.types.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +7 -2
- package/src/llm-plugin.ts +115 -0
- package/src/llm.ts +156 -443
- package/src/llm.types.ts +68 -50
- package/test/unit/spec/llm-plugin.js +391 -0
- package/test/unit/spec/llm.js +440 -567
package/src/llm.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
/* eslint-disable consistent-return */
|
|
2
|
-
|
|
3
2
|
import Mercury from '@webex/internal-plugin-mercury';
|
|
4
3
|
|
|
5
|
-
// eslint-disable-next-line no-unused-vars
|
|
6
4
|
import {
|
|
7
5
|
LLM,
|
|
8
6
|
DATA_CHANNEL_WITH_JWT_TOKEN,
|
|
9
7
|
AWARE_DATA_CHANNEL,
|
|
10
8
|
SUBSCRIPTION_AWARE_SUBCHANNELS_PARAM,
|
|
11
|
-
LLM_DEFAULT_SESSION,
|
|
12
9
|
} from './constants';
|
|
13
10
|
import {ILLMChannel, DataChannelTokenType, RegisterAndConnectTiming} from './llm.types';
|
|
14
11
|
|
|
@@ -44,50 +41,45 @@ export const config = {
|
|
|
44
41
|
};
|
|
45
42
|
|
|
46
43
|
/**
|
|
47
|
-
* LLMChannel to
|
|
44
|
+
* LLMChannel — a single WebSocket connection to the LLM data channel.
|
|
45
|
+
*
|
|
46
|
+
* Created via `webex.internal.llm.createChannel()`. The caller owns the
|
|
47
|
+
* channel and is responsible for its lifecycle (connect, disconnect, cleanup).
|
|
48
|
+
* Multiple LLMChannels can exist simultaneously for different meetings.
|
|
48
49
|
*/
|
|
49
50
|
export default class LLMChannel extends (Mercury as any) implements ILLMChannel {
|
|
50
51
|
namespace = LLM;
|
|
51
|
-
|
|
52
|
+
|
|
53
|
+
private webSocketUrl?: string;
|
|
54
|
+
private binding?: string;
|
|
55
|
+
private locusUrl?: string;
|
|
56
|
+
private datachannelUrl?: string;
|
|
57
|
+
private datachannelToken?: string;
|
|
58
|
+
private refreshHandler?: () => Promise<{
|
|
59
|
+
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
|
|
60
|
+
}>;
|
|
61
|
+
|
|
62
|
+
/** In-flight connection promise for deduplication. */
|
|
63
|
+
private connectingPromise?: Promise<RegisterAndConnectTiming | void>;
|
|
64
|
+
|
|
52
65
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @private
|
|
56
|
-
* @type {Map<string, {webSocketUrl?: string; binding?: string; locusUrl?: string; datachannelUrl?: string}>}
|
|
66
|
+
* Check if a connection is currently in progress.
|
|
67
|
+
* @returns {boolean} True if connecting
|
|
57
68
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
webSocketUrl?: string;
|
|
62
|
-
binding?: string;
|
|
63
|
-
locusUrl?: string;
|
|
64
|
-
datachannelUrl?: string;
|
|
65
|
-
ownerMeetingId?: string;
|
|
66
|
-
refreshHandler?: () => Promise<{
|
|
67
|
-
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
|
|
68
|
-
}>;
|
|
69
|
-
}
|
|
70
|
-
> = new Map();
|
|
71
|
-
|
|
72
|
-
// Session-keyed token cache is intentionally decoupled from connection state.
|
|
73
|
-
// Disconnecting a socket session must not implicitly wipe token cache.
|
|
74
|
-
private datachannelTokens: Record<string, string | undefined> = {
|
|
75
|
-
[DataChannelTokenType.Default]: undefined,
|
|
76
|
-
[DataChannelTokenType.PracticeSession]: undefined,
|
|
77
|
-
};
|
|
69
|
+
public isConnecting(): boolean {
|
|
70
|
+
return !!this.connectingPromise;
|
|
71
|
+
}
|
|
78
72
|
|
|
79
73
|
/**
|
|
80
74
|
* Register to the websocket
|
|
81
75
|
* @param {string} llmSocketUrl
|
|
82
76
|
* @param {string} datachannelToken
|
|
83
|
-
* @
|
|
84
|
-
* @returns {Promise<void>}
|
|
77
|
+
* @returns {Promise<object>} Promise resolving to {webSocketUrl, binding}
|
|
85
78
|
*/
|
|
86
79
|
private register = async (
|
|
87
80
|
llmSocketUrl: string,
|
|
88
|
-
datachannelToken?: string
|
|
89
|
-
|
|
90
|
-
): Promise<void> => {
|
|
81
|
+
datachannelToken?: string
|
|
82
|
+
): Promise<{webSocketUrl: string; binding: string}> => {
|
|
91
83
|
const isDataChannelTokenEnabled = await this.isDataChannelTokenEnabled();
|
|
92
84
|
|
|
93
85
|
return this.request({
|
|
@@ -99,364 +91,178 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
99
91
|
? {'Data-Channel-Auth-Token': datachannelToken}
|
|
100
92
|
: {},
|
|
101
93
|
})
|
|
102
|
-
.then((res: {body: {webSocketUrl: string; binding: string}}) =>
|
|
103
|
-
// Get or create connection data
|
|
104
|
-
const sessionData = this.connections.get(sessionId) || {};
|
|
105
|
-
sessionData.webSocketUrl = res.body.webSocketUrl;
|
|
106
|
-
sessionData.binding = res.body.binding;
|
|
107
|
-
this.connections.set(sessionId, sessionData);
|
|
108
|
-
})
|
|
94
|
+
.then((res: {body: {webSocketUrl: string; binding: string}}) => res.body)
|
|
109
95
|
.catch((error: any) => {
|
|
110
|
-
this.logger.error(`Error connecting to websocket for
|
|
96
|
+
this.logger.error(`Error connecting to websocket for : ${error}`);
|
|
111
97
|
throw error;
|
|
112
98
|
});
|
|
113
99
|
};
|
|
114
100
|
|
|
115
101
|
/**
|
|
116
|
-
* Register and connect to the websocket
|
|
102
|
+
* Register and connect to the websocket.
|
|
103
|
+
* Handles deduplication: returns existing promise if connection in progress,
|
|
104
|
+
* or resolves immediately if already connected to the same URLs.
|
|
117
105
|
* @param {string} locusUrl
|
|
118
106
|
* @param {string} datachannelUrl
|
|
119
107
|
* @param {string} datachannelToken
|
|
120
|
-
* @
|
|
121
|
-
* @returns {Promise<void>}
|
|
108
|
+
* @returns {Promise<RegisterAndConnectTiming | void>}
|
|
122
109
|
*/
|
|
123
110
|
public registerAndConnect = (
|
|
124
111
|
locusUrl: string,
|
|
125
112
|
datachannelUrl: string,
|
|
126
|
-
datachannelToken?: string
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
// Pre-populate locusUrl and datachannelUrl before register() fires the
|
|
132
|
-
// HTTP POST, so that any token refresh triggered during registration can
|
|
133
|
-
// be routed via connections without falling back to a locusInfo URL scan.
|
|
134
|
-
if (locusUrl && datachannelUrl) {
|
|
135
|
-
const sessionData = this.connections.get(sessionId) || {};
|
|
136
|
-
sessionData.locusUrl = locusUrl;
|
|
137
|
-
sessionData.datachannelUrl = datachannelUrl;
|
|
138
|
-
this.connections.set(sessionId, sessionData);
|
|
113
|
+
datachannelToken?: string
|
|
114
|
+
): Promise<RegisterAndConnectTiming | void> => {
|
|
115
|
+
// Deduplicate concurrent calls while a connection is in-flight
|
|
116
|
+
if (this.connectingPromise) {
|
|
117
|
+
return this.connectingPromise;
|
|
139
118
|
}
|
|
140
119
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (!sessionData.webSocketUrl) {
|
|
152
|
-
// register() succeeded but the response carried no websocket URL. Attach the measured
|
|
153
|
-
// datachannel time so callers don't misreport a registration that completed as time 0.
|
|
154
|
-
const error: any = new Error(`LLM registration for ${sessionId} returned no websocket URL`);
|
|
155
|
-
error.timing = {clientLLMDatachannelResponseTime};
|
|
156
|
-
throw error;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const isDataChannelTokenEnabled = await this.isDataChannelTokenEnabled();
|
|
160
|
-
|
|
161
|
-
const connectUrl = isDataChannelTokenEnabled
|
|
162
|
-
? LLMChannel.buildUrlWithAwareSubchannels(sessionData.webSocketUrl, AWARE_DATA_CHANNEL)
|
|
163
|
-
: sessionData.webSocketUrl;
|
|
120
|
+
// If already connected to the exact same datachannel URL, avoid triggering
|
|
121
|
+
// a reconnect that would cause the server to replace with 4000 Replaced
|
|
122
|
+
if (
|
|
123
|
+
this.isConnected() &&
|
|
124
|
+
this.datachannelUrl === datachannelUrl &&
|
|
125
|
+
this.locusUrl === locusUrl
|
|
126
|
+
) {
|
|
127
|
+
return Promise.resolve();
|
|
128
|
+
}
|
|
164
129
|
|
|
165
|
-
|
|
130
|
+
this.locusUrl = locusUrl;
|
|
131
|
+
this.datachannelUrl = datachannelUrl;
|
|
132
|
+
const registerStart = performance.now();
|
|
166
133
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
//
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
134
|
+
const connectionPromise: Promise<RegisterAndConnectTiming | void> = this.register(
|
|
135
|
+
datachannelUrl,
|
|
136
|
+
datachannelToken
|
|
137
|
+
)
|
|
138
|
+
.then(async ({webSocketUrl, binding}) => {
|
|
139
|
+
// Check if this operation was invalidated by a disconnect
|
|
140
|
+
if (this.connectingPromise !== connectionPromise) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.webSocketUrl = webSocketUrl;
|
|
145
|
+
this.binding = binding;
|
|
146
|
+
|
|
147
|
+
const clientLLMDatachannelResponseTime = Math.round(performance.now() - registerStart);
|
|
148
|
+
const isDataChannelTokenEnabled = await this.isDataChannelTokenEnabled();
|
|
149
|
+
|
|
150
|
+
// Check again after async operation - disconnect may have run during the await
|
|
151
|
+
if (this.connectingPromise !== connectionPromise) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const connectUrl =
|
|
156
|
+
isDataChannelTokenEnabled && this.webSocketUrl
|
|
157
|
+
? LLMChannel.buildUrlWithAwareSubchannels(this.webSocketUrl, AWARE_DATA_CHANNEL)
|
|
158
|
+
: this.webSocketUrl;
|
|
159
|
+
|
|
160
|
+
const connectStart = performance.now();
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
await this.connect(connectUrl);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
// register() succeeded; only connect() failed. Attach the measured datachannel time so
|
|
166
|
+
// callers don't misreport a websocket failure as a registration that never completed.
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
error.timing = {clientLLMDatachannelResponseTime};
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const clientLLMWebSocketConnectTime = Math.round(performance.now() - connectStart);
|
|
173
|
+
|
|
174
|
+
return {clientLLMDatachannelResponseTime, clientLLMWebSocketConnectTime};
|
|
175
|
+
})
|
|
176
|
+
.finally(() => {
|
|
177
|
+
// Only clear if this operation is still current
|
|
178
|
+
if (this.connectingPromise === connectionPromise) {
|
|
179
|
+
this.connectingPromise = undefined;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
176
182
|
|
|
177
|
-
|
|
183
|
+
this.connectingPromise = connectionPromise;
|
|
178
184
|
|
|
179
|
-
|
|
180
|
-
});
|
|
185
|
+
return this.connectingPromise;
|
|
181
186
|
};
|
|
182
187
|
|
|
183
188
|
/**
|
|
184
189
|
* Tells if LLM socket is connected
|
|
185
|
-
* @param {string} sessionId - Connection identifier
|
|
186
190
|
* @returns {boolean} connected
|
|
187
191
|
*/
|
|
188
|
-
public isConnected = (
|
|
189
|
-
const socket = this.getSocket(sessionId);
|
|
190
|
-
|
|
191
|
-
return socket ? socket.connected : false;
|
|
192
|
-
};
|
|
192
|
+
public isConnected = (): boolean => this.connected;
|
|
193
193
|
|
|
194
194
|
/**
|
|
195
195
|
* Tells if LLM socket is binding
|
|
196
|
-
* @
|
|
197
|
-
* @returns {string} binding
|
|
196
|
+
* @returns {string | undefined} binding
|
|
198
197
|
*/
|
|
199
|
-
public getBinding = (
|
|
200
|
-
const sessionData = this.connections.get(sessionId);
|
|
201
|
-
|
|
202
|
-
return sessionData?.binding;
|
|
203
|
-
};
|
|
198
|
+
public getBinding = (): string | undefined => this.binding;
|
|
204
199
|
|
|
205
200
|
/**
|
|
206
201
|
* Get Locus URL for the connection
|
|
207
|
-
* @
|
|
208
|
-
* @returns {string} locus Url
|
|
202
|
+
* @returns {string | undefined} locus Url
|
|
209
203
|
*/
|
|
210
|
-
public getLocusUrl = (
|
|
211
|
-
const sessionData = this.connections.get(sessionId);
|
|
212
|
-
|
|
213
|
-
return sessionData?.locusUrl;
|
|
214
|
-
};
|
|
204
|
+
public getLocusUrl = (): string | undefined => this.locusUrl;
|
|
215
205
|
|
|
216
206
|
/**
|
|
217
207
|
* Get data channel URL for the connection
|
|
218
|
-
* @
|
|
219
|
-
* @returns {string} data channel Url
|
|
208
|
+
* @returns {string | undefined} data channel Url
|
|
220
209
|
*/
|
|
221
|
-
public getDatachannelUrl = (
|
|
222
|
-
const sessionData = this.connections.get(sessionId);
|
|
223
|
-
|
|
224
|
-
return sessionData?.datachannelUrl;
|
|
225
|
-
};
|
|
210
|
+
public getDatachannelUrl = (): string | undefined => this.datachannelUrl;
|
|
226
211
|
|
|
227
212
|
/**
|
|
228
|
-
* Get
|
|
229
|
-
* @
|
|
230
|
-
* @returns {string | undefined} WebSocket URL
|
|
213
|
+
* Get data channel token for this connection.
|
|
214
|
+
* @returns {string | undefined}
|
|
231
215
|
*/
|
|
232
|
-
public
|
|
233
|
-
const sessionData = this.connections.get(sessionId);
|
|
234
|
-
|
|
235
|
-
return sessionData?.webSocketUrl;
|
|
236
|
-
};
|
|
216
|
+
public getDatachannelToken = (): string | undefined => this.datachannelToken;
|
|
237
217
|
|
|
238
218
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
* connection so that other Meeting instances can avoid disconnecting or
|
|
242
|
-
* re-initializing a connection they do not own.
|
|
243
|
-
*
|
|
244
|
-
* Does NOT create a connections entry if one does not already exist — this
|
|
245
|
-
* method is a no-op when there is no active session data. Callers should
|
|
246
|
-
* invoke it after a successful `registerAndConnect` or during an explicit
|
|
247
|
-
* ownership handoff.
|
|
248
|
-
*
|
|
249
|
-
* @param {string | undefined} ownerMeetingId - Meeting ID (or undefined to clear)
|
|
250
|
-
* @param {string} sessionId - Connection identifier (defaults to default session)
|
|
219
|
+
* Store the data channel token for this connection.
|
|
220
|
+
* @param {string} token
|
|
251
221
|
* @returns {void}
|
|
252
222
|
*/
|
|
253
|
-
public
|
|
254
|
-
|
|
255
|
-
sessionId: string = LLM_DEFAULT_SESSION
|
|
256
|
-
): void => {
|
|
257
|
-
const sessionData = this.connections.get(sessionId);
|
|
258
|
-
|
|
259
|
-
if (!sessionData) {
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
sessionData.ownerMeetingId = ownerMeetingId;
|
|
264
|
-
this.connections.set(sessionId, sessionData);
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Get the owner meeting ID currently associated with an LLM session.
|
|
269
|
-
* Returns undefined when no owner has been assigned (e.g. before the
|
|
270
|
-
* first successful `registerAndConnect`, or after `disconnectLLM`).
|
|
271
|
-
*
|
|
272
|
-
* @param {string} sessionId - Connection identifier (defaults to default session)
|
|
273
|
-
* @returns {string | undefined} ownerMeetingId
|
|
274
|
-
*/
|
|
275
|
-
public getOwnerMeetingId = (sessionId: string = LLM_DEFAULT_SESSION): string | undefined => {
|
|
276
|
-
const sessionData = this.connections.get(sessionId);
|
|
277
|
-
|
|
278
|
-
return sessionData?.ownerMeetingId;
|
|
279
|
-
};
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Resolve ownership information for an LLM session.
|
|
283
|
-
*
|
|
284
|
-
* Rules:
|
|
285
|
-
* - no current owner => caller may proceed
|
|
286
|
-
* - caller has no identity to assert => treat as owner
|
|
287
|
-
* - otherwise caller must match current owner
|
|
288
|
-
*
|
|
289
|
-
* @param {string | undefined} ownerMeetingId - Candidate owner to evaluate
|
|
290
|
-
* @param {string} sessionId - Connection identifier (defaults to default session)
|
|
291
|
-
* @returns {{currentOwner: (string|undefined), isOwner: boolean}}
|
|
292
|
-
*/
|
|
293
|
-
public resolveSessionOwnership = (
|
|
294
|
-
ownerMeetingId?: string,
|
|
295
|
-
sessionId: string = LLM_DEFAULT_SESSION
|
|
296
|
-
): {
|
|
297
|
-
currentOwner: string | undefined;
|
|
298
|
-
isOwner: boolean;
|
|
299
|
-
} => {
|
|
300
|
-
const currentOwner = this.getOwnerMeetingId(sessionId);
|
|
301
|
-
const isOwner = !currentOwner || !ownerMeetingId || currentOwner === ownerMeetingId;
|
|
302
|
-
|
|
303
|
-
return {
|
|
304
|
-
currentOwner,
|
|
305
|
-
isOwner,
|
|
306
|
-
};
|
|
223
|
+
public setDatachannelToken = (token: string): void => {
|
|
224
|
+
this.datachannelToken = token;
|
|
307
225
|
};
|
|
308
226
|
|
|
309
227
|
/**
|
|
310
|
-
*
|
|
311
|
-
* @param {DataChannelTokenType|string} tokenKey
|
|
312
|
-
* @param {string | undefined} ownerMeetingId - Meeting id asserting read ownership
|
|
313
|
-
* @returns {string | undefined} data channel token
|
|
314
|
-
*/
|
|
315
|
-
public getDatachannelToken = (
|
|
316
|
-
tokenKey?: DataChannelTokenType | string,
|
|
317
|
-
ownerMeetingId?: string
|
|
318
|
-
): string | undefined => {
|
|
319
|
-
const resolvedTokenKey = tokenKey ?? DataChannelTokenType.Default;
|
|
320
|
-
|
|
321
|
-
const {currentOwner, isOwner} = this.resolveSessionOwnership(ownerMeetingId, resolvedTokenKey);
|
|
322
|
-
|
|
323
|
-
if (!isOwner) {
|
|
324
|
-
this.logger.info(
|
|
325
|
-
`llm#getDatachannelToken --> skip read for session ${resolvedTokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
|
|
326
|
-
);
|
|
327
|
-
|
|
328
|
-
return undefined;
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
return this.datachannelTokens[resolvedTokenKey];
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
/**
|
|
335
|
-
* Set data channel token for the connection
|
|
336
|
-
* @param {string} datachannelToken - data channel token
|
|
337
|
-
* @param {DataChannelTokenType|string} [tokenKey]
|
|
338
|
-
* @param {string | undefined} ownerMeetingId - Meeting id asserting write ownership
|
|
228
|
+
* Clear the data channel token for this connection.
|
|
339
229
|
* @returns {void}
|
|
340
230
|
*/
|
|
341
|
-
public
|
|
342
|
-
datachannelToken
|
|
343
|
-
tokenKey?: DataChannelTokenType | string,
|
|
344
|
-
ownerMeetingId?: string
|
|
345
|
-
): void => {
|
|
346
|
-
const resolvedTokenKey = tokenKey ?? DataChannelTokenType.Default;
|
|
347
|
-
|
|
348
|
-
const {currentOwner, isOwner} = this.resolveSessionOwnership(ownerMeetingId, resolvedTokenKey);
|
|
349
|
-
|
|
350
|
-
if (!isOwner) {
|
|
351
|
-
this.logger.info(
|
|
352
|
-
`llm#setDatachannelToken --> skip write for session ${resolvedTokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
|
|
353
|
-
);
|
|
354
|
-
|
|
355
|
-
return;
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
this.datachannelTokens[resolvedTokenKey] = datachannelToken;
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Clears a single session's data channel token.
|
|
363
|
-
* @param {DataChannelTokenType|string} tokenKey
|
|
364
|
-
* @param {string} ownerMeetingId - Meeting id asserting delete ownership
|
|
365
|
-
* @returns {void}
|
|
366
|
-
*/
|
|
367
|
-
public clearDatachannelToken = (
|
|
368
|
-
tokenKey: DataChannelTokenType | string,
|
|
369
|
-
ownerMeetingId: string
|
|
370
|
-
): void => {
|
|
371
|
-
const resolvedTokenKey = tokenKey;
|
|
372
|
-
|
|
373
|
-
const {currentOwner, isOwner} = this.resolveSessionOwnership(ownerMeetingId, resolvedTokenKey);
|
|
374
|
-
|
|
375
|
-
if (!isOwner) {
|
|
376
|
-
this.logger.info(
|
|
377
|
-
`llm#clearDatachannelToken --> skip clear for session ${resolvedTokenKey}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
|
|
378
|
-
);
|
|
379
|
-
|
|
380
|
-
return;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
this.datachannelTokens[resolvedTokenKey] = undefined;
|
|
384
|
-
delete this.datachannelTokens[resolvedTokenKey];
|
|
231
|
+
public clearDatachannelToken = (): void => {
|
|
232
|
+
this.datachannelToken = undefined;
|
|
385
233
|
};
|
|
386
234
|
|
|
387
235
|
/**
|
|
388
|
-
* Set the handler used to refresh the DataChannel token
|
|
389
|
-
*
|
|
390
|
-
* @param {function} handler - Function that returns a refreshed token
|
|
391
|
-
* @param {string} [sessionId] - Connection identifier
|
|
392
|
-
* @param {string | undefined} ownerMeetingId - Meeting id asserting refresh-handler ownership
|
|
236
|
+
* Set the handler used to refresh the DataChannel token.
|
|
237
|
+
* @param {function} handler
|
|
393
238
|
* @returns {void}
|
|
394
239
|
*/
|
|
395
|
-
public setRefreshHandler(
|
|
240
|
+
public setRefreshHandler = (
|
|
396
241
|
handler: () => Promise<{
|
|
397
242
|
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
const resolvedSessionId = sessionId ?? LLM_DEFAULT_SESSION;
|
|
403
|
-
|
|
404
|
-
const {currentOwner, isOwner} = this.resolveSessionOwnership(ownerMeetingId, resolvedSessionId);
|
|
405
|
-
|
|
406
|
-
if (!isOwner) {
|
|
407
|
-
this.logger.info(
|
|
408
|
-
`llm#setRefreshHandler --> skip write for session ${resolvedSessionId}; owned by ${currentOwner}, candidate ${ownerMeetingId}`
|
|
409
|
-
);
|
|
410
|
-
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const sessionData = this.connections.get(resolvedSessionId);
|
|
415
|
-
|
|
416
|
-
if (sessionData) {
|
|
417
|
-
sessionData.refreshHandler = handler;
|
|
418
|
-
if (ownerMeetingId) {
|
|
419
|
-
sessionData.ownerMeetingId = ownerMeetingId;
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// Intentionally allow a pre-connection session shape here.
|
|
426
|
-
// Some flows inject refreshHandler before register/connect so token refresh
|
|
427
|
-
// is already wired when the socket lifecycle starts. register()/
|
|
428
|
-
// registerAndConnect() will later fill webSocketUrl/binding/locusUrl/
|
|
429
|
-
// datachannelUrl into this same session entry.
|
|
430
|
-
this.connections.set(resolvedSessionId, {
|
|
431
|
-
refreshHandler: handler,
|
|
432
|
-
ownerMeetingId,
|
|
433
|
-
});
|
|
434
|
-
}
|
|
243
|
+
}>
|
|
244
|
+
): void => {
|
|
245
|
+
this.refreshHandler = handler;
|
|
246
|
+
};
|
|
435
247
|
|
|
436
248
|
/**
|
|
437
249
|
* Refresh the data channel token using the injected handler.
|
|
438
|
-
*
|
|
439
|
-
* @param {string} sessionId - Connection identifier (defaults to default session)
|
|
440
|
-
* @returns {Promise<string>} The refreshed token.
|
|
250
|
+
* @returns {Promise<object | null>}
|
|
441
251
|
*/
|
|
442
|
-
public async refreshDataChannelToken(
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
if (!refreshHandler) {
|
|
252
|
+
public async refreshDataChannelToken() {
|
|
253
|
+
if (!this.refreshHandler) {
|
|
446
254
|
this.logger.warn(
|
|
447
|
-
|
|
255
|
+
'llm#refreshDataChannelToken --> LLM refreshHandler is not set, skipping token refresh'
|
|
448
256
|
);
|
|
449
257
|
|
|
450
258
|
return null;
|
|
451
259
|
}
|
|
452
260
|
|
|
453
261
|
try {
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
return res;
|
|
262
|
+
return await this.refreshHandler();
|
|
457
263
|
} catch (error: any) {
|
|
458
264
|
this.logger.warn(
|
|
459
|
-
`llm#refreshDataChannelToken --> DataChannel token refresh failed
|
|
265
|
+
`llm#refreshDataChannelToken --> DataChannel token refresh failed: ${
|
|
460
266
|
error?.message || error
|
|
461
267
|
}`
|
|
462
268
|
);
|
|
@@ -466,121 +272,30 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
466
272
|
}
|
|
467
273
|
|
|
468
274
|
/**
|
|
469
|
-
* Disconnects
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
sessionId?: string,
|
|
478
|
-
ownerMeetingId?: string
|
|
479
|
-
): Promise<boolean> => {
|
|
480
|
-
const resolvedSessionId = sessionId ?? LLM_DEFAULT_SESSION;
|
|
481
|
-
|
|
482
|
-
// Backward-compat path: historically callers could omit ownerMeetingId
|
|
483
|
-
// (and sometimes sessionId). Reuse current owner when available so legacy
|
|
484
|
-
// calls remain best-effort without throwing at teardown time.
|
|
485
|
-
const resolvedOwnerMeetingId = ownerMeetingId || this.getOwnerMeetingId(resolvedSessionId);
|
|
486
|
-
|
|
487
|
-
if (!ownerMeetingId) {
|
|
488
|
-
this.logger.warn(
|
|
489
|
-
`llm#disconnectLLM --> ownerMeetingId is omitted for session ${resolvedSessionId}; using legacy compatibility path`
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
const {currentOwner, isOwner} = this.resolveSessionOwnership(
|
|
494
|
-
resolvedOwnerMeetingId,
|
|
495
|
-
resolvedSessionId
|
|
496
|
-
);
|
|
497
|
-
|
|
498
|
-
if (!isOwner) {
|
|
499
|
-
this.logger.info(
|
|
500
|
-
`llm#disconnectLLM --> skip disconnect for session ${resolvedSessionId}; owned by ${currentOwner}, candidate ${resolvedOwnerMeetingId}`
|
|
501
|
-
);
|
|
502
|
-
|
|
503
|
-
return Promise.resolve(false);
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
return this.disconnect(options, resolvedSessionId).then(() => {
|
|
507
|
-
// Clear owner tag before cleanup to ensure it's not lingering
|
|
508
|
-
// if another meeting claimed it during disconnect
|
|
509
|
-
this.setOwnerMeetingId(undefined, resolvedSessionId);
|
|
510
|
-
|
|
511
|
-
// Clean up sessions data
|
|
512
|
-
this.connections.delete(resolvedSessionId);
|
|
513
|
-
|
|
514
|
-
return true;
|
|
515
|
-
});
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
/**
|
|
519
|
-
* Disconnects all LLM websocket connections
|
|
520
|
-
* @param {{code: number, reason: string}} options - The disconnect option object with code and reason
|
|
275
|
+
* Disconnects the WebSocket and clears all connection state.
|
|
276
|
+
* Overrides Mercury's disconnect to also clear LLM-specific state.
|
|
277
|
+
* Clears connectingPromise first to invalidate any in-flight operations,
|
|
278
|
+
* then clears state before calling super.disconnect(). This ensures:
|
|
279
|
+
* 1. Stale register() responses won't overwrite state (they check promise identity)
|
|
280
|
+
* 2. No deadlock with Mercury's _connectWithBackoff() (we don't await connectingPromise)
|
|
281
|
+
* 3. State is cleared before 'disconnected' event fires
|
|
282
|
+
* @param {object} [options]
|
|
521
283
|
* @returns {Promise<void>}
|
|
522
284
|
*/
|
|
523
|
-
public
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
binding?: string;
|
|
538
|
-
locusUrl?: string;
|
|
539
|
-
datachannelUrl?: string;
|
|
540
|
-
ownerMeetingId?: string;
|
|
541
|
-
}
|
|
542
|
-
> => new Map(this.connections);
|
|
543
|
-
|
|
544
|
-
/**
|
|
545
|
-
* Look up the locusUrl associated with a datachannel request URL.
|
|
546
|
-
* Iterates all active LLM sessions and returns the locusUrl of the
|
|
547
|
-
* session whose stored datachannelUrl is a prefix of the given request URL.
|
|
548
|
-
*
|
|
549
|
-
* @param {string} requestUrl - The in-flight request URL to match
|
|
550
|
-
* @returns {string | undefined} The matching locusUrl, or undefined if not found
|
|
551
|
-
*/
|
|
552
|
-
public getLocusUrlByDatachannelUrl(requestUrl: string): string | undefined {
|
|
553
|
-
for (const [, connection] of this.connections) {
|
|
554
|
-
if (
|
|
555
|
-
connection.datachannelUrl &&
|
|
556
|
-
LLMChannel.matchesDatachannelRequestUrl(requestUrl, connection.datachannelUrl)
|
|
557
|
-
) {
|
|
558
|
-
return connection.locusUrl;
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
return undefined;
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
/**
|
|
566
|
-
* Look up the sessionId associated with a datachannel request URL.
|
|
567
|
-
* Iterates all active LLM sessions and returns the sessionId whose
|
|
568
|
-
* stored datachannelUrl is a prefix of the given request URL.
|
|
569
|
-
*
|
|
570
|
-
* @param {string} requestUrl - The in-flight request URL to match
|
|
571
|
-
* @returns {string | undefined} The matching sessionId, or undefined if not found
|
|
572
|
-
*/
|
|
573
|
-
public getSessionIdByDatachannelUrl(requestUrl: string): string | undefined {
|
|
574
|
-
for (const [sessionId, connection] of this.connections) {
|
|
575
|
-
if (
|
|
576
|
-
connection.datachannelUrl &&
|
|
577
|
-
LLMChannel.matchesDatachannelRequestUrl(requestUrl, connection.datachannelUrl)
|
|
578
|
-
) {
|
|
579
|
-
return sessionId;
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
return undefined;
|
|
285
|
+
public async disconnect(options?: {code: number; reason: string}): Promise<void> {
|
|
286
|
+
// Clear connectingPromise first to invalidate any in-flight operations.
|
|
287
|
+
// Their promise identity checks will detect they've been superseded.
|
|
288
|
+
this.connectingPromise = undefined;
|
|
289
|
+
|
|
290
|
+
// Clear LLM-specific state before Mercury emits 'disconnected'
|
|
291
|
+
this.webSocketUrl = undefined;
|
|
292
|
+
this.binding = undefined;
|
|
293
|
+
this.locusUrl = undefined;
|
|
294
|
+
this.datachannelUrl = undefined;
|
|
295
|
+
this.datachannelToken = undefined;
|
|
296
|
+
this.refreshHandler = undefined;
|
|
297
|
+
|
|
298
|
+
await super.disconnect(options);
|
|
584
299
|
}
|
|
585
300
|
|
|
586
301
|
/**
|
|
@@ -611,8 +326,8 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
611
326
|
}
|
|
612
327
|
|
|
613
328
|
/**
|
|
614
|
-
* Returns true if
|
|
615
|
-
* @returns {Promise<boolean>}
|
|
329
|
+
* Returns true if the data channel token feature flag is enabled.
|
|
330
|
+
* @returns {Promise<boolean>}
|
|
616
331
|
*/
|
|
617
332
|
public isDataChannelTokenEnabled(): Promise<boolean> {
|
|
618
333
|
// @ts-ignore
|
|
@@ -621,12 +336,10 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
621
336
|
|
|
622
337
|
/**
|
|
623
338
|
* Builds a WebSocket URL with the `subscriptionAwareSubchannels` query parameter.
|
|
624
|
-
*
|
|
625
|
-
* @param {string}
|
|
626
|
-
* @
|
|
627
|
-
* @returns {string} The final URL with updated query parameters.
|
|
339
|
+
* @param {string} baseUrl
|
|
340
|
+
* @param {string[]} subchannels
|
|
341
|
+
* @returns {string}
|
|
628
342
|
*/
|
|
629
|
-
|
|
630
343
|
public static buildUrlWithAwareSubchannels = (baseUrl: string, subchannels: string[]) => {
|
|
631
344
|
const urlObj = new URL(baseUrl);
|
|
632
345
|
urlObj.searchParams.set(SUBSCRIPTION_AWARE_SUBCHANNELS_PARAM, subchannels.join(','));
|