@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 307 lines 9.91 KB
Raw
1 import * as pubsub from "@distilled.cloud/gcp/pubsub_v1";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { tagRecord } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as Stream from "effect/Stream";
12 import { GcpEnvironment } from "../Environment.ts";
13 import {
14 createInternalLabels,
15 diffLabels,
16 hasAlchemyLabels,
17 stripInternalLabels,
18 toLabels,
19 } from "../Labels.ts";
20 import type { Providers } from "../Providers.ts";
21
22 export type TopicProps = {
23 /**
24 * Topic id (the `{topic}` segment of `projects/{project}/topics/{topic}`).
25 * If omitted, a unique name is generated from the stack, stage, and
26 * logical id.
27 */
28 topicId?: string;
29 /**
30 * User labels. Alchemy ownership labels are merged in automatically.
31 */
32 labels?: Record<string, string>;
33 /**
34 * Cloud KMS key used to encrypt messages, as
35 * `projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`.
36 */
37 kmsKeyName?: string;
38 /**
39 * Minimum duration to retain published messages (e.g. `"86400s"`).
40 */
41 messageRetentionDuration?: string;
42 };
43
44 export type Topic = Resource<
45 "GCP.PubSub.Topic",
46 TopicProps,
47 {
48 /** Full resource name `projects/{project}/topics/{topic}`. */
49 name: string;
50 /** Topic id (last path segment). */
51 topicId: string;
52 /** Project id. */
53 project: string;
54 /** User labels (Alchemy ownership labels stripped). */
55 labels: Record<string, string>;
56 /** KMS key used for encryption, if any. */
57 kmsKeyName: string | undefined;
58 /** Message retention duration, if set. */
59 messageRetentionDuration: string | undefined;
60 /** Server-reported topic state. */
61 state: string | undefined;
62 },
63 never,
64 Providers
65 >;
66
67 /**
68 * A Google Cloud Pub/Sub topic.
69 *
70 * ### Creating a Topic
71 * **Example:** Generated name
72 * ```typescript
73 * const topic = yield* GCP.PubSub.Topic("events", {});
74 * ```
75 *
76 * **Example:** Explicit id and labels
77 * ```typescript
78 * const topic = yield* GCP.PubSub.Topic("events", {
79 * topicId: "order-events",
80 * labels: { env: "prod" },
81 * });
82 * ```
83 *
84 * ### Updating a Topic
85 * **Example:** Change labels
86 * ```typescript
87 * const topic = yield* GCP.PubSub.Topic("events", {
88 * labels: { env: "prod" },
89 * });
90 * ```
91 *
92 * ### Binding from a Function
93 * **Example:** Publish from Cloud Run
94 * ```typescript
95 * export class Api extends GCP.Function<Api>()(
96 * "Api",
97 * { main: import.meta.url },
98 * Effect.gen(function* () {
99 * const topic = yield* GCP.PubSub.Topic("events", {});
100 * const publish = yield* GCP.PubSub.Publish(topic);
101 * return {
102 * fetch: Effect.gen(function* () {
103 * yield* publish({
104 * body: { messages: [{ data: btoa("hello") }] },
105 * }).pipe(Effect.orDie);
106 * return HttpServerResponse.text("ok");
107 * }),
108 * };
109 * }).pipe(Effect.provide([GCP.PubSub.PublishHttp])),
110 * ) {}
111 * ```
112 *
113 * ### Destroying a Topic
114 * **Example:** `alchemy destroy` deletes the topic after its subscriptions.
115 *
116 * @resource
117 * @category PubSub
118 */
119 export const Topic = Resource<Topic>("GCP.PubSub.Topic");
120
121 export class TopicStillExists extends Data.TaggedError("GCP.PubSub.TopicStillExists")<{
122 resourceName: string;
123 }> {}
124
125 export class TopicNotResolved extends Data.TaggedError("GCP.PubSub.TopicNotResolved")<{
126 resourceName: string;
127 }> {}
128
129 const topicIdOf = (name: string) => name.split("/").pop() ?? name;
130
131 const resourceName = (project: string, topicId: string) => `projects/${project}/topics/${topicId}`;
132
133 const userLabels = (
134 labels: Record<string, string | undefined> | null | undefined,
135 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
136
137 const toId = (id: string, topicId: string | undefined, existing?: string) =>
138 Effect.gen(function* () {
139 return (
140 topicId ?? existing ?? (yield* createPhysicalName({ id, maxLength: 255, lowercase: true }))
141 );
142 });
143
144 const toAttrs = (topic: pubsub.Topic, project: string) => {
145 const name = topic.name ?? "";
146 return {
147 name,
148 topicId: topicIdOf(name),
149 project,
150 labels: userLabels(topic.labels),
151 kmsKeyName: topic.kmsKeyName,
152 messageRetentionDuration: topic.messageRetentionDuration,
153 state: topic.state,
154 };
155 };
156
157 const getByName = (name: string) =>
158 pubsub
159 .getProjectsTopics({ topic: name })
160 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
161
162 // Pub/Sub serves reads from replicas that lag writes: one successful GET
163 // does not mean the next one (or a publish routed to it) sees the
164 // resource. Require consecutive successful reads before reporting ready.
165 const CONSISTENT_READS = 3;
166
167 const waitUntilPresent = (name: string) =>
168 Effect.gen(function* () {
169 let found: pubsub.Topic | undefined;
170 for (let read = 0; read < CONSISTENT_READS; read++) {
171 if (read > 0) yield* Effect.sleep("1 second");
172 found = yield* getByName(name);
173 if (found === undefined) {
174 return yield* new TopicNotResolved({ resourceName: name });
175 }
176 }
177 return found!;
178 }).pipe(
179 Effect.retry({
180 while: (error) => error._tag === "GCP.PubSub.TopicNotResolved",
181 schedule: Schedule.spaced("1 second"),
182 times: 60,
183 }),
184 );
185
186 export const TopicProvider = () =>
187 Provider.succeed(Topic, {
188 stables: ["name", "topicId", "project"],
189
190 // topicId is the physical identity: a new id is a new topic, and the
191 // old one must be deleted (not left behind by an in-place "update").
192 diff: Effect.fn(function* ({ id, news, olds, output }) {
193 if (!isResolved(news)) return undefined;
194 const previous = output?.topicId ?? olds?.topicId;
195 if (previous === undefined) return undefined;
196 const next = yield* toId(id, news.topicId, output?.topicId);
197 return next !== previous ? { action: "replace" as const, deleteFirst: false } : undefined;
198 }),
199
200 read: Effect.fn(function* ({ id, olds, output }) {
201 const env = yield* GcpEnvironment.current;
202 const topicId = yield* toId(id, olds?.topicId, output?.topicId);
203 const name = output?.name ?? resourceName(env.project, topicId);
204 const existing = yield* getByName(name);
205 if (existing === undefined) return undefined;
206 const attrs = toAttrs(existing, env.project);
207 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
208 }),
209
210 list: () =>
211 Effect.gen(function* () {
212 const env = yield* GcpEnvironment.current;
213 return yield* pubsub.listProjectsTopics
214 .pages({
215 project: `projects/${env.project}`,
216 pageSize: 1000,
217 })
218 .pipe(
219 Stream.flatMap((page) => Stream.fromIterable(page.topics ?? [])),
220 Stream.filter((topic) =>
221 Object.keys(topic.labels ?? {}).some((key) => key.startsWith("alchemy-")),
222 ),
223 Stream.map((topic) => toAttrs(topic, env.project)),
224 Stream.runCollect,
225 Effect.map((chunk) => Array.from(chunk)),
226 );
227 }),
228
229 reconcile: Effect.fn(function* ({ id, news, output }) {
230 const env = yield* GcpEnvironment.current;
231 const topicId = yield* toId(id, news.topicId, output?.topicId);
232 const name = resourceName(env.project, topicId);
233 const desiredLabels = {
234 ...toLabels(news.labels),
235 ...(yield* createInternalLabels(id)),
236 };
237
238 let current = yield* getByName(name);
239
240 if (current === undefined) {
241 yield* pubsub
242 .createProjectsTopics({
243 name,
244 body: {
245 labels: desiredLabels,
246 kmsKeyName: news.kmsKeyName,
247 messageRetentionDuration: news.messageRetentionDuration,
248 },
249 })
250 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
251 // Block until Pub/Sub serves the topic, not just the create response.
252 current = yield* waitUntilPresent(name);
253 }
254
255 if (current === undefined) {
256 return yield* new TopicNotResolved({ resourceName: name });
257 }
258
259 const observedLabels = tagRecord(current.labels);
260 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
261 const labelsChanged = upsert.length > 0 || removed.length > 0;
262 const kmsChanged = (current.kmsKeyName ?? "") !== (news.kmsKeyName ?? "");
263 const retentionChanged =
264 (current.messageRetentionDuration ?? "") !== (news.messageRetentionDuration ?? "");
265
266 if (labelsChanged || kmsChanged || retentionChanged) {
267 current = yield* pubsub.patchProjectsTopics({
268 name,
269 body: {
270 topic: {
271 name,
272 labels: desiredLabels,
273 kmsKeyName: news.kmsKeyName,
274 messageRetentionDuration: news.messageRetentionDuration,
275 },
276 updateMask: [
277 labelsChanged ? "labels" : undefined,
278 kmsChanged ? "kmsKeyName" : undefined,
279 retentionChanged ? "messageRetentionDuration" : undefined,
280 ]
281 .filter((field): field is string => field !== undefined)
282 .join(","),
283 },
284 });
285 }
286
287 return toAttrs(current, env.project);
288 }),
289
290 delete: Effect.fn(function* ({ output }) {
291 yield* pubsub
292 .deleteProjectsTopics({ topic: output.name })
293 .pipe(Effect.catchTag("NotFound", () => Effect.void));
294 // Pub/Sub reads lag deletes; block until it stops serving it.
295 yield* getByName(output.name).pipe(
296 Effect.filterOrFail(
297 (existing) => existing === undefined,
298 () => new TopicStillExists({ resourceName: output.name }),
299 ),
300 Effect.retry({
301 while: (error) => error._tag === "GCP.PubSub.TopicStillExists",
302 schedule: Schedule.spaced("2 seconds"),
303 times: 30,
304 }),
305 );
306 }),
307 });