Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Revert "send the whole page"
Browse filesThis reverts commit fb9c1a7b5745939605ac45d53eecc059256ea15e.
- app/api/ask/route.ts +36 -4
    	
        app/api/ask/route.ts
    CHANGED
    
    | @@ -324,9 +324,41 @@ export async function PUT(request: NextRequest) { | |
| 324 | 
             
                const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
         | 
| 325 | 
             
                const userContext = "You are modifying the HTML file based on the user's request.";
         | 
| 326 |  | 
| 327 | 
            -
                 | 
| 328 | 
            -
             | 
| 329 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 330 | 
             
                  .map((p: Page) => `- ${p.path}\n${p.html}`)
         | 
| 331 | 
             
                  .join("\n\n");
         | 
| 332 |  | 
| @@ -334,7 +366,7 @@ export async function PUT(request: NextRequest) { | |
| 334 | 
             
                  selectedElementHtml
         | 
| 335 | 
             
                    ? `\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.`
         | 
| 336 | 
             
                    : ""
         | 
| 337 | 
            -
                }. Current pages (${ | 
| 338 |  | 
| 339 | 
             
                const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
         | 
| 340 | 
             
                const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
         | 
|  | |
| 324 | 
             
                const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
         | 
| 325 | 
             
                const userContext = "You are modifying the HTML file based on the user's request.";
         | 
| 326 |  | 
| 327 | 
            +
                const getRelevantPages = (pages: Page[], prompt: string, maxPages: number = 2): Page[] => {
         | 
| 328 | 
            +
                  if (pages.length <= maxPages) return pages;
         | 
| 329 | 
            +
                  
         | 
| 330 | 
            +
                  const indexPage = pages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index');
         | 
| 331 | 
            +
                  const otherPages = pages.filter(p => p !== indexPage);
         | 
| 332 | 
            +
                  
         | 
| 333 | 
            +
                  if (selectedElementHtml) {
         | 
| 334 | 
            +
                    const elementKeywords = selectedElementHtml.toLowerCase().match(/class=["']([^"']*)["']|id=["']([^"']*)["']/g) || [];
         | 
| 335 | 
            +
                    const relevantPages = otherPages.filter(page => {
         | 
| 336 | 
            +
                      const pageContent = page.html.toLowerCase();
         | 
| 337 | 
            +
                      return elementKeywords.some((keyword: string) => pageContent.includes(keyword.toLowerCase()));
         | 
| 338 | 
            +
                    });
         | 
| 339 | 
            +
                    
         | 
| 340 | 
            +
                    return indexPage ? [indexPage, ...relevantPages.slice(0, maxPages - 1)] : relevantPages.slice(0, maxPages);
         | 
| 341 | 
            +
                  }
         | 
| 342 | 
            +
                  
         | 
| 343 | 
            +
                  const keywords = prompt.toLowerCase().split(/\s+/).filter(word => word.length > 3);
         | 
| 344 | 
            +
                  const scoredPages = otherPages.map(page => {
         | 
| 345 | 
            +
                    const pageContent = (page.path + ' ' + page.html).toLowerCase();
         | 
| 346 | 
            +
                    const score = keywords.reduce((acc, keyword) => {
         | 
| 347 | 
            +
                      return acc + (pageContent.includes(keyword) ? 1 : 0);
         | 
| 348 | 
            +
                    }, 0);
         | 
| 349 | 
            +
                    return { page, score };
         | 
| 350 | 
            +
                  });
         | 
| 351 | 
            +
                  
         | 
| 352 | 
            +
                  const topPages = scoredPages
         | 
| 353 | 
            +
                    .sort((a, b) => b.score - a.score)
         | 
| 354 | 
            +
                    .slice(0, maxPages - (indexPage ? 1 : 0))
         | 
| 355 | 
            +
                    .map(item => item.page);
         | 
| 356 | 
            +
                  
         | 
| 357 | 
            +
                  return indexPage ? [indexPage, ...topPages] : topPages;
         | 
| 358 | 
            +
                };
         | 
| 359 | 
            +
             | 
| 360 | 
            +
                const relevantPages = getRelevantPages(pages || [], prompt);
         | 
| 361 | 
            +
                const pagesContext = relevantPages
         | 
| 362 | 
             
                  .map((p: Page) => `- ${p.path}\n${p.html}`)
         | 
| 363 | 
             
                  .join("\n\n");
         | 
| 364 |  | 
|  | |
| 366 | 
             
                  selectedElementHtml
         | 
| 367 | 
             
                    ? `\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.`
         | 
| 368 | 
             
                    : ""
         | 
| 369 | 
            +
                }. Current pages (${relevantPages.length}/${pages?.length || 0} shown): ${pagesContext}. ${files?.length > 0 ? `Available images: ${files?.map((f: string) => f).join(', ')}.` : ""}`;
         | 
| 370 |  | 
| 371 | 
             
                const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
         | 
| 372 | 
             
                const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
         | 
