@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 71 lines 3.02 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as Alchemy from "alchemy";
3 import type { RuntimeContext } from "alchemy/RuntimeContext";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import { ProbeEnv, ProbeEnvBinding } from "./env-binding.ts";
9 import { Storage } from "./storage.ts";
10
11 export class MyContainer extends Cloudflare.Container<
12 MyContainer,
13 {
14 ping: () => Effect.Effect<string>;
15 /** The value a `Binding.Service` injected into the container's env. */
16 boundEnv: () => Effect.Effect<string | undefined, never, RuntimeContext>;
17 /** Read an object's text body from R2 (or `null` when absent). */
18 readObject: (key: string) => Effect.Effect<string | null, never, RuntimeContext>;
19 }
20 >()("EffectfulContainer") {}
21
22 export default MyContainer.make(
23 {
24 main: import.meta.url,
25 image: "oven/bun:latest",
26 },
27 Effect.gen(function* () {
28 // The container reads R2 over a scoped HTTP API token (not the native
29 // Worker binding) — this proves the HTTP R2 client works from inside a
30 // container process. `Alchemy.remote()` pins the bucket (and the minted
31 // token) live even under `alchemy dev`: the HTTP client talks to real
32 // Cloudflare, which a `dev:` local bucket can't serve. This is the
33 // FIRST registration site (the container layer builds before the stack
34 // body), so the DO's later undecorated native-binding site inherits the
35 // pin and both paths converge on the same live bucket.
36 const bucket = yield* Cloudflare.R2.ReadWriteBucket(Storage).pipe(Alchemy.remote());
37
38 // A capability whose only deploy-time contribution is an env var. A
39 // container has no workerd bindings, so this is the channel every
40 // env-shaped capability (Prisma.Connect, …) reaches it through.
41 const boundEnv = yield* ProbeEnv(Storage);
42
43 const read = (key: string) =>
44 bucket.get(key).pipe(
45 Effect.flatMap((object) => (object ? object.text() : Effect.succeed(null))),
46 Effect.orDie,
47 );
48
49 return {
50 ping: () => Effect.succeed("pong"),
51 boundEnv: () => boundEnv,
52 readObject: read,
53 fetch: Effect.gen(function* () {
54 const request = yield* HttpServerRequest;
55 const url = new URL(request.url, "http://container");
56 if (url.pathname === "/bound-env") {
57 return yield* HttpServerResponse.json({ value: yield* boundEnv });
58 }
59 if (url.pathname === "/health") {
60 return yield* HttpServerResponse.json({ ok: true });
61 }
62 if (url.pathname === "/object") {
63 const key = url.searchParams.get("key") ?? "";
64 const value = yield* read(key);
65 return yield* HttpServerResponse.json({ value });
66 }
67 return HttpServerResponse.text("hello from effectful container");
68 }),
69 };
70 }).pipe(Effect.provide(Layer.mergeAll(Cloudflare.R2.ReadWriteBucketHttp, ProbeEnvBinding))),
71 );