@causyne/agent 1.20.45 → 1.20.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,139 @@
1
+ // Blocks: a left accent bar and a filled surface, the shape that makes a run of
2
+ // terminal rows read as ONE thing.
3
+ //
4
+ // Causyne's chat printed every kind of row the same way — the user's message, the
5
+ // agent's reply, an aside from the loop and the prompt itself were all just
6
+ // lines in the scrollback, distinguishable only by a leading glyph. On a busy
7
+ // turn there was no visual seam anywhere, which is the difference the reference
8
+ // tools actually make. opencode gives each of those a left border in the agent's
9
+ // colour and a background one step off the terminal's own
10
+ // (component/prompt/index.tsx:1353 — `border={["left"]}` + `borderColor` +
11
+ // `backgroundColor={theme.backgroundElement}` + `paddingLeft={2}`).
12
+ //
13
+ // Two properties make it work, and both are easy to get wrong:
14
+ //
15
+ // * EVERY ROW IS PADDED TO THE SAME WIDTH. A background only reads as a surface
16
+ // if it runs past the last character; ragged rows read as highlighted text.
17
+ // * THE PADDING IS MEASURED, NOT COUNTED. padEnd() on a coloured string pads by
18
+ // byte length, so the escape sequences count toward the column and the right
19
+ // edge of the block frays. text.js padEnd() measures visible width.
20
+ //
21
+ // It degrades in two steps rather than one: without truecolor/256 there is no
22
+ // fill and the bar alone carries the block; without colour at all the bar is an
23
+ // ASCII `|`. A transcript piped to a file stays clean either way.
24
+
25
+ // @ts-expect-error theme.js has no TypeScript declaration yet.
26
+ import { paint, fill, dim, GLYPHS, HAS_COLOR } from './theme.js';
27
+ import { visibleWidth, padEnd, truncate, wrap } from './text.js';
28
+
29
+ // Columns between the bar and the text. opencode uses paddingLeft={2} inside its
30
+ // bordered box; one column belongs to the bar, so the gutter is one more.
31
+ const GUTTER = 1;
32
+
33
+ export interface BarBlockOptions {
34
+ width?: number;
35
+ role?: string;
36
+ filled?: boolean;
37
+ surface?: string;
38
+ punctuate?: boolean;
39
+ }
40
+
41
+ /**
42
+ * Render `lines` as one block with a left accent bar.
43
+ * Uses a continuous unbroken box-drawing heavy rule (barHeavy '┃')
44
+ * that dynamically spans the exact length of lines without gaps or bullet interruptions.
45
+ *
46
+ * @param lines already-styled rows; NOT re-wrapped (see blockOf)
47
+ * @param options configuration options
48
+ * @returns rows, each exactly `width` columns wide
49
+ */
50
+ export function barBlock(lines: string[], {
51
+ width = 80,
52
+ role = 'accent',
53
+ filled = true,
54
+ surface = 'surface',
55
+ punctuate = false
56
+ }: BarBlockOptions = {}): string[] {
57
+ // Use continuous heavy vertical line ('┃') for clean, solid, unbroken alignment.
58
+ // Box-drawing characters connect seamlessly across lines in all terminal fonts without line-height leading gaps.
59
+ const glyph = GLYPHS.barHeavy || GLYPHS.bar || '┃';
60
+
61
+ const barWidth = visibleWidth(glyph);
62
+ const body = Math.max(1, width - barWidth - GUTTER);
63
+ const bar = paint(role, glyph);
64
+ const gutter = ' '.repeat(GUTTER);
65
+
66
+ return lines.map((line) => {
67
+ // Clip first, then pad: a line longer than the block would otherwise push
68
+ // the fill past the right edge and wrap, which is the failure ChatPrompt
69
+ // clampRows() exists to prevent one layer up.
70
+ const clipped = visibleWidth(line) > body ? truncate(line, body, GLYPHS.ellipsis) : line;
71
+ const padded = padEnd(clipped, body);
72
+ const inner = filled ? fill(surface, gutter + padded) : gutter + padded;
73
+ return bar + inner;
74
+ });
75
+ }
76
+
77
+ /**
78
+ * Wrap `text` to the block's inner width, then render it as a block.
79
+ *
80
+ * The convenience form for plain prose. Callers that have already styled their
81
+ * rows individually (the prompt editor, a status row) use barBlock directly.
82
+ */
83
+ export function blockOf(text: string, options: BarBlockOptions = {}): string[] {
84
+ const width = options.width || 80;
85
+ const glyph = GLYPHS.barHeavy || GLYPHS.bar || '┃';
86
+ const inner = Math.max(1, width - visibleWidth(glyph) - GUTTER);
87
+ const lines = wrap(String(text == null ? '' : text), inner);
88
+ return barBlock(lines.length ? lines : [''], options);
89
+ }
90
+
91
+ export interface PanelOptions {
92
+ width?: number;
93
+ title?: string;
94
+ role?: string;
95
+ }
96
+
97
+ /**
98
+ * A bordered panel with its title inset into the top border.
99
+ */
100
+ export function panel(rows: string[], { width = 80, title = '', role = 'rule' }: PanelOptions = {}): string[] {
101
+ const inner = Math.max(4, width - 2);
102
+ const h = GLYPHS.hLine;
103
+ const label = title ? ` ${title} ` : '';
104
+ const clippedLabel = visibleWidth(label) > inner - 2 ? '' : label;
105
+ const lead = clippedLabel ? h : '';
106
+ const trail = h.repeat(Math.max(0, inner - visibleWidth(clippedLabel) - visibleWidth(lead)));
107
+
108
+ const top = paint(role, GLYPHS.topLeft + lead)
109
+ + (clippedLabel ? paint('accent', clippedLabel) : '')
110
+ + paint(role, trail + GLYPHS.topRight);
111
+ const bottom = paint(role, GLYPHS.bottomLeft + h.repeat(inner) + GLYPHS.bottomRight);
112
+
113
+ const side = paint(role, GLYPHS.vLine);
114
+ const middle = rows.map((row) => {
115
+ const clipped = visibleWidth(row) > inner ? truncate(row, inner, GLYPHS.ellipsis) : row;
116
+ return side + padEnd(clipped, inner) + side;
117
+ });
118
+
119
+ return [top, ...middle, bottom];
120
+ }
121
+
122
+ /**
123
+ * `9.2K (5%)` — what the context window has left, at a glance.
124
+ */
125
+ export function contextMeter(usedTokens: number, limitTokens: number): string {
126
+ const used = Number(usedTokens) || 0;
127
+ const limit = Number(limitTokens) || 0;
128
+ if (used <= 0) return '';
129
+ const compact = used < 1000
130
+ ? String(Math.round(used))
131
+ : used < 1000000
132
+ ? `${(used / 1000).toFixed(used < 10000 ? 1 : 0)}K`
133
+ : `${(used / 1000000).toFixed(1)}M`;
134
+ if (limit <= 0) return compact;
135
+ return `${compact} (${Math.round((used / limit) * 100)}%)`;
136
+ }
137
+
138
+ export { GUTTER };
139
+
@@ -14,8 +14,9 @@ export class ChatRenderer {
14
14
  _activeReasoning?: boolean;
15
15
  handle(message: { type: string; [key: string]: unknown }): boolean;
16
16
  resetTurn(): void;
17
- onAgentReasoning(message: { content?: string; streaming?: boolean; elapsedMs?: number }): void;
18
- renderReasoning(content: string, options?: { streaming?: boolean; durationText?: string; collapsed?: boolean; startLine?: number }): void;
17
+ turnFooter(durationMs?: number): void;
18
+ onAgentReasoning(message: { content?: string; streaming?: boolean; elapsedMs?: number; activity?: string; title?: string; completedTitle?: string }): void;
19
+ renderReasoning(content: string, options?: { streaming?: boolean; durationText?: string; collapsed?: boolean; startLine?: number; activity?: string; title?: string; completedTitle?: string }): void;
19
20
  scrollThinkingPanel?(key: unknown, surface: unknown): boolean;
20
21
  stopSpinner(): void;
21
22
  frame(kind: string, produce: () => void, meta?: Record<string, unknown> | null): void;
@@ -25,7 +25,7 @@ import { composerWidth, openingRows, sessionRows } from './layout.js';
25
25
  import { sessionComposerWidth } from './sessionSidebar.js';
26
26
  import { chatProseWidth, wrappedChatRows } from './textLayout.js';
27
27
  import { activityRows } from './activityLayout.js';
28
- import { cleanToolOutputPreview, normalizeHistoricalToolResult } from './toolOutputNormalizer.js';
28
+ import { cleanToolOutputPreview, normalizeHistoricalToolResult, normalizeHistoricalExternalAction } from './toolOutputNormalizer.js';
29
29
  import { formatMarkdownMath } from './markdownMath.js';
30
30
  import { renderThinkingFrame } from './thinkingBox.js';
31
31
 
@@ -505,8 +505,8 @@ export class ChatRenderer {
505
505
  const width = this.transcript
506
506
  ? sessionComposerWidth(this.width())
507
507
  : composerWidth(this.measure());
508
- const blockWidth = Math.max(8, width - 1);
509
- const innerWidth = Math.max(8, blockWidth - visibleWidth(GLYPHS.bar) - 1);
508
+ const blockWidth = Math.max(8, width);
509
+ const innerWidth = Math.max(8, blockWidth - visibleWidth(GLYPHS.barHeavy || GLYPHS.bar) - 1);
510
510
  const rows = barBlock(
511
511
  wrapBlock(safeDisplay(text), innerWidth),
512
512
  { width: blockWidth, role: 'user', filled: true, surface: 'surfaceHi' }
@@ -590,17 +590,40 @@ export class ChatRenderer {
590
590
  this.resetTurn();
591
591
  this.renderUserMessage(msg.content);
592
592
  } else if (role === 'assistant') {
593
- // If assistant has textual content, render it
594
- const text = typeof msg.content === 'string'
595
- ? msg.content
596
- : (Array.isArray(msg.content)
597
- ? msg.content.map(p => (typeof p === 'string' ? p : (p && p.text) || '')).filter(Boolean).join('\n')
598
- : String(msg.content || ''));
599
- if (text.trim()) {
600
- this.renderAssistantMessage(text);
593
+ // 1. Restore reasoning content as an auto-minimized thinking box
594
+ const reasoning = typeof msg.reasoning_content === 'string'
595
+ ? msg.reasoning_content.trim()
596
+ : (Array.isArray(msg.thinking_blocks)
597
+ ? msg.thinking_blocks.map(b => (typeof b === 'string' ? b : b?.thinking || b?.text || '')).filter(Boolean).join('\n').trim()
598
+ : '');
599
+ if (reasoning) {
600
+ const meta = {
601
+ reasoning: true,
602
+ done: true,
603
+ streaming: false,
604
+ collapsed: true,
605
+ durationText: '',
606
+ content: reasoning,
607
+ scrollStart: 0
608
+ };
609
+ this.frame('reasoning', () => this.renderReasoning(meta.content, {
610
+ streaming: false,
611
+ durationText: '',
612
+ collapsed: Boolean(meta.collapsed),
613
+ startLine: 0
614
+ }), meta);
615
+ }
616
+
617
+ // 2. Restore external / native ACP tool executions
618
+ if (Array.isArray(msg.external_actions) && msg.external_actions.length > 0) {
619
+ for (const ext of msg.external_actions) {
620
+ const { toolName, args, result } = normalizeHistoricalExternalAction(ext);
621
+ this.onStepStart({ tool: toolName, args });
622
+ this.onStepComplete({ tool: toolName, result, elapsed: '' });
623
+ }
601
624
  }
602
625
 
603
- // If assistant invoked tool calls, render each tool step and pair with its result
626
+ // 3. If assistant invoked tool calls, render each tool step and pair with its result
604
627
  if (Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0) {
605
628
  for (const call of msg.tool_calls) {
606
629
  const toolName = call.function?.name || call.name || 'tool';
@@ -647,6 +670,16 @@ export class ChatRenderer {
647
670
  }
648
671
  }
649
672
  }
673
+
674
+ // 4. If assistant has textual content, render it
675
+ const text = typeof msg.content === 'string'
676
+ ? msg.content
677
+ : (Array.isArray(msg.content)
678
+ ? msg.content.map(p => (typeof p === 'string' ? p : (p && p.text) || '')).filter(Boolean).join('\n')
679
+ : String(msg.content || ''));
680
+ if (text.trim()) {
681
+ this.renderAssistantMessage(text);
682
+ }
650
683
  } else if (role === 'tool') {
651
684
  // Standalone or un-paired tool message (e.g. from compacted session turns)
652
685
  if (!renderedToolMessages.has(msg)) {
@@ -737,6 +770,9 @@ export class ChatRenderer {
737
770
  const isStreaming = Boolean(msg.streaming);
738
771
  const elapsedMs = Number(msg.elapsedMs) || 0;
739
772
  const durationText = elapsedMs > 0 ? formatDuration(elapsedMs) : '';
773
+ const activity = typeof msg.activity === 'string' ? msg.activity : '';
774
+ const title = typeof msg.title === 'string' ? msg.title : '';
775
+ const completedTitle = typeof msg.completedTitle === 'string' ? msg.completedTitle : '';
740
776
 
741
777
  const tailFrame = this.transcript?.frames?.at(-1);
742
778
  const tailIsReasoning = !this.transcript
@@ -754,26 +790,38 @@ export class ChatRenderer {
754
790
  streaming: true,
755
791
  durationText,
756
792
  content,
793
+ activity,
794
+ title,
795
+ completedTitle,
757
796
  scrollStart: 0
758
797
  };
759
798
  this.frame('reasoning', () => this.renderReasoning(meta.content, {
760
- streaming: true,
799
+ streaming: Boolean(meta.streaming),
761
800
  durationText: meta.durationText,
762
801
  collapsed: Boolean(meta.collapsed),
763
- startLine: Number(meta.scrollStart) || 0
802
+ startLine: Number(meta.scrollStart) || 0,
803
+ activity: meta.activity,
804
+ title: meta.title,
805
+ completedTitle: meta.completedTitle
764
806
  }), meta);
765
807
  } else {
766
808
  const meta = tailFrame?.meta || { reasoning: true };
767
809
  meta.streaming = true;
768
810
  meta.durationText = durationText;
769
811
  meta.content = content;
812
+ if (activity) meta.activity = activity;
813
+ if (title) meta.title = title;
814
+ if (completedTitle) meta.completedTitle = completedTitle;
770
815
  if (meta.collapsed === undefined) meta.collapsed = false;
771
816
  if (meta.scrollStart === undefined) meta.scrollStart = 0;
772
817
  this.updateTailFrame('reasoning', () => this.renderReasoning(meta.content, {
773
- streaming: true,
818
+ streaming: Boolean(meta.streaming),
774
819
  durationText: meta.durationText,
775
820
  collapsed: Boolean(meta.collapsed),
776
- startLine: Number(meta.scrollStart) || 0
821
+ startLine: Number(meta.scrollStart) || 0,
822
+ activity: meta.activity,
823
+ title: meta.title,
824
+ completedTitle: meta.completedTitle
777
825
  }), meta);
778
826
  }
779
827
  return;
@@ -786,13 +834,19 @@ export class ChatRenderer {
786
834
  meta.done = true;
787
835
  meta.durationText = durationText;
788
836
  meta.content = content;
789
- if (meta.collapsed === undefined) meta.collapsed = false;
837
+ if (activity) meta.activity = activity;
838
+ if (title) meta.title = title;
839
+ if (completedTitle) meta.completedTitle = completedTitle;
840
+ if (meta.collapsed === undefined) meta.collapsed = Boolean(msg.collapsed);
790
841
  if (meta.scrollStart === undefined) meta.scrollStart = 0;
791
842
  this.updateTailFrame('reasoning', () => this.renderReasoning(meta.content, {
792
- streaming: false,
843
+ streaming: Boolean(meta.streaming),
793
844
  durationText: meta.durationText,
794
845
  collapsed: Boolean(meta.collapsed),
795
- startLine: Number(meta.scrollStart) || 0
846
+ startLine: Number(meta.scrollStart) || 0,
847
+ activity: meta.activity,
848
+ title: meta.title,
849
+ completedTitle: meta.completedTitle
796
850
  }), meta);
797
851
  } else if (content.trim()) {
798
852
  const meta = {
@@ -801,26 +855,35 @@ export class ChatRenderer {
801
855
  streaming: false,
802
856
  durationText,
803
857
  content,
804
- collapsed: false,
858
+ activity,
859
+ title,
860
+ completedTitle,
861
+ collapsed: Boolean(msg.collapsed),
805
862
  scrollStart: 0
806
863
  };
807
864
  this.frame('reasoning', () => this.renderReasoning(meta.content, {
808
- streaming: false,
865
+ streaming: Boolean(meta.streaming),
809
866
  durationText: meta.durationText,
810
867
  collapsed: Boolean(meta.collapsed),
811
- startLine: Number(meta.scrollStart) || 0
868
+ startLine: Number(meta.scrollStart) || 0,
869
+ activity: meta.activity,
870
+ title: meta.title,
871
+ completedTitle: meta.completedTitle
812
872
  }), meta);
813
873
  }
814
874
  }
815
875
 
816
- renderReasoning(content, { streaming = false, durationText = '', collapsed = false, startLine = 0 } = {}) {
876
+ renderReasoning(content, { streaming = false, durationText = '', collapsed = false, startLine = 0, activity = '', title = '', completedTitle = '' } = {}) {
817
877
  const width = this.measure();
818
878
  const rows = renderThinkingFrame(content, {
819
879
  width,
820
880
  streaming,
821
881
  durationText,
822
882
  collapsed,
823
- startLine: Number(startLine) || 0
883
+ startLine: Number(startLine) || 0,
884
+ activity,
885
+ title,
886
+ completedTitle
824
887
  });
825
888
 
826
889
  if (rows.length === 0) return;
@@ -10,6 +10,7 @@ export class CodeStreamRenderer extends ChatRenderer {
10
10
  activeCode = null;
11
11
  modelNote = '';
12
12
  lastReasoning = '';
13
+ currentActivity = 'thinking';
13
14
  codePanels = new WeakMap();
14
15
  /** Scroll only the code beneath the pointer; leave the chat viewport alone. */
15
16
  scrollCodePanel(key, surface) {
@@ -81,11 +82,85 @@ export class CodeStreamRenderer extends ChatRenderer {
81
82
  this.activeCode = null;
82
83
  this.modelNote = '';
83
84
  this.lastReasoning = '';
85
+ this.collapseFinishedReasoningFrames(true);
84
86
  super.resetTurn();
85
87
  }
88
+ turnFooter(durationMs) {
89
+ this.collapseFinishedReasoningFrames(true);
90
+ super.turnFooter(durationMs);
91
+ }
92
+ /**
93
+ * Auto-collapses completed thinking box frames when a session turn finishes,
94
+ * keeping the conversation viewport clean while preserving interactive click-to-expand.
95
+ */
96
+ collapseFinishedReasoningFrames(forceAll = false) {
97
+ const transcript = this.transcript;
98
+ if (!transcript || !Array.isArray(transcript.frames))
99
+ return;
100
+ let changed = false;
101
+ for (const frame of transcript.frames) {
102
+ if (frame?.meta?.reasoning === true && (forceAll || frame.meta.userInteracted !== true) && frame.meta.collapsed !== true) {
103
+ frame.meta.collapsed = true;
104
+ frame.meta.streaming = false;
105
+ frame.rows = null;
106
+ frame.renderedWidth = -1;
107
+ changed = true;
108
+ }
109
+ }
110
+ if (changed) {
111
+ transcript.cache = null;
112
+ transcript.revision++;
113
+ transcript.changed();
114
+ }
115
+ }
86
116
  handle(message) {
117
+ if (message?.type === 'agent_activity') {
118
+ const rawActivity = String(message.activity || message.message || '').trim();
119
+ if (rawActivity) {
120
+ this.currentActivity = rawActivity.replace(/…|\.\.\.$/, '').trim();
121
+ if (this._activeReasoning && this.lastReasoning) {
122
+ super.onAgentReasoning({
123
+ content: this.lastReasoning,
124
+ streaming: true,
125
+ activity: this.currentActivity,
126
+ title: rawActivity
127
+ });
128
+ return true;
129
+ }
130
+ }
131
+ }
87
132
  if (message?.type === 'notice' && message.nativeStep === true) {
88
133
  this.stepCount = (this.stepCount || 0) + 1;
134
+ const stepLine = String(message.message || '').trim();
135
+ if (stepLine) {
136
+ const actionName = String(message.actionName || stepLine).toLowerCase();
137
+ let activity = this.currentActivity || 'processing';
138
+ if (['read_file', 'fs_read', 'view_file', 'cat', 'grep', 'find', 'ls'].some(k => actionName.includes(k))) {
139
+ activity = 'analyzing';
140
+ }
141
+ else if (['run_command', 'sysrun', 'bash', 'terminal', 'sh', 'exec'].some(k => actionName.includes(k))) {
142
+ activity = 'executing';
143
+ }
144
+ else if (['write_file', 'edit_file', 'multiedit', 'replace_file_content', 'fs_write'].some(k => actionName.includes(k))) {
145
+ activity = 'editing';
146
+ }
147
+ else if (['web_search', 'web_fetch', 'browse'].some(k => actionName.includes(k))) {
148
+ activity = 'researching';
149
+ }
150
+ else if (['plan', 'todo'].some(k => actionName.includes(k))) {
151
+ activity = 'planning';
152
+ }
153
+ this.currentActivity = activity;
154
+ const currentContent = this.lastReasoning ? `${this.lastReasoning}\n${stepLine}` : stepLine;
155
+ this.lastReasoning = currentContent;
156
+ super.onAgentReasoning({
157
+ content: currentContent,
158
+ streaming: true,
159
+ activity,
160
+ title: `${activity}…`
161
+ });
162
+ return true;
163
+ }
89
164
  }
90
165
  if (message?.type === 'code_progress') {
91
166
  this.onCodeProgress(message);
@@ -104,21 +179,34 @@ export class CodeStreamRenderer extends ChatRenderer {
104
179
  const note = content.trim().split('\n').at(-1) || '';
105
180
  this.modelNote = note.slice(-140);
106
181
  this.paintCodePanel(this.activeCode);
107
- if (message.streaming === false)
182
+ if (message.streaming === false) {
108
183
  this.lastReasoning = '';
184
+ }
109
185
  return true;
110
186
  }
111
- const handled = super.handle(message);
112
- if (message.streaming === false)
187
+ const handled = super.handle({
188
+ ...message,
189
+ activity: message.activity || this.currentActivity
190
+ });
191
+ if (message.streaming === false) {
113
192
  this.lastReasoning = '';
193
+ }
114
194
  return handled;
115
195
  }
116
- if (['agent_message', 'agent_activity', 'notice', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')) {
196
+ if (['agent_message', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')
197
+ || (message?.type === 'notice' && !message.nativeStep)) {
117
198
  if (this._activeReasoning && this.lastReasoning) {
118
- super.onAgentReasoning({ content: this.lastReasoning, streaming: false });
199
+ super.onAgentReasoning({
200
+ content: this.lastReasoning,
201
+ streaming: false,
202
+ activity: this.currentActivity
203
+ });
119
204
  }
120
205
  this._activeReasoning = false;
121
206
  this.lastReasoning = '';
207
+ if (message?.type === 'task_complete' || (message?.type === 'agent_message' && message.streaming !== true)) {
208
+ this.collapseFinishedReasoningFrames(true);
209
+ }
122
210
  if (['step_start', 'task_complete', 'error'].includes(message?.type || '')
123
211
  || (message?.type === 'agent_message' && message.streaming !== true)) {
124
212
  this.activeCode = null;
@@ -29,6 +29,7 @@ export class CodeStreamRenderer extends ChatRenderer {
29
29
  private activeCode: CodeProgressEvent | null = null;
30
30
  private modelNote = '';
31
31
  private lastReasoning = '';
32
+ private currentActivity = 'thinking';
32
33
  private readonly codePanels = new WeakMap<CodeFrame, CodePanelState>();
33
34
 
34
35
  /** Scroll only the code beneath the pointer; leave the chat viewport alone. */
@@ -99,13 +100,88 @@ export class CodeStreamRenderer extends ChatRenderer {
99
100
  this.activeCode = null;
100
101
  this.modelNote = '';
101
102
  this.lastReasoning = '';
103
+ this.collapseFinishedReasoningFrames(true);
102
104
  super.resetTurn();
103
105
  }
104
106
 
107
+ override turnFooter(durationMs: number): void {
108
+ this.collapseFinishedReasoningFrames(true);
109
+ super.turnFooter(durationMs);
110
+ }
111
+
112
+ /**
113
+ * Auto-collapses completed thinking box frames when a session turn finishes,
114
+ * keeping the conversation viewport clean while preserving interactive click-to-expand.
115
+ */
116
+ collapseFinishedReasoningFrames(forceAll = false): void {
117
+ const transcript = this.transcript as unknown as CodeTranscript | null;
118
+ if (!transcript || !Array.isArray(transcript.frames)) return;
119
+ let changed = false;
120
+ for (const frame of transcript.frames) {
121
+ if (frame?.meta?.reasoning === true && (forceAll || frame.meta.userInteracted !== true) && frame.meta.collapsed !== true) {
122
+ frame.meta.collapsed = true;
123
+ frame.meta.streaming = false;
124
+ frame.rows = null;
125
+ frame.renderedWidth = -1;
126
+ changed = true;
127
+ }
128
+ }
129
+ if (changed) {
130
+ transcript.cache = null;
131
+ transcript.revision++;
132
+ transcript.changed();
133
+ }
134
+ }
135
+
105
136
  override handle(message: RendererEvent): boolean {
137
+ if (message?.type === 'agent_activity') {
138
+ const rawActivity = String((message as any).activity || (message as any).message || '').trim();
139
+ if (rawActivity) {
140
+ this.currentActivity = rawActivity.replace(/…|\.\.\.$/, '').trim();
141
+ if (this._activeReasoning && this.lastReasoning) {
142
+ super.onAgentReasoning({
143
+ content: this.lastReasoning,
144
+ streaming: true,
145
+ activity: this.currentActivity,
146
+ title: rawActivity
147
+ });
148
+ return true;
149
+ }
150
+ }
151
+ }
152
+
106
153
  if (message?.type === 'notice' && message.nativeStep === true) {
107
154
  this.stepCount = (this.stepCount || 0) + 1;
155
+ const stepLine = String(message.message || '').trim();
156
+ if (stepLine) {
157
+ const actionName = String((message as any).actionName || stepLine).toLowerCase();
158
+ let activity = this.currentActivity || 'processing';
159
+ if (['read_file', 'fs_read', 'view_file', 'cat', 'grep', 'find', 'ls'].some(k => actionName.includes(k))) {
160
+ activity = 'analyzing';
161
+ } else if (['run_command', 'sysrun', 'bash', 'terminal', 'sh', 'exec'].some(k => actionName.includes(k))) {
162
+ activity = 'executing';
163
+ } else if (['write_file', 'edit_file', 'multiedit', 'replace_file_content', 'fs_write'].some(k => actionName.includes(k))) {
164
+ activity = 'editing';
165
+ } else if (['web_search', 'web_fetch', 'browse'].some(k => actionName.includes(k))) {
166
+ activity = 'researching';
167
+ } else if (['plan', 'todo'].some(k => actionName.includes(k))) {
168
+ activity = 'planning';
169
+ }
170
+ this.currentActivity = activity;
171
+
172
+ const currentContent = this.lastReasoning ? `${this.lastReasoning}\n${stepLine}` : stepLine;
173
+ this.lastReasoning = currentContent;
174
+
175
+ super.onAgentReasoning({
176
+ content: currentContent,
177
+ streaming: true,
178
+ activity,
179
+ title: `${activity}…`
180
+ });
181
+ return true;
182
+ }
108
183
  }
184
+
109
185
  if (message?.type === 'code_progress') {
110
186
  this.onCodeProgress(message as unknown as CodeProgressEvent);
111
187
  return true;
@@ -122,19 +198,34 @@ export class CodeStreamRenderer extends ChatRenderer {
122
198
  const note = content.trim().split('\n').at(-1) || '';
123
199
  this.modelNote = note.slice(-140);
124
200
  this.paintCodePanel(this.activeCode);
125
- if (message.streaming === false) this.lastReasoning = '';
201
+ if (message.streaming === false) {
202
+ this.lastReasoning = '';
203
+ }
126
204
  return true;
127
205
  }
128
- const handled = super.handle(message);
129
- if (message.streaming === false) this.lastReasoning = '';
206
+ const handled = super.handle({
207
+ ...message,
208
+ activity: (message as any).activity || this.currentActivity
209
+ });
210
+ if (message.streaming === false) {
211
+ this.lastReasoning = '';
212
+ }
130
213
  return handled;
131
214
  }
132
- if (['agent_message', 'agent_activity', 'notice', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')) {
215
+ if (['agent_message', 'budget_warning', 'step_start', 'task_complete', 'error', 'agent_plan'].includes(message?.type || '')
216
+ || (message?.type === 'notice' && !message.nativeStep)) {
133
217
  if (this._activeReasoning && this.lastReasoning) {
134
- super.onAgentReasoning({ content: this.lastReasoning, streaming: false });
218
+ super.onAgentReasoning({
219
+ content: this.lastReasoning,
220
+ streaming: false,
221
+ activity: this.currentActivity
222
+ });
135
223
  }
136
224
  this._activeReasoning = false;
137
225
  this.lastReasoning = '';
226
+ if (message?.type === 'task_complete' || (message?.type === 'agent_message' && message.streaming !== true)) {
227
+ this.collapseFinishedReasoningFrames(true);
228
+ }
138
229
  if (['step_start', 'task_complete', 'error'].includes(message?.type || '')
139
230
  || (message?.type === 'agent_message' && message.streaming !== true)) {
140
231
  this.activeCode = null;
@@ -115,6 +115,7 @@ export function handleMessageMouse(host, loop, event) {
115
115
  const frame = Array.isArray(frames) ? frames.find(f => f.id === rawMarker.id) : null;
116
116
  if (frame && frame.meta && frame.meta.reasoning) {
117
117
  frame.meta.collapsed = !frame.meta.collapsed;
118
+ frame.meta.userInteracted = true;
118
119
  frame.rows = null;
119
120
  frame.renderedWidth = -1;
120
121
  if (transcript) {
@@ -22,7 +22,7 @@ export interface MessageMarker {
22
22
  id: number;
23
23
  kind: string;
24
24
  row: number;
25
- meta?: { messageText?: string; reasoning?: boolean; collapsed?: boolean } | null;
25
+ meta?: { messageText?: string; reasoning?: boolean; collapsed?: boolean; userInteracted?: boolean } | null;
26
26
  }
27
27
 
28
28
  interface MousePoint { x: number; y: number }
@@ -188,7 +188,7 @@ export function handleMessageMouse(
188
188
  const transcript = surface.transcript as unknown as {
189
189
  frames?: Array<{
190
190
  id: number;
191
- meta?: { reasoning?: boolean; collapsed?: boolean } | null;
191
+ meta?: { reasoning?: boolean; collapsed?: boolean; userInteracted?: boolean } | null;
192
192
  rows?: unknown;
193
193
  renderedWidth?: number;
194
194
  }>;
@@ -200,6 +200,7 @@ export function handleMessageMouse(
200
200
  const frame = Array.isArray(frames) ? frames.find(f => f.id === rawMarker.id) : null;
201
201
  if (frame && frame.meta && frame.meta.reasoning) {
202
202
  frame.meta.collapsed = !frame.meta.collapsed;
203
+ frame.meta.userInteracted = true;
203
204
  frame.rows = null;
204
205
  frame.renderedWidth = -1;
205
206
  if (transcript) {