Skip to content

[Feature]: Stream thinking/reasoning to webchat clients during agent turns #5086

Description

@abra5umente

Summary

Webchat and other WebSocket clients can't display thinking/reasoning content during streaming - it only appears after the turn completes when history is reloaded.

Current Behavior

  1. Agent has extended thinking enabled (e.g., thinkingLevel: "low")
  2. During streaming, assistant events only contain text/delta - no thinking
  3. Thinking content is only available via chat.history after the turn completes
  4. Clients have to reload history post-completion to show thinking blocks

Expected Behavior

Thinking content should be emitted as agent events during streaming, allowing clients to display it in real-time (like claude.ai does).

Proposed Solution

In src/agents/pi-embedded-subscribe.ts, the emitReasoningStream function currently only calls params.onReasoningStream. It should also emit an agent event:

const emitReasoningStream = (text: string) => {
  if (!state.streamReasoning || !params.onReasoningStream) return;
  const formatted = formatReasoningMessage(text);
  if (!formatted) return;
  if (formatted === state.lastStreamedReasoning) return;
  state.lastStreamedReasoning = formatted;
  
  // ADD: Emit as agent event for webchat/WS clients
  emitAgentEvent({
    runId: params.runId,
    stream: "thinking",  // or include in "assistant" with data.thinking
    data: { thinking: formatted },
  });
  
  void params.onReasoningStream({ text: formatted });
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions