Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(chat): see the agent's thinking live, in the order it happens (#16705)

ziyadev3fae9b1b71df
diff --git a/packages/web/src/app/routes/chat-with-ai/components/activity-accordion.tsx b/packages/web/src/app/routes/chat-with-ai/components/activity-accordion.tsx
index 8cea00328ba..252fa8de693 100644
--- a/packages/web/src/app/routes/chat-with-ai/components/activity-accordion.tsx
+++ b/packages/web/src/app/routes/chat-with-ai/components/activity-accordion.tsx
@@ -20,13 +20,11 @@ import { cn } from '@/lib/utils';
export function ThinkingBlock({
thinkingSteps,
- reasoningText,
isStreaming,
thinkingDurationMs,
onOpenChange,
}: {
thinkingSteps: ThinkingStep[];
- reasoningText: string;
isStreaming: boolean;
thinkingDurationMs?: number;
onOpenChange?: (open: boolean) => void;
@@ -41,10 +39,9 @@ export function ThinkingBlock({
[onOpenChange],
);
- const hasReasoning = reasoningText.length > 0;
const hasSteps = thinkingSteps.length > 0;
- if (!hasSteps && !hasReasoning && !isStreaming) return null;
+ if (!hasSteps && !isStreaming) return null;
const isExpandable = hasSteps;
@@ -104,6 +101,12 @@ export function ThinkingBlock({
function StepRenderer({ step }: { step: ThinkingStep }) {
switch (step.kind) {
+ case 'reasoning':
+ return (
+ <p className="max-h-64 overflow-y-auto py-0.5 text-sm text-gray-11 whitespace-pre-wrap break-words">
+ {step.text}
+ </p>
+ );
case 'thinking-status':
return (
<div className="py-0.5">
diff --git a/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx b/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
index c7c57b8536e..4f08c74b1e6 100644
--- a/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
+++ b/packages/web/src/app/routes/chat-with-ai/components/assistant-message.tsx
@@ -45,6 +45,7 @@ import { CopyIconButton } from './copy-icon-button';
import { FeedbackButtons } from './feedback-buttons';
import { FlowBuildCard } from './flow-build-card';
import { GeneratedImageCard } from './generated-image-card';
+import { LiveReasoning } from './live-reasoning';
import { McpReconnectCard, McpReconnectData } from './mcp-reconnect-card';
import { DocumentPreview } from './previews/document-preview';
import { markdownPreviewComponents } from './previews/markdown-preview-components';
@@ -280,9 +281,7 @@ function MessageBlocks({
!hasSkeletonAfter &&
!hasBuildAfter;
const lastStep =
- block.steps.length > 0
- ? block.steps[block.steps.length - 1]
- : null;
+ block.steps.findLast((step) => step.kind !== 'reasoning') ?? null;
// Collapsed view = ONLY the last segment: the last thought and the
// tools after it. Earlier segments live in the expanded accordion.
// The thought lives in the first tool's `description`, so derive the
@@ -301,8 +300,11 @@ function MessageBlocks({
lastThinkingStatus ??
lastSegmentToolSteps[0]?.part.toolCallId ??
'segment';
+ const isLive = isMessageStreaming && !isAccordionOpen;
+ const showLiveReasoning =
+ isLive && block.trailingReasoningText.length > 0;
const showLiveGroup =
- isMessageStreaming && !isAccordionOpen && Boolean(lastStep);
+ isLive && Boolean(lastStep) && !showLiveReasoning;
return (
<div
key={`thinking-${i}`}
@@ -310,7 +312,6 @@ function MessageBlocks({
>
<ThinkingBlock
thinkingSteps={block.steps}
- reasoningText={block.reasoningText}
isStreaming={isMessageStreaming}
thinkingDurationMs={
i === lastThinkingIdx
@@ -350,6 +351,14 @@ function MessageBlocks({
</motion.div>
)}
</AnimatePresence>
+ <AnimatePresence initial={false}>
+ {showLiveReasoning && (
+ <LiveReasoning
+ key="live-reasoning"
+ text={block.trailingReasoningText}
+ />
+ )}
+ </AnimatePresence>
</div>
);
}
diff --git a/packages/web/src/app/routes/chat-with-ai/components/live-reasoning.tsx b/packages/web/src/app/routes/chat-with-ai/components/live-reasoning.tsx
new file mode 100644
index 00000000000..aaae38dfbd1
--- /dev/null
+++ b/packages/web/src/app/routes/chat-with-ai/components/live-reasoning.tsx
@@ -0,0 +1,57 @@
+import { motion, useReducedMotion, useSpring } from 'motion/react';
+import { useEffect, useRef } from 'react';
+
+import { StreamingText } from '@/components/prompt-kit/streaming-text';
+
+export function LiveReasoning({ text }: { text: string }) {
+ const viewportRef = useRef<HTMLDivElement>(null);
+ const contentRef = useRef<HTMLDivElement>(null);
+ const prefersReducedMotion = useReducedMotion();
+ const rollY = useSpring(0, ROLL_SPRING);
+
+ useEffect(() => {
+ const viewport = viewportRef.current;
+ const content = contentRef.current;
+ if (!viewport || !content) return;
+ const observer = new ResizeObserver(() => {
+ const target = Math.min(0, viewport.clientHeight - content.offsetHeight);
+ if (prefersReducedMotion) {
+ rollY.jump(target);
+ } else {
+ rollY.set(target);
+ }
+ });
+ observer.observe(content);
+ return () => observer.disconnect();
+ }, [rollY, prefersReducedMotion]);
+
+ return (
+ <motion.div
+ initial={{ height: 0, opacity: 0 }}
+ animate={{ height: 'auto', opacity: 1 }}
+ exit={{ height: 0, opacity: 0 }}
+ transition={
+ prefersReducedMotion
+ ? { duration: 0.12 }
+ : {
+ height: { duration: 0.3, ease: [0.32, 0.72, 0, 1] },
+ opacity: { duration: 0.2, ease: 'easeOut' },
+ }
+ }
+ style={{ overflow: 'hidden' }}
+ >
+ <div
+ ref={viewportRef}
+ className="relative mt-1.5 max-h-20 overflow-hidden text-sm leading-5 text-gray-11 break-words [mask-image:linear-gradient(to_bottom,transparent,black_30%,black_85%,transparent)]"
+ >
+ <motion.div ref={contentRef} style={{ y: rollY }} className="py-2">
+ <StreamingText text={text} className="[&_p]:my-0" />
+ </motion.div>
+ <div className="pointer-events-none absolute inset-x-0 top-0 h-6 backdrop-blur-xs [mask-image:linear-gradient(to_bottom,black,transparent)]" />
+ <div className="pointer-events-none absolute inset-x-0 bottom-0 h-4 backdrop-blur-xs [mask-image:linear-gradient(to_top,black,transparent)]" />
+ </div>
+ </motion.div>
+ );
+}
+
+const ROLL_SPRING = { stiffness: 40, damping: 16, mass: 1 };
diff --git a/packages/web/src/app/routes/chat-with-ai/lib/message-blocks.ts b/packages/web/src/app/routes/chat-with-ai/lib/message-blocks.ts
index 4348ceedcd1..5fe8eac887f 100644
--- a/packages/web/src/app/routes/chat-with-ai/lib/message-blocks.ts
+++ b/packages/web/src/app/routes/chat-with-ai/lib/message-blocks.ts
@@ -45,7 +45,11 @@ export function buildMessageBlocks({
function ensureThinking() {
if (!currentThinking) {
- currentThinking = { kind: 'thinking', steps: [], reasoningText: '' };
+ currentThinking = {
+ kind: 'thinking',
+ steps: [],
+ trailingReasoningText: '',
+ };
result.push(currentThinking);
}
return currentThinking;
@@ -72,6 +76,7 @@ export function buildMessageBlocks({
const description = pendingDescription;
pendingDescription = null;
thinking.steps.push({ kind: 'tool', part: p, description });
+ thinking.trailingReasoningText = '';
return description;
}
@@ -160,7 +165,15 @@ export function buildMessageBlocks({
} else if (p.type === 'reasoning') {
flushPendingDescription();
const thinking = ensureThinking();
- thinking.reasoningText += p.text;
+ if (p.text.length > 0) {
+ const lastStep = thinking.steps[thinking.steps.length - 1];
+ if (lastStep?.kind === 'reasoning') {
+ lastStep.text += p.text;
+ } else {
+ thinking.steps.push({ kind: 'reasoning', text: p.text });
+ }
+ thinking.trailingReasoningText += p.text;
+ }
} else if (chatPartUtils.isAnyToolPart(p)) {
const toolName = chatPartUtils.getToolPartName(p);
if (chatPartUtils.isThinkingStatusTool(toolName)) {
@@ -238,7 +251,7 @@ export function buildMessageBlocks({
if (isStreaming && !hasText) {
const hasThinkingBlock = result.some((b) => b.kind === 'thinking');
if (!hasThinkingBlock) {
- result.push({ kind: 'thinking', steps: [], reasoningText: '' });
+ result.push({ kind: 'thinking', steps: [], trailingReasoningText: '' });
}
}
@@ -246,7 +259,7 @@ export function buildMessageBlocks({
const cleaned = result.filter((block, idx) => {
if (block.kind !== 'thinking') return true;
if (idx === lastThinkingIdx && isStreaming) return true;
- return block.steps.length > 0 || block.reasoningText.length > 0;
+ return block.steps.length > 0;
});
return {
@@ -394,6 +407,7 @@ export function getLastThinkingSegment(steps: ThinkingStep[]): {
let thought: string | null = null;
for (let i = steps.length - 1; i >= 0; i--) {
const step = steps[i];
+ if (step.kind === 'reasoning') continue;
if (step.kind === 'thinking-status') {
thought = step.text;
break;
@@ -411,7 +425,7 @@ export type MessageBlock =
| {
kind: 'thinking';
steps: ThinkingStep[];
- reasoningText: string;
+ trailingReasoningText: string;
}
| { kind: 'text'; text: string }
| { kind: 'display-tool'; part: AnyToolPart }
diff --git a/packages/web/src/features/chat/lib/chat-types.ts b/packages/web/src/features/chat/lib/chat-types.ts
index 75c3f45bfb5..4d64e2938c3 100644
--- a/packages/web/src/features/chat/lib/chat-types.ts
+++ b/packages/web/src/features/chat/lib/chat-types.ts
@@ -299,6 +299,7 @@ export type CardSkeletonPhase = 'pending' | 'failed';
export type ThinkingStep =
| { kind: 'thinking-status'; text: string }
+ | { kind: 'reasoning'; text: string }
| { kind: 'tool'; part: AnyToolPart; description: string | null };
export type ToolStatus = 'running' | 'completed' | 'failed' | 'stopped';
diff --git a/packages/web/test/app/routes/chat-with-ai/lib/message-blocks.test.ts b/packages/web/test/app/routes/chat-with-ai/lib/message-blocks.test.ts
index df49e3c30d2..24327be2e80 100644
--- a/packages/web/test/app/routes/chat-with-ai/lib/message-blocks.test.ts
+++ b/packages/web/test/app/routes/chat-with-ai/lib/message-blocks.test.ts
@@ -142,16 +142,44 @@ describe('buildMessageBlocks — one accordion per segment', () => {
expect(blocks).toHaveLength(1);
expect(blocks[0].kind).toBe('thinking');
if (blocks[0].kind !== 'thinking') throw new Error('expected thinking');
- expect(blocks[0].reasoningText).toBe('firstsecond');
- // both rounds' tools live in one accordion; each carries the status that
- // immediately preceded it as its description.
- expect(blocks[0].steps).toHaveLength(2);
- expect(blocks[0].steps.every((s) => s.kind === 'tool')).toBe(true);
+ expect(blocks[0].trailingReasoningText).toBe('');
+ // both rounds live in one accordion, in the order they streamed; each tool
+ // carries the status that immediately preceded it as its description.
+ expect(blocks[0].steps.map((s) => s.kind)).toEqual([
+ 'reasoning',
+ 'tool',
+ 'reasoning',
+ 'tool',
+ ]);
expect(
- blocks[0].steps.map((s) => (s.kind === 'tool' ? s.description : null)),
+ blocks[0].steps.flatMap((s) =>
+ s.kind === 'tool' ? [s.description] : [],
+ ),
).toEqual(['Looking things up', 'Checking connections']);
});
+ it('keeps only the reasoning after the last tool as the live reasoning', () => {
+ const blocks = buildMessageBlocks({
+ parts: [
+ reasoning('first'),
+ tool({ name: 'ap_research_pieces', id: 'a' }),
+ reasoning('second '),
+ reasoning('round'),
+ ],
+ isStreaming: true,
+ toolCallMeta: {},
+ claimedBuildIds: new Set(),
+ }).blocks;
+
+ if (blocks[0].kind !== 'thinking') throw new Error('expected thinking');
+ expect(blocks[0].trailingReasoningText).toBe('second round');
+ expect(blocks[0].steps.map((s) => s.kind)).toEqual([
+ 'reasoning',
+ 'tool',
+ 'reasoning',
+ ]);
+ });
+
it('starts a new accordion only after a real text part', () => {
expect(
kinds([
@@ -440,7 +468,7 @@ describe('buildMessageBlocks — one accordion per segment', () => {
expect(children.some((c) => c.kind === 'thinking')).toBe(true);
const thinking = children.find((c) => c.kind === 'thinking');
if (thinking?.kind !== 'thinking') throw new Error('expected thinking');
- expect(thinking.reasoningText).toBe('all good');
+ expect(thinking.trailingReasoningText).toBe('all good');
expect(thinking.steps.filter((s) => s.kind === 'tool')).toHaveLength(1);
});