@sofer_agent/cli 0.3.11 → 0.3.13
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/dist/chat.d.ts +0 -1
- package/dist/chat.d.ts.map +1 -1
- package/dist/chat.js +24 -46
- package/dist/chat.js.map +1 -1
- package/dist/tui.d.ts +3 -37
- package/dist/tui.d.ts.map +1 -1
- package/dist/tui.js +190 -337
- package/dist/tui.jsx +170 -0
- package/dist/tui.jsx.map +1 -0
- package/package.json +23 -5
- package/src/chat.ts +24 -57
- package/src/tui.tsx +227 -0
- package/src/tui.ts +0 -371
package/src/tui.ts
DELETED
|
@@ -1,371 +0,0 @@
|
|
|
1
|
-
import type { SoferAgent, SoferConfig } from "@sofer_agent/core";
|
|
2
|
-
|
|
3
|
-
const SIDEBAR_W = 28;
|
|
4
|
-
const MIN_CHAT_W = 34;
|
|
5
|
-
const GUTTER = " ";
|
|
6
|
-
const LABEL_W = 6;
|
|
7
|
-
const INDENT = `${GUTTER}${" ".repeat(LABEL_W + 1)}`;
|
|
8
|
-
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
9
|
-
const SPINNER_MS = 80;
|
|
10
|
-
|
|
11
|
-
// Box-drawing: ╭ ─ ╮ │ ╰ ╯ ├ ┤ ┬ ┴ ┼
|
|
12
|
-
const B = {
|
|
13
|
-
tl: "╭", tr: "╮", bl: "╰", br: "╯",
|
|
14
|
-
h: "─", v: "│",
|
|
15
|
-
lt: "├", rt: "┤", tt: "┬", bt: "┴",
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export interface ChatLine {
|
|
19
|
-
role: "you" | "agent" | "system" | "error";
|
|
20
|
-
text: string;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface TuiInfo {
|
|
24
|
-
lastTurnIn: number;
|
|
25
|
-
lastTurnOut: number;
|
|
26
|
-
balance?: string;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class SoferTui {
|
|
30
|
-
private state: ChatLine[] = [];
|
|
31
|
-
private inputBuf = "";
|
|
32
|
-
private cursor = 0;
|
|
33
|
-
private columns = 90;
|
|
34
|
-
private rows = 28;
|
|
35
|
-
private chatW = 58;
|
|
36
|
-
private sideW = 26;
|
|
37
|
-
private bodyH = 22;
|
|
38
|
-
private scrollOff = 0;
|
|
39
|
-
// spinner
|
|
40
|
-
private status: "idle" | "thinking" = "idle";
|
|
41
|
-
private spinnerFrame = 0;
|
|
42
|
-
private spinnerTimer: ReturnType<typeof setInterval> | null = null;
|
|
43
|
-
private turnStartedAt = 0;
|
|
44
|
-
// interrupt
|
|
45
|
-
private abortCtrl: AbortController | null = null;
|
|
46
|
-
// sidebar
|
|
47
|
-
private info: TuiInfo = { lastTurnIn: 0, lastTurnOut: 0 };
|
|
48
|
-
// callbacks
|
|
49
|
-
private resolve: (() => void) | null = null;
|
|
50
|
-
private onLine: ((line: string, signal?: AbortSignal) => Promise<void>) | null = null;
|
|
51
|
-
|
|
52
|
-
constructor(
|
|
53
|
-
private readonly agent: SoferAgent,
|
|
54
|
-
private readonly config: SoferConfig,
|
|
55
|
-
) {}
|
|
56
|
-
|
|
57
|
-
private measure(): void {
|
|
58
|
-
this.columns = Math.max(80, process.stdout.columns ?? 80);
|
|
59
|
-
this.rows = Math.max(24, process.stdout.rows ?? 24);
|
|
60
|
-
this.chatW = Math.max(MIN_CHAT_W, this.columns - SIDEBAR_W - 3);
|
|
61
|
-
this.sideW = this.columns - this.chatW - 3;
|
|
62
|
-
this.bodyH = this.rows - 5; // top border, input box(3), footer(1)
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
addMessage(role: ChatLine["role"], text: string): void {
|
|
66
|
-
this.state.push({ role, text });
|
|
67
|
-
this.scrollOff = Number.MAX_SAFE_INTEGER; // will be clamped to maxScroll in buildFrame
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
setInfo(info: Partial<TuiInfo>): void {
|
|
71
|
-
Object.assign(this.info, info);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
private startSpinner(): void {
|
|
75
|
-
if (this.spinnerTimer) return;
|
|
76
|
-
this.status = "thinking";
|
|
77
|
-
this.turnStartedAt = Date.now();
|
|
78
|
-
this.spinnerFrame = 0;
|
|
79
|
-
this.spinnerTimer = setInterval(() => {
|
|
80
|
-
this.spinnerFrame = (this.spinnerFrame + 1) % SPINNER_FRAMES.length;
|
|
81
|
-
this.render();
|
|
82
|
-
}, SPINNER_MS);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private stopSpinner(): void {
|
|
86
|
-
this.status = "idle";
|
|
87
|
-
this.turnStartedAt = 0;
|
|
88
|
-
if (this.spinnerTimer) {
|
|
89
|
-
clearInterval(this.spinnerTimer);
|
|
90
|
-
this.spinnerTimer = null;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async run(onLine: (line: string, signal?: AbortSignal) => Promise<void>): Promise<void> {
|
|
95
|
-
this.onLine = onLine;
|
|
96
|
-
this.measure();
|
|
97
|
-
if (process.stdin.isTTY) process.stdin.setRawMode?.(true);
|
|
98
|
-
process.stdin.on("data", this.handleInput);
|
|
99
|
-
process.stdout.on("resize", this.handleResize);
|
|
100
|
-
process.on("SIGWINCH", this.handleResize);
|
|
101
|
-
this.render();
|
|
102
|
-
return new Promise<void>((resolve) => { this.resolve = resolve; });
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private shutdown(): void {
|
|
106
|
-
this.stopSpinner();
|
|
107
|
-
process.stdin.setRawMode?.(false);
|
|
108
|
-
process.stdin.removeAllListeners("data");
|
|
109
|
-
process.stdout.removeAllListeners("resize");
|
|
110
|
-
process.removeAllListeners("SIGWINCH");
|
|
111
|
-
this.render();
|
|
112
|
-
process.stdout.write("\n");
|
|
113
|
-
this.resolve?.();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// ── Input handling ──────────────────────────────────────────────────────
|
|
117
|
-
|
|
118
|
-
private handleInput = (data: Buffer): void => {
|
|
119
|
-
const str = data.toString();
|
|
120
|
-
for (const ch of str) {
|
|
121
|
-
const code = ch.charCodeAt(0);
|
|
122
|
-
if (code === 3) { this.shutdown(); return; }
|
|
123
|
-
if (code === 4 && this.inputBuf.length === 0) { this.shutdown(); return; }
|
|
124
|
-
if (code === 27) {
|
|
125
|
-
if (this.status === "thinking" && this.abortCtrl) { this.abortCtrl.abort(); return; }
|
|
126
|
-
this.inputBuf = "";
|
|
127
|
-
this.cursor = 0;
|
|
128
|
-
this.render();
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
if (this.status === "thinking") continue;
|
|
132
|
-
if (code === 13) { this.submit(); return; }
|
|
133
|
-
if (code === 127) {
|
|
134
|
-
if (this.cursor > 0) {
|
|
135
|
-
this.inputBuf = this.inputBuf.slice(0, this.cursor - 1) + this.inputBuf.slice(this.cursor);
|
|
136
|
-
this.cursor--;
|
|
137
|
-
}
|
|
138
|
-
this.render();
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
if (ch >= " ") {
|
|
142
|
-
this.inputBuf = this.inputBuf.slice(0, this.cursor) + ch + this.inputBuf.slice(this.cursor);
|
|
143
|
-
this.cursor++;
|
|
144
|
-
this.render();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
private async submit(): Promise<void> {
|
|
150
|
-
const line = this.inputBuf.trim();
|
|
151
|
-
this.inputBuf = "";
|
|
152
|
-
this.cursor = 0;
|
|
153
|
-
if (!line) { this.render(); return; }
|
|
154
|
-
if (line === "/exit" || line === "/quit") { this.shutdown(); return; }
|
|
155
|
-
this.addMessage("you", line);
|
|
156
|
-
// scrollOff already set to MAX by addMessage (pinned to bottom)
|
|
157
|
-
this.abortCtrl = new AbortController();
|
|
158
|
-
this.startSpinner();
|
|
159
|
-
this.render();
|
|
160
|
-
await this.onLine?.(line, this.abortCtrl.signal);
|
|
161
|
-
this.abortCtrl = null;
|
|
162
|
-
this.stopSpinner();
|
|
163
|
-
this.render();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// ── Rendering ───────────────────────────────────────────────────────────
|
|
167
|
-
|
|
168
|
-
private handleResize = (): void => { this.measure(); this.render(); };
|
|
169
|
-
|
|
170
|
-
render(): void {
|
|
171
|
-
this.measure();
|
|
172
|
-
process.stdout.write("\x1b[?25l\x1b[H" + this.buildFrame() + "\x1b[?25h");
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
private buildFrame(): string {
|
|
176
|
-
const { columns, chatW, sideW, bodyH } = this;
|
|
177
|
-
const innerSideW = sideW - 2;
|
|
178
|
-
const sep = `${B.lt}${B.h.repeat(chatW)}${B.bt}${B.h.repeat(sideW)}${B.rt}`;
|
|
179
|
-
const inputW = columns - 2; // inside border
|
|
180
|
-
|
|
181
|
-
let buf = "";
|
|
182
|
-
|
|
183
|
-
// ── Top border ────────────────────────────────────────────────────────
|
|
184
|
-
buf += `${B.tl}${B.h.repeat(chatW)}${B.tt}${B.h.repeat(sideW)}${B.tr}\n`;
|
|
185
|
-
|
|
186
|
-
// ── Body ──────────────────────────────────────────────────────────────
|
|
187
|
-
const wrapped = this.state.flatMap((m) => this.wrapMessage(m, chatW));
|
|
188
|
-
const maxScroll = Math.max(0, wrapped.length - bodyH);
|
|
189
|
-
if (this.scrollOff > maxScroll) this.scrollOff = maxScroll;
|
|
190
|
-
if (this.scrollOff < 0) this.scrollOff = 0;
|
|
191
|
-
const visible = wrapped.slice(this.scrollOff, this.scrollOff + bodyH);
|
|
192
|
-
|
|
193
|
-
const sidebarLines = this.buildSidebar(innerSideW);
|
|
194
|
-
const sbTop = Math.max(0, Math.floor((bodyH - sidebarLines.length) / 2));
|
|
195
|
-
|
|
196
|
-
for (let r = 0; r < bodyH; r++) {
|
|
197
|
-
buf += B.v;
|
|
198
|
-
buf += visible[r] ? padTrunc(visible[r]!, chatW) : " ".repeat(chatW);
|
|
199
|
-
buf += B.v;
|
|
200
|
-
const sbIdx = r - sbTop;
|
|
201
|
-
const sbLine = (sbIdx >= 0 && sbIdx < sidebarLines.length) ? sidebarLines[sbIdx]! : "";
|
|
202
|
-
buf += ` ${padRight(sbLine, innerSideW)} `;
|
|
203
|
-
buf += `${B.v}\n`;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// ── Separator ─────────────────────────────────────────────────────────
|
|
207
|
-
buf += `${sep}\n`;
|
|
208
|
-
|
|
209
|
-
// ── Spinner row ───────────────────────────────────────────────────────
|
|
210
|
-
if (this.status === "thinking") {
|
|
211
|
-
const frame = SPINNER_FRAMES[this.spinnerFrame]!;
|
|
212
|
-
const elapsed = this.turnStartedAt ? formatElapsed(Date.now() - this.turnStartedAt) : "";
|
|
213
|
-
const spinnerText = `${frame} thinking\u2026${elapsed ? ` ${elapsed}` : ""} (esc to interrupt)`;
|
|
214
|
-
buf += `${B.v} \x1b[36m${spinnerText}\x1b[0m${" ".repeat(Math.max(0, inputW - 1 - spinnerText.length))}${B.v}\n`;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// ── Input bar (bordered box) ─────────────────────────────────────────
|
|
218
|
-
buf += `${B.tl}${B.h.repeat(inputW)}${B.tr}\n`;
|
|
219
|
-
const cursorCh = this.status === "idle" ? "\x1b[5m▋\x1b[25m" : "";
|
|
220
|
-
const inputMaxW = inputW - 3; // "> " prefix + 1 padding
|
|
221
|
-
const visibleInput = this.inputBuf.length > inputMaxW
|
|
222
|
-
? this.inputBuf.slice(this.inputBuf.length - inputMaxW)
|
|
223
|
-
: this.inputBuf;
|
|
224
|
-
const inputPad = inputMaxW - visibleInput.length;
|
|
225
|
-
buf += `${B.v} \x1b[36m>\x1b[0m ${visibleInput}${cursorCh}${" ".repeat(Math.max(0, inputPad))} ${B.v}\n`;
|
|
226
|
-
buf += `${B.bl}${B.h.repeat(inputW)}${B.br}\n`;
|
|
227
|
-
|
|
228
|
-
// ── Footer ────────────────────────────────────────────────────────────
|
|
229
|
-
const totalIn = this.agent.usage.tokens.inputTokens;
|
|
230
|
-
const totalOut = this.agent.usage.tokens.outputTokens;
|
|
231
|
-
const cost = this.agent.usage.costUsd.toFixed(4);
|
|
232
|
-
const ft = `\x1b[90m${this.agent.address.slice(0, 10)}… · in ${totalIn} out ${totalOut} $${cost} /exit\x1b[0m`;
|
|
233
|
-
buf += `${padTrunc(ft, columns)}\n`;
|
|
234
|
-
|
|
235
|
-
// ── Cursor position ───────────────────────────────────────────────────
|
|
236
|
-
// Row layout: 1(top border) + bodyH(body) + 1(sep) + [1 spinner] + 1(input top) + 1(input content) = bodyH+4|5
|
|
237
|
-
const cursorRow = bodyH + 3 + (this.status === "thinking" ? 1 : 0); // 1-indexed: body+sep+[spinner]+input-top+input-content
|
|
238
|
-
const cursorCol = 4 + Math.min(this.cursor, inputMaxW);
|
|
239
|
-
buf += `\x1b[${cursorRow};${cursorCol}H`;
|
|
240
|
-
|
|
241
|
-
return buf;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// ── Messages ────────────────────────────────────────────────────────────
|
|
245
|
-
|
|
246
|
-
private wrapMessage(msg: ChatLine, width: number): string[] {
|
|
247
|
-
const label = this.roleLabel(msg.role);
|
|
248
|
-
const bodyWidth = Math.max(10, width - GUTTER.length - LABEL_W - 1);
|
|
249
|
-
const lines = wrapText(msg.text, bodyWidth);
|
|
250
|
-
return lines.map((l, i) =>
|
|
251
|
-
i === 0 ? `${GUTTER}${label} ${l}` : `${INDENT}${l}`,
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
private roleLabel(role: ChatLine["role"]): string {
|
|
256
|
-
switch (role) {
|
|
257
|
-
case "you": return "\x1b[36myou \x1b[0m";
|
|
258
|
-
case "agent": return `\x1b[32m${this.agent.name.toLowerCase().padEnd(LABEL_W)}\x1b[0m`;
|
|
259
|
-
case "system": return "\x1b[90msys \x1b[0m";
|
|
260
|
-
case "error": return "\x1b[31merr \x1b[0m";
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
// ── Sidebar ─────────────────────────────────────────────────────────────
|
|
265
|
-
|
|
266
|
-
private buildSidebar(width: number): string[] {
|
|
267
|
-
const { agent, config, info } = this;
|
|
268
|
-
const addr = `${agent.address.slice(0, 6)}…${agent.address.slice(-4)}`;
|
|
269
|
-
const totalIn = agent.usage.tokens.inputTokens;
|
|
270
|
-
const totalOut = agent.usage.tokens.outputTokens;
|
|
271
|
-
const cost = agent.usage.costUsd.toFixed(6);
|
|
272
|
-
|
|
273
|
-
const rows = [
|
|
274
|
-
`\x1b[1;36m${agent.name}\x1b[0m`,
|
|
275
|
-
`\x1b[90m${addr}\x1b[0m`,
|
|
276
|
-
"",
|
|
277
|
-
`${B.h.repeat(width)}`,
|
|
278
|
-
`\x1b[90mnetwork\x1b[0m ${config.network}`,
|
|
279
|
-
`\x1b[90mmodel\x1b[0m ${shortModel(config.brain.model)}`,
|
|
280
|
-
];
|
|
281
|
-
|
|
282
|
-
if (info.balance) {
|
|
283
|
-
rows.push("", `\x1b[90mbalance\x1b[0m \x1b[33m${info.balance} SUI\x1b[0m`);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
rows.push(
|
|
287
|
-
"",
|
|
288
|
-
`\x1b[90m▸ last turn\x1b[0m`,
|
|
289
|
-
` in ${info.lastTurnIn}`,
|
|
290
|
-
` out ${info.lastTurnOut}`,
|
|
291
|
-
"",
|
|
292
|
-
`\x1b[90m▸ total\x1b[0m`,
|
|
293
|
-
` in ${totalIn}`,
|
|
294
|
-
` out ${totalOut}`,
|
|
295
|
-
` \x1b[33m$${cost}\x1b[0m`,
|
|
296
|
-
);
|
|
297
|
-
|
|
298
|
-
return rows;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
303
|
-
|
|
304
|
-
function wrapText(text: string, width: number): string[] {
|
|
305
|
-
if (!text) return [""];
|
|
306
|
-
const lines: string[] = [];
|
|
307
|
-
// First split on hard newlines, then word-wrap each paragraph
|
|
308
|
-
for (const para of text.split("\n")) {
|
|
309
|
-
if (!para) { lines.push(""); continue; }
|
|
310
|
-
const words = para.split(" ");
|
|
311
|
-
let cur = "";
|
|
312
|
-
for (const w of words) {
|
|
313
|
-
// Break words longer than width
|
|
314
|
-
if (w.length > width) {
|
|
315
|
-
if (cur) { lines.push(cur); cur = ""; }
|
|
316
|
-
for (let i = 0; i < w.length; i += width) lines.push(w.slice(i, i + width));
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
if (cur && cur.length + 1 + w.length > width) { lines.push(cur); cur = w; }
|
|
320
|
-
else cur = cur ? `${cur} ${w}` : w;
|
|
321
|
-
}
|
|
322
|
-
if (cur) lines.push(cur);
|
|
323
|
-
}
|
|
324
|
-
return lines.length > 0 ? lines : [""];
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
function stripAnsi(s: string): string {
|
|
328
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape stripping
|
|
329
|
-
return s.replace(/\x1b\[\d*;?\d*m/g, "");
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
function visibleLen(s: string): number {
|
|
333
|
-
// biome-ignore lint/suspicious/noControlCharactersInRegex: ANSI escape stripping
|
|
334
|
-
const re = /\x1b\[\d*;?\d*m/g;
|
|
335
|
-
let len = 0;
|
|
336
|
-
let last = 0;
|
|
337
|
-
let m = re.exec(s);
|
|
338
|
-
while (m !== null) {
|
|
339
|
-
len += m.index - last;
|
|
340
|
-
last = m.index + m[0].length;
|
|
341
|
-
m = re.exec(s);
|
|
342
|
-
}
|
|
343
|
-
return len + s.length - last;
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function padRight(s: string, w: number): string {
|
|
347
|
-
const vl = visibleLen(s);
|
|
348
|
-
return vl >= w ? s : s + " ".repeat(w - vl);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
function padTrunc(s: string, w: number): string {
|
|
352
|
-
const stripped = stripAnsi(s);
|
|
353
|
-
if (stripped.length <= w) return s + " ".repeat(w - stripped.length);
|
|
354
|
-
return stripped.slice(0, w);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
function formatElapsed(ms: number): string {
|
|
358
|
-
const sec = Math.floor(ms / 1000);
|
|
359
|
-
if (sec < 60) return `${sec}s`;
|
|
360
|
-
const m = Math.floor(sec / 60);
|
|
361
|
-
return `${m}m${String(sec % 60).padStart(2, "0")}s`;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
function shortModel(model: string): string {
|
|
365
|
-
const m = model.toLowerCase();
|
|
366
|
-
if (m.includes("sonnet")) return "Sonnet 4";
|
|
367
|
-
if (m.includes("haiku")) return "Haiku";
|
|
368
|
-
if (m.includes("opus")) return "Opus 4";
|
|
369
|
-
if (m.includes("fable")) return "Fable 5";
|
|
370
|
-
return model.length > 14 ? `${model.slice(0, 13)}…` : model;
|
|
371
|
-
}
|