Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve front end display of Anthropic-side AI errors #2383

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/pages/policy/output/Analysis.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ export function GenerateAnalysisButton(props) {
aiOutputStream,
} = props;

const errorMessages = {
overloaded_error:
"Claude, our partner service, is currently overloaded. Please try again later.",
api_error:
"Claude, our partner service, is currently experiencing an error. Please try again later.",
default: "The AI service has experienced an error.",
};

const [analysisLoading, setAnalysisLoading] = useState(false);

const displayCharts = (markdown) =>
Expand All @@ -124,7 +132,7 @@ export function GenerateAnalysisButton(props) {
);

function resetAnalysis() {
setAnalysisError(false);
setAnalysisError(null);
setAnalysis(""); // Reset analysis content
setAnalysisLoading(false);
}
Expand Down Expand Up @@ -178,7 +186,12 @@ export function GenerateAnalysisButton(props) {
for (const chunk of chunks) {
if (chunk) {
const data = JSON.parse(chunk);
if (data.stream) {
if (data.type === "error") {
const errorMessage =
errorMessages[data.error] || errorMessages["default"];
setAnalysisError(errorMessage);
break;
} else if (data.type === "text" && data.stream) {
setAnalysis((prevAnalysis) => prevAnalysis + data.stream);
}
}
Expand Down Expand Up @@ -259,7 +272,7 @@ export default function Analysis(props) {
// Stateful vars for analysis output
const [audience, setAudience] = useState("Normal");
const [analysis, setAnalysis] = useState("");
const [analysisError, setAnalysisError] = useState(false);
const [analysisError, setAnalysisError] = useState(null);

// Stateful vars for generating prompt itself
const [prompt, setPrompt] = useState("");
Expand Down Expand Up @@ -368,7 +381,7 @@ export default function Analysis(props) {
</div>
) : null}
{analysisError ? (
<ErrorComponent message="There was an error generating the analysis." />
<ErrorComponent message={analysisError} />
) : (
analysis && <MarkdownFormatter markdown={analysis} dict={chartDict} />
)}
Expand Down
Loading