@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, 04:45 UTC

main
ts 98 lines 3.46 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import * as Namespace from "alchemy/Namespace";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Stream from "effect/Stream";
6 import { Topic } from "../PubSub/Topic.ts";
7 import { TopicEventSource } from "../PubSub/TopicEventSource.ts";
8 import type { Bucket } from "./Bucket.ts";
9 import {
10 BucketEventSource,
11 toBucketEvent,
12 type BucketEvent,
13 type BucketEventSourceProps,
14 type BucketEventSourceService,
15 } from "./BucketEventSource.ts";
16 import { Notification } from "./Notification.ts";
17
18 /**
19 * Implementation of `GCP.Storage.BucketEventSource` over Pub/Sub.
20 *
21 * Deploy-time: creates a Pub/Sub topic owned by the host
22 * (`{bucket}-BucketEvents`) and a `JSON_API_V1` bucket notification that
23 * publishes the requested event types (and object prefix) to it; the
24 * notification grants the Cloud Storage service agent
25 * `roles/pubsub.publisher` on the topic. Delivery is delegated to the
26 * provided `GCP.PubSub.TopicEventSource` implementation, so provide one
27 * alongside this layer: `GCP.Run.TopicEventSource` (push, HTTP hosts) or
28 * `GCP.Run.TopicPullEventSource` (pull, Jobs and WorkerPools). Runtime:
29 * each message is parsed into a `BucketEvent`.
30 *
31 * ### Choosing the delivery
32 * **Example:** Push to a Cloud Run service
33 * ```typescript
34 * Effect.gen(function* () {
35 * yield* GCP.Storage.consumeBucketEvents(bucket, (events) =>
36 * events.pipe(Stream.runForEach((event) => Effect.log(event.object))),
37 * );
38 * }).pipe(
39 * Effect.provide(GCP.Storage.BucketEventSourceLive),
40 * Effect.provide(GCP.Run.TopicEventSource),
41 * );
42 * ```
43 *
44 * **Example:** Pull on a worker pool
45 * ```typescript
46 * Effect.gen(function* () {
47 * yield* GCP.Storage.consumeBucketEvents(bucket, (events) =>
48 * events.pipe(Stream.runForEach((event) => Effect.log(event.object))),
49 * );
50 * return { run: Effect.never };
51 * }).pipe(
52 * Effect.provide(GCP.Storage.BucketEventSourceLive),
53 * Effect.provide(GCP.Run.TopicPullEventSource),
54 * );
55 * ```
56 *
57 * @layer
58 * @provides GCP.Storage.BucketEventSource
59 * @category Storage
60 */
61 export const BucketEventSourceLive = Layer.effect(
62 BucketEventSource,
63 Effect.gen(function* () {
64 const topics = yield* Topic;
65 const notifications = yield* Notification;
66 const source = yield* TopicEventSource;
67
68 return Effect.fn(function* <Req = never>(
69 bucket: Bucket,
70 props: BucketEventSourceProps,
71 handler: (events: Stream.Stream<BucketEvent>) => Effect.Effect<void, never, Req>,
72 ) {
73 const host = yield* Binding.Host;
74 const hostId = host?.LogicalId ?? "Host";
75 // Declared in both phases: the delivery implementation declares its
76 // subscription against this topic at runtime too.
77 const topic = yield* Namespace.push(hostId, topics(`${bucket.LogicalId}-BucketEvents`, {}));
78
79 if (!globalThis.__ALCHEMY_RUNTIME__) {
80 yield* Namespace.push(
81 hostId,
82 notifications(`${bucket.LogicalId}-Notification`, {
83 bucketName: bucket.bucketName,
84 topic: topic.name,
85 payloadFormat: "JSON_API_V1",
86 eventTypes: props.eventTypes ?? ["OBJECT_FINALIZE"],
87 objectNamePrefix: props.prefix,
88 }),
89 );
90 }
91
92 const { eventTypes: _eventTypes, prefix: _prefix, ...delivery } = props;
93 yield* source(topic, delivery, (messages) =>
94 handler(messages.pipe(Stream.mapEffect(toBucketEvent))),
95 );
96 }) as BucketEventSourceService;
97 }),
98 );