The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as cf from "@cloudflare/workers-types"; |
| 2 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import * as Binding from "./Binding.ts"; |
| 6 | import { makeBindingLayer } from "./BindingLayer.ts"; |
| 7 | import { Browser, type BrowserClient, BrowserError, type BrowserResponse } from "./Browser.ts"; |
| 8 | |
| 9 | /** The binding value produced by calling {@link Browser} (declared on `env` or `yield*`-ed). */ |
| 10 | export type BrowserBinding = Binding.Binding<Browser["key"], BrowserClient, Browser>; |
| 11 | |
| 12 | /** |
| 13 | * The layer that provides the Effect-native interface for the Cloudflare |
| 14 | * Workers Browser Rendering binding. |
| 15 | * |
| 16 | * Provide it on the Worker effect (`Effect.provide(Cloudflare.Workers.BrowserBinding)`) |
| 17 | * so that yielding a {@link Browser} binding attaches the native `browser` |
| 18 | * binding to the surrounding Worker at deploy time and, at runtime, resolves to |
| 19 | * the Effect-native {@link BrowserClient}. |
| 20 | */ |
| 21 | export const BrowserBinding = makeBindingLayer<Browser, cf.BrowserRun, BrowserClient>( |
| 22 | Browser, |
| 23 | (raw): BrowserClient => { |
| 24 | const respond = ( |
| 25 | action: string, |
| 26 | options: unknown, |
| 27 | ): Effect.Effect<BrowserResponse, BrowserError, RuntimeContext> => |
| 28 | raw.pipe( |
| 29 | Effect.flatMap((binding) => |
| 30 | tryPromise(() => binding.quickAction(action as any, options as any)), |
| 31 | ), |
| 32 | Effect.flatMap((response) => |
| 33 | response.ok ? Effect.succeed(response) : failResponse(action, response), |
| 34 | ), |
| 35 | ); |
| 36 | |
| 37 | const jsonAction = <T>( |
| 38 | action: string, |
| 39 | options: unknown, |
| 40 | ): Effect.Effect<T, BrowserError, RuntimeContext> => |
| 41 | respond(action, options).pipe( |
| 42 | Effect.flatMap((response) => tryPromise(() => response.json() as Promise<T>)), |
| 43 | ); |
| 44 | |
| 45 | const streamAction = ( |
| 46 | action: string, |
| 47 | options: unknown, |
| 48 | ): Stream.Stream<Uint8Array, BrowserError, RuntimeContext> => |
| 49 | respond(action, options).pipe( |
| 50 | Effect.map((response) => |
| 51 | Stream.fromReadableStream({ |
| 52 | evaluate: () => response.body as any as ReadableStream<Uint8Array<ArrayBufferLike>>, |
| 53 | onError: (cause) => |
| 54 | new BrowserError({ |
| 55 | message: `Browser Rendering '${action}' stream failed`, |
| 56 | cause, |
| 57 | }), |
| 58 | }), |
| 59 | ), |
| 60 | Stream.unwrap, |
| 61 | ); |
| 62 | |
| 63 | const quickAction = ((action: string, options: unknown) => |
| 64 | BINARY_ACTIONS.has(action) |
| 65 | ? streamAction(action, options) |
| 66 | : jsonAction(action, options)) as BrowserClient["quickAction"]; |
| 67 | |
| 68 | return { |
| 69 | raw, |
| 70 | fetch: (...args) => |
| 71 | raw.pipe(Effect.flatMap((binding) => tryPromise(() => binding.fetch(...args)))), |
| 72 | quickAction, |
| 73 | screenshot: (options) => streamAction("screenshot", options), |
| 74 | pdf: (options) => streamAction("pdf", options), |
| 75 | content: (options) => jsonAction("content", options), |
| 76 | scrape: (options) => jsonAction("scrape", options), |
| 77 | links: (options) => jsonAction("links", options), |
| 78 | snapshot: (options) => jsonAction("snapshot", options), |
| 79 | json: (options) => jsonAction("json", options), |
| 80 | markdown: (options) => jsonAction("markdown", options), |
| 81 | } satisfies BrowserClient; |
| 82 | }, |
| 83 | ); |
| 84 | |
| 85 | /** Actions whose successful response is raw binary rather than JSON. */ |
| 86 | const BINARY_ACTIONS = new Set(["screenshot", "pdf"]); |
| 87 | |
| 88 | /** Build a {@link BrowserError} from a non-success Browser Run response. */ |
| 89 | const failResponse = ( |
| 90 | action: string, |
| 91 | response: BrowserResponse, |
| 92 | ): Effect.Effect<never, BrowserError> => |
| 93 | tryPromise(() => response.text()).pipe( |
| 94 | Effect.flatMap((body) => { |
| 95 | let cause: unknown = body; |
| 96 | try { |
| 97 | cause = JSON.parse(body); |
| 98 | } catch { |
| 99 | // keep the raw text as the cause |
| 100 | } |
| 101 | const message = |
| 102 | (cause as cf.BrowserRunErrorResponse | undefined)?.errors?.[0]?.message ?? |
| 103 | `Browser Rendering '${action}' failed with status ${response.status}`; |
| 104 | return Effect.fail(new BrowserError({ message, cause })); |
| 105 | }), |
| 106 | ); |
| 107 | |
| 108 | const tryPromise = <T>(fn: () => Promise<T>): Effect.Effect<T, BrowserError> => |
| 109 | Effect.tryPromise({ |
| 110 | try: fn, |
| 111 | catch: (error) => |
| 112 | new BrowserError({ |
| 113 | message: error instanceof Error ? error.message : "Unknown Browser Rendering error", |
| 114 | cause: error, |
| 115 | }), |
| 116 | }); |