The best way to control Claude Code, Codex and more from any computer or phone

main
ts 119 lines 3.8 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Predicate from "effect/Predicate";
3 import * as Result from "effect/Result";
4 import {
5 type AuthError,
6 reconfigureHint,
7 type AuthProviders,
8 type ProviderDetailLine,
9 } from "./AuthProvider.ts";
10 import { ProfileStore, reloadProviderConfig, type ProviderConfig } from "./Profile.ts";
11
12 /**
13 * What a connected provider looks like right now. Establishing this means
14 * decoding the stored config and asking the provider to describe itself,
15 * either of which can fail — which is the point: a profile listing has to
16 * show a broken connection rather than fail because of one.
17 */
18 export interface ProviderConnection {
19 readonly name: string;
20 /** How credentials were supplied (`oauth`, `api-token`, …). */
21 readonly method: string;
22 readonly status: "connected" | "needs-reauth" | "needs-reconfigure" | "invalid" | "unavailable";
23 readonly details: ReadonlyArray<ProviderDetailLine>;
24 /** Present whenever `status` is anything but `connected`. */
25 readonly diagnostic?: {
26 readonly severity: "warning" | "error";
27 readonly code: string;
28 readonly message: string;
29 };
30 }
31
32 const broken = (
33 name: string,
34 method: string,
35 severity: "warning" | "error",
36 code: string,
37 message: string,
38 ): ProviderConnection => ({
39 name,
40 method,
41 status: severity === "warning" ? "needs-reauth" : "invalid",
42 details: [],
43 diagnostic: { severity, code, message },
44 });
45
46 /**
47 * Resolve one stored provider's live connection status. Never fails: every
48 * way this can go wrong is reported as a `status` + `diagnostic` so a listing
49 * can show the whole profile.
50 */
51 export const inspectProvider = Effect.fn("inspectProvider")(function* (
52 profile: string,
53 name: string,
54 config: ProviderConfig,
55 registered: AuthProviders["Service"],
56 updateConfig?: (config: ProviderConfig) => Effect.Effect<void, AuthError>,
57 ) {
58 const method = config.method ?? "unknown";
59 const provider = registered[name];
60 if (provider === undefined) {
61 return {
62 name,
63 method,
64 status: "unavailable",
65 details: [],
66 diagnostic: {
67 severity: "warning",
68 code: "provider.unregistered",
69 message: `Provider '${name}' is not registered. Install the package that provides it.`,
70 },
71 } satisfies ProviderConnection;
72 }
73
74 // A deliberately empty migrated document records that the provider was
75 // connected in v0 while making no claim that its obsolete credentials are
76 // usable. Keep that distinct from malformed provider-owned values.
77 if (config.method === undefined) {
78 return {
79 name,
80 method,
81 status: "needs-reconfigure",
82 details: [],
83 diagnostic: {
84 severity: "warning",
85 code: "provider.needs-reconfigure",
86 message: `Provider '${name}' needs to be reconfigured. ${reconfigureHint(name, profile)}`,
87 },
88 } satisfies ProviderConnection;
89 }
90
91 const decoded = yield* Effect.result(provider.decodeConfig(profile, config));
92 if (Result.isFailure(decoded)) {
93 return broken(name, method, "error", "provider.invalid-config", decoded.failure.message);
94 }
95
96 const store = yield* ProfileStore;
97 const details = yield* Effect.result(
98 provider.details(profile, reloadProviderConfig(store, provider, profile), updateConfig),
99 );
100 if (Result.isSuccess(details)) {
101 return {
102 name,
103 method,
104 status: "connected",
105 details: details.success.lines,
106 } satisfies ProviderConnection;
107 }
108
109 // A provider that can no longer prove who it is has expired credentials,
110 // which the user can fix by re-authenticating — not a broken config.
111 const needsReauth = Predicate.isTagged("NeedsReauth")(details.failure);
112 return broken(
113 name,
114 method,
115 needsReauth ? "warning" : "error",
116 needsReauth ? "provider.needs-reauth" : "provider.details-failed",
117 details.failure.message,
118 );
119 });