@@ -50,20 +50,38 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
5050 return `${ this . sessionSharingAgentId ( sessionKey ) ?? "" } \0${ sessionKey } ` ;
5151 }
5252
53+ protected currentSessionSharingRow (
54+ scope : ChatPaneConnectionScope ,
55+ row : GatewaySessionRow ,
56+ ) : GatewaySessionRow | null {
57+ const current = scope . state . sessionsResult ?. sessions . find ( ( candidate ) =>
58+ areUiSessionKeysEquivalent ( candidate . key , row . key ) ,
59+ ) ;
60+ if ( ! current || ! canManageChatSessionSharing ( row ) || ! canManageChatSessionSharing ( current ) ) {
61+ return null ;
62+ }
63+ if ( current === row ) {
64+ return current ;
65+ }
66+ const rowSessionId = row . sessionId ?. trim ( ) ;
67+ const currentSessionId = current . sessionId ?. trim ( ) ;
68+ return rowSessionId && currentSessionId && rowSessionId === currentSessionId ? current : null ;
69+ }
70+
5371 protected async loadSessionSharing ( row : GatewaySessionRow , force = false ) : Promise < void > {
54- const state = this . state ;
72+ const scope = this . captureConnectionScope ( ) ;
73+ const currentRow = scope ? this . currentSessionSharingRow ( scope , row ) : null ;
5574 if (
56- ! state ?. connected ||
57- ! state . client ||
58- ! canManageChatSessionSharing ( row ) ||
59- ! readSessionMethodAccess ( this . context . gateway . snapshot , {
75+ ! scope ||
76+ ! currentRow ||
77+ ! readSessionMethodAccess ( scope . context . gateway . snapshot , {
6078 method : "session.members.list" ,
6179 requiredScope : "operator.read" ,
6280 } ) . allowed
6381 ) {
6482 return ;
6583 }
66- const cacheKey = this . sessionSharingCacheKey ( row . key ) ;
84+ const cacheKey = this . sessionSharingCacheKey ( currentRow . key ) ;
6785 const current = this . sessionSharingStates . get ( cacheKey ) ;
6886 if ( current ?. loading && ! force ) {
6987 return ;
@@ -72,21 +90,26 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
7290 // gateway/account change bumps the generation and clears this cache, so a
7391 // request that resolves after the switch must be dropped rather than
7492 // overwrite the new connection's menu with the previous account's data.
75- const generation = this . connectionGeneration ;
7693 this . setSessionSharingState ( cacheKey , { ...current , loading : true , error : undefined } ) ;
7794 try {
78- const result = await state . client . request < SessionMembersListResult > ( "session.members.list" , {
79- sessionKey : row . key ,
80- ...( this . sessionSharingAgentId ( row . key )
81- ? { agentId : this . sessionSharingAgentId ( row . key ) }
95+ const result = await scope . client . request < SessionMembersListResult > ( "session.members.list" , {
96+ sessionKey : currentRow . key ,
97+ ...( this . sessionSharingAgentId ( currentRow . key )
98+ ? { agentId : this . sessionSharingAgentId ( currentRow . key ) }
8299 : { } ) ,
83100 } ) ;
84- if ( this . connectionGeneration !== generation ) {
101+ if (
102+ ! this . isConnectionScopeCurrent ( scope ) ||
103+ ! this . currentSessionSharingRow ( scope , currentRow )
104+ ) {
85105 return ;
86106 }
87107 this . setSessionSharingState ( cacheKey , { loading : false , result } ) ;
88108 } catch ( error ) {
89- if ( this . connectionGeneration !== generation ) {
109+ if (
110+ ! this . isConnectionScopeCurrent ( scope ) ||
111+ ! this . currentSessionSharingRow ( scope , currentRow )
112+ ) {
90113 return ;
91114 }
92115 this . setSessionSharingState ( cacheKey , { loading : false , error : String ( error ) } ) ;
@@ -98,13 +121,14 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
98121 visibility : SessionVisibility ,
99122 ) : Promise < void > {
100123 const scope = this . captureConnectionScope ( ) ;
101- if ( ! scope || visibility === row . visibility || ! canManageChatSessionSharing ( row ) ) {
124+ const currentRow = scope ? this . currentSessionSharingRow ( scope , row ) : null ;
125+ if ( ! scope || ! currentRow || visibility === currentRow . visibility ) {
102126 return ;
103127 }
104- const agentId = this . sessionSharingAgentId ( row . key ) ;
105- const cacheKey = this . sessionSharingCacheKey ( row . key ) ;
128+ const agentId = this . sessionSharingAgentId ( currentRow . key ) ;
129+ const cacheKey = this . sessionSharingCacheKey ( currentRow . key ) ;
106130 const params = {
107- sessionKey : row . key ,
131+ sessionKey : currentRow . key ,
108132 visibility,
109133 ...( agentId ? { agentId } : { } ) ,
110134 } ;
@@ -122,10 +146,11 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
122146 return ;
123147 }
124148 await scope . sessions . refreshReplacement ( agentId ) ;
125- if ( ! this . isConnectionScopeCurrent ( scope ) ) {
149+ const refreshedRow = this . currentSessionSharingRow ( scope , currentRow ) ;
150+ if ( ! this . isConnectionScopeCurrent ( scope ) || ! refreshedRow ) {
126151 return ;
127152 }
128- await this . loadSessionSharing ( row , true ) ;
153+ await this . loadSessionSharing ( refreshedRow , true ) ;
129154 } catch ( error ) {
130155 if ( ! this . isConnectionScopeCurrent ( scope ) ) {
131156 return ;
@@ -144,14 +169,15 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
144169 member : boolean ,
145170 ) : Promise < void > {
146171 const scope = this . captureConnectionScope ( ) ;
147- if ( ! scope || ! canManageChatSessionSharing ( row ) ) {
172+ const currentRow = scope ? this . currentSessionSharingRow ( scope , row ) : null ;
173+ if ( ! scope || ! currentRow ) {
148174 return ;
149175 }
150- const agentId = this . sessionSharingAgentId ( row . key ) ;
151- const cacheKey = this . sessionSharingCacheKey ( row . key ) ;
176+ const agentId = this . sessionSharingAgentId ( currentRow . key ) ;
177+ const cacheKey = this . sessionSharingCacheKey ( currentRow . key ) ;
152178 const method = member ? "session.members.add" : "session.members.remove" ;
153179 const params = {
154- sessionKey : row . key ,
180+ sessionKey : currentRow . key ,
155181 identityId,
156182 ...( agentId ? { agentId } : { } ) ,
157183 } ;
@@ -168,7 +194,7 @@ export abstract class ChatPaneSharing extends ChatPaneBase {
168194 if ( ! this . isConnectionScopeCurrent ( scope ) ) {
169195 return ;
170196 }
171- await this . loadSessionSharing ( row , true ) ;
197+ await this . loadSessionSharing ( currentRow , true ) ;
172198 if ( ! this . isConnectionScopeCurrent ( scope ) ) {
173199 return ;
174200 }
0 commit comments