omo-web 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 +21 -0
- package/README.md +21 -2
- package/bin/locale.js +6 -0
- package/bin/omo-web.js +96 -0
- package/bin/tui.js +495 -0
- package/bin/windows-password.js +76 -0
- package/dist/app.js +7 -0
- package/dist/server.js +12505 -0
- package/package.json +34 -4
- package/public/dag-graph.css +80 -0
- package/public/favicon.svg +13 -0
- package/public/icons.svg +89 -0
- package/public/index.html +486 -0
- package/public/licenses/teenyicons.txt +25 -0
- package/public/licenses/zod.txt +21 -0
- package/public/styles.css +422 -0
- package/public/tool-ledger.css +354 -0
- package/public/workbench.css +782 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OmO Web contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
#
|
|
1
|
+
# OmO Web
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A local browser workbench for OmO Native. Open sessions from different project folders, handle approvals, and inspect tool output or DAGs from the browser. OmO Native keeps your model settings and credentials.
|
|
4
|
+
|
|
5
|
+
## Get started
|
|
6
|
+
|
|
7
|
+
Install Node.js 18+, [Bun](https://bun.sh/docs/installation), and [OmO Native](https://github.com/code-yeongyu/oh-my-openagent). Configure your model in `omo` first.
|
|
8
|
+
|
|
9
|
+
If the CLI is already installed, run it from a project directory:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
cd /path/to/your/project
|
|
13
|
+
omo-web
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Open the printed URL (normally `http://127.0.0.1:4317`). The local browser connects to OmO on the host. `omo-web --help` lists startup commands.
|
|
17
|
+
|
|
18
|
+
In an interactive terminal, `omo-web` opens a keyboard-driven launcher. Use ↑/↓ and Enter to choose working directories and start the service. Once it is running, you can open the browser or stop the server from the same screen. For scripts or logging, run `omo-web serve` instead.
|
|
19
|
+
|
|
20
|
+
OmO Web is licensed under MIT. The package includes the original notices for its third-party assets.
|
|
21
|
+
|
|
22
|
+
[Installation](https://github.com/NekoCCN/OmO-Web#install) · [简体中文](https://github.com/NekoCCN/OmO-Web/blob/main/README.zh-CN.md) · [GitHub Releases](https://github.com/NekoCCN/OmO-Web/releases) · [Remote access](https://github.com/NekoCCN/OmO-Web/blob/main/docs/usage.md#密码与远程访问)
|
package/bin/locale.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const system = process.env.LC_ALL || process.env.LC_MESSAGES || process.env.LANG
|
|
2
|
+
|| Intl.DateTimeFormat().resolvedOptions().locale;
|
|
3
|
+
export const cliLocale = /^zh(?:[-_.@]|$)/iu.test(system) ? "zh-CN" : "en";
|
|
4
|
+
|
|
5
|
+
/** @param {string} zh @param {string} en */
|
|
6
|
+
export const cliTr = (zh, en) => cliLocale === "zh-CN" ? zh : en;
|
package/bin/omo-web.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { randomBytes } from "node:crypto";
|
|
4
|
+
import { closeSync, fchmodSync, openSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { cliTr } from "./locale.js";
|
|
7
|
+
import { startTui, stopWindowsServer } from "./tui.js";
|
|
8
|
+
import { createWindowsPasswordFile } from "./windows-password.js";
|
|
9
|
+
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
const version = () => JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
|
|
12
|
+
const entry = fileURLToPath(new URL("../dist/server.js", import.meta.url));
|
|
13
|
+
const terminal = process.stdin.isTTY && process.stdout.isTTY && process.env.TERM !== "dumb";
|
|
14
|
+
|
|
15
|
+
if (args.length === 1 && args[0] === "--version") {
|
|
16
|
+
console.log(version());
|
|
17
|
+
} else if (args.length === 1 && ["--help", "-h"].includes(args[0])) {
|
|
18
|
+
console.log(`${cliTr("用法:", "Usage:")} omo-web [tui | serve | password-file <path>]
|
|
19
|
+
|
|
20
|
+
omo-web ${cliTr("在交互终端打开启动器,否则直接启动服务。", "Open the launcher in a terminal; start directly otherwise.")}
|
|
21
|
+
omo-web tui ${cliTr("打开交互式启动器。", "Open the interactive launcher.")}
|
|
22
|
+
omo-web serve ${cliTr("直接启动 Web 服务。", "Start the Web service directly.")}
|
|
23
|
+
omo-web password-file <path> ${cliTr("创建私密随机密码文件,不覆盖。", "Create a private random password file without overwriting.")}
|
|
24
|
+
|
|
25
|
+
${cliTr("需要 Bun 和 OmO Native (omo)。默认使用当前目录。", "Requires Bun and OmO Native (omo). Defaults to the current directory.")}
|
|
26
|
+
${cliTr("用 OMO_WEB_WORKSPACES 和 OMO_WEB_PORT 设置目录和端口。", "Set directories and port with OMO_WEB_WORKSPACES and OMO_WEB_PORT.")}
|
|
27
|
+
${cliTr("监听 0.0.0.0 必须使用 HTTPS。建议设置 OMO_WEB_PASSWORD_FILE。",
|
|
28
|
+
"Listening on 0.0.0.0 requires HTTPS. Set OMO_WEB_PASSWORD_FILE for protection.")}
|
|
29
|
+
${cliTr("仅在受限私网或 VPN 使用无密码模式;脚本需显式设置 OMO_WEB_ALLOW_UNAUTHENTICATED=1。",
|
|
30
|
+
"Use password-free access only on a restricted private network or VPN. Scripts must set OMO_WEB_ALLOW_UNAUTHENTICATED=1.")}
|
|
31
|
+
https://github.com/NekoCCN/OmO-Web/blob/main/docs/usage.md`);
|
|
32
|
+
} else if (args.length === 2 && args[0] === "password-file" && args[1]) {
|
|
33
|
+
process.exitCode = await createPasswordFile(args[1]);
|
|
34
|
+
} else if (args.length > 1 || (args.length === 1 && args[0] !== "tui" && args[0] !== "serve")) {
|
|
35
|
+
console.error(cliTr("omo-web: 参数不受支持。请查看 omo-web --help。",
|
|
36
|
+
"omo-web: unsupported arguments. See omo-web --help."));
|
|
37
|
+
process.exitCode = 2;
|
|
38
|
+
} else if (args[0] === "tui" && !terminal) {
|
|
39
|
+
console.error(cliTr("omo-web: 启动器需要交互终端。请改用 omo-web serve。",
|
|
40
|
+
"omo-web: the launcher needs an interactive terminal. Use omo-web serve instead."));
|
|
41
|
+
process.exitCode = 2;
|
|
42
|
+
} else if (args[0] !== "serve" && terminal) {
|
|
43
|
+
process.exitCode = await startTui({ entry, version: version() });
|
|
44
|
+
} else {
|
|
45
|
+
const child = spawn("bun", ["--no-env-file", entry], { stdio: "inherit" });
|
|
46
|
+
let stopping = false;
|
|
47
|
+
const stop = signal => {
|
|
48
|
+
if (stopping) return;
|
|
49
|
+
stopping = true;
|
|
50
|
+
if (process.platform === "win32") stopWindowsServer(child);
|
|
51
|
+
else child.kill(signal);
|
|
52
|
+
};
|
|
53
|
+
const onInterrupt = () => stop("SIGINT");
|
|
54
|
+
const onTerminate = () => stop("SIGTERM");
|
|
55
|
+
process.once("SIGINT", onInterrupt);
|
|
56
|
+
process.once("SIGTERM", onTerminate);
|
|
57
|
+
child.once("error", error => {
|
|
58
|
+
console.error(error.code === "ENOENT"
|
|
59
|
+
? cliTr("omo-web: PATH 中找不到 Bun。请从 https://bun.sh 安装。",
|
|
60
|
+
"omo-web: Bun was not found on PATH. Install Bun from https://bun.sh.")
|
|
61
|
+
: cliTr(`omo-web: 无法运行 Bun (${error.code ?? "?"})。`, `omo-web: Could not run Bun (${error.code ?? "?"}).`));
|
|
62
|
+
process.exitCode = 1;
|
|
63
|
+
});
|
|
64
|
+
child.once("close", (code, signal) => {
|
|
65
|
+
process.removeListener("SIGINT", onInterrupt);
|
|
66
|
+
process.removeListener("SIGTERM", onTerminate);
|
|
67
|
+
process.exitCode ??= code ?? (signal === "SIGINT" ? 130 : signal === "SIGTERM" ? 143 : 1);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function createPasswordFile(path) {
|
|
72
|
+
try {
|
|
73
|
+
const password = randomBytes(32).toString("base64url");
|
|
74
|
+
if (process.platform === "win32") {
|
|
75
|
+
await createWindowsPasswordFile(path, password);
|
|
76
|
+
} else {
|
|
77
|
+
const fd = openSync(path, "wx", 0o600);
|
|
78
|
+
try {
|
|
79
|
+
writeFileSync(fd, `${password}\n`);
|
|
80
|
+
fchmodSync(fd, 0o600);
|
|
81
|
+
} finally {
|
|
82
|
+
closeSync(fd);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (!(error instanceof Error)) throw error;
|
|
87
|
+
console.error(error.code === "EEXIST"
|
|
88
|
+
? cliTr("omo-web: 目标已存在,未覆盖。", "omo-web: destination already exists; not overwritten.")
|
|
89
|
+
: cliTr("omo-web: 无法创建私密密码文件。请检查目标路径和权限。",
|
|
90
|
+
"omo-web: could not create a private password file. Check the destination and permissions."));
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
console.log(cliTr("密码文件已创建。请将 OMO_WEB_PASSWORD_FILE 设为该路径。",
|
|
94
|
+
"Password file created. Set OMO_WEB_PASSWORD_FILE to its path."));
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
package/bin/tui.js
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
// allow: SIZE_OK — The npm package ships this indivisible terminal session as one module.
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { statSync } from "node:fs";
|
|
4
|
+
import { delimiter, join, resolve } from "node:path";
|
|
5
|
+
import { emitKeypressEvents } from "node:readline";
|
|
6
|
+
import { stripVTControlCharacters } from "node:util";
|
|
7
|
+
import { cliTr } from "./locale.js";
|
|
8
|
+
|
|
9
|
+
const CSI = "\x1b[";
|
|
10
|
+
const HOME_CLEAR = `${CSI}H${CSI}2J`;
|
|
11
|
+
const SHOW_CURSOR = `${CSI}?25h`;
|
|
12
|
+
const HIDE_CURSOR = `${CSI}?25l`;
|
|
13
|
+
const MAX_RECEIPT = 4096;
|
|
14
|
+
const CONFIG_PREFIX = "OmO Web config: ";
|
|
15
|
+
const START_MS = 30_000;
|
|
16
|
+
const STOP_MS = 4_000;
|
|
17
|
+
const REMOTE_WARNING = cliTr("警告:无密码保护", "Warning: No password protection");
|
|
18
|
+
const REMOTE_RISK = [
|
|
19
|
+
cliTr("0.0.0.0 无密码访问", "0.0.0.0 without a password"),
|
|
20
|
+
cliTr("能连上的人可访问 OmO 和工作目录。", "Anyone who connects can access OmO and your working directories."),
|
|
21
|
+
];
|
|
22
|
+
const windows = process.platform === "win32";
|
|
23
|
+
const segmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
|
24
|
+
const controls = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/gu;
|
|
25
|
+
const safeText = value => value.replace(controls, " ");
|
|
26
|
+
const glyphs = text => Array.from(segmenter.segment(text), ({ segment }) => segment);
|
|
27
|
+
// Reserve two cells for non-ASCII graphemes so wide paths cannot wrap at the edge.
|
|
28
|
+
const cells = glyph => /^[\x20-\x7e]+$/.test(glyph) ? glyph.length : 2;
|
|
29
|
+
const textWidth = text => glyphs(text).reduce((sum, glyph) => sum + cells(glyph), 0);
|
|
30
|
+
|
|
31
|
+
function fold(text, width) {
|
|
32
|
+
const lines = [""];
|
|
33
|
+
let remaining = width;
|
|
34
|
+
for (const glyph of glyphs(safeText(text))) {
|
|
35
|
+
const size = cells(glyph);
|
|
36
|
+
if (size > remaining) {
|
|
37
|
+
lines.push("");
|
|
38
|
+
remaining = width;
|
|
39
|
+
}
|
|
40
|
+
lines[lines.length - 1] += glyph;
|
|
41
|
+
remaining -= size;
|
|
42
|
+
}
|
|
43
|
+
return lines;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pathLabel(path, width) {
|
|
47
|
+
const chars = glyphs(safeText(path));
|
|
48
|
+
const size = chars.reduce((sum, glyph) => sum + cells(glyph), 0);
|
|
49
|
+
if (size <= width) return chars.join("");
|
|
50
|
+
const head = fold(path, Math.min(10, Math.floor(width / 3)))[0];
|
|
51
|
+
let tail = "";
|
|
52
|
+
let room = width - [...head].reduce((sum, glyph) => sum + cells(glyph), 0) - 3;
|
|
53
|
+
for (const glyph of chars.reverse()) {
|
|
54
|
+
room -= cells(glyph);
|
|
55
|
+
if (room < 0) break;
|
|
56
|
+
tail = glyph + tail;
|
|
57
|
+
}
|
|
58
|
+
return `${head}...${tail}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function editorLine(draft, width) {
|
|
62
|
+
let left = "", right = "";
|
|
63
|
+
let column = 0;
|
|
64
|
+
for (const glyph of glyphs(safeText(draft.text.slice(0, draft.cursor))).reverse()) {
|
|
65
|
+
if (column + cells(glyph) >= width) break;
|
|
66
|
+
left = glyph + left;
|
|
67
|
+
column += cells(glyph);
|
|
68
|
+
}
|
|
69
|
+
let room = width - column;
|
|
70
|
+
for (const glyph of glyphs(safeText(draft.text.slice(draft.cursor)))) {
|
|
71
|
+
if (cells(glyph) > room) break;
|
|
72
|
+
right += glyph;
|
|
73
|
+
room -= cells(glyph);
|
|
74
|
+
}
|
|
75
|
+
// Let the terminal measure the cursor position, including ambiguous-width glyphs.
|
|
76
|
+
return `${left}${CSI}s${right}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function receiveLines(stream, onLine) {
|
|
80
|
+
let pending = "";
|
|
81
|
+
stream.setEncoding("utf8");
|
|
82
|
+
stream.on("data", chunk => {
|
|
83
|
+
const pieces = chunk.split("\n");
|
|
84
|
+
for (const [index, piece] of pieces.entries()) {
|
|
85
|
+
if (pending !== null) {
|
|
86
|
+
pending = pending.length + piece.length > MAX_RECEIPT ? null : pending + piece;
|
|
87
|
+
}
|
|
88
|
+
if (index === pieces.length - 1) break;
|
|
89
|
+
const line = pending?.replace(/\r$/, "");
|
|
90
|
+
pending = "";
|
|
91
|
+
if (line !== undefined) onLine(line);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function receiveListener(stream, onUrl) {
|
|
97
|
+
receiveLines(stream, line => {
|
|
98
|
+
// Match the actual stdout receipt as a whole line, not its sanitized display.
|
|
99
|
+
const receipt = line.match(/^OmO Web: (https?:\/\/[^\s\\\p{Cc}\p{Cf}]+)$/u);
|
|
100
|
+
if (!receipt || receipt[0] !== line) return;
|
|
101
|
+
try {
|
|
102
|
+
const parsed = new URL(receipt[1]);
|
|
103
|
+
if (!parsed.username && !parsed.password) onUrl(receipt[1]);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (!(error instanceof TypeError)) throw error;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Windows signals sent through Node terminate only the direct child.
|
|
111
|
+
// Stop the owned server process tree so a native engine cannot outlive it.
|
|
112
|
+
export function stopWindowsServer(child) {
|
|
113
|
+
if (child.pid === undefined || child.exitCode !== null || child.signalCode !== null) return;
|
|
114
|
+
const taskkill = join(process.env.SystemRoot || "C:\\Windows", "System32", "taskkill.exe");
|
|
115
|
+
const killer = spawn(taskkill, ["/PID", String(child.pid), "/T", "/F"], {
|
|
116
|
+
stdio: "ignore", windowsHide: true,
|
|
117
|
+
});
|
|
118
|
+
const fallback = () => {
|
|
119
|
+
if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
|
|
120
|
+
};
|
|
121
|
+
killer.once("error", fallback);
|
|
122
|
+
killer.once("exit", code => { if (code !== 0) fallback(); });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function killServerGroup(child) {
|
|
126
|
+
if (!windows && child.pid !== undefined) {
|
|
127
|
+
try {
|
|
128
|
+
process.kill(-child.pid, "SIGKILL");
|
|
129
|
+
} catch (error) {
|
|
130
|
+
if (error.code !== "ESRCH") throw error;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Run the launcher until the user quits (0), Ctrl+C interrupts (130), or a signal ends it. */
|
|
136
|
+
export async function startTui({ entry, version }) {
|
|
137
|
+
const input = process.stdin;
|
|
138
|
+
const output = process.stdout;
|
|
139
|
+
const color = process.env.NO_COLOR === undefined;
|
|
140
|
+
const ink = (sgr, text) => color ? `${CSI}${sgr}m${text}${CSI}0m` : text;
|
|
141
|
+
const cwd = process.cwd();
|
|
142
|
+
const unprotected = process.env.OMO_WEB_HOST === "0.0.0.0" && !process.env.OMO_WEB_PASSWORD_FILE;
|
|
143
|
+
const optedIn = process.env.OMO_WEB_ALLOW_UNAUTHENTICATED === "1";
|
|
144
|
+
let workspaces;
|
|
145
|
+
let draft;
|
|
146
|
+
let pasting = false;
|
|
147
|
+
let selected = 0;
|
|
148
|
+
let server;
|
|
149
|
+
let url;
|
|
150
|
+
let phase = "idle";
|
|
151
|
+
let stopped = false;
|
|
152
|
+
let notice = "";
|
|
153
|
+
let exitCode;
|
|
154
|
+
let settle;
|
|
155
|
+
let paint;
|
|
156
|
+
const finished = new Promise(resolve => { settle = resolve; });
|
|
157
|
+
|
|
158
|
+
const directoriesLabel = cliTr("工作目录", "Working directories");
|
|
159
|
+
const quitAction = [cliTr("退出", "Quit"), () => quit(0)];
|
|
160
|
+
const stopAction = [cliTr("停止服务", "Stop web"), stopWeb];
|
|
161
|
+
const menus = {
|
|
162
|
+
idle: [[cliTr("启动服务", "Start web"), requestStart], [directoriesLabel, editWorkspaces], quitAction],
|
|
163
|
+
confirming: [
|
|
164
|
+
[cliTr("返回", "Back"), backFromConfirmation],
|
|
165
|
+
[cliTr("仅本次无密码启动", "Start once without a password"), () => startWeb(true)],
|
|
166
|
+
],
|
|
167
|
+
starting: [stopAction, quitAction],
|
|
168
|
+
ready: [[cliTr("打开浏览器", "Open browser"), openBrowser], stopAction, quitAction],
|
|
169
|
+
stopping: [quitAction],
|
|
170
|
+
};
|
|
171
|
+
const status = {
|
|
172
|
+
idle: [() => notice ? cliTr("待机 / 见提示", "Idle / see notice")
|
|
173
|
+
: stopped ? cliTr("已停止", "Stopped") : cliTr("待机", "Idle"), "36"],
|
|
174
|
+
confirming: [() => cliTr("确认远程访问", "Confirm remote access"), "33;1"],
|
|
175
|
+
starting: [() => cliTr("启动中", "Starting"), "33;1"],
|
|
176
|
+
ready: [() => cliTr("监听中", "Listening"), "32;1"],
|
|
177
|
+
stopping: [() => cliTr("停止中", "Stopping"), "33;1"],
|
|
178
|
+
};
|
|
179
|
+
const configured = () => workspaces ?? process.env.OMO_WEB_WORKSPACES;
|
|
180
|
+
const activePaths = () => configured()
|
|
181
|
+
? configured().split(delimiter).filter(Boolean).map(path => resolve(cwd, path))
|
|
182
|
+
: [cwd];
|
|
183
|
+
const viewport = () => ({
|
|
184
|
+
width: Math.min(72, (output.columns || 80) - 4),
|
|
185
|
+
height: Math.max(1, (output.rows || 24) - 1),
|
|
186
|
+
});
|
|
187
|
+
const riskLines = width => REMOTE_RISK.flatMap(line => fold(line, width));
|
|
188
|
+
const confirmationFits = ({ width, height }) => width >= 32 && riskLines(width).length + 2 <= height;
|
|
189
|
+
const activeMenu = dimensions => phase === "confirming" && !confirmationFits(dimensions)
|
|
190
|
+
? menus.confirming.slice(0, 1) : menus[phase];
|
|
191
|
+
|
|
192
|
+
function render() {
|
|
193
|
+
paint = undefined;
|
|
194
|
+
const dimensions = viewport();
|
|
195
|
+
const { width, height } = dimensions;
|
|
196
|
+
if (width < 2) {
|
|
197
|
+
output.write(HIDE_CURSOR + HOME_CLEAR);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const [state, tone] = status[phase];
|
|
201
|
+
const label = state();
|
|
202
|
+
const build = fold(`v${version}`, 20)[0];
|
|
203
|
+
const title = cliTr(" Web 监听 ", " WEB LISTENER ");
|
|
204
|
+
const brand = "-- OmO Web ";
|
|
205
|
+
const gap = width - textWidth(title) - textWidth(label) - textWidth(build) - 1;
|
|
206
|
+
const boxed = width >= 44 && gap >= 2;
|
|
207
|
+
const header = boxed ? [
|
|
208
|
+
ink("36;1", `+${brand}${"-".repeat(width - brand.length)}+`),
|
|
209
|
+
`|${title}${ink(tone, label)}${" ".repeat(gap)}${build} |`,
|
|
210
|
+
ink("36", `+${"-".repeat(width)}+`),
|
|
211
|
+
] : fold(`OmO Web / ${label}`, width).map(line => ink(tone, line));
|
|
212
|
+
const short = text => fold(text, width)[0];
|
|
213
|
+
const editorTitle = ink("1", short(cliTr("编辑目录", "Edit directories")));
|
|
214
|
+
const menu = activeMenu(dimensions);
|
|
215
|
+
const focus = Math.min(selected, menu.length - 1);
|
|
216
|
+
const actions = menu.map(([action], index) => index === focus
|
|
217
|
+
? ink("7;1", short(`> ${action} `)) : short(` ${action}`));
|
|
218
|
+
const message = notice ? fold(notice, width).slice(0, 2).map(line => ink("33;1", line)) : [];
|
|
219
|
+
const warning = unprotected && (optedIn || server) && phase !== "confirming"
|
|
220
|
+
? fold(REMOTE_WARNING, width).map(line => ink("33;1", line)) : [];
|
|
221
|
+
const risk = phase === "confirming" && menu.length > 1
|
|
222
|
+
? riskLines(width).map(line => ink("33;1", line)) : [];
|
|
223
|
+
let editor;
|
|
224
|
+
let lines;
|
|
225
|
+
if (draft) {
|
|
226
|
+
editor = editorLine(draft, Math.max(2, width - 2));
|
|
227
|
+
lines = [...header, "", editorTitle, `> ${editor}`, "",
|
|
228
|
+
...fold(cliTr(`用 "${delimiter}" 分隔路径,留空使用启动目录。`,
|
|
229
|
+
`Separate paths with "${delimiter}". Empty uses the launch directory.`), width),
|
|
230
|
+
...message, "", ...fold(cliTr("Enter 确认 / Esc 取消 / Ctrl+U 清空", "Enter confirm / Esc cancel / Ctrl+U clear"), width)];
|
|
231
|
+
} else if (phase === "confirming") {
|
|
232
|
+
lines = [...header, "", ...risk, "", ...actions, "",
|
|
233
|
+
...fold(menu.length > 1
|
|
234
|
+
? cliTr("↑/↓ 移动 / Enter 选择 / Esc 返回", "Up/Down move / Enter select / Esc back")
|
|
235
|
+
: cliTr("请放大终端以查看风险提示。", "Enlarge the terminal to review the risk."), width)];
|
|
236
|
+
} else {
|
|
237
|
+
const paths = activePaths();
|
|
238
|
+
const urlRows = url ? fold(url, Math.max(2, width - 2)) : [];
|
|
239
|
+
const pathCount = height >= 23 ? 4 : 2;
|
|
240
|
+
const details = url ? [ink("1", short(cliTr("浏览器地址", "Browser URL"))), ...urlRows.slice(0, 2).map(line => ` ${line}`),
|
|
241
|
+
...(urlRows.length > 2 ? [" ..."] : []), ""] : [];
|
|
242
|
+
const directories = paths.length ? paths.slice(0, pathCount).map(path => ` ${pathLabel(path, width - 2)}`)
|
|
243
|
+
: [short(cliTr(" (未配置)", " (none configured)"))];
|
|
244
|
+
if (paths.length > pathCount) directories.push(short(cliTr(` 另有 ${paths.length - pathCount} 个`, ` +${paths.length - pathCount} more`)));
|
|
245
|
+
lines = [...header, ...warning, "", ...details, ink("1", short(directoriesLabel)), ...directories,
|
|
246
|
+
"", ...actions, ...message, "", ...fold(cliTr("↑/↓ 移动 / Enter 选择 / Q 退出", "Up/Down move / Enter select / Q quit"), width)];
|
|
247
|
+
}
|
|
248
|
+
for (let blank = lines.lastIndexOf(""); lines.length > height && blank >= 0; blank = lines.lastIndexOf("")) {
|
|
249
|
+
lines.splice(blank, 1);
|
|
250
|
+
}
|
|
251
|
+
// On very short terminals, prioritize the active control (or the draft).
|
|
252
|
+
if (lines.length > height) {
|
|
253
|
+
if (draft) {
|
|
254
|
+
lines = [...(height > 1 ? [editorTitle] : []), "> " + editor, ...message,
|
|
255
|
+
short(cliTr("Enter 确认 / Esc 取消", "Enter confirm / Esc cancel"))];
|
|
256
|
+
} else if (phase === "confirming") {
|
|
257
|
+
lines = [...risk, ...actions];
|
|
258
|
+
} else {
|
|
259
|
+
const compactStatus = ink(tone, short(`OmO Web / ${label}`));
|
|
260
|
+
lines = warning.length && height > 1
|
|
261
|
+
? [compactStatus, ...warning.slice(0, Math.max(1, height - 2)), actions[focus]]
|
|
262
|
+
: [...actions, ...message, compactStatus];
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
output.write(HIDE_CURSOR + HOME_CLEAR + lines.slice(0, height).map(line => ` ${line}`).join("\r\n")
|
|
266
|
+
+ (editor ? `${CSI}u${SHOW_CURSOR}` : ""));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function redraw() {
|
|
270
|
+
paint ??= setImmediate(render);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function editWorkspaces() {
|
|
274
|
+
const text = configured() || cwd;
|
|
275
|
+
draft = { text, cursor: text.length };
|
|
276
|
+
notice = "";
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function editKey(text, key) {
|
|
280
|
+
if (!pasting && key.name === "escape") {
|
|
281
|
+
draft = undefined;
|
|
282
|
+
notice = "";
|
|
283
|
+
} else if (!pasting && (key.name === "return" || key.name === "enter")) {
|
|
284
|
+
const paths = draft.text.split(delimiter).filter(Boolean).map(path => resolve(cwd, path));
|
|
285
|
+
for (const path of paths) {
|
|
286
|
+
try {
|
|
287
|
+
if (statSync(path, { throwIfNoEntry: false })?.isDirectory()) continue;
|
|
288
|
+
notice = cliTr(`不是目录:${path}`, `Not a directory: ${path}`);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (!(error instanceof Error)) throw error;
|
|
291
|
+
notice = cliTr(`无法使用目录 (${error.code ?? "?"}):${path}`,
|
|
292
|
+
`Cannot use directory (${error.code ?? "?"}): ${path}`);
|
|
293
|
+
}
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
workspaces = paths.join(delimiter);
|
|
297
|
+
draft = undefined;
|
|
298
|
+
notice = "";
|
|
299
|
+
} else {
|
|
300
|
+
const { text: value, cursor } = draft;
|
|
301
|
+
const left = value.slice(0, cursor), right = value.slice(cursor);
|
|
302
|
+
const previous = () => cursor - (glyphs(left).at(-1)?.length ?? 0);
|
|
303
|
+
const next = () => cursor + (glyphs(right)[0]?.length ?? 0);
|
|
304
|
+
if (!pasting && key.ctrl && key.name === "u") draft = { text: "", cursor: 0 };
|
|
305
|
+
else if (!pasting && key.name === "left") draft.cursor = previous();
|
|
306
|
+
else if (!pasting && key.name === "right") draft.cursor = next();
|
|
307
|
+
else if (!pasting && key.name === "home") draft.cursor = 0;
|
|
308
|
+
else if (!pasting && key.name === "end") draft.cursor = value.length;
|
|
309
|
+
else if (!pasting && key.name === "backspace") {
|
|
310
|
+
draft = { text: value.slice(0, previous()) + right, cursor: previous() };
|
|
311
|
+
} else if (!pasting && key.name === "delete") draft.text = left + value.slice(next());
|
|
312
|
+
else if (!key.ctrl && !key.meta && text && text === safeText(text) && value.length + text.length <= 8192) {
|
|
313
|
+
draft = { text: left + text + right, cursor: cursor + text.length };
|
|
314
|
+
} else return;
|
|
315
|
+
notice = "";
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function requestStart() {
|
|
320
|
+
if (unprotected && !optedIn) {
|
|
321
|
+
phase = "confirming";
|
|
322
|
+
selected = 0;
|
|
323
|
+
} else startWeb();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function backFromConfirmation() {
|
|
327
|
+
phase = "idle";
|
|
328
|
+
selected = 0;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function startWeb(confirmed = false) {
|
|
332
|
+
phase = "starting";
|
|
333
|
+
selected = 0;
|
|
334
|
+
notice = "";
|
|
335
|
+
redraw();
|
|
336
|
+
const env = {
|
|
337
|
+
...process.env,
|
|
338
|
+
...(workspaces === undefined ? {} : { OMO_WEB_WORKSPACES: workspaces }),
|
|
339
|
+
...(confirmed ? { OMO_WEB_ALLOW_UNAUTHENTICATED: "1" } : {}),
|
|
340
|
+
};
|
|
341
|
+
const child = spawn("bun", ["--no-env-file", entry], {
|
|
342
|
+
env, stdio: ["ignore", "pipe", "pipe"], detached: !windows, windowsHide: true,
|
|
343
|
+
});
|
|
344
|
+
let errorMessage;
|
|
345
|
+
let configMessage;
|
|
346
|
+
const startup = setTimeout(() => {
|
|
347
|
+
errorMessage = cliTr("未收到监听地址。请重试或运行 omo-web serve 查看详情。",
|
|
348
|
+
"No listener receipt. Retry or run omo-web serve for details.");
|
|
349
|
+
stopWeb();
|
|
350
|
+
}, START_MS);
|
|
351
|
+
server = { child, startup, killTimer: undefined };
|
|
352
|
+
receiveListener(child.stdout, href => {
|
|
353
|
+
if (phase !== "starting" || child.exitCode !== null || child.signalCode !== null) return;
|
|
354
|
+
clearTimeout(startup);
|
|
355
|
+
url = href;
|
|
356
|
+
phase = "ready";
|
|
357
|
+
selected = 0;
|
|
358
|
+
redraw();
|
|
359
|
+
});
|
|
360
|
+
receiveLines(child.stderr, line => {
|
|
361
|
+
if (phase !== "starting" || configMessage || !line.startsWith(CONFIG_PREFIX)) return;
|
|
362
|
+
configMessage = safeText(stripVTControlCharacters(line.slice(CONFIG_PREFIX.length))).trim();
|
|
363
|
+
});
|
|
364
|
+
child.once("error", error => {
|
|
365
|
+
errorMessage = error.code === "ENOENT"
|
|
366
|
+
? cliTr("PATH 中找不到 Bun。请从 bun.sh 安装。", "Bun was not found on PATH. Install it from bun.sh.")
|
|
367
|
+
: cliTr(`无法运行 Bun (${error.code ?? "?"})。`, `Could not run Bun (${error.code ?? "?"}).`);
|
|
368
|
+
});
|
|
369
|
+
child.once("exit", () => {
|
|
370
|
+
clearTimeout(server.killTimer);
|
|
371
|
+
// The main server closes its resources on SIGTERM; reap the group if
|
|
372
|
+
// anything survived that cleanup or the server exited unexpectedly.
|
|
373
|
+
if (!windows) killServerGroup(child);
|
|
374
|
+
});
|
|
375
|
+
child.once("close", (code, signal) => {
|
|
376
|
+
clearTimeout(startup);
|
|
377
|
+
clearTimeout(server.killTimer);
|
|
378
|
+
if (exitCode !== undefined) {
|
|
379
|
+
settle(exitCode);
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
stopped = phase === "stopping" && !errorMessage;
|
|
383
|
+
notice = stopped ? "" : (configMessage || errorMessage || cliTr(`Web 服务已退出 (${signal ?? code})。请重试或运行 omo-web serve。`,
|
|
384
|
+
`Web exited (${signal ?? code}). Retry or run omo-web serve.`));
|
|
385
|
+
server = undefined;
|
|
386
|
+
phase = "idle";
|
|
387
|
+
url = undefined;
|
|
388
|
+
selected = 0;
|
|
389
|
+
redraw();
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function stopWeb() {
|
|
394
|
+
if (!server || phase === "stopping") return;
|
|
395
|
+
phase = "stopping";
|
|
396
|
+
url = undefined;
|
|
397
|
+
selected = 0;
|
|
398
|
+
notice = "";
|
|
399
|
+
const { child, startup } = server;
|
|
400
|
+
clearTimeout(startup);
|
|
401
|
+
if (windows) {
|
|
402
|
+
stopWindowsServer(child);
|
|
403
|
+
server.killTimer = setTimeout(() => {
|
|
404
|
+
if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
|
|
405
|
+
}, STOP_MS);
|
|
406
|
+
} else {
|
|
407
|
+
child.kill("SIGTERM");
|
|
408
|
+
server.killTimer = setTimeout(() => killServerGroup(child), STOP_MS);
|
|
409
|
+
}
|
|
410
|
+
redraw();
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function openBrowser() {
|
|
414
|
+
const current = server;
|
|
415
|
+
const commands = {
|
|
416
|
+
darwin: ["open", url],
|
|
417
|
+
win32: ["rundll32.exe", "url.dll,FileProtocolHandler", url],
|
|
418
|
+
};
|
|
419
|
+
const [command, ...args] = commands[process.platform] ?? ["xdg-open", url];
|
|
420
|
+
const browser = spawn(command, args, { stdio: "ignore", windowsHide: true, detached: !windows });
|
|
421
|
+
const failed = () => {
|
|
422
|
+
if (server !== current || phase !== "ready" || exitCode !== undefined) return;
|
|
423
|
+
notice = cliTr("无法打开浏览器。请手动打开上方地址。", "Could not open a browser. Open the URL above.");
|
|
424
|
+
redraw();
|
|
425
|
+
};
|
|
426
|
+
browser.once("error", failed);
|
|
427
|
+
browser.once("exit", code => { if (code !== 0) failed(); });
|
|
428
|
+
browser.unref();
|
|
429
|
+
notice = "";
|
|
430
|
+
redraw();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function quit(code) {
|
|
434
|
+
if (exitCode !== undefined) return;
|
|
435
|
+
exitCode = code;
|
|
436
|
+
if (server) stopWeb();
|
|
437
|
+
else settle(code);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function onKey(text, key) {
|
|
441
|
+
if (exitCode !== undefined || !key) return;
|
|
442
|
+
if (key.ctrl && key.name === "c") return quit(130);
|
|
443
|
+
if (key.name === "paste-start" || key.name === "paste-end") {
|
|
444
|
+
pasting = key.name === "paste-start";
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
if (draft) editKey(text, key);
|
|
448
|
+
else if (pasting) return;
|
|
449
|
+
else if (key.name === "escape" && phase === "confirming") backFromConfirmation();
|
|
450
|
+
else if (key.name === "q" || key.name === "escape") return quit(0);
|
|
451
|
+
else {
|
|
452
|
+
const menu = activeMenu(viewport());
|
|
453
|
+
selected = Math.min(selected, menu.length - 1);
|
|
454
|
+
if (key.name === "return" || key.name === "enter") menu[selected][1]();
|
|
455
|
+
else if (key.name === "up" || key.name === "down") {
|
|
456
|
+
selected = (selected + (key.name === "up" ? -1 : 1) + menu.length) % menu.length;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
redraw();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
const wasRaw = input.isRaw;
|
|
463
|
+
const wasFlowing = input.readableFlowing === true;
|
|
464
|
+
const onInt = () => quit(130);
|
|
465
|
+
const onTerm = () => quit(143);
|
|
466
|
+
const onHangup = () => quit(129);
|
|
467
|
+
try {
|
|
468
|
+
process.on("SIGINT", onInt);
|
|
469
|
+
process.on("SIGTERM", onTerm);
|
|
470
|
+
process.on("SIGHUP", onHangup);
|
|
471
|
+
input.on("end", onHangup);
|
|
472
|
+
input.setRawMode(true);
|
|
473
|
+
output.write(`${CSI}?1049h${CSI}?2004h`);
|
|
474
|
+
emitKeypressEvents(input);
|
|
475
|
+
input.on("keypress", onKey);
|
|
476
|
+
input.resume();
|
|
477
|
+
output.on("resize", redraw);
|
|
478
|
+
render();
|
|
479
|
+
return await finished;
|
|
480
|
+
} finally {
|
|
481
|
+
clearImmediate(paint);
|
|
482
|
+
output.off("resize", redraw);
|
|
483
|
+
input.off("keypress", onKey);
|
|
484
|
+
input.off("end", onHangup);
|
|
485
|
+
if (!wasFlowing) input.pause();
|
|
486
|
+
try {
|
|
487
|
+
input.setRawMode(wasRaw);
|
|
488
|
+
} finally {
|
|
489
|
+
output.write(`${CSI}?2004l${CSI}?1049l${SHOW_CURSOR}`);
|
|
490
|
+
process.off("SIGINT", onInt);
|
|
491
|
+
process.off("SIGTERM", onTerm);
|
|
492
|
+
process.off("SIGHUP", onHangup);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|