uitest-console 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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/NOTICE +9 -0
- package/README.md +45 -2
- package/SECURITY.md +23 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +15 -0
- package/dist/cli.d.ts +72 -0
- package/dist/cli.js +247 -0
- package/dist/page.d.ts +50 -0
- package/dist/page.js +223 -0
- package/dist/server.d.ts +52 -0
- package/dist/server.js +100 -0
- package/dist/version.d.ts +4 -0
- package/dist/version.js +12 -0
- package/dist/window.d.ts +39 -0
- package/dist/window.js +53 -0
- package/package.json +44 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# 更新日志
|
|
2
|
+
|
|
3
|
+
本文件记录 `uitest-console` 的公开版本。格式参考 [Keep a Changelog](https://keepachangelog.com/),
|
|
4
|
+
版本号遵循语义化版本。
|
|
5
|
+
|
|
6
|
+
## [0.1.0] — 2026-11-18
|
|
7
|
+
|
|
8
|
+
第一个版本:`uitest-sdk` 0.9.4 里的 `live` 页面成为独立的人机产品。
|
|
9
|
+
|
|
10
|
+
- 镜像与节点表复用 core 的 viewer(播放器、inspect、隐私开关、来源校验)。
|
|
11
|
+
- 新增**接管**:Hold 写「人持有」租约,agent 命令停在 `lease_held`;Resume 交还。
|
|
12
|
+
只观看不占租约,可与 agent 同时运行。
|
|
13
|
+
- 新增**审批收件箱**:列出 core 网关的待审批步骤,Allow(once/task/always)或 Deny。
|
|
14
|
+
- 活动视图只读展示最近 trace。
|
|
15
|
+
- 浏览器窗口优先用 Edge/Chrome 的 `--app`;没有可用浏览器时打印 URL。
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 uitest-sdk 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/NOTICE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
uitest-console
|
|
2
|
+
==============
|
|
3
|
+
|
|
4
|
+
This package's own source is MIT-licensed (see LICENSE).
|
|
5
|
+
|
|
6
|
+
It contains no native binaries and no bundled assets: the device extensions it
|
|
7
|
+
uses — agent.so and libscreen_casting.z.so — ship inside the `uitest-sdk`
|
|
8
|
+
dependency and are Huawei's signed builds; the MIT licence does not reach them.
|
|
9
|
+
See uitest-sdk's NOTICE for their versions and checksums.
|
package/README.md
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# uitest-console
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The person-facing half of [`uitest-phone`](https://www.npmjs.com/package/uitest-phone):
|
|
4
|
+
a local web page that mirrors the phone, lets you take it, and shows the steps
|
|
5
|
+
waiting for your approval. Built on [`uitest-sdk`](https://www.npmjs.com/package/uitest-sdk).
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm i -g uitest-console
|
|
9
|
+
uitest-console
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
It opens a browser window with:
|
|
13
|
+
|
|
14
|
+
- **the mirror and the node table** — the same player, masking and Origin check
|
|
15
|
+
as the SDK; click a node (or its box) to copy its ref, role, label and centre;
|
|
16
|
+
- **Hold / Resume** — Hold writes the person's lease, so every agent command
|
|
17
|
+
stops with `lease_held`; Resume hands the phone back and the next command
|
|
18
|
+
continues. Watching alone never takes the lease, so the console runs beside an
|
|
19
|
+
agent session;
|
|
20
|
+
- **the approval inbox** — the steps the policy gated, with the app, the rule and
|
|
21
|
+
the time left; Allow (once / this task / always) or Deny. It writes the same
|
|
22
|
+
decision files `uitest-phone approvals` writes, so either channel can answer;
|
|
23
|
+
- **recent activity** — the last trace steps, read-only, newest last.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
uitest-console --device <id> --state-dir .uitest-phone # pick a device/state
|
|
27
|
+
uitest-console --transport mjpeg --scale 2 # emulator or slow link
|
|
28
|
+
uitest-console --record demo.mp4 # record while you watch
|
|
29
|
+
uitest-console --no-layout # mirror only, no node table
|
|
30
|
+
uitest-console --no-open # print the URL only
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The same flags the old `uitest-sdk live` took are still here: `--privacy on|off`,
|
|
34
|
+
`--port`, `--host`, `--fps`, `--cast-port`, `--ws-extension`.
|
|
35
|
+
|
|
36
|
+
## Security
|
|
37
|
+
|
|
38
|
+
The server listens on `127.0.0.1:7042` unless `--host` says otherwise. **Anyone
|
|
39
|
+
who can reach that port can watch and drive the phone** — `--host 0.0.0.0` hands
|
|
40
|
+
the device to the whole network. The WebSocket handshake only accepts an `Origin`
|
|
41
|
+
matching the address the client dialled (the implementation is core's, shared
|
|
42
|
+
with every product), the inbox writes the gateway's decision files, Hold is an
|
|
43
|
+
explicit lease, and device text is escaped before it reaches the page. Device
|
|
44
|
+
serials only ever appear as the 8-character tag.
|
|
45
|
+
|
|
46
|
+
Full documentation: <https://growvv.github.io/uitest-sdk/>.
|
package/SECURITY.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# 安全说明
|
|
2
|
+
|
|
3
|
+
## 报告漏洞
|
|
4
|
+
|
|
5
|
+
请通过 GitHub Security Advisories 私下报告:
|
|
6
|
+
|
|
7
|
+
https://github.com/growvv/uitest-sdk/security/advisories/new
|
|
8
|
+
|
|
9
|
+
## 信任边界
|
|
10
|
+
|
|
11
|
+
`uitest-console` 起一个本地 HTTP/WebSocket 服务,默认只监听 `127.0.0.1`。
|
|
12
|
+
**任何能访问这个端口的人都能看屏幕并驱动手机**——`--host 0.0.0.0` 等于把设备
|
|
13
|
+
交给整个局域网,只在明确需要时使用。
|
|
14
|
+
|
|
15
|
+
- **来源校验在 core。** WebSocket 握手只接受与页面同源的 `Origin`;拒绝路径
|
|
16
|
+
和状态码由 `uitest-sdk/viewer` 提供,所有产品共用一份实现。
|
|
17
|
+
- **审批由人决定。** 收件箱写入的是 core 网关的决策文件,与
|
|
18
|
+
`uitest-phone approvals` 完全等价;agent 持有 id 也不能自己批准。
|
|
19
|
+
- **接管是显式的。** Hold 写「人持有」租约;关闭页面不会留下租约(只看不占)。
|
|
20
|
+
没有基于进程存活或设备侧触摸的隐式接管。
|
|
21
|
+
- **页面不执行设备数据。** 审批行、应用名、规则和 trace 文本都经过转义后插入;
|
|
22
|
+
设备上的标签不会变成脚本。
|
|
23
|
+
- **序列号不落盘。** 页面、日志和租约只使用设备序列号的 8 字符哈希。
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { consoleMain } from "./cli.js";
|
|
3
|
+
import { CORE_VERSION, VERSION } from "./version.js";
|
|
4
|
+
const [first] = process.argv.slice(2);
|
|
5
|
+
if (first === "--version" || first === "-v") {
|
|
6
|
+
process.stdout.write(`uitest-console ${VERSION} (uitest-sdk ${CORE_VERSION})\n`);
|
|
7
|
+
}
|
|
8
|
+
else {
|
|
9
|
+
consoleMain(process.argv.slice(2)).then((code) => {
|
|
10
|
+
process.exitCode = code;
|
|
11
|
+
}, (e) => {
|
|
12
|
+
process.stderr.write(`error: ${e instanceof Error ? e.message : String(e)}\n`);
|
|
13
|
+
process.exitCode = 1;
|
|
14
|
+
});
|
|
15
|
+
}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { connect } from "uitest-sdk";
|
|
2
|
+
import type { ConnectOptions } from "uitest-sdk";
|
|
3
|
+
import { consoleHooks } from "./server.js";
|
|
4
|
+
export interface ConsoleCliOptions {
|
|
5
|
+
port: number;
|
|
6
|
+
host: string;
|
|
7
|
+
privacy: boolean;
|
|
8
|
+
open: boolean;
|
|
9
|
+
device?: string;
|
|
10
|
+
stateDir: string;
|
|
11
|
+
task: string;
|
|
12
|
+
transport?: "grpc" | "ws" | "mjpeg";
|
|
13
|
+
record?: string;
|
|
14
|
+
/** Serve the node table beside the video. `false` is video-only mirroring. */
|
|
15
|
+
layout: boolean;
|
|
16
|
+
scale: 1 | 2 | 3 | 4 | 5;
|
|
17
|
+
frameRate: 15 | 30 | 60 | 120;
|
|
18
|
+
/** ws transport only: device-side listening port. */
|
|
19
|
+
castPort?: number;
|
|
20
|
+
/** ws transport only: local path to the 7.0 extension. */
|
|
21
|
+
wsExtension?: string;
|
|
22
|
+
}
|
|
23
|
+
/** Reads the command line, rejecting values it cannot honour. */
|
|
24
|
+
export declare function consoleOptions(args: Record<string, string | boolean | string[]>): ConsoleCliOptions;
|
|
25
|
+
export interface ConsoleRun {
|
|
26
|
+
url: string;
|
|
27
|
+
port: number;
|
|
28
|
+
/** The device's 8-character hash, the only identifier ever printed. */
|
|
29
|
+
tag: string;
|
|
30
|
+
/** Stops the page, releases a hold this console took, and lets the device go. */
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Opens the device, builds the page, and resolves once it is listening.
|
|
35
|
+
*
|
|
36
|
+
* Split from `consoleMain` so a test can drive the wiring without a browser
|
|
37
|
+
* and without owning the process's signals.
|
|
38
|
+
*/
|
|
39
|
+
export declare function startConsole(opts: ConsoleCliOptions, open?: (o: ConnectOptions) => Promise<Awaited<ReturnType<typeof connect>>>): Promise<ConsoleRun>;
|
|
40
|
+
/**
|
|
41
|
+
* The console's options from the command line.
|
|
42
|
+
*
|
|
43
|
+
* `parseArgv` files `--device`, `--state-dir` and `--task` under `options`
|
|
44
|
+
* (they are global flags shared with the phone), and everything else under
|
|
45
|
+
* `args`. Reading only `args` silently drops the device a person named and
|
|
46
|
+
* opens whichever device is attached — which is how a two-device acceptance
|
|
47
|
+
* run ended up pointed at the wrong phone. Merge both, options last: the
|
|
48
|
+
* parser's normalised view of a global flag is the one to trust.
|
|
49
|
+
*/
|
|
50
|
+
export declare function consoleOptionsFrom(argv: string[]): ConsoleCliOptions;
|
|
51
|
+
/**
|
|
52
|
+
* Resolves when the person or their supervisor asks this console to stop.
|
|
53
|
+
*
|
|
54
|
+
* SIGINT/SIGTERM cover a terminal. They are not enough for a supervisor:
|
|
55
|
+
* on Windows `child.kill()` terminates the process outright and no handler
|
|
56
|
+
* runs, so the device lease and the hdc port forward are never released —
|
|
57
|
+
* twenty abandoned forwards filled 8012..8031 and the next run could not
|
|
58
|
+
* open the tunnel. Closing the child's stdin is a signal every platform
|
|
59
|
+
* delivers, so it is honoured as the same request to stop.
|
|
60
|
+
*/
|
|
61
|
+
export declare function waitForStop(io: {
|
|
62
|
+
stdin: {
|
|
63
|
+
resume(): void;
|
|
64
|
+
on(event: string, listener: () => void): unknown;
|
|
65
|
+
};
|
|
66
|
+
signals: {
|
|
67
|
+
once(event: string, listener: () => void): unknown;
|
|
68
|
+
};
|
|
69
|
+
}): Promise<void>;
|
|
70
|
+
export declare function consoleMain(argv: string[]): Promise<number>;
|
|
71
|
+
/** Exported for tests: the hooks the page talks to, without a device. */
|
|
72
|
+
export { consoleHooks };
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { connect } from "uitest-sdk";
|
|
3
|
+
import { parseArgv } from "uitest-sdk/commands";
|
|
4
|
+
import { Approvals, holdDevice, readHold, readRecentTrace, resumeDevice } from "uitest-sdk/govern";
|
|
5
|
+
import { createViewerSource } from "uitest-sdk/viewer";
|
|
6
|
+
import { createConsoleServer, consoleHooks } from "./server.js";
|
|
7
|
+
import { openWindow } from "./window.js";
|
|
8
|
+
import { VERSION } from "./version.js";
|
|
9
|
+
/**
|
|
10
|
+
* `uitest-console` — the person-facing half of the same device session.
|
|
11
|
+
*
|
|
12
|
+
* One process, one browser window, one page: the mirror and node table come
|
|
13
|
+
* from core's viewer, and this product adds takeover and the approval inbox.
|
|
14
|
+
* It never holds the device lease while merely watching (an agent may be
|
|
15
|
+
* working at the same time); `Hold` writes the person's lease, which is what
|
|
16
|
+
* pauses the agents, and `Resume` hands it back.
|
|
17
|
+
*/
|
|
18
|
+
const DEFAULT_PORT = 7042;
|
|
19
|
+
/** Reads the command line, rejecting values it cannot honour. */
|
|
20
|
+
export function consoleOptions(args) {
|
|
21
|
+
const str = (name) => {
|
|
22
|
+
const raw = args[name];
|
|
23
|
+
if (raw === undefined || raw === true)
|
|
24
|
+
return undefined;
|
|
25
|
+
return Array.isArray(raw) ? raw[0] : String(raw);
|
|
26
|
+
};
|
|
27
|
+
const num = (name, fallback) => {
|
|
28
|
+
const raw = args[name];
|
|
29
|
+
if (raw === undefined)
|
|
30
|
+
return fallback;
|
|
31
|
+
const value = Number(Array.isArray(raw) ? raw[0] : raw);
|
|
32
|
+
if (!Number.isFinite(value) || value < 0 || value > 65_535) {
|
|
33
|
+
throw new Error(`--${name} must be a port number`);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
};
|
|
37
|
+
const privacyRaw = str("privacy");
|
|
38
|
+
let privacy = true;
|
|
39
|
+
if (privacyRaw !== undefined) {
|
|
40
|
+
const normalized = privacyRaw.toLowerCase();
|
|
41
|
+
if (["off", "false", "no", "0"].includes(normalized))
|
|
42
|
+
privacy = false;
|
|
43
|
+
else if (!["on", "true", "yes", "1"].includes(normalized)) {
|
|
44
|
+
throw new Error(`--privacy takes on or off, got "${privacyRaw}"`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const transport = str("transport");
|
|
48
|
+
if (transport !== undefined && !["grpc", "ws", "mjpeg"].includes(transport)) {
|
|
49
|
+
throw new Error(`--transport takes grpc, ws or mjpeg, got "${transport}"`);
|
|
50
|
+
}
|
|
51
|
+
// The stream settings the old `live` command took. They are not decoration:
|
|
52
|
+
// a person on a slow link needs `--scale 2`, and a 7.0 device needs
|
|
53
|
+
// `--transport ws` with its extension.
|
|
54
|
+
const scaleRaw = num("scale", 1);
|
|
55
|
+
if (![1, 2, 3, 4, 5].includes(scaleRaw))
|
|
56
|
+
throw new Error("--scale must be 1..5");
|
|
57
|
+
const frameRate = num("fps", 30);
|
|
58
|
+
if (![15, 30, 60, 120].includes(frameRate)) {
|
|
59
|
+
throw new Error("--fps must be 15, 30, 60 or 120");
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
port: num("port", DEFAULT_PORT),
|
|
63
|
+
host: str("host") ?? "127.0.0.1",
|
|
64
|
+
privacy,
|
|
65
|
+
open: args["noOpen"] !== true,
|
|
66
|
+
...(str("device") ? { device: str("device") } : {}),
|
|
67
|
+
stateDir: str("stateDir") ?? ".uitest-phone",
|
|
68
|
+
task: str("task") ?? "console",
|
|
69
|
+
...(transport ? { transport: transport } : {}),
|
|
70
|
+
...(str("record") ? { record: str("record") } : {}),
|
|
71
|
+
layout: args["noLayout"] !== true,
|
|
72
|
+
scale: scaleRaw,
|
|
73
|
+
frameRate: frameRate,
|
|
74
|
+
...(str("castPort") ? { castPort: num("castPort", 5000) } : {}),
|
|
75
|
+
...(str("wsExtension") ? { wsExtension: str("wsExtension") } : {}),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Opens the device, builds the page, and resolves once it is listening.
|
|
80
|
+
*
|
|
81
|
+
* Split from `consoleMain` so a test can drive the wiring without a browser
|
|
82
|
+
* and without owning the process's signals.
|
|
83
|
+
*/
|
|
84
|
+
export async function startConsole(opts, open = connect) {
|
|
85
|
+
// No lease: watching a device an agent is driving is the point. `Hold` is the
|
|
86
|
+
// explicit statement that the person wants it, and it writes the same lease
|
|
87
|
+
// the CLI's `hold` writes.
|
|
88
|
+
const session = await open({
|
|
89
|
+
...(opts.device ? { deviceId: opts.device } : {}),
|
|
90
|
+
stateDir: opts.stateDir,
|
|
91
|
+
task: opts.task,
|
|
92
|
+
lease: false,
|
|
93
|
+
});
|
|
94
|
+
const source = createViewerSource(session.core, session.device, {
|
|
95
|
+
stateDir: opts.stateDir,
|
|
96
|
+
layout: opts.layout,
|
|
97
|
+
cast: {
|
|
98
|
+
scale: opts.scale,
|
|
99
|
+
frameRate: opts.frameRate,
|
|
100
|
+
...(opts.transport ? { transport: opts.transport } : {}),
|
|
101
|
+
...(opts.castPort !== undefined ? { port: opts.castPort } : {}),
|
|
102
|
+
...(opts.wsExtension ? { wsExtension: opts.wsExtension } : {}),
|
|
103
|
+
},
|
|
104
|
+
...(opts.record ? { recordPath: opts.record } : {}),
|
|
105
|
+
});
|
|
106
|
+
const deps = {
|
|
107
|
+
approvals: new Approvals({ dir: join(opts.stateDir, "approvals", session.tag) }),
|
|
108
|
+
stateDir: opts.stateDir,
|
|
109
|
+
tag: session.tag,
|
|
110
|
+
hold: () => holdDevice({ stateDir: opts.stateDir, tag: session.tag }),
|
|
111
|
+
resume: () => resumeDevice({ stateDir: opts.stateDir, tag: session.tag }),
|
|
112
|
+
// The lease is the truth: a hold may have been written by `uitest-phone
|
|
113
|
+
// hold` before this page started, and `resume` may run from another shell.
|
|
114
|
+
isHeld: async () => {
|
|
115
|
+
const lease = await readHold({ stateDir: opts.stateDir, tag: session.tag });
|
|
116
|
+
return lease?.hold === true;
|
|
117
|
+
},
|
|
118
|
+
activity: () => readRecentTrace(opts.stateDir, { limit: 20 }),
|
|
119
|
+
};
|
|
120
|
+
let server;
|
|
121
|
+
try {
|
|
122
|
+
server = await createConsoleServer({
|
|
123
|
+
...deps,
|
|
124
|
+
source,
|
|
125
|
+
port: opts.port,
|
|
126
|
+
host: opts.host,
|
|
127
|
+
privacy: opts.privacy,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
// A port already in use is the everyday case: the person ran the console
|
|
132
|
+
// twice. Leaving the session open would keep a daemon and an fport behind
|
|
133
|
+
// for a page that never existed.
|
|
134
|
+
await session.close().catch(() => undefined);
|
|
135
|
+
throw e;
|
|
136
|
+
}
|
|
137
|
+
const shown = opts.host === "0.0.0.0" || opts.host === "::" ? "127.0.0.1" : opts.host;
|
|
138
|
+
const url = `http://${shown}:${server.port}/`;
|
|
139
|
+
let closed = false;
|
|
140
|
+
return {
|
|
141
|
+
url,
|
|
142
|
+
port: server.port,
|
|
143
|
+
tag: session.tag,
|
|
144
|
+
async close() {
|
|
145
|
+
if (closed)
|
|
146
|
+
return;
|
|
147
|
+
closed = true;
|
|
148
|
+
await server.close();
|
|
149
|
+
await session.close();
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The console's options from the command line.
|
|
155
|
+
*
|
|
156
|
+
* `parseArgv` files `--device`, `--state-dir` and `--task` under `options`
|
|
157
|
+
* (they are global flags shared with the phone), and everything else under
|
|
158
|
+
* `args`. Reading only `args` silently drops the device a person named and
|
|
159
|
+
* opens whichever device is attached — which is how a two-device acceptance
|
|
160
|
+
* run ended up pointed at the wrong phone. Merge both, options last: the
|
|
161
|
+
* parser's normalised view of a global flag is the one to trust.
|
|
162
|
+
*/
|
|
163
|
+
export function consoleOptionsFrom(argv) {
|
|
164
|
+
const parsed = parseArgv(argv);
|
|
165
|
+
const merged = { ...parsed.args };
|
|
166
|
+
// The three global flags the console acts on. The rest of `options`
|
|
167
|
+
// (read-only, confirm, apps, settle-ms) belong to the phone's session, and
|
|
168
|
+
// the console must not silently adopt them as if they were its own.
|
|
169
|
+
for (const key of ["device", "stateDir", "task"]) {
|
|
170
|
+
const value = parsed.options[key];
|
|
171
|
+
if (value !== undefined)
|
|
172
|
+
merged[key] = value;
|
|
173
|
+
}
|
|
174
|
+
return consoleOptions(merged);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Resolves when the person or their supervisor asks this console to stop.
|
|
178
|
+
*
|
|
179
|
+
* SIGINT/SIGTERM cover a terminal. They are not enough for a supervisor:
|
|
180
|
+
* on Windows `child.kill()` terminates the process outright and no handler
|
|
181
|
+
* runs, so the device lease and the hdc port forward are never released —
|
|
182
|
+
* twenty abandoned forwards filled 8012..8031 and the next run could not
|
|
183
|
+
* open the tunnel. Closing the child's stdin is a signal every platform
|
|
184
|
+
* delivers, so it is honoured as the same request to stop.
|
|
185
|
+
*/
|
|
186
|
+
export function waitForStop(io) {
|
|
187
|
+
io.stdin.resume();
|
|
188
|
+
return new Promise((resolve) => {
|
|
189
|
+
let stopped = false;
|
|
190
|
+
const stop = () => {
|
|
191
|
+
if (stopped)
|
|
192
|
+
return;
|
|
193
|
+
stopped = true;
|
|
194
|
+
resolve();
|
|
195
|
+
};
|
|
196
|
+
io.signals.once("SIGINT", stop);
|
|
197
|
+
io.signals.once("SIGTERM", stop);
|
|
198
|
+
io.stdin.on("end", stop);
|
|
199
|
+
io.stdin.on("close", stop);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
export async function consoleMain(argv) {
|
|
203
|
+
const parsed = parseArgv(argv);
|
|
204
|
+
if (parsed.help) {
|
|
205
|
+
process.stdout.write("usage: uitest-console [--device id] [--state-dir dir] [--port 7042] [--host 127.0.0.1]\n" +
|
|
206
|
+
" [--transport grpc|ws|mjpeg] [--privacy on|off] [--no-open]\n" +
|
|
207
|
+
" [--task console] [--record file.mp4] [--no-layout]\n" +
|
|
208
|
+
" [--scale 1..5] [--fps 15|30|60|120] [--cast-port 5000]\n" +
|
|
209
|
+
" [--ws-extension path]\n\n" +
|
|
210
|
+
"Opens the phone's screen in a browser window: mirror, node table, takeover,\n" +
|
|
211
|
+
"and the approvals waiting for a person. Watching does not take the device\n" +
|
|
212
|
+
"from an agent; Hold does, and Resume hands it back.\n\n" +
|
|
213
|
+
"The server listens on 127.0.0.1 unless --host says otherwise; anyone who\n" +
|
|
214
|
+
"can reach the port can watch and drive the phone.\n");
|
|
215
|
+
return 0;
|
|
216
|
+
}
|
|
217
|
+
if (parsed.command) {
|
|
218
|
+
process.stderr.write(`error: uitest-console takes flags, not a command (got "${parsed.command}")\n`);
|
|
219
|
+
return 2;
|
|
220
|
+
}
|
|
221
|
+
let opts;
|
|
222
|
+
try {
|
|
223
|
+
opts = consoleOptionsFrom(argv);
|
|
224
|
+
}
|
|
225
|
+
catch (e) {
|
|
226
|
+
process.stderr.write(`error: ${e instanceof Error ? e.message : String(e)}\n`);
|
|
227
|
+
return 2;
|
|
228
|
+
}
|
|
229
|
+
let run;
|
|
230
|
+
try {
|
|
231
|
+
run = await startConsole(opts);
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
process.stderr.write(`error: ${e instanceof Error ? e.message : String(e)}\n`);
|
|
235
|
+
return 1;
|
|
236
|
+
}
|
|
237
|
+
process.stdout.write(`uitest-console ${VERSION} · ${run.url} · device ${run.tag}\n` +
|
|
238
|
+
` privacy ${opts.privacy ? "on" : "off"} · hold pauses agents, resume hands the phone back\n` +
|
|
239
|
+
` Ctrl-C closes the page\n`);
|
|
240
|
+
if (opts.open)
|
|
241
|
+
openWindow(run.url);
|
|
242
|
+
await waitForStop({ stdin: process.stdin, signals: process });
|
|
243
|
+
await run.close().catch(() => undefined);
|
|
244
|
+
return 0;
|
|
245
|
+
}
|
|
246
|
+
/** Exported for tests: the hooks the page talks to, without a device. */
|
|
247
|
+
export { consoleHooks };
|
package/dist/page.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The console's own markup and script, appended to core's player page.
|
|
3
|
+
*
|
|
4
|
+
* Everything the console adds is a product layer: the stream, the node table,
|
|
5
|
+
* the privacy switch and the takeover arbitration all stay in core's page and
|
|
6
|
+
* server. This file adds the two things only a person-facing product has — the
|
|
7
|
+
* approval inbox and an explicit hold/resume button — plus a read-only view of
|
|
8
|
+
* what the agent has been doing.
|
|
9
|
+
*
|
|
10
|
+
* Rows are built by the same functions the tests drive: they are serialised
|
|
11
|
+
* into the page so there is one implementation of "how an approval row looks"
|
|
12
|
+
* and one place that escapes device-side text.
|
|
13
|
+
*/
|
|
14
|
+
export interface ApprovalRow {
|
|
15
|
+
id: string;
|
|
16
|
+
/** The 8-character device hash the approval is filed under. */
|
|
17
|
+
tag: string;
|
|
18
|
+
command: string;
|
|
19
|
+
bundle?: string;
|
|
20
|
+
task?: string;
|
|
21
|
+
rule?: string;
|
|
22
|
+
status: string;
|
|
23
|
+
expiresInMs: number;
|
|
24
|
+
}
|
|
25
|
+
export interface ActivityRow {
|
|
26
|
+
at: string;
|
|
27
|
+
step: number;
|
|
28
|
+
command: string;
|
|
29
|
+
status: string;
|
|
30
|
+
text: string;
|
|
31
|
+
}
|
|
32
|
+
/** Escapes text for interpolation into markup. Device text is untrusted. */
|
|
33
|
+
export declare function escapeHtml(text: string): string;
|
|
34
|
+
/** `42000` → `42s`; a minute or more reads `1m05s`. */
|
|
35
|
+
export declare function formatSeconds(ms: number): string;
|
|
36
|
+
/** One approval: what it is, where, how long is left, and the two answers. */
|
|
37
|
+
export declare function renderApprovalRow(row: ApprovalRow): string;
|
|
38
|
+
/** One recorded step. Text is the CLI's own output, escaped. */
|
|
39
|
+
export declare function renderActivityRow(row: ActivityRow): string;
|
|
40
|
+
/**
|
|
41
|
+
* The page's markup and styles.
|
|
42
|
+
*
|
|
43
|
+
* The viewer's stylesheet is loaded on this page too, and it defines a global
|
|
44
|
+
* `.row { display: flex }` for its node table. The console's rows are three
|
|
45
|
+
* stacked blocks (head, meta, actions), so they are reset to `display: block`
|
|
46
|
+
* below — as flex children they sat side by side and the meta line was
|
|
47
|
+
* squeezed to one character per line, which an acceptance screenshot caught.
|
|
48
|
+
*/
|
|
49
|
+
export declare const CONSOLE_HTML = "\n<style>\n #console-panel {\n position: fixed; left: 14px; top: 56px; width: 360px; max-height: calc(100% - 90px);\n display: flex; flex-direction: column; background: var(--panel);\n border: 1px solid var(--line); border-radius: 8px; overflow: hidden;\n box-shadow: 0 8px 30px rgba(0,0,0,.45); z-index: 5;\n }\n #console-panel header {\n display: flex; align-items: center; gap: 8px; padding: 8px 10px;\n background: #1c2128; border-bottom: 1px solid var(--line); font-weight: 600;\n }\n #console-panel header .spacer { flex: 1; }\n #console-panel section { border-bottom: 1px solid var(--line); min-height: 0; }\n #console-panel h3 {\n margin: 0; padding: 6px 10px; font-size: 11px; letter-spacing: .6px;\n text-transform: uppercase; color: var(--dim); border-bottom: 1px solid #21262d;\n }\n #console-panel .list { max-height: 200px; overflow: auto; }\n #console-panel .row { display: block; padding: 7px 10px; border-bottom: 1px solid #21262d; }\n #console-panel .row:last-child { border-bottom: 0; }\n #console-panel .head { display: flex; gap: 6px; align-items: baseline; }\n #console-panel .head code { color: var(--accent); font-size: 11px; }\n #console-panel .rule { color: var(--warn); font-size: 11px; }\n #console-panel .status { color: var(--dim); font-size: 11px; }\n #console-panel .meta { color: var(--dim); font-size: 11px; margin-top: 2px; word-break: break-word; }\n #console-panel .actions { display: flex; gap: 6px; margin-top: 6px; }\n #console-panel .actions select {\n background: var(--bg); color: var(--ink); border: 1px solid var(--line);\n border-radius: 5px; padding: 3px 6px; font: inherit; font-size: 11px;\n }\n #console-panel .actions button { padding: 3px 9px; font-size: 11px; }\n #console-panel .empty { padding: 10px; color: var(--dim); font-size: 12px; }\n #held-banner {\n display: none; padding: 7px 10px; background: rgba(248,81,73,.16);\n color: var(--bad); font-weight: 600; font-size: 12px;\n }\n #held-banner.on { display: block; }\n #hold.on { border-color: var(--bad); color: var(--bad); }\n @media (max-width: 900px) {\n #console-panel { display: none; }\n }\n</style>\n<div id=\"console-panel\" aria-label=\"Console\">\n <header><span>uitest-console</span><span class=\"spacer\"></span>\n <button id=\"hold\" title=\"Take the phone; agent commands stop with lease_held\">Hold</button>\n </header>\n <div id=\"held-banner\">You have the phone \u00B7 agents are paused</div>\n <section id=\"inbox\">\n <h3>Approvals waiting for you</h3>\n <div id=\"approvals-list\" class=\"list\"><div class=\"empty\">none</div></div>\n </section>\n <section id=\"activity\">\n <h3>Recent activity</h3>\n <div id=\"activity-list\" class=\"list\"><div class=\"empty\">none</div></div>\n </section>\n</div>\n";
|
|
50
|
+
export declare const CONSOLE_SCRIPT: string;
|
package/dist/page.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The console's own markup and script, appended to core's player page.
|
|
3
|
+
*
|
|
4
|
+
* Everything the console adds is a product layer: the stream, the node table,
|
|
5
|
+
* the privacy switch and the takeover arbitration all stay in core's page and
|
|
6
|
+
* server. This file adds the two things only a person-facing product has — the
|
|
7
|
+
* approval inbox and an explicit hold/resume button — plus a read-only view of
|
|
8
|
+
* what the agent has been doing.
|
|
9
|
+
*
|
|
10
|
+
* Rows are built by the same functions the tests drive: they are serialised
|
|
11
|
+
* into the page so there is one implementation of "how an approval row looks"
|
|
12
|
+
* and one place that escapes device-side text.
|
|
13
|
+
*/
|
|
14
|
+
/** Escapes text for interpolation into markup. Device text is untrusted. */
|
|
15
|
+
export function escapeHtml(text) {
|
|
16
|
+
return String(text ?? "")
|
|
17
|
+
.replace(/&/g, "&")
|
|
18
|
+
.replace(/</g, "<")
|
|
19
|
+
.replace(/>/g, ">")
|
|
20
|
+
.replace(/"/g, """);
|
|
21
|
+
}
|
|
22
|
+
/** `42000` → `42s`; a minute or more reads `1m05s`. */
|
|
23
|
+
export function formatSeconds(ms) {
|
|
24
|
+
const total = Math.max(0, Math.round(Number(ms) / 1000));
|
|
25
|
+
if (total < 60)
|
|
26
|
+
return `${total}s`;
|
|
27
|
+
const rest = total % 60;
|
|
28
|
+
return `${Math.floor(total / 60)}m${rest ? String(rest).padStart(2, "0") : ""}s`;
|
|
29
|
+
}
|
|
30
|
+
/** One approval: what it is, where, how long is left, and the two answers. */
|
|
31
|
+
export function renderApprovalRow(row) {
|
|
32
|
+
const where = [row.bundle || "unknown app", row.task ? `task ${row.task}` : ""]
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.join(" · ");
|
|
35
|
+
// An answered request keeps its row so the person sees their answer land,
|
|
36
|
+
// but the answers and the countdown go: the buttons would suggest it is
|
|
37
|
+
// still open, and "expires in 42s" is about a window that no longer matters.
|
|
38
|
+
const open = row.status === "pending";
|
|
39
|
+
const tail = open
|
|
40
|
+
? ` · expires in ${formatSeconds(row.expiresInMs)}`
|
|
41
|
+
: "";
|
|
42
|
+
return (`<div class="row" data-id="${escapeHtml(row.id)}">` +
|
|
43
|
+
`<div class="head"><code>${escapeHtml(row.id)}</code> ` +
|
|
44
|
+
`<b>${escapeHtml(row.command)}</b> ` +
|
|
45
|
+
`<span class="rule">${escapeHtml(row.rule ?? "-")}</span> ` +
|
|
46
|
+
`<span class="status">${escapeHtml(row.status)}</span></div>` +
|
|
47
|
+
`<div class="meta">${escapeHtml(where)} · device ${escapeHtml(row.tag)}${tail}</div>` +
|
|
48
|
+
(open
|
|
49
|
+
? `<div class="actions">` +
|
|
50
|
+
`<select data-scope aria-label="How long this answer lasts">` +
|
|
51
|
+
`<option value="once">once</option><option value="task">this task</option>` +
|
|
52
|
+
`<option value="always">always</option></select>` +
|
|
53
|
+
`<button data-allow>Allow</button><button data-deny>Deny</button></div>`
|
|
54
|
+
: "") +
|
|
55
|
+
`</div>`);
|
|
56
|
+
}
|
|
57
|
+
/** One recorded step. Text is the CLI's own output, escaped. */
|
|
58
|
+
export function renderActivityRow(row) {
|
|
59
|
+
return (`<div class="row">` +
|
|
60
|
+
`<div class="head"><b>${escapeHtml(row.command)}</b> ` +
|
|
61
|
+
`<span class="rule">${escapeHtml(row.status)}</span> ` +
|
|
62
|
+
`<span class="meta">step ${escapeHtml(String(row.step))}</span></div>` +
|
|
63
|
+
`<div class="meta">${escapeHtml(row.text).slice(0, 400)}</div>` +
|
|
64
|
+
`</div>`);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The row builders as page source.
|
|
68
|
+
*
|
|
69
|
+
* Serialising the functions keeps the tested implementation and the shipped
|
|
70
|
+
* one identical: a second copy inside a template string is the copy that
|
|
71
|
+
* forgets an escape.
|
|
72
|
+
*/
|
|
73
|
+
const CLIENT_ROWS = [escapeHtml, formatSeconds, renderApprovalRow, renderActivityRow]
|
|
74
|
+
.map((fn) => fn.toString())
|
|
75
|
+
.join("\n");
|
|
76
|
+
/**
|
|
77
|
+
* The page's markup and styles.
|
|
78
|
+
*
|
|
79
|
+
* The viewer's stylesheet is loaded on this page too, and it defines a global
|
|
80
|
+
* `.row { display: flex }` for its node table. The console's rows are three
|
|
81
|
+
* stacked blocks (head, meta, actions), so they are reset to `display: block`
|
|
82
|
+
* below — as flex children they sat side by side and the meta line was
|
|
83
|
+
* squeezed to one character per line, which an acceptance screenshot caught.
|
|
84
|
+
*/
|
|
85
|
+
export const CONSOLE_HTML = `
|
|
86
|
+
<style>
|
|
87
|
+
#console-panel {
|
|
88
|
+
position: fixed; left: 14px; top: 56px; width: 360px; max-height: calc(100% - 90px);
|
|
89
|
+
display: flex; flex-direction: column; background: var(--panel);
|
|
90
|
+
border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
|
|
91
|
+
box-shadow: 0 8px 30px rgba(0,0,0,.45); z-index: 5;
|
|
92
|
+
}
|
|
93
|
+
#console-panel header {
|
|
94
|
+
display: flex; align-items: center; gap: 8px; padding: 8px 10px;
|
|
95
|
+
background: #1c2128; border-bottom: 1px solid var(--line); font-weight: 600;
|
|
96
|
+
}
|
|
97
|
+
#console-panel header .spacer { flex: 1; }
|
|
98
|
+
#console-panel section { border-bottom: 1px solid var(--line); min-height: 0; }
|
|
99
|
+
#console-panel h3 {
|
|
100
|
+
margin: 0; padding: 6px 10px; font-size: 11px; letter-spacing: .6px;
|
|
101
|
+
text-transform: uppercase; color: var(--dim); border-bottom: 1px solid #21262d;
|
|
102
|
+
}
|
|
103
|
+
#console-panel .list { max-height: 200px; overflow: auto; }
|
|
104
|
+
#console-panel .row { display: block; padding: 7px 10px; border-bottom: 1px solid #21262d; }
|
|
105
|
+
#console-panel .row:last-child { border-bottom: 0; }
|
|
106
|
+
#console-panel .head { display: flex; gap: 6px; align-items: baseline; }
|
|
107
|
+
#console-panel .head code { color: var(--accent); font-size: 11px; }
|
|
108
|
+
#console-panel .rule { color: var(--warn); font-size: 11px; }
|
|
109
|
+
#console-panel .status { color: var(--dim); font-size: 11px; }
|
|
110
|
+
#console-panel .meta { color: var(--dim); font-size: 11px; margin-top: 2px; word-break: break-word; }
|
|
111
|
+
#console-panel .actions { display: flex; gap: 6px; margin-top: 6px; }
|
|
112
|
+
#console-panel .actions select {
|
|
113
|
+
background: var(--bg); color: var(--ink); border: 1px solid var(--line);
|
|
114
|
+
border-radius: 5px; padding: 3px 6px; font: inherit; font-size: 11px;
|
|
115
|
+
}
|
|
116
|
+
#console-panel .actions button { padding: 3px 9px; font-size: 11px; }
|
|
117
|
+
#console-panel .empty { padding: 10px; color: var(--dim); font-size: 12px; }
|
|
118
|
+
#held-banner {
|
|
119
|
+
display: none; padding: 7px 10px; background: rgba(248,81,73,.16);
|
|
120
|
+
color: var(--bad); font-weight: 600; font-size: 12px;
|
|
121
|
+
}
|
|
122
|
+
#held-banner.on { display: block; }
|
|
123
|
+
#hold.on { border-color: var(--bad); color: var(--bad); }
|
|
124
|
+
@media (max-width: 900px) {
|
|
125
|
+
#console-panel { display: none; }
|
|
126
|
+
}
|
|
127
|
+
</style>
|
|
128
|
+
<div id="console-panel" aria-label="Console">
|
|
129
|
+
<header><span>uitest-console</span><span class="spacer"></span>
|
|
130
|
+
<button id="hold" title="Take the phone; agent commands stop with lease_held">Hold</button>
|
|
131
|
+
</header>
|
|
132
|
+
<div id="held-banner">You have the phone · agents are paused</div>
|
|
133
|
+
<section id="inbox">
|
|
134
|
+
<h3>Approvals waiting for you</h3>
|
|
135
|
+
<div id="approvals-list" class="list"><div class="empty">none</div></div>
|
|
136
|
+
</section>
|
|
137
|
+
<section id="activity">
|
|
138
|
+
<h3>Recent activity</h3>
|
|
139
|
+
<div id="activity-list" class="list"><div class="empty">none</div></div>
|
|
140
|
+
</section>
|
|
141
|
+
</div>
|
|
142
|
+
`;
|
|
143
|
+
export const CONSOLE_SCRIPT = `
|
|
144
|
+
(function () {
|
|
145
|
+
'use strict';
|
|
146
|
+
var viewer = window.uitestViewer;
|
|
147
|
+
if (!viewer) return;
|
|
148
|
+
|
|
149
|
+
${CLIENT_ROWS}
|
|
150
|
+
|
|
151
|
+
var approvalsEl = document.getElementById('approvals-list');
|
|
152
|
+
var activityEl = document.getElementById('activity-list');
|
|
153
|
+
var holdEl = document.getElementById('hold');
|
|
154
|
+
var heldEl = document.getElementById('held-banner');
|
|
155
|
+
var held = false;
|
|
156
|
+
|
|
157
|
+
function ask() {
|
|
158
|
+
viewer.send({ type: 'console:approvals:list' });
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function renderApprovals(items) {
|
|
162
|
+
if (!items || !items.length) {
|
|
163
|
+
approvalsEl.innerHTML = '<div class="empty">none</div>';
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
approvalsEl.innerHTML = items.map(renderApprovalRow).join('');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function renderActivity(items) {
|
|
170
|
+
if (!items || !items.length) {
|
|
171
|
+
activityEl.innerHTML = '<div class="empty">none</div>';
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
activityEl.innerHTML = items.slice(-20).reverse().map(renderActivityRow).join('');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function setHeld(on) {
|
|
178
|
+
held = !!on;
|
|
179
|
+
heldEl.className = held ? 'on' : '';
|
|
180
|
+
holdEl.textContent = held ? 'Resume' : 'Hold';
|
|
181
|
+
holdEl.className = held ? 'on' : '';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
approvalsEl.addEventListener('click', function (event) {
|
|
185
|
+
var target = event.target;
|
|
186
|
+
if (!target || !target.hasAttribute) return;
|
|
187
|
+
var allow = target.hasAttribute('data-allow');
|
|
188
|
+
var deny = target.hasAttribute('data-deny');
|
|
189
|
+
if (!allow && !deny) return;
|
|
190
|
+
var row = target.closest('[data-id]');
|
|
191
|
+
if (!row) return;
|
|
192
|
+
var select = row.querySelector('[data-scope]');
|
|
193
|
+
viewer.send({
|
|
194
|
+
type: 'console:approvals:decide',
|
|
195
|
+
id: row.getAttribute('data-id'),
|
|
196
|
+
decision: allow ? 'allow' : 'deny',
|
|
197
|
+
scope: select ? select.value : 'once'
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
holdEl.addEventListener('click', function () {
|
|
202
|
+
viewer.send({ type: held ? 'console:resume' : 'console:hold' });
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
viewer.on('console:approvals', function (msg) { renderApprovals(msg.items); });
|
|
206
|
+
viewer.on('console:activity', function (msg) { renderActivity(msg.items); });
|
|
207
|
+
viewer.on('console:held', function (msg) { setHeld(msg.held); });
|
|
208
|
+
viewer.on('console:error', function (msg) {
|
|
209
|
+
var text = String(msg.message || 'console error');
|
|
210
|
+
approvalsEl.insertAdjacentHTML('afterbegin', '<div class="empty">' + escapeHtml(text) + '</div>');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// Poll while idle: an agent in another process can be gated at any moment,
|
|
214
|
+
// and the person watching this page should see the request without touching
|
|
215
|
+
// anything. Two seconds is fast enough to answer inside the 60 s window.
|
|
216
|
+
setInterval(function () {
|
|
217
|
+
ask();
|
|
218
|
+
viewer.send({ type: 'console:activity' });
|
|
219
|
+
}, 2000);
|
|
220
|
+
ask();
|
|
221
|
+
viewer.send({ type: 'console:activity' });
|
|
222
|
+
})();
|
|
223
|
+
`;
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ViewerHooks, ViewerSource, ViewerServer } from "uitest-sdk/viewer";
|
|
2
|
+
import type { ApprovalDecision, ApprovalScope, DecisionResult, PendingApproval, HoldResult, TraceRecord } from "uitest-sdk/govern";
|
|
3
|
+
/** The person-side half of core's approval gateway. */
|
|
4
|
+
export interface ApprovalStore {
|
|
5
|
+
pending(): PendingApproval[];
|
|
6
|
+
decide(id: string, decision: ApprovalDecision, opts?: {
|
|
7
|
+
by?: "cli" | "console";
|
|
8
|
+
scope?: ApprovalScope;
|
|
9
|
+
reason?: string;
|
|
10
|
+
}): DecisionResult;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* What the console asks core for, injected so the hooks can be tested without
|
|
14
|
+
* a device: the approval store, the two lease writes, and the trace reader.
|
|
15
|
+
*/
|
|
16
|
+
export interface ConsoleDeps {
|
|
17
|
+
approvals: ApprovalStore;
|
|
18
|
+
stateDir: string;
|
|
19
|
+
/** This console session's device tag, so a hold names the right device. */
|
|
20
|
+
tag: string;
|
|
21
|
+
hold(): Promise<HoldResult>;
|
|
22
|
+
resume(): Promise<boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* Whether a person holds the device right now.
|
|
25
|
+
*
|
|
26
|
+
* Read from the lease rather than remembered here: the hold may have been
|
|
27
|
+
* written by `uitest-phone hold` before this console started, and it may be
|
|
28
|
+
* released from another shell while the page is open.
|
|
29
|
+
*/
|
|
30
|
+
isHeld(): Promise<boolean>;
|
|
31
|
+
activity(): Promise<TraceRecord[]>;
|
|
32
|
+
}
|
|
33
|
+
export interface ConsoleOptions extends ConsoleDeps {
|
|
34
|
+
source: ViewerSource;
|
|
35
|
+
port?: number;
|
|
36
|
+
host?: string;
|
|
37
|
+
privacy?: boolean;
|
|
38
|
+
/** What `--record <file>` asked for; omitted means a timestamped name. */
|
|
39
|
+
recordPath?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The console's protocol over the viewer socket.
|
|
43
|
+
*
|
|
44
|
+
* The viewer answers its own message types first, so nothing here can shadow
|
|
45
|
+
* streaming, input or privacy. Everything a person does to *governance* goes
|
|
46
|
+
* through this hook, which means the console never invents a second path to
|
|
47
|
+
* the lease or the approval files: it calls the same core functions the CLI
|
|
48
|
+
* does.
|
|
49
|
+
*/
|
|
50
|
+
export declare function consoleHooks(deps: ConsoleDeps): ViewerHooks;
|
|
51
|
+
/** Starts the console page on top of core's viewer server. */
|
|
52
|
+
export declare function createConsoleServer(opts: ConsoleOptions): Promise<ViewerServer>;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { renderPlayerPage, startViewerServer } from "uitest-sdk/viewer";
|
|
2
|
+
import { CONSOLE_HTML, CONSOLE_SCRIPT } from "./page.js";
|
|
3
|
+
function approvalItems(approvals) {
|
|
4
|
+
const now = Date.now();
|
|
5
|
+
return approvals.pending().map((entry) => ({
|
|
6
|
+
...entry,
|
|
7
|
+
expiresInMs: Math.max(0, entry.expiresAt - now),
|
|
8
|
+
}));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The console's protocol over the viewer socket.
|
|
12
|
+
*
|
|
13
|
+
* The viewer answers its own message types first, so nothing here can shadow
|
|
14
|
+
* streaming, input or privacy. Everything a person does to *governance* goes
|
|
15
|
+
* through this hook, which means the console never invents a second path to
|
|
16
|
+
* the lease or the approval files: it calls the same core functions the CLI
|
|
17
|
+
* does.
|
|
18
|
+
*/
|
|
19
|
+
export function consoleHooks(deps) {
|
|
20
|
+
const list = (client) => {
|
|
21
|
+
client.send({ type: "console:approvals", items: approvalItems(deps.approvals) });
|
|
22
|
+
};
|
|
23
|
+
const fail = (client, message) => {
|
|
24
|
+
client.send({ type: "console:error", message });
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
onClient(client) {
|
|
28
|
+
// A tab that joins sees the current inbox and activity without waiting
|
|
29
|
+
// for its first poll: an approval can expire in 60 s.
|
|
30
|
+
list(client);
|
|
31
|
+
void deps.activity().then((items) => client.send({ type: "console:activity", items }), () => undefined);
|
|
32
|
+
// And the lease state: the button is a toggle, so a tab that opens while
|
|
33
|
+
// a hold is in place must start at "Resume". Without this its first click
|
|
34
|
+
// holds the device again and the person cannot hand it back.
|
|
35
|
+
void deps.isHeld().then((held) => client.send({ type: "console:held", held }), () => undefined);
|
|
36
|
+
},
|
|
37
|
+
onMessage(client, message) {
|
|
38
|
+
switch (message.type) {
|
|
39
|
+
case "console:approvals:list":
|
|
40
|
+
list(client);
|
|
41
|
+
return true;
|
|
42
|
+
case "console:approvals:decide": {
|
|
43
|
+
const id = String(message.id ?? "");
|
|
44
|
+
const decision = message.decision === "allow" ? "allow" : "deny";
|
|
45
|
+
const scope = message.scope === "task" || message.scope === "always" ? message.scope : "once";
|
|
46
|
+
const result = deps.approvals.decide(id, decision, {
|
|
47
|
+
by: "console",
|
|
48
|
+
...(decision === "allow" ? { scope } : {}),
|
|
49
|
+
...(typeof message.reason === "string" && message.reason
|
|
50
|
+
? { reason: message.reason }
|
|
51
|
+
: {}),
|
|
52
|
+
});
|
|
53
|
+
if (result.kind === "unknown")
|
|
54
|
+
fail(client, result.reason ?? "unknown approval id");
|
|
55
|
+
else if (result.kind === "expired")
|
|
56
|
+
fail(client, result.reason ?? "approval expired");
|
|
57
|
+
else if (result.kind === "already-used")
|
|
58
|
+
fail(client, result.reason ?? "already used");
|
|
59
|
+
else if (result.kind === "denied" && decision === "allow") {
|
|
60
|
+
fail(client, result.reason ?? "already decided");
|
|
61
|
+
}
|
|
62
|
+
list(client);
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
case "console:hold":
|
|
66
|
+
void deps.hold().then((result) => {
|
|
67
|
+
client.send({
|
|
68
|
+
type: "console:held",
|
|
69
|
+
held: true,
|
|
70
|
+
...(result.tookOverFrom ? { tookOverFrom: result.tookOverFrom } : {}),
|
|
71
|
+
});
|
|
72
|
+
}, (e) => fail(client, e instanceof Error ? e.message : String(e)));
|
|
73
|
+
return true;
|
|
74
|
+
case "console:resume":
|
|
75
|
+
void deps.resume().then(() => client.send({ type: "console:held", held: false }), (e) => fail(client, e instanceof Error ? e.message : String(e)));
|
|
76
|
+
return true;
|
|
77
|
+
case "console:activity":
|
|
78
|
+
void deps.activity().then((items) => client.send({ type: "console:activity", items }), (e) => fail(client, e instanceof Error ? e.message : String(e)));
|
|
79
|
+
return true;
|
|
80
|
+
default:
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Starts the console page on top of core's viewer server. */
|
|
87
|
+
export async function createConsoleServer(opts) {
|
|
88
|
+
return startViewerServer({
|
|
89
|
+
port: opts.port ?? 7042,
|
|
90
|
+
host: opts.host ?? "127.0.0.1",
|
|
91
|
+
privacy: opts.privacy ?? true,
|
|
92
|
+
source: opts.source,
|
|
93
|
+
page: renderPlayerPage({
|
|
94
|
+
title: "uitest-console",
|
|
95
|
+
extraHtml: CONSOLE_HTML,
|
|
96
|
+
extraScript: CONSOLE_SCRIPT,
|
|
97
|
+
}),
|
|
98
|
+
hooks: consoleHooks(opts),
|
|
99
|
+
});
|
|
100
|
+
}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { VERSION as CORE_VERSION } from "uitest-sdk";
|
|
3
|
+
function readVersion() {
|
|
4
|
+
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
5
|
+
if (typeof pkg.version !== "string" || !pkg.version) {
|
|
6
|
+
throw new Error("uitest-console: package.json has no version");
|
|
7
|
+
}
|
|
8
|
+
return pkg.version;
|
|
9
|
+
}
|
|
10
|
+
/** This product's version, independent of the core library it runs on. */
|
|
11
|
+
export const VERSION = readVersion();
|
|
12
|
+
export { CORE_VERSION };
|
package/dist/window.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Opens the console as a browser *app* window, or prints the URL.
|
|
3
|
+
*
|
|
4
|
+
* A tab brings the address bar, the tab strip and every other tab with it; the
|
|
5
|
+
* mirror then looks like a web page rather than a device window. Edge and
|
|
6
|
+
* Chrome both take `--app=<url>`, which is the cheapest window a product can
|
|
7
|
+
* have without shipping a native shell (spec §6.2 defers that shell).
|
|
8
|
+
*/
|
|
9
|
+
export type Browser = "edge" | "chrome";
|
|
10
|
+
export interface WindowCommand {
|
|
11
|
+
command: string;
|
|
12
|
+
args: string[];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The command line for one browser.
|
|
16
|
+
*
|
|
17
|
+
* Windows paths are the default because that is where the device workbench is;
|
|
18
|
+
* the macOS and Linux branches name the same binaries, since both browsers
|
|
19
|
+
* install a shell wrapper there.
|
|
20
|
+
*/
|
|
21
|
+
export declare function appWindowCommand(browser: Browser, url: string, platform?: NodeJS.Platform): WindowCommand;
|
|
22
|
+
export interface OpenWindowOptions {
|
|
23
|
+
/** Which browser to try first. Defaults to Edge, then Chrome. */
|
|
24
|
+
browser?: Browser;
|
|
25
|
+
platform?: NodeJS.Platform;
|
|
26
|
+
spawn?: (command: string, args: string[]) => unknown;
|
|
27
|
+
print?: (line: string) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Tries the browsers in order and reports whether one started.
|
|
31
|
+
*
|
|
32
|
+
* `spawn` can throw synchronously (`ENOENT` on a bench without a browser) or
|
|
33
|
+
* emit `error` afterwards; both are handled, because either one means the
|
|
34
|
+
* person has to open the printed URL themselves.
|
|
35
|
+
*/
|
|
36
|
+
export declare function openWindow(url: string, opts?: OpenWindowOptions): {
|
|
37
|
+
opened: boolean;
|
|
38
|
+
command?: string;
|
|
39
|
+
};
|
package/dist/window.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
const WINDOW_SIZE = "1100,900";
|
|
3
|
+
/**
|
|
4
|
+
* The command line for one browser.
|
|
5
|
+
*
|
|
6
|
+
* Windows paths are the default because that is where the device workbench is;
|
|
7
|
+
* the macOS and Linux branches name the same binaries, since both browsers
|
|
8
|
+
* install a shell wrapper there.
|
|
9
|
+
*/
|
|
10
|
+
export function appWindowCommand(browser, url, platform = process.platform) {
|
|
11
|
+
const win = platform === "win32";
|
|
12
|
+
if (browser === "edge") {
|
|
13
|
+
return {
|
|
14
|
+
command: win ? "msedge.exe" : "microsoft-edge",
|
|
15
|
+
args: [`--app=${url}`, `--window-size=${WINDOW_SIZE}`],
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
command: win ? "chrome.exe" : "google-chrome",
|
|
20
|
+
args: [`--app=${url}`, `--window-size=${WINDOW_SIZE}`],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Tries the browsers in order and reports whether one started.
|
|
25
|
+
*
|
|
26
|
+
* `spawn` can throw synchronously (`ENOENT` on a bench without a browser) or
|
|
27
|
+
* emit `error` afterwards; both are handled, because either one means the
|
|
28
|
+
* person has to open the printed URL themselves.
|
|
29
|
+
*/
|
|
30
|
+
export function openWindow(url, opts = {}) {
|
|
31
|
+
const platform = opts.platform ?? process.platform;
|
|
32
|
+
const spawnFn = opts.spawn ??
|
|
33
|
+
((command, args) => {
|
|
34
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore" });
|
|
35
|
+
child.on("error", () => undefined);
|
|
36
|
+
child.unref();
|
|
37
|
+
return child;
|
|
38
|
+
});
|
|
39
|
+
const print = opts.print ?? ((line) => process.stdout.write(`${line}\n`));
|
|
40
|
+
const order = opts.browser ? [opts.browser] : ["edge", "chrome"];
|
|
41
|
+
for (const browser of order) {
|
|
42
|
+
const { command, args } = appWindowCommand(browser, url, platform);
|
|
43
|
+
try {
|
|
44
|
+
spawnFn(command, args);
|
|
45
|
+
return { opened: true, command };
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Try the next browser.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
print(`open ${url} in a browser (no Edge or Chrome found)`);
|
|
52
|
+
return { opened: false };
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uitest-console",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Local web console for a HarmonyOS device: mirror, inspect, takeover and the approval inbox",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/growvv/uitest-sdk.git",
|
|
9
|
+
"directory": "packages/console"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://growvv.github.io/uitest-sdk/",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"harmonyos",
|
|
14
|
+
"openharmony",
|
|
15
|
+
"uitest",
|
|
16
|
+
"mirror",
|
|
17
|
+
"console",
|
|
18
|
+
"takeover",
|
|
19
|
+
"approvals"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"bin": {
|
|
23
|
+
"uitest-console": "dist/bin.js"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"NOTICE",
|
|
28
|
+
"CHANGELOG.md",
|
|
29
|
+
"SECURITY.md",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsc",
|
|
38
|
+
"prepublishOnly": "npm run build && npm --prefix ../.. test",
|
|
39
|
+
"test": "vitest run",
|
|
40
|
+
"typecheck": "tsc --noEmit",
|
|
41
|
+
"typecheck:tests": "tsc -p tsconfig.test.json"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"uitest-sdk": "~0.10.0"
|
|
45
|
+
}
|
|
46
|
+
}
|