The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | |
| 7 | const TARGET_URL = "https://example.com"; |
| 8 | |
| 9 | const byteLength = <E, R>(stream: Stream.Stream<Uint8Array, E, R>) => |
| 10 | stream.pipe( |
| 11 | Stream.runCollect, |
| 12 | Effect.map((chunks) => Array.from(chunks).reduce((total, chunk) => total + chunk.length, 0)), |
| 13 | ); |
| 14 | |
| 15 | export default class BrowserEffectWorker extends Cloudflare.Worker<BrowserEffectWorker>()( |
| 16 | "BrowserEffectWorker", |
| 17 | { |
| 18 | main: import.meta.url, |
| 19 | }, |
| 20 | Effect.gen(function* () { |
| 21 | const browser = yield* Cloudflare.Browser("BROWSER"); |
| 22 | |
| 23 | return { |
| 24 | fetch: Effect.gen(function* () { |
| 25 | const request = yield* HttpServerRequest; |
| 26 | const path = request.url.split("?")[0]; |
| 27 | |
| 28 | switch (path) { |
| 29 | // JSON quick actions resolve to their parsed payload directly. |
| 30 | case "/content": { |
| 31 | const content = yield* browser.content({ url: TARGET_URL }).pipe(Effect.orDie); |
| 32 | return yield* HttpServerResponse.json({ |
| 33 | title: content.meta.title, |
| 34 | contentLength: content.result.length, |
| 35 | }); |
| 36 | } |
| 37 | case "/markdown": { |
| 38 | const markdown = yield* browser.markdown({ url: TARGET_URL }).pipe(Effect.orDie); |
| 39 | return yield* HttpServerResponse.json({ |
| 40 | markdownLength: markdown.result.length, |
| 41 | }); |
| 42 | } |
| 43 | case "/links": { |
| 44 | const links = yield* browser.links({ url: TARGET_URL }).pipe(Effect.orDie); |
| 45 | return yield* HttpServerResponse.json({ |
| 46 | linkCount: links.result.length, |
| 47 | }); |
| 48 | } |
| 49 | case "/scrape": { |
| 50 | const scrape = yield* browser |
| 51 | // example.com has no heading; its only link reads "Learn more". |
| 52 | .scrape({ url: TARGET_URL, elements: [{ selector: "a" }] }) |
| 53 | .pipe(Effect.orDie); |
| 54 | return yield* HttpServerResponse.json({ |
| 55 | text: scrape.result[0]?.results[0]?.text ?? null, |
| 56 | }); |
| 57 | } |
| 58 | case "/snapshot": { |
| 59 | const snapshot = yield* browser.snapshot({ url: TARGET_URL }).pipe(Effect.orDie); |
| 60 | return yield* HttpServerResponse.json({ |
| 61 | title: snapshot.meta.title, |
| 62 | screenshotLength: snapshot.result.screenshot?.length ?? 0, |
| 63 | }); |
| 64 | } |
| 65 | case "/json": { |
| 66 | const extracted = yield* browser |
| 67 | .json({ |
| 68 | url: TARGET_URL, |
| 69 | prompt: "Extract the page heading as { heading: string }", |
| 70 | }) |
| 71 | .pipe(Effect.orDie); |
| 72 | return yield* HttpServerResponse.json({ |
| 73 | success: extracted.success, |
| 74 | }); |
| 75 | } |
| 76 | // Binary actions stream bytes; drain the stream to count them. |
| 77 | case "/screenshot": { |
| 78 | const bytes = yield* byteLength(browser.screenshot({ url: TARGET_URL })).pipe( |
| 79 | Effect.orDie, |
| 80 | ); |
| 81 | return yield* HttpServerResponse.json({ bytes }); |
| 82 | } |
| 83 | case "/pdf": { |
| 84 | const bytes = yield* byteLength(browser.pdf({ url: TARGET_URL })).pipe(Effect.orDie); |
| 85 | return yield* HttpServerResponse.json({ bytes }); |
| 86 | } |
| 87 | // Generic `quickAction` passthrough. |
| 88 | case "/quickAction": { |
| 89 | const snapshot = yield* browser |
| 90 | .quickAction("snapshot", { url: TARGET_URL }) |
| 91 | .pipe(Effect.orDie); |
| 92 | return yield* HttpServerResponse.json({ |
| 93 | title: snapshot.meta.title, |
| 94 | }); |
| 95 | } |
| 96 | // `raw` exposes the underlying `cf.BrowserRun` binding (puppeteer, etc.). |
| 97 | case "/raw": { |
| 98 | const binding = yield* browser.raw; |
| 99 | const res = yield* Effect.tryPromise(() => |
| 100 | binding.quickAction("content", { url: TARGET_URL }), |
| 101 | ).pipe(Effect.orDie); |
| 102 | const body = yield* Effect.promise( |
| 103 | () => res.json() as Promise<Cloudflare.Workers.BrowserContentResult>, |
| 104 | ); |
| 105 | return yield* HttpServerResponse.json({ title: body.meta.title }); |
| 106 | } |
| 107 | default: |
| 108 | return HttpServerResponse.text("ok"); |
| 109 | } |
| 110 | }), |
| 111 | }; |
| 112 | }).pipe(Effect.provide(Cloudflare.Workers.BrowserBinding)), |
| 113 | ) {} |