@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
package/test/unit/spec/llm.js
CHANGED
|
@@ -2,20 +2,20 @@ import MockWebex from '@webex/test-helper-mock-webex';
|
|
|
2
2
|
import {assert} from '@webex/test-helper-chai';
|
|
3
3
|
import sinon from 'sinon';
|
|
4
4
|
import Mercury from '@webex/internal-plugin-mercury';
|
|
5
|
-
import
|
|
5
|
+
import LLMChannel from '@webex/internal-plugin-llm/src/llm';
|
|
6
6
|
|
|
7
7
|
describe('plugin-llm', () => {
|
|
8
8
|
const locusUrl = 'locusUrl';
|
|
9
9
|
const datachannelUrl = 'datachannelUrl';
|
|
10
10
|
|
|
11
|
-
describe('
|
|
12
|
-
let webex,
|
|
11
|
+
describe('LLMChannel', () => {
|
|
12
|
+
let webex, llmChannel;
|
|
13
13
|
|
|
14
14
|
beforeEach(() => {
|
|
15
15
|
webex = new MockWebex({
|
|
16
16
|
children: {
|
|
17
17
|
mercury: Mercury,
|
|
18
|
-
llm:
|
|
18
|
+
llm: LLMChannel,
|
|
19
19
|
},
|
|
20
20
|
});
|
|
21
21
|
|
|
@@ -24,121 +24,151 @@ describe('plugin-llm', () => {
|
|
|
24
24
|
getFeature: sinon.stub().resolves(true),
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
llmChannel = webex.internal.llm;
|
|
28
|
+
// Stub Mercury's prototype disconnect so super.disconnect() works in tests.
|
|
29
|
+
// The real Mercury emits 'disconnected' after disconnect completes, so we simulate that.
|
|
30
|
+
sinon
|
|
31
|
+
.stub(Object.getPrototypeOf(Object.getPrototypeOf(llmChannel)), 'disconnect')
|
|
32
|
+
.callsFake(function () {
|
|
33
|
+
this.connected = false;
|
|
34
|
+
this._emit('disconnected');
|
|
35
|
+
|
|
36
|
+
return Promise.resolve();
|
|
37
|
+
});
|
|
38
|
+
llmChannel.request = sinon.stub().resolves({
|
|
31
39
|
headers: {},
|
|
32
40
|
body: {
|
|
33
41
|
binding: 'binding',
|
|
34
42
|
webSocketUrl: 'wss://example.com/socket',
|
|
35
43
|
},
|
|
36
44
|
});
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
llmService.connect = sinon.stub().callsFake((url, sessionId) => {
|
|
40
|
-
sockets.set(sessionId, {connected: true});
|
|
41
|
-
llmService.getSocket = sinon.stub().callsFake((sid) => sockets.get(sid));
|
|
45
|
+
llmChannel.connect = sinon.stub().callsFake(() => {
|
|
46
|
+
llmChannel.connected = true;
|
|
42
47
|
});
|
|
43
|
-
llmService.connections.set('llm-default-session',{
|
|
44
|
-
webSocketUrl: 'wss://example.com/socket',
|
|
45
|
-
})
|
|
46
48
|
});
|
|
47
49
|
|
|
48
50
|
afterEach(() => sinon.restore());
|
|
49
51
|
|
|
50
52
|
describe('#registerAndConnect', () => {
|
|
51
|
-
it('registers
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
});
|
|
53
|
+
it('registers and connects', async () => {
|
|
54
|
+
assert.equal(llmChannel.isConnected(), false);
|
|
55
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
56
|
+
assert.equal(llmChannel.isConnected(), true);
|
|
57
|
+
assert.equal(llmChannel.getLocusUrl(), locusUrl);
|
|
58
|
+
assert.equal(llmChannel.getDatachannelUrl(), datachannelUrl);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('connects with subscriptionAwareSubchannels when token enabled', async () => {
|
|
62
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(true);
|
|
62
63
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
const buildSpy = sinon.spy(LLMChannel, 'buildUrlWithAwareSubchannels');
|
|
65
|
+
|
|
66
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl, 'abc123');
|
|
67
|
+
|
|
68
|
+
sinon.assert.calledOnce(buildSpy);
|
|
69
|
+
sinon.assert.calledOnce(llmChannel.connect);
|
|
70
|
+
|
|
71
|
+
const calledUrl = llmChannel.connect.getCall(0).args[0];
|
|
72
|
+
assert.include(calledUrl, 'subscriptionAwareSubchannels=');
|
|
66
73
|
});
|
|
67
74
|
|
|
68
|
-
it(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
binding: 'binding',
|
|
75
|
-
webSocketUrl: 'wss://example.com/socket',
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
});
|
|
75
|
+
it('connects without subscriptionAwareSubchannels when token disabled', async () => {
|
|
76
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(false);
|
|
77
|
+
|
|
78
|
+
const buildSpy = sinon.spy(LLMChannel, 'buildUrlWithAwareSubchannels');
|
|
79
|
+
|
|
80
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
79
81
|
|
|
80
|
-
|
|
81
|
-
assert.
|
|
82
|
+
sinon.assert.notCalled(buildSpy);
|
|
83
|
+
sinon.assert.calledOnce(llmChannel.connect);
|
|
84
|
+
|
|
85
|
+
const calledUrl = llmChannel.connect.getCall(0).args[0];
|
|
86
|
+
assert.equal(calledUrl, 'wss://example.com/socket');
|
|
82
87
|
});
|
|
83
88
|
|
|
84
|
-
it('
|
|
85
|
-
|
|
86
|
-
llmService.binding = 'binding';
|
|
87
|
-
llmService.webSocketUrl = 'wss://example.com/socket';
|
|
88
|
-
return {
|
|
89
|
-
body: {
|
|
90
|
-
binding: 'binding',
|
|
91
|
-
webSocketUrl: 'wss://example.com/socket',
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
});
|
|
89
|
+
it('connects with subscriptionAwareSubchannels when token enabled but token missing', async () => {
|
|
90
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(true);
|
|
95
91
|
|
|
96
|
-
|
|
92
|
+
const buildSpy = sinon.spy(LLMChannel, 'buildUrlWithAwareSubchannels');
|
|
97
93
|
|
|
98
|
-
await
|
|
94
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl, undefined);
|
|
99
95
|
|
|
100
|
-
sinon.assert.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
96
|
+
sinon.assert.calledOnce(buildSpy);
|
|
97
|
+
sinon.assert.calledOnce(llmChannel.connect);
|
|
98
|
+
|
|
99
|
+
const calledUrl = llmChannel.connect.getCall(0).args[0];
|
|
100
|
+
assert.include(calledUrl, 'subscriptionAwareSubchannels=');
|
|
101
|
+
|
|
102
|
+
buildSpy.restore();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('deduplicates concurrent calls', async () => {
|
|
106
|
+
const promise1 = llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
107
|
+
const promise2 = llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
106
108
|
|
|
107
|
-
assert.
|
|
109
|
+
assert.strictEqual(promise1, promise2);
|
|
110
|
+
await promise1;
|
|
108
111
|
});
|
|
109
112
|
|
|
110
|
-
it('
|
|
111
|
-
|
|
113
|
+
it('avoids reconnect if already connected to same URLs', async () => {
|
|
114
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
115
|
+
sinon.assert.calledOnce(llmChannel.connect);
|
|
116
|
+
|
|
117
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
118
|
+
sinon.assert.calledOnce(llmChannel.connect);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('propagates error when registration request fails', async () => {
|
|
122
|
+
const error = new Error('Network failure');
|
|
123
|
+
llmChannel.request = sinon.stub().rejects(error);
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
127
|
+
assert.fail('should have thrown');
|
|
128
|
+
} catch (e) {
|
|
129
|
+
assert.equal(e.message, 'Network failure');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
sinon.assert.notCalled(llmChannel.connect);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('returns timing data on successful connection', async () => {
|
|
136
|
+
const clock = sinon.useFakeTimers();
|
|
137
|
+
|
|
138
|
+
llmChannel.request = sinon.stub().callsFake(async () => {
|
|
139
|
+
clock.tick(37);
|
|
112
140
|
|
|
113
|
-
llmService.register = sinon.stub().callsFake(async () => {
|
|
114
|
-
llmService.binding = 'binding';
|
|
115
|
-
llmService.webSocketUrl = 'wss://example.com/socket';
|
|
116
141
|
return {
|
|
142
|
+
headers: {},
|
|
117
143
|
body: {
|
|
118
144
|
binding: 'binding',
|
|
119
145
|
webSocketUrl: 'wss://example.com/socket',
|
|
120
146
|
},
|
|
121
147
|
};
|
|
122
148
|
});
|
|
149
|
+
llmChannel.connect = sinon.stub().callsFake(async () => {
|
|
150
|
+
clock.tick(23);
|
|
151
|
+
llmChannel.connected = true;
|
|
152
|
+
});
|
|
123
153
|
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
await llmService.registerAndConnect(locusUrl, datachannelUrl,'abc123');
|
|
154
|
+
const result = await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
127
155
|
|
|
128
|
-
|
|
129
|
-
|
|
156
|
+
assert.deepEqual(result, {
|
|
157
|
+
clientLLMDatachannelResponseTime: 37,
|
|
158
|
+
clientLLMWebSocketConnectTime: 23,
|
|
159
|
+
});
|
|
130
160
|
|
|
131
|
-
|
|
132
|
-
assert.include(calledUrl, 'subscriptionAwareSubchannels=');
|
|
161
|
+
clock.restore();
|
|
133
162
|
});
|
|
134
163
|
|
|
135
|
-
it('
|
|
136
|
-
|
|
164
|
+
it('attaches timing to error when connect fails', async () => {
|
|
165
|
+
const clock = sinon.useFakeTimers();
|
|
166
|
+
|
|
167
|
+
llmChannel.request = sinon.stub().callsFake(async () => {
|
|
168
|
+
clock.tick(37);
|
|
137
169
|
|
|
138
|
-
llmService.register = sinon.stub().callsFake(async () => {
|
|
139
|
-
llmService.binding = 'binding';
|
|
140
|
-
llmService.webSocketUrl = 'wss://example.com/socket';
|
|
141
170
|
return {
|
|
171
|
+
headers: {},
|
|
142
172
|
body: {
|
|
143
173
|
binding: 'binding',
|
|
144
174
|
webSocketUrl: 'wss://example.com/socket',
|
|
@@ -146,77 +176,109 @@ describe('plugin-llm', () => {
|
|
|
146
176
|
};
|
|
147
177
|
});
|
|
148
178
|
|
|
149
|
-
const
|
|
179
|
+
const connectError = new Error('websocket connect failed');
|
|
180
|
+
llmChannel.connect = sinon.stub().rejects(connectError);
|
|
150
181
|
|
|
151
|
-
|
|
182
|
+
let caughtError;
|
|
183
|
+
try {
|
|
184
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
caughtError = error;
|
|
187
|
+
}
|
|
152
188
|
|
|
153
|
-
|
|
154
|
-
|
|
189
|
+
assert.equal(caughtError, connectError);
|
|
190
|
+
assert.deepEqual(caughtError.timing, {clientLLMDatachannelResponseTime: 37});
|
|
155
191
|
|
|
156
|
-
|
|
157
|
-
assert.equal(calledUrl, llmService.webSocketUrl);
|
|
192
|
+
clock.restore();
|
|
158
193
|
});
|
|
159
194
|
|
|
160
|
-
it('
|
|
161
|
-
|
|
195
|
+
it('clears connectingPromise after successful connection', async () => {
|
|
196
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
162
197
|
|
|
163
|
-
|
|
198
|
+
// After completion, a new call should create a fresh promise (not deduplicated)
|
|
199
|
+
llmChannel.connect.resetHistory();
|
|
200
|
+
llmChannel.request.resetHistory();
|
|
164
201
|
|
|
165
|
-
|
|
202
|
+
// Simulate different URLs to force new connection
|
|
203
|
+
await llmChannel.registerAndConnect('newLocusUrl', 'newDatachannelUrl');
|
|
166
204
|
|
|
167
|
-
|
|
168
|
-
sinon.assert.calledOnce(
|
|
205
|
+
// Should have called request again since connectingPromise was cleared
|
|
206
|
+
sinon.assert.calledOnce(llmChannel.request);
|
|
207
|
+
});
|
|
169
208
|
|
|
170
|
-
|
|
171
|
-
|
|
209
|
+
it('clears connectingPromise after failed connection', async () => {
|
|
210
|
+
llmChannel.connect = sinon.stub().rejects(new Error('connect failed'));
|
|
172
211
|
|
|
173
|
-
|
|
212
|
+
try {
|
|
213
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
214
|
+
} catch (e) {
|
|
215
|
+
// expected
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Reset and try again - should not deduplicate since promise was cleared
|
|
219
|
+
llmChannel.connect = sinon.stub().callsFake(() => {
|
|
220
|
+
llmChannel.connected = true;
|
|
221
|
+
});
|
|
222
|
+
llmChannel.request.resetHistory();
|
|
223
|
+
|
|
224
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
225
|
+
|
|
226
|
+
// Should have called request again
|
|
227
|
+
sinon.assert.calledOnce(llmChannel.request);
|
|
174
228
|
});
|
|
175
|
-
});
|
|
176
229
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
230
|
+
it('returns undefined when already connected to same URLs', async () => {
|
|
231
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
232
|
+
|
|
233
|
+
const result = await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
234
|
+
|
|
235
|
+
assert.equal(result, undefined);
|
|
180
236
|
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('#register (via registerAndConnect)', () => {
|
|
240
|
+
it('sends request with token header when token enabled and provided', async () => {
|
|
241
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(true);
|
|
181
242
|
|
|
182
|
-
|
|
183
|
-
llmService.isDataChannelTokenEnabled.resolves(true);
|
|
184
|
-
await llmService.register(datachannelUrl, 'abc123');
|
|
243
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl, 'abc123');
|
|
185
244
|
|
|
186
245
|
sinon.assert.calledOnceWithExactly(
|
|
187
|
-
|
|
246
|
+
llmChannel.request,
|
|
188
247
|
sinon.match({
|
|
189
248
|
method: 'POST',
|
|
190
|
-
url:
|
|
249
|
+
url: datachannelUrl,
|
|
191
250
|
body: {deviceUrl: webex.internal.device.url},
|
|
192
251
|
headers: {'Data-Channel-Auth-Token': 'abc123'},
|
|
193
252
|
})
|
|
194
253
|
);
|
|
195
254
|
});
|
|
196
255
|
|
|
197
|
-
it('
|
|
198
|
-
|
|
256
|
+
it('sends request without token header when none provided', async () => {
|
|
257
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(true);
|
|
258
|
+
|
|
259
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
199
260
|
|
|
200
261
|
sinon.assert.calledOnceWithExactly(
|
|
201
|
-
|
|
262
|
+
llmChannel.request,
|
|
202
263
|
sinon.match({
|
|
203
264
|
method: 'POST',
|
|
204
|
-
url:
|
|
265
|
+
url: datachannelUrl,
|
|
205
266
|
body: {deviceUrl: webex.internal.device.url},
|
|
206
267
|
headers: {},
|
|
207
268
|
})
|
|
208
269
|
);
|
|
209
270
|
});
|
|
210
271
|
|
|
211
|
-
it('
|
|
212
|
-
|
|
272
|
+
it('sends request without token header when toggle disabled', async () => {
|
|
273
|
+
llmChannel.isDataChannelTokenEnabled = sinon.stub().resolves(false);
|
|
274
|
+
|
|
275
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl, 'abc123');
|
|
213
276
|
|
|
214
|
-
await llmService.register(datachannelUrl,'abc123');
|
|
215
277
|
sinon.assert.calledOnceWithExactly(
|
|
216
|
-
|
|
278
|
+
llmChannel.request,
|
|
217
279
|
sinon.match({
|
|
218
280
|
method: 'POST',
|
|
219
|
-
url:
|
|
281
|
+
url: datachannelUrl,
|
|
220
282
|
body: {deviceUrl: webex.internal.device.url},
|
|
221
283
|
headers: {},
|
|
222
284
|
})
|
|
@@ -226,109 +288,289 @@ describe('plugin-llm', () => {
|
|
|
226
288
|
|
|
227
289
|
describe('#getLocusUrl', () => {
|
|
228
290
|
it('gets LocusUrl', async () => {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return {
|
|
233
|
-
body: {
|
|
234
|
-
binding: 'binding',
|
|
235
|
-
webSocketUrl: 'wss://example.com/socket',
|
|
236
|
-
},
|
|
237
|
-
};
|
|
238
|
-
});
|
|
291
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
292
|
+
assert.equal(llmChannel.getLocusUrl(), locusUrl);
|
|
293
|
+
});
|
|
239
294
|
|
|
240
|
-
|
|
241
|
-
assert.equal(
|
|
295
|
+
it('returns undefined before connection', () => {
|
|
296
|
+
assert.equal(llmChannel.getLocusUrl(), undefined);
|
|
242
297
|
});
|
|
243
298
|
});
|
|
244
299
|
|
|
245
300
|
describe('#getDatachannelUrl', () => {
|
|
246
301
|
it('gets dataChannel Url', async () => {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
webSocketUrl: 'wss://example.com/socket',
|
|
254
|
-
},
|
|
255
|
-
};
|
|
256
|
-
});
|
|
257
|
-
await llmService.registerAndConnect(locusUrl, datachannelUrl);
|
|
258
|
-
assert.equal(llmService.getDatachannelUrl(), datachannelUrl);
|
|
302
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
303
|
+
assert.equal(llmChannel.getDatachannelUrl(), datachannelUrl);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('returns undefined before connection', () => {
|
|
307
|
+
assert.equal(llmChannel.getDatachannelUrl(), undefined);
|
|
259
308
|
});
|
|
260
309
|
});
|
|
261
310
|
|
|
262
|
-
describe('#
|
|
263
|
-
it('
|
|
264
|
-
await
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
assert.equal(
|
|
311
|
+
describe('#getBinding', () => {
|
|
312
|
+
it('gets binding after connection', async () => {
|
|
313
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
314
|
+
assert.equal(llmChannel.getBinding(), 'binding');
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('returns undefined before connection', () => {
|
|
318
|
+
assert.equal(llmChannel.getBinding(), undefined);
|
|
270
319
|
});
|
|
271
320
|
});
|
|
272
321
|
|
|
273
|
-
describe('#
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
},
|
|
292
|
-
};
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
it('calls disconnect and clears session connection + token', async () => {
|
|
296
|
-
await instance.disconnectLLM({ code: 3000, reason: 'bye' });
|
|
297
|
-
|
|
298
|
-
expect(instance.disconnect).toHaveBeenCalledWith(
|
|
299
|
-
{ code: 3000, reason: 'bye' },
|
|
300
|
-
'llm-default-session'
|
|
301
|
-
);
|
|
322
|
+
describe('#isConnecting', () => {
|
|
323
|
+
it('returns true while connection is in progress', () => {
|
|
324
|
+
// Start connection but don't await
|
|
325
|
+
llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
326
|
+
|
|
327
|
+
assert.equal(llmChannel.isConnecting(), true);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it('returns false after connection completes', async () => {
|
|
331
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
332
|
+
|
|
333
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it('returns false before any connection attempt', () => {
|
|
337
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
338
|
+
});
|
|
339
|
+
});
|
|
302
340
|
|
|
303
|
-
|
|
341
|
+
describe('#disconnect', () => {
|
|
342
|
+
it('calls super.disconnect and clears all connection state', async () => {
|
|
343
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
344
|
+
llmChannel.setDatachannelToken('token123');
|
|
345
|
+
|
|
346
|
+
assert.equal(llmChannel.isConnected(), true);
|
|
347
|
+
assert.equal(llmChannel.getLocusUrl(), locusUrl);
|
|
348
|
+
assert.equal(llmChannel.getDatachannelUrl(), datachannelUrl);
|
|
349
|
+
assert.equal(llmChannel.getBinding(), 'binding');
|
|
350
|
+
assert.equal(llmChannel.getDatachannelToken(), 'token123');
|
|
351
|
+
|
|
352
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
353
|
+
|
|
354
|
+
assert.equal(llmChannel.getLocusUrl(), undefined);
|
|
355
|
+
assert.equal(llmChannel.getDatachannelUrl(), undefined);
|
|
356
|
+
assert.equal(llmChannel.getBinding(), undefined);
|
|
357
|
+
assert.equal(llmChannel.getDatachannelToken(), undefined);
|
|
358
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('works without options', async () => {
|
|
362
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
304
363
|
|
|
305
|
-
|
|
364
|
+
await llmChannel.disconnect();
|
|
365
|
+
|
|
366
|
+
assert.equal(llmChannel.getLocusUrl(), undefined);
|
|
306
367
|
});
|
|
307
368
|
|
|
308
369
|
it('propagates disconnect errors', async () => {
|
|
309
|
-
|
|
370
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
371
|
+
// Change the existing stub to reject
|
|
372
|
+
Object.getPrototypeOf(Object.getPrototypeOf(llmChannel)).disconnect.rejects(
|
|
373
|
+
new Error('disconnect failed')
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
try {
|
|
377
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
378
|
+
assert.fail('should have thrown');
|
|
379
|
+
} catch (error) {
|
|
380
|
+
assert.equal(error.message, 'disconnect failed');
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('emits disconnected event', async () => {
|
|
385
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
386
|
+
const disconnectedSpy = sinon.spy();
|
|
387
|
+
|
|
388
|
+
llmChannel.on('disconnected', disconnectedSpy);
|
|
389
|
+
|
|
390
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
391
|
+
|
|
392
|
+
sinon.assert.calledOnce(disconnectedSpy);
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
it('clears state before disconnected event fires', async () => {
|
|
396
|
+
await llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
397
|
+
llmChannel.setDatachannelToken('token123');
|
|
398
|
+
|
|
399
|
+
let stateAtEventTime;
|
|
400
|
+
|
|
401
|
+
llmChannel.on('disconnected', () => {
|
|
402
|
+
// Capture state when event fires - should already be cleared
|
|
403
|
+
stateAtEventTime = {
|
|
404
|
+
locusUrl: llmChannel.getLocusUrl(),
|
|
405
|
+
datachannelUrl: llmChannel.getDatachannelUrl(),
|
|
406
|
+
binding: llmChannel.getBinding(),
|
|
407
|
+
datachannelToken: llmChannel.getDatachannelToken(),
|
|
408
|
+
isConnecting: llmChannel.isConnecting(),
|
|
409
|
+
};
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
413
|
+
|
|
414
|
+
// State should have been undefined when the event fired
|
|
415
|
+
assert.deepEqual(stateAtEventTime, {
|
|
416
|
+
locusUrl: undefined,
|
|
417
|
+
datachannelUrl: undefined,
|
|
418
|
+
binding: undefined,
|
|
419
|
+
datachannelToken: undefined,
|
|
420
|
+
isConnecting: false,
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
it('invalidates in-flight connection via promise identity', async () => {
|
|
425
|
+
let resolveRequest;
|
|
426
|
+
llmChannel.request = sinon.stub().returns(
|
|
427
|
+
new Promise((resolve) => {
|
|
428
|
+
resolveRequest = resolve;
|
|
429
|
+
})
|
|
430
|
+
);
|
|
431
|
+
|
|
432
|
+
// Start connection but don't await - request is pending
|
|
433
|
+
const connectPromise = llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
434
|
+
|
|
435
|
+
assert.equal(llmChannel.isConnecting(), true);
|
|
436
|
+
|
|
437
|
+
// Disconnect while connection is in-flight - should complete immediately
|
|
438
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
439
|
+
|
|
440
|
+
// State should be cleared immediately, not waiting for connection
|
|
441
|
+
assert.equal(llmChannel.getLocusUrl(), undefined);
|
|
442
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
443
|
+
|
|
444
|
+
// Now resolve the pending request - stale operation should not overwrite state
|
|
445
|
+
resolveRequest({
|
|
446
|
+
headers: {},
|
|
447
|
+
body: {binding: 'stale-binding', webSocketUrl: 'wss://example.com/stale'},
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
// Wait for the stale promise to settle
|
|
451
|
+
await connectPromise;
|
|
452
|
+
|
|
453
|
+
// State should still be undefined - stale operation detected invalidation
|
|
454
|
+
assert.equal(llmChannel.getBinding(), undefined);
|
|
455
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
it('stale operation does not clear new connectingPromise', async () => {
|
|
459
|
+
let resolveFirstRequest;
|
|
460
|
+
let resolveSecondRequest;
|
|
461
|
+
llmChannel.request = sinon.stub().returns(
|
|
462
|
+
new Promise((resolve) => {
|
|
463
|
+
resolveFirstRequest = resolve;
|
|
464
|
+
})
|
|
465
|
+
);
|
|
310
466
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
467
|
+
// Start first connection
|
|
468
|
+
const firstConnectPromise = llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
469
|
+
|
|
470
|
+
// Disconnect invalidates the first connection
|
|
471
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
472
|
+
|
|
473
|
+
// Start second connection before first resolves
|
|
474
|
+
llmChannel.request = sinon.stub().returns(
|
|
475
|
+
new Promise((resolve) => {
|
|
476
|
+
resolveSecondRequest = resolve;
|
|
477
|
+
})
|
|
478
|
+
);
|
|
479
|
+
const secondConnectPromise = llmChannel.registerAndConnect(
|
|
480
|
+
'newLocusUrl',
|
|
481
|
+
'newDatachannelUrl'
|
|
482
|
+
);
|
|
483
|
+
|
|
484
|
+
assert.equal(llmChannel.isConnecting(), true);
|
|
485
|
+
|
|
486
|
+
// Resolve the first (stale) request
|
|
487
|
+
resolveFirstRequest({
|
|
488
|
+
headers: {},
|
|
489
|
+
body: {binding: 'stale-binding', webSocketUrl: 'wss://example.com/stale'},
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
await firstConnectPromise;
|
|
493
|
+
|
|
494
|
+
// isConnecting should still be true because second connection is in progress
|
|
495
|
+
assert.equal(llmChannel.isConnecting(), true);
|
|
496
|
+
|
|
497
|
+
// State should not have been overwritten by stale operation
|
|
498
|
+
assert.equal(llmChannel.getBinding(), undefined);
|
|
499
|
+
|
|
500
|
+
// Complete second connection
|
|
501
|
+
resolveSecondRequest({
|
|
502
|
+
headers: {},
|
|
503
|
+
body: {binding: 'new-binding', webSocketUrl: 'wss://example.com/new'},
|
|
504
|
+
});
|
|
505
|
+
await secondConnectPromise;
|
|
506
|
+
|
|
507
|
+
// Now isConnecting should be false and state should reflect second connection
|
|
508
|
+
assert.equal(llmChannel.isConnecting(), false);
|
|
509
|
+
assert.equal(llmChannel.getBinding(), 'new-binding');
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it('does not call connect() if disconnect runs during isDataChannelTokenEnabled await', async () => {
|
|
513
|
+
let resolveFeatureToggle;
|
|
514
|
+
const featureTogglePromise = new Promise((resolve) => {
|
|
515
|
+
resolveFeatureToggle = resolve;
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
// Request resolves immediately
|
|
519
|
+
llmChannel.request = sinon.stub().resolves({
|
|
520
|
+
headers: {},
|
|
521
|
+
body: {binding: 'binding', webSocketUrl: 'wss://example.com/socket'},
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
// Feature toggle will block
|
|
525
|
+
webex.internal.feature.getFeature = sinon
|
|
526
|
+
.stub()
|
|
527
|
+
.onFirstCall()
|
|
528
|
+
.resolves(true) // First call in register()
|
|
529
|
+
.onSecondCall()
|
|
530
|
+
.returns(featureTogglePromise); // Second call blocks
|
|
531
|
+
|
|
532
|
+
// Start connection
|
|
533
|
+
const connectPromise = llmChannel.registerAndConnect(locusUrl, datachannelUrl);
|
|
534
|
+
|
|
535
|
+
// Wait for register() to complete and state to be set
|
|
536
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
537
|
+
|
|
538
|
+
// Binding should be set after register() completes
|
|
539
|
+
assert.equal(llmChannel.getBinding(), 'binding');
|
|
540
|
+
|
|
541
|
+
// Disconnect while isDataChannelTokenEnabled() is pending
|
|
542
|
+
await llmChannel.disconnect({code: 1000, reason: 'test'});
|
|
543
|
+
|
|
544
|
+
// State should be cleared
|
|
545
|
+
assert.equal(llmChannel.getBinding(), undefined);
|
|
546
|
+
|
|
547
|
+
// Now resolve the feature toggle - stale operation should NOT call connect()
|
|
548
|
+
resolveFeatureToggle(true);
|
|
549
|
+
|
|
550
|
+
await connectPromise;
|
|
551
|
+
|
|
552
|
+
// connect() should NOT have been called - identity check after await should have aborted
|
|
553
|
+
sinon.assert.notCalled(llmChannel.connect);
|
|
314
554
|
});
|
|
315
555
|
});
|
|
316
556
|
|
|
317
557
|
describe('#setRefreshHandler', () => {
|
|
318
|
-
it('stores the provided handler', () => {
|
|
319
|
-
const handler = sinon.stub().resolves({
|
|
320
|
-
|
|
558
|
+
it('stores the provided handler', async () => {
|
|
559
|
+
const handler = sinon.stub().resolves({body: {datachannelToken: 'newToken'}});
|
|
560
|
+
llmChannel.setRefreshHandler(handler);
|
|
561
|
+
|
|
562
|
+
const result = await llmChannel.refreshDataChannelToken();
|
|
321
563
|
|
|
322
|
-
|
|
323
|
-
assert.
|
|
564
|
+
assert.equal(result.body.datachannelToken, 'newToken');
|
|
565
|
+
sinon.assert.calledOnce(handler);
|
|
324
566
|
});
|
|
325
567
|
});
|
|
326
568
|
|
|
327
569
|
describe('#isDataChannelTokenEnabled', () => {
|
|
328
|
-
it('
|
|
329
|
-
webex.internal.feature.getFeature.
|
|
570
|
+
it('returns value from feature toggle', async () => {
|
|
571
|
+
webex.internal.feature.getFeature.resolves(true);
|
|
330
572
|
|
|
331
|
-
const result = await
|
|
573
|
+
const result = await llmChannel.isDataChannelTokenEnabled();
|
|
332
574
|
|
|
333
575
|
sinon.assert.calledOnceWithExactly(
|
|
334
576
|
webex.internal.feature.getFeature,
|
|
@@ -342,26 +584,23 @@ describe('plugin-llm', () => {
|
|
|
342
584
|
|
|
343
585
|
describe('#refreshDataChannelToken', () => {
|
|
344
586
|
it('returns null and logs warn if no handler is set', async () => {
|
|
345
|
-
const warnSpy =
|
|
587
|
+
const warnSpy = llmChannel.logger.warn;
|
|
346
588
|
|
|
347
|
-
const result = await
|
|
589
|
+
const result = await llmChannel.refreshDataChannelToken();
|
|
348
590
|
|
|
349
591
|
assert.equal(result, null);
|
|
350
592
|
|
|
351
593
|
sinon.assert.calledOnce(warnSpy);
|
|
352
|
-
sinon.assert.calledWithMatch(
|
|
353
|
-
warnSpy,
|
|
354
|
-
sinon.match('LLM refreshHandler is not set')
|
|
355
|
-
);
|
|
594
|
+
sinon.assert.calledWithMatch(warnSpy, sinon.match('LLM refreshHandler is not set'));
|
|
356
595
|
});
|
|
357
596
|
|
|
358
597
|
it('returns token when handler resolves', async () => {
|
|
359
|
-
const mockToken = {
|
|
598
|
+
const mockToken = {body: {datachannelToken: 'newToken', datachannelTokenType: 'default'}};
|
|
360
599
|
const handler = sinon.stub().resolves(mockToken);
|
|
361
600
|
|
|
362
|
-
|
|
601
|
+
llmChannel.setRefreshHandler(handler);
|
|
363
602
|
|
|
364
|
-
const token = await
|
|
603
|
+
const token = await llmChannel.refreshDataChannelToken();
|
|
365
604
|
|
|
366
605
|
assert.equal(token, mockToken);
|
|
367
606
|
sinon.assert.calledOnce(handler);
|
|
@@ -369,80 +608,102 @@ describe('plugin-llm', () => {
|
|
|
369
608
|
|
|
370
609
|
it('logs warn and returns null when handler rejects', async () => {
|
|
371
610
|
const handler = sinon.stub().rejects(new Error('throw error'));
|
|
372
|
-
|
|
611
|
+
llmChannel.setRefreshHandler(handler);
|
|
373
612
|
|
|
374
|
-
const warnSpy =
|
|
613
|
+
const warnSpy = llmChannel.logger.warn;
|
|
375
614
|
|
|
376
|
-
const result = await
|
|
615
|
+
const result = await llmChannel.refreshDataChannelToken();
|
|
377
616
|
|
|
378
617
|
assert.equal(result, null);
|
|
379
618
|
|
|
380
619
|
sinon.assert.calledOnce(warnSpy);
|
|
381
|
-
sinon.assert.calledWithMatch(
|
|
382
|
-
warnSpy,
|
|
383
|
-
sinon.match('DataChannel token refresh failed'),
|
|
384
|
-
);
|
|
620
|
+
sinon.assert.calledWithMatch(warnSpy, sinon.match('DataChannel token refresh failed'));
|
|
385
621
|
});
|
|
386
622
|
});
|
|
387
623
|
|
|
388
|
-
describe('#getDatachannelToken / #setDatachannelToken', () => {
|
|
624
|
+
describe('#getDatachannelToken / #setDatachannelToken / #clearDatachannelToken', () => {
|
|
389
625
|
it('sets and gets datachannel token', () => {
|
|
390
|
-
|
|
391
|
-
assert.equal(
|
|
392
|
-
|
|
393
|
-
|
|
626
|
+
llmChannel.setDatachannelToken('abc123');
|
|
627
|
+
assert.equal(llmChannel.getDatachannelToken(), 'abc123');
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it('returns undefined when no token is set', () => {
|
|
631
|
+
assert.equal(llmChannel.getDatachannelToken(), undefined);
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
it('clears datachannel token', () => {
|
|
635
|
+
llmChannel.setDatachannelToken('abc123');
|
|
636
|
+
assert.equal(llmChannel.getDatachannelToken(), 'abc123');
|
|
637
|
+
|
|
638
|
+
llmChannel.clearDatachannelToken();
|
|
639
|
+
assert.equal(llmChannel.getDatachannelToken(), undefined);
|
|
394
640
|
});
|
|
395
641
|
});
|
|
396
642
|
|
|
397
|
-
describe('
|
|
398
|
-
|
|
399
|
-
|
|
643
|
+
describe('.matchesDatachannelRequestUrl', () => {
|
|
644
|
+
it('returns true when request URL starts with registration URL', () => {
|
|
645
|
+
const registrationUrl =
|
|
646
|
+
'https://board.wbx2.com/datachannel/api/v1/locus/ps-encoded/registrations';
|
|
647
|
+
const requestUrl = registrationUrl + '/some-path';
|
|
648
|
+
|
|
649
|
+
const result = LLMChannel.matchesDatachannelRequestUrl(requestUrl, registrationUrl);
|
|
650
|
+
|
|
651
|
+
assert.equal(result, true);
|
|
652
|
+
});
|
|
400
653
|
|
|
401
|
-
it('
|
|
402
|
-
|
|
403
|
-
|
|
654
|
+
it('returns false when URLs do not match', () => {
|
|
655
|
+
const registrationUrl =
|
|
656
|
+
'https://board.wbx2.com/datachannel/api/v1/locus/ps-encoded/registrations';
|
|
657
|
+
const requestUrl = 'https://unknown.example.com/path';
|
|
404
658
|
|
|
405
|
-
|
|
406
|
-
assert.equal(llmService.isConnected('s2'), true);
|
|
407
|
-
assert.equal(llmService.getLocusUrl('s1'), locusUrl);
|
|
408
|
-
assert.equal(llmService.getLocusUrl('s2'), locusUrl2);
|
|
409
|
-
assert.equal(llmService.getDatachannelUrl('s1'), datachannelUrl);
|
|
410
|
-
assert.equal(llmService.getDatachannelUrl('s2'), datachannelUrl2);
|
|
659
|
+
const result = LLMChannel.matchesDatachannelRequestUrl(requestUrl, registrationUrl);
|
|
411
660
|
|
|
412
|
-
|
|
413
|
-
assert.equal(all.has('s1'), true);
|
|
414
|
-
assert.equal(all.has('s2'), true);
|
|
661
|
+
assert.equal(result, false);
|
|
415
662
|
});
|
|
416
663
|
|
|
417
|
-
it('
|
|
418
|
-
|
|
664
|
+
it('returns true when request host is rewritten but pathname matches', () => {
|
|
665
|
+
const registrationUrl =
|
|
666
|
+
'https://board-b.wbx2.com/datachannel/api/v1/locus/ps-encoded/registrations';
|
|
667
|
+
const requestUrl =
|
|
668
|
+
'https://hostmap-rewritten.example.com/datachannel/api/v1/locus/ps-encoded/registrations/events';
|
|
419
669
|
|
|
420
|
-
|
|
421
|
-
await llmService.registerAndConnect(locusUrl2, datachannelUrl2, undefined, 's2');
|
|
670
|
+
const result = LLMChannel.matchesDatachannelRequestUrl(requestUrl, registrationUrl);
|
|
422
671
|
|
|
423
|
-
|
|
424
|
-
|
|
672
|
+
assert.equal(result, true);
|
|
673
|
+
});
|
|
425
674
|
|
|
426
|
-
|
|
675
|
+
it('returns false for empty or null URLs', () => {
|
|
676
|
+
assert.equal(LLMChannel.matchesDatachannelRequestUrl('', 'https://example.com'), false);
|
|
677
|
+
assert.equal(LLMChannel.matchesDatachannelRequestUrl('https://example.com', ''), false);
|
|
678
|
+
assert.equal(LLMChannel.matchesDatachannelRequestUrl(null, 'https://example.com'), false);
|
|
679
|
+
assert.equal(LLMChannel.matchesDatachannelRequestUrl('https://example.com', null), false);
|
|
680
|
+
});
|
|
427
681
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
assert.equal(all.has('s2'), true);
|
|
682
|
+
it('returns false for invalid URLs', () => {
|
|
683
|
+
const result = LLMChannel.matchesDatachannelRequestUrl('not-a-url', 'also-not-a-url');
|
|
431
684
|
|
|
432
|
-
assert.equal(
|
|
685
|
+
assert.equal(result, false);
|
|
433
686
|
});
|
|
687
|
+
});
|
|
434
688
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
689
|
+
describe('.buildUrlWithAwareSubchannels', () => {
|
|
690
|
+
it('adds subscriptionAwareSubchannels query param', () => {
|
|
691
|
+
const baseUrl = 'wss://example.com/socket';
|
|
692
|
+
const subchannels = ['channel1', 'channel2'];
|
|
693
|
+
|
|
694
|
+
const result = LLMChannel.buildUrlWithAwareSubchannels(baseUrl, subchannels);
|
|
695
|
+
|
|
696
|
+
assert.include(result, 'subscriptionAwareSubchannels=channel1%2Cchannel2');
|
|
697
|
+
});
|
|
438
698
|
|
|
439
|
-
|
|
440
|
-
|
|
699
|
+
it('preserves existing query params', () => {
|
|
700
|
+
const baseUrl = 'wss://example.com/socket?existing=param';
|
|
701
|
+
const subchannels = ['channel1'];
|
|
441
702
|
|
|
442
|
-
|
|
703
|
+
const result = LLMChannel.buildUrlWithAwareSubchannels(baseUrl, subchannels);
|
|
443
704
|
|
|
444
|
-
|
|
445
|
-
assert.
|
|
705
|
+
assert.include(result, 'existing=param');
|
|
706
|
+
assert.include(result, 'subscriptionAwareSubchannels=channel1');
|
|
446
707
|
});
|
|
447
708
|
});
|
|
448
709
|
|