import { ensureBrowser } from "../browser-launcher.js";
import { ensurePlaywriterChrome } from "./claude/session-browser-policy.js";
import { getCachedRelayStatus, reconcilePlaywriterRelay, invalidateRelayStatusCache } from "../../utils/playwriter-relay.js";
import { logger } from "../../logger.js";
export function isPlaywrightToolName(toolName) {
    return toolName.startsWith("mcp__playwright__") || toolName.startsWith("playwright_");
}
export async function ensureBrowserForTool(options) {
    logger.debug(`[ToolUse] ensuring browser for ${options.toolName}`);
    if (options.browserBackend === "playwriter") {
        const host = options.playwriterRelayHost ?? "127.0.0.1";
        const port = options.playwriterRelayPort ?? 19988;
        const status = await getCachedRelayStatus(host, port);
        if (!status.reachable) {
            logger.system("Playwriter relay unreachable — reconciling");
            invalidateRelayStatusCache();
            reconcilePlaywriterRelay(host, port);
        }
        if (status.extensionConnected) {
            logger.debug("Playwriter extension already connected — skipping Chrome restart");
            return { playwriterConnectionsResetThisTurn: options.playwriterConnectionsResetThisTurn };
        }
        const didReset = ensurePlaywriterChrome(options.playwriterConnectionsResetThisTurn);
        return { playwriterConnectionsResetThisTurn: options.playwriterConnectionsResetThisTurn || didReset };
    }
    await ensureBrowser();
    return { playwriterConnectionsResetThisTurn: options.playwriterConnectionsResetThisTurn };
}
//# sourceMappingURL=browser-tool-policy.js.map