@webex/internal-plugin-llm 3.12.0-webex-services-ready.4 → 3.13.0

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.
@@ -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;