diff options
| author | Jesse Vincent <jesse@primeradiant.com> | 2026-06-09 15:26:19 -0700 |
|---|---|---|
| committer | Drew Ritter <drew@primeradiant.com> | 2026-06-16 10:09:45 -0700 |
| commit | 463dfb7fd4737e51c2a17d5beae6b4e510f88b6f (patch) | |
| tree | b8399420363d0463c2052c10cba8f9bce83cc648 /skills/brainstorming/scripts/server.cjs | |
| parent | dd9fcc21ee08c36b66ca7d730abd28f679ccf1f5 (diff) | |
feat(brainstorm-server): opt-in auto-open of the browser on the first screen
When the user approves the visual companion, open their browser automatically the
first time a screen is actually ready to show — rather than at startup (just the
waiting page) or making them open the URL by hand.
Opt-in and gated on approval: off unless BRAINSTORM_OPEN is set (start-server.sh
--open, which the agent passes only after the user agrees to use the companion).
Even then it fires once, and is skipped if a browser is already connected, on a
non-loopback/remote bind, or when headless. Launcher is the platform default
(open / xdg-open / WSL cmd.exe) or BRAINSTORM_OPEN_CMD; best-effort, never fatal.
lifecycle.test.js: opens once on the first screen when approved; does NOT open
without approval.
Closes #755
Refs #759
Diffstat (limited to 'skills/brainstorming/scripts/server.cjs')
| -rw-r--r-- | skills/brainstorming/scripts/server.cjs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/skills/brainstorming/scripts/server.cjs b/skills/brainstorming/scripts/server.cjs index 30b92b1..7e4e1b2 100644 --- a/skills/brainstorming/scripts/server.cjs +++ b/skills/brainstorming/scripts/server.cjs @@ -267,6 +267,27 @@ function broadcast(msg) { } } +// Best-effort: open the user's browser the first time a screen is actually ready +// to show. Skips when disabled, on a non-loopback (remote) bind, or when a +// browser is already connected. Override the launcher with BRAINSTORM_OPEN_CMD. +let browserOpened = false; +function maybeOpenBrowser() { + if (browserOpened) return; + browserOpened = true; + if (!process.env.BRAINSTORM_OPEN) return; // opt-in: only after the user approves the companion + if (HOST !== '127.0.0.1' && HOST !== 'localhost') return; + if (clients.size > 0) return; // the user already opened it + const url = 'http://' + URL_HOST + ':' + PORT; + let cmd = process.env.BRAINSTORM_OPEN_CMD; + if (!cmd) { + if (process.platform === 'darwin') cmd = 'open'; + else if (/microsoft/i.test(require('os').release())) cmd = 'cmd.exe /c start ""'; // WSL → Windows browser + else if (process.env.DISPLAY || process.env.WAYLAND_DISPLAY) cmd = 'xdg-open'; + else return; // headless: nothing to open + } + try { require('child_process').exec(cmd + ' ' + JSON.stringify(url), () => {}); } catch (e) { /* best effort */ } +} + // ========== Activity Tracking ========== // Idle timeout: shut down after this long with no activity. Default 4 hours; @@ -323,6 +344,7 @@ function startServer() { const eventsFile = path.join(STATE_DIR, 'events'); if (fs.existsSync(eventsFile)) fs.unlinkSync(eventsFile); console.log(JSON.stringify({ type: 'screen-added', file: filePath })); + maybeOpenBrowser(); } else { console.log(JSON.stringify({ type: 'screen-updated', file: filePath })); } |
