Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion frontend/app/settings/_components/agent-settings-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function AgentSettingsSection() {
const pathname = usePathname();

const focusLlmModel = searchParams.get("focusLlmModel") === "true";
const [isRestoringFlow, setIsRestoringFlow] = useState<boolean>(false);
const [openLlmSelector, setOpenLlmSelector] = useState(false);
const [systemPrompt, setSystemPrompt] = useState<string>("");

Expand Down Expand Up @@ -183,19 +184,24 @@ export function AgentSettingsSection() {
};

const handleRestoreRetrievalFlow = (closeDialog: () => void) => {
setIsRestoringFlow(true);

fetch("/api/reset-flow/retrieval", { method: "POST" })
.then((res) => {
if (res.ok) return res.json();
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
})
Comment on lines 186 to 193

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Handle successful empty responses without forcing JSON parse.

At Line 191, res.json() is called for every successful response. If the endpoint returns success with no JSON body, this throws and shows a false error toast.

Suggested fix
 fetch("/api/reset-flow/retrieval", { method: "POST" })
   .then((res) => {
-    if (res.ok) return res.json();
+    if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
+    return res;
+  })
+  .then(async (res) => {
+    const contentType = res.headers.get("content-type") || "";
+    if (contentType.includes("application/json")) {
+      await res.json();
+    }
+    return;
-    throw new Error(`HTTP ${res.status}: ${res.statusText}`);
   })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleRestoreRetrievalFlow = (closeDialog: () => void) => {
setIsRestoringFlow(true);
fetch("/api/reset-flow/retrieval", { method: "POST" })
.then((res) => {
if (res.ok) return res.json();
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
})
const handleRestoreRetrievalFlow = (closeDialog: () => void) => {
setIsRestoringFlow(true);
fetch("/api/reset-flow/retrieval", { method: "POST" })
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}: ${res.statusText}`);
return res;
})
.then(async (res) => {
const contentType = res.headers.get("content-type") || "";
if (contentType.includes("application/json")) {
await res.json();
}
return;
})
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/app/settings/_components/agent-settings-section.tsx` around lines
186 - 193, The handleRestoreRetrievalFlow function unconditionally calls
res.json() for all successful responses, but the endpoint may return a
successful response with an empty body, which causes JSON parsing to fail.
Modify the .then() handler to check if the response has content before
attempting to parse JSON. You can do this by checking the Content-Length header
or the response status, or by using res.text() first and only parsing as JSON if
there is actual content in the response body. This will prevent false error
toasts when the endpoint returns a successful empty response.

.then(() => {
setSystemPrompt(DEFAULT_AGENT_SETTINGS.system_prompt);
toast.success("Default agent flow settings restored successfully");
closeDialog();
})
.catch((err) => {
console.error("Error restoring retrieval flow:", err);
toast.error("Failed to restore default agent flow settings");
closeDialog();
});
})
.finally(() => setIsRestoringFlow(false));
};

return (
Expand Down Expand Up @@ -223,6 +229,7 @@ export function AgentSettingsSection() {
confirmText="Restore"
variant="destructive"
onConfirm={handleRestoreRetrievalFlow}
isLoading={isRestoringFlow}
/>
<ConfirmationDialog
trigger={
Expand Down
10 changes: 9 additions & 1 deletion frontend/app/settings/_components/ingest-settings-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export function IngestSettingsSection() {
const isCloudBrand = useIsCloudBrand();
const { isAuthenticated, isNoAuthMode, isIbmAuthMode, runMode } = useAuth();

const [isRestoringFlow, setIsRestoringFlow] = useState<boolean>(false);

const [chunkSize, setChunkSize] = useState<number>(1024);
const [chunkOverlap, setChunkOverlap] = useState<number>(50);
const [chunkValidationError, setChunkValidationError] = useState<
Expand Down Expand Up @@ -228,6 +230,8 @@ export function IngestSettingsSection() {
};

const handleRestoreIngestFlow = (closeDialog: () => void) => {
setIsRestoringFlow(true);

fetch("/api/reset-flow/ingest", { method: "POST" })
.then((res) => {
if (res.ok) return res.json();
Expand All @@ -241,12 +245,15 @@ export function IngestSettingsSection() {
setPictureDescriptions(DEFAULT_KNOWLEDGE_SETTINGS.picture_descriptions);
setDisableIngestWithLangflow(false);
setChunkValidationError(null);
toast.success("Default ingest flow settings restored successfully");
closeDialog();
})
.catch((err) => {
console.error("Error restoring ingest flow:", err);
toast.error("Failed to restore default ingest flow settings");
closeDialog();
});
})
.finally(() => setIsRestoringFlow(false));
};

return (
Expand Down Expand Up @@ -274,6 +281,7 @@ export function IngestSettingsSection() {
confirmText="Restore"
variant="destructive"
onConfirm={handleRestoreIngestFlow}
isLoading={isRestoringFlow}
/>
<ConfirmationDialog
trigger={
Expand Down
11 changes: 10 additions & 1 deletion frontend/components/confirmation-dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { Loader2 } from "lucide-react";
import { ReactNode, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Expand All @@ -22,6 +23,7 @@ interface ConfirmationDialogProps {
onCancel?: () => void;
variant?: "default" | "destructive" | "warning";
confirmIcon?: ReactNode | null;
isLoading?: boolean;
}

export function ConfirmationDialog({
Expand All @@ -34,6 +36,7 @@ export function ConfirmationDialog({
onCancel,
variant = "default",
confirmIcon = null,
isLoading = false,
}: ConfirmationDialogProps) {
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -61,7 +64,13 @@ export function ConfirmationDialog({
<Button variant="ghost" onClick={handleCancel} size="sm">
{cancelText}
</Button>
<Button variant={variant} onClick={handleConfirm} size="sm">
<Button
variant={variant}
onClick={handleConfirm}
size="sm"
disabled={isLoading}
>
{isLoading && <Loader2 className="h-4 w-4 mr-2 animate-spin" />}
{confirmText}
{confirmIcon}
</Button>
Expand Down
Loading