temporary fix
Browse files- app/api/ask-ai/route.ts +1 -9
- components/editor/ask-ai/index.tsx +61 -75
app/api/ask-ai/route.ts
CHANGED
|
@@ -192,7 +192,6 @@ export async function POST(request: NextRequest) {
|
|
| 192 |
encoder.encode(
|
| 193 |
JSON.stringify({
|
| 194 |
ok: false,
|
| 195 |
-
openSelectProvider: true,
|
| 196 |
message:
|
| 197 |
error.message ||
|
| 198 |
"An error occurred while processing your request.",
|
|
@@ -201,14 +200,7 @@ export async function POST(request: NextRequest) {
|
|
| 201 |
);
|
| 202 |
}
|
| 203 |
} finally {
|
| 204 |
-
|
| 205 |
-
// Only close if writer exists and is not already closed
|
| 206 |
-
if (writer && !writer.closed) {
|
| 207 |
-
await writer.close();
|
| 208 |
-
}
|
| 209 |
-
} catch {
|
| 210 |
-
// Ignore errors on close
|
| 211 |
-
}
|
| 212 |
}
|
| 213 |
})();
|
| 214 |
|
|
|
|
| 192 |
encoder.encode(
|
| 193 |
JSON.stringify({
|
| 194 |
ok: false,
|
|
|
|
| 195 |
message:
|
| 196 |
error.message ||
|
| 197 |
"An error occurred while processing your request.",
|
|
|
|
| 200 |
);
|
| 201 |
}
|
| 202 |
} finally {
|
| 203 |
+
await writer?.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}
|
| 205 |
})();
|
| 206 |
|
components/editor/ask-ai/index.tsx
CHANGED
|
@@ -145,21 +145,6 @@ export function AskAI({
|
|
| 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(
|
|
@@ -169,6 +154,26 @@ export function AskAI({
|
|
| 169 |
const read = async () => {
|
| 170 |
const { done, value } = await reader.read();
|
| 171 |
if (done) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
toast.success("AI responded successfully");
|
| 173 |
setPreviousPrompt(prompt);
|
| 174 |
setPrompt("");
|
|
@@ -190,74 +195,55 @@ export function AskAI({
|
|
| 190 |
}
|
| 191 |
|
| 192 |
const chunk = decoder.decode(value, { stream: true });
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
setOpen(true);
|
| 200 |
-
} else if (res.openSelectProvider) {
|
| 201 |
-
setOpenProvider(true);
|
| 202 |
-
setProviderError(res.message);
|
| 203 |
-
} else if (res.openProModal) {
|
| 204 |
-
setOpenProModal(true);
|
| 205 |
-
} else {
|
| 206 |
-
toast.error(res.message);
|
| 207 |
-
}
|
| 208 |
-
setisAiWorking(false);
|
| 209 |
-
return;
|
| 210 |
-
} else {
|
| 211 |
-
thinkResponse += chunk;
|
| 212 |
-
if (selectedModel?.isThinker) {
|
| 213 |
-
const thinkMatch = thinkResponse.match(/<think>[\s\S]*/)?.[0];
|
| 214 |
-
if (thinkMatch && !thinkResponse?.includes("</think>")) {
|
| 215 |
-
if ((contentThink?.length ?? 0) < 3) {
|
| 216 |
-
setOpenThink(true);
|
| 217 |
-
}
|
| 218 |
-
setThink(thinkMatch.replace("<think>", "").trim());
|
| 219 |
-
contentThink += chunk;
|
| 220 |
-
return read();
|
| 221 |
}
|
|
|
|
|
|
|
|
|
|
| 222 |
}
|
|
|
|
| 223 |
|
| 224 |
-
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
}
|
| 258 |
}
|
| 259 |
-
read();
|
| 260 |
}
|
|
|
|
| 261 |
};
|
| 262 |
|
| 263 |
read();
|
|
|
|
| 145 |
signal: abortController.signal,
|
| 146 |
});
|
| 147 |
if (request && request.body) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
const reader = request.body.getReader();
|
| 149 |
const decoder = new TextDecoder("utf-8");
|
| 150 |
const selectedModel = MODELS.find(
|
|
|
|
| 154 |
const read = async () => {
|
| 155 |
const { done, value } = await reader.read();
|
| 156 |
if (done) {
|
| 157 |
+
const isJson =
|
| 158 |
+
contentResponse.trim().startsWith("{") &&
|
| 159 |
+
contentResponse.trim().endsWith("}");
|
| 160 |
+
const jsonResponse = isJson ? JSON.parse(contentResponse) : null;
|
| 161 |
+
if (jsonResponse && !jsonResponse.ok) {
|
| 162 |
+
if (jsonResponse.openLogin) {
|
| 163 |
+
setOpen(true);
|
| 164 |
+
} else if (jsonResponse.openSelectProvider) {
|
| 165 |
+
setOpenProvider(true);
|
| 166 |
+
setProviderError(jsonResponse.message);
|
| 167 |
+
} else if (jsonResponse.openProModal) {
|
| 168 |
+
setOpenProModal(true);
|
| 169 |
+
} else {
|
| 170 |
+
toast.error(jsonResponse.message);
|
| 171 |
+
}
|
| 172 |
+
setisAiWorking(false);
|
| 173 |
+
return;
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
console.log("AI response complete", contentResponse);
|
| 177 |
toast.success("AI responded successfully");
|
| 178 |
setPreviousPrompt(prompt);
|
| 179 |
setPrompt("");
|
|
|
|
| 195 |
}
|
| 196 |
|
| 197 |
const chunk = decoder.decode(value, { stream: true });
|
| 198 |
+
thinkResponse += chunk;
|
| 199 |
+
if (selectedModel?.isThinker) {
|
| 200 |
+
const thinkMatch = thinkResponse.match(/<think>[\s\S]*/)?.[0];
|
| 201 |
+
if (thinkMatch && !thinkResponse?.includes("</think>")) {
|
| 202 |
+
if ((contentThink?.length ?? 0) < 3) {
|
| 203 |
+
setOpenThink(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
}
|
| 205 |
+
setThink(thinkMatch.replace("<think>", "").trim());
|
| 206 |
+
contentThink += chunk;
|
| 207 |
+
return read();
|
| 208 |
}
|
| 209 |
+
}
|
| 210 |
|
| 211 |
+
contentResponse += chunk;
|
| 212 |
|
| 213 |
+
const newHtml = contentResponse.match(
|
| 214 |
+
/<!DOCTYPE html>[\s\S]*/
|
| 215 |
+
)?.[0];
|
| 216 |
+
if (newHtml) {
|
| 217 |
+
setIsThinking(false);
|
| 218 |
+
let partialDoc = newHtml;
|
| 219 |
+
if (
|
| 220 |
+
partialDoc.includes("<head>") &&
|
| 221 |
+
!partialDoc.includes("</head>")
|
| 222 |
+
) {
|
| 223 |
+
partialDoc += "\n</head>";
|
| 224 |
+
}
|
| 225 |
+
if (
|
| 226 |
+
partialDoc.includes("<body") &&
|
| 227 |
+
!partialDoc.includes("</body>")
|
| 228 |
+
) {
|
| 229 |
+
partialDoc += "\n</body>";
|
| 230 |
+
}
|
| 231 |
+
if (!partialDoc.includes("</html>")) {
|
| 232 |
+
partialDoc += "\n</html>";
|
| 233 |
+
}
|
| 234 |
|
| 235 |
+
// Throttle the re-renders to avoid flashing/flicker
|
| 236 |
+
const now = Date.now();
|
| 237 |
+
if (now - lastRenderTime > 300) {
|
| 238 |
+
setHtml(partialDoc);
|
| 239 |
+
lastRenderTime = now;
|
| 240 |
+
}
|
| 241 |
|
| 242 |
+
if (partialDoc.length > 200) {
|
| 243 |
+
onScrollToBottom();
|
|
|
|
| 244 |
}
|
|
|
|
| 245 |
}
|
| 246 |
+
read();
|
| 247 |
};
|
| 248 |
|
| 249 |
read();
|