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
| 1 | import * as Config from "effect/Config"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import { AlchemyContext } from "../AlchemyContext.ts"; |
| 5 | import * as Interaction from "../Interaction.ts"; |
| 6 | import { StateStoreError } from "./State.ts"; |
| 7 | |
| 8 | /** |
| 9 | * What every plugin state store does when it's missing or outdated, so the |
| 10 | * policy lives in one place: create on first use and upgrade on `--yes` or |
| 11 | * confirmation. In CI, only `--yes` does either. |
| 12 | */ |
| 13 | |
| 14 | const CI = Config.Boolean("CI").pipe(Config.withDefault(false)); |
| 15 | |
| 16 | /** A logical store name; stores derive physical names from it. */ |
| 17 | export const storeName = (name: string) => |
| 18 | /^[a-z0-9](?:[a-z0-9-]{0,18}[a-z0-9])?$/.test(name) |
| 19 | ? Effect.succeed(name) |
| 20 | : Effect.fail( |
| 21 | new StateStoreError({ |
| 22 | message: `Invalid state store name '${name}': use 1-20 lowercase letters, digits and hyphens.`, |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | export interface StoreRef { |
| 27 | readonly backend: string; |
| 28 | readonly name?: string; |
| 29 | /** e.g. `Cloudflare State Store 'alchemy-state-store'`. */ |
| 30 | readonly label: string; |
| 31 | /** |
| 32 | * Only the stack's config describes this store, e.g. a custom Worker or |
| 33 | * bucket name or a pinned account, so `--backend` can't address it. |
| 34 | */ |
| 35 | readonly configured?: boolean; |
| 36 | } |
| 37 | |
| 38 | const required = (store: StoreRef, action: "bootstrap" | "upgrade", problem: string) => { |
| 39 | const command = store.configured |
| 40 | ? "alchemy state bootstrap" |
| 41 | : `alchemy state bootstrap --backend ${store.backend}${store.name ? ` --name ${store.name}` : ""}`; |
| 42 | return new StateStoreError({ |
| 43 | message: `${store.label} ${problem}. Run '${command} --profile <your-ci-profile>' first, or pass --yes.`, |
| 44 | requires: { action, command }, |
| 45 | }); |
| 46 | }; |
| 47 | |
| 48 | /** Deploy a missing (or half-bootstrapped) store; in CI, only with `--yes`. */ |
| 49 | export const deployMissing = <A, E, R>( |
| 50 | store: StoreRef & { readonly incomplete?: boolean }, |
| 51 | deploy: Effect.Effect<A, E, R>, |
| 52 | ) => |
| 53 | Effect.gen(function* () { |
| 54 | const problem = store.incomplete ? "has an unfinished bootstrap" : "not found"; |
| 55 | const context = yield* Effect.serviceOption(AlchemyContext); |
| 56 | const yes = Option.isSome(context) && context.value.updateStateStore; |
| 57 | if (!yes && (yield* CI)) return yield* required(store, "bootstrap", problem); |
| 58 | const interaction = yield* Effect.serviceOption(Interaction.Interaction); |
| 59 | if (Option.isSome(interaction)) { |
| 60 | yield* interaction.value.output.info(`${store.label} ${problem}; deploying it.`); |
| 61 | } |
| 62 | return yield* deploy; |
| 63 | }); |
| 64 | |
| 65 | /** Upgrade an outdated store with `--yes` or on confirmation; in CI, only with `--yes`. */ |
| 66 | export const upgradeOutdated = <A, E, R>( |
| 67 | store: StoreRef & { readonly observed: number | undefined; readonly expected: number }, |
| 68 | upgrade: Effect.Effect<A, E, R>, |
| 69 | ) => |
| 70 | Effect.gen(function* () { |
| 71 | const problem = `is out of date (expected v${store.expected}, observed v${store.observed ?? "unknown"})`; |
| 72 | const context = yield* Effect.serviceOption(AlchemyContext); |
| 73 | if (Option.isSome(context) && context.value.updateStateStore) return yield* upgrade; |
| 74 | if (yield* CI) return yield* required(store, "upgrade", problem); |
| 75 | const interaction = yield* Effect.serviceOption(Interaction.Interaction); |
| 76 | const confirmed = Option.isSome(interaction) |
| 77 | ? yield* interaction.value.prompt |
| 78 | .confirm({ message: `${store.label} ${problem}`, initialValue: true }) |
| 79 | .pipe(Effect.orElseSucceed(() => false)) |
| 80 | : false; |
| 81 | return confirmed ? yield* upgrade : yield* required(store, "upgrade", problem); |
| 82 | }); |