@anusornneal/chat-relay 0.11.1 → 0.12.0
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/README.md +21 -2
- package/agent/capability-scheduler.mjs +3 -1
- package/agent/connection-state.mjs +19 -0
- package/agent/desktop-manager.mjs +4 -3
- package/agent/lifecycle.mjs +68 -4
- package/agent/local-agent.mjs +34 -10
- package/agent/macos-desktop-runner.mjs +10 -4
- package/agent/toolcall-summary.mjs +157 -41
- package/agent/windows-desktop-worker.ps1 +12 -5
- package/cli/config.mjs +45 -9
- package/cli/index.mjs +37 -5
- package/cli/relay-health.mjs +185 -0
- package/cli/remote.mjs +37 -5
- package/cli/secret-store.mjs +223 -0
- package/cli/self-update.mjs +88 -0
- package/cli/tui.mjs +388 -43
- package/package.json +8 -2
package/cli/tui.mjs
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import { humanizeToolCall } from "../agent/toolcall-summary.mjs";
|
|
2
2
|
|
|
3
3
|
const ESC = "\x1b[";
|
|
4
|
+
const RESET = "\x1b[0m";
|
|
5
|
+
const STYLE = {
|
|
6
|
+
reset: RESET,
|
|
7
|
+
bold: "\x1b[1m",
|
|
8
|
+
dim: "\x1b[2m",
|
|
9
|
+
cyan: "\x1b[36m",
|
|
10
|
+
green: "\x1b[32m",
|
|
11
|
+
yellow: "\x1b[33m",
|
|
12
|
+
red: "\x1b[31m",
|
|
13
|
+
gray: "\x1b[90m",
|
|
14
|
+
};
|
|
4
15
|
const LABEL_WIDTH = 12;
|
|
5
16
|
const GAP = 3;
|
|
6
17
|
const MIN_FRAME_WIDTH = 60;
|
|
7
18
|
const MAX_FRAME_WIDTH = 179;
|
|
8
|
-
const FRAME_FIXED_ROWS =
|
|
19
|
+
const FRAME_FIXED_ROWS = 12;
|
|
9
20
|
const FRAME_BOTTOM_MARGIN = 1;
|
|
10
21
|
|
|
11
22
|
function clamp(value, min, max) {
|
|
@@ -20,31 +31,153 @@ function truncate(value, width) {
|
|
|
20
31
|
return text.slice(0, width - 1) + "…";
|
|
21
32
|
}
|
|
22
33
|
|
|
23
|
-
function
|
|
34
|
+
function codePointCellWidth(codePoint) {
|
|
35
|
+
if (codePoint === 0) return 0;
|
|
36
|
+
if (codePoint < 32 || (codePoint >= 0x7f && codePoint < 0xa0)) return 0;
|
|
37
|
+
if (
|
|
38
|
+
(codePoint >= 0x0300 && codePoint <= 0x036f)
|
|
39
|
+
|| (codePoint >= 0x1ab0 && codePoint <= 0x1aff)
|
|
40
|
+
|| (codePoint >= 0x1dc0 && codePoint <= 0x1dff)
|
|
41
|
+
|| (codePoint >= 0x20d0 && codePoint <= 0x20ff)
|
|
42
|
+
|| (codePoint >= 0xfe20 && codePoint <= 0xfe2f)
|
|
43
|
+
|| codePoint === 0xfe0f
|
|
44
|
+
|| codePoint === 0x200d
|
|
45
|
+
) return 0;
|
|
46
|
+
if (
|
|
47
|
+
codePoint >= 0x1100 && (
|
|
48
|
+
codePoint <= 0x115f
|
|
49
|
+
|| codePoint === 0x2329
|
|
50
|
+
|| codePoint === 0x232a
|
|
51
|
+
|| (codePoint >= 0x2e80 && codePoint <= 0xa4cf && codePoint !== 0x303f)
|
|
52
|
+
|| (codePoint >= 0xac00 && codePoint <= 0xd7a3)
|
|
53
|
+
|| (codePoint >= 0xf900 && codePoint <= 0xfaff)
|
|
54
|
+
|| (codePoint >= 0xfe10 && codePoint <= 0xfe19)
|
|
55
|
+
|| (codePoint >= 0xfe30 && codePoint <= 0xfe6f)
|
|
56
|
+
|| (codePoint >= 0xff00 && codePoint <= 0xff60)
|
|
57
|
+
|| (codePoint >= 0xffe0 && codePoint <= 0xffe6)
|
|
58
|
+
|| (codePoint >= 0x1f000 && codePoint <= 0x1faff)
|
|
59
|
+
|| (codePoint >= 0x20000 && codePoint <= 0x3fffd)
|
|
60
|
+
)
|
|
61
|
+
) return 2;
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function terminalCellWidth(value) {
|
|
66
|
+
let width = 0;
|
|
67
|
+
for (const char of String(value ?? "")) width += codePointCellWidth(char.codePointAt(0));
|
|
68
|
+
return width;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function truncateCells(value, width) {
|
|
72
|
+
const text = String(value ?? "");
|
|
73
|
+
const limit = Math.max(0, Number(width) || 0);
|
|
74
|
+
if (limit === 0) return "";
|
|
75
|
+
if (terminalCellWidth(text) <= limit) return text;
|
|
76
|
+
if (limit === 1) return "…";
|
|
77
|
+
let output = "";
|
|
78
|
+
let used = 0;
|
|
79
|
+
for (const char of text) {
|
|
80
|
+
const cells = codePointCellWidth(char.codePointAt(0));
|
|
81
|
+
if (used + cells > limit - 1) break;
|
|
82
|
+
output += char;
|
|
83
|
+
used += cells;
|
|
84
|
+
}
|
|
85
|
+
return output + "…";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function paint(value, ...styles) {
|
|
89
|
+
if (!styles.length) return String(value ?? "");
|
|
90
|
+
return styles.join("") + String(value ?? "") + RESET;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function field(label, value, width, color = false, valueStyle = null) {
|
|
24
94
|
const valueWidth = Math.max(1, width - LABEL_WIDTH);
|
|
25
|
-
|
|
95
|
+
const plainLabel = String(label).padEnd(LABEL_WIDTH);
|
|
96
|
+
const plainValue = truncate(value, valueWidth);
|
|
97
|
+
const plain = truncate(plainLabel + plainValue, width).padEnd(width);
|
|
98
|
+
if (!color) return plain;
|
|
99
|
+
const renderedLabel = paint(plainLabel, STYLE.gray);
|
|
100
|
+
const renderedValue = valueStyle ? paint(plainValue, valueStyle) : plainValue;
|
|
101
|
+
return renderedLabel + renderedValue + " ".repeat(Math.max(0, width - plainLabel.length - plainValue.length));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function statusStyle(value) {
|
|
105
|
+
switch (String(value || "").toLowerCase()) {
|
|
106
|
+
case "connected": return STYLE.green;
|
|
107
|
+
case "connecting":
|
|
108
|
+
case "reconnecting": return STYLE.yellow;
|
|
109
|
+
case "offline": return STYLE.red;
|
|
110
|
+
default: return STYLE.gray;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function relayHealthStyle(health) {
|
|
115
|
+
switch (health?.state) {
|
|
116
|
+
case "reachable": return STYLE.green;
|
|
117
|
+
case "checking": return STYLE.yellow;
|
|
118
|
+
default: return STYLE.red;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function relayHealthText(health) {
|
|
123
|
+
switch (health?.state) {
|
|
124
|
+
case "reachable": return "● Online";
|
|
125
|
+
case "checking": return "◐ Checking";
|
|
126
|
+
case "limited":
|
|
127
|
+
return health.code === 1027
|
|
128
|
+
? "✕ Cloudflare 1027"
|
|
129
|
+
: `✕ Rate limited (${health.code || "429"})`;
|
|
130
|
+
case "cloudflare-error": return `✕ Cloudflare ${health.code || "error"}`;
|
|
131
|
+
case "unauthorized": return health.code === 403 ? "✕ Access denied" : "✕ Sign-in required";
|
|
132
|
+
case "reauthorize": return "✕ Agent reauthorization required";
|
|
133
|
+
case "agent-disabled": return "✕ Agent disabled";
|
|
134
|
+
case "agent-access": return "✕ Agent access required";
|
|
135
|
+
case "unavailable":
|
|
136
|
+
if (health.reason === "relay_unavailable") return "✕ Relay unavailable";
|
|
137
|
+
if (health.reason === "agent_offline") return "✕ Agent offline";
|
|
138
|
+
return `✕ ${health.code ? `HTTP ${health.code}` : "Unavailable"}`;
|
|
139
|
+
default: return "? Unknown";
|
|
140
|
+
}
|
|
26
141
|
}
|
|
27
142
|
|
|
28
|
-
|
|
143
|
+
function relayHealthAge(health, now = Date.now()) {
|
|
144
|
+
const checkedAt = Number(health?.checkedAt);
|
|
145
|
+
if (!Number.isFinite(checkedAt)) return "pending";
|
|
146
|
+
const ageSeconds = Math.max(0, Math.floor((now - checkedAt) / 1000));
|
|
147
|
+
if (ageSeconds < 10) return "just now";
|
|
148
|
+
if (ageSeconds < 60) return `${ageSeconds}s ago`;
|
|
149
|
+
const ageMinutes = Math.floor(ageSeconds / 60);
|
|
150
|
+
if (ageMinutes < 60) return `${ageMinutes}m ago`;
|
|
151
|
+
return `${Math.floor(ageMinutes / 60)}h ago`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function formatTwoColumnHeader(state, width = 110, { color = false } = {}) {
|
|
29
155
|
const usable = clamp(Number(width) || 110, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
|
|
30
156
|
const leftWidth = Math.floor((usable - GAP) / 2);
|
|
31
157
|
const rightWidth = usable - GAP - leftWidth;
|
|
32
158
|
const rows = [
|
|
33
159
|
["Account", state.account || "-", "Agent", state.agent || "-"],
|
|
34
|
-
["
|
|
35
|
-
["
|
|
36
|
-
["
|
|
160
|
+
["Agent link", statusText(state.status), "Relay", state.relay || "-"],
|
|
161
|
+
["Relay API", relayHealthText(state.relayHealth), "Reconnects", String(state.reconnects ?? 0)],
|
|
162
|
+
["Session up", formatUptime(state.uptimeMs), "Terminal", state.terminal ? "enabled" : "disabled"],
|
|
163
|
+
["Desktop", state.desktop ? "enabled" : "disabled", "Checked", relayHealthAge(state.relayHealth)],
|
|
37
164
|
];
|
|
38
165
|
return rows
|
|
39
|
-
.map(([leftLabel, leftValue, rightLabel, rightValue]) =>
|
|
40
|
-
|
|
41
|
-
|
|
166
|
+
.map(([leftLabel, leftValue, rightLabel, rightValue]) => {
|
|
167
|
+
const leftStyle = leftLabel === "Agent link"
|
|
168
|
+
? statusStyle(state.status)
|
|
169
|
+
: leftLabel === "Relay API" ? relayHealthStyle(state.relayHealth) : null;
|
|
170
|
+
const rightStyle = rightLabel === "Reconnects" && Number(state.reconnects || 0) > 0 ? STYLE.yellow : null;
|
|
171
|
+
return field(leftLabel, leftValue, leftWidth, color, leftStyle)
|
|
172
|
+
+ " ".repeat(GAP)
|
|
173
|
+
+ field(rightLabel, rightValue, rightWidth, color, rightStyle).trimEnd();
|
|
174
|
+
})
|
|
42
175
|
.join("\n");
|
|
43
176
|
}
|
|
44
177
|
|
|
45
178
|
function statusText(value) {
|
|
46
179
|
switch (String(value || "").toLowerCase()) {
|
|
47
|
-
case "connected": return "●
|
|
180
|
+
case "connected": return "● WebSocket open";
|
|
48
181
|
case "connecting": return "◐ Connecting";
|
|
49
182
|
case "reconnecting": return "◐ Reconnecting";
|
|
50
183
|
case "stopping": return "○ Stopping";
|
|
@@ -74,14 +207,68 @@ function clock(value) {
|
|
|
74
207
|
return date.toLocaleTimeString("en-GB", { hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit" });
|
|
75
208
|
}
|
|
76
209
|
|
|
77
|
-
|
|
78
|
-
const
|
|
210
|
+
function wrapText(value, width, maxLines = 3) {
|
|
211
|
+
const text = String(value ?? "").replace(/\s+/g, " ").trim();
|
|
212
|
+
const usable = Math.max(1, Number(width) || 1);
|
|
213
|
+
if (!text) return [""];
|
|
214
|
+
const lines = [];
|
|
215
|
+
let remaining = text;
|
|
216
|
+
|
|
217
|
+
while (remaining && lines.length < maxLines) {
|
|
218
|
+
if (remaining.length <= usable) {
|
|
219
|
+
lines.push(remaining);
|
|
220
|
+
remaining = "";
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let cut = remaining.lastIndexOf(" ", usable);
|
|
225
|
+
if (cut < Math.floor(usable * 0.45)) cut = usable;
|
|
226
|
+
lines.push(remaining.slice(0, cut).trimEnd());
|
|
227
|
+
remaining = remaining.slice(cut).trimStart();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (remaining && lines.length) {
|
|
231
|
+
const index = lines.length - 1;
|
|
232
|
+
lines[index] = truncate(lines[index] + " " + remaining, usable);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return lines.length ? lines : [""];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function formatTransactionRows(item, width = 110, { color = false } = {}) {
|
|
239
|
+
const requested = Number(width) || 110;
|
|
240
|
+
const usable = clamp(requested, Math.min(60, Math.max(1, requested)), 180);
|
|
79
241
|
const status = item.status === "running" ? "●" : item.ok === false ? "✕" : "✓";
|
|
242
|
+
if (usable < 32) {
|
|
243
|
+
const compact = truncateCells(`${clock(item.at)} ${status} ${item.summary || item.action || "Tool call"}`, usable);
|
|
244
|
+
if (!color) return [compact];
|
|
245
|
+
const glyphStyle = item.status === "running" ? STYLE.yellow : item.ok === false ? STYLE.red : STYLE.green;
|
|
246
|
+
return [paint(compact, glyphStyle)];
|
|
247
|
+
}
|
|
80
248
|
const prefix = `${clock(item.at)} ${status} `;
|
|
249
|
+
const continuationPrefix = " ".repeat(prefix.length);
|
|
81
250
|
const duration = formatDuration(item.durationMs, item.status);
|
|
82
251
|
const suffix = " " + duration.padStart(8);
|
|
83
|
-
const summaryWidth = Math.max(
|
|
84
|
-
|
|
252
|
+
const summaryWidth = Math.max(4, usable - prefix.length - suffix.length);
|
|
253
|
+
const summaryLines = wrapText(item.summary || item.action || "Tool call", summaryWidth, 3);
|
|
254
|
+
|
|
255
|
+
return summaryLines.map((line, index) => {
|
|
256
|
+
if (index === 0) {
|
|
257
|
+
if (!color) return prefix + line.padEnd(summaryWidth) + suffix;
|
|
258
|
+
const time = paint(clock(item.at), STYLE.gray);
|
|
259
|
+
const glyphStyle = item.status === "running" ? STYLE.yellow : item.ok === false ? STYLE.red : STYLE.green;
|
|
260
|
+
const glyph = paint(status, glyphStyle, STYLE.bold);
|
|
261
|
+
const summary = item.status === "running" ? paint(line.padEnd(summaryWidth), STYLE.bold) : line.padEnd(summaryWidth);
|
|
262
|
+
const timing = paint(suffix, STYLE.gray);
|
|
263
|
+
return `${time} ${glyph} ${summary}${timing}`;
|
|
264
|
+
}
|
|
265
|
+
if (!color) return continuationPrefix + line;
|
|
266
|
+
return " ".repeat(prefix.length) + paint(line, STYLE.dim);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function formatTransactionRow(item, width = 110, options) {
|
|
271
|
+
return formatTransactionRows(item, width, options).join("\n");
|
|
85
272
|
}
|
|
86
273
|
|
|
87
274
|
function normalizeRelay(value) {
|
|
@@ -99,31 +286,77 @@ export function shouldUseTui(options = {}, stdout = process.stdout) {
|
|
|
99
286
|
return Boolean(stdout?.isTTY);
|
|
100
287
|
}
|
|
101
288
|
|
|
289
|
+
export function shouldUseColor(stdout = process.stdout, env = process.env) {
|
|
290
|
+
if (!stdout?.isTTY) return false;
|
|
291
|
+
if (Object.prototype.hasOwnProperty.call(env || {}, "NO_COLOR")) return false;
|
|
292
|
+
if (String(env?.TERM || "").toLowerCase() === "dumb") return false;
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
|
|
102
296
|
export class RemoteTui {
|
|
103
|
-
constructor({
|
|
297
|
+
constructor({
|
|
298
|
+
config,
|
|
299
|
+
version,
|
|
300
|
+
output = process.stdout,
|
|
301
|
+
input = process.stdin,
|
|
302
|
+
env = process.env,
|
|
303
|
+
onInterrupt = () => process.emit("SIGINT"),
|
|
304
|
+
}) {
|
|
104
305
|
this.output = output;
|
|
306
|
+
this.input = input;
|
|
307
|
+
this.onInterrupt = onInterrupt;
|
|
105
308
|
this.startedAt = Date.now();
|
|
106
309
|
this.state = {
|
|
107
310
|
account: config.user?.name || config.user?.login || "signed in",
|
|
108
311
|
agent: config.agentName || config.agentId || "-",
|
|
109
312
|
relay: normalizeRelay(config.relayUrl),
|
|
110
313
|
status: "starting",
|
|
314
|
+
relayHealth: { state: "checking" },
|
|
111
315
|
reconnects: 0,
|
|
112
316
|
terminal: config.terminalEnabled !== false,
|
|
113
317
|
desktop: config.desktopEnabled === true,
|
|
114
318
|
};
|
|
115
319
|
this.version = version || "-";
|
|
320
|
+
this.colorEnabled = shouldUseColor(output, env);
|
|
116
321
|
this.transactions = [];
|
|
117
322
|
this.timer = null;
|
|
118
323
|
this.closed = false;
|
|
119
324
|
this.renderPending = false;
|
|
325
|
+
this.scrollOffset = 0;
|
|
326
|
+
this.unseenTransactions = 0;
|
|
327
|
+
this.inputWasRaw = false;
|
|
328
|
+
this.inputWasPaused = false;
|
|
329
|
+
this.inputFlowingState = null;
|
|
330
|
+
this.mouseEnabled = false;
|
|
331
|
+
this.onInput = (chunk) => this.handleInput(chunk);
|
|
120
332
|
}
|
|
121
333
|
|
|
122
334
|
start() {
|
|
123
335
|
if (this.closed) return;
|
|
124
336
|
// Use the terminal's alternate screen so periodic redraws never accumulate
|
|
125
|
-
// in scrollback.
|
|
126
|
-
|
|
337
|
+
// in scrollback. Mouse reporting is enabled only when stdin can actually
|
|
338
|
+
// consume the events, otherwise ordinary terminal scrollback remains usable.
|
|
339
|
+
const interactiveInput = Boolean(this.input?.isTTY && typeof this.input.setRawMode === "function");
|
|
340
|
+
if (interactiveInput) {
|
|
341
|
+
this.inputWasRaw = Boolean(this.input.isRaw);
|
|
342
|
+
this.inputWasPaused = Boolean(this.input.isPaused?.());
|
|
343
|
+
this.inputFlowingState = this.input.readableFlowing ?? null;
|
|
344
|
+
try {
|
|
345
|
+
this.input.setRawMode(true);
|
|
346
|
+
this.input.resume?.();
|
|
347
|
+
this.input.on?.("data", this.onInput);
|
|
348
|
+
this.mouseEnabled = true;
|
|
349
|
+
} catch {
|
|
350
|
+
this.mouseEnabled = false;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
this.output.write(
|
|
354
|
+
ESC + "?1049h"
|
|
355
|
+
+ ESC + "?25l"
|
|
356
|
+
+ (this.mouseEnabled ? ESC + "?1000h" + ESC + "?1006h" : "")
|
|
357
|
+
+ ESC + "2J"
|
|
358
|
+
+ ESC + "H",
|
|
359
|
+
);
|
|
127
360
|
this.render();
|
|
128
361
|
this.timer = setInterval(() => this.render(), 1000);
|
|
129
362
|
this.timer.unref?.();
|
|
@@ -134,7 +367,92 @@ export class RemoteTui {
|
|
|
134
367
|
this.closed = true;
|
|
135
368
|
if (this.timer) clearInterval(this.timer);
|
|
136
369
|
this.timer = null;
|
|
137
|
-
this.
|
|
370
|
+
this.input?.removeListener?.("data", this.onInput);
|
|
371
|
+
if (this.input?.isTTY && typeof this.input.setRawMode === "function" && !this.inputWasRaw) {
|
|
372
|
+
try { this.input.setRawMode(false); } catch {}
|
|
373
|
+
}
|
|
374
|
+
if (this.inputFlowingState !== true) this.input?.pause?.();
|
|
375
|
+
this.output.write(
|
|
376
|
+
(this.mouseEnabled ? ESC + "?1000l" + ESC + "?1006l" : "")
|
|
377
|
+
+ ESC + "?25h"
|
|
378
|
+
+ ESC + "?1049l",
|
|
379
|
+
);
|
|
380
|
+
this.mouseEnabled = false;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
viewportMetrics() {
|
|
384
|
+
const terminalWidth = Number(this.output.columns) || 110;
|
|
385
|
+
const width = clamp(terminalWidth - 1, Math.min(MIN_FRAME_WIDTH, Math.max(1, terminalWidth - 1)), MAX_FRAME_WIDTH);
|
|
386
|
+
const height = Math.max(1, Number(this.output.rows) || 30);
|
|
387
|
+
const compact = height < 14;
|
|
388
|
+
const headerRows = compact ? Math.max(1, Math.min(2, height - 3)) : 5;
|
|
389
|
+
const fixedRows = compact ? 2 + headerRows : FRAME_FIXED_ROWS;
|
|
390
|
+
const availableRows = Math.max(1, height - fixedRows - FRAME_BOTTOM_MARGIN);
|
|
391
|
+
return { width, height, availableRows, compact, headerRows };
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
transactionRows(width) {
|
|
395
|
+
return this.transactions.flatMap((item) => formatTransactionRows(item, width, { color: this.colorEnabled }));
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
maxScrollOffset() {
|
|
399
|
+
const { width, availableRows } = this.viewportMetrics();
|
|
400
|
+
return Math.max(0, this.transactionRows(width).length - availableRows);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
scrollBy(lines) {
|
|
404
|
+
const next = clamp(this.scrollOffset + Number(lines || 0), 0, this.maxScrollOffset());
|
|
405
|
+
if (next === this.scrollOffset) return;
|
|
406
|
+
this.scrollOffset = next;
|
|
407
|
+
if (this.scrollOffset === 0) this.unseenTransactions = 0;
|
|
408
|
+
this.scheduleRender();
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
handleInput(chunk) {
|
|
412
|
+
const data = Buffer.isBuffer(chunk) ? chunk.toString("utf8") : String(chunk || "");
|
|
413
|
+
if (!data) return;
|
|
414
|
+
|
|
415
|
+
// Raw mode turns Ctrl+C into input data, so forward it to the existing
|
|
416
|
+
// shutdown handler instead of swallowing it.
|
|
417
|
+
if (data.includes("\x03")) {
|
|
418
|
+
this.onInterrupt?.();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
let handledMouse = false;
|
|
423
|
+
const mouse = /\x1b\[<(\d+);\d+;\d+[Mm]/g;
|
|
424
|
+
let match;
|
|
425
|
+
while ((match = mouse.exec(data)) !== null) {
|
|
426
|
+
const button = Number(match[1]);
|
|
427
|
+
if ((button & 64) !== 64) continue;
|
|
428
|
+
this.scrollBy((button & 1) === 0 ? 3 : -3);
|
|
429
|
+
handledMouse = true;
|
|
430
|
+
}
|
|
431
|
+
if (handledMouse) return;
|
|
432
|
+
|
|
433
|
+
if (data.includes("\x1b[5~")) return this.scrollBy(this.viewportMetrics().availableRows);
|
|
434
|
+
if (data.includes("\x1b[6~")) return this.scrollBy(-this.viewportMetrics().availableRows);
|
|
435
|
+
if (data.includes("\x1b[H") || data.includes("\x1b[1~")) {
|
|
436
|
+
this.scrollOffset = this.maxScrollOffset();
|
|
437
|
+
return this.scheduleRender();
|
|
438
|
+
}
|
|
439
|
+
if (data.includes("\x1b[F") || data.includes("\x1b[4~")) {
|
|
440
|
+
this.scrollOffset = 0;
|
|
441
|
+
this.unseenTransactions = 0;
|
|
442
|
+
return this.scheduleRender();
|
|
443
|
+
}
|
|
444
|
+
if (data.includes("\x1b[A")) return this.scrollBy(1);
|
|
445
|
+
if (data.includes("\x1b[B")) return this.scrollBy(-1);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
appendTransaction(item) {
|
|
449
|
+
if (this.scrollOffset > 0) {
|
|
450
|
+
const { width } = this.viewportMetrics();
|
|
451
|
+
this.scrollOffset += formatTransactionRows(item, width, { color: false }).length;
|
|
452
|
+
this.unseenTransactions += 1;
|
|
453
|
+
}
|
|
454
|
+
this.transactions.push(item);
|
|
455
|
+
if (this.transactions.length > 100) this.transactions.splice(0, this.transactions.length - 100);
|
|
138
456
|
}
|
|
139
457
|
|
|
140
458
|
handleMessage(message) {
|
|
@@ -143,7 +461,7 @@ export class RemoteTui {
|
|
|
143
461
|
this.state.status = message.state || this.state.status;
|
|
144
462
|
if (Number.isFinite(Number(message.reconnects))) this.state.reconnects = Number(message.reconnects);
|
|
145
463
|
} else if (message.event === "tool:start") {
|
|
146
|
-
this.
|
|
464
|
+
this.appendTransaction({
|
|
147
465
|
requestId: message.requestId,
|
|
148
466
|
at: message.at || new Date().toISOString(),
|
|
149
467
|
action: message.action,
|
|
@@ -151,7 +469,6 @@ export class RemoteTui {
|
|
|
151
469
|
status: "running",
|
|
152
470
|
ok: null,
|
|
153
471
|
});
|
|
154
|
-
if (this.transactions.length > 100) this.transactions.splice(0, this.transactions.length - 100);
|
|
155
472
|
} else if (message.event === "tool:end") {
|
|
156
473
|
const item = [...this.transactions].reverse().find((entry) => entry.requestId === message.requestId);
|
|
157
474
|
if (item) {
|
|
@@ -161,18 +478,26 @@ export class RemoteTui {
|
|
|
161
478
|
if (message.error && message.ok === false) item.summary += ` · ${truncate(message.error, 48)}`;
|
|
162
479
|
}
|
|
163
480
|
} else if (message.event === "system") {
|
|
164
|
-
this.
|
|
481
|
+
this.appendTransaction({
|
|
165
482
|
requestId: "system-" + Date.now() + "-" + Math.random(),
|
|
166
483
|
at: message.at || new Date().toISOString(),
|
|
167
484
|
summary: message.message || "Agent event",
|
|
168
485
|
status: "done",
|
|
169
486
|
ok: message.level !== "error",
|
|
170
487
|
});
|
|
171
|
-
if (this.transactions.length > 100) this.transactions.splice(0, this.transactions.length - 100);
|
|
172
488
|
}
|
|
173
489
|
this.scheduleRender();
|
|
174
490
|
}
|
|
175
491
|
|
|
492
|
+
setRelayHealth(health) {
|
|
493
|
+
if (this.closed) return;
|
|
494
|
+
this.state.relayHealth = {
|
|
495
|
+
...(health && typeof health === "object" ? health : { state: "unavailable" }),
|
|
496
|
+
checkedAt: Date.now(),
|
|
497
|
+
};
|
|
498
|
+
this.scheduleRender();
|
|
499
|
+
}
|
|
500
|
+
|
|
176
501
|
scheduleRender() {
|
|
177
502
|
if (this.closed || this.renderPending) return;
|
|
178
503
|
this.renderPending = true;
|
|
@@ -184,31 +509,51 @@ export class RemoteTui {
|
|
|
184
509
|
|
|
185
510
|
render() {
|
|
186
511
|
if (this.closed) return;
|
|
187
|
-
const
|
|
188
|
-
const width = clamp(terminalWidth - 1, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
|
|
189
|
-
const height = Math.max(16, Number(this.output.rows) || 30);
|
|
512
|
+
const { width, height, availableRows, compact, headerRows } = this.viewportMetrics();
|
|
190
513
|
const separator = "─".repeat(width);
|
|
191
|
-
const
|
|
514
|
+
const headerState = {
|
|
192
515
|
...this.state,
|
|
193
516
|
uptimeMs: Date.now() - this.startedAt,
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
517
|
+
};
|
|
518
|
+
let headerLines;
|
|
519
|
+
if (width < MIN_FRAME_WIDTH) {
|
|
520
|
+
const compactHeader = [
|
|
521
|
+
truncateCells(`👤 ${headerState.account || "-"} · 💻 ${headerState.agent || "-"}`, width),
|
|
522
|
+
truncateCells(`🔗 ${statusText(headerState.status)} · ${relayHealthText(headerState.relayHealth)}`, width),
|
|
523
|
+
];
|
|
524
|
+
headerLines = compactHeader.map((line) => this.colorEnabled ? paint(line, STYLE.gray) : line);
|
|
525
|
+
} else {
|
|
526
|
+
headerLines = formatTwoColumnHeader(headerState, width, { color: this.colorEnabled }).split("\n");
|
|
527
|
+
}
|
|
528
|
+
if (compact) headerLines = headerLines.slice(0, headerRows);
|
|
529
|
+
|
|
530
|
+
const allRows = this.transactionRows(width);
|
|
531
|
+
const maxOffset = Math.max(0, allRows.length - availableRows);
|
|
532
|
+
this.scrollOffset = clamp(this.scrollOffset, 0, maxOffset);
|
|
533
|
+
const end = Math.max(0, allRows.length - this.scrollOffset);
|
|
534
|
+
const start = Math.max(0, end - availableRows);
|
|
535
|
+
const rows = allRows.slice(start, end);
|
|
199
536
|
while (rows.length < Math.min(availableRows, 4)) rows.unshift("");
|
|
200
537
|
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
538
|
+
const titleText = truncateCells(`🚀 Chat Relay v${this.version}`, width);
|
|
539
|
+
const title = this.colorEnabled ? paint(titleText, STYLE.bold, STYLE.cyan) : titleText;
|
|
540
|
+
const divider = this.colorEnabled ? paint(separator, STYLE.gray) : separator;
|
|
541
|
+
const mode = this.scrollOffset === 0
|
|
542
|
+
? "🟢 LIVE"
|
|
543
|
+
: this.unseenTransactions > 0
|
|
544
|
+
? `📬 +${this.unseenTransactions} new`
|
|
545
|
+
: "📜 HISTORY";
|
|
546
|
+
const sectionText = truncateCells(
|
|
547
|
+
`📜 Transactions ${mode} · wheel/↑↓ scroll · End = live`,
|
|
548
|
+
width,
|
|
549
|
+
);
|
|
550
|
+
const section = this.colorEnabled ? paint(sectionText, STYLE.bold, STYLE.cyan) : sectionText;
|
|
551
|
+
|
|
552
|
+
const screenLines = compact
|
|
553
|
+
? [title, ...headerLines, section, ...rows]
|
|
554
|
+
: [title, divider, "", ...headerLines, "", divider, section, "", ...rows];
|
|
555
|
+
const maxLines = Math.max(1, height - FRAME_BOTTOM_MARGIN);
|
|
556
|
+
const screen = screenLines.slice(0, maxLines).join("\n");
|
|
212
557
|
|
|
213
558
|
this.output.write(ESC + "2J" + ESC + "H" + screen);
|
|
214
559
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anusornneal/chat-relay",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Zero-checkout remote MCP agent for ChatGPT with terminal, filesystem, process, multi-user, and multi-agent support.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"logout": "node bin/chat-relay.js logout",
|
|
26
26
|
"status": "node bin/chat-relay.js status",
|
|
27
27
|
"dev": "wrangler dev",
|
|
28
|
-
"dev:test": "wrangler dev --local --port 8790 --var AGENT_TOKEN:test-agent --var CALLER_TOKEN
|
|
28
|
+
"dev:test": "wrangler dev --local --port 8790 --var AGENT_TOKEN:test-agent --var CALLER_TOKEN:test-caller --var ADMIN_TOKEN:test-admin --var OPENAI_APPS_CHALLENGE:test-openai-apps-challenge",
|
|
29
29
|
"deploy": "wrangler deploy",
|
|
30
30
|
"agent": "node --env-file-if-exists=.dev.vars agent/local-agent.mjs",
|
|
31
31
|
"admin": "node scripts/admin.mjs",
|
|
@@ -50,6 +50,12 @@
|
|
|
50
50
|
"test:device-management": "node test/device-management-smoke.mjs",
|
|
51
51
|
"test:submission": "node test/submission-smoke.mjs",
|
|
52
52
|
"test:agent-state": "node --experimental-strip-types --test test/agent-state.test.mjs",
|
|
53
|
+
"test:tui": "node --test test/cli-tui.test.mjs",
|
|
54
|
+
"test:config-security": "node --test test/config-security.test.mjs",
|
|
55
|
+
"test:self-update": "node --test test/self-update.test.mjs test/agent-lifecycle-upgrade.test.mjs",
|
|
56
|
+
"test:budget": "node --test test/relay-health.test.mjs test/usage-budget.test.mjs",
|
|
57
|
+
"test:soak": "node test/soak-smoke.mjs",
|
|
58
|
+
"test:hardening": "npm run test:tui && npm run test:config-security && npm run test:self-update && npm run test:budget && node --test test/connection-generation.test.mjs && npm run test:agent-state && node test/heartbeat-ack-watchdog.test.mjs && npm run test:usage-storage && npm run test:soak",
|
|
53
59
|
"verify:review": "node scripts/verify-review-artifacts.mjs"
|
|
54
60
|
},
|
|
55
61
|
"dependencies": {
|