Skip to content

feat(extension): add --reuse-window flag for tab-mode automation#1202

Open
young001 wants to merge 1 commit intojackwener:mainfrom
young001:feat/reuse-window-flag
Open

feat(extension): add --reuse-window flag for tab-mode automation#1202
young001 wants to merge 1 commit intojackwener:mainfrom
young001:feat/reuse-window-flag

Conversation

@young001
Copy link
Copy Markdown

Summary

Adds --reuse-window (and OPENCLI_REUSE_WINDOW=1) flag that opens
automation 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 for opencli browser *
but not for adapter commands. This flag makes "in-place tab automation"
a first-class option for both.

Behaviour

Flag combo Result
(default) New automation window (unchanged)
--reuse-window New background tab in last-focused window
--reuse-window --focus Foreground tab in last-focused window
--reuse-window --live Tab kept open after command finishes
--reuse-window + zero Chrome windows Falls back to chrome.windows.create

The flag is orthogonal to --focus and --live. Default behaviour is
fully preserved when the flag is absent.

Implementation notes

  • Argv interception in src/main.ts mirrors the existing --focus /
    --live pattern (env var → daemon command field → extension global).
  • AutomationSession gains an ownsWindow boolean. Idle timeout,
    close-window, and chrome.tabs.onRemoved branch on this to close
    the tab (not the host window) in tab mode.
  • resolveTabId fails closed in tab mode when the preferred tab is
    gone, refusing to silently take over an unrelated user tab.
  • 3 new unit tests in extension/src/background.test.ts cover the
    happy path, the zero-window fallback, and idle-timeout cleanup.

Test plan

  • npx tsc --noEmit (root + extension) — clean
  • npm test — 247 files / 2002 passing / 0 new failures
  • npm run build (root + extension) — produced fresh dist/
  • Manual end-to-end: load unpacked extension, npm link, run
    opencli xiaohongshu search '纸尿裤' --reuse-window --focus --live
    and verify a new tab opens in the existing window instead of a new
    window. (Reviewer: please verify locally; I cannot reproduce in CI.)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant