The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Cause from "effect/Cause"; |
| 2 | import * as Context from "effect/Context"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import type { PlatformError } from "effect/PlatformError"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import * as Scope from "effect/Scope"; |
| 11 | import { AuthError, AuthProviders } from "../Auth/AuthProvider.ts"; |
| 12 | import { SuppressMissingProviderConfig } from "../Auth/Profile.ts"; |
| 13 | import * as Contributions from "../Contributions.ts"; |
| 14 | import type { ContributionError } from "../Contributions.ts"; |
| 15 | import { makeCachedFunction } from "../Util/cached-function.ts"; |
| 16 | import { DEFAULT_ENTRYPOINT, importStack } from "./Entrypoint.ts"; |
| 17 | import * as Operations from "./Operations.ts"; |
| 18 | import type { Services } from "./Runtime.ts"; |
| 19 | |
| 20 | // Every auth provider a project can reach: what `alchemy profile` and |
| 21 | // `alchemy provider check-env` list and configure. Collected by building the |
| 22 | // installed providers' auth and the project's providers into one registry. |
| 23 | |
| 24 | /** What collecting an auth registry can fail with. */ |
| 25 | export type CollectAuthProvidersError = AuthError | ContributionError | PlatformError; |
| 26 | |
| 27 | /** What collecting an auth registry runs under. */ |
| 28 | export type CollectAuthProvidersServices = Services | Scope.Scope; |
| 29 | |
| 30 | type CollectAuthProviders = ( |
| 31 | options: CollectAuthProvidersOptions, |
| 32 | ) => Effect.Effect< |
| 33 | AuthProviders["Service"], |
| 34 | CollectAuthProvidersError, |
| 35 | CollectAuthProvidersServices |
| 36 | >; |
| 37 | |
| 38 | export interface CollectAuthProvidersOptions { |
| 39 | readonly main: string; |
| 40 | readonly envFile: Option.Option<string>; |
| 41 | readonly profile: string; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Every provider's auth provider, merged into one registering layer. One |
| 46 | * provider whose auth fails to load is skipped with a warning, so it can't |
| 47 | * take every other provider's auth down with it. |
| 48 | */ |
| 49 | const contributedAuth = Effect.gen(function* () { |
| 50 | const layers = yield* Effect.forEach( |
| 51 | yield* Contributions.load(), |
| 52 | (provider) => |
| 53 | provider.auth |
| 54 | ? Contributions.resolve(provider, "auth", provider.auth).pipe( |
| 55 | Effect.catch((error) => Effect.as(Effect.logWarning(error.message), Layer.empty)), |
| 56 | ) |
| 57 | : Effect.succeed(Layer.empty), |
| 58 | { concurrency: "unbounded" }, |
| 59 | ); |
| 60 | return Layer.mergeAll(Layer.empty, ...layers); |
| 61 | }); |
| 62 | |
| 63 | /** Register every installed provider's auth into the ambient registry. */ |
| 64 | const registerContributedAuth = Effect.fn("registerContributedAuth")(function* (options: { |
| 65 | readonly envFile: Option.Option<string>; |
| 66 | readonly profile: string; |
| 67 | }) { |
| 68 | const shared = yield* Operations.configServices({ |
| 69 | ...options, |
| 70 | envFile: Option.getOrUndefined(options.envFile), |
| 71 | }); |
| 72 | yield* Layer.build(Layer.provide(yield* contributedAuth, shared)); |
| 73 | }); |
| 74 | |
| 75 | const isMissingProviderConfig = Schema.is( |
| 76 | Schema.Struct({ _tag: Schema.Literals(["MissingProviderConfig"]) }), |
| 77 | ); |
| 78 | |
| 79 | /** |
| 80 | * Every auth provider reachable from the current project: the built-ins, plus |
| 81 | * whatever the user's stack module registers when it is importable. |
| 82 | */ |
| 83 | const collectAuthProvidersUncached = Effect.fn("collectAuthProvidersUncached")( |
| 84 | function* (options: CollectAuthProvidersOptions) { |
| 85 | // Everything built below registers into this one registry: the |
| 86 | // registry layers reuse an ambient registry instead of starting fresh. |
| 87 | const authProviders: AuthProviders["Service"] = {}; |
| 88 | const collect = Effect.provideService(AuthProviders, authProviders); |
| 89 | yield* registerContributedAuth(options).pipe(collect); |
| 90 | |
| 91 | const fs = yield* FileSystem.FileSystem; |
| 92 | const entrypointExists = yield* fs.exists(options.main); |
| 93 | const missingDefault = options.main === DEFAULT_ENTRYPOINT && !entrypointExists; |
| 94 | if (!entrypointExists && !missingDefault) { |
| 95 | return yield* AuthError.make({ |
| 96 | message: `Stack entrypoint '${options.main}' does not exist.`, |
| 97 | }); |
| 98 | } |
| 99 | if (!missingDefault) { |
| 100 | yield* importStack(options.main).pipe( |
| 101 | Effect.flatMap((stack) => |
| 102 | Operations.buildProviders(stack, { |
| 103 | profile: options.profile, |
| 104 | envFile: Option.getOrUndefined(options.envFile), |
| 105 | }), |
| 106 | ), |
| 107 | collect, |
| 108 | Effect.timeoutOption(Duration.seconds(15)), |
| 109 | Effect.catchCause((cause) => { |
| 110 | const suppressed = cause.reasons.some((reason) => { |
| 111 | const error = Cause.isFailReason(reason) |
| 112 | ? reason.error |
| 113 | : Cause.isDieReason(reason) |
| 114 | ? reason.defect |
| 115 | : undefined; |
| 116 | return isMissingProviderConfig(error); |
| 117 | }); |
| 118 | return suppressed |
| 119 | ? Effect.void |
| 120 | : Effect.fail( |
| 121 | AuthError.make({ |
| 122 | message: `Could not load auth providers from '${options.main}'.`, |
| 123 | cause, |
| 124 | }), |
| 125 | ); |
| 126 | }), |
| 127 | ); |
| 128 | } |
| 129 | return authProviders; |
| 130 | }, |
| 131 | Effect.provideService(SuppressMissingProviderConfig, true), |
| 132 | ); |
| 133 | |
| 134 | /** |
| 135 | * Collects auth registries. Uncached by default; {@link authRegistryLayer} |
| 136 | * memoizes them for its own lifetime. |
| 137 | */ |
| 138 | const AuthRegistry = Context.Reference<CollectAuthProviders>("Alchemy/Alchemist/AuthRegistry", { |
| 139 | defaultValue: () => collectAuthProvidersUncached, |
| 140 | }); |
| 141 | |
| 142 | /** |
| 143 | * Memoize auth registries for one command or API runtime: profile flows |
| 144 | * collect them repeatedly, and concurrent collections of the same project |
| 145 | * share one. Registries are built in this layer's scope, so a cached one |
| 146 | * never outlives the services its providers captured. Failures are not |
| 147 | * cached. |
| 148 | */ |
| 149 | export const authRegistryLayer = Layer.effect( |
| 150 | AuthRegistry, |
| 151 | Effect.map(Effect.scope, (scope) => |
| 152 | makeCachedFunction( |
| 153 | (options: CollectAuthProvidersOptions) => |
| 154 | collectAuthProvidersUncached(options).pipe(Scope.provide(scope)), |
| 155 | { |
| 156 | key: ([options]) => |
| 157 | JSON.stringify([options.main, Option.getOrNull(options.envFile), options.profile]), |
| 158 | }, |
| 159 | ), |
| 160 | ), |
| 161 | ); |
| 162 | |
| 163 | export const collectAuthProviders = (options: CollectAuthProvidersOptions) => |
| 164 | Effect.flatMap(AuthRegistry, (collect) => collect(options)); |