@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, 04:45 UTC

main
ts 54 lines 2 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as Schema from "effect/Schema";
3 import * as Option from "effect/Option";
4 import {
5 receiveCodexAuthCallback,
6 cancelCodexAuthCallback,
7 CodexAuthCallbackError,
8 } from "../../app/CodexAuthCallback.ts";
9 import * as ElectronShell from "../../electron/ElectronShell.ts";
10 import * as ElectronWindow from "../../electron/ElectronWindow.ts";
11 import * as DesktopIpc from "../DesktopIpc.ts";
12 import * as IpcChannels from "../channels.ts";
13
14 const Request = Schema.String.check(Schema.isMaxLength(16_384));
15
16 export const receiveProviderAuthCallback = DesktopIpc.makeIpcMethod({
17 channel: IpcChannels.RECEIVE_PROVIDER_AUTH_CALLBACK_CHANNEL,
18 payload: Request,
19 result: Schema.String,
20 handler: Effect.fn("desktop.ipc.providerAuth.receive")(function* (authorizationUrl) {
21 const shell = yield* ElectronShell.ElectronShell;
22 const windows = yield* ElectronWindow.ElectronWindow;
23 const context = yield* Effect.context<ElectronShell.ElectronShell>();
24 const runPromise = Effect.runPromiseWith(context);
25 const callbackUrl = yield* Effect.tryPromise({
26 try: (signal) =>
27 receiveCodexAuthCallback(
28 authorizationUrl,
29 (url) => runPromise(shell.openExternal(url)),
30 undefined,
31 signal,
32 ),
33 catch: () =>
34 new CodexAuthCallbackError({
35 detail:
36 "Could not receive ChatGPT sign-in on this computer. Try again or paste the redirect URL.",
37 }),
38 });
39 const window = yield* windows.currentMainOrFirst;
40 if (Option.isSome(window)) yield* windows.reveal(window.value);
41 return callbackUrl;
42 }),
43 });
44
45 export const cancelProviderAuthCallback = DesktopIpc.makeIpcMethod({
46 channel: IpcChannels.CANCEL_PROVIDER_AUTH_CALLBACK_CHANNEL,
47 payload: Request,
48 result: Schema.Void,
49 handler: (authorizationUrl) =>
50 Effect.try({
51 try: () => cancelCodexAuthCallback(authorizationUrl),
52 catch: () => new CodexAuthCallbackError({ detail: "Invalid ChatGPT sign-in request." }),
53 }),
54 });