mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-16 13:03:31 +08:00
Introduce the Electron desktop shell alongside the existing React renderer and local Bun server boundary. The migration keeps the DesktopHost contract explicit across Tauri, Electron, and browser runtimes while adding Electron main/preload services for dialogs, shell, notifications, updates, tray/window lifecycle, terminal, preview WebContentsView, app mode, and release/package validation.
The commit also carries the latest local main desktop command updates, including agent slash entries and hidden-by-default markdown thinking details, so the packaged Electron build matches the current main UX surface.
Constraint: React renderer, local Bun server, REST/WebSocket, and sidecar boundaries must remain reusable during the migration
Constraint: macOS dev packages are ad-hoc signed and cannot prove Developer ID notarization or Gatekeeper release launch
Rejected: Browser-only smoke validation | it cannot exercise native dialogs, keychain prompts, notification behavior, or packaged app startup
Confidence: medium
Scope-risk: broad
Directive: Do not remove Tauri host support until signed Electron release artifacts pass native OS smoke on macOS, Windows, and Linux
Tested: bun run check:desktop
Tested: cd desktop && bun run check:electron
Tested: CSC_IDENTITY_AUTO_DISCOVERY=false bun run electron📦dir
Tested: bun run test:package-smoke --platform macos --package-kind dir --artifacts-dir desktop/build-artifacts/electron
Tested: Computer Use read packaged Electron app window at desktop/build-artifacts/electron/mac-arm64/Claude Code Haha.app
Not-tested: Developer ID signed/notarized Gatekeeper launch
Not-tested: Real OS notification click-to-session action
Not-tested: Windows and Linux packaged app smoke on real hosts
158 lines
4.9 KiB
TypeScript
158 lines
4.9 KiB
TypeScript
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import process from 'node:process'
|
|
import type { AppModeConfig, AppModeSetInput } from '../../src/lib/desktopHost/types'
|
|
|
|
const APP_MODE_FILE = 'app-mode.json'
|
|
|
|
export type AppModeAppLike = {
|
|
getPath(name: 'exe' | 'userData'): string
|
|
}
|
|
|
|
type PersistedAppModeConfig = {
|
|
mode?: string
|
|
portable_dir?: string | null
|
|
}
|
|
|
|
export type PortableDetection = {
|
|
defaultPortableDir: string | null
|
|
hasData: boolean
|
|
}
|
|
|
|
export function defaultPortableDir(app: AppModeAppLike): string {
|
|
return path.join(path.dirname(app.getPath('exe')), 'CLAUDE_CONFIG_DIR')
|
|
}
|
|
|
|
export function dirHasPortableData(dir: string): boolean {
|
|
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) return false
|
|
return [
|
|
'settings.json',
|
|
'.claude.json',
|
|
'.mcp.json',
|
|
'window-state.json',
|
|
'terminal-config.json',
|
|
].some(file => fs.existsSync(path.join(dir, file)) && fs.statSync(path.join(dir, file)).isFile())
|
|
|| [
|
|
'Cache',
|
|
'EBWebView',
|
|
'projects',
|
|
'skills',
|
|
'plugins',
|
|
'cowork_plugins',
|
|
'cc-haha',
|
|
].some(file => fs.existsSync(path.join(dir, file)) && fs.statSync(path.join(dir, file)).isDirectory())
|
|
}
|
|
|
|
export function readAppModeConfig(configDir: string): PersistedAppModeConfig | null {
|
|
try {
|
|
const parsed = JSON.parse(fs.readFileSync(path.join(configDir, APP_MODE_FILE), 'utf8')) as PersistedAppModeConfig
|
|
return {
|
|
mode: typeof parsed.mode === 'string' ? parsed.mode.toLowerCase() : 'default',
|
|
portable_dir: typeof parsed.portable_dir === 'string' ? parsed.portable_dir : null,
|
|
}
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
export function writeAppModeConfig(configDir: string, config: PersistedAppModeConfig): void {
|
|
fs.mkdirSync(configDir, { recursive: true })
|
|
fs.writeFileSync(path.join(configDir, APP_MODE_FILE), JSON.stringify(config, null, 2))
|
|
}
|
|
|
|
export function determineStartupPortableDir(
|
|
app: AppModeAppLike,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): string | null {
|
|
if (env.CLAUDE_CONFIG_DIR) return null
|
|
|
|
const defaultDir = defaultPortableDir(app)
|
|
const defaultMode = readAppModeConfig(defaultDir)
|
|
if (defaultMode) {
|
|
if (defaultMode.mode === 'portable') {
|
|
return dirHasPortableData(defaultDir) ? defaultDir : defaultMode.portable_dir ?? defaultDir
|
|
}
|
|
return null
|
|
}
|
|
|
|
const systemMode = readAppModeConfig(app.getPath('userData'))
|
|
if (systemMode) {
|
|
if (systemMode.mode === 'portable') return systemMode.portable_dir ?? defaultDir
|
|
return null
|
|
}
|
|
|
|
return dirHasPortableData(defaultDir) ? defaultDir : null
|
|
}
|
|
|
|
export function applyStartupPortableMode(
|
|
app: AppModeAppLike,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): string | null {
|
|
const portableDir = determineStartupPortableDir(app, env)
|
|
if (!portableDir) return null
|
|
env.CLAUDE_CONFIG_DIR = portableDir
|
|
env.CC_HAHA_APP_PORTABLE_DIR = '1'
|
|
env.WEBVIEW2_USER_DATA_FOLDER = path.join(portableDir, 'EBWebView')
|
|
fs.mkdirSync(env.WEBVIEW2_USER_DATA_FOLDER, { recursive: true })
|
|
return portableDir
|
|
}
|
|
|
|
export function getAppMode(
|
|
app: AppModeAppLike,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): AppModeConfig {
|
|
const envConfigDir = env.CLAUDE_CONFIG_DIR || null
|
|
const activeConfigDir = envConfigDir || app.getPath('userData')
|
|
const portableDir = envConfigDir || defaultPortableDir(app)
|
|
return {
|
|
mode: envConfigDir ? 'portable' : 'default',
|
|
portableDir,
|
|
defaultPortableDir: defaultPortableDir(app),
|
|
activeConfigDir,
|
|
configDirSource: envConfigDir
|
|
? env.CC_HAHA_APP_PORTABLE_DIR ? 'portable' : 'environment'
|
|
: 'system',
|
|
}
|
|
}
|
|
|
|
export function setAppMode(
|
|
app: AppModeAppLike,
|
|
input: AppModeSetInput,
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
): void {
|
|
const activeConfigDir = env.CLAUDE_CONFIG_DIR || app.getPath('userData')
|
|
let config: PersistedAppModeConfig = { mode: 'default', portable_dir: null }
|
|
let targetPortableDir: string | null = null
|
|
|
|
if (input.mode === 'portable') {
|
|
const selectedDir = input.portableDir?.trim() || defaultPortableDir(app)
|
|
if (fs.existsSync(selectedDir) && !fs.statSync(selectedDir).isDirectory()) {
|
|
throw new Error(`portable config path is not a directory: ${selectedDir}`)
|
|
}
|
|
fs.mkdirSync(selectedDir, { recursive: true })
|
|
targetPortableDir = selectedDir
|
|
config = {
|
|
mode: 'portable',
|
|
portable_dir: selectedDir === defaultPortableDir(app) ? null : selectedDir,
|
|
}
|
|
}
|
|
|
|
writeAppModeConfig(activeConfigDir, config)
|
|
if (targetPortableDir && targetPortableDir !== activeConfigDir) {
|
|
writeAppModeConfig(targetPortableDir, config)
|
|
}
|
|
|
|
const systemConfigDir = app.getPath('userData')
|
|
if (systemConfigDir !== activeConfigDir) {
|
|
writeAppModeConfig(systemConfigDir, config)
|
|
}
|
|
}
|
|
|
|
export function detectPortableDir(app: AppModeAppLike): PortableDetection {
|
|
const portableDir = defaultPortableDir(app)
|
|
return {
|
|
defaultPortableDir: portableDir,
|
|
hasData: dirHasPortableData(portableDir),
|
|
}
|
|
}
|