@@ -20,11 +20,12 @@ import {
2020} from "./api-client.js"
2121import { type ControllerRuntime , ensureControllerReady } from "./controller.js"
2222import type { Command } from "./frontend-lib/core/domain.js"
23- import type { ApiRequestError , CliError , ControllerBootstrapError } from "./host-errors.js"
23+ import type { ApiRequestError , CliError } from "./host-errors.js"
2424import { terminalAuthTitle } from "./menu-auth-shared.js"
25- import { attachTerminalSession , type TerminalSessionClientError } from "./terminal-session-client.js"
25+ import { attachTerminalSession } from "./terminal-session-client.js"
2626
2727type OperationalCommand = Exclude < Command , { readonly _tag : "Help" } >
28+ type RoutedAuthEffect = Effect . Effect < void , CliError , ControllerRuntime >
2829
2930export type RoutedAuthCommand = Extract <
3031 OperationalCommand ,
@@ -46,10 +47,9 @@ export type RoutedAuthCommand = Extract<
4647 }
4748>
4849
49- const withControllerReady = < E extends CliError , R > (
50- effect : Effect . Effect < void , E , R >
51- ) : Effect . Effect < void , E | ControllerBootstrapError , R | ControllerRuntime > =>
52- pipe ( ensureControllerReady ( ) , Effect . zipRight ( effect ) )
50+ const withControllerReady = < R > (
51+ effect : Effect . Effect < void , CliError , R >
52+ ) : Effect . Effect < void , CliError , ControllerRuntime | R > => pipe ( ensureControllerReady ( ) , Effect . zipRight ( effect ) )
5353
5454const renderAuthPayload = ( payload : JsonValue ) => Effect . log ( renderJsonPayload ( payload ) )
5555
@@ -60,17 +60,6 @@ const missingAuthTerminalSessionError = (provider: "GrokOauth"): ApiRequestError
6060 message : `Controller did not create a terminal session for ${ provider } .`
6161} )
6262
63- const attachGrokTerminalSession = (
64- session : ApiTerminalSession | null
65- ) : Effect . Effect < void , ApiRequestError | TerminalSessionClientError > =>
66- session === null
67- ? Effect . fail ( missingAuthTerminalSessionError ( "GrokOauth" ) )
68- : attachTerminalSession ( {
69- header : terminalAuthTitle ( "GrokOauth" ) ,
70- session,
71- websocketPath : `/auth/terminal-sessions/${ encodeURIComponent ( session . id ) } /ws`
72- } )
73-
7463const routedAuthTags : Readonly < Record < string , true > > = {
7564 AuthCodexImport : true ,
7665 AuthCodexLogin : true ,
@@ -120,12 +109,34 @@ const handleCodexLoginCommand = (
120109 command : Extract < OperationalCommand , { readonly _tag : "AuthCodexLogin" } >
121110) => withControllerReady ( codexLogin ( command ) )
122111
112+ /**
113+ * Attaches the Grok OAuth terminal session created by the controller.
114+ *
115+ * @pure false
116+ * @effect terminal websocket attachment through `attachTerminalSession`
117+ * @invariant null controller sessions fail with a typed ApiRequestError
118+ * @precondition controller response has already been decoded as ApiTerminalSession | null
119+ * @postcondition non-null sessions are attached through the auth terminal websocket path
120+ * @complexity O(1) before terminal IO
121+ * @throws Never; errors are represented in the Effect error channel as CliError
122+ */
123+ const attachGrokAuthTerminalSession = (
124+ session : ApiTerminalSession | null
125+ ) : Effect . Effect < void , CliError > =>
126+ session === null
127+ ? Effect . fail ( missingAuthTerminalSessionError ( "GrokOauth" ) )
128+ : attachTerminalSession ( {
129+ header : terminalAuthTitle ( "GrokOauth" ) ,
130+ session,
131+ websocketPath : `/auth/terminal-sessions/${ encodeURIComponent ( session . id ) } /ws`
132+ } )
133+
123134const handleGrokLoginCommand = (
124135 command : Extract < OperationalCommand , { readonly _tag : "AuthGrokLogin" } >
125136) =>
126137 withControllerReady (
127138 createAuthTerminalSession ( "GrokOauth" , command . label ) . pipe (
128- Effect . flatMap ( ( session ) => attachGrokTerminalSession ( session ) )
139+ Effect . flatMap ( ( session ) => attachGrokAuthTerminalSession ( session ) )
129140 )
130141 )
131142
@@ -157,7 +168,7 @@ const handleCodexLogoutCommand = (
157168
158169export const dispatchRoutedAuthCommand = (
159170 command : RoutedAuthCommand
160- ) : Effect . Effect < void , CliError , ControllerRuntime > =>
171+ ) : RoutedAuthEffect =>
161172 Match . value ( command ) . pipe (
162173 Match . when ( { _tag : "AuthGithubLogin" } , handleGithubLoginCommand ) ,
163174 Match . when ( { _tag : "AuthGithubStatus" } , handleGithubStatusCommand ) ,
0 commit comments