Skip to content

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

fix(branding): fall back to heuristic logo pick when the LLM call fails (#4142)

Abimael Martellec141381b057
diff --git a/apps/api/src/__tests__/lib/branding/llm-failure-fallback.test.ts b/apps/api/src/__tests__/lib/branding/llm-failure-fallback.test.ts
new file mode 100644
index 000000000..da2bec978
--- /dev/null
+++ b/apps/api/src/__tests__/lib/branding/llm-failure-fallback.test.ts
@@ -0,0 +1,90 @@
+import { vi, describe, it, expect } from "vitest";
+
+vi.mock("ai", () => ({
+ generateObject: vi.fn().mockRejectedValue(new Error("connection reset")),
+}));
+vi.mock("../../../lib/generic-ai", () => ({
+ getModel: vi.fn().mockReturnValue({}),
+}));
+vi.mock("../../../services/sentry", () => ({
+ captureExceptionWithZdrCheck: vi.fn(),
+}));
+
+import { enhanceBrandingWithLLM } from "../../../lib/branding/llm";
+import { mergeBrandingResults } from "../../../lib/branding/merge";
+import { logger } from "../../../lib/logger";
+
+const CANDIDATES = [
+ {
+ src: "https://example.com/logo.svg",
+ alt: "Example logo",
+ isSvg: true,
+ isVisible: true,
+ location: "header" as const,
+ position: { top: 10, left: 10, width: 140, height: 40 },
+ indicators: {
+ inHeader: true,
+ altMatch: true,
+ srcMatch: true,
+ classMatch: false,
+ hrefMatch: true,
+ },
+ href: "/",
+ source: "header a img",
+ },
+];
+
+describe("LLM failure fallback", () => {
+ it("omits logoSelection, personality and designSystem on failure", async () => {
+ const result = await enhanceBrandingWithLLM({
+ jsAnalysis: {},
+ buttons: [],
+ logoCandidates: CANDIDATES,
+ url: "https://example.com",
+ logger,
+ });
+
+ expect(result.logoSelection).toBeUndefined();
+ expect(result.personality).toBeUndefined();
+ expect(result.designSystem).toBeUndefined();
+ expect(result.buttonClassification.primaryButtonReasoning).toBe(
+ "LLM failed",
+ );
+ });
+
+ it("merge keeps the heuristic logo pick and stamps no personality", () => {
+ // Shape the transformer produces when it restores the heuristic after an
+ // absent LLM logoSelection.
+ const merged = mergeBrandingResults(
+ { images: {} },
+ {
+ cleanedFonts: [],
+ buttonClassification: {
+ primaryButtonIndex: -1,
+ primaryButtonReasoning: "LLM failed",
+ secondaryButtonIndex: -1,
+ secondaryButtonReasoning: "LLM failed",
+ confidence: 0,
+ },
+ colorRoles: {
+ primaryColor: "",
+ accentColor: "",
+ backgroundColor: "",
+ textPrimary: "",
+ confidence: 0,
+ },
+ logoSelection: {
+ selectedLogoIndex: 0,
+ selectedLogoReasoning: "Heuristic fallback: strong indicators",
+ confidence: 0.8,
+ },
+ },
+ [],
+ CANDIDATES,
+ );
+
+ expect(merged.images?.logo).toBe("https://example.com/logo.svg");
+ expect((merged as any).personality).toBeUndefined();
+ expect((merged as any).designSystem).toBeUndefined();
+ });
+});
diff --git a/apps/api/src/lib/branding/llm.ts b/apps/api/src/lib/branding/llm.ts
index 910705ac8..b877615fd 100644
--- a/apps/api/src/lib/branding/llm.ts
+++ b/apps/api/src/lib/branding/llm.ts
@@ -216,6 +216,11 @@ export async function enhanceBrandingWithLLM(
});
}
+ // On LLM failure return only the fields that have honest fallback values.
+ // Omitting logoSelection lets the transformer restore the heuristic's
+ // pick (a failure object with confidence 0 used to override it and drop
+ // the logo entirely); omitting personality/designSystem avoids stamping
+ // fabricated values into the output.
return {
cleanedFonts: [],
buttonClassification: {
@@ -232,20 +237,6 @@ export async function enhanceBrandingWithLLM(
textPrimary: "",
confidence: 0,
},
- personality: {
- tone: "professional",
- energy: "medium",
- targetAudience: "unknown",
- },
- designSystem: {
- framework: "unknown",
- componentLibrary: "",
- },
- logoSelection: {
- selectedLogoIndex: -1,
- selectedLogoReasoning: "LLM failed",
- confidence: 0,
- },
};
}
}
diff --git a/apps/api/src/lib/branding/schema.ts b/apps/api/src/lib/branding/schema.ts
index 291510d2e..fd36b1222 100644
--- a/apps/api/src/lib/branding/schema.ts
+++ b/apps/api/src/lib/branding/schema.ts
@@ -137,12 +137,20 @@ export function getBrandingEnhancementSchema(hasLogoCandidates: boolean) {
: baseBrandingEnhancementSchema;
}
-// Type - logoSelection is optional in the type even though it's required in schema when candidates exist
+// Type - logoSelection is optional in the type even though it's required in
+// schema when candidates exist. personality/designSystem are optional so the
+// LLM-failure fallback can omit them instead of fabricating values.
export type BrandingEnhancement = Omit<
z.infer<typeof brandingEnhancementSchemaWithLogo>,
- "logoSelection"
+ "logoSelection" | "personality" | "designSystem"
> & {
logoSelection?: z.infer<
typeof brandingEnhancementSchemaWithLogo
>["logoSelection"];
+ personality?: z.infer<
+ typeof brandingEnhancementSchemaWithLogo
+ >["personality"];
+ designSystem?: z.infer<
+ typeof brandingEnhancementSchemaWithLogo
+ >["designSystem"];
};