gz-laya-pilot 0.1.2 → 0.1.3
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/NOTICE.md +29 -29
- package/README.md +102 -98
- package/dist/assertions.d.ts +1 -1
- package/dist/assertions.js +4 -9
- package/dist/assertions.js.map +1 -1
- package/dist/replay.js +18 -2
- package/dist/replay.js.map +1 -1
- package/dist/request-assertions.d.ts +6 -0
- package/dist/request-assertions.js +86 -0
- package/dist/request-assertions.js.map +1 -0
- package/dist/schema.d.ts +6 -0
- package/dist/schema.js +42 -0
- package/dist/schema.js.map +1 -1
- package/package.json +57 -57
- package/src/agent/bridge.ts +180 -180
- package/src/agent/report.ts +139 -139
- package/src/agent/run.ts +136 -136
- package/src/agent/schema.ts +38 -38
- package/src/agent/worker.ts +114 -114
- package/src/assertions.ts +4 -10
- package/src/browser.ts +20 -20
- package/src/cli.ts +5 -5
- package/src/decision.ts +85 -85
- package/src/index.ts +10 -10
- package/src/planner.ts +28 -28
- package/src/readiness.ts +92 -92
- package/src/replay.ts +24 -7
- package/src/request-assertions.ts +83 -0
- package/src/schema.ts +46 -0
- package/src/targets.ts +61 -61
- package/vendor/.gitattributes +5 -5
package/dist/schema.js
CHANGED
|
@@ -4,6 +4,8 @@ export const assertionSources = {
|
|
|
4
4
|
'element-value': '输入框值',
|
|
5
5
|
'element-state': '元素可见性',
|
|
6
6
|
'element-count': '元素数量',
|
|
7
|
+
'request-count': '接口调用次数',
|
|
8
|
+
'request-success': '接口成功',
|
|
7
9
|
};
|
|
8
10
|
export const assertionOperators = {
|
|
9
11
|
contains: '包含',
|
|
@@ -12,14 +14,20 @@ export const assertionOperators = {
|
|
|
12
14
|
'not-equals': '不等于',
|
|
13
15
|
visible: '可见',
|
|
14
16
|
hidden: '不可见或不存在',
|
|
17
|
+
successful: '全部成功',
|
|
15
18
|
};
|
|
16
19
|
export function operatorsFor(source) {
|
|
20
|
+
if (source === 'request-count')
|
|
21
|
+
return ['equals'];
|
|
22
|
+
if (source === 'request-success')
|
|
23
|
+
return ['successful'];
|
|
17
24
|
if (source === 'element-state')
|
|
18
25
|
return ['visible', 'hidden'];
|
|
19
26
|
if (source === 'element-count')
|
|
20
27
|
return ['equals', 'not-equals'];
|
|
21
28
|
return ['contains', 'not-contains', 'equals', 'not-equals'];
|
|
22
29
|
}
|
|
30
|
+
export const isRequestAssertion = (source) => source === 'request-count' || source === 'request-success';
|
|
23
31
|
export function parseAssertion(value) {
|
|
24
32
|
const a = value;
|
|
25
33
|
if (!a || typeof a.name !== 'string' || !a.name.trim() || a.name.length > 100)
|
|
@@ -28,6 +36,33 @@ export function parseAssertion(value) {
|
|
|
28
36
|
throw Error('不支持的断言对象或条件');
|
|
29
37
|
if (!Number.isInteger(a.timeout) || a.timeout < 500 || a.timeout > 30000)
|
|
30
38
|
throw Error('断言等待时间应为 500–30,000 毫秒');
|
|
39
|
+
if (isRequestAssertion(a.source)) {
|
|
40
|
+
const method = typeof a.method === 'string' ? a.method.trim().toUpperCase() : '';
|
|
41
|
+
const pathname = a.pathname;
|
|
42
|
+
if (!/^[A-Z]{1,16}$/.test(method))
|
|
43
|
+
throw Error('请填写 HTTP 方法');
|
|
44
|
+
if (typeof pathname !== 'string' ||
|
|
45
|
+
pathname.length > 2000 ||
|
|
46
|
+
!pathname.startsWith('/') ||
|
|
47
|
+
pathname.startsWith('//') ||
|
|
48
|
+
/[?#\\\s\x00-\x1f\x7f]/.test(pathname) ||
|
|
49
|
+
new URL(pathname, 'http://localhost').pathname !== pathname)
|
|
50
|
+
throw Error('请填写站内接口路径,勿包含查询参数或片段');
|
|
51
|
+
if (a.source === 'request-count' &&
|
|
52
|
+
(typeof a.expected !== 'string' ||
|
|
53
|
+
!/^\d{1,6}$/.test(a.expected) ||
|
|
54
|
+
Number(a.expected) > 100000))
|
|
55
|
+
throw Error('调用次数应为 0–100,000 的整数');
|
|
56
|
+
return {
|
|
57
|
+
name: a.name.trim(),
|
|
58
|
+
source: a.source,
|
|
59
|
+
operator: a.operator,
|
|
60
|
+
timeout: a.timeout,
|
|
61
|
+
method,
|
|
62
|
+
pathname,
|
|
63
|
+
...(a.source === 'request-count' ? { expected: a.expected } : {}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
31
66
|
if (a.source !== 'page-text' &&
|
|
32
67
|
(typeof a.selector !== 'string' || !a.selector.trim() || a.selector.length > 2000))
|
|
33
68
|
throw Error('请填写有效的 CSS 定位信息');
|
|
@@ -113,6 +148,13 @@ export function parseCase(value) {
|
|
|
113
148
|
...(step.supplemented === true ? { supplemented: true } : {}),
|
|
114
149
|
};
|
|
115
150
|
});
|
|
151
|
+
let hasAction = false;
|
|
152
|
+
for (const step of steps) {
|
|
153
|
+
if (step.assertion && isRequestAssertion(step.assertion.source) && !hasAction)
|
|
154
|
+
throw Error('接口断言必须放在一次操作之后');
|
|
155
|
+
if (step.target)
|
|
156
|
+
hasAction = true;
|
|
157
|
+
}
|
|
116
158
|
let timeline;
|
|
117
159
|
if (input.timeline !== undefined) {
|
|
118
160
|
if (!input.timeline ||
|
package/dist/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,WAAW,EAAE,MAAM;IACnB,cAAc,EAAE,MAAM;IACtB,eAAe,EAAE,MAAM;IACvB,eAAe,EAAE,OAAO;IACxB,eAAe,EAAE,MAAM;
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,WAAW,EAAE,MAAM;IACnB,cAAc,EAAE,MAAM;IACtB,eAAe,EAAE,MAAM;IACvB,eAAe,EAAE,OAAO;IACxB,eAAe,EAAE,MAAM;IACvB,eAAe,EAAE,QAAQ;IACzB,iBAAiB,EAAE,MAAM;CACjB,CA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AG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aACzF,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,KAAK,SAAS,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC;YAC/E,MAAM,KAAK,CAAC,cAAc,CAAC,CAAC;QAC9B,QAAQ,GAAG,EAAE,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;IAC/D,CAAC;SAAM,IAAI,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,KAAK,SAAS,CAAC;QAAE,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC;IACvF,OAAO;QACL,MAAM,EAAE,sBAAsB;QAC9B,OAAO,EAAE,CAAC;QACV,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE;QACvB,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,KAAK;QACL,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAClC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "gz-laya-pilot",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "Functional TypeScript browser testing runtime adapted from LayaPilot",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"files": [
|
|
8
|
-
"dist",
|
|
9
|
-
"src",
|
|
10
|
-
"vendor",
|
|
11
|
-
"NOTICE.md",
|
|
12
|
-
"README.md",
|
|
13
|
-
"LICENSE"
|
|
14
|
-
],
|
|
15
|
-
"engines": {
|
|
16
|
-
"node": ">=22"
|
|
17
|
-
},
|
|
18
|
-
"sideEffects": false,
|
|
19
|
-
"bin": {
|
|
20
|
-
"gz-laya-pilot": "./dist/cli.js"
|
|
21
|
-
},
|
|
22
|
-
"exports": {
|
|
23
|
-
".": {
|
|
24
|
-
"types": "./dist/index.d.ts",
|
|
25
|
-
"import": "./dist/index.js"
|
|
26
|
-
},
|
|
27
|
-
"./agent-schema": {
|
|
28
|
-
"types": "./dist/agent/schema.d.ts",
|
|
29
|
-
"import": "./dist/agent/schema.js"
|
|
30
|
-
},
|
|
31
|
-
"./schema": {
|
|
32
|
-
"types": "./dist/schema.d.ts",
|
|
33
|
-
"import": "./dist/schema.js"
|
|
34
|
-
},
|
|
35
|
-
"./browser": {
|
|
36
|
-
"types": "./dist/targets.d.ts",
|
|
37
|
-
"import": "./dist/targets.js"
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"scripts": {
|
|
41
|
-
"build": "tsc -p tsconfig.json",
|
|
42
|
-
"prepack": "npm run build",
|
|
43
|
-
"test": "node --test tests/*.test.mjs && node --test --test-concurrency=1 vendor/laya-pilot/tests/*.test.mjs"
|
|
44
|
-
},
|
|
45
|
-
"dependencies": {
|
|
46
|
-
"dom-accessibility-api": "^0.7.1",
|
|
47
|
-
"playwright": "1.62.1"
|
|
48
|
-
},
|
|
49
|
-
"devDependencies": {
|
|
50
|
-
"@types/node": "^24.12.0",
|
|
51
|
-
"typescript": "~5.9.3"
|
|
52
|
-
},
|
|
53
|
-
"publishConfig": {
|
|
54
|
-
"access": "public",
|
|
55
|
-
"registry": "https://registry.npmjs.org/"
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "gz-laya-pilot",
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Functional TypeScript browser testing runtime adapted from LayaPilot",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"src",
|
|
10
|
+
"vendor",
|
|
11
|
+
"NOTICE.md",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=22"
|
|
17
|
+
},
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"bin": {
|
|
20
|
+
"gz-laya-pilot": "./dist/cli.js"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./agent-schema": {
|
|
28
|
+
"types": "./dist/agent/schema.d.ts",
|
|
29
|
+
"import": "./dist/agent/schema.js"
|
|
30
|
+
},
|
|
31
|
+
"./schema": {
|
|
32
|
+
"types": "./dist/schema.d.ts",
|
|
33
|
+
"import": "./dist/schema.js"
|
|
34
|
+
},
|
|
35
|
+
"./browser": {
|
|
36
|
+
"types": "./dist/targets.d.ts",
|
|
37
|
+
"import": "./dist/targets.js"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc -p tsconfig.json",
|
|
42
|
+
"prepack": "npm run build",
|
|
43
|
+
"test": "node --test tests/*.test.mjs && node --test --test-concurrency=1 vendor/laya-pilot/tests/*.test.mjs"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"dom-accessibility-api": "^0.7.1",
|
|
47
|
+
"playwright": "1.62.1"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^24.12.0",
|
|
51
|
+
"typescript": "~5.9.3"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"registry": "https://registry.npmjs.org/"
|
|
56
|
+
}
|
|
57
|
+
}
|
package/src/agent/bridge.ts
CHANGED
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
import { existsSync, createReadStream } from 'node:fs';
|
|
2
|
-
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
-
import { testUrl, type AgentStatus } from './schema.js';
|
|
7
|
-
import { runAgent } from './run.js';
|
|
8
|
-
|
|
9
|
-
export interface AgentBridgeTarget {
|
|
10
|
-
url: string;
|
|
11
|
-
authState?: string;
|
|
12
|
-
authCheck?: string;
|
|
13
|
-
}
|
|
14
|
-
export interface AgentBridgeOptions {
|
|
15
|
-
targets: AgentBridgeTarget[];
|
|
16
|
-
outputDir: string;
|
|
17
|
-
python: string;
|
|
18
|
-
model: string;
|
|
19
|
-
channel?: string;
|
|
20
|
-
headed?: boolean;
|
|
21
|
-
isBusy?: () => boolean;
|
|
22
|
-
}
|
|
23
|
-
/** Local HTTP adapter shared by React and SDK. All test decisions/execution remain upstream. */
|
|
24
|
-
export function createAgentBridge(options: AgentBridgeOptions) {
|
|
25
|
-
const targets = options.targets.map((t) => ({ ...t, url: testUrl(t.url) }));
|
|
26
|
-
const root = path.resolve(options.outputDir);
|
|
27
|
-
let active: Promise<void> | undefined, controller: AbortController | undefined;
|
|
28
|
-
let status: AgentStatus = {
|
|
29
|
-
state: 'idle',
|
|
30
|
-
mode: 'explore',
|
|
31
|
-
message: '尚未探索',
|
|
32
|
-
cases: [],
|
|
33
|
-
decisions: 0,
|
|
34
|
-
};
|
|
35
|
-
const stop = () => controller?.abort();
|
|
36
|
-
const middleware = async (req: IncomingMessage, res: ServerResponse, next: () => void) => {
|
|
37
|
-
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
38
|
-
if (!['/status', '/explore', '/replay', '/stop', '/artifact'].includes(url.pathname))
|
|
39
|
-
return next();
|
|
40
|
-
const send = (code: number, body: unknown) => {
|
|
41
|
-
res.statusCode = code;
|
|
42
|
-
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
43
|
-
res.setHeader('Cache-Control', 'no-store');
|
|
44
|
-
res.end(JSON.stringify(body));
|
|
45
|
-
};
|
|
46
|
-
const host = req.headers.host ?? '';
|
|
47
|
-
if (
|
|
48
|
-
!['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(req.socket.remoteAddress ?? '') ||
|
|
49
|
-
!/^(localhost|127\.0\.0\.1|\[::1\]):\d+$/.test(host)
|
|
50
|
-
)
|
|
51
|
-
return send(403, { message: '执行服务仅允许本机访问' });
|
|
52
|
-
if (req.headers.origin && req.headers.origin !== `http://${host}`)
|
|
53
|
-
return send(403, { message: '仅接受同源访问' });
|
|
54
|
-
if (req.method === 'GET' && url.pathname === '/status') {
|
|
55
|
-
const configured =
|
|
56
|
-
existsSync(options.python) && existsSync(options.model) && targets.length > 0;
|
|
57
|
-
return send(200, {
|
|
58
|
-
...status,
|
|
59
|
-
capabilities: {
|
|
60
|
-
configured,
|
|
61
|
-
targets: targets.map((t) => t.url),
|
|
62
|
-
message: configured
|
|
63
|
-
? '本地 Laya 已配置 · 上游 LayaPilot 引擎'
|
|
64
|
-
: '请在宿主配置 LAYA_PYTHON、LAYA_MODEL 和 LAYA_TEST_URLS',
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
if (req.method === 'GET' && url.pathname === '/artifact') {
|
|
69
|
-
const id = url.searchParams.get('id') ?? '',
|
|
70
|
-
file = url.searchParams.get('file') ?? '';
|
|
71
|
-
if (
|
|
72
|
-
!/^[\da-f-]{36}$/.test(id) ||
|
|
73
|
-
!/^(cases\.xlsx|results\.xlsx|results\.json|decisions\.ndjson|checkpoint\.json|trace\.zip|persistence-[a-zA-Z0-9_-]{1,85}\.zip|evidence\/[a-zA-Z0-9_-]{1,150}\.png)$/.test(
|
|
74
|
-
file,
|
|
75
|
-
)
|
|
76
|
-
)
|
|
77
|
-
return send(400, { message: '无效的报告文件' });
|
|
78
|
-
const filename = path.join(root, id, file === 'results.xlsx' ? '执行结果.xlsx' : file);
|
|
79
|
-
if (!existsSync(filename)) return send(404, { message: '报告尚未生成' });
|
|
80
|
-
res.setHeader(
|
|
81
|
-
'Content-Type',
|
|
82
|
-
file.endsWith('.png') ? 'image/png' : 'application/octet-stream',
|
|
83
|
-
);
|
|
84
|
-
res.setHeader('Content-Disposition', `attachment; filename="${path.basename(file)}"`);
|
|
85
|
-
res.setHeader('Cache-Control', 'no-store');
|
|
86
|
-
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
87
|
-
const stream = createReadStream(filename);
|
|
88
|
-
stream.on('error', () => res.destroy());
|
|
89
|
-
stream.pipe(res);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
if (req.method !== 'POST' || !['/explore', '/replay', '/stop'].includes(url.pathname))
|
|
93
|
-
return send(405, { message: '请求方法不支持' });
|
|
94
|
-
if (
|
|
95
|
-
req.headers.origin !== `http://${host}` ||
|
|
96
|
-
req.headers['x-laya-pilot'] !== 'agent-v1' ||
|
|
97
|
-
!req.headers['content-type']?.startsWith('application/json')
|
|
98
|
-
)
|
|
99
|
-
return send(403, { message: '仅接受页面内同源请求' });
|
|
100
|
-
if (url.pathname === '/stop') {
|
|
101
|
-
stop();
|
|
102
|
-
return send(200, status);
|
|
103
|
-
}
|
|
104
|
-
if (active || options.isBusy?.()) return send(409, { message: '已有测试正在运行' });
|
|
105
|
-
try {
|
|
106
|
-
let body = '';
|
|
107
|
-
for await (const chunk of req) {
|
|
108
|
-
body += chunk;
|
|
109
|
-
if (Buffer.byteLength(body) > 3 * 1024 * 1024) throw Error('请求超过 3 MB');
|
|
110
|
-
}
|
|
111
|
-
const input = JSON.parse(body),
|
|
112
|
-
target = targets.find((t) => t.url === testUrl(input.url));
|
|
113
|
-
if (!target) throw Error('入口不在宿主配置的测试地址列表中');
|
|
114
|
-
if (typeof input.allowWrite !== 'boolean') throw Error('需要明确选择是否允许创建测试数据');
|
|
115
|
-
if (!existsSync(options.python) || !existsSync(options.model))
|
|
116
|
-
throw Error('生成和回放均需配置本地 Laya');
|
|
117
|
-
if (active || options.isBusy?.()) return send(409, { message: '已有测试正在运行' });
|
|
118
|
-
const replay = url.pathname === '/replay';
|
|
119
|
-
let content: Buffer | undefined;
|
|
120
|
-
if (replay) {
|
|
121
|
-
if (typeof input.workbook !== 'string' || !/^[a-z\d+/]+={0,2}$/i.test(input.workbook))
|
|
122
|
-
throw Error('请上传有效的 Excel 用例');
|
|
123
|
-
content = Buffer.from(input.workbook, 'base64');
|
|
124
|
-
if (
|
|
125
|
-
content.length > 2 * 1024 * 1024 ||
|
|
126
|
-
content.length < 4 ||
|
|
127
|
-
content.readUInt32LE(0) !== 0x04034b50
|
|
128
|
-
)
|
|
129
|
-
throw Error('仅支持 2 MB 内的 xlsx 用例');
|
|
130
|
-
}
|
|
131
|
-
const id = randomUUID(),
|
|
132
|
-
outputDir = path.join(root, id);
|
|
133
|
-
controller = new AbortController();
|
|
134
|
-
const signal = controller.signal;
|
|
135
|
-
status = {
|
|
136
|
-
id,
|
|
137
|
-
state: 'running',
|
|
138
|
-
mode: replay ? 'replay' : 'explore',
|
|
139
|
-
message: '正在启动上游 LayaPilot',
|
|
140
|
-
cases: [],
|
|
141
|
-
decisions: 0,
|
|
142
|
-
};
|
|
143
|
-
active = (async () => {
|
|
144
|
-
try {
|
|
145
|
-
await mkdir(outputDir, { recursive: true });
|
|
146
|
-
const workbook = content ? path.join(outputDir, 'uploaded.xlsx') : undefined;
|
|
147
|
-
if (workbook && content) await writeFile(workbook, content);
|
|
148
|
-
const result = await runAgent({
|
|
149
|
-
...target,
|
|
150
|
-
outputDir,
|
|
151
|
-
python: options.python,
|
|
152
|
-
model: options.model,
|
|
153
|
-
channel: options.channel,
|
|
154
|
-
workbook,
|
|
155
|
-
allowWrite: input.allowWrite,
|
|
156
|
-
headed: options.headed ?? false,
|
|
157
|
-
signal,
|
|
158
|
-
onProgress: (value) => {
|
|
159
|
-
status = { ...value, id };
|
|
160
|
-
},
|
|
161
|
-
});
|
|
162
|
-
status = { ...result, id };
|
|
163
|
-
} catch (error) {
|
|
164
|
-
status = {
|
|
165
|
-
...status,
|
|
166
|
-
state: signal.aborted ? 'stopped' : 'failed',
|
|
167
|
-
message: error instanceof Error ? error.message : String(error),
|
|
168
|
-
};
|
|
169
|
-
} finally {
|
|
170
|
-
active = undefined;
|
|
171
|
-
controller = undefined;
|
|
172
|
-
}
|
|
173
|
-
})();
|
|
174
|
-
send(202, status);
|
|
175
|
-
} catch (error) {
|
|
176
|
-
send(400, { message: error instanceof Error ? error.message : '无法启动测试' });
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
return { middleware, stop, isRunning: () => Boolean(active) };
|
|
180
|
-
}
|
|
1
|
+
import { existsSync, createReadStream } from 'node:fs';
|
|
2
|
+
import { mkdir, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
6
|
+
import { testUrl, type AgentStatus } from './schema.js';
|
|
7
|
+
import { runAgent } from './run.js';
|
|
8
|
+
|
|
9
|
+
export interface AgentBridgeTarget {
|
|
10
|
+
url: string;
|
|
11
|
+
authState?: string;
|
|
12
|
+
authCheck?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface AgentBridgeOptions {
|
|
15
|
+
targets: AgentBridgeTarget[];
|
|
16
|
+
outputDir: string;
|
|
17
|
+
python: string;
|
|
18
|
+
model: string;
|
|
19
|
+
channel?: string;
|
|
20
|
+
headed?: boolean;
|
|
21
|
+
isBusy?: () => boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Local HTTP adapter shared by React and SDK. All test decisions/execution remain upstream. */
|
|
24
|
+
export function createAgentBridge(options: AgentBridgeOptions) {
|
|
25
|
+
const targets = options.targets.map((t) => ({ ...t, url: testUrl(t.url) }));
|
|
26
|
+
const root = path.resolve(options.outputDir);
|
|
27
|
+
let active: Promise<void> | undefined, controller: AbortController | undefined;
|
|
28
|
+
let status: AgentStatus = {
|
|
29
|
+
state: 'idle',
|
|
30
|
+
mode: 'explore',
|
|
31
|
+
message: '尚未探索',
|
|
32
|
+
cases: [],
|
|
33
|
+
decisions: 0,
|
|
34
|
+
};
|
|
35
|
+
const stop = () => controller?.abort();
|
|
36
|
+
const middleware = async (req: IncomingMessage, res: ServerResponse, next: () => void) => {
|
|
37
|
+
const url = new URL(req.url ?? '/', 'http://localhost');
|
|
38
|
+
if (!['/status', '/explore', '/replay', '/stop', '/artifact'].includes(url.pathname))
|
|
39
|
+
return next();
|
|
40
|
+
const send = (code: number, body: unknown) => {
|
|
41
|
+
res.statusCode = code;
|
|
42
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
43
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
44
|
+
res.end(JSON.stringify(body));
|
|
45
|
+
};
|
|
46
|
+
const host = req.headers.host ?? '';
|
|
47
|
+
if (
|
|
48
|
+
!['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(req.socket.remoteAddress ?? '') ||
|
|
49
|
+
!/^(localhost|127\.0\.0\.1|\[::1\]):\d+$/.test(host)
|
|
50
|
+
)
|
|
51
|
+
return send(403, { message: '执行服务仅允许本机访问' });
|
|
52
|
+
if (req.headers.origin && req.headers.origin !== `http://${host}`)
|
|
53
|
+
return send(403, { message: '仅接受同源访问' });
|
|
54
|
+
if (req.method === 'GET' && url.pathname === '/status') {
|
|
55
|
+
const configured =
|
|
56
|
+
existsSync(options.python) && existsSync(options.model) && targets.length > 0;
|
|
57
|
+
return send(200, {
|
|
58
|
+
...status,
|
|
59
|
+
capabilities: {
|
|
60
|
+
configured,
|
|
61
|
+
targets: targets.map((t) => t.url),
|
|
62
|
+
message: configured
|
|
63
|
+
? '本地 Laya 已配置 · 上游 LayaPilot 引擎'
|
|
64
|
+
: '请在宿主配置 LAYA_PYTHON、LAYA_MODEL 和 LAYA_TEST_URLS',
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (req.method === 'GET' && url.pathname === '/artifact') {
|
|
69
|
+
const id = url.searchParams.get('id') ?? '',
|
|
70
|
+
file = url.searchParams.get('file') ?? '';
|
|
71
|
+
if (
|
|
72
|
+
!/^[\da-f-]{36}$/.test(id) ||
|
|
73
|
+
!/^(cases\.xlsx|results\.xlsx|results\.json|decisions\.ndjson|checkpoint\.json|trace\.zip|persistence-[a-zA-Z0-9_-]{1,85}\.zip|evidence\/[a-zA-Z0-9_-]{1,150}\.png)$/.test(
|
|
74
|
+
file,
|
|
75
|
+
)
|
|
76
|
+
)
|
|
77
|
+
return send(400, { message: '无效的报告文件' });
|
|
78
|
+
const filename = path.join(root, id, file === 'results.xlsx' ? '执行结果.xlsx' : file);
|
|
79
|
+
if (!existsSync(filename)) return send(404, { message: '报告尚未生成' });
|
|
80
|
+
res.setHeader(
|
|
81
|
+
'Content-Type',
|
|
82
|
+
file.endsWith('.png') ? 'image/png' : 'application/octet-stream',
|
|
83
|
+
);
|
|
84
|
+
res.setHeader('Content-Disposition', `attachment; filename="${path.basename(file)}"`);
|
|
85
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
86
|
+
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
87
|
+
const stream = createReadStream(filename);
|
|
88
|
+
stream.on('error', () => res.destroy());
|
|
89
|
+
stream.pipe(res);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (req.method !== 'POST' || !['/explore', '/replay', '/stop'].includes(url.pathname))
|
|
93
|
+
return send(405, { message: '请求方法不支持' });
|
|
94
|
+
if (
|
|
95
|
+
req.headers.origin !== `http://${host}` ||
|
|
96
|
+
req.headers['x-laya-pilot'] !== 'agent-v1' ||
|
|
97
|
+
!req.headers['content-type']?.startsWith('application/json')
|
|
98
|
+
)
|
|
99
|
+
return send(403, { message: '仅接受页面内同源请求' });
|
|
100
|
+
if (url.pathname === '/stop') {
|
|
101
|
+
stop();
|
|
102
|
+
return send(200, status);
|
|
103
|
+
}
|
|
104
|
+
if (active || options.isBusy?.()) return send(409, { message: '已有测试正在运行' });
|
|
105
|
+
try {
|
|
106
|
+
let body = '';
|
|
107
|
+
for await (const chunk of req) {
|
|
108
|
+
body += chunk;
|
|
109
|
+
if (Buffer.byteLength(body) > 3 * 1024 * 1024) throw Error('请求超过 3 MB');
|
|
110
|
+
}
|
|
111
|
+
const input = JSON.parse(body),
|
|
112
|
+
target = targets.find((t) => t.url === testUrl(input.url));
|
|
113
|
+
if (!target) throw Error('入口不在宿主配置的测试地址列表中');
|
|
114
|
+
if (typeof input.allowWrite !== 'boolean') throw Error('需要明确选择是否允许创建测试数据');
|
|
115
|
+
if (!existsSync(options.python) || !existsSync(options.model))
|
|
116
|
+
throw Error('生成和回放均需配置本地 Laya');
|
|
117
|
+
if (active || options.isBusy?.()) return send(409, { message: '已有测试正在运行' });
|
|
118
|
+
const replay = url.pathname === '/replay';
|
|
119
|
+
let content: Buffer | undefined;
|
|
120
|
+
if (replay) {
|
|
121
|
+
if (typeof input.workbook !== 'string' || !/^[a-z\d+/]+={0,2}$/i.test(input.workbook))
|
|
122
|
+
throw Error('请上传有效的 Excel 用例');
|
|
123
|
+
content = Buffer.from(input.workbook, 'base64');
|
|
124
|
+
if (
|
|
125
|
+
content.length > 2 * 1024 * 1024 ||
|
|
126
|
+
content.length < 4 ||
|
|
127
|
+
content.readUInt32LE(0) !== 0x04034b50
|
|
128
|
+
)
|
|
129
|
+
throw Error('仅支持 2 MB 内的 xlsx 用例');
|
|
130
|
+
}
|
|
131
|
+
const id = randomUUID(),
|
|
132
|
+
outputDir = path.join(root, id);
|
|
133
|
+
controller = new AbortController();
|
|
134
|
+
const signal = controller.signal;
|
|
135
|
+
status = {
|
|
136
|
+
id,
|
|
137
|
+
state: 'running',
|
|
138
|
+
mode: replay ? 'replay' : 'explore',
|
|
139
|
+
message: '正在启动上游 LayaPilot',
|
|
140
|
+
cases: [],
|
|
141
|
+
decisions: 0,
|
|
142
|
+
};
|
|
143
|
+
active = (async () => {
|
|
144
|
+
try {
|
|
145
|
+
await mkdir(outputDir, { recursive: true });
|
|
146
|
+
const workbook = content ? path.join(outputDir, 'uploaded.xlsx') : undefined;
|
|
147
|
+
if (workbook && content) await writeFile(workbook, content);
|
|
148
|
+
const result = await runAgent({
|
|
149
|
+
...target,
|
|
150
|
+
outputDir,
|
|
151
|
+
python: options.python,
|
|
152
|
+
model: options.model,
|
|
153
|
+
channel: options.channel,
|
|
154
|
+
workbook,
|
|
155
|
+
allowWrite: input.allowWrite,
|
|
156
|
+
headed: options.headed ?? false,
|
|
157
|
+
signal,
|
|
158
|
+
onProgress: (value) => {
|
|
159
|
+
status = { ...value, id };
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
status = { ...result, id };
|
|
163
|
+
} catch (error) {
|
|
164
|
+
status = {
|
|
165
|
+
...status,
|
|
166
|
+
state: signal.aborted ? 'stopped' : 'failed',
|
|
167
|
+
message: error instanceof Error ? error.message : String(error),
|
|
168
|
+
};
|
|
169
|
+
} finally {
|
|
170
|
+
active = undefined;
|
|
171
|
+
controller = undefined;
|
|
172
|
+
}
|
|
173
|
+
})();
|
|
174
|
+
send(202, status);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
send(400, { message: error instanceof Error ? error.message : '无法启动测试' });
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
return { middleware, stop, isRunning: () => Boolean(active) };
|
|
180
|
+
}
|