Spaces:
Running
Running
✨ patch diff
Browse files- server.js +51 -34
- src/components/ask-ai/ask-ai.tsx +2 -2
- src/components/preview/preview.tsx +1 -1
server.js
CHANGED
@@ -308,11 +308,7 @@ app.post("/api/ask-ai", async (req, res) => {
|
|
308 |
content: prompt,
|
309 |
},
|
310 |
],
|
311 |
-
|
312 |
-
? {
|
313 |
-
max_tokens: selectedProvider.max_tokens,
|
314 |
-
}
|
315 |
-
: {}),
|
316 |
});
|
317 |
|
318 |
while (true) {
|
@@ -513,40 +509,61 @@ ${REPLACE_END}
|
|
513 |
});
|
514 |
|
515 |
const chunk = response.choices[0]?.message?.content;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
516 |
|
517 |
-
// return the new HTML using the html and the chunk
|
518 |
if (chunk) {
|
519 |
-
|
520 |
-
const replaceEndIndex = chunk.indexOf(REPLACE_END);
|
521 |
-
|
522 |
-
if (
|
523 |
-
searchStartIndex === -1 ||
|
524 |
-
replaceEndIndex === -1 ||
|
525 |
-
replaceEndIndex <= searchStartIndex
|
526 |
-
) {
|
527 |
-
return res.status(400).send({
|
528 |
-
ok: false,
|
529 |
-
message: "Invalid response format",
|
530 |
-
});
|
531 |
-
}
|
532 |
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
);
|
537 |
-
const replaceBlock = chunk.substring(
|
538 |
-
chunk.indexOf(DIVIDER) + DIVIDER.length,
|
539 |
-
replaceEndIndex
|
540 |
-
);
|
541 |
|
542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
|
544 |
-
|
545 |
-
|
546 |
-
newHtml = `${replaceBlock}\n${newHtml}`;
|
547 |
-
} else {
|
548 |
-
// Replacing existing code
|
549 |
-
newHtml = newHtml.replace(searchBlock, replaceBlock);
|
550 |
}
|
551 |
|
552 |
return res.status(200).send({
|
|
|
308 |
content: prompt,
|
309 |
},
|
310 |
],
|
311 |
+
max_tokens: selectedProvider.max_tokens,
|
|
|
|
|
|
|
|
|
312 |
});
|
313 |
|
314 |
while (true) {
|
|
|
509 |
});
|
510 |
|
511 |
const chunk = response.choices[0]?.message?.content;
|
512 |
+
// TO DO: handle the case where there are multiple SEARCH/REPLACE blocks
|
513 |
+
if (!chunk) {
|
514 |
+
return res.status(400).send({
|
515 |
+
ok: false,
|
516 |
+
message: "No content returned from the model",
|
517 |
+
});
|
518 |
+
}
|
519 |
|
|
|
520 |
if (chunk) {
|
521 |
+
let newHtml = html;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
522 |
|
523 |
+
// Find all search/replace blocks in the chunk
|
524 |
+
let position = 0;
|
525 |
+
let moreBlocks = true;
|
|
|
|
|
|
|
|
|
|
|
526 |
|
527 |
+
while (moreBlocks) {
|
528 |
+
const searchStartIndex = chunk.indexOf(SEARCH_START, position);
|
529 |
+
if (searchStartIndex === -1) {
|
530 |
+
moreBlocks = false;
|
531 |
+
continue;
|
532 |
+
}
|
533 |
+
|
534 |
+
const dividerIndex = chunk.indexOf(DIVIDER, searchStartIndex);
|
535 |
+
if (dividerIndex === -1) {
|
536 |
+
moreBlocks = false;
|
537 |
+
continue;
|
538 |
+
}
|
539 |
+
|
540 |
+
const replaceEndIndex = chunk.indexOf(REPLACE_END, dividerIndex);
|
541 |
+
if (replaceEndIndex === -1) {
|
542 |
+
moreBlocks = false;
|
543 |
+
continue;
|
544 |
+
}
|
545 |
+
|
546 |
+
// Extract the search and replace blocks
|
547 |
+
const searchBlock = chunk.substring(
|
548 |
+
searchStartIndex + SEARCH_START.length,
|
549 |
+
dividerIndex
|
550 |
+
);
|
551 |
+
const replaceBlock = chunk.substring(
|
552 |
+
dividerIndex + DIVIDER.length,
|
553 |
+
replaceEndIndex
|
554 |
+
);
|
555 |
+
|
556 |
+
// Apply the replacement
|
557 |
+
if (searchBlock.trim() === "") {
|
558 |
+
// Inserting at the beginning
|
559 |
+
newHtml = `${replaceBlock}\n${newHtml}`;
|
560 |
+
} else {
|
561 |
+
// Replacing existing code
|
562 |
+
newHtml = newHtml.replace(searchBlock, replaceBlock);
|
563 |
+
}
|
564 |
|
565 |
+
// Move position to after this block to find the next one
|
566 |
+
position = replaceEndIndex + REPLACE_END.length;
|
|
|
|
|
|
|
|
|
567 |
}
|
568 |
|
569 |
return res.status(200).send({
|
src/components/ask-ai/ask-ai.tsx
CHANGED
@@ -63,8 +63,7 @@ function AskAI({
|
|
63 |
let thinkResponse = "";
|
64 |
let lastRenderTime = 0;
|
65 |
|
66 |
-
const isFollowUp =
|
67 |
-
previousPrompt && previousPrompt.length > 0 && html !== defaultHTML;
|
68 |
try {
|
69 |
onNewPrompt(prompt);
|
70 |
if (isFollowUp) {
|
@@ -102,6 +101,7 @@ function AskAI({
|
|
102 |
setPrompt("");
|
103 |
setisAiWorking(false);
|
104 |
onSuccess(res.html, prompt);
|
|
|
105 |
}
|
106 |
} else {
|
107 |
const request = await fetch("/api/ask-ai", {
|
|
|
63 |
let thinkResponse = "";
|
64 |
let lastRenderTime = 0;
|
65 |
|
66 |
+
const isFollowUp = html !== defaultHTML;
|
|
|
67 |
try {
|
68 |
onNewPrompt(prompt);
|
69 |
if (isFollowUp) {
|
|
|
101 |
setPrompt("");
|
102 |
setisAiWorking(false);
|
103 |
onSuccess(res.html, prompt);
|
104 |
+
audio.play();
|
105 |
}
|
106 |
} else {
|
107 |
const request = await fetch("/api/ask-ai", {
|
src/components/preview/preview.tsx
CHANGED
@@ -53,7 +53,7 @@ function Preview({
|
|
53 |
"w-full select-none transition-all duration-200 bg-black max-lg:h-full",
|
54 |
{
|
55 |
"pointer-events-none": isResizing || isAiWorking,
|
56 |
-
"lg:max-w-md lg:mx-auto lg:h-[80dvh] lg:!rounded-[
|
57 |
device === "mobile",
|
58 |
"h-full": device === "desktop",
|
59 |
"lg:border-[8px] lg:border-neutral-700 lg:shadow-2xl lg:rounded-[44px]":
|
|
|
53 |
"w-full select-none transition-all duration-200 bg-black max-lg:h-full",
|
54 |
{
|
55 |
"pointer-events-none": isResizing || isAiWorking,
|
56 |
+
"lg:max-w-md lg:mx-auto lg:h-[80dvh] lg:!rounded-[48px] lg:border-[8px] lg:border-neutral-700 lg:shadow-2xl":
|
57 |
device === "mobile",
|
58 |
"h-full": device === "desktop",
|
59 |
"lg:border-[8px] lg:border-neutral-700 lg:shadow-2xl lg:rounded-[44px]":
|