* New workspace page with agent catalog, composer modal, and terminal integration * fix lint * better blank state * fix: resolve typecheck errors in new-workspace flow - widen activateAndRevealWorktree's issueCommand to accept direct command shape used by NewWorkspacePage, not just the main-process runner-script variant - use a ref object in pty-connection tests to dodge TS narrowing the captured callback to never across the mock closure boundary * fix: bound worktree name auto-suffix loop to prevent OOM in tests The auto-suffix loop in createLocalWorktree had no termination cap. When tests (or a misconfigured env) mocked getBranchConflictKind / getPRForBranch to always return a collision, the loop ran forever and the vitest worker crashed with "Ineffective mark-compacts near heap limit". Cap the search at 100 suffixes, and if all fail, fall back to the original specific error messages (branch already exists / PR already owns the name) instead of a generic failure. Update the two tests that asserted the old throw-on-first-conflict behavior to verify the new auto-suffix path end-to-end.
145 lines
4.2 KiB
TypeScript
145 lines
4.2 KiB
TypeScript
import { ipcMain, shell, dialog } from 'electron'
|
|
import { constants, copyFile, stat } from 'node:fs/promises'
|
|
import { isAbsolute, normalize } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
async function pathExists(pathValue: string): Promise<boolean> {
|
|
try {
|
|
await stat(pathValue)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
export function registerShellHandlers(): void {
|
|
ipcMain.handle('shell:openPath', (_event, path: string) => {
|
|
shell.showItemInFolder(path)
|
|
})
|
|
|
|
ipcMain.handle('shell:openUrl', (_event, rawUrl: string) => {
|
|
let parsed: URL
|
|
try {
|
|
parsed = new URL(rawUrl)
|
|
} catch {
|
|
return
|
|
}
|
|
|
|
if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
|
|
return
|
|
}
|
|
|
|
return shell.openExternal(parsed.toString())
|
|
})
|
|
|
|
ipcMain.handle('shell:openFilePath', async (_event, filePath: string) => {
|
|
if (!isAbsolute(filePath)) {
|
|
return
|
|
}
|
|
const normalizedPath = normalize(filePath)
|
|
if (!(await pathExists(normalizedPath))) {
|
|
return
|
|
}
|
|
await shell.openPath(normalizedPath)
|
|
})
|
|
|
|
ipcMain.handle('shell:openFileUri', async (_event, rawUri: string) => {
|
|
let parsed: URL
|
|
try {
|
|
parsed = new URL(rawUri)
|
|
} catch {
|
|
return
|
|
}
|
|
|
|
if (parsed.protocol !== 'file:') {
|
|
return
|
|
}
|
|
|
|
// Only local files are supported. Remote hosts are intentionally rejected.
|
|
if (parsed.hostname && parsed.hostname !== 'localhost') {
|
|
return
|
|
}
|
|
|
|
let filePath: string
|
|
try {
|
|
filePath = fileURLToPath(parsed)
|
|
} catch {
|
|
return
|
|
}
|
|
|
|
const normalizedPath = normalize(filePath)
|
|
if (!isAbsolute(normalizedPath)) {
|
|
return
|
|
}
|
|
if (!(await pathExists(normalizedPath))) {
|
|
return
|
|
}
|
|
|
|
await shell.openPath(normalizedPath)
|
|
})
|
|
|
|
ipcMain.handle('shell:pathExists', async (_event, filePath: string): Promise<boolean> => {
|
|
return pathExists(filePath)
|
|
})
|
|
|
|
ipcMain.handle(
|
|
'shell:pickDirectory',
|
|
async (_event, args: { defaultPath?: string }): Promise<string | null> => {
|
|
const result = await dialog.showOpenDialog({
|
|
defaultPath: args.defaultPath,
|
|
properties: ['openDirectory', 'createDirectory']
|
|
})
|
|
if (result.canceled || result.filePaths.length === 0) {
|
|
return null
|
|
}
|
|
return result.filePaths[0]
|
|
}
|
|
)
|
|
|
|
// Why: window.prompt() and <input type="file"> are unreliable in Electron,
|
|
// so we use the native OS dialog to let the user pick any attachment file.
|
|
ipcMain.handle('shell:pickAttachment', async (): Promise<string | null> => {
|
|
const result = await dialog.showOpenDialog({
|
|
properties: ['openFile']
|
|
})
|
|
if (result.canceled || result.filePaths.length === 0) {
|
|
return null
|
|
}
|
|
return result.filePaths[0]
|
|
})
|
|
|
|
// Why: window.prompt() and <input type="file"> are unreliable in Electron,
|
|
// so we use the native OS dialog to let the user pick an image file.
|
|
ipcMain.handle('shell:pickImage', async (): Promise<string | null> => {
|
|
const result = await dialog.showOpenDialog({
|
|
properties: ['openFile'],
|
|
filters: [
|
|
{ name: 'Images', extensions: ['png', 'jpg', 'jpeg', 'gif', 'webp', 'svg', 'bmp', 'ico'] }
|
|
]
|
|
})
|
|
if (result.canceled || result.filePaths.length === 0) {
|
|
return null
|
|
}
|
|
return result.filePaths[0]
|
|
})
|
|
|
|
// Why: copying a picked image next to the markdown file lets us insert a
|
|
// relative path (e.g. ``) instead of embedding base64,
|
|
// keeping markdown files small and portable.
|
|
ipcMain.handle(
|
|
'shell:copyFile',
|
|
async (_event, args: { srcPath: string; destPath: string }): Promise<void> => {
|
|
const src = normalize(args.srcPath)
|
|
const dest = normalize(args.destPath)
|
|
if (!isAbsolute(src) || !isAbsolute(dest)) {
|
|
throw new Error('Both source and destination must be absolute paths')
|
|
}
|
|
// Why: COPYFILE_EXCL prevents silently overwriting an existing file.
|
|
// The renderer-side deconfliction loop already picks a unique name, so
|
|
// the dest should never exist — if it does, something is wrong and we
|
|
// should fail loudly rather than clobber data.
|
|
await copyFile(src, dest, constants.COPYFILE_EXCL)
|
|
}
|
|
)
|
|
}
|