index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const express = require('express');
  2. const http = require('http');
  3. const WebSocket = require('ws');
  4. const chokidar = require('chokidar');
  5. const fs = require('fs');
  6. const path = require('path');
  7. // Use provided port or pick a random high port (49152-65535)
  8. const PORT = process.env.BRAINSTORM_PORT || (49152 + Math.floor(Math.random() * 16383));
  9. const SCREEN_DIR = process.env.BRAINSTORM_DIR || '/tmp/brainstorm';
  10. // Ensure screen directory exists
  11. if (!fs.existsSync(SCREEN_DIR)) {
  12. fs.mkdirSync(SCREEN_DIR, { recursive: true });
  13. }
  14. // Find the newest .html file in the directory by mtime
  15. function getNewestScreen() {
  16. const files = fs.readdirSync(SCREEN_DIR)
  17. .filter(f => f.endsWith('.html'))
  18. .map(f => ({
  19. name: f,
  20. path: path.join(SCREEN_DIR, f),
  21. mtime: fs.statSync(path.join(SCREEN_DIR, f)).mtime.getTime()
  22. }))
  23. .sort((a, b) => b.mtime - a.mtime);
  24. return files.length > 0 ? files[0].path : null;
  25. }
  26. // Default waiting page (served when no screens exist yet)
  27. const WAITING_PAGE = `<!DOCTYPE html>
  28. <html>
  29. <head>
  30. <title>Brainstorm Companion</title>
  31. <style>
  32. body { font-family: system-ui, sans-serif; padding: 2rem; max-width: 800px; margin: 0 auto; }
  33. h1 { color: #333; }
  34. p { color: #666; }
  35. </style>
  36. </head>
  37. <body>
  38. <h1>Brainstorm Companion</h1>
  39. <p>Waiting for Claude to push a screen...</p>
  40. </body>
  41. </html>`;
  42. const app = express();
  43. const server = http.createServer(app);
  44. const wss = new WebSocket.Server({ server });
  45. // Track connected browsers for reload notifications
  46. const clients = new Set();
  47. wss.on('connection', (ws) => {
  48. clients.add(ws);
  49. ws.on('close', () => clients.delete(ws));
  50. ws.on('message', (data) => {
  51. // User interaction event - write to stdout for Claude
  52. const event = JSON.parse(data.toString());
  53. console.log(JSON.stringify({ source: 'user-event', ...event }));
  54. });
  55. });
  56. // Serve newest screen with helper.js injected
  57. app.get('/', (req, res) => {
  58. const screenFile = getNewestScreen();
  59. let html = screenFile ? fs.readFileSync(screenFile, 'utf-8') : WAITING_PAGE;
  60. // Inject helper script before </body>
  61. const helperScript = fs.readFileSync(path.join(__dirname, 'helper.js'), 'utf-8');
  62. const injection = `<script>\n${helperScript}\n</script>`;
  63. if (html.includes('</body>')) {
  64. html = html.replace('</body>', `${injection}\n</body>`);
  65. } else {
  66. html += injection;
  67. }
  68. res.type('html').send(html);
  69. });
  70. // Watch for new or changed .html files in the directory
  71. chokidar.watch(SCREEN_DIR, { ignoreInitial: true })
  72. .on('add', (filePath) => {
  73. if (filePath.endsWith('.html')) {
  74. console.log(JSON.stringify({ type: 'screen-added', file: filePath }));
  75. // Notify all browsers to reload
  76. clients.forEach(ws => {
  77. if (ws.readyState === WebSocket.OPEN) {
  78. ws.send(JSON.stringify({ type: 'reload' }));
  79. }
  80. });
  81. }
  82. })
  83. .on('change', (filePath) => {
  84. if (filePath.endsWith('.html')) {
  85. console.log(JSON.stringify({ type: 'screen-updated', file: filePath }));
  86. clients.forEach(ws => {
  87. if (ws.readyState === WebSocket.OPEN) {
  88. ws.send(JSON.stringify({ type: 'reload' }));
  89. }
  90. });
  91. }
  92. });
  93. server.listen(PORT, '127.0.0.1', () => {
  94. console.log(JSON.stringify({
  95. type: 'server-started',
  96. port: PORT,
  97. url: `http://localhost:${PORT}`,
  98. screen_dir: SCREEN_DIR
  99. }));
  100. });