11import { Relate } from "@/app/interfaces/relate" ;
22import { Source } from "@/app/interfaces/source" ;
33import { fetchStream } from "@/app/utils/fetch-stream" ;
4+ import { LocalHistory } from "@/app/interfaces/history" ;
45
56const LLM_SPLIT = "__LLM_RESPONSE__" ;
67const RELATED_SPLIT = "__RELATED_QUESTIONS__" ;
@@ -12,6 +13,7 @@ export const parseStreaming = async (
1213 onSources : ( value : Source [ ] ) => void ,
1314 onMarkdown : ( value : string ) => void ,
1415 onRelates : ( value : Relate [ ] ) => void ,
16+ onFinish : ( result : LocalHistory ) => void ,
1517 onError ?: ( status : number ) => void ,
1618) => {
1719 const decoder = new TextDecoder ( ) ;
@@ -30,18 +32,19 @@ export const parseStreaming = async (
3032 search_uuid,
3133 } ) ,
3234 } ) ;
35+ let finalRelates : Relate [ ] = [ ] ;
36+ let finalMarkdown : string = "" ;
37+ let finalSources : Source [ ] = [ ] ;
3338 if ( response . status !== 200 ) {
3439 onError ?.( response . status ) ;
3540 return ;
3641 }
3742 const markdownParse = ( text : string ) => {
38- onMarkdown (
39- text
40- . replace ( / \[ \[ ( [ c C ] ) i t a t i o n / g, "[citation" )
41- . replace ( / [ c C ] i t a t i o n : ( \d + ) ] ] / g, "citation:$1]" )
42- . replace ( / \[ \[ ( [ c C ] i t a t i o n : \d + ) ] ] (? ! ] ) / g, `[$1]` )
43- . replace ( / \[ [ c C ] i t a t i o n : ( \d + ) ] / g, "[citation]($1)" ) ,
44- ) ;
43+ return text
44+ . replace ( / \[ \[ ( [ c C ] ) i t a t i o n / g, "[citation" )
45+ . replace ( / [ c C ] i t a t i o n : ( \d + ) ] ] / g, "citation:$1]" )
46+ . replace ( / \[ \[ ( [ c C ] i t a t i o n : \d + ) ] ] (? ! ] ) / g, `[$1]` )
47+ . replace ( / \[ [ c C ] i t a t i o n : ( \d + ) ] / g, "[citation]($1)" ) ;
4548 } ;
4649 fetchStream (
4750 response ,
@@ -52,27 +55,38 @@ export const parseStreaming = async (
5255 const [ sources , rest ] = chunks . split ( LLM_SPLIT ) ;
5356 if ( ! sourcesEmitted ) {
5457 try {
55- onSources ( JSON . parse ( sources ) ) ;
58+ finalSources = JSON . parse ( sources ) ;
5659 } catch ( e ) {
57- onSources ( [ ] ) ;
60+ finalSources = [ ] ;
5861 }
62+ onSources ( finalSources ) ;
5963 }
6064 sourcesEmitted = true ;
6165 if ( rest . includes ( RELATED_SPLIT ) ) {
6266 const [ md ] = rest . split ( RELATED_SPLIT ) ;
63- markdownParse ( md ) ;
67+ finalMarkdown = markdownParse ( md ) ;
6468 } else {
65- markdownParse ( rest ) ;
69+ finalMarkdown = markdownParse ( rest ) ;
6670 }
71+ onMarkdown ( finalMarkdown ) ;
6772 }
6873 } ,
6974 ( ) => {
7075 const [ _ , relates ] = chunks . split ( RELATED_SPLIT ) ;
7176 try {
72- onRelates ( JSON . parse ( relates ) ) ;
77+ finalRelates = JSON . parse ( relates ) ;
7378 } catch ( e ) {
74- onRelates ( [ ] ) ;
79+ finalRelates = [ ] ;
7580 }
81+ onRelates ( finalRelates ) ;
82+ onFinish ( {
83+ markdown : finalMarkdown ,
84+ sources : finalSources ,
85+ relates : finalRelates ,
86+ rid : search_uuid ,
87+ query,
88+ timestamp : new Date ( ) . valueOf ( ) ,
89+ } ) ;
7690 } ,
7791 ) ;
7892} ;
0 commit comments