rucan 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.
- package/LICENSE +202 -0
- package/NOTICE +6 -0
- package/README.md +48 -2
- package/agents/THIRD_PARTY_LICENSES.txt +6535 -0
- package/agents/darwin-arm64/rucan-agent +0 -0
- package/agents/darwin-x64/rucan-agent +0 -0
- package/agents/linux-arm64/rucan-agent +0 -0
- package/agents/linux-x64/rucan-agent +0 -0
- package/agents/manifest.json +9 -0
- package/dist/agent/artifacts.js +52 -0
- package/dist/agent/local.js +80 -0
- package/dist/agent/ssh.js +165 -0
- package/dist/commands/doctor.js +56 -0
- package/dist/commands/remote.js +28 -0
- package/dist/commands/sessions.js +94 -0
- package/dist/commands/tmux.js +42 -0
- package/dist/commands/update.js +17 -0
- package/dist/config/load.js +78 -0
- package/dist/config/schema.js +161 -0
- package/dist/explorer/explorer.js +220 -0
- package/dist/main.js +62 -0
- package/dist/protocol/client.js +124 -0
- package/dist/protocol/framing.js +57 -0
- package/dist/protocol/operation.js +1 -0
- package/dist/release/updates.js +142 -0
- package/dist/release/version.js +30 -0
- package/dist/terminal/input.js +94 -0
- package/dist/terminal/interactive.js +676 -0
- package/dist/terminal/mouse.js +27 -0
- package/dist/terminal/raw-input.js +42 -0
- package/dist/terminal/screen.js +182 -0
- package/dist/terminal/snapshot.js +114 -0
- package/dist/terminal/tmux.js +38 -0
- package/dist/terminal/view.js +64 -0
- package/dist/terminal/watch.js +66 -0
- package/dist/terminal/workspaces.js +85 -0
- package/dist/ui/workspace.js +48 -0
- package/package.json +36 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { InputRouter } from "./input.js";
|
|
2
|
+
/** Set terminal input state for the lifetime of a mounted view. */
|
|
3
|
+
export function mountRawInput(prefix, handler) {
|
|
4
|
+
const router = new InputRouter(prefix);
|
|
5
|
+
const prior = process.stdin.isRaw;
|
|
6
|
+
let active = true;
|
|
7
|
+
let timer;
|
|
8
|
+
const data = (chunk) => {
|
|
9
|
+
process.stdin.pause();
|
|
10
|
+
clearTimeout(timer);
|
|
11
|
+
void handler.input(router.route(chunk)).finally(() => {
|
|
12
|
+
if (!active)
|
|
13
|
+
return;
|
|
14
|
+
if (router.hasPending) {
|
|
15
|
+
timer = setTimeout(() => {
|
|
16
|
+
const bytes = router.takePending();
|
|
17
|
+
if (bytes.length)
|
|
18
|
+
void handler.input([{ type: "bytes", bytes }]);
|
|
19
|
+
}, 18);
|
|
20
|
+
}
|
|
21
|
+
process.stdin.resume();
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
process.stdin.setRawMode(true);
|
|
25
|
+
process.stdin.on("data", data);
|
|
26
|
+
process.stdin.once("end", handler.stop);
|
|
27
|
+
process.stdout.on("resize", handler.resize);
|
|
28
|
+
for (const signal of ["SIGTERM", "SIGHUP", "SIGINT"])
|
|
29
|
+
process.once(signal, handler.stop);
|
|
30
|
+
process.stdin.resume();
|
|
31
|
+
return () => {
|
|
32
|
+
active = false;
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
process.stdin.off("data", data);
|
|
35
|
+
process.stdin.off("end", handler.stop);
|
|
36
|
+
process.stdout.off("resize", handler.resize);
|
|
37
|
+
for (const signal of ["SIGTERM", "SIGHUP", "SIGINT"])
|
|
38
|
+
process.off(signal, handler.stop);
|
|
39
|
+
process.stdin.pause();
|
|
40
|
+
process.stdin.setRawMode(prior);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import stringWidth from "string-width";
|
|
2
|
+
import { version } from "../release/version.js";
|
|
3
|
+
const reset = "\x1b[0m";
|
|
4
|
+
const chrome = "\x1b[1;36m";
|
|
5
|
+
const brand = "\x1b[1;30;106m";
|
|
6
|
+
const activeTab = "\x1b[1;97;44m";
|
|
7
|
+
const connected = "\x1b[32m";
|
|
8
|
+
const attention = "\x1b[33m";
|
|
9
|
+
const divider = "\x1b[2m";
|
|
10
|
+
const hideCursor = "\x1b[?25l";
|
|
11
|
+
const move = (row, col) => `\x1b[${row};${col}H`;
|
|
12
|
+
const segmenter = new Intl.Segmenter("en", { granularity: "grapheme" });
|
|
13
|
+
export function geometry(rows, cols) {
|
|
14
|
+
const sidebar = cols >= 48 ? Math.max(16, Math.floor(cols * 0.25)) : 0;
|
|
15
|
+
const terminalCols = Math.max(1, Math.min(500, cols - sidebar));
|
|
16
|
+
const terminalRows = Math.max(1, Math.min(200, rows - 2, Math.floor(12000 / terminalCols)));
|
|
17
|
+
return { rows, cols, sidebar, terminalRows, terminalCols };
|
|
18
|
+
}
|
|
19
|
+
function fit(text, width) {
|
|
20
|
+
const safe = text.replace(/[\p{Cc}\p{Bidi_Control}]/gu, "");
|
|
21
|
+
if (stringWidth(safe) <= width)
|
|
22
|
+
return safe;
|
|
23
|
+
let result = "";
|
|
24
|
+
let used = 0;
|
|
25
|
+
for (const { segment } of segmenter.segment(safe)) {
|
|
26
|
+
used += stringWidth(segment);
|
|
27
|
+
if (used > width)
|
|
28
|
+
break;
|
|
29
|
+
result += segment;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
function sgr([flags, fg, bg]) {
|
|
34
|
+
const attributes = [0];
|
|
35
|
+
const styles = [1, 2, 3, 4, 7];
|
|
36
|
+
for (const [bit, code] of styles.entries()) {
|
|
37
|
+
if (flags & (1 << bit))
|
|
38
|
+
attributes.push(code);
|
|
39
|
+
}
|
|
40
|
+
for (const [color, code] of [
|
|
41
|
+
[fg, 38],
|
|
42
|
+
[bg, 48],
|
|
43
|
+
]) {
|
|
44
|
+
if (color >= 256) {
|
|
45
|
+
const rgb = color - 256;
|
|
46
|
+
attributes.push(code, 2, rgb >> 16, (rgb >> 8) & 255, rgb & 255);
|
|
47
|
+
}
|
|
48
|
+
else if (color >= 0)
|
|
49
|
+
attributes.push(code, 5, color);
|
|
50
|
+
}
|
|
51
|
+
return `\x1b[${attributes.join(";")}m`;
|
|
52
|
+
}
|
|
53
|
+
function renderHeader(header, width) {
|
|
54
|
+
if (typeof header === "string")
|
|
55
|
+
return chrome + fit(header, width) + reset;
|
|
56
|
+
const logo = fit(` rucan v${version} `, width);
|
|
57
|
+
const remaining = width - stringWidth(logo);
|
|
58
|
+
if (remaining < 3)
|
|
59
|
+
return brand + logo + reset;
|
|
60
|
+
const tab = fit(` ${header.tab} `, Math.min(remaining - 1, Math.max(12, Math.floor(width * 0.45))));
|
|
61
|
+
const status = fit(` ● ${header.status}`, remaining - stringWidth(tab) - 1);
|
|
62
|
+
return (brand +
|
|
63
|
+
logo +
|
|
64
|
+
reset +
|
|
65
|
+
" " +
|
|
66
|
+
activeTab +
|
|
67
|
+
tab +
|
|
68
|
+
reset +
|
|
69
|
+
(header.healthy ? connected : attention) +
|
|
70
|
+
status +
|
|
71
|
+
reset);
|
|
72
|
+
}
|
|
73
|
+
/** Own every write to the outer terminal while the interactive UI is mounted. */
|
|
74
|
+
export class Screen {
|
|
75
|
+
#output;
|
|
76
|
+
#previous = [];
|
|
77
|
+
#paintedLayout;
|
|
78
|
+
#latest;
|
|
79
|
+
#blocked = false;
|
|
80
|
+
#keyModes = "";
|
|
81
|
+
#mouseMode = "";
|
|
82
|
+
constructor(output) {
|
|
83
|
+
this.#output = output;
|
|
84
|
+
}
|
|
85
|
+
enter() {
|
|
86
|
+
this.#output.write("\x1b[?1049h\x1b[2J\x1b[?2004h" + hideCursor);
|
|
87
|
+
}
|
|
88
|
+
draw(model) {
|
|
89
|
+
this.#latest = model;
|
|
90
|
+
if (!this.#blocked)
|
|
91
|
+
this.#flush();
|
|
92
|
+
}
|
|
93
|
+
#onDrain = () => {
|
|
94
|
+
this.#blocked = false;
|
|
95
|
+
this.#flush();
|
|
96
|
+
};
|
|
97
|
+
#flush() {
|
|
98
|
+
const model = this.#latest;
|
|
99
|
+
if (!model)
|
|
100
|
+
return;
|
|
101
|
+
const { layout, header, footer, sidebarLines, snapshot, showCursor, contentLines, } = model;
|
|
102
|
+
const lines = [renderHeader(header, layout.cols)];
|
|
103
|
+
for (let row = 0; row < layout.rows - 2; row++) {
|
|
104
|
+
const sidebar = fit(sidebarLines[row] ?? "", Math.max(0, layout.sidebar - 1));
|
|
105
|
+
let line = layout.sidebar > 0
|
|
106
|
+
? (row === 0 || sidebar.startsWith("›")
|
|
107
|
+
? chrome + sidebar + reset
|
|
108
|
+
: sidebar) +
|
|
109
|
+
" ".repeat(Math.max(0, layout.sidebar - 1 - stringWidth(sidebar))) +
|
|
110
|
+
divider +
|
|
111
|
+
"│" +
|
|
112
|
+
reset
|
|
113
|
+
: "";
|
|
114
|
+
let col = layout.sidebar;
|
|
115
|
+
if (contentLines && layout.cols - col > 2) {
|
|
116
|
+
const content = fit(contentLines[row] ?? "", layout.cols - col - 2);
|
|
117
|
+
if (content)
|
|
118
|
+
line += " " + (row === 0 ? chrome : reset) + content + reset;
|
|
119
|
+
}
|
|
120
|
+
for (const [width, text, style] of snapshot?.grid.lines[row] ?? []) {
|
|
121
|
+
if (col >= layout.cols)
|
|
122
|
+
break;
|
|
123
|
+
line +=
|
|
124
|
+
move(row + 2, col + 1) +
|
|
125
|
+
sgr(style) +
|
|
126
|
+
fit(text, Math.min(width, layout.cols - col));
|
|
127
|
+
col += width;
|
|
128
|
+
}
|
|
129
|
+
lines.push(line + reset);
|
|
130
|
+
}
|
|
131
|
+
lines.push(chrome + fit(footer, layout.cols) + reset);
|
|
132
|
+
let output = hideCursor;
|
|
133
|
+
if (this.#paintedLayout &&
|
|
134
|
+
(this.#paintedLayout.rows !== layout.rows ||
|
|
135
|
+
this.#paintedLayout.cols !== layout.cols)) {
|
|
136
|
+
// The outer terminal may have reflowed rows since the last frame.
|
|
137
|
+
output += reset + "\x1b[2J";
|
|
138
|
+
this.#previous = [];
|
|
139
|
+
}
|
|
140
|
+
this.#paintedLayout = layout;
|
|
141
|
+
const last = Math.max(lines.length, this.#previous.length);
|
|
142
|
+
for (let index = 0; index < last; index++) {
|
|
143
|
+
const line = lines[index] ?? "";
|
|
144
|
+
if (line !== this.#previous[index])
|
|
145
|
+
output += move(index + 1, 1) + reset + "\x1b[2K" + line;
|
|
146
|
+
}
|
|
147
|
+
const grid = showCursor ? snapshot?.grid : undefined;
|
|
148
|
+
const modes = (grid?.applicationCursor ? "\x1b[?1h" : "\x1b[?1l") +
|
|
149
|
+
(grid?.applicationKeypad ? "\x1b=" : "\x1b>");
|
|
150
|
+
if (modes !== this.#keyModes) {
|
|
151
|
+
output += modes;
|
|
152
|
+
this.#keyModes = modes;
|
|
153
|
+
}
|
|
154
|
+
const mouse = grid?.mouse === "any-motion"
|
|
155
|
+
? "1003"
|
|
156
|
+
: grid?.mouse === "button-motion"
|
|
157
|
+
? "1002"
|
|
158
|
+
: "1000";
|
|
159
|
+
if (mouse !== this.#mouseMode) {
|
|
160
|
+
output +=
|
|
161
|
+
"\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006h" + `\x1b[?${mouse}h`;
|
|
162
|
+
this.#mouseMode = mouse;
|
|
163
|
+
}
|
|
164
|
+
this.#previous = lines;
|
|
165
|
+
if (snapshot &&
|
|
166
|
+
showCursor &&
|
|
167
|
+
!snapshot.grid.cursorHidden &&
|
|
168
|
+
snapshot.cursor.row < layout.terminalRows) {
|
|
169
|
+
output +=
|
|
170
|
+
move(snapshot.cursor.row + 2, Math.min(layout.cols, layout.sidebar + snapshot.cursor.column + 1)) + "\x1b[?25h";
|
|
171
|
+
}
|
|
172
|
+
this.#blocked = !this.#output.write(output);
|
|
173
|
+
if (this.#blocked)
|
|
174
|
+
this.#output.once("drain", this.#onDrain);
|
|
175
|
+
}
|
|
176
|
+
leave() {
|
|
177
|
+
this.#latest = undefined;
|
|
178
|
+
this.#output.off("drain", this.#onDrain);
|
|
179
|
+
this.#output.write(reset +
|
|
180
|
+
"\x1b[?1l\x1b>\x1b[?2004l\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
function record(value) {
|
|
2
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
3
|
+
throw new Error("Invalid terminal snapshot object");
|
|
4
|
+
return { ...value };
|
|
5
|
+
}
|
|
6
|
+
function integer(value, low, high) {
|
|
7
|
+
if (typeof value !== "number" ||
|
|
8
|
+
!Number.isSafeInteger(value) ||
|
|
9
|
+
value < low ||
|
|
10
|
+
value > high)
|
|
11
|
+
throw new Error("Invalid terminal snapshot integer");
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function flag(value) {
|
|
15
|
+
if (typeof value !== "boolean")
|
|
16
|
+
throw new Error("Invalid terminal mode flag");
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function choice(value, accepted) {
|
|
20
|
+
const item = accepted.find((item) => item === value);
|
|
21
|
+
if (!item)
|
|
22
|
+
throw new Error("Invalid terminal snapshot value");
|
|
23
|
+
return item;
|
|
24
|
+
}
|
|
25
|
+
function decodeSpan(value) {
|
|
26
|
+
if (!Array.isArray(value) || value.length !== 3)
|
|
27
|
+
throw new Error("Invalid terminal span");
|
|
28
|
+
const [width, text, style] = value;
|
|
29
|
+
if (typeof text !== "string" ||
|
|
30
|
+
/\p{Cc}/u.test(text) ||
|
|
31
|
+
!Array.isArray(style) ||
|
|
32
|
+
style.length !== 3) {
|
|
33
|
+
throw new Error("Invalid terminal span text or style");
|
|
34
|
+
}
|
|
35
|
+
const [flags, foreground, background] = style;
|
|
36
|
+
return [
|
|
37
|
+
integer(width, 1, 500),
|
|
38
|
+
text,
|
|
39
|
+
[
|
|
40
|
+
integer(flags, 0, 31),
|
|
41
|
+
integer(foreground, -1, 0x10000ff),
|
|
42
|
+
integer(background, -1, 0x10000ff),
|
|
43
|
+
],
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
export function decodeSnapshot(data, previous) {
|
|
47
|
+
const value = record(data);
|
|
48
|
+
const grid = record(value.grid);
|
|
49
|
+
const cursor = record(value.cursor);
|
|
50
|
+
const state = record(value.state);
|
|
51
|
+
const rows = integer(grid.rows, 1, 200);
|
|
52
|
+
const cols = integer(grid.cols, 1, 500);
|
|
53
|
+
if (rows * cols > 12000)
|
|
54
|
+
throw new Error("Invalid terminal grid size");
|
|
55
|
+
const decodeLine = (entry) => {
|
|
56
|
+
if (!Array.isArray(entry) || entry.length > cols)
|
|
57
|
+
throw new Error("Invalid terminal row");
|
|
58
|
+
const line = entry.map(decodeSpan);
|
|
59
|
+
if (line.reduce((width, [span]) => width + span, 0) > cols)
|
|
60
|
+
throw new Error("Terminal row exceeds grid");
|
|
61
|
+
return line;
|
|
62
|
+
};
|
|
63
|
+
let lines;
|
|
64
|
+
if (Array.isArray(grid.lines) && grid.lines.length === rows) {
|
|
65
|
+
lines = grid.lines.map(decodeLine);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
if (!previous ||
|
|
69
|
+
value.baseSequence !== previous.sequence ||
|
|
70
|
+
previous.grid.rows !== rows ||
|
|
71
|
+
previous.grid.cols !== cols ||
|
|
72
|
+
!Array.isArray(grid.patch) ||
|
|
73
|
+
grid.patch.length > rows) {
|
|
74
|
+
throw new Error("Terminal update requires a matching snapshot");
|
|
75
|
+
}
|
|
76
|
+
lines = [...previous.grid.lines];
|
|
77
|
+
const seen = new Set();
|
|
78
|
+
for (const entry of grid.patch) {
|
|
79
|
+
if (!Array.isArray(entry) || entry.length !== 2)
|
|
80
|
+
throw new Error("Invalid terminal row update");
|
|
81
|
+
const [index, data] = entry;
|
|
82
|
+
const row = integer(index, 0, rows - 1);
|
|
83
|
+
if (seen.has(row))
|
|
84
|
+
throw new Error("Duplicate terminal row update");
|
|
85
|
+
seen.add(row);
|
|
86
|
+
lines[row] = decodeLine(data);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
sequence: integer(value.sequence, 0, Number.MAX_SAFE_INTEGER),
|
|
91
|
+
state: choice(state.type, ["running", "exited", "wait-failed"]),
|
|
92
|
+
cursor: {
|
|
93
|
+
row: integer(cursor.row, 0, rows - 1),
|
|
94
|
+
column: integer(cursor.column, 0, cols),
|
|
95
|
+
},
|
|
96
|
+
grid: {
|
|
97
|
+
rows,
|
|
98
|
+
cols,
|
|
99
|
+
lines,
|
|
100
|
+
cursorHidden: flag(grid.cursorHidden),
|
|
101
|
+
applicationCursor: flag(grid.applicationCursor),
|
|
102
|
+
applicationKeypad: flag(grid.applicationKeypad),
|
|
103
|
+
bracketedPaste: flag(grid.bracketedPaste),
|
|
104
|
+
mouse: choice(grid.mouse, [
|
|
105
|
+
"none",
|
|
106
|
+
"press",
|
|
107
|
+
"press-release",
|
|
108
|
+
"button-motion",
|
|
109
|
+
"any-motion",
|
|
110
|
+
]),
|
|
111
|
+
mouseEncoding: choice(grid.mouseEncoding, ["default", "utf8", "sgr"]),
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { decodeWorkspace } from "./workspaces.js";
|
|
2
|
+
function decodeTarget(value) {
|
|
3
|
+
if (typeof value !== "object" || value === null)
|
|
4
|
+
throw new Error("Invalid tmux target");
|
|
5
|
+
const target = { ...value };
|
|
6
|
+
if (!("id" in target) ||
|
|
7
|
+
typeof target.id !== "string" ||
|
|
8
|
+
!/^\$\d{1,16}$/u.test(target.id) ||
|
|
9
|
+
!("name" in target) ||
|
|
10
|
+
typeof target.name !== "string" ||
|
|
11
|
+
!("cwd" in target) ||
|
|
12
|
+
typeof target.cwd !== "string") {
|
|
13
|
+
throw new Error("Invalid tmux target");
|
|
14
|
+
}
|
|
15
|
+
return { id: target.id, name: target.name, cwd: target.cwd };
|
|
16
|
+
}
|
|
17
|
+
export async function listTmux(client) {
|
|
18
|
+
const raw = await client.request({ type: "tmux-list" });
|
|
19
|
+
if (typeof raw !== "object" ||
|
|
20
|
+
raw === null ||
|
|
21
|
+
!("available" in raw) ||
|
|
22
|
+
typeof raw.available !== "boolean" ||
|
|
23
|
+
!("sessions" in raw) ||
|
|
24
|
+
!Array.isArray(raw.sessions))
|
|
25
|
+
throw new Error("Invalid tmux inventory");
|
|
26
|
+
return { available: raw.available, sessions: raw.sessions.map(decodeTarget) };
|
|
27
|
+
}
|
|
28
|
+
export async function openTmux(client, layout, cwd, target) {
|
|
29
|
+
const name = "rucan-" + Date.now().toString(36);
|
|
30
|
+
return decodeWorkspace(await client.request({
|
|
31
|
+
type: "tmux-open",
|
|
32
|
+
target,
|
|
33
|
+
name,
|
|
34
|
+
cwd,
|
|
35
|
+
rows: layout.terminalRows,
|
|
36
|
+
cols: layout.terminalCols,
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function footerFor(mode, prefix, name, scrollback) {
|
|
2
|
+
switch (mode) {
|
|
3
|
+
case "create":
|
|
4
|
+
return "↑↓ Choose workspace · Enter Open · Esc Back";
|
|
5
|
+
case "hosts":
|
|
6
|
+
return "↑↓ Select host · Enter Connect · Esc Back";
|
|
7
|
+
case "rename":
|
|
8
|
+
return `Rename workspace: ${name} · Enter Save · Esc Cancel`;
|
|
9
|
+
case "history":
|
|
10
|
+
return `History · ${scrollback} lines back · ↑↓ Scroll · Esc Back`;
|
|
11
|
+
case "update":
|
|
12
|
+
return "1 Update now · 2 Continue · 3 Skip this version";
|
|
13
|
+
case "tmux":
|
|
14
|
+
return "↑↓ Select · Enter Attach · N Open New Tmux Session · Esc Back";
|
|
15
|
+
case "explorer":
|
|
16
|
+
return "↑↓ Select · Enter Expand/Preview · R Refresh · O Editor · Esc Back";
|
|
17
|
+
case "command":
|
|
18
|
+
return "Enter Run command · Esc Back";
|
|
19
|
+
case "exit":
|
|
20
|
+
return "Enter Keep all · Esc Back";
|
|
21
|
+
case "menu":
|
|
22
|
+
return "Q Quit · Esc Return to terminal · ↑↓ / 1–9 Switch";
|
|
23
|
+
case "terminal":
|
|
24
|
+
return `${prefix} Menu · Q or /exit to quit`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function menuFor(update) {
|
|
28
|
+
return [
|
|
29
|
+
"Workspace actions",
|
|
30
|
+
"",
|
|
31
|
+
"/exit or Q Quit Rucan",
|
|
32
|
+
"D Close UI and keep sessions",
|
|
33
|
+
"",
|
|
34
|
+
"N New workspace",
|
|
35
|
+
"P Pin",
|
|
36
|
+
"H Hosts",
|
|
37
|
+
"E Files",
|
|
38
|
+
"T Tmux",
|
|
39
|
+
"S History",
|
|
40
|
+
"R Rename",
|
|
41
|
+
...(update ? [`U Update to v${update}`] : []),
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
export function selectionLines(title, entries, index, available) {
|
|
45
|
+
const start = Math.max(0, index - Math.max(0, available - 2));
|
|
46
|
+
return [
|
|
47
|
+
title,
|
|
48
|
+
...entries
|
|
49
|
+
.slice(start)
|
|
50
|
+
.map((entry, offset) => `${index === start + offset ? "›" : " "} ${entry}`),
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
export function exitLines(host) {
|
|
54
|
+
return [
|
|
55
|
+
"Quit Rucan",
|
|
56
|
+
"",
|
|
57
|
+
"Enter / 1 Close UI and keep all sessions",
|
|
58
|
+
"2 Keep pinned sessions only",
|
|
59
|
+
"3 Stop all Rucan-managed sessions",
|
|
60
|
+
"Esc / 4 Go back",
|
|
61
|
+
"",
|
|
62
|
+
`Session options apply to ${host ?? "this computer"}.`,
|
|
63
|
+
];
|
|
64
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { setTimeout as delay } from "node:timers/promises";
|
|
2
|
+
import { decodeSnapshot } from "./snapshot.js";
|
|
3
|
+
export class TerminalWatch {
|
|
4
|
+
#generation = 0;
|
|
5
|
+
#connection;
|
|
6
|
+
#connect;
|
|
7
|
+
constructor(connect) {
|
|
8
|
+
this.#connect = connect;
|
|
9
|
+
}
|
|
10
|
+
stop() {
|
|
11
|
+
this.#generation++;
|
|
12
|
+
this.#connection?.close();
|
|
13
|
+
this.#connection = undefined;
|
|
14
|
+
}
|
|
15
|
+
start(id, initial, onSnapshot, onError) {
|
|
16
|
+
this.stop();
|
|
17
|
+
const generation = this.#generation;
|
|
18
|
+
const active = () => this.#generation === generation;
|
|
19
|
+
void (async () => {
|
|
20
|
+
let snapshot = initial;
|
|
21
|
+
for (const wait of [0, 150, 350, 900]) {
|
|
22
|
+
if (wait)
|
|
23
|
+
await delay(wait);
|
|
24
|
+
if (!active())
|
|
25
|
+
return;
|
|
26
|
+
let connection;
|
|
27
|
+
try {
|
|
28
|
+
connection = await this.#connect();
|
|
29
|
+
if (!active())
|
|
30
|
+
return;
|
|
31
|
+
this.#connection = connection;
|
|
32
|
+
if (wait) {
|
|
33
|
+
const full = await connection.request({
|
|
34
|
+
type: "snapshot",
|
|
35
|
+
session: id,
|
|
36
|
+
});
|
|
37
|
+
if (!active())
|
|
38
|
+
return;
|
|
39
|
+
snapshot = decodeSnapshot(full);
|
|
40
|
+
onSnapshot(snapshot);
|
|
41
|
+
}
|
|
42
|
+
while (active()) {
|
|
43
|
+
const raw = await connection.request({
|
|
44
|
+
type: "watch",
|
|
45
|
+
session: id,
|
|
46
|
+
after: snapshot.sequence,
|
|
47
|
+
});
|
|
48
|
+
if (raw === null || !active())
|
|
49
|
+
continue;
|
|
50
|
+
snapshot = decodeSnapshot(raw, snapshot);
|
|
51
|
+
onSnapshot(snapshot);
|
|
52
|
+
}
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (!active())
|
|
57
|
+
return;
|
|
58
|
+
onError(error);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
connection?.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
})();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export function decodeWorkspace(raw) {
|
|
2
|
+
if (typeof raw !== "object" || raw === null)
|
|
3
|
+
throw new Error("Invalid agent workspace");
|
|
4
|
+
const workspace = { ...raw };
|
|
5
|
+
if (!("name" in workspace) ||
|
|
6
|
+
typeof workspace.name !== "string" ||
|
|
7
|
+
!("backend" in workspace) ||
|
|
8
|
+
(workspace.backend !== "native" && workspace.backend !== "tmux") ||
|
|
9
|
+
!("ownership" in workspace) ||
|
|
10
|
+
(workspace.ownership !== "managed" && workspace.ownership !== "external") ||
|
|
11
|
+
!("cwd" in workspace) ||
|
|
12
|
+
typeof workspace.cwd !== "string" ||
|
|
13
|
+
!("id" in workspace) ||
|
|
14
|
+
typeof workspace.id !== "string" ||
|
|
15
|
+
!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/u.test(workspace.id) ||
|
|
16
|
+
!("persistent" in workspace) ||
|
|
17
|
+
typeof workspace.persistent !== "boolean" ||
|
|
18
|
+
!("state" in workspace) ||
|
|
19
|
+
typeof workspace.state !== "object" ||
|
|
20
|
+
workspace.state === null ||
|
|
21
|
+
!("type" in workspace.state) ||
|
|
22
|
+
!(workspace.state.type === "running" ||
|
|
23
|
+
workspace.state.type === "exited" ||
|
|
24
|
+
workspace.state.type === "wait-failed"))
|
|
25
|
+
throw new Error("Invalid agent workspace");
|
|
26
|
+
return {
|
|
27
|
+
name: workspace.name,
|
|
28
|
+
backend: workspace.backend,
|
|
29
|
+
ownership: workspace.ownership,
|
|
30
|
+
id: workspace.id,
|
|
31
|
+
cwd: workspace.cwd,
|
|
32
|
+
persistent: workspace.persistent,
|
|
33
|
+
running: workspace.state.type === "running",
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
export async function listWorkspaces(client) {
|
|
37
|
+
const raw = await client.request({ type: "list" });
|
|
38
|
+
if (!Array.isArray(raw))
|
|
39
|
+
throw new Error("Invalid agent workspace list");
|
|
40
|
+
return raw
|
|
41
|
+
.map(decodeWorkspace)
|
|
42
|
+
.sort((left, right) => left.name.localeCompare(right.name));
|
|
43
|
+
}
|
|
44
|
+
function defaultWorkspace(config, host) {
|
|
45
|
+
return (config.workspaces.find(({ host: entry, backend }) => entry === host && backend === "native") ?? {
|
|
46
|
+
name: host ?? "local-shell",
|
|
47
|
+
cwd: "~",
|
|
48
|
+
command: (host ? undefined : process.env.SHELL) ?? "/bin/sh",
|
|
49
|
+
backend: "native",
|
|
50
|
+
persistence: config.defaults.persistence,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
export async function createWorkspace(client, config, layout, host, requested) {
|
|
54
|
+
const workspace = requested ?? defaultWorkspace(config, host);
|
|
55
|
+
return decodeWorkspace(await client.request({
|
|
56
|
+
type: "create",
|
|
57
|
+
name: workspace.name,
|
|
58
|
+
cwd: workspace.cwd,
|
|
59
|
+
command: workspace.command,
|
|
60
|
+
args: [],
|
|
61
|
+
rows: layout.terminalRows,
|
|
62
|
+
cols: layout.terminalCols,
|
|
63
|
+
persistent: workspace.persistence === "persistent",
|
|
64
|
+
scrollback: config.terminal.scrollback_lines,
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
export function workspaceLabels(config, sessions, activeId) {
|
|
68
|
+
const labels = [
|
|
69
|
+
"WORKSPACES",
|
|
70
|
+
...sessions.map((workspace, index) => {
|
|
71
|
+
const selected = workspace.id === activeId ? "›" : " ";
|
|
72
|
+
const pin = workspace.backend === "tmux" ? "T" : workspace.persistent ? "◆" : "◇";
|
|
73
|
+
const slot = index < 9 ? String(index + 1) : " ";
|
|
74
|
+
const exited = workspace.running ? "" : " ×";
|
|
75
|
+
return `${selected}${slot} ${pin} ${workspace.name}${exited}`;
|
|
76
|
+
}),
|
|
77
|
+
];
|
|
78
|
+
for (const workspace of config.workspaces) {
|
|
79
|
+
if (workspace.host)
|
|
80
|
+
labels.push(` ○ ${workspace.name} (SSH)`);
|
|
81
|
+
else if (workspace.backend === "tmux")
|
|
82
|
+
labels.push(` ○ ${workspace.name} (tmux)`);
|
|
83
|
+
}
|
|
84
|
+
return labels;
|
|
85
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { Box, Text, useApp, useInput, useWindowSize } from "ink";
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { version } from "../release/version.js";
|
|
5
|
+
const fixtureExplorer = ["▾ example-project", " ▸ src", " README.md"];
|
|
6
|
+
export function WorkspaceView({ config }) {
|
|
7
|
+
const { exit } = useApp();
|
|
8
|
+
const { columns, rows } = useWindowSize();
|
|
9
|
+
const [selected, setSelected] = useState(0);
|
|
10
|
+
const [menu, setMenu] = useState(false);
|
|
11
|
+
const workspace = config.workspaces[selected];
|
|
12
|
+
const prefix = config.terminal.prefix_key;
|
|
13
|
+
useInput((input, key) => {
|
|
14
|
+
if (key.ctrl && input === prefix.slice(-1)) {
|
|
15
|
+
setMenu((open) => !open);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (key.escape && menu) {
|
|
19
|
+
setMenu(false);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!key.ctrl && !key.meta && input === "q") {
|
|
23
|
+
exit();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if (menu || config.workspaces.length === 0)
|
|
27
|
+
return;
|
|
28
|
+
if (key.upArrow || key.downArrow) {
|
|
29
|
+
const step = key.upArrow ? -1 : 1;
|
|
30
|
+
setSelected((index) => Math.max(0, Math.min(index + step, config.workspaces.length - 1)));
|
|
31
|
+
}
|
|
32
|
+
else if (!key.ctrl &&
|
|
33
|
+
!key.meta &&
|
|
34
|
+
/^[1-9]$/.test(input) &&
|
|
35
|
+
Number(input) <= config.workspaces.length) {
|
|
36
|
+
setSelected(Number(input) - 1);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
if (columns < 48 || rows < 12) {
|
|
40
|
+
return _jsx(Text, { children: "Resize to at least 48\u00D712. Q quits." });
|
|
41
|
+
}
|
|
42
|
+
const maxVisible = Math.max(1, rows - 11);
|
|
43
|
+
const start = Math.max(0, selected - maxVisible + 1);
|
|
44
|
+
const sidebarWidth = Math.max(16, Math.floor(columns * 0.25));
|
|
45
|
+
return (_jsxs(Box, { flexDirection: "column", width: columns, height: rows, children: [_jsxs(Box, { justifyContent: "space-between", paddingX: 1, children: [_jsxs(Text, { bold: true, color: "cyan", children: ["rucan v", version] }), _jsx(Text, { color: "yellow", children: "Fixture mode \u00B7 agent offline" })] }), _jsxs(Box, { flexGrow: 1, flexShrink: 1, children: [_jsxs(Box, { width: sidebarWidth, flexShrink: 0, flexDirection: "column", children: [_jsxs(Box, { borderStyle: "single", flexDirection: "column", flexGrow: 1, children: [_jsx(Text, { bold: true, children: "WORKSPACES" }), config.workspaces.length === 0 && (_jsx(Text, { dimColor: true, children: "No workspaces" })), config.workspaces
|
|
46
|
+
.slice(start, start + maxVisible)
|
|
47
|
+
.map((entry, index) => (_jsxs(Text, { wrap: "truncate-end", ...(start + index === selected ? { color: "cyan" } : {}), children: [start + index === selected ? "›" : " ", " ", entry.name, entry.persistence === "persistent" ? " ◆" : ""] }, entry.name)))] }), _jsxs(Box, { borderStyle: "single", flexDirection: "column", children: [_jsx(Text, { bold: true, children: "EXPLORER" }), fixtureExplorer.map((item) => (_jsx(Text, { wrap: "truncate-end", dimColor: true, children: item }, item)))] })] }), _jsxs(Box, { flexGrow: 1, flexShrink: 1, flexBasis: 0, borderStyle: "single", flexDirection: "column", paddingX: 1, children: [_jsx(Text, { bold: true, children: menu ? "MENU" : "TERMINAL" }), menu ? (_jsx(Text, { children: "Esc closes the menu. Q quits." })) : (_jsxs(_Fragment, { children: [workspace && (_jsxs(Text, { wrap: "truncate-end", children: [workspace.host ?? "local", " \u00B7 ", workspace.backend, " \u00B7", " ", workspace.persistence] })), workspace && (_jsxs(Text, { wrap: "truncate-end", color: "green", children: [workspace.cwd, " $ ", workspace.command] })), _jsx(Text, { wrap: "truncate-end", dimColor: true, children: "Fixture preview. No shell is running." })] }))] })] }), _jsxs(Text, { wrap: "truncate-end", dimColor: true, children: [prefix, " Menu \u00B7 \u2191\u2193 / 1\u20139 Switch \u00B7 Q Quit"] })] }));
|
|
48
|
+
}
|