Spaces:
Sleeping
Sleeping
Commit
·
6077c4c
1
Parent(s):
878a096
adicionar tratamento de erro para requisições à API do AI e incluir baseUrl na requisição
Browse files
components/editor/ask-ai/index.tsx
CHANGED
|
@@ -122,6 +122,7 @@ export function AskAI({
|
|
| 122 |
}
|
| 123 |
} else {
|
| 124 |
const apiKey = localStorage.getItem("openai_api_key");
|
|
|
|
| 125 |
const request = await fetch("/api/ask-ai", {
|
| 126 |
method: "POST",
|
| 127 |
body: JSON.stringify({
|
|
@@ -131,6 +132,7 @@ export function AskAI({
|
|
| 131 |
html: isSameHtml ? "" : html,
|
| 132 |
redesignMarkdown,
|
| 133 |
apiKey,
|
|
|
|
| 134 |
}),
|
| 135 |
headers: {
|
| 136 |
"Content-Type": "application/json",
|
|
@@ -139,6 +141,21 @@ export function AskAI({
|
|
| 139 |
signal: abortController.signal,
|
| 140 |
});
|
| 141 |
if (request && request.body) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
const reader = request.body.getReader();
|
| 143 |
const decoder = new TextDecoder("utf-8");
|
| 144 |
const selectedModel = MODELS.find(
|
|
@@ -153,6 +170,7 @@ export function AskAI({
|
|
| 153 |
contentResponse.trim().endsWith("}");
|
| 154 |
const jsonResponse = isJson ? JSON.parse(contentResponse) : null;
|
| 155 |
if (jsonResponse && !jsonResponse.ok) {
|
|
|
|
| 156 |
setisAiWorking(false);
|
| 157 |
return;
|
| 158 |
}
|
|
|
|
| 122 |
}
|
| 123 |
} else {
|
| 124 |
const apiKey = localStorage.getItem("openai_api_key");
|
| 125 |
+
const baseUrl = localStorage.getItem("openai_base_url");
|
| 126 |
const request = await fetch("/api/ask-ai", {
|
| 127 |
method: "POST",
|
| 128 |
body: JSON.stringify({
|
|
|
|
| 132 |
html: isSameHtml ? "" : html,
|
| 133 |
redesignMarkdown,
|
| 134 |
apiKey,
|
| 135 |
+
baseUrl,
|
| 136 |
}),
|
| 137 |
headers: {
|
| 138 |
"Content-Type": "application/json",
|
|
|
|
| 141 |
signal: abortController.signal,
|
| 142 |
});
|
| 143 |
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 |
+
}
|
| 159 |
const reader = request.body.getReader();
|
| 160 |
const decoder = new TextDecoder("utf-8");
|
| 161 |
const selectedModel = MODELS.find(
|
|
|
|
| 170 |
contentResponse.trim().endsWith("}");
|
| 171 |
const jsonResponse = isJson ? JSON.parse(contentResponse) : null;
|
| 172 |
if (jsonResponse && !jsonResponse.ok) {
|
| 173 |
+
toast.error(jsonResponse?.error || jsonResponse?.message || "AI request failed.");
|
| 174 |
setisAiWorking(false);
|
| 175 |
return;
|
| 176 |
}
|