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
| 1 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | import { AuthProvidersLive } from "../Auth/Registry.ts"; |
| 7 | import { withProfileOverride } from "../Auth/Resolve.ts"; |
| 8 | import * as Contributions from "../Contributions.ts"; |
| 9 | import * as State from "../State/index.ts"; |
| 10 | import { loadConfigProvider } from "../Util/ConfigProvider.ts"; |
| 11 | import { |
| 12 | DEFAULT_ENTRYPOINT, |
| 13 | importStack, |
| 14 | StackEntrypointError, |
| 15 | type Target, |
| 16 | } from "./Entrypoint.ts"; |
| 17 | import { buildWithoutProgram, PLACEHOLDER_STAGE } from "./Operations.ts"; |
| 18 | |
| 19 | /** |
| 20 | * Build a provider's layers from scratch: the process may already have built |
| 21 | * them (credentials, auth registration) for another profile, and layers are |
| 22 | * memoized process-wide. |
| 23 | */ |
| 24 | const isolated = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 25 | Effect.provideServiceEffect(effect, Layer.CurrentMemoMap, Layer.makeMemoMap); |
| 26 | |
| 27 | /** Which store a state request addresses. */ |
| 28 | export type StateSource = |
| 29 | /** `.alchemy/` on this machine, ignoring whatever the project configures. */ |
| 30 | | { readonly type: "local" } |
| 31 | /** Whatever the project's entrypoint configures. */ |
| 32 | | ({ |
| 33 | readonly type: "configured"; |
| 34 | /** Stage the store is opened for. @default "placeholder" */ |
| 35 | readonly stage?: string; |
| 36 | } & Target) |
| 37 | /** A provider's state store, without loading a project entrypoint. */ |
| 38 | | ({ |
| 39 | readonly type: "provider"; |
| 40 | readonly backend: string; |
| 41 | /** A store other than the account's default. */ |
| 42 | readonly name?: string; |
| 43 | } & Pick<Target, "profile" | "envFile">); |
| 44 | |
| 45 | /** What lifecycle operations act on. */ |
| 46 | export type ProviderSource = Extract<StateSource, { readonly type: "provider" }>; |
| 47 | |
| 48 | /** A provider's state store by `--backend` name. */ |
| 49 | const findBackend = Effect.fn(function* (backend: string) { |
| 50 | const stores = yield* Contributions.stateStores(); |
| 51 | const store = stores.get(backend); |
| 52 | if (store === undefined) { |
| 53 | return yield* new Contributions.ContributionError({ |
| 54 | message: `Unknown state backend '${backend}'. Available: local, configured${[...stores.keys()] |
| 55 | .map((name) => `, ${name}`) |
| 56 | .join("")}. Install the package that provides it.`, |
| 57 | }); |
| 58 | } |
| 59 | return store; |
| 60 | }); |
| 61 | |
| 62 | /** |
| 63 | * What a provider's store runs under, its layer and lifecycle alike: the |
| 64 | * profile's config and an auth registry with the provider's auth. |
| 65 | * |
| 66 | * @internal exported for tests. |
| 67 | */ |
| 68 | export const providerContext = Effect.fn(function* ( |
| 69 | provider: Contributions.Contributions, |
| 70 | target: Pick<Target, "profile" | "envFile">, |
| 71 | ) { |
| 72 | const config = ConfigProvider.layer( |
| 73 | withProfileOverride( |
| 74 | yield* loadConfigProvider(Option.fromNullishOr(target.envFile)), |
| 75 | target.profile, |
| 76 | ), |
| 77 | ); |
| 78 | const auth = provider.auth |
| 79 | ? yield* Contributions.resolve(provider, "auth", provider.auth) |
| 80 | : Layer.empty; |
| 81 | return auth.pipe(Layer.provideMerge(AuthProvidersLive), Layer.provideMerge(config)); |
| 82 | }); |
| 83 | |
| 84 | /** |
| 85 | * Resolve the state service a source addresses, in the caller's `Scope`: the |
| 86 | * store's resources live until that scope closes. The tree operations |
| 87 | * (`State.listState`, `State.readState`, `State.deleteState`) take `State` |
| 88 | * from context — provide the resolved service (or {@link layer}) to run |
| 89 | * them against the chosen store: |
| 90 | * |
| 91 | * ```ts |
| 92 | * const state = yield* Alchemist.State.store({ type: "local" }); |
| 93 | * const items = yield* State.listState({ path }).pipe( |
| 94 | * Effect.provideService(State.State, Effect.succeed(state)), |
| 95 | * ); |
| 96 | * ``` |
| 97 | */ |
| 98 | export const store = Effect.fn("Alchemist.state.store")(function* (source: StateSource) { |
| 99 | switch (source.type) { |
| 100 | case "local": |
| 101 | return yield* Effect.provide( |
| 102 | Effect.flatten(State.State), |
| 103 | yield* Layer.build(State.localState()), |
| 104 | ); |
| 105 | case "provider": { |
| 106 | const { provider, store } = yield* findBackend(source.backend); |
| 107 | const { layer } = yield* store.load; |
| 108 | const context = yield* Layer.build( |
| 109 | layer({ name: source.name }).pipe(Layer.provide(yield* providerContext(provider, source))), |
| 110 | ).pipe(isolated); |
| 111 | return yield* Effect.provide(Effect.flatten(State.State), context); |
| 112 | } |
| 113 | case "configured": { |
| 114 | // The configured store lives in the stack entrypoint; this is the one |
| 115 | // command that can also work without one. |
| 116 | const built = yield* importStack(source.entrypoint ?? DEFAULT_ENTRYPOINT).pipe( |
| 117 | Effect.flatMap((stack) => |
| 118 | buildWithoutProgram(stack, { |
| 119 | stage: source.stage ?? PLACEHOLDER_STAGE, |
| 120 | profile: source.profile, |
| 121 | envFile: source.envFile, |
| 122 | }), |
| 123 | ), |
| 124 | Effect.catchIf( |
| 125 | (error): error is StackEntrypointError => error instanceof StackEntrypointError, |
| 126 | (error) => |
| 127 | Effect.fail( |
| 128 | new StackEntrypointError({ |
| 129 | message: `${error.message} For config-less state access pass --backend with a state store name.`, |
| 130 | }), |
| 131 | ), |
| 132 | ), |
| 133 | ); |
| 134 | return yield* Effect.provide(Effect.flatten(State.State), built.context); |
| 135 | } |
| 136 | } |
| 137 | }); |
| 138 | |
| 139 | /** The chosen store as a `State` layer. */ |
| 140 | export const layer = (source: StateSource) => |
| 141 | Layer.effect(State.State, Effect.map(store(source), Effect.succeed)); |
| 142 | |
| 143 | /** A provider store's lifecycle operation, with the context to run it under. */ |
| 144 | const lifecycleOperation = Effect.fn(function* <K extends keyof Contributions.StateStoreLifecycle>( |
| 145 | source: ProviderSource, |
| 146 | operation: K, |
| 147 | ) { |
| 148 | if (Contributions.RESERVED_BACKENDS.includes(source.backend)) { |
| 149 | return yield* new Contributions.ContributionError({ |
| 150 | message: `The ${source.backend} state store has no ${operation}; pass --backend with a provider's store.`, |
| 151 | }); |
| 152 | } |
| 153 | const { provider, store } = yield* findBackend(source.backend); |
| 154 | const run = (yield* store.load).lifecycle?.[operation]; |
| 155 | if (run === undefined) { |
| 156 | return yield* new Contributions.ContributionError({ |
| 157 | message: `State backend '${source.backend}' does not support ${operation}.`, |
| 158 | }); |
| 159 | } |
| 160 | return { |
| 161 | run, |
| 162 | context: yield* providerContext(provider, source), |
| 163 | }; |
| 164 | }); |
| 165 | |
| 166 | /** Provision (or adopt) a state store. */ |
| 167 | export const bootstrap = Effect.fn("Alchemist.state.bootstrap")(function* ( |
| 168 | source: ProviderSource & { readonly force?: boolean }, |
| 169 | ) { |
| 170 | const { run, context } = yield* lifecycleOperation(source, "bootstrap"); |
| 171 | return yield* run({ name: source.name, force: source.force }).pipe( |
| 172 | Effect.provide(context), |
| 173 | isolated, |
| 174 | ); |
| 175 | }); |
| 176 | |
| 177 | /** |
| 178 | * Provision (or upgrade) the store a project's entrypoint configures, with |
| 179 | * every option it sets, such as a custom name, a pinned account or a domain. |
| 180 | */ |
| 181 | export const bootstrapConfigured = Effect.fn("Alchemist.state.bootstrapConfigured")(function* ( |
| 182 | source: Extract<StateSource, { readonly type: "configured" }>, |
| 183 | ) { |
| 184 | const built = yield* importStack(source.entrypoint ?? DEFAULT_ENTRYPOINT).pipe( |
| 185 | Effect.flatMap((stack) => |
| 186 | buildWithoutProgram(stack, { |
| 187 | stage: source.stage ?? PLACEHOLDER_STAGE, |
| 188 | profile: source.profile, |
| 189 | envFile: source.envFile, |
| 190 | updateStateStore: true, |
| 191 | }), |
| 192 | ), |
| 193 | ); |
| 194 | const store = yield* Effect.provide(Effect.flatten(State.State), built.context); |
| 195 | return { version: yield* store.getVersion() }; |
| 196 | }); |
| 197 | |
| 198 | /** Delete a state store and its backing resources. */ |
| 199 | export const teardown = Effect.fn("Alchemist.state.teardown")(function* (source: ProviderSource) { |
| 200 | const { run, context } = yield* lifecycleOperation(source, "teardown"); |
| 201 | return yield* run({ name: source.name }).pipe(Effect.provide(context), isolated); |
| 202 | }); |
| 203 | |
| 204 | /** |
| 205 | * Move stages into a state store from the one it replaces, or back. The |
| 206 | * configured source migrates exactly the store the project's entrypoint |
| 207 | * configures, options included. |
| 208 | */ |
| 209 | export const migrate = Effect.fn("Alchemist.state.migrate")(function* ( |
| 210 | source: StateSource, |
| 211 | request: State.StateMigrationRequest = {}, |
| 212 | ) { |
| 213 | return yield* Effect.scoped( |
| 214 | Effect.gen(function* () { |
| 215 | const resolved = yield* store(source); |
| 216 | if (resolved.migrate === undefined) { |
| 217 | return yield* new State.StateStoreError({ |
| 218 | message: `The ${source.type === "provider" ? `'${source.backend}'` : "configured"} state store does not replace another store, so it has nothing to migrate`, |
| 219 | }); |
| 220 | } |
| 221 | return { stages: yield* resolved.migrate(request) }; |
| 222 | }), |
| 223 | ); |
| 224 | }); |
| 225 | |
| 226 | /** Past log entries from a state store, oldest first. */ |
| 227 | export const logs = Effect.fn("Alchemist.state.logs")(function* ( |
| 228 | source: ProviderSource & { readonly limit?: number; readonly since?: Date }, |
| 229 | ) { |
| 230 | const { run, context } = yield* lifecycleOperation(source, "logs"); |
| 231 | return yield* run({ name: source.name, limit: source.limit, since: source.since }).pipe( |
| 232 | Effect.provide(context), |
| 233 | isolated, |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | /** Live log entries from a state store. */ |
| 238 | export const tailLogs = (source: ProviderSource) => |
| 239 | Stream.unwrap( |
| 240 | Effect.map(lifecycleOperation(source, "tailLogs"), ({ run, context }) => |
| 241 | run({ name: source.name }).pipe( |
| 242 | Stream.provide(context), |
| 243 | Stream.provideServiceEffect(Layer.CurrentMemoMap, Layer.makeMemoMap), |
| 244 | ), |
| 245 | ), |
| 246 | ); |