@webex/internal-plugin-llm 3.12.0-next.7 → 3.12.0-next.70
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/.repo-context.json +416 -0
- package/.sdd/manifest.json +355 -0
- package/AGENTS.md +198 -0
- package/README.md +45 -65
- package/dist/constants.js +2 -1
- package/dist/constants.js.map +1 -1
- package/dist/index.js +30 -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 +225 -158
- package/dist/llm.js.map +1 -1
- package/dist/llm.types.js +13 -1
- package/dist/llm.types.js.map +1 -1
- package/docs/adr/0001-retain-product-readme.md +94 -0
- package/docs/adr/index.md +22 -0
- package/docs/architecture.md +279 -0
- package/docs/getting-started.md +104 -0
- package/docs/index.md +47 -0
- package/docs/specs/README.md +73 -0
- package/package.json +6 -6
- package/schemas/repo-context.schema.json +796 -0
- package/src/constants.ts +1 -0
- package/src/docs/README.md +522 -0
- package/src/index.ts +10 -2
- package/src/llm-plugin.ts +115 -0
- package/src/llm.ts +177 -162
- package/src/llm.types.ts +86 -22
- package/test/unit/spec/llm-plugin.js +391 -0
- package/test/unit/spec/llm.js +504 -243
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/* eslint-disable require-jsdoc */
|
|
2
|
+
import {WebexPlugin} from '@webex/webex-core';
|
|
3
|
+
import LLMChannel, {config} from './llm';
|
|
4
|
+
import {DATA_CHANNEL_WITH_JWT_TOKEN} from './constants';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* LLMPlugin — registered as `webex.internal.llm`.
|
|
8
|
+
*
|
|
9
|
+
* Factory for creating LLMChannel instances. Each Meeting creates and owns
|
|
10
|
+
* its own LLMChannel(s), allowing multiple independent connections without
|
|
11
|
+
* the need for session IDs or ownership tracking.
|
|
12
|
+
*
|
|
13
|
+
* This is a breaking API change from the previous design where the plugin
|
|
14
|
+
* maintained a Map of sessions and exposed session-keyed methods.
|
|
15
|
+
*
|
|
16
|
+
* Old usage (no longer supported):
|
|
17
|
+
* webex.internal.llm.isConnected()
|
|
18
|
+
* webex.internal.llm.registerAndConnect(url, dcUrl, token, sessionId)
|
|
19
|
+
*
|
|
20
|
+
* New usage:
|
|
21
|
+
* const llm = webex.internal.llm.createChannel();
|
|
22
|
+
* await llm.registerAndConnect(url, dcUrl, token);
|
|
23
|
+
* llm.isConnected();
|
|
24
|
+
* // When done:
|
|
25
|
+
* await llm.disconnect();
|
|
26
|
+
*/
|
|
27
|
+
export class LLMPlugin extends (WebexPlugin as any) {
|
|
28
|
+
namespace = 'llm';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Registry of active LLM channels for interceptor lookup.
|
|
32
|
+
* Channels are registered when created and unregistered on disconnect.
|
|
33
|
+
*/
|
|
34
|
+
private channels = new Set<LLMChannel>();
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new LLMChannel instance. The caller owns the channel and is
|
|
38
|
+
* responsible for connecting, disconnecting, and cleaning it up.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* const llm = webex.internal.llm.createChannel();
|
|
42
|
+
* llm.setRefreshHandler(() => meeting.refreshDataChannelToken());
|
|
43
|
+
* await llm.registerAndConnect(locusUrl, datachannelUrl, token);
|
|
44
|
+
*
|
|
45
|
+
* // Subscribe to events directly on the channel
|
|
46
|
+
* llm.on('event:relay.event', handler);
|
|
47
|
+
* llm.on('online', onlineHandler);
|
|
48
|
+
*
|
|
49
|
+
* // When done
|
|
50
|
+
* await llm.disconnect();
|
|
51
|
+
*
|
|
52
|
+
* @returns {LLMChannel} A new LLM channel instance
|
|
53
|
+
*/
|
|
54
|
+
public createChannel(): LLMChannel {
|
|
55
|
+
// @ts-ignore — WebexPlugin children require {parent: this.webex}
|
|
56
|
+
const channel = new LLMChannel({parent: this.webex});
|
|
57
|
+
|
|
58
|
+
this.channels.add(channel);
|
|
59
|
+
channel.on('disconnected', () => this.channels.delete(channel));
|
|
60
|
+
|
|
61
|
+
return channel;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Returns true if the data channel token feature flag is enabled.
|
|
66
|
+
* This is a global check, not per-connection.
|
|
67
|
+
* @returns {Promise<boolean>}
|
|
68
|
+
*/
|
|
69
|
+
public isDataChannelTokenEnabled(): Promise<boolean> {
|
|
70
|
+
// @ts-ignore
|
|
71
|
+
return this.webex.internal.feature.getFeature('developer', DATA_CHANNEL_WITH_JWT_TOKEN);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Find a channel by its datachannel URL. Used by the interceptor to
|
|
76
|
+
* route token refresh requests to the correct channel.
|
|
77
|
+
* @param {string} url - The request URL to match
|
|
78
|
+
* @returns {LLMChannel | undefined}
|
|
79
|
+
*/
|
|
80
|
+
public getChannelByDatachannelUrl(url: string): LLMChannel | undefined {
|
|
81
|
+
for (const channel of this.channels) {
|
|
82
|
+
const datachannelUrl = channel.getDatachannelUrl();
|
|
83
|
+
|
|
84
|
+
if (datachannelUrl && LLMChannel.matchesDatachannelRequestUrl(url, datachannelUrl)) {
|
|
85
|
+
return channel;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get all active channels. Useful for diagnostics/debugging.
|
|
94
|
+
* @returns {Set<LLMChannel>}
|
|
95
|
+
*/
|
|
96
|
+
public getAllChannels(): Set<LLMChannel> {
|
|
97
|
+
return new Set(this.channels);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Disconnect all active channels. Useful for cleanup on logout.
|
|
102
|
+
* @param {object} [options] - Disconnect options
|
|
103
|
+
* @param {number} [options.code] - WebSocket close code
|
|
104
|
+
* @param {string} [options.reason] - WebSocket close reason
|
|
105
|
+
* @returns {Promise<void>}
|
|
106
|
+
*/
|
|
107
|
+
public async disconnectAllChannels(options?: {code: number; reason: string}): Promise<void> {
|
|
108
|
+
const promises = Array.from(this.channels).map((channel) => channel.disconnect(options));
|
|
109
|
+
|
|
110
|
+
await Promise.all(promises);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export {config};
|
|
115
|
+
export default LLMPlugin;
|
package/src/llm.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
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
|
-
import {ILLMChannel, DataChannelTokenType} from './llm.types';
|
|
10
|
+
import {ILLMChannel, DataChannelTokenType, RegisterAndConnectTiming} from './llm.types';
|
|
14
11
|
|
|
15
12
|
export const config = {
|
|
16
13
|
llm: {
|
|
@@ -44,48 +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
|
-
defaultSessionId = LLM_DEFAULT_SESSION;
|
|
52
|
-
/**
|
|
53
|
-
* Map to store connection-specific data for multiple LLM connections
|
|
54
|
-
* @private
|
|
55
|
-
* @type {Map<string, {webSocketUrl?: string; binding?: string; locusUrl?: string; datachannelUrl?: string}>}
|
|
56
|
-
*/
|
|
57
|
-
private connections: Map<
|
|
58
|
-
string,
|
|
59
|
-
{
|
|
60
|
-
webSocketUrl?: string;
|
|
61
|
-
binding?: string;
|
|
62
|
-
locusUrl?: string;
|
|
63
|
-
datachannelUrl?: string;
|
|
64
|
-
datachannelToken?: string;
|
|
65
|
-
}
|
|
66
|
-
> = new Map();
|
|
67
|
-
|
|
68
|
-
private datachannelTokens: Record<DataChannelTokenType, string> = {
|
|
69
|
-
[DataChannelTokenType.Default]: undefined,
|
|
70
|
-
[DataChannelTokenType.PracticeSession]: undefined,
|
|
71
|
-
};
|
|
72
52
|
|
|
53
|
+
private webSocketUrl?: string;
|
|
54
|
+
private binding?: string;
|
|
55
|
+
private locusUrl?: string;
|
|
56
|
+
private datachannelUrl?: string;
|
|
57
|
+
private datachannelToken?: string;
|
|
73
58
|
private refreshHandler?: () => Promise<{
|
|
74
59
|
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
|
|
75
60
|
}>;
|
|
76
61
|
|
|
62
|
+
/** In-flight connection promise for deduplication. */
|
|
63
|
+
private connectingPromise?: Promise<RegisterAndConnectTiming | void>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Check if a connection is currently in progress.
|
|
67
|
+
* @returns {boolean} True if connecting
|
|
68
|
+
*/
|
|
69
|
+
public isConnecting(): boolean {
|
|
70
|
+
return !!this.connectingPromise;
|
|
71
|
+
}
|
|
72
|
+
|
|
77
73
|
/**
|
|
78
74
|
* Register to the websocket
|
|
79
75
|
* @param {string} llmSocketUrl
|
|
80
76
|
* @param {string} datachannelToken
|
|
81
|
-
* @
|
|
82
|
-
* @returns {Promise<void>}
|
|
77
|
+
* @returns {Promise<object>} Promise resolving to {webSocketUrl, binding}
|
|
83
78
|
*/
|
|
84
79
|
private register = async (
|
|
85
80
|
llmSocketUrl: string,
|
|
86
|
-
datachannelToken?: string
|
|
87
|
-
|
|
88
|
-
): Promise<void> => {
|
|
81
|
+
datachannelToken?: string
|
|
82
|
+
): Promise<{webSocketUrl: string; binding: string}> => {
|
|
89
83
|
const isDataChannelTokenEnabled = await this.isDataChannelTokenEnabled();
|
|
90
84
|
|
|
91
85
|
return this.request({
|
|
@@ -97,151 +91,163 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
97
91
|
? {'Data-Channel-Auth-Token': datachannelToken}
|
|
98
92
|
: {},
|
|
99
93
|
})
|
|
100
|
-
.then((res: {body: {webSocketUrl: string; binding: string}}) =>
|
|
101
|
-
// Get or create connection data
|
|
102
|
-
const sessionData = this.connections.get(sessionId) || {};
|
|
103
|
-
sessionData.webSocketUrl = res.body.webSocketUrl;
|
|
104
|
-
sessionData.binding = res.body.binding;
|
|
105
|
-
this.connections.set(sessionId, sessionData);
|
|
106
|
-
})
|
|
94
|
+
.then((res: {body: {webSocketUrl: string; binding: string}}) => res.body)
|
|
107
95
|
.catch((error: any) => {
|
|
108
|
-
this.logger.error(`Error connecting to websocket for
|
|
96
|
+
this.logger.error(`Error connecting to websocket for : ${error}`);
|
|
109
97
|
throw error;
|
|
110
98
|
});
|
|
111
99
|
};
|
|
112
100
|
|
|
113
101
|
/**
|
|
114
|
-
* 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.
|
|
115
105
|
* @param {string} locusUrl
|
|
116
106
|
* @param {string} datachannelUrl
|
|
117
107
|
* @param {string} datachannelToken
|
|
118
|
-
* @
|
|
119
|
-
* @returns {Promise<void>}
|
|
108
|
+
* @returns {Promise<RegisterAndConnectTiming | void>}
|
|
120
109
|
*/
|
|
121
110
|
public registerAndConnect = (
|
|
122
111
|
locusUrl: string,
|
|
123
112
|
datachannelUrl: string,
|
|
124
|
-
datachannelToken?: string
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.
|
|
128
|
-
|
|
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;
|
|
118
|
+
}
|
|
129
119
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
this.
|
|
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
|
+
}
|
|
136
129
|
|
|
137
|
-
|
|
130
|
+
this.locusUrl = locusUrl;
|
|
131
|
+
this.datachannelUrl = datachannelUrl;
|
|
132
|
+
const registerStart = performance.now();
|
|
133
|
+
|
|
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
|
+
});
|
|
138
182
|
|
|
139
|
-
|
|
140
|
-
? LLMChannel.buildUrlWithAwareSubchannels(sessionData.webSocketUrl, AWARE_DATA_CHANNEL)
|
|
141
|
-
: sessionData.webSocketUrl;
|
|
183
|
+
this.connectingPromise = connectionPromise;
|
|
142
184
|
|
|
143
|
-
|
|
144
|
-
|
|
185
|
+
return this.connectingPromise;
|
|
186
|
+
};
|
|
145
187
|
|
|
146
188
|
/**
|
|
147
189
|
* Tells if LLM socket is connected
|
|
148
|
-
* @param {string} sessionId - Connection identifier
|
|
149
190
|
* @returns {boolean} connected
|
|
150
191
|
*/
|
|
151
|
-
public isConnected = (
|
|
152
|
-
const socket = this.getSocket(sessionId);
|
|
153
|
-
|
|
154
|
-
return socket ? socket.connected : false;
|
|
155
|
-
};
|
|
192
|
+
public isConnected = (): boolean => this.connected;
|
|
156
193
|
|
|
157
194
|
/**
|
|
158
195
|
* Tells if LLM socket is binding
|
|
159
|
-
* @
|
|
160
|
-
* @returns {string} binding
|
|
196
|
+
* @returns {string | undefined} binding
|
|
161
197
|
*/
|
|
162
|
-
public getBinding = (
|
|
163
|
-
const sessionData = this.connections.get(sessionId);
|
|
164
|
-
|
|
165
|
-
return sessionData?.binding;
|
|
166
|
-
};
|
|
198
|
+
public getBinding = (): string | undefined => this.binding;
|
|
167
199
|
|
|
168
200
|
/**
|
|
169
201
|
* Get Locus URL for the connection
|
|
170
|
-
* @
|
|
171
|
-
* @returns {string} locus Url
|
|
202
|
+
* @returns {string | undefined} locus Url
|
|
172
203
|
*/
|
|
173
|
-
public getLocusUrl = (
|
|
174
|
-
const sessionData = this.connections.get(sessionId);
|
|
175
|
-
|
|
176
|
-
return sessionData?.locusUrl;
|
|
177
|
-
};
|
|
204
|
+
public getLocusUrl = (): string | undefined => this.locusUrl;
|
|
178
205
|
|
|
179
206
|
/**
|
|
180
207
|
* Get data channel URL for the connection
|
|
181
|
-
* @
|
|
182
|
-
* @returns {string} data channel Url
|
|
208
|
+
* @returns {string | undefined} data channel Url
|
|
183
209
|
*/
|
|
184
|
-
public getDatachannelUrl = (
|
|
185
|
-
const sessionData = this.connections.get(sessionId);
|
|
186
|
-
|
|
187
|
-
return sessionData?.datachannelUrl;
|
|
188
|
-
};
|
|
210
|
+
public getDatachannelUrl = (): string | undefined => this.datachannelUrl;
|
|
189
211
|
|
|
190
212
|
/**
|
|
191
|
-
* Get data channel token for
|
|
192
|
-
* @
|
|
193
|
-
* @returns {string} data channel token
|
|
213
|
+
* Get data channel token for this connection.
|
|
214
|
+
* @returns {string | undefined}
|
|
194
215
|
*/
|
|
195
|
-
public getDatachannelToken = (
|
|
196
|
-
dataChannelTokenType: DataChannelTokenType = DataChannelTokenType.Default
|
|
197
|
-
): string => {
|
|
198
|
-
return this.datachannelTokens[dataChannelTokenType];
|
|
199
|
-
};
|
|
216
|
+
public getDatachannelToken = (): string | undefined => this.datachannelToken;
|
|
200
217
|
|
|
201
218
|
/**
|
|
202
|
-
*
|
|
203
|
-
* @param {string}
|
|
204
|
-
* @param {DataChannelTokenType} dataChannelTokenType
|
|
219
|
+
* Store the data channel token for this connection.
|
|
220
|
+
* @param {string} token
|
|
205
221
|
* @returns {void}
|
|
206
222
|
*/
|
|
207
|
-
public setDatachannelToken = (
|
|
208
|
-
datachannelToken
|
|
209
|
-
dataChannelTokenType: DataChannelTokenType = DataChannelTokenType.Default
|
|
210
|
-
): void => {
|
|
211
|
-
this.datachannelTokens[dataChannelTokenType] = datachannelToken;
|
|
223
|
+
public setDatachannelToken = (token: string): void => {
|
|
224
|
+
this.datachannelToken = token;
|
|
212
225
|
};
|
|
213
226
|
|
|
214
227
|
/**
|
|
215
|
-
*
|
|
216
|
-
* Used when leaving or disconnecting from a meeting.
|
|
228
|
+
* Clear the data channel token for this connection.
|
|
217
229
|
* @returns {void}
|
|
218
230
|
*/
|
|
219
|
-
public
|
|
220
|
-
this.
|
|
221
|
-
|
|
222
|
-
[DataChannelTokenType.PracticeSession]: undefined,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
231
|
+
public clearDatachannelToken = (): void => {
|
|
232
|
+
this.datachannelToken = undefined;
|
|
233
|
+
};
|
|
225
234
|
|
|
226
235
|
/**
|
|
227
|
-
* Set the handler used to refresh the DataChannel token
|
|
228
|
-
*
|
|
229
|
-
* @param {function} handler - Function that returns a refreshed token
|
|
236
|
+
* Set the handler used to refresh the DataChannel token.
|
|
237
|
+
* @param {function} handler
|
|
230
238
|
* @returns {void}
|
|
231
239
|
*/
|
|
232
|
-
public setRefreshHandler(
|
|
240
|
+
public setRefreshHandler = (
|
|
233
241
|
handler: () => Promise<{
|
|
234
242
|
body: {datachannelToken: string; datachannelTokenType: DataChannelTokenType};
|
|
235
243
|
}>
|
|
236
|
-
) {
|
|
244
|
+
): void => {
|
|
237
245
|
this.refreshHandler = handler;
|
|
238
|
-
}
|
|
246
|
+
};
|
|
239
247
|
|
|
240
248
|
/**
|
|
241
249
|
* Refresh the data channel token using the injected handler.
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* @returns {Promise<string>} The refreshed token.
|
|
250
|
+
* @returns {Promise<object | null>}
|
|
245
251
|
*/
|
|
246
252
|
public async refreshDataChannelToken() {
|
|
247
253
|
if (!this.refreshHandler) {
|
|
@@ -253,12 +259,10 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
253
259
|
}
|
|
254
260
|
|
|
255
261
|
try {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
return res;
|
|
262
|
+
return await this.refreshHandler();
|
|
259
263
|
} catch (error: any) {
|
|
260
264
|
this.logger.warn(
|
|
261
|
-
`llm#refreshDataChannelToken --> DataChannel token refresh failed
|
|
265
|
+
`llm#refreshDataChannelToken --> DataChannel token refresh failed: ${
|
|
262
266
|
error?.message || error
|
|
263
267
|
}`
|
|
264
268
|
);
|
|
@@ -268,49 +272,62 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
268
272
|
}
|
|
269
273
|
|
|
270
274
|
/**
|
|
271
|
-
* Disconnects
|
|
272
|
-
*
|
|
273
|
-
*
|
|
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]
|
|
274
283
|
* @returns {Promise<void>}
|
|
275
284
|
*/
|
|
276
|
-
public
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
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);
|
|
299
|
+
}
|
|
284
300
|
|
|
285
301
|
/**
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
*
|
|
302
|
+
* Matches a request URL to a stored datachannel registration URL.
|
|
303
|
+
* Host can differ (e.g. rewritten by hostmap interceptor), so we first
|
|
304
|
+
* try full URL prefix and then fall back to pathname prefix.
|
|
305
|
+
* @param {string} requestUrl
|
|
306
|
+
* @param {string} registrationUrl
|
|
307
|
+
* @returns {boolean}
|
|
289
308
|
*/
|
|
290
|
-
public
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
});
|
|
309
|
+
public static matchesDatachannelRequestUrl(requestUrl: string, registrationUrl: string): boolean {
|
|
310
|
+
if (!requestUrl || !registrationUrl) {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
295
313
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
* @returns {Map} Map of sessionId to session data
|
|
299
|
-
*/
|
|
300
|
-
public getAllConnections = (): Map<
|
|
301
|
-
string,
|
|
302
|
-
{
|
|
303
|
-
webSocketUrl?: string;
|
|
304
|
-
binding?: string;
|
|
305
|
-
locusUrl?: string;
|
|
306
|
-
datachannelUrl?: string;
|
|
307
|
-
datachannelToken?: string;
|
|
314
|
+
if (requestUrl.startsWith(registrationUrl)) {
|
|
315
|
+
return true;
|
|
308
316
|
}
|
|
309
|
-
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
const request = new URL(requestUrl);
|
|
320
|
+
const registration = new URL(registrationUrl);
|
|
321
|
+
|
|
322
|
+
return request.pathname.startsWith(registration.pathname);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
310
327
|
|
|
311
328
|
/**
|
|
312
|
-
* Returns true if
|
|
313
|
-
* @returns {Promise<boolean>}
|
|
329
|
+
* Returns true if the data channel token feature flag is enabled.
|
|
330
|
+
* @returns {Promise<boolean>}
|
|
314
331
|
*/
|
|
315
332
|
public isDataChannelTokenEnabled(): Promise<boolean> {
|
|
316
333
|
// @ts-ignore
|
|
@@ -319,12 +336,10 @@ export default class LLMChannel extends (Mercury as any) implements ILLMChannel
|
|
|
319
336
|
|
|
320
337
|
/**
|
|
321
338
|
* Builds a WebSocket URL with the `subscriptionAwareSubchannels` query parameter.
|
|
322
|
-
*
|
|
323
|
-
* @param {string}
|
|
324
|
-
* @
|
|
325
|
-
* @returns {string} The final URL with updated query parameters.
|
|
339
|
+
* @param {string} baseUrl
|
|
340
|
+
* @param {string[]} subchannels
|
|
341
|
+
* @returns {string}
|
|
326
342
|
*/
|
|
327
|
-
|
|
328
343
|
public static buildUrlWithAwareSubchannels = (baseUrl: string, subchannels: string[]) => {
|
|
329
344
|
const urlObj = new URL(baseUrl);
|
|
330
345
|
urlObj.searchParams.set(SUBSCRIPTION_AWARE_SUBCHANNELS_PARAM, subchannels.join(','));
|