Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(branding): show the model each button style, not just the top-ranked (#5115)

Abimael Martelle94bf2159523
diff --git a/apps/api/src/__tests__/lib/branding/button-selection.test.ts b/apps/api/src/__tests__/lib/branding/button-selection.test.ts
new file mode 100644
index 000000000..f6bd9f1fd
--- /dev/null
+++ b/apps/api/src/__tests__/lib/branding/button-selection.test.ts
@@ -0,0 +1,72 @@
+import { describe, it, expect } from "vitest";
+import { isPageControl } from "../../../lib/branding/page-controls";
+import { pickDistinctStylesFirst } from "../../../lib/branding/transformer";
+
+const button = (text: string, background: string, borderColor?: string) => ({
+ btn: {
+ index: 0,
+ text,
+ html: "",
+ classes: "",
+ background,
+ textColor: background === "transparent" ? "#111111" : "#FFFFFF",
+ borderColor,
+ },
+});
+
+describe("choosing which buttons the model sees", () => {
+ it("keeps a second style that ranks below many look-alike buttons", () => {
+ const ranked = [
+ ...Array.from({ length: 14 }, (_, i) => button(`Order ${i}`, "#C82540")),
+ button("Contact sales", "transparent", "#111111"),
+ ];
+
+ const picked = pickDistinctStylesFirst(ranked, 12);
+
+ expect(picked).toHaveLength(12);
+ expect(picked.map(p => p.btn.text)).toContain("Contact sales");
+ expect(picked[0].btn.text).toBe("Order 0");
+ });
+
+ it("keeps ranking order and fills up with the best of the rest", () => {
+ const ranked = [
+ button("Buy", "#C82540"),
+ button("Buy again", "#C82540"),
+ button("Learn more", "transparent", "#111111"),
+ ];
+
+ expect(pickDistinctStylesFirst(ranked, 2).map(p => p.btn.text)).toEqual([
+ "Buy",
+ "Learn more",
+ ]);
+ expect(pickDistinctStylesFirst(ranked, 3).map(p => p.btn.text)).toEqual([
+ "Buy",
+ "Buy again",
+ "Learn more",
+ ]);
+ });
+});
+
+describe("page controls", () => {
+ it("recognises skip links and map controls, but not real calls to action", () => {
+ const controls = [
+ "Skip to content",
+ "Skip to main content",
+ "Satellite",
+ "Map",
+ "View fullsize",
+ "Toggle fullscreen",
+ "Zoom in",
+ "Zoom out",
+ ];
+ const actions = [
+ "Search",
+ "Next",
+ "Shop the map collection",
+ "Contact sales",
+ "View our work",
+ ];
+ expect(controls.filter(text => isPageControl({ text }))).toEqual(controls);
+ expect(actions.filter(text => isPageControl({ text }))).toEqual([]);
+ });
+});
diff --git a/apps/api/src/__tests__/lib/branding/jev.test.ts b/apps/api/src/__tests__/lib/branding/jev.test.ts
index 9ea17cbe2..bb88869d1 100644
--- a/apps/api/src/__tests__/lib/branding/jev.test.ts
+++ b/apps/api/src/__tests__/lib/branding/jev.test.ts
@@ -359,6 +359,17 @@ describe("branding with Jev", () => {
expect(merged.colors?.primary).toBe("#6D28D9");
});
+ it("tells cookie settings apart from buttons that merely contain the words", () => {
+ const input = baseInput(new CostTracking());
+ input.buttons = [
+ { ...input.buttons[0], index: 0, text: "View preferences" },
+ { ...input.buttons[1], index: 1, text: "Preview settings" },
+ ];
+ expect(Object.keys(buildJevRequest(input).state.buttons as object)).toEqual(
+ ["button_1"],
+ );
+ });
+
it("never asks Jev to pick a cookie-consent button", async () => {
const input = baseInput(new CostTracking());
input.buttons = [
diff --git a/apps/api/src/lib/branding/jev.ts b/apps/api/src/lib/branding/jev.ts
index 27c77511e..ea3ba0eba 100644
--- a/apps/api/src/lib/branding/jev.ts
+++ b/apps/api/src/lib/branding/jev.ts
@@ -362,7 +362,7 @@ function fileName(src: string): string {
// Cookie-consent controls are page chrome, never the site's call to action.
const CONSENT =
- /cookie|consent|gdpr|accept all|reject all|allow all|decline|manage (?:preferences|settings)|akzeptieren|ablehnen|accepter|refuser|aceptar|rechazar/i;
+ /cookie|consent|gdpr|accept all|reject all|allow all|decline|\b(?:manage|view) (?:preferences|settings)|akzeptieren|ablehnen|accepter|refuser|aceptar|rechazar/i;
const isConsentButton = (button: { text?: string; classes?: string }) =>
CONSENT.test(`${button.text ?? ""} ${button.classes ?? ""}`);
diff --git a/apps/api/src/lib/branding/page-controls.ts b/apps/api/src/lib/branding/page-controls.ts
new file mode 100644
index 000000000..ce0341b5e
--- /dev/null
+++ b/apps/api/src/lib/branding/page-controls.ts
@@ -0,0 +1,8 @@
+// Controls every page has that aren't calls to action: skip links, map and
+// image-viewer buttons. Kept narrow: "Search" or "Next" can be a site's real
+// call to action.
+const PAGE_CONTROL =
+ /^(?:skip (?:to|navigation)|map$|satellite$|(?:enter |exit |toggle )?full ?screen$|view full ?size|zoom (?:in|out)$)/i;
+
+export const isPageControl = (button: { text?: string }): boolean =>
+ PAGE_CONTROL.test((button.text ?? "").trim());
diff --git a/apps/api/src/lib/branding/transformer.ts b/apps/api/src/lib/branding/transformer.ts
index 760a89b57..963dbd69a 100644
--- a/apps/api/src/lib/branding/transformer.ts
+++ b/apps/api/src/lib/branding/transformer.ts
@@ -7,6 +7,7 @@ import { Meta } from "../../scraper/scrapeURL";
import { Document } from "../../controllers/v2/types";
import { BrandingScriptReturn, ButtonSnapshot } from "./types";
import { mergeBrandingResults } from "./merge";
+import { isPageControl } from "./page-controls";
import {
selectLogoWithConfidence,
getTopCandidatesForLLM,
@@ -26,6 +27,32 @@ function isDebugBrandingEnabled(meta: Meta): boolean {
);
}
+/**
+ * The best-ranked button of each look first, then the rest, up to `max`, in
+ * ranking order: a page with many buttons in one style still shows the model
+ * its other styles, such as a secondary action.
+ */
+export function pickDistinctStylesFirst<T extends { btn: ButtonSnapshot }>(
+ ranked: T[],
+ max: number,
+): T[] {
+ const style = ({ btn }: T) =>
+ `${btn.background}|${btn.textColor}|${btn.borderColor ?? ""}`.toUpperCase();
+ const seen = new Set<string>();
+ const firsts: T[] = [];
+ const rest: T[] = [];
+ for (const item of ranked) {
+ if (seen.has(style(item))) {
+ rest.push(item);
+ } else {
+ seen.add(style(item));
+ firsts.push(item);
+ }
+ }
+ const picked = new Set([...firsts, ...rest].slice(0, max));
+ return ranked.filter(item => picked.has(item));
+}
+
export async function brandingTransformer(
meta: Meta,
document: Document,
@@ -117,12 +144,16 @@ export async function brandingTransformer(
// 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) => {
+ // Skip links and map controls aren't calls to action: leave them out
+ // before choosing, so they take no place among the buttons sent.
+ const candidates = buttonSnapshots
+ .map((btn, idx) => ({ btn, originalIdx: idx }))
+ .filter(({ btn }) => !isPageControl(btn));
+ let chosen = candidates;
+
+ if (candidates.length > 12) {
+ const ranked = candidates
+ .map(({ btn, originalIdx }) => {
let score = 0;
const text = (btn.text || "").toLowerCase();
const bgColor = btn.background || "";
@@ -161,24 +192,18 @@ export async function brandingTransformer(
// On the first screen of the page
if (typeof btn.top === "number" && btn.top < 1000) score += 40;
- return { btn, originalIdx: idx, score };
+ return { btn, originalIdx, 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);
- });
+ .sort((a, b) => b.score - a.score);
+ chosen = pickDistinctStylesFirst(ranked, 12);
}
+ const limitedButtons = chosen.map(item => item.btn);
+ const buttonIndexMap = new Map<number, number>(); // LLM index -> original index
+ chosen.forEach((item, llmIdx) => {
+ buttonIndexMap.set(llmIdx, item.originalIdx);
+ });
+
meta.logger.info("Button filtering for LLM", {
totalButtons: buttonSnapshots.length,
limitedButtons: limitedButtons.length,