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
feat(branding): give Jev the logo's own colors (#5101)
Abimael Martell1b690470a5f6
diff --git a/apps/api/src/__tests__/lib/branding/branding-script-logo-colors.test.ts b/apps/api/src/__tests__/lib/branding/branding-script-logo-colors.test.tsnew file mode 100644index 000000000..54f6d8ee2--- /dev/null+++ b/apps/api/src/__tests__/lib/branding/branding-script-logo-colors.test.ts@@ -0,0 +1,94 @@+import { describe, it, expect } from "vitest";+import { JSDOM } from "jsdom";++import { getBrandingScript } from "../../../scraper/scrapeURL/engines/fire-engine/brandingScript";++// jsdom has no layout: elements get a box from data-top / data-w / data-h.+function logoCandidates(body: string) {+ const dom = new JSDOM(+ `<html><head><title>Acme</title></head><body>${body}</body></html>`,+ { runScripts: "outside-only", url: "https://acme.test/" },+ );+ const window = dom.window as any;+ window.Element.prototype.getBoundingClientRect = function () {+ const top = Number(this.getAttribute?.("data-top") ?? NaN);+ if (Number.isNaN(top)) {+ return {+ x: 0,+ y: 0,+ top: 0,+ left: 0,+ width: 0,+ height: 0,+ right: 0,+ bottom: 0,+ };+ }+ const width = Number(this.getAttribute("data-w") ?? 120);+ const height = Number(this.getAttribute("data-h") ?? 32);+ return {+ x: 0,+ y: top,+ top,+ left: 0,+ width,+ height,+ right: width,+ bottom: top + height,+ };+ };+ const result = window.eval(getBrandingScript());+ return result.branding.logoCandidates as Array<{+ alt: string;+ colors?: Array<{ color: string; share: number }>;+ }>;+}++describe("branding script logo colors", () => {+ it("reads an inline SVG logo's colors, including ones it inherits", () => {+ const candidates = logoCandidates(+ `<header data-top="0" data-w="1200" data-h="64">+ <a href="/" data-top="16" aria-label="Acme home">+ <svg data-top="16" data-w="120" data-h="32" viewBox="0 0 120 32" style="color: rgb(255, 56, 92); fill: currentColor" aria-label="Acme logo">+ <path d="M0 0h80v32H0z"></path>+ <path d="M90 0h30v32H90z" style="fill: rgb(34, 34, 34)"></path>+ </svg>+ </a>+ </header>`,+ );++ const logo = candidates.find(c => c.colors);+ expect(logo?.colors?.map(c => c.color)).toEqual(+ expect.arrayContaining(["#FF385C", "#222222"]),+ );+ });++ it("counts only painted shapes: not definitions, clip paths or invisible strokes", () => {+ const candidates = logoCandidates(+ `<header data-top="0" data-w="1200" data-h="64">+ <a href="/" data-top="16" aria-label="Acme home">+ <svg data-top="16" data-w="120" data-h="32" viewBox="0 0 120 32" aria-label="Acme logo">+ <defs><path id="unused" d="M0 0h10v10H0z" style="fill: rgb(255, 0, 0)"></path></defs>+ <clipPath id="clip"><rect width="120" height="32" style="fill: rgb(0, 255, 0)"></rect></clipPath>+ <path d="M0 0h120v32H0z" style="fill: rgb(14, 126, 78); stroke: rgb(0, 0, 255); stroke-width: 2; stroke-opacity: 0"></path>+ </svg>+ </a>+ </header>`,+ );++ const logo = candidates.find(c => c.colors);+ expect(logo?.colors).toEqual([{ color: "#0E7E4E", share: 1 }]);+ });++ it("leaves colors out when they can't be read", () => {+ // jsdom can't paint images, like a cross-origin logo in a browser.+ const candidates = logoCandidates(+ `<header data-top="0" data-w="1200" data-h="64">+ <a href="/" data-top="16"><img src="https://cdn.other.test/logo.png" alt="Acme logo" data-top="16"></a>+ </header>`,+ );++ expect(candidates.length).toBeGreaterThan(0);+ expect(candidates.every(c => c.colors === undefined)).toBe(true);+ });+});diff --git a/apps/api/src/__tests__/lib/branding/jev.test.ts b/apps/api/src/__tests__/lib/branding/jev.test.tsindex 356a2dead..9ea17cbe2 100644--- a/apps/api/src/__tests__/lib/branding/jev.test.ts+++ b/apps/api/src/__tests__/lib/branding/jev.test.ts@@ -812,6 +812,39 @@ describe("Jev request building", () => {]);});+ it("offers the likeliest logo's colors, labelled as the logo's", () => {+ const input = baseInput(new CostTracking());+ input.logoCandidates = [+ { ...MENU_ICON, colors: [{ color: "#FF0000", share: 1 }] },+ {+ ...LOGO,+ colors: [+ { color: "#0E7E4E", share: 0.7 },+ { color: "#6D28D9", share: 0.3 },+ ],+ },+ ];+ input.heuristicLogoPick = {+ selectedIndexInFilteredList: 1,+ confidence: 0.9,+ reasoning: "header home link",+ };++ const request = buildJevRequest(input);+ const usage = (hex: string) =>+ request.colors.find(c => c.hex === hex)?.usage ?? [];++ expect(usage("#0E7E4E")).toEqual(["color of the logo (70% of it)"]);+ // A color in both the logo and a button carries both.+ expect(usage("#6D28D9")).toEqual(+ expect.arrayContaining([+ "color of the logo (30% of it)",+ 'background of button "Get started"',+ ]),+ );+ expect(request.colors.map(c => c.hex)).not.toContain("#FF0000");+ });+it("sends only well-formed Unicode text", () => {// "π" (U+1D401) is a surrogate pair. The branding script truncates button// text by code unit, which can leave its first half alone at the end.diff --git a/apps/api/src/lib/branding/jev.ts b/apps/api/src/lib/branding/jev.tsindex 6d9bc4f87..27c77511e 100644--- a/apps/api/src/lib/branding/jev.ts+++ b/apps/api/src/lib/branding/jev.ts@@ -174,6 +174,14 @@ function collectColors(input: BrandingLLMInput): ColorCandidate[] {for (const [role, value] of Object.entries(input.jsAnalysis.colors ?? {})) {add(value, `heuristic guess for ${role}`);}+ // The likeliest logo's own colors: the brand color is usually among them.+ const logos = input.logoCandidates ?? [];+ const logo =+ logos[input.heuristicLogoPick?.selectedIndexInFilteredList ?? -1] ??+ logos[0];+ for (const { color, share } of logo?.colors ?? []) {+ add(color, `color of the logo (${Math.round(share * 100)}% of it)`);+ }for (const candidate of input.backgroundCandidates ?? []) {add(candidate.color, `page background candidate (${candidate.source})`);}diff --git a/apps/api/src/lib/branding/logo-selector.ts b/apps/api/src/lib/branding/logo-selector.tsindex cae15f7d7..286d1a450 100644--- a/apps/api/src/lib/branding/logo-selector.ts+++ b/apps/api/src/lib/branding/logo-selector.ts@@ -17,6 +17,7 @@ export interface LogoCandidate {};href?: string;source: string;+ colors?: Array<{ color: string; share: number }>;}interface LogoSelectionResult {diff --git a/apps/api/src/lib/branding/types.ts b/apps/api/src/lib/branding/types.tsindex e91a06e1f..8454c6bcf 100644--- a/apps/api/src/lib/branding/types.ts+++ b/apps/api/src/lib/branding/types.ts@@ -68,6 +68,8 @@ export interface BrandingLLMInput {href?: string;source: string;logoSvgScore?: number;+ /** The colors the logo is drawn in, most used first, when readable. */+ colors?: Array<{ color: string; share: number }>;}>;brandName?: string;/** Full page title (e.g. "AI Innovation Workspace | Miro") β LLM infers brand from this. */@@ -184,6 +186,8 @@ export interface BrandingScriptReturn {href?: string;source: string;logoSvgScore?: number;+ /** The colors the logo is drawn in, most used first, when readable. */+ colors?: Array<{ color: string; share: number }>;}>;brandName?: string;/** Full page title (e.g. "AI Innovation Workspace | Miro") β LLM can infer brand from this. */diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/images.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/images.tsindex 32e52bdd1..8b324b215 100644--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/images.ts+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/images.ts@@ -4,6 +4,7 @@ import {getComputedStyleCached,recordError,} from "./helpers";+import { getLogoColors, LogoColor } from "./logo-colors";import { resolveSvgStyles } from "./svg-utils";import {findDeclaredJsonLdLogo,@@ -29,8 +30,12 @@ interface LogoCandidate {href?: string;source: string;logoSvgScore: number;+ /** The colors the logo is drawn in, most used first, when readable. */+ colors?: LogoColor[];}+const LOGO_COLOR_CANDIDATES = 10;+interface ImageData {type: string;src: string;@@ -44,6 +49,7 @@ export interface FindImagesResult {export const findImages = (): FindImagesResult => {const imgs: ImageData[] = [];const logoCandidates: LogoCandidate[] = [];+ const candidateElements = new Map<LogoCandidate, Element>();const push = (src: string | undefined | null, type: string): void => {if (src) imgs.push({ type, src });@@ -928,6 +934,7 @@ export const findImages = (): FindImagesResult => {};logoCandidates.push(logoCandidate);+ candidateElements.set(logoCandidate, el);}};@@ -1397,5 +1404,25 @@ export const findImages = (): FindImagesResult => {}}+ // Only the likeliest candidates (roughly the server's ranking in+ // logo-selector): reading colors means painting each one.+ const likelihood = (c: LogoCandidate): number =>+ (c.indicators.hrefMatch ? 40 : 0) ++ (c.indicators.inHeader ? 30 : 0) ++ (c.location === "header" ? 20 : 0) ++ (c.isVisible ? 15 : 0) ++ (c.indicators.classMatch ? 10 : 0) ++ (c.indicators.srcMatch ? 10 : 0) ++ (c.indicators.altMatch ? 5 : 0) ++ (c.source === "document.images" ? 15 : 0);+ [...uniqueCandidates]+ .sort((a, b) => likelihood(b) - likelihood(a))+ .slice(0, LOGO_COLOR_CANDIDATES)+ .forEach(candidate => {+ const el = candidateElements.get(candidate);+ const colors = el ? getLogoColors(el) : [];+ if (colors.length > 0) candidate.colors = colors;+ });+return { images: imgs, logoCandidates: uniqueCandidates };};diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/logo-colors.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/logo-colors.tsnew file mode 100644index 000000000..a08d378ec--- /dev/null+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/logo-colors.ts@@ -0,0 +1,186 @@+import { getComputedStyleCached, recordError } from "./helpers";++export interface LogoColor {+ /** #RRGGBB */+ color: string;+ /** Share of the logo's painted area, 0β1. */+ share: number;+}++const MAX_COLORS = 4;+const MIN_SHARE = 0.03;+const SAMPLE_PX = 64;++const toHex = (r: number, g: number, b: number): string =>+ "#" ++ [r, g, b]+ .map(v => Math.round(v).toString(16).padStart(2, "0"))+ .join("")+ .toUpperCase();++/** rgb()/rgba() as the browser computes paint, or null if not a solid color. */+const parsePaint = (paint: string): [number, number, number] | null => {+ const m = paint.match(/^rgba?\(([^)]+)\)$/);+ if (!m) return null;+ const parts = m[1]+ .split(/[\s,/]+/)+ .filter(Boolean)+ .map(parseFloat);+ if (parts.length < 3 || parts.slice(0, 3).some(Number.isNaN)) return null;+ if (parts.length >= 4 && parts[3] < 0.5) return null;+ return [parts[0], parts[1], parts[2]];+};++const rgbOf = (hex: string): [number, number, number] => [+ parseInt(hex.slice(1, 3), 16),+ parseInt(hex.slice(3, 5), 16),+ parseInt(hex.slice(5, 7), 16),+];++// Shades closer than this count as one color: anti-aliased edges and+// gradients split a logo's color into many near-identical ones.+const SAME_COLOR = 40;++const ranked = (weights: Map<string, number>): LogoColor[] => {+ const total = [...weights.values()].reduce((s, w) => s + w, 0);+ if (total <= 0) return [];+ const merged: Array<{ color: string; rgb: number[]; weight: number }> = [];+ [...weights.entries()]+ .sort((a, b) => b[1] - a[1])+ .forEach(([color, weight]) => {+ const rgb = rgbOf(color);+ const same = merged.find(+ m =>+ Math.hypot(m.rgb[0] - rgb[0], m.rgb[1] - rgb[1], m.rgb[2] - rgb[2]) <+ SAME_COLOR,+ );+ if (same) same.weight += weight;+ else merged.push({ color, rgb, weight });+ });+ return merged+ .map(({ color, weight }) => ({+ color,+ share: Math.round((weight / total) * 100) / 100,+ }))+ .filter(c => c.share >= MIN_SHARE)+ .sort((a, b) => b.share - a.share)+ .slice(0, MAX_COLORS);+};++// Definitions, clip paths and masks shape the logo but aren't painted. Tag+// names compared in lowercase: selectors match camel-case SVG names unevenly.+const UNPAINTED = new Set([+ "defs",+ "clippath",+ "mask",+ "symbol",+ "pattern",+ "marker",+]);++const isUnpainted = (shape: Element, svg: Element): boolean => {+ for (let n = shape.parentElement; n && n !== svg; n = n.parentElement) {+ if (UNPAINTED.has(n.localName.toLowerCase())) return true;+ }+ return false;+};++/** Fill and stroke colors of an inline SVG's shapes, weighted by their area. */+const svgColors = (svg: SVGSVGElement): LogoColor[] => {+ const weights = new Map<string, number>();+ const shapes = svg.querySelectorAll(+ "path, rect, circle, ellipse, polygon, polyline, line, text, use",+ );+ shapes.forEach(shape => {+ if (isUnpainted(shape, svg)) return;+ const cs = getComputedStyleCached(shape);+ if (cs.display === "none" || cs.visibility === "hidden") return;+ if (parseFloat(cs.opacity) < 0.5) return;+ let area = 1;+ try {+ const box = (shape as SVGGraphicsElement).getBBox();+ area = Math.max(1, box.width * box.height);+ } catch {+ // Not rendered: count it once.+ }+ const add = (paint: string, weight: number) => {+ const rgb = parsePaint(paint);+ if (!rgb) return;+ const hex = toHex(...rgb);+ weights.set(hex, (weights.get(hex) ?? 0) + weight);+ };+ if (parseFloat(cs.fillOpacity) >= 0.5) add(cs.fill, area);+ if (parseFloat(cs.strokeWidth) > 0 && parseFloat(cs.strokeOpacity) >= 0.5)+ add(cs.stroke, area * 0.1);+ });+ return ranked(weights);+};++/**+ * Colors of a loaded image, from a small copy on a canvas. Images from+ * another origin without CORS can't be read and return nothing.+ */+const imageColors = (img: HTMLImageElement): LogoColor[] => {+ if (!img.complete || !img.naturalWidth || !img.naturalHeight) return [];+ const scale = Math.min(+ 1,+ SAMPLE_PX / Math.max(img.naturalWidth, img.naturalHeight),+ );+ const w = Math.max(1, Math.round(img.naturalWidth * scale));+ const h = Math.max(1, Math.round(img.naturalHeight * scale));+ const canvas = document.createElement("canvas");+ canvas.width = w;+ canvas.height = h;+ const ctx = canvas.getContext("2d");+ if (!ctx) return [];+ let data: Uint8ClampedArray;+ try {+ ctx.drawImage(img, 0, 0, w, h);+ data = ctx.getImageData(0, 0, w, h).data;+ } catch {+ return []; // cross-origin+ }+ // Group pixels into 4-bit-per-channel buckets; report each bucket's average.+ const buckets = new Map<+ number,+ { n: number; r: number; g: number; b: number }+ >();+ for (let i = 0; i < data.length; i += 4) {+ if (data[i + 3] < 128) continue;+ const key =+ ((data[i] >> 4) << 8) | ((data[i + 1] >> 4) << 4) | (data[i + 2] >> 4);+ const bucket = buckets.get(key) ?? { n: 0, r: 0, g: 0, b: 0 };+ bucket.n++;+ bucket.r += data[i];+ bucket.g += data[i + 1];+ bucket.b += data[i + 2];+ buckets.set(key, bucket);+ }+ const weights = new Map<string, number>();+ buckets.forEach(b => {+ const hex = toHex(b.r / b.n, b.g / b.n, b.b / b.n);+ weights.set(hex, (weights.get(hex) ?? 0) + b.n);+ });+ return ranked(weights);+};++/** The colors a logo is drawn in, most used first, when they can be read. */+export const getLogoColors = (el: Element): LogoColor[] => {+ try {+ if (el instanceof SVGSVGElement) return svgColors(el);+ if (el instanceof HTMLImageElement) return imageColors(el);+ // A logo drawn as a CSS background: read the image when the browser+ // already has it (an image it still has to load reads as nothing).+ const url = getComputedStyleCached(el).backgroundImage.match(+ /url\(["']?([^"')]+)["']?\)/,+ )?.[1];+ if (url) {+ const img = new Image();+ img.src = url;+ return imageColors(img);+ }+ } catch (e) {+ recordError("getLogoColors", e);+ }+ return [];+};