Skip to content

Commit 1112215

Browse files
feat: ad hoc playwright code exec AP|
1 parent 63aee26 commit 1112215

File tree

6 files changed

+135
-4
lines changed

6 files changed

+135
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,16 @@ Methods:
162162
- <code title="post /browsers/{id}/computer/scroll">client.browsers.computer.<a href="./src/resources/browsers/computer.ts">scroll</a>(id, { ...params }) -> void</code>
163163
- <code title="post /browsers/{id}/computer/type">client.browsers.computer.<a href="./src/resources/browsers/computer.ts">typeText</a>(id, { ...params }) -> void</code>
164164

165+
## Playwright
166+
167+
Types:
168+
169+
- <code><a href="./src/resources/browsers/playwright.ts">PlaywrightExecuteResponse</a></code>
170+
171+
Methods:
172+
173+
- <code title="post /browsers/{id}/playwright/execute">client.browsers.playwright.<a href="./src/resources/browsers/playwright.ts">execute</a>(id, { ...params }) -> PlaywrightExecuteResponse</code>
174+
165175
# Profiles
166176

167177
Types:

src/resources/browsers/browsers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from './computer';
1616
import * as LogsAPI from './logs';
1717
import { LogStreamParams, Logs } from './logs';
18+
import * as PlaywrightAPI from './playwright';
19+
import { Playwright, PlaywrightExecuteParams, PlaywrightExecuteResponse } from './playwright';
1820
import * as ProcessAPI from './process';
1921
import {
2022
Process,
@@ -71,6 +73,7 @@ export class Browsers extends APIResource {
7173
process: ProcessAPI.Process = new ProcessAPI.Process(this._client);
7274
logs: LogsAPI.Logs = new LogsAPI.Logs(this._client);
7375
computer: ComputerAPI.Computer = new ComputerAPI.Computer(this._client);
76+
playwright: PlaywrightAPI.Playwright = new PlaywrightAPI.Playwright(this._client);
7477

7578
/**
7679
* Create a new browser session from within an action.
@@ -695,6 +698,7 @@ Browsers.Fs = Fs;
695698
Browsers.Process = Process;
696699
Browsers.Logs = Logs;
697700
Browsers.Computer = Computer;
701+
Browsers.Playwright = Playwright;
698702

699703
export declare namespace Browsers {
700704
export {
@@ -763,4 +767,10 @@ export declare namespace Browsers {
763767
type ComputerScrollParams as ComputerScrollParams,
764768
type ComputerTypeTextParams as ComputerTypeTextParams,
765769
};
770+
771+
export {
772+
Playwright as Playwright,
773+
type PlaywrightExecuteResponse as PlaywrightExecuteResponse,
774+
type PlaywrightExecuteParams as PlaywrightExecuteParams,
775+
};
766776
}

src/resources/browsers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export {
3939
type FWriteFileParams,
4040
} from './fs/index';
4141
export { Logs, type LogStreamParams } from './logs';
42+
export { Playwright, type PlaywrightExecuteResponse, type PlaywrightExecuteParams } from './playwright';
4243
export {
4344
Process,
4445
type ProcessExecResponse,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { APIResource } from '../../core/resource';
4+
import { APIPromise } from '../../core/api-promise';
5+
import { RequestOptions } from '../../internal/request-options';
6+
import { path } from '../../internal/utils/path';
7+
8+
export class Playwright extends APIResource {
9+
/**
10+
* Execute arbitrary Playwright code in a fresh execution context against the
11+
* browser. The code runs in the same VM as the browser, minimizing latency and
12+
* maximizing throughput. It has access to 'page', 'context', and 'browser'
13+
* variables. It can `return` a value, and this value is returned in the response.
14+
*
15+
* @example
16+
* ```ts
17+
* const response = await client.browsers.playwright.execute(
18+
* 'id',
19+
* { code: 'code' },
20+
* );
21+
* ```
22+
*/
23+
execute(
24+
id: string,
25+
body: PlaywrightExecuteParams,
26+
options?: RequestOptions,
27+
): APIPromise<PlaywrightExecuteResponse> {
28+
return this._client.post(path`/browsers/${id}/playwright/execute`, { body, ...options });
29+
}
30+
}
31+
32+
/**
33+
* Result of Playwright code execution
34+
*/
35+
export interface PlaywrightExecuteResponse {
36+
/**
37+
* Whether the code executed successfully
38+
*/
39+
success: boolean;
40+
41+
/**
42+
* Error message if execution failed
43+
*/
44+
error?: string;
45+
46+
/**
47+
* The value returned by the code (if any)
48+
*/
49+
result?: unknown;
50+
51+
/**
52+
* Standard error from the execution
53+
*/
54+
stderr?: string;
55+
56+
/**
57+
* Standard output from the execution
58+
*/
59+
stdout?: string;
60+
}
61+
62+
export interface PlaywrightExecuteParams {
63+
/**
64+
* 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+
code: string;
71+
72+
/**
73+
* Maximum execution time in seconds. Default is 60.
74+
*/
75+
timeout_sec?: number;
76+
}
77+
78+
export declare namespace Playwright {
79+
export {
80+
type PlaywrightExecuteResponse as PlaywrightExecuteResponse,
81+
type PlaywrightExecuteParams as PlaywrightExecuteParams,
82+
};
83+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import Kernel from '@onkernel/sdk';
4+
5+
const client = new Kernel({
6+
apiKey: 'My API Key',
7+
baseURL: process.env['TEST_API_BASE_URL'] ?? 'http://127.0.0.1:4010',
8+
});
9+
10+
describe('resource playwright', () => {
11+
// Prism tests are disabled
12+
test.skip('execute: only required params', async () => {
13+
const responsePromise = client.browsers.playwright.execute('id', { code: 'code' });
14+
const rawResponse = await responsePromise.asResponse();
15+
expect(rawResponse).toBeInstanceOf(Response);
16+
const response = await responsePromise;
17+
expect(response).not.toBeInstanceOf(Response);
18+
const dataAndResponse = await responsePromise.withResponse();
19+
expect(dataAndResponse.data).toBe(response);
20+
expect(dataAndResponse.response).toBe(rawResponse);
21+
});
22+
23+
// Prism tests are disabled
24+
test.skip('execute: required and optional params', async () => {
25+
const response = await client.browsers.playwright.execute('id', { code: 'code', timeout_sec: 1 });
26+
});
27+
});

0 commit comments

Comments
 (0)