Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(ui): download artifacts through the service's save dialog

Sanile81d56d77a12
diff --git a/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx b/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
index ce2fe851ec..7fad95f52b 100644
--- a/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
+++ b/app/src/features/conversations/aui/ArtifactPreviewPanel.refresh.test.tsx
@@ -80,3 +80,16 @@ describe('ArtifactPreviewPanel — versions saved elsewhere', () => {
expect((screen.getByRole('textbox') as HTMLTextAreaElement).value).toBe('# my edit');
});
});
+
+describe('ArtifactPreviewPanel — download', () => {
+ beforeEach(() => rpc.mockReset());
+
+ it('says why a download is not possible instead of failing silently', async () => {
+ rpc.mockResolvedValueOnce(body('# first', 1));
+ setup();
+ await screen.findByRole('heading', { level: 1 });
+ // Not the desktop app: the service refuses before any dialog.
+ fireEvent.click(screen.getByTitle(/Download/i));
+ expect(await screen.findByText('chat.files.error.not_desktop')).toBeTruthy();
+ });
+});
diff --git a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
index c68dfd27c6..07040d3440 100644
--- a/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
+++ b/app/src/features/conversations/aui/ArtifactPreviewPanel.tsx
@@ -1,6 +1,5 @@
'use client';
-import { invoke } from '@tauri-apps/api/core';
import { Check, Clipboard, Code2, Download, Eye, FileBox, FileText, Share2, X } from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import Markdown from 'react-markdown';
@@ -11,6 +10,7 @@ import {
WINDOW_DRAG_BAR_HEIGHT,
} from '../../../components/layout/shell/WindowDragBar';
import { useT } from '../../../lib/i18n/I18nContext';
+import { saveArtifactViaDialog } from '../../../services/artifactDownloadService';
import { callCoreRpc } from '../../../services/coreRpcClient';
import type { ArtifactSnapshot } from '../../../store/chatRuntimeSlice';
import {
@@ -322,6 +322,8 @@ function FileViewer({
version: number;
error: string | null;
}) {
+ const { t } = useT();
+ const [downloadError, setDownloadError] = useState<string | null>(null);
const filePath = body || artifact.path || '';
const fileName = filePath.split('/').pop() || filePath;
// Share is hidden until the backend serves viewable files inline (links download today); see useArtifactShare.
@@ -351,10 +353,11 @@ function FileViewer({
Binary {artifact.kind} — preview not available
</p>
{error && <p className="text-xs text-destructive mt-2">{error}</p>}
+ {downloadError && <p className="text-xs text-destructive mt-2">{downloadError}</p>}
</div>
{filePath && (
<button
- onClick={() => void saveArtifactAs(artifact)}
+ onClick={() => void saveArtifactAs(artifact, t).then(setDownloadError)}
className="inline-flex items-center gap-2 rounded-md bg-primary px-4 py-2 text-xs font-medium text-primary-foreground hover:bg-primary/90">
<Download className="h-3.5 w-3.5" />
Download
@@ -378,21 +381,20 @@ export const AUTOSAVE_MAX_RETRIES = 3;
export const AUTOSAVE_DELAY_MS = 5_000;
/** The artifact's file name: the one it has in the files folder. */
-function downloadName(artifact: ArtifactSnapshot): string {
- const fromPath = artifact.path?.split(/[\\/]/).pop();
- return fromPath || artifact.title;
-}
-
-/** Let the user pick where to save the artifact's file (OS save dialog). */
-async function saveArtifactAs(artifact: ArtifactSnapshot): Promise<void> {
- try {
- await invoke<string | null>('save_artifact_as', {
- artifactId: artifact.artifactId,
- filename: downloadName(artifact),
- });
- } catch (err) {
- console.error('[ArtifactPreviewPanel] download failed:', err);
- }
+/**
+ * Let the user pick where to save the artifact's file (the OS save dialog),
+ * through the download service. Resolves to a localized error message, or
+ * `null` when saved or cancelled.
+ */
+async function saveArtifactAs(
+ artifact: ArtifactSnapshot,
+ t: (key: string) => string
+): Promise<string | null> {
+ const extension = artifact.path?.split('.').pop() ?? '';
+ const outcome = await saveArtifactViaDialog(artifact.artifactId, artifact.title, extension);
+ if (outcome.ok || outcome.code === 'CANCELLED') return null;
+ console.warn('[ArtifactPreviewPanel] download failed:', outcome.code, outcome.error);
+ return t(`chat.files.error.${(outcome.code ?? 'download_failed').toLowerCase()}`);
}
type AutosaveStatus = 'saved' | 'pending' | 'saving' | 'error';
@@ -675,6 +677,8 @@ function EditableTextViewer({
const [tab, setTab] = useState<'preview' | 'code'>('preview');
const [editedBody, setEditedBody] = useState(body);
const [copied, setCopied] = useState(false);
+ const { t } = useT();
+ const [downloadError, setDownloadError] = useState<string | null>(null);
// Only kinds whose record holds the content itself can take a new version;
// a `file` record holds a path, and its text here is read off that file.
// A body that failed to load is not the artifact's content: editing it
@@ -708,8 +712,8 @@ function EditableTextViewer({
const handleDownload = useCallback(async () => {
// The file is the newest saved version: save a pending edit first.
await autosave.flush();
- await saveArtifactAs(artifact);
- }, [artifact, autosave]);
+ setDownloadError(await saveArtifactAs(artifact, t));
+ }, [artifact, autosave, t]);
// Share the newest saved version: save a pending edit first.
// Share is hidden until the backend serves viewable files inline (links download today); see useArtifactShare.
@@ -776,6 +780,11 @@ function EditableTextViewer({
{/* <ShareStatus state={sharing.state} onCopy={sharing.copyAgain} /> */}
{/* Error banner */}
+ {downloadError && (
+ <div className="border-b bg-destructive/10 px-3 py-1.5 text-[11px] text-destructive">
+ {downloadError}
+ </div>
+ )}
{error && (
<div className="border-b bg-destructive/10 px-3 py-1.5 text-[11px] text-destructive">
{error}
diff --git a/app/src/services/__tests__/artifactDownloadService.test.ts b/app/src/services/__tests__/artifactDownloadService.test.ts
index e179ee1d42..8a539beb4a 100644
--- a/app/src/services/__tests__/artifactDownloadService.test.ts
+++ b/app/src/services/__tests__/artifactDownloadService.test.ts
@@ -457,7 +457,7 @@ describe('revealArtifact', () => {
});
});
-describe('saveArtifactViaDialog (dialog removed with rfd)', () => {
+describe('saveArtifactViaDialog (OS save dialog)', () => {
beforeEach(() => {
vi.clearAllMocks();
hoisted.isTauri.mockReturnValue(true);
@@ -482,20 +482,25 @@ describe('saveArtifactViaDialog (dialog removed with rfd)', () => {
expect(callCoreRpc).not.toHaveBeenCalled();
});
- it('routes to the Downloads copy rather than a dialog command', async () => {
+ it('opens the save dialog prefilled with the artifact filename', async () => {
resolveOk();
- hoisted.invoke.mockResolvedValueOnce('/Users/me/Downloads/Deck.pptx');
+ hoisted.invoke.mockResolvedValueOnce('/Users/me/Desktop/Deck.pptx');
const outcome = await saveArtifactViaDialog('a-1', 'Deck', 'pptx');
- expect(outcome).toEqual({ ok: true, path: '/Users/me/Downloads/Deck.pptx' });
- expect(hoisted.invoke).toHaveBeenCalledWith('download_artifact_to_downloads', {
+ expect(outcome).toEqual({ ok: true, path: '/Users/me/Desktop/Deck.pptx' });
+ expect(hoisted.invoke).toHaveBeenCalledWith('save_artifact_as', {
artifactId: 'a-1',
filename: 'Deck.pptx',
});
- // The removed `rfd` command must never be invoked again.
- expect(hoisted.invoke).not.toHaveBeenCalledWith('save_artifact_via_dialog', expect.anything());
});
- it('surfaces a copy failure as DOWNLOAD_FAILED', async () => {
+ it('reports a closed dialog as CANCELLED', async () => {
+ resolveOk();
+ hoisted.invoke.mockResolvedValueOnce(null);
+ const outcome = await saveArtifactViaDialog('a-1', 'Deck', 'pptx');
+ expect(outcome).toEqual({ ok: false, code: 'CANCELLED' });
+ });
+
+ it('surfaces a save failure as DOWNLOAD_FAILED', async () => {
resolveOk();
hoisted.invoke.mockRejectedValueOnce(new Error('disk full'));
const outcome = await saveArtifactViaDialog('a-1', 'Deck', 'pptx');
diff --git a/app/src/services/artifactDownloadService.ts b/app/src/services/artifactDownloadService.ts
index 294955026a..31b080d120 100644
--- a/app/src/services/artifactDownloadService.ts
+++ b/app/src/services/artifactDownloadService.ts
@@ -261,21 +261,43 @@ export async function downloadArtifact(
}
/**
- * Export an artifact under its own filename.
+ * Let the user choose where to save an artifact: the OS save dialog
+ * (`save_artifact_as`), prefilled with the artifact's own filename. The shell
+ * copies the file the core's artifact store vouches for.
*
- * Historically (#3162) this opened a native Save-As dialog and fell back
- * to the Downloads copy when no dialog was available. The dialog is gone
- * along with the shell's `rfd` dependency, so the fallback is now the only
- * path: the artifact lands in Downloads and the caller offers "Reveal in
- * Finder". Kept as a named export because `ArtifactCard` calls it, and
- * because a real destination picker may return here later.
+ * Returns `{ ok: true, path }` once saved and `{ ok: false, code: 'CANCELLED' }`
+ * when the user closes the dialog; other failures carry the usual codes.
+ * Desktop only (`NOT_DESKTOP` elsewhere).
*/
export async function saveArtifactViaDialog(
artifactId: string,
fallbackTitle: string,
extension: string
): Promise<DownloadArtifactOutcome> {
- return downloadArtifact(artifactId, fallbackTitle, extension);
+ if (!isTauri()) {
+ return {
+ ok: false,
+ code: 'NOT_DESKTOP',
+ error: 'Downloads are only available in the desktop app',
+ };
+ }
+
+ const resolved = await resolveArtifactForExport(artifactId, fallbackTitle, extension);
+ if (!resolved.ok) {
+ return { ok: false, code: resolved.code, error: resolved.error };
+ }
+
+ try {
+ const dest = await invoke<string | null>('save_artifact_as', {
+ artifactId,
+ filename: resolved.filename,
+ });
+ if (!dest) return { ok: false, code: 'CANCELLED' };
+ return { ok: true, path: dest };
+ } catch (err) {
+ const reason = err instanceof Error ? err.message : String(err);
+ return { ok: false, code: 'DOWNLOAD_FAILED', error: reason };
+ }
}
/**