@principal-ai/control-tower-core 0.2.0 → 0.2.1

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/dist/index.mjs CHANGED
@@ -2856,6 +2856,134 @@ var require_websocket_server = __commonJS((exports, module) => {
2856
2856
  }
2857
2857
  }
2858
2858
  });
2859
+ // src/abstractions/RoomManager.ts
2860
+ class RoomManager {
2861
+ rooms = new Map;
2862
+ }
2863
+
2864
+ // src/abstractions/DefaultRoomManager.ts
2865
+ class DefaultRoomManager extends RoomManager {
2866
+ async createRoom(id, config) {
2867
+ const room = {
2868
+ id,
2869
+ name: config.name || id,
2870
+ createdAt: Date.now(),
2871
+ maxUsers: config.maxUsers,
2872
+ maxHistory: config.maxHistory,
2873
+ permissions: config.permissions,
2874
+ metadata: config.metadata || {}
2875
+ };
2876
+ const roomState = {
2877
+ room,
2878
+ users: new Map,
2879
+ eventHistory: [],
2880
+ locks: new Map
2881
+ };
2882
+ this.rooms.set(id, roomState);
2883
+ return room;
2884
+ }
2885
+ async deleteRoom(id) {
2886
+ this.rooms.delete(id);
2887
+ }
2888
+ async joinRoom(roomId, user) {
2889
+ const roomState = this.rooms.get(roomId);
2890
+ if (!roomState) {
2891
+ throw new Error(`Room ${roomId} not found`);
2892
+ }
2893
+ if (roomState.room.maxUsers && roomState.users.size >= roomState.room.maxUsers) {
2894
+ throw new Error(`Room ${roomId} is full`);
2895
+ }
2896
+ roomState.users.set(user.id, user);
2897
+ }
2898
+ async leaveRoom(roomId, userId) {
2899
+ const roomState = this.rooms.get(roomId);
2900
+ if (!roomState) {
2901
+ return;
2902
+ }
2903
+ roomState.users.delete(userId);
2904
+ }
2905
+ async broadcastToRoom(roomId, event, _excludeUserId) {
2906
+ const roomState = this.rooms.get(roomId);
2907
+ if (!roomState) {
2908
+ throw new Error(`Room ${roomId} not found`);
2909
+ }
2910
+ await this.addEventToHistory(roomId, event);
2911
+ }
2912
+ async getRoomState(roomId) {
2913
+ return this.rooms.get(roomId) || null;
2914
+ }
2915
+ async getUsersInRoom(roomId) {
2916
+ const roomState = this.rooms.get(roomId);
2917
+ if (!roomState) {
2918
+ return [];
2919
+ }
2920
+ return Array.from(roomState.users.values());
2921
+ }
2922
+ async addEventToHistory(roomId, event) {
2923
+ const roomState = this.rooms.get(roomId);
2924
+ if (!roomState) {
2925
+ throw new Error(`Room ${roomId} not found`);
2926
+ }
2927
+ roomState.eventHistory.push(event);
2928
+ if (roomState.room.maxHistory && roomState.eventHistory.length > roomState.room.maxHistory) {
2929
+ roomState.eventHistory = roomState.eventHistory.slice(-roomState.room.maxHistory);
2930
+ }
2931
+ }
2932
+ async getEventHistory(roomId, limit) {
2933
+ const roomState = this.rooms.get(roomId);
2934
+ if (!roomState) {
2935
+ return [];
2936
+ }
2937
+ const history = roomState.eventHistory;
2938
+ return limit ? history.slice(-limit) : history;
2939
+ }
2940
+ async updateUserStatus(roomId, userId, status) {
2941
+ const roomState = this.rooms.get(roomId);
2942
+ if (!roomState) {
2943
+ throw new Error(`Room ${roomId} not found`);
2944
+ }
2945
+ const user = roomState.users.get(userId);
2946
+ if (user) {
2947
+ user.status = status;
2948
+ user.lastActivity = Date.now();
2949
+ }
2950
+ }
2951
+ }
2952
+
2953
+ // src/abstractions/ConnectedRoomManager.ts
2954
+ class ConnectedRoomManager extends DefaultRoomManager {
2955
+ sendToClient;
2956
+ getClientIdsInRoom;
2957
+ constructor(sendToClient, getClientIdsInRoom) {
2958
+ super();
2959
+ this.sendToClient = sendToClient;
2960
+ this.getClientIdsInRoom = getClientIdsInRoom;
2961
+ }
2962
+ async broadcastToRoom(roomId, event, excludeUserId) {
2963
+ const roomState = this.rooms.get(roomId);
2964
+ if (!roomState) {
2965
+ throw new Error(`Room ${roomId} not found`);
2966
+ }
2967
+ await this.addEventToHistory(roomId, event);
2968
+ const users = Array.from(roomState.users.values());
2969
+ const sendPromises = [];
2970
+ for (const user of users) {
2971
+ if (excludeUserId && user.id === excludeUserId) {
2972
+ continue;
2973
+ }
2974
+ const clientIds = this.getClientIdsInRoom(roomId, user.id);
2975
+ for (const clientId of clientIds) {
2976
+ sendPromises.push(this.sendToClient(clientId, {
2977
+ type: "event_received",
2978
+ event
2979
+ }).catch((error) => {
2980
+ console.error(`[ConnectedRoomManager] Failed to send event to client ${clientId}:`, error);
2981
+ }));
2982
+ }
2983
+ }
2984
+ await Promise.all(sendPromises);
2985
+ }
2986
+ }
2859
2987
  // src/abstractions/LockManager.ts
2860
2988
  class LockManager {
2861
2989
  lockState = {
@@ -3374,99 +3502,6 @@ class DefaultPresenceManager extends PresenceManager {
3374
3502
  return this.userPresences.size + this.gracePeriodEntries.size;
3375
3503
  }
3376
3504
  }
3377
- // src/abstractions/RoomManager.ts
3378
- class RoomManager {
3379
- rooms = new Map;
3380
- }
3381
-
3382
- // src/abstractions/DefaultRoomManager.ts
3383
- class DefaultRoomManager extends RoomManager {
3384
- async createRoom(id, config) {
3385
- const room = {
3386
- id,
3387
- name: config.name || id,
3388
- createdAt: Date.now(),
3389
- maxUsers: config.maxUsers,
3390
- maxHistory: config.maxHistory,
3391
- permissions: config.permissions,
3392
- metadata: config.metadata || {}
3393
- };
3394
- const roomState = {
3395
- room,
3396
- users: new Map,
3397
- eventHistory: [],
3398
- locks: new Map
3399
- };
3400
- this.rooms.set(id, roomState);
3401
- return room;
3402
- }
3403
- async deleteRoom(id) {
3404
- this.rooms.delete(id);
3405
- }
3406
- async joinRoom(roomId, user) {
3407
- const roomState = this.rooms.get(roomId);
3408
- if (!roomState) {
3409
- throw new Error(`Room ${roomId} not found`);
3410
- }
3411
- if (roomState.room.maxUsers && roomState.users.size >= roomState.room.maxUsers) {
3412
- throw new Error(`Room ${roomId} is full`);
3413
- }
3414
- roomState.users.set(user.id, user);
3415
- }
3416
- async leaveRoom(roomId, userId) {
3417
- const roomState = this.rooms.get(roomId);
3418
- if (!roomState) {
3419
- return;
3420
- }
3421
- roomState.users.delete(userId);
3422
- }
3423
- async broadcastToRoom(roomId, event, _excludeUserId) {
3424
- const roomState = this.rooms.get(roomId);
3425
- if (!roomState) {
3426
- throw new Error(`Room ${roomId} not found`);
3427
- }
3428
- await this.addEventToHistory(roomId, event);
3429
- }
3430
- async getRoomState(roomId) {
3431
- return this.rooms.get(roomId) || null;
3432
- }
3433
- async getUsersInRoom(roomId) {
3434
- const roomState = this.rooms.get(roomId);
3435
- if (!roomState) {
3436
- return [];
3437
- }
3438
- return Array.from(roomState.users.values());
3439
- }
3440
- async addEventToHistory(roomId, event) {
3441
- const roomState = this.rooms.get(roomId);
3442
- if (!roomState) {
3443
- throw new Error(`Room ${roomId} not found`);
3444
- }
3445
- roomState.eventHistory.push(event);
3446
- if (roomState.room.maxHistory && roomState.eventHistory.length > roomState.room.maxHistory) {
3447
- roomState.eventHistory = roomState.eventHistory.slice(-roomState.room.maxHistory);
3448
- }
3449
- }
3450
- async getEventHistory(roomId, limit) {
3451
- const roomState = this.rooms.get(roomId);
3452
- if (!roomState) {
3453
- return [];
3454
- }
3455
- const history = roomState.eventHistory;
3456
- return limit ? history.slice(-limit) : history;
3457
- }
3458
- async updateUserStatus(roomId, userId, status) {
3459
- const roomState = this.rooms.get(roomId);
3460
- if (!roomState) {
3461
- throw new Error(`Room ${roomId} not found`);
3462
- }
3463
- const user = roomState.users.get(userId);
3464
- if (user) {
3465
- user.status = status;
3466
- user.lastActivity = Date.now();
3467
- }
3468
- }
3469
- }
3470
3505
  // src/abstractions/EventEmitter.ts
3471
3506
  class TypedEventEmitter {
3472
3507
  listeners = new Map;
@@ -6764,6 +6799,13 @@ class BaseServer extends TypedEventEmitter {
6764
6799
  getClientIdsForUser(userId) {
6765
6800
  return Array.from(this.userClientMap.get(userId) || []);
6766
6801
  }
6802
+ getClientIdsInRoom(roomId, userId) {
6803
+ const allClientIds = this.getClientIdsForUser(userId);
6804
+ return allClientIds.filter((clientId) => {
6805
+ const client = this.clients.get(clientId);
6806
+ return client && client.roomIds.has(roomId);
6807
+ });
6808
+ }
6767
6809
  getDeviceIdFromClientId(clientId) {
6768
6810
  return clientId;
6769
6811
  }
@@ -6857,6 +6899,11 @@ class ServerBuilder {
6857
6899
  }
6858
6900
  };
6859
6901
  const server = new BaseServer(config);
6902
+ if (this.roomManager.constructor.name === "DefaultRoomManager") {
6903
+ const connectedManager = new ConnectedRoomManager((clientId, message) => server.sendToClient(clientId, message), (roomId, userId) => server.getClientIdsInRoom(roomId, userId));
6904
+ connectedManager.rooms = this.roomManager.rooms;
6905
+ server.roomManager = connectedManager;
6906
+ }
6860
6907
  if (this.presenceManager instanceof DefaultPresenceManager) {
6861
6908
  this.presenceManager.setServer(server);
6862
6909
  }
@@ -6916,10 +6963,11 @@ export {
6916
6963
  DefaultRoomManager,
6917
6964
  DefaultPresenceManager,
6918
6965
  DefaultLockManager,
6966
+ ConnectedRoomManager,
6919
6967
  ClientBuilder,
6920
6968
  BrowserWebSocketTransportAdapter,
6921
6969
  BaseServer,
6922
6970
  BaseClient
6923
6971
  };
6924
6972
 
6925
- //# debugId=94389897A03B4D7064756E2164756E21
6973
+ //# debugId=AC1FE1DD89B6DDC464756E2164756E21