fix: remove local browser fallback — Kernel only, fail fast if unavailable

This commit is contained in:
2026-03-06 01:09:57 +00:00
parent f065a9a786
commit e49851e12a

View File

@@ -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 };
}