@principal-ai/control-tower-core 0.2.2 → 0.3.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
@@ -5485,6 +5485,7 @@ class BaseClient extends TypedEventEmitter {
5485
5485
  reconnectTimer = null;
5486
5486
  lastConnectionUrl = null;
5487
5487
  lastToken = null;
5488
+ pendingRequests = new Map;
5488
5489
  constructor(config) {
5489
5490
  super();
5490
5491
  this.config = config;
@@ -5656,6 +5657,34 @@ class BaseClient extends TypedEventEmitter {
5656
5657
  };
5657
5658
  await this.transport.send(message);
5658
5659
  }
5660
+ async request(type, payload, timeoutMs = 5000) {
5661
+ if (this.connectionState !== "connected") {
5662
+ throw new Error("Client must be connected to send requests");
5663
+ }
5664
+ const id = this.generateId();
5665
+ const message = {
5666
+ id,
5667
+ type,
5668
+ payload,
5669
+ timestamp: Date.now()
5670
+ };
5671
+ return new Promise((resolve, reject) => {
5672
+ const timeout = setTimeout(() => {
5673
+ this.pendingRequests.delete(id);
5674
+ reject(new Error(`Request '${type}' timed out after ${timeoutMs}ms`));
5675
+ }, timeoutMs);
5676
+ this.pendingRequests.set(id, {
5677
+ resolve,
5678
+ reject,
5679
+ timeout
5680
+ });
5681
+ this.transport.send(message).catch((error) => {
5682
+ this.pendingRequests.delete(id);
5683
+ clearTimeout(timeout);
5684
+ reject(error);
5685
+ });
5686
+ });
5687
+ }
5659
5688
  getConnectionState() {
5660
5689
  return this.connectionState;
5661
5690
  }
@@ -5672,6 +5701,13 @@ class BaseClient extends TypedEventEmitter {
5672
5701
  return this.userId;
5673
5702
  }
5674
5703
  async handleMessage(message) {
5704
+ if (message.id && this.pendingRequests.has(message.id)) {
5705
+ const pending = this.pendingRequests.get(message.id);
5706
+ clearTimeout(pending.timeout);
5707
+ this.pendingRequests.delete(message.id);
5708
+ pending.resolve(message.payload);
5709
+ return;
5710
+ }
5675
5711
  try {
5676
5712
  switch (message.type) {
5677
5713
  case "auth_result":
@@ -5874,13 +5910,46 @@ class PresenceClient extends TypedEventEmitter {
5874
5910
  }
5875
5911
  }
5876
5912
  async getOnlineUsers() {
5877
- throw new Error("getOnlineUsers() requires a REST API endpoint. " + "Implement this method based on your server API.");
5913
+ const response = await this.client.request("presence:get_users", {});
5914
+ return response.users;
5878
5915
  }
5879
- async setStatus(status, message) {
5880
- throw new Error("setStatus() requires a REST API endpoint. " + "Implement this method based on your server API.");
5916
+ async getUserPresence(userId) {
5917
+ const response = await this.client.request("presence:get_user", { userId });
5918
+ return response.user;
5919
+ }
5920
+ async getRepoUsers(owner, repo) {
5921
+ const response = await this.client.request("presence:get_repo_users", { owner, repo });
5922
+ return response.users;
5923
+ }
5924
+ async setStatus(status, statusMessage) {
5925
+ const response = await this.client.request("presence:set_status", { status, statusMessage });
5926
+ if (!response.success) {
5927
+ throw new Error(response.error || "Failed to set status");
5928
+ }
5881
5929
  }
5882
5930
  async setVisibility(visible) {
5883
- throw new Error("setVisibility() requires a REST API endpoint. " + "Implement this method based on your server API.");
5931
+ const response = await this.client.request("presence:set_visibility", { visible });
5932
+ if (!response.success) {
5933
+ throw new Error(response.error || "Failed to set visibility");
5934
+ }
5935
+ }
5936
+ async reportRepoOpened(repoId, branch) {
5937
+ const response = await this.client.request("presence:repo_open", { repoId, branch });
5938
+ if (!response.success) {
5939
+ throw new Error(response.error || "Failed to report repo opened");
5940
+ }
5941
+ }
5942
+ async reportRepoClosed(repoId) {
5943
+ const response = await this.client.request("presence:repo_close", { repoId });
5944
+ if (!response.success) {
5945
+ throw new Error(response.error || "Failed to report repo closed");
5946
+ }
5947
+ }
5948
+ async reportRepoFocused(repoId) {
5949
+ const response = await this.client.request("presence:repo_focus", { repoId });
5950
+ if (!response.success) {
5951
+ throw new Error(response.error || "Failed to report repo focused");
5952
+ }
5884
5953
  }
5885
5954
  getClient() {
5886
5955
  return this.client;
@@ -6504,11 +6573,23 @@ class BaseServer extends TypedEventEmitter {
6504
6573
  timestamp: Date.now()
6505
6574
  });
6506
6575
  break;
6507
- default:
6576
+ default: {
6577
+ if (this.config.customMessageHandler) {
6578
+ const sendResponse = async (response) => {
6579
+ await this.sendToClient(clientId, {
6580
+ id: message.id,
6581
+ ...response
6582
+ });
6583
+ };
6584
+ const handled = await this.config.customMessageHandler(clientId, message, sendResponse);
6585
+ if (handled)
6586
+ break;
6587
+ }
6508
6588
  await this.sendToClient(clientId, {
6509
6589
  type: "error",
6510
6590
  error: `Unknown message type: ${message.type}`
6511
6591
  });
6592
+ }
6512
6593
  }
6513
6594
  } catch (error) {
6514
6595
  await this.sendToClient(clientId, {
@@ -6856,6 +6937,7 @@ class ServerBuilder {
6856
6937
  webSocketPath;
6857
6938
  webSocketServer;
6858
6939
  experimentalFeatures;
6940
+ customMessageHandler;
6859
6941
  withTransport(transport) {
6860
6942
  this.transport = transport;
6861
6943
  return this;
@@ -6900,6 +6982,10 @@ class ServerBuilder {
6900
6982
  this.experimentalFeatures = features;
6901
6983
  return this;
6902
6984
  }
6985
+ withCustomMessageHandler(handler) {
6986
+ this.customMessageHandler = handler;
6987
+ return this;
6988
+ }
6903
6989
  build() {
6904
6990
  if (!this.transport) {
6905
6991
  throw new Error("Transport adapter is required");
@@ -6929,7 +7015,8 @@ class ServerBuilder {
6929
7015
  enableBroadcast: false,
6930
7016
  warnOnExperimentalUse: true,
6931
7017
  trackUsageMetrics: false
6932
- }
7018
+ },
7019
+ customMessageHandler: this.customMessageHandler
6933
7020
  };
6934
7021
  const server = new BaseServer(config);
6935
7022
  if (this.roomManager.constructor.name === "DefaultRoomManager") {
@@ -7003,4 +7090,4 @@ export {
7003
7090
  BaseClient
7004
7091
  };
7005
7092
 
7006
- //# debugId=A5E31B6ED779287364756E2164756E21
7093
+ //# debugId=9959D96F79712BE364756E2164756E21