cc-haha/desktop/electron/services/serverRuntime.ts
程序员阿江(Relakkes) 16f4137954 fix(desktop): stabilize Electron proxy and fullscreen handling
Electron desktop runs network-sensitive OpenAI OAuth token exchange in the sidecar process, so the sidecar now receives system proxy env derived from Electron's cross-platform proxy resolver and the OAuth token client uses the existing proxy fetch options. General manual proxy settings also document and preserve authenticated proxy URLs.

The macOS fullscreen black-screen path is addressed by avoiding native fullscreen Spaces for app fullscreen toggles and by leaving fullscreen before hiding or closing the window.

Constraint: Electron packaged apps may not inherit shell HTTPS_PROXY env when launched from Finder.
Constraint: Manual authenticated proxies must remain standard HTTP(S) proxy URLs for Bun/undici compatibility.
Rejected: Store proxy username and password as separate fields | would require new secret-storage semantics and migration beyond this bugfix.
Rejected: Use native macOS fullscreen Spaces for the desktop app | reproduced black-screen behavior when hiding/closing from fullscreen.
Confidence: high
Scope-risk: moderate
Directive: Do not remove sidecar proxy env injection without retesting OpenAI OAuth from a packaged app launched outside a shell.
Tested: bun test src/services/openaiAuth/client.test.ts src/server/__tests__/haha-openai-oauth-service.test.ts
Tested: bun test src/server/__tests__/network-settings.test.ts
Tested: cd desktop && bun run test -- src/__tests__/generalSettings.test.tsx --run
Tested: cd desktop && bun run check:electron
Tested: git diff --check
Not-tested: bun run check:server blocked by expired quarantine entries server:cron-scheduler, server:providers-real, server:tasks, server:e2e:business-flow, server:e2e:full-flow
Not-tested: live OpenAI OAuth through a corporate authenticated proxy
2026-06-02 21:37:35 +08:00

174 lines
5.2 KiB
TypeScript

import path from 'node:path'
import {
createAdapterPlan,
createServerPlan,
formatStartupError,
killSidecar,
mergeProxyEnv,
proxyUrlFromElectronProxyRules,
pushStartupLog,
reserveLocalPort,
SERVER_BIND_HOST,
SERVER_CONTROL_HOST,
spawnSidecar,
waitForServer,
type SidecarChild,
} from './sidecarManager'
type ServerRuntimeOptions = {
desktopRoot: string
appRoot?: string
h5DistDir?: string
resolveSystemProxy?: (url: string) => Promise<string>
}
export class ElectronServerRuntime {
private readonly desktopRoot: string
private readonly appRoot: string
private readonly h5DistDir: string
private readonly resolveSystemProxy?: (url: string) => Promise<string>
private sidecarEnvPromise: Promise<NodeJS.ProcessEnv> | null = null
private server: { url: string, child: SidecarChild } | null = null
private adapters: SidecarChild[] = []
private startupError: string | null = null
private startPromise: Promise<string> | null = null
constructor(options: ServerRuntimeOptions) {
this.desktopRoot = options.desktopRoot
this.appRoot = options.appRoot ?? options.desktopRoot
this.h5DistDir = options.h5DistDir ?? path.join(options.desktopRoot, 'dist')
this.resolveSystemProxy = options.resolveSystemProxy
}
async startServer(): Promise<string> {
if (this.server) return this.server.url
if (this.startPromise) return this.startPromise
this.startPromise = this.startServerOnce()
try {
return await this.startPromise
} finally {
this.startPromise = null
}
}
async getServerUrl(): Promise<string> {
if (this.server) return this.server.url
if (this.startupError) throw new Error(this.startupError)
return await this.startServer()
}
async restartAdaptersSidecars(): Promise<void> {
this.stopAdaptersSidecars()
const serverUrl = await this.getServerUrl()
await this.startAdaptersSidecars(serverUrl)
}
stopAll() {
this.stopAdaptersSidecars()
if (this.server) {
killSidecar(this.server.child)
this.server = null
}
}
private async startServerOnce(): Promise<string> {
const port = await reserveLocalPort(SERVER_BIND_HOST)
const url = `http://${SERVER_CONTROL_HOST}:${port}`
const logs: string[] = []
const env = await this.resolveSidecarBaseEnv()
const plan = createServerPlan({
desktopRoot: this.desktopRoot,
appRoot: this.appRoot,
port,
h5DistDir: this.h5DistDir,
env,
})
try {
const child = spawnSidecar(plan)
this.captureLogs(child, 'claude-server', logs)
await waitForServer(SERVER_CONTROL_HOST, port)
this.server = { url, child }
this.startupError = null
await this.startAdaptersSidecars(url)
return url
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
this.startupError = formatStartupError(message, logs)
throw new Error(this.startupError)
}
}
private async startAdaptersSidecars(serverUrl: string): Promise<void> {
const env = await this.resolveSidecarBaseEnv()
for (const [label, flag] of [
['feishu', '--feishu'],
['telegram', '--telegram'],
['wechat', '--wechat'],
['dingtalk', '--dingtalk'],
] as const) {
try {
const child = spawnSidecar(createAdapterPlan({
desktopRoot: this.desktopRoot,
appRoot: this.appRoot,
h5DistDir: this.h5DistDir,
serverUrl,
flag,
env,
}))
this.captureLogs(child, `claude-adapters:${label}`)
this.adapters.push(child)
} catch (error) {
console.error(`[desktop] failed to start ${label} adapter sidecar`, error)
}
}
}
private stopAdaptersSidecars() {
for (const child of this.adapters.splice(0)) {
killSidecar(child)
}
}
private captureLogs(child: SidecarChild, label: string, startupLogs?: string[]) {
child.stdout.on('data', chunk => {
const line = String(chunk).trimEnd()
if (!line) return
console.log(`[${label}] ${line}`)
if (startupLogs) pushStartupLog(startupLogs, `[stdout] ${line}`)
})
child.stderr.on('data', chunk => {
const line = String(chunk).trimEnd()
if (!line) return
console.error(`[${label}] ${line}`)
if (startupLogs) pushStartupLog(startupLogs, `[stderr] ${line}`)
})
child.on('exit', (code, signal) => {
const line = `sidecar exited (code=${code}, signal=${signal})`
console.log(`[${label}] ${line}`)
if (startupLogs) pushStartupLog(startupLogs, `[exit] ${line}`)
})
}
private async resolveSidecarBaseEnv(): Promise<NodeJS.ProcessEnv> {
this.sidecarEnvPromise ??= this.resolveSidecarBaseEnvOnce()
return await this.sidecarEnvPromise
}
private async resolveSidecarBaseEnvOnce(): Promise<NodeJS.ProcessEnv> {
if (!this.resolveSystemProxy) return process.env
try {
const rules = await this.resolveSystemProxy('https://auth.openai.com/')
return mergeProxyEnv(
process.env,
proxyUrlFromElectronProxyRules(rules),
)
} catch (error) {
console.error('[desktop] failed to resolve system proxy for sidecars', error)
return process.env
}
}
}