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, DEFAULT_API_BASE_URL } from "@distilled.cloud/turso/Credentials"; |
| 3 | import * as turso from "@distilled.cloud/turso/turso"; |
| 4 | import { lazyCredentials, orDieCredentialsUnavailable } from "alchemy/Auth/Resolve"; |
| 5 | import * as Context from "effect/Context"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import { TURSO_AUTH_PROVIDER_NAME, type TursoResolvedCredentials } from "./AuthProvider.ts"; |
| 11 | |
| 12 | export { |
| 13 | Credentials, |
| 14 | CredentialsFromEnv, |
| 15 | DEFAULT_API_BASE_URL, |
| 16 | } from "@distilled.cloud/turso/Credentials"; |
| 17 | |
| 18 | /** |
| 19 | * The Turso organization slug every org-scoped Platform API call targets. |
| 20 | * Holds an effect so the slug is resolved (and, when not configured, |
| 21 | * discovered from the API token) on first use, never at layer build. |
| 22 | */ |
| 23 | export class Organization extends Context.Service<Organization, Effect.Effect<string>>()( |
| 24 | "Turso.Organization", |
| 25 | ) {} |
| 26 | |
| 27 | /** The organization slug for the current Turso credentials. */ |
| 28 | export const organization = Effect.gen(function* () { |
| 29 | return yield* yield* Organization; |
| 30 | }); |
| 31 | |
| 32 | /** |
| 33 | * Discover the organization an API token acts on: the token's own |
| 34 | * organization when it is org-scoped, else the account's only organization. |
| 35 | */ |
| 36 | const discoverOrganization = Effect.gen(function* () { |
| 37 | const orgs = yield* turso.listOrganizations({}); |
| 38 | if (orgs.length === 1 && orgs[0]?.slug) { |
| 39 | return orgs[0].slug; |
| 40 | } |
| 41 | return yield* new ConfigError({ |
| 42 | message: |
| 43 | orgs.length === 0 |
| 44 | ? "Turso: this API token has no organizations." |
| 45 | : `Turso: this API token can access ${orgs.length} organizations (${orgs |
| 46 | .map((org) => org.slug) |
| 47 | .join(", ")}). Set TURSO_ORGANIZATION or configure the organization in your profile.`, |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | const make = ( |
| 52 | resolve: Effect.Effect<TursoResolvedCredentials, ConfigError>, |
| 53 | ): Effect.Effect<Context.Context<Credentials | Organization>, never, HttpClient.HttpClient> => |
| 54 | Effect.gen(function* () { |
| 55 | const http = yield* HttpClient.HttpClient; |
| 56 | const credentials = Effect.map(resolve, (creds) => ({ |
| 57 | apiKey: creds.apiToken, |
| 58 | apiBaseUrl: creds.apiBaseUrl, |
| 59 | })); |
| 60 | // `Organization` is typed as never failing, so a credentials failure dies |
| 61 | // here; the engine still reports it as `CredentialsRequired`. |
| 62 | const organization = yield* resolve.pipe( |
| 63 | Effect.orDie, |
| 64 | Effect.flatMap((creds) => |
| 65 | creds.organization !== undefined && creds.organization.length > 0 |
| 66 | ? Effect.succeed(creds.organization) |
| 67 | : discoverOrganization.pipe( |
| 68 | Effect.provideService(Credentials, credentials), |
| 69 | Effect.provideService(HttpClient.HttpClient, http), |
| 70 | orDieCredentialsUnavailable(TURSO_AUTH_PROVIDER_NAME), |
| 71 | ), |
| 72 | ), |
| 73 | Effect.cached, |
| 74 | ); |
| 75 | return Context.make(Credentials, credentials).pipe(Context.add(Organization, organization)); |
| 76 | }); |
| 77 | |
| 78 | /** |
| 79 | * Build Turso `Credentials` + `Organization` from an explicit API token. |
| 80 | * Useful for tests or when the caller already has a token in hand. |
| 81 | * |
| 82 | * @example |
| 83 | * ```ts |
| 84 | * Effect.provide(Turso.fromToken({ apiToken: "...", organization: "my-org" })) |
| 85 | * ``` |
| 86 | */ |
| 87 | export const fromToken = (input: { |
| 88 | apiToken: string | Redacted.Redacted<string>; |
| 89 | organization?: string; |
| 90 | apiBaseUrl?: string; |
| 91 | }) => |
| 92 | Effect.succeed<TursoResolvedCredentials>({ |
| 93 | type: "apiToken", |
| 94 | apiToken: typeof input.apiToken === "string" ? Redacted.make(input.apiToken) : input.apiToken, |
| 95 | apiBaseUrl: input.apiBaseUrl ?? DEFAULT_API_BASE_URL, |
| 96 | organization: input.organization, |
| 97 | source: { type: "stored" }, |
| 98 | }).pipe(make, Layer.effectContext); |
| 99 | |
| 100 | /** |
| 101 | * Build Turso `Credentials` + `Organization` that resolve through the |
| 102 | * Alchemy AuthProvider: environment credentials (`TURSO_API_TOKEN`) when |
| 103 | * present, otherwise the selected profile. Resolution is deferred until the |
| 104 | * first API call, so building the provider layers never needs a profile. |
| 105 | */ |
| 106 | export const fromAuthProvider = () => |
| 107 | Layer.effectContext( |
| 108 | lazyCredentials<TursoResolvedCredentials, ConfigError>()( |
| 109 | TURSO_AUTH_PROVIDER_NAME, |
| 110 | (creds) => creds, |
| 111 | ).pipe(Effect.flatMap(make)), |
| 112 | ); |