@peer2peer/cli 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/README.md +366 -2
- package/dist/ancestry.d.ts +13 -0
- package/dist/ancestry.js +71 -0
- package/dist/app.d.ts +363 -0
- package/dist/app.js +1177 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +56 -0
- package/dist/circle-ops.d.ts +402 -0
- package/dist/circle-ops.js +1725 -0
- package/dist/cli.d.ts +611 -0
- package/dist/cli.js +1650 -0
- package/dist/codes.d.ts +231 -0
- package/dist/codes.js +998 -0
- package/dist/config.d.ts +112 -0
- package/dist/config.js +243 -0
- package/dist/doctor.d.ts +99 -0
- package/dist/doctor.js +817 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +29 -0
- package/dist/ext-host.d.ts +32 -0
- package/dist/ext-host.js +259 -0
- package/dist/extensions.d.ts +386 -0
- package/dist/extensions.js +395 -0
- package/dist/features.d.ts +91 -0
- package/dist/features.js +164 -0
- package/dist/format.d.ts +57 -0
- package/dist/format.js +248 -0
- package/dist/identity.d.ts +236 -0
- package/dist/identity.js +751 -0
- package/dist/inbound.d.ts +32 -0
- package/dist/inbound.js +258 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +14 -0
- package/dist/join-answers.d.ts +49 -0
- package/dist/join-answers.js +149 -0
- package/dist/outbox.d.ts +109 -0
- package/dist/outbox.js +337 -0
- package/dist/pretty.d.ts +100 -0
- package/dist/pretty.js +256 -0
- package/dist/procstart.d.ts +1 -0
- package/dist/procstart.js +66 -0
- package/dist/progress.d.ts +30 -0
- package/dist/progress.js +78 -0
- package/dist/projects.d.ts +240 -0
- package/dist/projects.js +699 -0
- package/dist/prompt/picker.d.ts +64 -0
- package/dist/prompt/picker.js +152 -0
- package/dist/prompt/tty.d.ts +29 -0
- package/dist/prompt/tty.js +122 -0
- package/dist/readonly.d.ts +20 -0
- package/dist/readonly.js +50 -0
- package/dist/receipts.d.ts +99 -0
- package/dist/receipts.js +180 -0
- package/dist/relay-client.d.ts +277 -0
- package/dist/relay-client.js +873 -0
- package/dist/relay-info.d.ts +24 -0
- package/dist/relay-info.js +53 -0
- package/dist/rotation.d.ts +402 -0
- package/dist/rotation.js +2054 -0
- package/dist/running.d.ts +5 -0
- package/dist/running.js +74 -0
- package/dist/sessions.d.ts +340 -0
- package/dist/sessions.js +1152 -0
- package/dist/shortcode.d.ts +149 -0
- package/dist/shortcode.js +465 -0
- package/dist/social.d.ts +139 -0
- package/dist/social.js +485 -0
- package/dist/state.d.ts +483 -0
- package/dist/state.js +217 -0
- package/dist/stdin.d.ts +17 -0
- package/dist/stdin.js +46 -0
- package/dist/sync.d.ts +77 -0
- package/dist/sync.js +375 -0
- package/dist/transient.d.ts +1 -0
- package/dist/transient.js +28 -0
- package/dist/trust.d.ts +37 -0
- package/dist/trust.js +134 -0
- package/dist/tty-confirm.d.ts +2 -0
- package/dist/tty-confirm.js +53 -0
- package/dist/tui/command.d.ts +20 -0
- package/dist/tui/command.js +70 -0
- package/dist/tui/frame.d.ts +43 -0
- package/dist/tui/frame.js +111 -0
- package/dist/tui/model.d.ts +215 -0
- package/dist/tui/model.js +255 -0
- package/dist/tui/sanitize.d.ts +13 -0
- package/dist/tui/sanitize.js +151 -0
- package/dist/tui/terminal.d.ts +88 -0
- package/dist/tui/terminal.js +354 -0
- package/dist/tui/tui.d.ts +71 -0
- package/dist/tui/tui.js +571 -0
- package/dist/tui/view.d.ts +4 -0
- package/dist/tui/view.js +356 -0
- package/dist/unknown-kinds.d.ts +12 -0
- package/dist/unknown-kinds.js +66 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +5 -0
- package/dist/want-pow.d.ts +12 -0
- package/dist/want-pow.js +46 -0
- package/dist/watch.d.ts +35 -0
- package/dist/watch.js +325 -0
- package/dist/xstate.d.ts +125 -0
- package/dist/xstate.js +422 -0
- package/package.json +60 -4
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// `p2p tui`: interactive only. It refuses to start unless stdin and stdout
|
|
4
|
+
// are both a terminal, and refuses inside an agent's tool call (Codex and
|
|
5
|
+
// Claude Code set CODEX_THREAD_ID / CLAUDECODE there); the Claude mod's
|
|
6
|
+
// command guard refuses it too. Diagnostics the app would print while the
|
|
7
|
+
// TUI owns the screen are kept and printed once the terminal is restored.
|
|
8
|
+
import { CliError, toErrorJson } from "../errors.js";
|
|
9
|
+
import { checkSid } from "../sessions.js";
|
|
10
|
+
import { personHere } from "../projects.js";
|
|
11
|
+
import { TtyTerminal } from "./terminal.js";
|
|
12
|
+
import { Tui, TUI_SESSION } from "./tui.js";
|
|
13
|
+
/** Why `p2p tui` won't start here, or undefined. */
|
|
14
|
+
export function tuiRefusal(env, isTTY, front = false) {
|
|
15
|
+
// An agent's session: its environment, or (scrubbed) a Claude Code or Codex ancestor (projects.ts).
|
|
16
|
+
if (env.CODEX_THREAD_ID || env.CODEX_SESSION_ID || env.CLAUDECODE || !personHere({ env, front })) {
|
|
17
|
+
return "p2p tui is for you to run in your own terminal, not from an agent's tool call";
|
|
18
|
+
}
|
|
19
|
+
if (!isTTY)
|
|
20
|
+
return "p2p tui needs an interactive terminal (stdin and stdout); in scripts use `p2p ls`, `p2p held`, `p2p watch`";
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
export async function tuiCommand(d, o) {
|
|
24
|
+
const isTTY = d.isTTY ?? Boolean(process.stdin.isTTY && process.stdout.isTTY);
|
|
25
|
+
const refusal = tuiRefusal(d.env, d.terminal ? true : isTTY, d.front === true);
|
|
26
|
+
if (refusal) {
|
|
27
|
+
d.io.err(`p2p tui: ${refusal}`);
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
let session;
|
|
31
|
+
try {
|
|
32
|
+
session = checkSid(o.session ?? TUI_SESSION);
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
d.io.err(`p2p tui: ${String(toErrorJson(e).reason)}`);
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
const kept = [];
|
|
39
|
+
const keep = (m) => {
|
|
40
|
+
kept.push(m);
|
|
41
|
+
if (kept.length > 200)
|
|
42
|
+
kept.shift();
|
|
43
|
+
};
|
|
44
|
+
const app = d.makeApp(d.env, { out: () => { }, err: keep });
|
|
45
|
+
try {
|
|
46
|
+
if (!(await app.load(false)))
|
|
47
|
+
throw new CliError("no_identity", "no identity here yet: run `p2p setup` first");
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
const j = toErrorJson(e);
|
|
51
|
+
d.io.err(`p2p tui: ${String(j.error)}: ${String(j.reason)}`);
|
|
52
|
+
return 1;
|
|
53
|
+
}
|
|
54
|
+
const term = d.terminal ?? new TtyTerminal(process.stdin, process.stdout, d.env);
|
|
55
|
+
const tui = new Tui(app, { term, session, version: d.version, ...(d.signal ? { signal: d.signal } : {}), log: keep });
|
|
56
|
+
try {
|
|
57
|
+
await tui.run();
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
const j = toErrorJson(e);
|
|
61
|
+
d.io.err(`p2p tui: ${String(j.error)}: ${String(j.reason)}`);
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
finally {
|
|
65
|
+
// What the app said while the screen was ours, now that it is the terminal's again (last few).
|
|
66
|
+
for (const m of kept.slice(-5))
|
|
67
|
+
d.io.err(m);
|
|
68
|
+
}
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type Color = "green" | "red" | "yellow" | "cyan" | "magenta" | "gray";
|
|
2
|
+
export interface Style {
|
|
3
|
+
fg?: Color;
|
|
4
|
+
bold?: boolean;
|
|
5
|
+
dim?: boolean;
|
|
6
|
+
inverse?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface Span {
|
|
9
|
+
text: string;
|
|
10
|
+
style?: Style;
|
|
11
|
+
}
|
|
12
|
+
export declare class Line {
|
|
13
|
+
readonly width: number;
|
|
14
|
+
readonly spans: Span[];
|
|
15
|
+
private used;
|
|
16
|
+
constructor(width: number);
|
|
17
|
+
get remaining(): number;
|
|
18
|
+
/** Appends sanitized text, cut to `max` columns (and to what's left of the line). */
|
|
19
|
+
add(text: unknown, style?: Style, max?: number): this;
|
|
20
|
+
/** Spaces: a fixed gap (raw spaces, not peer text). */
|
|
21
|
+
gap(n?: number): this;
|
|
22
|
+
/** `text` padded (or cut) to exactly `w` columns. */
|
|
23
|
+
col(text: unknown, w: number, style?: Style): this;
|
|
24
|
+
/** Pads the line to its width (with `style`, e.g. inverse for a selected row). */
|
|
25
|
+
fill(style?: Style): this;
|
|
26
|
+
/** The same line with every span shown selected (inverse). */
|
|
27
|
+
selected(): this;
|
|
28
|
+
text(): string;
|
|
29
|
+
}
|
|
30
|
+
export interface Frame {
|
|
31
|
+
cols: number;
|
|
32
|
+
rows: number;
|
|
33
|
+
lines: Line[];
|
|
34
|
+
}
|
|
35
|
+
export declare function makeFrame(cols: number, rows: number): Frame;
|
|
36
|
+
/** Plain text, one line per row, padded: for snapshot tests and NO_COLOR. */
|
|
37
|
+
export declare function frameText(f: Frame): string;
|
|
38
|
+
/**
|
|
39
|
+
* The whole frame as terminal output: cursor home, each row then erase-to-end,
|
|
40
|
+
* styles reset after every styled span. Without colour, only bold and inverse
|
|
41
|
+
* (selection must stay visible) are used.
|
|
42
|
+
*/
|
|
43
|
+
export declare function toAnsi(f: Frame, color: boolean): string;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// A frame: rows of styled spans, exactly `cols` wide. Spans are built only by
|
|
4
|
+
// Line.add, which runs every piece of text through cell() (sanitize.ts), so
|
|
5
|
+
// nothing reaches the terminal unsanitized. toAnsi() adds the only escape
|
|
6
|
+
// sequences the TUI ever writes (SGR colours, erase-line, cursor home); they
|
|
7
|
+
// come from this file, never from data.
|
|
8
|
+
import { cell, displayWidth } from "./sanitize.js";
|
|
9
|
+
export class Line {
|
|
10
|
+
width;
|
|
11
|
+
spans = [];
|
|
12
|
+
used = 0;
|
|
13
|
+
constructor(width) {
|
|
14
|
+
this.width = width;
|
|
15
|
+
}
|
|
16
|
+
get remaining() {
|
|
17
|
+
return Math.max(0, this.width - this.used);
|
|
18
|
+
}
|
|
19
|
+
/** Appends sanitized text, cut to `max` columns (and to what's left of the line). */
|
|
20
|
+
add(text, style, max = Infinity) {
|
|
21
|
+
const room = Math.min(this.remaining, max);
|
|
22
|
+
const t = cell(text, room);
|
|
23
|
+
if (t !== "") {
|
|
24
|
+
this.spans.push(style ? { text: t, style } : { text: t });
|
|
25
|
+
this.used += displayWidth(t);
|
|
26
|
+
}
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
/** Spaces: a fixed gap (raw spaces, not peer text). */
|
|
30
|
+
gap(n = 1) {
|
|
31
|
+
const k = Math.min(n, this.remaining);
|
|
32
|
+
if (k > 0) {
|
|
33
|
+
this.spans.push({ text: " ".repeat(k) });
|
|
34
|
+
this.used += k;
|
|
35
|
+
}
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
/** `text` padded (or cut) to exactly `w` columns. */
|
|
39
|
+
col(text, w, style) {
|
|
40
|
+
const before = this.used;
|
|
41
|
+
this.add(text, style, w);
|
|
42
|
+
return this.gap(w - (this.used - before));
|
|
43
|
+
}
|
|
44
|
+
/** Pads the line to its width (with `style`, e.g. inverse for a selected row). */
|
|
45
|
+
fill(style) {
|
|
46
|
+
const k = this.remaining;
|
|
47
|
+
if (k > 0) {
|
|
48
|
+
this.spans.push(style ? { text: " ".repeat(k), style } : { text: " ".repeat(k) });
|
|
49
|
+
this.used += k;
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/** The same line with every span shown selected (inverse). */
|
|
54
|
+
selected() {
|
|
55
|
+
for (const s of this.spans)
|
|
56
|
+
s.style = { ...s.style, inverse: true };
|
|
57
|
+
return this.fill({ inverse: true });
|
|
58
|
+
}
|
|
59
|
+
text() {
|
|
60
|
+
return this.spans.map((s) => s.text).join("");
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function makeFrame(cols, rows) {
|
|
64
|
+
return { cols, rows, lines: [] };
|
|
65
|
+
}
|
|
66
|
+
/** Plain text, one line per row, padded: for snapshot tests and NO_COLOR. */
|
|
67
|
+
export function frameText(f) {
|
|
68
|
+
const out = [];
|
|
69
|
+
for (let i = 0; i < f.rows; i++) {
|
|
70
|
+
const l = f.lines[i];
|
|
71
|
+
const t = l ? l.text() : "";
|
|
72
|
+
out.push(t + " ".repeat(Math.max(0, f.cols - displayWidth(t))));
|
|
73
|
+
}
|
|
74
|
+
return out.join("\n");
|
|
75
|
+
}
|
|
76
|
+
const FG = { red: 31, green: 32, yellow: 33, magenta: 35, cyan: 36, gray: 90 };
|
|
77
|
+
function sgr(s, color) {
|
|
78
|
+
if (!s)
|
|
79
|
+
return "";
|
|
80
|
+
const codes = [];
|
|
81
|
+
if (s.bold)
|
|
82
|
+
codes.push(1);
|
|
83
|
+
if (s.dim && color)
|
|
84
|
+
codes.push(2);
|
|
85
|
+
if (s.inverse)
|
|
86
|
+
codes.push(7);
|
|
87
|
+
if (s.fg && color)
|
|
88
|
+
codes.push(FG[s.fg]);
|
|
89
|
+
return codes.length ? `\u001b[${codes.join(";")}m` : "";
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The whole frame as terminal output: cursor home, each row then erase-to-end,
|
|
93
|
+
* styles reset after every styled span. Without colour, only bold and inverse
|
|
94
|
+
* (selection must stay visible) are used.
|
|
95
|
+
*/
|
|
96
|
+
export function toAnsi(f, color) {
|
|
97
|
+
let out = "\u001b[H";
|
|
98
|
+
for (let i = 0; i < f.rows; i++) {
|
|
99
|
+
const l = f.lines[i];
|
|
100
|
+
if (l) {
|
|
101
|
+
for (const s of l.spans) {
|
|
102
|
+
const on = sgr(s.style, color);
|
|
103
|
+
out += on ? `${on}${s.text}\u001b[0m` : s.text;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
out += "\u001b[K";
|
|
107
|
+
if (i < f.rows - 1)
|
|
108
|
+
out += "\r\n";
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
export type Screen = "people" | "held" | "messages" | "send" | "health";
|
|
2
|
+
export declare const SCREENS: readonly Screen[];
|
|
3
|
+
export declare const SCREEN_TITLES: Record<Screen, string>;
|
|
4
|
+
export interface AgentRow {
|
|
5
|
+
id: string;
|
|
6
|
+
parent?: string;
|
|
7
|
+
type: string;
|
|
8
|
+
task: string;
|
|
9
|
+
state: string;
|
|
10
|
+
branch?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface SessionRow {
|
|
13
|
+
own: string;
|
|
14
|
+
sid: string;
|
|
15
|
+
ref: string;
|
|
16
|
+
label: string;
|
|
17
|
+
state: string;
|
|
18
|
+
live: boolean;
|
|
19
|
+
ageSec: number;
|
|
20
|
+
summary?: string;
|
|
21
|
+
repo?: string;
|
|
22
|
+
branch?: string;
|
|
23
|
+
files: string[];
|
|
24
|
+
agents: AgentRow[];
|
|
25
|
+
thisDevice?: boolean;
|
|
26
|
+
}
|
|
27
|
+
export interface PersonRow {
|
|
28
|
+
userId: string;
|
|
29
|
+
address: string;
|
|
30
|
+
/** How they relate: friend, circle-mate (names of shared circles), or you. */
|
|
31
|
+
relation: string;
|
|
32
|
+
self: boolean;
|
|
33
|
+
live: boolean;
|
|
34
|
+
sessions: SessionRow[];
|
|
35
|
+
}
|
|
36
|
+
export interface HeldRow {
|
|
37
|
+
/** "item": a CLI held item (p2p held); "codex": a message a Codex session holds. */
|
|
38
|
+
source: "item" | "codex";
|
|
39
|
+
id: string;
|
|
40
|
+
type: string;
|
|
41
|
+
from: string;
|
|
42
|
+
/** codex: the session that holds it. */
|
|
43
|
+
session?: string;
|
|
44
|
+
summary: string;
|
|
45
|
+
at: number;
|
|
46
|
+
}
|
|
47
|
+
export type Outcome = "shown" | "held" | "declined" | "expired" | "ended";
|
|
48
|
+
export interface MessageRow {
|
|
49
|
+
id: string;
|
|
50
|
+
dir: "in" | "out";
|
|
51
|
+
at: number;
|
|
52
|
+
/** The other side: address, and the session (ref) when known. */
|
|
53
|
+
who: string;
|
|
54
|
+
userId?: string;
|
|
55
|
+
own?: string;
|
|
56
|
+
sid?: string;
|
|
57
|
+
ref?: string;
|
|
58
|
+
text: string;
|
|
59
|
+
/** out: delivery state and receipt outcomes. */
|
|
60
|
+
state?: "sent" | "offline" | "outbox" | "failed" | "expired";
|
|
61
|
+
outcomes?: Outcome[];
|
|
62
|
+
lastSeen?: number;
|
|
63
|
+
receiptId?: string;
|
|
64
|
+
note?: string;
|
|
65
|
+
}
|
|
66
|
+
export interface RelayRow {
|
|
67
|
+
url: string;
|
|
68
|
+
up: boolean;
|
|
69
|
+
reason?: string;
|
|
70
|
+
access?: string;
|
|
71
|
+
attention?: boolean;
|
|
72
|
+
software?: string;
|
|
73
|
+
pow?: number;
|
|
74
|
+
}
|
|
75
|
+
export interface CheckRow {
|
|
76
|
+
status: "ok" | "fail" | "unknown";
|
|
77
|
+
title: string;
|
|
78
|
+
detail: string;
|
|
79
|
+
fix?: string;
|
|
80
|
+
}
|
|
81
|
+
export interface Health {
|
|
82
|
+
relays: RelayRow[];
|
|
83
|
+
doctor?: {
|
|
84
|
+
scope: string;
|
|
85
|
+
who?: string;
|
|
86
|
+
checks: CheckRow[];
|
|
87
|
+
summary: {
|
|
88
|
+
ok: number;
|
|
89
|
+
fail: number;
|
|
90
|
+
unknown: number;
|
|
91
|
+
};
|
|
92
|
+
at: number;
|
|
93
|
+
};
|
|
94
|
+
running?: boolean;
|
|
95
|
+
}
|
|
96
|
+
export interface Data {
|
|
97
|
+
me?: {
|
|
98
|
+
userId: string;
|
|
99
|
+
address: string;
|
|
100
|
+
};
|
|
101
|
+
session: string;
|
|
102
|
+
people: PersonRow[];
|
|
103
|
+
held: HeldRow[];
|
|
104
|
+
/** Why held messages of the Claude Code mod aren't listed (shown on the Held screen). */
|
|
105
|
+
heldNote: string;
|
|
106
|
+
messages: MessageRow[];
|
|
107
|
+
health: Health;
|
|
108
|
+
loading: boolean;
|
|
109
|
+
}
|
|
110
|
+
export type Modal = {
|
|
111
|
+
kind: "confirm";
|
|
112
|
+
title: string;
|
|
113
|
+
lines: string[];
|
|
114
|
+
effect: Effect;
|
|
115
|
+
} | {
|
|
116
|
+
kind: "prompt";
|
|
117
|
+
title: string;
|
|
118
|
+
lines: string[];
|
|
119
|
+
text: string;
|
|
120
|
+
secret?: boolean;
|
|
121
|
+
}
|
|
122
|
+
/** A confirmation that takes a typed word (not y, not a paste): for trust. */
|
|
123
|
+
| {
|
|
124
|
+
kind: "typed";
|
|
125
|
+
title: string;
|
|
126
|
+
lines: string[];
|
|
127
|
+
word: string;
|
|
128
|
+
text: string;
|
|
129
|
+
effect: Effect;
|
|
130
|
+
} | {
|
|
131
|
+
kind: "detail";
|
|
132
|
+
title: string;
|
|
133
|
+
lines: string[];
|
|
134
|
+
} | {
|
|
135
|
+
kind: "help";
|
|
136
|
+
};
|
|
137
|
+
export interface SendTarget {
|
|
138
|
+
label: string;
|
|
139
|
+
/** What sendMessage gets as `to`: a userId, with "[ref]" for one session. */
|
|
140
|
+
to: string;
|
|
141
|
+
}
|
|
142
|
+
export interface Ui {
|
|
143
|
+
screen: Screen;
|
|
144
|
+
sel: Record<Screen, number>;
|
|
145
|
+
modal?: Modal;
|
|
146
|
+
/** Send screen: the chosen target and the text being typed. */
|
|
147
|
+
compose?: {
|
|
148
|
+
target: SendTarget;
|
|
149
|
+
text: string;
|
|
150
|
+
};
|
|
151
|
+
flash?: {
|
|
152
|
+
text: string;
|
|
153
|
+
error?: boolean;
|
|
154
|
+
};
|
|
155
|
+
busy?: string;
|
|
156
|
+
}
|
|
157
|
+
export type Effect = {
|
|
158
|
+
kind: "quit";
|
|
159
|
+
} | {
|
|
160
|
+
kind: "accept-item";
|
|
161
|
+
id: string;
|
|
162
|
+
} | {
|
|
163
|
+
kind: "decline-item";
|
|
164
|
+
id: string;
|
|
165
|
+
} | {
|
|
166
|
+
kind: "codex-release";
|
|
167
|
+
id: string;
|
|
168
|
+
} | {
|
|
169
|
+
kind: "codex-decline";
|
|
170
|
+
id: string;
|
|
171
|
+
} | {
|
|
172
|
+
kind: "trust-and-release";
|
|
173
|
+
id: string;
|
|
174
|
+
from: string;
|
|
175
|
+
} | {
|
|
176
|
+
kind: "send";
|
|
177
|
+
to: string;
|
|
178
|
+
label: string;
|
|
179
|
+
text: string;
|
|
180
|
+
} | {
|
|
181
|
+
kind: "doctor";
|
|
182
|
+
user?: string;
|
|
183
|
+
} | {
|
|
184
|
+
kind: "prompt-answer";
|
|
185
|
+
text: string;
|
|
186
|
+
} | {
|
|
187
|
+
kind: "prompt-cancel";
|
|
188
|
+
};
|
|
189
|
+
export interface Key {
|
|
190
|
+
name: string;
|
|
191
|
+
ch?: string;
|
|
192
|
+
ctrl?: boolean;
|
|
193
|
+
shift?: boolean;
|
|
194
|
+
/** Part of a bracketed paste: typed only into a message or a prompt, never a command key. */
|
|
195
|
+
paste?: boolean;
|
|
196
|
+
}
|
|
197
|
+
export declare function initialUi(): Ui;
|
|
198
|
+
export type PeopleItem = {
|
|
199
|
+
kind: "person";
|
|
200
|
+
person: PersonRow;
|
|
201
|
+
} | {
|
|
202
|
+
kind: "session";
|
|
203
|
+
person: PersonRow;
|
|
204
|
+
session: SessionRow;
|
|
205
|
+
};
|
|
206
|
+
export declare function peopleItems(d: Data): PeopleItem[];
|
|
207
|
+
/** Send targets: live sessions first (not this TUI's own), then each person (their one session, or our session). */
|
|
208
|
+
export declare function sendTargets(d: Data): SendTarget[];
|
|
209
|
+
export declare function messagesSorted(d: Data): MessageRow[];
|
|
210
|
+
export declare function clampSel(ui: Ui, d: Data): Ui;
|
|
211
|
+
/** One key press. Returns the next ui and, at most, one effect. */
|
|
212
|
+
export declare function onKey(ui0: Ui, d: Data, key: Key, page?: number): {
|
|
213
|
+
ui: Ui;
|
|
214
|
+
effect?: Effect;
|
|
215
|
+
};
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// Copyright 2026 Ix Infrastructure Inc.
|
|
2
|
+
//
|
|
3
|
+
// The TUI's state and its key handling, as pure functions: a key press turns
|
|
4
|
+
// (ui, data) into a new ui and, at most, one effect for the controller
|
|
5
|
+
// (tui.ts) to run. Only key presses produce effects that change anything
|
|
6
|
+
// (accept, decline, trust, send); loading data and live updates
|
|
7
|
+
// never do.
|
|
8
|
+
export const SCREENS = ["people", "held", "messages", "send", "health"];
|
|
9
|
+
export const SCREEN_TITLES = { people: "People", held: "Held", messages: "Messages", send: "Send", health: "Health" };
|
|
10
|
+
export function initialUi() {
|
|
11
|
+
return { screen: "people", sel: { people: 0, held: 0, messages: 0, send: 0, health: 0 } };
|
|
12
|
+
}
|
|
13
|
+
export function peopleItems(d) {
|
|
14
|
+
const out = [];
|
|
15
|
+
for (const p of d.people) {
|
|
16
|
+
out.push({ kind: "person", person: p });
|
|
17
|
+
for (const s of p.sessions)
|
|
18
|
+
out.push({ kind: "session", person: p, session: s });
|
|
19
|
+
}
|
|
20
|
+
return out;
|
|
21
|
+
}
|
|
22
|
+
/** Send targets: live sessions first (not this TUI's own), then each person (their one session, or our session). */
|
|
23
|
+
export function sendTargets(d) {
|
|
24
|
+
const sessions = [];
|
|
25
|
+
const people = [];
|
|
26
|
+
for (const p of d.people) {
|
|
27
|
+
for (const s of p.sessions) {
|
|
28
|
+
if (p.self && s.thisDevice && s.sid === d.session)
|
|
29
|
+
continue;
|
|
30
|
+
sessions.push({ label: `${p.address} [${s.ref}] ${s.label}${s.live ? "" : " (offline)"}`, to: `${p.userId} [${s.ref}]` });
|
|
31
|
+
}
|
|
32
|
+
if (!p.self)
|
|
33
|
+
people.push({ label: `${p.address} (any session)`, to: p.userId });
|
|
34
|
+
}
|
|
35
|
+
return [...sessions, ...people];
|
|
36
|
+
}
|
|
37
|
+
export function messagesSorted(d) {
|
|
38
|
+
return [...d.messages].sort((a, b) => b.at - a.at);
|
|
39
|
+
}
|
|
40
|
+
function count(ui, d) {
|
|
41
|
+
switch (ui.screen) {
|
|
42
|
+
case "people":
|
|
43
|
+
return peopleItems(d).length;
|
|
44
|
+
case "held":
|
|
45
|
+
return d.held.length;
|
|
46
|
+
case "messages":
|
|
47
|
+
return d.messages.length;
|
|
48
|
+
case "send":
|
|
49
|
+
return sendTargets(d).length;
|
|
50
|
+
case "health":
|
|
51
|
+
return d.health.relays.length + (d.health.doctor?.checks.length ?? 0);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export function clampSel(ui, d) {
|
|
55
|
+
const n = count(ui, d);
|
|
56
|
+
const cur = ui.sel[ui.screen];
|
|
57
|
+
const v = n === 0 ? 0 : Math.max(0, Math.min(cur, n - 1));
|
|
58
|
+
return v === cur ? ui : { ...ui, sel: { ...ui.sel, [ui.screen]: v } };
|
|
59
|
+
}
|
|
60
|
+
const flash = (ui, text, error = false) => ({ ...ui, flash: { text, ...(error ? { error } : {}) } });
|
|
61
|
+
/** One key press. Returns the next ui and, at most, one effect. */
|
|
62
|
+
export function onKey(ui0, d, key, page = 10) {
|
|
63
|
+
const ui = { ...ui0, flash: undefined };
|
|
64
|
+
if (key.ctrl && key.name === "c")
|
|
65
|
+
return { ui, effect: { kind: "quit" } };
|
|
66
|
+
// Modals take every key.
|
|
67
|
+
const m = ui.modal;
|
|
68
|
+
if (m) {
|
|
69
|
+
if (m.kind === "typed") {
|
|
70
|
+
if (key.name === "escape")
|
|
71
|
+
return { ui: flash({ ...ui, modal: undefined }, "Cancelled.") };
|
|
72
|
+
if (key.paste)
|
|
73
|
+
return { ui: { ...ui, flash: { text: "Type it: a paste doesn't count here.", error: true } } };
|
|
74
|
+
if (key.name === "return") {
|
|
75
|
+
if (m.text.trim().toLowerCase() === m.word.toLowerCase())
|
|
76
|
+
return { ui: { ...ui, modal: undefined }, effect: m.effect };
|
|
77
|
+
return { ui: { ...ui, modal: { ...m, text: "" }, flash: { text: `Type ${m.word} exactly, or Esc to cancel.`, error: true } } };
|
|
78
|
+
}
|
|
79
|
+
if (key.name === "backspace")
|
|
80
|
+
return { ui: { ...ui, modal: { ...m, text: [...m.text].slice(0, -1).join("") } } };
|
|
81
|
+
if (key.ch && !key.ctrl && m.text.length < 32)
|
|
82
|
+
return { ui: { ...ui, modal: { ...m, text: m.text + key.ch } } };
|
|
83
|
+
return { ui };
|
|
84
|
+
}
|
|
85
|
+
// A paste never answers a confirmation or closes a dialog.
|
|
86
|
+
if (key.paste && m.kind !== "prompt")
|
|
87
|
+
return { ui };
|
|
88
|
+
if (m.kind === "confirm") {
|
|
89
|
+
if (key.ch === "y" || key.ch === "Y")
|
|
90
|
+
return { ui: { ...ui, modal: undefined }, effect: m.effect };
|
|
91
|
+
if (key.ch === "n" || key.ch === "N" || key.name === "escape" || key.ch === "q")
|
|
92
|
+
return { ui: flash({ ...ui, modal: undefined }, "Cancelled.") };
|
|
93
|
+
return { ui: { ...ui, flash: { text: "Press y to confirm, n to cancel." } } };
|
|
94
|
+
}
|
|
95
|
+
if (m.kind === "prompt") {
|
|
96
|
+
if (key.name === "escape")
|
|
97
|
+
return { ui: { ...ui, modal: undefined }, effect: { kind: "prompt-cancel" } };
|
|
98
|
+
if (key.name === "return")
|
|
99
|
+
return { ui: { ...ui, modal: undefined }, effect: { kind: "prompt-answer", text: m.text } };
|
|
100
|
+
if (key.name === "backspace")
|
|
101
|
+
return { ui: { ...ui, modal: { ...m, text: [...m.text].slice(0, -1).join("") } } };
|
|
102
|
+
if (key.ch && !key.ctrl && m.text.length < 64)
|
|
103
|
+
return { ui: { ...ui, modal: { ...m, text: m.text + key.ch } } };
|
|
104
|
+
return { ui };
|
|
105
|
+
}
|
|
106
|
+
// detail, help: any of these closes.
|
|
107
|
+
if (key.name === "escape" || key.name === "return" || key.ch === "q" || key.ch === "?" || key.name === "space")
|
|
108
|
+
return { ui: { ...ui, modal: undefined } };
|
|
109
|
+
return { ui };
|
|
110
|
+
}
|
|
111
|
+
// Typing a message.
|
|
112
|
+
if (ui.screen === "send" && ui.compose) {
|
|
113
|
+
const c = ui.compose;
|
|
114
|
+
if (key.name === "escape")
|
|
115
|
+
return { ui: flash({ ...ui, compose: undefined }, "Not sent.") };
|
|
116
|
+
if (key.name === "return") {
|
|
117
|
+
const text = c.text.trim();
|
|
118
|
+
if (!text)
|
|
119
|
+
return { ui: flash(ui, "Type a message first (Esc to cancel).", true) };
|
|
120
|
+
return { ui: { ...ui, compose: undefined }, effect: { kind: "send", to: c.target.to, label: c.target.label, text } };
|
|
121
|
+
}
|
|
122
|
+
if (key.name === "backspace")
|
|
123
|
+
return { ui: { ...ui, compose: { ...c, text: [...c.text].slice(0, -1).join("") } } };
|
|
124
|
+
if (key.ctrl && key.name === "u")
|
|
125
|
+
return { ui: { ...ui, compose: { ...c, text: "" } } };
|
|
126
|
+
if (key.ch && !key.ctrl && Buffer.byteLength(c.text + key.ch) <= 4000)
|
|
127
|
+
return { ui: { ...ui, compose: { ...c, text: c.text + key.ch } } };
|
|
128
|
+
return { ui };
|
|
129
|
+
}
|
|
130
|
+
// Pasted text outside a message or prompt is dropped: it must never act as command keys.
|
|
131
|
+
if (key.paste)
|
|
132
|
+
return { ui: { ...ui, flash: { text: "A paste only goes into a message: pick someone on Send first." } } };
|
|
133
|
+
if (key.ch === "q")
|
|
134
|
+
return { ui, effect: { kind: "quit" } };
|
|
135
|
+
if (key.ch === "?")
|
|
136
|
+
return { ui: { ...ui, modal: { kind: "help" } } };
|
|
137
|
+
const n = Number(key.ch);
|
|
138
|
+
if (key.ch && n >= 1 && n <= SCREENS.length)
|
|
139
|
+
return { ui: clampSel({ ...ui, screen: SCREENS[n - 1] }, d) };
|
|
140
|
+
if (key.name === "tab") {
|
|
141
|
+
const i = SCREENS.indexOf(ui.screen);
|
|
142
|
+
const next = SCREENS[(i + (key.shift ? SCREENS.length - 1 : 1)) % SCREENS.length];
|
|
143
|
+
return { ui: clampSel({ ...ui, screen: next }, d) };
|
|
144
|
+
}
|
|
145
|
+
const move = (delta) => clampSel({ ...ui, sel: { ...ui.sel, [ui.screen]: ui.sel[ui.screen] + delta } }, d);
|
|
146
|
+
if (key.name === "up" || key.ch === "k")
|
|
147
|
+
return { ui: move(-1) };
|
|
148
|
+
if (key.name === "down" || key.ch === "j")
|
|
149
|
+
return { ui: move(1) };
|
|
150
|
+
if (key.name === "pageup")
|
|
151
|
+
return { ui: move(-page) };
|
|
152
|
+
if (key.name === "pagedown")
|
|
153
|
+
return { ui: move(page) };
|
|
154
|
+
if (key.name === "home" || key.ch === "g")
|
|
155
|
+
return { ui: move(-1e9) };
|
|
156
|
+
if (key.name === "end" || key.ch === "G")
|
|
157
|
+
return { ui: move(1e9) };
|
|
158
|
+
const sel = ui.sel[ui.screen];
|
|
159
|
+
switch (ui.screen) {
|
|
160
|
+
case "people": {
|
|
161
|
+
const it = peopleItems(d)[sel];
|
|
162
|
+
if (!it)
|
|
163
|
+
return { ui };
|
|
164
|
+
if (key.ch === "m" || key.name === "return") {
|
|
165
|
+
if (it.person.self && it.kind === "person")
|
|
166
|
+
return { ui: flash(ui, "Pick one of your sessions, or someone else.") };
|
|
167
|
+
const target = it.kind === "session" ? { label: `${it.person.address} [${it.session.ref}] ${it.session.label}`, to: `${it.person.userId} [${it.session.ref}]` } : { label: `${it.person.address} (any session)`, to: it.person.userId };
|
|
168
|
+
return { ui: { ...ui, screen: "send", compose: { target, text: "" } } };
|
|
169
|
+
}
|
|
170
|
+
if (key.ch === "d")
|
|
171
|
+
return { ui: { ...ui, screen: "health" }, effect: { kind: "doctor", user: it.person.self ? undefined : it.person.userId } };
|
|
172
|
+
return { ui };
|
|
173
|
+
}
|
|
174
|
+
case "held": {
|
|
175
|
+
const h = d.held[sel];
|
|
176
|
+
if (!h)
|
|
177
|
+
return { ui };
|
|
178
|
+
if (key.ch === "a") {
|
|
179
|
+
// A Codex release asks for the code its window shows; everything else confirms here.
|
|
180
|
+
if (h.source === "codex")
|
|
181
|
+
return { ui, effect: { kind: "codex-release", id: h.id } };
|
|
182
|
+
const what = h.type === "circle-join"
|
|
183
|
+
? `Admit ${h.from} to your circle?`
|
|
184
|
+
: h.type === "invite"
|
|
185
|
+
? `Accept the friend invite from ${h.from}? You will be friends.`
|
|
186
|
+
: h.type === "circle-invite"
|
|
187
|
+
? `Join the circle ${h.from} invited you to?`
|
|
188
|
+
: `Act on the ${h.type} from ${h.from}?`;
|
|
189
|
+
return { ui: { ...ui, modal: { kind: "confirm", title: "Accept", lines: [what, h.summary], effect: { kind: "accept-item", id: h.id } } } };
|
|
190
|
+
}
|
|
191
|
+
if (key.ch === "x") {
|
|
192
|
+
return {
|
|
193
|
+
ui: {
|
|
194
|
+
...ui,
|
|
195
|
+
modal: { kind: "confirm", title: "Decline", lines: [`Decline ${h.type} from ${h.from}?`], effect: h.source === "codex" ? { kind: "codex-decline", id: h.id } : { kind: "decline-item", id: h.id } },
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
if (key.ch === "t") {
|
|
200
|
+
if (h.source !== "codex")
|
|
201
|
+
return { ui: flash(ui, "Always deliver applies to held messages, not to invites or join requests.", true) };
|
|
202
|
+
const fp = h.from.split("@")[1] ?? "";
|
|
203
|
+
if (!/^[a-z2-7]{4}(-[a-z2-7]{4}){3}$/.test(fp))
|
|
204
|
+
return { ui: flash(ui, "That sender has no fingerprint to check: trust them from your terminal instead.", true) };
|
|
205
|
+
const word = fp.slice(0, 4);
|
|
206
|
+
return {
|
|
207
|
+
ui: {
|
|
208
|
+
...ui,
|
|
209
|
+
modal: {
|
|
210
|
+
kind: "typed",
|
|
211
|
+
title: "Always deliver",
|
|
212
|
+
lines: [
|
|
213
|
+
`Always deliver messages from ${h.from}?`,
|
|
214
|
+
`Check the fingerprint with them, out of band: ${fp}`,
|
|
215
|
+
"Their messages then reach sessions that approve their own tool calls without the hold.",
|
|
216
|
+
"Trust only skips the hold: their messages are still their words, never your instructions.",
|
|
217
|
+
"",
|
|
218
|
+
`To trust them, type the fingerprint's first four characters (${word}) and press Enter. Esc cancels.`,
|
|
219
|
+
],
|
|
220
|
+
word,
|
|
221
|
+
text: "",
|
|
222
|
+
effect: { kind: "trust-and-release", id: h.id, from: h.from },
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return { ui };
|
|
228
|
+
}
|
|
229
|
+
case "messages": {
|
|
230
|
+
const msg = messagesSorted(d)[sel];
|
|
231
|
+
if (!msg)
|
|
232
|
+
return { ui };
|
|
233
|
+
if (key.name === "return" || key.name === "space") {
|
|
234
|
+
const head = msg.dir === "in" ? `From ${msg.who}${msg.ref ? ` [${msg.ref}]` : ""}` : `To ${msg.who}${msg.ref ? ` [${msg.ref}]` : ""}`;
|
|
235
|
+
// No receipt goes out from here: the TUI's session is private (see tui.ts).
|
|
236
|
+
return { ui: { ...ui, modal: { kind: "detail", title: head, lines: [msg.text] } } };
|
|
237
|
+
}
|
|
238
|
+
if (key.ch === "r" && msg.dir === "in" && msg.userId) {
|
|
239
|
+
const target = msg.ref ? { label: `${msg.who} [${msg.ref}]`, to: `${msg.userId} [${msg.ref}]` } : { label: `${msg.who} (any session)`, to: msg.userId };
|
|
240
|
+
return { ui: { ...ui, screen: "send", compose: { target, text: "" } } };
|
|
241
|
+
}
|
|
242
|
+
return { ui };
|
|
243
|
+
}
|
|
244
|
+
case "send": {
|
|
245
|
+
const t = sendTargets(d)[sel];
|
|
246
|
+
if (t && key.name === "return")
|
|
247
|
+
return { ui: { ...ui, compose: { target: t, text: "" } } };
|
|
248
|
+
return { ui };
|
|
249
|
+
}
|
|
250
|
+
case "health":
|
|
251
|
+
if (key.ch === "r")
|
|
252
|
+
return { ui, effect: { kind: "doctor" } };
|
|
253
|
+
return { ui };
|
|
254
|
+
}
|
|
255
|
+
}
|