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 { ConfigProvider } from "effect/ConfigProvider"; |
| 2 | import type * as Context from "effect/Context"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Scope from "effect/Scope"; |
| 8 | import { AlchemyContext, AlchemyContextLive } from "./AlchemyContext.ts"; |
| 9 | import { provideFreshArtifactStore } from "./Artifacts.ts"; |
| 10 | import { CredentialsStoreLive } from "./Auth/Credentials.ts"; |
| 11 | import { ProfileStoreLive } from "./Auth/Profile.ts"; |
| 12 | import { type StackEffect, StateScope } from "./Stack.ts"; |
| 13 | import { Stage } from "./Stage.ts"; |
| 14 | import { loadConfigProvider } from "./Util/ConfigProvider.ts"; |
| 15 | import { PlatformServices } from "./Util/PlatformServices.ts"; |
| 16 | |
| 17 | // TODO(unify stack builds): this is one of several ways to build a stack; |
| 18 | // the others are `Alchemist/Operations.ts` (`buildStack`, |
| 19 | // `buildWithoutProgram`) and the test harness's scratch stacks |
| 20 | // (`alchemy-test/src/Core.ts`). They differ in config (this one reloads `.env` |
| 21 | // and drops the caller's ConfigProvider), adopt/updateStateStore, and how |
| 22 | // the state-store lease is scoped. Migrate everything to `Alchemist.plan` / |
| 23 | // `Alchemist.apply`: give Alchemist this file's per-call lease scope |
| 24 | // (`StateScope`), reimplement `Deploy.deploy` / `Destroy.destroy` on it, |
| 25 | // move scratch stacks onto it, then delete this file. |
| 26 | |
| 27 | // Deploy-time only: kept out of `Stack.ts` because every runtime imports the |
| 28 | // `Stack` tag, and this module's top-level platform layer (Node + Bun |
| 29 | // platforms, `ws`, profile and credential stores) survives tree-shaking. |
| 30 | |
| 31 | const platform = Layer.mergeAll( |
| 32 | PlatformServices, |
| 33 | FetchHttpClient.layer, |
| 34 | Layer.provide(ProfileStoreLive, PlatformServices), |
| 35 | Layer.provide(CredentialsStoreLive, PlatformServices), |
| 36 | ); |
| 37 | // override alchemy state store, CLI/reporting, state, and Config |
| 38 | const alchemy = (overrides?: { dev?: boolean }) => |
| 39 | Layer.mergeAll( |
| 40 | // optional |
| 41 | overrides?.dev |
| 42 | ? Layer.provide( |
| 43 | Layer.effect( |
| 44 | AlchemyContext, |
| 45 | AlchemyContext.useSync((ctx) => ({ ...ctx, dev: overrides.dev! })), |
| 46 | ), |
| 47 | AlchemyContextLive, |
| 48 | ) |
| 49 | : AlchemyContextLive, |
| 50 | ); |
| 51 | |
| 52 | export const evalStack = <S extends { services: Context.Context<any> }, B, StackErr, Err, Req>( |
| 53 | effect: StackEffect<S, StackErr, Stage | AlchemyContext>, |
| 54 | fn: (stack: S) => Effect.Effect<B, Err, Req>, |
| 55 | options: { |
| 56 | stage: string; |
| 57 | dev?: boolean; |
| 58 | /** |
| 59 | * Optional caller-supplied scope. When provided, scoped resources |
| 60 | * (e.g. the dev sidecar process) live until the caller closes this |
| 61 | * scope instead of being torn down when `evalStack` resolves. Used |
| 62 | * by the test harness so the sidecar survives across `beforeAll`, |
| 63 | * tests, and `afterAll`. When omitted, behaves as before with a |
| 64 | * private scope closed on completion. The state store never outlives |
| 65 | * the call. |
| 66 | */ |
| 67 | scope?: Scope.Scope; |
| 68 | }, |
| 69 | ) => { |
| 70 | const body = Effect.gen(function* () { |
| 71 | const stack = yield* effect; |
| 72 | const configProvider = yield* loadConfigProvider(Option.none()); |
| 73 | return yield* fn(stack).pipe( |
| 74 | provideFreshArtifactStore, |
| 75 | Effect.provide( |
| 76 | Layer.succeedContext(stack.services).pipe( |
| 77 | Layer.provideMerge(Layer.succeed(ConfigProvider, configProvider)), |
| 78 | ), |
| 79 | ), |
| 80 | ); |
| 81 | }).pipe( |
| 82 | Effect.provide( |
| 83 | Layer.succeed(Stage, options.stage).pipe( |
| 84 | Layer.provideMerge(Layer.provideMerge(alchemy({ dev: options.dev }), platform)), |
| 85 | ), |
| 86 | ), |
| 87 | ); |
| 88 | |
| 89 | const scope = options.scope; |
| 90 | return scope === undefined |
| 91 | ? Effect.scoped(body) |
| 92 | : // The state store closes with this call and releases its lease, so |
| 93 | // the next call on the same long-lived scope can take it. |
| 94 | Effect.scopedWith((stateScope) => |
| 95 | Scope.provide(body.pipe(Effect.provideService(StateScope, stateScope)), scope), |
| 96 | ); |
| 97 | }; |