Spaces:
Running
Running
send the whole page
Browse files- app/api/ask/route.ts +4 -36
app/api/ask/route.ts
CHANGED
|
@@ -300,41 +300,9 @@ export async function PUT(request: NextRequest) {
|
|
| 300 |
const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
|
| 301 |
const userContext = "You are modifying the HTML file based on the user's request.";
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
const indexPage = pages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index');
|
| 307 |
-
const otherPages = pages.filter(p => p !== indexPage);
|
| 308 |
-
|
| 309 |
-
if (selectedElementHtml) {
|
| 310 |
-
const elementKeywords = selectedElementHtml.toLowerCase().match(/class=["']([^"']*)["']|id=["']([^"']*)["']/g) || [];
|
| 311 |
-
const relevantPages = otherPages.filter(page => {
|
| 312 |
-
const pageContent = page.html.toLowerCase();
|
| 313 |
-
return elementKeywords.some((keyword: string) => pageContent.includes(keyword.toLowerCase()));
|
| 314 |
-
});
|
| 315 |
-
|
| 316 |
-
return indexPage ? [indexPage, ...relevantPages.slice(0, maxPages - 1)] : relevantPages.slice(0, maxPages);
|
| 317 |
-
}
|
| 318 |
-
|
| 319 |
-
const keywords = prompt.toLowerCase().split(/\s+/).filter(word => word.length > 3);
|
| 320 |
-
const scoredPages = otherPages.map(page => {
|
| 321 |
-
const pageContent = (page.path + ' ' + page.html).toLowerCase();
|
| 322 |
-
const score = keywords.reduce((acc, keyword) => {
|
| 323 |
-
return acc + (pageContent.includes(keyword) ? 1 : 0);
|
| 324 |
-
}, 0);
|
| 325 |
-
return { page, score };
|
| 326 |
-
});
|
| 327 |
-
|
| 328 |
-
const topPages = scoredPages
|
| 329 |
-
.sort((a, b) => b.score - a.score)
|
| 330 |
-
.slice(0, maxPages - (indexPage ? 1 : 0))
|
| 331 |
-
.map(item => item.page);
|
| 332 |
-
|
| 333 |
-
return indexPage ? [indexPage, ...topPages] : topPages;
|
| 334 |
-
};
|
| 335 |
-
|
| 336 |
-
const relevantPages = getRelevantPages(pages || [], prompt);
|
| 337 |
-
const pagesContext = relevantPages
|
| 338 |
.map((p: Page) => `- ${p.path}\n${p.html}`)
|
| 339 |
.join("\n\n");
|
| 340 |
|
|
@@ -342,7 +310,7 @@ export async function PUT(request: NextRequest) {
|
|
| 342 |
selectedElementHtml
|
| 343 |
? `\n\nYou have to update ONLY the following element, NOTHING ELSE: \n\n\`\`\`html\n${selectedElementHtml}\n\`\`\` Could be in multiple pages, if so, update all the pages.`
|
| 344 |
: ""
|
| 345 |
-
}. Current pages (${
|
| 346 |
|
| 347 |
const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
|
| 348 |
const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
|
|
|
|
| 300 |
const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
|
| 301 |
const userContext = "You are modifying the HTML file based on the user's request.";
|
| 302 |
|
| 303 |
+
// Send all pages without filtering
|
| 304 |
+
const allPages = pages || [];
|
| 305 |
+
const pagesContext = allPages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
.map((p: Page) => `- ${p.path}\n${p.html}`)
|
| 307 |
.join("\n\n");
|
| 308 |
|
|
|
|
| 310 |
selectedElementHtml
|
| 311 |
? `\n\nYou have to update ONLY the following element, NOTHING ELSE: \n\n\`\`\`html\n${selectedElementHtml}\n\`\`\` Could be in multiple pages, if so, update all the pages.`
|
| 312 |
: ""
|
| 313 |
+
}. Current pages (${allPages.length} total): ${pagesContext}. ${files?.length > 0 ? `Available images: ${files?.map((f: string) => f).join(', ')}.` : ""}`;
|
| 314 |
|
| 315 |
const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
|
| 316 |
const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
|