feat: add performance controls for large browser ops
- Add throttled large-operation handling for tab, group, and session commands. - Introduce performance profiles, audible-tab aware gentle mode, and job progress tracking. - Support background session restores with status/cancel commands and lazy placeholders. - Expose new perf and extension CLI groups plus matching Python SDK methods. - Preserve pinned tabs during session snapshots and debounce auto-save updates. - Bump browser-cli and extension versions to 0.10.0 and add pytest-cov to dev deps. - Add coverage for performance controls, background jobs, lazy restores, and tab metadata.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import time
|
||||
import click
|
||||
from rich.console import Console
|
||||
from browser_cli.commands import _handle
|
||||
|
||||
console = Console()
|
||||
|
||||
@click.group("extension")
|
||||
def extension_group():
|
||||
"""Manage the browser-cli browser extension."""
|
||||
|
||||
@extension_group.command("reload")
|
||||
def extension_reload():
|
||||
"""Reload the browser-cli extension service worker.
|
||||
|
||||
Useful after updating background.js without restarting the browser.
|
||||
The command returns immediately; the extension restarts ~200 ms later.
|
||||
Re-connects automatically via the keepalive alarm within ~25 seconds.
|
||||
"""
|
||||
_handle("extension.reload")
|
||||
console.print("[green]Extension reloading…[/green] reconnects automatically")
|
||||
@@ -103,9 +103,10 @@ def group_query(search):
|
||||
|
||||
@group_group.command("close")
|
||||
@click.argument("group_id", type=int)
|
||||
def group_close(group_id):
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large group operations.")
|
||||
def group_close(group_id, gentle_mode):
|
||||
"""Close (ungroup and optionally close) a tab group."""
|
||||
_handle("group.close", {"groupId": group_id})
|
||||
_handle("group.close", {"groupId": group_id, "gentleMode": gentle_mode})
|
||||
console.print(f"[green]Group {group_id} closed[/green]")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import click
|
||||
from rich.console import Console
|
||||
from rich.table import Table
|
||||
from browser_cli.commands import _handle
|
||||
|
||||
console = Console()
|
||||
|
||||
@click.group("perf")
|
||||
def perf_group():
|
||||
"""Inspect and tune browser-cli performance behavior."""
|
||||
|
||||
@perf_group.command("status")
|
||||
def perf_status():
|
||||
"""Show performance profile, throttle and running jobs."""
|
||||
result = _handle("perf.status") or {}
|
||||
console.print(f"Profile: [bold]{result.get('performanceProfile', 'auto')}[/bold]")
|
||||
console.print(f"Audible tabs: {'yes' if result.get('audible') else 'no'}")
|
||||
throttle = result.get("throttle") or {}
|
||||
console.print(f"Throttle: batch={throttle.get('batchSize')} pause={throttle.get('pauseMs')}ms mode={throttle.get('mode')}")
|
||||
|
||||
jobs = result.get("jobs") or []
|
||||
if not jobs:
|
||||
console.print("[yellow]No running jobs[/yellow]")
|
||||
return
|
||||
|
||||
table = Table(show_header=True, header_style="bold cyan")
|
||||
table.add_column("Job")
|
||||
table.add_column("Command")
|
||||
table.add_column("Status")
|
||||
table.add_column("Progress", justify="right")
|
||||
table.add_column("Phase")
|
||||
for job in jobs:
|
||||
total = job.get("total")
|
||||
current = job.get("current") or 0
|
||||
percent = job.get("percent") or 0
|
||||
progress = f"{current}/{total} ({percent}%)" if total else f"{percent}%"
|
||||
table.add_row(job.get("id", ""), job.get("command", ""), job.get("status", ""), progress, job.get("phase", ""))
|
||||
console.print(table)
|
||||
|
||||
@perf_group.command("profile")
|
||||
@click.argument("profile", type=click.Choice(["auto", "normal", "gentle", "ultra"]))
|
||||
def perf_profile(profile):
|
||||
"""Set global performance profile."""
|
||||
result = _handle("perf.set_profile", {"profile": profile}) or {}
|
||||
console.print(f"[green]Performance profile set to {result.get('performanceProfile', profile)}[/green]")
|
||||
@@ -4,12 +4,10 @@ from rich.console import Console
|
||||
|
||||
console = Console()
|
||||
|
||||
|
||||
@click.group("session")
|
||||
def session_group():
|
||||
"""Save and restore browser sessions."""
|
||||
|
||||
|
||||
@session_group.command("save")
|
||||
@click.argument("name")
|
||||
def session_save(name):
|
||||
@@ -18,16 +16,29 @@ def session_save(name):
|
||||
count = result.get("tabs", 0) if isinstance(result, dict) else 0
|
||||
console.print(f"[green]Session '{name}' saved[/green] ({count} tabs)")
|
||||
|
||||
|
||||
@session_group.command("load")
|
||||
@click.argument("name")
|
||||
def session_load(name):
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large restores.")
|
||||
@click.option("--discard-background-tabs", is_flag=True, help="Discard restored background tabs after opening to reduce load.")
|
||||
@click.option("--lazy", is_flag=True, help="Create lightweight placeholder tabs after --eager-tabs; placeholders load when selected.")
|
||||
@click.option("--eager-tabs", type=int, default=10, show_default=True, help="Number of real tabs to open before lazy placeholders.")
|
||||
@click.option("--background", "background_job", is_flag=True, help="Start restore as a background job and return immediately.")
|
||||
def session_load(name, gentle_mode, discard_background_tabs, lazy, eager_tabs, background_job):
|
||||
"""Restore session NAME (opens all saved tabs)."""
|
||||
result = _handle("session.load", {"name": name})
|
||||
result = _handle("session.load", {
|
||||
"name": name,
|
||||
"gentleMode": gentle_mode,
|
||||
"discardBackgroundTabs": discard_background_tabs,
|
||||
"lazy": lazy,
|
||||
"eagerTabs": eager_tabs,
|
||||
"__background": background_job,
|
||||
})
|
||||
if background_job and isinstance(result, dict) and result.get("jobId"):
|
||||
console.print(f"[green]Session restore started[/green] job={result['jobId']}")
|
||||
return
|
||||
count = result.get("tabs", 0) if isinstance(result, dict) else 0
|
||||
console.print(f"[green]Session '{name}' loaded[/green] ({count} tabs opened)")
|
||||
|
||||
|
||||
@session_group.command("diff")
|
||||
@click.argument("name_a")
|
||||
@click.argument("name_b")
|
||||
@@ -54,7 +65,6 @@ def session_diff(name_a, name_b):
|
||||
if not added and not removed:
|
||||
console.print("[green]Sessions are identical[/green]")
|
||||
|
||||
|
||||
@session_group.command("list")
|
||||
def session_list():
|
||||
"""List all saved sessions."""
|
||||
@@ -90,7 +100,6 @@ def session_list():
|
||||
table.add_row(*row)
|
||||
console.print(table)
|
||||
|
||||
|
||||
@session_group.command("remove")
|
||||
@click.argument("name")
|
||||
def session_remove(name):
|
||||
@@ -98,6 +107,24 @@ def session_remove(name):
|
||||
_handle("session.remove", {"name": name})
|
||||
console.print(f"[green]Session '{name}' removed[/green]")
|
||||
|
||||
@session_group.command("job-status")
|
||||
@click.argument("job_id")
|
||||
def session_job_status(job_id):
|
||||
"""Show status for a background session job."""
|
||||
result = _handle("jobs.status", {"jobId": job_id}) or {}
|
||||
status = result.get("status", "unknown")
|
||||
console.print(f"[bold]{job_id}[/bold]: {status}")
|
||||
if result.get("error"):
|
||||
console.print(f"[red]{result['error']}[/red]")
|
||||
elif result.get("result"):
|
||||
console.print(result["result"])
|
||||
|
||||
@session_group.command("job-cancel")
|
||||
@click.argument("job_id")
|
||||
def session_job_cancel(job_id):
|
||||
"""Cancel a running background job."""
|
||||
_handle("jobs.cancel", {"jobId": job_id})
|
||||
console.print(f"[green]Cancel requested for {job_id}[/green]")
|
||||
|
||||
@session_group.command("auto-save")
|
||||
@click.argument("state", type=click.Choice(["on", "off"]))
|
||||
|
||||
@@ -66,9 +66,10 @@ def tabs_list():
|
||||
@click.argument("tab_id", type=int, required=False)
|
||||
@click.option("--inactive", is_flag=True, help="Close all inactive tabs")
|
||||
@click.option("--duplicates", is_flag=True, help="Close duplicate tabs (keep first)")
|
||||
def tabs_close(tab_id, inactive, duplicates):
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large close operations.")
|
||||
def tabs_close(tab_id, inactive, duplicates, gentle_mode):
|
||||
"""Close a tab, all inactive tabs, or all duplicate tabs."""
|
||||
result = _handle("tabs.close", {"tabId": tab_id, "inactive": inactive, "duplicates": duplicates})
|
||||
result = _handle("tabs.close", {"tabId": tab_id, "inactive": inactive, "duplicates": duplicates, "gentleMode": gentle_mode})
|
||||
count = result.get("closed", 0) if isinstance(result, dict) else 1
|
||||
console.print(f"[green]Closed {count} tab(s)[/green]")
|
||||
|
||||
@@ -171,25 +172,28 @@ def tabs_html(tab_id):
|
||||
|
||||
|
||||
@tabs_group.command("dedupe")
|
||||
def tabs_dedupe():
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large dedupe operations.")
|
||||
def tabs_dedupe(gentle_mode):
|
||||
"""Close duplicate tabs (keep the first occurrence of each URL)."""
|
||||
result = _handle("tabs.dedupe")
|
||||
result = _handle("tabs.dedupe", {"gentleMode": gentle_mode})
|
||||
count = result.get("closed", 0) if isinstance(result, dict) else 0
|
||||
console.print(f"[green]Closed {count} duplicate tab(s)[/green]")
|
||||
|
||||
|
||||
@tabs_group.command("sort")
|
||||
@click.option("--by", type=click.Choice(["domain", "title", "time"]), default="domain", show_default=True)
|
||||
def tabs_sort(by):
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large sort operations.")
|
||||
def tabs_sort(by, gentle_mode):
|
||||
"""Sort tabs within each window."""
|
||||
_handle("tabs.sort", {"by": by})
|
||||
_handle("tabs.sort", {"by": by, "gentleMode": gentle_mode})
|
||||
console.print(f"[green]Tabs sorted by {by}[/green]")
|
||||
|
||||
|
||||
@tabs_group.command("merge-windows")
|
||||
def tabs_merge_windows():
|
||||
@click.option("--gentle-mode", type=click.Choice(["auto", "normal", "gentle", "ultra"]), default="auto", show_default=True, help="Throttle mode for large merge operations.")
|
||||
def tabs_merge_windows(gentle_mode):
|
||||
"""Move all tabs into the focused window."""
|
||||
result = _handle("tabs.merge_windows")
|
||||
result = _handle("tabs.merge_windows", {"gentleMode": gentle_mode})
|
||||
count = result.get("moved", 0) if isinstance(result, dict) else 0
|
||||
console.print(f"[green]Merged — moved {count} tab(s) into current window[/green]")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user