@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 182 lines 6.41 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Option from "effect/Option";
3 import * as Ref from "effect/Ref";
4 import * as Alchemist from "../../../Alchemist/index.ts";
5 import { DEFAULT_PROFILE_NAME } from "../../../Auth/Profile.ts";
6 import { Interaction } from "../../../Interaction.ts";
7 import { isPromptCancellation } from "../errors.ts";
8
9 export const profileHub = Effect.fn(function* (options: {
10 envFile: Option.Option<string>;
11 main: string;
12 }) {
13 const envFile = Option.getOrUndefined(options.envFile);
14 const computeEntries = Alchemist.Profile.list().pipe(
15 Effect.map((profiles) =>
16 profiles.map((profile) => ({
17 name: profile.name,
18 isActive: profile.active,
19 isDefault: profile.name === DEFAULT_PROFILE_NAME,
20 })),
21 ),
22 );
23 const initialEntries = yield* computeEntries;
24 // The dashboard falls back to the last good listing when a reload fails.
25 const lastEntries = yield* Ref.make(initialEntries);
26 const refreshEntries = computeEntries.pipe(
27 Effect.tap((entries) => Ref.set(lastEntries, entries)),
28 );
29 const { runProfileDashboardSession } = yield* Effect.promise(
30 () => import("../../components/view/ProfileDashboard.tsx"),
31 );
32 // Warm the entrypoint module cache before the dashboard mounts: the user's
33 // stack module evaluates synchronously on the main thread and would freeze
34 // the credential spinner mid-frame. Like deploy's planning session, show a
35 // static (non-spinning) row that makes no motion promise. Failures are
36 // ignored here — `loadDetails` re-imports from the cache and surfaces them.
37 const cli = yield* Interaction;
38 yield* Effect.scoped(
39 Effect.gen(function* () {
40 yield* cli.progress({
41 label: "Importing stack module",
42 spinning: false,
43 });
44 // let the row paint before synchronous module evaluation starts
45 yield* Effect.sleep("1 millis");
46 yield* Alchemist.importStack(options.main).pipe(Effect.ignore);
47 }),
48 );
49
50 yield* runProfileDashboardSession({
51 entries: initialEntries,
52 selected: initialEntries.find(({ isActive }) => isActive)?.name,
53 loadDetails: (name) =>
54 Effect.gen(function* () {
55 const [profile, providers] = yield* Effect.all([
56 Alchemist.Profile.get({
57 name,
58 includeProviderStatus: true,
59 entrypoint: options.main,
60 envFile,
61 }),
62 Alchemist.Profile.providers({
63 profile: name,
64 entrypoint: options.main,
65 envFile,
66 }),
67 ]);
68 return {
69 providers: profile.providers.map((provider) => ({
70 name: provider.name,
71 method: provider.method,
72 status:
73 provider.status === "connected"
74 ? ("configured" as const)
75 : provider.status === "needs-reauth"
76 ? ("reauth" as const)
77 : provider.status === "needs-reconfigure"
78 ? ("reconfigure" as const)
79 : ("error" as const),
80 lines: [
81 ...provider.details.map(({ key, value }) => `${key}: ${value}`),
82 ...(provider.diagnostic ? [provider.diagnostic.message] : []),
83 ],
84 })),
85 available: providers.filter(({ connected }) => !connected).map(({ name }) => name),
86 };
87 }),
88 execute: (action) =>
89 Effect.gen(function* () {
90 let selected: string | undefined;
91 let message: string;
92 if (action.kind === "create") {
93 yield* Alchemist.Profile.create({ name: action.name });
94 selected = action.name;
95 message = `Created profile '${action.name}'.`;
96 } else if (action.kind === "rename") {
97 yield* Alchemist.Profile.rename({
98 name: action.name,
99 newName: action.newName,
100 });
101 selected = action.newName;
102 message = `Renamed '${action.name}' to '${action.newName}'.`;
103 } else {
104 yield* Alchemist.Profile.deleteProfile({ name: action.name });
105 message = `Deleted '${action.name}' and its credentials.`;
106 }
107 const entries = yield* refreshEntries;
108 return { ok: true, message, entries, selected };
109 }).pipe(
110 Effect.catch((error) =>
111 Effect.map(Ref.get(lastEntries), (entries) => ({
112 ok: false,
113 message: error.message,
114 entries,
115 })),
116 ),
117 ),
118 runFlow: (action, events) =>
119 Effect.gen(function* () {
120 if (action.kind === "refresh") {
121 yield* Alchemist.Profile.refresh({
122 profile: action.name,
123 providers: action.provider === undefined ? undefined : [action.provider],
124 entrypoint: options.main,
125 envFile,
126 }).pipe(
127 Alchemist.onEvent((event) =>
128 event.type === "provider.refresh.started"
129 ? events.onProviderStart(event.provider)
130 : Effect.void,
131 ),
132 );
133 return {
134 ok: true,
135 message:
136 action.provider === undefined
137 ? "Credentials refreshed."
138 : `${action.provider} credentials refreshed.`,
139 };
140 }
141
142 const outcomes: string[] = [];
143 for (const provider of action.remove) {
144 yield* Alchemist.Profile.removeProvider({
145 profile: action.name,
146 provider,
147 entrypoint: options.main,
148 envFile,
149 });
150 outcomes.push(`${provider} removed`);
151 }
152 for (const [kind, names] of [
153 ["add", action.add],
154 ["reconfigure", action.reconfigure],
155 ] as const) {
156 for (const provider of names) {
157 yield* Alchemist.Profile.configure({
158 profile: action.name,
159 provider,
160 entrypoint: options.main,
161 envFile,
162 action: kind,
163 });
164 outcomes.push(`${provider} ${kind === "add" ? "added" : "updated"}`);
165 }
166 }
167 return {
168 ok: true,
169 message: outcomes.length === 0 ? "No changes." : outcomes.join("; "),
170 };
171 }).pipe(
172 Effect.catch((error) =>
173 Effect.succeed(
174 isPromptCancellation(error)
175 ? { ok: true, message: "Cancelled." }
176 : { ok: false, message: error.message },
177 ),
178 ),
179 ),
180 reloadEntries: refreshEntries.pipe(Effect.catch(() => Ref.get(lastEntries))),
181 });
182 });