@@ -50,14 +50,28 @@ export const ResearchTopicCard = (): JSX.Element => {
5050 }
5151
5252 const generatedSection = await documentSectionGenerate ( appStateContext ?. state . researchTopic , documentSection )
53- if ( ( generatedSection ?. body ) != null && ( generatedSection ?. status ) != 400 ) {
54- set_is_bad_request ( false )
55- const response = await generatedSection . json ( )
56- return response . content
57- } else {
58- setTimeout ( ( ) => {
59- set_is_bad_request ( true )
60- } , 2000 )
53+ if ( ! generatedSection ) {
54+ console . error ( 'No response from /draft_document/generate_section' )
55+ set_is_bad_request ( true )
56+ return ''
57+ }
58+
59+ // If backend rejected (e.g., unethical input), show message; do NOT try to parse JSON
60+ if ( ! generatedSection . ok ) {
61+ console . error ( `POST /draft_document/generate_section ${ generatedSection . status } (Bad Request)` )
62+ set_is_bad_request ( true )
63+ return ''
64+ }
65+
66+ // 200 OK -> read text and TRY to parse JSON; if not JSON, treat as error but keep UI responsive
67+ try {
68+ const bodyText = await generatedSection . text ( )
69+ const response = JSON . parse ( bodyText )
70+ // do NOT clear error here; success of one section must not hide an earlier failure
71+ return response . content
72+ } catch ( e ) {
73+ console . error ( 'Unexpected non-JSON response from /draft_document/generate_section' , e )
74+ set_is_bad_request ( true )
6175 return ''
6276 }
6377 }
@@ -105,26 +119,37 @@ export const ResearchTopicCard = (): JSX.Element => {
105119 open = { open }
106120 onOpenChange = { async ( event , data ) => {
107121 setOpen ( data . open )
122+ if ( ! data . open ) return
123+
124+ set_is_bad_request ( false ) // ← add this: start clean for this run
125+
108126 const documentSections = appStateContext ?. state . documentSections ?? [ ]
109- const newDocumentSectionContent = [ ]
110- for ( let i = 0 ; i < documentSections . length ; i ++ ) {
111- const section = documentSections [ i ]
112- newDocumentSectionContent . push ( callGenerateSectionContent ( section ) )
113- }
114- const newDocumentSections = await Promise . all ( newDocumentSectionContent )
115- for ( let i = 0 ; i < newDocumentSections . length ; i ++ ) {
116- if ( newDocumentSections [ i ] === '' ) {
117- documentSections [ i ] . content = newDocumentSections [ i ]
118- console . error ( 'Error generating section content' )
119- setOpen ( false )
120- } else {
121- documentSections [ i ] . content = newDocumentSections [ i ]
122- documentSections [ i ] . metaPrompt = documentSectionPrompt ( documentSections [ i ] . title , appStateContext ?. state . researchTopic ?? '' )
127+ const newDocumentSectionContent : Array < Promise < string > > = [ ]
128+
129+ try {
130+ for ( let i = 0 ; i < documentSections . length ; i ++ ) {
131+ const section = documentSections [ i ]
132+ newDocumentSectionContent . push ( callGenerateSectionContent ( section ) )
133+ }
134+ const newDocumentSections = await Promise . all ( newDocumentSectionContent )
135+ for ( let i = 0 ; i < newDocumentSections . length ; i ++ ) {
136+ if ( newDocumentSections [ i ] === '' ) {
137+ documentSections [ i ] . content = SystemErrMessage
138+ console . error ( 'Error generating section content' )
139+ } else {
140+ documentSections [ i ] . content = newDocumentSections [ i ]
141+ documentSections [ i ] . metaPrompt = documentSectionPrompt ( documentSections [ i ] . title , appStateContext ?. state . researchTopic ?? '' )
142+ }
123143 }
124- }
125144
126- appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : documentSections } )
127- setOpen ( false )
145+ appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : documentSections } )
146+ } catch ( err ) {
147+ console . error ( 'Generation failed:' , err )
148+ set_is_bad_request ( true )
149+ } finally {
150+ // ALWAYS close the overlay
151+ setOpen ( false )
152+ }
128153 } }
129154 >
130155 < DialogTrigger disableButtonEnhancement >
@@ -170,22 +195,29 @@ export const Card = (props: CardProps) => {
170195 const generatedSection = await documentSectionGenerate ( appStateContext ?. state . researchTopic || '' , { ...section , metaPrompt : newPrompt } )
171196
172197 const updatedDocumentSections = [ ...documentSections ]
173- if ( generatedSection && generatedSection . body && generatedSection ?. status != 400 ) {
174- const response = await generatedSection . json ( )
175- const newContent = response . content
176- updatedDocumentSections [ index ] . content = newContent
177- appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : updatedDocumentSections } )
178- setLoading ( false )
198+ if ( generatedSection && generatedSection . ok ) {
199+ try {
200+ const bodyText = await generatedSection . text ( )
201+ const response = JSON . parse ( bodyText )
202+ const newContent = response . content
203+ updatedDocumentSections [ index ] . content = newContent
204+ appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : updatedDocumentSections } )
205+ } catch ( e ) {
206+ console . error ( 'Unexpected non-JSON 200 response from /draft_document/generate_section' , e )
207+ updatedDocumentSections [ index ] . content = SystemErrMessage
208+ appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : updatedDocumentSections } )
209+ }
179210 } else if ( ( generatedSection ?. body ) != null && ( generatedSection ?. status ) === 400 ) {
211+ console . error ( 'Guardrail/Bad Request while generating section (Section card).' )
180212 updatedDocumentSections [ index ] . content = SystemErrMessage
181213 appStateContext ?. dispatch ( { type : 'UPDATE_DRAFT_DOCUMENTS_SECTIONS' , payload : updatedDocumentSections } )
182- setLoading ( false )
183214 }
184215 } else {
185216 console . error ( 'Section information is undefined.' )
186217 }
187218 } catch ( error ) {
188219 console . error ( 'Error generating section:' , error )
220+ } finally {
189221 setLoading ( false )
190222 }
191223 }
0 commit comments