@anusornneal/chat-relay 1.7.0 → 1.9.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 +54 -0
- package/agent/capability-scheduler.mjs +8 -1
- package/agent/file-manager.mjs +183 -77
- package/agent/fs-batch-preview.mjs +70 -0
- package/agent/lifecycle.mjs +7 -1
- package/agent/local-agent.mjs +36 -11
- package/agent/local-usage.mjs +254 -0
- package/agent/protocol.mjs +5 -1
- package/agent/terminal-manager.mjs +89 -0
- package/agent/terminal-output.mjs +136 -0
- package/agent/toolcall-context.mjs +13 -5
- package/agent/toolcall-summary.mjs +11 -1
- package/package.json +6 -5
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { createTerminalSessionId } from "./terminal-continuity.mjs";
|
|
3
|
+
|
|
4
|
+
const MODES = new Set(["full", "tail", "summary", "errors"]);
|
|
5
|
+
const ERROR_LINE = /(?:\b(?:error|warning|fatal|failed|failure|exception|traceback|caused by)\b|^\s*[ew]:)/i;
|
|
6
|
+
const EXEC_REPLAY_TTL = 10 * 60_000;
|
|
7
|
+
const MAX_EXEC_REPLAYS = 16;
|
|
8
|
+
const REPLAY_CHUNK_CHARS = 1024;
|
|
9
|
+
const REPLAY_RESPONSE_BYTES = 40 * 1024;
|
|
10
|
+
|
|
11
|
+
export function outputLimit(value) {
|
|
12
|
+
if (value === undefined) return 2048;
|
|
13
|
+
if (!Number.isInteger(value) || value < 256 || value > 8192) throw new Error("invalid_output_chars");
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function validateOutputMode(mode = "full") {
|
|
18
|
+
if (!MODES.has(mode)) throw new Error("invalid_output_mode");
|
|
19
|
+
return mode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function tail(text, limit) {
|
|
23
|
+
if (limit <= 0) return "";
|
|
24
|
+
if (text.length <= limit) return text;
|
|
25
|
+
let start = text.length - limit;
|
|
26
|
+
if (start > 0 && /[\uDC00-\uDFFF]/.test(text[start])) start++;
|
|
27
|
+
return text.slice(start);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function diagnostic(text) {
|
|
31
|
+
return text.split(/\r?\n/).filter(line => ERROR_LINE.test(line)).join("\n");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function failed(result) {
|
|
35
|
+
return result.ok === false || ["failed", "cancelled", "killed"].includes(result.status) || Boolean(result.signal) ||
|
|
36
|
+
(typeof result.exitCode === "number" && result.exitCode !== 0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A compact view is a projection of captured output, never a new execution.
|
|
40
|
+
export function compactTerminalResult(result, mode, limit = 2048) {
|
|
41
|
+
validateOutputMode(mode);
|
|
42
|
+
if (mode === "full") return result;
|
|
43
|
+
const stdout = typeof result.stdout === "string" ? result.stdout : "";
|
|
44
|
+
const stderr = typeof result.stderr === "string" ? result.stderr : "";
|
|
45
|
+
let out = stdout, err = stderr;
|
|
46
|
+
if (mode === "summary" && !failed(result)) { out = ""; err = ""; }
|
|
47
|
+
else if (mode === "errors") {
|
|
48
|
+
out = diagnostic(stdout);
|
|
49
|
+
if (!out && !err && failed(result)) out = stdout;
|
|
50
|
+
}
|
|
51
|
+
const errBudget = out ? Math.min(err.length, Math.ceil(limit / 2)) : limit;
|
|
52
|
+
err = tail(err, errBudget);
|
|
53
|
+
out = tail(out, Math.max(0, limit - err.length));
|
|
54
|
+
return { ...result, stdout: out, stderr: err, outputMode: mode,
|
|
55
|
+
capturedChars: stdout.length + stderr.length,
|
|
56
|
+
omittedChars: Math.max(0, stdout.length + stderr.length - out.length - err.length) };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function compactTerminalPage(page, mode, limit, afterSeq) {
|
|
60
|
+
validateOutputMode(mode);
|
|
61
|
+
if (mode === "full" || !Array.isArray(page.chunks)) return page;
|
|
62
|
+
const raw = page.chunks;
|
|
63
|
+
const capturedChars = raw.reduce((sum, c) => sum + c.text.length, 0);
|
|
64
|
+
let remaining = limit;
|
|
65
|
+
const chunks = [];
|
|
66
|
+
for (let i = raw.length - 1; i >= 0 && remaining > 0; i--) {
|
|
67
|
+
const c = raw[i];
|
|
68
|
+
let text = c.text;
|
|
69
|
+
if (mode === "summary" && !failed(page)) text = "";
|
|
70
|
+
else if (mode === "errors" && c.stream !== "stderr") text = diagnostic(text);
|
|
71
|
+
if (!text) continue;
|
|
72
|
+
text = tail(text, remaining);
|
|
73
|
+
if (text) { chunks.push({ ...c, text }); remaining -= text.length; }
|
|
74
|
+
}
|
|
75
|
+
if (!chunks.length && failed(page) && capturedChars && mode === "errors") {
|
|
76
|
+
for (let i = raw.length - 1; i >= 0 && remaining > 0; i--) {
|
|
77
|
+
const text = tail(raw[i].text, remaining);
|
|
78
|
+
if (text) { chunks.push({ ...raw[i], text }); remaining -= text.length; }
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
chunks.reverse();
|
|
82
|
+
return { ...page, chunks, outputMode: mode, capturedChars, omittedChars: capturedChars - (limit - remaining),
|
|
83
|
+
// Re-read the original page with full mode and this cursor, not nextSeq.
|
|
84
|
+
rawAfterSeq: afterSeq };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Bounded process-local replay for compact short commands. No disk or extra RPC.
|
|
88
|
+
export class CompactExecOutputStore {
|
|
89
|
+
constructor(processEpoch, now = Date.now) {
|
|
90
|
+
this.processEpoch = processEpoch;
|
|
91
|
+
this.now = now;
|
|
92
|
+
this.entries = new Map();
|
|
93
|
+
}
|
|
94
|
+
#prune() {
|
|
95
|
+
const now = this.now();
|
|
96
|
+
for (const [id, entry] of this.entries) if (entry.expiresAt <= now) this.entries.delete(id);
|
|
97
|
+
}
|
|
98
|
+
record(result, startedAt, endedAt) {
|
|
99
|
+
this.#prune();
|
|
100
|
+
while (this.entries.size >= MAX_EXEC_REPLAYS) this.entries.delete(this.entries.keys().next().value);
|
|
101
|
+
const sessionId = createTerminalSessionId(this.processEpoch, randomUUID());
|
|
102
|
+
const chunks = [];
|
|
103
|
+
for (const stream of ["stdout", "stderr"]) {
|
|
104
|
+
const text = typeof result[stream] === "string" ? result[stream] : "";
|
|
105
|
+
for (let start = 0; start < text.length;) {
|
|
106
|
+
let end = Math.min(text.length, start + REPLAY_CHUNK_CHARS);
|
|
107
|
+
if (end < text.length && /[\uDC00-\uDFFF]/.test(text[end])) end--;
|
|
108
|
+
chunks.push({ seq: chunks.length + 1, stream, text: text.slice(start, end) });
|
|
109
|
+
start = end;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.entries.set(sessionId, { expiresAt: this.now() + EXEC_REPLAY_TTL, chunks,
|
|
113
|
+
summary: { sessionId, processEpoch: this.processEpoch, type: "exec-output", status: "exited",
|
|
114
|
+
exitCode: result.exitCode ?? null, startedAt, endedAt, error: result.error ?? null,
|
|
115
|
+
captureLimited: /maxBuffer|timed out/i.test(String(result.error || "")) } });
|
|
116
|
+
return sessionId;
|
|
117
|
+
}
|
|
118
|
+
read(sessionId, afterSeq = 0, maxChars = 24576) {
|
|
119
|
+
this.#prune();
|
|
120
|
+
const entry = this.entries.get(sessionId);
|
|
121
|
+
if (!entry) return null;
|
|
122
|
+
const limit = Math.min(Math.max(Number(maxChars) || 24576, 1024), 24576);
|
|
123
|
+
const chunks = [];
|
|
124
|
+
let chars = 0, bytes = 0;
|
|
125
|
+
for (const chunk of entry.chunks) {
|
|
126
|
+
if (chunk.seq <= afterSeq) continue;
|
|
127
|
+
const chunkBytes = Buffer.byteLength(JSON.stringify(chunk), "utf8");
|
|
128
|
+
if (chunks.length && (chars + chunk.text.length > limit || bytes + chunkBytes > REPLAY_RESPONSE_BYTES)) break;
|
|
129
|
+
chunks.push(chunk);
|
|
130
|
+
chars += chunk.text.length; bytes += chunkBytes;
|
|
131
|
+
}
|
|
132
|
+
const nextSeq = chunks.at(-1)?.seq ?? afterSeq;
|
|
133
|
+
return { ...entry.summary, chunks, oldestSeq: 1, nextSeq,
|
|
134
|
+
truncated: false, hasMore: nextSeq < entry.chunks.length };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -65,20 +65,20 @@ function candidatePaths(payload) {
|
|
|
65
65
|
.map((job) => ({ value: job && typeof job === "object" ? job.cwd || fallbackCwd : fallbackCwd, directory: true }));
|
|
66
66
|
}
|
|
67
67
|
if (action.startsWith("terminal.")) {
|
|
68
|
-
if (action === "terminal.exec" || action === "terminal.start" || action === "terminal.shell.start") {
|
|
68
|
+
if (action === "terminal.exec" || action === "terminal.exec.compact" || action === "terminal.start" || action === "terminal.shell.start") {
|
|
69
69
|
return [{ value: payload.cwd, directory: true }];
|
|
70
70
|
}
|
|
71
71
|
return [];
|
|
72
72
|
}
|
|
73
|
-
if (
|
|
73
|
+
if (["fs.batch", "fs.batch.compact", "fs.editMany"].includes(action)) {
|
|
74
74
|
return (Array.isArray(payload.operations) ? payload.operations : [])
|
|
75
75
|
.flatMap((item) => [item?.path, item?.source, item?.destination]
|
|
76
76
|
.filter((value) => typeof value === "string")
|
|
77
|
-
.map((value) => ({ value, cwd: payload.cwd, directory: item?.op === "list" })));
|
|
77
|
+
.map((value) => ({ value, cwd: payload.basePath || payload.cwd, directory: item?.op === "list" || item?.op === "search" })));
|
|
78
78
|
}
|
|
79
|
-
if (action === "fs.readMany") {
|
|
79
|
+
if (action === "fs.readMany" || action === "fs.readMany.relative") {
|
|
80
80
|
return (Array.isArray(payload.paths) ? payload.paths : [])
|
|
81
|
-
.map((item) => ({ value: typeof item === "string" ? item : item?.path, cwd: payload.cwd }));
|
|
81
|
+
.map((item) => ({ value: typeof item === "string" ? item : item?.path, cwd: payload.basePath || payload.cwd }));
|
|
82
82
|
}
|
|
83
83
|
if (action === "fs.move") {
|
|
84
84
|
return [{ value: payload.source, cwd: payload.cwd }, { value: payload.destination, cwd: payload.cwd }];
|
|
@@ -183,9 +183,14 @@ export function describeToolActivity(payload = {}, allowedRoots = [], linkedCont
|
|
|
183
183
|
case "fs.read":
|
|
184
184
|
operation = "Read " + shortName(payload.path);
|
|
185
185
|
break;
|
|
186
|
+
case "fs.readMany.relative":
|
|
186
187
|
case "fs.readMany":
|
|
187
188
|
operation = "Read " + count(payload.paths) + " files";
|
|
188
189
|
break;
|
|
190
|
+
case "fs.editMany":
|
|
191
|
+
operation = "Edit " + count(payload.operations) + " files";
|
|
192
|
+
break;
|
|
193
|
+
case "fs.batch.compact":
|
|
189
194
|
case "fs.batch":
|
|
190
195
|
operation = "Inspect " + count(payload.operations) + " filesystem items";
|
|
191
196
|
break;
|
|
@@ -195,6 +200,7 @@ export function describeToolActivity(payload = {}, allowedRoots = [], linkedCont
|
|
|
195
200
|
case "fs.list":
|
|
196
201
|
operation = "Browse " + shortName(payload.path);
|
|
197
202
|
break;
|
|
203
|
+
case "terminal.exec.compact":
|
|
198
204
|
case "terminal.exec":
|
|
199
205
|
case "terminal.start":
|
|
200
206
|
operation = terminalTitle(payload);
|
|
@@ -202,12 +208,14 @@ export function describeToolActivity(payload = {}, allowedRoots = [], linkedCont
|
|
|
202
208
|
case "terminal.batch.start":
|
|
203
209
|
operation = "Run " + count(payload.jobs) + " terminal tasks";
|
|
204
210
|
break;
|
|
211
|
+
case "terminal.batch.read.compact":
|
|
205
212
|
case "terminal.batch.read":
|
|
206
213
|
operation = "Review terminal batch results";
|
|
207
214
|
break;
|
|
208
215
|
case "terminal.batch.status":
|
|
209
216
|
operation = "Check terminal batch progress";
|
|
210
217
|
break;
|
|
218
|
+
case "terminal.read.compact":
|
|
211
219
|
case "terminal.read":
|
|
212
220
|
operation = "Review terminal output";
|
|
213
221
|
break;
|
|
@@ -164,6 +164,9 @@ export function humanizeToolCall(payload = {}) {
|
|
|
164
164
|
return "Check agent connection";
|
|
165
165
|
case "agent.config":
|
|
166
166
|
return "Read agent configuration";
|
|
167
|
+
case "agent.usageStats":
|
|
168
|
+
return `Read ${number(payload.days, 30)} days of local usage statistics`;
|
|
169
|
+
case "agent.recentCalls.page":
|
|
167
170
|
case "agent.recentCalls":
|
|
168
171
|
return `Read recent activity · up to ${number(payload.limit, 50)} calls`;
|
|
169
172
|
case "agent.lifecycle.status":
|
|
@@ -181,8 +184,12 @@ export function humanizeToolCall(payload = {}) {
|
|
|
181
184
|
return `Browse ${compactPath(payload.path)} · depth ${number(payload.depth)} · up to ${number(payload.limit, 100)} items`;
|
|
182
185
|
case "fs.read":
|
|
183
186
|
return `Read ${compactPath(payload.path)} · ${lineRange(payload)}`;
|
|
187
|
+
case "fs.readMany.relative":
|
|
184
188
|
case "fs.readMany":
|
|
185
189
|
return `Read ${Array.isArray(payload.paths) ? payload.paths.length : 0} files`;
|
|
190
|
+
case "fs.editMany":
|
|
191
|
+
return `Edit ${Array.isArray(payload.operations) ? payload.operations.length : 0} files`;
|
|
192
|
+
case "fs.batch.compact":
|
|
186
193
|
case "fs.batch":
|
|
187
194
|
return `Inspect filesystem · ${Array.isArray(payload.operations) ? payload.operations.length : 0} operations`;
|
|
188
195
|
case "fs.artifact":
|
|
@@ -212,14 +219,16 @@ export function humanizeToolCall(payload = {}) {
|
|
|
212
219
|
case "process.kill":
|
|
213
220
|
return `Stop process ${number(payload.pid)}`;
|
|
214
221
|
|
|
222
|
+
case "terminal.exec.compact":
|
|
215
223
|
case "terminal.exec":
|
|
216
224
|
return withCwd(humanizeCommand(payload.command), payload.cwd);
|
|
217
225
|
case "terminal.start":
|
|
218
226
|
return withCwd(`Start background task · ${humanizeCommand(payload.command).replace(/^Run /, "")}`, payload.cwd);
|
|
219
227
|
case "terminal.shell.start":
|
|
220
228
|
return withCwd("Open interactive shell", payload.cwd);
|
|
229
|
+
case "terminal.read.compact":
|
|
221
230
|
case "terminal.read":
|
|
222
|
-
return `Read terminal output · ${cleanText(payload.sessionId, 24)} · after #${number(payload.afterSeq)}`;
|
|
231
|
+
return `Read terminal output · ${cleanText(payload.sessionId, 24)} · after #${number(payload.afterSeq)}${Number(payload.waitMs) > 0 ? ` · wait up to ${number(payload.waitMs)}ms` : ""}`;
|
|
223
232
|
case "terminal.write":
|
|
224
233
|
return `Send input to terminal · ${String(payload.input ?? "").length} chars`;
|
|
225
234
|
case "terminal.kill":
|
|
@@ -232,6 +241,7 @@ export function humanizeToolCall(payload = {}) {
|
|
|
232
241
|
return `Run terminal batch · ${Array.isArray(payload.jobs) ? payload.jobs.length : 0} jobs · ${number(payload.concurrency, 1)} at a time`;
|
|
233
242
|
case "terminal.batch.status":
|
|
234
243
|
return `Check batch progress · ${cleanText(payload.batchId, 24)}`;
|
|
244
|
+
case "terminal.batch.read.compact":
|
|
235
245
|
case "terminal.batch.read":
|
|
236
246
|
return `Read batch output · ${cleanText(payload.batchId, 24)}`;
|
|
237
247
|
case "terminal.batch.cancel":
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anusornneal/chat-relay",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.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": {
|
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
"prepublishOnly": "npm run verify:publish",
|
|
44
44
|
"test:oauth": "node test/oauth-smoke.mjs",
|
|
45
45
|
"test:usage": "node test/usage-smoke.mjs",
|
|
46
|
-
"test:usage-storage": "node test/usage-storage.test.mjs && node --test test/usage-batch.test.mjs",
|
|
46
|
+
"test:usage-storage": "node test/usage-storage.test.mjs && node --test test/usage-batch.test.mjs test/usage-fourhour-profile.test.mjs",
|
|
47
47
|
"test:no-quota": "node test/no-quota-regression.test.mjs",
|
|
48
48
|
"test:admin-api": "node test/admin-api-smoke.mjs",
|
|
49
49
|
"test:admin-auth": "node test/admin-auth-smoke.mjs",
|
|
50
|
-
"test:dashboard": "node test/dashboard-smoke.mjs",
|
|
50
|
+
"test:dashboard": "node --test test/dashboard-ui-contract.test.mjs test/dashboard-theme.test.mjs && node test/dashboard-smoke.mjs",
|
|
51
51
|
"test:dashboard-timers": "node test/dashboard-timers.test.mjs",
|
|
52
52
|
"test:ops": "node test/ops-smoke.mjs",
|
|
53
53
|
"test:desktop": "node --test test/agent-desktop-adapter.test.mjs && node test/mcp-desktop-smoke.mjs",
|
|
@@ -61,9 +61,10 @@
|
|
|
61
61
|
"test:soak": "node test/soak-smoke.mjs",
|
|
62
62
|
"test:file-search": "node --test test/file-search.test.mjs test/search-preview.test.mjs test/adaptive-tools.test.mjs",
|
|
63
63
|
"bench:search-preview:e2e": "node test/search-preview-e2e-benchmark.mjs",
|
|
64
|
-
"test:hardening": "npm run test:file-search && npm run test:batch-contract && npm run test:terminal-perf && npm run test:tool-guidance && npm run test:tui && npm run test:config-security && npm run test:self-update && npm run test:budget && npm run test:worker-hotpath && node --test test/connection-generation.test.mjs && npm run test:agent-state && node test/heartbeat-ack-watchdog.test.mjs && npm run test:no-quota && npm run test:usage-storage && npm run test:learn && npm run test:learn-bootstrap && npm run test:soak",
|
|
64
|
+
"test:hardening": "npm run test:file-search && npm run test:batch-contract && npm run test:tool-efficiency && npm run test:terminal-perf && npm run test:tool-guidance && npm run test:tui && npm run test:config-security && npm run test:self-update && npm run test:budget && npm run test:worker-hotpath && node --test test/connection-generation.test.mjs && npm run test:agent-state && node test/heartbeat-ack-watchdog.test.mjs && npm run test:no-quota && npm run test:usage-storage && npm run test:learn && npm run test:learn-bootstrap && npm run test:soak",
|
|
65
65
|
"test:batch-contract": "node --test test/tool-contract-batch.test.mjs",
|
|
66
|
-
"test:
|
|
66
|
+
"test:tool-efficiency": "node --test --test-concurrency=1 test/tool-efficiency.test.mjs test/tool-efficiency-mcp.test.mjs",
|
|
67
|
+
"test:terminal-perf": "node --test test/terminal-perf.test.mjs test/terminal-wait.test.mjs",
|
|
67
68
|
"test:tool-guidance": "node --test test/tool-error-guidance.test.mjs",
|
|
68
69
|
"verify:review": "node scripts/verify-review-artifacts.mjs",
|
|
69
70
|
"test:learn": "node test/learning-smoke.mjs && node test/project-alias-sqlite.test.mjs && node test/project-merge-mcp.test.mjs && node --test test/shared-learn.test.mjs test/shared-learn-ui.test.mjs && npm run test:learn-usage",
|