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/copilot): Show block name instead of ID during `run_block` (#11918)
Bently18cd46ffb5de
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/RunBlock.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/RunBlock.tsxindex fd83e8abb5..52921c296a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/RunBlock.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/RunBlock.tsx@@ -1,5 +1,6 @@"use client";+import { useGetV1ListAvailableBlocks } from "@/app/api/__generated__/endpoints/blocks/blocks";import type { ToolUIPart } from "ai";import { MorphingTextAnimation } from "../../components/MorphingTextAnimation/MorphingTextAnimation";import { ToolAccordion } from "../../components/ToolAccordion/ToolAccordion";@@ -15,6 +16,7 @@ import {import {getAccordionMeta,getAnimationText,+ getBlockNamesById,getRunBlockToolOutput,isRunBlockBlockOutput,isRunBlockDetailsOutput,@@ -38,12 +40,23 @@ interface Props {}export function RunBlockTool({ part }: Props) {- const text = getAnimationText(part);+ const input = part.input as RunBlockInput | undefined;+ // The LLM's input only carries a block_id; resolve it to a name so the+ // in-flight state doesn't show a raw UUID (outputs carry the name already)+ const needsBlockNameLookup =+ !input?.block_name?.trim() && !!input?.block_id?.trim();+ const { data: blocksResponse } = useGetV1ListAvailableBlocks({+ query: { enabled: needsBlockNameLookup, staleTime: 5 * 60 * 1000 },+ });+ const text = getAnimationText(+ part,+ needsBlockNameLookup ? getBlockNamesById(blocksResponse) : undefined,+ );const isStreaming =part.state === "input-streaming" || part.state === "input-available";const output = getRunBlockToolOutput(part);- const inputData = (part.input as RunBlockInput | undefined)?.input_data;+ const inputData = input?.input_data;const hasInputData = inputData != null && Object.keys(inputData).length > 0;const isCorrupted =part.state === "output-available" &&diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/RunBlock.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/RunBlock.test.tsxnew file mode 100644index 0000000000..d060f4a167--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/RunBlock.test.tsx@@ -0,0 +1,46 @@+import { describe, expect, it } from "vitest";+import { getGetV1ListAvailableBlocksMockHandler } from "@/app/api/__generated__/endpoints/blocks/blocks.msw";+import { server } from "@/mocks/mock-server";+import { render, screen } from "@/tests/integrations/test-utils";+import { RunBlockTool, type RunBlockToolPart } from "../RunBlock";++const blockId = "87840993-2053-44b7-8da4-187ad4ee518c";++function makePart(overrides: Partial<RunBlockToolPart> = {}): RunBlockToolPart {+ return {+ type: "tool-run_block",+ toolCallId: "call-run-block-1",+ state: "input-available",+ input: { block_id: blockId, input_data: {} },+ ...overrides,+ };+}++describe("RunBlockTool block name lookup", () => {+ it("shows the resolved block name instead of the raw id while running", async () => {+ server.use(+ getGetV1ListAvailableBlocksMockHandler([+ { id: blockId, name: "AIConversationBlock" },+ ]),+ );+ render(<RunBlockTool part={makePart()} />);+ expect(await screen.findByText('Running "AI Conversation"')).not.toBeNull();+ });++ it("shows the raw block id until a name is known", () => {+ server.use(getGetV1ListAvailableBlocksMockHandler([]));+ render(<RunBlockTool part={makePart()} />);+ expect(screen.getByText(`Running "${blockId}"`)).not.toBeNull();+ });++ it("uses block_name from the input without needing the lookup", () => {+ render(+ <RunBlockTool+ part={makePart({+ input: { block_id: blockId, block_name: "My Block", input_data: {} },+ })}+ />,+ );+ expect(screen.getByText('Running "My Block"')).not.toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/helpers.test.tsnew file mode 100644index 0000000000..c1b3b19cb5--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/__tests__/helpers.test.ts@@ -0,0 +1,97 @@+import { describe, expect, it } from "vitest";+import type { getV1ListAvailableBlocksResponse } from "@/app/api/__generated__/endpoints/blocks/blocks";+import { getAnimationText, getBlockNamesById } from "../helpers";++function make200(+ blocks: Record<string, unknown>[],+): getV1ListAvailableBlocksResponse {+ return {+ data: blocks,+ status: 200,+ headers: new Headers(),+ };+}++describe("getBlockNamesById", () => {+ it("returns undefined without a response", () => {+ expect(getBlockNamesById(undefined)).toBeUndefined();+ });++ it("returns undefined for a non-200 response", () => {+ const response = {+ data: { detail: "Not authenticated" },+ status: 401,+ headers: new Headers(),+ } as getV1ListAvailableBlocksResponse;+ expect(getBlockNamesById(response)).toBeUndefined();+ });++ it("maps ids to beautified names without the Block suffix", () => {+ const names = getBlockNamesById(+ make200([{ id: "block-1", name: "AIConversationBlock" }]),+ );+ expect(names?.get("block-1")).toBe("AI Conversation");+ });++ it("skips entries without string id and name", () => {+ const names = getBlockNamesById(+ make200([+ { id: "block-1" },+ { name: "OrphanBlock" },+ { id: 42, name: "NumericIdBlock" },+ { id: "block-2", name: "GetWeatherBlock" },+ ]),+ );+ expect(names?.size).toBe(1);+ expect(names?.get("block-2")).toBe("Get Weather");+ });+});++describe("getAnimationText block name resolution", () => {+ const blockId = "87840993-2053-44b7-8da4-187ad4ee518c";++ it("prefers block_name from the input", () => {+ const text = getAnimationText(+ {+ state: "input-available",+ input: { block_id: blockId, block_name: "My Block" },+ },+ new Map([[blockId, "Looked Up"]]),+ );+ expect(text).toBe('Running "My Block"');+ });++ it("falls back to the looked-up name when input has only block_id", () => {+ const text = getAnimationText(+ { state: "input-available", input: { block_id: blockId } },+ new Map([[blockId, "AI Conversation"]]),+ );+ expect(text).toBe('Running "AI Conversation"');+ });++ it("falls back to the raw block_id when no name is known", () => {+ expect(+ getAnimationText({+ state: "input-available",+ input: { block_id: blockId },+ }),+ ).toBe(`Running "${blockId}"`);+ expect(+ getAnimationText(+ { state: "input-available", input: { block_id: blockId } },+ new Map([["other-id", "Other"]]),+ ),+ ).toBe(`Running "${blockId}"`);+ });++ it("uses the looked-up name for dry runs too", () => {+ const text = getAnimationText(+ {+ state: "input-available",+ input: { block_id: blockId, dry_run: true },+ },+ new Map([[blockId, "AI Conversation"]]),+ );+ expect(text).toBe('Simulating "AI Conversation"');+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/helpers.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/helpers.tsxindex fa71751ca9..a64de193dd 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/helpers.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/RunBlock/helpers.tsx@@ -1,7 +1,9 @@+import type { getV1ListAvailableBlocksResponse } from "@/app/api/__generated__/endpoints/blocks/blocks";import type { BlockOutputResponse } from "@/app/api/__generated__/models/blockOutputResponse";import type { ErrorResponse } from "@/app/api/__generated__/models/errorResponse";import { ResponseType } from "@/app/api/__generated__/models/responseType";import type { SetupRequirementsResponse } from "@/app/api/__generated__/models/setupRequirementsResponse";+import { beautifyString } from "@/lib/utils";import {PlayCircleIcon,PlayIcon,@@ -149,18 +151,24 @@ export function getRunBlockToolOutput(return parseOutput((part as { output?: unknown }).output);}-export function getAnimationText(part: {- state: ToolUIPart["state"];- input?: unknown;- output?: unknown;-}): string {+export function getAnimationText(+ part: {+ state: ToolUIPart["state"];+ input?: unknown;+ output?: unknown;+ },+ blockNamesById?: Map<string, string>,+): string {const input = part.input as RunBlockInput | undefined;const blockName = input?.block_name?.trim();const blockId = input?.block_id?.trim();const isDryRun = input?.dry_run === true;- // Prefer block_name if available, otherwise fall back to block_id- const blockText = blockName- ? ` "${blockName}"`+ // Prefer block_name, then a name looked up from the blocks API,+ // and only fall back to the raw block_id when neither is known+ const displayName =+ blockName || (blockId ? blockNamesById?.get(blockId) : undefined);+ const blockText = displayName+ ? ` "${displayName}"`: blockId? ` "${blockId}"`: "";@@ -195,6 +203,21 @@ export function getAnimationText(part: {}}+export function getBlockNamesById(+ response: getV1ListAvailableBlocksResponse | undefined,+): Map<string, string> | undefined {+ if (response?.status !== 200) return undefined;+ const names = new Map<string, string>();+ for (const block of response.data) {+ if (typeof block.id === "string" && typeof block.name === "string") {+ // Same display treatment as the blocks menu: beautify and+ // drop the redundant "Block" suffix+ names.set(block.id, beautifyString(block.name).replace(/ Block$/, ""));+ }+ }+ return names;+}+export function ToolIcon({isStreaming,isError,