@@ -387,12 +387,17 @@ export const makeCodexChatGptAuth = Effect.fn("makeCodexChatGptAuth")(function*
387387 ) ;
388388 const saveRegistration = Effect . fnUntraced ( function * ( profile : typeof Registration . Type ) {
389389 const saved = yield * readRegistrations ;
390+ let connectionNumber = saved . profiles . length + 1 ;
391+ while (
392+ saved . profiles . some ( ( entry ) => entry . connectionLabel === `Connection ${ connectionNumber } ` )
393+ )
394+ connectionNumber ++ ;
390395 profile = {
391396 ...profile ,
392397 connectionLabel :
393398 saved . profiles . find ( ( entry ) => entry . clientId === profile . clientId ) ?. connectionLabel ??
394399 profile . connectionLabel ??
395- `Connection ${ saved . profiles . length + 1 } ` ,
400+ `Connection ${ connectionNumber } ` ,
396401 } ;
397402 yield * encodeRegistration ( {
398403 profiles : [ profile , ...saved . profiles . filter ( ( entry ) => entry . clientId !== profile . clientId ) ] ,
@@ -911,6 +916,7 @@ export const makeCodexChatGptAuth = Effect.fn("makeCodexChatGptAuth")(function*
911916 methods : Effect . gen ( function * ( ) {
912917 const saved = yield * readRegistrations ;
913918 const active = Option . getOrUndefined ( yield * read ) ;
919+ const sessions = ( yield * readSessions ) . sessions ;
914920 const current = saved . profiles . find (
915921 ( profile ) => profile . clientId === ( active ?. clientId ?? saved . lastClientId ) ,
916922 ) ;
@@ -933,6 +939,9 @@ export const makeCodexChatGptAuth = Effect.fn("makeCodexChatGptAuth")(function*
933939 } ,
934940 ...profiles . slice ( 0 , 30 ) . map ( ( profile ) => ( {
935941 id : profileMethodId ( profile . clientId ) ,
942+ canRemove :
943+ profile . subject === undefined &&
944+ ! sessions . some ( ( session ) => session . clientId === profile . clientId ) ,
936945 name : `${ profile . email ?? "ChatGPT account" } · ${ profile . connectionLabel } ` ,
937946 ...( profile . email ? { accountEmail : profile . email } : { } ) ,
938947 description : "Reuse this account's original sign-in registration." ,
@@ -962,6 +971,45 @@ export const makeCodexChatGptAuth = Effect.fn("makeCodexChatGptAuth")(function*
962971 context . expiresAt ,
963972 ) ,
964973 ) ,
974+ removeMethod : Effect . fn ( "CodexChatGptAuth.removeMethod" ) ( function ( methodId ) {
975+ return lock . withPermit (
976+ withSessionLock (
977+ Effect . gen ( function * ( ) {
978+ const saved = yield * readRegistrations ;
979+ const profile = saved . profiles . find (
980+ ( entry ) => profileMethodId ( entry . clientId ) === methodId ,
981+ ) ;
982+ if (
983+ ! profile ||
984+ profile . subject !== undefined ||
985+ ( yield * readSessions ) . sessions . some (
986+ ( session ) => session . clientId === profile . clientId ,
987+ )
988+ )
989+ return yield * failure (
990+ "removeMethod" ,
991+ "Only incomplete ChatGPT connections can be removed." ,
992+ ) ;
993+ const profiles = saved . profiles . filter ( ( entry ) => entry . clientId !== profile . clientId ) ;
994+ yield * encodeRegistration ( {
995+ profiles,
996+ lastClientId :
997+ saved . lastClientId === profile . clientId
998+ ? ( profiles [ 0 ] ?. clientId ?? null )
999+ : saved . lastClientId ,
1000+ } ) . pipe (
1001+ Effect . flatMap ( ( json ) => registrationStore . set ( new TextEncoder ( ) . encode ( json ) ) ) ,
1002+ Effect . mapError ( ( ) =>
1003+ failure (
1004+ "removeMethod" ,
1005+ "Could not remove the saved ChatGPT connection. Try again." ,
1006+ ) ,
1007+ ) ,
1008+ ) ;
1009+ } ) ,
1010+ ) ,
1011+ ) ;
1012+ } ) ,
9651013 logout : lock . withPermit ( withSessionLock ( logout ) ) ,
9661014 } ) ;
9671015 const reconnectProfile = Effect . fnUntraced ( function * ( methodId : string ) {
0 commit comments