@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 121 lines 4.38 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import * as Namespace from "alchemy/Namespace";
3 import { ServerHost } from "alchemy/Runtime/Process";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Stream from "effect/Stream";
7 import { Acknowledge } from "../PubSub/Acknowledge.ts";
8 import { AcknowledgeHttp } from "../PubSub/AcknowledgeHttp.ts";
9 import { Pull } from "../PubSub/Pull.ts";
10 import { PullHttp } from "../PubSub/PullHttp.ts";
11 import { Subscription } from "../PubSub/Subscription.ts";
12 import type { Topic } from "../PubSub/Topic.ts";
13 import {
14 TopicEventSource as PubSubTopicEventSource,
15 type TopicEventSourceProps,
16 type TopicEventSourceService,
17 type TopicMessage,
18 } from "../PubSub/TopicEventSource.ts";
19
20 /**
21 * Subscription `deadLetterPolicy` and `retryPolicy` for the event source's
22 * props. Dead-lettering defaults to a backoff so its attempts span more
23 * than a transient failure.
24 */
25 const deliveryPolicyOf = (props: TopicEventSourceProps) => ({
26 deadLetterPolicy:
27 props.deadLetter === undefined
28 ? undefined
29 : {
30 deadLetterTopic: props.deadLetter.topic.name,
31 maxDeliveryAttempts: props.deadLetter.maxDeliveryAttempts ?? 5,
32 },
33 retryPolicy:
34 props.retryPolicy ??
35 (props.deadLetter === undefined
36 ? undefined
37 : { minimumBackoff: "10s", maximumBackoff: "600s" }),
38 });
39
40 /**
41 * Pull implementation of `GCP.PubSub.TopicEventSource` for hosts without
42 * an inbound URL (`GCP.Run.Job`, `GCP.Run.WorkerPool`; any host with
43 * `ServerHost`).
44 *
45 * Deploy-time: creates a pull subscription owned by the host and grants
46 * the host's runtime service account `roles/pubsub.subscriber` on it
47 * (through the `Pull` / `Acknowledge` bindings). Runtime: a background
48 * loop pulls batches, runs the handler, and acks the batch once the
49 * handler succeeds. A failed handler leaves the batch unacked, so Pub/Sub
50 * redelivers after the ack deadline.
51 *
52 * @layer
53 * @provides GCP.PubSub.TopicEventSource
54 * @category Run
55 */
56 export const TopicPullEventSource = Layer.effect(
57 PubSubTopicEventSource,
58 Effect.gen(function* () {
59 const { run } = yield* ServerHost;
60 const subscription = yield* Subscription;
61 const pull = yield* Pull;
62 const acknowledge = yield* Acknowledge;
63
64 return Effect.fn(function* <Req = never>(
65 topic: Topic,
66 props: TopicEventSourceProps,
67 process: (messages: Stream.Stream<TopicMessage>) => Effect.Effect<void, never, Req>,
68 ) {
69 const host = yield* Binding.Host;
70 const hostId = host?.LogicalId ?? "Host";
71 // Declared in both phases: at runtime the declaration resolves to the
72 // deployed subscription's outputs, which the bindings below read.
73 const sub = yield* Namespace.push(
74 hostId,
75 subscription(`${topic.LogicalId}-Pull`, {
76 topic: topic.name,
77 ackDeadlineSeconds: props.ackDeadlineSeconds ?? 60,
78 filter: props.filter,
79 ...deliveryPolicyOf(props),
80 }),
81 );
82 const pullBatch = yield* pull(sub);
83 const ackBatch = yield* acknowledge(sub);
84 const subscriptionName = yield* sub.name;
85
86 yield* run(
87 Effect.gen(function* () {
88 const name = yield* subscriptionName;
89 const { receivedMessages = [] } = yield* pullBatch({
90 body: { maxMessages: props.maxMessages ?? 10 },
91 });
92 const received = receivedMessages.filter(
93 (item) => item.ackId !== undefined && item.message !== undefined,
94 );
95 if (received.length === 0) return;
96 yield* process(
97 Stream.fromIterable(
98 received.map((item) => ({
99 message: item.message!,
100 subscription: name,
101 deliveryAttempt: item.deliveryAttempt,
102 })),
103 ),
104 );
105 yield* ackBatch({
106 body: { ackIds: received.map((item) => item.ackId!) },
107 });
108 }).pipe(
109 // A failed pull, ack, or handler must not end the consumer; the
110 // unacked batch is redelivered after its ack deadline.
111 Effect.catchCause((cause) =>
112 Effect.logWarning("Pub/Sub pull iteration failed", cause).pipe(
113 Effect.andThen(Effect.sleep("1 second")),
114 ),
115 ),
116 Effect.forever,
117 ),
118 );
119 }) as TopicEventSourceService;
120 }),
121 ).pipe(Layer.provide(Layer.mergeAll(PullHttp, AcknowledgeHttp)));