The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ConfigError } from "@distilled.cloud/core/errors"; |
| 2 | import { Credentials, CredentialsFromEnv } from "@distilled.cloud/fly-io"; |
| 3 | import { lazyCredentials, orDieCredentialsUnavailable } from "alchemy/Auth/Resolve"; |
| 4 | import * as Output from "alchemy/Output"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Redacted from "effect/Redacted"; |
| 9 | import { FLY_AUTH_PROVIDER_NAME, type FlyResolvedCredentials } from "./AuthProvider.ts"; |
| 10 | |
| 11 | export { |
| 12 | Credentials, |
| 13 | CredentialsFromEnv, |
| 14 | credentials, |
| 15 | DEFAULT_API_BASE_URL, |
| 16 | normalizeApiBaseUrl, |
| 17 | type Config as CredentialsConfig, |
| 18 | } from "@distilled.cloud/fly-io"; |
| 19 | |
| 20 | /** |
| 21 | * Capture the deployment's org token as a named runtime Output. |
| 22 | * Resolve it during binding initialization so plan/diff retains |
| 23 | * profile-backed credentials rather than falling back to process env. |
| 24 | */ |
| 25 | export const bindFlyApiToken = (): Effect.Effect<void, never, Credentials> => |
| 26 | Effect.gen(function* () { |
| 27 | const token = globalThis.__ALCHEMY_RUNTIME__ |
| 28 | ? "" |
| 29 | : yield* Credentials.pipe( |
| 30 | Effect.flatMap((resolve) => resolve), |
| 31 | Effect.map((cfg) => Redacted.value(cfg.apiKey)), |
| 32 | // Binding initialization cannot fail; the engine reports this |
| 33 | // defect as `CredentialsRequired`. |
| 34 | orDieCredentialsUnavailable("Fly"), |
| 35 | ); |
| 36 | yield* Output.named(Output.asOutput(token), "FLY_API_TOKEN").asEffect().pipe(Effect.asVoid); |
| 37 | }); |
| 38 | |
| 39 | /** |
| 40 | * `Credentials` for the HTTP binding layers (`GetSecretHttp`, `ExecHttp`, …). |
| 41 | * |
| 42 | * Those layers are built in two places. Inside a stack (plan/deploy, or an |
| 43 | * Action) `providers()` has already resolved the profile-backed |
| 44 | * `Credentials`, and the binding must use them — a laptop deploy has no |
| 45 | * `FLY_API_TOKEN` in its env once the token lives in the Alchemy profile. |
| 46 | * Inside a deployed Machine there is no profile; `bindFlyApiToken` |
| 47 | * has already `RuntimeContext.set` the org token, which Platform copies |
| 48 | * into Machine env as `FLY_API_TOKEN`. So: reuse the ambient |
| 49 | * `Credentials` when present, otherwise read the env. |
| 50 | */ |
| 51 | export const CredentialsFromAmbientOrEnv: Layer.Layer<Credentials> = Layer.effect( |
| 52 | Credentials, |
| 53 | Effect.gen(function* () { |
| 54 | const ambient = yield* Effect.serviceOption(Credentials); |
| 55 | if (Option.isSome(ambient)) return ambient.value; |
| 56 | return yield* Credentials.pipe(Effect.provide(CredentialsFromEnv)); |
| 57 | }), |
| 58 | ); |
| 59 | |
| 60 | /** |
| 61 | * Build a `Credentials` layer that resolves Fly credentials via the current |
| 62 | * Alchemy profile, or directly from environment variables in CI. |
| 63 | * |
| 64 | * Maps onto `@distilled.cloud/fly-io`'s `{ apiKey, apiBaseUrl }` shape. |
| 65 | * Distilled's own `CredentialsFromEnv` also accepts `FLY_IO_API_KEY` as a |
| 66 | * fallback — Alchemy itself only reads `FLY_API_TOKEN`. |
| 67 | */ |
| 68 | export const fromAuthProvider = () => |
| 69 | Layer.effect( |
| 70 | Credentials, |
| 71 | lazyCredentials<FlyResolvedCredentials, ConfigError>()(FLY_AUTH_PROVIDER_NAME, (creds) => ({ |
| 72 | apiKey: creds.apiKey, |
| 73 | apiBaseUrl: creds.apiBaseUrl, |
| 74 | })), |
| 75 | ); |