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

main
ts 276 lines 9.84 KB
Raw
1 import * as dataform from "@distilled.cloud/gcp/dataform_v1";
2 import { Unowned } 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 { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 expandRepository,
12 forEachOwnedRepository,
13 hasAlchemyLabelMap,
14 listWorkflowInvocations,
15 normalizeLocation,
16 parseResourceName,
17 replaceOnIdentity,
18 retryTransient,
19 sameJson,
20 waitUntilGone,
21 } from "./internal.ts";
22 import type { InvocationConfig } from "./RepositoriesWorkflowConfig.ts";
23
24 export type RepositoriesWorkflowInvocationProps = {
25 /**
26 * Parent repository. Full name
27 * `projects/{project}/locations/{location}/repositories/{repository}`
28 * or the repository id (combined with `location`). Immutable —
29 * changing it replaces the invocation.
30 */
31 repository: string;
32 /**
33 * Region used when `repository` is a bare id.
34 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
35 */
36 location?: string;
37 /**
38 * Compilation result to invoke
39 * (`.../repositories/{repository}/compilationResults/{result}`).
40 * Mutually exclusive with `workflowConfig`. Immutable.
41 */
42 compilationResult?: string;
43 /**
44 * Workflow config to invoke
45 * (`.../repositories/{repository}/workflowConfigs/{workflowConfig}`).
46 * Mutually exclusive with `compilationResult`. Immutable.
47 */
48 workflowConfig?: string;
49 /**
50 * Invocation options. Immutable — changing them replaces the
51 * invocation.
52 */
53 invocationConfig?: InvocationConfig;
54 };
55
56 export type RepositoriesWorkflowInvocation = Resource<
57 "GCP.Dataform.RepositoriesWorkflowInvocation",
58 RepositoriesWorkflowInvocationProps,
59 {
60 /** Full resource name `.../repositories/{repository}/workflowInvocations/{invocation}`. */
61 name: string;
62 /** Workflow invocation id (last path segment). */
63 workflowInvocationId: string;
64 /** Parent repository resource name. */
65 repository: string;
66 /** Project id. */
67 project: string;
68 /** Location id. */
69 location: string;
70 /** Compilation result used, if any. */
71 compilationResult: string | undefined;
72 /** Workflow config used, if any. */
73 workflowConfig: string | undefined;
74 /** Resolved compilation result. */
75 resolvedCompilationResult: string | undefined;
76 /** Current state (`RUNNING`, `SUCCEEDED`, `FAILED`, …). */
77 state: string | undefined;
78 /** Inclusive start of the invocation. */
79 startTime: string | undefined;
80 /** Exclusive end of the invocation. */
81 endTime: string | undefined;
82 },
83 never,
84 Providers
85 >;
86
87 /**
88 * A Dataform workflow invocation — a single run of a compilation
89 * result (directly or via a workflow config).
90 *
91 * Invocations have no update API and no labels. Identity is
92 * server-assigned; changing `repository`, `compilationResult`,
93 * `workflowConfig`, or `invocationConfig` replaces the invocation.
94 * `list` / nuke finds invocations under Alchemy-labeled repositories.
95 * A running invocation is cancelled before delete.
96 *
97 * ### Creating a Workflow Invocation
98 * **Example:** Invoke a compilation result
99 * ```typescript
100 * const run = yield* GCP.Dataform.RepositoriesWorkflowInvocation("Run", {
101 * repository: repo.name,
102 * compilationResult: compilation.name,
103 * });
104 * ```
105 *
106 * **Example:** Invoke a workflow config
107 * ```typescript
108 * const run = yield* GCP.Dataform.RepositoriesWorkflowInvocation("Run", {
109 * repository: repo.name,
110 * workflowConfig: workflow.name,
111 * });
112 * ```
113 *
114 * @resource
115 * @category Dataform
116 */
117 export const RepositoriesWorkflowInvocation = Resource<RepositoriesWorkflowInvocation>(
118 "GCP.Dataform.RepositoriesWorkflowInvocation",
119 );
120
121 export class RepositoriesWorkflowInvocationNotResolved extends Data.TaggedError(
122 "GCP.Dataform.RepositoriesWorkflowInvocationNotResolved",
123 )<{
124 resourceName: string;
125 }> {}
126
127 const toAttrs = (invocation: dataform.WorkflowInvocation, project: string, region: string) => {
128 const name = invocation.name ?? "";
129 const parsed = parseResourceName(name, "workflowInvocations", region);
130 return {
131 name,
132 workflowInvocationId: parsed.id,
133 repository: parsed.parent,
134 project: parsed.project || project,
135 location: parsed.location,
136 compilationResult: invocation.compilationResult,
137 workflowConfig: invocation.workflowConfig,
138 resolvedCompilationResult: invocation.resolvedCompilationResult,
139 state: invocation.state,
140 startTime: invocation.invocationTiming?.startTime,
141 endTime: invocation.invocationTiming?.endTime,
142 };
143 };
144
145 const getByName = (name: string) =>
146 name.length === 0 || name.includes("//")
147 ? Effect.undefined
148 : dataform
149 .getProjectsLocationsRepositoriesWorkflowInvocations({ name })
150 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
151
152 const invocationBody = (
153 news: RepositoriesWorkflowInvocationProps,
154 ): dataform.WorkflowInvocation => ({
155 compilationResult: news.compilationResult,
156 workflowConfig: news.workflowConfig,
157 invocationConfig: news.invocationConfig
158 ? {
159 transitiveDependentsIncluded: news.invocationConfig.transitiveDependentsIncluded,
160 serviceAccount: news.invocationConfig.serviceAccount,
161 includedTags: news.invocationConfig.includedTags,
162 fullyRefreshIncrementalTablesEnabled:
163 news.invocationConfig.fullyRefreshIncrementalTablesEnabled,
164 queryPriority: news.invocationConfig.queryPriority,
165 transitiveDependenciesIncluded: news.invocationConfig.transitiveDependenciesIncluded,
166 includedTargets: news.invocationConfig.includedTargets,
167 }
168 : undefined,
169 });
170
171 export const RepositoriesWorkflowInvocationProvider = () =>
172 Provider.succeed(RepositoriesWorkflowInvocation, {
173 stables: ["name", "workflowInvocationId", "repository", "project", "location"],
174
175 diff: Effect.fn(function* ({ news, olds, output }) {
176 if (!isResolved(news)) return undefined;
177 const env = yield* GcpEnvironment.current;
178 const location = normalizeLocation(
179 news.location ?? olds?.location ?? output?.location,
180 env.region,
181 );
182 const compilationChanged =
183 (olds?.compilationResult ?? output?.compilationResult) !== undefined &&
184 news.compilationResult !== undefined &&
185 news.compilationResult !== (olds?.compilationResult ?? output?.compilationResult);
186 const workflowChanged =
187 (olds?.workflowConfig ?? output?.workflowConfig) !== undefined &&
188 news.workflowConfig !== undefined &&
189 news.workflowConfig !== (olds?.workflowConfig ?? output?.workflowConfig);
190 const configChanged =
191 news.invocationConfig !== undefined &&
192 olds?.invocationConfig !== undefined &&
193 !sameJson(olds.invocationConfig, news.invocationConfig);
194 return replaceOnIdentity({
195 previousLocation: olds?.location ?? output?.location,
196 nextLocation: news.location ?? olds?.location ?? output?.location,
197 previousParent: olds?.repository ?? output?.repository,
198 nextParent: expandRepository(news.repository, env.project, location),
199 extra: compilationChanged || workflowChanged || configChanged,
200 });
201 }),
202
203 read: Effect.fn(function* ({ output }) {
204 const env = yield* GcpEnvironment.current;
205 const existing = yield* getByName(output?.name ?? "");
206 if (existing === undefined) return undefined;
207 const attrs = toAttrs(existing, env.project, env.region);
208 const parent = yield* dataform
209 .getProjectsLocationsRepositories({ name: attrs.repository })
210 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
211 if (parent === undefined) return Unowned(attrs);
212 return hasAlchemyLabelMap(parent.labels) ? attrs : Unowned(attrs);
213 }),
214
215 list: () =>
216 Effect.gen(function* () {
217 const env = yield* GcpEnvironment.current;
218 const invocations = yield* forEachOwnedRepository(env.project, env.region, (repo) =>
219 listWorkflowInvocations(repo.name ?? ""),
220 );
221 return invocations.map((item) => toAttrs(item, env.project, env.region));
222 }),
223
224 reconcile: Effect.fn(function* ({ news, output }) {
225 const env = yield* GcpEnvironment.current;
226 const location = normalizeLocation(news.location ?? output?.location, env.region);
227 const repository = expandRepository(news.repository, env.project, location);
228
229 let current = yield* getByName(output?.name ?? "");
230
231 if (current === undefined) {
232 current = yield* retryTransient(
233 dataform.createProjectsLocationsRepositoriesWorkflowInvocations({
234 parent: repository,
235 body: invocationBody(news),
236 }),
237 );
238 }
239
240 if (current === undefined) {
241 return yield* new RepositoriesWorkflowInvocationNotResolved({
242 resourceName: output?.name ?? `${repository}/workflowInvocations`,
243 });
244 }
245
246 return toAttrs(current, env.project, env.region);
247 }),
248
249 delete: Effect.fn(function* ({ output }) {
250 if (!output.name) return;
251 const current = yield* getByName(output.name);
252 if (
253 current?.state === "RUNNING" ||
254 current?.state === "CANCELING" ||
255 current?.state === "STATE_UNSPECIFIED"
256 ) {
257 yield* retryTransient(
258 dataform.cancelProjectsLocationsRepositoriesWorkflowInvocations({
259 name: output.name,
260 body: {},
261 }),
262 ).pipe(
263 Effect.catchTags({
264 NotFound: () => Effect.void,
265 BadRequest: () => Effect.void,
266 }),
267 );
268 }
269 yield* retryTransient(
270 dataform.deleteProjectsLocationsRepositoriesWorkflowInvocations({
271 name: output.name,
272 }),
273 ).pipe(Effect.catchTag("NotFound", () => Effect.void));
274 yield* waitUntilGone(getByName(output.name));
275 }),
276 });