π OpenHands: AI-Driven Development
feat: stream OpenHands agent responses (#1474)
Vasco Schiavo94f4a0754e81
diff --git a/__tests__/api/agent-server-adapter.test.ts b/__tests__/api/agent-server-adapter.test.tsindex 18b3e13aa6..d55e061399 100644--- a/__tests__/api/agent-server-adapter.test.ts+++ b/__tests__/api/agent-server-adapter.test.ts@@ -106,6 +106,8 @@ describe("buildStartConversationRequest", () => {model: "nested-model",api_key: "nested-key",base_url: "https://nested.example.com",+ // Streaming is enabled so the OpenHands agent emits StreamingDeltaEvents.+ stream: true,});expect(payload.agent_settings.condenser).toEqual({enabled: true,@@ -183,6 +185,7 @@ describe("buildStartConversationRequest", () => {expect(payload.agent_settings.llm).toEqual({model: "gpt-5.2-codex",+ stream: true,auth_type: LLM_AUTH_TYPE_SUBSCRIPTION,subscription_vendor: OPENAI_SUBSCRIPTION_VENDOR,});diff --git a/__tests__/api/agent-server-conversation-service.test.ts b/__tests__/api/agent-server-conversation-service.test.tsindex 22fa9305fa..5cfa169c72 100644--- a/__tests__/api/agent-server-conversation-service.test.ts+++ b/__tests__/api/agent-server-conversation-service.test.ts@@ -775,6 +775,8 @@ describe("AgentServerConversationService", () => {expect.objectContaining({model: "openhands/claude-haiku-4-5",api_key: "encrypted-key",+ // Streaming must stay enabled after a mid-conversation switch.+ stream: true,usage_id: expect.stringMatching(/^profile:haiku:/),}),);diff --git a/__tests__/components/conversation-events/chat/event-thought-helpers.test.ts b/__tests__/components/conversation-events/chat/event-thought-helpers.test.tsnew file mode 100644index 0000000000..3b5f8064d5--- /dev/null+++ b/__tests__/components/conversation-events/chat/event-thought-helpers.test.ts@@ -0,0 +1,86 @@+import { describe, it, expect } from "vitest";+import { splitInlineThink } from "#/components/conversation-events/chat/event-thought-helpers";++describe("splitInlineThink", () => {+ it("returns content unchanged when there is no <think> block", () => {+ expect(splitInlineThink("Hello! How can I help?")).toEqual({+ reasoning: "",+ message: "Hello! How can I help?",+ });+ });++ it("extracts a leading closed <think> block and keeps the trailing message", () => {+ const content =+ "<think>The user wants a greeting. Simple.</think>\n\n\nHello!";+ expect(splitInlineThink(content)).toEqual({+ reasoning: "The user wants a greeting. Simple.",+ message: "Hello!",+ });+ });++ it("handles leading whitespace before the <think> block", () => {+ expect(splitInlineThink("\n <think>thinking</think>\n\nHi")).toEqual({+ reasoning: "thinking",+ message: "Hi",+ });+ });++ it("returns an empty message when the content is reasoning only", () => {+ expect(splitInlineThink("<think>just thinking</think>")).toEqual({+ reasoning: "just thinking",+ message: "",+ });+ });++ // Regression: an agent that literally emits "<think>" as its finalized+ // answer (no closing tag) must render verbatim, not vanish into Thinking.+ it("renders a finalized unclosed leading <think> verbatim", () => {+ expect(splitInlineThink("<think>")).toEqual({+ reasoning: "",+ message: "<think>",+ });+ });++ // Mid-stream, an unclosed leading <think> is reasoning still arriving, so+ // it is hidden until </think> shows up.+ it("treats an unclosed leading <think> as reasoning while streaming", () => {+ expect(+ splitInlineThink("<think>The user is asking me to", { streaming: true }),+ ).toEqual({ reasoning: "The user is asking me to", message: "" });+ });++ // Only the first block is peeled; a <think> that is part of the answer+ // (after </think>) is preserved verbatim.+ it("preserves a <think> that appears after the reasoning block", () => {+ expect(splitInlineThink("<think>reasoning</think>\n\n<think>")).toEqual({+ reasoning: "reasoning",+ message: "<think>",+ });+ });++ // Regression (reviewer): a <think> that is NOT at the start must be+ // preserved verbatim β only litellm's leading reasoning block is extracted.+ it("leaves a mid-message <think> reference untouched", () => {+ const content = "You can wrap reasoning in <think> and </think> tags.";+ expect(splitInlineThink(content)).toEqual({+ reasoning: "",+ message: content,+ });+ });++ it("leaves an unclosed mid-message <think> untouched", () => {+ const content = "See the <think> tag for details.";+ expect(splitInlineThink(content)).toEqual({+ reasoning: "",+ message: content,+ });+ });++ it("does not extract a <think> block that follows real message text", () => {+ const content = "Here is an example: <think>not reasoning</think> done.";+ expect(splitInlineThink(content)).toEqual({+ reasoning: "",+ message: content,+ });+ });+});diff --git a/src/api/agent-server-adapter.ts b/src/api/agent-server-adapter.tsindex 22fcefdcc7..2e4f572bd6 100644--- a/src/api/agent-server-adapter.ts+++ b/src/api/agent-server-adapter.ts@@ -750,6 +750,10 @@ function buildConfiguredOpenHandsAgentSettings(? llm.model: DEFAULT_SETTINGS.llm_model;+ // Stream assistant tokens (parity with ACP agents). The agent-server only+ // emits StreamingDeltaEvents for SDK LLM agents when an LLM has stream=True.+ llm.stream = true;+const apiKey = normalizeSecretString(llm.api_key);if (apiKey) {llm.api_key = apiKey;diff --git a/src/api/conversation-service/agent-server-conversation-service.api.ts b/src/api/conversation-service/agent-server-conversation-service.api.tsindex 8505aa7e80..8ee737d268 100644--- a/src/api/conversation-service/agent-server-conversation-service.api.ts+++ b/src/api/conversation-service/agent-server-conversation-service.api.ts@@ -715,6 +715,9 @@ class AgentServerConversationService {await conversationClient.switchLLM(conversationId, {...profile.config,model,+ // Keep streaming on after a switch (parity with conversation start);+ // the profile config would otherwise default it to stream=False.+ stream: true,// Avoid stale first-write-wins entries in the backend LLM registry.usage_id: `profile:${profileName}:${uuidv4()}`,} as LLMConfig);diff --git a/src/components/conversation-events/chat/event-message-components/user-assistant-event-message.tsx b/src/components/conversation-events/chat/event-message-components/user-assistant-event-message.tsxindex 33233ac7f9..98c210d673 100644--- a/src/components/conversation-events/chat/event-message-components/user-assistant-event-message.tsx+++ b/src/components/conversation-events/chat/event-message-components/user-assistant-event-message.tsx@@ -5,6 +5,8 @@ import { ImageCarousel } from "../../../features/images/image-carousel";import { ConversationConfirmationButtons } from "#/components/shared/buttons/conversation-confirmation-buttons";import { parseMessageFromEvent } from "../event-content-helpers/parse-message-from-event";import { CriticResultDisplay } from "./critic-result-display";+import { CollapsibleThinking } from "./collapsible-thinking";+import { splitInlineThink } from "../event-thought-helpers";interface UserAssistantEventMessageProps {event: MessageEvent;@@ -17,7 +19,13 @@ export function UserAssistantEventMessage({isLastMessage,isFromPlanningAgent,}: UserAssistantEventMessageProps) {- const message = parseMessageFromEvent(event);+ const parsed = parseMessageFromEvent(event);+ // Route an inline <think> block (e.g. from a streamed reply) to the thinking+ // section so reloaded conversations match the live rendering.+ const { reasoning, message } =+ event.source === "agent"+ ? splitInlineThink(parsed)+ : { reasoning: "", message: parsed };const imageUrls: string[] = [];if (Array.isArray(event.llm_message.content)) {@@ -30,6 +38,7 @@ export function UserAssistantEventMessage({return (<>+ {reasoning && <CollapsibleThinking content={reasoning} />}<ChatMessagetype={event.source}message={message}diff --git a/src/components/conversation-events/chat/event-message.tsx b/src/components/conversation-events/chat/event-message.tsxindex f32185096d..e90c3e669b 100644--- a/src/components/conversation-events/chat/event-message.tsx+++ b/src/components/conversation-events/chat/event-message.tsx@@ -33,7 +33,7 @@ import { CollapsibleThinking } from "./event-message-components/collapsible-thinimport { HookExecutionEventMessage } from "./event-message-components/hook-execution-event-message";import { createSkillReadyEvent } from "./event-content-helpers/create-skill-ready-event";import { shouldShowPlanPreview } from "./hooks/use-plan-preview-events";-import { getReasoningContent } from "./event-thought-helpers";+import { getReasoningContent, splitInlineThink } from "./event-thought-helpers";interface EventMessageProps {event: OpenHandsEvent & { isFromPlanningAgent?: boolean };@@ -179,15 +179,21 @@ export function EventMessage({}if (isStreamingDeltaEvent(event)) {- const content = event.content ?? "";- const reasoningContent = event.reasoning_content ?? "";+ // Route an inline <think> block to the thinking section, not the bubble.+ const { reasoning: inlineThink, message } = splitInlineThink(+ event.content ?? "",+ { streaming: true },+ );+ const reasoningContent = [event.reasoning_content ?? "", inlineThink]+ .filter(Boolean)+ .join("\n\n");return (<>{reasoningContent && <CollapsibleThinking content={reasoningContent} />}- {content && (+ {message && (<ChatMessagetype="agent"- message={content}+ message={message}isFromPlanningAgent={isFromPlanningAgent}/>)}diff --git a/src/components/conversation-events/chat/event-thought-helpers.ts b/src/components/conversation-events/chat/event-thought-helpers.tsindex f9c7dcf295..101240af65 100644--- a/src/components/conversation-events/chat/event-thought-helpers.ts+++ b/src/components/conversation-events/chat/event-thought-helpers.ts@@ -43,6 +43,45 @@ export const getReasoningContent = (action: ActionEvent): string => {export const hasNonEmptyThought = (action: ActionEvent): boolean =>getActionThoughtText(action).trim().length > 0;+/**+ * Splits a leading `<think>β¦</think>` reasoning block out of assistant content+ * so it renders in the collapsible thinking section, not the message bubble.+ * Some models stream reasoning inline instead of via `reasoning_content`.+ *+ * Conservative to avoid mangling normal messages: only a `<think>` at the very+ * start is touched (later occurrences, e.g. quoted in docs, stay verbatim),+ * only the first block is peeled, and an unclosed leading `<think>` is reasoning+ * only while `streaming` β in a finalized message it's literal output.+ */+export const splitInlineThink = (+ content: string,+ options?: { streaming?: boolean },+): { reasoning: string; message: string } => {+ const OPEN = "<think>";+ const CLOSE = "</think>";++ // Only a <think> at the very start is reasoning.+ const leading = content.replace(/^\s+/, "");+ if (!leading.startsWith(OPEN)) {+ return { reasoning: "", message: content };+ }++ const afterOpen = leading.slice(OPEN.length);+ const close = afterOpen.indexOf(CLOSE);++ if (close === -1) {+ // Unclosed: reasoning-in-progress while streaming, else literal output.+ return options?.streaming+ ? { reasoning: afterOpen.trim(), message: "" }+ : { reasoning: "", message: content };+ }++ return {+ reasoning: afterOpen.slice(0, close).trim(),+ message: afterOpen.slice(close + CLOSE.length).trim(),+ };+};+/*** Find the `ActionEvent` whose thought should be rendered alongside the* given UI event. For an `ActionEvent` the thought belongs to itself; for