// ---------------------------------------------------------------------------
// Example: Browser Research Toolkit
//
// Usage:
//   yarn example:browser-research-toolkit
//   yarn example:browser-research-toolkit:headed
//   yarn example:browser-research-toolkit:headless
//
// This deterministic example validates the opt-in browser research tools without
// involving an LLM. It calls browser_open, browser_snapshot, browser_click,
// browser_screenshot, and browser_close in the order an agent would use them.
// ---------------------------------------------------------------------------
import { join } from 'node:path';
import { createBrowserResearchToolkit } from '../src/index.js';
const STATE_ROOT = join(process.cwd(), '.heddle', 'examples', 'browser-research-toolkit');
const START_URL = process.env.HEDDLE_BROWSER_START_URL ?? 'https://en.wikipedia.org/wiki/Browser_automation';
const ALLOWED_DOMAINS = ['wikipedia.org'];
async function main() {
    const headless = resolveHeadlessMode(process.argv.slice(2));
    const tools = createBrowserResearchToolkit({
        stateRoot: STATE_ROOT,
        allowedDomains: ALLOWED_DOMAINS,
        profileId: 'wikipedia-research-tools',
        headless,
        channel: resolveChannel(process.env.HEDDLE_BROWSER_CHANNEL),
        maxElementsPerSnapshot: 60,
    }).createTools({
        workspaceRoot: process.cwd(),
        stateRoot: STATE_ROOT,
        model: 'gpt-5.1-codex-mini',
        memoryDir: join(STATE_ROOT, 'memory'),
        memoryMode: 'none',
    });
    const toolMap = Object.fromEntries(tools.map((tool) => [tool.name, tool]));
    try {
        console.log(`[browser-tools] mode=${headless ? 'headless' : 'headed'} stateRoot=${STATE_ROOT}`);
        const open = await runTool(toolMap.browser_open, { url: START_URL });
        console.log(`[browser_open] ${formatResult(open)}`);
        if (!open.ok) {
            return;
        }
        const snapshot = await runTool(toolMap.browser_snapshot, {});
        const elements = snapshot.output?.elements ?? [];
        console.log(`[browser_snapshot] ${elements.length} interactive elements`);
        for (const element of elements.slice(0, 8)) {
            console.log(`  ${element.ref} ${element.role} ${element.name}`);
        }
        const safeLink = elements.find((element) => (element.role === 'link'
            && element.href?.includes('wikipedia.org')
            && !element.href.includes('#')));
        if (safeLink) {
            const click = await runTool(toolMap.browser_click, { ref: safeLink.ref });
            console.log(`[browser_click] ${safeLink.name}: ${formatResult(click)}`);
        }
        else {
            console.log('[browser_click] skipped: no safe same-domain link found');
        }
        const screenshot = await runTool(toolMap.browser_screenshot, { name: 'toolkit-final-page' });
        console.log(`[browser_screenshot] ${formatResult(screenshot)}`);
    }
    finally {
        const close = await runTool(toolMap.browser_close, {});
        console.log(`[browser_close] ${formatResult(close)}`);
    }
}
async function runTool(tool, input) {
    if (!tool) {
        return { ok: false, error: 'Tool was not registered.' };
    }
    return await tool.execute(input);
}
function formatResult(result) {
    if (!result.ok) {
        return `failed ${result.error}`;
    }
    return JSON.stringify(result.output);
}
function resolveHeadlessMode(args) {
    if (args.includes('--headed')) {
        return false;
    }
    if (args.includes('--headless')) {
        return true;
    }
    return process.env.HEDDLE_BROWSER_HEADLESS !== 'false';
}
function resolveChannel(value) {
    const channels = ['chrome', 'chromium', 'msedge'];
    return channels.find((channel) => channel === value);
}
main().catch((error) => {
    console.error(error);
    process.exit(1);
});
//# sourceMappingURL=browser-research-toolkit.js.map