cc-haha/desktop/electron/services/notificationSmoke.ts
程序员阿江(Relakkes) 386a41e606 feat(desktop): enable Electron migration path
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
2026-06-01 22:43:16 +08:00

160 lines
4.5 KiB
TypeScript

import { appendFileSync, mkdirSync } from 'node:fs'
import { dirname } from 'node:path'
import {
sendDesktopNotification,
type ElectronNotificationConstructor,
} from './notifications'
const DEFAULT_DELAY_MS = 2500
const MAX_DELAY_MS = 60_000
export type NotificationSmokeTimer = (handler: () => void, delayMs: number) => unknown
export type NotificationSmokeLogEvent =
| {
event: 'scheduled'
timestamp: string
sessionId: string
title: string
body: string
delayMs: number
}
| {
event: 'sent'
timestamp: string
sessionId: string
sent: boolean
}
| {
event: 'action'
timestamp: string
sessionId: string
payload: unknown
}
| {
event: 'synthetic_action'
timestamp: string
sessionId: string
payload: unknown
}
| {
event: 'renderer_ack'
timestamp: string
payload: unknown
}
| {
event: 'lifecycle'
timestamp: string
sessionId: string
lifecycle: 'close' | 'failed'
}
| {
event: 'send_failed'
timestamp: string
sessionId: string
error: string
}
export type NotificationSmokeLogWriter = (event: NotificationSmokeLogEvent) => void
export function parseNotificationSmokeDelay(value: string | undefined): number {
if (!value) return DEFAULT_DELAY_MS
const parsed = Number(value)
if (!Number.isFinite(parsed)) return DEFAULT_DELAY_MS
return Math.min(Math.max(Math.round(parsed), 0), MAX_DELAY_MS)
}
function shouldTriggerSyntheticAction(value: string | undefined): boolean {
return value === '1' || value?.toLowerCase() === 'true'
}
export function appendNotificationSmokeLog(logPath: string, event: NotificationSmokeLogEvent) {
mkdirSync(dirname(logPath), { recursive: true })
appendFileSync(logPath, `${JSON.stringify(event)}\n`)
}
export function logNotificationSmokeRendererAck(env: NodeJS.ProcessEnv, payload: unknown): boolean {
const logPath = env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_LOG?.trim()
if (!logPath) return false
appendNotificationSmokeLog(logPath, {
event: 'renderer_ack',
timestamp: new Date().toISOString(),
payload,
})
return true
}
export function scheduleNotificationSmoke({
env,
NotificationClass,
onAction,
setTimer = setTimeout,
writeLog,
}: {
env: NodeJS.ProcessEnv
NotificationClass: ElectronNotificationConstructor
onAction: (payload: unknown) => void
setTimer?: NotificationSmokeTimer
writeLog?: NotificationSmokeLogWriter
}): boolean {
const sessionId = env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_SESSION_ID?.trim()
if (!sessionId) return false
const title = env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_TITLE?.trim() || 'Claude Code Haha notification smoke'
const body = env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_BODY?.trim() || 'Click to return to the target session.'
const delayMs = parseNotificationSmokeDelay(env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_DELAY_MS)
const triggerSyntheticAction = shouldTriggerSyntheticAction(env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_TRIGGER_ACTION)
const logPath = env.CC_HAHA_ELECTRON_NOTIFICATION_SMOKE_LOG?.trim()
const log: NotificationSmokeLogWriter = (event) => {
if (writeLog) {
writeLog(event)
return
}
if (logPath) appendNotificationSmokeLog(logPath, event)
}
log({ event: 'scheduled', timestamp: new Date().toISOString(), sessionId, title, body, delayMs })
setTimer(() => {
try {
const target = {
type: 'session' as const,
sessionId,
title,
}
const sent = sendDesktopNotification({
NotificationClass,
options: {
title,
body,
target,
},
onAction: (payload) => {
log({ event: 'action', timestamp: new Date().toISOString(), sessionId, payload })
onAction(payload)
},
onLifecycle: (lifecycle) => {
log({ event: 'lifecycle', timestamp: new Date().toISOString(), sessionId, lifecycle })
},
})
log({ event: 'sent', timestamp: new Date().toISOString(), sessionId, sent })
if (sent && triggerSyntheticAction) {
const payload = {
target,
action: 'synthetic-click',
}
log({ event: 'synthetic_action', timestamp: new Date().toISOString(), sessionId, payload })
onAction(payload)
}
} catch (error) {
log({
event: 'send_failed',
timestamp: new Date().toISOString(),
sessionId,
error: error instanceof Error ? error.message : String(error),
})
throw error
}
}, delayMs)
return true
}