Fix Remote Host downloads and agent status parity (#6436)
* Fix remote host downloads and agent status parity Co-authored-by: Orca <help@stably.ai> * Address remote download review comments Co-authored-by: Orca <help@stably.ai> * Avoid inefficient SSH chunk fallback Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -23,8 +23,10 @@ import {
|
||||
import { FileExplorerVirtualRows } from './FileExplorerVirtualRows'
|
||||
import type { TreeNode } from './file-explorer-types'
|
||||
import { createFileExplorerRowProjection } from './file-explorer-row-projection'
|
||||
import type * as RuntimeFileClient from '@/runtime/runtime-file-client'
|
||||
|
||||
const { toastErrorMock, toastSuccessMock } = vi.hoisted(() => ({
|
||||
const { downloadRuntimeFileMock, toastErrorMock, toastSuccessMock } = vi.hoisted(() => ({
|
||||
downloadRuntimeFileMock: vi.fn(),
|
||||
toastErrorMock: vi.fn(),
|
||||
toastSuccessMock: vi.fn()
|
||||
}))
|
||||
@@ -36,6 +38,14 @@ vi.mock('sonner', () => ({
|
||||
}
|
||||
}))
|
||||
|
||||
vi.mock('@/runtime/runtime-file-client', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof RuntimeFileClient>()
|
||||
return {
|
||||
...actual,
|
||||
downloadRuntimeFile: downloadRuntimeFileMock
|
||||
}
|
||||
})
|
||||
|
||||
type ReactElementLike = {
|
||||
type: unknown
|
||||
props: Record<string, unknown>
|
||||
@@ -565,15 +575,24 @@ describe('FileExplorerRow collapse folder action', () => {
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('shows remote download only for desktop SSH file-like rows', () => {
|
||||
it('shows remote download only for desktop SSH or Remote Host file-like rows', () => {
|
||||
const runtimeContext = {
|
||||
settings: { activeRuntimeEnvironmentId: 'runtime-1' },
|
||||
worktreeId: 'wt-1',
|
||||
worktreePath: '/repo'
|
||||
}
|
||||
|
||||
expect(shouldShowRemoteDownloadAction(fileNode, 'ssh-1')).toBe(true)
|
||||
expect(shouldShowRemoteDownloadAction({ ...fileNode, isSymlink: true }, 'ssh-1')).toBe(true)
|
||||
expect(shouldShowRemoteDownloadAction(fileNode, null, runtimeContext)).toBe(true)
|
||||
expect(shouldShowRemoteDownloadAction(fileNode, null)).toBe(false)
|
||||
expect(shouldShowRemoteDownloadAction(directoryNode, 'ssh-1')).toBe(false)
|
||||
expect(shouldShowRemoteDownloadAction(directoryNode, null, runtimeContext)).toBe(false)
|
||||
|
||||
;(globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__ = true
|
||||
|
||||
expect(shouldShowRemoteDownloadAction(fileNode, 'ssh-1')).toBe(false)
|
||||
expect(shouldShowRemoteDownloadAction(fileNode, null, runtimeContext)).toBe(false)
|
||||
})
|
||||
|
||||
it('shows OS file copy for single local rows and SSH file rows on desktop', () => {
|
||||
@@ -677,6 +696,43 @@ describe('FileExplorerRow collapse folder action', () => {
|
||||
expect(toastErrorMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('downloads Remote Host rows through the runtime download path', async () => {
|
||||
const runtimeContext = {
|
||||
settings: { activeRuntimeEnvironmentId: 'runtime-1' },
|
||||
worktreeId: 'wt-1',
|
||||
worktreePath: '/repo'
|
||||
}
|
||||
downloadRuntimeFileMock.mockResolvedValueOnce({
|
||||
canceled: false,
|
||||
destinationPath: '/downloads/index.ts'
|
||||
})
|
||||
const openPath = vi.fn().mockResolvedValue(undefined)
|
||||
;(
|
||||
globalThis as unknown as {
|
||||
window: {
|
||||
api: {
|
||||
shell: { openPath: typeof openPath }
|
||||
}
|
||||
}
|
||||
}
|
||||
).window = { api: { shell: { openPath } } }
|
||||
|
||||
await downloadRemoteFile(fileNode, runtimeContext)
|
||||
|
||||
expect(downloadRuntimeFileMock).toHaveBeenCalledWith(
|
||||
runtimeContext,
|
||||
'/repo/src/index.ts',
|
||||
'index.ts'
|
||||
)
|
||||
expect(toastSuccessMock).toHaveBeenCalledWith("Downloaded 'index.ts'", {
|
||||
action: {
|
||||
label: 'Open',
|
||||
onClick: expect.any(Function)
|
||||
}
|
||||
})
|
||||
expect(toastErrorMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('shows a failure toast when remote download fails', async () => {
|
||||
const downloadFile = vi.fn().mockRejectedValue(new Error('Remote connection dropped'))
|
||||
;(
|
||||
|
||||
@@ -53,6 +53,7 @@ import { useFileExplorerVisibleRowProjection } from './useFileExplorerVisibleRow
|
||||
import { translate } from '@/i18n/i18n'
|
||||
import { CLOSE_ALL_CONTEXT_MENUS_EVENT } from '@/components/tab-bar/SortableTab'
|
||||
import type { RightSidebarExplorerView } from '../../../../shared/types'
|
||||
import { getRuntimeEnvironmentIdForWorktree } from '@/lib/worktree-runtime-owner'
|
||||
|
||||
function FileExplorerFiles(): React.JSX.Element {
|
||||
const explorerView = useAppStore((s) => s.rightSidebarExplorerView)
|
||||
@@ -78,6 +79,9 @@ function FileExplorerFiles(): React.JSX.Element {
|
||||
const activeWorktreeId = useAppStore((s) => s.activeWorktreeId)
|
||||
const activeWorktree = useActiveWorktree()
|
||||
const activeRepo = useRepoById(activeWorktree?.repoId ?? null)
|
||||
const activeRuntimeEnvironmentId = useAppStore((s) =>
|
||||
getRuntimeEnvironmentIdForWorktree(s, activeWorktreeId)
|
||||
)
|
||||
const sshConnectedGeneration = useAppStore((s) => s.sshConnectedGeneration)
|
||||
const expandedDirs = useAppStore((s) => s.expandedDirs)
|
||||
const collapseAllDirs = useAppStore((s) => s.collapseAllDirs)
|
||||
@@ -99,6 +103,18 @@ function FileExplorerFiles(): React.JSX.Element {
|
||||
const toggleShowDotfilesForWorktree = useAppStore((s) => s.toggleShowDotfilesForWorktree)
|
||||
|
||||
const worktreePath = activeWorktree?.path ?? null
|
||||
const runtimeDownloadContext = useMemo(
|
||||
() =>
|
||||
activeRuntimeEnvironmentId && activeWorktreeId && worktreePath
|
||||
? {
|
||||
settings: { activeRuntimeEnvironmentId },
|
||||
worktreeId: activeWorktreeId,
|
||||
worktreePath,
|
||||
connectionId: activeRepo?.connectionId ?? undefined
|
||||
}
|
||||
: null,
|
||||
[activeRepo?.connectionId, activeRuntimeEnvironmentId, activeWorktreeId, worktreePath]
|
||||
)
|
||||
const isFilesViewActive = explorerView === 'files'
|
||||
const visibleFilesWorktreePath = getVisibleFileExplorerWorktreePath({
|
||||
explorerView,
|
||||
@@ -716,6 +732,7 @@ function FileExplorerFiles(): React.JSX.Element {
|
||||
flashingPath={flashingPath}
|
||||
deleteShortcutLabel={deleteShortcutLabel}
|
||||
connectionId={activeRepo?.connectionId ?? null}
|
||||
runtimeDownloadContext={runtimeDownloadContext}
|
||||
onClick={handleRowClick}
|
||||
onDoubleClick={handleDoubleClick}
|
||||
onContextMenuSelect={preserveSelectionForContextMenu}
|
||||
|
||||
@@ -50,6 +50,7 @@ import { isLocalPathOpenBlocked, showLocalPathOpenBlockedToast } from '@/lib/loc
|
||||
import { translate } from '@/i18n/i18n'
|
||||
import { extractIpcErrorMessage } from '@/lib/ipc-error'
|
||||
import { CLOSE_ALL_CONTEXT_MENUS_EVENT } from '@/components/tab-bar/SortableTab'
|
||||
import { downloadRuntimeFile, type RuntimeFileOperationArgs } from '@/runtime/runtime-file-client'
|
||||
|
||||
const isMac = navigator.userAgent.includes('Mac')
|
||||
const isLinux = navigator.userAgent.includes('Linux')
|
||||
@@ -270,6 +271,7 @@ type FileExplorerRowProps = {
|
||||
isIgnored: boolean
|
||||
deleteShortcutLabel: string
|
||||
connectionId?: string | null
|
||||
runtimeDownloadContext?: RuntimeFileOperationArgs | null
|
||||
canCollapseFolderSubtree: boolean
|
||||
targetDir: string
|
||||
targetDepth: number
|
||||
@@ -304,12 +306,13 @@ export function shouldShowFindInFolderAction(node: TreeNode): boolean {
|
||||
|
||||
export function shouldShowRemoteDownloadAction(
|
||||
node: TreeNode,
|
||||
connectionId?: string | null
|
||||
connectionId?: string | null,
|
||||
runtimeDownloadContext?: RuntimeFileOperationArgs | null
|
||||
): boolean {
|
||||
// Why: Desktop-only because download depends on Electron's native save dialog.
|
||||
return (
|
||||
!node.isDirectory &&
|
||||
Boolean(connectionId) &&
|
||||
Boolean(connectionId || runtimeDownloadContext) &&
|
||||
(globalThis as { __ORCA_WEB_CLIENT__?: boolean }).__ORCA_WEB_CLIENT__ !== true
|
||||
)
|
||||
}
|
||||
@@ -328,9 +331,18 @@ export function shouldShowCopyFileAction(
|
||||
)
|
||||
}
|
||||
|
||||
export async function downloadRemoteFile(node: TreeNode, connectionId: string): Promise<void> {
|
||||
export async function downloadRemoteFile(
|
||||
node: TreeNode,
|
||||
connectionIdOrRuntimeContext: string | RuntimeFileOperationArgs
|
||||
): Promise<void> {
|
||||
try {
|
||||
const result = await window.api.fs.downloadFile({ filePath: node.path, connectionId })
|
||||
const result =
|
||||
typeof connectionIdOrRuntimeContext === 'string'
|
||||
? await window.api.fs.downloadFile({
|
||||
filePath: node.path,
|
||||
connectionId: connectionIdOrRuntimeContext
|
||||
})
|
||||
: await downloadRuntimeFile(connectionIdOrRuntimeContext, node.path, node.name)
|
||||
// Why: Suppress toasts when the user cancels the native save dialog per design.
|
||||
if (result.canceled) {
|
||||
return
|
||||
@@ -396,6 +408,7 @@ export function FileExplorerRow({
|
||||
isIgnored,
|
||||
deleteShortcutLabel,
|
||||
connectionId,
|
||||
runtimeDownloadContext,
|
||||
canCollapseFolderSubtree,
|
||||
targetDir,
|
||||
targetDepth,
|
||||
@@ -426,7 +439,11 @@ export function FileExplorerRow({
|
||||
const findInFolderShortcutLabel = useShortcutLabel('sidebar.search.toggle')
|
||||
const FileIcon = getFileTypeIcon(node.relativePath || node.name)
|
||||
const rowDropDir = node.isDirectory ? node.path : targetDir
|
||||
const showRemoteDownloadAction = shouldShowRemoteDownloadAction(node, connectionId)
|
||||
const showRemoteDownloadAction = shouldShowRemoteDownloadAction(
|
||||
node,
|
||||
connectionId,
|
||||
runtimeDownloadContext
|
||||
)
|
||||
const showCopyFileAction = shouldShowCopyFileAction(node, connectionId, selectionSize)
|
||||
const { setRowDragNode, handleDragOver, handleDragEnter, handleDragLeave, handleDrop } =
|
||||
useFileExplorerRowDrag({
|
||||
@@ -450,11 +467,12 @@ export function FileExplorerRow({
|
||||
}
|
||||
}, [activeWorktreeId, node.path])
|
||||
const handleDownload = useCallback(() => {
|
||||
if (!connectionId) {
|
||||
const downloadTarget = connectionId || runtimeDownloadContext
|
||||
if (!downloadTarget) {
|
||||
return
|
||||
}
|
||||
void downloadRemoteFile(node, connectionId)
|
||||
}, [connectionId, node])
|
||||
void downloadRemoteFile(node, downloadTarget)
|
||||
}, [connectionId, node, runtimeDownloadContext])
|
||||
const handleCopyFile = useCallback(() => {
|
||||
void copyFileToOsClipboard(node, connectionId)
|
||||
}, [connectionId, node])
|
||||
|
||||
@@ -7,6 +7,7 @@ import { FileExplorerRow, InlineInputRow, type InlineInput } from './FileExplore
|
||||
import { shouldShowIgnoredDecoration, STATUS_COLORS } from './status-display'
|
||||
import type { DirCache, TreeNode } from './file-explorer-types'
|
||||
import type { FileExplorerRowProjection } from './file-explorer-row-projection'
|
||||
import type { RuntimeFileOperationArgs } from '@/runtime/runtime-file-client'
|
||||
|
||||
type FileExplorerVirtualRowsProps = {
|
||||
virtualizer: Virtualizer<HTMLDivElement, Element>
|
||||
@@ -26,6 +27,7 @@ type FileExplorerVirtualRowsProps = {
|
||||
flashingPath: string | null
|
||||
deleteShortcutLabel: string
|
||||
connectionId?: string | null
|
||||
runtimeDownloadContext?: RuntimeFileOperationArgs | null
|
||||
onClick: (node: TreeNode, event: React.MouseEvent<HTMLButtonElement>) => void
|
||||
onDoubleClick: (node: TreeNode) => void
|
||||
onContextMenuSelect: (node: TreeNode) => void
|
||||
@@ -68,6 +70,7 @@ export function FileExplorerVirtualRows(props: FileExplorerVirtualRowsProps): Re
|
||||
flashingPath,
|
||||
deleteShortcutLabel,
|
||||
connectionId,
|
||||
runtimeDownloadContext,
|
||||
onClick,
|
||||
onDoubleClick,
|
||||
onContextMenuSelect,
|
||||
@@ -168,6 +171,7 @@ export function FileExplorerVirtualRows(props: FileExplorerVirtualRowsProps): Re
|
||||
isIgnored={isIgnored}
|
||||
deleteShortcutLabel={deleteShortcutLabel}
|
||||
connectionId={connectionId}
|
||||
runtimeDownloadContext={runtimeDownloadContext}
|
||||
canCollapseFolderSubtree={canCollapseFolderSubtree}
|
||||
targetDir={n.isDirectory ? n.path : dirname(n.path)}
|
||||
targetDepth={n.isDirectory ? n.depth + 1 : n.depth}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
copyRuntimePath,
|
||||
createRuntimePath,
|
||||
deleteRuntimePath,
|
||||
downloadRuntimeFile,
|
||||
getRuntimeFileReadScope,
|
||||
importExternalPathsToRuntime,
|
||||
listRuntimeFiles,
|
||||
@@ -36,6 +37,12 @@ const fsDeletePath = vi.fn()
|
||||
const fsStat = vi.fn()
|
||||
const fsPathExists = vi.fn()
|
||||
const fsSearch = vi.fn()
|
||||
const fsDownloadFile = vi.fn()
|
||||
const fsSaveDownloadedFile = vi.fn()
|
||||
const fsStartDownloadedFile = vi.fn()
|
||||
const fsAppendDownloadedFileChunk = vi.fn()
|
||||
const fsFinishDownloadedFile = vi.fn()
|
||||
const fsCancelDownloadedFile = vi.fn()
|
||||
const fsImportExternalPaths = vi.fn()
|
||||
const fsStageExternalPathsForRuntimeUpload = vi.fn()
|
||||
const runtimeEnvironmentCall = vi.fn()
|
||||
@@ -56,6 +63,12 @@ beforeEach(() => {
|
||||
fsStat.mockReset()
|
||||
fsPathExists.mockReset()
|
||||
fsSearch.mockReset()
|
||||
fsDownloadFile.mockReset()
|
||||
fsSaveDownloadedFile.mockReset()
|
||||
fsStartDownloadedFile.mockReset()
|
||||
fsAppendDownloadedFileChunk.mockReset()
|
||||
fsFinishDownloadedFile.mockReset()
|
||||
fsCancelDownloadedFile.mockReset()
|
||||
fsImportExternalPaths.mockReset()
|
||||
fsStageExternalPathsForRuntimeUpload.mockReset()
|
||||
runtimeEnvironmentCall.mockReset()
|
||||
@@ -89,6 +102,12 @@ beforeEach(() => {
|
||||
stat: fsStat,
|
||||
pathExists: fsPathExists,
|
||||
search: fsSearch,
|
||||
downloadFile: fsDownloadFile,
|
||||
saveDownloadedFile: fsSaveDownloadedFile,
|
||||
startDownloadedFile: fsStartDownloadedFile,
|
||||
appendDownloadedFileChunk: fsAppendDownloadedFileChunk,
|
||||
finishDownloadedFile: fsFinishDownloadedFile,
|
||||
cancelDownloadedFile: fsCancelDownloadedFile,
|
||||
importExternalPaths: fsImportExternalPaths,
|
||||
stageExternalPathsForRuntimeUpload: fsStageExternalPathsForRuntimeUpload
|
||||
},
|
||||
@@ -348,6 +367,96 @@ describe('runtime file client', () => {
|
||||
expect(runtimeEnvironmentCall).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('downloads remote runtime files in chunks instead of using preview content', async () => {
|
||||
fsStartDownloadedFile.mockResolvedValue({
|
||||
canceled: false,
|
||||
transferId: 'download-1',
|
||||
destinationPath: '/downloads/archive.zip'
|
||||
})
|
||||
fsAppendDownloadedFileChunk.mockResolvedValue({ ok: true })
|
||||
fsFinishDownloadedFile.mockResolvedValue({
|
||||
canceled: false,
|
||||
destinationPath: '/downloads/archive.zip'
|
||||
})
|
||||
runtimeEnvironmentCall
|
||||
.mockResolvedValueOnce({
|
||||
id: 'chunk-1',
|
||||
ok: true,
|
||||
result: { contentBase64: 'YWJj', bytesRead: 3, eof: false },
|
||||
_meta: { runtimeId: 'remote-runtime' }
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
id: 'chunk-2',
|
||||
ok: true,
|
||||
result: { contentBase64: 'ZA==', bytesRead: 1, eof: true },
|
||||
_meta: { runtimeId: 'remote-runtime' }
|
||||
})
|
||||
|
||||
await expect(
|
||||
downloadRuntimeFile(
|
||||
{
|
||||
settings: { activeRuntimeEnvironmentId: 'env-1' },
|
||||
worktreeId: 'wt-1',
|
||||
worktreePath: '/remote/repo'
|
||||
},
|
||||
'/remote/repo/archive.zip',
|
||||
'archive.zip'
|
||||
)
|
||||
).resolves.toEqual({ canceled: false, destinationPath: '/downloads/archive.zip' })
|
||||
|
||||
expect(fsStartDownloadedFile).toHaveBeenCalledWith({ suggestedName: 'archive.zip' })
|
||||
expect(runtimeEnvironmentCall).toHaveBeenNthCalledWith(1, {
|
||||
selector: 'env-1',
|
||||
method: 'files.readChunk',
|
||||
params: {
|
||||
worktree: 'id:wt-1',
|
||||
relativePath: 'archive.zip',
|
||||
offset: 0,
|
||||
length: 384 * 1024
|
||||
},
|
||||
timeoutMs: 60_000
|
||||
})
|
||||
expect(runtimeEnvironmentCall).toHaveBeenNthCalledWith(2, {
|
||||
selector: 'env-1',
|
||||
method: 'files.readChunk',
|
||||
params: {
|
||||
worktree: 'id:wt-1',
|
||||
relativePath: 'archive.zip',
|
||||
offset: 3,
|
||||
length: 384 * 1024
|
||||
},
|
||||
timeoutMs: 60_000
|
||||
})
|
||||
expect(fsAppendDownloadedFileChunk).toHaveBeenCalledTimes(2)
|
||||
expect(fsFinishDownloadedFile).toHaveBeenCalledWith({ transferId: 'download-1' })
|
||||
expect(fsCancelDownloadedFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('cancels the local temp download when a remote chunk fails', async () => {
|
||||
fsStartDownloadedFile.mockResolvedValue({
|
||||
canceled: false,
|
||||
transferId: 'download-1',
|
||||
destinationPath: '/downloads/archive.zip'
|
||||
})
|
||||
fsCancelDownloadedFile.mockResolvedValue({ ok: true })
|
||||
runtimeEnvironmentCall.mockRejectedValueOnce(new Error('connection dropped'))
|
||||
|
||||
await expect(
|
||||
downloadRuntimeFile(
|
||||
{
|
||||
settings: { activeRuntimeEnvironmentId: 'env-1' },
|
||||
worktreeId: 'wt-1',
|
||||
worktreePath: '/remote/repo'
|
||||
},
|
||||
'/remote/repo/archive.zip',
|
||||
'archive.zip'
|
||||
)
|
||||
).rejects.toThrow('connection dropped')
|
||||
|
||||
expect(fsCancelDownloadedFile).toHaveBeenCalledWith({ transferId: 'download-1' })
|
||||
expect(fsFinishDownloadedFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('routes root directory reads with an empty relative path', async () => {
|
||||
runtimeEnvironmentCall.mockResolvedValue({
|
||||
id: 'rpc-1',
|
||||
|
||||
@@ -9,7 +9,11 @@ import type {
|
||||
SearchOptions,
|
||||
SearchResult
|
||||
} from '../../../shared/types'
|
||||
import type { RuntimeFilePreviewResult, RuntimeFileReadResult } from '../../../shared/runtime-types'
|
||||
import type {
|
||||
RuntimeFilePreviewResult,
|
||||
RuntimeFileReadChunkResult,
|
||||
RuntimeFileReadResult
|
||||
} from '../../../shared/runtime-types'
|
||||
import {
|
||||
callRuntimeRpc,
|
||||
getActiveRuntimeTarget,
|
||||
@@ -49,6 +53,10 @@ export type RuntimeFileOperationArgs = {
|
||||
connectionId?: string
|
||||
}
|
||||
|
||||
export type RuntimeFileDownloadResult =
|
||||
| { canceled: true }
|
||||
| { canceled: false; destinationPath: string }
|
||||
|
||||
type StagedRuntimeImportSource =
|
||||
| {
|
||||
sourcePath: string
|
||||
@@ -93,6 +101,7 @@ type RuntimeFileWatchEvent =
|
||||
| { type: 'end' }
|
||||
|
||||
const REMOTE_UPLOAD_BASE64_CHUNK_CHARS = 512 * 1024
|
||||
const REMOTE_DOWNLOAD_CHUNK_BYTES = 384 * 1024
|
||||
|
||||
type RuntimeFileWatchListener = {
|
||||
onPayload: (payload: FsChangedPayload) => void
|
||||
@@ -179,6 +188,71 @@ export async function readRuntimeFilePreview(
|
||||
)
|
||||
}
|
||||
|
||||
export async function downloadRuntimeFile(
|
||||
context: RuntimeFileOperationArgs,
|
||||
filePath: string,
|
||||
suggestedName: string
|
||||
): Promise<RuntimeFileDownloadResult> {
|
||||
const remoteArgs = getRemoteFileArgs(context, filePath)
|
||||
if (!remoteArgs) {
|
||||
if (hasRemoteRuntimeOwner(context)) {
|
||||
throw new Error('Remote file is outside the owning runtime worktree')
|
||||
}
|
||||
if (context.connectionId) {
|
||||
return window.api.fs.downloadFile({ filePath, connectionId: context.connectionId })
|
||||
}
|
||||
const result = await readRuntimeFilePreview(context, filePath)
|
||||
return window.api.fs.saveDownloadedFile({
|
||||
suggestedName,
|
||||
content: result.content,
|
||||
encoding: result.isBinary ? 'base64' : 'utf8'
|
||||
})
|
||||
}
|
||||
|
||||
const download = await window.api.fs.startDownloadedFile({ suggestedName })
|
||||
if (download.canceled) {
|
||||
return download
|
||||
}
|
||||
|
||||
let finished = false
|
||||
try {
|
||||
let offset = 0
|
||||
for (;;) {
|
||||
const chunk = await callRuntimeRpc<RuntimeFileReadChunkResult>(
|
||||
remoteArgs.target,
|
||||
'files.readChunk',
|
||||
{
|
||||
worktree: remoteArgs.worktreeSelector,
|
||||
relativePath: remoteArgs.relativePath,
|
||||
offset,
|
||||
length: REMOTE_DOWNLOAD_CHUNK_BYTES
|
||||
},
|
||||
{ timeoutMs: 60_000 }
|
||||
)
|
||||
if (chunk.bytesRead > 0) {
|
||||
await window.api.fs.appendDownloadedFileChunk({
|
||||
transferId: download.transferId,
|
||||
contentBase64: chunk.contentBase64
|
||||
})
|
||||
}
|
||||
offset += chunk.bytesRead
|
||||
if (chunk.eof) {
|
||||
break
|
||||
}
|
||||
if (chunk.bytesRead <= 0) {
|
||||
throw new Error('Remote download stalled before reaching EOF')
|
||||
}
|
||||
}
|
||||
const result = await window.api.fs.finishDownloadedFile({ transferId: download.transferId })
|
||||
finished = true
|
||||
return result
|
||||
} finally {
|
||||
if (!finished) {
|
||||
await window.api.fs.cancelDownloadedFile({ transferId: download.transferId }).catch(() => {})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function readRuntimeDirectory(
|
||||
context: RuntimeFileOperationArgs,
|
||||
dirPath: string
|
||||
|
||||
@@ -1274,6 +1274,21 @@ function createFileApi(): NonNullable<Partial<PreloadApi>['fs']> {
|
||||
downloadFile: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
saveDownloadedFile: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
startDownloadedFile: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
appendDownloadedFileChunk: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
finishDownloadedFile: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
cancelDownloadedFile: async () => {
|
||||
throw new Error('Remote file download is unavailable in paired web clients.')
|
||||
},
|
||||
listMarkdownDocuments: async ({ rootPath }) => {
|
||||
const file = await resolveRuntimeFilePath(rootPath)
|
||||
return callRuntimeResult('files.listMarkdownDocuments', {
|
||||
|
||||
Reference in New Issue
Block a user