Skip to content

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

fix(branding): keep the model's button picks when it is unsure (#5247)

Abimael Martell4085a5750c01
diff --git a/apps/api/src/__tests__/lib/branding/merge-buttons.test.ts b/apps/api/src/__tests__/lib/branding/merge-buttons.test.ts
new file mode 100644
index 000000000..48e11abfb
--- /dev/null
+++ b/apps/api/src/__tests__/lib/branding/merge-buttons.test.ts
@@ -0,0 +1,71 @@
+import { describe, it, expect } from "vitest";
+
+import { mergeBrandingResults } from "../../../lib/branding/merge";
+import type { BrandingEnhancement } from "../../../lib/branding/schema";
+import type { ButtonSnapshot } from "../../../lib/branding/types";
+
+const button = (
+ index: number,
+ text: string,
+ background: string,
+ textColor: string,
+): ButtonSnapshot => ({
+ index,
+ text,
+ html: `<a>${text}</a>`,
+ classes: "btn",
+ background,
+ textColor,
+ borderRadius: "30px",
+});
+
+const BUTTONS = [
+ button(0, "Home", "transparent", "#333333"),
+ button(1, "Book an Appointment", "#0099FF", "#FFFFFF"),
+ button(2, "Contact us", "#0B2545", "#FFFFFF"),
+];
+
+const answer = (
+ confidence: number,
+ primaryButtonIndex = 1,
+ secondaryButtonIndex = 2,
+): BrandingEnhancement => ({
+ cleanedFonts: [],
+ buttonClassification: {
+ primaryButtonIndex,
+ primaryButtonReasoning: "jev: p=0.39",
+ secondaryButtonIndex,
+ secondaryButtonReasoning: "jev: p=0.31",
+ confidence,
+ },
+ colorRoles: {
+ primaryColor: "",
+ accentColor: "",
+ backgroundColor: "",
+ textPrimary: "",
+ confidence: 0,
+ },
+});
+
+describe("merging the model's button picks", () => {
+ it("keeps a pick the model was unsure of rather than leaving the page without buttons", () => {
+ const merged = mergeBrandingResults({}, answer(0.39), BUTTONS);
+
+ expect(merged.components?.buttonPrimary).toMatchObject({
+ background: "#0099FF",
+ textColor: "#FFFFFF",
+ borderRadius: "30px",
+ });
+ expect(merged.components?.buttonSecondary).toMatchObject({
+ background: "#0B2545",
+ });
+ });
+
+ it("adds no buttons when the model call failed", () => {
+ // Valid indices, so only the confidence check keeps them out.
+ const merged = mergeBrandingResults({}, answer(0), BUTTONS);
+
+ expect(merged.components?.buttonPrimary).toBeUndefined();
+ expect(merged.components?.buttonSecondary).toBeUndefined();
+ });
+});
diff --git a/apps/api/src/lib/branding/merge.ts b/apps/api/src/lib/branding/merge.ts
index 7f895b1ee..45ffadb90 100644
--- a/apps/api/src/lib/branding/merge.ts
+++ b/apps/api/src/lib/branding/merge.ts
@@ -236,7 +236,11 @@ export function mergeBrandingResults(
};
}
- if (llm.buttonClassification.confidence > 0.5 && buttonSnapshots.length > 0) {
+ // A model that answered gave its most likely buttons. Jev's confidence is a
+ // calibrated probability, often under 0.5 when a page has several plausible
+ // calls to action, and its pick still beats a page without buttons. A
+ // failed call reports 0 and keeps the heuristic result.
+ if (llm.buttonClassification.confidence > 0 && buttonSnapshots.length > 0) {
const primaryIdx = llm.buttonClassification.primaryButtonIndex;
const secondaryIdx = llm.buttonClassification.secondaryButtonIndex;