@@ -67,14 +67,12 @@ export interface HandoffManagerDeps {
6767export interface ConnectScope {
6868 workspaceId ?: string
6969 credentialId ?: string
70- chatAttemptId ?: string
7170}
7271
7372export interface HandoffManager {
7473 begin ( ) : Promise < boolean >
7574 beginConnect ( providerId : string , scope ?: ConnectScope ) : Promise < boolean >
7675 consume ( state : string , kind : HandoffKind ) : boolean
77- consumeConnect ( state : string ) : ConnectScope | null
7876 clear ( ) : void
7977}
8078
@@ -94,12 +92,7 @@ export function createHandoffManager(
9492 const now = deps . now ?? Date . now
9593 let loopbackServer : Server | null = null
9694 let loopbackTimer : NodeJS . Timeout | undefined
97- let pending : {
98- state : string
99- createdAt : number
100- kind : HandoffKind
101- connectScope ?: ConnectScope
102- } | null = null
95+ let pending : { state : string ; createdAt : number ; kind : HandoffKind } | null = null
10396
10497 const stopLoopback = ( ) => {
10598 clearTimeout ( loopbackTimer )
@@ -221,23 +214,10 @@ export function createHandoffManager(
221214 pending = null
222215 }
223216
224- const consumePending = ( state : string , kind : HandoffKind ) : NonNullable < typeof pending > | null => {
225- if ( ! pending || pending . kind !== kind ) return null
226- if ( now ( ) - pending . createdAt > HANDOFF_TTL_MS ) {
227- clear ( )
228- return null
229- }
230- if ( ! safeCompare ( pending . state , state ) ) return null
231- const consumed = pending
232- clear ( )
233- return consumed
234- }
235-
236217 const beginFlow = async (
237218 kind : HandoffKind ,
238219 landingPath : string ,
239- params : Record < string , string > ,
240- connectScope ?: ConnectScope
220+ params : Record < string , string >
241221 ) : Promise < boolean > => {
242222 const state = generateShortId ( STATE_LENGTH )
243223 // startLoopback() already tore down any prior server; if this bind fails,
@@ -248,12 +228,7 @@ export function createHandoffManager(
248228 clear ( )
249229 return false
250230 }
251- pending = {
252- state,
253- createdAt : now ( ) ,
254- kind,
255- ...( connectScope ? { connectScope : { ...connectScope } } : { } ) ,
256- }
231+ pending = { state, createdAt : now ( ) , kind }
257232 const landing = new URL ( landingPath , deps . origin ( ) )
258233 for ( const [ key , value ] of Object . entries ( params ) ) {
259234 landing . searchParams . set ( key , value )
@@ -284,24 +259,26 @@ export function createHandoffManager(
284259 // unknown (offline, signed out): the page then falls back to its normal
285260 // login redirect rather than blocking a connect on a failed probe.
286261 const userId = await deps . currentUserId ( )
287- return beginFlow (
288- 'connect' ,
289- '/desktop/connect' ,
290- {
291- provider : providerId ,
292- ...( userId ? { user : userId } : { } ) ,
293- ...( scope . workspaceId ? { workspaceId : scope . workspaceId } : { } ) ,
294- ...( scope . credentialId ? { credentialId : scope . credentialId } : { } ) ,
295- } ,
296- scope
297- )
262+ return beginFlow ( 'connect' , '/desktop/connect' , {
263+ provider : providerId ,
264+ ...( userId ? { user : userId } : { } ) ,
265+ ...( scope . workspaceId ? { workspaceId : scope . workspaceId } : { } ) ,
266+ ...( scope . credentialId ? { credentialId : scope . credentialId } : { } ) ,
267+ } )
298268 } ,
299269 consume ( state : string , kind : HandoffKind ) {
300- return consumePending ( state , kind ) !== null
301- } ,
302- consumeConnect ( state : string ) {
303- const consumed = consumePending ( state , 'connect' )
304- return consumed ? { ...( consumed . connectScope ?? { } ) } : null
270+ if ( ! pending || pending . kind !== kind ) {
271+ return false
272+ }
273+ if ( now ( ) - pending . createdAt > HANDOFF_TTL_MS ) {
274+ clear ( )
275+ return false
276+ }
277+ if ( ! safeCompare ( pending . state , state ) ) {
278+ return false
279+ }
280+ clear ( )
281+ return true
305282 } ,
306283 clear,
307284 }
@@ -466,8 +443,6 @@ export function createAuthFlow(deps: AuthFlowDeps): AuthFlow {
466443export interface ConnectHandoffResult {
467444 ok : boolean
468445 error ?: string
469- /** Exact Mothership chat attempt, or null for ordinary integration flows. */
470- chatAttemptId : string | null
471446}
472447
473448export interface ConnectFlowDeps {
@@ -501,24 +476,19 @@ export function createConnectFlow(deps: ConnectFlowDeps): ConnectFlow {
501476 return opened
502477 } ,
503478 handleCallback ( callback : ConnectHandoffCallback ) {
504- const scope = deps . handoff . consumeConnect ( callback . state )
505- if ( ! scope ) {
479+ if ( ! deps . handoff . consume ( callback . state , 'connect' ) ) {
506480 deps . events . record ( 'connect_handoff_state_fail' )
507481 return
508482 }
509483 if ( callback . error === undefined ) {
510484 deps . events . record ( 'connect_handoff_ok' )
511485 deps . focusMainWindow ( )
512- deps . notifyRenderer ( { ok : true , chatAttemptId : scope . chatAttemptId ?? null } )
486+ deps . notifyRenderer ( { ok : true } )
513487 return
514488 }
515489 deps . events . record ( 'connect_handoff_error' , { error : callback . error } )
516490 deps . focusMainWindow ( )
517- deps . notifyRenderer ( {
518- ok : false ,
519- error : callback . error ,
520- chatAttemptId : scope . chatAttemptId ?? null ,
521- } )
491+ deps . notifyRenderer ( { ok : false , error : callback . error } )
522492 } ,
523493 }
524494}
0 commit comments