feat: add task-bound real browser checks and verified page delivery

This commit is contained in:
Agent Workbench Dev
2026-10-30 08:46:53 +08:00
parent 29a65760c9
commit 5921b8efdc
78 changed files with 4563 additions and 76 deletions
+42
View File
@@ -0,0 +1,42 @@
import { createHash } from 'node:crypto';
export const BROWSER_ACTIVE = new Set(['queued', 'snapshotting', 'launching', 'running', 'closing']);
export const BROWSER_ACTIONS = ['fill', 'select', 'click', 'assert_text', 'assert_count', 'assert_visible'];
export const BROWSER_LIMITS = Object.freeze({ files: 512, bytes: 32 * 1024 * 1024, fileBytes: 8 * 1024 * 1024,
steps: 20, durationMs: 45000, stepTimeoutMs: 3000, records: 200, text: 20000 });
export const browserError = (message, status = 409) => Object.assign(new Error(message), { status });
export const browserHash = value => createHash('sha256').update(typeof value === 'string' || Buffer.isBuffer(value) ? value : JSON.stringify(value)).digest('hex');
export function browserPlanInput(input = {}) {
if (!input || typeof input !== 'object' || Array.isArray(input)
|| Object.keys(input).some(key => !['build_directory', 'path', 'viewport', 'steps'].includes(key))) throw browserError('浏览器计划只接受构建目录、页面路径、视口和交互步骤。', 400);
const buildDirectory = input.build_directory ?? 'dist';
if (typeof buildDirectory !== 'string' || !buildDirectory.trim() || buildDirectory.length > 160 || /[\\:\0]/.test(buildDirectory)
|| buildDirectory.startsWith('/') || buildDirectory.split('/').some(part => !part || part.startsWith('.') || ['node_modules', 'runtime'].includes(part.toLowerCase()))) throw browserError('构建目录需要项目内的常规相对目录,例如dist或build。', 400);
const pathname = input.path ?? '/';
if (typeof pathname !== 'string' || pathname.length > 300 || !pathname.startsWith('/') || pathname.startsWith('//') || /[\\\0\r\n]/.test(pathname)
|| pathname.split(/[/?#]/).includes('..') || new URL(pathname, 'http://127.0.0.1').origin !== 'http://127.0.0.1') throw browserError('页面路径必须位于所属本地预览,例如/。', 400);
const viewport = input.viewport ?? { width: 1280, height: 900 };
if (!viewport || Object.keys(viewport).some(key => !['width', 'height'].includes(key)) || !Number.isSafeInteger(viewport.width) || !Number.isSafeInteger(viewport.height)
|| viewport.width < 320 || viewport.width > 1440 || viewport.height < 400 || viewport.height > 1200) throw browserError('浏览器视口范围为宽320–1440、高400–1200像素。', 400);
const steps = input.steps ?? [];
if (!Array.isArray(steps) || steps.length > BROWSER_LIMITS.steps) throw browserError('浏览器计划最多20个交互或断言。', 400);
for (const step of steps) {
if (!step || typeof step !== 'object' || Array.isArray(step) || !BROWSER_ACTIONS.includes(step.action)
|| Object.keys(step).some(key => !['action', 'selector', 'value', 'text', 'count', 'visible', 'contains'].includes(key))) throw browserError('浏览器步骤只支持填写、选择、点击和文本/数量/可见性断言。', 400);
if (typeof step.selector !== 'string' || !step.selector.trim() || step.selector.length > 300 || /[\0\r\n]|>>|^[a-z_-]+=/i.test(step.selector)) throw browserError('步骤需要最多300字的CSS选择器,不能使用脚本或跨页面选择器。', 400);
if (['fill', 'select'].includes(step.action) && (typeof step.value !== 'string' || step.value.length > 2000)) throw browserError('填写或选择需要最多2000字的value。', 400);
if (step.action === 'assert_text' && (typeof step.text !== 'string' || step.text.length > 4000)) throw browserError('文本断言需要最多4000字的text。', 400);
if (step.action === 'assert_count' && (!Number.isSafeInteger(step.count) || step.count < 0 || step.count > 1000)) throw browserError('数量断言需要0–1000的整数count。', 400);
if (step.visible !== undefined && typeof step.visible !== 'boolean' || step.contains !== undefined && typeof step.contains !== 'boolean') throw browserError('可见性和包含选项需要布尔值。', 400);
}
return { buildDirectory, path: pathname, viewport: { ...viewport }, steps: structuredClone(steps) };
}
export const browserPlanContract = plan => ({ schemaVersion: 1, kind: 'sealed-browser-plan', id: plan.id, sessionId: plan.sessionId,
projectId: plan.projectId, taskId: plan.taskId, runId: plan.runId, projectRoot: plan.projectRoot, fingerprint: plan.fingerprint,
sourceFingerprint: plan.sourceFingerprint, build: plan.build, path: plan.path, viewport: plan.viewport, steps: plan.steps, installation: plan.installation, limits: plan.limits });
export function assertBrowserResolved(store, projectId) {
if ([...store.core.browser_job.values()].some(job => job.projectId === projectId && job.requiresVerification)) throw browserError('项目有中断且进程结束状态未知的浏览器检查,请在所属任务核对并确认原记录后再执行。');
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
export const EXECUTION_SCHEMA = 1;
export const ACTIVE_STATES = new Set(['running', 'awaiting_approval', 'stopping']);
export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job'];
export const CORE_KINDS = ['task', 'run', 'step', 'tool', 'artifact', 'check_plan', 'check_job', 'delivery_plan', 'delivery_job', 'change_set', 'context_plan', 'workspace', 'research_target', 'research_job', 'engine_job', 'browser_plan', 'browser_job'];
export const timestamp = () => new Date().toISOString();
export const isId = value => typeof value === 'string' && /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i.test(value);