elythia-cli 0.0.0-stage → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -3
- package/bin/elythia.js +320 -0
- package/package.json +32 -4
- package/src/agent.js +106 -0
- package/src/api.js +47 -0
- package/src/art.js +247 -0
- package/src/config.js +41 -0
- package/src/defaults.js +1 -0
- package/src/home.js +82 -0
- package/src/login.js +115 -0
- package/src/miner.js +448 -0
- package/src/ui.js +191 -0
package/src/art.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Procedural terminal art for Elythia: the wordmark, the temple over the Aegean on the start
|
|
2
|
+
// screen, and the altar flame that burns while the agent consults the Oracle. Everything is
|
|
3
|
+
// drawn into a grid of colored cells.
|
|
4
|
+
|
|
5
|
+
const colorEnabled = process.stdout.isTTY && !process.env.NO_COLOR;
|
|
6
|
+
|
|
7
|
+
export class Grid {
|
|
8
|
+
constructor(width, height) {
|
|
9
|
+
this.width = width;
|
|
10
|
+
this.height = height;
|
|
11
|
+
this.chars = Array.from({ length: height }, () => Array(width).fill(' '));
|
|
12
|
+
this.colors = Array.from({ length: height }, () => Array(width).fill(null));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
set(x, y, char, color = null) {
|
|
16
|
+
x = Math.round(x);
|
|
17
|
+
y = Math.round(y);
|
|
18
|
+
if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
|
|
19
|
+
this.chars[y][x] = char;
|
|
20
|
+
this.colors[y][x] = color;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get(x, y) {
|
|
24
|
+
return this.chars[y]?.[x];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
text(x, y, string, color = null) {
|
|
28
|
+
[...string].forEach((char, i) => this.set(x + i, y, char, color));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Joins runs of the same color so a frame stays a few hundred bytes.
|
|
32
|
+
lines() {
|
|
33
|
+
return this.chars.map((row, y) => {
|
|
34
|
+
let out = '';
|
|
35
|
+
let current = null;
|
|
36
|
+
row.forEach((char, x) => {
|
|
37
|
+
const color = colorEnabled ? this.colors[y][x] : null;
|
|
38
|
+
if (color !== current) {
|
|
39
|
+
if (current !== null) out += '\x1b[39m';
|
|
40
|
+
if (color !== null) out += `\x1b[38;5;${color}m`;
|
|
41
|
+
current = color;
|
|
42
|
+
}
|
|
43
|
+
out += char;
|
|
44
|
+
});
|
|
45
|
+
return current !== null ? `${out}\x1b[39m` : out;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const hash2 = (x, y, seed = 0) => {
|
|
51
|
+
let h = (x * 374761393 + y * 668265263 + seed * 2147483647) >>> 0;
|
|
52
|
+
h = Math.imul(h ^ (h >>> 13), 1274126177);
|
|
53
|
+
return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// ---------- Wordmark ----------
|
|
57
|
+
|
|
58
|
+
export const WORDMARK = [
|
|
59
|
+
'█▀▀ █ █▄█ ▀█▀ █ █ █ ▄▀█',
|
|
60
|
+
'██▄ █▄▄ █ █ █▀█ █ █▀█',
|
|
61
|
+
];
|
|
62
|
+
export const WORDMARK_WIDTH = WORDMARK[0].length;
|
|
63
|
+
|
|
64
|
+
// The wordmark in marble with a slow gold glint sweeping across it.
|
|
65
|
+
export function wordmarkLines(time = 0) {
|
|
66
|
+
const glint = ((time / 70) % (WORDMARK_WIDTH + 30)) - 10;
|
|
67
|
+
return WORDMARK.map((line) => [...line].map((char, x) => {
|
|
68
|
+
if (char === ' ' || !colorEnabled) return char;
|
|
69
|
+
const distance = Math.abs(x - glint);
|
|
70
|
+
const color = distance < 1.5 ? 231 : distance < 3.5 ? 194 : distance < 6 ? 152 : 255;
|
|
71
|
+
return `\x1b[38;5;${color}m${char}`;
|
|
72
|
+
}).join('') + (colorEnabled ? '\x1b[39m' : ''));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ---------- The temple over the sea (start screen) ----------
|
|
76
|
+
|
|
77
|
+
export const SCENE_WIDTH = 46;
|
|
78
|
+
export const SCENE_HEIGHT = 15;
|
|
79
|
+
|
|
80
|
+
const MARBLE = [242, 244, 246, 248, 250, 252, 254];
|
|
81
|
+
const SEA = [17, 23, 24, 60, 67, 73];
|
|
82
|
+
|
|
83
|
+
function marble(grid, x, y, light, char = '█') {
|
|
84
|
+
const grain = hash2(x, y, 11);
|
|
85
|
+
const level = Math.max(0, Math.min(MARBLE.length - 1, Math.round(light * (MARBLE.length - 1) + (grain - 0.5) * 1.2)));
|
|
86
|
+
grid.set(x, y, char, MARBLE[level]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// A temple with six columns and a pediment, the moon over it, a flame in the doorway and the
|
|
90
|
+
// sea rolling below. `time` is in ms.
|
|
91
|
+
export function templeFrame(time) {
|
|
92
|
+
const grid = new Grid(SCENE_WIDTH, SCENE_HEIGHT);
|
|
93
|
+
const sea = SCENE_HEIGHT - 3;
|
|
94
|
+
|
|
95
|
+
// Night sky.
|
|
96
|
+
for (let i = 0; i < 24; i += 1) {
|
|
97
|
+
const x = Math.floor(hash2(i, 1, 7) * SCENE_WIDTH);
|
|
98
|
+
const y = Math.floor(hash2(i, 2, 7) * (sea - 2));
|
|
99
|
+
const twinkle = (Math.sin(time / 600 + i * 1.7) + 1) / 2;
|
|
100
|
+
grid.set(x, y, twinkle > 0.82 ? '✦' : '·', twinkle > 0.5 ? 252 : 240);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The moon, upper right.
|
|
104
|
+
const mx = 40;
|
|
105
|
+
const my = 1.6;
|
|
106
|
+
for (let y = 0; y < 5; y += 1) {
|
|
107
|
+
for (let x = 33; x < SCENE_WIDTH; x += 1) {
|
|
108
|
+
const d = Math.hypot((x - mx) / 2, y - my);
|
|
109
|
+
if (d < 1.6) grid.set(x, y, '█', d < 0.9 ? 231 : 194);
|
|
110
|
+
else if (d < 2.4) grid.set(x, y, '░', 152);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Pediment: a low triangle from x 8 to 37, apex on row 1.
|
|
115
|
+
const left = 8;
|
|
116
|
+
const right = 37;
|
|
117
|
+
const apex = 22.5;
|
|
118
|
+
for (let y = 1; y <= 3; y += 1) {
|
|
119
|
+
const half = ((y - 0.4) / 3) * ((right - left) / 2);
|
|
120
|
+
for (let x = Math.ceil(apex - half); x <= Math.floor(apex + half); x += 1) {
|
|
121
|
+
const edge = x === Math.ceil(apex - half) || x === Math.floor(apex + half);
|
|
122
|
+
marble(grid, x, y, 0.55 + (x - left) / (right - left) * 0.35, edge && y < 3 ? '▄' : '█');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// A gold disc in the tympanum, glowing with the flame.
|
|
126
|
+
const flicker = (Math.sin(time / 130) + Math.sin(time / 311)) / 4 + 0.5;
|
|
127
|
+
grid.set(22, 2, '◆', flicker > 0.5 ? 194 : 151);
|
|
128
|
+
grid.set(23, 2, '◆', flicker > 0.5 ? 194 : 151);
|
|
129
|
+
|
|
130
|
+
// Entablature with a meander frieze.
|
|
131
|
+
for (let x = left; x <= right; x += 1) {
|
|
132
|
+
marble(grid, x, 4, 0.8);
|
|
133
|
+
grid.set(x, 5, (x - left) % 4 === 0 ? '▀' : (x - left) % 4 === 2 ? '▀' : '▔', (x - left) % 4 < 2 ? 109 : 67);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Columns with a dark cella behind them, and the flame in the middle bay.
|
|
137
|
+
const columns = [10, 14, 18, 26, 30, 34];
|
|
138
|
+
for (let y = 6; y <= 9; y += 1) {
|
|
139
|
+
for (let x = left + 1; x <= right - 1; x += 1) grid.set(x, y, ' ', null);
|
|
140
|
+
}
|
|
141
|
+
for (const x of columns) {
|
|
142
|
+
for (let y = 6; y <= 9; y += 1) {
|
|
143
|
+
marble(grid, x, y, 0.9);
|
|
144
|
+
marble(grid, x + 1, y, 0.45);
|
|
145
|
+
}
|
|
146
|
+
grid.set(x - 1, 6, '▄', 248);
|
|
147
|
+
grid.set(x + 2, 6, '▄', 244);
|
|
148
|
+
}
|
|
149
|
+
const fh = 1.5 + flicker * 1.2;
|
|
150
|
+
for (let y = 9; y > 9 - fh; y -= 1) {
|
|
151
|
+
const row = Math.round(y);
|
|
152
|
+
const top = row <= 9 - fh + 1;
|
|
153
|
+
grid.set(22, row, top ? '▲' : '█', top ? 194 : 158);
|
|
154
|
+
grid.set(23, row, top ? '▲' : '█', top ? 194 : 151);
|
|
155
|
+
}
|
|
156
|
+
grid.set(21, 9, '▄', 67);
|
|
157
|
+
grid.set(24, 9, '▄', 67);
|
|
158
|
+
|
|
159
|
+
// Steps.
|
|
160
|
+
for (let x = left - 1; x <= right + 1; x += 1) marble(grid, x, 10, 0.75);
|
|
161
|
+
for (let x = left - 3; x <= right + 3; x += 1) marble(grid, x, 11, 0.5, '▀');
|
|
162
|
+
|
|
163
|
+
// The sea, rolling.
|
|
164
|
+
for (let y = sea; y < SCENE_HEIGHT; y += 1) {
|
|
165
|
+
for (let x = 0; x < SCENE_WIDTH; x += 1) {
|
|
166
|
+
const wave = Math.sin(x * 0.55 - time / 380 + y * 1.3) + Math.sin(x * 0.21 + time / 610);
|
|
167
|
+
const glitter = Math.abs(x - mx) < 3 + (y - sea) * 1.5 && hash2(x, y, Math.floor(time / 240)) < 0.35;
|
|
168
|
+
if (glitter) grid.set(x, y, '━', 194);
|
|
169
|
+
else grid.set(x, y, wave > 0.9 ? '≈' : wave > -0.2 ? '~' : '·', SEA[Math.max(0, Math.min(SEA.length - 1, Math.round((wave + 2) * 1.3)))]);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return grid.lines();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------- The altar (divining screen) ----------
|
|
176
|
+
|
|
177
|
+
export const ALTAR_WIDTH = 34;
|
|
178
|
+
export const ALTAR_HEIGHT = 14;
|
|
179
|
+
const CX = ALTAR_WIDTH / 2 - 0.5;
|
|
180
|
+
const FLAME_BASE = 7;
|
|
181
|
+
|
|
182
|
+
const FLAMES = {
|
|
183
|
+
consulting: [195, 152, 116, 73, 67, 60],
|
|
184
|
+
blessed: [231, 255, 194, 158, 151, 115],
|
|
185
|
+
failed: [244, 241, 239, 238, 237, 236],
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// state: 'idle' (altar dark), 'consulting' (teal flame while the agent thinks), 'blessed' (true
|
|
189
|
+
// answer: the flame turns gold and flares), 'failed' (false answer: it dies to embers).
|
|
190
|
+
// `progress` (0–1) is how much of the answer window has passed; the flame burns lower with it.
|
|
191
|
+
export function altarFrame({ progress = 0, state = 'consulting', time = 0, particles = [] }) {
|
|
192
|
+
const grid = new Grid(ALTAR_WIDTH, ALTAR_HEIGHT + 1);
|
|
193
|
+
|
|
194
|
+
// The flame.
|
|
195
|
+
if (state !== 'idle') {
|
|
196
|
+
const palette = FLAMES[state] ?? FLAMES.consulting;
|
|
197
|
+
const height = state === 'blessed' ? 7.5 : state === 'failed' ? 1.6 : 6.6 - progress * 2.4;
|
|
198
|
+
const width = state === 'blessed' ? 7.5 : state === 'failed' ? 3 : 6;
|
|
199
|
+
const frame = Math.floor(time / 90);
|
|
200
|
+
for (let y = FLAME_BASE; y >= 0; y -= 1) {
|
|
201
|
+
const t = (FLAME_BASE - y) / height; // 0 at the base, 1 at the tip
|
|
202
|
+
if (t > 1) continue;
|
|
203
|
+
const half = width * (1 - t) ** 0.75 * (0.82 + 0.18 * Math.sin(Math.PI * Math.min(1, t * 2.2)));
|
|
204
|
+
const sway = Math.sin(time / 170 + y * 0.8) * 1.2 * t;
|
|
205
|
+
for (let x = 0; x < ALTAR_WIDTH; x += 1) {
|
|
206
|
+
const dx = (x - CX - sway) / Math.max(0.6, half);
|
|
207
|
+
const d = Math.abs(dx);
|
|
208
|
+
const noise = hash2(x, y, frame);
|
|
209
|
+
if (d > 1 + (noise - 0.5) * 0.5) continue;
|
|
210
|
+
const heat = (1 - d) * 0.65 + (1 - t) * 0.35 + (noise - 0.5) * 0.2;
|
|
211
|
+
const tone = palette[Math.max(0, Math.min(palette.length - 1, Math.round((1 - heat) * (palette.length - 1))))];
|
|
212
|
+
const char = state === 'failed' ? (noise < 0.5 ? '·' : '˙') : t > 0.82 ? (noise < 0.5 ? '▴' : '\'') : heat > 0.62 ? '█' : heat > 0.42 ? '▓' : heat > 0.25 ? '▒' : '░';
|
|
213
|
+
grid.set(x, y, char, tone);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
for (let x = Math.round(CX) - 3; x <= Math.round(CX) + 4; x += 1) {
|
|
218
|
+
if (hash2(x, 3, Math.floor(time / 700)) < 0.35) grid.set(x, FLAME_BASE, '·', 238);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// The bronze bowl: a lip, a body with a meander band, and the narrow foot.
|
|
223
|
+
for (let x = 5; x <= 28; x += 1) grid.set(x, 8, '▄', x < CX ? 152 : 109);
|
|
224
|
+
for (let x = 6; x <= 27; x += 1) {
|
|
225
|
+
const band = (x - 6) % 4;
|
|
226
|
+
grid.set(x, 9, band === 0 ? '▓' : '█', band === 0 ? 60 : x < CX ? 109 : 67);
|
|
227
|
+
}
|
|
228
|
+
for (let x = 9; x <= 24; x += 1) grid.set(x, 10, '▀', x < CX ? 67 : 60);
|
|
229
|
+
|
|
230
|
+
// Tripod legs.
|
|
231
|
+
for (let i = 0; i < 3; i += 1) {
|
|
232
|
+
grid.set(11 - i * 1.5, 11 + i, '╱', 67);
|
|
233
|
+
grid.set(CX + 0.5, 11 + i, '┃', 67);
|
|
234
|
+
grid.set(22 + i * 1.5, 11 + i, '╲', 60);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
for (const particle of particles) grid.set(particle.x, particle.y, particle.char, particle.color);
|
|
238
|
+
|
|
239
|
+
// The marble floor.
|
|
240
|
+
for (let x = 0; x < ALTAR_WIDTH; x += 1) grid.set(x, ALTAR_HEIGHT, hash2(x, 99, 3) < 0.2 ? '▂' : '▁', 244);
|
|
241
|
+
return grid.lines();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Where sparks rise from: the top of the flame.
|
|
245
|
+
export function sparkOrigin() {
|
|
246
|
+
return { x: CX, y: FLAME_BASE - 4 };
|
|
247
|
+
}
|
package/src/config.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { chmodSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { DEFAULT_SERVER_URL } from './defaults.js';
|
|
5
|
+
|
|
6
|
+
export const DEFAULT_SERVER = process.env.ELYTHIA_SERVER ?? DEFAULT_SERVER_URL;
|
|
7
|
+
|
|
8
|
+
const CONFIG_DIR = path.join(os.homedir(), '.elythia');
|
|
9
|
+
export const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
10
|
+
|
|
11
|
+
const readJson = (file) => {
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
14
|
+
} catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function loadConfig() {
|
|
20
|
+
return readJson(CONFIG_FILE) ?? {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// The file holds the login token and possibly an AI provider key: readable by this user only.
|
|
24
|
+
export function saveConfig(config) {
|
|
25
|
+
mkdirSync(CONFIG_DIR, { recursive: true, mode: 0o700 });
|
|
26
|
+
writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2), { mode: 0o600 });
|
|
27
|
+
try {
|
|
28
|
+
chmodSync(CONFIG_FILE, 0o600);
|
|
29
|
+
} catch {
|
|
30
|
+
// Not supported on every file system (e.g. some Windows setups).
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const updateConfig = (changes) => saveConfig({ ...loadConfig(), ...changes });
|
|
35
|
+
|
|
36
|
+
// Logging out removes the Elythia key but keeps the agent settings.
|
|
37
|
+
export function clearLogin() {
|
|
38
|
+
const { server, apiKey, ...rest } = loadConfig();
|
|
39
|
+
if (Object.keys(rest).length) saveConfig(rest);
|
|
40
|
+
else rmSync(CONFIG_FILE, { force: true });
|
|
41
|
+
}
|
package/src/defaults.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_SERVER_URL = "https://elythia.run";
|
package/src/home.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
2
|
+
import { SCENE_WIDTH, WORDMARK_WIDTH, templeFrame, wordmarkLines } from './art.js';
|
|
3
|
+
import { TICKER, c, chip, clearScreen, columns, drawCentered, enterFullScreen, formatCoin, key, leaveFullScreen, spread } from './ui.js';
|
|
4
|
+
|
|
5
|
+
const FRAME_MS = 70;
|
|
6
|
+
const SIDE_WIDTH = 34;
|
|
7
|
+
|
|
8
|
+
// The full-screen start screen a bare `elythia` opens: a temple over the moonlit sea, the
|
|
9
|
+
// account, and an arrow-key menu. Resolves with the chosen option's value, or null on quit.
|
|
10
|
+
// `account` is a promise for the logged-in user, or null when logged out.
|
|
11
|
+
export function home({ version, options, account, agentLabel }) {
|
|
12
|
+
return new Promise((resolve) => {
|
|
13
|
+
let index = 0;
|
|
14
|
+
const startedAt = Date.now();
|
|
15
|
+
let accountLine = account ? c.slate('reading the scrolls…') : c.slate('not logged in');
|
|
16
|
+
|
|
17
|
+
account?.then(
|
|
18
|
+
(me) => { accountLine = `${c.laurel('●')} ${c.bone(c.bold(me.username))} ${c.slate('·')} ${c.gold(`${formatCoin(me.balance)} ${TICKER}`)}`; },
|
|
19
|
+
(error) => { accountLine = c.crimson(error.status === 401 ? 'logged out, run elythia login' : error.message); },
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
function menuLines() {
|
|
23
|
+
return options.map((option, i) => (i === index
|
|
24
|
+
? `${c.gold('❯')} ${c.bone(c.bold(option.label.padEnd(14)))} ${c.granite(option.hint ?? '')}`
|
|
25
|
+
: ` ${c.granite(option.label.padEnd(14))} ${c.shadow(option.hint ?? '')}`));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function draw() {
|
|
29
|
+
const time = Date.now() - startedAt;
|
|
30
|
+
const side = [
|
|
31
|
+
...wordmarkLines(time),
|
|
32
|
+
c.gold('═'.repeat(WORDMARK_WIDTH)),
|
|
33
|
+
`${c.marble('ask the Oracle,')} ${c.sea('earn its favor')}`,
|
|
34
|
+
'',
|
|
35
|
+
accountLine,
|
|
36
|
+
agentLabel ? `${c.laurel('◈')} ${c.granite('agent')} ${c.marble(agentLabel)}` : `${c.shadow('◈')} ${c.slate('no agent chosen')}`,
|
|
37
|
+
'',
|
|
38
|
+
...menuLines(),
|
|
39
|
+
];
|
|
40
|
+
const keys = `${key('↑↓')} ${c.slate('move')} ${key('enter')} ${c.slate('select')} ${key('q')} ${c.slate('quit')}`;
|
|
41
|
+
if (columns() >= SCENE_WIDTH + SIDE_WIDTH + 6) {
|
|
42
|
+
const scene = templeFrame(time);
|
|
43
|
+
const top = Math.max(0, Math.floor((scene.length - side.length) / 2));
|
|
44
|
+
const rows = scene.map((line, row) => `${line} ${side[row - top] ?? ''}`);
|
|
45
|
+
const title = `${chip('ELYTHIA', 73)} ${c.slate('the oracle of the agents')}`;
|
|
46
|
+
drawCentered([title, '', ...rows, '', spread(keys, c.shadow(`v${version}`), SCENE_WIDTH + 4 + SIDE_WIDTH)]);
|
|
47
|
+
} else {
|
|
48
|
+
drawCentered([...side, '', keys]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const timer = setInterval(draw, FRAME_MS);
|
|
53
|
+
const onResize = () => clearScreen();
|
|
54
|
+
|
|
55
|
+
function finish(value) {
|
|
56
|
+
clearInterval(timer);
|
|
57
|
+
process.stdin.off('keypress', onKey);
|
|
58
|
+
process.stdout.off('resize', onResize);
|
|
59
|
+
process.stdin.setRawMode(false);
|
|
60
|
+
process.stdin.pause();
|
|
61
|
+
leaveFullScreen();
|
|
62
|
+
resolve(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function onKey(_, pressed = {}) {
|
|
66
|
+
if ((pressed.ctrl && pressed.name === 'c') || pressed.name === 'q' || pressed.name === 'escape') return finish(null);
|
|
67
|
+
if (pressed.name === 'up' || pressed.name === 'k') index = (index + options.length - 1) % options.length;
|
|
68
|
+
else if (pressed.name === 'down' || pressed.name === 'j' || pressed.name === 'tab') index = (index + 1) % options.length;
|
|
69
|
+
else if (/^[1-9]$/.test(pressed.sequence ?? '') && Number(pressed.sequence) <= options.length) index = Number(pressed.sequence) - 1;
|
|
70
|
+
else if (pressed.name === 'return' || pressed.name === 'enter') return finish(options[index].value);
|
|
71
|
+
draw();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
enterFullScreen();
|
|
75
|
+
emitKeypressEvents(process.stdin);
|
|
76
|
+
process.stdin.setRawMode(true);
|
|
77
|
+
process.stdin.resume();
|
|
78
|
+
process.stdin.on('keypress', onKey);
|
|
79
|
+
process.stdout.on('resize', onResize);
|
|
80
|
+
draw();
|
|
81
|
+
});
|
|
82
|
+
}
|
package/src/login.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
4
|
+
import { createClient } from './api.js';
|
|
5
|
+
import { SPINNER, c, chip, hideCursor, isTTY, showCursor, sleep } from './ui.js';
|
|
6
|
+
|
|
7
|
+
const PLATFORMS = { win32: 'Windows', darwin: 'macOS', linux: 'Linux' };
|
|
8
|
+
|
|
9
|
+
// Arrow-key menu. Returns the chosen option's value; falls back to the first one without a TTY.
|
|
10
|
+
export function select(question, options) {
|
|
11
|
+
if (!isTTY || !process.stdin.isTTY) return Promise.resolve(options[0].value);
|
|
12
|
+
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
let index = 0;
|
|
15
|
+
let drawn = false;
|
|
16
|
+
const draw = () => {
|
|
17
|
+
const lines = options.map((option, i) => (i === index
|
|
18
|
+
? ` ${c.sea('▶')} ${c.bone(c.bold(option.label))} ${c.granite(option.hint ?? '')}`
|
|
19
|
+
: ` ${c.granite(option.label)}`));
|
|
20
|
+
process.stdout.write(`${drawn ? `\x1b[${lines.length}A\r` : ''}${lines.map((line) => `\x1b[2K${line}`).join('\n')}\n`);
|
|
21
|
+
drawn = true;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const finish = (value) => {
|
|
25
|
+
process.stdin.off('keypress', onKey);
|
|
26
|
+
process.stdin.setRawMode(false);
|
|
27
|
+
process.stdin.pause();
|
|
28
|
+
showCursor();
|
|
29
|
+
resolve(value);
|
|
30
|
+
};
|
|
31
|
+
const onKey = (_, key = {}) => {
|
|
32
|
+
if (key.ctrl && key.name === 'c') {
|
|
33
|
+
showCursor();
|
|
34
|
+
process.stdout.write('\n');
|
|
35
|
+
process.exit(130);
|
|
36
|
+
}
|
|
37
|
+
if (key.name === 'up' || key.name === 'k') index = (index + options.length - 1) % options.length;
|
|
38
|
+
else if (key.name === 'down' || key.name === 'j' || key.name === 'tab') index = (index + 1) % options.length;
|
|
39
|
+
else if (/^[1-9]$/.test(key.sequence ?? '') && Number(key.sequence) <= options.length) index = Number(key.sequence) - 1;
|
|
40
|
+
else if (key.name === 'return' || key.name === 'enter') return finish(options[index].value);
|
|
41
|
+
draw();
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
console.log(` ${c.bone(question)} ${c.slate('(↑/↓, enter)')}\n`);
|
|
45
|
+
hideCursor();
|
|
46
|
+
emitKeypressEvents(process.stdin);
|
|
47
|
+
process.stdin.setRawMode(true);
|
|
48
|
+
process.stdin.resume();
|
|
49
|
+
process.stdin.on('keypress', onKey);
|
|
50
|
+
draw();
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Opens a URL in the default browser. Skipped over SSH, where there's no local browser.
|
|
55
|
+
export function openBrowser(url) {
|
|
56
|
+
if (process.env.SSH_CONNECTION || process.env.ELYTHIA_NO_BROWSER) return false;
|
|
57
|
+
const [command, args, options] = process.platform === 'win32'
|
|
58
|
+
? ['cmd', ['/c', 'start', '""', url], { windowsVerbatimArguments: true }]
|
|
59
|
+
: [process.platform === 'darwin' ? 'open' : 'xdg-open', [url], {}];
|
|
60
|
+
try {
|
|
61
|
+
const child = spawn(command, args, { stdio: 'ignore', detached: true, windowsHide: true, ...options });
|
|
62
|
+
child.on('error', () => {});
|
|
63
|
+
child.unref();
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Approve this terminal in the browser, and its login token comes back here.
|
|
71
|
+
export async function browserLogin(server) {
|
|
72
|
+
const client = createClient({ server });
|
|
73
|
+
const device = `${os.hostname()} · ${PLATFORMS[process.platform] ?? process.platform}`;
|
|
74
|
+
const { deviceCode, userCode, interval, expiresIn } = await client.loginStart(device);
|
|
75
|
+
const url = `${server}/authorize?code=${userCode}`;
|
|
76
|
+
|
|
77
|
+
const opened = openBrowser(url);
|
|
78
|
+
console.log([
|
|
79
|
+
'',
|
|
80
|
+
` ${c.granite('login code')} ${chip(userCode, 37)}`,
|
|
81
|
+
'',
|
|
82
|
+
opened ? ` ${c.marble('Opening your browser…')} ${c.slate('If nothing opens, visit:')}` : ` ${c.marble('Open this link to log in:')}`,
|
|
83
|
+
` ${c.sea(c.underline(url))}`,
|
|
84
|
+
'',
|
|
85
|
+
].join('\n'));
|
|
86
|
+
|
|
87
|
+
const deadline = Date.now() + expiresIn * 1000;
|
|
88
|
+
let frame = 0;
|
|
89
|
+
const status = () => {
|
|
90
|
+
if (!isTTY) return;
|
|
91
|
+
const left = Math.max(0, Math.ceil((deadline - Date.now()) / 1000));
|
|
92
|
+
const time = `${Math.floor(left / 60)}:${String(left % 60).padStart(2, '0')}`;
|
|
93
|
+
process.stdout.write(`\r\x1b[2K ${c.sea(SPINNER[frame++ % SPINNER.length])} ${c.granite('Waiting for you to authorize in the browser…')} ${c.slate(`${time} · ctrl+c to cancel`)}`);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
hideCursor();
|
|
97
|
+
const spinner = setInterval(status, 80);
|
|
98
|
+
try {
|
|
99
|
+
for (;;) {
|
|
100
|
+
await sleep(interval * 1000);
|
|
101
|
+
let result;
|
|
102
|
+
try {
|
|
103
|
+
result = await client.loginPoll(deviceCode);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (error.status === 0) continue; // network blip: keep waiting
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
if (result.status === 'approved') return result.apiKey;
|
|
109
|
+
}
|
|
110
|
+
} finally {
|
|
111
|
+
clearInterval(spinner);
|
|
112
|
+
if (isTTY) process.stdout.write('\r\x1b[2K');
|
|
113
|
+
showCursor();
|
|
114
|
+
}
|
|
115
|
+
}
|