1+ import { Conversation , ChatMessage , ToolMessageContent } from '../api/models'
2+
3+
4+ // -------------Chat.tsx-------------
5+
6+ const enum contentTemplateSections {
7+ NewLine = '\n\n' ,
8+ Intro = 'The proposal will include the following sections:' ,
9+ Closing = 'Does this look good? If so, you can **generate the document** now. You can also ask me to **add an item** or **change the order of the sections**.' ,
10+ JSONParseError = 'I was unable to find content related to your query and could not generate a template. Please try again.' ,
11+ JSONStructureError = 'Unable to render the sections within the template. Please try again.'
12+ }
13+
14+
15+ export const parseCitationFromMessage = ( message : ChatMessage ) => {
16+ if ( message ?. role && message ?. role === 'tool' ) {
17+ try {
18+ const toolMessage = JSON . parse ( message . content ) as ToolMessageContent
19+ return toolMessage . citations
20+ } catch {
21+ return [ ]
22+ }
23+ }
24+ return [ ]
25+ }
26+
27+ export const cleanJSON = ( jsonString : string ) => {
28+ try {
29+ let lines : string [ ]
30+ let cleanString = ''
31+ lines = jsonString . split ( '\n' )
32+ lines ?. forEach ( ( line : string ) => {
33+ if ( ! line . includes ( 'json' ) && ! line . includes ( '```' ) ) {
34+ cleanString += line . trim ( )
35+ }
36+ } )
37+ return cleanString
38+ } catch ( e ) {
39+ return ''
40+ }
41+ }
42+
43+ export const generateTemplateSections = ( jsonString : string ) => {
44+ let jsonResponse
45+ try {
46+ let cleanString = cleanJSON ( jsonString )
47+ jsonResponse = JSON . parse ( cleanString )
48+ } catch ( e ) {
49+ return contentTemplateSections . JSONParseError
50+ }
51+
52+ if ( ! Array . isArray ( jsonResponse . template ) ) {
53+ return contentTemplateSections . JSONStructureError
54+ }
55+
56+ let sections = `${ contentTemplateSections . Intro } ${ contentTemplateSections . NewLine } `
57+ jsonResponse . template . forEach ( ( row : { section_title : string } ) => {
58+ sections += `${ row . section_title } ${ contentTemplateSections . NewLine } `
59+ } )
60+ sections += `${ contentTemplateSections . Closing } `
61+ return sections . trim ( ) // Remove any trailing newline
62+ }
63+
64+
65+ export const tryGetRaiPrettyError = ( errorMessage : string ) => {
66+ try {
67+ // Using a regex to extract the JSON part that contains "innererror"
68+ const match = errorMessage . match ( / ' i n n e r e r r o r ' : ( { .* } ) \} \} / )
69+ if ( match ) {
70+ // Replacing single quotes with double quotes and converting Python-like booleans to JSON booleans
71+ const fixedJson = match [ 1 ]
72+ . replace ( / ' / g, '"' )
73+ . replace ( / \b T r u e \b / g, 'true' )
74+ . replace ( / \b F a l s e \b / g, 'false' )
75+ const innerErrorJson = JSON . parse ( fixedJson )
76+ let reason = ''
77+ // Check if jailbreak content filter is the reason of the error
78+ const jailbreak = innerErrorJson . content_filter_result . jailbreak
79+ if ( jailbreak . filtered === true ) {
80+ reason = 'Jailbreak'
81+ }
82+
83+ // Returning the prettified error message
84+ if ( reason !== '' ) {
85+ return (
86+ 'The prompt was filtered due to triggering Azure OpenAI’s content filtering system.\n' +
87+ 'Reason: This prompt contains content flagged as ' +
88+ reason +
89+ '\n\n' +
90+ 'Please modify your prompt and retry. Learn more: https://go.microsoft.com/fwlink/?linkid=2198766'
91+ )
92+ }
93+ }
94+ } catch ( e ) {
95+ console . error ( 'Failed to parse the error:' , e )
96+ }
97+ return errorMessage
98+ }
99+
100+
101+ export const parseErrorMessage = ( errorMessage : string ) => {
102+ let errorCodeMessage = errorMessage . substring ( 0 , errorMessage . indexOf ( '-' ) + 1 )
103+ const innerErrorCue = "{\\'error\\': {\\'message\\': "
104+ if ( errorMessage . includes ( innerErrorCue ) ) {
105+ try {
106+ let innerErrorString = errorMessage . substring ( errorMessage . indexOf ( innerErrorCue ) )
107+ if ( innerErrorString . endsWith ( "'}}" ) ) {
108+ innerErrorString = innerErrorString . substring ( 0 , innerErrorString . length - 3 )
109+ }
110+ innerErrorString = innerErrorString . replaceAll ( "\\'" , "'" )
111+ let newErrorMessage = errorCodeMessage + ' ' + innerErrorString
112+ errorMessage = newErrorMessage
113+ } catch ( e ) {
114+ console . error ( 'Error parsing inner error message: ' , e )
115+ }
116+ }
117+
118+ return tryGetRaiPrettyError ( errorMessage )
119+ }
0 commit comments