@causyne/agent 1.20.38 → 1.20.41
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.
- package/LICENSE +1 -1
- package/package.json +1 -1
- package/src/commands.js +41 -0
- package/src/decisionPolicy.js +1 -1
- package/src/decisionPolicy.ts +1 -1
- package/src/plugins/pluginsFactory.js +4 -0
- package/src/plugins/pluginsFactory.ts +6 -0
- package/src/pluginsFactory/costEstimate.js +89 -0
- package/src/pluginsFactory/costEstimate.ts +110 -0
- package/src/pluginsFactory/discovery.js +97 -0
- package/src/pluginsFactory/discovery.ts +114 -0
- package/src/pluginsFactory/index.js +9 -0
- package/src/pluginsFactory/index.ts +11 -0
- package/src/pluginsFactory/pluginsFactory.js +255 -0
- package/src/pluginsFactory/pluginsFactory.ts +307 -0
- package/src/pluginsFactory/strategy.js +93 -0
- package/src/pluginsFactory/strategy.ts +106 -0
- package/src/pluginsFactory/templates.js +624 -0
- package/src/pluginsFactory/templates.ts +662 -0
- package/src/pluginsFactory/types.js +12 -0
- package/src/pluginsFactory/types.ts +107 -0
- package/src/stallGuard.js +1 -0
- package/src/stallGuard.ts +1 -0
- package/src/toolPacks.js +2 -1
- package/src/toolPacks.ts +2 -1
- package/src/tools/pluginsFactory.js +209 -0
- package/src/tools/pluginsFactory.ts +254 -0
- package/src/tools/registry.js +2 -1
- package/src/toolsList.js +1 -0
- package/src/toolsList.ts +1 -0
- package/src/tui/chatRenderer.d.ts +1 -0
- package/src/tui/chatRenderer.js +60 -37
- package/src/tui/messageInteraction.js +26 -3
- package/src/tui/messageInteraction.ts +39 -4
- package/src/tui/thinkingBox.js +141 -0
- package/src/tui/thinkingBox.ts +190 -0
package/src/tui/chatRenderer.js
CHANGED
|
@@ -27,6 +27,7 @@ import { chatProseWidth, wrappedChatRows } from './textLayout.js';
|
|
|
27
27
|
import { activityRows } from './activityLayout.js';
|
|
28
28
|
import { cleanToolOutputPreview, normalizeHistoricalToolResult } from './toolOutputNormalizer.js';
|
|
29
29
|
import { formatMarkdownMath } from './markdownMath.js';
|
|
30
|
+
import { renderThinkingFrame } from './thinkingBox.js';
|
|
30
31
|
|
|
31
32
|
// Bound the preview AFTER wrapping, not by source newlines: one 400 KB line of
|
|
32
33
|
// minified output would otherwise become thousands of rows and lock the
|
|
@@ -737,8 +738,9 @@ export class ChatRenderer {
|
|
|
737
738
|
const elapsedMs = Number(msg.elapsedMs) || 0;
|
|
738
739
|
const durationText = elapsedMs > 0 ? formatDuration(elapsedMs) : '';
|
|
739
740
|
|
|
741
|
+
const tailFrame = this.transcript?.frames?.at(-1);
|
|
740
742
|
const tailIsReasoning = !this.transcript
|
|
741
|
-
||
|
|
743
|
+
|| tailFrame?.meta?.reasoning === true;
|
|
742
744
|
if (this._activeReasoning && !tailIsReasoning) {
|
|
743
745
|
this._activeReasoning = false;
|
|
744
746
|
}
|
|
@@ -746,56 +748,77 @@ export class ChatRenderer {
|
|
|
746
748
|
if (isStreaming) {
|
|
747
749
|
if (!this._activeReasoning) {
|
|
748
750
|
this._activeReasoning = true;
|
|
749
|
-
|
|
751
|
+
const meta = {
|
|
752
|
+
reasoning: true,
|
|
753
|
+
collapsed: false,
|
|
754
|
+
streaming: true,
|
|
755
|
+
durationText,
|
|
756
|
+
content
|
|
757
|
+
};
|
|
758
|
+
this.frame('reasoning', () => this.renderReasoning(meta.content, {
|
|
759
|
+
streaming: true,
|
|
760
|
+
durationText: meta.durationText,
|
|
761
|
+
collapsed: Boolean(meta.collapsed)
|
|
762
|
+
}), meta);
|
|
750
763
|
} else {
|
|
751
|
-
|
|
764
|
+
const meta = tailFrame?.meta || { reasoning: true };
|
|
765
|
+
meta.streaming = true;
|
|
766
|
+
meta.durationText = durationText;
|
|
767
|
+
meta.content = content;
|
|
768
|
+
if (meta.collapsed === undefined) meta.collapsed = false;
|
|
769
|
+
this.updateTailFrame('reasoning', () => this.renderReasoning(meta.content, {
|
|
770
|
+
streaming: true,
|
|
771
|
+
durationText: meta.durationText,
|
|
772
|
+
collapsed: Boolean(meta.collapsed)
|
|
773
|
+
}), meta);
|
|
752
774
|
}
|
|
753
775
|
return;
|
|
754
776
|
}
|
|
755
777
|
|
|
756
778
|
if (this._activeReasoning) {
|
|
757
779
|
this._activeReasoning = false;
|
|
758
|
-
|
|
780
|
+
const meta = tailFrame?.meta || { reasoning: true };
|
|
781
|
+
meta.streaming = false;
|
|
782
|
+
meta.done = true;
|
|
783
|
+
meta.durationText = durationText;
|
|
784
|
+
meta.content = content;
|
|
785
|
+
if (meta.collapsed === undefined) meta.collapsed = false;
|
|
786
|
+
this.updateTailFrame('reasoning', () => this.renderReasoning(meta.content, {
|
|
787
|
+
streaming: false,
|
|
788
|
+
durationText: meta.durationText,
|
|
789
|
+
collapsed: Boolean(meta.collapsed)
|
|
790
|
+
}), meta);
|
|
759
791
|
} else if (content.trim()) {
|
|
760
|
-
|
|
792
|
+
const meta = {
|
|
793
|
+
reasoning: true,
|
|
794
|
+
done: true,
|
|
795
|
+
streaming: false,
|
|
796
|
+
durationText,
|
|
797
|
+
content,
|
|
798
|
+
collapsed: false
|
|
799
|
+
};
|
|
800
|
+
this.frame('reasoning', () => this.renderReasoning(meta.content, {
|
|
801
|
+
streaming: false,
|
|
802
|
+
durationText: meta.durationText,
|
|
803
|
+
collapsed: Boolean(meta.collapsed)
|
|
804
|
+
}), meta);
|
|
761
805
|
}
|
|
762
806
|
}
|
|
763
807
|
|
|
764
|
-
renderReasoning(content, { streaming = false, durationText = '' } = {}) {
|
|
808
|
+
renderReasoning(content, { streaming = false, durationText = '', collapsed = false } = {}) {
|
|
765
809
|
const width = this.measure();
|
|
766
|
-
const rows = content
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
this.write(`${paint('warn', GLYPHS.vLine || '│')} ${bold(paint('warn', 'thinking…'))}`);
|
|
773
|
-
} else {
|
|
774
|
-
const timePart = durationText ? ` · ${durationText}` : '';
|
|
775
|
-
this.write(`${paint('muted', GLYPHS.vLine || '│')} ${dim(`thinking${timePart}`)}`);
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
if (streaming && rows.length > REASONING_LINES) {
|
|
779
|
-
const hidden = rows.length - REASONING_LINES;
|
|
780
|
-
this.write(`${paint('rule', GLYPHS.vLine || '│')} ${dim(`${GLYPHS.ellipsis} ${hidden} earlier thinking line${hidden === 1 ? '' : 's'}`)}`);
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
const displayRows = streaming && rows.length > REASONING_LINES
|
|
784
|
-
? rows.slice(rows.length - REASONING_LINES)
|
|
785
|
-
: rows.slice(0, REASONING_LINES);
|
|
810
|
+
const rows = renderThinkingFrame(content, {
|
|
811
|
+
width,
|
|
812
|
+
streaming,
|
|
813
|
+
durationText,
|
|
814
|
+
collapsed
|
|
815
|
+
});
|
|
786
816
|
|
|
787
|
-
|
|
788
|
-
this.write(`${paint('rule', GLYPHS.vLine || '│')} ${dim(row)}`);
|
|
789
|
-
}
|
|
817
|
+
if (rows.length === 0) return;
|
|
790
818
|
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
this.write(
|
|
794
|
-
} else if (streaming) {
|
|
795
|
-
const timePart = durationText ? ` · ${durationText}` : '';
|
|
796
|
-
this.write(`${paint('rule', GLYPHS.corner || '└')} ${bold(paint('warn', 'thinking…'))} ${dim(timePart || 'streaming thoughts')}`);
|
|
797
|
-
} else {
|
|
798
|
-
this.write(`${paint('rule', GLYPHS.corner || '└')}`);
|
|
819
|
+
this.write();
|
|
820
|
+
for (const row of rows) {
|
|
821
|
+
this.write(row);
|
|
799
822
|
}
|
|
800
823
|
this.write();
|
|
801
824
|
}
|
|
@@ -108,6 +108,24 @@ export function handleMessageMouse(host, loop, event) {
|
|
|
108
108
|
if (isMouseMotion(event.sequence))
|
|
109
109
|
return true;
|
|
110
110
|
if (mouse.button === 0 && mouse.pressed) {
|
|
111
|
+
const rawMarker = markerAt(surface, mouse);
|
|
112
|
+
if (rawMarker && rawMarker.kind === 'reasoning') {
|
|
113
|
+
const transcript = surface.transcript;
|
|
114
|
+
const frames = transcript?.frames;
|
|
115
|
+
const frame = Array.isArray(frames) ? frames.find(f => f.id === rawMarker.id) : null;
|
|
116
|
+
if (frame && frame.meta && frame.meta.reasoning) {
|
|
117
|
+
frame.meta.collapsed = !frame.meta.collapsed;
|
|
118
|
+
frame.rows = null;
|
|
119
|
+
frame.renderedWidth = -1;
|
|
120
|
+
if (transcript) {
|
|
121
|
+
transcript.cache = null;
|
|
122
|
+
transcript.revision = (transcript.revision || 0) + 1;
|
|
123
|
+
transcript.changed?.();
|
|
124
|
+
}
|
|
125
|
+
host.scheduleDraw();
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
111
129
|
// Smart link detection: if the user clicked (or Cmd/Ctrl+Clicked) on a URL or OSC 8 hyperlink,
|
|
112
130
|
// open the link directly in the system browser instead of popping up the Message Actions modal.
|
|
113
131
|
const hitLink = detectLinkAtPoint(surface, mouse, marker);
|
|
@@ -130,8 +148,8 @@ export function handleMessageMouse(host, loop, event) {
|
|
|
130
148
|
}
|
|
131
149
|
const isMessage = (marker) => (marker.kind === 'user' || marker.kind === 'assistant')
|
|
132
150
|
&& typeof marker.meta?.messageText === 'string';
|
|
133
|
-
/** Resolve screen coordinates against the painted viewport
|
|
134
|
-
export function
|
|
151
|
+
/** Resolve screen coordinates against the painted viewport to find any frame marker. */
|
|
152
|
+
export function markerAt(surface, point) {
|
|
135
153
|
if (!surface?.stage.reserved || !Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
136
154
|
return null;
|
|
137
155
|
const mainWidth = sidebarVisible(surface.width) ? sessionMainWidth(surface.width) : surface.width;
|
|
@@ -153,12 +171,17 @@ export function messageAt(surface, point) {
|
|
|
153
171
|
if (!marker || marker.row > row)
|
|
154
172
|
continue;
|
|
155
173
|
const next = flat.markers[i + 1];
|
|
156
|
-
if (
|
|
174
|
+
if (row < (next?.row ?? flat.rows.length))
|
|
157
175
|
return marker;
|
|
158
176
|
return null;
|
|
159
177
|
}
|
|
160
178
|
return null;
|
|
161
179
|
}
|
|
180
|
+
/** Resolve screen coordinates against the painted viewport, including top padding. */
|
|
181
|
+
export function messageAt(surface, point) {
|
|
182
|
+
const marker = markerAt(surface, point);
|
|
183
|
+
return marker && isMessage(marker) ? marker : null;
|
|
184
|
+
}
|
|
162
185
|
/** Highlight only the visible non-empty rows of the hovered message, without altering transcript data. */
|
|
163
186
|
export function markHoveredMessage(rows, surface, contentWidth) {
|
|
164
187
|
registerStageConsoleGuard(surface);
|
|
@@ -22,7 +22,7 @@ export interface MessageMarker {
|
|
|
22
22
|
id: number;
|
|
23
23
|
kind: string;
|
|
24
24
|
row: number;
|
|
25
|
-
meta?: { messageText?: string } | null;
|
|
25
|
+
meta?: { messageText?: string; reasoning?: boolean; collapsed?: boolean } | null;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
interface MousePoint { x: number; y: number }
|
|
@@ -183,6 +183,35 @@ export function handleMessageMouse(
|
|
|
183
183
|
if (isMouseMotion(event.sequence)) return true;
|
|
184
184
|
|
|
185
185
|
if (mouse.button === 0 && mouse.pressed) {
|
|
186
|
+
const rawMarker = markerAt(surface, mouse);
|
|
187
|
+
if (rawMarker && rawMarker.kind === 'reasoning') {
|
|
188
|
+
const transcript = surface.transcript as unknown as {
|
|
189
|
+
frames?: Array<{
|
|
190
|
+
id: number;
|
|
191
|
+
meta?: { reasoning?: boolean; collapsed?: boolean } | null;
|
|
192
|
+
rows?: unknown;
|
|
193
|
+
renderedWidth?: number;
|
|
194
|
+
}>;
|
|
195
|
+
cache?: unknown;
|
|
196
|
+
revision?: number;
|
|
197
|
+
changed?: () => void;
|
|
198
|
+
};
|
|
199
|
+
const frames = transcript?.frames;
|
|
200
|
+
const frame = Array.isArray(frames) ? frames.find(f => f.id === rawMarker.id) : null;
|
|
201
|
+
if (frame && frame.meta && frame.meta.reasoning) {
|
|
202
|
+
frame.meta.collapsed = !frame.meta.collapsed;
|
|
203
|
+
frame.rows = null;
|
|
204
|
+
frame.renderedWidth = -1;
|
|
205
|
+
if (transcript) {
|
|
206
|
+
transcript.cache = null;
|
|
207
|
+
transcript.revision = (transcript.revision || 0) + 1;
|
|
208
|
+
transcript.changed?.();
|
|
209
|
+
}
|
|
210
|
+
host.scheduleDraw();
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
186
215
|
// Smart link detection: if the user clicked (or Cmd/Ctrl+Clicked) on a URL or OSC 8 hyperlink,
|
|
187
216
|
// open the link directly in the system browser instead of popping up the Message Actions modal.
|
|
188
217
|
const hitLink = detectLinkAtPoint(surface, mouse, marker);
|
|
@@ -208,8 +237,8 @@ const isMessage = (marker: MessageMarker): boolean =>
|
|
|
208
237
|
(marker.kind === 'user' || marker.kind === 'assistant')
|
|
209
238
|
&& typeof marker.meta?.messageText === 'string';
|
|
210
239
|
|
|
211
|
-
/** Resolve screen coordinates against the painted viewport
|
|
212
|
-
export function
|
|
240
|
+
/** Resolve screen coordinates against the painted viewport to find any frame marker. */
|
|
241
|
+
export function markerAt(surface: MessageSurface | null, point: MousePoint): MessageMarker | null {
|
|
213
242
|
if (!surface?.stage.reserved || !Number.isFinite(point.x) || !Number.isFinite(point.y)) return null;
|
|
214
243
|
const mainWidth = sidebarVisible(surface.width) ? sessionMainWidth(surface.width) : surface.width;
|
|
215
244
|
const leftGutter = (surface.turnRailLayout?.turns?.length ?? 0) > 0 ? 3 : 0;
|
|
@@ -226,12 +255,18 @@ export function messageAt(surface: MessageSurface | null, point: MousePoint): Me
|
|
|
226
255
|
const marker = flat.markers[i];
|
|
227
256
|
if (!marker || marker.row > row) continue;
|
|
228
257
|
const next = flat.markers[i + 1];
|
|
229
|
-
if (
|
|
258
|
+
if (row < (next?.row ?? flat.rows.length)) return marker;
|
|
230
259
|
return null;
|
|
231
260
|
}
|
|
232
261
|
return null;
|
|
233
262
|
}
|
|
234
263
|
|
|
264
|
+
/** Resolve screen coordinates against the painted viewport, including top padding. */
|
|
265
|
+
export function messageAt(surface: MessageSurface | null, point: MousePoint): MessageMarker | null {
|
|
266
|
+
const marker = markerAt(surface, point);
|
|
267
|
+
return marker && isMessage(marker) ? marker : null;
|
|
268
|
+
}
|
|
269
|
+
|
|
235
270
|
/** Highlight only the visible non-empty rows of the hovered message, without altering transcript data. */
|
|
236
271
|
export function markHoveredMessage(
|
|
237
272
|
rows: string[], surface: MessageSurface, contentWidth: number
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Collapsible & auto-scrolling thinking box container for Causyne Agent TUI.
|
|
2
|
+
//
|
|
3
|
+
// Encapsulates streaming reasoning into a bounded, scrolling card container
|
|
4
|
+
// with rounded border framing, auto-scrolling tail pinning, and interactive
|
|
5
|
+
// collapse/expand folding.
|
|
6
|
+
//
|
|
7
|
+
// Pure TypeScript under strict mode.
|
|
8
|
+
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
9
|
+
import { paint, bold, dim, GLYPHS, HAS_UNICODE } from './theme.js';
|
|
10
|
+
import { padEnd, truncate, visibleWidth, wrapBlock } from './text.js';
|
|
11
|
+
export const DEFAULT_MAX_STREAMING_LINES = 8;
|
|
12
|
+
export const DEFAULT_MAX_EXPANDED_LINES = 12;
|
|
13
|
+
/**
|
|
14
|
+
* Single-line compact badge for collapsed reasoning frames.
|
|
15
|
+
*/
|
|
16
|
+
export function renderCollapsedThinkingBadge(options, totalLines) {
|
|
17
|
+
const isStreaming = Boolean(options.streaming);
|
|
18
|
+
const durationText = options.durationText ? String(options.durationText).trim() : '';
|
|
19
|
+
const arrow = HAS_UNICODE ? '▸' : '>';
|
|
20
|
+
if (isStreaming) {
|
|
21
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
22
|
+
return ` ${paint('warn', arrow)} ${bold(paint('warn', 'thinking…'))}${dim(timePart)} ${dim('· click to expand')}`;
|
|
23
|
+
}
|
|
24
|
+
const timePart = durationText ? ` for ${durationText}` : '';
|
|
25
|
+
const lineWord = totalLines === 1 ? 'line' : 'lines';
|
|
26
|
+
return ` ${paint('muted', arrow)} ${dim(`Thought${timePart}`)} ${dim(`(${totalLines} ${lineWord})`)} ${dim('· click to expand')}`;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Builds the top, bottom, and side frame borders for the thinking box.
|
|
30
|
+
*/
|
|
31
|
+
export function buildThinkingBoxBorders(title, footer, width, role = 'rule') {
|
|
32
|
+
const w = Math.max(20, Math.floor(width));
|
|
33
|
+
const inner = w - 2; // total space between left and right outer border corners
|
|
34
|
+
const h = GLYPHS.hLine || '─';
|
|
35
|
+
const v = GLYPHS.vLine || '│';
|
|
36
|
+
const tl = GLYPHS.topLeft || '╭';
|
|
37
|
+
const tr = GLYPHS.topRight || '╮';
|
|
38
|
+
const bl = GLYPHS.bottomLeft || '╰';
|
|
39
|
+
const br = GLYPHS.bottomRight || '╯';
|
|
40
|
+
// --- Top Border ---
|
|
41
|
+
const rawTitle = title ? ` ${title} ` : '';
|
|
42
|
+
const titleVis = visibleWidth(rawTitle);
|
|
43
|
+
const maxTitleVis = Math.max(0, inner - 4); // reserve at least 1 leading hLine and 2 trailing hLines
|
|
44
|
+
const safeTitle = titleVis > maxTitleVis ? (rawTitle ? ` ${truncate(title, Math.max(4, maxTitleVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawTitle;
|
|
45
|
+
const safeTitleVis = visibleWidth(safeTitle);
|
|
46
|
+
const topLead = h;
|
|
47
|
+
const topTrailCount = Math.max(0, inner - 1 - safeTitleVis);
|
|
48
|
+
const topTrail = h.repeat(topTrailCount);
|
|
49
|
+
const top = paint(role, tl + topLead) + safeTitle + paint(role, topTrail + tr);
|
|
50
|
+
// --- Bottom Border ---
|
|
51
|
+
const rawFooter = footer ? ` ${footer} ` : '';
|
|
52
|
+
const footerVis = visibleWidth(rawFooter);
|
|
53
|
+
const maxFooterVis = Math.max(0, inner - 4);
|
|
54
|
+
const safeFooter = footerVis > maxFooterVis ? (rawFooter ? ` ${truncate(footer, Math.max(4, maxFooterVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawFooter;
|
|
55
|
+
const safeFooterVis = visibleWidth(safeFooter);
|
|
56
|
+
const bottomLead = h;
|
|
57
|
+
const bottomTrailCount = Math.max(0, inner - 1 - safeFooterVis);
|
|
58
|
+
const bottomTrail = h.repeat(bottomTrailCount);
|
|
59
|
+
const bottom = paint(role, bl + bottomLead) + safeFooter + paint(role, bottomTrail + br);
|
|
60
|
+
// --- Middle Row Wrapper ---
|
|
61
|
+
// Format: "│ " + content + " │"
|
|
62
|
+
// content column width = inner - 2 = w - 4
|
|
63
|
+
const contentWidth = Math.max(2, inner - 2);
|
|
64
|
+
const side = paint(role, v);
|
|
65
|
+
const wrapRow = (line) => {
|
|
66
|
+
const truncated = truncate(line, contentWidth, GLYPHS.ellipsis || '…');
|
|
67
|
+
const padded = padEnd(truncated, contentWidth);
|
|
68
|
+
return `${side} ${padded} ${side}`;
|
|
69
|
+
};
|
|
70
|
+
return { top, bottom, wrapRow };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Render a complete thinking frame (either compact collapsed badge or expanded box).
|
|
74
|
+
*/
|
|
75
|
+
export function renderThinkingFrame(content, options = {}) {
|
|
76
|
+
const text = String(content ?? '').trim();
|
|
77
|
+
if (!text)
|
|
78
|
+
return [];
|
|
79
|
+
const width = Math.max(20, Math.floor(options.width ?? 80));
|
|
80
|
+
const isStreaming = Boolean(options.streaming);
|
|
81
|
+
const isCollapsed = Boolean(options.collapsed);
|
|
82
|
+
const durationText = options.durationText ? String(options.durationText).trim() : '';
|
|
83
|
+
// Wrap lines for inner box width (w - 4)
|
|
84
|
+
const contentBudget = Math.max(10, width - 4);
|
|
85
|
+
const allRows = wrapBlock(text, contentBudget);
|
|
86
|
+
const totalLines = allRows.length;
|
|
87
|
+
if (isCollapsed) {
|
|
88
|
+
return [renderCollapsedThinkingBadge(options, totalLines)];
|
|
89
|
+
}
|
|
90
|
+
// Expanded Box Mode
|
|
91
|
+
const role = isStreaming ? 'warn' : (options.role ?? 'rule');
|
|
92
|
+
const collapseIndicator = HAS_UNICODE ? '▾' : 'v';
|
|
93
|
+
const collapseHint = dim(`${collapseIndicator} click to collapse`);
|
|
94
|
+
if (isStreaming) {
|
|
95
|
+
const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_STREAMING_LINES;
|
|
96
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
97
|
+
const title = `${bold(paint('warn', 'thinking…'))}${dim(timePart)}`;
|
|
98
|
+
const footer = collapseHint;
|
|
99
|
+
const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, footer, width, role);
|
|
100
|
+
const rows = [top];
|
|
101
|
+
if (totalLines > maxLines) {
|
|
102
|
+
const hiddenCount = totalLines - (maxLines - 1);
|
|
103
|
+
const scrollIndicator = HAS_UNICODE ? '↑' : '^';
|
|
104
|
+
const hiddenNotice = `${paint('rule', scrollIndicator)} ${dim(`${hiddenCount} earlier thinking line${hiddenCount === 1 ? '' : 's'} scrolled`)}`;
|
|
105
|
+
rows.push(wrapRow(hiddenNotice));
|
|
106
|
+
const tailRows = allRows.slice(totalLines - (maxLines - 1));
|
|
107
|
+
for (const row of tailRows) {
|
|
108
|
+
rows.push(wrapRow(dim(row)));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
for (const row of allRows) {
|
|
113
|
+
rows.push(wrapRow(dim(row)));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
rows.push(bottom);
|
|
117
|
+
return rows;
|
|
118
|
+
}
|
|
119
|
+
// Completed Expanded Mode
|
|
120
|
+
const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_EXPANDED_LINES;
|
|
121
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
122
|
+
const title = `${paint('muted', 'thinking')}${dim(timePart)} ${dim(`(${totalLines} line${totalLines === 1 ? '' : 's'})`)}`;
|
|
123
|
+
const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, collapseHint, width, 'rule');
|
|
124
|
+
const rows = [top];
|
|
125
|
+
if (totalLines > maxLines) {
|
|
126
|
+
const hiddenCount = totalLines - maxLines;
|
|
127
|
+
const displayRows = allRows.slice(0, maxLines);
|
|
128
|
+
for (const row of displayRows) {
|
|
129
|
+
rows.push(wrapRow(dim(row)));
|
|
130
|
+
}
|
|
131
|
+
const moreNotice = `${GLYPHS.ellipsis || '…'} ${hiddenCount} more line${hiddenCount === 1 ? '' : 's'} of reasoning`;
|
|
132
|
+
rows.push(wrapRow(dim(moreNotice)));
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
for (const row of allRows) {
|
|
136
|
+
rows.push(wrapRow(dim(row)));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
rows.push(bottom);
|
|
140
|
+
return rows;
|
|
141
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Collapsible & auto-scrolling thinking box container for Causyne Agent TUI.
|
|
2
|
+
//
|
|
3
|
+
// Encapsulates streaming reasoning into a bounded, scrolling card container
|
|
4
|
+
// with rounded border framing, auto-scrolling tail pinning, and interactive
|
|
5
|
+
// collapse/expand folding.
|
|
6
|
+
//
|
|
7
|
+
// Pure TypeScript under strict mode.
|
|
8
|
+
|
|
9
|
+
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
10
|
+
import { paint, bold, dim, GLYPHS, HAS_UNICODE } from './theme.js';
|
|
11
|
+
import { padEnd, truncate, visibleWidth, wrapBlock } from './text.js';
|
|
12
|
+
|
|
13
|
+
export interface ThinkingFrameOptions {
|
|
14
|
+
width?: number;
|
|
15
|
+
streaming?: boolean;
|
|
16
|
+
durationText?: string;
|
|
17
|
+
collapsed?: boolean;
|
|
18
|
+
maxVisibleLines?: number;
|
|
19
|
+
role?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const DEFAULT_MAX_STREAMING_LINES = 8;
|
|
23
|
+
export const DEFAULT_MAX_EXPANDED_LINES = 12;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Single-line compact badge for collapsed reasoning frames.
|
|
27
|
+
*/
|
|
28
|
+
export function renderCollapsedThinkingBadge(
|
|
29
|
+
options: ThinkingFrameOptions,
|
|
30
|
+
totalLines: number
|
|
31
|
+
): string {
|
|
32
|
+
const isStreaming = Boolean(options.streaming);
|
|
33
|
+
const durationText = options.durationText ? String(options.durationText).trim() : '';
|
|
34
|
+
const arrow = HAS_UNICODE ? '▸' : '>';
|
|
35
|
+
|
|
36
|
+
if (isStreaming) {
|
|
37
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
38
|
+
return ` ${paint('warn', arrow)} ${bold(paint('warn', 'thinking…'))}${dim(timePart)} ${dim('· click to expand')}`;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const timePart = durationText ? ` for ${durationText}` : '';
|
|
42
|
+
const lineWord = totalLines === 1 ? 'line' : 'lines';
|
|
43
|
+
return ` ${paint('muted', arrow)} ${dim(`Thought${timePart}`)} ${dim(`(${totalLines} ${lineWord})`)} ${dim('· click to expand')}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Builds the top, bottom, and side frame borders for the thinking box.
|
|
48
|
+
*/
|
|
49
|
+
export function buildThinkingBoxBorders(
|
|
50
|
+
title: string,
|
|
51
|
+
footer: string,
|
|
52
|
+
width: number,
|
|
53
|
+
role: string = 'rule'
|
|
54
|
+
): {
|
|
55
|
+
top: string;
|
|
56
|
+
bottom: string;
|
|
57
|
+
wrapRow: (line: string) => string;
|
|
58
|
+
} {
|
|
59
|
+
const w = Math.max(20, Math.floor(width));
|
|
60
|
+
const inner = w - 2; // total space between left and right outer border corners
|
|
61
|
+
|
|
62
|
+
const h = GLYPHS.hLine || '─';
|
|
63
|
+
const v = GLYPHS.vLine || '│';
|
|
64
|
+
const tl = GLYPHS.topLeft || '╭';
|
|
65
|
+
const tr = GLYPHS.topRight || '╮';
|
|
66
|
+
const bl = GLYPHS.bottomLeft || '╰';
|
|
67
|
+
const br = GLYPHS.bottomRight || '╯';
|
|
68
|
+
|
|
69
|
+
// --- Top Border ---
|
|
70
|
+
const rawTitle = title ? ` ${title} ` : '';
|
|
71
|
+
const titleVis = visibleWidth(rawTitle);
|
|
72
|
+
const maxTitleVis = Math.max(0, inner - 4); // reserve at least 1 leading hLine and 2 trailing hLines
|
|
73
|
+
const safeTitle = titleVis > maxTitleVis ? (rawTitle ? ` ${truncate(title, Math.max(4, maxTitleVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawTitle;
|
|
74
|
+
const safeTitleVis = visibleWidth(safeTitle);
|
|
75
|
+
|
|
76
|
+
const topLead = h;
|
|
77
|
+
const topTrailCount = Math.max(0, inner - 1 - safeTitleVis);
|
|
78
|
+
const topTrail = h.repeat(topTrailCount);
|
|
79
|
+
const top = paint(role, tl + topLead) + safeTitle + paint(role, topTrail + tr);
|
|
80
|
+
|
|
81
|
+
// --- Bottom Border ---
|
|
82
|
+
const rawFooter = footer ? ` ${footer} ` : '';
|
|
83
|
+
const footerVis = visibleWidth(rawFooter);
|
|
84
|
+
const maxFooterVis = Math.max(0, inner - 4);
|
|
85
|
+
const safeFooter = footerVis > maxFooterVis ? (rawFooter ? ` ${truncate(footer, Math.max(4, maxFooterVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawFooter;
|
|
86
|
+
const safeFooterVis = visibleWidth(safeFooter);
|
|
87
|
+
|
|
88
|
+
const bottomLead = h;
|
|
89
|
+
const bottomTrailCount = Math.max(0, inner - 1 - safeFooterVis);
|
|
90
|
+
const bottomTrail = h.repeat(bottomTrailCount);
|
|
91
|
+
const bottom = paint(role, bl + bottomLead) + safeFooter + paint(role, bottomTrail + br);
|
|
92
|
+
|
|
93
|
+
// --- Middle Row Wrapper ---
|
|
94
|
+
// Format: "│ " + content + " │"
|
|
95
|
+
// content column width = inner - 2 = w - 4
|
|
96
|
+
const contentWidth = Math.max(2, inner - 2);
|
|
97
|
+
const side = paint(role, v);
|
|
98
|
+
|
|
99
|
+
const wrapRow = (line: string): string => {
|
|
100
|
+
const truncated = truncate(line, contentWidth, GLYPHS.ellipsis || '…');
|
|
101
|
+
const padded = padEnd(truncated, contentWidth);
|
|
102
|
+
return `${side} ${padded} ${side}`;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return { top, bottom, wrapRow };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Render a complete thinking frame (either compact collapsed badge or expanded box).
|
|
110
|
+
*/
|
|
111
|
+
export function renderThinkingFrame(
|
|
112
|
+
content: string,
|
|
113
|
+
options: ThinkingFrameOptions = {}
|
|
114
|
+
): string[] {
|
|
115
|
+
const text = String(content ?? '').trim();
|
|
116
|
+
if (!text) return [];
|
|
117
|
+
|
|
118
|
+
const width = Math.max(20, Math.floor(options.width ?? 80));
|
|
119
|
+
const isStreaming = Boolean(options.streaming);
|
|
120
|
+
const isCollapsed = Boolean(options.collapsed);
|
|
121
|
+
const durationText = options.durationText ? String(options.durationText).trim() : '';
|
|
122
|
+
|
|
123
|
+
// Wrap lines for inner box width (w - 4)
|
|
124
|
+
const contentBudget = Math.max(10, width - 4);
|
|
125
|
+
const allRows = wrapBlock(text, contentBudget);
|
|
126
|
+
const totalLines = allRows.length;
|
|
127
|
+
|
|
128
|
+
if (isCollapsed) {
|
|
129
|
+
return [renderCollapsedThinkingBadge(options, totalLines)];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Expanded Box Mode
|
|
133
|
+
const role = isStreaming ? 'warn' : (options.role ?? 'rule');
|
|
134
|
+
const collapseIndicator = HAS_UNICODE ? '▾' : 'v';
|
|
135
|
+
const collapseHint = dim(`${collapseIndicator} click to collapse`);
|
|
136
|
+
|
|
137
|
+
if (isStreaming) {
|
|
138
|
+
const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_STREAMING_LINES;
|
|
139
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
140
|
+
const title = `${bold(paint('warn', 'thinking…'))}${dim(timePart)}`;
|
|
141
|
+
const footer = collapseHint;
|
|
142
|
+
|
|
143
|
+
const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, footer, width, role);
|
|
144
|
+
const rows: string[] = [top];
|
|
145
|
+
|
|
146
|
+
if (totalLines > maxLines) {
|
|
147
|
+
const hiddenCount = totalLines - (maxLines - 1);
|
|
148
|
+
const scrollIndicator = HAS_UNICODE ? '↑' : '^';
|
|
149
|
+
const hiddenNotice = `${paint('rule', scrollIndicator)} ${dim(`${hiddenCount} earlier thinking line${hiddenCount === 1 ? '' : 's'} scrolled`)}`;
|
|
150
|
+
rows.push(wrapRow(hiddenNotice));
|
|
151
|
+
|
|
152
|
+
const tailRows = allRows.slice(totalLines - (maxLines - 1));
|
|
153
|
+
for (const row of tailRows) {
|
|
154
|
+
rows.push(wrapRow(dim(row)));
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
for (const row of allRows) {
|
|
158
|
+
rows.push(wrapRow(dim(row)));
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
rows.push(bottom);
|
|
163
|
+
return rows;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Completed Expanded Mode
|
|
167
|
+
const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_EXPANDED_LINES;
|
|
168
|
+
const timePart = durationText ? ` · ${durationText}` : '';
|
|
169
|
+
const title = `${paint('muted', 'thinking')}${dim(timePart)} ${dim(`(${totalLines} line${totalLines === 1 ? '' : 's'})`)}`;
|
|
170
|
+
|
|
171
|
+
const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, collapseHint, width, 'rule');
|
|
172
|
+
const rows: string[] = [top];
|
|
173
|
+
|
|
174
|
+
if (totalLines > maxLines) {
|
|
175
|
+
const hiddenCount = totalLines - maxLines;
|
|
176
|
+
const displayRows = allRows.slice(0, maxLines);
|
|
177
|
+
for (const row of displayRows) {
|
|
178
|
+
rows.push(wrapRow(dim(row)));
|
|
179
|
+
}
|
|
180
|
+
const moreNotice = `${GLYPHS.ellipsis || '…'} ${hiddenCount} more line${hiddenCount === 1 ? '' : 's'} of reasoning`;
|
|
181
|
+
rows.push(wrapRow(dim(moreNotice)));
|
|
182
|
+
} else {
|
|
183
|
+
for (const row of allRows) {
|
|
184
|
+
rows.push(wrapRow(dim(row)));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
rows.push(bottom);
|
|
189
|
+
return rows;
|
|
190
|
+
}
|