@causyne/agent 1.20.54 → 1.20.55
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/package.json +1 -1
- package/src/decisionPolicy.js +73 -3
- package/src/decisionPolicy.ts +71 -3
- package/src/tui/chatRenderer.js +21 -0
- package/src/tui/sessionSidebar.js +2 -4
- package/src/tui/sessionSidebar.ts +2 -4
- package/src/tui/statusLine.js +156 -214
- package/src/tui/statusLine.ts +222 -0
- package/src/tui/toolDisplay.js +117 -120
- package/src/tui/toolDisplay.ts +191 -0
package/src/tui/statusLine.js
CHANGED
|
@@ -7,35 +7,30 @@
|
|
|
7
7
|
// and that was the entire signal for what could be a four-minute, forty-step,
|
|
8
8
|
// several-dollar run. The user could not see elapsed time, which model was
|
|
9
9
|
// spending, how many steps had gone by, how many tokens the turn had burned, or
|
|
10
|
-
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
// openclaw puts elapsed and connection state on its waiting line; opencode keeps
|
|
15
|
-
// a permanent footer with cwd, LSP and MCP counts. This is the same idea in
|
|
16
|
-
// Causyne's register: no whimsy, just the four numbers a person needs to decide
|
|
17
|
-
// whether to let a run continue or press esc.
|
|
10
|
+
// what was left. All of those numbers were already in memory — the loop broadcasts
|
|
11
|
+
// `usage_update` on every response and the config carries the model — and none of
|
|
12
|
+
// it was shown.
|
|
18
13
|
//
|
|
19
14
|
// The line degrades by DROPPING SEGMENTS, right to left, until it fits. A narrow
|
|
20
15
|
// terminal loses the cost before it loses the elapsed time, and loses the
|
|
21
16
|
// elapsed time before it loses the message. It never wraps, because a wrapping
|
|
22
17
|
// spinner leaves debris every tick.
|
|
23
18
|
//
|
|
24
|
-
// Pure
|
|
25
|
-
|
|
19
|
+
// Pure TypeScript over ./theme.js and ./text.js. No dependency, no shell.
|
|
20
|
+
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
26
21
|
import { paint, dim, GLYPHS } from './theme.js';
|
|
27
22
|
import { truncate, visibleWidth, safeDisplay, shortenHome } from './text.js';
|
|
28
23
|
import { contextMeter } from './block.js';
|
|
29
24
|
import { CHARS_PER_TOKEN } from '../contextManager.js';
|
|
30
|
-
|
|
31
25
|
/** 1234 -> "1.2k", 1234567 -> "1.2M". Token counts are read, not summed. */
|
|
32
26
|
export function compactCount(value) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
const n = Number(value) || 0;
|
|
28
|
+
if (n < 1000)
|
|
29
|
+
return String(Math.round(n));
|
|
30
|
+
if (n < 1000000)
|
|
31
|
+
return `${(n / 1000).toFixed(n < 10000 ? 1 : 0)}k`;
|
|
32
|
+
return `${(n / 1000000).toFixed(1)}M`;
|
|
37
33
|
}
|
|
38
|
-
|
|
39
34
|
/**
|
|
40
35
|
* Money, at the precision the number deserves.
|
|
41
36
|
*
|
|
@@ -43,212 +38,159 @@ export function compactCount(value) {
|
|
|
43
38
|
* to look at the cost at all.
|
|
44
39
|
*/
|
|
45
40
|
export function formatCost(value) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
41
|
+
const n = Number(value) || 0;
|
|
42
|
+
if (n <= 0)
|
|
43
|
+
return '$0';
|
|
44
|
+
if (n < 0.01)
|
|
45
|
+
return `$${n.toFixed(4)}`;
|
|
46
|
+
if (n < 1)
|
|
47
|
+
return `$${n.toFixed(3)}`;
|
|
48
|
+
return `$${n.toFixed(2)}`;
|
|
51
49
|
}
|
|
52
|
-
|
|
53
50
|
export function formatElapsed(ms) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
51
|
+
const total = Math.max(0, Math.round(ms / 1000));
|
|
52
|
+
if (total < 60)
|
|
53
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
54
|
+
const minutes = Math.floor(total / 60);
|
|
55
|
+
const seconds = total % 60;
|
|
56
|
+
if (minutes < 60)
|
|
57
|
+
return `${minutes}m${String(seconds).padStart(2, '0')}s`;
|
|
58
|
+
return `${Math.floor(minutes / 60)}h${String(minutes % 60).padStart(2, '0')}m`;
|
|
60
59
|
}
|
|
61
|
-
|
|
62
60
|
/** Strip a provider prefix so "anthropic/claude-opus-5" reads as the model. */
|
|
63
61
|
function shortModel(model) {
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
const name = String(model || '').split('/').pop() || '';
|
|
63
|
+
return name.length > 22 ? name.slice(0, 21) + GLYPHS.ellipsis : name;
|
|
66
64
|
}
|
|
67
|
-
|
|
68
65
|
export class StatusLine {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.cwd = '';
|
|
66
|
+
model = '';
|
|
67
|
+
provider = '';
|
|
68
|
+
tier = '';
|
|
69
|
+
cwd = '';
|
|
74
70
|
// Both in CHARACTERS — the unit the window is actually budgeted in.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
71
|
+
contextLimit = 0;
|
|
72
|
+
contextChars = 0;
|
|
73
|
+
turnTokens = 0;
|
|
74
|
+
turnCost = 0;
|
|
75
|
+
cachedTokens = 0;
|
|
76
|
+
constructor() {
|
|
77
|
+
this.reset();
|
|
78
|
+
}
|
|
79
|
+
/** Session-lifetime facts, set once from the same info as the header. */
|
|
80
|
+
setSession(info = {}) {
|
|
81
|
+
if (info.model !== undefined)
|
|
82
|
+
this.model = shortModel(info.model);
|
|
83
|
+
if (info.provider !== undefined)
|
|
84
|
+
this.provider = info.provider ? String(info.provider) : '';
|
|
85
|
+
if (info.tier !== undefined)
|
|
86
|
+
this.tier = info.tier ? String(info.tier) : '';
|
|
87
|
+
if (info.cwd !== undefined)
|
|
88
|
+
this.cwd = info.cwd ? String(info.cwd) : '';
|
|
89
|
+
if (Number(info.contextLimit) > 0)
|
|
90
|
+
this.contextLimit = Number(info.contextLimit);
|
|
91
|
+
if (Number(info.contextChars) > 0)
|
|
92
|
+
this.contextChars = Number(info.contextChars);
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
/** Per-turn counters. */
|
|
96
|
+
reset() {
|
|
97
|
+
this.turnTokens = 0;
|
|
98
|
+
this.turnCost = 0;
|
|
99
|
+
this.cachedTokens = 0;
|
|
100
|
+
// Note: Do NOT reset this.contextChars to 0 here. Context window usage
|
|
101
|
+
// belongs to the whole conversation, not just a single active turn.
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The idle footer: where you are, and how much context is left.
|
|
106
|
+
*/
|
|
107
|
+
renderIdle({ width = 80, hint = '' } = {}) {
|
|
108
|
+
const budget = Math.max(10, width - 1);
|
|
109
|
+
const meter = contextMeter(Math.round(this.contextChars / CHARS_PER_TOKEN), Math.round(this.contextLimit / CHARS_PER_TOKEN));
|
|
110
|
+
const segments = [];
|
|
111
|
+
if (meter)
|
|
112
|
+
segments.push(meter);
|
|
113
|
+
if (hint)
|
|
114
|
+
segments.push(hint);
|
|
115
|
+
const separator = ` ${GLYPHS.dot} `;
|
|
116
|
+
const kept = segments.slice();
|
|
117
|
+
let tail = kept.join(separator);
|
|
118
|
+
while (kept.length > 1 && visibleWidth(tail) > Math.floor(budget / 2)) {
|
|
119
|
+
kept.pop();
|
|
120
|
+
tail = kept.join(separator);
|
|
121
|
+
}
|
|
122
|
+
if (visibleWidth(tail) > budget)
|
|
123
|
+
tail = '';
|
|
124
|
+
const room = tail ? Math.max(0, budget - visibleWidth(tail) - visibleWidth(separator)) : budget;
|
|
125
|
+
const where = this.cwd ? truncate(shortenHome(this.cwd), room, GLYPHS.ellipsis) : '';
|
|
126
|
+
const left = dim(where);
|
|
127
|
+
if (!tail)
|
|
128
|
+
return left;
|
|
129
|
+
const gap = Math.max(visibleWidth(separator), width - 1 - visibleWidth(left) - visibleWidth(tail));
|
|
130
|
+
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Consume a `usage_update` frame.
|
|
134
|
+
*/
|
|
135
|
+
onUsage(msg = {}) {
|
|
136
|
+
this.turnTokens = Number(msg.taskTokens) || Number(msg.usedTokens) || this.turnTokens;
|
|
137
|
+
if (Number(msg.contextChars) > 0)
|
|
138
|
+
this.contextChars = Number(msg.contextChars);
|
|
139
|
+
if (Number(msg.contextLimitChars) > 0)
|
|
140
|
+
this.contextLimit = Number(msg.contextLimitChars);
|
|
141
|
+
const responseCost = Number(msg.costUsd) || 0;
|
|
142
|
+
const taskCost = Number(msg.taskCostUsd) || 0;
|
|
143
|
+
this.turnCost = taskCost > 0 ? taskCost : this.turnCost + responseCost;
|
|
144
|
+
this.cachedTokens = Number(msg.cachedTokens) || 0;
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Build the line.
|
|
149
|
+
*/
|
|
150
|
+
render({ message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '' } = {}) {
|
|
151
|
+
const head = spinnerFrame ? `${paint('accent', spinnerFrame)} ` : '';
|
|
152
|
+
const budget = Math.max(10, width - visibleWidth(head) - 1);
|
|
153
|
+
const segments = [];
|
|
154
|
+
if (elapsedMs > 0)
|
|
155
|
+
segments.push(formatElapsed(elapsedMs));
|
|
156
|
+
if (steps > 0)
|
|
157
|
+
segments.push(`step ${steps}`);
|
|
158
|
+
if (this.turnTokens > 0)
|
|
159
|
+
segments.push(`${compactCount(this.turnTokens)} tok`);
|
|
160
|
+
if (this.turnCost > 0)
|
|
161
|
+
segments.push(formatCost(this.turnCost));
|
|
162
|
+
if (this.cachedTokens > 0)
|
|
163
|
+
segments.push(`${compactCount(this.cachedTokens)} cached`);
|
|
164
|
+
if (this.model)
|
|
165
|
+
segments.push(this.model);
|
|
166
|
+
const separator = ` ${GLYPHS.dot} `;
|
|
167
|
+
const text = safeDisplay(message);
|
|
168
|
+
const kept = segments.slice();
|
|
169
|
+
const wanted = Math.min(visibleWidth(text), Math.max(18, Math.floor(budget / 2)));
|
|
170
|
+
while (kept.length > 0) {
|
|
171
|
+
const tail = kept.join(separator);
|
|
172
|
+
if (visibleWidth(tail) + separator.length + wanted <= budget)
|
|
173
|
+
break;
|
|
174
|
+
kept.pop();
|
|
175
|
+
}
|
|
176
|
+
const tail = kept.length > 0 ? kept.join(separator) : '';
|
|
177
|
+
const messageBudget = tail
|
|
178
|
+
? Math.max(1, budget - visibleWidth(tail) - separator.length)
|
|
179
|
+
: budget;
|
|
180
|
+
const body = truncate(text, messageBudget, GLYPHS.ellipsis);
|
|
181
|
+
const left = `${head}${paint('muted', body)}`;
|
|
182
|
+
if (!tail)
|
|
183
|
+
return left;
|
|
184
|
+
const gap = Math.max(visibleWidth(separator), width - 1 - visibleWidth(left) - visibleWidth(tail));
|
|
185
|
+
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
133
186
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
visibleWidth(separator),
|
|
143
|
-
width - 1 - visibleWidth(left) - visibleWidth(tail)
|
|
144
|
-
);
|
|
145
|
-
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Consume a `usage_update` frame.
|
|
150
|
-
*
|
|
151
|
-
* taskCostUsd is the whole task; costUsd is this response. The turn total is
|
|
152
|
-
* what the user is deciding about right now, so accumulate that.
|
|
153
|
-
*/
|
|
154
|
-
onUsage(msg = {}) {
|
|
155
|
-
// TASK scope, explicitly. `usedTokens` now carries the DAILY total, which is
|
|
156
|
-
|
|
157
|
-
// shows — the working line is about the turn the user is deciding on.
|
|
158
|
-
this.turnTokens = Number(msg.taskTokens) || Number(msg.usedTokens) || this.turnTokens;
|
|
159
|
-
// What the request actually carried, measured at the point it was built.
|
|
160
|
-
if (Number(msg.contextChars) > 0) this.contextChars = Number(msg.contextChars);
|
|
161
|
-
if (Number(msg.contextLimitChars) > 0) this.contextLimit = Number(msg.contextLimitChars);
|
|
162
|
-
const responseCost = Number(msg.costUsd) || 0;
|
|
163
|
-
const taskCost = Number(msg.taskCostUsd) || 0;
|
|
164
|
-
// Prefer the task total when the provider reports one — it already includes
|
|
165
|
-
// every response, and summing on top would double-count.
|
|
166
|
-
this.turnCost = taskCost > 0 ? taskCost : this.turnCost + responseCost;
|
|
167
|
-
this.cachedTokens = Number(msg.cachedTokens) || 0;
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/**
|
|
172
|
-
* Build the line.
|
|
173
|
-
*
|
|
174
|
-
* `segments` are ordered most to least important. Anything that does not fit
|
|
175
|
-
* is dropped whole — never truncated into a misleading half-number.
|
|
176
|
-
*/
|
|
177
|
-
render({ message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '' } = {}) {
|
|
178
|
-
const head = spinnerFrame ? `${paint('accent', spinnerFrame)} ` : '';
|
|
179
|
-
const budget = Math.max(10, width - visibleWidth(head) - 1);
|
|
180
|
-
|
|
181
|
-
const segments = [];
|
|
182
|
-
if (elapsedMs > 0) segments.push(formatElapsed(elapsedMs));
|
|
183
|
-
if (steps > 0) segments.push(`step ${steps}`);
|
|
184
|
-
if (this.turnTokens > 0) segments.push(`${compactCount(this.turnTokens)} tok`);
|
|
185
|
-
if (this.turnCost > 0) segments.push(formatCost(this.turnCost));
|
|
186
|
-
if (this.cachedTokens > 0) segments.push(`${compactCount(this.cachedTokens)} cached`);
|
|
187
|
-
if (this.model) segments.push(this.model);
|
|
188
|
-
|
|
189
|
-
const separator = ` ${GLYPHS.dot} `;
|
|
190
|
-
const text = safeDisplay(message);
|
|
191
|
-
|
|
192
|
-
// Drop segments from the right until the message fits at its natural width.
|
|
193
|
-
//
|
|
194
|
-
// The message is what the user is reading; the numbers are what they glance
|
|
195
|
-
// at. Reserving space for the message FIRST is what stops a wide terminal
|
|
196
|
-
// from printing "reading project fi…" next to six perfectly intact counters.
|
|
197
|
-
// The reservation is capped at half the line so a very long status cannot
|
|
198
|
-
// squeeze every number out.
|
|
199
|
-
let kept = segments.slice();
|
|
200
|
-
const wanted = Math.min(visibleWidth(text), Math.max(18, Math.floor(budget / 2)));
|
|
201
|
-
while (kept.length > 0) {
|
|
202
|
-
const tail = kept.join(separator);
|
|
203
|
-
if (visibleWidth(tail) + separator.length + wanted <= budget) break;
|
|
204
|
-
kept.pop();
|
|
187
|
+
/**
|
|
188
|
+
* The non-TTY form: no spinner, no elapsed clock, printed once.
|
|
189
|
+
*/
|
|
190
|
+
renderStatic(message, width = 80) {
|
|
191
|
+
const parts = [safeDisplay(message)];
|
|
192
|
+
if (this.model)
|
|
193
|
+
parts.push(this.model);
|
|
194
|
+
return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, width, GLYPHS.ellipsis);
|
|
205
195
|
}
|
|
206
|
-
|
|
207
|
-
const tail = kept.length > 0 ? kept.join(separator) : '';
|
|
208
|
-
const messageBudget = tail
|
|
209
|
-
? Math.max(1, budget - visibleWidth(tail) - separator.length)
|
|
210
|
-
: budget;
|
|
211
|
-
const body = truncate(text, messageBudget, GLYPHS.ellipsis);
|
|
212
|
-
|
|
213
|
-
const left = `${head}${paint('muted', body)}`;
|
|
214
|
-
if (!tail) return left;
|
|
215
|
-
|
|
216
|
-
// Justify: message left, numbers hard right.
|
|
217
|
-
//
|
|
218
|
-
// Packing everything against the left edge wastes the whole right half of a
|
|
219
|
-
// wide terminal and makes the counters drift horizontally as the status text
|
|
220
|
-
// changes length — the eye has to re-find them every frame. Pinning them to
|
|
221
|
-
// the right edge means they sit in the same column all session. This is the
|
|
222
|
-
// shape opencode's session footer uses (justifyContent="space-between", the
|
|
223
|
-
// directory left and the counters right).
|
|
224
|
-
//
|
|
225
|
-
// The fitting loop above already guarantees room for a separator between the
|
|
226
|
-
// two, so the gap can never collapse to nothing.
|
|
227
|
-
// Justify against width - 1, not width.
|
|
228
|
-
//
|
|
229
|
-
// The fitting loop above budgets `width - head - 1`, but this gap was
|
|
230
|
-
// computed against the full `width`, so the finished line came back exactly
|
|
231
|
-
// `width` columns — flush against the right edge, with no slack at all. A
|
|
232
|
-
// terminal that renders one of the ambiguous-width glyphs in this line (`·`
|
|
233
|
-
// appears twice) as two columns then wraps it, and a wrapped row in the live
|
|
234
|
-
// region corrupts erase()'s row accounting. One reserved column removes the
|
|
235
|
-
// whole class of failure at its source; ChatPrompt.clampRows() is the net.
|
|
236
|
-
const gap = Math.max(
|
|
237
|
-
visibleWidth(separator),
|
|
238
|
-
width - 1 - visibleWidth(left) - visibleWidth(tail)
|
|
239
|
-
);
|
|
240
|
-
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* The non-TTY form: no spinner, no elapsed clock, printed once.
|
|
245
|
-
*
|
|
246
|
-
* Piped into a CI log an animated line becomes thousands of junk rows, and an
|
|
247
|
-
* elapsed time that never updates is worse than none.
|
|
248
|
-
*/
|
|
249
|
-
renderStatic(message, width = 80) {
|
|
250
|
-
const parts = [safeDisplay(message)];
|
|
251
|
-
if (this.model) parts.push(this.model);
|
|
252
|
-
return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, width, GLYPHS.ellipsis);
|
|
253
|
-
}
|
|
254
196
|
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
// The line that tells the user what is happening to their money and their time.
|
|
2
|
+
//
|
|
3
|
+
// While a turn ran, the chat printed one thing:
|
|
4
|
+
//
|
|
5
|
+
// ⠋ Thinking…
|
|
6
|
+
//
|
|
7
|
+
// and that was the entire signal for what could be a four-minute, forty-step,
|
|
8
|
+
// several-dollar run. The user could not see elapsed time, which model was
|
|
9
|
+
// spending, how many steps had gone by, how many tokens the turn had burned, or
|
|
10
|
+
// what was left. All of those numbers were already in memory — the loop broadcasts
|
|
11
|
+
// `usage_update` on every response and the config carries the model — and none of
|
|
12
|
+
// it was shown.
|
|
13
|
+
//
|
|
14
|
+
// The line degrades by DROPPING SEGMENTS, right to left, until it fits. A narrow
|
|
15
|
+
// terminal loses the cost before it loses the elapsed time, and loses the
|
|
16
|
+
// elapsed time before it loses the message. It never wraps, because a wrapping
|
|
17
|
+
// spinner leaves debris every tick.
|
|
18
|
+
//
|
|
19
|
+
// Pure TypeScript over ./theme.js and ./text.js. No dependency, no shell.
|
|
20
|
+
|
|
21
|
+
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
22
|
+
import { paint, dim, GLYPHS } from './theme.js';
|
|
23
|
+
import { truncate, visibleWidth, safeDisplay, shortenHome } from './text.js';
|
|
24
|
+
import { contextMeter } from './block.js';
|
|
25
|
+
import { CHARS_PER_TOKEN } from '../contextManager.js';
|
|
26
|
+
|
|
27
|
+
/** 1234 -> "1.2k", 1234567 -> "1.2M". Token counts are read, not summed. */
|
|
28
|
+
export function compactCount(value: unknown): string {
|
|
29
|
+
const n = Number(value) || 0;
|
|
30
|
+
if (n < 1000) return String(Math.round(n));
|
|
31
|
+
if (n < 1000000) return `${(n / 1000).toFixed(n < 10000 ? 1 : 0)}k`;
|
|
32
|
+
return `${(n / 1000000).toFixed(1)}M`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Money, at the precision the number deserves.
|
|
37
|
+
*
|
|
38
|
+
* Sub-cent runs printed as "$0.00" read as free, which is how a user learns not
|
|
39
|
+
* to look at the cost at all.
|
|
40
|
+
*/
|
|
41
|
+
export function formatCost(value: unknown): string {
|
|
42
|
+
const n = Number(value) || 0;
|
|
43
|
+
if (n <= 0) return '$0';
|
|
44
|
+
if (n < 0.01) return `$${n.toFixed(4)}`;
|
|
45
|
+
if (n < 1) return `$${n.toFixed(3)}`;
|
|
46
|
+
return `$${n.toFixed(2)}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatElapsed(ms: number): string {
|
|
50
|
+
const total = Math.max(0, Math.round(ms / 1000));
|
|
51
|
+
if (total < 60) return `${(ms / 1000).toFixed(1)}s`;
|
|
52
|
+
const minutes = Math.floor(total / 60);
|
|
53
|
+
const seconds = total % 60;
|
|
54
|
+
if (minutes < 60) return `${minutes}m${String(seconds).padStart(2, '0')}s`;
|
|
55
|
+
return `${Math.floor(minutes / 60)}h${String(minutes % 60).padStart(2, '0')}m`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Strip a provider prefix so "anthropic/claude-opus-5" reads as the model. */
|
|
59
|
+
function shortModel(model: unknown): string {
|
|
60
|
+
const name = String(model || '').split('/').pop() || '';
|
|
61
|
+
return name.length > 22 ? name.slice(0, 21) + GLYPHS.ellipsis : name;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface StatusLineSessionInfo {
|
|
65
|
+
model?: string | undefined;
|
|
66
|
+
provider?: string | undefined;
|
|
67
|
+
tier?: string | undefined;
|
|
68
|
+
cwd?: string | undefined;
|
|
69
|
+
contextLimit?: number | string | undefined;
|
|
70
|
+
contextChars?: number | string | undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface StatusLineRenderOptions {
|
|
74
|
+
message?: string | undefined;
|
|
75
|
+
elapsedMs?: number | undefined;
|
|
76
|
+
steps?: number | undefined;
|
|
77
|
+
width?: number | undefined;
|
|
78
|
+
spinnerFrame?: string | undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface StatusLineIdleOptions {
|
|
82
|
+
width?: number | undefined;
|
|
83
|
+
hint?: string | undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class StatusLine {
|
|
87
|
+
model = '';
|
|
88
|
+
provider = '';
|
|
89
|
+
tier = '';
|
|
90
|
+
cwd = '';
|
|
91
|
+
// Both in CHARACTERS — the unit the window is actually budgeted in.
|
|
92
|
+
contextLimit = 0;
|
|
93
|
+
contextChars = 0;
|
|
94
|
+
|
|
95
|
+
turnTokens = 0;
|
|
96
|
+
turnCost = 0;
|
|
97
|
+
cachedTokens = 0;
|
|
98
|
+
|
|
99
|
+
constructor() {
|
|
100
|
+
this.reset();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Session-lifetime facts, set once from the same info as the header. */
|
|
104
|
+
setSession(info: StatusLineSessionInfo = {}): this {
|
|
105
|
+
if (info.model !== undefined) this.model = shortModel(info.model);
|
|
106
|
+
if (info.provider !== undefined) this.provider = info.provider ? String(info.provider) : '';
|
|
107
|
+
if (info.tier !== undefined) this.tier = info.tier ? String(info.tier) : '';
|
|
108
|
+
if (info.cwd !== undefined) this.cwd = info.cwd ? String(info.cwd) : '';
|
|
109
|
+
if (Number(info.contextLimit) > 0) this.contextLimit = Number(info.contextLimit);
|
|
110
|
+
if (Number(info.contextChars) > 0) this.contextChars = Number(info.contextChars);
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Per-turn counters. */
|
|
115
|
+
reset(): this {
|
|
116
|
+
this.turnTokens = 0;
|
|
117
|
+
this.turnCost = 0;
|
|
118
|
+
this.cachedTokens = 0;
|
|
119
|
+
// Note: Do NOT reset this.contextChars to 0 here. Context window usage
|
|
120
|
+
// belongs to the whole conversation, not just a single active turn.
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The idle footer: where you are, and how much context is left.
|
|
126
|
+
*/
|
|
127
|
+
renderIdle({ width = 80, hint = '' }: StatusLineIdleOptions = {}): string {
|
|
128
|
+
const budget = Math.max(10, width - 1);
|
|
129
|
+
const meter = contextMeter(
|
|
130
|
+
Math.round(this.contextChars / CHARS_PER_TOKEN),
|
|
131
|
+
Math.round(this.contextLimit / CHARS_PER_TOKEN)
|
|
132
|
+
);
|
|
133
|
+
const segments: string[] = [];
|
|
134
|
+
if (meter) segments.push(meter);
|
|
135
|
+
if (hint) segments.push(hint);
|
|
136
|
+
|
|
137
|
+
const separator = ` ${GLYPHS.dot} `;
|
|
138
|
+
const kept = segments.slice();
|
|
139
|
+
let tail = kept.join(separator);
|
|
140
|
+
while (kept.length > 1 && visibleWidth(tail) > Math.floor(budget / 2)) {
|
|
141
|
+
kept.pop();
|
|
142
|
+
tail = kept.join(separator);
|
|
143
|
+
}
|
|
144
|
+
if (visibleWidth(tail) > budget) tail = '';
|
|
145
|
+
|
|
146
|
+
const room = tail ? Math.max(0, budget - visibleWidth(tail) - visibleWidth(separator)) : budget;
|
|
147
|
+
const where = this.cwd ? truncate(shortenHome(this.cwd), room, GLYPHS.ellipsis) : '';
|
|
148
|
+
const left = dim(where);
|
|
149
|
+
if (!tail) return left;
|
|
150
|
+
|
|
151
|
+
const gap = Math.max(
|
|
152
|
+
visibleWidth(separator),
|
|
153
|
+
width - 1 - visibleWidth(left) - visibleWidth(tail)
|
|
154
|
+
);
|
|
155
|
+
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Consume a `usage_update` frame.
|
|
160
|
+
*/
|
|
161
|
+
onUsage(msg: Record<string, unknown> = {}): this {
|
|
162
|
+
this.turnTokens = Number(msg.taskTokens) || Number(msg.usedTokens) || this.turnTokens;
|
|
163
|
+
if (Number(msg.contextChars) > 0) this.contextChars = Number(msg.contextChars);
|
|
164
|
+
if (Number(msg.contextLimitChars) > 0) this.contextLimit = Number(msg.contextLimitChars);
|
|
165
|
+
const responseCost = Number(msg.costUsd) || 0;
|
|
166
|
+
const taskCost = Number(msg.taskCostUsd) || 0;
|
|
167
|
+
this.turnCost = taskCost > 0 ? taskCost : this.turnCost + responseCost;
|
|
168
|
+
this.cachedTokens = Number(msg.cachedTokens) || 0;
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Build the line.
|
|
174
|
+
*/
|
|
175
|
+
render({ message = '', elapsedMs = 0, steps = 0, width = 80, spinnerFrame = '' }: StatusLineRenderOptions = {}): string {
|
|
176
|
+
const head = spinnerFrame ? `${paint('accent', spinnerFrame)} ` : '';
|
|
177
|
+
const budget = Math.max(10, width - visibleWidth(head) - 1);
|
|
178
|
+
|
|
179
|
+
const segments: string[] = [];
|
|
180
|
+
if (elapsedMs > 0) segments.push(formatElapsed(elapsedMs));
|
|
181
|
+
if (steps > 0) segments.push(`step ${steps}`);
|
|
182
|
+
if (this.turnTokens > 0) segments.push(`${compactCount(this.turnTokens)} tok`);
|
|
183
|
+
if (this.turnCost > 0) segments.push(formatCost(this.turnCost));
|
|
184
|
+
if (this.cachedTokens > 0) segments.push(`${compactCount(this.cachedTokens)} cached`);
|
|
185
|
+
if (this.model) segments.push(this.model);
|
|
186
|
+
|
|
187
|
+
const separator = ` ${GLYPHS.dot} `;
|
|
188
|
+
const text = safeDisplay(message);
|
|
189
|
+
|
|
190
|
+
const kept = segments.slice();
|
|
191
|
+
const wanted = Math.min(visibleWidth(text), Math.max(18, Math.floor(budget / 2)));
|
|
192
|
+
while (kept.length > 0) {
|
|
193
|
+
const tail = kept.join(separator);
|
|
194
|
+
if (visibleWidth(tail) + separator.length + wanted <= budget) break;
|
|
195
|
+
kept.pop();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const tail = kept.length > 0 ? kept.join(separator) : '';
|
|
199
|
+
const messageBudget = tail
|
|
200
|
+
? Math.max(1, budget - visibleWidth(tail) - separator.length)
|
|
201
|
+
: budget;
|
|
202
|
+
const body = truncate(text, messageBudget, GLYPHS.ellipsis);
|
|
203
|
+
|
|
204
|
+
const left = `${head}${paint('muted', body)}`;
|
|
205
|
+
if (!tail) return left;
|
|
206
|
+
|
|
207
|
+
const gap = Math.max(
|
|
208
|
+
visibleWidth(separator),
|
|
209
|
+
width - 1 - visibleWidth(left) - visibleWidth(tail)
|
|
210
|
+
);
|
|
211
|
+
return `${left}${' '.repeat(gap)}${dim(tail)}`;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* The non-TTY form: no spinner, no elapsed clock, printed once.
|
|
216
|
+
*/
|
|
217
|
+
renderStatic(message: string, width = 80): string {
|
|
218
|
+
const parts = [safeDisplay(message)];
|
|
219
|
+
if (this.model) parts.push(this.model);
|
|
220
|
+
return truncate(`${GLYPHS.dot} ${parts.join(` ${GLYPHS.dot} `)}`, width, GLYPHS.ellipsis);
|
|
221
|
+
}
|
|
222
|
+
}
|