feat(extension): add --reuse-window flag for tab-mode automation#1202
Open
young001 wants to merge 1 commit intojackwener:mainfrom
Open
feat(extension): add --reuse-window flag for tab-mode automation#1202young001 wants to merge 1 commit intojackwener:mainfrom
young001 wants to merge 1 commit intojackwener:mainfrom
Conversation
Open automation as a new tab in the user's last-focused Chrome window
instead of spawning a separate window. Solves the "two windows" problem
when the user already has a Chrome window dedicated to OpenCLI.
Behaviour:
- `--reuse-window` (or OPENCLI_REUSE_WINDOW=1) is intercepted in
src/main.ts and propagated through the daemon → extension protocol.
- The extension calls chrome.windows.getLastFocused({windowTypes:['normal']})
and chrome.tabs.create on that window, instead of chrome.windows.create.
- Sessions track ownsWindow: when false (tab mode), idle timeout and
close-window cleanup remove the tab via chrome.tabs.remove and never
touch the host window.
- Falls back to the legacy new-window path when no normal Chrome window
is available, preserving zero-window behaviour.
- Orthogonal to --focus (defaults to background tab) and --live (keeps
the tab alive instead of the window).
Safety:
- resolveTabId fails closed in tab mode when the preferred tab is gone,
refusing to silently take over an unrelated user tab in the host window.
- chrome.tabs.onRemoved listener cleans up tab-mode sessions when the
user manually closes our owned tab.
Tests added in extension/src/background.test.ts:
- Opens a new tab in the last-focused window when --reuse-window is set.
- Falls back to chrome.windows.create when no normal window exists.
- Idle timeout closes the tab (not the host window) for reuse-window
sessions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
--reuse-window(andOPENCLI_REUSE_WINDOW=1) flag that opensautomation as a new tab in the user's last-focused Chrome window instead
of spawning a separate window. Solves the "two windows" friction for
users who already keep a dedicated Chrome window for OpenCLI.
Motivation
Today, every adapter command creates a new automation window via
chrome.windows.create. Users who keep one Chrome window open as the"OpenCLI host" end up with a second window popping up on every command,
which clutters the screen.
bound:*workspaces help foropencli browser *but not for adapter commands. This flag makes "in-place tab automation"
a first-class option for both.
Behaviour
--reuse-window--reuse-window --focus--reuse-window --live--reuse-window+ zero Chrome windowschrome.windows.createThe flag is orthogonal to
--focusand--live. Default behaviour isfully preserved when the flag is absent.
Implementation notes
src/main.tsmirrors the existing--focus/--livepattern (env var → daemon command field → extension global).AutomationSessiongains anownsWindowboolean. Idle timeout,close-window, andchrome.tabs.onRemovedbranch on this to closethe tab (not the host window) in tab mode.
resolveTabIdfails closed in tab mode when the preferred tab isgone, refusing to silently take over an unrelated user tab.
extension/src/background.test.tscover thehappy path, the zero-window fallback, and idle-timeout cleanup.
Test plan
npx tsc --noEmit(root + extension) — cleannpm test— 247 files / 2002 passing / 0 new failuresnpm run build(root + extension) — produced freshdist/npm link, runopencli xiaohongshu search '纸尿裤' --reuse-window --focus --liveand verify a new tab opens in the existing window instead of a new
window. (Reviewer: please verify locally; I cannot reproduce in CI.)