@beastmode-develeap/beastmode 0.1.132 → 0.1.134

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.js CHANGED
@@ -3616,6 +3616,9 @@ import { readFileSync as readFileSync11, writeFileSync as writeFileSync11, exist
3616
3616
  import { join as join12 } from "path";
3617
3617
  import http from "http";
3618
3618
  import { WebSocketServer, WebSocket as WsWebSocket } from "ws";
3619
+ function getRecentTokenUsage() {
3620
+ return recentTokenUsage.slice();
3621
+ }
3619
3622
  function boardGet(path, boardUrl) {
3620
3623
  return new Promise((resolve20) => {
3621
3624
  const url = new URL(path, boardUrl);
@@ -4544,10 +4547,12 @@ Respond concisely. Continue the conversation naturally.`;
4544
4547
  }
4545
4548
  return { sessions, startSession, stopSession, handleUpgrade, shutdown };
4546
4549
  }
4550
+ var recentTokenUsage;
4547
4551
  var init_chat_handler = __esm({
4548
4552
  "src/cli/ui/chat-handler.ts"() {
4549
4553
  "use strict";
4550
4554
  init_inception();
4555
+ recentTokenUsage = [];
4551
4556
  }
4552
4557
  });
4553
4558
 
@@ -6312,6 +6317,45 @@ import { createHmac } from "crypto";
6312
6317
  import { readFileSync as readFileSync13, existsSync as existsSync16 } from "fs";
6313
6318
  import { join as join14, dirname as dirname6 } from "path";
6314
6319
  import { fileURLToPath as fileURLToPath2 } from "url";
6320
+ import { WebSocketServer as WebSocketServer2, WebSocket as WsClient } from "ws";
6321
+ function proxyBoardWebSocket(req, socket, head, boardWsUrl) {
6322
+ _boardWsProxy.handleUpgrade(req, socket, head, (clientWs) => {
6323
+ const upstream = new WsClient(boardWsUrl);
6324
+ let closed = false;
6325
+ const cleanup = (code, reason) => {
6326
+ if (closed) return;
6327
+ closed = true;
6328
+ try {
6329
+ clientWs.close(code, reason);
6330
+ } catch {
6331
+ }
6332
+ try {
6333
+ upstream.close(code, reason);
6334
+ } catch {
6335
+ }
6336
+ };
6337
+ upstream.on("open", () => {
6338
+ clientWs.on("message", (data) => {
6339
+ try {
6340
+ upstream.send(data);
6341
+ } catch {
6342
+ cleanup(1011, "relay failed");
6343
+ }
6344
+ });
6345
+ upstream.on("message", (data) => {
6346
+ try {
6347
+ clientWs.send(data);
6348
+ } catch {
6349
+ cleanup(1011, "relay failed");
6350
+ }
6351
+ });
6352
+ });
6353
+ upstream.on("error", () => cleanup(1011, "board WS upstream error"));
6354
+ upstream.on("close", (code, reason) => cleanup(code || 1e3, reason?.toString() || "upstream closed"));
6355
+ clientWs.on("error", () => cleanup(1011, "client WS error"));
6356
+ clientWs.on("close", (code, reason) => cleanup(code || 1e3, reason?.toString() || "client closed"));
6357
+ });
6358
+ }
6315
6359
  function resolveStaticDir() {
6316
6360
  const devPath = join14(__dirname, "static");
6317
6361
  if (existsSync16(join14(devPath, "index.html"))) {
@@ -6387,6 +6431,7 @@ function isAuthExempt(method, url) {
6387
6431
  if (url.startsWith("/static/")) return true;
6388
6432
  if (url === "/health" && method === "GET") return true;
6389
6433
  if (url === "/api/build-stamp" && method === "GET") return true;
6434
+ if (url === "/api/token-usage/recent" && method === "GET") return true;
6390
6435
  return false;
6391
6436
  }
6392
6437
  function setSessionCookie(res) {
@@ -6519,6 +6564,10 @@ async function startServer(options = {}) {
6519
6564
  sendJson(res, 200, { status: "ok", uptime: process.uptime() });
6520
6565
  return;
6521
6566
  }
6567
+ if (method === "GET" && url === "/api/token-usage/recent") {
6568
+ sendJson(res, 200, getRecentTokenUsage());
6569
+ return;
6570
+ }
6522
6571
  if (method === "GET" && url === "/api/build-stamp") {
6523
6572
  let stamp = "unknown";
6524
6573
  let commit_sha = null;
@@ -6692,17 +6741,30 @@ async function startServer(options = {}) {
6692
6741
  }
6693
6742
  sendJson(res, 404, { error: "Not found" });
6694
6743
  });
6695
- if (chatManager) {
6696
- server.on("upgrade", (req, socket, head) => {
6697
- const url = (req.url || "").split("?")[0];
6698
- if (url === "/ws/chat") {
6699
- chatManager.handleUpgrade(req, socket, head);
6700
- } else {
6701
- socket.write("HTTP/1.1 404 Not Found\r\n\r\n");
6744
+ server.on("upgrade", (req, socket, head) => {
6745
+ const url = (req.url || "").split("?")[0];
6746
+ if (url === "/ws/chat") {
6747
+ if (!chatManager) {
6748
+ socket.write("HTTP/1.1 503 Service Unavailable\r\n\r\n");
6702
6749
  socket.end();
6750
+ return;
6703
6751
  }
6704
- });
6705
- }
6752
+ if (options.factoryPath && chatManager.sessions.size === 0) {
6753
+ chatManager.startSession(options.factoryPath);
6754
+ }
6755
+ chatManager.handleUpgrade(req, socket, head);
6756
+ return;
6757
+ }
6758
+ if (url === "/ws") {
6759
+ const factoryDir = options.factoryPath || process.cwd();
6760
+ const boardUrl = getBoardUrl2(factoryDir);
6761
+ const wsBoardUrl = boardUrl.replace(/^http/, "ws").replace(/\/$/, "") + "/ws";
6762
+ proxyBoardWebSocket(req, socket, head, wsBoardUrl);
6763
+ return;
6764
+ }
6765
+ socket.write("HTTP/1.1 404 Not Found\r\n\r\n");
6766
+ socket.end();
6767
+ });
6706
6768
  async function shutdown() {
6707
6769
  clearTimeout(inactivityTimer);
6708
6770
  if (chatManager) {
@@ -6733,13 +6795,14 @@ async function startServer(options = {}) {
6733
6795
  });
6734
6796
  });
6735
6797
  }
6736
- var __filename, __dirname, INACTIVITY_TIMEOUT_MS, UI_PASSWORD, AUTH_COOKIE_NAME, AUTH_COOKIE_MAX_AGE, LOGIN_PAGE;
6798
+ var _boardWsProxy, __filename, __dirname, INACTIVITY_TIMEOUT_MS, UI_PASSWORD, AUTH_COOKIE_NAME, AUTH_COOKIE_MAX_AGE, LOGIN_PAGE;
6737
6799
  var init_server = __esm({
6738
6800
  "src/cli/ui/server.ts"() {
6739
6801
  "use strict";
6740
6802
  init_api_routes();
6741
6803
  init_board_api_routes();
6742
6804
  init_chat_handler();
6805
+ _boardWsProxy = new WebSocketServer2({ noServer: true });
6743
6806
  __filename = fileURLToPath2(import.meta.url);
6744
6807
  __dirname = dirname6(__filename);
6745
6808
  INACTIVITY_TIMEOUT_MS = 30 * 60 * 1e3;