33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 *--------------------------------------------------------------------------------------------*/
55
6- import { autorun , derivedOpts , IReader } from '../../../../base/common/observable.js' ;
6+ import { autorun , derived , derivedOpts , observableSignalFromEvent } from '../../../../base/common/observable.js' ;
77import { Disposable } from '../../../../base/common/lifecycle.js' ;
88import { ResourceMap } from '../../../../base/common/map.js' ;
99import { isEqual } from '../../../../base/common/resources.js' ;
1010import { URI } from '../../../../base/common/uri.js' ;
11- import { AgentSessionProviders } from '../../../../workbench/contrib/chat/browser/agentSessions/agentSessions.js' ;
1211import { IAgentSessionsService } from '../../../../workbench/contrib/chat/browser/agentSessions/agentSessionsService.js' ;
1312import { IChatService } from '../../../../workbench/contrib/chat/common/chatService/chatService.js' ;
14- import { IChatEditingService } from '../../../../workbench/contrib/chat/common/editing/chatEditingService.js' ;
15- import { getChatSessionType } from '../../../../workbench/contrib/chat/common/model/chatUri.js' ;
1613import { IWorkbenchLayoutService , Parts } from '../../../../workbench/services/layout/browser/layoutService.js' ;
1714import { ISessionsManagementService } from '../../sessions/browser/sessionsManagementService.js' ;
1815import { IViewsService } from '../../../../workbench/services/views/common/viewsService.js' ;
@@ -32,30 +29,44 @@ export class ToggleChangesViewContribution extends Disposable {
3229 constructor (
3330 @IWorkbenchLayoutService private readonly layoutService : IWorkbenchLayoutService ,
3431 @ISessionsManagementService private readonly sessionManagementService : ISessionsManagementService ,
35- @IChatEditingService private readonly chatEditingService : IChatEditingService ,
3632 @IAgentSessionsService private readonly agentSessionsService : IAgentSessionsService ,
3733 @IChatService private readonly chatService : IChatService ,
3834 @IViewsService private readonly viewsService : IViewsService ,
3935 ) {
4036 super ( ) ;
4137
42- const activeSessionResourceObs = derivedOpts < URI | undefined > ( {
43- equalsFn : isEqual ,
44- } , ( reader ) => {
45- return this . sessionManagementService . activeSession . map ( activeSession => activeSession ?. resource ) . read ( reader ) ;
46- } ) . recomputeInitiallyAndOnChange ( this . _store ) ;
38+ const activeSessionChangedSignal = observableSignalFromEvent ( this ,
39+ this . agentSessionsService . model . onDidChangeSessions ) ;
4740
48- this . _register ( this . chatService . onDidSubmitRequest ( ( { chatSessionResource } ) => {
49- this . pendingTurnStateByResource . set ( chatSessionResource , {
50- hadChangesBeforeSend : this . hasSessionChanges ( chatSessionResource ) ,
51- submittedAt : Date . now ( ) ,
52- } ) ;
41+ const activeSessionResourceObs = derivedOpts < URI | undefined > ( { equalsFn : isEqual , } , reader => {
42+ const activeSession = this . sessionManagementService . activeSession . read ( reader ) ;
43+ return activeSession ?. resource ;
44+ } ) ;
45+
46+ const activeSessionHasChangesObs = derived < boolean > ( reader => {
47+ const sessionResource = activeSessionResourceObs . read ( reader ) ;
48+ if ( ! sessionResource ) {
49+ return false ;
50+ }
51+
52+ activeSessionChangedSignal . read ( reader ) ;
53+ const model = this . agentSessionsService . getSession ( sessionResource ) ;
54+ const changes = model ?. changes instanceof Array ? model . changes : [ ] ;
55+ return changes . length > 0 ;
56+ } ) ;
57+
58+ // Switch between sessions
59+ this . _register ( autorun ( reader => {
60+ const activeSessionHasChanges = activeSessionHasChangesObs . read ( reader ) ;
61+ this . syncAuxiliaryBarVisibility ( activeSessionHasChanges ) ;
5362 } ) ) ;
5463
55- // When a turn is completed, check if there were changes before the turn and if there are changes after the turn.
56- // If there were no changes before the turn and there are changes after the turn, show the auxiliary bar.
64+ // When a turn is completed, check if there were changes before the turn and
65+ // if there are changes after the turn. If there were no changes before the
66+ // turn and there are changes after the turn, show the auxiliary bar.
5767 this . _register ( autorun ( ( reader ) => {
5868 const activeSessionResource = activeSessionResourceObs . read ( reader ) ;
69+ const activeSessionHasChanges = activeSessionHasChangesObs . read ( reader ) ;
5970 if ( ! activeSessionResource ) {
6071 return ;
6172 }
@@ -71,43 +82,21 @@ export class ToggleChangesViewContribution extends Disposable {
7182 return ;
7283 }
7384
74- const hasChangesAfterTurn = this . hasSessionChanges ( activeSessionResource , reader ) ;
75- if ( ! pendingTurnState . hadChangesBeforeSend && hasChangesAfterTurn ) {
85+ if ( ! pendingTurnState . hadChangesBeforeSend && activeSessionHasChanges ) {
7686 this . layoutService . setPartHidden ( false , Parts . AUXILIARYBAR_PART ) ;
7787 }
7888
7989 this . pendingTurnStateByResource . delete ( activeSessionResource ) ;
8090 } ) ) ;
8191
82- // When the session is switched, show the auxiliary bar if there are pending changes from the session
83- this . _register ( autorun ( reader => {
84- const sessionResource = activeSessionResourceObs . read ( reader ) ;
85- if ( ! sessionResource ) {
86- this . syncAuxiliaryBarVisibility ( false ) ;
87- return ;
88- }
89-
90- const hasChanges = this . hasSessionChanges ( sessionResource , reader ) ;
91- this . syncAuxiliaryBarVisibility ( hasChanges ) ;
92+ this . _register ( this . chatService . onDidSubmitRequest ( ( { chatSessionResource } ) => {
93+ this . pendingTurnStateByResource . set ( chatSessionResource , {
94+ hadChangesBeforeSend : activeSessionHasChangesObs . get ( ) ,
95+ submittedAt : Date . now ( ) ,
96+ } ) ;
9297 } ) ) ;
9398 }
9499
95- private hasSessionChanges ( sessionResource : URI , reader ?: IReader ) : boolean {
96- const isBackgroundSession = getChatSessionType ( sessionResource ) === AgentSessionProviders . Background ;
97-
98- let editingSessionCount = 0 ;
99- if ( ! isBackgroundSession ) {
100- const sessions = this . chatEditingService . editingSessionsObs . read ( reader ) ;
101- const editingSession = sessions . find ( candidate => isEqual ( candidate . chatSessionResource , sessionResource ) ) ;
102- editingSessionCount = editingSession ? editingSession . entries . read ( reader ) . length : 0 ;
103- }
104-
105- const session = this . agentSessionsService . getSession ( sessionResource ) ;
106- const sessionFilesCount = session ?. changes instanceof Array ? session . changes . length : 0 ;
107-
108- return editingSessionCount + sessionFilesCount > 0 ;
109- }
110-
111100 private syncAuxiliaryBarVisibility ( hasChanges : boolean ) : void {
112101 if ( hasChanges ) {
113102 this . viewsService . openView ( CHANGES_VIEW_ID , false ) ;
0 commit comments