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/marketplace): Use agent name in graph download filename (#13523)
Reinier van der Leer44a9d57c36ae
diff --git a/autogpt_platform/frontend/src/app/(platform)/admin/marketplace/components/DownloadAgentButton.tsx b/autogpt_platform/frontend/src/app/(platform)/admin/marketplace/components/DownloadAgentButton.tsxindex e18c46b126..d9ef669227 100644--- a/autogpt_platform/frontend/src/app/(platform)/admin/marketplace/components/DownloadAgentButton.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/admin/marketplace/components/DownloadAgentButton.tsx@@ -2,6 +2,7 @@import { downloadAsAdmin } from "@/app/(platform)/admin/marketplace/actions";import { Button } from "@/components/__legacy__/ui/button";+import { agentGraphExportFilename, exportAsJSONFile } from "@/lib/utils";import { ExternalLink } from "lucide-react";import { useState } from "react";@@ -18,25 +19,7 @@ export function DownloadAgentAdminButton({// Call the server action to get the dataconst fileData = await downloadAsAdmin(storeListingVersionId);- // Client-side download logic- const jsonData = JSON.stringify(fileData, null, 2);- const blob = new Blob([jsonData], { type: "application/json" });-- // Create a temporary URL for the Blob- const url = window.URL.createObjectURL(blob);-- // Create a temporary anchor element- const a = document.createElement("a");- a.href = url;- a.download = `agent_${storeListingVersionId}.json`;-- // Append the anchor to the body, click it, and remove it- document.body.appendChild(a);- a.click();- document.body.removeChild(a);-- // Revoke the temporary URL- window.URL.revokeObjectURL(url);+ exportAsJSONFile(fileData as object, agentGraphExportFilename(fileData));} catch (error) {console.error("Download failed:", error);} finally {diff --git a/autogpt_platform/frontend/src/app/(platform)/marketplace/components/AgentInfo/useAgentInfo.ts b/autogpt_platform/frontend/src/app/(platform)/marketplace/components/AgentInfo/useAgentInfo.tsindex 37d8a5d75a..573ee9cef1 100644--- a/autogpt_platform/frontend/src/app/(platform)/marketplace/components/AgentInfo/useAgentInfo.ts+++ b/autogpt_platform/frontend/src/app/(platform)/marketplace/components/AgentInfo/useAgentInfo.ts@@ -8,6 +8,7 @@ import * as Sentry from "@sentry/nextjs";import { useGetV2DownloadAgentFile } from "@/app/api/__generated__/endpoints/store/store";import { analytics } from "@/services/analytics";import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";+import { agentGraphExportFilename, exportAsJSONFile } from "@/lib/utils";import { useQueryClient } from "@tanstack/react-query";interface UseAgentInfoProps {@@ -79,19 +80,10 @@ export const useAgentInfo = ({ storeListingVersionId }: UseAgentInfoProps) => {try {const { data: file } = await downloadAgent();- const jsonData = JSON.stringify(file, null, 2);- const blob = new Blob([jsonData], { type: "application/json" });- const url = window.URL.createObjectURL(blob);-- const a = document.createElement("a");- a.href = url;- a.download = `agent_${storeListingVersionId}.json`;-- document.body.appendChild(a);- a.click();- document.body.removeChild(a);-- window.URL.revokeObjectURL(url);+ exportAsJSONFile(+ file as object,+ agentGraphExportFilename(file, agentName),+ );analytics.sendDatafastEvent("download_agent", {name: agentName,diff --git a/autogpt_platform/frontend/src/lib/__tests__/utils.test.ts b/autogpt_platform/frontend/src/lib/__tests__/utils.test.tsindex e7497c18fb..72b79e4633 100644--- a/autogpt_platform/frontend/src/lib/__tests__/utils.test.ts+++ b/autogpt_platform/frontend/src/lib/__tests__/utils.test.ts@@ -1,5 +1,5 @@import { describe, expect, test } from "vitest";-import { setNestedProperty } from "../utils";+import { agentGraphExportFilename, setNestedProperty } from "../utils";const testCases = [{@@ -95,3 +95,39 @@ describe("setNestedProperty", () => {expect(({} as { polluted?: boolean }).polluted).toBeUndefined();});});++describe("agentGraphExportFilename", () => {+ test("uses the graph's name and version", () => {+ const graph = { name: "Email Digest", version: 3 };+ expect(agentGraphExportFilename(graph)).toBe("Email Digest_v3.json");+ });++ test("omits the version suffix when the graph has no version", () => {+ expect(agentGraphExportFilename({ name: "Email Digest" })).toBe(+ "Email Digest.json",+ );+ });++ test("falls back to the given name when the graph has none", () => {+ expect(agentGraphExportFilename({ version: 2 }, "Store Agent")).toBe(+ "Store Agent_v2.json",+ );+ });++ test("replaces filesystem-hostile characters in the name", () => {+ expect(+ agentGraphExportFilename({ name: 'A/B: "test" <agent>?', version: 1 }),+ ).toBe("A_B_ _test_ _agent_v1.json");+ });++ test('falls back to "agent" for non-object graphs without a fallback name', () => {+ expect(agentGraphExportFilename(null)).toBe("agent.json");+ expect(agentGraphExportFilename("nope")).toBe("agent.json");+ });++ test("ignores blank names", () => {+ expect(agentGraphExportFilename({ name: " ", version: 1 }, " ")).toBe(+ "agent_v1.json",+ );+ });+});diff --git a/autogpt_platform/frontend/src/lib/utils.ts b/autogpt_platform/frontend/src/lib/utils.tsindex c2997a2481..197048beb3 100644--- a/autogpt_platform/frontend/src/lib/utils.ts+++ b/autogpt_platform/frontend/src/lib/utils.ts@@ -194,6 +194,24 @@ export function exportAsJSONFile(obj: object, filename: string): void {URL.revokeObjectURL(url);}+export function agentGraphExportFilename(+ graph: unknown,+ fallbackName = "agent",+): string {+ const { name, version } = (+ typeof graph === "object" && graph !== null ? graph : {}+ ) as { name?: unknown; version?: unknown };+ const rawName = typeof name === "string" && name.trim() ? name : fallbackName;+ const safeName =+ rawName+ .trim()+ .replace(/[\\/:*?"<>|\x00-\x1f]/g, "_")+ .replace(/[_ ]+$/, "") || "agent";+ return typeof version === "number"+ ? `${safeName}_v${version}.json`+ : `${safeName}.json`;+}+export function setNestedProperty(obj: any, path: string, value: any) {if (!obj || typeof obj !== "object") {throw new Error("Target must be a non-null object");