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 } from "@distilled.cloud/prisma"; |
| 3 | import { deferUntilFirstUse, lazyCredentials } from "alchemy/Auth/Resolve"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import { PRISMA_AUTH_PROVIDER_NAME, type PrismaResolvedCredentials } from "../AuthProvider.ts"; |
| 7 | import { PrismaEnvironment } from "../PrismaEnvironment.ts"; |
| 8 | import { resolveBaseUrl } from "./BaseUrl.ts"; |
| 9 | |
| 10 | export { Credentials } from "@distilled.cloud/prisma"; |
| 11 | |
| 12 | const toConfigError = (cause: { readonly message: string }) => |
| 13 | ConfigError.make({ message: `Invalid Prisma Management API URL: ${cause.message}` }); |
| 14 | |
| 15 | /** |
| 16 | * Bridge an already-resolved {@link PrismaEnvironment} into the distilled |
| 17 | * `Credentials` service. Use this when the environment is provided by the |
| 18 | * caller (tests, standalone operation helpers). |
| 19 | */ |
| 20 | export const fromEnvironment = () => |
| 21 | Layer.effect( |
| 22 | Credentials, |
| 23 | Effect.gen(function* () { |
| 24 | const env = yield* PrismaEnvironment; |
| 25 | return Effect.succeed({ |
| 26 | apiToken: env.serviceToken, |
| 27 | apiBaseUrl: env.baseUrl, |
| 28 | }); |
| 29 | }), |
| 30 | ); |
| 31 | |
| 32 | /** |
| 33 | * Resolve Prisma credentials lazily from the configured auth profile. |
| 34 | * |
| 35 | * The `Credentials` service holds an *effect*, so nothing is resolved until |
| 36 | * the first API operation runs. That keeps `alchemy dev` — which only |
| 37 | * exercises local providers — from ever needing a Prisma token. |
| 38 | */ |
| 39 | export const fromAuthProvider = () => |
| 40 | Layer.effect( |
| 41 | Credentials, |
| 42 | Effect.gen(function* () { |
| 43 | const credentials = yield* lazyCredentials<PrismaResolvedCredentials, ConfigError>()( |
| 44 | PRISMA_AUTH_PROVIDER_NAME, |
| 45 | (creds) => creds.serviceToken, |
| 46 | ); |
| 47 | const baseUrl = yield* resolveBaseUrl.pipe( |
| 48 | Effect.mapError(toConfigError), |
| 49 | deferUntilFirstUse, |
| 50 | Effect.flatMap(Effect.cached), |
| 51 | ); |
| 52 | return Effect.all({ apiToken: credentials, apiBaseUrl: baseUrl }); |
| 53 | }), |
| 54 | ); |