The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { ProfileStore } from "alchemy/Auth/Profile"; |
| 2 | import * as Config from "effect/Config"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import { CLOUDFLARE_AUTH_PROVIDER_NAME, CloudflareAuthConfigSchema } from "./Auth/AuthConfig.ts"; |
| 7 | |
| 8 | /** Fallback identity when local dev has no configured Cloudflare account. */ |
| 9 | export const LOCAL_ACCOUNT_ID = "00000000000000000000000000000000"; |
| 10 | |
| 11 | // Read identity only: expired OAuth credentials must not prevent local dev, |
| 12 | // and discovering an account must never authenticate or refresh a token. |
| 13 | export const localAccountId = Effect.gen(function* () { |
| 14 | const accountId = (yield* Config.String("CLOUDFLARE_ACCOUNT_ID").pipe( |
| 15 | Config.withDefault(""), |
| 16 | )).trim(); |
| 17 | if (accountId.length > 0) { |
| 18 | return accountId; |
| 19 | } |
| 20 | |
| 21 | const isCI = yield* Config.Boolean("CI").pipe(Config.withDefault(false)); |
| 22 | if (isCI) { |
| 23 | return LOCAL_ACCOUNT_ID; |
| 24 | } |
| 25 | |
| 26 | const profiles = Option.getOrUndefined(yield* Effect.serviceOption(ProfileStore)); |
| 27 | if (profiles === undefined) { |
| 28 | return LOCAL_ACCOUNT_ID; |
| 29 | } |
| 30 | |
| 31 | const currentProfile = yield* profiles.current; |
| 32 | const profile = yield* profiles.getProfile(currentProfile.name); |
| 33 | const cloudflareConfig = profile?.providers[CLOUDFLARE_AUTH_PROVIDER_NAME]; |
| 34 | if (cloudflareConfig === undefined) { |
| 35 | return LOCAL_ACCOUNT_ID; |
| 36 | } |
| 37 | |
| 38 | const config = yield* Schema.decodeUnknownEffect(CloudflareAuthConfigSchema)(cloudflareConfig); |
| 39 | if ("accountId" in config) { |
| 40 | return config.accountId; |
| 41 | } |
| 42 | |
| 43 | return LOCAL_ACCOUNT_ID; |
| 44 | }).pipe(Effect.orDie); |