feat: add task records and verified engineering check jobs

This commit is contained in:
Agent Workbench Dev
2026-10-29 14:03:50 +08:00
parent 93f4d90ccb
commit 2ea34850c4
26 changed files with 2703 additions and 56 deletions
+29 -8
View File
@@ -2,6 +2,7 @@ import fs from 'node:fs';
import path from 'node:path';
import { listFiles, readText, proposeFile, applyChange } from './files.mjs';
import { APP_VERSION, executionEnvironment, sessionSnapshot } from './diagnostics.mjs';
import { inspectProject } from '../core/project-inspector.mjs';
export function createSessionPublisher(clients) {
return session => {
@@ -19,7 +20,7 @@ function cursorNumber(value) {
return Number(value);
}
export function createRequestHandler({ appDirectory, store, provider, runtime, token, clients, started, getPort, exampleProject, shutdown }) {
export function createRequestHandler({ appDirectory, server, store, provider, runtime, token, clients, started, getPort, exampleProject, shutdown }) {
const research = [
{ id: 'overview', title: '统一机制报告', file: 'RESEARCH.md', description: '研究结论、证据与本产品采用的独立设计。' },
{ id: 'cursor', title: 'Cursor:执行与编辑', file: 'research/CURSOR-MECHANISMS.md', description: '工具能力、执行事件、取消、恢复与文本改动。' },
@@ -59,6 +60,7 @@ function allowRequest(request, url) {
return async (request, response) => {
try {
const actualPort = getPort();
if (server && (!server.listening || server.address().port !== actualPort)) throw new Error('本地执行宿主已经变化,请重新连接工作台。');
const url = new URL(request.url, 'http://127.0.0.1:' + actualPort);
allowRequest(request, url);
const method = request.method;
@@ -69,7 +71,7 @@ return async (request, response) => {
eventCursor: store.eventCursor, storage: store.storageInfo(), runtimeRoot: appDirectory,
environment: executionEnvironment(),
projects: store.projects, sessions: store.listSessions(), exampleProjectId: exampleProject?.id ?? null,
capabilities: { fileChanges: 'review', commands: 'per-call-approval', recovery: 'durable-tool-results', model: 'chat-completions-compatible', execution: 'task-run-step', events: 'persistent-cursor', artifacts: 'sha256-verified' },
capabilities: { fileChanges: 'review', commands: 'per-call-approval', recovery: 'durable-tool-results', model: 'chat-completions-compatible', execution: 'task-run-step', events: 'persistent-cursor', artifacts: 'sha256-verified', checks: 'npm-script-jobs', projectInspection: 'bounded-source-and-git' },
});
if (method === 'GET' && route === '/api/diagnostics') {
const selected = url.searchParams.get('session');
@@ -109,9 +111,10 @@ return async (request, response) => {
if (typeof input.root !== 'string' || input.root.length > 2000) throw new Error('请输入项目文件夹的完整路径。');
return json(response, store.addProject(input.root), 201);
}
const projectMatch = route.match(/^\/api\/projects\/([a-f0-9-]+)\/(files|file)$/);
const projectMatch = route.match(/^\/api\/projects\/([a-f0-9-]+)\/(files|file|inspection)$/);
if (method === 'GET' && projectMatch) {
const project = store.project(projectMatch[1]);
if (projectMatch[2] === 'inspection') return json(response, await inspectProject(project));
return json(response, projectMatch[2] === 'files'
? listFiles(project, url.searchParams.get('path') || '', 7, 1200)
: readText(project, url.searchParams.get('path') || ''));
@@ -127,12 +130,29 @@ return async (request, response) => {
const session = store.session(id);
if (method === 'GET' && !action) return json(response, store.publicSession(session));
if (method === 'GET' && action === 'execution') return json(response, store.executionSnapshot(session));
if (method === 'POST' && action === 'check-plans') {
const input = await body(request);
if (runtime.isActive(id)) throw Object.assign(new Error('任务或检查正在执行,请等待结束后再准备检查。'), { status: 409 });
await runtime.checkRunner().plan(session, input.names);
return json(response, store.publicSession(session), 201);
}
if (method === 'POST' && action === 'check-jobs') {
const input = await body(request);
await runtime.checkRunner().start(session, input.planId);
return json(response, store.publicSession(session), 202);
}
const checkJob = action.match(/^check-jobs\/([a-f0-9-]+)(?:\/(stop))?$/);
if (checkJob && method === 'GET' && !checkJob[2]) return json(response, store.owned('check_job', checkJob[1], id));
if (checkJob && method === 'POST' && checkJob[2] === 'stop') {
await body(request); await runtime.checkRunner().stop(id, checkJob[1]);
return json(response, store.publicSession(session));
}
const runMatch = action.match(/^runs\/([a-f0-9-]+)$/);
if (method === 'GET' && runMatch) return json(response, store.owned('run', runMatch[1], session.id));
if (method === 'GET' && action === 'export') {
response.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8',
'Content-Disposition': 'attachment; filename="agent-workbench-session-' + id + '.json"' });
response.end(JSON.stringify({ schemaVersion: 1, exportedAt: new Date().toISOString(), project: store.project(session.projectId), session }, null, 2));
response.end(JSON.stringify({ schemaVersion: 1, executionSchemaVersion: 1, exportedAt: new Date().toISOString(), project: store.project(session.projectId), session, execution: store.executionSnapshot(session) }, null, 2));
return;
}
if (method === 'POST' && action === 'messages') {
@@ -164,7 +184,7 @@ return async (request, response) => {
const change = action.match(/^changes\/([a-f0-9-]+)\/(apply|reject)$/);
if (method === 'POST' && change) {
await body(request);
if (runtime.isActive(id)) throw Object.assign(new Error('任务正在执行,请等待结束或停止后再审查文件改动。'), { status: 409 });
if (runtime.projectHasActivity(session.projectId)) throw Object.assign(new Error('项目正在执行任务或检查,请等待结束或停止后再审查文件改动。'), { status: 409 });
if (change[2] === 'apply') applyChange(store, session, change[1]);
else {
const item = session.changes.find(candidate => candidate.id === change[1]);
@@ -177,13 +197,14 @@ return async (request, response) => {
}
if (method === 'POST' && action === 'proposals') {
const input = await body(request);
if (runtime.checks?.projectActive(session.projectId)) throw Object.assign(new Error('工程检查正在执行,请等待结束后再编辑文件。'), { status: 409 });
proposeFile(store, session, input);
return json(response, store.publicSession(session), 201);
}
}
if (method === 'POST' && route === '/api/config') {
const input = await body(request);
if (runtime.active.size) throw Object.assign(new Error('请先停止正在运行的任务,再改变模型连接。'), { status: 409 });
if (runtime.hasActivity()) throw Object.assign(new Error('请先停止正在运行的任务或检查,再改变模型连接。'), { status: 409 });
if (!['demo', 'compatible'].includes(input.mode)) throw new Error('请选择有效运行模式。');
const config = {
mode: input.mode, baseUrl: String(input.baseUrl || '').trim(), model: String(input.model || '').trim(),
@@ -201,13 +222,13 @@ return async (request, response) => {
}
if (method === 'POST' && route === '/api/config/models') {
const input = await body(request);
if (runtime.active.size) throw Object.assign(new Error('请先停止正在运行的任务,再测试新的模型连接。'), { status: 409 });
if (runtime.hasActivity()) throw Object.assign(new Error('请先停止正在运行的任务或检查,再测试新的模型连接。'), { status: 409 });
const config = { ...store.config, baseUrl: String(input.baseUrl || store.config.baseUrl), mode: 'demo' };
return json(response, { models: await provider.models(config, Object.hasOwn(input, 'apiKey') ? String(input.apiKey).trim() : provider.apiKey) });
}
if (method === 'POST' && route === '/api/config/test') {
const input = await body(request);
if (runtime.active.size) throw Object.assign(new Error('请先停止正在运行的任务,再测试模型连接。'), { status: 409 });
if (runtime.hasActivity()) throw Object.assign(new Error('请先停止正在运行的任务或检查,再测试模型连接。'), { status: 409 });
const config = { ...store.config, baseUrl: String(input.baseUrl || store.config.baseUrl), model: String(input.model || store.config.model), mode: 'compatible' };
provider.validate(config);
const startedAt = Date.now();