The best way to control Claude Code, Codex and more from any computer or phone

main
ts 60 lines 2.41 KB
Raw
1 import * as Effect from "effect/Effect";
2 import type * as HttpClient from "effect/http/HttpClient";
3 import * as Layer from "effect/Layer";
4 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
5 import type { Credentials } from "../Credentials.ts";
6 import { Browser } from "./Browser.ts";
7 import type { BrowserBinding } from "./BrowserBinding.ts";
8 import { type BrowserAuth, makeHttpBrowserClient } from "./BrowserHttpClient.ts";
9
10 /**
11 * Local implementation of the {@link Browser} binding — drives Cloudflare
12 * Browser Rendering over its REST data-plane (`/accounts/{id}/browser-rendering/*`)
13 * using the **current credentials** instead of a native Worker binding
14 * (`BrowserBinding`).
15 *
16 * Provide it on an {@link Action} (or any deploy-time Effect) to run the JSON
17 * quick actions — `content`, `markdown`, `scrape`, `links`, `snapshot`,
18 * `json` — with the same client you'd use inside a Worker; no Worker host, no
19 * `host.bind`, no minted token:
20 *
21 * @example Convert a page to Markdown from an Action
22 * ```typescript
23 * const Scrape = Alchemy.Action(
24 * "Scrape",
25 * Effect.gen(function* () {
26 * const browser = yield* Cloudflare.Browser("BROWSER");
27 * return Effect.fn(function* () {
28 * const { result } = yield* browser.markdown({
29 * url: "https://example.com",
30 * });
31 * return result;
32 * });
33 * }).pipe(Effect.provide(Cloudflare.Workers.BrowserLocal)),
34 * );
35 * ```
36 *
37 * `raw`, `fetch`, and the binary actions (`screenshot`/`pdf`) have no
38 * Cloudflare REST equivalent and die — see {@link makeHttpBrowserClient}.
39 */
40 export const BrowserLocal = Layer.effect(
41 Browser,
42 Effect.gen(function* () {
43 // Account + credentials are ambient during stack-eval (the stack's
44 // providers layer). Capture the full context so the REST ops run with the
45 // current credentials — no `host.bind`, no minted token.
46 const { accountId } = yield* yield* CloudflareEnvironment;
47 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
48 const auth: BrowserAuth = {
49 authorize: (eff) => eff.pipe(Effect.provideContext(context)),
50 accountId,
51 };
52
53 return Effect.fn(function* (_binding: BrowserBinding) {
54 // Browser Rendering is account-scoped; the binding carries no cloud
55 // resource id, so nothing to resolve — the client only needs the account
56 // + injected auth.
57 return makeHttpBrowserClient(auth);
58 });
59 }),
60 );