import type { PathExistenceResult } from '../../shared/file-access/path-existence-batch' import { ipcRenderer, webUtils } from 'electron' import type { PrepareDroppedPathsRequest, PreparedDroppedPaths } from '../../shared/file-import/native-file-drop-preparation' import type { SshMutationExpectation } from '../../shared/ssh-types' import type { LocalFileAccess } from '../../shared/file-access/local-file-access' import type { RemoteFilePreviewAddress } from '../../shared/file-preview/remote-file-preview-address' import type { FileBytesWritePrecondition } from '../../shared/file-write/file-bytes-write' import type { FileWriteDiskPrecondition, FileWriteResult } from '../../shared/file-write/file-write-disk-precondition' import type { RuntimeUploadFileStreamRequest, StagedRuntimeUploadSource as StagedExternalImportSource } from '../../shared/file-import/runtime-upload-staging-contract' import type { AgentSessionAttachmentPathUploadResult, AgentSessionAttachmentUploadTarget } from '../../shared/agent-session/agent-session-attachments' import type { SearchResult } from '../../shared/file-search/code-search-types' import type { FsChangedPayload } from '../../shared/file-watch/filesystem-change-event' import type { ImportItemResult, ResolveDroppedPathsResult } from '../../shared/file-import/filesystem-import-result' import type { LocalLogTailChangedPayload, LocalLogTailReadArgs, LocalLogTailReadResult, LocalLogTailWatchArgs } from '../../shared/local-log-tail-types' import type { PreloadApi } from '../api-types' export const fsApi = { getPathForFile: (file: File): string => webUtils.getPathForFile(file), prepareDroppedPaths: (args: PrepareDroppedPathsRequest): Promise => ipcRenderer.invoke('fs:prepareDroppedPaths', args), readFileChunk: (args: { filePath: string connectionId?: string access?: LocalFileAccess offset: number length: number wakeWslDistro?: boolean }) => ipcRenderer.invoke('fs:readFileChunk', args), readDir: (args: { dirPath: string connectionId?: string followSymlinks?: boolean wakeWslDistro?: boolean access?: LocalFileAccess }): Promise<{ name: string; isDirectory: boolean; isSymlink: boolean }[]> => ipcRenderer.invoke('fs:readDir', args), readFile: (args: { filePath: string connectionId?: string includeLocalLogMetadata?: boolean access?: LocalFileAccess wakeWslDistro?: boolean rangePreview?: boolean }): Promise<{ content: string isBinary: boolean isImage?: boolean mediaUrl?: string size?: number mimeType?: string fileIdentity?: string }> => ipcRenderer.invoke('fs:readFile', args), readPreviewRange: (args: { url: string; offset: number; length: number }): Promise => ipcRenderer.invoke('fs:readPreviewRange', args), grantRuntimeFilePreview: ( args: RemoteFilePreviewAddress ): Promise<{ content: string isBinary: true mimeType: string mediaUrl: string isImage?: true size?: number } | null> => ipcRenderer.invoke('fs:grantRuntimeFilePreview', args), readLocalLogTail: (args: LocalLogTailReadArgs): Promise => ipcRenderer.invoke('fs:readLocalLogTail', args), startLocalLogTail: (args: LocalLogTailWatchArgs): Promise => ipcRenderer.invoke('fs:startLocalLogTail', args), stopLocalLogTail: (args: { subscriptionId: string }): Promise => ipcRenderer.invoke('fs:stopLocalLogTail', args), onLocalLogTailChanged: ( callback: (payload: LocalLogTailChangedPayload) => void ): (() => void) => { const listener = ( _event: Electron.IpcRendererEvent, payload: LocalLogTailChangedPayload ): void => callback(payload) ipcRenderer.on('fs:localLogTailChanged', listener) return () => ipcRenderer.removeListener('fs:localLogTailChanged', listener) }, downloadFile: (args: { filePath: string connectionId: string }): Promise<{ canceled: true } | { canceled: false; destinationPath: string }> => ipcRenderer.invoke('fs:downloadFile', args), downloadFolder: (args: { dirPath: string connectionId: string }): Promise<{ canceled: true } | { canceled: false; destinationPath: string }> => ipcRenderer.invoke('fs:downloadFolder', args), saveDownloadedFile: (args: { suggestedName: string content: string encoding: 'utf8' | 'base64' }): Promise<{ canceled: true } | { canceled: false; destinationPath: string }> => ipcRenderer.invoke('fs:saveDownloadedFile', args), startDownloadedFile: (args: { suggestedName: string }): Promise< { canceled: true } | { canceled: false; transferId: string; destinationPath: string } > => ipcRenderer.invoke('fs:startDownloadedFile', args), appendDownloadedFileChunk: (args: { transferId: string contentBase64: string }): Promise<{ ok: true }> => ipcRenderer.invoke('fs:appendDownloadedFileChunk', args), finishDownloadedFile: (args: { transferId: string }): Promise<{ canceled: false; destinationPath: string }> => ipcRenderer.invoke('fs:finishDownloadedFile', args), cancelDownloadedFile: (args: { transferId: string }): Promise<{ ok: true }> => ipcRenderer.invoke('fs:cancelDownloadedFile', args), listMarkdownDocuments: (args: { rootPath: string connectionId?: string }): Promise<{ filePath: string; relativePath: string; basename: string; name: string }[]> => ipcRenderer.invoke('fs:listMarkdownDocuments', args), writeFile: ( args: { filePath: string content: string connectionId?: string access?: LocalFileAccess diskPrecondition?: FileWriteDiskPrecondition } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:writeFile', args), writeFileBytes: ( args: { filePath: string bytes: Uint8Array connectionId?: string access?: LocalFileAccess diskPrecondition: FileBytesWritePrecondition } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:writeFileBytes', args), createFile: ( args: { filePath: string; connectionId?: string } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:createFile', args), createDir: ( args: { dirPath: string connectionId?: string followSymlinks?: boolean } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:createDir', args), rename: ( args: { oldPath: string newPath: string connectionId?: string access?: LocalFileAccess } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:rename', args), copy: ( args: { sourcePath: string destinationPath: string connectionId?: string } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:copy', args), deletePath: ( args: { targetPath: string connectionId?: string recursive?: boolean permanent?: boolean } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:deletePath', args), stat: (args: { filePath: string connectionId?: string access?: LocalFileAccess wakeWslDistro?: boolean }): Promise<{ size: number; isDirectory: boolean; mtime: number }> => ipcRenderer.invoke('fs:stat', args), hasSymlinkBelowRoot: (args: { rootPath: string; path: string }): Promise => ipcRenderer.invoke('fs:hasSymlinkBelowRoot', args), pathsExist: (args: { filePaths: string[] connectionId?: string }): Promise => ipcRenderer.invoke('fs:pathsExist', args), pathExists: (args: { filePath: string connectionId?: string access?: LocalFileAccess }): Promise => ipcRenderer.invoke('fs:pathExists', args), listFiles: (args: { rootPath: string connectionId?: string excludePaths?: string[] requestToken?: string maxResults?: number searchQuery?: string candidatePaths?: string[] includeIgnored?: boolean allowLegacyIncludeIgnored?: boolean followSymlinks?: boolean nameFilter?: string }): Promise => ipcRenderer.invoke('fs:listFiles', args), cancelListFiles: (args: { requestToken: string }): Promise => ipcRenderer.invoke('fs:cancelListFiles', args), cancelSearch: (args: { requestToken: string }): Promise => ipcRenderer.invoke('fs:cancelSearch', args), search: (args: { requestToken?: string query: string rootPath: string caseSensitive?: boolean wholeWord?: boolean useRegex?: boolean includePattern?: string excludePattern?: string maxResults?: number connectionId?: string }): Promise => ipcRenderer.invoke('fs:search', args), importExternalPaths: ( args: { sourcePaths: string[] destDir: string connectionId?: string ensureDir?: boolean access?: LocalFileAccess } & SshMutationExpectation ): Promise<{ results: ImportItemResult[] }> => ipcRenderer.invoke('fs:importExternalPaths', args), stageExternalPathsForRuntimeUpload: (args: { sourcePaths: string[] nameExtensionlessImages?: boolean }): Promise<{ sources: StagedExternalImportSource[] }> => ipcRenderer.invoke('fs:stageExternalPathsForRuntimeUpload', args), uploadExternalFileToRuntime: ( args: RuntimeUploadFileStreamRequest ): Promise<{ byteLength: number }> => ipcRenderer.invoke('fs:uploadExternalFileToRuntime', args), resolveDroppedPathsForAgent: ( args: { paths: string[] worktreePath: string connectionId?: string } & SshMutationExpectation ): Promise => ipcRenderer.invoke('fs:resolveDroppedPathsForAgent', args), uploadPathsToAgentSessionAttachments: ( args: AgentSessionAttachmentUploadTarget & { paths: string[] } ): Promise => ipcRenderer.invoke('fs:uploadPathsToAgentSessionAttachments', args), watchWorktree: (args: { worktreePath: string; connectionId?: string }): Promise => ipcRenderer.invoke('fs:watchWorktree', args), unwatchWorktree: (args: { worktreePath: string; connectionId?: string }): Promise => ipcRenderer.invoke('fs:unwatchWorktree', args), onFsChanged: (callback: (payload: FsChangedPayload) => void): (() => void) => { const listener = (_event: Electron.IpcRendererEvent, payload: FsChangedPayload) => callback(payload) ipcRenderer.on('fs:changed', listener) return () => ipcRenderer.removeListener('fs:changed', listener) } } satisfies PreloadApi['fs']