The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import * as Redacted from "effect/Redacted"; |
| 4 | import { AuthError } from "../Auth/AuthProvider.ts"; |
| 5 | import { CredentialsStore } from "../Auth/Credentials.ts"; |
| 6 | import { inspectProvider, type ProviderConnection } from "../Auth/Inspect.ts"; |
| 7 | import { withProfileCredentialsLock } from "../Auth/Lock.ts"; |
| 8 | import { ProfileStore, reloadProviderConfig } from "../Auth/Profile.ts"; |
| 9 | import { emit } from "../Events.ts"; |
| 10 | import type { Interaction } from "../Interaction.ts"; |
| 11 | import { collectAuthProviders } from "./AuthRegistry.ts"; |
| 12 | import { DEFAULT_ENTRYPOINT, type Target } from "./Entrypoint.ts"; |
| 13 | import { AlchemistInvalidInput, AlchemistNotFound } from "./Errors.ts"; |
| 14 | |
| 15 | /** Which project/profile pair a provider-scoped operation resolves against. */ |
| 16 | export interface ProviderContext extends Target { |
| 17 | readonly profile: string; |
| 18 | } |
| 19 | |
| 20 | export interface ProfileSummary { |
| 21 | readonly name: string; |
| 22 | readonly active: boolean; |
| 23 | readonly providers: ReadonlyArray<{ |
| 24 | readonly name: string; |
| 25 | readonly method: string; |
| 26 | }>; |
| 27 | } |
| 28 | |
| 29 | export type { ProviderConnection } from "../Auth/Inspect.ts"; |
| 30 | |
| 31 | export interface ProfileSnapshot { |
| 32 | readonly name: string; |
| 33 | readonly active: boolean; |
| 34 | readonly providers: ReadonlyArray<ProviderConnection>; |
| 35 | } |
| 36 | |
| 37 | export interface ConfigureField { |
| 38 | readonly name: string; |
| 39 | readonly label: string; |
| 40 | readonly secret: boolean; |
| 41 | readonly required: boolean; |
| 42 | readonly description?: string; |
| 43 | readonly placeholder?: string; |
| 44 | } |
| 45 | |
| 46 | export interface ConfigureMethod { |
| 47 | readonly method: string; |
| 48 | readonly label: string; |
| 49 | readonly fields: ReadonlyArray<ConfigureField>; |
| 50 | } |
| 51 | |
| 52 | export interface AuthProviderDescriptor { |
| 53 | readonly name: string; |
| 54 | readonly connected: boolean; |
| 55 | readonly configureMethods: ReadonlyArray<ConfigureMethod>; |
| 56 | readonly supportsRefresh: boolean; |
| 57 | readonly supportsLogout: boolean; |
| 58 | } |
| 59 | |
| 60 | export interface ConfigureInput extends ProviderContext { |
| 61 | readonly provider: string; |
| 62 | readonly action: "add" | "reconfigure"; |
| 63 | readonly method?: string; |
| 64 | readonly values?: Readonly<Record<string, Redacted.Redacted<string>>>; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Every auth provider reachable from the given project: the built-ins plus |
| 69 | * whatever the user's stack module registers. |
| 70 | */ |
| 71 | const registry = (input: ProviderContext) => |
| 72 | collectAuthProviders({ |
| 73 | main: input.entrypoint ?? DEFAULT_ENTRYPOINT, |
| 74 | envFile: Option.fromNullishOr(input.envFile), |
| 75 | profile: input.profile, |
| 76 | }); |
| 77 | |
| 78 | /** The effective profile and how it was selected. */ |
| 79 | export const current = Effect.fn("Alchemist.profile.current")(function* () { |
| 80 | return yield* (yield* ProfileStore).current; |
| 81 | }); |
| 82 | |
| 83 | /** Every profile with its connected providers, active profile first. */ |
| 84 | export const list = Effect.fn("Alchemist.profile.list")(function* () { |
| 85 | const profiles = yield* ProfileStore; |
| 86 | const [manifest, selected] = yield* Effect.all([profiles.readManifest, profiles.current]); |
| 87 | return Object.entries(manifest.profiles) |
| 88 | .sort(([a], [b]) => (a === "default" ? -1 : b === "default" ? 1 : a.localeCompare(b))) |
| 89 | .map(([name, profile]): ProfileSummary => ({ |
| 90 | name, |
| 91 | active: name === selected.name, |
| 92 | providers: Object.entries(profile.providers) |
| 93 | .sort(([a], [b]) => a.localeCompare(b)) |
| 94 | .map(([name, config]) => ({ |
| 95 | name, |
| 96 | method: config.method ?? "unknown", |
| 97 | })), |
| 98 | })); |
| 99 | }); |
| 100 | |
| 101 | /** One profile with the live status of each connected provider. */ |
| 102 | export const get = Effect.fn("Alchemist.profile.get")(function* (input: { |
| 103 | readonly name: string; |
| 104 | readonly includeProviderStatus?: boolean; |
| 105 | readonly entrypoint?: string; |
| 106 | readonly envFile?: string; |
| 107 | }) { |
| 108 | const includeProviderStatus = input.includeProviderStatus ?? true; |
| 109 | const profiles = yield* ProfileStore; |
| 110 | const [profile, selected] = yield* Effect.all([ |
| 111 | profiles.getProfile(input.name), |
| 112 | profiles.current, |
| 113 | ]); |
| 114 | if (profile === undefined) { |
| 115 | return yield* new AlchemistNotFound({ kind: "profile", id: input.name }); |
| 116 | } |
| 117 | // Skipping the status probe is what makes `profile list` fast: it |
| 118 | // reaches no provider APIs, so every connection reads as connected. |
| 119 | const registered = includeProviderStatus |
| 120 | ? yield* registry({ |
| 121 | profile: input.name, |
| 122 | entrypoint: input.entrypoint, |
| 123 | envFile: input.envFile, |
| 124 | }) |
| 125 | : {}; |
| 126 | const entries = Object.entries(profile.providers).sort(([a], [b]) => a.localeCompare(b)); |
| 127 | return { |
| 128 | name: input.name, |
| 129 | active: selected.name === input.name, |
| 130 | providers: yield* Effect.forEach( |
| 131 | entries, |
| 132 | ([provider, config]): Effect.Effect<ProviderConnection, never, Interaction> => |
| 133 | includeProviderStatus |
| 134 | ? inspectProvider(input.name, provider, config, registered, (updated) => |
| 135 | profiles.setProviderConfig(input.name, provider, updated).pipe( |
| 136 | Effect.mapError((cause) => |
| 137 | AuthError.make({ |
| 138 | message: `Could not persist repaired ${provider} credentials for profile '${input.name}'.`, |
| 139 | cause, |
| 140 | }), |
| 141 | ), |
| 142 | ), |
| 143 | ).pipe(Effect.provideService(ProfileStore, profiles)) |
| 144 | : Effect.succeed({ |
| 145 | name: provider, |
| 146 | method: config.method ?? "unknown", |
| 147 | status: "connected", |
| 148 | details: [], |
| 149 | }), |
| 150 | ), |
| 151 | } satisfies ProfileSnapshot; |
| 152 | }); |
| 153 | |
| 154 | export const create = Effect.fn("Alchemist.profile.create")(function* (input: { |
| 155 | readonly name: string; |
| 156 | }) { |
| 157 | yield* (yield* ProfileStore).createProfile(input.name); |
| 158 | return yield* get({ name: input.name }); |
| 159 | }); |
| 160 | |
| 161 | export const rename = Effect.fn("Alchemist.profile.rename")(function* (input: { |
| 162 | readonly name: string; |
| 163 | readonly newName: string; |
| 164 | }) { |
| 165 | yield* (yield* ProfileStore).renameProfile(input.name, input.newName); |
| 166 | return yield* get({ name: input.newName }); |
| 167 | }); |
| 168 | |
| 169 | /** Delete a profile and every credential stored for it. */ |
| 170 | export const deleteProfile = Effect.fn("Alchemist.profile.delete")(function* (input: { |
| 171 | readonly name: string; |
| 172 | }) { |
| 173 | const profiles = yield* ProfileStore; |
| 174 | const credentials = yield* CredentialsStore; |
| 175 | return yield* withProfileCredentialsLock( |
| 176 | input.name, |
| 177 | Effect.gen(function* () { |
| 178 | const deleted = yield* profiles.deleteProfile(input.name); |
| 179 | if (!deleted) { |
| 180 | return yield* new AlchemistNotFound({ kind: "profile", id: input.name }); |
| 181 | } |
| 182 | yield* credentials.deleteProfile(input.name); |
| 183 | return { name: input.name, credentialsDeleted: true } as const; |
| 184 | }), |
| 185 | ); |
| 186 | }); |
| 187 | |
| 188 | /** Every registered auth provider and how it can be configured. */ |
| 189 | export const providers = Effect.fn("Alchemist.profile.providers")(function* (input: { |
| 190 | readonly profile?: string; |
| 191 | readonly entrypoint?: string; |
| 192 | readonly envFile?: string; |
| 193 | }) { |
| 194 | const profiles = yield* ProfileStore; |
| 195 | const profile = input.profile ?? (yield* profiles.current).name; |
| 196 | const stored = yield* profiles.ensureProfile(profile); |
| 197 | const registered = yield* registry({ ...input, profile }); |
| 198 | return Object.values(registered) |
| 199 | .sort((a, b) => a.name.localeCompare(b.name)) |
| 200 | .map((provider): AuthProviderDescriptor => ({ |
| 201 | name: provider.name, |
| 202 | connected: provider.name in stored.providers, |
| 203 | configureMethods: (provider.configureMethods ?? []).map((method) => ({ |
| 204 | method: method.method, |
| 205 | label: method.method, |
| 206 | fields: method.fields.map((field) => ({ |
| 207 | name: field.name, |
| 208 | label: field.label, |
| 209 | secret: field.secret ?? false, |
| 210 | required: !(field.optional ?? false), |
| 211 | description: field.description, |
| 212 | placeholder: field.placeholder, |
| 213 | })), |
| 214 | })), |
| 215 | supportsRefresh: true, |
| 216 | supportsLogout: true, |
| 217 | })); |
| 218 | }); |
| 219 | |
| 220 | /** The configure methods (and fields) one provider accepts. */ |
| 221 | export const configureForm = Effect.fn("Alchemist.profile.configureForm")(function* (input: { |
| 222 | readonly profile: string; |
| 223 | readonly provider: string; |
| 224 | readonly method?: string; |
| 225 | }) { |
| 226 | const registered = yield* providers({ profile: input.profile }); |
| 227 | const provider = registered.find(({ name }) => name === input.provider); |
| 228 | if (provider === undefined) { |
| 229 | return yield* new AlchemistNotFound({ kind: "provider", id: input.provider }); |
| 230 | } |
| 231 | return input.method === undefined |
| 232 | ? provider.configureMethods |
| 233 | : provider.configureMethods.filter(({ method }) => method === input.method); |
| 234 | }); |
| 235 | |
| 236 | /** |
| 237 | * Connect or reconfigure a provider in a profile. Reported through |
| 238 | * the `provider.configure.started` event; interactive providers drive |
| 239 | * their own prompts. |
| 240 | */ |
| 241 | export const configure = Effect.fn("Alchemist.profile.configure")(function* ( |
| 242 | input: ConfigureInput, |
| 243 | ) { |
| 244 | yield* emit({ |
| 245 | type: "provider.configure.started", |
| 246 | provider: input.provider, |
| 247 | }); |
| 248 | const profiles = yield* ProfileStore; |
| 249 | const stored = yield* profiles.ensureProfile(input.profile); |
| 250 | const provider = (yield* registry(input))[input.provider]; |
| 251 | if (provider === undefined) { |
| 252 | return yield* new AlchemistInvalidInput({ |
| 253 | field: "provider", |
| 254 | message: `Auth provider '${input.provider}' is not registered. Install the package that provides it.`, |
| 255 | }); |
| 256 | } |
| 257 | const connected = input.provider in stored.providers; |
| 258 | if ((input.action === "add" && connected) || (input.action === "reconfigure" && !connected)) { |
| 259 | return yield* new AlchemistInvalidInput({ |
| 260 | field: "provider", |
| 261 | message: `Provider '${input.provider}' is ${connected ? "already" : "not"} connected.`, |
| 262 | }); |
| 263 | } |
| 264 | const config = |
| 265 | input.method !== undefined && input.values !== undefined && provider.configureWith !== undefined |
| 266 | ? yield* provider.configureWith(input.profile, { |
| 267 | method: input.method, |
| 268 | values: Object.fromEntries( |
| 269 | Object.entries(input.values).map(([key, value]) => [key, Redacted.value(value)]), |
| 270 | ), |
| 271 | }) |
| 272 | : yield* provider.configure( |
| 273 | input.profile, |
| 274 | connected |
| 275 | ? yield* provider |
| 276 | .decodeConfig(input.profile, stored.providers[input.provider]!) |
| 277 | .pipe(Effect.orElseSucceed(() => undefined)) |
| 278 | : undefined, |
| 279 | ); |
| 280 | yield* profiles.setProviderConfig(input.profile, input.provider, config); |
| 281 | yield* emit({ |
| 282 | type: "provider.configure.completed", |
| 283 | provider: input.provider, |
| 284 | }); |
| 285 | return yield* get({ |
| 286 | name: input.profile, |
| 287 | entrypoint: input.entrypoint, |
| 288 | envFile: input.envFile, |
| 289 | }); |
| 290 | }); |
| 291 | |
| 292 | /** Log a provider out and disconnect it from the profile. */ |
| 293 | export const removeProvider = Effect.fn("Alchemist.profile.removeProvider")(function* ( |
| 294 | input: ProviderContext & { |
| 295 | readonly provider: string; |
| 296 | readonly logout?: boolean; |
| 297 | }, |
| 298 | ) { |
| 299 | const profiles = yield* ProfileStore; |
| 300 | const stored = yield* profiles.ensureProfile(input.profile); |
| 301 | const config = stored.providers[input.provider]; |
| 302 | if (config === undefined) { |
| 303 | return yield* new AlchemistNotFound({ kind: "provider", id: input.provider }); |
| 304 | } |
| 305 | const provider = (yield* registry(input))[input.provider]; |
| 306 | let logout: "completed" | "skipped-invalid-config" | "unavailable" = "unavailable"; |
| 307 | if (provider !== undefined) { |
| 308 | const decoded = yield* provider.decodeConfig(input.profile, config).pipe(Effect.option); |
| 309 | if (Option.isSome(decoded)) { |
| 310 | if (input.logout ?? true) yield* provider.logout(input.profile, decoded.value); |
| 311 | logout = "completed"; |
| 312 | } else logout = "skipped-invalid-config"; |
| 313 | } |
| 314 | yield* profiles.deleteProviderConfig(input.profile, input.provider); |
| 315 | return { profile: input.profile, provider: input.provider, logout }; |
| 316 | }); |
| 317 | |
| 318 | /** |
| 319 | * Re-run login for connected providers without reconfiguring them. Each |
| 320 | * provider is reported as the `provider.refresh.started` event. |
| 321 | */ |
| 322 | export const refresh = Effect.fn("Alchemist.profile.refresh")(function* ( |
| 323 | input: ProviderContext & { |
| 324 | readonly providers?: ReadonlyArray<string>; |
| 325 | }, |
| 326 | ) { |
| 327 | const profiles = yield* ProfileStore; |
| 328 | const stored = yield* profiles.ensureProfile(input.profile); |
| 329 | const registered = yield* registry(input); |
| 330 | const connected = Object.keys(stored.providers).sort(); |
| 331 | const requested: Array<string> = []; |
| 332 | for (const requestedName of input.providers ?? []) { |
| 333 | // `--provider aws` resolves to the connected `AWS`. |
| 334 | const name = connected.find((name) => name.toLowerCase() === requestedName.toLowerCase()); |
| 335 | if (name === undefined || registered[name] === undefined) { |
| 336 | const available = connected.filter((name) => registered[name] !== undefined); |
| 337 | return yield* new AlchemistInvalidInput({ |
| 338 | field: "providers", |
| 339 | message: |
| 340 | `Provider '${requestedName}' is not connected to profile '${input.profile}'. ` + |
| 341 | (available.length === 0 |
| 342 | ? "No providers are connected." |
| 343 | : `Connected providers: ${available.join(", ")}.`), |
| 344 | }); |
| 345 | } |
| 346 | if (!requested.includes(name)) requested.push(name); |
| 347 | } |
| 348 | if (requested.length === 0) requested.push(...connected); |
| 349 | for (const name of requested) { |
| 350 | const config = stored.providers[name]; |
| 351 | const provider = registered[name]; |
| 352 | if (config === undefined || provider === undefined) { |
| 353 | return yield* new AlchemistInvalidInput({ |
| 354 | field: "providers", |
| 355 | message: `Provider '${name}' is not connected or registered.`, |
| 356 | }); |
| 357 | } |
| 358 | yield* emit({ type: "provider.refresh.started", provider: name }); |
| 359 | const refreshed = yield* provider.login( |
| 360 | input.profile, |
| 361 | yield* provider.decodeConfig(input.profile, config), |
| 362 | (updated) => |
| 363 | profiles.setProviderConfig(input.profile, name, updated).pipe( |
| 364 | Effect.mapError((cause) => |
| 365 | AuthError.make({ |
| 366 | message: `Could not persist refreshed ${name} credentials for profile '${input.profile}'.`, |
| 367 | cause, |
| 368 | }), |
| 369 | ), |
| 370 | ), |
| 371 | reloadProviderConfig(profiles, provider, input.profile), |
| 372 | ); |
| 373 | if (refreshed !== undefined) { |
| 374 | yield* profiles.setProviderConfig(input.profile, name, refreshed); |
| 375 | } |
| 376 | yield* emit({ type: "provider.refresh.completed", provider: name }); |
| 377 | } |
| 378 | return yield* get({ |
| 379 | name: input.profile, |
| 380 | entrypoint: input.entrypoint, |
| 381 | envFile: input.envFile, |
| 382 | }); |
| 383 | }); |