oomi-ai 0.10.2 → 0.12.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.
@@ -1,78 +1,78 @@
1
- const WS_CONNECTING = 0;
2
- const WS_OPEN = 1;
3
-
4
- /**
5
- * Ensure session state exists so client frames can be buffered before client.open arrives.
6
- */
7
- export function ensureSessionBridge({ sessions, sessionId, createSocket }) {
8
- const id = String(sessionId || '').trim();
9
- if (!id) return null;
10
-
11
- const existing = sessions.get(id);
12
- if (existing) return existing;
13
-
14
- const socket = createSocket(id);
15
- const next = {
16
- socket,
17
- queue: [],
18
- connectAccepted: false,
19
- waitingForConnect: [],
20
- };
21
- sessions.set(id, next);
22
- return next;
23
- }
24
-
25
- /**
26
- * Forward a frame to the gateway socket or queue it while connecting.
27
- */
28
- export function forwardFrameToSession(sessionBridge, frameText, options = {}) {
29
- if (!sessionBridge || !sessionBridge.socket || typeof frameText !== 'string' || !frameText) {
30
- return 'dropped';
31
- }
32
-
33
- if (options.requiresConnectAccepted === true && sessionBridge.connectAccepted !== true) {
34
- if (!Array.isArray(sessionBridge.waitingForConnect)) {
35
- sessionBridge.waitingForConnect = [];
36
- }
37
- sessionBridge.waitingForConnect.push(frameText);
38
- return 'waiting_for_connect';
39
- }
40
-
41
- const { socket } = sessionBridge;
42
- if (socket.readyState === WS_OPEN) {
43
- socket.send(frameText);
44
- return 'sent';
45
- }
46
-
47
- if (socket.readyState === WS_CONNECTING) {
48
- sessionBridge.queue.push(frameText);
49
- return 'queued';
50
- }
51
-
52
- return 'dropped';
53
- }
54
-
55
- export function flushWaitingForConnect(sessionBridge) {
56
- if (!sessionBridge) return [];
57
-
58
- sessionBridge.connectAccepted = true;
59
- const pending = Array.isArray(sessionBridge.waitingForConnect)
60
- ? sessionBridge.waitingForConnect.splice(0, sessionBridge.waitingForConnect.length)
61
- : [];
62
-
63
- return pending.map((frameText) => ({
64
- frameText,
65
- result: forwardFrameToSession(sessionBridge, frameText),
66
- }));
67
- }
68
-
69
- export function flushSessionQueue(sessionBridge) {
70
- if (!sessionBridge || !sessionBridge.socket) return;
71
- const socket = sessionBridge.socket;
72
- while (sessionBridge.queue.length > 0 && socket.readyState === WS_OPEN) {
73
- const nextFrame = sessionBridge.queue.shift();
74
- if (typeof nextFrame === 'string' && nextFrame) {
75
- socket.send(nextFrame);
76
- }
77
- }
78
- }
1
+ const WS_CONNECTING = 0;
2
+ const WS_OPEN = 1;
3
+
4
+ /**
5
+ * Ensure session state exists so client frames can be buffered before client.open arrives.
6
+ */
7
+ export function ensureSessionBridge({ sessions, sessionId, createSocket }) {
8
+ const id = String(sessionId || '').trim();
9
+ if (!id) return null;
10
+
11
+ const existing = sessions.get(id);
12
+ if (existing) return existing;
13
+
14
+ const socket = createSocket(id);
15
+ const next = {
16
+ socket,
17
+ queue: [],
18
+ connectAccepted: false,
19
+ waitingForConnect: [],
20
+ };
21
+ sessions.set(id, next);
22
+ return next;
23
+ }
24
+
25
+ /**
26
+ * Forward a frame to the gateway socket or queue it while connecting.
27
+ */
28
+ export function forwardFrameToSession(sessionBridge, frameText, options = {}) {
29
+ if (!sessionBridge || !sessionBridge.socket || typeof frameText !== 'string' || !frameText) {
30
+ return 'dropped';
31
+ }
32
+
33
+ if (options.requiresConnectAccepted === true && sessionBridge.connectAccepted !== true) {
34
+ if (!Array.isArray(sessionBridge.waitingForConnect)) {
35
+ sessionBridge.waitingForConnect = [];
36
+ }
37
+ sessionBridge.waitingForConnect.push(frameText);
38
+ return 'waiting_for_connect';
39
+ }
40
+
41
+ const { socket } = sessionBridge;
42
+ if (socket.readyState === WS_OPEN) {
43
+ socket.send(frameText);
44
+ return 'sent';
45
+ }
46
+
47
+ if (socket.readyState === WS_CONNECTING) {
48
+ sessionBridge.queue.push(frameText);
49
+ return 'queued';
50
+ }
51
+
52
+ return 'dropped';
53
+ }
54
+
55
+ export function flushWaitingForConnect(sessionBridge) {
56
+ if (!sessionBridge) return [];
57
+
58
+ sessionBridge.connectAccepted = true;
59
+ const pending = Array.isArray(sessionBridge.waitingForConnect)
60
+ ? sessionBridge.waitingForConnect.splice(0, sessionBridge.waitingForConnect.length)
61
+ : [];
62
+
63
+ return pending.map((frameText) => ({
64
+ frameText,
65
+ result: forwardFrameToSession(sessionBridge, frameText),
66
+ }));
67
+ }
68
+
69
+ export function flushSessionQueue(sessionBridge) {
70
+ if (!sessionBridge || !sessionBridge.socket) return;
71
+ const socket = sessionBridge.socket;
72
+ while (sessionBridge.queue.length > 0 && socket.readyState === WS_OPEN) {
73
+ const nextFrame = sessionBridge.queue.shift();
74
+ if (typeof nextFrame === 'string' && nextFrame) {
75
+ socket.send(nextFrame);
76
+ }
77
+ }
78
+ }