22import { LitElement , html } from 'lit' ;
33import DOMPurify from 'dompurify' ;
44import { 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' ;
66import { chatStyle } from '../styles/chat-component.js' ;
77import { unsafeSVG } from 'lit/directives/unsafe-svg.js' ;
88import { chatEntryToString , newListWithEntryAtIndex } from '../utils/index.js' ;
@@ -13,16 +13,16 @@ import iconDelete from '../../public/svg/delete-icon.svg?raw';
1313import iconCancel from '../../public/svg/cancel-icon.svg?raw' ;
1414import iconSend from '../../public/svg/send-icon.svg?raw' ;
1515import iconLogo from '../../public/branding/brand-logo.svg?raw' ;
16- import iconUp from '../../public/svg/chevron-up-icon.svg?raw' ;
1716
1817import { ChatController } from './chat-controller.js' ;
19- import { ChatHistoryController } from './chat-history-controller.js' ;
2018import {
2119 lazyMultiInject ,
2220 ControllerType ,
2321 type ChatInputController ,
2422 type ChatInputFooterController ,
2523 type ChatSectionController ,
24+ type ChatActionController ,
25+ type ChatThreadController ,
2626} from './composable.js' ;
2727import { ChatContextController } from './chat-context.js' ;
2828
@@ -99,7 +99,6 @@ export class ChatComponent extends LitElement {
9999 isResetInput = false ;
100100
101101 private chatController = new ChatController ( this ) ;
102- private chatHistoryController = new ChatHistoryController ( this ) ;
103102 private chatContext = new ChatContextController ( this ) ;
104103
105104 // These are the chat bubbles that will be displayed in the chat
@@ -116,9 +115,16 @@ export class ChatComponent extends LitElement {
116115 @lazyMultiInject ( ControllerType . ChatSection )
117116 chatSectionControllers : ChatSectionController [ ] | undefined ;
118117
118+ @lazyMultiInject ( ControllerType . ChatAction )
119+ chatActionControllers : ChatActionController [ ] | undefined ;
120+
121+ @lazyMultiInject ( ControllerType . ChatThread )
122+ chatThreadControllers : ChatThreadController [ ] | undefined ;
123+
119124 public constructor ( ) {
120125 super ( ) ;
121126 this . setQuestionInputValue = this . setQuestionInputValue . bind ( this ) ;
127+ this . renderChatThread = this . renderChatThread . bind ( this ) ;
122128 }
123129
124130 // Lifecycle method that runs when the component is first connected to the DOM
@@ -139,6 +145,16 @@ export class ChatComponent extends LitElement {
139145 component . attach ( this , this . chatContext ) ;
140146 }
141147 }
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+ }
142158 }
143159
144160 override updated ( changedProperties : Map < string | number | symbol , unknown > ) {
@@ -189,13 +205,14 @@ export class ChatComponent extends LitElement {
189205 return [ ] ;
190206 }
191207
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+ }
197214
198- const messages : Message [ ] = history . map ( ( entry ) => {
215+ const messages : Message [ ] = thread . map ( ( entry ) => {
199216 return {
200217 content : chatEntryToString ( entry ) ,
201218 role : entry . isUserMessage ? 'user' : 'assistant' ,
@@ -234,7 +251,7 @@ export class ChatComponent extends LitElement {
234251 ) ;
235252
236253 if ( this . interactionModel === 'chat' ) {
237- this . chatHistoryController . saveChatHistory ( this . chatThread ) ;
254+ this . saveChatThreads ( this . chatThread ) ;
238255 }
239256
240257 this . questionInput . value = '' ;
@@ -249,15 +266,22 @@ export class ChatComponent extends LitElement {
249266 this . isResetInput = false ;
250267 }
251268
269+ saveChatThreads ( chatThread : ChatThreadEntry [ ] ) : void {
270+ if ( this . chatThreadControllers ) {
271+ for ( const component of this . chatThreadControllers ) {
272+ component . save ( chatThread ) ;
273+ }
274+ }
275+ }
276+
252277 // Reset the chat and show the default prompts
253278 resetCurrentChat ( event : Event ) : void {
254279 this . isChatStarted = false ;
255280 this . chatThread = [ ] ;
256281 this . isDisabled = false ;
257282 this . chatContext . selectedCitation = undefined ;
258283 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 ) ;
261285 this . collapseAside ( event ) ;
262286 this . handleUserChatCancel ( event ) ;
263287 }
@@ -360,9 +384,7 @@ export class ChatComponent extends LitElement {
360384 ${ this . isChatStarted
361385 ? html `
362386 < 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 ) ) }
366388 < chat-action-button
367389 .label ="${ globalConfig . RESET_CHAT_BUTTON_TITLE } "
368390 actionId ="chat-reset-button "
@@ -371,19 +393,7 @@ export class ChatComponent extends LitElement {
371393 >
372394 </ chat-action-button >
373395 </ 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 ) ) }
387397 ${ this . renderChatThread ( this . chatThread ) }
388398 `
389399 : '' }
0 commit comments