2
2
import { LitElement , html } from 'lit' ;
3
3
import DOMPurify from 'dompurify' ;
4
4
import { customElement , property , query , state } from 'lit/decorators.js' ;
5
- import { chatHttpOptions , globalConfig , requestOptions , MAX_CHAT_HISTORY } from '../config/global-config.js' ;
5
+ import { chatHttpOptions , globalConfig , requestOptions } from '../config/global-config.js' ;
6
6
import { chatStyle } from '../styles/chat-component.js' ;
7
7
import { unsafeSVG } from 'lit/directives/unsafe-svg.js' ;
8
8
import { chatEntryToString , newListWithEntryAtIndex } from '../utils/index.js' ;
@@ -13,16 +13,16 @@ import iconDelete from '../../public/svg/delete-icon.svg?raw';
13
13
import iconCancel from '../../public/svg/cancel-icon.svg?raw' ;
14
14
import iconSend from '../../public/svg/send-icon.svg?raw' ;
15
15
import iconLogo from '../../public/branding/brand-logo.svg?raw' ;
16
- import iconUp from '../../public/svg/chevron-up-icon.svg?raw' ;
17
16
18
17
import { ChatController } from './chat-controller.js' ;
19
- import { ChatHistoryController } from './chat-history-controller.js' ;
20
18
import {
21
19
lazyMultiInject ,
22
20
ControllerType ,
23
21
type ChatInputController ,
24
22
type ChatInputFooterController ,
25
23
type ChatSectionController ,
24
+ type ChatActionController ,
25
+ type ChatThreadController ,
26
26
} from './composable.js' ;
27
27
import { ChatContextController } from './chat-context.js' ;
28
28
@@ -99,7 +99,6 @@ export class ChatComponent extends LitElement {
99
99
isResetInput = false ;
100
100
101
101
private chatController = new ChatController ( this ) ;
102
- private chatHistoryController = new ChatHistoryController ( this ) ;
103
102
private chatContext = new ChatContextController ( this ) ;
104
103
105
104
// These are the chat bubbles that will be displayed in the chat
@@ -116,9 +115,16 @@ export class ChatComponent extends LitElement {
116
115
@lazyMultiInject ( ControllerType . ChatSection )
117
116
chatSectionControllers : ChatSectionController [ ] | undefined ;
118
117
118
+ @lazyMultiInject ( ControllerType . ChatAction )
119
+ chatActionControllers : ChatActionController [ ] | undefined ;
120
+
121
+ @lazyMultiInject ( ControllerType . ChatThread )
122
+ chatThreadControllers : ChatThreadController [ ] | undefined ;
123
+
119
124
public constructor ( ) {
120
125
super ( ) ;
121
126
this . setQuestionInputValue = this . setQuestionInputValue . bind ( this ) ;
127
+ this . renderChatThread = this . renderChatThread . bind ( this ) ;
122
128
}
123
129
124
130
// Lifecycle method that runs when the component is first connected to the DOM
@@ -139,6 +145,16 @@ export class ChatComponent extends LitElement {
139
145
component . attach ( this , this . chatContext ) ;
140
146
}
141
147
}
148
+ if ( this . chatActionControllers ) {
149
+ for ( const component of this . chatActionControllers ) {
150
+ component . attach ( this , this . chatContext ) ;
151
+ }
152
+ }
153
+ if ( this . chatThreadControllers ) {
154
+ for ( const component of this . chatThreadControllers ) {
155
+ component . attach ( this , this . chatContext ) ;
156
+ }
157
+ }
142
158
}
143
159
144
160
override updated ( changedProperties : Map < string | number | symbol , unknown > ) {
@@ -189,13 +205,14 @@ export class ChatComponent extends LitElement {
189
205
return [ ] ;
190
206
}
191
207
192
- const history = [
193
- ...this . chatThread ,
194
- // include the history from the previous session if the user has enabled the chat history
195
- ...( this . chatHistoryController . showChatHistory ? this . chatHistoryController . chatHistory : [ ] ) ,
196
- ] ;
208
+ let thread : ChatThreadEntry [ ] = [ ...this . chatThread ] ;
209
+ if ( this . chatThreadControllers ) {
210
+ for ( const controller of this . chatThreadControllers ) {
211
+ thread = controller . merge ( thread ) ;
212
+ }
213
+ }
197
214
198
- const messages : Message [ ] = history . map ( ( entry ) => {
215
+ const messages : Message [ ] = thread . map ( ( entry ) => {
199
216
return {
200
217
content : chatEntryToString ( entry ) ,
201
218
role : entry . isUserMessage ? 'user' : 'assistant' ,
@@ -234,7 +251,7 @@ export class ChatComponent extends LitElement {
234
251
) ;
235
252
236
253
if ( this . interactionModel === 'chat' ) {
237
- this . chatHistoryController . saveChatHistory ( this . chatThread ) ;
254
+ this . saveChatThreads ( this . chatThread ) ;
238
255
}
239
256
240
257
this . questionInput . value = '' ;
@@ -249,15 +266,22 @@ export class ChatComponent extends LitElement {
249
266
this . isResetInput = false ;
250
267
}
251
268
269
+ saveChatThreads ( chatThread : ChatThreadEntry [ ] ) : void {
270
+ if ( this . chatThreadControllers ) {
271
+ for ( const component of this . chatThreadControllers ) {
272
+ component . save ( chatThread ) ;
273
+ }
274
+ }
275
+ }
276
+
252
277
// Reset the chat and show the default prompts
253
278
resetCurrentChat ( event : Event ) : void {
254
279
this . isChatStarted = false ;
255
280
this . chatThread = [ ] ;
256
281
this . isDisabled = false ;
257
282
this . chatContext . selectedCitation = undefined ;
258
283
this . chatController . reset ( ) ;
259
- // clean up the current session content from the history too
260
- this . chatHistoryController . saveChatHistory ( this . chatThread ) ;
284
+ this . saveChatThreads ( this . chatThread ) ;
261
285
this . collapseAside ( event ) ;
262
286
this . handleUserChatCancel ( event ) ;
263
287
}
@@ -360,9 +384,7 @@ export class ChatComponent extends LitElement {
360
384
${ this . isChatStarted
361
385
? html `
362
386
< div class ="chat__header--thread ">
363
- ${ this . interactionModel === 'chat'
364
- ? this . chatHistoryController . renderHistoryButton ( { disabled : this . isDisabled } )
365
- : '' }
387
+ ${ this . chatActionControllers ?. map ( ( component ) => component . render ( this . isDisabled ) ) }
366
388
< chat-action-button
367
389
.label ="${ globalConfig . RESET_CHAT_BUTTON_TITLE } "
368
390
actionId ="chat-reset-button "
@@ -371,19 +393,7 @@ export class ChatComponent extends LitElement {
371
393
>
372
394
</ chat-action-button >
373
395
</ div >
374
- ${ this . chatHistoryController . showChatHistory
375
- ? html `< div class ="chat-history__container ">
376
- ${ this . renderChatThread ( this . chatHistoryController . chatHistory ) }
377
- < div class ="chat-history__footer ">
378
- ${ unsafeSVG ( iconUp ) }
379
- ${ globalConfig . CHAT_HISTORY_FOOTER_TEXT . replace (
380
- globalConfig . CHAT_MAX_COUNT_TAG ,
381
- MAX_CHAT_HISTORY ,
382
- ) }
383
- ${ unsafeSVG ( iconUp ) }
384
- </ div >
385
- </ div > `
386
- : '' }
396
+ ${ this . chatThreadControllers ?. map ( ( component ) => component . render ( this . renderChatThread ) ) }
387
397
${ this . renderChatThread ( this . chatThread ) }
388
398
`
389
399
: '' }
0 commit comments