Skip to content

Commit 6b4d7bd

Browse files
Merge pull request #242 from microsoft/section-generate-fix
fix: Section generate issue fix
2 parents ae9438c + 41da683 commit 6b4d7bd

File tree

10 files changed

+106
-30
lines changed

10 files changed

+106
-30
lines changed

frontend/src/api/api.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,6 @@ export const historyMessageFeedback = async (messageId: string, feedback: string
372372
}
373373

374374
export const sectionGenerate = async (options: SectionGenerateRequest): Promise<Response> => {
375-
// set timeout to 10 seconds
376-
const abortController = new AbortController()
377-
const abortSignal = abortController.signal
378-
379-
const timeout = setTimeout(() => {
380-
abortController.abort()
381-
}, 10000)
382-
383375
let body = JSON.stringify({
384376
sectionTitle: options.sectionTitle,
385377
sectionDescription: options.sectionDescription
@@ -390,15 +382,12 @@ export const sectionGenerate = async (options: SectionGenerateRequest): Promise<
390382
headers: {
391383
'Content-Type': 'application/json'
392384
},
393-
body: body,
394-
signal: abortSignal
385+
body: body
395386
})
396387
.then(res => {
397-
clearTimeout(timeout)
398388
return res
399389
})
400390
.catch(_err => {
401-
clearTimeout(timeout)
402391
console.error('There was an issue fetching your data.')
403392
return new Response(
404393
JSON.stringify({ section_content: 'There was an issue fetching your data. Please try again.' })

frontend/src/components/ChatHistory/ChatHistoryList.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const mockState = {
2525
draftedDocumentTitle: '',
2626
isGenerating: false,
2727
isRequestInitiated: false,
28+
failedSections : [],
29+
isFailedReqInitiated : false
2830
};
2931

3032
const renderChatHistoryList = (stateOverride = {}) => {

frontend/src/components/ChatHistory/ChatHistoryPanel.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const mockState = {
5252
draftedDocumentTitle: 'Some Title',
5353
isGenerating: false,
5454
isRequestInitiated: false,
55+
failedSections : [],
56+
isFailedReqInitiated : false
5557
};
5658

5759
const mockDispatch = jest.fn();

frontend/src/components/DraftCards/SectionCard.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ const mockState = {
5353
draftedDocumentTitle: '',
5454

5555
isGenerating: false,
56-
isRequestInitiated: false
56+
isRequestInitiated: false,
57+
failedSections : [],
58+
isFailedReqInitiated : false
5759
}
5860

5961
const renderWithContext = (idx = 0) =>

frontend/src/components/DraftCards/SectionCard.tsx

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,72 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
138138
setCharCount(sectionContent.length)
139139
}, [location])
140140

141+
142+
useEffect(() => {
143+
if (appStateContext.state?.failedSections.length >0 && appStateContext.state?.failedSections[0].title === sectionTitle && isLoading && !appStateContext.state.isFailedReqInitiated) {
144+
console.log("appStateContext.state?.failedSections", appStateContext.state?.failedSections);
145+
const tempItem = {
146+
title: sectionTitle,
147+
description: sectionDescription,
148+
content: sectionContent
149+
}
150+
appStateContext?.dispatch({ type: 'REMOVED_FAILED_SECTION', payload: {section : tempItem} })
151+
appStateContext?.dispatch({ type: 'UPDATE_SECTION_API_REQ_STATUS', payload: true })
152+
fetchSectionContent(sectionTitle,sectionDescription, 'failed');
153+
}
154+
}, [appStateContext.state.failedSections]);
155+
141156
const handleOpenChange: PopoverProps['onOpenChange'] = (e, data) => setIsPopoverOpen(data.open || false)
142157

143-
async function fetchSectionContent(sectionTitle: string, sectionDescription: string) {
158+
async function fetchSectionContent(sectionTitle: string, sectionDescription: string , isReqFrom = '') {
144159
setIsLoading(true)
145160
const sectionGenerateRequest: SectionGenerateRequest = { sectionTitle, sectionDescription }
146161

147162
const response = await sectionGenerate(sectionGenerateRequest)
148163
const responseBody = await response.json()
149164

150-
const updatedSection: Section = {
151-
title: sectionTitle,
152-
description: sectionDescription,
153-
content: responseBody.section_content
154-
}
155-
appStateContext?.dispatch({ type: 'UPDATE_SECTION', payload: { sectionIdx: sectionIdx, section: updatedSection } })
156-
let content = updatedSection.content || ''
165+
if(responseBody?.error?.includes("429")) {
166+
console.log("retriggerd !!!")
167+
const failedSectionItems = {
168+
title: sectionTitle,
169+
description: sectionDescription,
170+
content: sectionContent
171+
}
172+
appStateContext?.dispatch({ type: 'ADD_FAILED_SECTION', payload: failedSectionItems })
173+
if(isReqFrom == 'failed')
174+
appStateContext?.dispatch({ type: 'UPDATE_SECTION_API_REQ_STATUS', payload: false })
175+
176+
setTimeout(()=>{
177+
},5000)
178+
179+
}else{
180+
const updatedSection: Section = {
181+
title: sectionTitle,
182+
description: sectionDescription,
183+
content: responseBody.section_content
184+
}
185+
appStateContext?.dispatch({ type: 'UPDATE_SECTION', payload: { sectionIdx: sectionIdx, section: updatedSection } })
186+
let content = updatedSection.content || ''
187+
188+
// limit the character count to 2000
189+
if (content.length > sectionCharacterLimit) {
190+
content = content.slice(0, sectionCharacterLimit)
191+
}
192+
193+
setCharCount(content.length)
194+
setIsLoading(false)
195+
196+
appStateContext?.dispatch({ type: 'REMOVED_FAILED_SECTION', payload: {section : updatedSection} })
157197

158-
// limit the character count to 2000
159-
if (content.length > sectionCharacterLimit) {
160-
content = content.slice(0, sectionCharacterLimit)
198+
if(isReqFrom == 'failed')
199+
appStateContext?.dispatch({ type: 'UPDATE_SECTION_API_REQ_STATUS', payload: false })
161200
}
162201

163-
setCharCount(content.length)
164-
setIsLoading(false)
202+
165203
}
166204

205+
206+
167207
useEffect(() => {
168208
if (sectionContent === '' && !isLoading && !isManuallyCleared) {
169209
fetchSectionContent(sectionTitle, sectionDescription)

frontend/src/components/Sidebar/Sidebar.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const mockState = {
5252
draftedDocumentTitle: '',
5353
isGenerating: false,
5454
isRequestInitiated: false,
55+
failedSections : [],
56+
isFailedReqInitiated : false
5557
};
5658
const mockState2 = {
5759
isChatHistoryOpen: false,

frontend/src/pages/draft/Draft.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { useContext } from 'react'
1+
import { useContext, useEffect, useState } from 'react'
22
import styles from './Draft.module.css'
33
import { useLocation, useNavigate } from 'react-router-dom'
44
import TitleCard from '../../components/DraftCards/TitleCard'
55
import SectionCard from '../../components/DraftCards/SectionCard'
66
import { Document, Packer, Paragraph, TextRun } from 'docx'
77
import { saveAs } from 'file-saver'
88
import { AppStateContext } from '../../state/AppProvider'
9-
import { CommandBarButton, Stack } from '@fluentui/react'
9+
import { CommandBarButton, Stack } from '@fluentui/react';
10+
import { Section } from '../../api/models'
1011

1112
const Draft = (): JSX.Element => {
1213
const appStateContext = useContext(AppStateContext)
@@ -16,9 +17,24 @@ const Draft = (): JSX.Element => {
1617
// get draftedDocument from context
1718
const draftedDocument = appStateContext?.state.draftedDocument
1819
const sections = draftedDocument?.sections ?? []
20+
21+
const [sectionItems , setSectionItems] = useState<Section[]>([])
1922
const aiWarningLabel = 'AI-generated content may be incorrect'
2023

2124
// redirect to home page if draftedDocument is empty
25+
26+
useEffect(() => {
27+
sections.forEach((item, index) => {
28+
setTimeout(() => {
29+
setSectionItems((prev) => [...prev, item]);
30+
}, index * 500);
31+
});
32+
}, []);
33+
34+
useEffect(()=>{
35+
console.log("sectionItems", sectionItems)
36+
},[sectionItems])
37+
2238
if (!draftedDocument) {
2339
navigate('/')
2440
}
@@ -100,7 +116,7 @@ const Draft = (): JSX.Element => {
100116
return (
101117
<Stack className={styles.container}>
102118
<TitleCard />
103-
{(sections ?? []).map((_, index) => (
119+
{(sectionItems ?? []).map((_, index : any) => (
104120
<SectionCard key={index} sectionIdx={index} />
105121
))}
106122
<Stack className={styles.buttonContainer}>

frontend/src/state/AppProvider.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export interface AppState {
3030
draftedDocument: DraftedDocument | null
3131
draftedDocumentTitle: string
3232
isGenerating: boolean
33-
isRequestInitiated : boolean
33+
isRequestInitiated : boolean,
34+
failedSections : Section[],
35+
isFailedReqInitiated : boolean,
3436
}
3537

3638
export type Action =
@@ -59,6 +61,10 @@ export type Action =
5961
| { type: 'GENERATE_ISLODING'; payload: boolean }
6062
| { type: 'SET_IS_REQUEST_INITIATED'; payload: boolean }
6163

64+
| { type: 'ADD_FAILED_SECTION'; payload: Section }
65+
| { type: 'REMOVED_FAILED_SECTION'; payload: {section : Section} }
66+
| { type: 'UPDATE_SECTION_API_REQ_STATUS'; payload: boolean }
67+
6268
const initialState: AppState = {
6369
isChatHistoryOpen: false,
6470
chatHistoryLoadingState: ChatHistoryLoadingState.Loading,
@@ -77,6 +83,8 @@ const initialState: AppState = {
7783
draftedDocumentTitle: '',
7884
isGenerating: false,
7985
isRequestInitiated: false,
86+
failedSections : [],
87+
isFailedReqInitiated : false
8088
}
8189

8290
export const AppStateContext = createContext<

frontend/src/state/AppReducer.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ export const appStateReducer = (state: AppState, action: Action): AppState => {
105105
return { ...state, isGenerating: action.payload }
106106
case 'SET_IS_REQUEST_INITIATED' :
107107
return {...state, isRequestInitiated : action.payload}
108+
case 'ADD_FAILED_SECTION':
109+
var tempFailedSections = [...state.failedSections];
110+
const exists = tempFailedSections.some((item) => item.title === action.payload.title);
111+
if (!exists)
112+
tempFailedSections.push(action.payload);
113+
return { ...state , failedSections : [...tempFailedSections] }
114+
case 'REMOVED_FAILED_SECTION' :
115+
var tempFailedSections = [...state.failedSections];
116+
tempFailedSections = state.failedSections.filter((item) => item.title !== action.payload.section.title);
117+
return { ...state , failedSections : [...tempFailedSections] }
118+
case 'UPDATE_SECTION_API_REQ_STATUS' :
119+
return {...state, isFailedReqInitiated : action.payload}
120+
108121
default:
109122
return state
110123
}

frontend/src/test/test.utils.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const defaultMockState = {
1919
draftedDocumentTitle: '',
2020
isGenerating: false,
2121
isRequestInitiated: false,
22+
failedSections : [],
23+
isFailedReqInitiated : false
2224
};
2325

2426
// Create a custom render function

0 commit comments

Comments
 (0)