@@ -149,9 +149,6 @@ import { multipartFormRequestOptions } from '../../internal/uploads';
149149import { path } from '../../internal/utils/path' ;
150150import { browserFetch , type BrowserFetchInit } from '../../lib/browser-fetch' ;
151151
152- /**
153- * Create and manage browser sessions.
154- */
155152export class Browsers extends APIResource {
156153 telemetry : TelemetryAPI . Telemetry = new TelemetryAPI . Telemetry ( this . _client ) ;
157154 replays : ReplaysAPI . Replays = new ReplaysAPI . Replays ( this . _client ) ;
@@ -306,6 +303,33 @@ export class Browsers extends APIResource {
306303 ) ,
307304 ) ;
308305 }
306+
307+ /**
308+ * Execute JavaScript in a persistent Node.js runtime inside the browser VM.
309+ * Top-level bindings, closures, mutations, and dynamically imported modules
310+ * persist across calls until the REPL is reset or replaced. Start with
311+ * `repl.help()` to list available methods, or call `repl.help("click")` for
312+ * detailed help.
313+ *
314+ * Expression values are ignored. Emit ordered text or image output with
315+ * `repl.write(...)`, console methods, or `repl.emitImage(...)`. The runtime also
316+ * exposes browser-control helpers, WebMCP, Patchright, Playwright, and raw CDP.
317+ *
318+ * Executions are serialized. A timeout, crash, OOM, or protocol failure terminates
319+ * the REPL and changes its `repl_id`. This is unrestricted code execution inside
320+ * the browser VM and is not sandboxed.
321+ *
322+ * @example
323+ * ```ts
324+ * const browserReplResult = await client.browsers.repl(
325+ * 'htzv5orfit78e1m2biiifpbv',
326+ * { code: 'code' },
327+ * );
328+ * ```
329+ */
330+ repl ( idOrName : string , body : BrowserReplParams , options ?: RequestOptions ) : APIPromise < BrowserReplResult > {
331+ return this . _client . post ( path `/browsers/${ idOrName } /repl` , { body, ...options } ) ;
332+ }
309333}
310334
311335export type BrowserListResponsesOffsetPagination = OffsetPagination < BrowserListResponse > ;
@@ -434,6 +458,104 @@ export interface BrowserProxyConfig {
434458 */
435459export type BrowserProxyMode = 'direct' | 'default' ;
436460
461+ /**
462+ * Ordered discriminated union of Browser REPL output items.
463+ */
464+ export type BrowserReplContent = BrowserReplTextContent | BrowserReplImageContent ;
465+
466+ export interface BrowserReplImageContent {
467+ data_b64 : string ;
468+
469+ mime_type : string ;
470+
471+ type : 'image' ;
472+ }
473+
474+ /**
475+ * Request to execute code in the persistent Browser REPL.
476+ */
477+ export interface BrowserReplRequest {
478+ /**
479+ * JavaScript evaluated in a persistent Node.js runtime. Top-level bindings persist
480+ * until the browser VM's API process exits, the REPL is reset, or the REPL is
481+ * terminated after a crash or timeout. Static top-level imports are unsupported;
482+ * use dynamic `import()`. Expression values are ignored; emit output with
483+ * `repl.write(...)`, console methods, or `repl.emitImage(...)`. May be empty only
484+ * when `reset` is true.
485+ */
486+ code : string ;
487+
488+ /**
489+ * Terminate the current REPL, start a fresh one, and then evaluate code.
490+ */
491+ reset ?: boolean ;
492+
493+ /**
494+ * Maximum execution time in seconds. Default is 60.
495+ */
496+ timeout_sec ?: number ;
497+ }
498+
499+ /**
500+ * Result of Browser REPL code execution.
501+ */
502+ export interface BrowserReplResult {
503+ /**
504+ * CUID2 identifying the exact state-holding REPL process used for this execution.
505+ * Stable across calls and Chromium reconnects; changes after an API restart,
506+ * explicit reset, execution timeout, or REPL crash.
507+ */
508+ repl_id : string ;
509+
510+ /**
511+ * Whether the code executed successfully.
512+ */
513+ success : boolean ;
514+
515+ /**
516+ * Optional ordered text/image output produced by the execution.
517+ */
518+ content ?: Array < BrowserReplContent > ;
519+
520+ /**
521+ * True if text or image output was dropped or truncated due to response limits.
522+ */
523+ content_truncated ?: boolean ;
524+
525+ /**
526+ * Wall-clock execution time in milliseconds.
527+ */
528+ duration_ms ?: number ;
529+
530+ /**
531+ * Error message if execution failed.
532+ */
533+ error ?: string ;
534+
535+ /**
536+ * True if the REPL identified by `repl_id` was terminated by this request. The
537+ * next request lazily starts a fresh REPL with a new `repl_id`.
538+ */
539+ repl_terminated ?: boolean ;
540+
541+ /**
542+ * Stack trace if execution failed.
543+ */
544+ stack ?: string ;
545+ }
546+
547+ export interface BrowserReplTextContent {
548+ /**
549+ * `write` is emitted by `repl.write`; `stdout` and `stderr` are emitted by console
550+ * methods.
551+ */
552+ channel : 'write' | 'stdout' | 'stderr' ;
553+
554+ text : string ;
555+
556+ type : 'text' ;
557+ }
558+
437559/**
438560 * Session usage metrics.
439561 */
@@ -1703,6 +1825,28 @@ export namespace BrowserLoadExtensionsParams {
17031825 }
17041826}
17051827
1828+ export interface BrowserReplParams {
1829+ /**
1830+ * JavaScript evaluated in a persistent Node.js runtime. Top-level bindings persist
1831+ * until the browser VM's API process exits, the REPL is reset, or the REPL is
1832+ * terminated after a crash or timeout. Static top-level imports are unsupported;
1833+ * use dynamic `import()`. Expression values are ignored; emit output with
1834+ * `repl.write(...)`, console methods, or `repl.emitImage(...)`. May be empty only
1835+ * when `reset` is true.
1836+ */
1837+ code : string ;
1838+
1839+ /**
1840+ * Terminate the current REPL, start a fresh one, and then evaluate code.
1841+ */
1842+ reset ?: boolean ;
1843+
1844+ /**
1845+ * Maximum execution time in seconds. Default is 60.
1846+ */
1847+ timeout_sec ?: number ;
1848+ }
1849+
17061850Browsers . Telemetry = TelemetryAPITelemetry ;
17071851Browsers . Replays = Replays ;
17081852Browsers . Fs = Fs ;
@@ -1721,6 +1865,11 @@ export declare namespace Browsers {
17211865 type BrowserProxy as BrowserProxy ,
17221866 type BrowserProxyConfig as BrowserProxyConfig ,
17231867 type BrowserProxyMode as BrowserProxyMode ,
1868+ type BrowserReplContent as BrowserReplContent ,
1869+ type BrowserReplImageContent as BrowserReplImageContent ,
1870+ type BrowserReplRequest as BrowserReplRequest ,
1871+ type BrowserReplResult as BrowserReplResult ,
1872+ type BrowserReplTextContent as BrowserReplTextContent ,
17241873 type BrowserUsage as BrowserUsage ,
17251874 type Profile as Profile ,
17261875 type Tags as Tags ,
@@ -1737,6 +1886,7 @@ export declare namespace Browsers {
17371886 type BrowserListParams as BrowserListParams ,
17381887 type BrowserCurlParams as BrowserCurlParams ,
17391888 type BrowserLoadExtensionsParams as BrowserLoadExtensionsParams ,
1889+ type BrowserReplParams as BrowserReplParams ,
17401890 } ;
17411891
17421892 export {
0 commit comments