Skip to content

Commit 4204ad5

Browse files
feat: ad hoc playwright code exec AP|
1 parent 1130759 commit 4204ad5

File tree

9 files changed

+450
-4
lines changed

9 files changed

+450
-4
lines changed

.stats.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 64
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-e21f0324774a1762bc2bba0da3a8a6b0d0e74720d7a1c83dec813f9e027fcf58.yml
3-
openapi_spec_hash: f1b636abfd6cb8e7c2ba7ffb8e53b9ba
4-
config_hash: 09a2df23048cb16689c9a390d9e5bc47
1+
configured_endpoints: 65
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/kernel%2Fkernel-ecf484375ede1edd7754779ad8beeebd4ba9118152fe6cd65772dc7245a19dee.yml
3+
openapi_spec_hash: b1f3f05005f75cbf5b82299459e2aa9b
4+
config_hash: 3ded7a0ed77b1bfd68eabc6763521fe8

api.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,18 @@ Methods:
178178
- <code title="post /browsers/{id}/computer/scroll">client.browsers.computer.<a href="./src/kernel/resources/browsers/computer.py">scroll</a>(id, \*\*<a href="src/kernel/types/browsers/computer_scroll_params.py">params</a>) -> None</code>
179179
- <code title="post /browsers/{id}/computer/type">client.browsers.computer.<a href="./src/kernel/resources/browsers/computer.py">type_text</a>(id, \*\*<a href="src/kernel/types/browsers/computer_type_text_params.py">params</a>) -> None</code>
180180

181+
## Playwright
182+
183+
Types:
184+
185+
```python
186+
from kernel.types.browsers import PlaywrightExecuteResponse
187+
```
188+
189+
Methods:
190+
191+
- <code title="post /browsers/{id}/playwright/execute">client.browsers.playwright.<a href="./src/kernel/resources/browsers/playwright.py">execute</a>(id, \*\*<a href="src/kernel/types/browsers/playwright_execute_params.py">params</a>) -> <a href="./src/kernel/types/browsers/playwright_execute_response.py">PlaywrightExecuteResponse</a></code>
192+
181193
# Profiles
182194

183195
Types:

src/kernel/resources/browsers/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@
4848
ComputerResourceWithStreamingResponse,
4949
AsyncComputerResourceWithStreamingResponse,
5050
)
51+
from .playwright import (
52+
PlaywrightResource,
53+
AsyncPlaywrightResource,
54+
PlaywrightResourceWithRawResponse,
55+
AsyncPlaywrightResourceWithRawResponse,
56+
PlaywrightResourceWithStreamingResponse,
57+
AsyncPlaywrightResourceWithStreamingResponse,
58+
)
5159

5260
__all__ = [
5361
"ReplaysResource",
@@ -80,6 +88,12 @@
8088
"AsyncComputerResourceWithRawResponse",
8189
"ComputerResourceWithStreamingResponse",
8290
"AsyncComputerResourceWithStreamingResponse",
91+
"PlaywrightResource",
92+
"AsyncPlaywrightResource",
93+
"PlaywrightResourceWithRawResponse",
94+
"AsyncPlaywrightResourceWithRawResponse",
95+
"PlaywrightResourceWithStreamingResponse",
96+
"AsyncPlaywrightResourceWithStreamingResponse",
8397
"BrowsersResource",
8498
"AsyncBrowsersResource",
8599
"BrowsersResourceWithRawResponse",

src/kernel/resources/browsers/browsers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
AsyncComputerResourceWithStreamingResponse,
5151
)
5252
from ..._compat import cached_property
53+
from .playwright import (
54+
PlaywrightResource,
55+
AsyncPlaywrightResource,
56+
PlaywrightResourceWithRawResponse,
57+
AsyncPlaywrightResourceWithRawResponse,
58+
PlaywrightResourceWithStreamingResponse,
59+
AsyncPlaywrightResourceWithStreamingResponse,
60+
)
5361
from ..._resource import SyncAPIResource, AsyncAPIResource
5462
from ..._response import (
5563
to_raw_response_wrapper,
@@ -87,6 +95,10 @@ def logs(self) -> LogsResource:
8795
def computer(self) -> ComputerResource:
8896
return ComputerResource(self._client)
8997

98+
@cached_property
99+
def playwright(self) -> PlaywrightResource:
100+
return PlaywrightResource(self._client)
101+
90102
@cached_property
91103
def with_raw_response(self) -> BrowsersResourceWithRawResponse:
92104
"""
@@ -391,6 +403,10 @@ def logs(self) -> AsyncLogsResource:
391403
def computer(self) -> AsyncComputerResource:
392404
return AsyncComputerResource(self._client)
393405

406+
@cached_property
407+
def playwright(self) -> AsyncPlaywrightResource:
408+
return AsyncPlaywrightResource(self._client)
409+
394410
@cached_property
395411
def with_raw_response(self) -> AsyncBrowsersResourceWithRawResponse:
396412
"""
@@ -719,6 +735,10 @@ def logs(self) -> LogsResourceWithRawResponse:
719735
def computer(self) -> ComputerResourceWithRawResponse:
720736
return ComputerResourceWithRawResponse(self._browsers.computer)
721737

738+
@cached_property
739+
def playwright(self) -> PlaywrightResourceWithRawResponse:
740+
return PlaywrightResourceWithRawResponse(self._browsers.playwright)
741+
722742

723743
class AsyncBrowsersResourceWithRawResponse:
724744
def __init__(self, browsers: AsyncBrowsersResource) -> None:
@@ -763,6 +783,10 @@ def logs(self) -> AsyncLogsResourceWithRawResponse:
763783
def computer(self) -> AsyncComputerResourceWithRawResponse:
764784
return AsyncComputerResourceWithRawResponse(self._browsers.computer)
765785

786+
@cached_property
787+
def playwright(self) -> AsyncPlaywrightResourceWithRawResponse:
788+
return AsyncPlaywrightResourceWithRawResponse(self._browsers.playwright)
789+
766790

767791
class BrowsersResourceWithStreamingResponse:
768792
def __init__(self, browsers: BrowsersResource) -> None:
@@ -807,6 +831,10 @@ def logs(self) -> LogsResourceWithStreamingResponse:
807831
def computer(self) -> ComputerResourceWithStreamingResponse:
808832
return ComputerResourceWithStreamingResponse(self._browsers.computer)
809833

834+
@cached_property
835+
def playwright(self) -> PlaywrightResourceWithStreamingResponse:
836+
return PlaywrightResourceWithStreamingResponse(self._browsers.playwright)
837+
810838

811839
class AsyncBrowsersResourceWithStreamingResponse:
812840
def __init__(self, browsers: AsyncBrowsersResource) -> None:
@@ -850,3 +878,7 @@ def logs(self) -> AsyncLogsResourceWithStreamingResponse:
850878
@cached_property
851879
def computer(self) -> AsyncComputerResourceWithStreamingResponse:
852880
return AsyncComputerResourceWithStreamingResponse(self._browsers.computer)
881+
882+
@cached_property
883+
def playwright(self) -> AsyncPlaywrightResourceWithStreamingResponse:
884+
return AsyncPlaywrightResourceWithStreamingResponse(self._browsers.playwright)
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
import httpx
6+
7+
from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
8+
from ..._utils import maybe_transform, async_maybe_transform
9+
from ..._compat import cached_property
10+
from ..._resource import SyncAPIResource, AsyncAPIResource
11+
from ..._response import (
12+
to_raw_response_wrapper,
13+
to_streamed_response_wrapper,
14+
async_to_raw_response_wrapper,
15+
async_to_streamed_response_wrapper,
16+
)
17+
from ..._base_client import make_request_options
18+
from ...types.browsers import playwright_execute_params
19+
from ...types.browsers.playwright_execute_response import PlaywrightExecuteResponse
20+
21+
__all__ = ["PlaywrightResource", "AsyncPlaywrightResource"]
22+
23+
24+
class PlaywrightResource(SyncAPIResource):
25+
@cached_property
26+
def with_raw_response(self) -> PlaywrightResourceWithRawResponse:
27+
"""
28+
This property can be used as a prefix for any HTTP method call to return
29+
the raw response object instead of the parsed content.
30+
31+
For more information, see https://www.github.com/onkernel/kernel-python-sdk#accessing-raw-response-data-eg-headers
32+
"""
33+
return PlaywrightResourceWithRawResponse(self)
34+
35+
@cached_property
36+
def with_streaming_response(self) -> PlaywrightResourceWithStreamingResponse:
37+
"""
38+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
39+
40+
For more information, see https://www.github.com/onkernel/kernel-python-sdk#with_streaming_response
41+
"""
42+
return PlaywrightResourceWithStreamingResponse(self)
43+
44+
def execute(
45+
self,
46+
id: str,
47+
*,
48+
code: str,
49+
timeout_sec: int | Omit = omit,
50+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
51+
# The extra values given here take precedence over values defined on the client or passed to this method.
52+
extra_headers: Headers | None = None,
53+
extra_query: Query | None = None,
54+
extra_body: Body | None = None,
55+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
56+
) -> PlaywrightExecuteResponse:
57+
"""
58+
Execute arbitrary Playwright code in a fresh execution context against the
59+
browser. The code runs in the same VM as the browser, minimizing latency and
60+
maximizing throughput. It has access to 'page', 'context', and 'browser'
61+
variables. It can `return` a value, and this value is returned in the response.
62+
63+
Args:
64+
code: TypeScript/JavaScript code to execute. The code has access to 'page', 'context',
65+
and 'browser' variables. It runs within a function, so you can use a return
66+
statement at the end to return a value. This value is returned as the `result`
67+
property in the response. Example: "await page.goto('https://example.com');
68+
return await page.title();"
69+
70+
timeout_sec: Maximum execution time in seconds. Default is 60.
71+
72+
extra_headers: Send extra headers
73+
74+
extra_query: Add additional query parameters to the request
75+
76+
extra_body: Add additional JSON properties to the request
77+
78+
timeout: Override the client-level default timeout for this request, in seconds
79+
"""
80+
if not id:
81+
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
82+
return self._post(
83+
f"/browsers/{id}/playwright/execute",
84+
body=maybe_transform(
85+
{
86+
"code": code,
87+
"timeout_sec": timeout_sec,
88+
},
89+
playwright_execute_params.PlaywrightExecuteParams,
90+
),
91+
options=make_request_options(
92+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
93+
),
94+
cast_to=PlaywrightExecuteResponse,
95+
)
96+
97+
98+
class AsyncPlaywrightResource(AsyncAPIResource):
99+
@cached_property
100+
def with_raw_response(self) -> AsyncPlaywrightResourceWithRawResponse:
101+
"""
102+
This property can be used as a prefix for any HTTP method call to return
103+
the raw response object instead of the parsed content.
104+
105+
For more information, see https://www.github.com/onkernel/kernel-python-sdk#accessing-raw-response-data-eg-headers
106+
"""
107+
return AsyncPlaywrightResourceWithRawResponse(self)
108+
109+
@cached_property
110+
def with_streaming_response(self) -> AsyncPlaywrightResourceWithStreamingResponse:
111+
"""
112+
An alternative to `.with_raw_response` that doesn't eagerly read the response body.
113+
114+
For more information, see https://www.github.com/onkernel/kernel-python-sdk#with_streaming_response
115+
"""
116+
return AsyncPlaywrightResourceWithStreamingResponse(self)
117+
118+
async def execute(
119+
self,
120+
id: str,
121+
*,
122+
code: str,
123+
timeout_sec: int | Omit = omit,
124+
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
125+
# The extra values given here take precedence over values defined on the client or passed to this method.
126+
extra_headers: Headers | None = None,
127+
extra_query: Query | None = None,
128+
extra_body: Body | None = None,
129+
timeout: float | httpx.Timeout | None | NotGiven = not_given,
130+
) -> PlaywrightExecuteResponse:
131+
"""
132+
Execute arbitrary Playwright code in a fresh execution context against the
133+
browser. The code runs in the same VM as the browser, minimizing latency and
134+
maximizing throughput. It has access to 'page', 'context', and 'browser'
135+
variables. It can `return` a value, and this value is returned in the response.
136+
137+
Args:
138+
code: TypeScript/JavaScript code to execute. The code has access to 'page', 'context',
139+
and 'browser' variables. It runs within a function, so you can use a return
140+
statement at the end to return a value. This value is returned as the `result`
141+
property in the response. Example: "await page.goto('https://example.com');
142+
return await page.title();"
143+
144+
timeout_sec: Maximum execution time in seconds. Default is 60.
145+
146+
extra_headers: Send extra headers
147+
148+
extra_query: Add additional query parameters to the request
149+
150+
extra_body: Add additional JSON properties to the request
151+
152+
timeout: Override the client-level default timeout for this request, in seconds
153+
"""
154+
if not id:
155+
raise ValueError(f"Expected a non-empty value for `id` but received {id!r}")
156+
return await self._post(
157+
f"/browsers/{id}/playwright/execute",
158+
body=await async_maybe_transform(
159+
{
160+
"code": code,
161+
"timeout_sec": timeout_sec,
162+
},
163+
playwright_execute_params.PlaywrightExecuteParams,
164+
),
165+
options=make_request_options(
166+
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
167+
),
168+
cast_to=PlaywrightExecuteResponse,
169+
)
170+
171+
172+
class PlaywrightResourceWithRawResponse:
173+
def __init__(self, playwright: PlaywrightResource) -> None:
174+
self._playwright = playwright
175+
176+
self.execute = to_raw_response_wrapper(
177+
playwright.execute,
178+
)
179+
180+
181+
class AsyncPlaywrightResourceWithRawResponse:
182+
def __init__(self, playwright: AsyncPlaywrightResource) -> None:
183+
self._playwright = playwright
184+
185+
self.execute = async_to_raw_response_wrapper(
186+
playwright.execute,
187+
)
188+
189+
190+
class PlaywrightResourceWithStreamingResponse:
191+
def __init__(self, playwright: PlaywrightResource) -> None:
192+
self._playwright = playwright
193+
194+
self.execute = to_streamed_response_wrapper(
195+
playwright.execute,
196+
)
197+
198+
199+
class AsyncPlaywrightResourceWithStreamingResponse:
200+
def __init__(self, playwright: AsyncPlaywrightResource) -> None:
201+
self._playwright = playwright
202+
203+
self.execute = async_to_streamed_response_wrapper(
204+
playwright.execute,
205+
)

src/kernel/types/browsers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
from .f_create_directory_params import FCreateDirectoryParams as FCreateDirectoryParams
3232
from .f_delete_directory_params import FDeleteDirectoryParams as FDeleteDirectoryParams
3333
from .f_download_dir_zip_params import FDownloadDirZipParams as FDownloadDirZipParams
34+
from .playwright_execute_params import PlaywrightExecuteParams as PlaywrightExecuteParams
3435
from .computer_drag_mouse_params import ComputerDragMouseParams as ComputerDragMouseParams
3536
from .computer_move_mouse_params import ComputerMoveMouseParams as ComputerMoveMouseParams
3637
from .computer_click_mouse_params import ComputerClickMouseParams as ComputerClickMouseParams
38+
from .playwright_execute_response import PlaywrightExecuteResponse as PlaywrightExecuteResponse
3739
from .f_set_file_permissions_params import FSetFilePermissionsParams as FSetFilePermissionsParams
3840
from .process_stdout_stream_response import ProcessStdoutStreamResponse as ProcessStdoutStreamResponse
3941
from .computer_capture_screenshot_params import ComputerCaptureScreenshotParams as ComputerCaptureScreenshotParams
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from __future__ import annotations
4+
5+
from typing_extensions import Required, TypedDict
6+
7+
__all__ = ["PlaywrightExecuteParams"]
8+
9+
10+
class PlaywrightExecuteParams(TypedDict, total=False):
11+
code: Required[str]
12+
"""TypeScript/JavaScript code to execute.
13+
14+
The code has access to 'page', 'context', and 'browser' variables. It runs
15+
within a function, so you can use a return statement at the end to return a
16+
value. This value is returned as the `result` property in the response. Example:
17+
"await page.goto('https://example.com'); return await page.title();"
18+
"""
19+
20+
timeout_sec: int
21+
"""Maximum execution time in seconds. Default is 60."""
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from typing import Optional
4+
5+
from ..._models import BaseModel
6+
7+
__all__ = ["PlaywrightExecuteResponse"]
8+
9+
10+
class PlaywrightExecuteResponse(BaseModel):
11+
success: bool
12+
"""Whether the code executed successfully"""
13+
14+
error: Optional[str] = None
15+
"""Error message if execution failed"""
16+
17+
result: Optional[object] = None
18+
"""The value returned by the code (if any)"""
19+
20+
stderr: Optional[str] = None
21+
"""Standard error from the execution"""
22+
23+
stdout: Optional[str] = None
24+
"""Standard output from the execution"""

0 commit comments

Comments
 (0)