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, 04:45 UTC
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import * as DomainFoldkit from "@alchemy.run/domain/Foldkit"; |
| 3 | import { CustomerId } from "@bank/domain/Customer/Customer.id"; |
| 4 | import { CustomerApi } from "@bank/domain/CustomerApi.api"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import * as Layer from "effect/Layer"; |
| 10 | import * as OtlpSerialization from "effect/observability/OtlpSerialization"; |
| 11 | import * as OtlpTracer from "effect/observability/OtlpTracer"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as Schema from "effect/Schema"; |
| 14 | import * as Socket from "effect/socket/Socket"; |
| 15 | import { Runtime } from "foldkit"; |
| 16 | import { Flags, Intent, init, intentsKey, Model, subscriptions, update, view } from "./Bank.ts"; |
| 17 | |
| 18 | /** |
| 19 | * The signed-in customer. A demo stand-in for a login: `?customer=c-2` picks |
| 20 | * who you are, defaulting to `c-1`. |
| 21 | */ |
| 22 | const signedIn = Effect.sync(() => |
| 23 | CustomerId.make(new URLSearchParams(location.search).get("customer") ?? "c-1"), |
| 24 | ); |
| 25 | |
| 26 | /** Intents an earlier page load could not deliver. Anything unreadable is dropped. */ |
| 27 | const savedIntents = (customerId: CustomerId) => |
| 28 | Effect.sync(() => |
| 29 | Option.getOrElse( |
| 30 | Schema.decodeUnknownOption(Schema.fromJsonString(Schema.Array(Intent)))( |
| 31 | localStorage.getItem(intentsKey(customerId)) ?? "[]", |
| 32 | ), |
| 33 | () => [], |
| 34 | ), |
| 35 | ); |
| 36 | |
| 37 | /** Sign in: the Api sets an `HttpOnly` session cookie on this origin. */ |
| 38 | const signIn = Effect.fn(function* (customerId: CustomerId) { |
| 39 | const client = HttpClient.filterStatusOk(yield* HttpClient.HttpClient); |
| 40 | yield* client.execute( |
| 41 | HttpClientRequest.post("/api/session").pipe(HttpClientRequest.bodyJsonUnsafe({ customerId })), |
| 42 | ); |
| 43 | }); |
| 44 | |
| 45 | /** |
| 46 | * The bank Api for the life of the app, on this origin, so the browser sends |
| 47 | * the session cookie with every call and every watch. Commands go over HTTP; |
| 48 | * each watch is a WebSocket to the Durable Object that owns what it reads, |
| 49 | * which pushes every change. |
| 50 | */ |
| 51 | const BankConnection = Layer.unwrap( |
| 52 | Effect.gen(function* () { |
| 53 | // The app cannot run signed out. |
| 54 | yield* signIn(yield* signedIn).pipe(Effect.orDie); |
| 55 | return CustomerApi.layerClient.pipe( |
| 56 | Layer.provide(Domain.protocolWebSocket(`${location.origin}/api`)), |
| 57 | Layer.provide(Socket.layerWebSocketConstructorGlobal), |
| 58 | ); |
| 59 | }), |
| 60 | ); |
| 61 | |
| 62 | /** |
| 63 | * The app's spans, exported to its own origin, where the bank Worker relays |
| 64 | * them on. foldkit traces every Command, and each Api call carries the trace |
| 65 | * on, so a click and everything it causes on the server is one trace. |
| 66 | */ |
| 67 | const Tracing = OtlpTracer.layer({ |
| 68 | url: `${location.origin}/api/telemetry/v1/traces`, |
| 69 | resource: { serviceName: "bank-web" }, |
| 70 | }).pipe(Layer.provide(OtlpSerialization.layerJson)); |
| 71 | |
| 72 | const Headers = Layer.succeed(DomainFoldkit.CommandHeaders, { |
| 73 | get: (command) => Effect.succeed(command === "WithdrawMoney" ? { "x-mfa-code": "000000" } : {}), |
| 74 | }); |
| 75 | |
| 76 | const Resources = Layer.mergeAll(BankConnection, Tracing, Headers).pipe( |
| 77 | Layer.provide(FetchHttpClient.layer), |
| 78 | ); |
| 79 | |
| 80 | Runtime.run( |
| 81 | Runtime.makeApplication({ |
| 82 | Model, |
| 83 | Flags, |
| 84 | init, |
| 85 | update, |
| 86 | view, |
| 87 | subscriptions, |
| 88 | resources: Resources, |
| 89 | container: document.getElementById("root"), |
| 90 | }), |
| 91 | { |
| 92 | flags: Effect.gen(function* () { |
| 93 | const customerId = yield* signedIn; |
| 94 | return { |
| 95 | customerId, |
| 96 | // Names this page's commands, so each is unique even across reloads. |
| 97 | session: crypto.randomUUID(), |
| 98 | online: navigator.onLine, |
| 99 | intents: yield* savedIntents(customerId), |
| 100 | }; |
| 101 | }), |
| 102 | }, |
| 103 | ); |