mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-17 13:13:35 +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
112 lines
3.5 KiB
TypeScript
112 lines
3.5 KiB
TypeScript
import { isDesktopRuntime } from './desktopRuntime'
|
|
import { getDesktopHost } from './desktopHost'
|
|
|
|
export type ComposerAttachment = {
|
|
id: string
|
|
name: string
|
|
type: 'image' | 'file'
|
|
path?: string
|
|
mimeType?: string
|
|
previewUrl?: string
|
|
data?: string
|
|
isDirectory?: boolean
|
|
lineStart?: number
|
|
lineEnd?: number
|
|
note?: string
|
|
quote?: string
|
|
}
|
|
|
|
function nextAttachmentId() {
|
|
return `att-${Date.now()}-${Math.random().toString(36).slice(2)}`
|
|
}
|
|
|
|
export function getFileNameFromPath(filePath: string): string {
|
|
const normalized = filePath.replace(/[\\/]+$/g, '')
|
|
return normalized.split(/[\\/]/).filter(Boolean).pop() || filePath
|
|
}
|
|
|
|
export function pathToComposerAttachment(filePath: string): ComposerAttachment {
|
|
return {
|
|
id: nextAttachmentId(),
|
|
name: getFileNameFromPath(filePath),
|
|
type: 'file',
|
|
path: filePath,
|
|
}
|
|
}
|
|
|
|
export function pathsToComposerAttachments(filePaths: string[]): ComposerAttachment[] {
|
|
return filePaths
|
|
.filter((filePath) => typeof filePath === 'string' && filePath.length > 0)
|
|
.map(pathToComposerAttachment)
|
|
}
|
|
|
|
export function dataTransferHasFiles(dataTransfer: DataTransfer): boolean {
|
|
const types = Array.from(dataTransfer.types ?? [])
|
|
return types.includes('Files') || dataTransfer.files.length > 0
|
|
}
|
|
|
|
export async function dataTransferToComposerAttachments(dataTransfer: DataTransfer): Promise<ComposerAttachment[]> {
|
|
return filesToComposerAttachments(dataTransfer.files)
|
|
}
|
|
|
|
export async function selectNativeFileAttachments(): Promise<ComposerAttachment[] | null> {
|
|
const host = getDesktopHost()
|
|
if (!host.isDesktop || !host.capabilities.dialogs) return null
|
|
|
|
try {
|
|
const selected = await host.dialogs.open({
|
|
multiple: true,
|
|
directory: false,
|
|
})
|
|
const paths = normalizeDialogSelection(selected)
|
|
return pathsToComposerAttachments(paths)
|
|
} catch (error) {
|
|
console.warn('[attachments] Native file picker failed; falling back to browser file input', error)
|
|
return null
|
|
}
|
|
}
|
|
|
|
export async function filesToComposerAttachments(files: FileList | File[]): Promise<ComposerAttachment[]> {
|
|
const entries = Array.from(files)
|
|
const attachments = await Promise.all(entries.map(fileToComposerAttachment))
|
|
return attachments.filter((attachment): attachment is ComposerAttachment => !!attachment)
|
|
}
|
|
|
|
function normalizeDialogSelection(selected: string | string[] | null): string[] {
|
|
if (!selected) return []
|
|
const paths = Array.isArray(selected) ? selected : [selected]
|
|
return paths.filter((filePath) => typeof filePath === 'string' && filePath.length > 0)
|
|
}
|
|
|
|
function getNativeFilePath(file: File): string | undefined {
|
|
const path = (file as File & { path?: unknown }).path
|
|
return typeof path === 'string' && path.length > 0 ? path : undefined
|
|
}
|
|
|
|
async function fileToComposerAttachment(file: File): Promise<ComposerAttachment | null> {
|
|
const nativePath = isDesktopRuntime() ? getNativeFilePath(file) : undefined
|
|
if (nativePath) {
|
|
return pathToComposerAttachment(nativePath)
|
|
}
|
|
|
|
const isImage = file.type.startsWith('image/')
|
|
const data = await readFileAsDataUrl(file)
|
|
return {
|
|
id: nextAttachmentId(),
|
|
name: file.name,
|
|
type: isImage ? 'image' : 'file',
|
|
mimeType: file.type || undefined,
|
|
previewUrl: isImage ? data : undefined,
|
|
data,
|
|
}
|
|
}
|
|
|
|
function readFileAsDataUrl(file: File): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader()
|
|
reader.onload = () => resolve(reader.result as string)
|
|
reader.onerror = () => reject(reader.error ?? new Error(`Failed to read ${file.name}`))
|
|
reader.readAsDataURL(file)
|
|
})
|
|
}
|