@peer2peer/cli 0.0.0-stage → 0.1.1

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.
Files changed (111) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +370 -2
  3. package/dist/ancestry.d.ts +13 -0
  4. package/dist/ancestry.js +71 -0
  5. package/dist/app.d.ts +368 -0
  6. package/dist/app.js +1181 -0
  7. package/dist/bin.d.ts +2 -0
  8. package/dist/bin.js +56 -0
  9. package/dist/circle-ops.d.ts +402 -0
  10. package/dist/circle-ops.js +1725 -0
  11. package/dist/cli.d.ts +660 -0
  12. package/dist/cli.js +1787 -0
  13. package/dist/codes.d.ts +231 -0
  14. package/dist/codes.js +998 -0
  15. package/dist/config.d.ts +159 -0
  16. package/dist/config.js +294 -0
  17. package/dist/doctor.d.ts +113 -0
  18. package/dist/doctor.js +930 -0
  19. package/dist/errors.d.ts +8 -0
  20. package/dist/errors.js +29 -0
  21. package/dist/ext-host.d.ts +38 -0
  22. package/dist/ext-host.js +293 -0
  23. package/dist/extensions.d.ts +487 -0
  24. package/dist/extensions.js +406 -0
  25. package/dist/features.d.ts +91 -0
  26. package/dist/features.js +164 -0
  27. package/dist/format.d.ts +57 -0
  28. package/dist/format.js +248 -0
  29. package/dist/graph-memory.d.ts +107 -0
  30. package/dist/graph-memory.js +393 -0
  31. package/dist/history.d.ts +155 -0
  32. package/dist/history.js +697 -0
  33. package/dist/identity.d.ts +239 -0
  34. package/dist/identity.js +759 -0
  35. package/dist/inbound.d.ts +32 -0
  36. package/dist/inbound.js +261 -0
  37. package/dist/index.d.ts +8 -0
  38. package/dist/index.js +14 -0
  39. package/dist/ix-local.d.ts +76 -0
  40. package/dist/ix-local.js +203 -0
  41. package/dist/join-answers.d.ts +49 -0
  42. package/dist/join-answers.js +149 -0
  43. package/dist/outbox.d.ts +109 -0
  44. package/dist/outbox.js +337 -0
  45. package/dist/pretty.d.ts +100 -0
  46. package/dist/pretty.js +256 -0
  47. package/dist/procstart.d.ts +1 -0
  48. package/dist/procstart.js +66 -0
  49. package/dist/progress.d.ts +30 -0
  50. package/dist/progress.js +78 -0
  51. package/dist/projects.d.ts +491 -0
  52. package/dist/projects.js +1383 -0
  53. package/dist/prompt/picker.d.ts +64 -0
  54. package/dist/prompt/picker.js +152 -0
  55. package/dist/prompt/tty.d.ts +29 -0
  56. package/dist/prompt/tty.js +122 -0
  57. package/dist/readonly.d.ts +20 -0
  58. package/dist/readonly.js +50 -0
  59. package/dist/receipts.d.ts +99 -0
  60. package/dist/receipts.js +182 -0
  61. package/dist/relay-client.d.ts +277 -0
  62. package/dist/relay-client.js +873 -0
  63. package/dist/relay-info.d.ts +24 -0
  64. package/dist/relay-info.js +53 -0
  65. package/dist/rotation.d.ts +402 -0
  66. package/dist/rotation.js +2054 -0
  67. package/dist/running.d.ts +5 -0
  68. package/dist/running.js +74 -0
  69. package/dist/sessions.d.ts +355 -0
  70. package/dist/sessions.js +1247 -0
  71. package/dist/shortcode.d.ts +149 -0
  72. package/dist/shortcode.js +465 -0
  73. package/dist/social.d.ts +139 -0
  74. package/dist/social.js +485 -0
  75. package/dist/state.d.ts +509 -0
  76. package/dist/state.js +220 -0
  77. package/dist/stdin.d.ts +17 -0
  78. package/dist/stdin.js +46 -0
  79. package/dist/sync.d.ts +77 -0
  80. package/dist/sync.js +375 -0
  81. package/dist/transient.d.ts +1 -0
  82. package/dist/transient.js +28 -0
  83. package/dist/trust.d.ts +37 -0
  84. package/dist/trust.js +134 -0
  85. package/dist/tty-confirm.d.ts +2 -0
  86. package/dist/tty-confirm.js +53 -0
  87. package/dist/tui/command.d.ts +20 -0
  88. package/dist/tui/command.js +70 -0
  89. package/dist/tui/frame.d.ts +43 -0
  90. package/dist/tui/frame.js +111 -0
  91. package/dist/tui/model.d.ts +221 -0
  92. package/dist/tui/model.js +259 -0
  93. package/dist/tui/sanitize.d.ts +13 -0
  94. package/dist/tui/sanitize.js +151 -0
  95. package/dist/tui/terminal.d.ts +88 -0
  96. package/dist/tui/terminal.js +354 -0
  97. package/dist/tui/tui.d.ts +78 -0
  98. package/dist/tui/tui.js +612 -0
  99. package/dist/tui/view.d.ts +4 -0
  100. package/dist/tui/view.js +358 -0
  101. package/dist/unknown-kinds.d.ts +12 -0
  102. package/dist/unknown-kinds.js +66 -0
  103. package/dist/version.d.ts +1 -0
  104. package/dist/version.js +5 -0
  105. package/dist/want-pow.d.ts +12 -0
  106. package/dist/want-pow.js +46 -0
  107. package/dist/watch.d.ts +35 -0
  108. package/dist/watch.js +331 -0
  109. package/dist/xstate.d.ts +125 -0
  110. package/dist/xstate.js +422 -0
  111. package/package.json +60 -4
@@ -0,0 +1,354 @@
1
+ // Copyright 2026 Ix Infrastructure Inc.
2
+ //
3
+ // The terminal under the TUI: raw keyboard input, the alternate screen,
4
+ // resize events and colour detection, with nothing but node:tty. A fake
5
+ // implementation (tests) records frames instead.
6
+ //
7
+ // Restoring the terminal is the one thing that must always happen: leave()
8
+ // is idempotent and runs on quit, on errors, on SIGTERM/SIGHUP and, as a
9
+ // last resort, on process exit.
10
+ import { StringDecoder } from "node:string_decoder";
11
+ /** NO_COLOR (any non-empty value) and TERM=dumb turn colour off; otherwise the terminal says. */
12
+ export function colorWanted(env, out) {
13
+ if (env.NO_COLOR !== undefined && env.NO_COLOR !== "")
14
+ return false;
15
+ if (env.TERM === "dumb")
16
+ return false;
17
+ if (env.FORCE_COLOR !== undefined && env.FORCE_COLOR !== "" && env.FORCE_COLOR !== "0")
18
+ return true;
19
+ try {
20
+ return Boolean(out.isTTY) && (out.hasColors?.(8) ?? true);
21
+ }
22
+ catch {
23
+ return false;
24
+ }
25
+ }
26
+ const CSI_KEYS = {
27
+ A: "up",
28
+ B: "down",
29
+ C: "right",
30
+ D: "left",
31
+ H: "home",
32
+ F: "end",
33
+ Z: "backtab",
34
+ "1~": "home",
35
+ "4~": "end",
36
+ "5~": "pageup",
37
+ "6~": "pagedown",
38
+ "7~": "home",
39
+ "8~": "end",
40
+ "3~": "delete",
41
+ };
42
+ /** A bracketed paste longer than this is cut (the rest is dropped). */
43
+ const PASTE_MAX = 64 * 1024;
44
+ /** An escape sequence left open this long is given up (and nothing of it is typed). */
45
+ const PENDING_MAX = 4096;
46
+ /** After a paste ends, keys this soon after it count as pasted too (a fake end marker inside the paste). */
47
+ export const PASTE_QUIET_MS = 150;
48
+ /** A paste, or a terminal answer, with no input for this long is over (its end marker never came). */
49
+ export const STUCK_MS = 1000;
50
+ /** A lone ESC with nothing after it for this long is the Escape key. */
51
+ export const ESC_MS = 50;
52
+ const CTRL_C = { name: "c", ctrl: true };
53
+ /**
54
+ * Splits raw input into keys, keeping state between chunks: a terminal's
55
+ * answer or a key's escape sequence split across reads is completed by the
56
+ * next one, never leaked as typed characters.
57
+ *
58
+ * - Escape sequences of the keys we use are recognised; any other escape
59
+ * or string sequence is dropped whole, however long (a string sequence
60
+ * is discarded up to its terminator, across chunks).
61
+ * - Text inside a bracketed paste comes through with `paste: true` (line
62
+ * breaks as spaces, controls dropped): the model types it only into a
63
+ * message or a prompt, never as command keys. For PASTE_QUIET_MS after
64
+ * a paste ends, keys still count as pasted: a paste carrying its own
65
+ * end marker (`ESC[201~ay`) can't press `a` or `y`.
66
+ * - Ctrl-C always gets through, even mid-paste or mid-answer: the way out.
67
+ * - Time-outs (idle(), called by the terminal after waitMs of no input): a
68
+ * lone ESC is the Escape key; a paste or answer whose end never came is
69
+ * over (STUCK_MS), so the TUI can't lock up.
70
+ */
71
+ export class KeyParser {
72
+ now;
73
+ pending = "";
74
+ pasting;
75
+ /** Inside a string sequence (OSC, DCS, ...) whose terminator hasn't come: dropping input. */
76
+ discarding = false;
77
+ quietUntil = 0;
78
+ constructor(now = Date.now) {
79
+ this.now = now;
80
+ }
81
+ feed(chunk) {
82
+ const keys = [];
83
+ const push = (k) => {
84
+ // Shortly after a paste ended, everything is still pasted (except Ctrl-C).
85
+ if (!k.paste && !(k.ctrl && k.name === "c") && this.now() < this.quietUntil) {
86
+ if (k.ch !== undefined)
87
+ keys.push({ ...k, paste: true });
88
+ return;
89
+ }
90
+ keys.push(k);
91
+ };
92
+ let data = this.pending + chunk;
93
+ this.pending = "";
94
+ if (this.discarding) {
95
+ const end = data.search(/\u0007|\u001b\\|\u0003/);
96
+ if (end < 0) {
97
+ // Keep a final ESC: it may be the first half of ST.
98
+ this.pending = data.endsWith("\u001b") ? "\u001b" : "";
99
+ return keys;
100
+ }
101
+ this.discarding = false;
102
+ if (data[end] === "\u0003") {
103
+ // Ctrl-C: the way out; whatever came with it in this read is dropped.
104
+ push(CTRL_C);
105
+ return keys;
106
+ }
107
+ data = data.slice(end + 2 - (data[end] === "\u0007" ? 1 : 0));
108
+ }
109
+ if (this.pasting !== undefined) {
110
+ // The paste so far and the new data, searched as one: an end marker split across reads is found.
111
+ const buf = this.pasting + data;
112
+ this.pasting = undefined;
113
+ const after = this.paste(keys, buf, push);
114
+ if (after === undefined)
115
+ return keys;
116
+ data = after;
117
+ }
118
+ let i = 0;
119
+ while (i < data.length) {
120
+ const c = data[i];
121
+ if (c === "\u001b") {
122
+ const next = data[i + 1];
123
+ if (next === undefined) {
124
+ // Maybe the start of a sequence: wait for more (idle() makes it Escape).
125
+ this.pending = "\u001b";
126
+ break;
127
+ }
128
+ if (next === "\u0003") {
129
+ // ESC then Ctrl-C: Ctrl-C still gets through.
130
+ push(CTRL_C);
131
+ return keys;
132
+ }
133
+ if (next === "[" || next === "O") {
134
+ let j = i + 2;
135
+ while (j < data.length && /[0-9;?]/.test(data[j]))
136
+ j++;
137
+ if (j >= data.length) {
138
+ // The final byte hasn't arrived yet.
139
+ this.pending = data.slice(i, i + PENDING_MAX);
140
+ break;
141
+ }
142
+ if (data[j] === "\u0003") {
143
+ // Ctrl-C in the middle of a key's sequence: still the way out.
144
+ push(CTRL_C);
145
+ return keys;
146
+ }
147
+ const body = data.slice(i + 2, j + 1);
148
+ i = j + 1;
149
+ if (body === "200~") {
150
+ const after = this.paste(keys, data.slice(i), push);
151
+ if (after === undefined)
152
+ return keys;
153
+ data = after;
154
+ i = 0;
155
+ continue;
156
+ }
157
+ const name = CSI_KEYS[body] ?? CSI_KEYS[body.replace(/^1;\d/, "")];
158
+ if (name === "backtab")
159
+ push({ name: "tab", shift: true });
160
+ else if (name)
161
+ push({ name });
162
+ continue;
163
+ }
164
+ if (next === "]" || next === "P" || next === "_" || next === "^" || next === "X") {
165
+ // A string sequence (a terminal's answer): skip to its terminator, across chunks, however long.
166
+ const rest = data.slice(i + 2);
167
+ const end = rest.search(/\u0007|\u001b\\|\u0003/);
168
+ if (end < 0) {
169
+ this.discarding = true;
170
+ this.pending = rest.endsWith("\u001b") ? "\u001b" : "";
171
+ break;
172
+ }
173
+ if (rest[end] === "\u0003") {
174
+ push(CTRL_C);
175
+ return keys;
176
+ }
177
+ i += 2 + end + (rest[end] === "\u001b" ? 2 : 1);
178
+ continue;
179
+ }
180
+ if (next !== "\u001b") {
181
+ // Alt+key: ignored.
182
+ i += 2;
183
+ continue;
184
+ }
185
+ push({ name: "escape" });
186
+ i++;
187
+ continue;
188
+ }
189
+ const code = c.codePointAt(0);
190
+ if (c === "\r" || c === "\n")
191
+ push({ name: "return" });
192
+ else if (c === "\t")
193
+ push({ name: "tab" });
194
+ else if (c === "\u007f" || c === "\b")
195
+ push({ name: "backspace" });
196
+ else if (c === " ")
197
+ push({ name: "space", ch: " " });
198
+ else if (code < 32)
199
+ push({ name: String.fromCharCode(code + 96), ctrl: true });
200
+ else if (code >= 0x80 && code < 0xa0) {
201
+ // C1 controls: never typed.
202
+ }
203
+ else {
204
+ const ch = String.fromCodePoint(data.codePointAt(i));
205
+ push({ name: ch, ch });
206
+ i += ch.length;
207
+ continue;
208
+ }
209
+ i++;
210
+ }
211
+ return keys;
212
+ }
213
+ /** How long the terminal should wait for more input before calling idle() (0: no need). */
214
+ get waitMs() {
215
+ if (this.pasting !== undefined || this.discarding)
216
+ return STUCK_MS;
217
+ return this.pending !== "" ? ESC_MS : 0;
218
+ }
219
+ /** No more input came: a lone ESC is Escape, and a paste or answer whose end never came is over. */
220
+ idle() {
221
+ const keys = [];
222
+ if (this.pasting !== undefined) {
223
+ this.endPaste(keys, this.pasting);
224
+ this.pending = "";
225
+ return keys;
226
+ }
227
+ if (this.discarding) {
228
+ this.discarding = false;
229
+ this.pending = "";
230
+ return keys;
231
+ }
232
+ const p = this.pending;
233
+ this.pending = "";
234
+ return p === "\u001b" ? [{ name: "escape" }] : [];
235
+ }
236
+ /** idle(), for callers that just want what's held. */
237
+ flush() {
238
+ return this.idle();
239
+ }
240
+ get waiting() {
241
+ return this.waitMs > 0;
242
+ }
243
+ /**
244
+ * Text after a paste's start marker: the paste ends at its end marker (the rest is returned, to be
245
+ * read as keys), or waits for more (undefined). Ctrl-C before the end drops the paste and the rest
246
+ * of the read (undefined).
247
+ */
248
+ paste(keys, buf, push) {
249
+ const cc = buf.indexOf("\u0003");
250
+ const end = buf.indexOf("\u001b[201~");
251
+ if (cc >= 0 && (end < 0 || cc < end)) {
252
+ push(CTRL_C);
253
+ return undefined;
254
+ }
255
+ if (end < 0) {
256
+ // Bounded: the head of an over-long paste, and its last few characters (a split end marker).
257
+ this.pasting = buf.length > PASTE_MAX + 16 ? buf.slice(0, PASTE_MAX) + buf.slice(-5) : buf;
258
+ return undefined;
259
+ }
260
+ this.endPaste(keys, buf.slice(0, end));
261
+ return buf.slice(end + 6);
262
+ }
263
+ endPaste(keys, text) {
264
+ this.pasting = undefined;
265
+ for (const ch of text.slice(0, PASTE_MAX).replace(/[\r\n\t]+/g, " ")) {
266
+ const cp = ch.codePointAt(0);
267
+ if (cp >= 32 && !(cp >= 0x7f && cp < 0xa0))
268
+ keys.push({ name: ch, ch, paste: true });
269
+ }
270
+ this.quietUntil = this.now() + PASTE_QUIET_MS;
271
+ }
272
+ }
273
+ /** One chunk on its own (tests): parse, then flush. */
274
+ export function parseKeys(data) {
275
+ const p = new KeyParser();
276
+ return [...p.feed(data), ...p.flush()];
277
+ }
278
+ export class TtyTerminal {
279
+ input;
280
+ output;
281
+ color;
282
+ active = false;
283
+ exitHook = () => this.leave();
284
+ keyFn;
285
+ parser = new KeyParser();
286
+ /** Multi-byte characters split across reads stay whole. */
287
+ decoder = new StringDecoder("utf8");
288
+ escTimer;
289
+ onData = (b) => {
290
+ if (this.escTimer)
291
+ clearTimeout(this.escTimer);
292
+ for (const k of this.parser.feed(typeof b === "string" ? b : this.decoder.write(b)))
293
+ this.keyFn?.(k);
294
+ // A lone ESC is Escape unless the rest follows at once; a paste or answer that never ends is over.
295
+ const ms = this.parser.waitMs;
296
+ if (ms > 0) {
297
+ this.escTimer = setTimeout(() => {
298
+ for (const k of this.parser.idle())
299
+ this.keyFn?.(k);
300
+ }, ms);
301
+ this.escTimer.unref();
302
+ }
303
+ };
304
+ constructor(input, output, env) {
305
+ this.input = input;
306
+ this.output = output;
307
+ this.color = colorWanted(env, output);
308
+ }
309
+ get cols() {
310
+ return this.output.columns || 80;
311
+ }
312
+ get rows() {
313
+ return this.output.rows || 24;
314
+ }
315
+ enter() {
316
+ if (this.active)
317
+ return;
318
+ this.active = true;
319
+ process.on("exit", this.exitHook);
320
+ this.input.setRawMode(true);
321
+ this.input.resume();
322
+ this.input.on("data", this.onData);
323
+ // Alternate screen, hidden cursor, no line wrap, bracketed paste (pasted text is typed only into a
324
+ // message or a prompt, and a pasted line break is a space there: it never acts as a key).
325
+ this.output.write("\u001b[?1049h\u001b[?25l\u001b[?7l\u001b[?2004h\u001b[2J");
326
+ }
327
+ leave() {
328
+ if (!this.active)
329
+ return;
330
+ this.active = false;
331
+ process.off("exit", this.exitHook);
332
+ this.input.off("data", this.onData);
333
+ if (this.escTimer)
334
+ clearTimeout(this.escTimer);
335
+ try {
336
+ this.input.setRawMode(false);
337
+ }
338
+ catch {
339
+ // already gone
340
+ }
341
+ this.input.pause();
342
+ this.output.write("\u001b[0m\u001b[?2004l\u001b[?7h\u001b[?25h\u001b[?1049l");
343
+ }
344
+ write(s) {
345
+ if (this.active)
346
+ this.output.write(s);
347
+ }
348
+ onKey(fn) {
349
+ this.keyFn = fn;
350
+ }
351
+ onResize(fn) {
352
+ this.output.on("resize", fn);
353
+ }
354
+ }
@@ -0,0 +1,78 @@
1
+ import type { App } from "../app.js";
2
+ import { type HistoryRec } from "../history.js";
3
+ import { onKey, type Data, type Effect, type MessageRow, type Ui } from "./model.js";
4
+ import type { Terminal } from "./terminal.js";
5
+ export declare const TUI_SESSION = "tui";
6
+ /** A relay's `/.well-known/p2p-relay.json` read at most this far (it is a few hundred bytes). */
7
+ export declare const WELL_KNOWN_MAX: number;
8
+ /** Messages kept in memory (the newest). */
9
+ export declare const MESSAGES_MAX = 100;
10
+ /** A response body as text, refusing more than `max` bytes (the read stops there). */
11
+ export declare function readCapped(body: ReadableStream<Uint8Array>, max: number): Promise<string>;
12
+ export interface TuiOptions {
13
+ term: Terminal;
14
+ session?: string;
15
+ version: string;
16
+ /** Stops the TUI (tests, signals). */
17
+ signal?: AbortSignal;
18
+ now?: () => number;
19
+ /** Lines the app or watch would print to stderr: kept here, shown in the status line. */
20
+ log?: (m: string) => void;
21
+ /** Tests: run without the in-process watch. */
22
+ noWatch?: boolean;
23
+ }
24
+ export declare class Tui {
25
+ readonly app: App;
26
+ private readonly o;
27
+ readonly data: Data;
28
+ ui: Ui;
29
+ private readonly now;
30
+ private readonly stop;
31
+ private renderQueued;
32
+ private timers;
33
+ private refreshTimers;
34
+ private promptAnswer?;
35
+ private sent;
36
+ private closed?;
37
+ lastFrame: string;
38
+ constructor(app: App, o: TuiOptions);
39
+ /** Runs until quit or abort; the terminal is always restored. */
40
+ run(): Promise<void>;
41
+ quit(): void;
42
+ private log;
43
+ private every;
44
+ /** Coalesces refreshes of one kind triggered by bursts of events. */
45
+ private soon;
46
+ draw(): void;
47
+ drawNow(): void;
48
+ private startWatch;
49
+ /** One event from the stream (already in the shared log). */
50
+ onEvent(e: Record<string, unknown>): void;
51
+ private messageFromEvent;
52
+ loadAll(): Promise<void>;
53
+ loadPeople(): Promise<void>;
54
+ loadHeld(): Promise<void>;
55
+ /**
56
+ * Received messages from the event log, and sent ones: this run's, receipts asked for, the outbox;
57
+ * then the message history (history.ts) for what is older or was sent elsewhere. The TUI runs
58
+ * only on the person's own terminal (command.ts tuiRefusal), so it reads the person's view.
59
+ */
60
+ loadMessages(): Promise<void>;
61
+ /** Keeps the newest MESSAGES_MAX messages (this run's sends are capped the same way). */
62
+ private capMessages;
63
+ /** Relay up/down from the in-memory pool (no network). */
64
+ relayState(): Promise<void>;
65
+ /** Each relay's `/.well-known/p2p-relay.json` (access, software, pow): read once per doctor run. */
66
+ private relayInfo;
67
+ runDoctor(user?: string): Promise<void>;
68
+ key(k: Parameters<typeof onKey>[2]): void;
69
+ flash(text: string, error?: boolean): void;
70
+ private busy;
71
+ /** Asks one line in a modal (Codex release codes, circle-join confirmations). */
72
+ private ask;
73
+ effect(e: Effect): Promise<void>;
74
+ /** For tests: resolves once the TUI stopped. */
75
+ done(): Promise<void>;
76
+ }
77
+ /** A message history record as a row of the Messages screen (peer text cleaned for the terminal). */
78
+ export declare function historyRow(r: HistoryRec, session: string): MessageRow;