1414 limitations under the License.
1515*/
1616
17- import { useState , useRef , useCallback , useEffect , useMemo } from 'react' ;
17+ import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
1818import { useAuth } from 'react-oidc-context' ;
1919import Form from '@cloudscape-design/components/form' ;
2020import Container from '@cloudscape-design/components/container' ;
2121import Box from '@cloudscape-design/components/box' ;
2222import { v4 as uuidv4 } from 'uuid' ;
2323import SpaceBetween from '@cloudscape-design/components/space-between' ;
24- import { Grid , TextContent , PromptInput , Autosuggest , ButtonGroup } from '@cloudscape-design/components' ;
24+ import {
25+ Autosuggest ,
26+ ButtonGroup ,
27+ ButtonGroupProps ,
28+ Grid ,
29+ PromptInput ,
30+ TextContent ,
31+ } from '@cloudscape-design/components' ;
2532import StatusIndicator from '@cloudscape-design/components/status-indicator' ;
2633
2734import Message from './Message' ;
28- import { LisaChatMessage , LisaChatSession , LisaChatMessageMetadata } from '../types' ;
29- import { RESTAPI_URI , formatDocumentsAsString , RESTAPI_VERSION } from '../utils' ;
35+ import { LisaChatMessage , LisaChatMessageMetadata , LisaChatSession } from '../types' ;
36+ import { formatDocumentsAsString , RESTAPI_URI , RESTAPI_VERSION } from '../utils' ;
3037import { LisaChatMessageHistory } from '../adapters/lisa-chat-history' ;
3138import { ChatPromptTemplate , MessagesPlaceholder , PromptTemplate } from '@langchain/core/prompts' ;
3239import { RunnableSequence } from '@langchain/core/runnables' ;
3340import { StringOutputParser } from '@langchain/core/output_parsers' ;
34- import { BufferWindowMemory } from 'langchain/memory' ;
3541import RagControls , { RagConfig } from './RagOptions' ;
3642import { ContextUploadModal , RagUploadModal } from './FileUploadModals' ;
3743import { ChatOpenAI } from '@langchain/openai' ;
@@ -41,7 +47,7 @@ import { useLazyGetConfigurationQuery } from '../../shared/reducers/configuratio
4147import {
4248 useGetSessionHealthQuery ,
4349 useLazyGetSessionByIdQuery ,
44- useUpdateSessionMutation
50+ useUpdateSessionMutation ,
4551} from '../../shared/reducers/session.reducer' ;
4652import { useAppDispatch } from '../../config/store' ;
4753import { useNotificationService } from '../../shared/util/hooks' ;
@@ -51,6 +57,7 @@ import { baseConfig, GenerateLLMRequestParams, IChatConfiguration } from '../../
5157import { useLazyGetRelevantDocumentsQuery } from '../../shared/reducers/rag.reducer' ;
5258import { IConfiguration } from '../../shared/model/configuration.model' ;
5359import { DocumentSummarizationModal } from './DocumentSummarizationModal' ;
60+ import { ChatMemory } from '../../shared/util/chat-memory' ;
5461
5562export default function Chat ( { sessionId } ) {
5663 const dispatch = useAppDispatch ( ) ;
@@ -94,7 +101,7 @@ export default function Chat ({ sessionId }) {
94101 const [ useRag , setUseRag ] = useState ( false ) ;
95102 const [ ragConfig , setRagConfig ] = useState < RagConfig > ( { } as RagConfig ) ;
96103 const [ memory , setMemory ] = useState (
97- new BufferWindowMemory ( {
104+ new ChatMemory ( {
98105 chatHistory : new LisaChatMessageHistory ( session ) ,
99106 returnMessages : false ,
100107 memoryKey : 'history' ,
@@ -157,12 +164,11 @@ export default function Chat ({ sessionId }) {
157164 const handleRagConfiguration = async ( chainSteps : any [ ] ) => {
158165 const ragStep = {
159166 input : ( { input } : { input : string } ) => input ,
160- chatHistory : ( ) => memory . loadMemoryVariables ( { } ) ,
167+ chatHistory : ( ) => memory . loadMemoryVariables ( ) ,
161168 context : async ( input : { input : string ; chatHistory ?: LisaChatMessage [ ] } ) => {
162169 const question = await getContextualizedQuestion ( input ) ;
163170 const relevantDocs = await fetchRelevantDocuments ( question ) ;
164- const serialized = await updateSessionWithRagContext ( relevantDocs ) ;
165- return serialized ;
171+ return await updateSessionWithRagContext ( relevantDocs ) ;
166172 } ,
167173 } ;
168174
@@ -218,7 +224,7 @@ export default function Chat ({ sessionId }) {
218224 const handleNonRagConfiguration = ( chainSteps : any [ ] ) => {
219225 const nonRagStep = {
220226 input : ( initialInput : any ) => initialInput . input ,
221- memory : ( ) => memory . loadMemoryVariables ( { } ) ,
227+ memory : ( ) => memory . loadMemoryVariables ( ) ,
222228 context : ( ) => fileContext || '' ,
223229 humanPrefix : ( initialInput : any ) => initialInput . humanPrefix ,
224230 aiPrefix : ( initialInput : any ) => initialInput . aiPrefix ,
@@ -376,7 +382,7 @@ export default function Chat ({ sessionId }) {
376382
377383 useEffect ( ( ) => {
378384 setMemory (
379- new BufferWindowMemory ( {
385+ new ChatMemory ( {
380386 chatHistory : new LisaChatMessageHistory ( session ) ,
381387 returnMessages : false ,
382388 memoryKey : 'history' ,
@@ -390,7 +396,7 @@ export default function Chat ({ sessionId }) {
390396
391397 useEffect ( ( ) => {
392398 if ( selectedModel && auth . isAuthenticated ) {
393- memory . loadMemoryVariables ( { } ) . then ( async ( formattedHistory ) => {
399+ memory . loadMemoryVariables ( ) . then ( async ( formattedHistory ) => {
394400 const promptValues = {
395401 input : userPrompt ,
396402 history : formattedHistory . history ,
@@ -543,9 +549,9 @@ export default function Chat ({ sessionId }) {
543549 < div ref = { bottomRef } />
544550 </ SpaceBetween >
545551 </ div >
546- < div className = 'fixed bottom-8' style = { { width : 'calc(var(--awsui-layout-width-g964ok) - 500px)' } } >
552+ < div className = 'sticky bottom-8' >
547553 < form onSubmit = { ( e ) => e . preventDefault ( ) } >
548- < Form variant = 'embedded' >
554+ < Form >
549555 < Container >
550556 < SpaceBetween size = 'm' direction = 'vertical' >
551557 < Grid
@@ -562,6 +568,7 @@ export default function Chat ({ sessionId }) {
562568 empty = { < div className = 'text-gray-500' > No models available.</ div > }
563569 filteringType = 'auto'
564570 value = { selectedModel ?. modelId ?? '' }
571+ enteredTextLabel = { ( text ) => `Use: "${ text } "` }
565572 onChange = { ( { detail : { value } } ) => {
566573 if ( ! value || value . length === 0 ) {
567574 setSelectedModel ( undefined ) ;
@@ -635,20 +642,20 @@ export default function Chat ({ sessionId }) {
635642 id : 'upload-to-rag' ,
636643 iconName : 'upload' ,
637644 text : 'Upload to RAG'
638- } ] : [ ] ) ,
645+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
639646 ...( config ?. configuration . enabledComponents . uploadContextDocs ?
640647 [ {
641648 type : 'icon-button' ,
642649 id : 'add-file-to-context' ,
643650 iconName : 'insert-row' ,
644651 text : 'Add file to context'
645- } ] : [ ] ) ,
652+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
646653 ...( config ?. configuration . enabledComponents . documentSummarization ? [ {
647654 type : 'icon-button' ,
648655 id : 'summarize-document' ,
649656 iconName : 'transcript' ,
650657 text : 'Summarize Document'
651- } ] : [ ] ) ,
658+ } ] as ButtonGroupProps . Item [ ] : [ ] ) ,
652659 ...( config ?. configuration . enabledComponents . editPromptTemplate ?
653660 [ {
654661 type : 'menu-dropdown' ,
@@ -661,7 +668,7 @@ export default function Chat ({ sessionId }) {
661668 text : 'Edit Prompt Template'
662669 } ,
663670 ]
664- } ] : [ ] )
671+ } ] as ButtonGroupProps . Item [ ] : [ ] )
665672 ] }
666673 variant = 'icon'
667674 />
0 commit comments