remove comment
Browse filesFiles changed (2)
- app/api/ask/route.ts+3-12
- middleware.ts+1-13
291 unmodified lines2922932942952962972982993003013023033043053063073081 unmodified line3103113123133143153163173183193203213223233243253263273284 unmodified lines3333343353363373383393 unmodified lines3433443453463473483492 unmodified lines352353354355356357358291 unmodified linesconst client = new InferenceClient(token);// Helper function to escape regex special charactersconst escapeRegExp = (string: string) => {return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');};// Helper function to create flexible HTML regex that handles varying spacesconst createFlexibleHtmlRegex = (searchBlock: string) => {let searchRegex = escapeRegExp(searchBlock).replace(/\s+/g, '\\s*') // Allow any amount of whitespace where there are spaces.replace(/>\s*</g, '>\\s*<') // Allow spaces between HTML tags.replace(/\s*>/g, '\\s*>'); // Allow spaces before closing >return new RegExp(searchRegex, 'g');};1 unmodified lineconst selectedProvider = await getBestProvider(selectedModel.value, provider)try {// Calculate dynamic max_tokens for PUT requestconst systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");const userContext = "You are modifying the HTML file based on the user's request.";// Helper function to get only the most relevant pages based on promptconst getRelevantPages = (pages: Page[], prompt: string, maxPages: number = 2): Page[] => {if (pages.length <= maxPages) return pages;// Always include index/main pageconst indexPage = pages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index');const otherPages = pages.filter(p => p !== indexPage);// If we have a selected element, only include pages that might contain itif (selectedElementHtml) {const elementKeywords = selectedElementHtml.toLowerCase().match(/class=["']([^"']*)["']|id=["']([^"']*)["']/g) || [];const relevantPages = otherPages.filter(page => {4 unmodified linesreturn indexPage ? [indexPage, ...relevantPages.slice(0, maxPages - 1)] : relevantPages.slice(0, maxPages);}// Score pages based on keyword matches from promptconst keywords = prompt.toLowerCase().split(/\s+/).filter(word => word.length > 3);const scoredPages = otherPages.map(page => {const pageContent = (page.path + ' ' + page.html).toLowerCase();3 unmodified linesreturn { page, score };});// Sort by relevance and take top pagesconst topPages = scoredPages.sort((a, b) => b.score - a.score).slice(0, maxPages - (indexPage ? 1 : 0))2 unmodified linesreturn indexPage ? [indexPage, ...topPages] : topPages;};// Get only the most relevant pages - provide FULL HTML for accurate replacementsconst relevantPages = getRelevantPages(pages || [], prompt);const pagesContext = relevantPages.map((p: Page) => `- ${p.path}\n${p.html}`)291 unmodified lines2922932942952962972982993003013023033043053061 unmodified line3083093103113123133143153163173183193203213224 unmodified lines3273283293303313323 unmodified lines3363373383393403412 unmodified lines344345346347348349291 unmodified linesconst client = new InferenceClient(token);const escapeRegExp = (string: string) => {return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');};const createFlexibleHtmlRegex = (searchBlock: string) => {let searchRegex = escapeRegExp(searchBlock).replace(/\s+/g, '\\s*').replace(/>\s*</g, '>\\s*<').replace(/\s*>/g, '\\s*>');return new RegExp(searchRegex, 'g');};1 unmodified lineconst selectedProvider = await getBestProvider(selectedModel.value, provider)try {const systemPrompt = FOLLOW_UP_SYSTEM_PROMPT + (isNew ? PROMPT_FOR_PROJECT_NAME : "");const userContext = "You are modifying the HTML file based on the user's request.";const getRelevantPages = (pages: Page[], prompt: string, maxPages: number = 2): Page[] => {if (pages.length <= maxPages) return pages;const indexPage = pages.find(p => p.path === '/' || p.path === '/index' || p.path === 'index');const otherPages = pages.filter(p => p !== indexPage);if (selectedElementHtml) {const elementKeywords = selectedElementHtml.toLowerCase().match(/class=["']([^"']*)["']|id=["']([^"']*)["']/g) || [];const relevantPages = otherPages.filter(page => {4 unmodified linesreturn indexPage ? [indexPage, ...relevantPages.slice(0, maxPages - 1)] : relevantPages.slice(0, maxPages);}const keywords = prompt.toLowerCase().split(/\s+/).filter(word => word.length > 3);const scoredPages = otherPages.map(page => {const pageContent = (page.path + ' ' + page.html).toLowerCase();3 unmodified linesreturn { page, score };});const topPages = scoredPages.sort((a, b) => b.score - a.score).slice(0, maxPages - (indexPage ? 1 : 0))2 unmodified linesreturn indexPage ? [indexPage, ...topPages] : topPages;};const relevantPages = getRelevantPages(pages || [], prompt);const pagesContext = relevantPages.map((p: Page) => `- ${p.path}\n${p.html}`)
4 unmodified lines567891011121314151617181920212223242526274 unmodified linesconst headers = new Headers(request.headers);headers.set("x-current-host", request.nextUrl.host);// Create response with headersconst response = NextResponse.next({ headers });// Add SEO and security headers// Only set X-Frame-Options for non-HF spaces domains// if (!request.nextUrl.host.includes('hf.space') && !request.nextUrl.host.includes('huggingface.co') && !request.nextUrl.host.includes('hf.co')) {// response.headers.set('X-Frame-Options', 'SAMEORIGIN');// }// response.headers.set('X-Content-Type-Options', 'nosniff');// response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');// Add cache control for better performanceif (request.nextUrl.pathname.startsWith('/_next/static')) {response.headers.set('Cache-Control', 'public, max-age=31536000, immutable');}// Add canonical URL headers for programmatic accessresponse.headers.set('X-Canonical-URL', `https://deepsite.hf.co${request.nextUrl.pathname}`);return response;4 unmodified lines567891011121314154 unmodified linesconst headers = new Headers(request.headers);headers.set("x-current-host", request.nextUrl.host);const response = NextResponse.next({ headers });if (request.nextUrl.pathname.startsWith('/_next/static')) {response.headers.set('Cache-Control', 'public, max-age=31536000, immutable');}response.headers.set('X-Canonical-URL', `https://deepsite.hf.co${request.nextUrl.pathname}`);return response;
app/api/ask/route.ts+3-12
middleware.ts+1-13