From e49851e12a0d507cd0d1778f81e7327eb5adba45 Mon Sep 17 00:00:00 2001 From: Claw Date: Fri, 6 Mar 2026 01:09:57 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20remove=20local=20browser=20fallback=20?= =?UTF-8?q?=E2=80=94=20Kernel=20only,=20fail=20fast=20if=20unavailable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/browser.mjs | 50 ++++++++++++------------------------------------- 1 file changed, 12 insertions(+), 38 deletions(-) diff --git a/lib/browser.mjs b/lib/browser.mjs index 7c5c25a..7687bae 100644 --- a/lib/browser.mjs +++ b/lib/browser.mjs @@ -1,18 +1,13 @@ /** * browser.mjs — Browser factory - * Creates Kernel stealth browsers or falls back to local Playwright + * Creates Kernel stealth browsers for all operations */ -import { LOCAL_USER_AGENT, KERNEL_SDK_PATH, DEFAULT_PLAYWRIGHT_PATH } from './constants.mjs'; +import { KERNEL_SDK_PATH, DEFAULT_PLAYWRIGHT_PATH } from './constants.mjs'; -// Use configured playwright path or fall back to npm global let _chromium; async function getChromium(playwrightPath) { if (_chromium) return _chromium; - const paths = [ - playwrightPath, - DEFAULT_PLAYWRIGHT_PATH, - 'playwright' - ].filter(Boolean); + const paths = [playwrightPath, DEFAULT_PLAYWRIGHT_PATH, 'playwright'].filter(Boolean); for (const p of paths) { try { const m = await import(p); _chromium = m.chromium; return _chromium; } catch {} } @@ -20,25 +15,12 @@ async function getChromium(playwrightPath) { } export async function createBrowser(settings, profileKey) { - const { provider, playwright_path } = settings.browser || {}; const kernelConfig = settings.kernel || {}; + const playwrightPath = settings.browser?.playwright_path; + const apiKey = process.env.KERNEL_API_KEY || settings.kernel_api_key; - const pwPath = settings.browser?.playwright_path; + if (!apiKey) throw new Error('KERNEL_API_KEY not set'); - if (provider === 'local') { - return createLocalBrowser(pwPath); - } - - // Default: Kernel - try { - return await createKernelBrowser(kernelConfig, profileKey, pwPath); - } catch (e) { - console.warn(`[browser] Kernel failed (${e.message}), falling back to local`); - return createLocalBrowser(pwPath); - } -} - -async function createKernelBrowser(kernelConfig, profileKey, playwrightPath) { let Kernel; try { const mod = await import(KERNEL_SDK_PATH); @@ -47,13 +29,13 @@ async function createKernelBrowser(kernelConfig, profileKey, playwrightPath) { throw new Error('Kernel SDK not installed — run: npm install @onkernel/sdk'); } - if (!process.env.KERNEL_API_KEY) throw new Error('KERNEL_API_KEY not set'); - const kernel = new Kernel({ apiKey: process.env.KERNEL_API_KEY }); + const kernel = new Kernel({ apiKey }); - const profileName = kernelConfig.profiles?.[profileKey]; - if (!profileName) throw new Error(`No Kernel profile configured for "${profileKey}"`); + const profileName = profileKey ? kernelConfig.profiles?.[profileKey] : null; + if (profileKey && !profileName) throw new Error(`No Kernel profile configured for "${profileKey}"`); - const opts = { stealth: true, profile: { name: profileName } }; + const opts = { stealth: true }; + if (profileName) opts.profile = { name: profileName }; if (kernelConfig.proxy_id) opts.proxy = { id: kernelConfig.proxy_id }; const kb = await kernel.browsers.create(opts); @@ -62,13 +44,5 @@ async function createKernelBrowser(kernelConfig, profileKey, playwrightPath) { const ctx = browser.contexts()[0] || await browser.newContext(); const page = ctx.pages()[0] || await ctx.newPage(); - return { browser, page, type: 'kernel' }; -} - -async function createLocalBrowser(playwrightPath) { - const chromium = await getChromium(playwrightPath); - const browser = await chromium.launch({ headless: true }); - const ctx = await browser.newContext({ userAgent: LOCAL_USER_AGENT }); - const page = await ctx.newPage(); - return { browser, page, type: 'local' }; + return { browser, page, type: 'kernel', kernel, kernelBrowserId: kb.id }; }