The best way to control Claude Code, Codex and more from any computer or phone

main
ts 418 lines 13.8 KB
Raw
1 import * as observability from "@distilled.cloud/gcp/observability_v1";
2 import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import { createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
11 import type { Providers } from "../Providers.ts";
12 import {
13 encodeDescription,
14 hasOwnershipMarker,
15 linkResourceName,
16 listLinksAt,
17 listProjectDatasets,
18 parseDescription,
19 parseLinkName,
20 resolveDatasetParent,
21 toLinkId,
22 } from "./internal.ts";
23 import { waitForOperation } from "./operations.ts";
24
25 export type BucketsDatasetsLinkProps = {
26 /**
27 * Parent observability dataset. Full name
28 * `projects/{project}/locations/{location}/buckets/{bucket}/datasets/{dataset}`
29 * or the dataset id (combined with `bucket` and `location`). A dataset
30 * may have at most one link. Immutable — changing it replaces the
31 * link (delete-first).
32 */
33 dataset: string;
34 /**
35 * Parent bucket used when `dataset` is a bare id. Full name or the
36 * bucket id. Defaults to `_Trace`. Immutable — changing it replaces
37 * the link.
38 */
39 bucket?: string;
40 /**
41 * Bucket location used when `dataset` is a bare id. Immutable —
42 * changing it replaces the link.
43 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
44 */
45 location?: string;
46 /**
47 * Link id (the `{link}` segment of
48 * `.../datasets/{dataset}/links/{link}`). Also used as the linked
49 * BigQuery dataset id, so it may contain only letters, digits, and
50 * underscores and must start with a letter. If omitted, a unique name
51 * is generated. Immutable — changing it replaces the link.
52 */
53 linkId?: string;
54 /**
55 * User-friendly display name.
56 */
57 displayName?: string;
58 /**
59 * Human-readable description (max 8000 characters). Links have no
60 * labels field, so Alchemy stamps ownership into the description for
61 * `list` / nuke.
62 */
63 description?: string;
64 };
65
66 export type BucketsDatasetsLink = Resource<
67 "GCP.Observability.BucketsDatasetsLink",
68 BucketsDatasetsLinkProps,
69 {
70 /** Full resource name `projects/{project}/locations/{location}/buckets/{bucket}/datasets/{dataset}/links/{linkId}`. */
71 name: string;
72 /** Link id (last path segment). */
73 linkId: string;
74 /** Parent dataset resource name. */
75 dataset: string;
76 /** Dataset id. */
77 datasetId: string;
78 /** Parent bucket resource name. */
79 bucket: string;
80 /** Bucket id. */
81 bucketId: string;
82 /** Project id. */
83 project: string;
84 /** Location of the parent bucket. */
85 location: string;
86 /** Display name. */
87 displayName: string | undefined;
88 /** User description with the Alchemy ownership prefix stripped. */
89 description: string | undefined;
90 /** RFC3339 creation timestamp. */
91 createTime: string | undefined;
92 },
93 never,
94 Providers
95 >;
96
97 /**
98 * A Cloud Observability dataset link that creates a linked BigQuery
99 * dataset over an observability analytics dataset (typically
100 * `_Trace/Spans`).
101 *
102 * A dataset may have only one link. Create, update, and delete are
103 * long-running operations. Links have no labels field — Alchemy stamps
104 * ownership into the description. `linkId` and the parent dataset are
105 * identity; replacement is delete-first because a dataset cannot hold
106 * two links.
107 *
108 * ### Creating a Link
109 * **Example:** Link the default trace dataset to BigQuery
110 * ```typescript
111 * const link = yield* GCP.Observability.BucketsDatasetsLink("Analytics", {
112 * dataset:
113 * "projects/my-project/locations/us-central1/buckets/_Trace/datasets/Spans",
114 * description: "bigquery analytics",
115 * });
116 * ```
117 *
118 * **Example:** Named link from bucket and dataset ids
119 * ```typescript
120 * const link = yield* GCP.Observability.BucketsDatasetsLink("Analytics", {
121 * bucket: "_Trace",
122 * dataset: "Spans",
123 * linkId: "trace_spans",
124 * displayName: "Trace spans",
125 * });
126 * ```
127 *
128 * ### Updating a Link
129 * Change props on the same logical id; the engine keeps the physical id.
130 *
131 * **Example:** Change the description
132 * ```typescript
133 * const link = yield* GCP.Observability.BucketsDatasetsLink("Analytics", {
134 * dataset:
135 * "projects/my-project/locations/us-central1/buckets/_Trace/datasets/Spans",
136 * description: "updated analytics",
137 * });
138 * ```
139 *
140 * @resource
141 * @category Observability
142 */
143 export const BucketsDatasetsLink = Resource<BucketsDatasetsLink>(
144 "GCP.Observability.BucketsDatasetsLink",
145 );
146
147 export class BucketsDatasetsLinkNotResolved extends Data.TaggedError(
148 "GCP.Observability.BucketsDatasetsLinkNotResolved",
149 )<{
150 resourceName: string;
151 }> {}
152
153 export class BucketsDatasetsLinkStillExists extends Data.TaggedError(
154 "GCP.Observability.BucketsDatasetsLinkStillExists",
155 )<{
156 resourceName: string;
157 }> {}
158
159 const isValidLinkName = (name: string) => parseLinkName(name) !== undefined;
160
161 const toAttrs = (
162 link: observability.Link,
163 project: string,
164 location: string,
165 bucketId: string,
166 datasetId: string,
167 ) => {
168 const parsedName = parseLinkName(link.name ?? "");
169 const parsed = parseDescription(link.description);
170 const linkId = parsedName?.linkId ?? (link.name ?? "").split("/").pop() ?? "";
171 const resolvedProject = parsedName?.project ?? project;
172 const resolvedLocation = parsedName?.location ?? location;
173 const resolvedBucketId = parsedName?.bucketId ?? bucketId;
174 const resolvedDatasetId = parsedName?.datasetId ?? datasetId;
175 return {
176 name:
177 link.name ??
178 (linkId
179 ? linkResourceName(
180 resolvedProject,
181 resolvedLocation,
182 resolvedBucketId,
183 resolvedDatasetId,
184 linkId,
185 )
186 : ""),
187 linkId,
188 dataset: `projects/${resolvedProject}/locations/${resolvedLocation}/buckets/${resolvedBucketId}/datasets/${resolvedDatasetId}`,
189 datasetId: resolvedDatasetId,
190 bucket: `projects/${resolvedProject}/locations/${resolvedLocation}/buckets/${resolvedBucketId}`,
191 bucketId: resolvedBucketId,
192 project: resolvedProject,
193 location: resolvedLocation,
194 displayName: link.displayName,
195 description: parsed.description,
196 createTime: link.createTime,
197 };
198 };
199
200 const getByName = (name: string) =>
201 !isValidLinkName(name)
202 ? Effect.undefined
203 : observability
204 .getProjectsLocationsBucketsDatasetsLinks({ name })
205 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
206
207 const waitUntilPresent = (name: string) =>
208 getByName(name).pipe(
209 Effect.flatMap((link) =>
210 link
211 ? Effect.succeed(link)
212 : Effect.fail(new BucketsDatasetsLinkNotResolved({ resourceName: name })),
213 ),
214 Effect.retry({
215 while: (error) => error._tag === "GCP.Observability.BucketsDatasetsLinkNotResolved",
216 times: 10,
217 schedule: Schedule.spaced("3 seconds"),
218 }),
219 );
220
221 const waitUntilDeleted = (name: string) =>
222 getByName(name).pipe(
223 Effect.flatMap((link) =>
224 link === undefined
225 ? Effect.void
226 : Effect.fail(new BucketsDatasetsLinkStillExists({ resourceName: name })),
227 ),
228 Effect.retry({
229 while: (error) => error._tag === "GCP.Observability.BucketsDatasetsLinkStillExists",
230 times: 10,
231 schedule: Schedule.spaced("3 seconds"),
232 }),
233 Effect.catchTag("GCP.Observability.BucketsDatasetsLinkStillExists", () => Effect.void),
234 );
235
236 export const BucketsDatasetsLinkProvider = () =>
237 Provider.succeed(BucketsDatasetsLink, {
238 stables: [
239 "name",
240 "linkId",
241 "dataset",
242 "datasetId",
243 "bucket",
244 "bucketId",
245 "project",
246 "location",
247 "createTime",
248 ],
249
250 diff: Effect.fn(function* ({ news, olds, output }) {
251 if (!isResolved(news)) return undefined;
252 const previousId = olds?.linkId ?? output?.linkId;
253 const idChanged =
254 previousId !== undefined && news.linkId !== undefined && news.linkId !== previousId;
255 const previousDataset = olds?.dataset ?? output?.dataset;
256 const datasetChanged =
257 previousDataset !== undefined &&
258 news.dataset.includes("/datasets/") &&
259 news.dataset !== previousDataset;
260 const previousBucket = olds?.bucket ?? output?.bucket;
261 const bucketChanged =
262 previousBucket !== undefined &&
263 news.bucket !== undefined &&
264 news.bucket.includes("/buckets/") &&
265 news.bucket !== previousBucket;
266 if (!idChanged && !datasetChanged && !bucketChanged) return undefined;
267 return { action: "replace" as const, deleteFirst: true };
268 }),
269
270 read: Effect.fn(function* ({ id, olds, output, previous }) {
271 const env = yield* GcpEnvironment.current;
272 const location = olds?.location ?? output?.location ?? env.region;
273 const parent = resolveDatasetParent(
274 olds?.dataset ?? output?.dataset ?? "",
275 olds?.bucket ?? output?.bucket,
276 location,
277 env.project,
278 );
279 const linkId = yield* toLinkId(id, olds?.linkId, output?.linkId);
280 const name =
281 output?.name ??
282 linkResourceName(
283 parent.project,
284 parent.location,
285 parent.bucketId,
286 parent.datasetId,
287 linkId,
288 );
289 const existing = yield* getByName(name);
290 if (existing === undefined) return undefined;
291 const attrs = toAttrs(
292 existing,
293 parent.project,
294 parent.location,
295 parent.bucketId,
296 parent.datasetId,
297 );
298 const { labels } = parseDescription(existing.description);
299 return (yield* hasAlchemyLabels(id, labels))
300 ? attrs
301 : unownedUnlessPrevious(attrs, previous, "name");
302 }),
303
304 list: () =>
305 Effect.gen(function* () {
306 const env = yield* GcpEnvironment.current;
307 const datasets = yield* listProjectDatasets();
308 const pages = yield* Effect.forEach(
309 datasets,
310 (dataset) =>
311 listLinksAt(dataset.name!).pipe(
312 Effect.map((links) =>
313 links
314 .filter((link) => hasOwnershipMarker(link.description))
315 .map((link) => {
316 const parsed = parseLinkName(link.name ?? "");
317 return toAttrs(
318 link,
319 parsed?.project ?? env.project,
320 parsed?.location ?? env.region,
321 parsed?.bucketId ?? "",
322 parsed?.datasetId ?? "",
323 );
324 }),
325 ),
326 ),
327 { concurrency: 4 },
328 );
329 return pages.flat();
330 }),
331
332 reconcile: Effect.fn(function* ({ id, news, output }) {
333 const env = yield* GcpEnvironment.current;
334 const location = news.location ?? output?.location ?? env.region;
335 const parent = resolveDatasetParent(news.dataset, news.bucket, location, env.project);
336 const linkId = yield* toLinkId(id, news.linkId, output?.linkId);
337 const name = linkResourceName(
338 parent.project,
339 parent.location,
340 parent.bucketId,
341 parent.datasetId,
342 linkId,
343 );
344 const ownership = yield* createInternalLabels(id);
345 const desiredDescription = encodeDescription(ownership, news.description);
346
347 let current = yield* getByName(output?.name ?? name);
348
349 if (current === undefined) {
350 const created = yield* observability
351 .createProjectsLocationsBucketsDatasetsLinks({
352 parent: parent.name,
353 linkId,
354 body: {
355 displayName: news.displayName,
356 description: desiredDescription,
357 },
358 })
359 .pipe(
360 Effect.catchTag("Conflict", () =>
361 Effect.succeed<observability.Operation>({ done: true }),
362 ),
363 );
364 yield* waitForOperation(created).pipe(Effect.catchTag("NotFound", () => Effect.void));
365 current = yield* waitUntilPresent(name);
366 }
367
368 if (current === undefined) {
369 return yield* new BucketsDatasetsLinkNotResolved({ resourceName: name });
370 }
371
372 const descriptionChanged = (current.description ?? "") !== desiredDescription;
373 const displayNameChanged = (current.displayName ?? "") !== (news.displayName ?? "");
374 const updateMask = [
375 descriptionChanged ? "description" : undefined,
376 displayNameChanged ? "displayName" : undefined,
377 ].filter((field): field is string => field !== undefined);
378
379 if (updateMask.length > 0) {
380 const patched = yield* observability.patchProjectsLocationsBucketsDatasetsLinks({
381 name: current.name ?? name,
382 updateMask: updateMask.join(","),
383 body: {
384 displayName: news.displayName,
385 description: desiredDescription,
386 },
387 });
388 yield* waitForOperation(patched).pipe(Effect.catchTag("NotFound", () => Effect.void));
389 current = (yield* getByName(current.name ?? name)) ?? current;
390 }
391
392 return toAttrs(current, parent.project, parent.location, parent.bucketId, parent.datasetId);
393 }),
394
395 delete: Effect.fn(function* ({ output }) {
396 const names = isValidLinkName(output.name)
397 ? [output.name]
398 : output.linkId
399 ? yield* listProjectDatasets().pipe(
400 Effect.map((datasets) =>
401 datasets.flatMap((dataset) =>
402 dataset.name ? [`${dataset.name}/links/${output.linkId}`] : [],
403 ),
404 ),
405 )
406 : [];
407 yield* Effect.forEach(
408 names,
409 (name) =>
410 observability.deleteProjectsLocationsBucketsDatasetsLinks({ name }).pipe(
411 Effect.flatMap((operation) => waitForOperation(operation, { notFoundOk: true })),
412 Effect.catchTag(["NotFound", "BadRequest"], () => Effect.void),
413 Effect.flatMap(() => waitUntilDeleted(name)),
414 ),
415 { concurrency: 1 },
416 );
417 }),
418 });