The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { CredentialsError, ResolvedCredentials } from "@distilled.cloud/aws/Credentials"; |
| 2 | import { lazyCredentials } from "alchemy/Auth/Resolve"; |
| 3 | import * as Config from "effect/Config"; |
| 4 | import * as Context from "effect/Context"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import { |
| 10 | AWS_AUTH_PROVIDER_NAME, |
| 11 | LOCAL_ACCOUNT_ID, |
| 12 | type AwsResolvedCredentials, |
| 13 | } from "./AuthProvider.ts"; |
| 14 | |
| 15 | export const AWS_PROFILE = Config.String("AWS_PROFILE").pipe(Config.withDefault("default")); |
| 16 | |
| 17 | export const AWS_REGION = Config.String("AWS_REGION"); |
| 18 | export const AWS_ACCOUNT_ID = Config.String("AWS_ACCOUNT_ID"); |
| 19 | export const AWS_ACCESS_KEY_ID = Config.String("AWS_ACCESS_KEY_ID"); |
| 20 | export const AWS_SECRET_ACCESS_KEY = Config.Redacted("AWS_SECRET_ACCESS_KEY"); |
| 21 | export const AWS_SESSION_TOKEN = Config.Redacted("AWS_SESSION_TOKEN"); |
| 22 | |
| 23 | export type AccountID = string; |
| 24 | export type RegionID = string; |
| 25 | |
| 26 | export class FailedToGetAccount extends Data.TaggedError("AWS::Environment::FailedToGetAccount")<{ |
| 27 | message: string; |
| 28 | cause: Error; |
| 29 | }> {} |
| 30 | |
| 31 | /** |
| 32 | * Fully-resolved AWS environment for a stack. Mirrors `CloudflareEnvironment`: |
| 33 | * one Context.Service that holds account, region, credentials, endpoint, and |
| 34 | * (optionally) the SSO profile name. |
| 35 | * |
| 36 | * `credentials` is held as an Effect so callers can refresh on each access |
| 37 | * (SSO sessions expire). The Effect itself is constructed once when this |
| 38 | * service is built; resolving it lazily preserves @distilled.cloud/aws's |
| 39 | * existing `Credentials` semantics. |
| 40 | */ |
| 41 | export interface AWSEnvironmentShape { |
| 42 | accountId: AccountID; |
| 43 | region: RegionID; |
| 44 | credentials: Effect.Effect<ResolvedCredentials, CredentialsError>; |
| 45 | endpoint?: string; |
| 46 | profile?: string; |
| 47 | } |
| 48 | |
| 49 | export class AWSEnvironment extends Context.Service< |
| 50 | AWSEnvironment, |
| 51 | Effect.Effect<AWSEnvironmentShape> |
| 52 | >()("AWS::Environment") { |
| 53 | static current = AWSEnvironment.use((env) => env); |
| 54 | /** |
| 55 | * Whether this environment is the floci emulator |
| 56 | * (dummy account {@link LOCAL_ACCOUNT_ID}). A set `endpoint` is not |
| 57 | * enough: `AWS_ENDPOINT_URL` and explicit endpoint overrides also |
| 58 | * populate it on real-account credentials. |
| 59 | */ |
| 60 | static isLocalEmulator = Effect.map( |
| 61 | AWSEnvironment.current, |
| 62 | (env) => env.accountId === LOCAL_ACCOUNT_ID, |
| 63 | ); |
| 64 | readonly kind = "Environment" as const; |
| 65 | } |
| 66 | |
| 67 | /** @see {@link AWSEnvironment.isLocalEmulator} */ |
| 68 | export const isLocalEmulator = AWSEnvironment.isLocalEmulator; |
| 69 | |
| 70 | export const Default = Layer.effect( |
| 71 | AWSEnvironment, |
| 72 | // Provider layers are built on every run — before any profile may be |
| 73 | // configured — so nothing may resolve at construction. A dev run with |
| 74 | // zero AWS credentials builds this layer too (the ambient environment |
| 75 | // is the emulator; only `Alchemy.remote()` rows ever need it), so the |
| 76 | // profile/CI precedence is captured here and evaluated on first use. |
| 77 | // Suspended: `AuthProvider.ts` imports this module, so its exports are not |
| 78 | // initialized yet when this module evaluates. |
| 79 | Effect.suspend(() => |
| 80 | // `AWSEnvironment` is typed as never failing, so a credentials failure |
| 81 | // dies here; the engine still reports it as `CredentialsRequired`. |
| 82 | lazyCredentials<AwsResolvedCredentials>()(AWS_AUTH_PROVIDER_NAME, (creds) => creds).pipe( |
| 83 | Effect.map(Effect.orDie), |
| 84 | ), |
| 85 | ), |
| 86 | ).pipe(Layer.orDie); |
| 87 | |
| 88 | /** |
| 89 | * The AWS environment for a provider or state-store layer: an |
| 90 | * `AWSEnvironment` the caller provided from outside wins, otherwise |
| 91 | * {@link Default} (profile / CI / ambient). Layers that build on this instead |
| 92 | * of `Default` can be pointed at another account, region or credential |
| 93 | * source with `layer.pipe(Layer.provide(environment))`. |
| 94 | */ |
| 95 | export const providedOrDefault = () => |
| 96 | // A fresh layer per call: layers are memoized by identity, so a shared |
| 97 | // instance built once with nothing provided (e.g. by a layer used to derive |
| 98 | // the provided environment) would be reused here and shadow it. |
| 99 | AWSEnvironment.pipe( |
| 100 | Effect.serviceOption, |
| 101 | Effect.map((provided) => |
| 102 | Option.isSome(provided) ? Layer.succeed(AWSEnvironment, provided.value) : Default, |
| 103 | ), |
| 104 | Layer.unwrap, |
| 105 | ); |