fix(ai-assist): prevent HTML-as-JSON crash; add safeJsonFetch + JSON-only API errors

- Created lib/safeJsonFetch.ts helper to safely parse JSON responses
- Updated AIAssist.tsx to use safeJsonFetch with proper Content-Type header
- Improved app/api/ai-assist/route.ts with requestId tracking and safe body parsing
- All API responses now return JSON with proper error messages
This commit is contained in:
Gemini AI
2025-12-29 00:09:31 +04:00
Unverified
parent ba1fee3fc7
commit 42570a14b7
3 changed files with 156 additions and 30 deletions

View File

@@ -18,6 +18,7 @@ import { Input } from "@/components/ui/input";
import useStore from "@/lib/store";
import { translations } from "@/lib/i18n/translations";
import modelAdapter from "@/lib/services/adapter-instance";
import { safeJsonFetch } from "@/lib/safeJsonFetch";
// --- Types ---
@@ -346,15 +347,34 @@ export default function AIAssist() {
try {
// First, get the plan orchestrator prompt from our new API
const apiRes = await fetch("/api/ai-assist", {
type AiAssistApiResponse = {
prompt?: string;
step?: string;
requestId?: string;
success?: boolean;
error?: string;
};
const apiResult = await safeJsonFetch<AiAssistApiResponse>("/api/ai-assist", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
request: finalInput,
step: assistStep === "plan" ? "generate" : "plan",
plan: aiPlan
}),
});
const { prompt } = await apiRes.json();
if (!apiResult.ok) {
console.error("AI Assist API failed:", apiResult.error);
throw new Error(apiResult.error.message);
}
if (apiResult.data.error) {
throw new Error(apiResult.data.error);
}
const prompt = apiResult.data.prompt ?? "";
let accumulated = "";
let lastParsedPreview: PreviewData | null = null;