feat: add Valkey cache configuration to client and session start#346
Open
edlng wants to merge 2 commits into
Open
feat: add Valkey cache configuration to client and session start#346edlng wants to merge 2 commits into
edlng wants to merge 2 commits into
Conversation
Signed-off-by: Edward Liang <edward.liang@improving.com>
There was a problem hiding this comment.
1 issue found across 8 files
Confidence score: 3/5
src/stagehand/types/__init__.pyappears to add a re-export directly in stainless-generated code, which can be overwritten on the next codegen run and silently dropValkeyCacheOptionsfrom the public surface, causing import breakages after regeneration. Move this change intosrc/stagehand/_custom(with the required custom-code annotation/path) before merging so the export is preserved across codegen updates.
Architecture diagram
sequenceDiagram
participant User as User Code
participant Client as Stagehand/AsyncStagehand Client
participant SEA as SeaServerManager
participant Sessions as Sessions Resource
participant Backend as Remote API Backend
participant Valkey as Valkey Cache
Note over User,Valkey: NEW: Valkey cache configuration flow
User->>Client: Stagehand(valkey_host="...", valkey_port=...)
Client->>Client: Store Valkey params as instance vars
alt remote mode (server != "local")
Client->>Client: Warn: client-level Valkey params ignored in remote mode
Note over Client: User should pass valkey_cache to sessions.start()
end
alt local mode (server == "local")
Client->>SEA: configure_client_base_url(server="local")
SEA->>SEA: Build SeaServerConfig with Valkey fields
SEA->>SEA: _build_process_env()
SEA->>SEA: Set env vars VALKEY_HOST, VALKEY_PORT, VALKEY_TLS, VALKEY_PASSWORD, VALKEY_USERNAME, CACHE_TTL, VALKEY_KEY_PREFIX
alt password set without TLS on non-local host
SEA->>User: Warning: "Credentials sent in cleartext"
end
SEA->>Valkey: Connect to Valkey cache via env vars
Note over SEA: Requires @valkey/valkey-glide addon
end
User->>Client: with_options(valkey_host="new.host", valkey_cache_ttl=900)
Client->>Client: Copy instance with overridden Valkey params
User->>Sessions: sessions.start(valkey_cache={"host": "...", "port": 6380, ...})
Sessions->>Backend: POST /sessions/start with valkeyCache (camelCase)
Note over Sessions,Backend: Remote mode: Valkey config sent in request body
Backend-->>Sessions: Session started with Valkey cache
Sessions-->>User: Session object
alt local mode session start
User->>Client: sessions.start() (no valkey_cache)
Client->>SEA: Use existing Valkey env vars
SEA-->>Client: Session with cache enabled
end
Reply with feedback, questions, or to request a fix.
Fix all with cubic | Re-trigger cubic
Signed-off-by: Edward Liang <edward.liang@improving.com>
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 first-class Valkey cache configuration across the Python SDK. Users can now pass connection details (host, port, TLS, auth, TTL, key prefix) either at the client level (propagated as env vars to the local SEA server) or per-session via a
valkey_cacheparam onsessions.start().Note: This PR depends on the Valkey implementation (browserbase/stagehand#2264) being merged first in
stagehand.Issue
N/A
Changes
src/stagehand/_client.py- Added 7 Valkey-related instance variables and constructor/with_optionsparams to bothStagehandandAsyncStagehandsrc/stagehand/_custom/sea_server.py- ExtendedSeaServerConfig,_HasLocalModeStateprotocol,LocalModeKwargs,_build_process_env(env var propagation),configure_client_base_url, andcopy_local_mode_kwargswith Valkey fields. Also warns when credentials are sent without TLS on non-local hosts, and when client-level Valkey params are used in remote mode.src/stagehand/_custom/session.py- Threadedvalkey_cacheparam through_sync_startand_async_startsrc/stagehand/resources/sessions.py- Addedvalkey_cacheparam toSessionsResource.start()andAsyncSessionsResource.start()with docstrings and body serializationsrc/stagehand/types/session_start_params.py- NewValkeyCacheOptionsTypedDict (host required, port/tls/password/username/cache_ttl/key_prefix optional) with camelCase aliases, plusvalkey_cachefield onSessionStartParamssrc/stagehand/types/__init__.py- Re-exportValkeyCacheOptionstests/api_resources/test_sessions.py- Addedvalkey_cachedict to existing sync/async session start teststests/test_valkey_cache.py- New test file coveringSeaServerConfigdefaults, env var propagation, client constructor params,with_optionspreserve/override, TypedDict shape, signature inspection, falsy-but-valid values (port=0, ttl=0), and empty string edge casesTests
tests/test_valkey_cache.pycovers the full surface: dataclass fields, env var generation, client param acceptance,with_optionsbehavior, type exports, method signatures, edge cases for falsy values, and the remote-mode warning. Existingtest_sessions.pytests updated to exercise the new param in the session start call.Blockers / Future work
stagehandrepo firstAdditional context
The local SEA binary and server receives Valkey config as environment variables (
VALKEY_HOST,VALKEY_PORT,VALKEY_TLS,VALKEY_PASSWORD,VALKEY_USERNAME,CACHE_TTL,VALKEY_KEY_PREFIX). For remote mode, thevalkeyCacheobject is sent in the session start request body. No new runtime dependencies added.Summary by cubic
Adds first-class Valkey cache support to the Python SDK. Configure Valkey at the client level (local SEA via env vars) or per session with
valkey_cachefor remote requests, with safety warnings and updated docs/tests.New Features
valkey_host,valkey_port,valkey_tls,valkey_password,valkey_username,valkey_cache_ttl,valkey_key_prefix; propagated to the local SEA asVALKEY_*andCACHE_TTL.sessions.start()acceptsvalkey_cache(host required; port/tls/password/username/cache_ttl/key_prefix optional), serialized with camelCase keys and documented in the method docstring.ValkeyCacheOptionsinstagehand.types.Migration
Stagehand/AsyncStagehand(or via.with_options()); the SEA binary must be built with@valkey/valkey-glidefor caching to activate.valkey_cachetosessions.start().port=0andcache_ttl=0are allowed; empty strings are sent as provided; prefer TLS when sending credentials.stagehandserver to be available.Written for commit e4bbc4d. Summary will update on new commits.