Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	try to improve follow up request speed
Browse files- app/api/ask/route.ts +47 -4
 - lib/prompts.ts +17 -17
 
    	
        app/api/ask/route.ts
    CHANGED
    
    | 
         @@ -318,14 +318,57 @@ export async function PUT(request: NextRequest) { 
     | 
|
| 318 | 
         
             
              try {
         
     | 
| 319 | 
         
             
                // Calculate dynamic max_tokens for PUT request
         
     | 
| 320 | 
         
             
                const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
         
     | 
| 321 | 
         
            -
                const userContext =  
     | 
| 322 | 
         
            -
             
     | 
| 323 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 324 | 
         
             
                const assistantContext = `${
         
     | 
| 325 | 
         
             
                  selectedElementHtml
         
     | 
| 326 | 
         
             
                    ? `\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.`
         
     | 
| 327 | 
         
             
                    : ""
         
     | 
| 328 | 
         
            -
                }. Current pages 
     | 
| 329 | 
         | 
| 330 | 
         
             
                const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
         
     | 
| 331 | 
         
             
                const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
         
     | 
| 
         | 
|
| 318 | 
         
             
              try {
         
     | 
| 319 | 
         
             
                // Calculate dynamic max_tokens for PUT request
         
     | 
| 320 | 
         
             
                const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");
         
     | 
| 321 | 
         
            +
                const userContext = "You are modifying the HTML file based on the user's request.";
         
     | 
| 322 | 
         
            +
                
         
     | 
| 323 | 
         
            +
                // Helper function to get only the most relevant pages based on prompt
         
     | 
| 324 | 
         
            +
                const getRelevantPages = (pages: Page[], prompt: string, maxPages: number = 2): Page[] => {
         
     | 
| 325 | 
         
            +
                  if (pages.length <= maxPages) return pages;
         
     | 
| 326 | 
         
            +
                  
         
     | 
| 327 | 
         
            +
                  // Always include index/main page
         
     | 
| 328 | 
         
            +
                  const indexPage = pages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index');
         
     | 
| 329 | 
         
            +
                  const otherPages = pages.filter(p => p !== indexPage);
         
     | 
| 330 | 
         
            +
                  
         
     | 
| 331 | 
         
            +
                  // If we have a selected element, only include pages that might contain it
         
     | 
| 332 | 
         
            +
                  if (selectedElementHtml) {
         
     | 
| 333 | 
         
            +
                    const elementKeywords = selectedElementHtml.toLowerCase().match(/class=["']([^"']*)["']|id=["']([^"']*)["']/g) || [];
         
     | 
| 334 | 
         
            +
                    const relevantPages = otherPages.filter(page => {
         
     | 
| 335 | 
         
            +
                      const pageContent = page.html.toLowerCase();
         
     | 
| 336 | 
         
            +
                      return elementKeywords.some((keyword: string) => pageContent.includes(keyword.toLowerCase()));
         
     | 
| 337 | 
         
            +
                    });
         
     | 
| 338 | 
         
            +
                    
         
     | 
| 339 | 
         
            +
                    return indexPage ? [indexPage, ...relevantPages.slice(0, maxPages - 1)] : relevantPages.slice(0, maxPages);
         
     | 
| 340 | 
         
            +
                  }
         
     | 
| 341 | 
         
            +
                  
         
     | 
| 342 | 
         
            +
                  // Score pages based on keyword matches from prompt
         
     | 
| 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 | 
         
            +
                  // Sort by relevance and take top pages
         
     | 
| 353 | 
         
            +
                  const topPages = scoredPages
         
     | 
| 354 | 
         
            +
                    .sort((a, b) => b.score - a.score)
         
     | 
| 355 | 
         
            +
                    .slice(0, maxPages - (indexPage ? 1 : 0))
         
     | 
| 356 | 
         
            +
                    .map(item => item.page);
         
     | 
| 357 | 
         
            +
                  
         
     | 
| 358 | 
         
            +
                  return indexPage ? [indexPage, ...topPages] : topPages;
         
     | 
| 359 | 
         
            +
                };
         
     | 
| 360 | 
         
            +
             
     | 
| 361 | 
         
            +
                // Get only the most relevant pages - provide FULL HTML for accurate replacements
         
     | 
| 362 | 
         
            +
                const relevantPages = getRelevantPages(pages || [], prompt);
         
     | 
| 363 | 
         
            +
                const pagesContext = relevantPages
         
     | 
| 364 | 
         
            +
                  .map((p: Page) => `- ${p.path}\n${p.html}`)
         
     | 
| 365 | 
         
            +
                  .join("\n\n");
         
     | 
| 366 | 
         
            +
                
         
     | 
| 367 | 
         
             
                const assistantContext = `${
         
     | 
| 368 | 
         
             
                  selectedElementHtml
         
     | 
| 369 | 
         
             
                    ? `\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.`
         
     | 
| 370 | 
         
             
                    : ""
         
     | 
| 371 | 
         
            +
                }. Current pages (${relevantPages.length}/${pages?.length || 0} shown): ${pagesContext}. ${files?.length > 0 ? `Available images: ${files?.map((f: string) => f.split('/').pop()).join(', ')}.` : ""}`;
         
     | 
| 372 | 
         | 
| 373 | 
         
             
                const estimatedInputTokens = estimateInputTokens(systemPrompt, prompt, userContext + assistantContext);
         
     | 
| 374 | 
         
             
                const dynamicMaxTokens = calculateMaxTokens(selectedProvider, estimatedInputTokens, false);
         
     | 
    	
        lib/prompts.ts
    CHANGED
    
    | 
         @@ -151,20 +151,20 @@ IMPORTANT: While creating a new page, UPDATE ALL THE OTHERS (using the UPDATE_PA 
     | 
|
| 151 | 
         
             
            No need to explain what you did. Just return the expected result.`
         
     | 
| 152 | 
         | 
| 153 | 
         
             
            export const PROMPTS_FOR_AI = [
         
     | 
| 154 | 
         
            -
             
     | 
| 155 | 
         
            -
             
     | 
| 156 | 
         
            -
             
     | 
| 157 | 
         
            -
             
     | 
| 158 | 
         
            -
             
     | 
| 159 | 
         
            -
             
     | 
| 160 | 
         
            -
             
     | 
| 161 | 
         
            -
             
     | 
| 162 | 
         
            -
             
     | 
| 163 | 
         
            -
             
     | 
| 164 | 
         
            -
             
     | 
| 165 | 
         
            -
             
     | 
| 166 | 
         
            -
             
     | 
| 167 | 
         
            -
             
     | 
| 168 | 
         
            -
             
     | 
| 169 | 
         
            -
             
     | 
| 170 | 
         
            -
             
     | 
| 
         | 
|
| 151 | 
         
             
            No need to explain what you did. Just return the expected result.`
         
     | 
| 152 | 
         | 
| 153 | 
         
             
            export const PROMPTS_FOR_AI = [
         
     | 
| 154 | 
         
            +
              "Create a landing page for a SaaS product, with a hero section, a features section, a pricing section, and a call to action section.",
         
     | 
| 155 | 
         
            +
              "Create a portfolio website for a designer, with a hero section, a projects section, a about section, and a contact section.",
         
     | 
| 156 | 
         
            +
              "Create a blog website for a writer, with a hero section, a blog section, a about section, and a contact section.",
         
     | 
| 157 | 
         
            +
              "Create a Tic Tac Toe game, with a game board, a history section, and a score section.",
         
     | 
| 158 | 
         
            +
              "Create a Weather App, with a search bar, a weather section, and a forecast section.",
         
     | 
| 159 | 
         
            +
              "Create a Calculator, with a calculator section, and a history section.",
         
     | 
| 160 | 
         
            +
              "Create a Todo List, with a todo list section, and a history section.",
         
     | 
| 161 | 
         
            +
              "Create a Calendar, with a calendar section, and a history section.",
         
     | 
| 162 | 
         
            +
              "Create a Music Player, with a music player section, and a history section.",
         
     | 
| 163 | 
         
            +
              "Create a Quiz App, with a quiz section, and a history section.",
         
     | 
| 164 | 
         
            +
              "Create a Pomodoro Timer, with a timer section, and a history section.",
         
     | 
| 165 | 
         
            +
              "Create a Notes App, with a notes section, and a history section.",
         
     | 
| 166 | 
         
            +
              "Create a Task Manager, with a task list section, and a history section.",
         
     | 
| 167 | 
         
            +
              "Create a Password Generator, with a password generator section, and a history section.",
         
     | 
| 168 | 
         
            +
              "Create a Currency Converter, with a currency converter section, and a history section.",
         
     | 
| 169 | 
         
            +
              "Create a Dictionary, with a dictionary section, and a history section.",
         
     | 
| 170 | 
         
            +
            ];
         
     |