@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, 06:00 UTC

main
ts 308 lines 10.2 KB
Raw
1 import * as bigtable from "@distilled.cloud/gcp/bigtableadmin_v2";
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 * as Schedule from "effect/Schedule";
9 import { GcpEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 instanceIdOf,
13 listAlchemyTables,
14 MAX_SCHEMA_BUNDLE_ID_LENGTH,
15 parentOwned,
16 parseResourceName,
17 tableIdOf,
18 tableNameOf,
19 toPhysicalId,
20 waitForOperation,
21 collectPages,
22 } from "./operations.ts";
23
24 export type InstancesTablesSchemaBundleProps = {
25 /**
26 * Parent instance. Full name `projects/{project}/instances/{instance}`
27 * or the instance id. Immutable — changing it replaces the bundle.
28 */
29 instance: string;
30 /**
31 * Parent table. Full name
32 * `projects/{project}/instances/{instance}/tables/{table}` or the table
33 * id. Immutable — changing it replaces the bundle.
34 */
35 table: string;
36 /**
37 * Schema bundle id (the `{schema_bundle}` segment of
38 * `.../tables/{table}/schemaBundles/{schema_bundle}`). If omitted, a
39 * unique name is generated from the stack, stage, and logical id.
40 * Immutable — changing it replaces the bundle.
41 */
42 schemaBundleId?: string;
43 /**
44 * Base64-encoded protobuf `FileDescriptorSet` (`protoc
45 * --include_imports --descriptor_set_out`).
46 */
47 protoDescriptors: string;
48 /**
49 * Ignore backwards-compatibility checks on update.
50 * @default true
51 */
52 ignoreWarnings?: boolean;
53 };
54
55 export type InstancesTablesSchemaBundle = Resource<
56 "GCP.Bigtable.InstancesTablesSchemaBundle",
57 InstancesTablesSchemaBundleProps,
58 {
59 /** Full resource name `.../tables/{table}/schemaBundles/{schema_bundle}`. */
60 name: string;
61 /** Schema bundle id (last path segment). */
62 schemaBundleId: string;
63 /** Parent table resource name. */
64 table: string;
65 /** Parent table id. */
66 tableId: string;
67 /** Parent instance resource name. */
68 instance: string;
69 /** Parent instance id. */
70 instanceId: string;
71 /** Project id. */
72 project: string;
73 /** Base64-encoded FileDescriptorSet currently stored. */
74 protoDescriptors: string | undefined;
75 /** Server etag. */
76 etag: string | undefined;
77 },
78 never,
79 Providers
80 >;
81
82 /**
83 * A Cloud Bigtable schema bundle — a named FileDescriptorSet used to
84 * decode protobuf cell values.
85 *
86 * The parent instance and table must already exist. Changing
87 * `schemaBundleId`, `instance`, or `table` replaces the bundle.
88 * `protoDescriptors` update in place (must be backwards compatible
89 * unless `ignoreWarnings` is true).
90 *
91 * Schema bundles have no labels field. Alchemy treats a bundle as owned
92 * when its parent instance carries Alchemy labels, so `list` /
93 * `pnpm nuke:gcp` can find it.
94 *
95 * ### Creating a Schema Bundle
96 * **Example:** Bundle from a FileDescriptorSet
97 * ```typescript
98 * const bundle = yield* GCP.Bigtable.InstancesTablesSchemaBundle("Rows", {
99 * instance: instance.name,
100 * table: table.name,
101 * protoDescriptors: descriptors,
102 * });
103 * ```
104 *
105 * @resource
106 * @category Bigtable
107 */
108 export const InstancesTablesSchemaBundle = Resource<InstancesTablesSchemaBundle>(
109 "GCP.Bigtable.InstancesTablesSchemaBundle",
110 );
111
112 export class SchemaBundleNotResolved extends Data.TaggedError(
113 "GCP.Bigtable.SchemaBundleNotResolved",
114 )<{
115 resourceName: string;
116 }> {}
117
118 export class SchemaBundleStillExists extends Data.TaggedError(
119 "GCP.Bigtable.SchemaBundleStillExists",
120 )<{
121 resourceName: string;
122 }> {}
123
124 const toId = (id: string, schemaBundleId: string | undefined, existing?: string) =>
125 toPhysicalId(id, schemaBundleId, existing, MAX_SCHEMA_BUNDLE_ID_LENGTH);
126
127 const descriptorsOf = (schema: bigtable.ProtoSchema | { protoDescriptors?: string } | undefined) =>
128 schema?.protoDescriptors ?? "";
129
130 const toAttrs = (bundle: bigtable.SchemaBundle, project: string) => {
131 const name = bundle.name ?? "";
132 const parsed = parseResourceName(name);
133 return {
134 name,
135 schemaBundleId: parsed.schemaBundleId,
136 table: parsed.table,
137 tableId: parsed.tableId,
138 instance: parsed.instance,
139 instanceId: parsed.instanceId,
140 project: parsed.project || project,
141 protoDescriptors: bundle.protoSchema?.protoDescriptors,
142 etag: bundle.etag,
143 };
144 };
145
146 const getByName = (name: string) =>
147 bigtable
148 .getProjectsInstancesTablesSchemaBundles({ name })
149 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
150
151 const waitUntilExists = (name: string) =>
152 getByName(name).pipe(
153 Effect.flatMap((bundle) =>
154 bundle
155 ? Effect.succeed(bundle)
156 : Effect.fail(new SchemaBundleNotResolved({ resourceName: name })),
157 ),
158 Effect.retry({
159 while: (error) => error._tag === "GCP.Bigtable.SchemaBundleNotResolved",
160 times: 8,
161 schedule: Schedule.spaced("1 second"),
162 }),
163 );
164
165 const waitUntilGone = (name: string) =>
166 getByName(name).pipe(
167 Effect.flatMap((bundle) =>
168 bundle === undefined
169 ? Effect.void
170 : Effect.fail(new SchemaBundleStillExists({ resourceName: name })),
171 ),
172 Effect.retry({
173 while: (error) => error._tag === "GCP.Bigtable.SchemaBundleStillExists",
174 times: 8,
175 schedule: Schedule.spaced("2 seconds"),
176 }),
177 );
178
179 export const InstancesTablesSchemaBundleProvider = () =>
180 Provider.succeed(InstancesTablesSchemaBundle, {
181 stables: ["name", "schemaBundleId", "table", "tableId", "instance", "instanceId", "project"],
182
183 diff: Effect.fn(function* ({ news, olds, output }) {
184 if (!isResolved(news)) return undefined;
185 const previousId = olds?.schemaBundleId ?? output?.schemaBundleId;
186 const nextId = news.schemaBundleId ?? previousId;
187 const previousInstance = instanceIdOf(
188 olds?.instance ?? output?.instance ?? output?.instanceId ?? "",
189 );
190 const nextInstance = instanceIdOf(news.instance);
191 const previousTable = tableIdOf(olds?.table ?? output?.table ?? output?.tableId ?? "");
192 const nextTable = tableIdOf(news.table);
193 if (
194 (previousId !== undefined && nextId !== undefined && previousId !== nextId) ||
195 (previousInstance.length > 0 && previousInstance !== nextInstance) ||
196 (previousTable.length > 0 && previousTable !== nextTable)
197 ) {
198 return { action: "replace" as const };
199 }
200 return undefined;
201 }),
202
203 read: Effect.fn(function* ({ id, olds, output }) {
204 const env = yield* GcpEnvironment.current;
205 const schemaBundleId = yield* toId(id, olds?.schemaBundleId, output?.schemaBundleId);
206 const instanceRef = olds?.instance ?? output?.instance;
207 const tableRef = olds?.table ?? output?.table;
208 const name =
209 output?.name ??
210 (instanceRef && tableRef
211 ? `${tableNameOf(env.project, instanceRef, tableRef)}/schemaBundles/${schemaBundleId}`
212 : undefined);
213 if (name === undefined) return undefined;
214 const existing = yield* getByName(name);
215 if (existing === undefined) return undefined;
216 const attrs = toAttrs(existing, env.project);
217 return (yield* parentOwned(attrs.instance)) ? attrs : Unowned(attrs);
218 }),
219
220 list: () =>
221 Effect.gen(function* () {
222 const env = yield* GcpEnvironment.current;
223 const tables = (yield* listAlchemyTables(env.project)).filter(
224 (table): table is bigtable.Table & { name: string } =>
225 typeof table.name === "string" && table.name.length > 0,
226 );
227 const pages = yield* Effect.forEach(
228 tables,
229 (table) =>
230 collectPages(
231 bigtable.listProjectsInstancesTablesSchemaBundles.pages({
232 parent: table.name,
233 pageSize: 1000,
234 }),
235 (page) => page.schemaBundles,
236 ).pipe(
237 Effect.catchTag("NotFound", () => Effect.succeed([] as bigtable.SchemaBundle[])),
238 ),
239 { concurrency: 4 },
240 );
241 return pages.flat().map((bundle) => toAttrs(bundle, env.project));
242 }),
243
244 reconcile: Effect.fn(function* ({ id, news, output }) {
245 const env = yield* GcpEnvironment.current;
246 const schemaBundleId = yield* toId(id, news.schemaBundleId, output?.schemaBundleId);
247 const parent = tableNameOf(env.project, news.instance, news.table);
248 const name = `${parent}/schemaBundles/${schemaBundleId}`;
249 const ignoreWarnings = news.ignoreWarnings ?? true;
250 const protoSchema = { protoDescriptors: news.protoDescriptors };
251
252 let current = yield* getByName(output?.name ?? name);
253
254 if (current === undefined) {
255 const created = yield* bigtable
256 .createProjectsInstancesTablesSchemaBundles({
257 parent,
258 schemaBundleId,
259 body: { protoSchema },
260 })
261 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
262 if (created !== undefined) {
263 yield* waitForOperation(created, { alreadyExistsOk: true });
264 }
265 current = yield* waitUntilExists(name);
266 }
267
268 if (descriptorsOf(current.protoSchema) !== news.protoDescriptors) {
269 const patched = yield* bigtable
270 .patchProjectsInstancesTablesSchemaBundles({
271 name,
272 updateMask: "proto_schema",
273 ignoreWarnings,
274 body: { protoSchema, etag: current.etag },
275 })
276 .pipe(
277 Effect.retry({
278 while: (error) => error._tag === "Conflict",
279 times: 8,
280 schedule: Schedule.spaced("1 second"),
281 }),
282 );
283 yield* waitForOperation(patched);
284 current = yield* getByName(name);
285 if (current === undefined) {
286 return yield* new SchemaBundleNotResolved({ resourceName: name });
287 }
288 }
289
290 return toAttrs(current, env.project);
291 }),
292
293 delete: Effect.fn(function* ({ output }) {
294 yield* bigtable
295 .deleteProjectsInstancesTablesSchemaBundles({
296 name: output.name,
297 })
298 .pipe(
299 Effect.catchTag("NotFound", () => Effect.void),
300 Effect.retry({
301 while: (error) => error._tag === "Conflict",
302 times: 8,
303 schedule: Schedule.spaced("2 seconds"),
304 }),
305 );
306 yield* waitUntilGone(output.name);
307 }),
308 });