Centralize custom code

Merged
PI
7mo ago
+1324-967

Summary by cubic

Centralizes all handwritten SDK logic under stagehand/_custom and patches the generated client at import time to simplify local server mode and bound session handling. Also ensures Playwright CDP sessions are detached after frame ID extraction to prevent leaks.

  • Refactors

    • Moved SEA binary/server and session patches to stagehand/_custom; _client now uses configure_client_base_url, prepare_*, close_*, and copy_local_mode_kwargs and lazily starts/stops the SEA server.
    • Runtime patches install on import: sessions.start() returns a bound Session/AsyncSession; Session.extract supports Pydantic schema validation; generated resources/sessions.start is type-hinted to return bound sessions.
    • Exported Session and AsyncSession from stagehand; _client.sessions now exposes the generated resources/sessions (helper wrappers removed).
    • Local mode behavior: when server="local" and no browser is passed, default to a local browser if Browserbase creds are set; if creds are missing, require an explicit local browser.
    • Consolidated scripts: scripts/download_binary.py (renamed) and scripts/test_local_mode.py (new); docs updated.
  • Bug Fixes

    • Always detach Playwright CDP sessions after reading Page.getFrameTree when injecting frame_id from a page param (sync/async) to avoid leaked CDP sessions and flaky tests.
  • Migration

    • Update imports to stagehand._custom (e.g., from stagehand._custom import sea_server, sea_binary).
    • Use scripts/download_binary.py and scripts/test_local_mode.py; type-checker excludes updated.
    • Code using sessions.start() can use the returned Session/AsyncSession directly; raw/streaming responses are unchanged.