mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 08:40:44 +00:00
fix(browser): finish existing-session attach port (#57245)
This commit is contained in:
@@ -463,37 +463,6 @@ async function getExistingSession(
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForChromeMcpReady(
|
||||
session: ChromeMcpSession,
|
||||
profileName: string,
|
||||
timeoutMs?: number,
|
||||
): Promise<void> {
|
||||
if (!timeoutMs || timeoutMs <= 0) {
|
||||
await session.ready;
|
||||
return;
|
||||
}
|
||||
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
session.ready,
|
||||
new Promise<never>((_, reject) => {
|
||||
timer = setTimeout(() => {
|
||||
reject(
|
||||
new BrowserProfileUnavailableError(
|
||||
`Chrome MCP existing-session attach for profile "${profileName}" timed out after ${timeoutMs}ms.`,
|
||||
),
|
||||
);
|
||||
}, timeoutMs);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function createEphemeralSession(
|
||||
profileName: string,
|
||||
userDataDir?: string,
|
||||
|
||||
@@ -886,22 +886,6 @@ export async function gotoPageWithNavigationGuard(
|
||||
}
|
||||
}
|
||||
|
||||
export async function getPageForTargetId(opts: {
|
||||
cdpUrl: string;
|
||||
targetId?: string;
|
||||
}): Promise<Page> {
|
||||
const reusedCachedBrowser = hasCachedPlaywrightBrowserConnection(opts.cdpUrl);
|
||||
try {
|
||||
return await getPageForTargetIdOnce(opts);
|
||||
} catch (err) {
|
||||
if (!isRecoverableStalePageSelectionError(err, reusedCachedBrowser)) {
|
||||
throw err;
|
||||
}
|
||||
await closePlaywrightBrowserConnection({ cdpUrl: opts.cdpUrl });
|
||||
return await getPageForTargetIdOnce(opts);
|
||||
}
|
||||
}
|
||||
|
||||
export function refLocator(page: Page, ref: string) {
|
||||
const normalized = ref.startsWith("@")
|
||||
? ref.slice(1)
|
||||
|
||||
@@ -90,6 +90,7 @@ export function createProfileAvailability({
|
||||
|
||||
const isTransportAvailable = async (timeoutMs?: number) => {
|
||||
if (capabilities.usesChromeMcp) {
|
||||
const { ensureChromeMcpAvailable } = await getChromeMcpModule();
|
||||
await ensureChromeMcpAvailable(profile.name, profile.userDataDir, {
|
||||
ephemeral: true,
|
||||
timeoutMs,
|
||||
|
||||
@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import "../../test-support/browser-security-runtime.mock.js";
|
||||
import type { BrowserServerState } from "./server-context.js";
|
||||
|
||||
vi.mock("./chrome-mcp.js", () => ({
|
||||
const chromeMcpMock = vi.hoisted(() => ({
|
||||
closeChromeMcpSession: vi.fn(async () => true),
|
||||
ensureChromeMcpAvailable: vi.fn(async () => {}),
|
||||
focusChromeMcpTab: vi.fn(async () => {}),
|
||||
@@ -20,8 +20,14 @@ vi.mock("./chrome-mcp.js", () => ({
|
||||
getChromeMcpPid: vi.fn(() => 4321),
|
||||
}));
|
||||
|
||||
vi.mock("./chrome-mcp.js", () => chromeMcpMock);
|
||||
|
||||
vi.mock("./chrome-mcp.runtime.js", () => ({
|
||||
getChromeMcpModule: vi.fn(async () => chromeMcpMock),
|
||||
}));
|
||||
|
||||
const { createBrowserRouteContext } = await import("./server-context.js");
|
||||
const chromeMcp = await import("./chrome-mcp.js");
|
||||
const chromeMcp = chromeMcpMock;
|
||||
|
||||
function makeState(): BrowserServerState {
|
||||
return {
|
||||
@@ -93,7 +99,8 @@ describe("browser server-context existing-session profile", () => {
|
||||
vi.mocked(chromeMcp.ensureChromeMcpAvailable).mockResolvedValueOnce();
|
||||
vi.mocked(chromeMcp.listChromeMcpTabs).mockRejectedValueOnce(new Error("No page selected"));
|
||||
|
||||
await expect(ctx.listProfiles()).resolves.toEqual([
|
||||
const profiles = await ctx.listProfiles();
|
||||
expect(profiles).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "chrome-live",
|
||||
transport: "chrome-mcp",
|
||||
|
||||
Reference in New Issue
Block a user