Skip to content

Commit 93615a1

Browse files
authored
Merge pull request #725 from microsoft/joshuakr/7652-Generating-Answer-Fix
Fixed Generating Answer being displayed on load
2 parents 4834881 + 77aa245 commit 93615a1

File tree

2 files changed

+13
-24
lines changed

2 files changed

+13
-24
lines changed

app/frontend/src/pages/tda/Tda.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const Tda = ({folderPath, tags}: Props) => {
3535
const [otherq, setOtherq] = useState('');
3636
const [selectedQuery, setSelectedQuery] = useState('');
3737
const [dataFrame, setDataFrame] = useState<object[]>([]);
38-
const [loading, setLoading] = useState(false);
38+
const [renderAnswer, setRenderAnswer] = useState(false);
3939
const [inputValue, setInputValue] = useState("");
4040
const [fileu, setFile] = useState<File | null>(null);
4141
const [images, setImages] = useState<string[]>([]);
@@ -76,7 +76,7 @@ const fetchImages = async () => {
7676
const handleAnalysis = () => {
7777
setImages([])
7878
setOutput('');
79-
setLoading(true);
79+
setRenderAnswer(true);
8080
setTimeout(async () => {
8181
try {
8282
const query = setOtherQ(selectedQuery);
@@ -90,11 +90,10 @@ const fetchImages = async () => {
9090
setStreamKey(prevKey => prevKey + 1);
9191
} else {
9292
setOutput("no file file has been uploaded.")
93-
setLoading(false);
9493
}
9594
} catch (error) {
9695
console.log(error);
97-
setLoading(false);
96+
setRenderAnswer(false);
9897
}
9998
}, 0);
10099
};
@@ -111,26 +110,23 @@ const fetchImages = async () => {
111110
setImages([])
112111
const query = setOtherQ(selectedQuery);
113112
setOutput('');
114-
setLoading(true);
113+
setRenderAnswer(true);
115114
if (fileu) {
116115
const result = await processCsvAgentResponse(query, fileu);
117-
setLoading(false);
118116
setOutput(result.toString());
119117
fetchImages();
120118
return;
121119

122120
}
123121
else {
124122
setOutput("no file file has been uploaded.")
125-
setLoading(false);
126123
}
127124
} catch (error) {
128125
lastError = error;
129126
}
130127
}
131128
// If the code reaches here, all retries have failed. Handle the error as needed.
132129
console.error(lastError);
133-
setLoading(false);
134130
setOutput('An error occurred.');
135131
};
136132

@@ -246,7 +242,6 @@ const fetchImages = async () => {
246242
}
247243
else {
248244
setOutput("no file file has been uploaded.")
249-
setLoading(false);
250245
}
251246
}
252247
}, [selectedQuery]);
@@ -265,7 +260,6 @@ const handleCloseEvent = () => {
265260
eventSourceRef.current.close();
266261
eventSourceRef.current = null;
267262
fetchImages();
268-
setLoading(false);
269263
console.log('EventSource closed');
270264
}
271265
}
@@ -399,7 +393,8 @@ const handleCloseEvent = () => {
399393
<div style={{width: '100%'}}>
400394
<h2>Tabular Data Assistant Response:</h2>
401395
<div>
402-
<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} classNames={cstyle.centeredAnswerContainer} nonEventString={output} onStreamingComplete={handleCloseEvent} typingSpeed={10} />
396+
{ renderAnswer &&
397+
<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} classNames={cstyle.centeredAnswerContainer} nonEventString={output} onStreamingComplete={handleCloseEvent} typingSpeed={10} /> }
403398
</div>
404399
<h2>Generated Images:</h2>
405400
<div>

app/frontend/src/pages/tutor/Tutor.tsx

+7-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import React, { useRef } from 'react';
55
//import { Button } from '@fluentui/react';
66
import { Accordion, Card, Button } from 'react-bootstrap';
7-
import {getHint, processAgentResponse, getSolve, streamData} from "../../api";
7+
import {getHint, processAgentResponse, streamData} from "../../api";
88
import { useEffect, useState } from "react";
99
import styles from './Tutor.module.css';
1010
import ReactMarkdown from 'react-markdown';
@@ -15,12 +15,11 @@ import CharacterStreamer from '../../components/CharacterStreamer/CharacterStrea
1515

1616
const Tutor = () => {
1717
const [streamKey, setStreamKey] = useState(0);
18-
const [loading, setLoading] = useState(false);
18+
const [renderAnswer, setRenderAnswer] = useState(false);
1919
const [error, setError] = useState(false);
2020
const [errorMessage, setErrorMessage] = useState('');
2121
const [mathProblem, setMathProblem] = useState('');
2222
const [output, setOutput] = useState('');
23-
//const [output, setOutput] = useState<string | null>("");
2423
const [selectedButton, setSelectedButton] = useState<string | null>(null);
2524
const eventSourceRef = useRef<EventSource | null>(null);
2625

@@ -32,14 +31,12 @@ const Tutor = () => {
3231

3332
const handleInput = (event: React.FormEvent<HTMLFormElement>) => {
3433
event.preventDefault();
35-
setLoading(true);
3634
userInput(mathProblem);
3735
};
3836

3937
const userInput = (problem: string) => {
4038
// Process the user's math problem here
4139
console.log(problem);
42-
setLoading(false);
4340
};
4441

4542
function delay(ms: number): Promise<void> {
@@ -53,7 +50,6 @@ const Tutor = () => {
5350
): Promise<T> {
5451

5552
setError(false);
56-
setLoading(true);
5753
for (let attempt = 1; attempt <= retries; attempt++) {
5854
try {
5955
return await asyncFn(); // Try executing the function
@@ -67,33 +63,33 @@ const Tutor = () => {
6763
}
6864
}
6965
setError(true);
70-
setLoading(false);
7166
// If we reach this point, all retries have failed
7267
throw new Error(`Max retries reached. Last error: ${errorMessage}`);
7368
}
7469

7570
async function hinter(question: string) {
7671
setStreamKey(prevKey => prevKey + 1);
7772
setOutput('');
73+
setRenderAnswer(true);
7874
await retryAsyncFn(() => getHint(question), 3, 1000).then((response) => {
7975
setOutput(response.toString());
8076
});
81-
setLoading(false);
8277

8378
}
8479

8580

8681
async function getAnswer(question: string) {
8782
setStreamKey(prevKey => prevKey + 1);
8883
setOutput('');
84+
setRenderAnswer(true);
8985
await retryAsyncFn(() => processAgentResponse(question), 3, 1000).then((response) => {
9086
setOutput(response.toString());
9187
});
92-
setLoading(false);
9388
};
9489

9590
async function handleExampleClick(value: string) {
9691
setStreamKey(prevKey => prevKey + 1);
92+
setRenderAnswer(true);
9793
setMathProblem(value);
9894
getAnswer(value);
9995
}
@@ -109,7 +105,7 @@ const EXAMPLES: ExampleModel[] = [
109105
const handleButton2Click = () => {
110106
setStreamKey(prevKey => prevKey + 1);
111107
setOutput('');
112-
setLoading(true);
108+
setRenderAnswer(true);
113109
setSelectedButton('button2');
114110
if (eventSourceRef.current) {
115111
eventSourceRef.current.close();
@@ -124,7 +120,6 @@ const EXAMPLES: ExampleModel[] = [
124120
if (eventSourceRef.current) {
125121
eventSourceRef.current.close();
126122
eventSourceRef.current = null;
127-
setLoading(false);
128123
console.log('EventSource closed');
129124
}
130125
}
@@ -134,7 +129,6 @@ const EXAMPLES: ExampleModel[] = [
134129
if (eventSourceRef.current) {
135130
eventSourceRef.current.close();
136131
eventSourceRef.current = null;
137-
setLoading(false);
138132
console.log('EventSource closed');
139133
}
140134
};
@@ -199,7 +193,7 @@ return (
199193
</div>
200194
</form>
201195
{error && <div className="spinner">{errorMessage}</div>}
202-
{<CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} onStreamingComplete={handleCloseEvent} classNames={styles.centeredAnswerContainer} nonEventString={output} /> }
196+
{renderAnswer && <CharacterStreamer key={streamKey} eventSource={eventSourceRef.current} onStreamingComplete={handleCloseEvent} classNames={styles.centeredAnswerContainer} nonEventString={output} /> }
203197
</div>
204198
</div>
205199
)

0 commit comments

Comments
 (0)