feat(core): support modelName "auto" when using the Stagehand API

Merged
MI
4mo ago
+342-5

Why

Passing model: "auto" failed at the constructor with:

ERROR: No known environment variable for provider 'auto'

followed by an UnsupportedModelError, because Stagehand always resolved a provider API key and built a local LLM client β€” even when all inference runs server-side through the Stagehand API.

What changed

model: "auto" is now accepted in the constructor and in per-primitive (act/extract/observe) model overrides when running through the Stagehand API (env: "BROWSERBASE", disableAPI: false, experimental: false). The API resolves "auto" server-side.

  • modelUtils.ts: new AUTO_MODEL_NAME constant + isAutoModel() helper.
  • v3.ts constructor: for "auto", skip provider API-key lookup and local client creation; throw a clear StagehandInvalidArgumentError when the API won't be used (LOCAL env, disableAPI: true, or experimental: true).
  • v3.ts resolveLlmClient: per-call "auto" overrides no-op onto the default client in API mode and throw the same clear error otherwise.
  • v3.ts act(): local act-cache replay is skipped for "auto" sessions β€” there is no local client for self-heal; the server-side cache still applies.
  • v3.ts init(): if the API reports available: false, "auto" has no local fallback β€” the just-created session is ended best-effort and a clear StagehandInitError (with sessionId) is thrown instead of failing later with an undefined-client TypeError.
  • api.ts prepareModelConfig: "auto" never inherits the default model's provider config or API key (the server picks the provider); explicit per-call options still pass through.

Agent/CUA mode is intentionally out of scope.

Known limits (intentional, kept surgical)

  • middleware on a per-call { modelName: "auto" } override is inert β€” consistent with middleware being documented local-only and already inert in API mode for concrete models.
  • The public llmClient!: LLMClient field is undefined for "auto" sessions; tightening that type ripples across the public surface and is left for a follow-up.

Tests

  • New tests/unit/auto-model.test.ts (10 tests): constructor accept/reject matrix (string + object form), per-call resolution guard, and wire-level assertions that "auto" payloads carry no inherited API key.
  • Full core unit suite: 869 passed; the 4 flowlogger-eventstore failures reproduce on clean main (pre-existing, unrelated).
  • Typecheck, eslint, and prettier clean.

πŸ€– Generated with Claude Code


Summary by cubic

Enables model: "auto" in @browserbasehq/stagehand when using the Stagehand API, delegating model selection to the server and avoiding local provider keys. Outside API mode, "auto" now fails fast with clear errors.

  • New Features
    • Accept "auto" in the constructor and per-call overrides only in API mode (env: "BROWSERBASE", disableAPI: false, experimental: false).
    • No local LLM client or provider API key; the API selects the provider and payloads don’t inherit API keys.
    • Clear errors: StagehandInvalidArgumentError when not in API mode or when a custom llmClient is supplied; StagehandInitError if the API is unavailable. On "auto" init failure, only end sessions created by this init and leave caller-supplied browserbaseSessionID sessions untouched.
    • Per-call "auto" reuses the default client in API mode; explicit options pass through. Local act-cache and agent-cache replay are disabled for "auto" sessions.