From bee812e6abb0bf6d942e89c2c7c6e2b702f720af Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Thu, 29 Oct 2026 01:11:05 +0800 Subject: [PATCH] refactor(web): share generated-file controls and reset stale asset state --- .../chat/GeneratedOutputFileActions.tsx | 183 +-------------- web/components/files/OutputFileActions.tsx | 211 ++++++++++++++++++ .../math-animator/RenderFileActions.tsx | 211 +----------------- 3 files changed, 217 insertions(+), 388 deletions(-) create mode 100644 web/components/files/OutputFileActions.tsx diff --git a/web/components/chat/GeneratedOutputFileActions.tsx b/web/components/chat/GeneratedOutputFileActions.tsx index b47c93981..7b075239a 100644 --- a/web/components/chat/GeneratedOutputFileActions.tsx +++ b/web/components/chat/GeneratedOutputFileActions.tsx @@ -1,10 +1,5 @@ -'use client' - -import { useState } from 'react' -import { Copy, FolderOpen } from 'lucide-react' -import { useTranslation } from 'react-i18next' -import { ConfirmDialog } from '@/components/ui/ConfirmDialog' -import { apiFetch } from '@/lib/api' +"use client"; +import { OutputFileActions } from "@/components/files/OutputFileActions"; interface GeneratedOutputAttachment { generated?: boolean @@ -12,13 +7,6 @@ interface GeneratedOutputAttachment { url?: string } -interface FileStorage { - file_path: string - file_bytes: number - fingerprint: string - can_reveal: boolean -} - export function supportsGeneratedOutputActions(attachment: GeneratedOutputAttachment): boolean { const parts = (attachment.relative_path || '').split('/') const directChatOutput = @@ -43,171 +31,6 @@ export function supportsGeneratedOutputActions(attachment: GeneratedOutputAttach ) } -function formatBytes(bytes: number): string { - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - return `${(bytes / 1024 / 1024).toFixed(1)} MB` -} - export function GeneratedOutputFileActions({ url }: { url: string }) { - const { t } = useTranslation() - const [open, setOpen] = useState(false) - const [busy, setBusy] = useState(false) - const [confirm, setConfirm] = useState(false) - const [storage, setStorage] = useState(null) - const [error, setError] = useState('') - const [message, setMessage] = useState('') - const [removed, setRemoved] = useState(false) - const [base, query = ''] = url.split('?', 2) - - async function request(action: string, init?: RequestInit) { - const response = await apiFetch(`${base}/${action}${query ? `?${query}` : ''}`, init) - const body = await response.json() - if (!response.ok) throw new Error(body.detail || response.statusText) - return body - } - - async function load() { - setBusy(true) - setError('') - try { - setStorage(await request('file-storage')) - } catch (cause) { - setError(cause instanceof Error ? t(cause.message) : t('Unable to load file details.')) - } finally { - setBusy(false) - } - } - - async function remove() { - if (!storage) return - setBusy(true) - setError('') - try { - await request('remove-generated-output', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ fingerprint: storage.fingerprint }), - }) - setRemoved(true) - setMessage(t('Workspace file removed. The chat attachment is still available.')) - } catch (cause) { - setError(cause instanceof Error ? t(cause.message) : t('Unable to remove generated file.')) - } finally { - setConfirm(false) - setBusy(false) - } - } - - const buttonClass = - 'rounded-md border border-[var(--border)] px-2 py-1 text-[12px] hover:bg-[var(--background)] disabled:opacity-40' - return ( -
- - {open ? ( -
- {busy && !storage ?

{t('Loading file storage…')}

: null} - {error ? ( -

- {error} -

- ) : null} - {message ?

{message}

: null} - {storage ? ( - <> - -
- - {storage.can_reveal && !removed ? ( - - ) : null} - {!removed ? ( - - ) : null} -
-

- {t('Removing the workspace copy keeps the chat attachment available.')} -

- - ) : !busy ? ( - - ) : null} -
- ) : null} - setConfirm(false)} - onConfirm={() => void remove()} - > -

- {t( - 'Remove the original generated file from the workspace? The chat attachment stays available.' - )} -

-
-
- ) + return ; } diff --git a/web/components/files/OutputFileActions.tsx b/web/components/files/OutputFileActions.tsx new file mode 100644 index 000000000..e16ca7a3f --- /dev/null +++ b/web/components/files/OutputFileActions.tsx @@ -0,0 +1,211 @@ +'use client' + +import { useState } from 'react' +import { Copy, FolderOpen } from 'lucide-react' +import { useTranslation } from 'react-i18next' +import { apiFetch } from '@/lib/api' +import { formatBytes } from '@/lib/doc-attachments' +import { ConfirmDialog } from '@/components/ui/ConfirmDialog' + +interface RenderStorage { + file_path: string + file_bytes: number + cache_path?: string + cache_bytes?: number + cache_files?: number + fingerprint: string + can_reveal: boolean +} + +async function request(path: string, init?: RequestInit) { + const response = await apiFetch(path, init) + const body = await response.json() + if (!response.ok) throw new Error(body.detail || response.statusText) + return body +} + +export function OutputFileActions({ url, kind }: { url: string; kind: 'render' | 'generated' }) { + const render = kind === 'render' + const [removed, setRemoved] = useState(false) + const { t } = useTranslation() + const [open, setOpen] = useState(false) + const [storage, setStorage] = useState(null) + const [busy, setBusy] = useState(false) + const [confirm, setConfirm] = useState(false) + const [error, setError] = useState('') + const [message, setMessage] = useState('') + // 只对当前应用发布的工作区文件提供管理操作。 + const asset = URL.canParse(url, 'http://workspace.local') + ? new URL(url, 'http://workspace.local') + : null + const path = asset?.pathname ?? '' + const endpoint = (action: string) => `${path}/${action}${asset?.search ?? ''}` + const supported = + url.startsWith('/files/') && + /^\/files\/workspace-items\/ws_[0-9a-f]{32}\/wsi_[0-9a-f]{32}$/.test(path) + if (!supported) return null + + async function load(): Promise { + setBusy(true) + setError('') + try { + const result = await request(endpoint(render ? 'render-storage' : 'file-storage')) + setStorage(result) + return result + } catch (cause) { + setError(cause instanceof Error ? t(cause.message) : t('Unable to load file storage.')) + return null + } finally { + setBusy(false) + } + } + + async function clean() { + if (!storage) return + setBusy(true) + setError('') + try { + const result = await request(endpoint(render ? 'clean-render-cache' : 'remove-generated-output'), { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ fingerprint: storage.fingerprint }), + }) + if (render) { + setStorage({ ...storage, ...result }) + setMessage(t('Freed {{size}}. Final output is kept.', { size: formatBytes(result.freed_bytes) })) + } else { + setRemoved(true) + setMessage(t('Workspace file removed. The chat attachment is still available.')) + } + } catch (cause) { + setError(cause instanceof Error ? t(cause.message) : t('Unable to clean render files.')) + } finally { + setConfirm(false) + setBusy(false) + } + } + + const buttonClass = + 'rounded-md border border-[var(--border)] px-2 py-1 text-[12px] hover:bg-[var(--background)] disabled:opacity-40' + return ( +
+ + {open ? ( +
+ {busy && !storage ?

{t('Loading file storage…')}

: null} + {error ? ( +

+ {error} +

+ ) : null} + {message ?

{message}

: null} + {storage ? ( + <> + +
+ + {storage.can_reveal && !removed ? ( + + ) : null} +
+ {render &&

+ {t('Render files: {{count}} files · {{size}}', { + count: storage.cache_files ?? 0, + size: formatBytes(storage.cache_bytes ?? 0), + })} +

} +

+ {render + ? t('Cleanup keeps the final output, source code, and chat playback.') + : t('Removing the workspace copy keeps the chat attachment available.')} +

+ {!removed && } + + ) : !busy ? ( + + ) : null} +
+ ) : null} + setConfirm(false)} + onConfirm={() => void clean()} + > + {render ? <> +

{t('Remove {{count}} render files and free {{size}}?', { + count: storage?.cache_files ?? 0, + size: formatBytes(storage?.cache_bytes ?? 0), + })}

+

{t('Cleanup keeps the final output, source code, and chat playback.')}

+ :

{t('Remove the original generated file from the workspace? The chat attachment stays available.')}

} +
+
+ ) +} diff --git a/web/components/math-animator/RenderFileActions.tsx b/web/components/math-animator/RenderFileActions.tsx index 1ffa5f30a..9433f2990 100644 --- a/web/components/math-animator/RenderFileActions.tsx +++ b/web/components/math-animator/RenderFileActions.tsx @@ -1,211 +1,6 @@ -'use client' - -import { useState } from 'react' -import { Copy, FolderOpen } from 'lucide-react' -import { useTranslation } from 'react-i18next' -import { apiFetch } from '@/lib/api' -import { ConfirmDialog } from '@/components/ui/ConfirmDialog' - -interface RenderStorage { - file_path: string - file_bytes: number - cache_path: string - cache_bytes: number - cache_files: number - fingerprint: string - can_reveal: boolean -} - -function formatBytes(bytes: number): string { - if (bytes < 1024) return `${bytes} B` - if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB` - return `${(bytes / 1024 / 1024).toFixed(1)} MB` -} - -async function request(path: string, init?: RequestInit) { - const response = await apiFetch(path, init) - const body = await response.json() - if (!response.ok) throw new Error(body.detail || response.statusText) - return body -} +"use client"; +import { OutputFileActions } from "@/components/files/OutputFileActions"; export function RenderFileActions({ url }: { url: string }) { - const { t } = useTranslation() - const [open, setOpen] = useState(false) - const [storage, setStorage] = useState(null) - const [busy, setBusy] = useState(false) - const [confirm, setConfirm] = useState(false) - const [error, setError] = useState('') - const [message, setMessage] = useState('') - // 只对当前应用发布的工作区文件提供管理操作。 - const asset = URL.canParse(url, 'http://workspace.local') - ? new URL(url, 'http://workspace.local') - : null - const path = asset?.pathname ?? '' - const endpoint = (action: string) => `${path}/${action}${asset?.search ?? ''}` - const supported = - url.startsWith('/files/') && - /^\/files\/workspace-items\/ws_[0-9a-f]{32}\/wsi_[0-9a-f]{32}$/.test(path) - if (!supported) return null - - async function load(): Promise { - setBusy(true) - setError('') - try { - const result = await request(endpoint('render-storage')) - setStorage(result) - return result - } catch (cause) { - setError(cause instanceof Error ? t(cause.message) : t('Unable to load file storage.')) - return null - } finally { - setBusy(false) - } - } - - async function clean() { - if (!storage) return - setBusy(true) - setError('') - try { - const result = await request(endpoint('clean-render-cache'), { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ fingerprint: storage.fingerprint }), - }) - setStorage({ ...storage, ...result }) - setMessage( - t('Freed {{size}}. Final output is kept.', { size: formatBytes(result.freed_bytes) }) - ) - } catch (cause) { - setError(cause instanceof Error ? t(cause.message) : t('Unable to clean render files.')) - } finally { - setConfirm(false) - setBusy(false) - } - } - - const buttonClass = - 'rounded-md border border-[var(--border)] px-2 py-1 text-[12px] hover:bg-[var(--background)] disabled:opacity-40' - return ( -
- - {open ? ( -
- {busy && !storage ?

{t('Loading file storage…')}

: null} - {error ? ( -

- {error} -

- ) : null} - {message ?

{message}

: null} - {storage ? ( - <> - -
- - {storage.can_reveal ? ( - - ) : null} -
-

- {t('Render files: {{count}} files · {{size}}', { - count: storage.cache_files, - size: formatBytes(storage.cache_bytes), - })} -

-

- {t('Cleanup keeps the final output, source code, and chat playback.')} -

- - - ) : !busy ? ( - - ) : null} -
- ) : null} - setConfirm(false)} - onConfirm={() => void clean()} - > -

- {t('Remove {{count}} render files and free {{size}}?', { - count: storage?.cache_files ?? 0, - size: formatBytes(storage?.cache_bytes ?? 0), - })} -

-

- {t('Cleanup keeps the final output, source code, and chat playback.')} -

-
-
- ) + return ; }