arc402-cli 0.4.3 → 0.6.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/dist/client.d.ts.map +1 -1
- package/dist/client.js +3 -1
- package/dist/client.js.map +1 -1
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +6 -0
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/wallet.d.ts.map +1 -1
- package/dist/commands/wallet.js +414 -20
- package/dist/commands/wallet.js.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -0
- package/dist/config.js.map +1 -1
- package/dist/index.js +22 -111
- package/dist/index.js.map +1 -1
- package/dist/program.d.ts +3 -0
- package/dist/program.d.ts.map +1 -0
- package/dist/program.js +85 -0
- package/dist/program.js.map +1 -0
- package/dist/repl.d.ts +2 -0
- package/dist/repl.d.ts.map +1 -0
- package/dist/repl.js +669 -0
- package/dist/repl.js.map +1 -0
- package/dist/ui/banner.d.ts +2 -0
- package/dist/ui/banner.d.ts.map +1 -1
- package/dist/ui/banner.js +27 -18
- package/dist/ui/banner.js.map +1 -1
- package/dist/ui/spinner.d.ts.map +1 -1
- package/dist/ui/spinner.js +11 -0
- package/dist/ui/spinner.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +3 -1
- package/src/commands/config.ts +6 -0
- package/src/commands/wallet.ts +458 -32
- package/src/config.ts +1 -0
- package/src/index.ts +22 -115
- package/src/program.ts +83 -0
- package/src/repl.ts +793 -0
- package/src/ui/banner.ts +26 -19
- package/src/ui/spinner.ts +10 -0
package/dist/repl.js
ADDED
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.startREPL = startREPL;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
const os_1 = __importDefault(require("os"));
|
|
11
|
+
const program_1 = require("./program");
|
|
12
|
+
const banner_1 = require("./ui/banner");
|
|
13
|
+
const colors_1 = require("./ui/colors");
|
|
14
|
+
// ─── Sentinel to intercept process.exit() from commands ──────────────────────
|
|
15
|
+
class REPLExitSignal extends Error {
|
|
16
|
+
constructor(code = 0) {
|
|
17
|
+
super("repl-exit-signal");
|
|
18
|
+
this.code = code;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// ─── Config / banner helpers ──────────────────────────────────────────────────
|
|
22
|
+
const CONFIG_PATH = path_1.default.join(os_1.default.homedir(), ".arc402", "config.json");
|
|
23
|
+
async function loadBannerConfig() {
|
|
24
|
+
if (!fs_1.default.existsSync(CONFIG_PATH))
|
|
25
|
+
return undefined;
|
|
26
|
+
try {
|
|
27
|
+
const raw = JSON.parse(fs_1.default.readFileSync(CONFIG_PATH, "utf-8"));
|
|
28
|
+
const cfg = { network: raw.network };
|
|
29
|
+
if (raw.walletContractAddress) {
|
|
30
|
+
const w = raw.walletContractAddress;
|
|
31
|
+
cfg.wallet = `${w.slice(0, 6)}...${w.slice(-4)}`;
|
|
32
|
+
}
|
|
33
|
+
if (raw.rpcUrl && raw.walletContractAddress) {
|
|
34
|
+
try {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
36
|
+
const ethersLib = require("ethers");
|
|
37
|
+
const provider = new ethersLib.ethers.JsonRpcProvider(raw.rpcUrl);
|
|
38
|
+
const bal = await Promise.race([
|
|
39
|
+
provider.getBalance(raw.walletContractAddress),
|
|
40
|
+
new Promise((_, r) => setTimeout(() => r(new Error("timeout")), 2000)),
|
|
41
|
+
]);
|
|
42
|
+
cfg.balance = `${parseFloat(ethersLib.ethers.formatEther(bal)).toFixed(4)} ETH`;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
/* skip balance on timeout */
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return cfg;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// ─── ANSI helpers ─────────────────────────────────────────────────────────────
|
|
55
|
+
const ESC = "\x1b";
|
|
56
|
+
const ansi = {
|
|
57
|
+
clearScreen: `${ESC}[2J`,
|
|
58
|
+
home: `${ESC}[H`,
|
|
59
|
+
clearLine: `${ESC}[2K`,
|
|
60
|
+
clearToEol: `${ESC}[K`,
|
|
61
|
+
hideCursor: `${ESC}[?25l`,
|
|
62
|
+
showCursor: `${ESC}[?25h`,
|
|
63
|
+
move: (r, col) => `${ESC}[${r};${col}H`,
|
|
64
|
+
scrollRegion: (top, bot) => `${ESC}[${top};${bot}r`,
|
|
65
|
+
resetScroll: `${ESC}[r`,
|
|
66
|
+
};
|
|
67
|
+
function write(s) {
|
|
68
|
+
process.stdout.write(s);
|
|
69
|
+
}
|
|
70
|
+
// ─── Prompt constants ─────────────────────────────────────────────────────────
|
|
71
|
+
const PROMPT_TEXT = chalk_1.default.cyanBright("◈") +
|
|
72
|
+
" " +
|
|
73
|
+
chalk_1.default.dim("arc402") +
|
|
74
|
+
" " +
|
|
75
|
+
chalk_1.default.white(">") +
|
|
76
|
+
" ";
|
|
77
|
+
// Visible character count of "◈ arc402 > "
|
|
78
|
+
const PROMPT_VIS = 11;
|
|
79
|
+
// ─── Known command detection ──────────────────────────────────────────────────
|
|
80
|
+
const BUILTIN_CMDS = ["help", "exit", "quit", "clear", "status"];
|
|
81
|
+
// ─── Shell-style tokenizer ────────────────────────────────────────────────────
|
|
82
|
+
function parseTokens(input) {
|
|
83
|
+
const tokens = [];
|
|
84
|
+
let current = "";
|
|
85
|
+
let inQuote = false;
|
|
86
|
+
let quoteChar = "";
|
|
87
|
+
for (const ch of input) {
|
|
88
|
+
if (inQuote) {
|
|
89
|
+
if (ch === quoteChar)
|
|
90
|
+
inQuote = false;
|
|
91
|
+
else
|
|
92
|
+
current += ch;
|
|
93
|
+
}
|
|
94
|
+
else if (ch === '"' || ch === "'") {
|
|
95
|
+
inQuote = true;
|
|
96
|
+
quoteChar = ch;
|
|
97
|
+
}
|
|
98
|
+
else if (ch === " ") {
|
|
99
|
+
if (current) {
|
|
100
|
+
tokens.push(current);
|
|
101
|
+
current = "";
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
current += ch;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (current)
|
|
109
|
+
tokens.push(current);
|
|
110
|
+
return tokens;
|
|
111
|
+
}
|
|
112
|
+
// ─── Tab completion logic ─────────────────────────────────────────────────────
|
|
113
|
+
function getCompletions(line, topCmds, subCmds) {
|
|
114
|
+
const allTop = [...BUILTIN_CMDS, ...topCmds];
|
|
115
|
+
const trimmed = line.trimStart();
|
|
116
|
+
const spaceIdx = trimmed.indexOf(" ");
|
|
117
|
+
if (spaceIdx === -1) {
|
|
118
|
+
return allTop.filter((cmd) => cmd.startsWith(trimmed));
|
|
119
|
+
}
|
|
120
|
+
const parent = trimmed.slice(0, spaceIdx);
|
|
121
|
+
const rest = trimmed.slice(spaceIdx + 1);
|
|
122
|
+
const subs = subCmds.get(parent) ?? [];
|
|
123
|
+
return subs.filter((s) => s.startsWith(rest)).map((s) => `${parent} ${s}`);
|
|
124
|
+
}
|
|
125
|
+
// ─── TUI class ────────────────────────────────────────────────────────────────
|
|
126
|
+
class TUI {
|
|
127
|
+
constructor() {
|
|
128
|
+
this.inputBuffer = "";
|
|
129
|
+
this.cursorPos = 0;
|
|
130
|
+
this.history = [];
|
|
131
|
+
this.historyIdx = -1;
|
|
132
|
+
this.historyTemp = "";
|
|
133
|
+
this.bannerLines = [];
|
|
134
|
+
this.topCmds = [];
|
|
135
|
+
this.subCmds = new Map();
|
|
136
|
+
this.commandRunning = false;
|
|
137
|
+
// ── Key handler ──────────────────────────────────────────────────────────────
|
|
138
|
+
this.boundKeyHandler = (key) => {
|
|
139
|
+
if (this.commandRunning)
|
|
140
|
+
return;
|
|
141
|
+
this.handleKey(key);
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
get termRows() {
|
|
145
|
+
return process.stdout.rows || 24;
|
|
146
|
+
}
|
|
147
|
+
get termCols() {
|
|
148
|
+
return process.stdout.columns || 80;
|
|
149
|
+
}
|
|
150
|
+
get scrollTop() {
|
|
151
|
+
// +1 for separator row after banner
|
|
152
|
+
return this.bannerLines.length + 2;
|
|
153
|
+
}
|
|
154
|
+
get scrollBot() {
|
|
155
|
+
return this.termRows - 1;
|
|
156
|
+
}
|
|
157
|
+
get inputRow() {
|
|
158
|
+
return this.termRows;
|
|
159
|
+
}
|
|
160
|
+
// ── Lifecycle ───────────────────────────────────────────────────────────────
|
|
161
|
+
async start() {
|
|
162
|
+
this.bannerCfg = await loadBannerConfig();
|
|
163
|
+
// Build command metadata for completion
|
|
164
|
+
const template = (0, program_1.createProgram)();
|
|
165
|
+
this.topCmds = template.commands.map((cmd) => cmd.name());
|
|
166
|
+
for (const cmd of template.commands) {
|
|
167
|
+
if (cmd.commands.length > 0) {
|
|
168
|
+
this.subCmds.set(cmd.name(), cmd.commands.map((s) => s.name()));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Draw initial screen
|
|
172
|
+
this.setupScreen();
|
|
173
|
+
this.drawInputLine();
|
|
174
|
+
// Enter raw mode
|
|
175
|
+
if (process.stdin.isTTY) {
|
|
176
|
+
process.stdin.setRawMode(true);
|
|
177
|
+
}
|
|
178
|
+
process.stdin.resume();
|
|
179
|
+
process.stdin.setEncoding("utf8");
|
|
180
|
+
process.stdin.on("data", this.boundKeyHandler);
|
|
181
|
+
// Resize handler
|
|
182
|
+
process.stdout.on("resize", () => {
|
|
183
|
+
this.setupScreen();
|
|
184
|
+
this.drawInputLine();
|
|
185
|
+
});
|
|
186
|
+
// SIGINT (shouldn't fire in raw mode, but just in case)
|
|
187
|
+
process.on("SIGINT", () => this.exitGracefully());
|
|
188
|
+
// Keep alive — process.stdin listener keeps the event loop running
|
|
189
|
+
await new Promise(() => {
|
|
190
|
+
/* never resolves; process.exit() is called on quit */
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
// ── Screen setup ────────────────────────────────────────────────────────────
|
|
194
|
+
setupScreen() {
|
|
195
|
+
this.bannerLines = (0, banner_1.getBannerLines)(this.bannerCfg);
|
|
196
|
+
write(ansi.hideCursor);
|
|
197
|
+
write(ansi.clearScreen + ansi.home);
|
|
198
|
+
// Banner
|
|
199
|
+
for (const line of this.bannerLines) {
|
|
200
|
+
write(line + "\n");
|
|
201
|
+
}
|
|
202
|
+
// Separator between banner and output area
|
|
203
|
+
write(chalk_1.default.dim("─".repeat(this.termCols)) + "\n");
|
|
204
|
+
// Set scroll region (output area, leaves last row free for input)
|
|
205
|
+
if (this.scrollTop <= this.scrollBot) {
|
|
206
|
+
write(ansi.scrollRegion(this.scrollTop, this.scrollBot));
|
|
207
|
+
}
|
|
208
|
+
// Position cursor at top of output area
|
|
209
|
+
write(ansi.move(this.scrollTop, 1));
|
|
210
|
+
write(ansi.showCursor);
|
|
211
|
+
}
|
|
212
|
+
// ── Banner repaint (in-place, preserves output area) ────────────────────────
|
|
213
|
+
repaintBanner() {
|
|
214
|
+
write(ansi.hideCursor);
|
|
215
|
+
for (let i = 0; i < this.bannerLines.length; i++) {
|
|
216
|
+
write(ansi.move(i + 1, 1) + ansi.clearToEol + this.bannerLines[i]);
|
|
217
|
+
}
|
|
218
|
+
// Separator
|
|
219
|
+
const sepRow = this.bannerLines.length + 1;
|
|
220
|
+
write(ansi.move(sepRow, 1) +
|
|
221
|
+
ansi.clearToEol +
|
|
222
|
+
chalk_1.default.dim("─".repeat(this.termCols)));
|
|
223
|
+
write(ansi.showCursor);
|
|
224
|
+
}
|
|
225
|
+
// ── Input line ───────────────────────────────────────────────────────────────
|
|
226
|
+
drawInputLine() {
|
|
227
|
+
write(ansi.move(this.inputRow, 1) + ansi.clearLine);
|
|
228
|
+
write(PROMPT_TEXT + this.inputBuffer);
|
|
229
|
+
// Place cursor at correct position within the input
|
|
230
|
+
write(ansi.move(this.inputRow, PROMPT_VIS + 1 + this.cursorPos));
|
|
231
|
+
}
|
|
232
|
+
handleKey(key) {
|
|
233
|
+
// Ctrl+C
|
|
234
|
+
if (key === "\u0003") {
|
|
235
|
+
this.exitGracefully();
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// Ctrl+L — refresh
|
|
239
|
+
if (key === "\u000C") {
|
|
240
|
+
this.setupScreen();
|
|
241
|
+
this.drawInputLine();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
// Enter
|
|
245
|
+
if (key === "\r" || key === "\n") {
|
|
246
|
+
void this.submit();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
// Backspace
|
|
250
|
+
if (key === "\u007F" || key === "\b") {
|
|
251
|
+
if (this.cursorPos > 0) {
|
|
252
|
+
this.inputBuffer =
|
|
253
|
+
this.inputBuffer.slice(0, this.cursorPos - 1) +
|
|
254
|
+
this.inputBuffer.slice(this.cursorPos);
|
|
255
|
+
this.cursorPos--;
|
|
256
|
+
this.drawInputLine();
|
|
257
|
+
}
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
// Delete (forward)
|
|
261
|
+
if (key === "\x1b[3~") {
|
|
262
|
+
if (this.cursorPos < this.inputBuffer.length) {
|
|
263
|
+
this.inputBuffer =
|
|
264
|
+
this.inputBuffer.slice(0, this.cursorPos) +
|
|
265
|
+
this.inputBuffer.slice(this.cursorPos + 1);
|
|
266
|
+
this.drawInputLine();
|
|
267
|
+
}
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
// Up arrow — history prev
|
|
271
|
+
if (key === "\x1b[A") {
|
|
272
|
+
if (this.historyIdx === -1) {
|
|
273
|
+
this.historyTemp = this.inputBuffer;
|
|
274
|
+
this.historyIdx = this.history.length - 1;
|
|
275
|
+
}
|
|
276
|
+
else if (this.historyIdx > 0) {
|
|
277
|
+
this.historyIdx--;
|
|
278
|
+
}
|
|
279
|
+
if (this.historyIdx >= 0) {
|
|
280
|
+
this.inputBuffer = this.history[this.historyIdx];
|
|
281
|
+
this.cursorPos = this.inputBuffer.length;
|
|
282
|
+
this.drawInputLine();
|
|
283
|
+
}
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
// Down arrow — history next
|
|
287
|
+
if (key === "\x1b[B") {
|
|
288
|
+
if (this.historyIdx >= 0) {
|
|
289
|
+
this.historyIdx++;
|
|
290
|
+
if (this.historyIdx >= this.history.length) {
|
|
291
|
+
this.historyIdx = -1;
|
|
292
|
+
this.inputBuffer = this.historyTemp;
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
this.inputBuffer = this.history[this.historyIdx];
|
|
296
|
+
}
|
|
297
|
+
this.cursorPos = this.inputBuffer.length;
|
|
298
|
+
this.drawInputLine();
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
// Right arrow
|
|
303
|
+
if (key === "\x1b[C") {
|
|
304
|
+
if (this.cursorPos < this.inputBuffer.length) {
|
|
305
|
+
this.cursorPos++;
|
|
306
|
+
this.drawInputLine();
|
|
307
|
+
}
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
// Left arrow
|
|
311
|
+
if (key === "\x1b[D") {
|
|
312
|
+
if (this.cursorPos > 0) {
|
|
313
|
+
this.cursorPos--;
|
|
314
|
+
this.drawInputLine();
|
|
315
|
+
}
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
// Home / Ctrl+A
|
|
319
|
+
if (key === "\x1b[H" || key === "\u0001") {
|
|
320
|
+
this.cursorPos = 0;
|
|
321
|
+
this.drawInputLine();
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
// End / Ctrl+E
|
|
325
|
+
if (key === "\x1b[F" || key === "\u0005") {
|
|
326
|
+
this.cursorPos = this.inputBuffer.length;
|
|
327
|
+
this.drawInputLine();
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
// Ctrl+U — clear line
|
|
331
|
+
if (key === "\u0015") {
|
|
332
|
+
this.inputBuffer = "";
|
|
333
|
+
this.cursorPos = 0;
|
|
334
|
+
this.drawInputLine();
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
// Ctrl+K — kill to end
|
|
338
|
+
if (key === "\u000B") {
|
|
339
|
+
this.inputBuffer = this.inputBuffer.slice(0, this.cursorPos);
|
|
340
|
+
this.drawInputLine();
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
// Tab — completion
|
|
344
|
+
if (key === "\t") {
|
|
345
|
+
this.handleTab();
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
// Printable characters
|
|
349
|
+
if (key >= " " && !key.startsWith("\x1b")) {
|
|
350
|
+
this.inputBuffer =
|
|
351
|
+
this.inputBuffer.slice(0, this.cursorPos) +
|
|
352
|
+
key +
|
|
353
|
+
this.inputBuffer.slice(this.cursorPos);
|
|
354
|
+
this.cursorPos += key.length;
|
|
355
|
+
this.drawInputLine();
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
// ── Tab completion ───────────────────────────────────────────────────────────
|
|
359
|
+
handleTab() {
|
|
360
|
+
const completions = getCompletions(this.inputBuffer, this.topCmds, this.subCmds);
|
|
361
|
+
if (completions.length === 0)
|
|
362
|
+
return;
|
|
363
|
+
if (completions.length === 1) {
|
|
364
|
+
this.inputBuffer = completions[0] + " ";
|
|
365
|
+
this.cursorPos = this.inputBuffer.length;
|
|
366
|
+
this.drawInputLine();
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
// Find common prefix
|
|
370
|
+
const common = completions.reduce((a, b) => {
|
|
371
|
+
let i = 0;
|
|
372
|
+
while (i < a.length && i < b.length && a[i] === b[i])
|
|
373
|
+
i++;
|
|
374
|
+
return a.slice(0, i);
|
|
375
|
+
});
|
|
376
|
+
if (common.length > this.inputBuffer.trimStart().length) {
|
|
377
|
+
this.inputBuffer = common;
|
|
378
|
+
this.cursorPos = common.length;
|
|
379
|
+
}
|
|
380
|
+
// Show options in output area
|
|
381
|
+
this.writeOutput("\n" + chalk_1.default.dim(completions.join(" ")) + "\n");
|
|
382
|
+
this.drawInputLine();
|
|
383
|
+
}
|
|
384
|
+
// ── Write to output area ─────────────────────────────────────────────────────
|
|
385
|
+
writeOutput(text) {
|
|
386
|
+
// Move cursor to bottom of scroll region to ensure scroll-down works
|
|
387
|
+
write(ansi.move(this.scrollBot, 1));
|
|
388
|
+
write(text);
|
|
389
|
+
}
|
|
390
|
+
// ── Submit line ──────────────────────────────────────────────────────────────
|
|
391
|
+
async submit() {
|
|
392
|
+
const input = this.inputBuffer.trim();
|
|
393
|
+
this.inputBuffer = "";
|
|
394
|
+
this.cursorPos = 0;
|
|
395
|
+
this.historyIdx = -1;
|
|
396
|
+
if (!input) {
|
|
397
|
+
this.drawInputLine();
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
// Add to history
|
|
401
|
+
if (input !== this.history[this.history.length - 1]) {
|
|
402
|
+
this.history.push(input);
|
|
403
|
+
}
|
|
404
|
+
// Echo the input into the output area
|
|
405
|
+
this.writeOutput("\n" + chalk_1.default.dim("◈ ") + chalk_1.default.white(input) + "\n");
|
|
406
|
+
// ── Built-in commands ──────────────────────────────────────────────────────
|
|
407
|
+
if (input === "exit" || input === "quit") {
|
|
408
|
+
this.exitGracefully();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (input === "clear") {
|
|
412
|
+
this.bannerCfg = await loadBannerConfig();
|
|
413
|
+
this.setupScreen();
|
|
414
|
+
this.drawInputLine();
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
if (input === "status") {
|
|
418
|
+
await this.runStatus();
|
|
419
|
+
this.afterCommand();
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (input === "help" || input === "/help") {
|
|
423
|
+
await this.runHelp();
|
|
424
|
+
this.afterCommand();
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
// ── /chat prefix — explicit chat route ────────────────────────────────────
|
|
428
|
+
if (input.startsWith("/chat ") || input === "/chat") {
|
|
429
|
+
const msg = input.slice(6).trim();
|
|
430
|
+
if (msg) {
|
|
431
|
+
this.commandRunning = true;
|
|
432
|
+
await this.sendChat(msg);
|
|
433
|
+
this.commandRunning = false;
|
|
434
|
+
}
|
|
435
|
+
this.afterCommand();
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
// ── Chat mode detection ────────────────────────────────────────────────────
|
|
439
|
+
const firstWord = input.split(/\s+/)[0];
|
|
440
|
+
const allKnown = [...BUILTIN_CMDS, ...this.topCmds];
|
|
441
|
+
if (!allKnown.includes(firstWord)) {
|
|
442
|
+
this.commandRunning = true;
|
|
443
|
+
await this.sendChat(input);
|
|
444
|
+
this.commandRunning = false;
|
|
445
|
+
this.afterCommand();
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
// ── Dispatch to commander ──────────────────────────────────────────────────
|
|
449
|
+
this.commandRunning = true;
|
|
450
|
+
// Move output cursor to bottom of scroll region
|
|
451
|
+
write(ansi.move(this.scrollBot, 1));
|
|
452
|
+
// Suspend TUI stdin so interactive commands (prompts, readline) work cleanly
|
|
453
|
+
process.stdin.removeListener("data", this.boundKeyHandler);
|
|
454
|
+
const tokens = parseTokens(input);
|
|
455
|
+
const prog = (0, program_1.createProgram)();
|
|
456
|
+
prog.exitOverride();
|
|
457
|
+
prog.configureOutput({
|
|
458
|
+
writeOut: (str) => process.stdout.write(str),
|
|
459
|
+
writeErr: (str) => process.stderr.write(str),
|
|
460
|
+
});
|
|
461
|
+
const origExit = process.exit;
|
|
462
|
+
process.exit = ((code) => {
|
|
463
|
+
throw new REPLExitSignal(code ?? 0);
|
|
464
|
+
});
|
|
465
|
+
try {
|
|
466
|
+
await prog.parseAsync(["node", "arc402", ...tokens]);
|
|
467
|
+
}
|
|
468
|
+
catch (err) {
|
|
469
|
+
if (err instanceof REPLExitSignal) {
|
|
470
|
+
// Command called process.exit() — normal
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
const e = err;
|
|
474
|
+
if (e.code === "commander.helpDisplayed" ||
|
|
475
|
+
e.code === "commander.version") {
|
|
476
|
+
// already written
|
|
477
|
+
}
|
|
478
|
+
else if (e.code === "commander.unknownCommand") {
|
|
479
|
+
process.stdout.write(`\n ${colors_1.c.failure} ${chalk_1.default.red(`Unknown command: ${chalk_1.default.white(tokens[0])}`)} \n`);
|
|
480
|
+
process.stdout.write(chalk_1.default.dim(" Type 'help' for available commands\n"));
|
|
481
|
+
}
|
|
482
|
+
else if (e.code?.startsWith("commander.")) {
|
|
483
|
+
process.stdout.write(`\n ${colors_1.c.failure} ${chalk_1.default.red(e.message ?? String(err))}\n`);
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
process.stdout.write(`\n ${colors_1.c.failure} ${chalk_1.default.red(e.message ?? String(err))}\n`);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
finally {
|
|
491
|
+
process.exit = origExit;
|
|
492
|
+
}
|
|
493
|
+
// Restore raw mode + our listener (interactive commands may have toggled it)
|
|
494
|
+
if (process.stdin.isTTY) {
|
|
495
|
+
process.stdin.setRawMode(true);
|
|
496
|
+
}
|
|
497
|
+
process.stdin.on("data", this.boundKeyHandler);
|
|
498
|
+
this.commandRunning = false;
|
|
499
|
+
this.afterCommand();
|
|
500
|
+
}
|
|
501
|
+
// ── OpenClaw chat ─────────────────────────────────────────────────────────────
|
|
502
|
+
async sendChat(message) {
|
|
503
|
+
write(ansi.move(this.scrollBot, 1));
|
|
504
|
+
let res;
|
|
505
|
+
try {
|
|
506
|
+
res = await fetch("http://localhost:19000/api/agent", {
|
|
507
|
+
method: "POST",
|
|
508
|
+
headers: { "Content-Type": "application/json" },
|
|
509
|
+
body: JSON.stringify({ message, session: "arc402-repl" }),
|
|
510
|
+
signal: AbortSignal.timeout(30000),
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
catch (err) {
|
|
514
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
515
|
+
const isDown = msg.includes("ECONNREFUSED") ||
|
|
516
|
+
msg.includes("fetch failed") ||
|
|
517
|
+
msg.includes("ENOTFOUND") ||
|
|
518
|
+
msg.includes("UND_ERR_SOCKET");
|
|
519
|
+
if (isDown) {
|
|
520
|
+
process.stdout.write("\n " +
|
|
521
|
+
chalk_1.default.yellow("⚠") +
|
|
522
|
+
" " +
|
|
523
|
+
chalk_1.default.dim("OpenClaw gateway not running. Start with: ") +
|
|
524
|
+
chalk_1.default.white("openclaw gateway start") +
|
|
525
|
+
"\n");
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
process.stdout.write("\n " + colors_1.c.failure + " " + chalk_1.default.red(msg) + "\n");
|
|
529
|
+
}
|
|
530
|
+
return;
|
|
531
|
+
}
|
|
532
|
+
if (!res.body) {
|
|
533
|
+
process.stdout.write("\n" + chalk_1.default.dim(" ◈ ") + chalk_1.default.white("(empty response)") + "\n");
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
process.stdout.write("\n");
|
|
537
|
+
const flushLine = (line) => {
|
|
538
|
+
// Unwrap SSE data lines
|
|
539
|
+
if (line.startsWith("data: ")) {
|
|
540
|
+
line = line.slice(6);
|
|
541
|
+
if (line === "[DONE]")
|
|
542
|
+
return;
|
|
543
|
+
try {
|
|
544
|
+
const j = JSON.parse(line);
|
|
545
|
+
line = j.text ?? j.content ?? j.delta?.text ?? line;
|
|
546
|
+
}
|
|
547
|
+
catch {
|
|
548
|
+
/* use raw */
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (line.trim()) {
|
|
552
|
+
process.stdout.write(chalk_1.default.dim(" ◈ ") + chalk_1.default.white(line) + "\n");
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
const reader = res.body.getReader();
|
|
556
|
+
const decoder = new TextDecoder();
|
|
557
|
+
let buffer = "";
|
|
558
|
+
while (true) {
|
|
559
|
+
const { done, value } = await reader.read();
|
|
560
|
+
if (done)
|
|
561
|
+
break;
|
|
562
|
+
buffer += decoder.decode(value, { stream: true });
|
|
563
|
+
const lines = buffer.split("\n");
|
|
564
|
+
buffer = lines.pop() ?? "";
|
|
565
|
+
for (const line of lines)
|
|
566
|
+
flushLine(line);
|
|
567
|
+
}
|
|
568
|
+
if (buffer.trim())
|
|
569
|
+
flushLine(buffer);
|
|
570
|
+
}
|
|
571
|
+
// ── After each command: repaint banner + input ───────────────────────────────
|
|
572
|
+
afterCommand() {
|
|
573
|
+
this.repaintBanner();
|
|
574
|
+
this.drawInputLine();
|
|
575
|
+
}
|
|
576
|
+
// ── Built-in: status ─────────────────────────────────────────────────────────
|
|
577
|
+
async runStatus() {
|
|
578
|
+
write(ansi.move(this.scrollBot, 1));
|
|
579
|
+
if (!fs_1.default.existsSync(CONFIG_PATH)) {
|
|
580
|
+
process.stdout.write(chalk_1.default.dim("\n No config found. Run 'config init' to get started.\n"));
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
try {
|
|
584
|
+
const raw = JSON.parse(fs_1.default.readFileSync(CONFIG_PATH, "utf-8"));
|
|
585
|
+
process.stdout.write("\n");
|
|
586
|
+
if (raw.network)
|
|
587
|
+
process.stdout.write(` ${chalk_1.default.dim("Network")} ${chalk_1.default.white(raw.network)}\n`);
|
|
588
|
+
if (raw.walletContractAddress) {
|
|
589
|
+
const w = raw.walletContractAddress;
|
|
590
|
+
process.stdout.write(` ${chalk_1.default.dim("Wallet")} ${chalk_1.default.white(`${w.slice(0, 6)}...${w.slice(-4)}`)}\n`);
|
|
591
|
+
}
|
|
592
|
+
if (raw.rpcUrl && raw.walletContractAddress) {
|
|
593
|
+
try {
|
|
594
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
595
|
+
const ethersLib = require("ethers");
|
|
596
|
+
const provider = new ethersLib.ethers.JsonRpcProvider(raw.rpcUrl);
|
|
597
|
+
const bal = await Promise.race([
|
|
598
|
+
provider.getBalance(raw.walletContractAddress),
|
|
599
|
+
new Promise((_, r) => setTimeout(() => r(new Error("timeout")), 2000)),
|
|
600
|
+
]);
|
|
601
|
+
process.stdout.write(` ${chalk_1.default.dim("Balance")} ${chalk_1.default.white(`${parseFloat(ethersLib.ethers.formatEther(bal)).toFixed(4)} ETH`)}\n`);
|
|
602
|
+
}
|
|
603
|
+
catch {
|
|
604
|
+
/* skip */
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
process.stdout.write("\n");
|
|
608
|
+
}
|
|
609
|
+
catch {
|
|
610
|
+
/* skip */
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
// ── Built-in: help ────────────────────────────────────────────────────────────
|
|
614
|
+
async runHelp() {
|
|
615
|
+
write(ansi.move(this.scrollBot, 1));
|
|
616
|
+
process.stdin.removeListener("data", this.boundKeyHandler);
|
|
617
|
+
const prog = (0, program_1.createProgram)();
|
|
618
|
+
prog.exitOverride();
|
|
619
|
+
prog.configureOutput({
|
|
620
|
+
writeOut: (str) => process.stdout.write(str),
|
|
621
|
+
writeErr: (str) => process.stderr.write(str),
|
|
622
|
+
});
|
|
623
|
+
try {
|
|
624
|
+
await prog.parseAsync(["node", "arc402", "--help"]);
|
|
625
|
+
}
|
|
626
|
+
catch {
|
|
627
|
+
/* commander throws after printing help */
|
|
628
|
+
}
|
|
629
|
+
if (process.stdin.isTTY)
|
|
630
|
+
process.stdin.setRawMode(true);
|
|
631
|
+
process.stdin.on("data", this.boundKeyHandler);
|
|
632
|
+
process.stdout.write("\n");
|
|
633
|
+
process.stdout.write(chalk_1.default.cyanBright("Chat") + "\n");
|
|
634
|
+
process.stdout.write(" " +
|
|
635
|
+
chalk_1.default.white("<message>") +
|
|
636
|
+
chalk_1.default.dim(" Send message to OpenClaw gateway\n"));
|
|
637
|
+
process.stdout.write(" " +
|
|
638
|
+
chalk_1.default.white("/chat <message>") +
|
|
639
|
+
chalk_1.default.dim(" Explicitly route to chat\n"));
|
|
640
|
+
process.stdout.write(chalk_1.default.dim(" Gateway: http://localhost:19000 (openclaw gateway start)\n"));
|
|
641
|
+
process.stdout.write("\n");
|
|
642
|
+
}
|
|
643
|
+
// ── Exit ──────────────────────────────────────────────────────────────────────
|
|
644
|
+
exitGracefully() {
|
|
645
|
+
write(ansi.move(this.inputRow, 1) + ansi.clearLine);
|
|
646
|
+
write(" " + chalk_1.default.cyanBright("◈") + chalk_1.default.dim(" goodbye") + "\n");
|
|
647
|
+
write(ansi.resetScroll);
|
|
648
|
+
write(ansi.showCursor);
|
|
649
|
+
if (process.stdin.isTTY) {
|
|
650
|
+
process.stdin.setRawMode(false);
|
|
651
|
+
}
|
|
652
|
+
process.exit(0);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
// ─── REPL entry point ─────────────────────────────────────────────────────────
|
|
656
|
+
async function startREPL() {
|
|
657
|
+
if (!process.stdout.isTTY) {
|
|
658
|
+
// Non-TTY (piped): fall back to minimal line-mode output
|
|
659
|
+
const bannerCfg = await loadBannerConfig();
|
|
660
|
+
for (const line of (0, banner_1.getBannerLines)(bannerCfg)) {
|
|
661
|
+
process.stdout.write(line + "\n");
|
|
662
|
+
}
|
|
663
|
+
process.stdout.write("Interactive TUI requires a TTY. Use arc402 <command> directly.\n");
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
const tui = new TUI();
|
|
667
|
+
await tui.start();
|
|
668
|
+
}
|
|
669
|
+
//# sourceMappingURL=repl.js.map
|