The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Config from "effect/Config"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | import * as Interaction from "../Interaction.ts"; |
| 5 | import { AuthError } from "./AuthProvider.ts"; |
| 6 | |
| 7 | export const getEnv = (key: string) => |
| 8 | Config.option(Config.String(key)).pipe( |
| 9 | Effect.map(Option.getOrUndefined), |
| 10 | Effect.mapError((cause) => |
| 11 | AuthError.make({ |
| 12 | message: `Could not read optional env: ${key}`, |
| 13 | cause, |
| 14 | }), |
| 15 | ), |
| 16 | ); |
| 17 | |
| 18 | export const getEnvRequired = (key: string) => |
| 19 | Config.String(key).pipe( |
| 20 | Effect.mapError((cause) => AuthError.make({ message: `Missing required env: ${key}`, cause })), |
| 21 | ); |
| 22 | |
| 23 | export const getEnvRedacted = (key: string) => |
| 24 | Config.option(Config.Redacted(key)).pipe( |
| 25 | Effect.map(Option.getOrUndefined), |
| 26 | Effect.mapError((cause) => |
| 27 | AuthError.make({ |
| 28 | message: `Could not read optional env: ${key}`, |
| 29 | cause, |
| 30 | }), |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | export const getEnvRedactedRequired = (key: string) => |
| 35 | Config.Redacted(key).pipe( |
| 36 | Effect.mapError((cause) => AuthError.make({ message: `Missing required env: ${key}`, cause })), |
| 37 | ); |
| 38 | |
| 39 | /** Turn a cancelled or unavailable prompt into an `AuthError`; other errors pass through. */ |
| 40 | export const mapPromptCancellation = <A, E, R>(self: Effect.Effect<A, E, R>) => |
| 41 | self.pipe( |
| 42 | Effect.catchIf( |
| 43 | (cause): cause is Extract<E, Interaction.InteractionError> => |
| 44 | cause instanceof Interaction.TerminalCancelled || |
| 45 | cause instanceof Interaction.NonInteractiveTerminal, |
| 46 | (cause) => |
| 47 | Effect.fail( |
| 48 | AuthError.make({ |
| 49 | message: cause._tag === "TerminalCancelled" ? "User cancelled prompt" : cause.message, |
| 50 | cause, |
| 51 | }), |
| 52 | ), |
| 53 | ), |
| 54 | ); |