Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(frontend): Fix ThinkAction rendering showing raw JSON in chat (#13981)

Xingyao Wang3fa9b84aa495
diff --git a/frontend/__tests__/components/v1/chat/event-message-think-action.test.tsx b/frontend/__tests__/components/v1/chat/event-message-think-action.test.tsx
new file mode 100644
index 0000000000..0f8ee73383
--- /dev/null
+++ b/frontend/__tests__/components/v1/chat/event-message-think-action.test.tsx
@@ -0,0 +1,166 @@
+import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
+import { screen } from "@testing-library/react";
+import { EventMessage } from "#/components/v1/chat/event-message";
+import { useAgentState } from "#/hooks/use-agent-state";
+import { AgentState } from "#/types/agent-state";
+import { ActionEvent, SecurityRisk } from "#/types/v1/core";
+import { ThinkAction, ExecuteBashAction } from "#/types/v1/core/base/action";
+import { renderWithProviders } from "test-utils";
+
+// Mock useConfig
+vi.mock("#/hooks/query/use-config", () => ({
+ useConfig: () => ({
+ data: { APP_MODE: "saas" },
+ }),
+}));
+
+// Mock useAgentState
+vi.mock("#/hooks/use-agent-state");
+
+// Mock useConversationId
+vi.mock("#/hooks/use-conversation-id", () => ({
+ useConversationId: () => ({ conversationId: "test-conversation-id" }),
+}));
+
+const createThinkActionEvent = (
+ id: string,
+ thought: string,
+): ActionEvent<ThinkAction> => ({
+ id,
+ timestamp: new Date().toISOString(),
+ source: "agent",
+ thought: [
+ {
+ type: "text",
+ text: `think: {"thought": "${thought}"}`,
+ },
+ ],
+ thinking_blocks: [],
+ action: {
+ kind: "ThinkAction",
+ thought,
+ },
+ tool_name: "think",
+ tool_call_id: `call_think_${id}`,
+ tool_call: {
+ id: `call_think_${id}`,
+ type: "function",
+ function: {
+ name: "think",
+ arguments: JSON.stringify({ thought }),
+ },
+ },
+ llm_response_id: `response_${id}`,
+ security_risk: SecurityRisk.UNKNOWN,
+});
+
+const createBashActionEvent = (
+ id: string,
+ command: string,
+ thoughtText: string,
+): ActionEvent<ExecuteBashAction> => ({
+ id,
+ timestamp: new Date().toISOString(),
+ source: "agent",
+ thought: [{ type: "text", text: thoughtText }],
+ thinking_blocks: [],
+ action: {
+ kind: "ExecuteBashAction",
+ command,
+ is_input: false,
+ timeout: null,
+ reset: false,
+ },
+ tool_name: "execute_bash",
+ tool_call_id: `call_bash_${id}`,
+ tool_call: {
+ id: `call_bash_${id}`,
+ type: "function",
+ function: {
+ name: "execute_bash",
+ arguments: JSON.stringify({ command }),
+ },
+ },
+ llm_response_id: `response_${id}`,
+ security_risk: SecurityRisk.UNKNOWN,
+});
+
+describe("EventMessage - ThinkAction rendering", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.mocked(useAgentState).mockReturnValue({
+ curAgentState: AgentState.INIT,
+ });
+ });
+
+ afterEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("should NOT render raw tool call text for ThinkAction events", () => {
+ const thinkEvent = createThinkActionEvent(
+ "think-1",
+ "Let me analyze the problem",
+ );
+
+ renderWithProviders(
+ <EventMessage
+ event={thinkEvent}
+ messages={[thinkEvent]}
+ isLastMessage={false}
+ isInLast10Actions={false}
+ />,
+ );
+
+ // The raw tool call text should NOT be displayed
+ expect(
+ screen.queryByText(/think: \{"thought":/),
+ ).not.toBeInTheDocument();
+ });
+
+ it("should render ThinkAction thought as a normal chat message", () => {
+ const thinkEvent = createThinkActionEvent(
+ "think-2",
+ "Let me analyze the problem",
+ );
+
+ renderWithProviders(
+ <EventMessage
+ event={thinkEvent}
+ messages={[thinkEvent]}
+ isLastMessage={false}
+ isInLast10Actions={false}
+ />,
+ );
+
+ // The thought content should be displayed as regular text
+ expect(
+ screen.getByText("Let me analyze the problem"),
+ ).toBeInTheDocument();
+
+ // It should NOT be inside a collapsible block (no expand button)
+ expect(screen.queryByLabelText("Expand")).not.toBeInTheDocument();
+ });
+
+ it("should render ThoughtEventMessage for non-ThinkAction events", () => {
+ const bashEvent = createBashActionEvent(
+ "bash-1",
+ "echo hello",
+ "I need to run a command",
+ );
+
+ renderWithProviders(
+ <EventMessage
+ event={bashEvent}
+ messages={[bashEvent]}
+ isLastMessage={false}
+ isInLast10Actions={false}
+ />,
+ );
+
+ // The thought should be displayed for non-think actions
+ expect(
+ screen.getByText("I need to run a command"),
+ ).toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/components/v1/chat/event-message.tsx b/frontend/src/components/v1/chat/event-message.tsx
index c6e1415122..42841b319b 100644
--- a/frontend/src/components/v1/chat/event-message.tsx
+++ b/frontend/src/components/v1/chat/event-message.tsx
@@ -1,6 +1,6 @@
import React from "react";
import { OpenHandsEvent, MessageEvent, ActionEvent } from "#/types/v1/core";
-import { FinishAction } from "#/types/v1/core/base/action";
+import { FinishAction, ThinkAction } from "#/types/v1/core/base/action";
import {
isActionEvent,
isObservationEvent,
@@ -13,6 +13,8 @@ import { useConfig } from "#/hooks/query/use-config";
import { useConversationStore } from "#/stores/conversation-store";
import { useAgentState } from "#/hooks/use-agent-state";
import { AgentState } from "#/types/agent-state";
+import { ChatMessage } from "../../features/chat/chat-message";
+import { PlanPreview } from "../../features/chat/plan-preview";
import {
ErrorEventMessage,
UserAssistantEventMessage,
@@ -22,7 +24,6 @@ import {
HookExecutionEventMessage,
} from "./event-message-components";
import { createSkillReadyEvent } from "./event-content-helpers/create-skill-ready-event";
-import { PlanPreview } from "../../features/chat/plan-preview";
import { shouldShowPlanPreview } from "./hooks/use-plan-preview-events";
interface EventMessageProps {
@@ -161,6 +162,20 @@ export function EventMessage({
);
}
+ // ThinkAction - render the thought as a normal chat message (not a collapsible block)
+ // The thought content IS the action, so we use event.action.thought directly
+ // instead of event.thought (which contains the raw tool call text).
+ if (isActionEvent(event) && event.action.kind === "ThinkAction") {
+ const thinkAction = event as ActionEvent<ThinkAction>;
+ return (
+ <ChatMessage
+ type="agent"
+ message={thinkAction.action.thought}
+ isFromPlanningAgent={isFromPlanningAgent}
+ />
+ );
+ }
+
// Action events - render thought + action (will be replaced by thought + observation)
if (isActionEvent(event)) {
return (
@@ -208,9 +223,15 @@ export function EventMessage({
(msg) => isActionEvent(msg) && msg.id === event.action_id,
);
+ // Skip ThoughtEventMessage for ThinkAction (thought IS the action)
+ const shouldShowThought =
+ correspondingAction &&
+ isActionEvent(correspondingAction) &&
+ correspondingAction.action.kind !== "ThinkAction";
+
return (
<>
- {correspondingAction && isActionEvent(correspondingAction) && (
+ {shouldShowThought && (
<ThoughtEventMessage
event={correspondingAction}
isFromPlanningAgent={isFromPlanningAgent}