π OpenHands: AI-Driven Development
feat: expand long command outputs inline (#1577)
Ash Clarked2cfffb7ef81
diff --git a/.pr/screenshots/command-output-collapsed.png b/.pr/screenshots/command-output-collapsed.pngnew file mode 100644index 0000000000..b92c3fb8ecBinary files /dev/null and b/.pr/screenshots/command-output-collapsed.png differdiff --git a/.pr/screenshots/command-output-expanded.png b/.pr/screenshots/command-output-expanded.pngnew file mode 100644index 0000000000..2d89328b28Binary files /dev/null and b/.pr/screenshots/command-output-expanded.png differdiff --git a/__tests__/components/features/chat/tool-visualizers/bash/bash.test.tsx b/__tests__/components/features/chat/tool-visualizers/bash/bash.test.tsxindex c9752530db..61138d058e 100644--- a/__tests__/components/features/chat/tool-visualizers/bash/bash.test.tsx+++ b/__tests__/components/features/chat/tool-visualizers/bash/bash.test.tsx@@ -1,7 +1,9 @@import { describe, it, expect } from "vitest";import { screen } from "@testing-library/react";+import userEvent from "@testing-library/user-event";import { SecurityRisk } from "#/types/agent-server/core";import { bashVisualizer } from "#/components/features/chat/tool-visualizers/bash/bash";+import { MAX_CONTENT_LENGTH } from "#/components/conversation-events/chat/event-content-helpers/shared";import {renderVisualizer,bashAction,@@ -43,10 +45,10 @@ describe("bashVisualizer", () => {});it("shows a placeholder when there is no output", () => {- renderVisualizer(<Body observation={bashObservation("", 0)} />);- expect(- screen.getByText("OBSERVATION$COMMAND_NO_OUTPUT"),- ).toBeInTheDocument();+ const { container } = renderVisualizer(+ <Body observation={bashObservation("", 0)} />,+ );+ expect(container).toHaveTextContent("OBSERVATION$COMMAND_NO_OUTPUT");});it("exposes a copy button for the command and the output", () => {@@ -65,6 +67,27 @@ describe("bashVisualizer", () => {expect(screen.getAllByTestId("copy-to-clipboard")).toHaveLength(1);});+ it("expands long command output inline", async () => {+ const user = userEvent.setup();+ const longOutput = `${"a".repeat(MAX_CONTENT_LENGTH)}tail-marker`;+ const { container } = renderVisualizer(+ <Body observation={bashObservation(longOutput, 0)} />,+ );++ expect(container).not.toHaveTextContent("tail-marker");++ await user.click(screen.getByRole("button", { name: "BUTTON$EXPAND" }));++ expect(container).toHaveTextContent("tail-marker");+ expect(+ screen.getByRole("button", { name: "BUTTON$COLLAPSE" }),+ ).toBeInTheDocument();++ await user.click(screen.getByRole("button", { name: "BUTTON$COLLAPSE" }));++ expect(container).not.toHaveTextContent("tail-marker");+ });+it("renders command and output for the terminal tool", () => {const { container } = renderVisualizer(<Body observation={terminalObservation("362 index.html", 0, "wc -l")} />,diff --git a/src/components/features/chat/tool-visualizers/primitives/code-block.tsx b/src/components/features/chat/tool-visualizers/primitives/code-block.tsxindex 16556cb2f1..41dfe18384 100644--- a/src/components/features/chat/tool-visualizers/primitives/code-block.tsx+++ b/src/components/features/chat/tool-visualizers/primitives/code-block.tsx@@ -1,8 +1,10 @@import React from "react";+import { useTranslation } from "react-i18next";import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism";import { SyntaxHighlighter } from "../../../markdown/syntax-highlighter";import { CopyableContentWrapper } from "#/components/shared/buttons/copyable-content-wrapper";import { MAX_CONTENT_LENGTH } from "#/components/conversation-events/chat/event-content-helpers/shared";+import { I18nKey } from "#/i18n/declaration";interface CodeBlockProps {code: string;@@ -10,18 +12,40 @@ interface CodeBlockProps {language?: string;/** Show a copy button on hover. Defaults to true. */copy?: boolean;+ /** Text shown when code is empty. */+ placeholder?: string;+ /** Let truncated content be expanded inline. Defaults to false. */+ expandable?: boolean;+ /** Wrap long lines instead of horizontal-only scrolling. Defaults to false. */+ wrapLongLines?: boolean;}/*** Syntax-highlighted code block with an optional hover copy button. Long- * content is truncated to the same limit the markdown path uses; the copy- * button always yields the full, untruncated text.+ * content is truncated to the same limit the markdown path uses, optionally+ * with an inline expand control. The copy button always yields the full,+ * untruncated text.*/-export function CodeBlock({ code, language, copy = true }: CodeBlockProps) {+export function CodeBlock({+ code,+ language,+ copy = true,+ placeholder,+ expandable = false,+ wrapLongLines = false,+}: CodeBlockProps) {+ const { t } = useTranslation("openhands");+ const [isExpanded, setIsExpanded] = React.useState(false);+ const isTruncated = code.length > MAX_CONTENT_LENGTH;const display =- code.length > MAX_CONTENT_LENGTH+ isTruncated && !(expandable && isExpanded)? `${code.slice(0, MAX_CONTENT_LENGTH)}β¦`: code;+ const text = display.trim() || placeholder || "";+ const canCopy = copy && code.trim().length > 0;+ const toggleLabel = isExpanded+ ? t(I18nKey.BUTTON$COLLAPSE)+ : t(I18nKey.BUTTON$EXPAND);const block = (<SyntaxHighlighter@@ -29,14 +53,32 @@ export function CodeBlock({ code, language, copy = true }: CodeBlockProps) {style={vscDarkPlus}language={language}PreTag="div"+ wrapLongLines={wrapLongLines}+ customStyle={wrapLongLines ? { whiteSpace: "pre-wrap" } : undefined}+ codeTagProps={+ wrapLongLines ? { style: { whiteSpace: "pre-wrap" } } : undefined+ }>- {display}+ {text}</SyntaxHighlighter>);- return copy ? (- <CopyableContentWrapper text={code}>{block}</CopyableContentWrapper>- ) : (- block+ return (+ <div className="flex flex-col gap-1">+ {canCopy ? (+ <CopyableContentWrapper text={code}>{block}</CopyableContentWrapper>+ ) : (+ block+ )}+ {expandable && isTruncated && (+ <button+ type="button"+ onClick={() => setIsExpanded((prev) => !prev)}+ className="self-start text-xs text-muted transition-colors hover:text-white hover:underline"+ >+ {toggleLabel}+ </button>+ )}+ </div>);}diff --git a/src/components/features/chat/tool-visualizers/primitives/output-pane.tsx b/src/components/features/chat/tool-visualizers/primitives/output-pane.tsxindex 0fec6bc5b3..a6875e422f 100644--- a/src/components/features/chat/tool-visualizers/primitives/output-pane.tsx+++ b/src/components/features/chat/tool-visualizers/primitives/output-pane.tsx@@ -1,8 +1,7 @@import React from "react";import { useTranslation } from "react-i18next";import { I18nKey } from "#/i18n/declaration";-import { CopyableContentWrapper } from "#/components/shared/buttons/copyable-content-wrapper";-import { MAX_CONTENT_LENGTH } from "#/components/conversation-events/chat/event-content-helpers/shared";+import { CodeBlock } from "./code-block";interface OutputPaneProps {output: string;@@ -17,24 +16,13 @@ interface OutputPaneProps {/*** Monospace output block for command results, with a failure exit-code badge* and an optional hover copy button. Long content is truncated in the display- * to the same limit the markdown path uses; the copy button always yields the- * full, untruncated output.+ * to the same limit the markdown path uses, with an inline control to reveal+ * the full output. The copy button always yields the untruncated output.*/export function OutputPane({ output, exitCode, copy = true }: OutputPaneProps) {const { t } = useTranslation("openhands");- const display =- output.length > MAX_CONTENT_LENGTH- ? `${output.slice(0, MAX_CONTENT_LENGTH)}β¦`- : output;- const text = display.trim();const showExitBadge = exitCode != null && exitCode !== 0 && exitCode !== -1;- const pane = (- <pre className="overflow-auto whitespace-pre-wrap rounded border border-surface-raised bg-surface-raised p-2 text-xs text-foreground">- {text || t(I18nKey.OBSERVATION$COMMAND_NO_OUTPUT)}- </pre>- );-return (<div className="flex flex-col gap-1">{showExitBadge && (@@ -42,11 +30,14 @@ export function OutputPane({ output, exitCode, copy = true }: OutputPaneProps) {{t(I18nKey.OBSERVATION$EXIT_CODE, { code: exitCode })}</span>)}- {copy && text ? (- <CopyableContentWrapper text={output}>{pane}</CopyableContentWrapper>- ) : (- pane- )}+ <CodeBlock+ code={output}+ language="bash"+ copy={copy}+ placeholder={t(I18nKey.OBSERVATION$COMMAND_NO_OUTPUT)}+ expandable+ wrapLongLines+ /></div>);}