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