feat(companion): add HTTPS pairing and scoped device authority

This commit is contained in:
Agent Workbench Dev
2026-10-31 17:38:57 +08:00
parent 06c9fac87e
commit dfe2e40387
24 changed files with 2572 additions and 5 deletions
+20
View File
@@ -14,6 +14,7 @@ import { ConfigImporter } from '../core/models/config-import.mjs';
import { ProjectMemoryManager } from '../core/memory/project-memory.mjs';
import { workerForProject, workerConflict, assertWorkersResolved } from '../shared/contracts/workers.mjs';
import { hostAuthority } from '../core/desktop/host-authority.mjs';
import { companionGateway } from './companion-gateway.mjs';
import { inspectOperations } from './operations.mjs';
export function createSessionPublisher(clients) {
@@ -85,6 +86,25 @@ return async (request, response) => {
const method = request.method;
const route = url.pathname;
if (method === 'GET' && route === '/api/health') return json(response, { app: 'agent-workbench', version: APP_VERSION, pid: process.pid, ready: server?.workbenchReady !== false, uptimeMs: Date.now() - started });
if (route.startsWith('/api/companion')) {
const gateway = companionGateway({ appDirectory, server, store, provider, runtime });
if (method === 'GET' && route === '/api/companion') return json(response, gateway.status());
if (method === 'POST' && route === '/api/companion/start') return json(response, await gateway.start(await body(request)));
if (method === 'POST' && route === '/api/companion/close') { const value = await body(request); if (Object.keys(value).length) throw Object.assign(new Error('关闭手机连接不接受额外字段。'), { status: 400 }); return json(response, await gateway.close()); }
if (method === 'POST' && route === '/api/companion/pair') return json(response, await gateway.pairing(await body(request)));
const revoke = route.match(/^\/api\/companion\/devices\/([a-f0-9-]+)\/revoke$/);
if (method === 'POST' && revoke) {
const value = await body(request);
if (Object.keys(value).length !== 1 || !Number.isSafeInteger(value.revision)) throw Object.assign(new Error('设备撤销需要原登记修订。'), { status: 400 });
return json(response, gateway.revoke(revoke[1], value.revision));
}
if (method === 'GET' && route === '/api/companion/certificate') {
const certificate = gateway.tls.publicCertificate();
response.writeHead(200, { 'Content-Type': 'application/pkix-cert', 'Content-Disposition': 'attachment; filename="Agent-Workbench-Mobile-CA.cer"', 'Cache-Control': 'no-store' });
return response.end(certificate.bytes);
}
return json(response, { error: '手机连接管理操作不存在。' }, 404);
}
if (method === 'GET' && route === '/api/host/identity') return json(response, host().attest(url.searchParams.get('challenge')));
if (method === 'GET' && route === '/api/host/status') return json(response, host().status());
if (method === 'POST' && route === '/api/host/windows/register') return json(response, host().registerWindow(await body(request)));