dsh-annotator 0.0.0-stage → 0.5.4
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 +159 -2
- package/cordis.patch.yml +10 -0
- package/lib/cdp.js +218 -0
- package/lib/client.js +1911 -0
- package/lib/index.js +447 -0
- package/lib/overlay.js +862 -0
- package/lib/session.js +1745 -0
- package/lib/shared/binding-action.js +26 -0
- package/lib/shared/browsers.js +126 -0
- package/lib/shared/cdp-endpoint.js +70 -0
- package/lib/shared/ego.js +68 -0
- package/lib/shared/embedded-runtime.js +53 -0
- package/lib/shared/gui-page.js +14 -0
- package/lib/shared/host-rev.js +120 -0
- package/lib/shared/pick-actions.js +21 -0
- package/lib/shared/pick-dedupe.js +26 -0
- package/lib/shared/png.js +173 -0
- package/lib/shared/scratch-dir.js +57 -0
- package/lib/shared/spaces.js +54 -0
- package/lib/shared/tabs.js +45 -0
- package/lib/shared/tool-schema.js +72 -0
- package/lib/shared/url.js +33 -0
- package/package.json +52 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Classify a JSON payload the page overlay reports through the CDP binding.
|
|
3
|
+
* Pure on purpose: the host dispatch in session.js and the unit tests share
|
|
4
|
+
* this one source of truth, and junk from the page must never reach a branch
|
|
5
|
+
* we did not design for.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param {unknown} payload parsed JSON from Runtime.bindingCalled
|
|
10
|
+
* @returns {'pick' | 'mode' | 'undo' | 'consume' | null} null for anything not actionable
|
|
11
|
+
*/
|
|
12
|
+
export function classifyBindingAction(payload) {
|
|
13
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) return null;
|
|
14
|
+
switch (payload.kind) {
|
|
15
|
+
case 'pick':
|
|
16
|
+
return 'pick';
|
|
17
|
+
case 'mode':
|
|
18
|
+
return payload.mode === 'pick' || payload.mode === 'off' ? 'mode' : null;
|
|
19
|
+
case 'undo':
|
|
20
|
+
return 'undo';
|
|
21
|
+
case 'consume':
|
|
22
|
+
return 'consume';
|
|
23
|
+
default:
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which browsers this plugin can drive, how to name them, and which one to
|
|
3
|
+
* offer first.
|
|
4
|
+
*
|
|
5
|
+
* Pure data + helpers only: executable discovery is fs/env work and stays in
|
|
6
|
+
* `lib/session.js`, so the host can import this module without side effects and
|
|
7
|
+
* a unit test can pin the registry. The panel does not import it — it receives
|
|
8
|
+
* `browserOrder` / `browserLabels` from the host snapshot, so both halves can
|
|
9
|
+
* never disagree about ids or display names.
|
|
10
|
+
*
|
|
11
|
+
* Why `ego`: ego lite (Citro Labs) is a Chromium fork that honours the same
|
|
12
|
+
* `--user-data-dir` + `--remote-debugging-port=0` contract as Chrome, so the
|
|
13
|
+
* existing CDP pipeline drives it unchanged.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** Stable ids, in default preference order. */
|
|
17
|
+
export const BROWSER_IDS = ['chrome', 'edge', 'ego'];
|
|
18
|
+
|
|
19
|
+
export const BROWSER_LABELS = {
|
|
20
|
+
chrome: 'Chrome',
|
|
21
|
+
edge: 'Edge',
|
|
22
|
+
ego: 'ego lite',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Env var that overrides the executable path for one browser. */
|
|
26
|
+
export const BROWSER_ENV = {
|
|
27
|
+
chrome: 'DSH_VISUAL_ANNOTATOR_CHROME',
|
|
28
|
+
edge: 'DSH_VISUAL_ANNOTATOR_EDGE',
|
|
29
|
+
ego: 'DSH_VISUAL_ANNOTATOR_EGO',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function isBrowserId(value) {
|
|
33
|
+
return typeof value === 'string' && BROWSER_IDS.includes(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Unknown / missing ids fall back (chrome by default) so old payloads keep working. */
|
|
37
|
+
export function normalizeBrowserId(value, fallback = 'chrome') {
|
|
38
|
+
return isBrowserId(value) ? value : fallback;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function browserLabel(value) {
|
|
42
|
+
return BROWSER_LABELS[normalizeBrowserId(value)];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* First registered browser that is actually installed, honouring an explicit
|
|
47
|
+
* preference when that browser is available too. `available` maps id -> path or
|
|
48
|
+
* null (the shape of `/state.browsers`). Returns null when nothing is installed.
|
|
49
|
+
*/
|
|
50
|
+
export function defaultBrowserId(available = {}, preferred = null) {
|
|
51
|
+
if (isBrowserId(preferred) && available?.[preferred]) return preferred;
|
|
52
|
+
for (const id of BROWSER_IDS) if (available?.[id]) return id;
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const SANDBOX_FALLBACK_ENV = 'DSH_VISUAL_ANNOTATOR_SANDBOX_FALLBACK';
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Decide whether a launch that just crashed should be retried once with
|
|
60
|
+
* `--no-sandbox`.
|
|
61
|
+
*
|
|
62
|
+
* Why this exists: when the host already runs inside a seatbelt/sandbox of its
|
|
63
|
+
* own (DSH Desktop's agent sandbox, CI sandboxes, some IDE terminals), Chrome's
|
|
64
|
+
* own sandbox cannot be installed and the *browser* process dies immediately
|
|
65
|
+
* with SIGTRAP. The CDP socket still completes its handshake and then closes,
|
|
66
|
+
* which surfaces far away as "chrome connection is closed". Chrome does start
|
|
67
|
+
* fine with `--no-sandbox` there, so the honest fix is: detect that exact
|
|
68
|
+
* signature, retry once, and say so — instead of making every caller remember
|
|
69
|
+
* an environment variable.
|
|
70
|
+
*
|
|
71
|
+
* The retry is deliberately narrow: only when we spawned the process ourselves,
|
|
72
|
+
* only once, only on a crash signature, and never for browsers that ship the
|
|
73
|
+
* flag already (ego lite passes its own `--no-sandbox`).
|
|
74
|
+
*/
|
|
75
|
+
export function shouldTrySandboxFallback({ alreadyNoSandbox, optOut, crashed } = {}) {
|
|
76
|
+
if (crashed !== true) return false;
|
|
77
|
+
if (alreadyNoSandbox === true) return false;
|
|
78
|
+
return optOut !== true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Installed ids, in preference order — handy for the panel's browser picker. */
|
|
82
|
+
export function availableBrowserIds(available = {}) {
|
|
83
|
+
return BROWSER_IDS.filter((id) => Boolean(available?.[id]));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 选择器要显示的全部浏览器(含没装的那些)。
|
|
88
|
+
*
|
|
89
|
+
* 面板早先只列「装了」的浏览器,结果这台机器没装 Edge 时,用户看到的选择器里
|
|
90
|
+
* 根本不存在 Edge,很自然会以为插件漏做了 Edge 支持。把未安装项也列出来(禁用 +
|
|
91
|
+
* 标注),「支持但没装」和「不支持」就分得清了。
|
|
92
|
+
*/
|
|
93
|
+
export function browserChoices(available = {}, order = BROWSER_IDS) {
|
|
94
|
+
return order.map((id) => ({
|
|
95
|
+
id,
|
|
96
|
+
label: BROWSER_LABELS[id] ?? id,
|
|
97
|
+
available: Boolean(available?.[id]),
|
|
98
|
+
}));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Env var holding extra Chromium switches for the spawned browser. */
|
|
102
|
+
export const BROWSER_ARGS_ENV = 'DSH_VISUAL_ANNOTATOR_BROWSER_ARGS';
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 是否允许接管用户已经开着的 ego lite(设 '0' 关掉)。
|
|
106
|
+
*
|
|
107
|
+
* 默认开:用户的 ego lite 里已经有登录态和书签,另起一个空 profile 的实例只会让
|
|
108
|
+
* 人再登一次。关掉的用处是测试要可预测、以及「我不想让插件碰我日常的浏览器」。
|
|
109
|
+
*/
|
|
110
|
+
export const ADOPT_RUNNING_ENV = 'DSH_VISUAL_ANNOTATOR_ADOPT_RUNNING';
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Extra command-line switches for every spawn, e.g. `--no-sandbox` when the host
|
|
114
|
+
* itself runs inside a sandbox that blocks Chrome's own sandbox (the renderer
|
|
115
|
+
* otherwise dies with SIGTRAP and the CDP socket closes right after handshake).
|
|
116
|
+
*
|
|
117
|
+
* Only `--flag` / `--flag=value` tokens survive: a stray word here would be read
|
|
118
|
+
* by Chromium as a URL to open, which is never what the caller meant.
|
|
119
|
+
*/
|
|
120
|
+
export function parseExtraArgs(raw) {
|
|
121
|
+
if (typeof raw !== 'string') return [];
|
|
122
|
+
return raw
|
|
123
|
+
.split(/[\s,]+/)
|
|
124
|
+
.map((item) => item.trim())
|
|
125
|
+
.filter((item) => item.startsWith('--') && item.length > 2);
|
|
126
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 「连别人已经开着调试端口的浏览器」所需的纯函数:从环境变量取出显式地址,
|
|
3
|
+
* 再从 /json/version 的回复里校验 browser 级 WebSocket 端点。
|
|
4
|
+
*
|
|
5
|
+
* 只做字符串与 JSON 处理,不联网、不读环境变量的副作用(调用方传入 env),
|
|
6
|
+
* 这样 host 侧可以薄委托,单测也能直接跑。
|
|
7
|
+
*
|
|
8
|
+
* 注意:这里**不自动探测**常见端口。插件自己的 profile 会留在临时目录里反复
|
|
9
|
+
* 复用(见 lib/session.js 的 _tryAdopt),接管用户日常 Chrome 既没必要也危险:
|
|
10
|
+
* 9222 上可能正是用户那扇带登录态的窗口。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 归一化成一个可拼 /json/version 的 base:补 http://、去掉尾部斜杠和路径。
|
|
15
|
+
* 用户可能贴 `127.0.0.1:9222`、`http://127.0.0.1:9222/` 或整条
|
|
16
|
+
* `http://127.0.0.1:9222/json/version`,都要能认。
|
|
17
|
+
*/
|
|
18
|
+
export function normalizeCdpBase(raw) {
|
|
19
|
+
const text = String(raw ?? '').trim();
|
|
20
|
+
if (!text) return null;
|
|
21
|
+
const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(text) ? text : 'http://' + text;
|
|
22
|
+
let parsed;
|
|
23
|
+
try {
|
|
24
|
+
parsed = new URL(withScheme);
|
|
25
|
+
} catch {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') return null;
|
|
29
|
+
if (!parsed.hostname) return null;
|
|
30
|
+
return parsed.protocol + '//' + parsed.host;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 这次 launch 可以连接的「外部浏览器」候选:只有显式设置
|
|
35
|
+
* `DSH_VISUAL_ANNOTATOR_CDP_URL` 才返回它,否则返回空数组(默认绝不接管
|
|
36
|
+
* 用户日常窗口)。插件自己的实例不走这条路,而是按 profile 目录重连。
|
|
37
|
+
*/
|
|
38
|
+
export function externalCandidates(env = {}) {
|
|
39
|
+
const explicit = normalizeCdpBase(env.DSH_VISUAL_ANNOTATOR_CDP_URL);
|
|
40
|
+
return explicit ? [explicit] : [];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 解析 `GET /json/version` 的正文,返回 browser 级 ws 地址。
|
|
45
|
+
* 只接受指向 /devtools/browser/ 的 ws(s) 端点:别的服务也可能在这几个端口上
|
|
46
|
+
* 回一段 JSON,不能因为「有 webSocketDebuggerUrl」就接管它。
|
|
47
|
+
*/
|
|
48
|
+
export function parseVersionReply(text, base = '') {
|
|
49
|
+
let value;
|
|
50
|
+
try {
|
|
51
|
+
value = JSON.parse(String(text ?? ''));
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const ws = value?.webSocketDebuggerUrl;
|
|
56
|
+
if (typeof ws !== 'string' || !/^wss?:\/\//i.test(ws)) return null;
|
|
57
|
+
if (!/\/devtools\/browser\//.test(ws)) return null;
|
|
58
|
+
// 端点主机必须是本机或与候选 base 同源,避免被一份伪造的版本回复带去别处。
|
|
59
|
+
if (base) {
|
|
60
|
+
try {
|
|
61
|
+
const expected = new URL(base);
|
|
62
|
+
const actual = new URL(ws);
|
|
63
|
+
const loopback = (host) => host === '127.0.0.1' || host === 'localhost' || host === '::1' || host === '[::1]';
|
|
64
|
+
if (!loopback(actual.hostname) && actual.hostname !== expected.hostname) return null;
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return ws;
|
|
70
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ego lite 专属的两件小事,都做成纯函数方便单测:
|
|
3
|
+
*
|
|
4
|
+
* 1. 新手引导窗口:全新 profile 启动后它会开一个 `browser_ui:chrome://onboarding/`
|
|
5
|
+
* 目标(标题「欢迎使用 ego(𝘭𝘪𝘵𝘦)」),既挡住用户又和拾取无关。插件启动后主动
|
|
6
|
+
* 关掉它——实测 `Target.closeTarget` 只关这一扇引导窗口,浏览器进程与页面都在。
|
|
7
|
+
* 2. 复用用户已经开着的 ego lite:它的 CDP 端口是随机分配的(`--remote-debugging-port=0`
|
|
8
|
+
* 的语义),没有端口文件可读,只能从进程的监听端口里找,于是把
|
|
9
|
+
* `lsof -nP -iTCP -sTCP:LISTEN` 的整表解析成 pid → 端口(沙箱里按 pid 查会被拒)。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** ego lite 引导窗口的 URL;只按前缀匹配,避免版本间加 query。 */
|
|
13
|
+
export const EGO_ONBOARDING_URL = 'chrome://onboarding/';
|
|
14
|
+
|
|
15
|
+
/** 插件在 ego 里使用的独立空间名(= 一个 BrowserContext,ego 的 TaskSpace 同源)。 */
|
|
16
|
+
export const EGO_AGENT_SPACE_NAME = 'agent 空间';
|
|
17
|
+
|
|
18
|
+
/** 这个 CDP target 是不是 ego 的新手引导窗口。 */
|
|
19
|
+
export function isEgoOnboardingTarget(target = {}) {
|
|
20
|
+
const url = String(target?.url ?? '');
|
|
21
|
+
if (!url.startsWith(EGO_ONBOARDING_URL)) return false;
|
|
22
|
+
const type = String(target?.type ?? '');
|
|
23
|
+
return type === '' || type === 'browser_ui' || type === 'page';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 这条命令行是不是 ego lite 的浏览器主进程(不是 renderer/helper 子进程)。
|
|
28
|
+
* macOS 是 `.../Contents/MacOS/ego lite`,Linux 可能是 `/opt/ego lite/ego lite`,
|
|
29
|
+
* Windows 是 `ego lite.exe`;`--type=` 开头的那种一律是子进程,跳过。
|
|
30
|
+
*/
|
|
31
|
+
export function isEgoBrowserCommand(command) {
|
|
32
|
+
const text = String(command ?? '');
|
|
33
|
+
if (!text || text.includes('--type=')) return false;
|
|
34
|
+
if (text.includes('/Contents/MacOS/ego lite')) return true;
|
|
35
|
+
return /(^|[\\/\s])ego lite(\.exe)?(\s|$)/i.test(text);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 解析 `lsof -nP -F -iTCP -sTCP:LISTEN` 的字段输出,得到 pid → 监听端口。
|
|
40
|
+
*
|
|
41
|
+
* 为什么用整表而不是 `lsof -p <pid>`:宿主沙箱里按 pid 查进程详情会被拒
|
|
42
|
+
* (EPERM),整表查询却是允许的;`ps` 同样被拒,所以进程列表改用 `pgrep`。
|
|
43
|
+
* 为什么用 `-F` 而不是默认表格:ego 的进程名是「ego lite」,带空格,按列切
|
|
44
|
+
* 会把命令名拆成两段、pid 错位;字段输出用 p/n 前缀,pid 与地址不会错位。
|
|
45
|
+
* 地址取 `:端口`(IPv4 是 127.0.0.1:49265,IPv6 是 [::1]:62122,监听全部是 *:3999)。
|
|
46
|
+
*/
|
|
47
|
+
export function listeningPortsByPid(fieldOutput) {
|
|
48
|
+
const out = new Map();
|
|
49
|
+
let pid = 0;
|
|
50
|
+
for (const raw of String(fieldOutput ?? '').split('\n')) {
|
|
51
|
+
const line = raw.trim();
|
|
52
|
+
if (!line) continue;
|
|
53
|
+
if (line.startsWith('p')) {
|
|
54
|
+
const value = Number(line.slice(1));
|
|
55
|
+
pid = Number.isInteger(value) && value > 0 ? value : 0;
|
|
56
|
+
if (pid && !out.has(pid)) out.set(pid, []);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (!pid || !line.startsWith('n')) continue; // 只关心地址字段,别的字段跳过
|
|
60
|
+
const match = /:(\d{2,5})\b/.exec(line.slice(1));
|
|
61
|
+
if (!match) continue;
|
|
62
|
+
const port = Number(match[1]);
|
|
63
|
+
if (!(port > 0 && port < 65536)) continue;
|
|
64
|
+
const ports = out.get(pid);
|
|
65
|
+
if (!ports.includes(port)) ports.push(port);
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime rules for code that leaves the Node host, as pure scanners.
|
|
3
|
+
*
|
|
4
|
+
* - lib/client.js runs inside the DSH GUI browser. It is a ModuleLoader
|
|
5
|
+
* factory: `(require) => { var module = { exports: {} }; ... }` —
|
|
6
|
+
* require / module / exports are loader idioms and must stay allowed.
|
|
7
|
+
* - The OVERLAY_SCRIPT payload (lib/overlay.js) runs in the target page's
|
|
8
|
+
* main world via CDP Runtime.evaluate: no loader, no ESM, no Node globals.
|
|
9
|
+
*
|
|
10
|
+
* Rules fire per line so violations carry a line number for the fix guide.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export const EMBEDDED_RULES = [
|
|
14
|
+
{ rule: 'esm-import', re: /^\s*import\b/, why: '注入脚本不是 ESM:DSH GUI 以经典 script 装载' },
|
|
15
|
+
{ rule: 'esm-export', re: /^\s*export\b/, why: '注入脚本不是 ESM:导出走 module.exports(loader 惯用法)' },
|
|
16
|
+
{ rule: 'node-specifier', re: /(?:require\s*\(\s*|from\s+|import\s*\(\s*)['"]node:/, why: '浏览器里没有 node: 模块' },
|
|
17
|
+
{ rule: 'process-global', re: /\bprocess\s*(?:\.|\[)/, why: '浏览器里没有 process 全局' },
|
|
18
|
+
{ rule: 'buffer-global', re: /\bBuffer\b/, why: '浏览器里没有 Buffer 全局' },
|
|
19
|
+
{ rule: 'node-file-vars', re: /__dirname|__filename/, why: '浏览器里没有 __dirname / __filename' },
|
|
20
|
+
{
|
|
21
|
+
rule: 'immediate',
|
|
22
|
+
re: /\b(?:set|clear)Immediate\s*\(/,
|
|
23
|
+
why: '浏览器没有 setImmediate / clearImmediate,用 setTimeout(fn, 0)',
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
/** Extra rules for the page payload, which has no ModuleLoader at all. */
|
|
28
|
+
const PAGE_ONLY_RULES = [
|
|
29
|
+
{ rule: 'require-call', re: /\brequire\s*\(/, why: '页面注入脚本没有 ModuleLoader,require 不存在' },
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
function scanWithRules(text, rules) {
|
|
33
|
+
const violations = [];
|
|
34
|
+
const lines = String(text ?? '').split('\n');
|
|
35
|
+
lines.forEach((line, index) => {
|
|
36
|
+
for (const { rule, re, why } of rules) {
|
|
37
|
+
if (re.test(line)) {
|
|
38
|
+
violations.push({ rule, line: index + 1, snippet: line.trim().slice(0, 120), why });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return violations;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Scan the GUI panel script (lib/client.js). Loader idioms are allowed. */
|
|
46
|
+
export function scanEmbeddedRuntime(text) {
|
|
47
|
+
return scanWithRules(text, EMBEDDED_RULES);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Scan the page payload (OVERLAY_SCRIPT). No loader idioms at all. */
|
|
51
|
+
export function scanPageScript(text) {
|
|
52
|
+
return scanWithRules(text, [...EMBEDDED_RULES, ...PAGE_ONLY_RULES]);
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DSH desktop UI is itself a local web page (Electron loads
|
|
3
|
+
* http://127.0.0.1:<port>/, document title "DeepSeek Harness"). Such a page
|
|
4
|
+
* must never be adopted as the debug target: it only ends up in the debug
|
|
5
|
+
* window as leftover state (session restore, manual paste), and re-selecting
|
|
6
|
+
* it makes the panel "navigate" the GUI web build. Match on loopback root
|
|
7
|
+
* path AND title so user dev servers (localhost:5173/) stay valid targets.
|
|
8
|
+
*/
|
|
9
|
+
export function isGuiPage(url, title) {
|
|
10
|
+
if (!/^https?:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d+)?\/?$/.test(String(url || ''))) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
return /deepseek harness/i.test(String(title || ''));
|
|
14
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build-revision sync contracts as pure functions.
|
|
3
|
+
*
|
|
4
|
+
* Three revision markers guard code that leaves this repo:
|
|
5
|
+
*
|
|
6
|
+
* - `HOST_REV = 'vN-...'` (lib/index.js) must bump whenever lib/client.js
|
|
7
|
+
* changes: the GUI uses the prefix to force-refresh the panel script.
|
|
8
|
+
* - `PANEL_BUILD = 'vN'` (lib/client.js) must equal the HOST_REV prefix, or
|
|
9
|
+
* the panel warns about a build skew that does not exist.
|
|
10
|
+
* - `var REV = N` inside OVERLAY_SCRIPT (lib/overlay.js) must bump whenever
|
|
11
|
+
* the injected payload changes: the script is injected twice per document
|
|
12
|
+
* (preload + evaluate), so a newer build must dispose the older instance
|
|
13
|
+
* instead of leaving stale listeners in charge for the page's life.
|
|
14
|
+
*
|
|
15
|
+
* Everything here is pure string/patch logic; the git plumbing lives in
|
|
16
|
+
* scripts/check-build-sync.mjs so tests can feed fixtures instead of a repo.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const HOST_REV_RE = /HOST_REV\s*=\s*'v(\d+)-'/;
|
|
20
|
+
const PANEL_BUILD_RE = /PANEL_BUILD\s*=\s*'([^']+)'/;
|
|
21
|
+
const OVERLAY_REV_RE = /\bvar\s+REV\s*=\s*(\d+)/;
|
|
22
|
+
|
|
23
|
+
/** @returns {{value: string, prefix: string, number: number} | null} */
|
|
24
|
+
export function parseHostRev(source) {
|
|
25
|
+
const match = HOST_REV_RE.exec(source ?? '');
|
|
26
|
+
if (!match) return null;
|
|
27
|
+
return { value: `v${match[1]}-`, prefix: `v${match[1]}`, number: Number(match[1]) };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @returns {string | null} the PANEL_BUILD literal, e.g. 'v28'. */
|
|
31
|
+
export function parsePanelBuild(source) {
|
|
32
|
+
const match = PANEL_BUILD_RE.exec(source ?? '');
|
|
33
|
+
return match ? match[1] : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @returns {number | null} the overlay takeover revision. */
|
|
37
|
+
export function parseOverlayRev(source) {
|
|
38
|
+
const match = OVERLAY_REV_RE.exec(source ?? '');
|
|
39
|
+
return match ? Number(match[1]) : null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Static half of the contract: all three markers present and in agreement.
|
|
44
|
+
* @returns {{ok: boolean, problems: {code: string, message: string}[]}}
|
|
45
|
+
*/
|
|
46
|
+
export function checkStaticBuildSync({ hostSource, clientSource, overlaySource }) {
|
|
47
|
+
const problems = [];
|
|
48
|
+
const hostRev = parseHostRev(hostSource);
|
|
49
|
+
if (!hostRev) {
|
|
50
|
+
problems.push({ code: 'host-rev-missing', message: "lib/index.js 缺少形如 HOST_REV = 'vN-' 的版本标记" });
|
|
51
|
+
}
|
|
52
|
+
const panelBuild = parsePanelBuild(clientSource);
|
|
53
|
+
if (!panelBuild) {
|
|
54
|
+
problems.push({ code: 'panel-build-missing', message: "lib/client.js 缺少形如 PANEL_BUILD = 'vN' 的版本标记" });
|
|
55
|
+
}
|
|
56
|
+
if (hostRev && panelBuild && hostRev.prefix !== panelBuild) {
|
|
57
|
+
problems.push({
|
|
58
|
+
code: 'prefix-mismatch',
|
|
59
|
+
message: `HOST_REV 前缀 ${hostRev.prefix} 与 PANEL_BUILD ${panelBuild} 不一致:面板会误报版本错位`,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (parseOverlayRev(overlaySource) === null) {
|
|
63
|
+
problems.push({
|
|
64
|
+
code: 'overlay-rev-missing',
|
|
65
|
+
message: 'lib/overlay.js 的 OVERLAY_SCRIPT 里缺少形如 var REV = N 的接管版本',
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return { ok: problems.length === 0, problems };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 1-based line ranges of the injected payload zone in overlay.js: the
|
|
73
|
+
* BINDING_NAME export (interpolated into the template) and the String.raw
|
|
74
|
+
* literal itself. Comment-only changes outside these ranges do not need a REV
|
|
75
|
+
* bump. Returns [] when the markers cannot be located.
|
|
76
|
+
* @returns {{start: number, end: number}[]}
|
|
77
|
+
*/
|
|
78
|
+
export function overlayPayloadRanges(source) {
|
|
79
|
+
const lines = String(source ?? '').split('\n');
|
|
80
|
+
const ranges = [];
|
|
81
|
+
const bindingLine = lines.findIndex((line) => /export\s+const\s+BINDING_NAME\b/.test(line));
|
|
82
|
+
if (bindingLine >= 0) ranges.push({ start: bindingLine + 1, end: bindingLine + 1 });
|
|
83
|
+
let templateEnd = -1;
|
|
84
|
+
for (let i = lines.length - 1; i >= 0; i -= 1) {
|
|
85
|
+
if (/`;\s*$/.test(lines[i])) {
|
|
86
|
+
templateEnd = i + 1;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const templateStart = lines.findIndex((line) => /String\.raw`/.test(line));
|
|
91
|
+
if (templateStart >= 0 && templateEnd > templateStart) {
|
|
92
|
+
ranges.push({ start: templateStart + 1, end: templateEnd });
|
|
93
|
+
}
|
|
94
|
+
return ranges;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* New-file line ranges covered by each @@ hunk of a unified diff (git show).
|
|
99
|
+
* @returns {{start: number, end: number}[]}
|
|
100
|
+
*/
|
|
101
|
+
export function diffHunkNewRanges(patch) {
|
|
102
|
+
const ranges = [];
|
|
103
|
+
for (const match of String(patch ?? '').matchAll(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/gm)) {
|
|
104
|
+
const start = Number(match[1]);
|
|
105
|
+
const count = match[2] === undefined ? 1 : Number(match[2]);
|
|
106
|
+
if (count === 0) {
|
|
107
|
+
// "inserted before this line": pin it to the neighbouring line so
|
|
108
|
+
// pure insertions still register as a touch.
|
|
109
|
+
ranges.push({ start: Math.max(1, start), end: Math.max(1, start) });
|
|
110
|
+
} else {
|
|
111
|
+
ranges.push({ start, end: start + count - 1 });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return ranges;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** True when any hunk range intersects any target range. */
|
|
118
|
+
export function hunksTouchRanges(hunks, ranges) {
|
|
119
|
+
return ranges.some((range) => hunks.some((hunk) => hunk.start <= range.end && hunk.end >= range.start));
|
|
120
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 页面内 HUD「撤销/完成」的纯函数语义。session 与面板共用同一套判定,
|
|
3
|
+
* 保证两侧行为一致,也便于不启动浏览器就单测(AGENTS.md:纯函数下沉 shared)。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 从列表尾部弹一条:返回 { remaining, removed };空列表/非法输入返回 null(no-op)。
|
|
8
|
+
* 不原地修改入参:调用方自行决定何时把 remaining 写回状态。
|
|
9
|
+
*/
|
|
10
|
+
export function popLastPick(picks) {
|
|
11
|
+
if (!Array.isArray(picks) || picks.length === 0) return null;
|
|
12
|
+
return { remaining: picks.slice(0, -1), removed: picks[picks.length - 1] };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 「完成·插入」token:只在列表非空时单调递增,空列表是 no-op——
|
|
17
|
+
* 否则面板会对一个没有内容的请求空转一轮。
|
|
18
|
+
*/
|
|
19
|
+
export function advancePendingConsume(current, picksLength) {
|
|
20
|
+
return picksLength > 0 ? current + 1 : current;
|
|
21
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 同一次提交的重复投递去重。
|
|
3
|
+
*
|
|
4
|
+
* 页面脚本一次提交只应发一份,但同一页面里可能同时活着两份注入脚本(旧版残留、
|
|
5
|
+
* 换浏览器后重新注入没清理),同一次点击就会投递多次,列表里成了「三条一模一样
|
|
6
|
+
* 的标注」。这里只按「同一元素 + 同一序号 + 同一位置 + 同一备注 + 同一地址」判重,
|
|
7
|
+
* 且只在一个短窗口内生效——用户真的又点了一次同一个元素、写了同样的备注,过了
|
|
8
|
+
* 窗口就应当照常记录。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** 去重窗口:同一份投递通常在同一个 tick 内到达,1.5s 足够覆盖重传抖动。 */
|
|
12
|
+
export const PICK_DEDUPE_WINDOW_MS = 1500;
|
|
13
|
+
|
|
14
|
+
/** 把一次投递压成可比对的签名;缺字段一律归一成空串,避免 undefined 参与比较。 */
|
|
15
|
+
export function pickSignature(payload, fallbackUrl = '') {
|
|
16
|
+
const url = payload?.url ?? fallbackUrl ?? '';
|
|
17
|
+
const rect = payload?.rect ?? {};
|
|
18
|
+
return [url, payload?.selector ?? '', payload?.seq ?? '', rect.x, rect.y, payload?.comment ?? ''].join('|');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** 上一次签名的记录(session 里保存 lastPickSignature / lastPickAt)。 */
|
|
22
|
+
export function isDuplicatePick(previous, signature, now, windowMs = PICK_DEDUPE_WINDOW_MS) {
|
|
23
|
+
if (!previous || typeof previous.signature !== 'string') return false;
|
|
24
|
+
if (previous.signature !== signature) return false;
|
|
25
|
+
return now - (previous.at ?? 0) < windowMs;
|
|
26
|
+
}
|