@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 83 lines 3.24 KB
Raw
1 import * as s3 from "@distilled.cloud/aws/s3";
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 { deleteAllObjects } from "../AssetsBucket.ts";
7 import { AWSEnvironment } from "../Environment.ts";
8 import { openLegacyBucket } from "../StateStore/LegacyLayout.ts";
9 import {
10 createStateBucketName,
11 provisionStateBucket,
12 s3State,
13 stateBucketExists,
14 stateEnvironment,
15 } from "../StateStore/State.ts";
16
17 /**
18 * Ensure a state bucket (by default the account's in this region) with the
19 * settings first use would give it.
20 */
21 const bootstrapStateBucket = Effect.fn("AWS.state.bootstrap")(function* (name?: string) {
22 const { accountId, region } = yield* AWSEnvironment.current;
23 if (name !== undefined) yield* Provisioning.storeName(name);
24 const bucketName = createStateBucketName(accountId, region, name);
25 const existed = yield* stateBucketExists(bucketName);
26 yield* provisionStateBucket(bucketName, region);
27 return { accountId, region, bucketName, existed };
28 });
29
30 /**
31 * Empty and delete a state bucket (by default the account's in this region),
32 * every object version included. A missing bucket is not an error.
33 */
34 export const teardownStateBucket = Effect.fn("AWS.state.teardown")(function* (name?: string) {
35 const { accountId, region } = yield* AWSEnvironment.current;
36 if (name !== undefined) yield* Provisioning.storeName(name);
37 const bucketName = createStateBucketName(accountId, region, name);
38 if (!(yield* stateBucketExists(bucketName))) {
39 return { bucketName, deleted: false };
40 }
41 // A migration denies object access in the bucket policy; lift it first.
42 yield* openLegacyBucket(bucketName);
43 yield* deleteAllObjects(bucketName);
44 yield* s3.deleteBucket({ Bucket: bucketName }).pipe(
45 Effect.catchTag("NoSuchBucket", () => Effect.void),
46 Effect.retry({
47 while: (error) => error._tag === "OperationAborted" || error._tag === "ServiceUnavailable",
48 schedule: Schedule.max([Schedule.exponential(100), Schedule.recurs(10)]),
49 }),
50 );
51 return { bucketName, deleted: true };
52 });
53
54 /** The AWS environment of the profile core runs the operation under. */
55 const environment = stateEnvironment();
56
57 /**
58 * The S3 state store's lifecycle, for `Alchemist.State` and `alchemy state`.
59 * The store also creates its bucket on first use, so bootstrapping is only
60 * needed to provision it ahead of time. S3 keeps no log stream to show.
61 */
62 export const lifecycle: StateStoreLifecycle = {
63 bootstrap: ({ name }) =>
64 bootstrapStateBucket(name).pipe(
65 Effect.map(({ bucketName, accountId, region, existed }) => ({
66 status: existed ? ("adopted" as const) : ("created" as const),
67 store: bucketName,
68 location: { account: accountId, region },
69 })),
70 Effect.provide(environment),
71 ),
72 teardown: ({ name }) =>
73 teardownStateBucket(name).pipe(
74 Effect.map(({ bucketName, deleted }) => ({ deleted: deleted ? [bucketName] : [] })),
75 Effect.provide(environment),
76 ),
77 };
78
79 /** The S3 state store AWS contributes, as `--backend aws-s3`. */
80 export const s3StateStore: StateStoreImpl = {
81 layer: (options) => s3State(options),
82 lifecycle,
83 };