|
| 1 | +// Cloud-browser attach (decisions §3.3 / §6 — single API key, BU cloud |
| 2 | +// surfaces). Provisions a Browser Use cloud browser via the public |
| 3 | +// /api/v3/browsers REST surface and connects a `Session` to its `cdpUrl`. |
| 4 | +// |
| 5 | +// Three calls per attach: |
| 6 | +// 1. POST /api/v3/browsers → { id, cdpUrl, liveUrl } |
| 7 | +// 2. (caller) session.connect({ wsUrl: cdpUrl }) |
| 8 | +// 3. PATCH /api/v3/browsers/<id> → { state: "stop" } (finalizer) |
| 9 | +// |
| 10 | +// `BROWSER_USE_API_KEY` must be set; we fail fast if absent so the Level-2 |
| 11 | +// wrapper can render a one-line error pointing at our docs without |
| 12 | +// constructing a bad request. |
| 13 | + |
| 14 | +import { Effect, Schema } from "effect" |
| 15 | +import { SessionStore } from "./session-store" |
| 16 | + |
| 17 | +const API_BASE = "https://api.browser-use.com/api/v3/browsers" |
| 18 | + |
| 19 | +export const provisionParameters = Schema.Struct({ |
| 20 | + profileId: Schema.optional(Schema.String).annotate({ |
| 21 | + description: "Existing BU cloud profile id to attach to. Omit for a fresh ephemeral profile.", |
| 22 | + }), |
| 23 | + proxyCountryCode: Schema.optional(Schema.String).annotate({ |
| 24 | + description: "ISO-2 country code for the proxy pool (e.g. \"us\", \"de\").", |
| 25 | + }), |
| 26 | +}) |
| 27 | + |
| 28 | +export type ProvisionParameters = Schema.Schema.Type<typeof provisionParameters> |
| 29 | + |
| 30 | +export interface ProvisionResult { |
| 31 | + readonly id: string |
| 32 | + readonly cdpUrl: string |
| 33 | + readonly liveUrl: string |
| 34 | +} |
| 35 | + |
| 36 | +const apiKey = () => { |
| 37 | + const k = process.env.BROWSER_USE_API_KEY |
| 38 | + if (!k) { |
| 39 | + throw new Error( |
| 40 | + "BROWSER_USE_API_KEY is not set. Cloud browsers require a Browser Use API key — get one at https://browser-use.com.", |
| 41 | + ) |
| 42 | + } |
| 43 | + return k |
| 44 | +} |
| 45 | + |
| 46 | +const provision = (args: ProvisionParameters) => |
| 47 | + Effect.tryPromise({ |
| 48 | + try: async () => { |
| 49 | + const res = await fetch(API_BASE, { |
| 50 | + method: "POST", |
| 51 | + headers: { |
| 52 | + "X-Browser-Use-API-Key": apiKey(), |
| 53 | + "Content-Type": "application/json", |
| 54 | + }, |
| 55 | + body: JSON.stringify({ |
| 56 | + profile_id: args.profileId, |
| 57 | + proxy_country_code: args.proxyCountryCode, |
| 58 | + }), |
| 59 | + }) |
| 60 | + if (!res.ok) throw new Error(`provision failed: ${res.status} ${await res.text()}`) |
| 61 | + const body = (await res.json()) as { id: string; cdp_url?: string; cdpUrl?: string; live_url?: string; liveUrl?: string } |
| 62 | + const cdpUrl = body.cdpUrl ?? body.cdp_url |
| 63 | + const liveUrl = body.liveUrl ?? body.live_url |
| 64 | + if (!cdpUrl || !liveUrl) throw new Error(`provision response missing cdpUrl/liveUrl: ${JSON.stringify(body)}`) |
| 65 | + return { id: body.id, cdpUrl, liveUrl } satisfies ProvisionResult |
| 66 | + }, |
| 67 | + catch: (err) => (err instanceof Error ? err : new Error(String(err))), |
| 68 | + }) |
| 69 | + |
| 70 | +const stop = (id: string) => |
| 71 | + Effect.tryPromise({ |
| 72 | + try: async () => { |
| 73 | + const res = await fetch(`${API_BASE}/${id}`, { |
| 74 | + method: "PATCH", |
| 75 | + headers: { |
| 76 | + "X-Browser-Use-API-Key": apiKey(), |
| 77 | + "Content-Type": "application/json", |
| 78 | + }, |
| 79 | + body: JSON.stringify({ state: "stop" }), |
| 80 | + }) |
| 81 | + if (!res.ok) throw new Error(`stop failed: ${res.status} ${await res.text()}`) |
| 82 | + }, |
| 83 | + catch: (err) => (err instanceof Error ? err : new Error(String(err))), |
| 84 | + }) |
| 85 | + |
| 86 | +// Provisions a cloud browser, connects the per-opencode-session `Session` to |
| 87 | +// it, and registers a stop-callback with `SessionStore` so the browser is |
| 88 | +// torn down when the session is evicted (or, in practice, at process exit |
| 89 | +// since opencode doesn't currently call evict — that's a known gap matching |
| 90 | +// today's `uv run` subprocess shape, where a stuck Python interpreter also |
| 91 | +// outlives the bcode session). The Session is shared with `browser_execute` |
| 92 | +// via `SessionStore`. Returns the public bits the agent needs. |
| 93 | +export const open = Effect.fn("CloudBrowser.open")(function* ( |
| 94 | + sessionID: string, |
| 95 | + args: ProvisionParameters, |
| 96 | +) { |
| 97 | + const { id, cdpUrl, liveUrl } = yield* provision(args) |
| 98 | + const session = SessionStore.get(sessionID) |
| 99 | + SessionStore.onEvict(sessionID, () => |
| 100 | + Effect.runPromise(stop(id).pipe(Effect.ignore)), |
| 101 | + ) |
| 102 | + yield* Effect.tryPromise({ |
| 103 | + try: () => session.connect({ wsUrl: cdpUrl }), |
| 104 | + catch: (err) => (err instanceof Error ? err : new Error(String(err))), |
| 105 | + }) |
| 106 | + return { id, liveUrl } as const |
| 107 | +}) |
| 108 | + |
| 109 | +export * as CloudBrowser from "./cloud-browser" |
0 commit comments