@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 521 lines 18.7 KB
Raw
1 import * as workflows from "@distilled.cloud/gcp/workflows_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 { waitForOperation as waitForGcpOperation } from "../Operation.ts";
21 import type { Providers } from "../Providers.ts";
22
23 const MAX_NAME_LENGTH = 64;
24
25 export type CallLogLevel = workflows.WorkflowCallLogLevelEnum | (string & {});
26 export type ExecutionHistoryLevel = workflows.WorkflowExecutionHistoryLevelEnum | (string & {});
27
28 export type WorkflowProps = {
29 /**
30 * Workflow id (the `{workflow}` segment of
31 * `projects/{project}/locations/{location}/workflows/{workflow}`). If
32 * omitted, a unique name is generated from the stack, stage, and logical
33 * id. Must match `[A-Za-z][A-Za-z0-9_-]{0,62}[A-Za-z0-9]` (1–64
34 * characters, start with a letter, end with a letter or number).
35 * Immutable — changing it replaces the workflow.
36 */
37 workflowId?: string;
38 /**
39 * Workflows location (`us-central1`, `us-east1`, …). Immutable —
40 * changing it replaces the workflow. `US-CENTRAL1` is accepted and
41 * normalized to `us-central1`.
42 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
43 */
44 location?: string;
45 /**
46 * Workflow YAML/JSON source. Max 128KB. Updating this creates a new
47 * revision used by subsequent executions.
48 */
49 sourceContents: string;
50 /**
51 * Human-readable description (max 1000 Unicode characters).
52 */
53 description?: string;
54 /**
55 * User labels. Alchemy ownership labels are merged in automatically.
56 */
57 labels?: Record<string, string>;
58 /**
59 * Service account the workflow runs as. Format
60 * `projects/{project}/serviceAccounts/{account}` or an email. Defaults
61 * to the project's Compute Engine default service account. Updating this
62 * creates a new revision.
63 */
64 serviceAccount?: string;
65 /**
66 * Platform logging for `call` steps and responses.
67 * `LOG_ALL_CALLS`, `LOG_ERRORS_ONLY`, or `LOG_NONE`.
68 */
69 callLogLevel?: CallLogLevel;
70 /**
71 * User-defined environment variables for this revision. At most 20
72 * entries; each value up to 4KiB. Keys cannot be empty or start with
73 * `GOOGLE` or `WORKFLOWS`. Updating this creates a new revision.
74 */
75 userEnvVars?: Record<string, string>;
76 /**
77 * Execution history recorded for runs of this workflow.
78 * `EXECUTION_HISTORY_BASIC` or `EXECUTION_HISTORY_DETAILED`.
79 */
80 executionHistoryLevel?: ExecutionHistoryLevel;
81 /**
82 * Cloud KMS key used to encrypt workflow and execution data, as
83 * `projects/{project}/locations/{location}/keyRings/{keyRing}/cryptoKeys/{cryptoKey}`.
84 */
85 cryptoKeyName?: string;
86 };
87
88 export type Workflow = Resource<
89 "GCP.Workflows.Workflow",
90 WorkflowProps,
91 {
92 /** Full resource name `projects/{project}/locations/{location}/workflows/{workflow}`. */
93 name: string;
94 /** Workflow id (last path segment). */
95 workflowId: string;
96 /** Location id (`us-central1`, …). */
97 location: string;
98 /** Project id. */
99 project: string;
100 /** Workflow YAML/JSON source of the latest revision. */
101 sourceContents: string | undefined;
102 /** User-provided description. */
103 description: string | undefined;
104 /** User labels (Alchemy ownership labels stripped). */
105 labels: Record<string, string>;
106 /** Service account the latest revision runs as. */
107 serviceAccount: string | undefined;
108 /** Call logging level. */
109 callLogLevel: string | undefined;
110 /** User environment variables on the latest revision. */
111 userEnvVars: Record<string, string>;
112 /** Execution history level. */
113 executionHistoryLevel: string | undefined;
114 /** CMEK key, if any. */
115 cryptoKeyName: string | undefined;
116 /** CMEK key version currently in use. */
117 cryptoKeyVersion: string | undefined;
118 /** Latest revision id (`000001-a4d`). */
119 revisionId: string | undefined;
120 /** RFC3339 timestamp of the latest revision. */
121 revisionCreateTime: string | undefined;
122 /** Server-reported deployment state (`ACTIVE`, `UNAVAILABLE`). */
123 state: string | undefined;
124 /** Error details when `state` is `UNAVAILABLE`. */
125 stateError: workflows.StateError | undefined;
126 /** RFC3339 creation timestamp. */
127 createTime: string | undefined;
128 /** RFC3339 last-update timestamp. */
129 updateTime: string | undefined;
130 },
131 never,
132 Providers
133 >;
134
135 /**
136 * A Google Cloud Workflows workflow — YAML or JSON orchestration that
137 * Workflows executes as a revisioned program.
138 *
139 * Changing `workflowId` or `location` replaces the workflow. Updating
140 * source, service account, or environment variables creates a new
141 * revision; already-running executions keep the old revision.
142 *
143 * ### Creating a Workflow
144 * **Example:** Generated name
145 * ```typescript
146 * const greet = yield* GCP.Workflows.Workflow("Greet", {
147 * sourceContents: `main:
148 * steps:
149 * - done:
150 * return: hello
151 * `,
152 * });
153 * ```
154 *
155 * **Example:** Named workflow with labels and env vars
156 * ```typescript
157 * const greet = yield* GCP.Workflows.Workflow("Greet", {
158 * workflowId: "order-greet",
159 * location: "us-central1",
160 * description: "say hello",
161 * labels: { env: "prod" },
162 * callLogLevel: "LOG_ERRORS_ONLY",
163 * userEnvVars: { greeting: "hello" },
164 * sourceContents: `main:
165 * steps:
166 * - done:
167 * return: hello
168 * `,
169 * });
170 * ```
171 *
172 * ### Executing a Workflow
173 * **Example:** Start an execution
174 * ```typescript
175 * const createExecution = yield* GCP.Workflows.CreateExecution(greet);
176 * const execution = yield* createExecution({
177 * body: { argument: JSON.stringify({ name: "world" }) },
178 * });
179 * ```
180 *
181 * @resource
182 * @category Workflows
183 */
184 export const Workflow = Resource<Workflow>("GCP.Workflows.Workflow");
185
186 export class WorkflowNotResolved extends Data.TaggedError("GCP.Workflows.WorkflowNotResolved")<{
187 resourceName: string;
188 }> {}
189
190 /** The deployed workflow is in state `UNAVAILABLE` (see `stateError`). */
191 export class WorkflowUnavailable extends Data.TaggedError("GCP.Workflows.WorkflowUnavailable")<{
192 resourceName: string;
193 message: string;
194 }> {}
195
196 export class WorkflowStillExists extends Data.TaggedError("GCP.Workflows.WorkflowStillExists")<{
197 resourceName: string;
198 }> {}
199
200 const lastSegment = (value: string) => {
201 const trimmed = value.replace(/\/+$/, "");
202 const parts = trimmed.split("/");
203 return parts[parts.length - 1] || trimmed;
204 };
205
206 const normalizeLocation = (location: string | undefined, fallback: string) =>
207 lastSegment(location ?? fallback).toLowerCase();
208
209 const resourceName = (project: string, location: string, workflowId: string) =>
210 `projects/${project}/locations/${location}/workflows/${workflowId}`;
211
212 const parentOf = (project: string, location: string) => `projects/${project}/locations/${location}`;
213
214 const parseName = (name: string) => {
215 const parts = name.split("/").filter((part) => part.length > 0);
216 const workflowsAt = parts.lastIndexOf("workflows");
217 const locationsAt = parts.lastIndexOf("locations");
218 const projectsAt = parts.lastIndexOf("projects");
219 return {
220 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
221 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
222 workflowId:
223 workflowsAt >= 0 && parts[workflowsAt + 1] ? parts[workflowsAt + 1]! : lastSegment(name),
224 };
225 };
226
227 const userLabels = (
228 labels: Record<string, string | undefined> | null | undefined,
229 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
230
231 const userMap = (
232 map: Record<string, string | undefined> | null | undefined,
233 ): Record<string, string> => tagRecord(map);
234
235 const toId = (id: string, workflowId: string | undefined, existing?: string) =>
236 Effect.gen(function* () {
237 return (
238 workflowId ??
239 existing ??
240 (yield* createPhysicalName({
241 id,
242 maxLength: MAX_NAME_LENGTH,
243 lowercase: true,
244 }))
245 );
246 });
247
248 const compact = <T extends Record<string, unknown>>(value: T): T =>
249 Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined)) as T;
250
251 const mapJson = (map: Record<string, string | undefined> | null | undefined) =>
252 JSON.stringify(
253 Object.fromEntries(
254 Object.entries(map ?? {})
255 .filter(([, value]) => value !== undefined)
256 .sort(([left], [right]) => left.localeCompare(right)),
257 ),
258 );
259
260 const logLevelOf = (value: string | undefined) =>
261 !value || value === "CALL_LOG_LEVEL_UNSPECIFIED" ? undefined : value;
262
263 const historyLevelOf = (value: string | undefined) =>
264 !value || value === "EXECUTION_HISTORY_LEVEL_UNSPECIFIED" ? undefined : value;
265
266 const toAttrs = (workflow: workflows.Workflow, project: string) => {
267 const name = workflow.name ?? "";
268 const parsed = parseName(name);
269 return {
270 name,
271 workflowId: parsed.workflowId,
272 location: parsed.location,
273 project: parsed.project || project,
274 sourceContents: workflow.sourceContents,
275 description: workflow.description,
276 labels: userLabels(workflow.labels),
277 serviceAccount: workflow.serviceAccount,
278 callLogLevel: workflow.callLogLevel,
279 userEnvVars: userMap(workflow.userEnvVars),
280 executionHistoryLevel: workflow.executionHistoryLevel,
281 cryptoKeyName: workflow.cryptoKeyName,
282 cryptoKeyVersion: workflow.cryptoKeyVersion,
283 revisionId: workflow.revisionId,
284 revisionCreateTime: workflow.revisionCreateTime,
285 state: workflow.state,
286 stateError: workflow.stateError,
287 createTime: workflow.createTime,
288 updateTime: workflow.updateTime,
289 };
290 };
291
292 const getByName = (name: string) =>
293 workflows
294 .getProjectsLocationsWorkflows({ name })
295 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
296
297 /**
298 * Wait for a workflow operation. ALREADY_EXISTS (code 6) counts as success
299 * (create race); with `notFoundOk`, so does NOT_FOUND (code 5, delete race)
300 * and an operation that is already gone.
301 */
302 const waitForOperation = (operation: workflows.Operation, options?: { notFoundOk?: boolean }) =>
303 waitForGcpOperation(operation, (name) => workflows.getProjectsLocationsOperations({ name }), {
304 budget: "5 minutes",
305 interval: "2 seconds",
306 }).pipe(
307 Effect.catchIf(
308 (error) =>
309 (error._tag === "GCP.OperationFailed" &&
310 (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) ||
311 (options?.notFoundOk === true && error._tag === "NotFound"),
312 () => Effect.succeed(operation),
313 ),
314 );
315
316 const waitUntilReady = (name: string) =>
317 getByName(name).pipe(
318 Effect.filterOrFail(
319 (workflow): workflow is workflows.Workflow => workflow !== undefined,
320 () => new WorkflowNotResolved({ resourceName: name }),
321 ),
322 Effect.filterOrFail(
323 (workflow) => workflow.state !== "UNAVAILABLE",
324 (workflow) =>
325 new WorkflowUnavailable({
326 resourceName: name,
327 message: workflow.stateError?.details ?? "workflow is UNAVAILABLE",
328 }),
329 ),
330 Effect.filterOrFail(
331 (workflow) => workflow.state === "ACTIVE" || workflow.state === undefined,
332 () => new WorkflowNotResolved({ resourceName: name }),
333 ),
334 Effect.retry({
335 while: (error) => error._tag === "GCP.Workflows.WorkflowNotResolved",
336 times: 8,
337 schedule: Schedule.spaced("1 second"),
338 }),
339 );
340
341 const waitUntilGone = (name: string) =>
342 getByName(name).pipe(
343 Effect.flatMap((workflow) =>
344 workflow === undefined
345 ? Effect.void
346 : Effect.fail(new WorkflowStillExists({ resourceName: name })),
347 ),
348 Effect.retry({
349 while: (error) => error._tag === "GCP.Workflows.WorkflowStillExists",
350 times: 10,
351 schedule: Schedule.spaced("1 second"),
352 }),
353 );
354
355 export const WorkflowProvider = () =>
356 Provider.succeed(Workflow, {
357 stables: ["name", "workflowId", "location", "project", "createTime"],
358
359 diff: Effect.fn(function* ({ news, olds, output }) {
360 const env = yield* GcpEnvironment.current;
361 if (!isResolved(news)) return undefined;
362 const previousId = olds?.workflowId ?? output?.workflowId;
363 const nextId = news.workflowId ?? previousId;
364 const idChanged = previousId !== undefined && nextId !== undefined && nextId !== previousId;
365
366 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
367 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
368 const locationChanged = previousLocation !== nextLocation;
369
370 if (idChanged || locationChanged) {
371 return { action: "replace" as const, deleteFirst: false };
372 }
373 return undefined;
374 }),
375
376 read: Effect.fn(function* ({ id, olds, output }) {
377 const env = yield* GcpEnvironment.current;
378 const workflowId = yield* toId(id, olds?.workflowId, output?.workflowId);
379 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
380 const name = output?.name ?? resourceName(env.project, location, workflowId);
381 const existing = yield* getByName(name);
382 if (existing === undefined) return undefined;
383 const attrs = toAttrs(existing, env.project);
384 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
385 }),
386
387 list: () =>
388 Effect.gen(function* () {
389 const env = yield* GcpEnvironment.current;
390 return yield* workflows.listProjectsLocationsWorkflows
391 .pages({
392 parent: `projects/${env.project}/locations/-`,
393 pageSize: 1000,
394 })
395 .pipe(
396 Stream.flatMap((page) => Stream.fromIterable(page.workflows ?? [])),
397 Stream.filter((workflow) =>
398 Object.keys(workflow.labels ?? {}).some((key) => key.startsWith("alchemy-")),
399 ),
400 Stream.map((workflow) => toAttrs(workflow, env.project)),
401 Stream.runCollect,
402 Effect.map((chunk) => Array.from(chunk)),
403 Effect.catchTag("NotFound", () => Effect.succeed([])),
404 );
405 }),
406
407 reconcile: Effect.fn(function* ({ id, news, output }) {
408 const env = yield* GcpEnvironment.current;
409 const workflowId = yield* toId(id, news.workflowId, output?.workflowId);
410 const location = normalizeLocation(news.location ?? output?.location, env.region);
411 const name = resourceName(env.project, location, workflowId);
412 const parent = parentOf(env.project, location);
413 const desiredLabels = {
414 ...toLabels(news.labels),
415 ...(yield* createInternalLabels(id)),
416 };
417 const desiredBody = compact({
418 sourceContents: news.sourceContents,
419 description: news.description,
420 labels: desiredLabels,
421 serviceAccount: news.serviceAccount,
422 callLogLevel: news.callLogLevel,
423 userEnvVars: news.userEnvVars,
424 executionHistoryLevel: news.executionHistoryLevel,
425 cryptoKeyName: news.cryptoKeyName,
426 }) as workflows.Workflow;
427
428 let current = yield* getByName(name);
429
430 if (current === undefined) {
431 const created = yield* workflows
432 .createProjectsLocationsWorkflows({
433 parent,
434 workflowId,
435 body: desiredBody,
436 })
437 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
438 if (created !== undefined) {
439 yield* waitForOperation(created);
440 }
441 current = yield* waitUntilReady(name);
442 }
443
444 if (current === undefined) {
445 return yield* new WorkflowNotResolved({ resourceName: name });
446 }
447
448 const observedLabels = tagRecord(current.labels);
449 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
450 const labelsChanged = upsert.length > 0 || removed.length > 0;
451 const sourceChanged = (current.sourceContents ?? "") !== news.sourceContents;
452 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
453 const serviceAccountChanged =
454 news.serviceAccount !== undefined &&
455 lastSegment(current.serviceAccount ?? "") !== lastSegment(news.serviceAccount);
456 const callLogLevelChanged =
457 news.callLogLevel !== undefined &&
458 logLevelOf(current.callLogLevel) !== logLevelOf(news.callLogLevel);
459 const envVarsChanged =
460 news.userEnvVars !== undefined &&
461 mapJson(current.userEnvVars) !== mapJson(news.userEnvVars);
462 const historyChanged =
463 news.executionHistoryLevel !== undefined &&
464 historyLevelOf(current.executionHistoryLevel) !==
465 historyLevelOf(news.executionHistoryLevel);
466 const cryptoKeyChanged =
467 news.cryptoKeyName !== undefined && (current.cryptoKeyName ?? "") !== news.cryptoKeyName;
468
469 const updateMask = [
470 labelsChanged ? "labels" : undefined,
471 sourceChanged ? "sourceContents" : undefined,
472 descriptionChanged ? "description" : undefined,
473 serviceAccountChanged ? "serviceAccount" : undefined,
474 callLogLevelChanged ? "callLogLevel" : undefined,
475 envVarsChanged ? "userEnvVars" : undefined,
476 historyChanged ? "executionHistoryLevel" : undefined,
477 cryptoKeyChanged ? "cryptoKeyName" : undefined,
478 ].filter((field): field is string => field !== undefined);
479
480 if (updateMask.length > 0) {
481 const operation = yield* workflows
482 .patchProjectsLocationsWorkflows({
483 name,
484 updateMask: updateMask.join(","),
485 body: desiredBody,
486 })
487 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
488 if (operation === undefined) {
489 const created = yield* workflows
490 .createProjectsLocationsWorkflows({
491 parent,
492 workflowId,
493 body: desiredBody,
494 })
495 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
496 if (created !== undefined) {
497 yield* waitForOperation(created);
498 }
499 } else {
500 yield* waitForOperation(operation);
501 }
502 current = yield* waitUntilReady(name);
503 }
504
505 if (current === undefined) {
506 return yield* new WorkflowNotResolved({ resourceName: name });
507 }
508
509 return toAttrs(current, env.project);
510 }),
511
512 delete: Effect.fn(function* ({ output }) {
513 const operation = yield* workflows
514 .deleteProjectsLocationsWorkflows({ name: output.name })
515 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
516 if (operation !== undefined) {
517 yield* waitForOperation(operation, { notFoundOk: true });
518 }
519 yield* waitUntilGone(output.name);
520 }),
521 });