fix(agent-browser): restore bounded presentation lifecycle

This commit is contained in:
2026-09-06 12:50:18 +08:00
parent 5c61110f46
commit 9fed0cc7c4
20 changed files with 1714 additions and 49 deletions

View File

@@ -1,6 +1,7 @@
import { accessSync, constants } from 'node:fs';
import path from 'node:path';
import type { AgentBrowserModule } from '../agent-browser';
import type { AgentBrowserSnapshot } from '../../shared/agent-browser';
import { CodingAttachmentStore } from '../coding-projects/attachment-store';
import { createCodingConversationStore } from '../coding-projects/conversation-store';
import { CodingProjectService } from '../coding-projects/project-service';
@@ -102,6 +103,8 @@ export interface CreateCodingCompositionOptions {
accountCache?: AccountPluginCache;
clientVersion?: string;
policyClient?: PluginPolicyClient;
requestAgentBrowserPresentation?(snapshot: AgentBrowserSnapshot): void;
publishAgentBrowserState?(snapshot: AgentBrowserSnapshot): void;
}
type PiWorkerExecutableProbe = (candidate: string) => boolean;
@@ -241,6 +244,8 @@ export function createCodingComposition(
bundledSkillsDir: options.paths.bundledSkillsDir,
modelToolRegistry,
devicePackageTools,
requestAgentBrowserPresentation: options.requestAgentBrowserPresentation,
publishAgentBrowserState: options.publishAgentBrowserState,
pluginSkillSources,
getPluginSkillSources: async () => effectiveResolver
? (await effectiveResolver.getSkillSources()).map((source) => ({
@@ -543,6 +548,9 @@ export function createCodingComposition(
runtime.dispose(conversationId, reason)
)));
if (reason === 'background_sleep' && runtime.hasActiveWork()) return;
if (reason === 'background_sleep') {
await options.browser.close().catch(() => undefined);
}
await agentServer.stop();
},
async shutdown() {

View File

@@ -7,6 +7,7 @@ import { hasRendererCapability } from '../renderer-capability';
import { parseJsonBody, sendJson } from '../route-utils';
type AgentBrowserBody = {
project_id?: unknown;
project_path?: unknown;
url?: unknown;
action?: unknown;
@@ -71,7 +72,12 @@ function parseStringArray(value: unknown): string[] | undefined {
return value.map((item) => item.trim()).filter(Boolean);
}
async function resolveActiveProject(ctx: HostApiContext, requestedPath?: unknown) {
async function resolveActiveProject(
ctx: HostApiContext,
requestedPath?: unknown,
requestedId?: unknown,
rendererPresentation = false,
) {
const activeProject = await ctx.codingProjectStore.getActiveProject();
if (!activeProject) {
throw new AgentBrowserRouteError('PROJECT_NOT_ACTIVE', '请先打开一个项目。', 409);
@@ -84,6 +90,24 @@ async function resolveActiveProject(ctx: HostApiContext, requestedPath?: unknown
throw new AgentBrowserRouteError('PROJECT_NOT_ACTIVE', '当前项目目录不可用。', 409);
}
const projectId = nonEmptyString(requestedId);
if (projectId) {
if (!rendererPresentation) {
throw new AgentBrowserRouteError(
'TARGET_DENIED',
'项目 ID 只能由 Makelore 界面用于浏览器操作。',
403,
);
}
if (projectId !== activeProject.id) {
throw new AgentBrowserRouteError('PROJECT_MISMATCH', '智能体只能调试当前项目。', 403);
}
return {
...activeProject,
path: activeRealPath,
};
}
const requested = nonEmptyString(requestedPath);
if (!requested) {
throw new AgentBrowserRouteError('INVALID_REQUEST', '缺少当前项目路径。');
@@ -188,6 +212,19 @@ async function readBody(req: IncomingMessage): Promise<AgentBrowserBody> {
return await parseJsonBody<AgentBrowserBody>(req);
}
function resolveRequestProject(
req: IncomingMessage,
ctx: HostApiContext,
body: AgentBrowserBody,
) {
return resolveActiveProject(
ctx,
body.project_path,
body.project_id,
hasRendererCapability(req),
);
}
export async function handleAgentBrowserRoutes(
req: IncomingMessage,
res: ServerResponse,
@@ -200,7 +237,12 @@ export async function handleAgentBrowserRoutes(
const service = requireService(ctx);
if (url.pathname === '/api/agent-browser/state' && req.method === 'GET') {
const project = await resolveActiveProject(ctx, url.searchParams.get('project_path'));
const project = await resolveActiveProject(
ctx,
url.searchParams.get('project_path'),
url.searchParams.get('project_id'),
hasRendererCapability(req),
);
const browser = await service.getSnapshot(project.path);
await ensureProjectStillActive(ctx, project);
sendJson(res, 200, { success: true, browser });
@@ -209,8 +251,13 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/open' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const rendererPresentation = hasRendererCapability(req);
const project = await resolveActiveProject(
ctx,
body.project_path,
body.project_id,
rendererPresentation,
);
if (body.bounds !== undefined && !rendererPresentation) {
requireRendererPresentation(req);
}
@@ -233,7 +280,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/present' && req.method === 'POST') {
requireRendererPresentation(req);
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveActiveProject(ctx, body.project_path, body.project_id, true);
const browser = await service.present({
projectPath: project.path,
visible: body.visible === true,
@@ -248,7 +295,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/diagnostics' && req.method === 'POST') {
requireRendererPresentation(req);
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveActiveProject(ctx, body.project_path, body.project_id, true);
const browser = await service.setDiagnostics({
projectPath: project.path,
enabled: body.enabled === true,
@@ -260,7 +307,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/navigate' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const action = nonEmptyString(body.action);
if (action !== 'url' && action !== 'back' && action !== 'forward' && action !== 'reload') {
throw new AgentBrowserRouteError('INVALID_REQUEST', '浏览器导航动作无效。');
@@ -278,7 +325,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/cdp/send' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const method = nonEmptyString(body.method);
if (!method) throw new AgentBrowserRouteError('INVALID_REQUEST', '缺少 CDP method。');
const params = body.params === undefined
@@ -302,7 +349,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/cdp/events' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const page = await service.readEvents({
projectPath: project.path,
after: finiteInteger(body.after),
@@ -317,7 +364,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/payload/read' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const handle = nonEmptyString(body.handle);
if (!handle) throw new AgentBrowserRouteError('INVALID_REQUEST', '缺少 payload handle。');
const chunk = await service.readPayload({
@@ -333,7 +380,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/close' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const browser = await service.close(project.path);
await ensureProjectStillActive(ctx, project);
emitState(ctx, 'agent-browser:state', {
@@ -347,7 +394,7 @@ export async function handleAgentBrowserRoutes(
if (url.pathname === '/api/agent-browser/reset-profile' && req.method === 'POST') {
const body = await readBody(req);
const project = await resolveActiveProject(ctx, body.project_path);
const project = await resolveRequestProject(req, ctx, body);
const browser = await service.resetProfile(project.path);
await ensureProjectStillActive(ctx, project);
emitState(ctx, 'agent-browser:state', {