@@ -7,7 +7,7 @@ import { enqueueSnackbar } from 'notistack'
7
7
import { useCallback , useEffect , useRef , useState } from 'react'
8
8
import { useTranslation } from 'react-i18next'
9
9
import { useParams , useSearchParams } from 'react-router-dom'
10
- import { DEFAULT_ASSISTANT_INSTRUCTIONS , DEFAULT_MODEL , DEFAULT_MODEL_TEMPERATURE , FREE_MODEL , inProduction , validModels } from '../../../config'
10
+ import { DEFAULT_MODEL , DEFAULT_MODEL_TEMPERATURE , FREE_MODEL , inProduction , validModels } from '../../../config'
11
11
import type { ChatMessage , MessageGenerationInfo , ToolCallResultEvent } from '../../../shared/chat'
12
12
import type { RagIndexAttributes } from '../../../shared/types'
13
13
import { getLanguageValue } from '../../../shared/utils'
@@ -21,7 +21,7 @@ import { useCourseRagIndices } from '../../hooks/useRagIndices'
21
21
import useRetryTimeout from '../../hooks/useRetryTimeout'
22
22
import useUserStatus from '../../hooks/useUserStatus'
23
23
import { useAnalyticsDispatch } from '../../stores/analytics'
24
- import type { Course , Prompt } from '../../types'
24
+ import type { Course } from '../../types'
25
25
import Footer from '../Footer'
26
26
import { ChatBox } from './ChatBox'
27
27
import { Conversation } from './Conversation'
@@ -32,13 +32,13 @@ import ToolResult from './ToolResult'
32
32
import { OutlineButtonBlack } from './general/Buttons'
33
33
import { ChatInfo } from './general/ChatInfo'
34
34
import RagSelector , { RagSelectorDescription } from './RagSelector'
35
- import { SettingsModal , useUrlPromptId } from './SettingsModal'
35
+ import { SettingsModal } from './SettingsModal'
36
36
import { useChatStream } from './useChatStream'
37
- import { getCompletionStreamV3 } from './util '
37
+ import { postCompletionStreamV3 } from './api '
38
38
import PromptSelector from './PromptSelector'
39
- import { useQuery } from '@tanstack/react-query'
40
39
import ModelSelector from './ModelSelector'
41
40
import { ConversationSplash } from './general/ConversationSplash'
41
+ import { PromptStateProvider , usePromptState } from './PromptState'
42
42
43
43
function useLocalStorageStateWithURLDefault ( key : string , defaultValue : string , urlKey : string ) {
44
44
const [ value , setValue ] = useLocalStorageState ( key , defaultValue )
@@ -58,7 +58,7 @@ function useLocalStorageStateWithURLDefault(key: string, defaultValue: string, u
58
58
return [ urlValue ?? value , modifiedSetValue ] as const
59
59
}
60
60
61
- export const ChatV2 = ( ) => {
61
+ const ChatV2Content = ( ) => {
62
62
const { courseId } = useParams ( )
63
63
const isEmbeddedMode = useIsEmbedded ( )
64
64
const theme = useTheme ( )
@@ -76,8 +76,6 @@ export const ChatV2 = () => {
76
76
const localStoragePrefix = courseId ? `course-${ courseId } ` : 'general'
77
77
const [ activeModel , setActiveModel ] = useLocalStorageStateWithURLDefault ( 'model-v2' , DEFAULT_MODEL , 'model' )
78
78
const [ disclaimerStatus , setDisclaimerStatus ] = useLocalStorageState < boolean > ( 'disclaimer-status' , true )
79
- const [ customSystemMessage , setCustomSystemMessage ] = useLocalStorageState < string > ( `${ localStoragePrefix } -chat-instructions` , DEFAULT_ASSISTANT_INSTRUCTIONS )
80
- const [ activePrompt , setActivePrompt ] = useLocalStorageState < Prompt | undefined > ( `${ localStoragePrefix } -active-prompt` , undefined )
81
79
const [ modelTemperature , setModelTemperature ] = useLocalStorageStateWithURLDefault (
82
80
`${ localStoragePrefix } -chat-model-temperature` ,
83
81
String ( DEFAULT_MODEL_TEMPERATURE ) ,
@@ -95,7 +93,7 @@ export const ChatV2 = () => {
95
93
const [ allowedModels , setAllowedModels ] = useState < string [ ] > ( [ ] )
96
94
const [ chatLeftSidePanelOpen , setChatLeftSidePanelOpen ] = useState < boolean > ( false )
97
95
// RAG states
98
- const [ ragIndexId , setRagIndexId ] = useState < number | undefined > ( )
96
+ const [ ragIndexId , _setRagIndexId ] = useState < number | undefined > ( )
99
97
const [ activeToolResult , setActiveToolResult0 ] = useState < ToolCallResultEvent | undefined > ( )
100
98
const ragIndex = ragIndices ?. find ( ( index ) => index . id === ragIndexId )
101
99
@@ -125,6 +123,8 @@ export const ChatV2 = () => {
125
123
126
124
const { t, i18n } = useTranslation ( )
127
125
126
+ const { promptInfo } = usePromptState ( )
127
+
128
128
const disclaimerInfo = infoTexts ?. find ( ( infoText ) => infoText . name === 'disclaimer' ) ?. text [ i18n . language ] ?? null
129
129
130
130
const { processStream, completion, isStreaming, setIsStreaming, toolCalls, streamController, generationInfo } = useChatStream ( {
@@ -190,13 +190,11 @@ export const ChatV2 = () => {
190
190
191
191
const generationInfo : MessageGenerationInfo = {
192
192
model : activeModel ,
193
- promptInfo : activePrompt
194
- ? { id : activePrompt . id , name : activePrompt . name , type : 'saved' , systemMessage : activePrompt . systemMessage }
195
- : { type : 'custom' , systemMessage : customSystemMessage } ,
193
+ promptInfo,
196
194
}
197
195
198
196
try {
199
- const { tokenUsageAnalysis, stream } = await getCompletionStreamV3 ( {
197
+ const { tokenUsageAnalysis, stream } = await postCompletionStreamV3 ( {
200
198
generationInfo,
201
199
messages : newMessages ,
202
200
ragIndexId,
@@ -293,7 +291,6 @@ export const ChatV2 = () => {
293
291
}
294
292
} , [ userStatus , course ] )
295
293
296
- const showRagSelector = ( ragIndices ?. length ?? 0 ) > 0
297
294
const rightMenuOpen = ! ! activeToolResult
298
295
const rightMenuWidth = rightMenuOpen ? '300px' : '0px'
299
296
const leftMenuWidth = ! isEmbeddedMode ? { md : '250px' , lg : '300px' } : { md : '0px' , lg : '0px' }
@@ -380,20 +377,13 @@ export const ChatV2 = () => {
380
377
} }
381
378
>
382
379
< LeftMenu
383
- course = { course }
384
380
handleReset = { handleReset }
385
381
onClose = { ( ) => {
386
382
setChatLeftSidePanelOpen ( false )
387
383
} }
388
384
setSettingsModalOpen = { setSettingsModalOpen }
389
385
setDisclaimerStatus = { setDisclaimerStatus }
390
- showRagSelector = { showRagSelector }
391
- ragIndex = { ragIndex }
392
- setRagIndexId = { setRagIndexId }
393
- ragIndices = { ragIndices }
394
386
messages = { messages }
395
- activePrompt = { activePrompt }
396
- setActivePrompt = { setActivePrompt }
397
387
currentModel = { activeModel }
398
388
setModel = { setActiveModel }
399
389
availableModels = { allowedModels }
@@ -406,17 +396,10 @@ export const ChatV2 = () => {
406
396
position : 'fixed' ,
407
397
top : 0 ,
408
398
} }
409
- course = { course }
410
399
handleReset = { handleReset }
411
400
setSettingsModalOpen = { setSettingsModalOpen }
412
401
setDisclaimerStatus = { setDisclaimerStatus }
413
- showRagSelector = { showRagSelector }
414
- ragIndex = { ragIndex }
415
- setRagIndexId = { setRagIndexId }
416
- ragIndices = { ragIndices }
417
402
messages = { messages }
418
- activePrompt = { activePrompt }
419
- setActivePrompt = { setActivePrompt }
420
403
currentModel = { activeModel }
421
404
setModel = { setActiveModel }
422
405
availableModels = { allowedModels }
@@ -575,13 +558,8 @@ export const ChatV2 = () => {
575
558
< SettingsModal
576
559
open = { settingsModalOpen }
577
560
setOpen = { setSettingsModalOpen }
578
- customSystemMessage = { customSystemMessage }
579
- setCustomSystemMessage = { setCustomSystemMessage }
580
- activePrompt = { activePrompt }
581
- setActivePrompt = { setActivePrompt }
582
561
modelTemperature = { parseFloat ( modelTemperature ) }
583
562
setModelTemperature = { ( updatedTemperature ) => setModelTemperature ( String ( updatedTemperature ) ) }
584
- course = { course }
585
563
/>
586
564
587
565
< DisclaimerModal disclaimer = { disclaimerInfo } disclaimerStatus = { disclaimerStatus } setDisclaimerStatus = { setDisclaimerStatus } />
@@ -596,13 +574,7 @@ const LeftMenu = ({
596
574
onClose,
597
575
setSettingsModalOpen,
598
576
setDisclaimerStatus,
599
- showRagSelector,
600
- ragIndex,
601
- setRagIndexId,
602
- ragIndices,
603
577
messages,
604
- activePrompt,
605
- setActivePrompt,
606
578
currentModel,
607
579
setModel,
608
580
availableModels,
@@ -613,13 +585,7 @@ const LeftMenu = ({
613
585
onClose ?: ( ) => void
614
586
setSettingsModalOpen : React . Dispatch < React . SetStateAction < boolean > >
615
587
setDisclaimerStatus : React . Dispatch < React . SetStateAction < boolean > >
616
- showRagSelector : boolean
617
- ragIndex ?: RagIndexAttributes
618
- setRagIndexId : React . Dispatch < React . SetStateAction < number | undefined > >
619
- ragIndices ?: RagIndexAttributes [ ]
620
588
messages : ChatMessage [ ]
621
- activePrompt : Prompt | undefined
622
- setActivePrompt : ( prompt : Prompt | undefined ) => void
623
589
currentModel : string
624
590
setModel : ( model : string ) => void
625
591
availableModels : string [ ]
@@ -628,11 +594,6 @@ const LeftMenu = ({
628
594
const { courseId } = useParams ( )
629
595
const { userStatus, isLoading : statusLoading } = useUserStatus ( courseId )
630
596
const [ isTokenLimitExceeded , setIsTokenLimitExceeded ] = useState < boolean > ( false )
631
- const urlPromptId = useUrlPromptId ( )
632
- const { data : myPrompts } = useQuery < Prompt [ ] > ( {
633
- queryKey : [ '/prompts/my-prompts' ] ,
634
- initialData : [ ] ,
635
- } )
636
597
637
598
useEffect ( ( ) => {
638
599
if ( ! userStatus ) return
@@ -661,28 +622,14 @@ const LeftMenu = ({
661
622
{ t ( 'chat:emptyConversation' ) }
662
623
</ OutlineButtonBlack >
663
624
< ModelSelector currentModel = { currentModel } setModel = { setModel } availableModels = { availableModels } isTokenLimitExceeded = { isTokenLimitExceeded } />
664
- < PromptSelector
665
- sx = { { width : '100%' } }
666
- coursePrompts = { course ?. prompts ?? [ ] }
667
- myPrompts = { myPrompts }
668
- activePrompt = { activePrompt }
669
- setActivePrompt = { setActivePrompt }
670
- mandatoryPrompt = { course ?. prompts . find ( ( p ) => p . mandatory ) }
671
- urlPrompt = { course ?. prompts . find ( ( p ) => p . id === urlPromptId ) }
672
- />
625
+ < PromptSelector sx = { { width : '100%' } } />
673
626
< EmailButton messages = { messages } disabled = { ! messages ?. length } />
674
627
< OutlineButtonBlack startIcon = { < Tune /> } onClick = { ( ) => setSettingsModalOpen ( true ) } data-testid = "settings-button" >
675
628
{ t ( 'chat:settings' ) }
676
629
</ OutlineButtonBlack >
677
630
< OutlineButtonBlack startIcon = { < HelpIcon /> } onClick = { ( ) => setDisclaimerStatus ( true ) } data-testid = "help-button" >
678
631
{ t ( 'info:title' ) }
679
632
</ OutlineButtonBlack >
680
- { course && showRagSelector && (
681
- < >
682
- < RagSelectorDescription />
683
- < RagSelector currentRagIndex = { ragIndex } setRagIndex = { setRagIndexId } ragIndices = { ragIndices ?? [ ] } />
684
- </ >
685
- ) }
686
633
</ Box >
687
634
</ Box >
688
635
{ onClose && (
@@ -694,3 +641,9 @@ const LeftMenu = ({
694
641
</ Box >
695
642
)
696
643
}
644
+
645
+ export const ChatV2 = ( ) => (
646
+ < PromptStateProvider >
647
+ < ChatV2Content />
648
+ </ PromptStateProvider >
649
+ )
0 commit comments