Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. π₯
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Branding Format Improvements (#2438)
Abimael Martell5839c26dc60d
diff --git a/apps/api/src/lib/branding/llm.ts b/apps/api/src/lib/branding/llm.tsindex b0cd31336..1cc4b60ea 100644--- a/apps/api/src/lib/branding/llm.ts+++ b/apps/api/src/lib/branding/llm.ts@@ -1,5 +1,7 @@import { generateObject } from "ai";+import * as Sentry from "@sentry/node";import { logger } from "../logger";+import { config } from "../../config";import { BrandingEnhancement, brandingEnhancementSchema } from "./schema";import { buildBrandingPrompt } from "./prompt";import { BrandingLLMInput } from "./types";@@ -8,10 +10,49 @@ import { getModel } from "../generic-ai";export async function enhanceBrandingWithLLM(input: BrandingLLMInput,): Promise<BrandingEnhancement> {- const model = getModel("gpt-4o-mini");-const prompt = buildBrandingPrompt(input);+ // Smart model selection: use more powerful model for complex cases+ // gpt-4o-mini: cheaper, good for simple cases+ // gpt-4o: more capable, better for complex prompts with many buttons/logos+ const buttonsCount = input.buttons?.length || 0;+ const logoCandidatesCount = input.logoCandidates?.length || 0;+ const promptLength = prompt.length;++ // Use gpt-4o for complex cases:+ // - Many buttons (>8)+ // - Many logo candidates (>5)+ // - Long prompt (>8000 chars)+ // - Has screenshot (adds complexity)+ const isComplexCase =+ buttonsCount > 8 ||+ logoCandidatesCount > 5 ||+ promptLength > 8000 ||+ !!input.screenshot;++ const modelName = isComplexCase ? "gpt-4o" : "gpt-4o-mini";+ const model = getModel(modelName);++ if (config.DEBUG_BRANDING === true) {+ logger.info("LLM model selection", {+ model: modelName,+ buttonsCount,+ logoCandidatesCount,+ promptLength,+ hasScreenshot: !!input.screenshot,+ isComplexCase,+ });++ logger.debug("LLM branding prompt preview", {+ promptStart: prompt.substring(0, 500),+ promptEnd: prompt.substring(prompt.length - 500),+ buttonsPreview: input.buttons?.slice(0, 3).map(b => ({+ text: b.text?.substring(0, 50),+ background: b.background,+ })),+ });+ }+try {const result = await generateObject({model,@@ -42,9 +83,48 @@ export async function enhanceBrandingWithLLM(},});+ if (config.DEBUG_BRANDING === true) {+ const reasoningPreview = result.reasoning+ ? result.reasoning.length > 1000+ ? result.reasoning.substring(0, 1000) + "..."+ : result.reasoning+ : undefined;++ logger.info("LLM branding response", {+ model: modelName,+ buttonsCount,+ logoCandidatesCount,+ promptLength,+ hasScreenshot: !!input.screenshot,+ usage: result.usage,+ finishReason: result.finishReason,+ reasoning: reasoningPreview,+ reasoningLength: result.reasoning?.length || 0,+ warnings: result.warnings,+ hasObject: !!result.object,+ objectKeys: result.object ? Object.keys(result.object) : [],+ buttonClassification: result.object?.buttonClassification,+ colorRoles: result.object?.colorRoles,+ cleanedFontsLength: result.object?.cleanedFonts?.length || 0,+ logoSelection: result.object?.logoSelection,+ });++ if (result.reasoning && result.reasoning.length > 1000) {+ logger.debug("LLM full reasoning", {+ reasoning: result.reasoning,+ });+ }+ }+return result.object;} catch (error) {- logger.error("LLM branding enhancement failed", { error });+ Sentry.captureException(error);++ logger.error("LLM branding enhancement failed", {+ error,+ buttonsCount: input.buttons?.length || 0,+ promptLength: prompt.length,+ });return {cleanedFonts: [],diff --git a/apps/api/src/lib/branding/logo-selector.ts b/apps/api/src/lib/branding/logo-selector.tsnew file mode 100644index 000000000..7b33f59f1--- /dev/null+++ b/apps/api/src/lib/branding/logo-selector.ts@@ -0,0 +1,502 @@+import { logger } from "../logger";+import { calculateLogoArea } from "./types";++interface LogoCandidate {+ src: string;+ alt: string;+ isSvg: boolean;+ isVisible: boolean;+ location: "header" | "body" | "footer";+ position: { top: number; left: number; width: number; height: number };+ indicators: {+ inHeader: boolean;+ altMatch: boolean;+ srcMatch: boolean;+ classMatch: boolean;+ hrefMatch: boolean;+ };+ href?: string;+ source: string;+}++interface LogoSelectionResult {+ selectedIndex: number;+ confidence: number;+ method: "heuristic" | "llm" | "fallback";+ reasoning: string;+}++const CONFIDENCE_THRESHOLDS = {+ STRONG_SCORE: 60,+ GOOD_SCORE: 45,+ MODERATE_SCORE: 30,+ STRONG_SEPARATION: 20,+ GOOD_SEPARATION: 15,+ STRONG_CONFIDENCE: 0.9,+ GOOD_CONFIDENCE: 0.75,+ MODERATE_CONFIDENCE: 0.6,+ WEAK_CONFIDENCE: 0.4,+ LLM_THRESHOLD: 0.85,+} as const;++/**+ * Safely extract filename from a URL (everything after the last "/")+ */+function extractFilename(src: string): string | null {+ if (!src) return null;+ const withoutQuery = src.split("?")[0];+ const parts = withoutQuery.split("/");+ const filename = parts.pop();+ return filename || null;+}++/**+ * Calculate score for hrefMatch/inHeader indicators and no-link penalty+ */+function scoreHrefAndHeaderIndicators(candidate: LogoCandidate): {+ score: number;+ reasons: string[];+} {+ let score = 0;+ const reasons: string[] = [];++ if (candidate.indicators.hrefMatch && candidate.indicators.inHeader) {+ score += 50;+ reasons.push("header logo linking to homepage");+ } else if (candidate.indicators.hrefMatch) {+ score += 35;+ reasons.push("links to homepage");+ } else if (candidate.indicators.inHeader) {+ score += 25;+ reasons.push("in header");+ }++ // Penalty for no link at all - brand logos are usually clickable+ if (!candidate.href || candidate.href.trim() === "") {+ score -= 15;+ reasons.push("no link (brand logos usually link to homepage, penalty)");+ }++ return { score, reasons };+}++/**+ * Detect logo variants - returns groups of similar logos+ */+function detectLogoVariants(+ candidates: LogoCandidate[],+): Map<number, number[]> {+ const groups = new Map<number, number[]>();+ const processed = new Set<number>();++ candidates.forEach((candidate, index) => {+ if (processed.has(index)) return;++ const similarIndices = [index];+ processed.add(index);++ candidates.forEach((other, otherIndex) => {+ if (index === otherIndex || processed.has(otherIndex)) return;++ const candidateFilename = extractFilename(candidate.src);+ const otherFilename = extractFilename(other.src);++ const isSimilar =+ (candidate.alt &&+ other.alt &&+ candidate.alt.toLowerCase().replace(/\s+/g, "") ===+ other.alt.toLowerCase().replace(/\s+/g, "")) ||+ // Same src (exact match or only differs by size/theme)+ candidate.src === other.src ||+ (candidateFilename &&+ otherFilename &&+ candidate.src.includes(otherFilename) &&+ candidateFilename === otherFilename) ||+ (Math.abs(candidate.position.top - other.position.top) < 20 &&+ Math.abs(candidate.position.left - other.position.left) < 50 &&+ Math.abs(candidate.position.width - other.position.width) < 30);++ if (isSimilar) {+ similarIndices.push(otherIndex);+ processed.add(otherIndex);+ }+ });++ if (similarIndices.length > 0) {+ groups.set(index, similarIndices);+ }+ });++ return groups;+}++/**+ * Pick the best variant from a group of similar logos+ */+function pickBestVariant(+ candidates: LogoCandidate[],+ variantIndices: number[],+): number {+ // Prefer: 1) Visible, 2) In header, 3) Highest position (top), 4) Has href+ return variantIndices.reduce((best, current) => {+ const bestCandidate = candidates[best];+ const currentCandidate = candidates[current];++ if (currentCandidate.isVisible && !bestCandidate.isVisible) return current;+ if (!currentCandidate.isVisible && bestCandidate.isVisible) return best;++ if (+ currentCandidate.indicators.inHeader &&+ !bestCandidate.indicators.inHeader+ )+ return current;+ if (+ !currentCandidate.indicators.inHeader &&+ bestCandidate.indicators.inHeader+ )+ return best;++ if (currentCandidate.position.top < bestCandidate.position.top)+ return current;+ if (currentCandidate.position.top > bestCandidate.position.top) return best;++ if (+ currentCandidate.indicators.hrefMatch &&+ !bestCandidate.indicators.hrefMatch+ )+ return current;++ return best;+ });+}++/**+ * Detect if a logo appears in multiple locations (strong brand indicator)+ */+function detectRepeatedLogos(candidates: LogoCandidate[]): Set<number> {+ const repeated = new Set<number>();+ const srcGroups = new Map<string, number[]>();++ candidates.forEach((candidate, index) => {+ const srcKey =+ candidate.src.split("?")[0].split("/").pop()?.toLowerCase() ||+ candidate.src;+ if (!srcGroups.has(srcKey)) {+ srcGroups.set(srcKey, []);+ }+ const group = srcGroups.get(srcKey);+ if (group) {+ group.push(index);+ }+ });++ // If a logo appears in different locations (header + footer), it's likely the brand logo+ srcGroups.forEach(indices => {+ if (indices.length > 1) {+ const locations = new Set(indices.map(i => candidates[i].location));+ if (locations.size > 1) {+ indices.forEach(i => repeated.add(i));+ }+ }+ });++ return repeated;+}++/**+ * Smart logo selection with tiered approach:+ * 1. Strong heuristics (fast, free) - handles 70-80% of cases+ * 2. LLM validation (slow, expensive) - only when ambiguous+ * 3. Fallback to best guess+ */+export function selectLogoWithConfidence(+ candidates: LogoCandidate[],+ brandName?: string,+): LogoSelectionResult {+ if (candidates.length === 0) {+ return {+ selectedIndex: -1,+ confidence: 0,+ method: "fallback",+ reasoning: "No logo candidates provided",+ };+ }++ const variantGroups = detectLogoVariants(candidates);+ const repeatedLogos = detectRepeatedLogos(candidates);++ logger.debug("Logo variant analysis", {+ totalCandidates: candidates.length,+ variantGroupsCount: variantGroups.size,+ repeatedLogosCount: repeatedLogos.size,+ });++ const indicesToScore = new Set<number>();+ const variantBonuses = new Map<number, number>();++ if (variantGroups.size > 0) {+ variantGroups.forEach(variants => {+ const bestIndex = pickBestVariant(candidates, variants);+ indicesToScore.add(bestIndex);++ if (variants.some(i => repeatedLogos.has(i))) {+ variantBonuses.set(bestIndex, 15);+ }+ if (variants.length > 1) {+ variantBonuses.set(bestIndex, (variantBonuses.get(bestIndex) || 0) + 8);+ }+ });+ } else {+ candidates.forEach((_, index) => indicesToScore.add(index));+ }++ const scored = candidates.map((candidate, index) => {+ if (!indicesToScore.has(index)) {+ return {+ index,+ score: -999,+ candidate,+ reasons: "skipped (duplicate variant)",+ };+ }++ let score = 0;+ const reasons: string[] = [];++ const variantBonus = variantBonuses.get(index) || 0;+ if (variantBonus > 0) {+ score += variantBonus;+ reasons.push(`variant bonus (+${variantBonus})`);+ }++ const hrefHeaderScore = scoreHrefAndHeaderIndicators(candidate);+ score += hrefHeaderScore.score;+ reasons.push(...hrefHeaderScore.reasons);++ if (candidate.location === "header") {+ score += 20;+ reasons.push("header location");+ }++ if (candidate.isVisible) {+ score += 15;+ reasons.push("visible");+ }++ if (candidate.position.top < 100 && candidate.position.left < 300) {+ score += 10;+ reasons.push("top-left position");+ }++ const isHighest = candidates.every(+ (other, otherIndex) =>+ otherIndex === index || candidate.position.top <= other.position.top,+ );+ if (isHighest && candidate.position.top < 200) {+ score += 12;+ reasons.push("highest logo on page");+ }++ if (candidate.indicators.altMatch) {+ score += 8;+ reasons.push("alt matches logo/brand");+ }++ if (candidate.indicators.srcMatch) {+ score += 5;+ reasons.push("src contains logo");+ }++ if (candidate.indicators.classMatch) {+ score += 5;+ reasons.push("class contains logo");+ }++ if (brandName) {+ const altLower = candidate.alt.toLowerCase().trim();+ const brandLower = brandName.toLowerCase().trim();++ if (altLower === brandLower) {+ score += 20;+ reasons.push(`alt exactly matches brand name "${brandName}"`);+ } else if (+ altLower &&+ (altLower.includes(brandLower) || brandLower.includes(altLower))+ ) {+ score += 12;+ reasons.push(`alt contains brand name "${brandName}"`);+ }++ if (candidate.src.toLowerCase().includes(brandLower)) {+ score += 6;+ reasons.push(`src contains brand name "${brandName}"`);+ }+ }++ const area = calculateLogoArea(candidate.position);+ const width = candidate.position.width;+ const height = candidate.position.height;++ if (area > 1000 && area < 50000) {+ score += 5;+ reasons.push("reasonable size");+ } else if (area < 500) {+ score -= 8;+ reasons.push("too small (likely icon, penalty)");+ } else if (area >= 50000 && area <= 200000) {+ score -= 10;+ reasons.push("too large (likely banner/og:image, penalty)");+ } else if (area > 200000) {+ score -= 20;+ reasons.push("extremely large (likely og:image, heavy penalty)");+ }+ const isSquare = Math.abs(width - height) < 5;+ if (isSquare && (width < 40 || height < 40)) {+ score -= 12;+ reasons.push("small square icon (likely UI icon, heavy penalty)");+ }++ // SVGs are often logos (but not always)+ if (candidate.isSvg) {+ score += 3;+ reasons.push("SVG format");+ }++ // Penalties+ if (candidate.location === "footer") {+ score -= 15;+ reasons.push("footer location (penalty)");+ }++ if (candidate.location === "body" && !candidate.indicators.inHeader) {+ score -= 10;+ reasons.push("body location without header (penalty)");+ }++ if (!candidate.isVisible) {+ score -= 10;+ reasons.push("not visible (penalty)");+ }++ return {+ index,+ score,+ candidate,+ reasons: reasons.join(", "),+ };+ });++ const validScored = scored.filter(s => s.score > -900);+ validScored.sort((a, b) => b.score - a.score);++ if (validScored.length === 0) {+ return {+ selectedIndex: -1,+ confidence: 0,+ method: "fallback",+ reasoning: "All candidates were filtered out as duplicate variants",+ };+ }++ const top = validScored[0];+ const secondBest = validScored[1];++ const scoreSeparation = secondBest ? top.score - secondBest.score : top.score;++ if (+ top.score >= CONFIDENCE_THRESHOLDS.STRONG_SCORE &&+ scoreSeparation >= CONFIDENCE_THRESHOLDS.STRONG_SEPARATION+ ) {+ return {+ selectedIndex: top.index,+ confidence: CONFIDENCE_THRESHOLDS.STRONG_CONFIDENCE,+ method: "heuristic",+ reasoning: `Strong indicators: ${top.reasons}. Score: ${top.score} (clear winner by ${scoreSeparation} points)`,+ };+ }++ if (+ top.score >= CONFIDENCE_THRESHOLDS.GOOD_SCORE &&+ scoreSeparation >= CONFIDENCE_THRESHOLDS.GOOD_SEPARATION+ ) {+ return {+ selectedIndex: top.index,+ confidence: CONFIDENCE_THRESHOLDS.GOOD_CONFIDENCE,+ method: "heuristic",+ reasoning: `Good indicators: ${top.reasons}. Score: ${top.score} (ahead by ${scoreSeparation} points)`,+ };+ }++ if (top.score >= CONFIDENCE_THRESHOLDS.MODERATE_SCORE) {+ return {+ selectedIndex: top.index,+ confidence: CONFIDENCE_THRESHOLDS.MODERATE_CONFIDENCE,+ method: "heuristic",+ reasoning: `Moderate indicators: ${top.reasons}. Score: ${top.score}. May benefit from LLM validation.`,+ };+ }++ return {+ selectedIndex: top.index,+ confidence: CONFIDENCE_THRESHOLDS.WEAK_CONFIDENCE,+ method: "heuristic",+ reasoning: `Weak indicators: ${top.reasons}. Score: ${top.score}. LLM validation recommended (close scores: top=${top.score}, second=${secondBest?.score || 0})`,+ };+}++/**+ * Determine if LLM validation is needed based on heuristic confidence+ */+export function shouldUseLLMForLogoSelection(confidence: number): boolean {+ return confidence < CONFIDENCE_THRESHOLDS.LLM_THRESHOLD;+}++/**+ * Get top N candidates for LLM validation (when needed)+ * Returns the highest-scoring candidates to reduce token usage+ */+export function getTopCandidatesForLLM(+ candidates: LogoCandidate[],+ maxCandidates: number = 10,+): { filteredCandidates: LogoCandidate[]; indexMap: Map<number, number> } {+ if (candidates.length <= maxCandidates) {+ // Return all candidates with identity mapping+ const indexMap = new Map<number, number>();+ candidates.forEach((_, i) => indexMap.set(i, i));+ return { filteredCandidates: candidates, indexMap };+ }++ // Score each candidate+ const scored = candidates.map((candidate, originalIndex) => {+ let score = 0;++ // Strong indicators+ const hrefHeaderScore = scoreHrefAndHeaderIndicators(candidate);+ score += hrefHeaderScore.score;++ // Location+ if (candidate.location === "header") score += 20;++ // Visibility+ if (candidate.isVisible) score += 15;++ // Class/src/alt matches+ if (candidate.indicators.classMatch) score += 10;+ if (candidate.indicators.srcMatch) score += 10;+ if (candidate.indicators.altMatch) score += 5;++ return { originalIndex, score, candidate };+ });++ scored.sort((a, b) => b.score - a.score);+ const topScored = scored.slice(0, maxCandidates);++ const indexMap = new Map<number, number>();+ topScored.forEach((item, newIndex) => {+ indexMap.set(newIndex, item.originalIndex);+ });++ const filteredCandidates = topScored.map(s => s.candidate);++ return { filteredCandidates, indexMap };+}diff --git a/apps/api/src/lib/branding/merge.ts b/apps/api/src/lib/branding/merge.tsindex 3cdcf68d3..5234a5eb4 100644--- a/apps/api/src/lib/branding/merge.ts+++ b/apps/api/src/lib/branding/merge.ts@@ -1,6 +1,6 @@import { BrandingProfile } from "../../types/branding";import { BrandingEnhancement } from "./schema";-import { ButtonSnapshot } from "./types";+import { ButtonSnapshot, calculateLogoArea } from "./types";export function mergeBrandingResults(js: BrandingProfile,@@ -11,41 +11,162 @@ export function mergeBrandingResults(alt: string;isSvg: boolean;isVisible: boolean;- location: "header" | "body";+ location: "header" | "body" | "footer";position: { top: number; left: number; width: number; height: number };indicators: {inHeader: boolean;altMatch: boolean;srcMatch: boolean;classMatch: boolean;+ hrefMatch: boolean;};+ href?: string;source: string;}>,): BrandingProfile {const merged: BrandingProfile = { ...js };- // Use LLM-selected logo if available- if (- llm.logoSelection &&- llm.logoSelection.selectedLogoIndex !== undefined &&- llm.logoSelection.selectedLogoIndex >= 0 &&- logoCandidates &&- logoCandidates.length > 0 &&- llm.logoSelection.selectedLogoIndex < logoCandidates.length- ) {- const selectedLogo = logoCandidates[llm.logoSelection.selectedLogoIndex];- if (selectedLogo) {- // Initialize images object if it doesn't exist- if (!merged.images) {- merged.images = {};+ // Handle logo selection: if LLM says no logo (-1), clear it+ if (llm.logoSelection && llm.logoSelection.selectedLogoIndex !== undefined) {+ // If LLM explicitly says no logo (returns -1), remove any logo that was set+ if (llm.logoSelection.selectedLogoIndex === -1) {+ if (merged.images) {+ delete merged.images.logo;}- merged.images.logo = selectedLogo.src;(merged as any).__llm_logo_reasoning = {- selectedIndex: llm.logoSelection.selectedLogoIndex,- reasoning: llm.logoSelection.selectedLogoReasoning,- confidence: llm.logoSelection.confidence,+ selectedIndex: -1,+ reasoning:+ llm.logoSelection.selectedLogoReasoning || "No valid logo found",+ confidence: llm.logoSelection.confidence || 0,+ rejected: true,};}+ // If LLM selected a valid logo index+ else if (+ llm.logoSelection.selectedLogoIndex >= 0 &&+ logoCandidates &&+ logoCandidates.length > 0 &&+ llm.logoSelection.selectedLogoIndex < logoCandidates.length+ ) {+ const selectedLogo = logoCandidates[llm.logoSelection.selectedLogoIndex];+ if (selectedLogo) {+ // Quality checks before accepting the logo+ const confidence = llm.logoSelection.confidence || 0;+ const alt = selectedLogo.alt || "";+ const altLower = alt.toLowerCase().trim();+ const href = selectedLogo.href || "";++ // Red flags: these patterns indicate it's NOT a brand logo+ const isLanguageWord =+ /^(english|espaΓ±ol|franΓ§ais|deutsch|italiano|portuguΓͺs|δΈζ|ζ₯ζ¬θͺ|νκ΅μ΄|ΡΡΡΡΠΊΠΈΠΉ|Ψ§ΩΨΉΨ±Ψ¨ΩΨ©|en|es|fr|de|it|pt|zh|ja|ko|ru|ar)$/i.test(+ altLower,+ );+ const isCommonMenuWord =+ /^(menu|search|cart|login|signup|register|account|profile|settings|help|support|contact|about|home|shop|store|products|services|blog|news)$/i.test(+ altLower,+ );+ const isUIIcon =+ /search|icon|menu|hamburger|cart|user|bell|notification|settings|close|times/i.test(+ altLower,+ );++ // Check for external links - brand logos should NOT link to external websites+ // Note: External links should already be filtered out in brandingScript.ts+ // This is a minimal safety check - we can't verify external links without page URL+ let isExternalLink = false;+ if (href && href.trim()) {+ const hrefLower = href.toLowerCase().trim();+ // Relative URLs (starting with /) are always internal+ // Full URLs should have been filtered already, but if they got through,+ // we can't verify they're internal without the page URL (window.location not available in Node.js)+ // So we'll only flag known external service patterns that should have been filtered+ if (+ hrefLower.startsWith("http://") ||+ hrefLower.startsWith("https://") ||+ hrefLower.startsWith("//")+ ) {+ // Check for known external service domains (should have been filtered already)+ const externalServiceDomains = [+ "github.com",+ "twitter.com",+ "x.com",+ "facebook.com",+ "linkedin.com",+ ];+ if (+ externalServiceDomains.some(domain => hrefLower.includes(domain))+ ) {+ isExternalLink = true;+ }+ // Note: We can't verify other full URLs without page URL, so we trust+ // that brandingScript.ts already filtered them correctly+ }+ }++ // Check for very small square icons (typical UI icons: 16x16, 20x20, 24x24, etc.)+ const width = selectedLogo.position?.width || 0;+ const height = selectedLogo.position?.height || 0;+ const isSmallSquareIcon =+ Math.abs(width - height) < 5 && width < 40 && width > 0;++ const hasRedFlags =+ isLanguageWord ||+ isCommonMenuWord ||+ isUIIcon ||+ isSmallSquareIcon ||+ isExternalLink;++ // Only set logo if:+ // 1. Confidence is good (>= 0.5) OR+ // 2. Logo has very strong indicators (inHeader + hrefMatch + reasonable size)+ // AND no red flags+ const area = calculateLogoArea(selectedLogo.position);+ const hasReasonableSize = area >= 500 && area <= 100000;+ const hasStrongIndicators =+ selectedLogo.indicators?.inHeader &&+ selectedLogo.indicators?.hrefMatch &&+ hasReasonableSize;++ const shouldIncludeLogo =+ !hasRedFlags &&+ (confidence >= 0.5 || (hasStrongIndicators && confidence >= 0.4));++ if (shouldIncludeLogo) {+ // Initialize images object if it doesn't exist+ if (!merged.images) {+ merged.images = {};+ }+ merged.images.logo = selectedLogo.src;+ (merged as any).__llm_logo_reasoning = {+ selectedIndex: llm.logoSelection.selectedLogoIndex,+ reasoning: llm.logoSelection.selectedLogoReasoning,+ confidence: llm.logoSelection.confidence,+ };+ } else {+ // Log why we're not including the logo+ let rejectionReason = "Low confidence";+ if (hasRedFlags) {+ const redFlagReasons: string[] = [];+ if (isLanguageWord) redFlagReasons.push("language word");+ if (isCommonMenuWord) redFlagReasons.push("menu word");+ if (isUIIcon) redFlagReasons.push("UI icon");+ if (isSmallSquareIcon) redFlagReasons.push("small square icon");+ if (isExternalLink) redFlagReasons.push("external link");+ rejectionReason = `Red flags detected (${redFlagReasons.join(", ")})`;+ }+ const selectedLogoReasoning =+ llm.logoSelection.selectedLogoReasoning?.trim() || "";+ (merged as any).__llm_logo_reasoning = {+ selectedIndex: llm.logoSelection.selectedLogoIndex,+ reasoning: selectedLogoReasoning+ ? `Logo rejected: ${rejectionReason}. ${selectedLogoReasoning}`+ : `Logo rejected: ${rejectionReason}.`,+ confidence: llm.logoSelection.confidence,+ rejected: true,+ };+ }+ }+ }}if (buttonSnapshots.length > 0) {@@ -79,6 +200,7 @@ export function mergeBrandingResults(textColor: primaryBtn.textColor,borderColor: primaryBtn.borderColor || undefined,borderRadius: primaryBtn.borderRadius || "0px",+ borderRadiusCorners: primaryBtn.borderRadiusCorners,shadow: primaryBtn.shadow || undefined,};}@@ -94,6 +216,7 @@ export function mergeBrandingResults(textColor: secondaryBtn.textColor,borderColor: secondaryBtn.borderColor || undefined,borderRadius: secondaryBtn.borderRadius || "0px",+ borderRadiusCorners: secondaryBtn.borderRadiusCorners,shadow: secondaryBtn.shadow || undefined,};}diff --git a/apps/api/src/lib/branding/processor.ts b/apps/api/src/lib/branding/processor.tsindex cc2f1af0f..b64a00312 100644--- a/apps/api/src/lib/branding/processor.ts+++ b/apps/api/src/lib/branding/processor.ts@@ -1,5 +1,5 @@import { BrandingProfile } from "../../types/branding";-import { BrandingScriptReturn } from "./types";+import { BrandingScriptReturn, InputSnapshot } from "./types";import { parse, rgb, formatHex } from "culori";// Export for testing@@ -81,6 +81,27 @@ export function hexify(}}+// Calculate representative borderRadius from corners (max non-zero value)+function calculateRepresentativeBorderRadius(borderRadius?: {+ topLeft?: number | null;+ topRight?: number | null;+ bottomRight?: number | null;+ bottomLeft?: number | null;+}): string {+ if (!borderRadius) {+ return "0px";+ }++ const cornerValues = [+ borderRadius.topLeft || 0,+ borderRadius.topRight || 0,+ borderRadius.bottomRight || 0,+ borderRadius.bottomLeft || 0,+ ];+ const maxCorner = Math.max(...cornerValues);+ return maxCorner > 0 ? `${maxCorner}px` : "0px";+}+// Calculate contrast for text readabilityfunction contrastYIQ(hex: string): number {if (!hex) return 0;@@ -244,14 +265,8 @@ function inferFontsList(// Pick logo from imagesfunction pickLogo(images: Array<{ type: string; src: string }>): string | null {const byType = (t: string) => images.find(i => i.type === t)?.src;- return (- byType("logo") ||- byType("logo-svg") ||- byType("og") ||- byType("twitter") ||- byType("favicon") ||- null- );+ // Only return actual logos, not og:image or twitter:image (those are for social sharing)+ return byType("logo") || byType("logo-svg") || null;}// Process raw branding data into BrandingProfile@@ -300,13 +315,8 @@ export function processRawBranding(raw: BrandingScriptReturn): BrandingProfile {null,};- // Components (empty for now - LLM will populate)- const components = {- input: {- borderColor: "#CCCCCC",- borderRadius: borderRadius,- },- };+ // Components - will be populated with actual detected styles+ let components: any = {};// Filter and score buttonsconst candidateButtons = raw.snapshots@@ -431,6 +441,33 @@ export function processRawBranding(raw: BrandingScriptReturn): BrandingProfile {bgHex = "transparent";}+ const corners = {+ topLeft:+ s.borderRadius?.topLeft !== null &&+ s.borderRadius?.topLeft !== undefined+ ? `${s.borderRadius.topLeft}px`+ : "0px",+ topRight:+ s.borderRadius?.topRight !== null &&+ s.borderRadius?.topRight !== undefined+ ? `${s.borderRadius.topRight}px`+ : "0px",+ bottomRight:+ s.borderRadius?.bottomRight !== null &&+ s.borderRadius?.bottomRight !== undefined+ ? `${s.borderRadius.bottomRight}px`+ : "0px",+ bottomLeft:+ s.borderRadius?.bottomLeft !== null &&+ s.borderRadius?.bottomLeft !== undefined+ ? `${s.borderRadius.bottomLeft}px`+ : "0px",+ };++ const representativeBorderRadius = calculateRepresentativeBorderRadius(+ s.borderRadius,+ );+return {index: idx,text: s.text || "",@@ -439,7 +476,8 @@ export function processRawBranding(raw: BrandingScriptReturn): BrandingProfile {background: bgHex,textColor: hexify(s.colors.text, raw.pageBackground) || "#000000",borderColor: borderHex,- borderRadius: s.radius ? `${s.radius}px` : "0px",+ borderRadius: representativeBorderRadius,+ borderRadiusCorners: corners,shadow: s.shadow || null,// Debug: original color values before hex conversionoriginalBackgroundColor: s.colors.background || undefined,@@ -448,6 +486,21 @@ export function processRawBranding(raw: BrandingScriptReturn): BrandingProfile {};});+ const inputSnapshots = extractInputSnapshots(raw, uniqueButtons);++ // Populate components from actual detected styles+ if (inputSnapshots.length > 0) {+ const primaryInput = inputSnapshots[0];+ components.input = {+ background: primaryInput.background,+ textColor: primaryInput.textColor,+ borderColor: primaryInput.borderColor,+ borderRadius: primaryInput.borderRadius,+ borderRadiusCorners: primaryInput.borderRadiusCorners,+ shadow: primaryInput.shadow,+ };+ }+return {colorScheme: raw.colorScheme,fonts: fontsList,@@ -460,6 +513,124 @@ export function processRawBranding(raw: BrandingScriptReturn): BrandingProfile {components,images,__button_snapshots: buttonSnapshots as any,+ __input_snapshots: inputSnapshots as any,__framework_hints: raw.frameworkHints as any,};}++function extractInputSnapshots(+ raw: BrandingScriptReturn,+ buttonSnapshots: any[],+): InputSnapshot[] {+ // Filter input fields (excluding button inputs which are already in buttonSnapshots)+ const candidateInputs = raw.snapshots.filter(s => {+ if (!s.isInput || !s.inputMetadata) return false;+ if (s.rect.w < 50 || s.rect.h < 20) return false; // Too small+ return true;+ });++ // Score and sort inputs+ const scoredInputs = candidateInputs+ .map((input, idx) => {+ if (!input.inputMetadata) return null;++ let score = 0;+ const meta = input.inputMetadata;++ // Prioritize by type+ if (meta.type === "email") score += 100;+ else if (meta.type === "text") score += 80;+ else if (meta.type === "password") score += 70;+ else if (meta.type === "search") score += 60;+ else if (meta.type === "tel") score += 50;+ else if (meta.type === "textarea") score += 40;+ else if (meta.type === "select") score += 30;++ // Required fields are more important+ if (meta.required) score += 50;++ // Has placeholder or label+ if (meta.placeholder) score += 30;+ if (meta.label) score += 40;++ // Common email/search field patterns+ const allText =+ `${meta.placeholder} ${meta.label} ${meta.name}`.toLowerCase();+ if (allText.includes("email")) score += 80;+ if (allText.includes("search")) score += 60;+ if (allText.includes("password")) score += 50;+ if (allText.includes("name")) score += 40;++ return { input, score, idx };+ })+ .filter(+ (+ item,+ ): item is {+ input: (typeof candidateInputs)[0];+ score: number;+ idx: number;+ } => item !== null,+ );++ scoredInputs.sort((a, b) => b.score - a.score);++ // Take top 20 inputs+ const topInputs = scoredInputs.slice(0, 20);++ return topInputs+ .map(({ input }) => {+ if (!input.inputMetadata) return null;++ const meta = input.inputMetadata;+ // Hexify with the actual page background to respect light/dark mode+ const bgHex = hexify(input.colors.background, raw.pageBackground);+ const borderHex =+ input.colors.borderWidth && input.colors.borderWidth > 0+ ? hexify(input.colors.border, raw.pageBackground)+ : null;++ const corners = {+ topLeft:+ input.borderRadius?.topLeft !== null &&+ input.borderRadius?.topLeft !== undefined+ ? `${input.borderRadius.topLeft}px`+ : "0px",+ topRight:+ input.borderRadius?.topRight !== null &&+ input.borderRadius?.topRight !== undefined+ ? `${input.borderRadius.topRight}px`+ : "0px",+ bottomRight:+ input.borderRadius?.bottomRight !== null &&+ input.borderRadius?.bottomRight !== undefined+ ? `${input.borderRadius.bottomRight}px`+ : "0px",+ bottomLeft:+ input.borderRadius?.bottomLeft !== null &&+ input.borderRadius?.bottomLeft !== undefined+ ? `${input.borderRadius.bottomLeft}px`+ : "0px",+ };++ const representativeBorderRadius = calculateRepresentativeBorderRadius(+ input.borderRadius,+ );++ return {+ type: meta.type,+ placeholder: meta.placeholder,+ label: meta.label,+ name: meta.name,+ required: meta.required,+ classes: input.classes,+ background: bgHex || "transparent",+ textColor: hexify(input.colors.text, raw.pageBackground),+ borderColor: borderHex,+ borderRadius: representativeBorderRadius,+ borderRadiusCorners: corners,+ shadow: input.shadow,+ };+ })+ .filter(item => item !== null) as InputSnapshot[];+}diff --git a/apps/api/src/lib/branding/prompt.ts b/apps/api/src/lib/branding/prompt.tsindex dacafecab..44c7c417c 100644--- a/apps/api/src/lib/branding/prompt.ts+++ b/apps/api/src/lib/branding/prompt.ts@@ -164,16 +164,23 @@ export function buildBrandingPrompt(input: BrandingLLMInput): string {if (btn.borderColor) prompt += `- Border Color: ${btn.borderColor}\n`;if (btn.borderRadius) prompt += `- Border Radius: ${btn.borderRadius}\n`;prompt += `- Classes: ${btn.classes.substring(0, 150)}${btn.classes.length > 150 ? "..." : ""}\n`;- prompt += `- HTML: \`${btn.html.substring(0, 200)}${btn.html.length > 200 ? "..." : ""}\`\n\n`;+ prompt += `\n`;});}// Add logo candidates section (optimized - compact format)if (logoCandidates && logoCandidates.length > 0) {prompt += `\n## Logo Candidates (${logoCandidates.length}):\n`;+ if (input.screenshot) {+ prompt += `**IMPORTANT**: Look at the screenshot provided. The brand logo is almost always in the TOP/HEADER area of the page.\n`;+ prompt += `Find the logo in the header area of the screenshot, then match it to one of the candidates below.\n\n`;+ } else {+ prompt += `**IMPORTANT**: The brand logo is almost always in the TOP/HEADER area of the page.\n`;+ prompt += `Find the logo in the header area (usually the top of the page), then match it to one of the candidates below.\n\n`;+ }if (brandName) {- prompt += `Brand: "${brandName}" - Use this to match logos visually.\n\n`;+ prompt += `Brand Name: "${brandName}" - The logo should visually represent or contain this name.\n\n`;}// Compact format: index, location, visible, alt, indicators, href, truncated URL@@ -191,35 +198,42 @@ export function buildBrandingPrompt(input: BrandingLLMInput): string {: candidate.src;const hrefInfo = candidate.href ? ` | href:${candidate.href}` : "";+ const typeLabel = candidate.isSvg ? "SVG" : "IMG";- prompt += `#${idx}: ${candidate.location} | ${candidate.isVisible ? "visible" : "hidden"} | ${candidate.isSvg ? "SVG" : "IMG"} | alt:"${candidate.alt || ""}" | [${indicators.join(", ")}]${hrefInfo} | ${urlPreview}\n`;+ prompt += `#${idx}: ${candidate.location} | ${candidate.isVisible ? "visible" : "hidden"} | ${typeLabel} | alt:"${candidate.alt || ""}" | [${indicators.join(", ")}]${hrefInfo} | ${urlPreview}\n`;});- prompt += `\n**CRITICAL LOGO SELECTION RULES:**\n`;- prompt += `1. **Brand Name Match**: The logo MUST visually represent or match the brand name "${brandName || "unknown"}"\n`;- prompt += ` - If you see text/logos that say DIFFERENT brand names, those are CUSTOMER/PARTNER logos - SKIP THEM\n`;- prompt += ` - Only select logos that match "${brandName || "the website's main brand"}"\n`;- prompt += `2. **Location**: Prefer logos in header/navbar (highest priority)\n`;- prompt += `3. **Href Indicator**: Logos that link to "/" (homepage) are VERY LIKELY the main brand logo\n`;- prompt += ` - href="/" or href="/home" indicates homepage logo - STRONG indicator\n`;- prompt += ` - Logos with hrefMatch indicator are prioritized\n`;- prompt += `4. **Visibility**: Prefer visible logos (not hidden by dark/light mode)\n`;- prompt += `5. **AVOID**:\n`;- prompt += ` - Customer/client logos (different brand names)\n`;- prompt += ` - Partner logos (different brand names)\n`;- prompt += ` - Testimonial logos (usually in testimonials/case studies sections)\n`;- prompt += ` - GitHub stars/social media icons\n`;- prompt += ` - Logos in footer (unless no header logo exists)\n`;- prompt += ` - Logos in "customers", "partners", "clients", "case studies" sections\n`;- prompt += `6. **Use Screenshot**: Look at the screenshot to visually identify which logo is the MAIN brand logo at the top of the page\n`;- prompt += ` - The logo in the header/navbar is almost always the brand logo\n`;- prompt += ` - If multiple logos exist, choose the one that matches "${brandName || "the website brand"}"\n\n`;+ prompt += `\n**LOGO SELECTION - SIMPLE APPROACH:**\n`;+ if (input.screenshot) {+ prompt += `Look at the screenshot and select the MOST PROMINENT primary brand logo.\n\n`;+ } else {+ prompt += `Select the MOST PROMINENT primary brand logo.\n\n`;+ }+ prompt += `**Simple Rules:**\n`;+ prompt += `1. **Look at the TOP of the page** - The main logo is almost always in the header/navbar at the very top\n`;+ prompt += `2. **Primary logo** - Choose the largest, most visible logo that represents "${brandName || "the website's brand"}"\n`;+ prompt += `3. **Prefer header logos** - Logos in the header/navbar area are the brand logo (highest priority)\n`;+ prompt += `4. **Ignore partner/client logos** - Skip smaller logos in "customers", "partners", or footer sections\n`;+ if (input.screenshot) {+ prompt += `5. **Use the screenshot** - Visually identify which logo is THE main brand logo users see first\n\n`;+ } else {+ prompt += `5. **Use visual indicators** - Identify which logo is THE main brand logo based on position, size, and indicators\n\n`;+ }+ prompt += `**What to avoid:**\n`;+ prompt += `- Customer/client logos (usually smaller, in groups, different brand names)\n`;+ prompt += `- Social media icons\n`;+ prompt += `- Footer logos (unless no header logo exists)\n\n`;+ prompt += `Just pick the obvious main brand logo at the top of the page that users see first.\n\n`;}// Add background color candidates sectionif (backgroundCandidates && backgroundCandidates.length > 0) {prompt += `\n## Background Color Candidates (${backgroundCandidates.length}):\n`;- prompt += `Multiple background colors were detected. Use the screenshot to identify which is the actual page background:\n\n`;+ if (input.screenshot) {+ prompt += `Multiple background colors were detected. Use the screenshot to identify which is the actual page background:\n\n`;+ } else {+ prompt += `Multiple background colors were detected. Identify which is the actual page background:\n\n`;+ }backgroundCandidates.forEach((candidate, idx) => {const areaInfo = candidate.area@@ -228,89 +242,149 @@ export function buildBrandingPrompt(input: BrandingLLMInput): string {prompt += `#${idx}: ${candidate.color} | source: ${candidate.source} | priority: ${candidate.priority}${areaInfo}\n`;});- prompt += `\n**Selection Rules:** Use the screenshot to visually identify the main page background. Consider:\n`;- prompt += `- Color scheme (dark mode should have dark background, light mode should have light background)\n`;- prompt += `- Most visible/largest area in the screenshot\n`;+ prompt += `\n**Selection Rules:** `;+ if (input.screenshot) {+ prompt += `Use the screenshot to visually identify the main page background. Consider:\n`;+ prompt += `- Color scheme (dark mode should have dark background, light mode should have light background)\n`;+ prompt += `- Most visible/largest area in the screenshot\n`;+ } else {+ prompt += `Identify the main page background based on priority and source. Consider:\n`;+ prompt += `- Color scheme (dark mode should have dark background, light mode should have light background)\n`;+ prompt += `- Highest priority sources (body/html > CSS vars > containers)\n`;+ prompt += `- Largest area coverage\n`;+ }prompt += `- Higher priority sources (body/html > CSS vars > containers)\n`;prompt += `- Return the hex color in the colorRoles.backgroundColor field\n\n`;}// Add specific questionsprompt += `\n## Your Task:\n`;- prompt += `1. **PRIMARY Button**: Identify which button (by index 0-${buttons.length - 1}) is the main call-to-action.\n`;- prompt += ` - **CRITICAL**: Buttons with VIBRANT/BRAND COLOR backgrounds (like green, blue, orange) are ALMOST ALWAYS the primary CTA\n`;- prompt += ` - **STRONG INDICATORS**: Look for these class patterns (very high priority):\n`;- prompt += ` * \`bg-brand-400\`, \`bg-brand-500\`, or similar brand utility classes\n`;- prompt += ` * \`bg-green-*\`, \`bg-blue-*\`, \`bg-purple-*\` with high numbers (400+)\n`;- prompt += ` * Any class containing "brand", "primary", or "cta"\n`;- prompt += ` - Look for: Bright, saturated colors (green, blue, purple, orange) + action-oriented text\n`;- prompt += ` - Action-oriented text examples: "Get Started", "Sign Up", "Start Free", "Start your Project", "Try Now", "Get Started Free"\n`;- prompt += ` - If a button has BOTH vibrant color AND strong CTA text, it's DEFINITELY the primary\n`;- prompt += ` - Avoid buttons with transparent, white, or muted gray backgrounds UNLESS no vibrant buttons exist\n`;- prompt += ` - Return the button INDEX (not text) and explain your reasoning\n\n`;-- prompt += `2. **SECONDARY Button**: Identify which button is secondary (outline, ghost, or less prominent).\n`;- prompt += ` - **CRITICAL**: MUST have a DIFFERENT background color than the primary button you selected\n`;- prompt += ` - **EXAMPLES OF VALID COMBINATIONS**:\n`;- prompt += ` * Primary: #00C853 (green, vibrant) β Secondary: transparent (outline style) β\n`;- prompt += ` * Primary: #1976D2 (blue, vibrant) β Secondary: #FFFFFF (white/light) β\n`;- prompt += ` * Primary: #FF6B35 (orange, vibrant) β Secondary: #F5F5F5 (gray/subtle) β\n`;- prompt += ` - **INVALID COMBINATION**:\n`;- prompt += ` * Primary: #00C853 β Secondary: #00C853 β SAME COLOR - NOT ALLOWED!\n`;- prompt += ` - Usually has transparent/subtle background, border, or muted colors\n`;- prompt += ` - Common for actions like "Login", "Learn More", "Contact", "Documentation"\n`;- prompt += ` - Often has an outline/border instead of filled background\n`;- prompt += ` - Look at the COLOR GROUPS above - pick from a DIFFERENT group than primary\n`;- prompt += ` - If all remaining buttons have the same color as primary, set secondaryButtonIndex to -1\n`;- prompt += ` - Return the button INDEX and reasoning\n\n`;-- prompt += `3. **Color Roles**: Based on button colors and page context:\n`;++ if (buttons && buttons.length > 0) {+ prompt += `1. **PRIMARY Button**: Identify which button (by index 0-${buttons.length - 1}) is the main call-to-action.\n`;+ prompt += ` - **CRITICAL**: Buttons with VIBRANT/BRAND COLOR backgrounds (like green, blue, orange) are ALMOST ALWAYS the primary CTA\n`;+ prompt += ` - **STRONG INDICATORS**: Look for these class patterns (very high priority):\n`;+ prompt += ` * \`bg-brand-400\`, \`bg-brand-500\`, or similar brand utility classes\n`;+ prompt += ` * \`bg-green-*\`, \`bg-blue-*\`, \`bg-purple-*\` with high numbers (400+)\n`;+ prompt += ` * Any class containing "brand", "primary", or "cta"\n`;+ prompt += ` - Look for: Bright, saturated colors (green, blue, purple, orange) + action-oriented text\n`;+ prompt += ` - Action-oriented text examples: "Get Started", "Sign Up", "Start Free", "Start your Project", "Try Now", "Get Started Free"\n`;+ prompt += ` - If a button has BOTH vibrant color AND strong CTA text, it's DEFINITELY the primary\n`;+ prompt += ` - Avoid buttons with transparent, white, or muted gray backgrounds UNLESS no vibrant buttons exist\n`;+ prompt += ` - Return the button INDEX (not text) and explain your reasoning\n\n`;++ prompt += `2. **SECONDARY Button**: Identify which button is secondary (outline, ghost, or less prominent).\n`;+ prompt += ` - **CRITICAL**: MUST have a DIFFERENT background color than the primary button you selected\n`;+ prompt += ` - **EXAMPLES OF VALID COMBINATIONS**:\n`;+ prompt += ` * Primary: #00C853 (green, vibrant) β Secondary: transparent (outline style) β\n`;+ prompt += ` * Primary: #1976D2 (blue, vibrant) β Secondary: #FFFFFF (white/light) β\n`;+ prompt += ` * Primary: #FF6B35 (orange, vibrant) β Secondary: #F5F5F5 (gray/subtle) β\n`;+ prompt += ` - **INVALID COMBINATION**:\n`;+ prompt += ` * Primary: #00C853 β Secondary: #00C853 β SAME COLOR - NOT ALLOWED!\n`;+ prompt += ` - Usually has transparent/subtle background, border, or muted colors\n`;+ prompt += ` - Common for actions like "Login", "Learn More", "Contact", "Documentation"\n`;+ prompt += ` - Often has an outline/border instead of filled background\n`;+ prompt += ` - Look at the COLOR GROUPS above - pick from a DIFFERENT group than primary\n`;+ prompt += ` - If all remaining buttons have the same color as primary, set secondaryButtonIndex to -1\n`;+ prompt += ` - Return the button INDEX and reasoning\n\n`;+ }++ prompt += `${buttons && buttons.length > 0 ? "3" : "1"}. **Color Roles**: Based on ${buttons && buttons.length > 0 ? "button colors and " : ""}page context:\n`;prompt += ` - PRIMARY brand color (usually logo/heading color)\n`;- prompt += ` - ACCENT color (usually the vibrant CTA button background - green, blue, etc.)\n`;+ prompt += ` - ACCENT color (${buttons && buttons.length > 0 ? "usually the vibrant CTA button background - green, blue, etc." : "vibrant accent color from the page"})\n`;prompt += ` - Background and text colors\n\n`;- prompt += `4. **Brand Personality**: Overall tone and energy\n\n`;+ prompt += `${buttons && buttons.length > 0 ? "4" : "2"}. **Brand Personality**: Overall tone and energy\n\n`;- prompt += `5. **Design System**: Based on the class patterns shown above:\n`;+ prompt += `${buttons && buttons.length > 0 ? "5" : "3"}. **Design System**: Based on the class patterns shown above:\n`;prompt += ` - **Framework**: Identify the CSS framework (tailwind/bootstrap/material/chakra/custom/unknown)\n`;prompt += ` - **Component Library**: Look for prefixes like \`radix-\`, \`shadcn-\`, \`headlessui-\`, or \`react-aria-\` in classes\n`;prompt += ` - If using Tailwind + a component library, identify both (e.g., framework: tailwind, componentLibrary: "radix-ui")\n\n`;- prompt += `6. **Clean Fonts**: Return up to 5 cleaned, human-readable font names\n`;+ prompt += `${buttons && buttons.length > 0 ? "6" : "4"}. **Clean Fonts**: Return up to 5 cleaned, human-readable font names\n`;prompt += ` - Remove framework obfuscation (Next.js hashes, etc.)\n`;prompt += ` - Filter out generics and CSS variables\n`;prompt += ` - Prioritize by frequency (shown in usage count)\n`;prompt += ` - Assign appropriate roles (heading, body, monospace, display)\n\n`;if (logoCandidates && logoCandidates.length > 0) {- prompt += `7. **Logo Selection**: Identify the best brand logo from the ${logoCandidates.length} candidates provided above.\n`;- prompt += ` - **CRITICAL**: The logo MUST match the brand name "${brandName || "unknown"}"\n`;- prompt += ` - **REJECT** any logo that shows a DIFFERENT brand name - those are customer/partner logos\n`;- prompt += ` - Use the screenshot to visually identify which logo appears at the top of the page\n`;- prompt += ` - The logo should visually represent "${brandName || "the website's main brand"}"\n`;- prompt += ` - Prefer visible logos in header/navbar locations (these are almost always the brand logo)\n`;- prompt += ` - **AVOID**:\n`;- prompt += ` * Customer/client logos (show different company names)\n`;- prompt += ` * Partner logos (show different company names)\n`;- prompt += ` * Logos in footer, testimonials, case studies, partners sections\n`;- prompt += ` * GitHub stars, social media icons, badges\n`;- prompt += ` - Return the logo INDEX (0-${logoCandidates.length - 1}) and explain your reasoning\n`;- prompt += ` - **If you see a logo with a different brand name than "${brandName || "the website brand"}", it's NOT the brand logo - return -1 if no suitable logo matches the brand\n\n`;+ const logoTaskNumber = buttons && buttons.length > 0 ? "7" : "5";+ prompt += `${logoTaskNumber}. **Logo Selection**: Identify the best brand logo from the ${logoCandidates.length} candidates provided above.\n`;+ prompt += ` - **YOU MUST RETURN**: selectedLogoIndex (number), selectedLogoReasoning (string), and confidence (0-1)\n`;+ prompt += ` - **CRITICAL**: The logo MUST match the brand name "${brandName || "unknown"}" and look like a brand logo\n`;+ prompt += ` - **IT'S OK TO RETURN -1**: If no candidate is a good brand logo, return -1 with low confidence\n`;+ prompt += ` - **DECISION PROCESS**:\n`;+ if (input.screenshot) {+ prompt += ` 1. Look at the screenshot - find the logo in the HEADER/TOP area\n`;+ prompt += ` 2. Check which candidate matches that visual position and appearance\n`;+ } else {+ prompt += ` 1. Find the logo in the HEADER/TOP area based on candidate indicators\n`;+ prompt += ` 2. Check which candidate matches the expected position and appearance\n`;+ }+ prompt += ` 3. Verify the candidate has indicators: "header", "href=home", or "alt=logo"\n`;+ prompt += ` 4. Verify it's NOT a UI icon (search, menu, cart, user, settings, etc.)\n`;+ prompt += ` 5. If multiple candidates look similar, prefer the one with href="/" (homepage link)\n`;+ prompt += ` 6. If you're unsure or none look like brand logos, return -1\n`;+ prompt += ` - **STRONG INDICATORS** (prioritize candidates with these):\n`;+ prompt += ` * "href:/" or "hrefMatch" β Logo links to homepage (VERY STRONG indicator of brand logo)\n`;+ prompt += ` * Internal links only β Logo should NOT link to external websites\n`;+ prompt += ` * Has a link β Brand logos are usually clickable, not inside buttons\n`;+ prompt += ` * "header" location β Logo in header/navbar area (STRONG indicator)\n`;+ prompt += ` * "visible" β Logo is currently visible (preferred)\n`;+ prompt += ` * "alt=logo" or alt text matching "${brandName || "brand name"}" β Likely the brand logo\n`;+ prompt += ` * Reasonable size (not tiny like icons, not huge like hero images)\n`;+ prompt += ` - **AVOID** (return -1 if ALL candidates are these):\n`;+ prompt += ` * Very large images (likely og:image, hero images, or banners - not logos)\n`;+ prompt += ` * Images with NO LINK or inside buttons (brand logos link to homepage via <a> tag)\n`;+ prompt += ` * Images that link to EXTERNAL websites (brand logos link to homepage, not external sites)\n`;+ prompt += ` * UI icons: search icons, menu hamburgers, cart icons, user icons, settings icons\n`;+ prompt += ` * Very small square icons (< 40x40px)\n`;+ prompt += ` * Language switcher flags or text\n`;+ prompt += ` * Customer/client logos (different brand names than "${brandName || "unknown"}")\n`;+ prompt += ` * Partner/testimonial logos (usually in body, not header)\n`;+ prompt += ` * Footer logos (unless no header logo exists)\n`;+ prompt += ` * GitHub stars, social media icons, badges, external service badges\n`;+ prompt += ` * Logos in "customers", "partners", "case studies" sections\n`;+ prompt += ` - **RETURN FORMAT**:\n`;+ prompt += ` * selectedLogoIndex: The INDEX number (0-${logoCandidates.length - 1}) of the best logo, or -1 if none are good brand logos\n`;+ prompt += ` * selectedLogoReasoning: "Selected #X because [it has href:/, in header, matches brand name, etc.]" OR "No valid brand logo found - candidates are UI icons/customer logos"\n`;+ prompt += ` * confidence: 0.8-1.0 if sure, 0.5-0.7 if uncertain, 0.0-0.4 if no good match or returning -1\n`;+ prompt += ` - **PREFER -1 OVER BAD LOGOS**: Better to return no logo than a search icon or customer logo\n\n`;+ }++ if (buttons && buttons.length > 0) {+ prompt += `## VALIDATION CHECKLIST - VERIFY BEFORE RESPONDING:\n`;+ prompt += `Before finalizing your answer, check:\n`;+ prompt += `1. β Are primaryButtonIndex and secondaryButtonIndex DIFFERENT numbers?\n`;+ prompt += `2. β Do they have DIFFERENT background colors? (Compare the actual hex/color values)\n`;+ prompt += `3. β If you selected buttons from the same COLOR GROUP, go back and pick a different secondary\n`;+ prompt += `4. β If no valid secondary exists (all buttons same color as primary), set secondaryButtonIndex to -1\n\n`;++ prompt += `## FINAL RULES:\n`;+ prompt += `- Primary and secondary buttons MUST have different background colors (not just different shades - completely different colors)\n`;+ prompt += `- Primary and secondary buttons MUST be different buttons (different indices)\n`;+ prompt += `- Refer to the COLOR GROUPS section above to ensure you're picking from different groups\n`;+ prompt += `- Be decisive and confident. Prioritize vibrant, saturated colors over neutral ones for primary buttons\n`;+ prompt += `- If no clear primary/secondary exists with different colors, return -1 for that index\n`;}- prompt += `## VALIDATION CHECKLIST - VERIFY BEFORE RESPONDING:\n`;- prompt += `Before finalizing your answer, check:\n`;- prompt += `1. β Are primaryButtonIndex and secondaryButtonIndex DIFFERENT numbers?\n`;- prompt += `2. β Do they have DIFFERENT background colors? (Compare the actual hex/color values)\n`;- prompt += `3. β If you selected buttons from the same COLOR GROUP, go back and pick a different secondary\n`;- prompt += `4. β If no valid secondary exists (all buttons same color as primary), set secondaryButtonIndex to -1\n\n`;-- prompt += `## FINAL RULES:\n`;- prompt += `- Primary and secondary buttons MUST have different background colors (not just different shades - completely different colors)\n`;- prompt += `- Primary and secondary buttons MUST be different buttons (different indices)\n`;- prompt += `- Refer to the COLOR GROUPS section above to ensure you're picking from different groups\n`;- prompt += `- Be decisive and confident. Prioritize vibrant, saturated colors over neutral ones for primary buttons\n`;- prompt += `- If no clear primary/secondary exists with different colors, return -1 for that index`;+ // CRITICAL: Ensure LLM returns all required fields+ prompt += `\n## β οΈ CRITICAL: YOU MUST RETURN ALL REQUIRED FIELDS\n`;+ prompt += `The response schema REQUIRES these fields. DO NOT return empty objects {}.\n`;+ prompt += `\n**REQUIRED FIELDS:**\n`;+ let fieldNumber = 1;+ if (buttons && buttons.length > 0) {+ prompt += `${fieldNumber}. buttonClassification: { primaryButtonIndex, primaryButtonReasoning, secondaryButtonIndex, secondaryButtonReasoning, confidence }\n`;+ fieldNumber++;+ }+ prompt += `${fieldNumber}. colorRoles: { primaryColor, accentColor, backgroundColor, textPrimary, confidence }\n`;+ fieldNumber++;+ prompt += `${fieldNumber}. cleanedFonts: [] (array, can be empty but must be present)\n`;+ if (logoCandidates && logoCandidates.length > 0) {+ fieldNumber++;+ prompt += `${fieldNumber}. logoSelection: { selectedLogoIndex, selectedLogoReasoning, confidence }\n`;+ }+ prompt += `\n**DO NOT** return empty objects {}. Fill in ALL fields with actual values or -1/null as appropriate.\n`;return prompt;}diff --git a/apps/api/src/lib/branding/schema.ts b/apps/api/src/lib/branding/schema.tsindex c87b534f9..1be3cc1da 100644--- a/apps/api/src/lib/branding/schema.ts+++ b/apps/api/src/lib/branding/schema.ts@@ -3,41 +3,63 @@ import { z } from "zod";// Schema for LLM outputexport const brandingEnhancementSchema = z.object({// Button classification - LLM picks which buttons are primary/secondary- buttonClassification: z.object({- primaryButtonIndex: z- .number()- .describe(- "Index of the primary CTA button in the provided list (0-based), or -1 if none found",- ),- primaryButtonReasoning: z- .string()- .describe("Why this button was selected as primary"),- secondaryButtonIndex: z- .number()- .describe(- "Index of the secondary button in the provided list (0-based), or -1 if none found",- ),- secondaryButtonReasoning: z- .string()- .describe("Why this button was selected as secondary"),- confidence: z- .number()- .min(0)- .max(1)- .describe("Confidence in button classification (0-1)"),- }),+ buttonClassification: z+ .object({+ primaryButtonIndex: z+ .number()+ .describe(+ "REQUIRED: Index of the primary CTA button in the provided list (0-based), or -1 if none found. YOU MUST RETURN THIS FIELD.",+ ),+ primaryButtonReasoning: z+ .string()+ .describe(+ "REQUIRED: Why this button was selected as primary. YOU MUST RETURN THIS FIELD.",+ ),+ secondaryButtonIndex: z+ .number()+ .describe(+ "REQUIRED: Index of the secondary button in the provided list (0-based), or -1 if none found. YOU MUST RETURN THIS FIELD.",+ ),+ secondaryButtonReasoning: z+ .string()+ .describe(+ "REQUIRED: Why this button was selected as secondary. YOU MUST RETURN THIS FIELD.",+ ),+ confidence: z+ .number()+ .min(0)+ .max(1)+ .describe(+ "REQUIRED: Confidence in button classification (0-1). YOU MUST RETURN THIS FIELD.",+ ),+ })+ .default({+ primaryButtonIndex: -1,+ primaryButtonReasoning: "LLM did not return button classification",+ secondaryButtonIndex: -1,+ secondaryButtonReasoning: "LLM did not return button classification",+ confidence: 0,+ }),// Color role clarification- colorRoles: z.object({- primaryColor: z.string().nullish().describe("Main brand color (hex)"),- accentColor: z.string().nullish().describe("Accent/CTA color (hex)"),- backgroundColor: z- .string()- .nullish()- .describe("Main background color (hex)"),- textPrimary: z.string().nullish().describe("Primary text color (hex)"),- confidence: z.number().min(0).max(1),- }),+ colorRoles: z+ .object({+ primaryColor: z.string().nullish().describe("Main brand color (hex)"),+ accentColor: z.string().nullish().describe("Accent/CTA color (hex)"),+ backgroundColor: z+ .string()+ .nullish()+ .describe("Main background color (hex)"),+ textPrimary: z.string().nullish().describe("Primary text color (hex)"),+ confidence: z.number().min(0).max(1),+ })+ .default({+ primaryColor: null,+ accentColor: null,+ backgroundColor: null,+ textPrimary: null,+ confidence: 0,+ }),// Brand personalitypersonality: z@@ -53,7 +75,10 @@ export const brandingEnhancementSchema = z.object({]).describe("Overall brand tone"),energy: z.enum(["low", "medium", "high"]).describe("Visual energy level"),- targetAudience: z.string().describe("Perceived target audience"),+ targetAudience: z+ .string()+ .optional()+ .describe("Perceived target audience"),}).optional(),@@ -91,7 +116,8 @@ export const brandingEnhancementSchema = z.object({.max(5).describe("Top 5 cleaned fonts (remove obfuscation, fallbacks, generics, CSS vars)",- ),+ )+ .default([]),// Logo selection - LLM picks the best logo from candidateslogoSelection: z@@ -99,21 +125,22 @@ export const brandingEnhancementSchema = z.object({selectedLogoIndex: z.number().describe(- "Index of the selected logo in the provided candidates list (0-based), or -1 if none found",- )- .optional(),+ "REQUIRED: Index of the selected logo in the provided candidates list (0-based), or -1 if NONE of the candidates match the brand name. YOU MUST RETURN A NUMBER.",+ ),selectedLogoReasoning: z.string()- .describe("Why this logo was selected as the brand logo")- .optional(),+ .describe(+ "REQUIRED: Detailed explanation of why this logo was selected, or why none were suitable if returning -1. YOU MUST PROVIDE REASONING.",+ ),confidence: z.number().min(0).max(1)- .describe("Confidence in logo selection (0-1)")- .optional(),+ .describe(+ "REQUIRED: Confidence in logo selection (0-1). Return 0 if no suitable logo found.",+ ),})- .optional(),+ .optional(), // Still optional at top level so it's only required when logo candidates are provided});export type BrandingEnhancement = z.infer<typeof brandingEnhancementSchema>;diff --git a/apps/api/src/lib/branding/transformer.ts b/apps/api/src/lib/branding/transformer.tsindex 8a6471448..66b390fcd 100644--- a/apps/api/src/lib/branding/transformer.ts+++ b/apps/api/src/lib/branding/transformer.ts@@ -6,6 +6,12 @@ import { Meta } from "../../scraper/scrapeURL";import { Document } from "../../controllers/v2/types";import { BrandingScriptReturn, ButtonSnapshot } from "./types";import { mergeBrandingResults } from "./merge";+import {+ selectLogoWithConfidence,+ shouldUseLLMForLogoSelection,+ getTopCandidatesForLLM,+} from "./logo-selector";+import { calculateLogoArea } from "./types";export async function brandingTransformer(meta: Meta,@@ -21,48 +27,116 @@ export async function brandingTransformer(let brandingProfile: BrandingProfile = jsBranding;try {- meta.logger.info("Enhancing branding with LLM...");-const buttonSnapshots: ButtonSnapshot[] =(jsBranding as any).__button_snapshots || [];+ const inputSnapshots = (jsBranding as any).__input_snapshots || [];const logoCandidates = rawBranding.logoCandidates || [];const brandName = rawBranding.brandName;const backgroundCandidates = rawBranding.backgroundCandidates || [];- // Optimize logo candidates: limit to top 15, prioritize by indicators- const optimizedCandidates = logoCandidates- .sort((a, b) => {- // Score candidates: higher score = better- const scoreA =- (a.indicators.inHeader ? 10 : 0) +- (a.indicators.hrefMatch ? 8 : 0) + // href="/" is strong indicator- (a.isVisible ? 5 : 0) +- (a.indicators.altMatch ? 3 : 0) +- (a.indicators.srcMatch ? 2 : 0) +- (a.indicators.classMatch ? 2 : 0) +- (a.location === "header" ? 5 : 0);- const scoreB =- (b.indicators.inHeader ? 10 : 0) +- (b.indicators.hrefMatch ? 8 : 0) + // href="/" is strong indicator- (b.isVisible ? 5 : 0) +- (b.indicators.altMatch ? 3 : 0) +- (b.indicators.srcMatch ? 2 : 0) +- (b.indicators.classMatch ? 2 : 0) +- (b.location === "header" ? 5 : 0);- return scoreB - scoreA;- })- .slice(0, 15); // Limit to top 15 candidates-- meta.logger.info(- `Sending ${buttonSnapshots.length} buttons and ${optimizedCandidates.length} logo candidates (from ${logoCandidates.length} total) to LLM for classification`,- );+ // TIER 1: Use smart heuristics to get initial selection+ const heuristicResult =+ logoCandidates.length > 0+ ? selectLogoWithConfidence(logoCandidates, brandName)+ : null;++ if (logoCandidates.length === 0) {+ meta.logger.warn("No logo candidates found", { brandName });+ } else {+ meta.logger.info("Logo heuristic selection", {+ candidatesCount: logoCandidates.length,+ selectedIndex: heuristicResult?.selectedIndex,+ confidence: heuristicResult?.confidence,+ method: heuristicResult?.method,+ });+ }++ // TIER 2: Decide if we need LLM validation+ const needsLLMValidation = heuristicResult+ ? shouldUseLLMForLogoSelection(heuristicResult.confidence)+ : false;++ // Filter to top 10 candidates for LLM (reduces token cost)+ const { filteredCandidates, indexMap } =+ needsLLMValidation && logoCandidates.length > 0+ ? getTopCandidatesForLLM(logoCandidates, 10)+ : { filteredCandidates: [], indexMap: new Map<number, number>() };++ // Limit buttons to top 12 most relevant ones to reduce prompt size+ // Prioritize buttons with CTA indicators, vibrant colors, or common CTA text+ let limitedButtons = buttonSnapshots;+ const buttonIndexMap = new Map<number, number>(); // LLM index -> original index++ if (buttonSnapshots.length > 12) {+ const scored = buttonSnapshots+ .map((btn, idx) => {+ let score = 0;+ const text = (btn.text || "").toLowerCase();+ const bgColor = btn.background || "";++ // Score common CTA text (higher priority)+ const primaryCtaKeywords = [+ "get started",+ "sign up",+ "sign in",+ "login",+ "register",+ "read",+ "learn",+ "download",+ "buy",+ "shop",+ ];+ if (primaryCtaKeywords.some(keyword => text.includes(keyword)))+ score += 100;++ // Score secondary CTA text+ const secondaryCtaKeywords = ["try", "start", "view", "explore"];+ if (secondaryCtaKeywords.some(keyword => text.includes(keyword)))+ score += 50;++ // Score vibrant colors (not white/transparent/gray)+ if (+ bgColor &&+ !bgColor.match(+ /transparent|white|#fff|#ffffff|gray|grey|#f[0-9a-f]{5}/i,+ )+ ) {+ score += 30;+ }++ return { btn, originalIdx: idx, score };+ })+ .sort((a, b) => b.score - a.score)+ .slice(0, 12);++ limitedButtons = scored.map(item => item.btn);++ // Create index map: LLM index (0-11) -> original index+ scored.forEach((item, llmIdx) => {+ buttonIndexMap.set(llmIdx, item.originalIdx);+ });+ } else {+ // No filtering needed, create identity map+ buttonSnapshots.forEach((_, idx) => {+ buttonIndexMap.set(idx, idx);+ });+ }++ meta.logger.info("Button filtering for LLM", {+ totalButtons: buttonSnapshots.length,+ limitedButtons: limitedButtons.length,+ });+ // TIER 2: Only call LLM if heuristics are uncertainconst llmEnhancement = await enhanceBrandingWithLLM({jsAnalysis: jsBranding,- buttons: buttonSnapshots,+ buttons: limitedButtons,logoCandidates:- optimizedCandidates.length > 0 ? optimizedCandidates : undefined,+ needsLLMValidation && filteredCandidates.length > 0+ ? filteredCandidates+ : undefined, // Don't send logo candidates if heuristics are confidentbrandName,backgroundCandidates:backgroundCandidates.length > 0 ? backgroundCandidates : undefined,@@ -71,7 +145,164 @@ export async function brandingTransformer(teamId: meta.internalOptions.teamId,});- meta.logger.info("LLM enhancement complete", {+ // Map LLM's filtered index back to original index for logos+ if (needsLLMValidation && llmEnhancement.logoSelection) {+ const llmFilteredIndex = llmEnhancement.logoSelection.selectedLogoIndex;+ const llmOriginalIndex = indexMap.get(llmFilteredIndex);++ if (llmOriginalIndex !== undefined) {+ // Update the selection with the original index+ llmEnhancement.logoSelection.selectedLogoIndex = llmOriginalIndex;+ } else {+ // LLM returned invalid index - fallback to heuristic+ meta.logger.warn(+ "LLM returned invalid logo index, falling back to heuristic",+ {+ llmFilteredIndex,+ indexMapSize: indexMap.size,+ validIndices: Array.from(indexMap.keys()),+ heuristicIndex: heuristicResult?.selectedIndex,+ },+ );+ // Use heuristic result if available, otherwise clear LLM selection+ if (heuristicResult) {+ llmEnhancement.logoSelection = {+ selectedLogoIndex: heuristicResult.selectedIndex,+ selectedLogoReasoning: `Heuristic fallback (LLM returned invalid index): ${heuristicResult.reasoning}`,+ confidence: Math.max(heuristicResult.confidence - 0.1, 0.3), // Slightly lower confidence+ };+ } else {+ // No heuristic result available - clear LLM selection+ llmEnhancement.logoSelection = undefined;+ }+ }+ }++ // Map LLM's button indices back to original indices+ if (llmEnhancement.buttonClassification) {+ const llmPrimaryIdx =+ llmEnhancement.buttonClassification.primaryButtonIndex;+ const llmSecondaryIdx =+ llmEnhancement.buttonClassification.secondaryButtonIndex;++ if (llmPrimaryIdx >= 0) {+ const originalPrimaryIdx = buttonIndexMap.get(llmPrimaryIdx);+ if (originalPrimaryIdx !== undefined) {+ llmEnhancement.buttonClassification.primaryButtonIndex =+ originalPrimaryIdx;+ }+ }++ if (llmSecondaryIdx >= 0) {+ const originalSecondaryIdx = buttonIndexMap.get(llmSecondaryIdx);+ if (originalSecondaryIdx !== undefined) {+ llmEnhancement.buttonClassification.secondaryButtonIndex =+ originalSecondaryIdx;+ }+ }+ }++ // TIER 3: Merge heuristic and LLM results+ if (heuristicResult && logoCandidates.length > 0) {+ if (needsLLMValidation && llmEnhancement.logoSelection) {+ // LLM validation was used - validate it against heuristic+ const llmOriginalIndex = llmEnhancement.logoSelection.selectedLogoIndex;+ const heuristicSelectedIndex = heuristicResult.selectedIndex;++ // If LLM picked a different logo, validate it's not worse+ if (+ llmOriginalIndex !== undefined &&+ llmOriginalIndex !== heuristicSelectedIndex &&+ llmOriginalIndex >= 0 &&+ llmOriginalIndex < logoCandidates.length+ ) {+ const llmCandidate = logoCandidates[llmOriginalIndex];+ const heuristicCandidate = logoCandidates[heuristicSelectedIndex];++ // Calculate logo sizes+ const llmArea = calculateLogoArea(llmCandidate.position);+ const heuristicArea = calculateLogoArea(heuristicCandidate.position);++ // Red flags: LLM picked a logo that's objectively worse+ const llmIsWorse =+ // LLM picked non-header logo when heuristic picked header logo+ (!llmCandidate.indicators?.inHeader &&+ heuristicCandidate.indicators?.inHeader) ||+ // LLM picked non-visible logo when heuristic picked visible logo+ (!llmCandidate.isVisible && heuristicCandidate.isVisible) ||+ // LLM picked logo without href="/" when heuristic has it+ (!llmCandidate.indicators?.hrefMatch &&+ heuristicCandidate.indicators?.hrefMatch) ||+ // LLM picked logo from body/footer when heuristic picked from header+ (llmCandidate.location !== "header" &&+ heuristicCandidate.location === "header") ||+ // LLM picked much smaller logo (less than 50% of heuristic area) AND heuristic is in header+ (llmArea < heuristicArea * 0.5 &&+ heuristicCandidate.indicators?.inHeader) ||+ // LLM picked very small logo (<500pxΒ²) when heuristic picked reasonable size+ (llmArea < 500 && heuristicArea > 1000);++ if (llmIsWorse) {+ meta.logger.warn(+ "LLM picked objectively worse logo - using heuristic instead",+ {+ llmOriginalIndex: llmOriginalIndex,+ llmLocation: llmCandidate.location,+ llmVisible: llmCandidate.isVisible,+ llmInHeader: llmCandidate.indicators?.inHeader,+ llmHrefMatch: llmCandidate.indicators?.hrefMatch,+ llmArea: Math.round(llmArea),+ llmSrc: llmCandidate.src.substring(0, 100),+ heuristicIndex: heuristicSelectedIndex,+ heuristicLocation: heuristicCandidate.location,+ heuristicVisible: heuristicCandidate.isVisible,+ heuristicInHeader: heuristicCandidate.indicators?.inHeader,+ heuristicHrefMatch: heuristicCandidate.indicators?.hrefMatch,+ heuristicArea: Math.round(heuristicArea),+ heuristicSrc: heuristicCandidate.src.substring(0, 100),+ },+ );++ // Override LLM with heuristic+ llmEnhancement.logoSelection = {+ selectedLogoIndex: heuristicResult.selectedIndex,+ selectedLogoReasoning: `Heuristic preferred over LLM (LLM picked worse logo): ${heuristicResult.reasoning}`,+ confidence: heuristicResult.confidence,+ };+ } else {+ // LLM picked something different but not objectively worse - trust it+ meta.logger.info(+ "Using LLM-validated logo selection (different from heuristic but valid)",+ );+ }+ } else {+ // LLM agreed with heuristic or picked invalid index+ meta.logger.info("Using LLM-validated logo selection");+ }+ } else if (!needsLLMValidation) {+ // Heuristics were confident - use them directly+ meta.logger.info(+ "Using heuristic logo selection (high confidence, skipped LLM)",+ );+ llmEnhancement.logoSelection = {+ selectedLogoIndex: heuristicResult.selectedIndex,+ selectedLogoReasoning: heuristicResult.reasoning,+ confidence: heuristicResult.confidence,+ };+ } else if (!llmEnhancement.logoSelection) {+ // LLM was called but didn't return selection - fallback to heuristic+ meta.logger.warn(+ "LLM validation requested but didn't return selection, using heuristic",+ );+ llmEnhancement.logoSelection = {+ selectedLogoIndex: heuristicResult.selectedIndex,+ selectedLogoReasoning: `Heuristic fallback: ${heuristicResult.reasoning}`,+ confidence: Math.max(heuristicResult.confidence - 0.1, 0.3), // Slightly lower confidence+ };+ }+ }++ meta.logger.info("Branding enhancement complete", {primary_btn_index: llmEnhancement.buttonClassification.primaryButtonIndex,secondary_btn_index:llmEnhancement.buttonClassification.secondaryButtonIndex,@@ -79,6 +310,12 @@ export async function brandingTransformer(color_confidence: llmEnhancement.colorRoles.confidence,logo_selected_index: llmEnhancement.logoSelection?.selectedLogoIndex,logo_confidence: llmEnhancement.logoSelection?.confidence,+ logo_selection_method: needsLLMValidation+ ? llmEnhancement.logoSelection+ ? "llm-validated"+ : "heuristic-fallback"+ : "heuristic-only",+ llm_called_for_logo: needsLLMValidation,});brandingProfile = mergeBrandingResults(@@ -87,6 +324,11 @@ export async function brandingTransformer(buttonSnapshots,logoCandidates.length > 0 ? logoCandidates : undefined,);++ meta.logger.info("Input fields detected", {+ count: inputSnapshots.length,+ types: inputSnapshots.map((i: any) => i.type).slice(0, 10),+ });} catch (error) {meta.logger.error("LLM branding enhancement failed, using JS analysis only",@@ -97,6 +339,7 @@ export async function brandingTransformer(if (config.DEBUG_BRANDING !== true) {delete (brandingProfile as any).__button_snapshots;+ delete (brandingProfile as any).__input_snapshots;}return brandingProfile;diff --git a/apps/api/src/lib/branding/types.ts b/apps/api/src/lib/branding/types.tsindex 6e86695e9..720c60ef8 100644--- a/apps/api/src/lib/branding/types.ts+++ b/apps/api/src/lib/branding/types.ts@@ -9,6 +9,12 @@ export interface ButtonSnapshot {textColor: string;borderColor?: string | null;borderRadius?: string;+ borderRadiusCorners?: {+ topLeft?: string;+ topRight?: string;+ bottomRight?: string;+ bottomLeft?: string;+ };shadow?: string | null;// Debug: original color values before conversion to hexoriginalBackgroundColor?: string;@@ -16,6 +22,26 @@ export interface ButtonSnapshot {originalBorderColor?: string;}+export interface InputSnapshot {+ type: string;+ placeholder: string;+ label: string;+ name: string;+ required: boolean;+ classes: string;+ background: string;+ textColor: string | null;+ borderColor?: string | null;+ borderRadius?: string;+ borderRadiusCorners?: {+ topLeft?: string;+ topRight?: string;+ bottomRight?: string;+ bottomLeft?: string;+ };+ shadow?: string | null;+}+export interface BrandingLLMInput {jsAnalysis: BrandingProfile;buttons: ButtonSnapshot[];@@ -24,7 +50,7 @@ export interface BrandingLLMInput {alt: string;isSvg: boolean;isVisible: boolean;- location: "header" | "body";+ location: "header" | "body" | "footer";position: { top: number; left: number; width: number; height: number };indicators: {inHeader: boolean;@@ -67,6 +93,14 @@ export interface BrandingScriptReturn {background: string;border: string;borderWidth: number | null;+ borderTop?: string;+ borderTopWidth?: number | null;+ borderRight?: string;+ borderRightWidth?: number | null;+ borderBottom?: string;+ borderBottomWidth?: number | null;+ borderLeft?: string;+ borderLeftWidth?: number | null;};typography: {fontStack: string[];@@ -74,11 +108,27 @@ export interface BrandingScriptReturn {weight: number | null;};radius: number | null;+ borderRadius: {+ topLeft: number | null;+ topRight: number | null;+ bottomRight: number | null;+ bottomLeft: number | null;+ };shadow: string | null;isButton: boolean;isNavigation?: boolean;hasCTAIndicator?: boolean;isInput: boolean;+ inputMetadata?: {+ type: string;+ placeholder: string;+ value: string;+ required: boolean;+ disabled: boolean;+ name: string;+ id: string;+ label: string;+ } | null;isLink: boolean;}>;images: Array<{ type: string; src: string }>;@@ -87,7 +137,7 @@ export interface BrandingScriptReturn {alt: string;isSvg: boolean;isVisible: boolean;- location: "header" | "body";+ location: "header" | "body" | "footer";position: { top: number; left: number; width: number; height: number };indicators: {inHeader: boolean;@@ -122,3 +172,14 @@ export interface BrandingScriptReturn {area?: number;}>;}++/**+ * Calculate logo area from position dimensions+ */+export function calculateLogoArea(position?: {+ width?: number;+ height?: number;+}): number {+ if (!position) return 0;+ return (position.width || 0) * (position.height || 0);+}diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/brandingScript.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/brandingScript.tsindex e06d77ed4..b79d0395a 100644--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/brandingScript.ts+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/brandingScript.ts@@ -1,5 +1,40 @@export const getBrandingScript = () => String.raw`(function __extractBrandDesign() {+ const errors = [];+ const recordError = (context, error) => {+ errors.push({+ context: context,+ message: error && error.message ? error.message : String(error),+ timestamp: Date.now(),+ });+ };++ const CONSTANTS = {+ BUTTON_MIN_WIDTH: 50,+ BUTTON_MIN_HEIGHT: 25,+ BUTTON_MIN_PADDING_VERTICAL: 3,+ BUTTON_MIN_PADDING_HORIZONTAL: 6,+ MAX_PARENT_TRAVERSAL: 5,+ MAX_BACKGROUND_SAMPLES: 100,+ MIN_SIGNIFICANT_AREA: 1000,+ MIN_LARGE_CONTAINER_AREA: 10000,+ DUPLICATE_POSITION_THRESHOLD: 1,+ MIN_LOGO_SIZE: 25,+ MIN_ALPHA_THRESHOLD: 0.1,+ MAX_TRANSPARENT_ALPHA: 0.01,+ BUTTON_SELECTOR: 'button,input[type="submit"],input[type="button"],[role=button],[data-primary-button],[data-secondary-button],[data-cta],a.button,a.btn,[class*="btn"],[class*="button"],a[class*="bg-brand"],a[class*="bg-primary"],a[class*="bg-accent"]',+ };++ const styleCache = new WeakMap();+ const getComputedStyleCached = (el) => {+ if (styleCache.has(el)) {+ return styleCache.get(el);+ }+ const style = getComputedStyle(el);+ styleCache.set(el, style);+ return style;+ };+const toPx = v => {if (!v || v === "auto") return null;if (v.endsWith("px")) return parseFloat(v);@@ -18,6 +53,24 @@ export const getBrandingScript = () => String.raw`return Number.isFinite(num) ? num : null;};+ const getClassNameString = (el) => {+ if (!el || !el.className) return '';+ try {+ if (el.className.baseVal !== undefined) {+ return String(el.className.baseVal || '');+ }+ if (typeof el.className.toString === 'function') {+ return String(el.className);+ }+ if (typeof el.className === 'string') {+ return el.className;+ }+ return String(el.className || '');+ } catch (e) {+ return '';+ }+ };+const resolveSvgStyles = svg => {const originalElements = [svg, ...svg.querySelectorAll("*")];const computedStyles = originalElements.map(el => ({@@ -100,6 +153,7 @@ export const getBrandingScript = () => String.raw`try {rules = sheet.cssRules;} catch (e) {+ recordError('collectCSSData - CORS stylesheet', e);continue;}if (!rules) continue;@@ -157,98 +211,96 @@ export const getBrandingScript = () => String.raw`return data;};- // Helper to check if an element looks like a button (has button-like styling)- const looksLikeButton = (el) => {+ const checkButtonLikeElement = (el, cs, rect, classNames) => {+ const hasButtonClasses =+ /rounded(-md|-lg|-xl|-full)?/.test(classNames) ||+ /px-\d+/.test(classNames) ||+ /py-\d+/.test(classNames) ||+ /p-\d+/.test(classNames) ||+ (/border/.test(classNames) && /rounded/.test(classNames)) ||+ (/inline-flex/.test(classNames) && /items-center/.test(classNames) && /justify-center/.test(classNames));++ if (hasButtonClasses && rect.width > CONSTANTS.BUTTON_MIN_WIDTH && rect.height > CONSTANTS.BUTTON_MIN_HEIGHT) {+ return true;+ }++ const paddingTop = parseFloat(cs.paddingTop) || 0;+ const paddingBottom = parseFloat(cs.paddingBottom) || 0;+ const paddingLeft = parseFloat(cs.paddingLeft) || 0;+ const paddingRight = parseFloat(cs.paddingRight) || 0;+ const hasPadding = paddingTop > CONSTANTS.BUTTON_MIN_PADDING_VERTICAL ||+ paddingBottom > CONSTANTS.BUTTON_MIN_PADDING_VERTICAL ||+ paddingLeft > CONSTANTS.BUTTON_MIN_PADDING_HORIZONTAL ||+ paddingRight > CONSTANTS.BUTTON_MIN_PADDING_HORIZONTAL;+ const hasMinSize = rect.width > CONSTANTS.BUTTON_MIN_WIDTH && rect.height > CONSTANTS.BUTTON_MIN_HEIGHT;+ const hasRounded = parseFloat(cs.borderRadius) > 0;+ const hasBorder = parseFloat(cs.borderTopWidth) > 0 || parseFloat(cs.borderBottomWidth) > 0 ||+ parseFloat(cs.borderLeftWidth) > 0 || parseFloat(cs.borderRightWidth) > 0;++ return hasPadding && hasMinSize && (hasRounded || hasBorder);+ };++ const isButtonElement = (el) => {if (!el || typeof el.matches !== 'function') return false;- // Check explicit button indicators- if (el.matches('button, [role=button], [data-primary-button], [data-secondary-button], [data-cta], a.button, a.btn, [class*="btn"], [class*="button"], a[class*="bg-brand"], a[class*="bg-primary"], a[class*="bg-accent"], a[type="button"]')) {+ if (el.matches(CONSTANTS.BUTTON_SELECTOR)) {return true;}- // For links, check if they have button-like stylingif (el.tagName.toLowerCase() === 'a') {try {- const classes = (el.className || '').toLowerCase();- const classStr = classes;-- // Check for common button class patterns (Tailwind, Bootstrap, etc.)- const hasButtonClasses =- /rounded(-md|-lg|-xl|-full)?/.test(classStr) || // rounded corners- /px-\d+/.test(classStr) || // horizontal padding (px-2, px-4, etc.)- /py-\d+/.test(classStr) || // vertical padding (py-2, py-4, etc.)- /p-\d+/.test(classStr) || // padding (p-2, p-4, etc.)- (/border/.test(classStr) && /rounded/.test(classStr)) || // border + rounded- (/inline-flex/.test(classStr) && /items-center/.test(classStr) && /justify-center/.test(classStr)); // flexbox button pattern-- if (hasButtonClasses) {- const cs = getComputedStyle(el);- const rect = el.getBoundingClientRect();-- // Verify it has reasonable button dimensions- if (rect.width > 50 && rect.height > 25) {- return true;- }- }-- // Also check computed styles for button-like appearance- const cs = getComputedStyle(el);+ const classNames = getClassNameString(el).toLowerCase();+ const cs = getComputedStyleCached(el);const rect = el.getBoundingClientRect();-- // Check for button-like padding and dimensions- const paddingTop = parseFloat(cs.paddingTop) || 0;- const paddingBottom = parseFloat(cs.paddingBottom) || 0;- const paddingLeft = parseFloat(cs.paddingLeft) || 0;- const paddingRight = parseFloat(cs.paddingRight) || 0;- const hasPadding = paddingTop > 3 || paddingBottom > 3 || paddingLeft > 6 || paddingRight > 6;- const hasMinSize = rect.width > 50 && rect.height > 25;- const hasRounded = parseFloat(cs.borderRadius) > 0;- const hasBorder = parseFloat(cs.borderTopWidth) > 0 || parseFloat(cs.borderBottomWidth) > 0 ||- parseFloat(cs.borderLeftWidth) > 0 || parseFloat(cs.borderRightWidth) > 0;-- // Button-like if has padding + (rounded or border) and reasonable size- if (hasPadding && hasMinSize && (hasRounded || hasBorder)) {- return true;- }+ return checkButtonLikeElement(el, cs, rect, classNames);} catch (e) {- // If we can't check styles, fall back to class matching+ recordError('isButtonElement', e);+ return false;}}return false;};+ const looksLikeButton = (el) => {+ return isButtonElement(el);+ };+const sampleElements = () => {- const picks = [];+ const picksSet = new Set();+const pushQ = (q, limit = 10) => {- for (const el of Array.from(document.querySelectorAll(q)).slice(0, limit))- picks.push(el);+ const elements = document.querySelectorAll(q);+ let count = 0;+ for (const el of elements) {+ if (count >= limit) break;+ picksSet.add(el);+ count++;+ }};pushQ('header img, .site-logo img, img[alt*=logo i], img[src*="logo"]', 5);- // First, get explicit buttonspushQ(- 'button, [role=button], [data-primary-button], [data-secondary-button], [data-cta], a.button, a.btn, [class*="btn"], [class*="button"], a[class*="bg-brand"], a[class*="bg-primary"], a[class*="bg-accent"], a[type="button"], a[type="button"][class*="bg-"]',+ 'button, input[type="submit"], input[type="button"], [role=button], [data-primary-button], [data-secondary-button], [data-cta], a.button, a.btn, [class*="btn"], [class*="button"], a[class*="bg-brand"], a[class*="bg-primary"], a[class*="bg-accent"]',100,);- // Also check all links for button-like styling- const allLinks = Array.from(document.querySelectorAll('a'));- for (const link of allLinks.slice(0, 100)) {- if (looksLikeButton(link)) {- picks.push(link);+ const allLinks = Array.from(document.querySelectorAll('a')).slice(0, 100);+ for (const link of allLinks) {+ if (!picksSet.has(link) && looksLikeButton(link)) {+ picksSet.add(link);}}pushQ('input, select, textarea, [class*="form-control"]', 25);pushQ("h1, h2, h3, p, a", 50);- return Array.from(new Set(picks.filter(Boolean)));+ return Array.from(picksSet).filter(Boolean);};const getStyleSnapshot = el => {- const cs = getComputedStyle(el);+ const cs = getComputedStyleCached(el);const rect = el.getBoundingClientRect();const fontStack =@@ -264,42 +316,37 @@ export const getBrandingScript = () => String.raw`const attrClass = el.getAttribute("class");if (attrClass) classNames = attrClass.toLowerCase();}- if (!classNames && el.className) {- if (typeof el.className === "string") {- classNames = el.className.toLowerCase();- } else if (el.className.baseVal) {- classNames = el.className.baseVal.toLowerCase();- }+ if (!classNames) {+ classNames = getClassNameString(el).toLowerCase();}} catch (e) {try {- if (el.className && typeof el.className === "string") {- classNames = el.className.toLowerCase();- }+ classNames = getClassNameString(el).toLowerCase();} catch (e2) {classNames = "";}}- // Get colors as-is from computed stylelet bgColor = cs.getPropertyValue("background-color");const textColor = cs.getPropertyValue("color");- // For transparent backgrounds, try to get the background from parent containerconst isTransparent = bgColor === "transparent" || bgColor === "rgba(0, 0, 0, 0)";const alphaMatch = bgColor.match(/rgba?\([^,]*,[^,]*,[^,]*,\s*([\d.]+)\)/);const hasZeroAlpha = alphaMatch && parseFloat(alphaMatch[1]) === 0;- if (isTransparent || hasZeroAlpha) {- // Walk up the DOM to find a non-transparent background+ const isInputElement = el.tagName.toLowerCase() === 'input' ||+ el.tagName.toLowerCase() === 'select' ||+ el.tagName.toLowerCase() === 'textarea';++ if ((isTransparent || hasZeroAlpha) && !isInputElement) {let parent = el.parentElement;let depth = 0;- while (parent && depth < 5) {- const parentBg = getComputedStyle(parent).getPropertyValue("background-color");+ while (parent && depth < CONSTANTS.MAX_PARENT_TRAVERSAL) {+ const parentBg = getComputedStyleCached(parent).getPropertyValue("background-color");if (parentBg && parentBg !== "transparent" && parentBg !== "rgba(0, 0, 0, 0)") {const parentAlphaMatch = parentBg.match(/rgba?\([^,]*,[^,]*,[^,]*,\s*([\d.]+)\)/);const parentAlpha = parentAlphaMatch ? parseFloat(parentAlphaMatch[1]) : 1;- if (parentAlpha > 0.1) {+ if (parentAlpha > CONSTANTS.MIN_ALPHA_THRESHOLD) {bgColor = parentBg;break;}@@ -309,47 +356,7 @@ export const getBrandingScript = () => String.raw`}}- // Check if element is a button - use same logic as sampleElements- let isButton = false;- if (el.matches('button,[role=button],[data-primary-button],[data-secondary-button],[data-cta],a.button,a.btn,[class*="btn"],[class*="button"],a[class*="bg-brand"],a[class*="bg-primary"],a[class*="bg-accent"],a[type="button"],a[type="button"][class*="bg-"]')) {- isButton = true;- } else if (el.tagName.toLowerCase() === 'a') {- // Check if link looks like a button (has button-like styling)- try {- const classes = classNames;-- // Check for common button class patterns (Tailwind, Bootstrap, etc.)- const hasButtonClasses =- /rounded(-md|-lg|-xl|-full)?/.test(classes) || // rounded corners- /px-\d+/.test(classes) || // horizontal padding (px-2, px-4, etc.)- /py-\d+/.test(classes) || // vertical padding (py-2, py-4, etc.)- /p-\d+/.test(classes) || // padding (p-2, p-4, etc.)- (/border/.test(classes) && /rounded/.test(classes)) || // border + rounded- (/inline-flex/.test(classes) && /items-center/.test(classes) && /justify-center/.test(classes)); // flexbox button pattern-- if (hasButtonClasses && rect.width > 50 && rect.height > 25) {- isButton = true;- } else {- // Also check computed styles for button-like appearance- const paddingTop = parseFloat(cs.paddingTop) || 0;- const paddingBottom = parseFloat(cs.paddingBottom) || 0;- const paddingLeft = parseFloat(cs.paddingLeft) || 0;- const paddingRight = parseFloat(cs.paddingRight) || 0;- const hasPadding = paddingTop > 3 || paddingBottom > 3 || paddingLeft > 6 || paddingRight > 6;- const hasMinSize = rect.width > 50 && rect.height > 25;- const hasRounded = parseFloat(cs.borderRadius) > 0;- const hasBorder = parseFloat(cs.borderTopWidth) > 0 || parseFloat(cs.borderBottomWidth) > 0 ||- parseFloat(cs.borderLeftWidth) > 0 || parseFloat(cs.borderRightWidth) > 0;-- // Button-like if has padding + (rounded or border) and reasonable size- if (hasPadding && hasMinSize && (hasRounded || hasBorder)) {- isButton = true;- }- }- } catch (e) {- // If we can't check styles, not a button- }- }+ const isButton = isButtonElement(el);let isNavigation = false;let hasCTAIndicator = false;@@ -363,7 +370,6 @@ export const getBrandingScript = () => String.raw`el.getAttribute("data-secondary-button") === "true";if (!hasCTAIndicator) {- // Check for navigation-related classes and attributesconst hasNavClass = classNames.includes("nav-") ||classNames.includes("-nav") ||classNames.includes("nav-anchor") ||@@ -375,17 +381,14 @@ export const getBrandingScript = () => String.raw`classNames.includes("toggle") ||classNames.includes("trigger");- // Check for navigation-related roles and attributesconst hasNavRole = el.matches('[role="tab"],[role="menuitem"],[role="menuitemcheckbox"],[aria-haspopup],[aria-expanded]',);- // Check if in navigation contexts (sidebar, nav, menu, etc.)const inNavContext = !!el.closest('nav, [role="navigation"], [role="menu"], [role="menubar"], [class*="navigation"], [class*="dropdown"], [class*="sidebar"], [id*="sidebar"], [id*="navigation"], [id*="nav-"], aside[class*="nav"], aside[id*="nav"]',);- // Check if it's a link in a list item (common nav pattern)let isNavLink = false;if (el.tagName.toLowerCase() === "a" && el.parentElement) {if (el.parentElement.tagName.toLowerCase() === "li") {@@ -400,16 +403,74 @@ export const getBrandingScript = () => String.raw`}} catch (e) {}+ let text = "";+ if (el.tagName.toLowerCase() === 'input' && (el.type === 'submit' || el.type === 'button')) {+ text = (el.value && el.value.trim().substring(0, 100)) || "";+ } else {+ text = (el.textContent && el.textContent.trim().substring(0, 100)) || "";+ }++ const isInputField = el.matches('input:not([type="submit"]):not([type="button"]),select,textarea,[class*="form-control"]');+ let inputMetadata = null;+ if (isInputField) {+ const tagName = el.tagName.toLowerCase();+ inputMetadata = {+ type: tagName === 'input' ? (el.type || 'text') : tagName,+ placeholder: el.placeholder || "",+ value: tagName === 'input' ? (el.value || "") : "",+ required: el.required || false,+ disabled: el.disabled || false,+ name: el.name || "",+ id: el.id || "",+ label: (() => {+ if (el.id) {+ const label = document.querySelector('label[for="' + el.id + '"]');+ if (label) return (label.textContent || "").trim().substring(0, 100);+ }+ const parentLabel = el.closest('label');+ if (parentLabel) {+ const clone = parentLabel.cloneNode(true);+ const inputInClone = clone.querySelector('input,select,textarea');+ if (inputInClone) inputInClone.remove();+ return (clone.textContent || "").trim().substring(0, 100);+ }+ return "";+ })(),+ };+ }+return {tag: el.tagName.toLowerCase(),classes: classNames,- text: (el.textContent && el.textContent.trim().substring(0, 100)) || "",+ text: text,rect: { w: rect.width, h: rect.height },colors: {text: textColor,background: bgColor,- border: cs.getPropertyValue("border-top-color"),- borderWidth: toPx(cs.getPropertyValue("border-top-width")),+ border: (() => {+ const top = cs.getPropertyValue("border-top-color");+ const right = cs.getPropertyValue("border-right-color");+ const bottom = cs.getPropertyValue("border-bottom-color");+ const left = cs.getPropertyValue("border-left-color");+ if (top === right && top === bottom && top === left) return top;+ return top;+ })(),+ borderWidth: (() => {+ const top = toPx(cs.getPropertyValue("border-top-width"));+ const right = toPx(cs.getPropertyValue("border-right-width"));+ const bottom = toPx(cs.getPropertyValue("border-bottom-width"));+ const left = toPx(cs.getPropertyValue("border-left-width"));+ if (top === right && top === bottom && top === left) return top;+ return top;+ })(),+ borderTop: cs.getPropertyValue("border-top-color"),+ borderTopWidth: toPx(cs.getPropertyValue("border-top-width")),+ borderRight: cs.getPropertyValue("border-right-color"),+ borderRightWidth: toPx(cs.getPropertyValue("border-right-width")),+ borderBottom: cs.getPropertyValue("border-bottom-color"),+ borderBottomWidth: toPx(cs.getPropertyValue("border-bottom-width")),+ borderLeft: cs.getPropertyValue("border-left-color"),+ borderLeftWidth: toPx(cs.getPropertyValue("border-left-width")),},typography: {fontStack,@@ -417,15 +478,24 @@ export const getBrandingScript = () => String.raw`weight: parseInt(cs.getPropertyValue("font-weight"), 10) || null,},radius: toPx(cs.getPropertyValue("border-radius")),+ borderRadius: {+ topLeft: toPx(cs.getPropertyValue("border-top-left-radius")),+ topRight: toPx(cs.getPropertyValue("border-top-right-radius")),+ bottomRight: toPx(cs.getPropertyValue("border-bottom-right-radius")),+ bottomLeft: toPx(cs.getPropertyValue("border-bottom-left-radius")),+ },shadow: cs.getPropertyValue("box-shadow") || null,isButton: isButton && !isNavigation,isNavigation: isNavigation,hasCTAIndicator: hasCTAIndicator,- isInput: el.matches('input,select,textarea,[class*="form-control"]'),+ isInput: isInputField,+ inputMetadata: inputMetadata,isLink: el.matches("a"),};};++const findImages = () => {const imgs = [];const logoCandidates = [];@@ -440,10 +510,9 @@ export const getBrandingScript = () => String.raw`"twitter",);- // Helper to collect logo candidate metadataconst collectLogoCandidate = (el, source) => {const rect = el.getBoundingClientRect();- const style = getComputedStyle(el);+ const style = getComputedStyleCached(el);const isVisible = (rect.width > 0 &&rect.height > 0 &&@@ -452,15 +521,101 @@ export const getBrandingScript = () => String.raw`style.opacity !== "0");+ const imgSrc = el.src || '';+ if (imgSrc) {+ const ogImageSrc = document.querySelector('meta[property="og:image"]')?.getAttribute('content') || '';+ const twitterImageSrc = document.querySelector('meta[name="twitter:image"]')?.getAttribute('content') || '';++ if ((ogImageSrc && imgSrc.includes(ogImageSrc)) ||+ (twitterImageSrc && imgSrc.includes(twitterImageSrc)) ||+ (ogImageSrc && ogImageSrc.includes(imgSrc)) ||+ (twitterImageSrc && twitterImageSrc.includes(imgSrc))) {+ return;+ }+ }+const inHeader = el.closest('header, nav, [role="banner"], #navbar, [id*="navbar"], [class*="navbar"], [class*="header"]');- // Check if logo is inside an anchor tag and get its href+ const langSwitcherParent = el.closest('[class*="lang"], [class*="language"], [class*="locale"], [class*="i18n"], [class*="translation"], [class*="switcher"], [id*="lang"], [id*="language"], [id*="locale"]');+ if (langSwitcherParent) {+ const parentClasses = getClassNameString(langSwitcherParent).toLowerCase();+ if (/lang|language|locale|i18n|translation|switcher|selector|dropdown/i.test(parentClasses)) {+ return;+ }+ }++ const insideButton = el.closest('button, [role="button"], input[type="button"], input[type="submit"]');+ if (insideButton) {+ return;+ }+++ const elementClasses = getClassNameString(el).toLowerCase();+ const elementId = (el.id || '').toLowerCase();+ const ariaLabel = (el.getAttribute?.('aria-label') || '').toLowerCase();++ const isSearchIcon =+ /search|magnif/i.test(elementClasses) ||+ /search|magnif/i.test(elementId) ||+ /search/i.test(ariaLabel) ||+ el.closest('[class*="search"], [id*="search"], [role="search"]');++ if (isSearchIcon) return;++ const isUIIcon =+ /icon|menu|hamburger|bars|close|times|cart|user|account|profile|settings|notification|bell|chevron|arrow|caret|dropdown/i.test(elementClasses) ||+ /icon|menu|hamburger|cart|user|bell/i.test(elementId) ||+ /menu|close|cart|user|settings/i.test(ariaLabel);++ if (isUIIcon) {+ const hasExplicitLogoIndicator =+ /logo|brand|site-name|site-title/i.test(elementClasses) ||+ /logo|brand/i.test(elementId);++ if (!hasExplicitLogoIndicator) return;+ }+const anchorParent = el.closest('a');const href = anchorParent ? (anchorParent.getAttribute('href') || '') : '';+ if (href && href.trim()) {+ const hrefLower = href.toLowerCase().trim();++ const isExternalLink =+ hrefLower.startsWith('http://') ||+ hrefLower.startsWith('https://') ||+ hrefLower.startsWith('//');++ if (isExternalLink) {+ const externalServiceDomains = [+ 'github.com', 'twitter.com', 'x.com', 'facebook.com', 'linkedin.com',+ 'instagram.com', 'youtube.com', 'discord.com', 'slack.com',+ 'npmjs.com', 'pypi.org', 'crates.io', 'packagist.org',+ 'badge.fury.io', 'shields.io', 'img.shields.io', 'badgen.net',+ 'codecov.io', 'coveralls.io', 'circleci.com', 'travis-ci.org',+ 'app.netlify.com', 'vercel.com'+ ];++ if (externalServiceDomains.some(domain => hrefLower.includes(domain))) {+ return;+ }++ try {+ const currentHostname = window.location.hostname.toLowerCase();+ const linkUrl = new URL(href, window.location.origin);+ const linkHostname = linkUrl.hostname.toLowerCase();++ if (linkHostname !== currentHostname) {+ return;+ }+ } catch (e) {+ return;+ }+ }+ }+const isSvg = el.tagName.toLowerCase() === "svg";- // For SVGs, check different propertieslet alt = "";let srcMatch = false;let altMatch = false;@@ -468,9 +623,8 @@ export const getBrandingScript = () => String.raw`let hrefMatch = false;if (isSvg) {- // SVGs don't have alt/src, check id, className, aria-label, and titleconst svgId = el.id || "";- const svgClass = el.className?.baseVal || el.className || "";+ const svgClass = getClassNameString(el);const svgAriaLabel = el.getAttribute("aria-label") || "";const svgTitle = el.querySelector("title")?.textContent || "";const svgText = el.textContent?.trim() || "";@@ -478,15 +632,17 @@ export const getBrandingScript = () => String.raw`alt = svgAriaLabel || svgTitle || svgText || svgId || "";altMatch = /logo/i.test(svgId) || /logo/i.test(svgAriaLabel) || /logo/i.test(svgTitle);classMatch = /logo/i.test(svgClass);- // For SVGs, we'll check if it's in a logo containersrcMatch = el.closest('[class*="logo"], [id*="logo"]') !== null;} else {- // For images+ const imgId = el.id || "";alt = el.alt || "";- srcMatch = el.src ? /logo/i.test(el.src) : false;++ const idMatch = /logo/i.test(imgId);+ srcMatch = (el.src ? /logo/i.test(el.src) : false) || idMatch;altMatch = /logo/i.test(alt);- const imgClass = el.className || "";- classMatch = /logo/i.test(imgClass);++ const imgClass = getClassNameString(el);+ classMatch = /logo/i.test(imgClass) || el.closest('[class*="logo"], [id*="logo"]') !== null || idMatch;}let src = "";@@ -497,12 +653,12 @@ export const getBrandingScript = () => String.raw`const serializer = new XMLSerializer();src = "data:image/svg+xml;utf8," + encodeURIComponent(serializer.serializeToString(resolvedSvg));} catch (e) {- // If serialization fails, try to serialize the original SVG+ recordError('resolveSvgStyles', e);try {const serializer = new XMLSerializer();src = "data:image/svg+xml;utf8," + encodeURIComponent(serializer.serializeToString(el));} catch (e2) {- // If that also fails, skip this candidate+ recordError('XMLSerializer fallback', e2);return;}}@@ -510,15 +666,25 @@ export const getBrandingScript = () => String.raw`src = el.src || "";}- // Check if href indicates homepage/root (common logo pattern)if (href) {const normalizedHref = href.toLowerCase().trim();- // Logos typically link to homepage: "/", "/home", "/index", or just "#" or empty+hrefMatch = normalizedHref === '/' ||normalizedHref === '/home' ||normalizedHref === '/index' ||- normalizedHref === '' ||- normalizedHref === '#';+ normalizedHref === '';++ if (!hrefMatch && (normalizedHref.startsWith('http://') || normalizedHref.startsWith('https://') || normalizedHref.startsWith('//'))) {+ try {+ const currentHostname = window.location.hostname.toLowerCase();+ const linkUrl = new URL(href, window.location.origin);+ const linkHostname = linkUrl.hostname.toLowerCase();++ if (linkHostname === currentHostname && (linkUrl.pathname === '/' || linkUrl.pathname === '/home' || linkUrl.pathname === '/index.html')) {+ hrefMatch = true;+ }+ } catch (e) {}+ }}if (src) {@@ -542,9 +708,6 @@ export const getBrandingScript = () => String.raw`}};- // Collect all potential logo candidates (including hidden ones for LLM to decide)- // More comprehensive selectors - include SVGs directly in header/nav, not just in anchors- // Also check for elements with class="header" (not just <header> tag)const allLogoSelectors = ['header a img, header a svg, header img, header svg','[class*="header"] a img, [class*="header"] a svg, [class*="header"] img, [class*="header"] svg',@@ -554,6 +717,8 @@ export const getBrandingScript = () => String.raw`'[id*="navbar"] a img, [id*="navbar"] a svg, [id*="navbar"] img, [id*="navbar"] svg','[class*="navbar"] a img, [class*="navbar"] a svg, [class*="navbar"] img, [class*="navbar"] svg','a[class*="logo"] img, a[class*="logo"] svg',+ '[class*="logo"] img, [class*="logo"] svg',+ '[id*="logo"] img, [id*="logo"] svg','img[class*="nav-logo"], svg[class*="nav-logo"]','img[class*="logo"], svg[class*="logo"]',];@@ -564,7 +729,6 @@ export const getBrandingScript = () => String.raw`});});- // Also collect from document.images and SVGsconst excludeSelectors = '[class*="testimonial"], [class*="client"], [class*="partner"], [class*="customer"], [class*="case-study"], [id*="testimonial"], [id*="client"], [id*="partner"], [id*="customer"], [id*="case-study"], footer, [class*="footer"]';Array.from(document.images).forEach(img => {@@ -573,21 +737,16 @@ export const getBrandingScript = () => String.raw`/logo/i.test(img.src) ||img.closest('[class*="logo"]')) {- // Exclude customer/partner logos more aggressivelyif (!img.closest(excludeSelectors)) {collectLogoCandidate(img, "document.images");}}});- // Collect SVGs from various sources - catch any SVGs in header/nav that weren't caught by selectors- // This is a fallback to ensure we catch all SVGs that the original logic would have foundArray.from(document.querySelectorAll("svg")).forEach(svg => {- // Skip if already collected by selectors above (check by position to avoid re-serialization)const svgRect = svg.getBoundingClientRect();const alreadyCollected = logoCandidates.some(c => {if (!c.isSvg) return false;- // Check if position matches (same SVG element)return Math.abs(c.position.top - svgRect.top) < 1 &&Math.abs(c.position.left - svgRect.left) < 1 &&Math.abs(c.position.width - svgRect.width) < 1 &&@@ -595,23 +754,46 @@ export const getBrandingScript = () => String.raw`});if (alreadyCollected) return;- // Check if SVG matches logo criteria - VERY permissive (like old code)- const hasLogoId = /logo/i.test(svg.id || "");- const svgClass = svg.className?.baseVal || svg.className || "";+ const insideButton = svg.closest('button, [role="button"], input[type="button"], input[type="submit"]');+ if (insideButton) return;++ // Check for UI icon indicators+ const svgId = svg.id || "";+ const svgClass = getClassNameString(svg);+ const svgAriaLabel = svg.getAttribute("aria-label") || "";+ const svgTitle = svg.querySelector("title")?.textContent || "";++ // Skip search icons+ const isSearchIcon =+ /search|magnif/i.test(svgId) ||+ /search|magnif/i.test(svgClass) ||+ /search/i.test(svgAriaLabel) ||+ /search/i.test(svgTitle) ||+ svg.closest('[class*="search"], [id*="search"], [role="search"]');++ if (isSearchIcon) return;++ // Skip other UI icons+ const isUIIcon =+ /icon|menu|hamburger|bars|close|times|cart|user|account|profile|settings|notification|bell|chevron|arrow|caret|dropdown/i.test(svgClass) ||+ /icon|menu|hamburger|cart|user|bell/i.test(svgId) ||+ /menu|close|cart|user|settings/i.test(svgAriaLabel);++ const hasLogoId = /logo/i.test(svgId);const hasLogoClass = /logo/i.test(svgClass);- const hasLogoAriaLabel = /logo/i.test(svg.getAttribute("aria-label") || "");- const hasLogoTitle = /logo/i.test(svg.querySelector("title")?.textContent || "");+ const hasLogoAriaLabel = /logo/i.test(svgAriaLabel);+ const hasLogoTitle = /logo/i.test(svgTitle);const inHeaderNav = svg.closest('header, nav, [role="banner"], #navbar, [id*="navbar"], [class*="navbar"], [class*="header"]');const inLogoContainer = svg.closest('[class*="logo"], [id*="logo"]');const inHeaderNavArea = !!inHeaderNav;const inAnchorInHeader = svg.closest('a') && inHeaderNav;- // VERY PERMISSIVE: Collect if:- // 1. Has logo indicators (id, class, aria-label, title)- // 2. Is in logo container- // 3. Is in header/nav (most common case - no size constraint, like old code)- // 4. Is in anchor in header/nav- // This matches the original logic which was very permissive+ // If it looks like a UI icon, only collect if it has explicit logo indicators+ if (isUIIcon) {+ const hasExplicitLogoIndicator = hasLogoId || hasLogoClass || hasLogoAriaLabel || hasLogoTitle || inLogoContainer;+ if (!hasExplicitLogoIndicator) return;+ }+const shouldCollect =hasLogoId ||hasLogoClass ||@@ -622,7 +804,6 @@ export const getBrandingScript = () => String.raw`inAnchorInHeader;if (shouldCollect) {- // Exclude customer/partner logos more aggressivelyconst excludeSelectors = '[class*="testimonial"], [class*="client"], [class*="partner"], [class*="customer"], [class*="case-study"], [id*="testimonial"], [id*="client"], [id*="partner"], [id*="customer"], [id*="case-study"], footer, [class*="footer"]';if (!svg.closest(excludeSelectors)) {collectLogoCandidate(svg, "document.querySelectorAll(svg)");@@ -630,7 +811,6 @@ export const getBrandingScript = () => String.raw`}});- // Remove duplicates (same src)const seen = new Set();const uniqueCandidates = logoCandidates.filter(candidate => {if (seen.has(candidate.src)) return false;@@ -638,19 +818,32 @@ export const getBrandingScript = () => String.raw`return true;});- // For backward compatibility, still pick one logo using the old logic- // Try visible candidates first, but fall back to any candidate if none visiblelet candidatesToPick = uniqueCandidates.filter(c => c.isVisible);if (candidatesToPick.length === 0 && uniqueCandidates.length > 0) {- // If no visible candidates, use all candidates (maybe hidden for dark/light mode)candidatesToPick = uniqueCandidates;}if (candidatesToPick.length > 0) {const best = candidatesToPick.reduce((best, candidate) => {if (!best) return candidate;+if (candidate.indicators.inHeader && !best.indicators.inHeader) return candidate;if (!candidate.indicators.inHeader && best.indicators.inHeader) return best;++ if (candidate.indicators.hrefMatch && !best.indicators.hrefMatch) return candidate;+ if (!candidate.indicators.hrefMatch && best.indicators.hrefMatch) return best;++ if (candidate.indicators.classMatch && !best.indicators.classMatch) return candidate;+ if (!candidate.indicators.classMatch && best.indicators.classMatch) return best;++ const candidateArea = candidate.position.width * candidate.position.height;+ const bestArea = best.position.width * best.position.height;+ const candidateTooSmall = candidate.position.width < CONSTANTS.MIN_LOGO_SIZE || candidate.position.height < CONSTANTS.MIN_LOGO_SIZE;+ const bestTooSmall = best.position.width < CONSTANTS.MIN_LOGO_SIZE || best.position.height < CONSTANTS.MIN_LOGO_SIZE;++ if (candidateTooSmall && !bestTooSmall) return best;+ if (!candidateTooSmall && bestTooSmall) return candidate;+return candidate.position.top < best.position.top ? candidate : best;}, null);@@ -669,7 +862,7 @@ export const getBrandingScript = () => String.raw`const getTypography = () => {const pickFontStack = el => {return (- getComputedStyle(el)+ getComputedStyleCached(el).fontFamily?.split(",").map(f => f.replace(/["']/g, "").trim()).filter(Boolean) || []@@ -688,9 +881,9 @@ export const getBrandingScript = () => String.raw`paragraph: pickFontStack(p),},sizes: {- h1: getComputedStyle(h1).fontSize || "32px",- h2: getComputedStyle(h2).fontSize || "24px",- body: getComputedStyle(p).fontSize || "16px",+ h1: getComputedStyleCached(h1).fontSize || "32px",+ h2: getComputedStyleCached(h2).fontSize || "24px",+ body: getComputedStyleCached(p).fontSize || "16px",},};};@@ -724,7 +917,6 @@ export const getBrandingScript = () => String.raw`const body = document.body;const html = document.documentElement;- // Check for explicit dark mode indicatorsconst hasDarkIndicator =html.classList.contains("dark") ||body.classList.contains("dark") ||@@ -734,7 +926,6 @@ export const getBrandingScript = () => String.raw`body.getAttribute("data-theme") === "dark" ||html.getAttribute("data-bs-theme") === "dark";- // Check for explicit light mode indicatorsconst hasLightIndicator =html.classList.contains("light") ||body.classList.contains("light") ||@@ -744,22 +935,19 @@ export const getBrandingScript = () => String.raw`body.getAttribute("data-theme") === "light" ||html.getAttribute("data-bs-theme") === "light";- // Check prefers-color-scheme media querylet prefersDark = false;try {prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;} catch (e) {}- // If explicit indicators exist, use them (explicit overrides media query)if (hasDarkIndicator) return "dark";if (hasLightIndicator) return "light";- // Analyze background colors from body/html and walk up the DOM if transparentconst getEffectiveBackground = (el) => {let current = el;let depth = 0;while (current && depth < 10) {- const bg = getComputedStyle(current).backgroundColor;+ const bg = getComputedStyleCached(current).backgroundColor;const match = bg.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);if (match) {const r = parseInt(match[1], 10);@@ -767,8 +955,7 @@ export const getBrandingScript = () => String.raw`const b = parseInt(match[3], 10);const alpha = match[4] ? parseFloat(match[4]) : 1;- // Only consider if not fully transparent- if (alpha > 0.1) {+ if (alpha > CONSTANTS.MIN_ALPHA_THRESHOLD) {return { r, g, b, alpha };}}@@ -786,106 +973,115 @@ export const getBrandingScript = () => String.raw`const { r, g, b } = effectiveBg;const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;- // Use luminance threshold: < 0.4 = dark, > 0.6 = light, 0.4-0.6 = use media queryif (luminance < 0.4) return "dark";if (luminance > 0.6) return "light";- // Ambiguous luminance: fall back to prefers-color-schemereturn prefersDark ? "dark" : "light";}- // No background color found: use prefers-color-scheme, default to lightreturn prefersDark ? "dark" : "light";};const extractBrandName = () => {- // Try multiple sources for brand nameconst ogSiteName = document.querySelector('meta[property="og:site_name"]')?.getAttribute("content");const title = document.title;const h1 = document.querySelector("h1")?.textContent?.trim();- // Extract domain name as fallbacklet domainName = "";try {const hostname = window.location.hostname;domainName = hostname.replace(/^www\./, "").split(".")[0];- // Capitalize first letterdomainName = domainName.charAt(0).toUpperCase() + domainName.slice(1);} catch (e) {}- // Try to extract brand from title (e.g., "Firecrawl - Documentation" -> "Firecrawl")let titleBrand = "";if (title) {- // Remove common suffixestitleBrand = title- .replace(/\s*[-|β|β]\s*.*$/, "") // Remove after dash- .replace(/\s*:\s*.*$/, "") // Remove after colon- .replace(/\s*\|.*$/, "") // Remove after pipe+ .replace(/\s*[-|β|β]\s*.*$/, "")+ .replace(/\s*:\s*.*$/, "")+ .replace(/\s*\|.*$/, "").trim();}return ogSiteName || titleBrand || h1 || domainName || "";};- // Helper to check if a color is valid (not transparent)+ const normalizeColor = (color) => {+ if (!color || typeof color !== "string") return null;+ const normalized = color.toLowerCase().trim();++ if (normalized === "transparent" || normalized === "rgba(0, 0, 0, 0)") {+ return null;+ }++ if (normalized === "#ffffff" || normalized === "#fff" ||+ normalized === "white" || normalized === "rgb(255, 255, 255)" ||+ /^rgba\(255,\s*255,\s*255(,\s*1(\.0)?)?\)$/.test(normalized)) {+ return "rgb(255, 255, 255)";+ }++ if (normalized === "#000000" || normalized === "#000" ||+ normalized === "black" || normalized === "rgb(0, 0, 0)" ||+ /^rgba\(0,\s*0,\s*0(,\s*1(\.0)?)?\)$/.test(normalized)) {+ return "rgb(0, 0, 0)";+ }++ if (normalized.startsWith("#")) {+ return normalized;+ }++ if (normalized.startsWith("rgb")) {+ return normalized.replace(/\s+/g, "");+ }++ return normalized;+ };+const isValidBackgroundColor = (color) => {if (!color || typeof color !== "string") return false;const normalized = color.toLowerCase().trim();- // Explicitly transparentif (normalized === "transparent" || normalized === "rgba(0, 0, 0, 0)") {return false;}- // Check for rgba with alpha exactly 0 (not just starting with 0)const rgbaMatch = normalized.match(/rgba\(\s*0\s*,\s*0\s*,\s*0\s*,\s*([\d.]+)\s*\)/);if (rgbaMatch) {const alpha = parseFloat(rgbaMatch[1]);- // Only filter if alpha is exactly 0 (or very close to 0 due to floating point)- if (alpha < 0.01) {+ if (alpha < CONSTANTS.MAX_TRANSPARENT_ALPHA) {return false;}- // rgba(0, 0, 0, 0.8) and similar opaque black backgrounds are validreturn true;}- // rgb(0, 0, 0) is a valid black background, not transparent- // Check for color() format with alpha 0const colorMatch = normalized.match(/color\([^)]+\)/);if (colorMatch) {- // If it's a color() format, include it (let the processor handle it)- // Modern formats like color(display-p3 0 0 0 / 0.039216) are validreturn true;}- // Any other non-empty string is validreturn normalized.length > 0;};const getBackgroundCandidates = () => {const candidates = [];- // First, sample actual visible background colors from elements to find the most commonconst colorFrequency = new Map();- const sampleElements = document.querySelectorAll("body, html, main, article, [role='main'], div, section");+ const allSampleElements = document.querySelectorAll("body, html, main, article, [role='main'], div, section");+ const sampleElements = Array.from(allSampleElements).slice(0, CONSTANTS.MAX_BACKGROUND_SAMPLES);- sampleElements.forEach((el, idx) => {- if (idx < 100) { // Limit to first 100 elements- try {- const bg = getComputedStyle(el).backgroundColor;- if (isValidBackgroundColor(bg)) {- const rect = el.getBoundingClientRect();- const area = rect.width * rect.height;- // Only count if element has significant area- if (area > 1000) {- const normalized = bg.toLowerCase().trim();+ sampleElements.forEach(el => {+ try {+ const bg = getComputedStyleCached(el).backgroundColor;+ if (isValidBackgroundColor(bg)) {+ const rect = el.getBoundingClientRect();+ const area = rect.width * rect.height;+ if (area > CONSTANTS.MIN_SIGNIFICANT_AREA) {+ const normalized = normalizeColor(bg);+ if (normalized) {const currentCount = colorFrequency.get(normalized) || 0;colorFrequency.set(normalized, currentCount + area);}}- } catch (e) {- // Skip errors}- }+ } catch (e) {}});- // Find the most common background color by total arealet mostCommonColor = null;let maxArea = 0;for (const [color, area] of colorFrequency.entries()) {@@ -895,49 +1091,46 @@ export const getBrandingScript = () => String.raw`}}- // Sample body and html background colors directly- const bodyBg = getComputedStyle(document.body).backgroundColor;- const htmlBg = getComputedStyle(document.documentElement).backgroundColor;+ const bodyBg = getComputedStyleCached(document.body).backgroundColor;+ const htmlBg = getComputedStyleCached(document.documentElement).backgroundColor;- // Prefer body/html if they're valid, but give extra boost if they match the most common colorif (isValidBackgroundColor(bodyBg)) {- const normalized = bodyBg.toLowerCase().trim();+ const normalized = normalizeColor(bodyBg);const priority = normalized === mostCommonColor ? 15 : 10;- candidates.push({- color: bodyBg,- source: "body",- priority: priority,- });+ if (normalized) {+ candidates.push({+ color: normalized,+ source: "body",+ priority: priority,+ });+ }}if (isValidBackgroundColor(htmlBg)) {- const normalized = htmlBg.toLowerCase().trim();+ const normalized = normalizeColor(htmlBg);const priority = normalized === mostCommonColor ? 14 : 9;- candidates.push({- color: htmlBg,- source: "html",- priority: priority,- });+ if (normalized) {+ candidates.push({+ color: normalized,+ source: "html",+ priority: priority,+ });+ }}- // Add the most common color as a candidate if it's different from body/html- if (mostCommonColor && mostCommonColor !== bodyBg?.toLowerCase().trim() && mostCommonColor !== htmlBg?.toLowerCase().trim()) {+ const normalizedBodyBg = normalizeColor(bodyBg);+ const normalizedHtmlBg = normalizeColor(htmlBg);+ if (mostCommonColor && mostCommonColor !== normalizedBodyBg && mostCommonColor !== normalizedHtmlBg) {candidates.push({color: mostCommonColor,source: "most-common-visible",- priority: 12, // High priority but below body/html+ priority: 12,area: maxArea,});}- // Try to get CSS custom properties (common in Tailwind/modern frameworks)- // Create a temporary element to resolve CSS variables- let tempEl = null;try {- tempEl = document.createElement("div");- tempEl.style.setProperty("background-color", "var(--background)");- document.body.appendChild(tempEl);- const tempStyle = getComputedStyle(tempEl);+ const rootStyle = getComputedStyleCached(document.documentElement);const cssVars = ["--background",@@ -953,106 +1146,53 @@ export const getBrandingScript = () => String.raw`cssVars.forEach(varName => {try {- // Try to resolve the CSS variable- tempEl.style.setProperty("background-color", "var(" + varName + ")");- const resolved = tempStyle.backgroundColor;+ const rawValue = rootStyle.getPropertyValue(varName).trim();- if (isValidBackgroundColor(resolved)) {+ if (rawValue && isValidBackgroundColor(rawValue)) {candidates.push({- color: resolved,+ color: rawValue,source: "css-var:" + varName,priority: 8,});}- } catch (e) {- // Skip this variable if there's an error- }+ } catch (e) {}});-- if (tempEl && tempEl.parentElement) {- document.body.removeChild(tempEl);- }- } catch (e) {- // If temp element creation fails, continue without CSS variables- if (tempEl && tempEl.parentElement) {- try {- document.body.removeChild(tempEl);- } catch (e2) {- // Ignore cleanup errors- }- }- }+ } catch (e) {}- // Sample from main containers (header, main, article, etc.)try {- const mainContainers = document.querySelectorAll("main, article, [role='main'], header, .main, .container");- mainContainers.forEach((el, idx) => {- if (idx < 5) { // Limit to first 5- try {- const bg = getComputedStyle(el).backgroundColor;- if (isValidBackgroundColor(bg)) {- const rect = el.getBoundingClientRect();- const area = rect.width * rect.height;- // Only include if it's a significant area- if (area > 10000) {+ const allContainers = document.querySelectorAll("main, article, [role='main'], header, .main, .container");+ const mainContainers = Array.from(allContainers).slice(0, 5);+ mainContainers.forEach(el => {+ try {+ const bg = getComputedStyleCached(el).backgroundColor;+ if (isValidBackgroundColor(bg)) {+ const rect = el.getBoundingClientRect();+ const area = rect.width * rect.height;+ if (area > CONSTANTS.MIN_LARGE_CONTAINER_AREA) {+ const normalized = normalizeColor(bg);+ if (normalized) {candidates.push({- color: bg,+ color: normalized,source: el.tagName.toLowerCase() + "-container",priority: 5,area: area,});}}- } catch (e) {- // Skip this element if there's an error}- }+ } catch (e) {}});- } catch (e) {- // If container selection fails, continue- }-- // Helper to normalize white color variants- const normalizeWhite = (color) => {- if (!color) return null;- const normalized = color.toLowerCase().trim();- // Check for various white formats- if (normalized === "#ffffff" || normalized === "#fff" ||- normalized === "rgb(255, 255, 255)" || normalized === "rgba(255, 255, 255, 1)" ||- normalized === "rgba(255, 255, 255, 1.0)" || normalized.startsWith("rgba(255, 255, 255")) {- return "rgb(255, 255, 255)";- }- return normalized;- };-- // Normalize white variants and boost priority for white backgrounds- const normalizedCandidates = candidates.map(c => {- const normalized = normalizeWhite(c.color);- if (normalized === "rgb(255, 255, 255)") {- // Boost white backgrounds slightly- return {- ...c,- color: normalized,- priority: (c.priority || 0) + 1,- };- }- return {- ...c,- color: normalized || c.color,- };- });+ } catch (e) {}- // Remove duplicates (same color value)const seen = new Set();- const unique = normalizedCandidates.filter(c => {+ const unique = candidates.filter(c => {if (!c || !c.color) return false;- const key = c.color.toLowerCase().trim();+ const key = normalizeColor(c.color);if (!key || seen.has(key)) return false;seen.add(key);return true;});- // Sort by priority (highest first)unique.sort((a, b) => (b.priority || 0) - (a.priority || 0));return unique;@@ -1068,7 +1208,6 @@ export const getBrandingScript = () => String.raw`const brandName = extractBrandName();const backgroundCandidates = getBackgroundCandidates();- // Keep pageBackground for backward compatibility (first candidate)const pageBackground = backgroundCandidates.length > 0 ? backgroundCandidates[0].color : null;return {@@ -1083,6 +1222,7 @@ export const getBrandingScript = () => String.raw`colorScheme,pageBackground,backgroundCandidates,+ errors: errors.length > 0 ? errors : undefined,},};})();`;diff --git a/apps/api/src/scraper/scrapeURL/index.ts b/apps/api/src/scraper/scrapeURL/index.tsindex 0ae5b054d..d03b63d33 100644--- a/apps/api/src/scraper/scrapeURL/index.ts+++ b/apps/api/src/scraper/scrapeURL/index.ts@@ -347,12 +347,24 @@ async function scrapeURLLoopIter(engine,);+ const hasMarkdown = hasFormatOfType(meta.options.formats, "markdown");+ const hasChangeTracking = hasFormatOfType(+ meta.options.formats,+ "changeTracking",+ );+ const hasJson = hasFormatOfType(meta.options.formats, "json");+ const hasSummary = hasFormatOfType(meta.options.formats, "summary");+ const needsMarkdown =+ hasMarkdown || hasChangeTracking || hasJson || hasSummary;+let checkMarkdown: string;if (meta.internalOptions.teamId === "sitemap" ||meta.internalOptions.teamId === "robots-txt") {checkMarkdown = engineResult.html?.trim() ?? "";+ } else if (!needsMarkdown) {+ checkMarkdown = engineResult.html?.trim() ?? "";} else {checkMarkdown = await parseMarkdown(await htmlTransform(diff --git a/apps/api/src/types/branding.ts b/apps/api/src/types/branding.tsindex 001c34069..9161ed5ad 100644--- a/apps/api/src/types/branding.ts+++ b/apps/api/src/types/branding.ts@@ -67,20 +67,43 @@ export interface BrandingProfile {textColor?: string;borderColor?: string;borderRadius?: string;- [key: string]: string | undefined;+ borderRadiusCorners?: {+ topLeft?: string;+ topRight?: string;+ bottomRight?: string;+ bottomLeft?: string;+ };+ shadow?: string;+ [key: string]: string | object | undefined;};buttonSecondary?: {background?: string;textColor?: string;borderColor?: string;borderRadius?: string;- [key: string]: string | undefined;+ borderRadiusCorners?: {+ topLeft?: string;+ topRight?: string;+ bottomRight?: string;+ bottomLeft?: string;+ };+ shadow?: string;+ [key: string]: string | object | undefined;};input?: {+ background?: string;+ textColor?: string;borderColor?: string;focusBorderColor?: string;borderRadius?: string;- [key: string]: string | undefined;+ borderRadiusCorners?: {+ topLeft?: string;+ topRight?: string;+ bottomRight?: string;+ bottomLeft?: string;+ };+ shadow?: string;+ [key: string]: string | object | undefined;};[key: string]: unknown;};