13
13
//
14
14
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
15
// *****************************************************************************
16
- import { FrontendApplication } from '@theia/core/lib/browser' ;
16
+ import { FrontendApplication , Widget , codicon } from '@theia/core/lib/browser' ;
17
17
import { AIViewContribution } from '@theia/ai-core/lib/browser' ;
18
- import { injectable } from '@theia/core/shared/inversify' ;
18
+ import { inject , injectable , postConstruct } from '@theia/core/shared/inversify' ;
19
19
import { AIHistoryView } from './ai-history-widget' ;
20
- import { Command , CommandRegistry } from '@theia/core' ;
20
+ import { Command , CommandRegistry , Emitter } from '@theia/core' ;
21
+ import { TabBarToolbarContribution , TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar' ;
22
+ import { CommunicationRecordingService } from '@theia/ai-core' ;
21
23
22
24
export const AI_HISTORY_TOGGLE_COMMAND_ID = 'aiHistory:toggle' ;
23
25
export const OPEN_AI_HISTORY_VIEW = Command . toLocalizedCommand ( {
24
26
id : 'aiHistory:open' ,
25
27
label : 'Open AI History view' ,
26
28
} ) ;
27
29
30
+ export const AI_HISTORY_VIEW_SORT_CHRONOLOGICALLY = Command . toLocalizedCommand ( {
31
+ id : 'aiHistory:sortChronologically' ,
32
+ label : 'AI History: Sort chronologically' ,
33
+ iconClass : codicon ( 'arrow-down' )
34
+ } ) ;
35
+
36
+ export const AI_HISTORY_VIEW_SORT_REVERSE_CHRONOLOGICALLY = Command . toLocalizedCommand ( {
37
+ id : 'aiHistory:sortReverseChronologically' ,
38
+ label : 'AI History: Sort reverse chronologically' ,
39
+ iconClass : codicon ( 'arrow-up' )
40
+ } ) ;
41
+
42
+ export const AI_HISTORY_VIEW_CLEAR = Command . toLocalizedCommand ( {
43
+ id : 'aiHistory:clear' ,
44
+ label : 'AI History: Clear History' ,
45
+ iconClass : codicon ( 'clear-all' )
46
+ } ) ;
47
+
28
48
@injectable ( )
29
- export class AIHistoryViewContribution extends AIViewContribution < AIHistoryView > {
30
- constructor ( ) {
49
+ export class AIHistoryViewContribution extends AIViewContribution < AIHistoryView > implements TabBarToolbarContribution {
50
+ recordingService : CommunicationRecordingService ;
51
+ constructor ( @inject ( CommunicationRecordingService ) recordingService : CommunicationRecordingService ) {
31
52
super ( {
32
53
widgetId : AIHistoryView . ID ,
33
54
widgetName : AIHistoryView . LABEL ,
@@ -37,16 +58,88 @@ export class AIHistoryViewContribution extends AIViewContribution<AIHistoryView>
37
58
} ,
38
59
toggleCommandId : AI_HISTORY_TOGGLE_COMMAND_ID ,
39
60
} ) ;
61
+ this . recordingService = recordingService ;
40
62
}
41
63
42
64
async initializeLayout ( _app : FrontendApplication ) : Promise < void > {
43
65
await this . openView ( ) ;
44
66
}
45
67
46
- override registerCommands ( commands : CommandRegistry ) : void {
47
- super . registerCommands ( commands ) ;
48
- commands . registerCommand ( OPEN_AI_HISTORY_VIEW , {
68
+ override registerCommands ( registry : CommandRegistry ) : void {
69
+ super . registerCommands ( registry ) ;
70
+ registry . registerCommand ( OPEN_AI_HISTORY_VIEW , {
49
71
execute : ( ) => this . openView ( { activate : true } ) ,
50
72
} ) ;
73
+ registry . registerCommand ( AI_HISTORY_VIEW_SORT_CHRONOLOGICALLY , {
74
+ isEnabled : widget => this . withWidget ( widget , ( ) => ! widget . isChronologial ) ,
75
+ isVisible : widget => this . withWidget ( widget , ( ) => ! widget . isChronologial ) ,
76
+ execute : widget => this . withWidget ( widget , chatWidget => {
77
+ widget . sortHistory ( true ) ;
78
+ return true ;
79
+ } )
80
+ } ) ;
81
+ registry . registerCommand ( AI_HISTORY_VIEW_SORT_REVERSE_CHRONOLOGICALLY , {
82
+ isEnabled : widget => this . withWidget ( widget , ( ) => widget . isChronologial ) ,
83
+ isVisible : widget => this . withWidget ( widget , ( ) => widget . isChronologial ) ,
84
+ execute : widget => this . withWidget ( widget , chatWidget => {
85
+ widget . sortHistory ( false ) ;
86
+ return true ;
87
+ } )
88
+ } ) ;
89
+ registry . registerCommand ( AI_HISTORY_VIEW_CLEAR , {
90
+ isEnabled : widget => this . withWidget ( widget , ( ) => true ) ,
91
+ isVisible : widget => this . withWidget ( widget , ( ) => true ) ,
92
+ execute : widget => this . withWidget ( widget , chatWidget => {
93
+ this . clearHistory ( ) ;
94
+ return true ;
95
+ } )
96
+ } ) ;
97
+ }
98
+ public clearHistory ( ) : void {
99
+ this . recordingService . clearHistory ( ) ;
100
+ }
101
+
102
+ protected withWidget (
103
+ widget : Widget | undefined = this . tryGetWidget ( ) ,
104
+ predicate : ( output : AIHistoryView ) => boolean = ( ) => true
105
+ ) : boolean | false {
106
+ return widget instanceof AIHistoryView ? predicate ( widget ) : false ;
107
+ }
108
+
109
+ protected readonly onAIHistoryWidgetStateChangedEmitter = new Emitter < void > ( ) ;
110
+ protected readonly onAIHistoryWidgettStateChanged = this . onAIHistoryWidgetStateChangedEmitter . event ;
111
+
112
+ @postConstruct ( )
113
+ protected override init ( ) : void {
114
+ super . init ( ) ;
115
+ this . widget . then ( widget => {
116
+ widget . onStateChanged ( ( ) => this . onAIHistoryWidgetStateChangedEmitter . fire ( ) ) ;
117
+ } ) ;
118
+ }
119
+
120
+ registerToolbarItems ( registry : TabBarToolbarRegistry ) : void {
121
+ registry . registerItem ( {
122
+ id : AI_HISTORY_VIEW_SORT_CHRONOLOGICALLY . id ,
123
+ command : AI_HISTORY_VIEW_SORT_CHRONOLOGICALLY . id ,
124
+ tooltip : 'Sort chronologically' ,
125
+ isVisible : widget => this . isHistoryViewWidget ( widget ) ,
126
+ onDidChange : this . onAIHistoryWidgettStateChanged
127
+ } ) ;
128
+ registry . registerItem ( {
129
+ id : AI_HISTORY_VIEW_SORT_REVERSE_CHRONOLOGICALLY . id ,
130
+ command : AI_HISTORY_VIEW_SORT_REVERSE_CHRONOLOGICALLY . id ,
131
+ tooltip : 'Sort reverse chronologically' ,
132
+ isVisible : widget => this . isHistoryViewWidget ( widget ) ,
133
+ onDidChange : this . onAIHistoryWidgettStateChanged
134
+ } ) ;
135
+ registry . registerItem ( {
136
+ id : AI_HISTORY_VIEW_CLEAR . id ,
137
+ command : AI_HISTORY_VIEW_CLEAR . id ,
138
+ tooltip : 'Clear History of all agents' ,
139
+ isVisible : widget => this . isHistoryViewWidget ( widget )
140
+ } ) ;
141
+ }
142
+ protected isHistoryViewWidget ( widget ?: Widget ) : boolean {
143
+ return ! ! widget && AIHistoryView . ID === widget . id ;
51
144
}
52
145
}
0 commit comments