The open-source agent harness - the runtime layer that turns an LLM into a working agent.
fix(trueforge-ui): encode artifact paths in download links (#686)
Animesh Kumar53de2fb588e9
diff --git a/.changeset/encode-artifact-download-path.md b/.changeset/encode-artifact-download-path.mdnew file mode 100644index 000000000..9efa812d8--- /dev/null+++ b/.changeset/encode-artifact-download-path.md@@ -0,0 +1,5 @@+---+'@truefoundry/trueforge-ui': patch+---++Encode sandbox artifact paths when building file-download links, so names containing spaces, `#`, `?`, `&`, `%` or non-ASCII characters resolve to the intended file instead of a truncated or misparsed URL.diff --git a/packages/trueforge-ui/src/atoms/ChatFileDownload.tsx b/packages/trueforge-ui/src/atoms/ChatFileDownload.tsxindex 4e1b536a7..0f66f8478 100644--- a/packages/trueforge-ui/src/atoms/ChatFileDownload.tsx+++ b/packages/trueforge-ui/src/atoms/ChatFileDownload.tsx@@ -30,6 +30,12 @@ export type ChatFileDownloadProps = {readOnlyTooltip?: string;};+// Artifact paths are model-authored, so a name may carry characters that change what the+// URL means. Encode per segment so `/` keeps its structural role and the rest stays literal.+function encodeArtifactPath(path: string): string {+ return path.split('/').map(encodeURIComponent).join('/');+}+export function ChatFileDownload({files,fileDownloadBaseUrl,@@ -100,7 +106,7 @@ export function ChatFileDownload({<div className="my-2 flex flex-col gap-2"><div className="flex flex-wrap gap-2">{visualFiles.map(file => {- const href = fileDownloadBaseUrl ? `${fileDownloadBaseUrl}${file.path}` : undefined;+ const href = fileDownloadBaseUrl ? `${fileDownloadBaseUrl}${encodeArtifactPath(file.path)}` : undefined;return (<VisualSandboxFilekey={file.path}@@ -217,7 +223,7 @@ function ArtifactChip({return <span className={itemClassName}>{label}</span>;}- const href = fileDownloadBaseUrl ? `${fileDownloadBaseUrl}${path}` : undefined;+ const href = fileDownloadBaseUrl ? `${fileDownloadBaseUrl}${encodeArtifactPath(path)}` : undefined;const canDownload = Boolean(onDownloadArtifact || href);const isDownloading = downloadingPath === path;diff --git a/packages/trueforge-ui/test/atoms/ChatFileDownload.test.tsx b/packages/trueforge-ui/test/atoms/ChatFileDownload.test.tsxindex 3ac3a6b72..c943d66d6 100644--- a/packages/trueforge-ui/test/atoms/ChatFileDownload.test.tsx+++ b/packages/trueforge-ui/test/atoms/ChatFileDownload.test.tsx@@ -1,7 +1,29 @@+import { AssistantRuntimeProvider, useExternalStoreRuntime, type ThreadMessageLike } from '@assistant-ui/react';import { act, fireEvent, render, screen } from '@testing-library/react';+import type { ReactNode } from 'react';import { describe, expect, it, vi } from 'vitest';import { ChatFileDownload } from '@/atoms/ChatFileDownload.js';+import { FilePreviewLoaderScope, FilePreviewProvider, FilePreviewTurnScope } from '@/filePreview/FilePreviewContext.js';++/** Enables inline previews, so image, PDF and HTML artifacts render as preview cards. */+function WithPreviews({ children }: { children: ReactNode }) {+ const runtime = useExternalStoreRuntime({+ messages: [] as ThreadMessageLike[],+ isRunning: false,+ convertMessage: (next: ThreadMessageLike) => next,+ onNew: async () => {},+ });+ return (+ <AssistantRuntimeProvider runtime={runtime}>+ <FilePreviewProvider>+ <FilePreviewTurnScope turnId="turn-1">+ <FilePreviewLoaderScope load={async () => new Blob(['x'])}>{children}</FilePreviewLoaderScope>+ </FilePreviewTurnScope>+ </FilePreviewProvider>+ </AssistantRuntimeProvider>+ );+}describe('ChatFileDownload', () => {it('renders direct download links with singular and plural summaries', () => {@@ -30,6 +52,48 @@ describe('ChatFileDownload', () => {expect(screen.getByRole('link', { name: 'Download data.csv' })).toHaveAttribute('href', '/downloads/data.csv');});+ it('encodes artifact names that would otherwise break or rewrite the URL', () => {+ render(+ <ChatFileDownload+ files={[+ { name: 'my report#final,v2.csv', path: '/tmp/my report#final,v2.csv' },+ { name: 'a&b?c%d.txt', path: '/tmp/a&b?c%d.txt' },+ { name: 'ünïcode.txt', path: '/tmp/ünïcode.txt' },+ ]}+ fileDownloadBaseUrl="https://files.example.com"+ />,+ );++ expect(screen.getByRole('link', { name: 'Download my report#final,v2.csv' })).toHaveAttribute(+ 'href',+ 'https://files.example.com/tmp/my%20report%23final%2Cv2.csv',+ );+ expect(screen.getByRole('link', { name: 'Download a&b?c%d.txt' })).toHaveAttribute(+ 'href',+ 'https://files.example.com/tmp/a%26b%3Fc%25d.txt',+ );+ expect(screen.getByRole('link', { name: 'Download ünïcode.txt' })).toHaveAttribute(+ 'href',+ 'https://files.example.com/tmp/%C3%BCn%C3%AFcode.txt',+ );+ });++ it('encodes the download link on a preview card too', () => {+ render(+ <WithPreviews>+ <ChatFileDownload+ files={[{ name: 'chart #1?.png', path: '/tmp/chart #1?.png' }]}+ fileDownloadBaseUrl="https://files.example.com"+ />+ </WithPreviews>,+ );++ expect(screen.getByRole('link', { name: 'Download chart #1?.png' })).toHaveAttribute(+ 'href',+ 'https://files.example.com/tmp/chart%20%231%3F.png',+ );+ });+it('shows per-file progress and suppresses duplicate artifact downloads', async () => {let resolveDownload: (() => void) | undefined;const downloadPromise = new Promise<void>(resolve => {