dify/cli/src/util/browser.ts
Yunlu Wen c0ee821d45
refactor: use absolute path for inter dir importing (#36822)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-06-01 01:32:16 +00:00

53 lines
1.4 KiB
TypeScript

import openModule from 'open'
import { platform } from '@/sys'
export const OpenDecision = {
Auto: 'auto-open',
SkipSSH: 'Detected SSH session',
SkipHeadlessLinux: 'Headless Linux (no DISPLAY / WAYLAND_DISPLAY)',
SkipNoTTY: 'Non-interactive TTY',
SkipUserOptOut: '--no-browser requested',
} as const
export type OpenDecision = typeof OpenDecision[keyof typeof OpenDecision]
export type BrowserEnv = {
getEnv: (key: string) => string | undefined
platform: NodeJS.Platform
isOutTTY: boolean
isErrTTY: boolean
}
export function realEnv(): BrowserEnv {
return {
getEnv: k => process.env[k],
platform: platform(),
isOutTTY: Boolean(process.stdout.isTTY),
isErrTTY: Boolean(process.stderr.isTTY),
}
}
export function decideOpen(env: BrowserEnv, userOptOut: boolean): OpenDecision {
if (userOptOut)
return OpenDecision.SkipUserOptOut
if (truthy(env.getEnv('SSH_CONNECTION')) || truthy(env.getEnv('SSH_TTY')))
return OpenDecision.SkipSSH
if (env.platform === 'linux'
&& !truthy(env.getEnv('DISPLAY'))
&& !truthy(env.getEnv('WAYLAND_DISPLAY'))) {
return OpenDecision.SkipHeadlessLinux
}
if (!env.isOutTTY || !env.isErrTTY)
return OpenDecision.SkipNoTTY
return OpenDecision.Auto
}
export type BrowserOpener = (url: string) => Promise<void>
export const openUrl: BrowserOpener = async (url) => {
await openModule(url)
}
function truthy(v: string | undefined): boolean {
return v !== undefined && v !== ''
}