mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-17 21:10:54 +00:00
* Browser: replace extension path with Chrome MCP * Browser: clarify relay stub and doctor checks * Docs: mark browser MCP migration as breaking * Browser: reject unsupported profile drivers * Browser: accept clawd alias on profile create * Doctor: narrow legacy browser driver migration
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import fs from "node:fs";
|
|
import type { ResolvedBrowserProfile } from "./config.js";
|
|
import { BrowserResetUnsupportedError } from "./errors.js";
|
|
import { getBrowserProfileCapabilities } from "./profile-capabilities.js";
|
|
import type { ProfileRuntimeState } from "./server-context.types.js";
|
|
import { movePathToTrash } from "./trash.js";
|
|
|
|
type ResetDeps = {
|
|
profile: ResolvedBrowserProfile;
|
|
getProfileState: () => ProfileRuntimeState;
|
|
stopRunningBrowser: () => Promise<{ stopped: boolean }>;
|
|
isHttpReachable: (timeoutMs?: number) => Promise<boolean>;
|
|
resolveOpenClawUserDataDir: (profileName: string) => string;
|
|
};
|
|
|
|
type ResetOps = {
|
|
resetProfile: () => Promise<{ moved: boolean; from: string; to?: string }>;
|
|
};
|
|
|
|
async function closePlaywrightBrowserConnectionForProfile(cdpUrl?: string): Promise<void> {
|
|
try {
|
|
const mod = await import("./pw-ai.js");
|
|
await mod.closePlaywrightBrowserConnection(cdpUrl ? { cdpUrl } : undefined);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
export function createProfileResetOps({
|
|
profile,
|
|
getProfileState,
|
|
stopRunningBrowser,
|
|
isHttpReachable,
|
|
resolveOpenClawUserDataDir,
|
|
}: ResetDeps): ResetOps {
|
|
const capabilities = getBrowserProfileCapabilities(profile);
|
|
const resetProfile = async () => {
|
|
if (!capabilities.supportsReset) {
|
|
throw new BrowserResetUnsupportedError(
|
|
`reset-profile is only supported for local profiles (profile "${profile.name}" is remote).`,
|
|
);
|
|
}
|
|
|
|
const userDataDir = resolveOpenClawUserDataDir(profile.name);
|
|
const profileState = getProfileState();
|
|
const httpReachable = await isHttpReachable(300);
|
|
if (httpReachable && !profileState.running) {
|
|
// Port in use but not by us - kill it.
|
|
await closePlaywrightBrowserConnectionForProfile(profile.cdpUrl);
|
|
}
|
|
|
|
if (profileState.running) {
|
|
await stopRunningBrowser();
|
|
}
|
|
|
|
await closePlaywrightBrowserConnectionForProfile(profile.cdpUrl);
|
|
|
|
if (!fs.existsSync(userDataDir)) {
|
|
return { moved: false, from: userDataDir };
|
|
}
|
|
|
|
const moved = await movePathToTrash(userDataDir);
|
|
return { moved: true, from: userDataDir, to: moved };
|
|
};
|
|
|
|
return { resetProfile };
|
|
}
|