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 AWS from "@alchemy.run/aws"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as HttpClient from "effect/http/HttpClient"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | |
| 8 | /** |
| 9 | * A service whose Layer performs real async I/O during the sandbox's init |
| 10 | * (cold start) — the "fetch a value once and cache it for every |
| 11 | * invocation" pattern. |
| 12 | */ |
| 13 | class TraceConfig extends Context.Service<TraceConfig, { trace: string; nonce: string }>()( |
| 14 | "InitIO.TraceConfig", |
| 15 | ) {} |
| 16 | |
| 17 | const TraceConfigLive = Layer.effect( |
| 18 | TraceConfig, |
| 19 | Effect.gen(function* () { |
| 20 | // Counted per sandbox so every response can assert the layer's I/O ran |
| 21 | // exactly once no matter how many invocations the sandbox has served. |
| 22 | yield* Effect.sync(() => { |
| 23 | (globalThis as any).__initFetches = ((globalThis as any).__initFetches ?? 0) + 1; |
| 24 | }); |
| 25 | const client = yield* HttpClient.HttpClient; |
| 26 | const response = yield* client.get("https://www.cloudflare.com/cdn-cgi/trace"); |
| 27 | const trace = yield* response.text; |
| 28 | return { trace, nonce: crypto.randomUUID() }; |
| 29 | }).pipe( |
| 30 | // A failed init fetch is a defect: the sandbox is useless without its |
| 31 | // config, and init's typed error channel is reserved for ConfigError. |
| 32 | Effect.orDie, |
| 33 | ), |
| 34 | ); |
| 35 | |
| 36 | /** |
| 37 | * Pins that one-shot async I/O during init / layer build works on Lambda: |
| 38 | * the layer's fetch runs exactly once per sandbox (at cold start, under the |
| 39 | * instance scope) and the cached plain value serves every invocation. |
| 40 | */ |
| 41 | export default class InitIOProbe extends AWS.Lambda.Function<InitIOProbe>()( |
| 42 | "InitIOProbe", |
| 43 | { |
| 44 | main: import.meta.url, |
| 45 | functionUrl: true, |
| 46 | }, |
| 47 | Effect.gen(function* () { |
| 48 | const config = yield* TraceConfig; |
| 49 | |
| 50 | return { |
| 51 | fetch: Effect.gen(function* () { |
| 52 | return yield* HttpServerResponse.json({ |
| 53 | nonce: config.nonce, |
| 54 | traceLength: config.trace.length, |
| 55 | hasUag: config.trace.includes("uag="), |
| 56 | initFetches: (globalThis as any).__initFetches ?? 0, |
| 57 | }); |
| 58 | }), |
| 59 | }; |
| 60 | }).pipe(Effect.provide(TraceConfigLive)), |
| 61 | ) {} |