import type { WebContents } from 'electron/main' import { app } from 'electron/main' function normalizeBooleanEnv(value: string | undefined): boolean | null { if (!value) { return null } const normalized = value.trim().toLowerCase() if (['1', 'true', 'yes', 'on'].includes(normalized)) { return true } if (['0', 'false', 'no', 'off'].includes(normalized)) { return false } return null } export function shouldAutoOpenExecutionDevtools(): boolean { if (app.isPackaged) { return false } const explicit = normalizeBooleanEnv(process.env.GEO_DESKTOP_EXECUTION_DEVTOOLS) if (explicit !== null) { return explicit } return Boolean(process.env.ELECTRON_RENDERER_URL) } export function shouldAutoOpenBackgroundExecutionDevtools(): boolean { if (app.isPackaged) { return false } const explicit = normalizeBooleanEnv(process.env.GEO_DESKTOP_EXECUTION_DEVTOOLS) if (explicit !== null) { return explicit } return false } export function maybeOpenExecutionDevtools( webContents: WebContents, label: string, options: { background?: boolean } = {}, ): void { const allowOpen = options.background ? shouldAutoOpenBackgroundExecutionDevtools() : shouldAutoOpenExecutionDevtools() if (!allowOpen) { return } if (webContents.isDestroyed() || webContents.isDevToolsOpened()) { return } try { webContents.openDevTools({ mode: 'detach' }) console.info('[desktop-devtools] opened execution devtools', { label }) } catch (error) { console.warn('[desktop-devtools] failed to open execution devtools', { label, error }) } }