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, 07:15 UTC
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | |
| 6 | export const INCOMING_PREFIX = "incoming/"; |
| 7 | |
| 8 | /** Marker object the consumer writes for one delivered event. */ |
| 9 | export const markerFor = (object: string) => `markers/${object.replaceAll("/", "_")}.json`; |
| 10 | |
| 11 | /** Bucket whose `incoming/` uploads trigger the consumer. */ |
| 12 | export const Uploads = GCP.Storage.Bucket("Uploads", { |
| 13 | location: "US-CENTRAL1", |
| 14 | forceDestroy: true, |
| 15 | }); |
| 16 | |
| 17 | /** |
| 18 | * Effect-native Cloud Run service that consumes `incoming/` finalize events |
| 19 | * over Pub/Sub push and records each one as a marker object in the same |
| 20 | * bucket (outside the notification prefix, so markers don't re-trigger). |
| 21 | * Deployed from {@link ../BucketEventSource.test.ts}. |
| 22 | */ |
| 23 | export default class BucketEventsService extends GCP.Function<BucketEventsService>()( |
| 24 | "BucketEventsService", |
| 25 | { |
| 26 | main: import.meta.url, |
| 27 | location: "us-central1", |
| 28 | }, |
| 29 | Effect.gen(function* () { |
| 30 | const bucket = yield* Uploads; |
| 31 | const putObject = yield* GCP.Storage.PutObject(bucket); |
| 32 | |
| 33 | yield* GCP.Storage.consumeBucketEvents(bucket, { prefix: INCOMING_PREFIX }, (events) => |
| 34 | events.pipe( |
| 35 | Stream.runForEach((event) => |
| 36 | putObject({ |
| 37 | name: markerFor(event.object), |
| 38 | body: JSON.stringify({ |
| 39 | eventType: event.eventType, |
| 40 | bucket: event.bucket, |
| 41 | object: event.object, |
| 42 | generation: event.generation, |
| 43 | eventTime: event.eventTime, |
| 44 | size: event.metadata.size, |
| 45 | contentType: event.metadata.contentType, |
| 46 | }), |
| 47 | }).pipe(Effect.orDie), |
| 48 | ), |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | return { |
| 53 | fetch: Effect.succeed(HttpServerResponse.text("ok")), |
| 54 | }; |
| 55 | }).pipe( |
| 56 | Effect.provide(GCP.Storage.BucketEventSourceLive), |
| 57 | Effect.provide(GCP.Run.TopicEventSource), |
| 58 | Effect.provide(GCP.Storage.PutObjectHttp), |
| 59 | ), |
| 60 | ) {} |