@@ -6,7 +6,7 @@ import useLocalStorageState from '../../hooks/useLocalStorageState'
6
6
import { DEFAULT_MODEL } from '../../../config'
7
7
import useInfoTexts from '../../hooks/useInfoTexts'
8
8
import { Message } from '../../types'
9
- import { FileCitation , ResponseStreamEventData } from '../../../shared/types'
9
+ import { FileCitation , FileSearchResult , ResponseStreamEventData } from '../../../shared/types'
10
10
import useRetryTimeout from '../../hooks/useRetryTimeout'
11
11
import { useTranslation } from 'react-i18next'
12
12
import { handleCompletionStreamError } from './error'
@@ -49,7 +49,7 @@ export const ChatV2 = () => {
49
49
const [ activePromptId , setActivePromptId ] = useState ( '' )
50
50
const [ fileName , setFileName ] = useState < string > ( '' )
51
51
const [ completion , setCompletion ] = useState ( '' )
52
- const [ citations , setCitations ] = useState < FileCitation [ ] > ( [ ] )
52
+ const [ fileSearchResult , setFileSearchResult ] = useLocalStorageState < FileSearchResult > ( 'last-file-search' , null )
53
53
const [ streamController , setStreamController ] = useState < AbortController > ( )
54
54
const [ alertOpen , setAlertOpen ] = useState ( false )
55
55
const [ disallowedFileType , setDisallowedFileType ] = useState ( '' )
@@ -75,7 +75,7 @@ export const ChatV2 = () => {
75
75
const reader = stream . getReader ( )
76
76
77
77
let content = ''
78
- const citations : FileCitation [ ] = [ ]
78
+ let fileSearchResult : FileSearchResult
79
79
80
80
while ( true ) {
81
81
const { value, done } = await reader . read ( )
@@ -86,7 +86,12 @@ export const ChatV2 = () => {
86
86
for ( const chunk of data . split ( '\n' ) ) {
87
87
if ( ! chunk || chunk . trim ( ) . length === 0 ) continue
88
88
89
- const parsedChunk : ResponseStreamEventData = JSON . parse ( chunk )
89
+ let parsedChunk : ResponseStreamEventData = null
90
+ try {
91
+ parsedChunk = JSON . parse ( chunk )
92
+ } catch ( _e ) {
93
+ console . error ( 'Could not parse the chunk:' , chunk )
94
+ }
90
95
91
96
switch ( parsedChunk . type ) {
92
97
case 'writing' :
@@ -96,8 +101,11 @@ export const ChatV2 = () => {
96
101
97
102
case 'annotation' :
98
103
console . log ( 'Received annotation:' , parsedChunk . annotation )
99
- setCitations ( ( prev ) => [ ...prev , parsedChunk . annotation ] )
100
- citations . push ( parsedChunk . annotation )
104
+ break
105
+
106
+ case 'fileSearchDone' :
107
+ fileSearchResult = parsedChunk . fileSearch
108
+ setFileSearchResult ( parsedChunk . fileSearch )
101
109
break
102
110
103
111
case 'complete' :
@@ -115,8 +123,7 @@ export const ChatV2 = () => {
115
123
}
116
124
}
117
125
118
- setMessages ( ( prev : Message [ ] ) => prev . concat ( { role : 'assistant' , content, citations } ) )
119
- setCitations ( [ ] )
126
+ setMessages ( ( prev : Message [ ] ) => prev . concat ( { role : 'assistant' , content, fileSearchResult } ) )
120
127
} catch ( err : any ) {
121
128
handleCompletionStreamError ( err , fileName )
122
129
} finally {
@@ -135,7 +142,7 @@ export const ChatV2 = () => {
135
142
setMessage ( { content : '' } )
136
143
setPrevResponse ( { id : '' } )
137
144
setCompletion ( '' )
138
- setCitations ( [ ] )
145
+ setFileSearchResult ( undefined )
139
146
setStreamController ( new AbortController ( ) )
140
147
setRetryTimeout ( ( ) => {
141
148
if ( streamController ) {
@@ -176,6 +183,7 @@ export const ChatV2 = () => {
176
183
setMessage ( { content : '' } )
177
184
setPrevResponse ( { id : '' } )
178
185
setCompletion ( '' )
186
+ setFileSearchResult ( null )
179
187
setStreamController ( undefined )
180
188
setTokenUsageWarning ( '' )
181
189
setTokenWarningVisible ( false )
@@ -218,8 +226,8 @@ export const ChatV2 = () => {
218
226
{ courseId ? < Link to = { '/v2' } > CurreChat</ Link > : < Link to = { '/v2/sandbox' } > Ohtu Sandbox</ Link > }
219
227
</ Box >
220
228
< Box sx = { { display : 'flex' } } >
221
- < Box ref = { chatContainerRef } >
222
- < Conversation messages = { messages } completion = { completion } citations = { citations } />
229
+ < Box ref = { chatContainerRef } flex = { 1 } >
230
+ < Conversation messages = { messages } completion = { completion } fileSearchResult = { fileSearchResult } />
223
231
< ChatBox
224
232
disabled = { false }
225
233
onSubmit = { ( message ) => {
@@ -230,7 +238,11 @@ export const ChatV2 = () => {
230
238
} }
231
239
/>
232
240
</ Box >
233
- { ragIndex && < CitationsBox messages = { messages } citations = { citations } ragIndex = { ragIndex } /> }
241
+ { ragIndex && (
242
+ < Box flex = { 1 } >
243
+ < CitationsBox messages = { messages } fileSearchResult = { fileSearchResult } />
244
+ </ Box >
245
+ ) }
234
246
</ Box >
235
247
</ Box >
236
248
)
0 commit comments