Skip to content

Commit b43ecf9

Browse files
authored
More custom code cleanup (#334)
* remove unneeded file * more clenaup * fix custom comments * add missing keep file
1 parent 083244c commit b43ecf9

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

examples/full_example.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ async def main() -> None:
8484

8585
# Use the first result
8686
result = results[0]
87-
print(f"Acting on: {result.description}")
87+
if not isinstance(result, dict):
88+
raise RuntimeError(f"Expected observe stream result item to be a dict, got {type(result).__name__}")
89+
print(f"Acting on: {result['description']}")
8890

8991
# Pass the action to Act
9092
act_stream = await session.act(

src/stagehand/_client.py

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import os
66
from typing import TYPE_CHECKING, Any, Mapping
7+
8+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
9+
# Keep the generated client thin: all runtime patch logic lives in `_custom`.
710
from typing_extensions import Self, Literal, override
811

912
import httpx
@@ -30,9 +33,6 @@
3033
SyncAPIClient,
3134
AsyncAPIClient,
3235
)
33-
34-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
35-
# Keep the generated client thin: all runtime patch logic lives in `_custom`.
3636
from ._custom.session import install_stainless_session_patches
3737
from ._custom.sea_server import (
3838
copy_local_mode_kwargs,
@@ -50,6 +50,7 @@
5050

5151
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
5252
from ._custom.sea_server import SeaServerManager
53+
from .resources.sessions import SessionsResource, AsyncSessionsResource
5354
### </END CUSTOM CODE>
5455

5556
__all__ = [
@@ -71,11 +72,11 @@
7172

7273

7374
class Stagehand(SyncAPIClient):
75+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
7476
# client options
7577
browserbase_api_key: str | None
7678
browserbase_project_id: str | None
7779
model_api_key: str | None
78-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
7980
# These are assigned indirectly by `configure_client_base_url(...)` so the
8081
# generated class still exposes typed local-mode state for `copy()` and tests.
8182
_server_mode: Literal["remote", "local"]
@@ -89,6 +90,7 @@ class Stagehand(SyncAPIClient):
8990
_sea_server: SeaServerManager | None
9091
### </END CUSTOM CODE>
9192

93+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
9294
def __init__(
9395
self,
9496
*,
@@ -142,7 +144,6 @@ def __init__(
142144

143145
self.model_api_key = model_api_key
144146

145-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
146147
# Centralize local-mode state hydration and base-url selection in `_custom`
147148
# so no constructor branching lives in the generated client.
148149
base_url = configure_client_base_url(
@@ -158,7 +159,7 @@ def __init__(
158159
base_url=base_url,
159160
model_api_key=model_api_key,
160161
)
161-
### </END CUSTOM CODE>
162+
### </END CUSTOM CODE>
162163

163164
super().__init__(
164165
version=__version__,
@@ -173,29 +174,27 @@ def __init__(
173174

174175
self._default_stream_cls = Stream
175176

177+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
176178
@override
177179
def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
178-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
179180
# Start the local SEA server lazily on first request instead of at client
180181
# construction time, then swap the base URL to the started process.
181182
local_base_url = prepare_sync_client_base_url(self)
182183
if local_base_url is not None:
183184
self.base_url = local_base_url
184-
### </END CUSTOM CODE>
185185
return super()._prepare_options(options)
186186

187187
@override
188188
def close(self) -> None:
189189
try:
190190
super().close()
191191
finally:
192-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
193192
# Tear down the managed SEA process after HTTP resources close.
194193
close_sync_client_sea_server(self)
195-
### </END CUSTOM CODE>
194+
### </END CUSTOM CODE>
196195

197196
@cached_property
198-
def sessions(self) -> sessions.SessionsResource:
197+
def sessions(self) -> SessionsResource:
199198
from .resources.sessions import SessionsResource
200199

201200
return SessionsResource(self)
@@ -218,6 +217,7 @@ def qs(self) -> Querystring:
218217
def auth_headers(self) -> dict[str, str]:
219218
return {**self._bb_api_key_auth, **self._bb_project_id_auth, **self._llm_model_api_key_auth}
220219

220+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
221221
@property
222222
def _bb_api_key_auth(self) -> dict[str, str]:
223223
browserbase_api_key = self.browserbase_api_key
@@ -243,7 +243,9 @@ def default_headers(self) -> dict[str, str | Omit]:
243243
"X-Stainless-Async": "false",
244244
**self._custom_headers,
245245
}
246+
### </END CUSTOM CODE>
246247

248+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
247249
def copy(
248250
self,
249251
*,
@@ -300,7 +302,6 @@ def copy(
300302
max_retries=max_retries if is_given(max_retries) else self.max_retries,
301303
default_headers=headers,
302304
default_query=params,
303-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
304305
# Preserve local-mode configuration when cloning the client without
305306
# duplicating that branching logic in generated code.
306307
**copy_local_mode_kwargs(
@@ -314,12 +315,12 @@ def copy(
314315
local_ready_timeout_s=local_ready_timeout_s,
315316
local_shutdown_on_close=local_shutdown_on_close,
316317
),
317-
### </END CUSTOM CODE>
318318
**_extra_kwargs,
319319
)
320+
### </END CUSTOM CODE>
320321

321322
# Alias for `copy` for nicer inline usage, e.g.
322-
# client.with_options(timeout=10).foo.start(...)
323+
# client.with_options(timeout=10).foo.create(...)
323324
with_options = copy
324325

325326
@override
@@ -357,11 +358,11 @@ def _make_status_error(
357358

358359

359360
class AsyncStagehand(AsyncAPIClient):
361+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
360362
# client options
361363
browserbase_api_key: str | None
362364
browserbase_project_id: str | None
363365
model_api_key: str | None
364-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
365366
# These are assigned indirectly by `configure_client_base_url(...)` so the
366367
# generated class still exposes typed local-mode state for `copy()` and tests.
367368
_server_mode: Literal["remote", "local"]
@@ -375,6 +376,7 @@ class AsyncStagehand(AsyncAPIClient):
375376
_sea_server: SeaServerManager | None
376377
### </END CUSTOM CODE>
377378

379+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
378380
def __init__(
379381
self,
380382
*,
@@ -428,7 +430,6 @@ def __init__(
428430

429431
self.model_api_key = model_api_key
430432

431-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
432433
# Centralize local-mode state hydration and base-url selection in `_custom`
433434
# so no constructor branching lives in the generated client.
434435
base_url = configure_client_base_url(
@@ -444,7 +445,7 @@ def __init__(
444445
base_url=base_url,
445446
model_api_key=model_api_key,
446447
)
447-
### </END CUSTOM CODE>
448+
### </END CUSTOM CODE>
448449

449450
super().__init__(
450451
version=__version__,
@@ -459,29 +460,27 @@ def __init__(
459460

460461
self._default_stream_cls = AsyncStream
461462

463+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
462464
@override
463465
async def _prepare_options(self, options: FinalRequestOptions) -> FinalRequestOptions:
464-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
465466
# Start the local SEA server lazily on first request instead of at client
466467
# construction time, then swap the base URL to the started process.
467468
local_base_url = await prepare_async_client_base_url(self)
468469
if local_base_url is not None:
469470
self.base_url = local_base_url
470-
### </END CUSTOM CODE>
471471
return await super()._prepare_options(options)
472472

473473
@override
474474
async def close(self) -> None:
475475
try:
476476
await super().close()
477477
finally:
478-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
479478
# Tear down the managed SEA process after HTTP resources close.
480479
await close_async_client_sea_server(self)
481-
### </END CUSTOM CODE>
480+
### </END CUSTOM CODE>
482481

483482
@cached_property
484-
def sessions(self) -> sessions.AsyncSessionsResource:
483+
def sessions(self) -> AsyncSessionsResource:
485484
from .resources.sessions import AsyncSessionsResource
486485

487486
return AsyncSessionsResource(self)
@@ -504,6 +503,7 @@ def qs(self) -> Querystring:
504503
def auth_headers(self) -> dict[str, str]:
505504
return {**self._bb_api_key_auth, **self._bb_project_id_auth, **self._llm_model_api_key_auth}
506505

506+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
507507
@property
508508
def _bb_api_key_auth(self) -> dict[str, str]:
509509
browserbase_api_key = self.browserbase_api_key
@@ -529,7 +529,9 @@ def default_headers(self) -> dict[str, str | Omit]:
529529
"X-Stainless-Async": f"async:{get_async_library()}",
530530
**self._custom_headers,
531531
}
532+
### </END CUSTOM CODE>
532533

534+
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
533535
def copy(
534536
self,
535537
*,
@@ -586,7 +588,6 @@ def copy(
586588
max_retries=max_retries if is_given(max_retries) else self.max_retries,
587589
default_headers=headers,
588590
default_query=params,
589-
### <CUSTOM CODE HANDWRITTEN BY STAGEHAND TEAM (not codegen)>
590591
# Preserve local-mode configuration when cloning the client without
591592
# duplicating that branching logic in generated code.
592593
**copy_local_mode_kwargs(
@@ -600,12 +601,12 @@ def copy(
600601
local_ready_timeout_s=local_ready_timeout_s,
601602
local_shutdown_on_close=local_shutdown_on_close,
602603
),
603-
### </END CUSTOM CODE>
604604
**_extra_kwargs,
605605
)
606+
### </END CUSTOM CODE>
606607

607608
# Alias for `copy` for nicer inline usage, e.g.
608-
# client.with_options(timeout=10).foo.start(...)
609+
# client.with_options(timeout=10).foo.create(...)
609610
with_options = copy
610611

611612
@override

src/stagehand/_sea/.keep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)