AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): prefer sub_heading over the long description on link-preview cards (#14496)
Reinier van der Leer5eca2d3000ec
diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.tsindex 506e30e06b..91a1a635a8 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/__tests__/generateMetadata.test.ts@@ -88,11 +88,12 @@ describe("generateMetadata", () => {);});- test("previews the agent's own name, description and image", async () => {+ test("previews the agent's own name, sub-heading and image", async () => {mockGetSpecificAgent.mockResolvedValue({data: {agent_name: "An Agent",- description: "What the agent does",+ sub_heading: "Summarises yesterday's runs",+ description: "- a bullet\n- another bullet\nand a long tail",agent_image: ["https://cdn.example.com/agent.png"],},});@@ -103,13 +104,37 @@ describe("generateMetadata", () => {expect(metadata.title).toBe("An Agent - AutoGPT Marketplace");expect(metadata.openGraph?.title).toBe("An Agent - AutoGPT Marketplace");- expect(metadata.openGraph?.description).toBe("What the agent does");+ expect(metadata.openGraph?.description).toBe("Summarises yesterday's runs");expect(metadata.openGraph?.images).toEqual(["https://cdn.example.com/agent.png",]);expect(metadata.twitter).toMatchObject({ card: "summary_large_image" });});+ test.each([+ ["missing", undefined],+ ["empty", ""],+ ["whitespace-only", " "],+ ])(+ "falls back to the agent name when sub_heading is %s",+ async (_label, sub_heading) => {+ mockGetSpecificAgent.mockResolvedValue({+ data: {+ agent_name: "An Agent",+ sub_heading,+ description: "The long listing description",+ agent_image: [],+ },+ });++ const metadata = await generateMetadata({+ params: Promise.resolve(params),+ });++ expect(metadata.openGraph?.description).toBe("An Agent");+ },+ );+test("uses only the first image when the listing carries several", async () => {mockGetSpecificAgent.mockResolvedValue({data: {diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsxindex b96a37eddf..509beb6a44 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/agent/[creator]/[slug]/page.tsx@@ -29,7 +29,7 @@ export async function generateMetadata({return buildPageMetadata({title: `${agent.agent_name} - AutoGPT Marketplace`,- description: agent.description,+ description: agent.sub_heading?.trim() || agent.agent_name,path: `/marketplace/agent/${params.creator}/${params.slug}`,images: agent.agent_image?.slice(0, 1),type: "article",diff --git a/autogpt_platform/frontend/src/lib/__tests__/metadata.test.ts b/autogpt_platform/frontend/src/lib/__tests__/metadata.test.tsindex ac5ce3f2a1..4b4a37a7b8 100644--- a/autogpt_platform/frontend/src/lib/__tests__/metadata.test.ts+++ b/autogpt_platform/frontend/src/lib/__tests__/metadata.test.ts@@ -43,6 +43,48 @@ describe("buildPageMetadata", () => {expect(metadata.twitter).toMatchObject({ card: "summary" });});+ test("keeps a description that fits under the cap", () => {+ const description = "Sends a summary of yesterday's runs every morning.";+ const metadata = buildPageMetadata({ title: "An Agent", description });++ expect(metadata.description).toBe(description);+ expect(metadata.openGraph?.description).toBe(description);+ expect(metadata.twitter?.description).toBe(description);+ });++ test("cuts an over-long description on a word boundary", () => {+ const description = `${"word ".repeat(60)}tail`;+ const metadata = buildPageMetadata({ title: "An Agent", description });++ const summary = metadata.description as string;+ expect(summary.length).toBeLessThanOrEqual(200);+ expect(summary.endsWith("…")).toBe(true);+ expect(summary).not.toMatch(/wor…$/);+ expect(summary.slice(0, -1).endsWith("word")).toBe(true);+ });++ test("keeps an over-long description with no spaces within the cap", () => {+ const metadata = buildPageMetadata({+ title: "An Agent",+ description: "x".repeat(300),+ });++ const summary = metadata.description as string;+ expect(summary.length).toBeLessThanOrEqual(200);+ expect(summary.endsWith("…")).toBe(true);+ });++ test.each([+ ["undefined", undefined],+ ["null", null],+ ["blank", " "],+ ])("omits the description when it is %s", (_label, description) => {+ const metadata = buildPageMetadata({ title: "An Agent", description });++ expect(metadata.description).toBeUndefined();+ expect(metadata.openGraph?.description).toBeUndefined();+ });+test("resolves the canonical and og:url against the site URL", () => {vi.stubEnv("NEXT_PUBLIC_FRONTEND_BASE_URL", "https://platform.agpt.co");diff --git a/autogpt_platform/frontend/src/lib/metadata.ts b/autogpt_platform/frontend/src/lib/metadata.tsindex 35f45c8a53..1a2592b5b2 100644--- a/autogpt_platform/frontend/src/lib/metadata.ts+++ b/autogpt_platform/frontend/src/lib/metadata.ts@@ -1,6 +1,8 @@import type { Metadata } from "next";const SITE_NAME = "AutoGPT";+// X truncates around 200 characters, the tightest of the platforms we target.+const DESCRIPTION_MAX_LENGTH = 200;interface PageMetadataOptions {title: string;@@ -23,7 +25,7 @@ export function buildPageMetadata({const cardImages = (images ?? []).filter((image): image is string => typeof image === "string" && image.length > 0,);- const summary = description || undefined;+ const summary = toCardDescription(description);return {title,@@ -46,6 +48,20 @@ export function buildPageMetadata({};}+// Unfurlers cut a long description at their own limit, mid-word; cutting on a+// word boundary first keeps the last word of the card whole.+function toCardDescription(description?: string | null): string | undefined {+ const text = description?.trim();+ if (!text) return undefined;+ if (text.length <= DESCRIPTION_MAX_LENGTH) return text;++ // One character is left for the ellipsis.+ const clipped = text.slice(0, DESCRIPTION_MAX_LENGTH - 1);+ const lastSpace = clipped.lastIndexOf(" ");+ const body = lastSpace > 0 ? clipped.slice(0, lastSpace) : clipped;+ return `${body.trimEnd()}…`;+}+// Falls back rather than returning a value `new URL()` would reject: the root// layout builds metadataBase from this, so a bad origin here fails the build.export function getSiteUrl(): string {