Spaces:
Running
Running
Handle Error properly
Browse files
components/editor/ask-ai/index.tsx
CHANGED
|
@@ -145,21 +145,21 @@ export function AskAI({
|
|
| 145 |
signal: abortController.signal,
|
| 146 |
});
|
| 147 |
if (request && request.body) {
|
| 148 |
-
if (!request.ok) {
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
}
|
| 163 |
const reader = request.body.getReader();
|
| 164 |
const decoder = new TextDecoder("utf-8");
|
| 165 |
const selectedModel = MODELS.find(
|
|
@@ -190,55 +190,71 @@ export function AskAI({
|
|
| 190 |
}
|
| 191 |
|
| 192 |
const chunk = decoder.decode(value, { stream: true });
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
}
|
| 200 |
-
setThink(thinkMatch.replace("<think>", "").trim());
|
| 201 |
-
contentThink += chunk;
|
| 202 |
-
return read();
|
| 203 |
}
|
| 204 |
-
}
|
| 205 |
|
| 206 |
-
|
| 207 |
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
| 239 |
}
|
|
|
|
| 240 |
}
|
| 241 |
-
read();
|
| 242 |
};
|
| 243 |
|
| 244 |
read();
|
|
|
|
| 145 |
signal: abortController.signal,
|
| 146 |
});
|
| 147 |
if (request && request.body) {
|
| 148 |
+
// if (!request.ok) {
|
| 149 |
+
// const res = await request.json();
|
| 150 |
+
// if (res.openLogin) {
|
| 151 |
+
// setOpen(true);
|
| 152 |
+
// } else if (res.openSelectProvider) {
|
| 153 |
+
// setOpenProvider(true);
|
| 154 |
+
// setProviderError(res.message);
|
| 155 |
+
// } else if (res.openProModal) {
|
| 156 |
+
// setOpenProModal(true);
|
| 157 |
+
// } else {
|
| 158 |
+
// toast.error(res.message);
|
| 159 |
+
// }
|
| 160 |
+
// setisAiWorking(false);
|
| 161 |
+
// return;
|
| 162 |
+
// }
|
| 163 |
const reader = request.body.getReader();
|
| 164 |
const decoder = new TextDecoder("utf-8");
|
| 165 |
const selectedModel = MODELS.find(
|
|
|
|
| 190 |
}
|
| 191 |
|
| 192 |
const chunk = decoder.decode(value, { stream: true });
|
| 193 |
+
try {
|
| 194 |
+
const res = JSON.parse(chunk);
|
| 195 |
+
if (res.openLogin) {
|
| 196 |
+
setOpen(true);
|
| 197 |
+
} else if (res.openSelectProvider) {
|
| 198 |
+
setOpenProvider(true);
|
| 199 |
+
setProviderError(res.message);
|
| 200 |
+
} else if (res.openProModal) {
|
| 201 |
+
setOpenProModal(true);
|
| 202 |
+
} else {
|
| 203 |
+
toast.error(res.message);
|
| 204 |
+
}
|
| 205 |
+
setisAiWorking(false);
|
| 206 |
+
return;
|
| 207 |
+
} catch {
|
| 208 |
+
thinkResponse += chunk;
|
| 209 |
+
if (selectedModel?.isThinker) {
|
| 210 |
+
const thinkMatch = thinkResponse.match(/<think>[\s\S]*/)?.[0];
|
| 211 |
+
if (thinkMatch && !thinkResponse?.includes("</think>")) {
|
| 212 |
+
if ((contentThink?.length ?? 0) < 3) {
|
| 213 |
+
setOpenThink(true);
|
| 214 |
+
}
|
| 215 |
+
setThink(thinkMatch.replace("<think>", "").trim());
|
| 216 |
+
contentThink += chunk;
|
| 217 |
+
return read();
|
| 218 |
}
|
|
|
|
|
|
|
|
|
|
| 219 |
}
|
|
|
|
| 220 |
|
| 221 |
+
contentResponse += chunk;
|
| 222 |
|
| 223 |
+
const newHtml = contentResponse.match(
|
| 224 |
+
/<!DOCTYPE html>[\s\S]*/
|
| 225 |
+
)?.[0];
|
| 226 |
+
if (newHtml) {
|
| 227 |
+
setIsThinking(false);
|
| 228 |
+
let partialDoc = newHtml;
|
| 229 |
+
if (
|
| 230 |
+
partialDoc.includes("<head>") &&
|
| 231 |
+
!partialDoc.includes("</head>")
|
| 232 |
+
) {
|
| 233 |
+
partialDoc += "\n</head>";
|
| 234 |
+
}
|
| 235 |
+
if (
|
| 236 |
+
partialDoc.includes("<body") &&
|
| 237 |
+
!partialDoc.includes("</body>")
|
| 238 |
+
) {
|
| 239 |
+
partialDoc += "\n</body>";
|
| 240 |
+
}
|
| 241 |
+
if (!partialDoc.includes("</html>")) {
|
| 242 |
+
partialDoc += "\n</html>";
|
| 243 |
+
}
|
| 244 |
|
| 245 |
+
// Throttle the re-renders to avoid flashing/flicker
|
| 246 |
+
const now = Date.now();
|
| 247 |
+
if (now - lastRenderTime > 300) {
|
| 248 |
+
setHtml(partialDoc);
|
| 249 |
+
lastRenderTime = now;
|
| 250 |
+
}
|
| 251 |
|
| 252 |
+
if (partialDoc.length > 200) {
|
| 253 |
+
onScrollToBottom();
|
| 254 |
+
}
|
| 255 |
}
|
| 256 |
+
read();
|
| 257 |
}
|
|
|
|
| 258 |
};
|
| 259 |
|
| 260 |
read();
|
components/pro-modal/index.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import { useLocalStorage } from "react-use";
|
| 2 |
-
import { Button } from "
|
| 3 |
-
import { Dialog, DialogContent } from "
|
| 4 |
import { CheckCheck } from "lucide-react";
|
| 5 |
import { isTheSameHtml } from "@/lib/compare-html-diff";
|
| 6 |
|
|
@@ -24,6 +24,7 @@ export const ProModal = ({
|
|
| 24 |
return (
|
| 25 |
<Dialog open={open} onOpenChange={onClose}>
|
| 26 |
<DialogContent className="sm:max-w-lg lg:!p-8 !rounded-3xl !bg-white !border-neutral-100">
|
|
|
|
| 27 |
<main className="flex flex-col items-start text-left relative pt-2">
|
| 28 |
<div className="flex items-center justify-start -space-x-4 mb-5">
|
| 29 |
<div className="size-14 rounded-full bg-pink-200 shadow-2xs flex items-center justify-center text-3xl opacity-50">
|
|
|
|
| 1 |
import { useLocalStorage } from "react-use";
|
| 2 |
+
import { Button } from "@/components/ui/button";
|
| 3 |
+
import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
|
| 4 |
import { CheckCheck } from "lucide-react";
|
| 5 |
import { isTheSameHtml } from "@/lib/compare-html-diff";
|
| 6 |
|
|
|
|
| 24 |
return (
|
| 25 |
<Dialog open={open} onOpenChange={onClose}>
|
| 26 |
<DialogContent className="sm:max-w-lg lg:!p-8 !rounded-3xl !bg-white !border-neutral-100">
|
| 27 |
+
<DialogTitle className="hidden" />
|
| 28 |
<main className="flex flex-col items-start text-left relative pt-2">
|
| 29 |
<div className="flex items-center justify-start -space-x-4 mb-5">
|
| 30 |
<div className="size-14 rounded-full bg-pink-200 shadow-2xs flex items-center justify-center text-3xl opacity-50">
|