@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 91 lines 3.38 KB
Raw
1 import * as storage from "@distilled.cloud/gcp/storage_v1";
2 import type { StateStoreImpl, StateStoreLifecycle } from "alchemy/Contributions";
3 import * as Provisioning from "alchemy/State/Provisioning";
4 import * as Effect from "effect/Effect";
5 import * as Schedule from "effect/Schedule";
6 import * as Stream from "effect/Stream";
7 import { GcpEnvironment } from "../Environment.ts";
8 import {
9 createStateBucketName,
10 provisionStateBucket,
11 state,
12 stateBucketExists,
13 stateEnvironment,
14 } from "./State.ts";
15
16 /**
17 * Ensure a state bucket (by default the project's) with the settings first
18 * use would give it.
19 */
20 const bootstrapStateBucket = Effect.fn("GCP.state.bootstrap")(function* (name?: string) {
21 const { project, region } = yield* GcpEnvironment.current;
22 if (name !== undefined) yield* Provisioning.storeName(name);
23 const bucketName = createStateBucketName(project, name);
24 const existed = yield* stateBucketExists(bucketName);
25 yield* provisionStateBucket(bucketName, project, region);
26 return { project, region, bucketName, existed };
27 });
28
29 /**
30 * Empty and delete a state bucket (by default the project's), every object
31 * generation included. A missing bucket is not an error.
32 */
33 const teardownStateBucket = Effect.fn("GCP.state.teardown")(function* (name?: string) {
34 const { project } = yield* GcpEnvironment.current;
35 if (name !== undefined) yield* Provisioning.storeName(name);
36 const bucketName = createStateBucketName(project, name);
37 if (!(yield* stateBucketExists(bucketName))) {
38 return { bucketName, deleted: false };
39 }
40 yield* storage.listObjects.pages({ bucket: bucketName, versions: true }).pipe(
41 Stream.flatMap((page) => Stream.fromIterable(page.items ?? [])),
42 Stream.runForEach((object) =>
43 object.name === undefined
44 ? Effect.void
45 : storage
46 .deleteObjects({
47 bucket: bucketName,
48 object: object.name,
49 generation: object.generation,
50 })
51 .pipe(Effect.catchTag("NotFound", () => Effect.void)),
52 ),
53 );
54 yield* storage.deleteBuckets({ bucket: bucketName }).pipe(
55 Effect.catchTag("NotFound", () => Effect.void),
56 // Deleted objects can take a moment to stop counting against the bucket.
57 Effect.retry({
58 while: (error) => error._tag === "Conflict",
59 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(6)]),
60 }),
61 );
62 return { bucketName, deleted: true };
63 });
64
65 /** The GCP environment of the profile core runs the operation under. */
66 const environment = stateEnvironment();
67
68 /**
69 * The Cloud Storage state store's lifecycle, for `alchemy state`. The store
70 * also creates its bucket on first use, so bootstrapping is only needed to
71 * provision it ahead of time (CI).
72 */
73 export const lifecycle: StateStoreLifecycle = {
74 bootstrap: ({ name }) =>
75 bootstrapStateBucket(name).pipe(
76 Effect.map(({ bucketName, project, region, existed }) => ({
77 status: existed ? ("adopted" as const) : ("created" as const),
78 store: bucketName,
79 location: { account: project, region },
80 })),
81 Effect.provide(environment),
82 ),
83 teardown: ({ name }) =>
84 teardownStateBucket(name).pipe(
85 Effect.map(({ bucketName, deleted }) => ({ deleted: deleted ? [bucketName] : [] })),
86 Effect.provide(environment),
87 ),
88 };
89
90 /** The state store GCP contributes. */
91 export const stateStore: StateStoreImpl = { layer: state, lifecycle };