Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

10.1 KB
Raw Blame
import type { PathExistenceResult } from '../../shared/path-existence-batch'
import { ipcRenderer, webUtils } from 'electron'
import type {
  PrepareDroppedPathsRequest,
  PreparedDroppedPaths
} from '../../shared/native-file-drop-preparation'
import type { SshMutationExpectation } from '../../shared/ssh-types'
import type { LocalFileAccess } from '../../shared/local-file-access'
import type { RemoteFilePreviewAddress } from '../../shared/remote-file-preview-address'
import type { FileBytesWritePrecondition } from '../../shared/file-bytes-write'
import type {
  FileWriteDiskPrecondition,
  FileWriteResult
} from '../../shared/file-write-disk-precondition'
import type { RuntimeUploadFileStreamRequest } from '../../shared/runtime-upload-staging-contract'
import type {
  AgentSessionAttachmentPathUploadResult,
  AgentSessionAttachmentUploadTarget
} from '../../shared/agent-session-attachments'
import type { SearchResult } from '../../shared/code-search-types'
import type { FsChangedPayload } from '../../shared/filesystem-entry-types'
import type {
  ImportItemResult,
  ResolveDroppedPathsResult,
  StagedExternalImportSource
} from '../../shared/filesystem-import-result-types'
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<PreparedDroppedPaths> =>
    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<Uint8Array> =>
    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<LocalLogTailReadResult> =>
    ipcRenderer.invoke('fs:readLocalLogTail', args),
  startLocalLogTail: (args: LocalLogTailWatchArgs): Promise<void> =>
    ipcRenderer.invoke('fs:startLocalLogTail', args),
  stopLocalLogTail: (args: { subscriptionId: string }): Promise<void> =>
    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<FileWriteResult> => ipcRenderer.invoke('fs:writeFile', args),
  writeFileBytes: (
    args: {
      filePath: string
      bytes: Uint8Array
      connectionId?: string
      access?: LocalFileAccess
      diskPrecondition: FileBytesWritePrecondition
    } & SshMutationExpectation
  ): Promise<FileWriteResult> => ipcRenderer.invoke('fs:writeFileBytes', args),
  createFile: (
    args: { filePath: string; connectionId?: string } & SshMutationExpectation
  ): Promise<void> => ipcRenderer.invoke('fs:createFile', args),
  createDir: (
    args: {
      dirPath: string
      connectionId?: string
      followSymlinks?: boolean
    } & SshMutationExpectation
  ): Promise<void> => ipcRenderer.invoke('fs:createDir', args),
  rename: (
    args: {
      oldPath: string
      newPath: string
      connectionId?: string
      access?: LocalFileAccess
    } & SshMutationExpectation
  ): Promise<void> => ipcRenderer.invoke('fs:rename', args),
  copy: (
    args: {
      sourcePath: string
      destinationPath: string
      connectionId?: string
    } & SshMutationExpectation
  ): Promise<void> => ipcRenderer.invoke('fs:copy', args),
  deletePath: (
    args: {
      targetPath: string
      connectionId?: string
      recursive?: boolean
      permanent?: boolean
    } & SshMutationExpectation
  ): Promise<void> => 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),
  pathsExist: (args: {
    filePaths: string[]
    connectionId?: string
  }): Promise<PathExistenceResult[]> => ipcRenderer.invoke('fs:pathsExist', args),
  pathExists: (args: {
    filePath: string
    connectionId?: string
    access?: LocalFileAccess
  }): Promise<boolean> => 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<string[]> => ipcRenderer.invoke('fs:listFiles', args),
  cancelListFiles: (args: { requestToken: string }): Promise<void> =>
    ipcRenderer.invoke('fs:cancelListFiles', args),
  cancelSearch: (args: { requestToken: string }): Promise<void> =>
    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<SearchResult> => 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<ResolveDroppedPathsResult> =>
    ipcRenderer.invoke('fs:resolveDroppedPathsForAgent', args),
  uploadPathsToAgentSessionAttachments: (
    args: AgentSessionAttachmentUploadTarget & { paths: string[] }
  ): Promise<AgentSessionAttachmentPathUploadResult> =>
    ipcRenderer.invoke('fs:uploadPathsToAgentSessionAttachments', args),
  watchWorktree: (args: { worktreePath: string; connectionId?: string }): Promise<void> =>
    ipcRenderer.invoke('fs:watchWorktree', args),
  unwatchWorktree: (args: { worktreePath: string; connectionId?: string }): Promise<void> =>
    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']