@causyne/agent 1.20.39 → 1.20.42

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,216 @@
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
+ startLine?: number;
21
+ }
22
+
23
+ export const DEFAULT_MAX_STREAMING_LINES = 8;
24
+ export const DEFAULT_MAX_EXPANDED_LINES = 12;
25
+
26
+ /**
27
+ * Calculates total wrapped lines for reasoning content under the thinking box budget.
28
+ */
29
+ export function calculateThinkingLineCount(content: string, width: number): number {
30
+ const text = String(content ?? '').trim();
31
+ if (!text) return 0;
32
+ const safeWidth = Math.max(20, Math.floor(width));
33
+ const contentBudget = Math.max(10, safeWidth - 4);
34
+ return wrapBlock(text, contentBudget).length;
35
+ }
36
+
37
+ /**
38
+ * Single-line compact badge for collapsed reasoning frames.
39
+ */
40
+ export function renderCollapsedThinkingBadge(
41
+ options: ThinkingFrameOptions,
42
+ totalLines: number
43
+ ): string {
44
+ const isStreaming = Boolean(options.streaming);
45
+ const durationText = options.durationText ? String(options.durationText).trim() : '';
46
+ const arrow = HAS_UNICODE ? '▸' : '>';
47
+
48
+ if (isStreaming) {
49
+ const timePart = durationText ? ` · ${durationText}` : '';
50
+ return ` ${paint('warn', arrow)} ${bold(paint('warn', 'thinking…'))}${dim(timePart)} ${dim('· click to expand')}`;
51
+ }
52
+
53
+ const timePart = durationText ? ` for ${durationText}` : '';
54
+ const lineWord = totalLines === 1 ? 'line' : 'lines';
55
+ return ` ${paint('muted', arrow)} ${dim(`Thought${timePart}`)} ${dim(`(${totalLines} ${lineWord})`)} ${dim('· click to expand')}`;
56
+ }
57
+
58
+ /**
59
+ * Builds the top, bottom, and side frame borders for the thinking box.
60
+ */
61
+ export function buildThinkingBoxBorders(
62
+ title: string,
63
+ footer: string,
64
+ width: number,
65
+ role: string = 'rule'
66
+ ): {
67
+ top: string;
68
+ bottom: string;
69
+ wrapRow: (line: string) => string;
70
+ } {
71
+ const w = Math.max(20, Math.floor(width));
72
+ const inner = w - 2; // total space between left and right outer border corners
73
+
74
+ const h = GLYPHS.hLine || '─';
75
+ const v = GLYPHS.vLine || '│';
76
+ const tl = GLYPHS.topLeft || '╭';
77
+ const tr = GLYPHS.topRight || '╮';
78
+ const bl = GLYPHS.bottomLeft || '╰';
79
+ const br = GLYPHS.bottomRight || '╯';
80
+
81
+ // --- Top Border ---
82
+ const rawTitle = title ? ` ${title} ` : '';
83
+ const titleVis = visibleWidth(rawTitle);
84
+ const maxTitleVis = Math.max(0, inner - 4); // reserve at least 1 leading hLine and 2 trailing hLines
85
+ const safeTitle = titleVis > maxTitleVis ? (rawTitle ? ` ${truncate(title, Math.max(4, maxTitleVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawTitle;
86
+ const safeTitleVis = visibleWidth(safeTitle);
87
+
88
+ const topLead = h;
89
+ const topTrailCount = Math.max(0, inner - 1 - safeTitleVis);
90
+ const topTrail = h.repeat(topTrailCount);
91
+ const top = paint(role, tl + topLead) + safeTitle + paint(role, topTrail + tr);
92
+
93
+ // --- Bottom Border ---
94
+ const rawFooter = footer ? ` ${footer} ` : '';
95
+ const footerVis = visibleWidth(rawFooter);
96
+ const maxFooterVis = Math.max(0, inner - 4);
97
+ const safeFooter = footerVis > maxFooterVis ? (rawFooter ? ` ${truncate(footer, Math.max(4, maxFooterVis - 2), GLYPHS.ellipsis || '…')} ` : '') : rawFooter;
98
+ const safeFooterVis = visibleWidth(safeFooter);
99
+
100
+ const bottomLead = h;
101
+ const bottomTrailCount = Math.max(0, inner - 1 - safeFooterVis);
102
+ const bottomTrail = h.repeat(bottomTrailCount);
103
+ const bottom = paint(role, bl + bottomLead) + safeFooter + paint(role, bottomTrail + br);
104
+
105
+ // --- Middle Row Wrapper ---
106
+ // Format: "│ " + content + " │"
107
+ // content column width = inner - 2 = w - 4
108
+ const contentWidth = Math.max(2, inner - 2);
109
+ const side = paint(role, v);
110
+
111
+ const wrapRow = (line: string): string => {
112
+ const truncated = truncate(line, contentWidth, GLYPHS.ellipsis || '…');
113
+ const padded = padEnd(truncated, contentWidth);
114
+ return `${side} ${padded} ${side}`;
115
+ };
116
+
117
+ return { top, bottom, wrapRow };
118
+ }
119
+
120
+ /**
121
+ * Render a complete thinking frame (either compact collapsed badge or expanded box).
122
+ */
123
+ export function renderThinkingFrame(
124
+ content: string,
125
+ options: ThinkingFrameOptions = {}
126
+ ): string[] {
127
+ const text = String(content ?? '').trim();
128
+ if (!text) return [];
129
+
130
+ const width = Math.max(20, Math.floor(options.width ?? 80));
131
+ const isStreaming = Boolean(options.streaming);
132
+ const isCollapsed = Boolean(options.collapsed);
133
+ const durationText = options.durationText ? String(options.durationText).trim() : '';
134
+
135
+ // Wrap lines for inner box width (w - 4)
136
+ const contentBudget = Math.max(10, width - 4);
137
+ const allRows = wrapBlock(text, contentBudget);
138
+ const totalLines = allRows.length;
139
+
140
+ if (isCollapsed) {
141
+ return [renderCollapsedThinkingBadge(options, totalLines)];
142
+ }
143
+
144
+ // Expanded Box Mode
145
+ const role = isStreaming ? 'warn' : (options.role ?? 'rule');
146
+ const collapseIndicator = HAS_UNICODE ? '▾' : 'v';
147
+ const collapseHint = dim(`${collapseIndicator} click to collapse`);
148
+
149
+ if (isStreaming) {
150
+ const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_STREAMING_LINES;
151
+ const timePart = durationText ? ` · ${durationText}` : '';
152
+ const title = `${bold(paint('warn', 'thinking…'))}${dim(timePart)}`;
153
+ const footer = collapseHint;
154
+
155
+ const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, footer, width, role);
156
+ const rows: string[] = [top];
157
+
158
+ if (totalLines > maxLines) {
159
+ const hiddenCount = totalLines - (maxLines - 1);
160
+ const scrollIndicator = HAS_UNICODE ? '↑' : '^';
161
+ const hiddenNotice = `${paint('rule', scrollIndicator)} ${dim(`${hiddenCount} earlier thinking line${hiddenCount === 1 ? '' : 's'} scrolled`)}`;
162
+ rows.push(wrapRow(hiddenNotice));
163
+
164
+ const tailRows = allRows.slice(totalLines - (maxLines - 1));
165
+ for (const row of tailRows) {
166
+ rows.push(wrapRow(dim(row)));
167
+ }
168
+ } else {
169
+ for (const row of allRows) {
170
+ rows.push(wrapRow(dim(row)));
171
+ }
172
+ }
173
+
174
+ rows.push(bottom);
175
+ return rows;
176
+ }
177
+
178
+ // Completed Expanded Mode
179
+ const maxLines = options.maxVisibleLines ?? DEFAULT_MAX_EXPANDED_LINES;
180
+ const timePart = durationText ? ` · ${durationText}` : '';
181
+ const title = `${paint('muted', 'thinking')}${dim(timePart)} ${dim(`(${totalLines} line${totalLines === 1 ? '' : 's'})`)}`;
182
+
183
+ const { top, bottom, wrapRow } = buildThinkingBoxBorders(title, collapseHint, width, 'rule');
184
+ const rows: string[] = [top];
185
+
186
+ if (totalLines > maxLines) {
187
+ const limit = Math.max(0, totalLines - maxLines);
188
+ const start = Math.max(0, Math.min(limit, Math.floor(options.startLine ?? 0)));
189
+ const visible = allRows.slice(start, start + maxLines);
190
+
191
+ for (const row of visible) {
192
+ rows.push(wrapRow(dim(row)));
193
+ }
194
+
195
+ const hiddenBelow = totalLines - (start + visible.length);
196
+ if (hiddenBelow > 0) {
197
+ const moreNotice = `${GLYPHS.ellipsis || '…'} ${hiddenBelow} more line${hiddenBelow === 1 ? '' : 's'} of reasoning`;
198
+ rows.push(wrapRow(dim(moreNotice)));
199
+ } else if (start > 0) {
200
+ const earlierNotice = `${HAS_UNICODE ? '↑' : '^'} ${start} earlier line${start === 1 ? '' : 's'} scrolled above`;
201
+ rows.push(wrapRow(dim(earlierNotice)));
202
+ }
203
+
204
+ const upArrow = start > 0 ? (HAS_UNICODE ? '↑ ' : '^ ') : '';
205
+ const downArrow = start + visible.length < totalLines ? (HAS_UNICODE ? ' ↓' : ' v') : '';
206
+ const scrollInfo = `${upArrow}lines ${start + 1}–${start + visible.length} of ${totalLines}${downArrow} · scroll over thinking`;
207
+ rows.push(wrapRow(dim(scrollInfo)));
208
+ } else {
209
+ for (const row of allRows) {
210
+ rows.push(wrapRow(dim(row)));
211
+ }
212
+ }
213
+
214
+ rows.push(bottom);
215
+ return rows;
216
+ }