Skip to content

Commit d3601d6

Browse files
adicionar tratamento de erro para requisições à API do AI e incluir baseUrl na requisição
1 parent 76d7fbd commit d3601d6

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

components/editor/ask-ai/index.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ export function AskAI({
122122
}
123123
} else {
124124
const apiKey = localStorage.getItem("openai_api_key");
125+
const baseUrl = localStorage.getItem("openai_base_url");
125126
const request = await fetch("/api/ask-ai", {
126127
method: "POST",
127128
body: JSON.stringify({
@@ -131,6 +132,7 @@ export function AskAI({
131132
html: isSameHtml ? "" : html,
132133
redesignMarkdown,
133134
apiKey,
135+
baseUrl,
134136
}),
135137
headers: {
136138
"Content-Type": "application/json",
@@ -139,6 +141,21 @@ export function AskAI({
139141
signal: abortController.signal,
140142
});
141143
if (request && request.body) {
144+
if (!request.ok) {
145+
let errorMsg = "AI request failed.";
146+
try {
147+
const errorJson = await request.json();
148+
errorMsg = errorJson?.error || errorJson?.message || errorMsg;
149+
} catch (e) {
150+
// fallback: try to read as text
151+
try {
152+
errorMsg = await request.text();
153+
} catch {}
154+
}
155+
toast.error(errorMsg);
156+
setisAiWorking(false);
157+
return;
158+
}
142159
const reader = request.body.getReader();
143160
const decoder = new TextDecoder("utf-8");
144161
const selectedModel = MODELS.find(
@@ -153,6 +170,7 @@ export function AskAI({
153170
contentResponse.trim().endsWith("}");
154171
const jsonResponse = isJson ? JSON.parse(contentResponse) : null;
155172
if (jsonResponse && !jsonResponse.ok) {
173+
toast.error(jsonResponse?.error || jsonResponse?.message || "AI request failed.");
156174
setisAiWorking(false);
157175
return;
158176
}

0 commit comments

Comments
 (0)