feat(extension): make the local daemon port configurable in the popup - #175
feat(extension): make the local daemon port configurable in the popup#175shnpd wants to merge 4 commits into
Conversation
The extension was hardwired to 52800; persist a custom loopback port and reconnect when it changes so users can match a non-default daemon. Co-authored-by: Cursor <cursoragent@cursor.com>
The field was too short to read; grow its height without widening the card. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
🟡 Changes recommended
Port parsing and URL construction need to be made stricter/more robust (e.g., avoid parseInt-accepting malformed input and avoid manual URL concatenation that can break IPv6 formatting).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR implements runtime configuration of the extension’s local daemon WebSocket port via the popup UI, persisting the setting in chrome.storage.local and updating the background connection behavior accordingly (addressing issue #114).
Changes:
- Added a popup “Connection port” input with validation + persistence (
bsk_daemon_port), plus friendlier disconnected-state messaging. - Updated background to read the stored port at startup and react to storage changes by updating the WebSocket URL and reconnecting.
- Extended transport to allow runtime URL changes and added/updated docs + i18n + tests.
File summaries
| File | Description |
|---|---|
| packages/i18n/src/locales/zh-CN/extension.json | Adds zh-CN strings for daemon port UI and unreachable messaging. |
| packages/i18n/src/locales/en-US/extension.json | Adds en-US strings for daemon port UI and unreachable messaging. |
| docs/architecture.md | Documents that the default daemon WS port is configurable via the popup. |
| apps/extension/src/transport/ws-transport.ts | Allows updating transport URL via setUrl() without auto-reconnect. |
| apps/extension/src/transport/daemon-endpoint.ts | New helpers for default port, URL resolution, and input/storage normalization. |
| apps/extension/src/transport/tests/ws-transport.test.ts | Adds coverage for setUrl() behavior. |
| apps/extension/src/transport/tests/daemon-endpoint.test.ts | Adds coverage for daemon endpoint URL/port parsing helpers. |
| apps/extension/src/lib/popup-bridge.ts | Removes unused set_port message variant; port now stored via storage. |
| apps/extension/src/lib/instance-id.ts | Adds get/set helpers and storage key for bsk_daemon_port. |
| apps/extension/src/lib/tests/instance-id.test.ts | Adds tests for daemon port persistence/normalization. |
| apps/extension/src/entrypoints/popup/use-daemon-port.ts | New hook to load/commit daemon port preference via chrome.storage.local. |
| apps/extension/src/entrypoints/popup/use-connection-state.ts | Updates comment to reflect current popup bridge semantics. |
| apps/extension/src/entrypoints/popup/App.tsx | Renders port input, adjusts disconnected messaging, and hides transport errors when disconnected. |
| apps/extension/src/entrypoints/popup/App.test.tsx | Adds tests for disconnected UX and daemon port input behavior. |
| apps/extension/src/entrypoints/background.ts | Reads stored port on startup and reconnects on storage updates. |
| apps/extension/PRIVACY.zh-CN.md | Updates privacy text to note configurable loopback port. |
| apps/extension/PRIVACY.md | Updates privacy text to note configurable loopback port. |
Review details
- Files reviewed: 17/17 changed files
- Comments generated: 5
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
如果有字符就报错,不直接转为数字 Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
修复注释不准确 Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
这个 PR 解决了 #114 中扩展端口无法与 CLI 对齐的问题。建议保留目前的主要实现方向: 合并前建议调整一下连接生命周期和 popup 的状态处理。这样既能保留当前功能,也能减少对现有连接行为的影响。下面是基于当前提交的具体改进建议。 1. 恢复连接开关与错误展示的原有语义连接开关只表示“是否允许连接”App.tsx 目前使用: checked={snapshot.connectionEnabled && !isDisconnected}当 建议恢复为: checked={snapshot.connectionEnabled}开关表示用户是否启用连接,状态文字表示当前是否已连接。这两个状态可以独立展示,断连期间也应允许用户关闭连接。 保留具体错误,友好提示作为补充App.tsx 新增的 建议本 PR 恢复原有 收益: 这两处主要通过撤回额外行为修改完成,不需要引入新状态模型,同时保留用户关闭连接和排查协议问题的能力。 2. 让 Controller 统一协调端口切换background.ts 当前直接执行 这里等待 建议增加一个由
继续复用同一个 切换过程实现时建议保证以下行为:
这里有一个需要特别注意的实现细节:不要让串行协调流程一直等待 同理,如果复用现有恢复逻辑,应区分“清理完成的 Promise”和“包含连接等待的整个恢复 Promise”,避免间接引入相同的阻塞。 收益: 端口切换和现有断连恢复共享协调规则,旧会话清理与新连接之间有明确顺序;同时覆盖快速切换端口、切换时关闭连接和后台唤醒等时序,不需要在各处分别补重连逻辑。 3. 启动读取与运行时更新走同一套配置规则建议启动时读取端口与连接开关,完成必要监听器的安装后,再允许发起首次连接。初始化期间收到的 storage 变化先记录,后续应用最新值,避免迟到的初始读取覆盖更新。 这里的“初始化完成”指配置和监听器准备完成,不能以首次连接成功为条件。#114 的场景本身就是默认端口不可达,用户必须能够在未连接时修改端口。 端口校验也建议统一:
收益: 首次启动、扩展被唤醒和运行时修改不会形成三套不同的配置行为。 4. popup 明确区分草稿、加载和保存状态use-daemon-port.ts 初始草稿为 建议 hook 明确维护以下状态,其中
处理规则建议如下:
交互上更推荐 “保存按钮 + Enter 提交”。端口修改会触发连接中断,明确提交能让生效时机更清楚,也能让保存失败在 popup 仍打开时被看到。 如果希望保留 blur 保存,也可以复用同一个提交入口。但建议不把“关闭 popup 时保存”作为正确性保证: 另外,“配置保存成功”和“连接成功”应继续分别展示:前者由 storage 写入结果确定,后者由现有 Controller 快照确定。 收益: 避免默认值误写和异步读取覆盖输入;用户能够分辨保存失败与 daemon 不可达,hook 的提交条件也更容易测试。 5. 建议补充的验证现有测试覆盖了端口解析、保存和单独的
修改范围可以集中在 popup/hook、端口配置模块、Controller,以及 background/keepalive 的连接入口接线;CLI 和 daemon 协议不需要随之调整。已有会话清理逻辑可以复用,重点是保证它完成之前不会建立新连接。 |
摘要
fix #114
扩展原先写死连接
ws://127.0.0.1:52800。本 PR 让用户在 popup 里配置本机 daemon 端口,写入chrome.storage.local后 background 自动改 WebSocket URL 并重连。默认端口仍为 52800。未连接时不再展示
[WSTransport] ...这类技术错误,改为提示确认 daemon 已启动且端口一致;连接开关在未连接时显示为关闭。改动说明
chrome.storage.local(bsk_daemon_port),不再使用未接线的set_port消息。setUrl+ disconnect/connect。WSTransport支持运行时更换 URL(不自动重连)。ui展示
测试
abc/0/65536)显示错误,且不写入 storage