-
Notifications
You must be signed in to change notification settings - Fork 11
fix(chat): merge consecutive reasoning steps into one pill #724
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
blove
wants to merge
2
commits into
main
Choose a base branch
from
fix/merge-reasoning-pills
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,7 @@ import { createPartialArgsBridge, type PartialArgsBridge } from '../../a2ui/part | |
| import { createA2uiSurfaceStore, type A2uiSurfaceStore } from '../../a2ui/surface-store'; | ||
| import { a2uiActionLabel } from '../../a2ui/action-label'; | ||
| import { messageContent } from '../shared/message-utils'; | ||
| import { formatDuration } from '../../utils/format-duration'; | ||
| import { CHAT_HOST_TOKENS, ensureChatRootStyles } from '../../styles/chat-tokens'; | ||
| import type { ChatRenderEvent } from './chat-render-event'; | ||
| import { CHAT_LIFECYCLE, type ChatLifecycle } from '../../lifecycle'; | ||
|
|
@@ -197,11 +198,19 @@ export function isPinned( | |
| [streaming]="agent().isLoading() && i === agent().messages().length - 1" | ||
| [current]="i === agent().messages().length - 1" | ||
| > | ||
| @if (message.reasoning) { | ||
| <!-- Reasoning is merged across a run of consecutive (tool- | ||
| separated) reasoning steps and rendered ONCE at the run's | ||
| first step as "Thought for {total} · {N} steps", so a | ||
| multi-step agent shows one compact pill instead of a | ||
| stack of "Thought for 1s" chips. Single-step turns render | ||
| a normal "Thought for {duration}" pill. --> | ||
| @if (message.reasoning && reasoningRunStart(i)) { | ||
| @let run = reasoningRun(i); | ||
| <chat-reasoning | ||
| [content]="message.reasoning" | ||
| [isStreaming]="isReasoningStreaming(message, i)" | ||
| [durationMs]="message.reasoningDurationMs" | ||
| [content]="run.content" | ||
| [isStreaming]="run.streaming" | ||
| [durationMs]="run.durationMs" | ||
| [label]="run.label" | ||
| /> | ||
| } | ||
| <chat-tool-calls [agent]="agent()" [message]="message" [excludeToolNames]="excludedToolNames()"> | ||
|
|
@@ -461,6 +470,59 @@ export class ChatComponent { | |
| return text.length === 0; | ||
| } | ||
|
|
||
| /** The nearest preceding assistant message (skipping hidden tool messages), or undefined. */ | ||
| private prevAssistant(msgs: Message[], index: number): Message | undefined { | ||
| for (let j = index - 1; j >= 0; j--) { | ||
| if (msgs[j].role === 'tool') continue; | ||
| return msgs[j].role === 'assistant' ? msgs[j] : undefined; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * True when message[index] starts a reasoning RUN — a maximal sequence of | ||
| * consecutive assistant reasoning steps separated only by (hidden) tool | ||
| * messages. The merged reasoning pill renders once, here. | ||
| */ | ||
| protected reasoningRunStart(index: number): boolean { | ||
| const msgs = this.agent().messages(); | ||
| if (!msgs[index]?.reasoning) return false; | ||
| return !this.prevAssistant(msgs, index)?.reasoning; | ||
| } | ||
|
|
||
| /** | ||
| * Aggregate the reasoning RUN starting at `index`: joins each step's | ||
| * reasoning, sums durations, counts steps, and computes the streaming flag | ||
| * and the merged label ("Thought for {total} · {N} steps" when N > 1). | ||
| */ | ||
| protected reasoningRun(index: number): { | ||
| content: string; | ||
| durationMs: number | undefined; | ||
| streaming: boolean; | ||
| label: string | undefined; | ||
| } { | ||
| const msgs = this.agent().messages(); | ||
| const steps: { msg: Message; idx: number }[] = []; | ||
| for (let j = index; j < msgs.length; j++) { | ||
| const m = msgs[j]; | ||
| if (m.role === 'tool') continue; // skip hidden tool messages | ||
| if (m.role === 'assistant' && m.reasoning) { steps.push({ msg: m, idx: j }); continue; } | ||
| break; // any other message ends the run | ||
| } | ||
| const content = steps.map((s) => s.msg.reasoning ?? '').filter(Boolean).join('\n\n'); | ||
| const durations = steps | ||
| .map((s) => s.msg.reasoningDurationMs) | ||
| .filter((d): d is number => typeof d === 'number'); | ||
| const durationMs = durations.length ? durations.reduce((a, b) => a + b, 0) : undefined; | ||
| const last = steps[steps.length - 1]; | ||
| const streaming = last ? this.isReasoningStreaming(last.msg, last.idx) : false; | ||
| const label = | ||
| steps.length > 1 | ||
| ? `Thought for ${formatDuration(durationMs ?? 0)} · ${steps.length} steps` | ||
| : undefined; | ||
| return { content, durationMs, streaming, label }; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test coverage —
These are good candidates for straight unit tests on the class methods (no template compile needed). |
||
|
|
||
| private readonly classifiers = new Map<string, ContentClassifier>(); | ||
| private readonly destroyRef = inject(DestroyRef); | ||
| private readonly injector = inject(Injector); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When every step has
reasoningDurationMs: undefined,durationMsisundefinedhere, sodurationMs ?? 0passes0toformatDuration, producing"Thought for <1s · N steps"—<1simplies it was fast, but the real meaning is "no timing data". Consider omitting the duration portion when unknown:Or at minimum document the fallback intent with a comment. (Low severity — timing data is almost always present in practice.)