1
1
import { renderToStaticMarkup } from "react-dom/server" ;
2
- import { getCitationFilePath } from "../../api" ;
2
+ import { ChatAppResponse , getCitationFilePath } from "../../api" ;
3
3
4
4
type HtmlParsedAnswer = {
5
5
answerHtml : string ;
6
6
citations : string [ ] ;
7
7
} ;
8
8
9
- // Function to check citation format
10
- // Citation format: AnyFileName.anyExtension
11
- function isValidCitation ( citation : string ) : boolean {
9
+ // Function to validate citation format and check if it is a valid citation within the context
10
+ function isCitationValid ( contextDataPoints : any , citationCandidate : string ) : boolean {
12
11
const regex = / ^ [ ^ \s ] + \. [ a - z A - Z 0 - 9 ] + / ;
13
- return regex . test ( citation ) ;
12
+ if ( ! regex . test ( citationCandidate ) ) {
13
+ return false ;
14
+ }
15
+
16
+ // Check if contextDataPoints is an object with a text property that is an array
17
+ let dataPointsArray : string [ ] ;
18
+ if ( Array . isArray ( contextDataPoints ) ) {
19
+ dataPointsArray = contextDataPoints ;
20
+ } else if ( contextDataPoints && Array . isArray ( contextDataPoints . text ) ) {
21
+ dataPointsArray = contextDataPoints . text ;
22
+ } else {
23
+ return false ;
24
+ }
25
+
26
+ // Check if the citation is included in any of the strings within the text array
27
+ const isValidCitation = dataPointsArray . some ( dataPoint => dataPoint . includes ( citationCandidate ) ) ;
28
+
29
+ if ( ! isValidCitation ) {
30
+ return false ;
31
+ }
32
+
33
+ return true ;
14
34
}
15
35
16
- export function parseAnswerToHtml ( answer : string , isStreaming : boolean , onCitationClicked : ( citationFilePath : string ) => void ) : HtmlParsedAnswer {
36
+ export function parseAnswerToHtml ( answer : ChatAppResponse , isStreaming : boolean , onCitationClicked : ( citationFilePath : string ) => void ) : HtmlParsedAnswer {
37
+ const contextDataPoints = answer . context . data_points ;
17
38
const citations : string [ ] = [ ] ;
18
39
19
40
// trim any whitespace from the end of the answer after removing follow-up questions
20
- let parsedAnswer = answer . trim ( ) ;
41
+ let parsedAnswer = answer . message . content . trim ( ) ;
21
42
22
43
// Omit a citation that is still being typed during streaming
23
44
if ( isStreaming ) {
@@ -42,7 +63,7 @@ export function parseAnswerToHtml(answer: string, isStreaming: boolean, onCitati
42
63
} else {
43
64
let citationIndex : number ;
44
65
45
- if ( ! isValidCitation ( part ) ) {
66
+ if ( ! isCitationValid ( contextDataPoints , part ) ) {
46
67
return `[${ part } ]` ;
47
68
}
48
69
0 commit comments