fix(browser): preserve chat focus during agent browsing

This commit is contained in:
2026-09-27 15:23:23 +08:00
parent 220e89992b
commit a52e747a00
10 changed files with 323 additions and 16 deletions

View File

@@ -458,6 +458,45 @@ describe('PI-090 product tools', () => {
});
});
it.each([false, true])('preserves the selected tab when reopening an initialized browser (visible=%s)', async (visible) => {
const root = await temporaryRoot('makelore-pi-browser-reuse-');
const initialized = {
browserId: 'browser-a', projectId: 'project-a', projectPath: root,
state: 'attached' as const, generation: 3, url: 'http://127.0.0.1:4173/', title: 'App',
visible, bounds: { x: 10, y: 20, width: 800, height: 600 },
canGoBack: false, canGoForward: false, eventCursor: 0,
};
const browser = {
open: vi.fn(async () => initialized),
waitForPresentation: vi.fn(async () => ({ ...initialized, visible: true })),
setDiagnostics: vi.fn(async () => initialized),
} as unknown as AgentBrowserModule;
const requestAgentBrowserPresentation = vi.fn();
const publishAgentBrowserState = vi.fn();
const tools = new PiProductTools({
browser,
attachments: new CodingAttachmentStore(path.join(root, 'attachments')),
bundledSkillsDir: path.resolve('resources/coding-skills'),
requestAgentBrowserPresentation,
publishAgentBrowserState,
});
const context = {
conversationId: 'conversation-a', runId: 'run-a', resourceId: 'browser-a',
projectId: 'project-a', projectPath: root, skillIds: [],
};
for (let attempt = 0; attempt < 3; attempt += 1) {
const result = await tools.execute('agent_browser', context, { action: 'open', url: initialized.url });
expect(JSON.parse(result.content[0].text)).toMatchObject({ visible, generation: 3 });
}
expect(requestAgentBrowserPresentation).not.toHaveBeenCalled();
expect(browser.waitForPresentation).not.toHaveBeenCalled();
expect(publishAgentBrowserState).toHaveBeenLastCalledWith(initialized);
await tools.settleRun('conversation-a', 'run-a');
expect(browser.setDiagnostics).toHaveBeenCalledWith({
projectPath: root, enabled: false, owner: 'agent:conversation-a:run-a',
});
});
it('closes an agent-opened browser when presentation times out', async () => {
const root = await temporaryRoot('makelore-pi-browser-timeout-');
const hidden = {