Skip to content

Commit 9e43743

Browse files
Merge branch 'section-generate-fix' of https://github.com/microsoft/Generic-Build-your-own-copilot-Solution-Accelerator into section-generate-fix
2 parents 53ce6ad + 471d51c commit 9e43743

File tree

4 files changed

+101
-17
lines changed

4 files changed

+101
-17
lines changed

frontend/src/components/DraftCards/SectionCard.tsx

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

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

143-
async function fetchSectionContent(sectionTitle: string, sectionDescription: string) {
162+
async function fetchSectionContent(sectionTitle: string, sectionDescription: string , isReqFrom = '') {
144163
setIsLoading(true)
145164
const sectionGenerateRequest: SectionGenerateRequest = { sectionTitle, sectionDescription }
146165

147166
const response = await sectionGenerate(sectionGenerateRequest)
148167
const responseBody = await response.json()
149168

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

158-
// limit the character count to 2000
159-
if (content.length > sectionCharacterLimit) {
160-
content = content.slice(0, sectionCharacterLimit)
201+
//appStateContext?.dispatch({ type: 'REMOVED_FAILED_SECTION', payload: updatedSection })
202+
203+
appStateContext?.dispatch({ type: 'REMOVED_FAILED_SECTION', payload: {section : updatedSection} })
204+
205+
if(isReqFrom == 'failed')
206+
appStateContext?.dispatch({ type: 'UPDATE_SECTION_API_REQ_STATUS', payload: false })
161207
}
162208

163-
setCharCount(content.length)
164-
setIsLoading(false)
209+
165210
}
166211

212+
213+
167214
useEffect(() => {
168215
if (sectionContent === '' && !isLoading && !isManuallyCleared) {
169216
fetchSectionContent(sectionTitle, sectionDescription)

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
}

0 commit comments

Comments
 (0)