Skip to content

Commit ecb7a10

Browse files
authored
Merge branch 'main' into chore/evaluate-script-on-service-workers
2 parents 3ffacb4 + f763da2 commit ecb7a10

5 files changed

Lines changed: 6 additions & 485 deletions

File tree

src/McpContext.ts

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ import {Locator} from './third_party/index.js';
3434
import {PredefinedNetworkConditions} from './third_party/index.js';
3535
import {listPages} from './tools/pages.js';
3636
import {CLOSE_PAGE_ERROR} from './tools/ToolDefinition.js';
37-
import type {
38-
Context,
39-
DevToolsData,
40-
ContextPage,
41-
} from './tools/ToolDefinition.js';
37+
import type {Context, DevToolsData} from './tools/ToolDefinition.js';
4238
import type {TraceResult} from './trace-processing/parse.js';
4339
import type {
4440
EmulationSettings,
@@ -111,7 +107,6 @@ export class McpContext implements Context {
111107
#isRunningTrace = false;
112108
#screenRecorderData: {recorder: ScreenRecorder; filePath: string} | null =
113109
null;
114-
#focusedPagePerContext = new Map<BrowserContext, Page>();
115110

116111
#nextPageId = 1;
117112

@@ -297,10 +292,6 @@ export class McpContext implements Context {
297292
page.dispose();
298293
this.#mcpPages.delete(page.pptrPage);
299294
}
300-
const ctx = page.pptrPage.browserContext();
301-
if (this.#focusedPagePerContext.get(ctx) === page.pptrPage) {
302-
this.#focusedPagePerContext.delete(ctx);
303-
}
304295
await page.pptrPage.close({runBeforeUnload: false});
305296
}
306297

@@ -494,38 +485,9 @@ export class McpContext implements Context {
494485
return this.#selectedPage?.pptrPage === page;
495486
}
496487

497-
assertPageIsFocused(pageToCheck: Page | ContextPage): void {
498-
const page = 'pptrPage' in pageToCheck ? pageToCheck.pptrPage : pageToCheck;
499-
const ctx = page.browserContext();
500-
const focused = this.#focusedPagePerContext.get(ctx);
501-
if (focused && focused !== page) {
502-
const targetId = this.#mcpPages.get(page)?.id ?? '?';
503-
const focusedId = this.#mcpPages.get(focused)?.id ?? '?';
504-
throw new Error(
505-
`Page ${targetId} is not the active page in its browser context (page ${focusedId} is). ` +
506-
`Call select_page with pageId ${targetId} first.`,
507-
);
508-
}
509-
}
510-
511488
selectPage(newPage: McpPage): void {
512-
const ctx = newPage.pptrPage.browserContext();
513-
const oldFocused = this.#focusedPagePerContext.get(ctx);
514-
if (
515-
oldFocused &&
516-
oldFocused !== newPage.pptrPage &&
517-
!oldFocused.isClosed()
518-
) {
519-
void oldFocused.emulateFocusedPage(false).catch(error => {
520-
this.logger('Error turning off focused page emulation', error);
521-
});
522-
}
523-
this.#focusedPagePerContext.set(ctx, newPage.pptrPage);
524489
this.#selectedPage = newPage;
525490
this.#updateSelectedPageTimeouts();
526-
void newPage.pptrPage.emulateFocusedPage(true).catch(error => {
527-
this.logger('Error turning on focused page emulation', error);
528-
});
529491
}
530492

531493
#updateSelectedPageTimeouts() {
@@ -601,6 +563,10 @@ export class McpContext implements Context {
601563
if (!mcpPage) {
602564
mcpPage = new McpPage(page, this.#nextPageId++);
603565
this.#mcpPages.set(page, mcpPage);
566+
// We emulate a focused page for all pages to support multi-agent workflows.
567+
void page.emulateFocusedPage(true).catch(error => {
568+
this.logger('Error turning on focused page emulation', error);
569+
});
604570
}
605571
mcpPage.isolatedContextName = isolatedContextNames.get(page);
606572
}
@@ -613,12 +579,6 @@ export class McpContext implements Context {
613579
this.#mcpPages.delete(page);
614580
}
615581
}
616-
// Prune stale #focusedPagePerContext entries.
617-
for (const [ctx, page] of this.#focusedPagePerContext) {
618-
if (!currentPages.has(page)) {
619-
this.#focusedPagePerContext.delete(ctx);
620-
}
621-
}
622582

623583
this.#pages = allPages.filter(page => {
624584
return (

src/tools/ToolDefinition.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ export type Context = Readonly<{
147147
): Promise<ContextPage>;
148148
closePage(pageId: number): Promise<void>;
149149
selectPage(page: ContextPage): void;
150-
assertPageIsFocused(page: Page): void;
151150
restoreEmulation(page: ContextPage): Promise<void>;
152151
emulate(
153152
options: {

src/tools/input.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export const clickAt = definePageTool({
9898
},
9999
handler: async (request, response, context) => {
100100
const page = request.page;
101-
context.assertPageIsFocused(page.pptrPage);
102101
await context.waitForEventsAfterAction(async () => {
103102
await page.pptrPage.mouse.click(request.params.x, request.params.y, {
104103
clickCount: request.params.dblClick ? 2 : 1,
@@ -263,7 +262,6 @@ export const typeText = definePageTool({
263262
},
264263
handler: async (request, response, context) => {
265264
const page = request.page;
266-
context.assertPageIsFocused(page.pptrPage);
267265
await context.waitForEventsAfterAction(async () => {
268266
await page.pptrPage.keyboard.type(request.params.text);
269267
if (request.params.submitKey) {
@@ -416,7 +414,6 @@ export const pressKey = definePageTool({
416414
},
417415
handler: async (request, response, context) => {
418416
const page = request.page;
419-
context.assertPageIsFocused(page.pptrPage);
420417
const tokens = parseKey(request.params.key);
421418
const [key, ...modifiers] = tokens;
422419

0 commit comments

Comments
 (0)