@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, 03:37 UTC

main
ts 69 lines 2.43 KB
Raw
1 import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
2 import { readCodexAuthDelivery } from "@t3tools/shared/codexAuthHandoff";
3 import { msg } from "gt-react";
4
5 import { toastManager } from "./components/ui/toast";
6 import { m } from "./i18n/messages";
7 import { appAtomRegistry } from "./rpc/atomRegistry";
8 import { environmentPresentations } from "./state/presentation";
9 import { serverEnvironment } from "./state/server";
10
11 type ProviderAuthDelivery = NonNullable<ReturnType<typeof readCodexAuthDelivery>>;
12
13 /**
14 * Remove the code before the router, tracing, or welcome screen sees the incoming address,
15 * then hand it to its environment once that environment connects. Hosted web receives only
16 * the one-time code; the selected environment verifies and stores tokens.
17 */
18 export function startProviderAuthDelivery(): void {
19 if (!window.location.hash.startsWith("#codex-auth=")) return;
20 const delivery = readCodexAuthDelivery(window.location.href);
21 window.history.replaceState(
22 window.history.state,
23 "",
24 delivery?.returnUrl ?? `${window.location.pathname}${window.location.search}`,
25 );
26 if (!delivery) return;
27
28 const presentationAtom = environmentPresentations.presentationAtom(delivery.environmentId);
29 if (appAtomRegistry.get(presentationAtom)?.connection.phase === "connected") {
30 void completeProviderAuth(delivery);
31 return;
32 }
33 const unsubscribe = appAtomRegistry.subscribe(presentationAtom, (presentation) => {
34 if (presentation?.connection.phase !== "connected") return;
35 unsubscribe();
36 void completeProviderAuth(delivery);
37 });
38 }
39
40 async function completeProviderAuth(delivery: ProviderAuthDelivery): Promise<void> {
41 try {
42 const result = await runAtomCommand(
43 appAtomRegistry,
44 serverEnvironment.completeProviderAuth,
45 {
46 environmentId: delivery.environmentId,
47 input: {
48 instanceId: delivery.instanceId,
49 flowId: delivery.flowId,
50 callbackUrl: delivery.callbackUrl,
51 },
52 },
53 { reportFailure: false },
54 );
55 if (result._tag === "Failure") {
56 toastManager.add({
57 type: "error",
58 title: m(msg("ChatGPT sign-in couldn't finish")),
59 description: m(msg("Return to the provider and try again.")),
60 });
61 }
62 } catch {
63 toastManager.add({
64 type: "error",
65 title: m(msg("ChatGPT sign-in couldn't finish")),
66 description: m(msg("Reconnect to the environment and try again.")),
67 });
68 }
69 }