From 30d87965a90c4283c5b27e26d58b47e2a41565c0 Mon Sep 17 00:00:00 2001 From: Satya Patel <14907857+saddlepaddle@users.noreply.github.com> Date: Tue, 20 Oct 2026 23:44:51 +0000 Subject: [PATCH 01/16] fix(desktop,host-service): send attachments through storage, not host-service bodies (SUPER-2658) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pasting or dropping an image into a cloud workspace failed with "Failed to send the file to the remote workspace" (terminal) or "Couldn't attach files / value$1 is not iterable" (chat). The desktop was shipping the bytes as a base64 `filesystem.writeFile` body over the streaming tRPC link. tRPC answers a streamed batch before it has read the request body, and a Vercel sandbox's ingress treats that early response as the end of the exchange and drops the upload still in flight — a race, not a size limit: a 500 KB body on a home uplink loses it about half the time. Desktop: the chat composer, the terminal rich input, and the terminal paste/drop override now take the route every other composer already takes (mobile, the new-workspace modal): `attachment.createUpload`, a presigned PUT to storage, `cloudWorkspace.attachFiles` for a box, then `attachments.materializeIntoWorkspace` on the host. The host-service request carries ids, never bytes. `uploadPastedFiles` is replaced by `sendAttachmentsToWorkspace` + `useSendAttachments`. host-service: `/trpc/*` buffers the request body before handing it to tRPC so no response can start before the request is complete; the integration test fails without it. `materializeIntoWorkspace` and the chat image writer share one helper that creates `.superset/attachments` with its `.gitignore`, which the former never wrote. --- .../useUploadAttachments.ts | 30 +-- .../components/TerminalPane/TerminalPane.tsx | 52 ++-- .../TerminalRichInput/TerminalRichInput.tsx | 33 +-- .../hooks/useSendAttachments/index.ts | 1 + .../useSendAttachments/useSendAttachments.ts | 54 ++++ .../utils/sendAttachmentsToWorkspace/index.ts | 5 + .../sendAttachmentsToWorkspace.test.ts | 181 +++++++++++++ .../sendAttachmentsToWorkspace.ts | 122 +++++++++ .../utils/uploadPastedFiles/index.ts | 4 - .../uploadPastedFiles.test.ts | 237 ------------------ .../uploadPastedFiles/uploadPastedFiles.ts | 165 ------------ docs/cloud-sandbox-mismatches.md | 29 ++- packages/host-service/src/app.ts | 13 + .../host-service/src/chat-v3/agentImages.ts | 17 +- .../src/runtime/workspace-attachments-dir.ts | 21 ++ .../trpc/router/attachments/attachments.ts | 6 +- .../trpc-request-body.integration.test.ts | 66 +++++ packages/i18n/locales/cs/messages.po | 3 - packages/i18n/locales/de/messages.po | 3 - packages/i18n/locales/en/messages.po | 3 - packages/i18n/locales/es/messages.po | 3 - packages/i18n/locales/fr/messages.po | 3 - packages/i18n/locales/id/messages.po | 3 - packages/i18n/locales/it/messages.po | 3 - packages/i18n/locales/ja/messages.po | 3 - packages/i18n/locales/ko/messages.po | 3 - packages/i18n/locales/nl/messages.po | 3 - packages/i18n/locales/pl/messages.po | 3 - packages/i18n/locales/pt-BR/messages.po | 3 - packages/i18n/locales/ru/messages.po | 3 - packages/i18n/locales/tr/messages.po | 3 - packages/i18n/locales/vi/messages.po | 3 - packages/i18n/locales/zh-CN/messages.po | 3 - packages/i18n/locales/zh-TW/messages.po | 3 - 34 files changed, 518 insertions(+), 569 deletions(-) create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts delete mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/index.ts delete mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts delete mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts create mode 100644 packages/host-service/src/runtime/workspace-attachments-dir.ts create mode 100644 packages/host-service/test/integration/trpc-request-body.integration.test.ts diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index 60d8a3b05a3..3a46d51ac8d 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -1,51 +1,33 @@ import { useLingui } from "@lingui/react/macro"; import { errorMessage } from "@superset/i18n/errors"; import { toast } from "@superset/ui/sonner"; -import { workspaceTrpc } from "@superset/workspace-client"; import { useCallback } from "react"; -import { - PasteUploadLimitError, - uploadPastedFiles, -} from "../../../../../../utils/uploadPastedFiles"; +import { useSendAttachments } from "../../../../../../hooks/useSendAttachments"; +import { AttachmentLimitError } from "../../../../../../utils/sendAttachmentsToWorkspace"; import { formatAttachmentTag } from "../../../../utils/attachmentTags"; export function useUploadAttachments(workspaceId: string) { const { t } = useLingui(); - const { data: workspace } = workspaceTrpc.workspace.get.useQuery({ - id: workspaceId, - }); - const createDirectory = - workspaceTrpc.filesystem.createDirectory.useMutation(); - const writeFile = workspaceTrpc.filesystem.writeFile.useMutation(); - const worktreePath = workspace?.worktreePath; + const sendAttachments = useSendAttachments(workspaceId); return useCallback( async (files: File[]): Promise => { if (files.length === 0) return []; try { - if (!worktreePath) throw new Error("Workspace is not ready"); - const paths = await uploadPastedFiles({ - deps: { - createDirectory: (input) => createDirectory.mutateAsync(input), - writeFile: (input) => writeFile.mutateAsync(input), - }, - workspaceId, - worktreePath, - files, - }); + const paths = await sendAttachments(files); return paths.map((path, index) => formatAttachmentTag({ path, type: files[index]?.type ?? "" }), ); } catch (error) { toast.error(t({ message: "Couldn't attach files" }), { description: - error instanceof PasteUploadLimitError + error instanceof AttachmentLimitError ? error.message : errorMessage(error, t({ message: "Unknown error" })), }); return null; } }, - [worktreePath, workspaceId, createDirectory, writeFile, t], + [sendAttachments, t], ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/TerminalPane.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/TerminalPane.tsx index c091ef0a449..efa204645eb 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/TerminalPane.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/TerminalPane.tsx @@ -46,6 +46,7 @@ import { useTheme } from "renderer/stores/theme"; import { resolveTerminalThemeType } from "renderer/stores/theme/utils"; import { isWithinWorkspacePath } from "shared/absolute-paths"; import { useLinkClickHint } from "../../hooks/useLinkClickHint"; +import { useSendAttachments } from "../../hooks/useSendAttachments"; import { runFileLinkAction, runFolderLinkAction, @@ -53,10 +54,7 @@ import { type TerminalLinkActionDeps, terminalPullRequestLinkAction, } from "../../utils/runTerminalLinkAction"; -import { - PasteUploadLimitError, - uploadPastedFiles, -} from "../../utils/uploadPastedFiles"; +import { AttachmentLimitError } from "../../utils/sendAttachmentsToWorkspace"; import { TerminalAgentAutoResume } from "./components/TerminalAgentAutoResume"; import { TerminalCopiedIndicator } from "./components/TerminalCopiedIndicator"; import { TerminalNarrowedBanner } from "./components/TerminalNarrowedBanner"; @@ -401,9 +399,9 @@ export function TerminalPane({ // The default paste path forwards Ctrl+V and lets the TUI read the OS // clipboard — which only exists on the machine the PTY runs on. When the // workspace lives on another host (relay-reached machine, cloud sandbox), - // ship the clipboard bytes there via filesystem.writeFile and paste the - // resulting paths instead. Local workspaces keep the Ctrl+V forward, which - // lets TUIs attach the image natively. + // send the clipboard files to the workspace and paste the resulting paths + // instead. Local workspaces keep the Ctrl+V forward, which lets TUIs + // attach the image natively. const { machineId } = useLocalHostService(); const hostWorkspaces = useHostWorkspaces(); const workspaceHostId = hostWorkspaces.workspaces.find( @@ -417,28 +415,14 @@ export function TerminalPane({ Boolean(machineId) && workspaceHostId !== machineId; - const writeFileMutation = workspaceTrpc.filesystem.writeFile.useMutation(); - const createDirectoryMutation = - workspaceTrpc.filesystem.createDirectory.useMutation(); - const writeFileRef = useRef(writeFileMutation.mutateAsync); - writeFileRef.current = writeFileMutation.mutateAsync; - const createDirectoryRef = useRef(createDirectoryMutation.mutateAsync); - createDirectoryRef.current = createDirectoryMutation.mutateAsync; + const sendAttachments = useSendAttachments(workspaceId); // Fire-and-forget: ship files to the workspace, then paste the paths. const uploadAndPasteFiles = useCallback( - (files: File[], worktree: string) => { + (files: File[]) => { void (async () => { try { - const paths = await uploadPastedFiles({ - deps: { - createDirectory: (input) => createDirectoryRef.current(input), - writeFile: (input) => writeFileRef.current(input), - }, - workspaceId, - worktreePath: worktree, - files, - }); + const paths = await sendAttachments(files); terminalRuntimeRegistry.paste( terminalId, shellEscapePaths(paths), @@ -447,7 +431,7 @@ export function TerminalPane({ } catch (error) { console.error("[v2 Terminal] remote file upload failed", error); toast.error( - error instanceof PasteUploadLimitError + error instanceof AttachmentLimitError ? error.message : files.length === 1 ? t({ @@ -460,15 +444,15 @@ export function TerminalPane({ } })(); }, - [terminalId, terminalInstanceId, workspaceId, t], + [terminalId, terminalInstanceId, sendAttachments, t], ); useEffect(() => { - if (!isRemoteHost || !worktreePath) return; + if (!isRemoteHost) return; terminalRuntimeRegistry.setImagePasteOverride( terminalId, - (files) => uploadAndPasteFiles(files, worktreePath), + uploadAndPasteFiles, terminalInstanceId, ); return () => { @@ -478,13 +462,7 @@ export function TerminalPane({ terminalInstanceId, ); }; - }, [ - terminalId, - terminalInstanceId, - worktreePath, - isRemoteHost, - uploadAndPasteFiles, - ]); + }, [terminalId, terminalInstanceId, isRemoteHost, uploadAndPasteFiles]); useTerminalInterruptClear({ terminalId, @@ -588,7 +566,7 @@ export function TerminalPane({ // remote workspace's PTY runs on. When every dropped entry is a plain // file, ship the bytes instead. Folders keep the path flow (their File // entries carry no content), which at least preserves today's behavior. - if (isRemoteHost && worktreePath) { + if (isRemoteHost) { const items = Array.from(event.dataTransfer.items); const allPlainFiles = items.length > 0 && @@ -603,7 +581,7 @@ export function TerminalPane({ terminalRuntimeRegistry .getTerminal(terminalId, terminalInstanceId) ?.focus(); - uploadAndPasteFiles(files, worktreePath); + uploadAndPasteFiles(files); return; } } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/components/TerminalRichInput/TerminalRichInput.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/components/TerminalRichInput/TerminalRichInput.tsx index d43131d5b51..b0a71f92c0f 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/components/TerminalRichInput/TerminalRichInput.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/TerminalPane/components/TerminalRichInput/TerminalRichInput.tsx @@ -21,10 +21,8 @@ import { usePluginMentionOptions } from "renderer/hooks/usePluginMentionOptions" import { useHotkeyDisplay } from "renderer/hotkeys"; import { track } from "renderer/lib/analytics"; import { terminalRuntimeRegistry } from "renderer/lib/terminal/terminal-runtime-registry"; -import { - PasteUploadLimitError, - uploadPastedFiles, -} from "../../../../utils/uploadPastedFiles"; +import { useSendAttachments } from "../../../../hooks/useSendAttachments"; +import { AttachmentLimitError } from "../../../../utils/sendAttachmentsToWorkspace"; import { TerminalPaneIcon } from "../TerminalPaneIcon"; import { prepareTerminalSubmission } from "./prepareTerminalSubmission"; @@ -125,13 +123,7 @@ function TerminalRichInputInner({ [trpcUtils, workspaceId], ); - const writeFileMutation = workspaceTrpc.filesystem.writeFile.useMutation(); - const createDirectoryMutation = - workspaceTrpc.filesystem.createDirectory.useMutation(); - const writeFileRef = useRef(writeFileMutation.mutateAsync); - writeFileRef.current = writeFileMutation.mutateAsync; - const createDirectoryRef = useRef(createDirectoryMutation.mutateAsync); - createDirectoryRef.current = createDirectoryMutation.mutateAsync; + const sendAttachments = useSendAttachments(workspaceId); const handleSubmit = useCallback( async (message: PromptInputMessage) => { @@ -141,27 +133,15 @@ function TerminalRichInputInner({ // async onSubmit), so the user does not lose the draft or the files. let attachmentPaths: string[] = []; if (message.files.length > 0) { - if (!cwd) { - toast.error(t({ message: "Workspace path is not available yet" })); - throw new Error("no worktree path"); - } try { - attachmentPaths = await uploadPastedFiles({ - deps: { - createDirectory: (input) => createDirectoryRef.current(input), - writeFile: (input) => writeFileRef.current(input), - }, - workspaceId, - worktreePath: cwd, - files: await toFiles(message.files), - }); + attachmentPaths = await sendAttachments(await toFiles(message.files)); } catch (error) { console.error( "[v2 TerminalRichInput] attachment upload failed", error, ); toast.error( - error instanceof PasteUploadLimitError + error instanceof AttachmentLimitError ? error.message : message.files.length === 1 ? "Failed to attach the file" @@ -193,8 +173,7 @@ function TerminalRichInputInner({ terminalInstanceId, controller, workspaceId, - cwd, - t, + sendAttachments, sendToTerminalAgent, ], ); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/index.ts new file mode 100644 index 00000000000..f52d0716aa7 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/index.ts @@ -0,0 +1 @@ +export { useSendAttachments } from "./useSendAttachments"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts new file mode 100644 index 00000000000..5741acaa914 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts @@ -0,0 +1,54 @@ +import { workspaceTrpc } from "@superset/workspace-client"; +import { useCallback, useRef } from "react"; +import { useWorkspaceHostTarget } from "renderer/hooks/host-service/useWorkspaceHostUrl"; +import { apiTrpcClient } from "renderer/lib/api-trpc-client"; +import { sendAttachmentsToWorkspace } from "../../utils/sendAttachmentsToWorkspace"; + +async function putObject( + target: { url: string; headers: Record }, + file: File, +): Promise { + return await fetch(target.url, { + method: "PUT", + headers: target.headers, + body: file, + }); +} + +/** + * A stable function that gets files into the workspace's worktree and + * returns their worktree-relative paths. Stable so a terminal's paste + * override, registered once, always sees the current host target. + */ +export function useSendAttachments( + workspaceId: string, +): (files: File[]) => Promise { + const target = useWorkspaceHostTarget(workspaceId); + const isCloudWorkspace = + target.status === "ready" && target.kind === "sandbox"; + const isCloudRef = useRef(isCloudWorkspace); + isCloudRef.current = isCloudWorkspace; + + const materialize = + workspaceTrpc.attachments.materializeIntoWorkspace.useMutation(); + const materializeRef = useRef(materialize.mutateAsync); + materializeRef.current = materialize.mutateAsync; + + return useCallback( + (files: File[]) => + sendAttachmentsToWorkspace({ + deps: { + createUpload: (input) => + apiTrpcClient.attachment.createUpload.mutate(input), + putObject, + attachFiles: (input) => + apiTrpcClient.cloudWorkspace.attachFiles.mutate(input), + materialize: (input) => materializeRef.current(input), + }, + workspaceId, + isCloudWorkspace: isCloudRef.current, + files, + }), + [workspaceId], + ); +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts new file mode 100644 index 00000000000..c3094082875 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts @@ -0,0 +1,5 @@ +export { + AttachmentLimitError, + type SendAttachmentsDeps, + sendAttachmentsToWorkspace, +} from "./sendAttachmentsToWorkspace"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts new file mode 100644 index 00000000000..e8ddd2758b8 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts @@ -0,0 +1,181 @@ +import { describe, expect, it } from "bun:test"; +import { + MAX_ATTACHMENT_BYTES, + MAX_ATTACHMENTS, +} from "@superset/shared/attachment-limits"; +import { + AttachmentLimitError, + type SendAttachmentsDeps, + sendAttachmentsToWorkspace, +} from "./sendAttachmentsToWorkspace"; + +interface Recorded { + uploads: Array<{ name: string; contentType: string; sizeBytes: number }>; + puts: Array<{ url: string; file: File }>; + attached: Array<{ id: string; fileIds: string[] }>; + materialized: Array<{ workspaceId: string; fileIds: string[] }>; +} + +function makeDeps(options: { putStatus?: number } = {}) { + const recorded: Recorded = { + uploads: [], + puts: [], + attached: [], + materialized: [], + }; + let nextId = 0; + const deps: SendAttachmentsDeps = { + async createUpload(input) { + recorded.uploads.push(input); + nextId += 1; + return { + fileId: `file-${nextId}`, + upload: { url: `https://r2.test/${nextId}`, headers: {} }, + }; + }, + async putObject(target, file) { + recorded.puts.push({ url: target.url, file }); + return new Response(null, { status: options.putStatus ?? 200 }); + }, + async attachFiles(input) { + recorded.attached.push(input); + }, + async materialize(input) { + recorded.materialized.push(input); + return { + paths: input.fileIds.map((id) => `.superset/attachments/${id}.png`), + }; + }, + }; + return { deps, recorded }; +} + +const png = (name: string, size = 4) => + new File([new Uint8Array(size)], name, { type: "image/png" }); + +describe("sendAttachmentsToWorkspace", () => { + it("uploads each file to storage, then materializes them by id in order", async () => { + const { deps, recorded } = makeDeps(); + const paths = await sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [png("a.png"), png("b.png", 9)], + }); + + expect(recorded.uploads).toEqual([ + { name: "a.png", contentType: "image/png", sizeBytes: 4 }, + { name: "b.png", contentType: "image/png", sizeBytes: 9 }, + ]); + expect(recorded.puts.map((p) => p.url)).toEqual([ + "https://r2.test/1", + "https://r2.test/2", + ]); + expect(recorded.materialized).toEqual([ + { workspaceId: "ws-1", fileIds: ["file-1", "file-2"] }, + ]); + expect(recorded.attached).toEqual([]); + expect(paths).toEqual([ + ".superset/attachments/file-1.png", + ".superset/attachments/file-2.png", + ]); + }); + + it("attaches the files to a cloud workspace before the box fetches them", async () => { + const { deps, recorded } = makeDeps(); + await sendAttachmentsToWorkspace({ + deps, + workspaceId: "cloud-ws", + isCloudWorkspace: true, + files: [png("shot.png")], + }); + expect(recorded.attached).toEqual([ + { id: "cloud-ws", fileIds: ["file-1"] }, + ]); + expect(recorded.materialized).toEqual([ + { workspaceId: "cloud-ws", fileIds: ["file-1"] }, + ]); + }); + + it("names a nameless blob from its mime type and defaults the content type", async () => { + const { deps, recorded } = makeDeps(); + await sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [ + new File([new Uint8Array(2)], "", { type: "image/jpeg" }), + new File([new Uint8Array(2)], "", { type: "" }), + ], + }); + expect(recorded.uploads).toEqual([ + { name: "attachment_1.jpg", contentType: "image/jpeg", sizeBytes: 2 }, + { + name: "attachment_2", + contentType: "application/octet-stream", + sizeBytes: 2, + }, + ]); + }); + + it("fails before uploading anything when a file is over the limit", async () => { + const { deps, recorded } = makeDeps(); + const big = { + name: "huge.png", + type: "image/png", + size: MAX_ATTACHMENT_BYTES + 1, + } as File; + await expect( + sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [png("ok.png"), big], + }), + ).rejects.toBeInstanceOf(AttachmentLimitError); + expect(recorded.uploads).toEqual([]); + }); + + it("refuses more files than one send may carry", async () => { + const { deps, recorded } = makeDeps(); + const files = Array.from({ length: MAX_ATTACHMENTS + 1 }, (_, i) => + png(`${i}.png`), + ); + await expect( + sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: false, + files, + }), + ).rejects.toBeInstanceOf(AttachmentLimitError); + expect(recorded.uploads).toEqual([]); + }); + + it("surfaces a rejected PUT and never materializes", async () => { + const { deps, recorded } = makeDeps({ putStatus: 403 }); + await expect( + sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [png("a.png")], + }), + ).rejects.toThrow("Upload failed for a.png (403)"); + expect(recorded.materialized).toEqual([]); + }); + + it("does nothing for an empty list", async () => { + const { deps, recorded } = makeDeps(); + expect( + await sendAttachmentsToWorkspace({ + deps, + workspaceId: "ws-1", + isCloudWorkspace: true, + files: [], + }), + ).toEqual([]); + expect(recorded.uploads).toEqual([]); + expect(recorded.attached).toEqual([]); + }); +}); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts new file mode 100644 index 00000000000..75b029eb8af --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts @@ -0,0 +1,122 @@ +import { + MAX_ATTACHMENT_BYTES, + MAX_ATTACHMENTS, +} from "@superset/shared/attachment-limits"; +import { attachmentFallbackName } from "@superset/shared/workspace-attachments"; + +/** Thrown for limits the user can act on; the message is toast-ready. */ +export class AttachmentLimitError extends Error {} + +function formatMb(bytes: number): string { + return `${Math.round(bytes / 1024 / 1024)}MB`; +} + +export interface AttachmentUploadTarget { + url: string; + headers: Record; +} + +export interface SendAttachmentsDeps { + createUpload(input: { + name: string; + contentType: string; + sizeBytes: number; + }): Promise<{ fileId: string; upload: AttachmentUploadTarget }>; + putObject(target: AttachmentUploadTarget, file: File): Promise; + /** Cloud only: a box can fetch only the files attached to it. */ + attachFiles(input: { id: string; fileIds: string[] }): Promise; + materialize(input: { + workspaceId: string; + fileIds: string[]; + }): Promise<{ paths: string[] }>; +} + +const MIME_EXTENSIONS: Record = { + "image/png": "png", + "image/jpeg": "jpg", + "image/gif": "gif", + "image/webp": "webp", + "image/svg+xml": "svg", + "image/tiff": "tiff", + "image/bmp": "bmp", +}; + +const FALLBACK_CONTENT_TYPE = "application/octet-stream"; + +/** + * Clipboard files keep their name when they have one (Chromium synthesizes + * "image.png" for a screenshot; Finder copies keep the real name); a blob + * with none takes its extension from the mime type, the only signal it + * carries. The host sanitizes and deduplicates whatever arrives. + */ +function nameOf(file: File, index: number): string { + if (file.name) return file.name; + const extension = MIME_EXTENSIONS[file.type]; + return attachmentFallbackName(index, extension ? `.${extension}` : ""); +} + +function assertWithinLimits(files: File[]): void { + if (files.length > MAX_ATTACHMENTS) { + throw new AttachmentLimitError( + `${files.length} files attached — a message carries at most ${MAX_ATTACHMENTS}`, + ); + } + for (const file of files) { + if (file.size > MAX_ATTACHMENT_BYTES) { + throw new AttachmentLimitError( + `"${file.name || "attachment"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_ATTACHMENT_BYTES)} can't be attached`, + ); + } + } +} + +async function uploadOne( + deps: SendAttachmentsDeps, + file: File, + index: number, +): Promise { + const name = nameOf(file, index); + const { fileId, upload } = await deps.createUpload({ + name, + contentType: file.type || FALLBACK_CONTENT_TYPE, + sizeBytes: file.size, + }); + const response = await deps.putObject(upload, file); + if (!response.ok) { + throw new Error(`Upload failed for ${name} (${response.status})`); + } + return fileId; +} + +/** + * Gets composer files into a workspace's worktree and returns the + * worktree-relative paths an agent is handed — the same route the mobile + * composer and the new-workspace modal take: bytes go client → cloud storage + * over a presigned PUT, and the host pulls them from there. The host-service + * request carries only ids, so neither a relay's body cap nor a sandbox + * ingress that cuts an upload the host answers early ever sees the bytes. + * + * The caller owns the error surface; a thrown `AttachmentLimitError` is + * already worded for a toast. + */ +export async function sendAttachmentsToWorkspace(options: { + deps: SendAttachmentsDeps; + workspaceId: string; + isCloudWorkspace: boolean; + files: File[]; +}): Promise { + const { deps, workspaceId, isCloudWorkspace, files } = options; + if (files.length === 0) return []; + assertWithinLimits(files); + + // Order is kept: the host names attachments positionally and the caller + // pairs each path back with the file it came from. + const fileIds = await Promise.all( + files.map((file, index) => uploadOne(deps, file, index)), + ); + if (isCloudWorkspace) { + await deps.attachFiles({ id: workspaceId, fileIds }); + } + const { paths } = await deps.materialize({ workspaceId, fileIds }); + return paths; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/index.ts deleted file mode 100644 index c1582bc0276..00000000000 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export { - PasteUploadLimitError, - uploadPastedFiles, -} from "./uploadPastedFiles"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts deleted file mode 100644 index afd97401f77..00000000000 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts +++ /dev/null @@ -1,237 +0,0 @@ -import { afterAll, beforeAll, describe, expect, it } from "bun:test"; -import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; -import { - PasteUploadLimitError, - type UploadPastedFilesDeps, - uploadPastedFiles, -} from "./uploadPastedFiles"; - -// fileToBase64 goes through FileReader.readAsDataURL, which the renderer has -// but Bun's test runtime may not — pin a minimal implementation over -// Blob.arrayBuffer so the tests exercise the real base64 pipeline. -let fileReads = 0; - -class TestFileReader { - result: string | null = null; - error: Error | null = null; - onload: (() => void) | null = null; - onerror: (() => void) | null = null; - - readAsDataURL(file: File) { - fileReads += 1; - file - .arrayBuffer() - .then((buffer) => { - const base64 = Buffer.from(buffer).toString("base64"); - this.result = `data:${file.type};base64,${base64}`; - this.onload?.(); - }) - .catch((error) => { - this.error = error; - this.onerror?.(); - }); - } -} - -const originalFileReader = (globalThis as { FileReader?: unknown }).FileReader; - -beforeAll(() => { - (globalThis as { FileReader?: unknown }).FileReader = TestFileReader; -}); - -afterAll(() => { - (globalThis as { FileReader?: unknown }).FileReader = originalFileReader; -}); - -interface RecordedWrite { - absolutePath: string; - content: string | { kind: "base64"; data: string }; - options: { create: boolean; overwrite: boolean }; -} - -/** - * Fake deps backed by a real "directory": create-only writes report "exists" - * on a name collision exactly like the host does, so the collision-probing - * naming is exercised for real. - */ -function makeDeps(options: { existing?: string[]; failWrites?: boolean } = {}) { - const createdDirs: string[] = []; - const writes: RecordedWrite[] = []; - const existing = new Set(options.existing ?? []); - const deps: UploadPastedFilesDeps = { - async createDirectory(input) { - createdDirs.push(input.absolutePath); - }, - async writeFile(input) { - const write: RecordedWrite = { - absolutePath: input.absolutePath, - content: input.content, - options: input.options, - }; - writes.push(write); - if (options.failWrites && !input.absolutePath.endsWith(".gitignore")) { - return { ok: false, reason: "conflict" }; - } - if (existing.has(input.absolutePath) && !input.options.overwrite) { - return { ok: false, reason: "exists" }; - } - existing.add(input.absolutePath); - return { ok: true }; - }, - }; - return { deps, createdDirs, writes }; -} - -const WORKTREE = "/home/user/.superset/worktrees/demo"; -const DIR_ABS = `${WORKTREE}/${WORKSPACE_ATTACHMENTS_DIR}`; - -describe("uploadPastedFiles", () => { - it("creates the attachments dir, self-ignores it, writes each file, returns relative paths", async () => { - const { deps, createdDirs, writes } = makeDeps(); - const files = [ - new File([new Uint8Array([1, 2, 3])], "image.png", { - type: "image/png", - }), - new File([new Uint8Array([4, 5])], "notes.pdf", { - type: "application/pdf", - }), - ]; - - const paths = await uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files, - }); - - expect(createdDirs).toEqual([DIR_ABS]); - expect(writes[0]).toEqual({ - absolutePath: `${DIR_ABS}/.gitignore`, - content: "*\n", - // create-only: a user's own .gitignore edit is never clobbered. - options: { create: true, overwrite: false }, - }); - // Worktree-relative, matching the agent-launch and mobile convention. - expect(paths).toEqual([ - `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, - `${WORKSPACE_ATTACHMENTS_DIR}/notes.pdf`, - ]); - expect(writes[1]?.options).toEqual({ create: true, overwrite: false }); - expect(writes[1]?.content).toEqual({ - kind: "base64", - data: Buffer.from([1, 2, 3]).toString("base64"), - }); - }); - - it("never overwrites an earlier paste — collisions probe to a fresh _N name", async () => { - const { deps, writes } = makeDeps({ - existing: [`${DIR_ABS}/image.png`, `${DIR_ABS}/image_1.png`], - }); - - const paths = await uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files: [new File(["x"], "image.png", { type: "image/png" })], - }); - - expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/image_2.png`]); - // Every probe was create-only; nothing was written over. - expect(writes.every((w) => w.options.overwrite === false)).toBe(true); - }); - - it("tolerates a .gitignore that already exists from an earlier paste", async () => { - const { deps } = makeDeps({ existing: [`${DIR_ABS}/.gitignore`] }); - - const paths = await uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files: [new File(["x"], "image.png", { type: "image/png" })], - }); - - expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/image.png`]); - }); - - it("throws when a file write is refused", async () => { - const { deps } = makeDeps({ failWrites: true }); - - await expect( - uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files: [new File(["x"], "image.png", { type: "image/png" })], - }), - ).rejects.toThrow("conflict"); - }); - - it("refuses oversized files before reading any bytes", async () => { - const { deps, createdDirs, writes } = makeDeps(); - fileReads = 0; - const big = new File([new Uint8Array(1)], "video.mov", { - type: "video/quicktime", - }); - // A 2GB Blob in the test would be real memory; fake the size instead — - // the check reads file.size only. - Object.defineProperty(big, "size", { value: 2 * 1024 * 1024 * 1024 }); - - await expect( - uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files: [big], - }), - ).rejects.toThrow(PasteUploadLimitError); - // Failed fast: nothing read, no directory created, no bytes shipped. - expect(fileReads).toBe(0); - expect(createdDirs).toHaveLength(0); - expect(writes).toHaveLength(0); - }); - - it("refuses batches over the total budget", async () => { - const { deps, createdDirs, writes } = makeDeps(); - fileReads = 0; - const files = Array.from({ length: 5 }, (_, i) => { - const f = new File([new Uint8Array(1)], `part${i}.bin`, { - type: "application/octet-stream", - }); - Object.defineProperty(f, "size", { value: 45 * 1024 * 1024 }); - return f; - }); - - await expect( - uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files, - }), - ).rejects.toThrow(PasteUploadLimitError); - expect(fileReads).toBe(0); - expect(createdDirs).toHaveLength(0); - expect(writes).toHaveLength(0); - }); - - it("sanitizes path-hostile clipboard names and falls back to attachment_N", async () => { - const { deps } = makeDeps(); - - const paths = await uploadPastedFiles({ - deps, - workspaceId: "ws-1", - worktreePath: WORKTREE, - files: [ - new File(["x"], "../shot: 1/final.png", { type: "image/png" }), - new File(["y"], "", { type: "image/png" }), - new File(["z"], "", { type: "application/x-thing" }), - ], - }); - - expect(paths).toEqual([ - `${WORKSPACE_ATTACHMENTS_DIR}/.._shot__1_final.png`, - `${WORKSPACE_ATTACHMENTS_DIR}/attachment_2.png`, - `${WORKSPACE_ATTACHMENTS_DIR}/attachment_3`, - ]); - }); -}); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts deleted file mode 100644 index 6d6e061c2cd..00000000000 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts +++ /dev/null @@ -1,165 +0,0 @@ -import { - attachmentFallbackName, - attachmentNameWithSuffix, - sanitizeAttachmentFileName, - WORKSPACE_ATTACHMENTS_DIR, -} from "@superset/shared/workspace-attachments"; -import { fileToBase64 } from "renderer/lib/file-to-base64"; - -// Pastes follow the shared workspace-attachments convention (same directory -// and naming as agent-launch and the mobile composer): worktree-relative -// paths, since `filesystem.writeFile` refuses paths outside the worktree. -// The directory self-ignores (see the `.gitignore` write below) so -// attachments never dirty git status. - -/** Collision-probe cap; matches the spirit of createUniqueEntry's bound. */ -const MAX_NAME_ATTEMPTS = 50; - -// Same caps as the agent-launch adapter's attachment writer. The bytes are -// FileReader-read and base64'd (~1.33x) in renderer memory, then travel as -// one JSON body — an accidentally pasted multi-GB Finder file must fail -// fast instead of stalling the renderer and the host. -const MAX_SINGLE_FILE_BYTES = 50 * 1024 * 1024; -const MAX_TOTAL_BYTES = 200 * 1024 * 1024; - -/** Thrown for limits the user can act on; the message is toast-ready. */ -export class PasteUploadLimitError extends Error {} - -function formatMb(bytes: number): string { - return `${Math.round(bytes / 1024 / 1024)}MB`; -} - -interface FsWriteOutcome { - ok: boolean; - reason?: string; -} - -export interface UploadPastedFilesDeps { - createDirectory(input: { - workspaceId: string; - absolutePath: string; - recursive: boolean; - }): Promise; - writeFile(input: { - workspaceId: string; - absolutePath: string; - content: string | { kind: "base64"; data: string }; - options: { create: boolean; overwrite: boolean }; - }): Promise; -} - -const MIME_EXTENSIONS: Record = { - "image/png": "png", - "image/jpeg": "jpg", - "image/gif": "gif", - "image/webp": "webp", - "image/svg+xml": "svg", - "image/tiff": "tiff", - "image/bmp": "bmp", -}; - -/** - * Clipboard names (Chromium synthesizes "image.png" for screenshots; Finder - * copies keep their real name) go through the shared sanitizer; generated - * fallbacks take their extension from the mime type, the only signal a - * clipboard file carries. - */ -function baseFileName(file: File, index: number): string { - const sanitized = sanitizeAttachmentFileName(file.name); - if (sanitized) return sanitized; - const extension = MIME_EXTENSIONS[file.type]; - return attachmentFallbackName(index, extension ? `.${extension}` : ""); -} - -/** - * Write one file under a collision-free name. The batch conventions dedupe - * against an in-memory set; pastes arrive one at a time across a session, so - * probe the real directory instead (create-only writes report "exists") — - * a second screenshot must never overwrite a path an earlier prompt already - * references. - */ -async function writeUnique( - deps: UploadPastedFilesDeps, - workspaceId: string, - dirAbs: string, - base: string, - data: string, -): Promise { - for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { - const fileName = attachmentNameWithSuffix(base, attempt); - const outcome = await deps.writeFile({ - workspaceId, - absolutePath: `${dirAbs}/${fileName}`, - content: { kind: "base64", data }, - options: { create: true, overwrite: false }, - }); - if (outcome.ok !== false) return fileName; - if (outcome.reason !== "exists") { - throw new Error(outcome.reason ?? "write failed"); - } - } - throw new Error(`Could not find a free name for ${base}`); -} - -/** - * Ship clipboard files to a workspace whose PTY runs on another machine and - * return the worktree-relative paths written, ready to paste into the - * terminal. Throws on the first failed write — the caller owns the error - * surface. - */ -export async function uploadPastedFiles(options: { - deps: UploadPastedFilesDeps; - workspaceId: string; - worktreePath: string; - files: File[]; -}): Promise { - const { deps, workspaceId, worktreePath, files } = options; - const dirAbs = `${worktreePath}/${WORKSPACE_ATTACHMENTS_DIR}`; - - let totalBytes = 0; - for (const file of files) { - if (file.size > MAX_SINGLE_FILE_BYTES) { - throw new PasteUploadLimitError( - `"${file.name || "pasted file"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_SINGLE_FILE_BYTES)} can't be sent to a remote workspace`, - ); - } - totalBytes += file.size; - } - if (totalBytes > MAX_TOTAL_BYTES) { - throw new PasteUploadLimitError( - `Pasted files total ${formatMb(totalBytes)} — more than the ${formatMb(MAX_TOTAL_BYTES)} limit for a remote workspace`, - ); - } - - await deps.createDirectory({ - workspaceId, - absolutePath: dirAbs, - recursive: true, - }); - - // `*` ignores every attachment and the .gitignore itself, so agents never - // see attachments as untracked changes. create-only: a user's own edit wins. - const ignoreOutcome = await deps.writeFile({ - workspaceId, - absolutePath: `${dirAbs}/.gitignore`, - content: "*\n", - options: { create: true, overwrite: false }, - }); - if (ignoreOutcome.ok === false && ignoreOutcome.reason !== "exists") { - throw new Error(ignoreOutcome.reason ?? "write failed"); - } - - const paths: string[] = []; - for (const [index, file] of files.entries()) { - const data = await fileToBase64(file); - const fileName = await writeUnique( - deps, - workspaceId, - dirAbs, - baseFileName(file, index), - data, - ); - paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); - } - return paths; -} diff --git a/docs/cloud-sandbox-mismatches.md b/docs/cloud-sandbox-mismatches.md index 4cf1cb861c6..5b456c6dc0b 100644 --- a/docs/cloud-sandbox-mismatches.md +++ b/docs/cloud-sandbox-mismatches.md @@ -252,13 +252,28 @@ forwards Ctrl+V and lets the TUI (Claude Code, Codex) read the image from the OS clipboard — of the machine the PTY runs on. A sandbox (or any relay-reached host) never holds the user's local screenshot, so the paste silently did nothing or surfaced "Failed to paste image". Fixed renderer-side: -for non-local hosts the desktop ships the clipboard bytes over -`filesystem.writeFile` into the shared `.superset/attachments/` worktree dir -(the same convention the agent-launch terminal adapter and the mobile -composer use — mobile proved the pattern) and pastes the worktree-relative -path instead (`setImagePasteOverride` in the terminal runtime registry). -Chosen over a new host endpoint because deployed sandboxes never update -their baked host-service. +for non-local hosts the desktop sends the clipboard files the way every +composer sends attachments — `attachment.createUpload` on the API, a +presigned PUT to storage, `cloudWorkspace.attachFiles` for a box, then +`attachments.materializeIntoWorkspace` on the host — and pastes the +worktree-relative paths instead (`setImagePasteOverride` in the terminal +runtime registry, `useSendAttachments` in the pane registry). + +**The ingress drops an upload the host answers early.** tRPC's streamed batch +(`httpBatchStreamLink`, `trpc-accept: application/jsonl`) returns its response +head before it has read the request body. Behind a Vercel sandbox's ingress +(`sb-*.vercel.run`, which the gate forwards to) that early response ends the +exchange: the rest of the upload is dropped, the procedure never runs, and the +client sees a stream that ends after the head line — "value$1 is not iterable" +in tRPC's consumer. It is a race, not a size limit: a 500 KB body on a home +uplink loses it about half the time, a 1 MB body on a datacenter link wins +it. This is why shipping file bytes through `filesystem.writeFile` broke +(SUPER-2658) and why attachments go through storage instead. host-service now +buffers every `/trpc/*` body before handing it to tRPC, so no response starts +before the request is complete; a sandbox provisioned before that bundle still +has the race for any request with a sizeable body. Reproducing with curl: pass +`-H "Expect:"` — curl's automatic `Expect: 100-continue` on bodies ≥1 MB +masks the bug, and browsers never send it. **A URL launcher succeeds and reaches nobody.** The image ships `xdg-utils` (`packages/sandbox/bundle/rootfs/usr/local/share/superset/desktop.Aptfile`) diff --git a/packages/host-service/src/app.ts b/packages/host-service/src/app.ts index 48b16743d13..46551054d6d 100644 --- a/packages/host-service/src/app.ts +++ b/packages/host-service/src/app.ts @@ -490,6 +490,19 @@ export function createApp(options: CreateAppOptions): CreateAppResult { }); registerChatV3Routes({ app, db, mount: chatV3, upgradeWebSocket }); + // tRPC answers a streamed batch before it has read the request body, and + // a sandbox's ingress treats that early response as the end of the + // exchange and drops the upload still in flight. Hand tRPC a body it + // already has, so no response starts before the request is complete. + app.use("/trpc/*", async (c, next) => { + if (c.req.raw.body !== null && !c.req.raw.bodyUsed) { + c.req.raw = new Request(c.req.raw, { + body: await c.req.raw.arrayBuffer(), + }); + } + await next(); + }); + app.use( "/trpc/*", trpcServer({ diff --git a/packages/host-service/src/chat-v3/agentImages.ts b/packages/host-service/src/chat-v3/agentImages.ts index 92c6e3cef32..8702c5b8fcf 100644 --- a/packages/host-service/src/chat-v3/agentImages.ts +++ b/packages/host-service/src/chat-v3/agentImages.ts @@ -22,6 +22,7 @@ import { WORKSPACE_ATTACHMENTS_DIR, } from "@superset/shared/workspace-attachments"; import mimeTypes from "mime-types"; +import { ensureWorkspaceAttachmentsDir } from "../runtime/workspace-attachments-dir"; const NAME_HASH_CHARS = 12; @@ -38,18 +39,6 @@ function stemFor(name: string | undefined): string { return sanitized ?? "image"; } -function attachmentsDir(cwd: string): string { - const dir = join(cwd, WORKSPACE_ATTACHMENTS_DIR); - mkdirSync(dir, { recursive: true }); - try { - closeSync(openSync(join(dir, ".gitignore"), "wx")); - writeFileSync(join(dir, ".gitignore"), "*\n"); - } catch (error) { - if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; - } - return dir; -} - function isUsableFile(path: string, maxBytes: number): boolean { try { const stat = statSync(path); @@ -100,7 +89,7 @@ function storeImage( .digest("hex") .slice(0, NAME_HASH_CHARS); const fileName = `${stemFor(name)}-${hash}.${extensionFor(source.mimeType, name)}`; - const target = join(attachmentsDir(cwd), fileName); + const target = join(ensureWorkspaceAttachmentsDir(cwd), fileName); if (!existsSync(target)) writeFileSync(target, bytes); return { path: `${WORKSPACE_ATTACHMENTS_DIR}/${fileName}` }; } @@ -147,7 +136,7 @@ export function storeAgentImage( } const stored = storeImage(source, cwd, maxBytes); if (stored && referencePath) { - attachmentsDir(cwd); + ensureWorkspaceAttachmentsDir(cwd); mkdirSync(referenceDir, { recursive: true }); writeFileSync(referencePath, JSON.stringify(basename(stored.path))); } diff --git a/packages/host-service/src/runtime/workspace-attachments-dir.ts b/packages/host-service/src/runtime/workspace-attachments-dir.ts new file mode 100644 index 00000000000..809fd73810b --- /dev/null +++ b/packages/host-service/src/runtime/workspace-attachments-dir.ts @@ -0,0 +1,21 @@ +import { closeSync, mkdirSync, openSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; + +/** + * The worktree directory attachments are written into, created with a + * `.gitignore` that hides everything in it so an agent never sees + * attachments as untracked changes. The ignore file is create-only: a + * user's own edit wins. + */ +export function ensureWorkspaceAttachmentsDir(worktreePath: string): string { + const dir = join(worktreePath, WORKSPACE_ATTACHMENTS_DIR); + mkdirSync(dir, { recursive: true }); + try { + closeSync(openSync(join(dir, ".gitignore"), "wx")); + writeFileSync(join(dir, ".gitignore"), "*\n"); + } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; + } + return dir; +} diff --git a/packages/host-service/src/trpc/router/attachments/attachments.ts b/packages/host-service/src/trpc/router/attachments/attachments.ts index c13505ba6dc..9a675bbad08 100644 --- a/packages/host-service/src/trpc/router/attachments/attachments.ts +++ b/packages/host-service/src/trpc/router/attachments/attachments.ts @@ -1,5 +1,5 @@ import { randomUUID } from "node:crypto"; -import { closeSync, mkdirSync, openSync } from "node:fs"; +import { closeSync, openSync } from "node:fs"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; import { @@ -10,6 +10,7 @@ import { import { TRPCError } from "@trpc/server"; import mimeTypes from "mime-types"; import { z } from "zod"; +import { ensureWorkspaceAttachmentsDir } from "../../../runtime/workspace-attachments-dir"; import { protectedProcedure, router } from "../../index"; import { resolveWorktreePath } from "../git/utils/resolve-worktree"; import { MAX_INLINE_ATTACHMENT_BYTES } from "./constants"; @@ -172,8 +173,7 @@ export const attachmentsRouter = router({ ) .mutation(async ({ ctx, input }) => { const worktreePath = resolveWorktreePath(ctx, input.workspaceId); - const directory = join(worktreePath, WORKSPACE_ATTACHMENTS_DIR); - mkdirSync(directory, { recursive: true }); + const directory = ensureWorkspaceAttachmentsDir(worktreePath); const resolved = await ctx.api.attachment.resolve.mutate({ fileIds: input.fileIds, diff --git a/packages/host-service/test/integration/trpc-request-body.integration.test.ts b/packages/host-service/test/integration/trpc-request-body.integration.test.ts new file mode 100644 index 00000000000..2baa91a704e --- /dev/null +++ b/packages/host-service/test/integration/trpc-request-body.integration.test.ts @@ -0,0 +1,66 @@ +import { afterAll, beforeAll, describe, expect, it } from "bun:test"; +import { createTestHost, type TestHost } from "../helpers/createTestHost"; + +let host: TestHost; + +beforeAll(async () => { + host = await createTestHost(); +}); + +afterAll(async () => { + await host.dispose(); +}); + +/** A request body that stays open until the test closes it. */ +function openBody(): { + stream: ReadableStream; + finish: (text: string) => void; +} { + let controller!: ReadableStreamDefaultController; + const stream = new ReadableStream({ + start(c) { + controller = c; + }, + }); + return { + stream, + finish(text) { + controller.enqueue(new TextEncoder().encode(text)); + controller.close(); + }, + }; +} + +describe("tRPC request bodies", () => { + // A sandbox's ingress drops the exchange when the host answers before the + // upload is complete, so a streamed batch must not start its response + // until the whole body has arrived. + it("does not answer a streamed batch before the body has fully arrived", async () => { + const body = openBody(); + let settled = false; + const response = host + .fetch("http://host-service.test/trpc/health.check?batch=1", { + method: "POST", + headers: { + authorization: `Bearer ${host.psk}`, + "content-type": "application/json", + "trpc-accept": "application/jsonl", + }, + body: body.stream, + // @ts-expect-error -- required by the Fetch spec for a stream body + duplex: "half", + }) + .then((res) => { + settled = true; + return res; + }); + + await new Promise((resolve) => setTimeout(resolve, 20)); + expect(settled).toBe(false); + + body.finish(JSON.stringify({ "0": { json: null } })); + const res = await response; + expect(res.status).toBe(200); + expect(await res.text()).toContain('"status":"ok"'); + }); +}); diff --git a/packages/i18n/locales/cs/messages.po b/packages/i18n/locales/cs/messages.po index 4ddbdd2d19c..cf7dd591f90 100644 --- a/packages/i18n/locales/cs/messages.po +++ b/packages/i18n/locales/cs/messages.po @@ -19309,9 +19309,6 @@ msgstr "Pracovní prostor je otevřený, ale terminál se nepodařilo spustit." msgid "Workspace path is not available" msgstr "Cesta k pracovnímu prostoru není dostupná" -msgid "Workspace path is not available yet" -msgstr "Cesta pracovního prostoru zatím není dostupná" - msgid "Workspace restored" msgstr "Pracovní prostor obnoven" diff --git a/packages/i18n/locales/de/messages.po b/packages/i18n/locales/de/messages.po index 7695c74b87e..256b08d6eeb 100644 --- a/packages/i18n/locales/de/messages.po +++ b/packages/i18n/locales/de/messages.po @@ -19309,9 +19309,6 @@ msgstr "Arbeitsbereich geöffnet, aber das Terminal konnte nicht starten." msgid "Workspace path is not available" msgstr "Pfad des Arbeitsbereichs ist nicht verfügbar" -msgid "Workspace path is not available yet" -msgstr "Der Workspace-Pfad ist noch nicht verfügbar" - msgid "Workspace restored" msgstr "Arbeitsbereich wiederhergestellt" diff --git a/packages/i18n/locales/en/messages.po b/packages/i18n/locales/en/messages.po index 35ecd2e3d55..61f55e6196c 100644 --- a/packages/i18n/locales/en/messages.po +++ b/packages/i18n/locales/en/messages.po @@ -19309,9 +19309,6 @@ msgstr "Workspace opened, but terminal failed to start." msgid "Workspace path is not available" msgstr "Workspace path is not available" -msgid "Workspace path is not available yet" -msgstr "Workspace path is not available yet" - msgid "Workspace restored" msgstr "Workspace restored" diff --git a/packages/i18n/locales/es/messages.po b/packages/i18n/locales/es/messages.po index 9b5132ceee9..c265e1a6c01 100644 --- a/packages/i18n/locales/es/messages.po +++ b/packages/i18n/locales/es/messages.po @@ -19309,9 +19309,6 @@ msgstr "El espacio de trabajo se abrió, pero el terminal no se pudo iniciar." msgid "Workspace path is not available" msgstr "La ruta del espacio de trabajo no está disponible" -msgid "Workspace path is not available yet" -msgstr "La ruta del espacio de trabajo aún no está disponible" - msgid "Workspace restored" msgstr "Espacio de trabajo restaurado" diff --git a/packages/i18n/locales/fr/messages.po b/packages/i18n/locales/fr/messages.po index fd31536b876..2e8cbe90dd7 100644 --- a/packages/i18n/locales/fr/messages.po +++ b/packages/i18n/locales/fr/messages.po @@ -19309,9 +19309,6 @@ msgstr "Espace de travail ouvert, mais le terminal n'a pas démarré." msgid "Workspace path is not available" msgstr "Le chemin de l'espace de travail n'est pas disponible" -msgid "Workspace path is not available yet" -msgstr "Le chemin de l’espace de travail n’est pas encore disponible" - msgid "Workspace restored" msgstr "Espace de travail restauré" diff --git a/packages/i18n/locales/id/messages.po b/packages/i18n/locales/id/messages.po index f02c4164dd2..6c2ad54ce0c 100644 --- a/packages/i18n/locales/id/messages.po +++ b/packages/i18n/locales/id/messages.po @@ -19309,9 +19309,6 @@ msgstr "Workspace terbuka, tapi terminal gagal dijalankan." msgid "Workspace path is not available" msgstr "Path workspace tidak tersedia" -msgid "Workspace path is not available yet" -msgstr "Jalur ruang kerja belum tersedia" - msgid "Workspace restored" msgstr "Workspace dipulihkan" diff --git a/packages/i18n/locales/it/messages.po b/packages/i18n/locales/it/messages.po index b9c51b8db5b..d7ec2281900 100644 --- a/packages/i18n/locales/it/messages.po +++ b/packages/i18n/locales/it/messages.po @@ -19309,9 +19309,6 @@ msgstr "Workspace aperto, ma il terminale non è partito." msgid "Workspace path is not available" msgstr "Il percorso del workspace non è disponibile" -msgid "Workspace path is not available yet" -msgstr "Il percorso dello spazio di lavoro non è ancora disponibile" - msgid "Workspace restored" msgstr "Workspace ripristinato" diff --git a/packages/i18n/locales/ja/messages.po b/packages/i18n/locales/ja/messages.po index 138a21d12b0..4b75d8bdf41 100644 --- a/packages/i18n/locales/ja/messages.po +++ b/packages/i18n/locales/ja/messages.po @@ -19309,9 +19309,6 @@ msgstr "ワークスペースを開きましたが、ターミナルを起動で msgid "Workspace path is not available" msgstr "ワークスペースのパスを利用できません" -msgid "Workspace path is not available yet" -msgstr "ワークスペースのパスはまだ利用できません" - msgid "Workspace restored" msgstr "ワークスペースを復元しました" diff --git a/packages/i18n/locales/ko/messages.po b/packages/i18n/locales/ko/messages.po index 15a5163b3fd..0b617b35b7f 100644 --- a/packages/i18n/locales/ko/messages.po +++ b/packages/i18n/locales/ko/messages.po @@ -19309,9 +19309,6 @@ msgstr "워크스페이스를 열었지만 터미널을 시작하지 못했습 msgid "Workspace path is not available" msgstr "워크스페이스 경로를 사용할 수 없습니다" -msgid "Workspace path is not available yet" -msgstr "작업 공간 경로를 아직 사용할 수 없습니다" - msgid "Workspace restored" msgstr "워크스페이스를 복원했습니다" diff --git a/packages/i18n/locales/nl/messages.po b/packages/i18n/locales/nl/messages.po index 420795c8181..749b3223dd3 100644 --- a/packages/i18n/locales/nl/messages.po +++ b/packages/i18n/locales/nl/messages.po @@ -19309,9 +19309,6 @@ msgstr "Werkruimte geopend, maar de terminal kon niet starten." msgid "Workspace path is not available" msgstr "Pad van werkruimte is niet beschikbaar" -msgid "Workspace path is not available yet" -msgstr "Het werkruimtepad is nog niet beschikbaar" - msgid "Workspace restored" msgstr "Werkruimte hersteld" diff --git a/packages/i18n/locales/pl/messages.po b/packages/i18n/locales/pl/messages.po index 08748ac37d0..3b2bb45a210 100644 --- a/packages/i18n/locales/pl/messages.po +++ b/packages/i18n/locales/pl/messages.po @@ -19309,9 +19309,6 @@ msgstr "Obszar roboczy otwarty, ale terminal się nie uruchomił." msgid "Workspace path is not available" msgstr "Ścieżka obszaru roboczego jest niedostępna" -msgid "Workspace path is not available yet" -msgstr "Ścieżka obszaru roboczego nie jest jeszcze dostępna" - msgid "Workspace restored" msgstr "Przywrócono obszar roboczy" diff --git a/packages/i18n/locales/pt-BR/messages.po b/packages/i18n/locales/pt-BR/messages.po index ae297648bfd..106927954bb 100644 --- a/packages/i18n/locales/pt-BR/messages.po +++ b/packages/i18n/locales/pt-BR/messages.po @@ -19309,9 +19309,6 @@ msgstr "Área de trabalho aberta, mas o terminal não iniciou." msgid "Workspace path is not available" msgstr "O caminho da área de trabalho não está disponível" -msgid "Workspace path is not available yet" -msgstr "O caminho do espaço de trabalho ainda não está disponível" - msgid "Workspace restored" msgstr "Área de trabalho restaurada" diff --git a/packages/i18n/locales/ru/messages.po b/packages/i18n/locales/ru/messages.po index 1e1eecb2f70..01d23a42202 100644 --- a/packages/i18n/locales/ru/messages.po +++ b/packages/i18n/locales/ru/messages.po @@ -19309,9 +19309,6 @@ msgstr "Рабочая область открыта, но терминал не msgid "Workspace path is not available" msgstr "Путь к рабочей области недоступен" -msgid "Workspace path is not available yet" -msgstr "Путь рабочего пространства пока недоступен" - msgid "Workspace restored" msgstr "Рабочая область восстановлена" diff --git a/packages/i18n/locales/tr/messages.po b/packages/i18n/locales/tr/messages.po index 9e5c742166c..cd789372af0 100644 --- a/packages/i18n/locales/tr/messages.po +++ b/packages/i18n/locales/tr/messages.po @@ -19309,9 +19309,6 @@ msgstr "Çalışma alanı açıldı, ancak terminal başlatılamadı." msgid "Workspace path is not available" msgstr "Çalışma alanı yolu kullanılamıyor" -msgid "Workspace path is not available yet" -msgstr "Çalışma alanı yolu henüz kullanılamıyor" - msgid "Workspace restored" msgstr "Çalışma alanı geri yüklendi" diff --git a/packages/i18n/locales/vi/messages.po b/packages/i18n/locales/vi/messages.po index d9ba9b02f05..5d4949bd943 100644 --- a/packages/i18n/locales/vi/messages.po +++ b/packages/i18n/locales/vi/messages.po @@ -19309,9 +19309,6 @@ msgstr "Đã mở workspace, nhưng terminal không khởi động được." msgid "Workspace path is not available" msgstr "Không có đường dẫn workspace" -msgid "Workspace path is not available yet" -msgstr "Đường dẫn không gian làm việc chưa sẵn sàng" - msgid "Workspace restored" msgstr "Đã khôi phục workspace" diff --git a/packages/i18n/locales/zh-CN/messages.po b/packages/i18n/locales/zh-CN/messages.po index 7eb481e8f31..481f0fbbac6 100644 --- a/packages/i18n/locales/zh-CN/messages.po +++ b/packages/i18n/locales/zh-CN/messages.po @@ -19309,9 +19309,6 @@ msgstr "工作区已打开,但终端启动失败。" msgid "Workspace path is not available" msgstr "工作区路径不可用" -msgid "Workspace path is not available yet" -msgstr "工作区路径尚不可用" - msgid "Workspace restored" msgstr "工作区已恢复" diff --git a/packages/i18n/locales/zh-TW/messages.po b/packages/i18n/locales/zh-TW/messages.po index efd43d97b23..fb8fd075113 100644 --- a/packages/i18n/locales/zh-TW/messages.po +++ b/packages/i18n/locales/zh-TW/messages.po @@ -19309,9 +19309,6 @@ msgstr "工作區已開啟,但終端啟動失敗。" msgid "Workspace path is not available" msgstr "工作區路徑不可用" -msgid "Workspace path is not available yet" -msgstr "工作區路徑尚不可用" - msgid "Workspace restored" msgstr "工作區已還原" From 10a2b744e591a786200d0d3db3d1e12092786192 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Wed, 21 Oct 2026 04:47:40 +0000 Subject: [PATCH 02/16] fix(api): scope attachment.resolve to visible cloud workspaces and reap files with the box First API step of the attachment plan (SUPER-2874, seam 6). - attachment.resolve no longer signs a URL for any composer upload in the org. A kept (ready) file resolves only through a cloud workspace the caller can see. An upload still in flight (pending) stays org-wide: a host resolves with its owner's credential, and the sender can be a teammate on that host. A box still sees only its own workspace's files. - New attachment.resolveEach returns one entry per id, with { fileId, missing: true } for a file that is gone or not visible, so a transcript can render the rest. resolve keeps its output and errors for released hosts. - The archive's final reap stage (and the fallback when it cannot be queued) detaches and deletes the files anchored to the workspace. --- .../trpc/src/router/attachment/attachment.ts | 131 ++++++++-------- .../attachment/resolvable.integration.ts | 148 ++++++++++++++++++ .../trpc/src/router/attachment/resolvable.ts | 69 ++++++++ .../router/cloud-workspace/cloud-workspace.ts | 3 +- .../src/router/cloud-workspace/reap.test.ts | 22 +++ .../trpc/src/router/cloud-workspace/reap.ts | 17 ++ 6 files changed, 323 insertions(+), 67 deletions(-) create mode 100644 packages/trpc/src/router/attachment/resolvable.integration.ts create mode 100644 packages/trpc/src/router/attachment/resolvable.ts diff --git a/packages/trpc/src/router/attachment/attachment.ts b/packages/trpc/src/router/attachment/attachment.ts index a0b96c3162a..47fadc75c44 100644 --- a/packages/trpc/src/router/attachment/attachment.ts +++ b/packages/trpc/src/router/attachment/attachment.ts @@ -1,25 +1,17 @@ import { db } from "@superset/db/client"; -import { attachments, files } from "@superset/db/schema"; +import { files, type SelectFile } from "@superset/db/schema"; import { fileOriginalKey } from "@superset/shared/usercontent"; import type { TRPCRouterRecord } from "@trpc/server"; -import { and, eq, inArray } from "drizzle-orm"; import { headObject, presignedGetUrl, presignedPutUrl } from "../../lib/r2"; +import type { SandboxCaller } from "../../lib/sandbox/api-credential"; import { protectedProcedure, userError } from "../../trpc"; import { requireActiveOrgMembership } from "../utils/active-org"; +import { resolvableFiles, TRANSFER_ONLY } from "./resolvable"; import { createAttachmentUploadSchema, resolveAttachmentsSchema, } from "./schema"; -/** - * What marks a `files` row as one of these. Every other kind of file is - * identified by its content — page assets deduplicate on the digest — and no - * real file hashes to the empty string, so this both keeps an attachment out - * of any reuse lookup and keeps `resolve` off files it has no business - * handing a URL for. - */ -const TRANSFER_ONLY = ""; - /** * Composer attachments: bytes on their way from a client to the machine an * agent is running on. @@ -29,11 +21,10 @@ const TRANSFER_ONLY = ""; * bodies in a Worker isolate and is refused outright by Cloudflare's edge * above 100 MB. What travels over the relay is a list of file ids. * - * Rows here stay `pending` for their whole life, which is the point: the - * object is a transfer buffer, dead as soon as the host has written it into - * the worktree, and `pending` is exactly what the file sweep reclaims a day - * later. Nothing references these bytes afterwards, so there is no `ready` - * state to move to and no parent to attach them to. + * A row stays `pending` while it is only a transfer buffer, which the file + * sweep reclaims a day later. A file sent to a cloud workspace is kept + * instead: `anchorAttachments` marks it `ready` on that workspace, and it is + * reaped with the workspace's box. */ export const attachmentRouter = { /** @@ -89,14 +80,8 @@ export const attachmentRouter = { * Presigned GETs for a host about to materialize these into a worktree, * with the metadata it needs to name the files. * - * Scoped to the caller's own organization and to attachments, which is - * what keeps a host to the bytes it asked for: `files.id` is the only - * thing the caller presents, and knowing one is not authority to read it. - * Without the second scope this would sign a URL for any file in the org, - * a page's private assets included. - * - * A box calling for itself gets only the files attached to its own - * workspace: access to the box is access to its files, nothing wider. + * Scoped by `resolvableFiles`: `files.id` is the only thing the caller + * presents, and knowing one is not authority to read it. * * A mutation despite reading nothing but rows: the URLs it mints are * short-lived credentials, and a query's response is cacheable. @@ -104,33 +89,7 @@ export const attachmentRouter = { resolve: protectedProcedure .input(resolveAttachmentsSchema) .mutation(async ({ ctx, input }) => { - const organizationId = await requireActiveOrgMembership(ctx); - - const rows = await db - .select() - .from(files) - .where( - and( - inArray(files.id, input.fileIds), - eq(files.organizationId, organizationId), - eq(files.sha256, TRANSFER_ONLY), - ctx.sandboxCaller - ? inArray( - files.id, - db - .select({ id: attachments.fileId }) - .from(attachments) - .where( - and( - eq(attachments.parentKind, "cloud_workspace"), - eq(attachments.parentId, ctx.sandboxCaller.workspaceId), - ), - ), - ) - : undefined, - ), - ); - const byId = new Map(rows.map((row) => [row.id, row])); + const byId = await loadResolvable(ctx, input.fileIds); // In the order asked for: the caller names attachments positionally // and a reordered answer would rename them. @@ -144,29 +103,69 @@ export const attachmentRouter = { i18nKey: "serverError.attachment.notFound", }); } - - // A presigned PUT lands without the API seeing it, so this is - // where an upload is held to what it declared. A short read - // that never finished is the case worth catching — it would - // otherwise reach the agent as a truncated file. - const key = fileOriginalKey(file.id); - const head = await headObject(key); - if (!head || head.sizeBytes !== file.sizeBytes) { + const resolved = await signUploaded(file); + if (!resolved) { throw userError({ code: "BAD_REQUEST", message: "Attachment was not uploaded — send the bytes first", i18nKey: "serverError.attachment.notUploaded", }); } + return resolved; + }), + ); + }), - return { - fileId: file.id, - name: file.name, - contentType: file.contentType, - sizeBytes: file.sizeBytes, - url: await presignedGetUrl(key), - }; + /** + * `resolve` for a reader that shows what it can: a transcript with one + * file gone still renders the rest. Every id gets an answer, in the order + * asked for, and one the caller may not see is reported the same as one + * that does not exist. + */ + resolveEach: protectedProcedure + .input(resolveAttachmentsSchema) + .mutation(async ({ ctx, input }) => { + const byId = await loadResolvable(ctx, input.fileIds); + return await Promise.all( + input.fileIds.map(async (fileId) => { + const file = byId.get(fileId); + const resolved = file ? await signUploaded(file) : null; + return resolved ?? { fileId, missing: true as const }; }), ); }), } satisfies TRPCRouterRecord; + +async function loadResolvable( + ctx: Parameters[0] & { + sandboxCaller: SandboxCaller | null; + }, + fileIds: string[], +) { + const rows = await resolvableFiles({ + organizationId: await requireActiveOrgMembership(ctx), + userId: ctx.session.user.id, + boxWorkspaceId: ctx.sandboxCaller?.workspaceId ?? null, + fileIds, + }); + return new Map(rows.map((row) => [row.id, row])); +} + +/** + * A presigned PUT lands without the API seeing it, so this is where an + * upload is held to what it declared. A short read that never finished is + * the case worth catching — it would otherwise reach the agent as a + * truncated file. Null when the bytes are not there. + */ +async function signUploaded(file: SelectFile) { + const key = fileOriginalKey(file.id); + const head = await headObject(key); + if (!head || head.sizeBytes !== file.sizeBytes) return null; + return { + fileId: file.id, + name: file.name, + contentType: file.contentType, + sizeBytes: file.sizeBytes, + url: await presignedGetUrl(key), + }; +} diff --git a/packages/trpc/src/router/attachment/resolvable.integration.ts b/packages/trpc/src/router/attachment/resolvable.integration.ts new file mode 100644 index 00000000000..d1dd4af50d0 --- /dev/null +++ b/packages/trpc/src/router/attachment/resolvable.integration.ts @@ -0,0 +1,148 @@ +import { afterAll, beforeAll, describe, expect, test } from "bun:test"; +import { db, dbWs } from "@superset/db/client"; +import { + attachments, + cloudWorkspaces, + environments, + files, + organizations, + users, +} from "@superset/db/schema"; +import { eq, inArray } from "drizzle-orm"; +import { resolvableFiles, TRANSFER_ONLY } from "./resolvable"; + +const ORG = crypto.randomUUID(); +const OWNER = crypto.randomUUID(); +const TEAMMATE = crypto.randomUUID(); +const now = Date.now(); + +const insertFile = async ( + status: "pending" | "ready", + sha256 = TRANSFER_ONLY, +) => { + const id = crypto.randomUUID(); + await db.insert(files).values({ + id, + organizationId: ORG, + name: "screenshot.png", + contentType: "image/png", + sizeBytes: 4, + sha256, + status, + createdByUserId: OWNER, + }); + return id; +}; + +const insertWorkspace = async (visibility: "org" | "just_me") => { + const [environment] = await db + .select({ id: environments.id }) + .from(environments) + .where(eq(environments.organizationId, ORG)); + const id = crypto.randomUUID(); + await db.insert(cloudWorkspaces).values({ + id, + organizationId: ORG, + name: "box", + branch: "main", + baseBranch: "main", + providerSandboxId: `box-${id}`, + environmentId: environment?.id as string, + visibility, + createdByUserId: OWNER, + }); + return id; +}; + +const anchor = (fileId: string, workspaceId: string) => + db.insert(attachments).values({ + fileId, + parentKind: "cloud_workspace", + parentId: workspaceId, + }); + +const resolvable = async ( + userId: string, + fileIds: string[], + boxWorkspaceId: string | null = null, +) => + ( + await resolvableFiles({ + organizationId: ORG, + userId, + boxWorkspaceId, + fileIds, + }) + ) + .map((row) => row.id) + .sort(); + +beforeAll(async () => { + await db + .insert(organizations) + .values({ id: ORG, name: "Resolve Org", slug: `resolve-org-${now}` }); + await db.insert(users).values([ + { id: OWNER, name: "Owner", email: `owner-${now}@example.com` }, + { id: TEAMMATE, name: "Teammate", email: `teammate-${now}@example.com` }, + ]); + await db.insert(environments).values({ + organizationId: ORG, + name: "default", + sourceKind: "image", + sourceRef: "image", + bundleSha: "sha", + }); +}); + +afterAll(async () => { + await db.delete(organizations).where(eq(organizations.id, ORG)); + await db.delete(users).where(inArray(users.id, [OWNER, TEAMMATE])); + await dbWs.$client.end?.().catch(() => {}); +}); + +describe("resolvableFiles", () => { + test("an upload in flight resolves for anyone in the org", async () => { + const pending = await insertFile("pending"); + expect(await resolvable(TEAMMATE, [pending])).toEqual([pending]); + }); + + test("a kept file resolves only through a workspace the caller can see", async () => { + const shared = await insertWorkspace("org"); + const hidden = await insertWorkspace("just_me"); + const inShared = await insertFile("ready"); + const inHidden = await insertFile("ready"); + await anchor(inShared, shared); + await anchor(inHidden, hidden); + + expect(await resolvable(OWNER, [inShared, inHidden])).toEqual( + [inShared, inHidden].sort(), + ); + expect(await resolvable(TEAMMATE, [inShared, inHidden])).toEqual([ + inShared, + ]); + }); + + test("a kept file on no cloud workspace does not resolve", async () => { + const loose = await insertFile("ready"); + expect(await resolvable(OWNER, [loose])).toEqual([]); + }); + + test("a box resolves only its own files, never one in flight", async () => { + const box = await insertWorkspace("org"); + const other = await insertWorkspace("org"); + const own = await insertFile("ready"); + const theirs = await insertFile("ready"); + const pending = await insertFile("pending"); + await anchor(own, box); + await anchor(theirs, other); + + expect(await resolvable(OWNER, [own, theirs, pending], box)).toEqual([own]); + }); + + test("a page asset never resolves", async () => { + const box = await insertWorkspace("org"); + const asset = await insertFile("ready", "ab".repeat(32)); + await anchor(asset, box); + expect(await resolvable(OWNER, [asset])).toEqual([]); + }); +}); diff --git a/packages/trpc/src/router/attachment/resolvable.ts b/packages/trpc/src/router/attachment/resolvable.ts new file mode 100644 index 00000000000..5e229aa2031 --- /dev/null +++ b/packages/trpc/src/router/attachment/resolvable.ts @@ -0,0 +1,69 @@ +import { db } from "@superset/db/client"; +import { attachments, cloudWorkspaces, files } from "@superset/db/schema"; +import { and, eq, inArray, or, type SQL } from "drizzle-orm"; +import { visibleTo } from "../cloud-workspace/access"; + +/** + * What marks a `files` row as a composer attachment. Every other kind of file is + * identified by its content — page assets deduplicate on the digest — and no + * real file hashes to the empty string, so this both keeps an attachment out + * of any reuse lookup and keeps `resolve` off files it has no business + * handing a URL for. + */ +export const TRANSFER_ONLY = ""; + +/** + * The attachments among `fileIds` a caller may read. + * + * A box gets only the files anchored to its own workspace. Anyone else gets + * an upload still in flight, and a kept file only through a cloud workspace + * they can see. In flight stays org-wide because a host resolves with its + * owner's credential, and the sender may be a teammate who shares that host. + */ +export function resolvableFiles({ + organizationId, + userId, + boxWorkspaceId, + fileIds, +}: { + organizationId: string; + userId: string; + boxWorkspaceId: string | null; + fileIds: string[]; +}) { + return db + .select() + .from(files) + .where( + and( + inArray(files.id, fileIds), + eq(files.organizationId, organizationId), + eq(files.sha256, TRANSFER_ONLY), + boxWorkspaceId + ? inArray( + files.id, + anchoredTo(eq(cloudWorkspaces.id, boxWorkspaceId)), + ) + : or( + eq(files.status, "pending"), + inArray( + files.id, + anchoredTo( + and( + eq(cloudWorkspaces.organizationId, organizationId), + visibleTo(userId), + ), + ), + ), + ), + ), + ); +} + +function anchoredTo(workspaces: SQL | undefined) { + return db + .select({ id: attachments.fileId }) + .from(attachments) + .innerJoin(cloudWorkspaces, eq(cloudWorkspaces.id, attachments.parentId)) + .where(and(eq(attachments.parentKind, "cloud_workspace"), workspaces)); +} diff --git a/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts b/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts index 9d8a5e914a4..eb2ce7bf6ee 100644 --- a/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts +++ b/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts @@ -44,7 +44,7 @@ import { import { recordCloudWorkspaceActivity } from "./activity"; import { queueSettleWorkspace, settleWorkspaceNow } from "./meter"; import { nextSandboxNameFor } from "./provision"; -import { queueReap } from "./reap"; +import { queueReap, reapWorkspaceFiles } from "./reap"; import { cloudWorkspaceRecordRouter } from "./record"; import { cloudWorkspaceSharingRouter } from "./sharing"; import { queueProvision, startCloudWorkspace } from "./start"; @@ -605,6 +605,7 @@ export const cloudWorkspaceRouter = { ), ); await deleteSandbox(row.providerSandboxId); + await reapWorkspaceFiles(row.id); }); } await recordCloudWorkspaceActivity( diff --git a/packages/trpc/src/router/cloud-workspace/reap.test.ts b/packages/trpc/src/router/cloud-workspace/reap.test.ts index 4247bd6c04f..9a54f220332 100644 --- a/packages/trpc/src/router/cloud-workspace/reap.test.ts +++ b/packages/trpc/src/router/cloud-workspace/reap.test.ts @@ -1,11 +1,13 @@ import { beforeEach, describe, expect, test } from "bun:test"; import { db } from "@superset/db/client"; import { stub } from "../../../test/stub"; +import * as files from "../../lib/files"; import * as sandbox from "../../lib/sandbox"; import * as jobs from "./jobs"; import * as meter from "./meter"; type Row = { + id: string; status: string; deletedAt: Date | null; provider: string; @@ -19,6 +21,7 @@ let queued: Array<{ path: string; body: unknown; delaySeconds?: number }> = []; let failingStage: Stage | null = null; let meterCalls: string[] = []; let meterFailure: Error | null = null; +let reapedFiles: string[] = []; stub(db.query.cloudWorkspaces, { findFirst: () => Promise.resolve(row) }); stub(sandbox, { @@ -49,6 +52,15 @@ stub(jobs, { }, }); +stub(files, { + detachAll: ({ parentIds }: { parentIds: string[] }) => + Promise.resolve(parentIds.map((id) => `file-of-${id}`)), + reapOrphanFiles: (fileIds: string[]) => { + reapedFiles.push(...fileIds); + return Promise.resolve(); + }, +}); + stub(meter, { settleWorkspaceNow: () => { meterCalls.push("settle"); @@ -112,6 +124,7 @@ describe("queueReap", () => { describe("reapArchivedCloudWorkspace", () => { beforeEach(() => { row = { + id: input.cloudWorkspaceId, status: "deleted", deletedAt: archivedAt, provider: "vercel", @@ -121,6 +134,7 @@ describe("reapArchivedCloudWorkspace", () => { queued = []; meterCalls = []; meterFailure = null; + reapedFiles = []; }); test("the stop stage stops the box and queues its final settle", async () => { @@ -130,6 +144,7 @@ describe("reapArchivedCloudWorkspace", () => { expect(sandboxCalls).toEqual(["stop:ws-box"]); expect(meterCalls).toEqual(["queue"]); expect(queued).toEqual([]); + expect(reapedFiles).toEqual([]); }); test("the delete stage settles the box's usage before deleting it", async () => { @@ -140,6 +155,11 @@ describe("reapArchivedCloudWorkspace", () => { expect(sandboxCalls).toEqual(["delete:ws-box"]); }); + test("the delete stage removes the files sent to the workspace", async () => { + await reapArchivedCloudWorkspace({ ...input, stage: "delete" }); + expect(reapedFiles).toEqual([`file-of-${input.cloudWorkspaceId}`]); + }); + test("a delete still deletes when the final settle fails: at most one lock window goes unbilled", async () => { meterFailure = new Error("autumn down"); expect( @@ -155,6 +175,7 @@ describe("reapArchivedCloudWorkspace", () => { "skipped", ); expect(sandboxCalls).toEqual([]); + expect(reapedFiles).toEqual([]); }); test(`the ${stage} stage leaves a later archive's box alone`, async () => { @@ -175,6 +196,7 @@ describe("reapArchivedCloudWorkspace", () => { await reapArchivedCloudWorkspace({ ...input, stage: "delete" }), ).toBe("skipped"); row = { + id: input.cloudWorkspaceId, status: "deleted", deletedAt: archivedAt, provider: "e2b", diff --git a/packages/trpc/src/router/cloud-workspace/reap.ts b/packages/trpc/src/router/cloud-workspace/reap.ts index 5f298a645f2..864d1d5d120 100644 --- a/packages/trpc/src/router/cloud-workspace/reap.ts +++ b/packages/trpc/src/router/cloud-workspace/reap.ts @@ -1,6 +1,7 @@ import { db } from "@superset/db/client"; import { cloudWorkspaces } from "@superset/db/schema"; import { eq } from "drizzle-orm"; +import { detachAll, reapOrphanFiles } from "../../lib/files"; import { deleteSandbox, stopSandbox } from "../../lib/sandbox"; import { publishCloudWorkspaceJob } from "./jobs"; import { queueSettleWorkspace, settleWorkspaceNow } from "./meter"; @@ -36,6 +37,7 @@ export async function reapArchivedCloudWorkspace( console.error(`[cloud-workspace] ${row.id} final settle failed`, error), ); await deleteSandbox(row.providerSandboxId); + await reapWorkspaceFiles(row.id); return "reaped"; } await stopSandbox(row.providerSandboxId); @@ -43,6 +45,21 @@ export async function reapArchivedCloudWorkspace( return "stopped"; } +/** Drops the files sent to a workspace whose box is gone for good. */ +export async function reapWorkspaceFiles(cloudWorkspaceId: string) { + await reapOrphanFiles( + await detachAll({ + parentKind: "cloud_workspace", + parentIds: [cloudWorkspaceId], + }), + ).catch((error) => + console.error( + `[cloud-workspace] ${cloudWorkspaceId} file reap failed`, + error, + ), + ); +} + function queueReapStage( input: ReapArchivedCloudWorkspaceInput, delaySeconds: number, From 6efd15dfd3e16a30fcbe82d868047f7a2fd5bc70 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:14:52 +0000 Subject: [PATCH 03/16] fix(desktop): copy local chat attachments instead of applying remote upload limits The chat composer always shipped attachments as base64 through filesystem.writeFile, with the 50MB/200MB caps meant for remote workspaces. In a local workspace a disk-backed file is now copied into .superset/attachments by the Electron main process, so no size cap applies and the bytes never pass through renderer memory. Clipboard blobs and remote workspaces keep the upload path and its limits. Fixes SUPER-3014 --- .../routers/attachments/attachments.test.ts | 70 +++++++++++++++++++ .../trpc/routers/attachments/attachments.ts | 69 ++++++++++++++++++ .../src/lib/trpc/routers/attachments/index.ts | 1 + apps/desktop/src/lib/trpc/routers/index.ts | 2 + .../useUploadAttachments.ts | 17 ++++- .../uploadPastedFiles.test.ts | 39 +++++++++++ .../uploadPastedFiles/uploadPastedFiles.ts | 66 ++++++++++------- 7 files changed, 238 insertions(+), 26 deletions(-) create mode 100644 apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts create mode 100644 apps/desktop/src/lib/trpc/routers/attachments/attachments.ts create mode 100644 apps/desktop/src/lib/trpc/routers/attachments/index.ts diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts new file mode 100644 index 00000000000..2b4935c7c47 --- /dev/null +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts @@ -0,0 +1,70 @@ +import { afterEach, describe, expect, it } from "bun:test"; +import { + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; +import { copyLocalFileExclusive } from "./attachments"; + +const roots: string[] = []; + +function setup() { + const root = mkdtempSync(join(tmpdir(), "attachments-")); + roots.push(root); + const source = join(root, "build.dmg"); + writeFileSync(source, "new"); + const directory = join(root, "worktree", WORKSPACE_ATTACHMENTS_DIR); + mkdirSync(directory, { recursive: true }); + return { root, source, directory }; +} + +afterEach(() => { + for (const root of roots.splice(0)) + rmSync(root, { recursive: true, force: true }); +}); + +describe("copyLocalFileExclusive", () => { + it("copies into the attachments directory and never overwrites", async () => { + const { source, directory } = setup(); + const destinationPath = join(directory, "build.dmg"); + + expect( + await copyLocalFileExclusive({ sourcePath: source, destinationPath }), + ).toEqual({ ok: true }); + writeFileSync(source, "changed"); + expect( + await copyLocalFileExclusive({ sourcePath: source, destinationPath }), + ).toEqual({ + ok: false, + reason: "exists", + }); + expect(readFileSync(destinationPath, "utf8")).toBe("new"); + }); + + it("refuses a destination outside the attachments directory", async () => { + const { root, source } = setup(); + + await expect( + copyLocalFileExclusive({ + sourcePath: source, + destinationPath: join(root, "out.dmg"), + }), + ).rejects.toThrow(WORKSPACE_ATTACHMENTS_DIR); + }); + + it("refuses a directory as the source", async () => { + const { root, directory } = setup(); + + await expect( + copyLocalFileExclusive({ + sourcePath: root, + destinationPath: join(directory, "x"), + }), + ).rejects.toThrow("not a file"); + }); +}); diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts new file mode 100644 index 00000000000..af9a908ecce --- /dev/null +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts @@ -0,0 +1,69 @@ +import { constants } from "node:fs"; +import { copyFile, stat } from "node:fs/promises"; +import { basename, dirname, isAbsolute } from "node:path"; +import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; +import { TRPCError } from "@trpc/server"; +import { z } from "zod"; +import { publicProcedure, router } from "../.."; + +const [ATTACHMENTS_PARENT_NAME, ATTACHMENTS_DIR_NAME] = + WORKSPACE_ATTACHMENTS_DIR.split("/"); + +export function isWorkspaceAttachmentDestination(path: string): boolean { + const directory = dirname(path); + return ( + isAbsolute(path) && + basename(directory) === ATTACHMENTS_DIR_NAME && + basename(dirname(directory)) === ATTACHMENTS_PARENT_NAME + ); +} + +export async function copyLocalFileExclusive(input: { + sourcePath: string; + destinationPath: string; +}): Promise<{ ok: true } | { ok: false; reason: string }> { + if (!isAbsolute(input.sourcePath)) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Source path is not absolute", + }); + } + if (!isWorkspaceAttachmentDestination(input.destinationPath)) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: `Destination must be inside ${WORKSPACE_ATTACHMENTS_DIR}`, + }); + } + if (!(await stat(input.sourcePath)).isFile()) { + throw new TRPCError({ + code: "BAD_REQUEST", + message: "Source is not a file", + }); + } + try { + await copyFile( + input.sourcePath, + input.destinationPath, + constants.COPYFILE_EXCL, + ); + return { ok: true }; + } catch (error) { + if ((error as NodeJS.ErrnoException).code === "EEXIST") { + return { ok: false, reason: "exists" }; + } + throw error; + } +} + +export const createAttachmentsRouter = () => { + return router({ + copyLocalFile: publicProcedure + .input( + z.object({ + sourcePath: z.string().min(1), + destinationPath: z.string().min(1), + }), + ) + .mutation(({ input }) => copyLocalFileExclusive(input)), + }); +}; diff --git a/apps/desktop/src/lib/trpc/routers/attachments/index.ts b/apps/desktop/src/lib/trpc/routers/attachments/index.ts new file mode 100644 index 00000000000..52dfd0d5254 --- /dev/null +++ b/apps/desktop/src/lib/trpc/routers/attachments/index.ts @@ -0,0 +1 @@ +export { createAttachmentsRouter } from "./attachments"; diff --git a/apps/desktop/src/lib/trpc/routers/index.ts b/apps/desktop/src/lib/trpc/routers/index.ts index ab502c806f3..4ac730fb987 100644 --- a/apps/desktop/src/lib/trpc/routers/index.ts +++ b/apps/desktop/src/lib/trpc/routers/index.ts @@ -1,6 +1,7 @@ import type { BrowserWindow } from "electron"; import { router } from ".."; import { createAnalyticsRouter } from "./analytics"; +import { createAttachmentsRouter } from "./attachments"; import { createAuthRouter } from "./auth"; import { createAutoUpdateRouter } from "./auto-update"; import { createBrowserRouter } from "./browser/browser"; @@ -38,6 +39,7 @@ import { createWorkspacesRouter } from "./workspaces"; export const createAppRouter = (getWindow: () => BrowserWindow | null) => { return router({ analytics: createAnalyticsRouter(), + attachments: createAttachmentsRouter(), browser: createBrowserRouter(), browserHistory: createBrowserHistoryRouter(), downloads: createDownloadsRouter(), diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index 60d8a3b05a3..906beae3f88 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -3,6 +3,9 @@ import { errorMessage } from "@superset/i18n/errors"; import { toast } from "@superset/ui/sonner"; import { workspaceTrpc } from "@superset/workspace-client"; import { useCallback } from "react"; +import { electronTrpcClient } from "renderer/lib/trpc-client"; +import { useHostWorkspaces } from "renderer/routes/_authenticated/providers/HostWorkspacesProvider"; +import { useLocalHostService } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider"; import { PasteUploadLimitError, uploadPastedFiles, @@ -18,6 +21,11 @@ export function useUploadAttachments(workspaceId: string) { workspaceTrpc.filesystem.createDirectory.useMutation(); const writeFile = workspaceTrpc.filesystem.writeFile.useMutation(); const worktreePath = workspace?.worktreePath; + const { machineId } = useLocalHostService(); + const { workspaces } = useHostWorkspaces(); + const isLocal = + Boolean(machineId) && + workspaces.find((w) => w.id === workspaceId)?.hostId === machineId; return useCallback( async (files: File[]): Promise => { @@ -28,6 +36,13 @@ export function useUploadAttachments(workspaceId: string) { deps: { createDirectory: (input) => createDirectory.mutateAsync(input), writeFile: (input) => writeFile.mutateAsync(input), + local: isLocal + ? { + pathOf: (file) => window.webUtils.getPathForFile(file), + copyFile: (input) => + electronTrpcClient.attachments.copyLocalFile.mutate(input), + } + : undefined, }, workspaceId, worktreePath, @@ -46,6 +61,6 @@ export function useUploadAttachments(workspaceId: string) { return null; } }, - [worktreePath, workspaceId, createDirectory, writeFile, t], + [worktreePath, workspaceId, isLocal, createDirectory, writeFile, t], ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts index afd97401f77..dfebdbb4271 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.test.ts @@ -234,4 +234,43 @@ describe("uploadPastedFiles", () => { `${WORKSPACE_ATTACHMENTS_DIR}/attachment_3`, ]); }); + + it("copies disk-backed files into a local workspace without the upload limits", async () => { + const { deps, writes } = makeDeps({ existing: [`${DIR_ABS}/build.dmg`] }); + fileReads = 0; + const dmg = new File([new Uint8Array(1)], "build.dmg"); + Object.defineProperty(dmg, "size", { value: 451 * 1024 * 1024 }); + const screenshot = new File(["x"], "image.png", { type: "image/png" }); + const copies: Array<{ sourcePath: string; destinationPath: string }> = []; + + const paths = await uploadPastedFiles({ + deps: { + ...deps, + local: { + pathOf: (file) => + file === dmg ? "/Users/me/Downloads/build.dmg" : "", + async copyFile(input) { + copies.push(input); + return input.destinationPath === `${DIR_ABS}/build.dmg` + ? { ok: false, reason: "exists" } + : { ok: true }; + }, + }, + }, + workspaceId: "ws-1", + worktreePath: WORKTREE, + files: [dmg, screenshot], + }); + + expect(paths).toEqual([ + `${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`, + `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, + ]); + expect(copies.at(-1)).toEqual({ + sourcePath: "/Users/me/Downloads/build.dmg", + destinationPath: `${DIR_ABS}/build_1.dmg`, + }); + expect(fileReads).toBe(1); + expect(writes.at(-1)?.absolutePath).toBe(`${DIR_ABS}/image.png`); + }); }); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts index 6d6e061c2cd..31d8ee1a662 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/uploadPastedFiles/uploadPastedFiles.ts @@ -46,6 +46,17 @@ export interface UploadPastedFilesDeps { content: string | { kind: "base64"; data: string }; options: { create: boolean; overwrite: boolean }; }): Promise; + /** + * Set only when the workspace is on this machine. Files with a disk path + * are then copied in place, so the upload size limits do not apply. + */ + local?: { + pathOf(file: File): string; + copyFile(input: { + sourcePath: string; + destinationPath: string; + }): Promise; + }; } const MIME_EXTENSIONS: Record = { @@ -79,20 +90,12 @@ function baseFileName(file: File, index: number): string { * references. */ async function writeUnique( - deps: UploadPastedFilesDeps, - workspaceId: string, - dirAbs: string, base: string, - data: string, + write: (fileName: string) => Promise, ): Promise { for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { const fileName = attachmentNameWithSuffix(base, attempt); - const outcome = await deps.writeFile({ - workspaceId, - absolutePath: `${dirAbs}/${fileName}`, - content: { kind: "base64", data }, - options: { create: true, overwrite: false }, - }); + const outcome = await write(fileName); if (outcome.ok !== false) return fileName; if (outcome.reason !== "exists") { throw new Error(outcome.reason ?? "write failed"); @@ -102,10 +105,9 @@ async function writeUnique( } /** - * Ship clipboard files to a workspace whose PTY runs on another machine and - * return the worktree-relative paths written, ready to paste into the - * terminal. Throws on the first failed write — the caller owns the error - * surface. + * Write files into the workspace's attachments directory and return the + * worktree-relative paths written. Throws on the first failed write — the + * caller owns the error surface. */ export async function uploadPastedFiles(options: { deps: UploadPastedFilesDeps; @@ -116,18 +118,22 @@ export async function uploadPastedFiles(options: { const { deps, workspaceId, worktreePath, files } = options; const dirAbs = `${worktreePath}/${WORKSPACE_ATTACHMENTS_DIR}`; + const localPaths = files.map((file) => deps.local?.pathOf(file) ?? ""); + const limitScope = deps.local ? "uploaded" : "sent to a remote workspace"; + let totalBytes = 0; - for (const file of files) { + for (const [index, file] of files.entries()) { + if (localPaths[index]) continue; if (file.size > MAX_SINGLE_FILE_BYTES) { throw new PasteUploadLimitError( - `"${file.name || "pasted file"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_SINGLE_FILE_BYTES)} can't be sent to a remote workspace`, + `"${file.name || "pasted file"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_SINGLE_FILE_BYTES)} can't be ${limitScope}`, ); } totalBytes += file.size; } if (totalBytes > MAX_TOTAL_BYTES) { throw new PasteUploadLimitError( - `Pasted files total ${formatMb(totalBytes)} — more than the ${formatMb(MAX_TOTAL_BYTES)} limit for a remote workspace`, + `Pasted files total ${formatMb(totalBytes)} — more than the ${formatMb(MAX_TOTAL_BYTES)} limit for ${deps.local ? "an upload" : "a remote workspace"}`, ); } @@ -151,14 +157,24 @@ export async function uploadPastedFiles(options: { const paths: string[] = []; for (const [index, file] of files.entries()) { - const data = await fileToBase64(file); - const fileName = await writeUnique( - deps, - workspaceId, - dirAbs, - baseFileName(file, index), - data, - ); + const sourcePath = localPaths[index]; + const data = sourcePath ? null : await fileToBase64(file); + const write = (fileName: string): Promise => { + const absolutePath = `${dirAbs}/${fileName}`; + if (sourcePath && deps.local) { + return deps.local.copyFile({ + sourcePath, + destinationPath: absolutePath, + }); + } + return deps.writeFile({ + workspaceId, + absolutePath, + content: { kind: "base64", data: data ?? "" }, + options: { create: true, overwrite: false }, + }); + }; + const fileName = await writeUnique(baseFileName(file, index), write); paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); } return paths; From d2ba203a357830be747ee7347aff6b82e660ce89 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Thu, 29 Oct 2026 19:26:55 +0000 Subject: [PATCH 04/16] style: format attachments test --- .../src/lib/trpc/routers/attachments/attachments.test.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts index 49b6feef7ab..5539cb120d3 100644 --- a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts @@ -1,10 +1,5 @@ import { afterEach, describe, expect, it } from "bun:test"; -import { - mkdtempSync, - readFileSync, - rmSync, - writeFileSync, -} from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; From 32cfbb774553b11a1b225b3159bdf15921d08fcb Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Thu, 29 Oct 2026 20:49:05 +0000 Subject: [PATCH 05/16] fix(desktop): keep every local-workspace attachment on the machine In a workspace on this machine, the main process now writes all attachments into the worktree: disk-backed files are copied, clipboard blobs are written from their bytes. Nothing from a local workspace goes to storage any more; remote and cloud workspaces keep #8704's storage route. A host target that is still resolving refuses the send instead of falling through to storage. Review fixes: the route validates every item before writing, refuses UNC paths and caps base64 bodies; a path-count mismatch from the host or the copy now throws instead of handing the agent an empty path; ensureWorkspaceAttachmentsDir moves to @superset/shared so the desktop and host-service share one .gitignore rule; stale docstrings updated. --- .../routers/attachments/attachments.test.ts | 79 ++++++++--- .../trpc/routers/attachments/attachments.ts | 125 +++++++++++------- .../useSendAttachments/useSendAttachments.ts | 72 ++++++---- .../sendAttachmentsToWorkspace.test.ts | 74 +++++++++-- .../sendAttachmentsToWorkspace.ts | 100 +++++++------- .../host-service/src/chat-v3/agentImages.ts | 2 +- .../trpc/router/attachments/attachments.ts | 2 +- packages/shared/package.json | 4 + .../src}/workspace-attachments-dir.ts | 2 +- 9 files changed, 303 insertions(+), 157 deletions(-) rename packages/{host-service/src/runtime => shared/src}/workspace-attachments-dir.ts (89%) diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts index 5539cb120d3..85ce777ba30 100644 --- a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts @@ -1,9 +1,16 @@ import { afterEach, describe, expect, it } from "bun:test"; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; -import { copyLocalFilesIntoWorktree } from "./attachments"; +import { writeAttachmentsIntoWorktree } from "./attachments"; const roots: string[] = []; @@ -12,7 +19,14 @@ function setup() { roots.push(root); const source = join(root, "build.dmg"); writeFileSync(source, "first"); - return { root, source, worktreePath: join(root, "worktree") }; + const worktreePath = join(root, "worktree"); + mkdirSync(worktreePath); + return { + root, + source, + worktreePath, + directory: join(worktreePath, WORKSPACE_ATTACHMENTS_DIR), + }; } afterEach(() => { @@ -21,29 +35,54 @@ afterEach(() => { } }); -describe("copyLocalFilesIntoWorktree", () => { - it("copies into a self-ignoring attachments dir and never overwrites", async () => { - const { source, worktreePath } = setup(); - const directory = join(worktreePath, WORKSPACE_ATTACHMENTS_DIR); +describe("writeAttachmentsIntoWorktree", () => { + it("copies paths and writes bytes in order, into a self-ignoring dir", async () => { + const { source, worktreePath, directory } = setup(); - expect( - await copyLocalFilesIntoWorktree({ worktreePath, sourcePaths: [source] }), - ).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/build.dmg`]); - writeFileSync(source, "second"); - expect( - await copyLocalFilesIntoWorktree({ worktreePath, sourcePaths: [source] }), - ).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`]); + const paths = await writeAttachmentsIntoWorktree({ + worktreePath, + items: [ + { kind: "path", sourcePath: source }, + { + kind: "base64", + name: "image.png", + data: Buffer.from("png").toString("base64"), + }, + ], + }); + expect(paths).toEqual([ + `${WORKSPACE_ATTACHMENTS_DIR}/build.dmg`, + `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, + ]); expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); - expect(readFileSync(join(directory, "build_1.dmg"), "utf8")).toBe("second"); + expect(readFileSync(join(directory, "image.png"), "utf8")).toBe("png"); expect(readFileSync(join(directory, ".gitignore"), "utf8")).toBe("*\n"); }); - it("refuses a directory as the source before writing anything", async () => { - const { root, worktreePath } = setup(); + it("never overwrites an earlier attachment", async () => { + const { source, worktreePath, directory } = setup(); + const items = [{ kind: "path" as const, sourcePath: source }]; + + await writeAttachmentsIntoWorktree({ worktreePath, items }); + writeFileSync(source, "second"); + expect(await writeAttachmentsIntoWorktree({ worktreePath, items })).toEqual( + [`${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`], + ); + expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); + }); + + it("refuses a directory or a UNC path as a source before writing anything", async () => { + const { root, worktreePath, directory } = setup(); - await expect( - copyLocalFilesIntoWorktree({ worktreePath, sourcePaths: [root] }), - ).rejects.toThrow("Not a file"); + for (const sourcePath of [root, "//server/share/x.png"]) { + await expect( + writeAttachmentsIntoWorktree({ + worktreePath, + items: [{ kind: "path", sourcePath }], + }), + ).rejects.toThrow(); + } + expect(existsSync(directory)).toBe(false); }); }); diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts index 3cbbc7c60ab..5a22a293756 100644 --- a/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts @@ -1,97 +1,124 @@ import { constants } from "node:fs"; -import { copyFile, mkdir, stat, writeFile } from "node:fs/promises"; +import { copyFile, stat, writeFile } from "node:fs/promises"; import { basename, isAbsolute, join } from "node:path"; +import { + MAX_ATTACHMENT_BYTES, + MAX_ATTACHMENTS, +} from "@superset/shared/attachment-limits"; import { attachmentFallbackName, attachmentNameWithSuffix, sanitizeAttachmentFileName, WORKSPACE_ATTACHMENTS_DIR, } from "@superset/shared/workspace-attachments"; +import { ensureWorkspaceAttachmentsDir } from "@superset/shared/workspace-attachments-dir"; import { TRPCError } from "@trpc/server"; import { z } from "zod"; import { publicProcedure, router } from "../.."; const MAX_NAME_ATTEMPTS = 50; -function isErrno(error: unknown, code: string): boolean { - return (error as NodeJS.ErrnoException | null)?.code === code; +const itemSchema = z.discriminatedUnion("kind", [ + z.object({ kind: z.literal("path"), sourcePath: z.string().min(1) }), + z.object({ + kind: z.literal("base64"), + name: z.string(), + data: z.string().max(Math.ceil(MAX_ATTACHMENT_BYTES / 3) * 4), + }), +]); + +export type LocalAttachmentItem = z.infer; + +function badRequest(message: string): TRPCError { + return new TRPCError({ code: "BAD_REQUEST", message }); } -async function ensureAttachmentsDir(worktreePath: string): Promise { - const directory = join(worktreePath, WORKSPACE_ATTACHMENTS_DIR); - await mkdir(directory, { recursive: true }); - try { - await writeFile(join(directory, ".gitignore"), "*\n", { flag: "wx" }); - } catch (error) { - if (!isErrno(error, "EEXIST")) throw error; +// Windows opens an SMB connection for a UNC path, which can leak the user's NTLM hash. +function assertLocalAbsolute(path: string): void { + if (!isAbsolute(path) || /^[\\/]{2}/.test(path)) { + throw badRequest(`Not a local absolute path: ${path}`); } - return directory; } -export async function copyLocalFilesIntoWorktree(input: { - worktreePath: string; - sourcePaths: string[]; -}): Promise { - for (const path of [input.worktreePath, ...input.sourcePaths]) { - if (!isAbsolute(path)) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: `Path is not absolute: ${path}`, - }); - } +async function assertValid( + worktreePath: string, + items: LocalAttachmentItem[], +): Promise { + assertLocalAbsolute(worktreePath); + if (!(await stat(worktreePath)).isDirectory()) { + throw badRequest(`Not a directory: ${worktreePath}`); } - for (const sourcePath of input.sourcePaths) { - if (!(await stat(sourcePath)).isFile()) { - throw new TRPCError({ - code: "BAD_REQUEST", - message: `Not a file: ${sourcePath}`, - }); + for (const item of items) { + if (item.kind !== "path") continue; + assertLocalAbsolute(item.sourcePath); + if (!(await stat(item.sourcePath)).isFile()) { + throw badRequest(`Not a file: ${item.sourcePath}`); } } +} - const directory = await ensureAttachmentsDir(input.worktreePath); - const paths: string[] = []; - for (const [index, sourcePath] of input.sourcePaths.entries()) { - const base = - sanitizeAttachmentFileName(basename(sourcePath)) ?? - attachmentFallbackName(index); - paths.push( - `${WORKSPACE_ATTACHMENTS_DIR}/${await copyUnique(sourcePath, directory, base)}`, - ); +function baseNameOf(item: LocalAttachmentItem, index: number): string { + const raw = item.kind === "path" ? basename(item.sourcePath) : item.name; + return sanitizeAttachmentFileName(raw) ?? attachmentFallbackName(index); +} + +async function writeExclusive( + item: LocalAttachmentItem, + destination: string, +): Promise { + if (item.kind === "path") { + await copyFile(item.sourcePath, destination, constants.COPYFILE_EXCL); + return; } - return paths; + await writeFile(destination, Buffer.from(item.data, "base64"), { + flag: "wx", + }); } -async function copyUnique( - sourcePath: string, +async function writeUnique( + item: LocalAttachmentItem, directory: string, base: string, ): Promise { for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { const fileName = attachmentNameWithSuffix(base, attempt); try { - await copyFile( - sourcePath, - join(directory, fileName), - constants.COPYFILE_EXCL, - ); + await writeExclusive(item, join(directory, fileName)); return fileName; } catch (error) { - if (!isErrno(error, "EEXIST")) throw error; + if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; } } throw new Error(`Could not find a free name for ${base}`); } +export async function writeAttachmentsIntoWorktree(input: { + worktreePath: string; + items: LocalAttachmentItem[]; +}): Promise { + await assertValid(input.worktreePath, input.items); + const directory = ensureWorkspaceAttachmentsDir(input.worktreePath); + const paths: string[] = []; + for (const [index, item] of input.items.entries()) { + const fileName = await writeUnique( + item, + directory, + baseNameOf(item, index), + ); + paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); + } + return paths; +} + export const createAttachmentsRouter = () => { return router({ - copyLocalFilesIntoWorktree: publicProcedure + writeIntoWorktree: publicProcedure .input( z.object({ worktreePath: z.string().min(1), - sourcePaths: z.array(z.string().min(1)).min(1), + items: z.array(itemSchema).min(1).max(MAX_ATTACHMENTS), }), ) - .mutation(({ input }) => copyLocalFilesIntoWorktree(input)), + .mutation(({ input }) => writeAttachmentsIntoWorktree(input)), }); }; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts index 5ca2f77b205..0ebe35d439f 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts @@ -1,9 +1,16 @@ import { workspaceTrpc } from "@superset/workspace-client"; import { useCallback, useRef } from "react"; -import { useWorkspaceHostTarget } from "renderer/hooks/host-service/useWorkspaceHostUrl"; +import { + useWorkspaceHostTarget, + type WorkspaceHostTarget, +} from "renderer/hooks/host-service/useWorkspaceHostUrl"; import { apiTrpcClient } from "renderer/lib/api-trpc-client"; +import { fileToBase64 } from "renderer/lib/file-to-base64"; import { electronTrpcClient } from "renderer/lib/trpc-client"; -import { sendAttachmentsToWorkspace } from "../../utils/sendAttachmentsToWorkspace"; +import { + type SendAttachmentsDeps, + sendAttachmentsToWorkspace, +} from "../../utils/sendAttachmentsToWorkspace"; async function putObject( target: { url: string; headers: Record }, @@ -16,15 +23,28 @@ async function putObject( }); } -function localDeps(worktreePath: string | undefined) { - if (!worktreePath) return undefined; +type Route = "same-machine" | "remote" | "cloud" | "unknown"; + +function routeOf(target: WorkspaceHostTarget): Route { + if (target.status === "local-starting") return "same-machine"; + if (target.status !== "ready") return "unknown"; + if (target.kind === "local") return "same-machine"; + return target.kind === "sandbox" ? "cloud" : "remote"; +} + +function sameMachineDeps( + worktreePath: string | undefined, +): SendAttachmentsDeps["sameMachine"] { return { - pathOf: (file: File) => window.webUtils.getPathForFile(file), - copy: (sourcePaths: string[]) => - electronTrpcClient.attachments.copyLocalFilesIntoWorktree.mutate({ + pathOf: (file) => window.webUtils.getPathForFile(file), + readBase64: fileToBase64, + write: (items) => { + if (!worktreePath) throw new Error("Workspace is not ready"); + return electronTrpcClient.attachments.writeIntoWorktree.mutate({ worktreePath, - sourcePaths, - }), + items, + }); + }, }; } @@ -36,21 +56,15 @@ function localDeps(worktreePath: string | undefined) { export function useSendAttachments( workspaceId: string, ): (files: File[]) => Promise { - const target = useWorkspaceHostTarget(workspaceId); - const isCloudWorkspace = - target.status === "ready" && target.kind === "sandbox"; - const isCloudRef = useRef(isCloudWorkspace); - isCloudRef.current = isCloudWorkspace; + const route = routeOf(useWorkspaceHostTarget(workspaceId)); + const routeRef = useRef(route); + routeRef.current = route; const { data: workspace } = workspaceTrpc.workspace.get.useQuery({ id: workspaceId, }); - const localWorktreePath = - target.status === "ready" && target.kind === "local" - ? workspace?.worktreePath - : undefined; - const localWorktreeRef = useRef(localWorktreePath); - localWorktreeRef.current = localWorktreePath; + const worktreePathRef = useRef(workspace?.worktreePath); + worktreePathRef.current = workspace?.worktreePath; const materialize = workspaceTrpc.attachments.materializeIntoWorkspace.useMutation(); @@ -58,8 +72,12 @@ export function useSendAttachments( materializeRef.current = materialize.mutateAsync; return useCallback( - (files: File[]) => - sendAttachmentsToWorkspace({ + async (files: File[]) => { + // An unresolved target must not fall through to storage: it may be this machine. + if (routeRef.current === "unknown") { + throw new Error("Workspace is not ready"); + } + return sendAttachmentsToWorkspace({ deps: { createUpload: (input) => apiTrpcClient.attachment.createUpload.mutate(input), @@ -67,12 +85,16 @@ export function useSendAttachments( attachFiles: (input) => apiTrpcClient.cloudWorkspace.attachFiles.mutate(input), materialize: (input) => materializeRef.current(input), - local: localDeps(localWorktreeRef.current), + sameMachine: + routeRef.current === "same-machine" + ? sameMachineDeps(worktreePathRef.current) + : undefined, }, workspaceId, - isCloudWorkspace: isCloudRef.current, + isCloudWorkspace: routeRef.current === "cloud", files, - }), + }); + }, [workspaceId], ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts index 48e00e882df..7b877ebfb83 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts @@ -5,6 +5,7 @@ import { } from "@superset/shared/attachment-limits"; import { AttachmentLimitError, + type LocalAttachmentItem, type SendAttachmentsDeps, sendAttachmentsToWorkspace, } from "./sendAttachmentsToWorkspace"; @@ -178,33 +179,82 @@ describe("sendAttachmentsToWorkspace", () => { expect(recorded.uploads).toEqual([]); expect(recorded.attached).toEqual([]); }); - it("copies disk-backed files in a local workspace instead of uploading them, with no size limit", async () => { + it("keeps a same-machine send off storage: paths are copied, blobs written, order kept", async () => { const { deps, recorded } = makeDeps(); const dmg = new File([new Uint8Array(1)], "build.dmg"); Object.defineProperty(dmg, "size", { value: MAX_ATTACHMENT_BYTES * 2 }); - const copied: string[][] = []; + const notes = new File(["n"], "notes.txt"); + const diskPaths = new Map([ + [dmg, "/Users/me/build.dmg"], + [notes, "/Users/me/notes.txt"], + ]); + const written: LocalAttachmentItem[][] = []; const paths = await sendAttachmentsToWorkspace({ deps: { ...deps, - local: { - pathOf: (file) => (file === dmg ? "/Users/me/build.dmg" : ""), - async copy(sourcePaths) { - copied.push(sourcePaths); - return [".superset/attachments/build.dmg"]; + sameMachine: { + pathOf: (file) => diskPaths.get(file) ?? "", + readBase64: async () => "cG5n", + async write(items) { + written.push(items); + return items.map((_, i) => `.superset/attachments/${i}`); }, }, }, workspaceId: "ws-1", isCloudWorkspace: false, - files: [png("shot.png"), dmg], + files: [dmg, png("shot.png"), notes], }); - expect(copied).toEqual([["/Users/me/build.dmg"]]); - expect(recorded.uploads.map((u) => u.name)).toEqual(["shot.png"]); + expect(written).toEqual([ + [ + { kind: "path", sourcePath: "/Users/me/build.dmg" }, + { kind: "base64", name: "shot.png", data: "cG5n" }, + { kind: "path", sourcePath: "/Users/me/notes.txt" }, + ], + ]); expect(paths).toEqual([ - ".superset/attachments/file-1.png", - ".superset/attachments/build.dmg", + ".superset/attachments/0", + ".superset/attachments/1", + ".superset/attachments/2", ]); + expect(recorded.uploads).toEqual([]); + expect(recorded.materialized).toEqual([]); + }); + + it("caps same-machine blobs, since their bytes cross IPC", async () => { + const { deps } = makeDeps(); + const big = png("huge.png"); + Object.defineProperty(big, "size", { value: MAX_ATTACHMENT_BYTES + 1 }); + + await expect( + sendAttachmentsToWorkspace({ + deps: { + ...deps, + sameMachine: { + pathOf: () => "", + readBase64: async () => "", + write: async () => [], + }, + }, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [big], + }), + ).rejects.toThrow(AttachmentLimitError); + }); + + it("throws when the host returns a different number of paths than files", async () => { + const { deps } = makeDeps(); + + await expect( + sendAttachmentsToWorkspace({ + deps: { ...deps, materialize: async () => ({ paths: [] }) }, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [png("a.png")], + }), + ).rejects.toThrow("Expected 1 attachment paths, got 0"); }); }); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts index 1be5b83f4c3..7e2f9e50f97 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts @@ -16,6 +16,10 @@ export interface AttachmentUploadTarget { headers: Record; } +export type LocalAttachmentItem = + | { kind: "path"; sourcePath: string } + | { kind: "base64"; name: string; data: string }; + export interface SendAttachmentsDeps { createUpload(input: { name: string; @@ -29,14 +33,10 @@ export interface SendAttachmentsDeps { workspaceId: string; fileIds: string[]; }): Promise<{ paths: string[] }>; - /** - * Set only when the workspace is on this machine. A file with a disk path - * is then copied into the worktree: it never goes to storage and has no - * size limit. - */ - local?: { + sameMachine?: { pathOf(file: File): string; - copy(sourcePaths: string[]): Promise; + readBase64(file: File): Promise; + write(items: LocalAttachmentItem[]): Promise; }; } @@ -64,13 +64,13 @@ function nameOf(file: File, index: number): string { return attachmentFallbackName(index, extension ? `.${extension}` : ""); } -function assertWithinLimits(files: File[], uploads: File[]): void { +function assertWithinLimits(files: File[], sized: File[] = files): void { if (files.length > MAX_ATTACHMENTS) { throw new AttachmentLimitError( `${files.length} files attached — a message carries at most ${MAX_ATTACHMENTS}`, ); } - for (const file of uploads) { + for (const file of sized) { if (file.size > MAX_ATTACHMENT_BYTES) { throw new AttachmentLimitError( `"${file.name || "attachment"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_ATTACHMENT_BYTES)} can't be attached`, @@ -97,13 +97,47 @@ async function uploadOne( return fileId; } +function expectOnePathEach(files: File[], paths: string[]): string[] { + if (paths.length !== files.length) { + throw new Error( + `Expected ${files.length} attachment paths, got ${paths.length}`, + ); + } + return paths; +} + +async function writeOnSameMachine( + sameMachine: NonNullable, + files: File[], +): Promise { + const sourcePaths = files.map((file) => sameMachine.pathOf(file)); + assertWithinLimits( + files, + files.filter((_, index) => !sourcePaths[index]), + ); + const items = await Promise.all( + files.map(async (file, index): Promise => { + const sourcePath = sourcePaths[index]; + if (sourcePath) return { kind: "path", sourcePath }; + return { + kind: "base64", + name: nameOf(file, index), + data: await sameMachine.readBase64(file), + }; + }), + ); + return expectOnePathEach(files, await sameMachine.write(items)); +} + /** * Gets composer files into a workspace's worktree and returns the - * worktree-relative paths an agent is handed — the same route the mobile - * composer and the new-workspace modal take: bytes go client → cloud storage - * over a presigned PUT, and the host pulls them from there. The host-service - * request carries only ids, so neither a relay's body cap nor a sandbox - * ingress that cuts an upload the host answers early ever sees the bytes. + * worktree-relative paths an agent is handed, in the order of `files`. + * + * A workspace on this machine gets them from the desktop main process and + * nothing leaves the machine. Any other workspace gets them through storage, + * like the mobile composer: client → presigned PUT, then the host pulls them, + * so the host-service request carries only ids and a sandbox ingress that + * cuts an upload answered early never sees the bytes. * * The caller owns the error surface; a thrown `AttachmentLimitError` is * already worded for a toast. @@ -116,45 +150,15 @@ export async function sendAttachmentsToWorkspace(options: { }): Promise { const { deps, workspaceId, isCloudWorkspace, files } = options; if (files.length === 0) return []; - const localPaths = files.map((file) => deps.local?.pathOf(file) ?? ""); - const uploads = files.filter((_, index) => !localPaths[index]); - assertWithinLimits(files, uploads); + if (deps.sameMachine) return writeOnSameMachine(deps.sameMachine, files); + assertWithinLimits(files); - // Order is kept: the host names attachments positionally and the caller - // pairs each path back with the file it came from. - const uploadedPaths = await uploadAndMaterialize( - deps, - workspaceId, - isCloudWorkspace, - files, - localPaths, - ); - const copySources = localPaths.filter(Boolean); - const copiedPaths = - deps.local && copySources.length > 0 - ? await deps.local.copy(copySources) - : []; - return localPaths.map((localPath) => - localPath ? (copiedPaths.shift() ?? "") : (uploadedPaths.shift() ?? ""), - ); -} - -async function uploadAndMaterialize( - deps: SendAttachmentsDeps, - workspaceId: string, - isCloudWorkspace: boolean, - files: File[], - localPaths: string[], -): Promise { const fileIds = await Promise.all( - files.flatMap((file, index) => - localPaths[index] ? [] : [uploadOne(deps, file, index)], - ), + files.map((file, index) => uploadOne(deps, file, index)), ); - if (fileIds.length === 0) return []; if (isCloudWorkspace) { await deps.attachFiles({ id: workspaceId, fileIds }); } const { paths } = await deps.materialize({ workspaceId, fileIds }); - return paths; + return expectOnePathEach(files, paths); } diff --git a/packages/host-service/src/chat-v3/agentImages.ts b/packages/host-service/src/chat-v3/agentImages.ts index 8702c5b8fcf..a9cbd354130 100644 --- a/packages/host-service/src/chat-v3/agentImages.ts +++ b/packages/host-service/src/chat-v3/agentImages.ts @@ -21,8 +21,8 @@ import { sanitizeAttachmentFileName, WORKSPACE_ATTACHMENTS_DIR, } from "@superset/shared/workspace-attachments"; +import { ensureWorkspaceAttachmentsDir } from "@superset/shared/workspace-attachments-dir"; import mimeTypes from "mime-types"; -import { ensureWorkspaceAttachmentsDir } from "../runtime/workspace-attachments-dir"; const NAME_HASH_CHARS = 12; diff --git a/packages/host-service/src/trpc/router/attachments/attachments.ts b/packages/host-service/src/trpc/router/attachments/attachments.ts index 9a675bbad08..8b6bfa6d5f5 100644 --- a/packages/host-service/src/trpc/router/attachments/attachments.ts +++ b/packages/host-service/src/trpc/router/attachments/attachments.ts @@ -7,10 +7,10 @@ import { attachmentNameWithSuffix, WORKSPACE_ATTACHMENTS_DIR, } from "@superset/shared/workspace-attachments"; +import { ensureWorkspaceAttachmentsDir } from "@superset/shared/workspace-attachments-dir"; import { TRPCError } from "@trpc/server"; import mimeTypes from "mime-types"; import { z } from "zod"; -import { ensureWorkspaceAttachmentsDir } from "../../../runtime/workspace-attachments-dir"; import { protectedProcedure, router } from "../../index"; import { resolveWorktreePath } from "../git/utils/resolve-worktree"; import { MAX_INLINE_ATTACHMENT_BYTES } from "./constants"; diff --git a/packages/shared/package.json b/packages/shared/package.json index 38e98c79772..a2e33dee1de 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -361,6 +361,10 @@ "types": "./src/workspace-attachments.ts", "default": "./src/workspace-attachments.ts" }, + "./workspace-attachments-dir": { + "types": "./src/workspace-attachments-dir.ts", + "default": "./src/workspace-attachments-dir.ts" + }, "./agent-permissions-migration": { "types": "./src/agent-permissions-migration.ts", "default": "./src/agent-permissions-migration.ts" diff --git a/packages/host-service/src/runtime/workspace-attachments-dir.ts b/packages/shared/src/workspace-attachments-dir.ts similarity index 89% rename from packages/host-service/src/runtime/workspace-attachments-dir.ts rename to packages/shared/src/workspace-attachments-dir.ts index 809fd73810b..c710ed5820a 100644 --- a/packages/host-service/src/runtime/workspace-attachments-dir.ts +++ b/packages/shared/src/workspace-attachments-dir.ts @@ -1,6 +1,6 @@ import { closeSync, mkdirSync, openSync, writeFileSync } from "node:fs"; import { join } from "node:path"; -import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; +import { WORKSPACE_ATTACHMENTS_DIR } from "./workspace-attachments"; /** * The worktree directory attachments are written into, created with a From f745e33547e3413002f2b9e8d647851ea51024ce Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:30:52 +0000 Subject: [PATCH 06/16] feat(desktop): show attachments by the name the user attached, as file cards Sent messages showed the sanitized disk name in a plain badge (Superset-arm64__21__1.dmg). The attachment tag now carries the original name in an optional, escaped name attribute, and every reader goes through attachmentDisplayName, falling back to the disk name for tags written before. Files render with the composer's card (icon, name, type) extracted into @superset/chat-ui/AttachmentFileCard; the name line drops the extension since the type line shows it, and transcript cards size to the name. --- .../useUploadAttachments.ts | 6 +- .../AttachmentImage/AttachmentImage.test.tsx | 4 +- .../AttachmentImage/AttachmentImage.tsx | 8 +- .../UserMessageRow/UserMessageRow.tsx | 48 +++++++--- .../utils/galleryImages/galleryImages.ts | 15 +++- .../QueuedPromptRow/QueuedPromptRow.tsx | 9 +- .../attachmentTags/attachmentTags.test.ts | 31 ++++++- .../utils/attachmentTags/attachmentTags.ts | 62 +++++++++++-- .../ChatSession/utils/attachmentTags/index.ts | 1 + .../AttachmentFileCard/AttachmentFileCard.tsx | 90 +++++++++++++++++++ .../components/AttachmentFileCard/index.ts | 4 + .../AttachmentPills/AttachmentPills.tsx | 70 ++------------- 12 files changed, 247 insertions(+), 101 deletions(-) create mode 100644 packages/chat-ui/src/components/AttachmentFileCard/AttachmentFileCard.tsx create mode 100644 packages/chat-ui/src/components/AttachmentFileCard/index.ts diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index 3a46d51ac8d..f8e954e3ce3 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -16,7 +16,11 @@ export function useUploadAttachments(workspaceId: string) { try { const paths = await sendAttachments(files); return paths.map((path, index) => - formatAttachmentTag({ path, type: files[index]?.type ?? "" }), + formatAttachmentTag({ + path, + type: files[index]?.type ?? "", + name: files[index]?.name, + }), ); } catch (error) { toast.error(t({ message: "Couldn't attach files" }), { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.test.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.test.tsx index de68d151bfa..25cc6208f60 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.test.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.test.tsx @@ -119,7 +119,7 @@ describe("AttachmentImage", () => { throw new Error("no workspace"); }, ); - await waitFor(() => view.getByText("big.png")); + await waitFor(() => view.getByTitle("big.png")); expect(view.queryByRole("button", tile)).toBeNull(); }); @@ -128,7 +128,7 @@ describe("AttachmentImage", () => { () => read(false), () => ({ worktreePath: null }), ); - await waitFor(() => view.getByText("big.png")); + await waitFor(() => view.getByTitle("big.png")); }); test("a failed read of a file over the cap says so in the overlay", async () => { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.tsx index 281234f1893..d5953bd2b77 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/AttachmentImage.tsx @@ -1,4 +1,4 @@ -import { Badge } from "@superset/ui/badge"; +import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; import { useCallback, useState } from "react"; import { useChatImageSource } from "../../hooks/useChatImageSource"; import { useChatImageGallery } from "../../providers/ChatImageGalleryProvider"; @@ -8,6 +8,7 @@ import { PreviewableImage } from "./components/PreviewableImage"; export function AttachmentImage({ alt, + name: attachedName, owner, path, type, @@ -17,10 +18,11 @@ export function AttachmentImage({ path: string; type: string; alt?: string | undefined; + name?: string | undefined; }) { const { open, registerThumbnail } = useChatImageGallery(); const id = galleryImageId(owner, path); - const name = path.split("/").pop() ?? path; + const name = attachedName || (path.split("/").pop() ?? path); const source = useChatImageSource({ path, mimeType: type }, { full: false }); const [unrenderable, setUnrenderable] = useState(false); const register = useCallback( @@ -33,7 +35,7 @@ export function AttachmentImage({ const openPreview = () => open(id); if (source.status === "error" || unrenderable) - return {name}; + return ; if (source.status === "loading") return (
diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx index 90e8a652c8a..ecf13373063 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx @@ -5,6 +5,7 @@ import { userMessageText, } from "@superset/chat/core"; import type { AvailableCommand, UserMessage } from "@superset/chat/protocol"; +import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; import { Message, MessageContent } from "@superset/ui/ai-elements/message"; import { Badge } from "@superset/ui/badge"; import { Button } from "@superset/ui/button"; @@ -13,7 +14,10 @@ import { cn } from "@superset/ui/utils"; import { Pencil } from "lucide-react"; import { useRef, useState } from "react"; import { useChatPaneActions } from "../../../../providers/ChatPaneActionsProvider"; -import { parseAttachmentTags } from "../../../../utils/attachmentTags"; +import { + attachmentDisplayName, + parseAttachmentTags, +} from "../../../../utils/attachmentTags"; import { type MessageSegment, splitByElements, @@ -78,11 +82,18 @@ export function UserMessageRow({ .filter((attachment) => !attachment.type.startsWith("image/")) .map((attachment) => ({ key: attachment.path, - name: attachment.path.split("/").pop() ?? attachment.path, + name: attachmentDisplayName(attachment), + mediaType: attachment.type, })), ...item.content.flatMap((content) => content.type === "attachment" - ? [{ key: content.attachmentId, name: content.name }] + ? [ + { + key: content.attachmentId, + name: content.name, + mediaType: content.mimeType, + }, + ] : [], ), ]; @@ -130,12 +141,30 @@ export function UserMessageRow({ ))}
)} + {files.length > 0 && ( +
+ {files.map((file) => ( + + ))} +
+ )} {pastedTexts.length > 0 && (
)} - {(text || files.length > 0) && ( + {text && (
- {files.length > 0 && ( -
- {files.map((file) => ( - - {file.name} - - ))} -
- )} )} {copyable && ( diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/utils/galleryImages/galleryImages.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/utils/galleryImages/galleryImages.ts index 769339ac78e..4dc4bc60749 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/utils/galleryImages/galleryImages.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/utils/galleryImages/galleryImages.ts @@ -7,7 +7,10 @@ import type { UserMessage, } from "@superset/chat/protocol"; import { isImageAttachment } from "@superset/chat/protocol"; -import { parseAttachmentTags } from "../../../../utils/attachmentTags"; +import { + attachmentDisplayName, + parseAttachmentTags, +} from "../../../../utils/attachmentTags"; import type { TranscriptRow } from "../transcriptRows"; export type GalleryImage = { @@ -19,7 +22,12 @@ export type GalleryImage = { alt?: string | undefined; }; -type ItemImage = { path: string; mimeType: string; alt?: string | undefined }; +type ItemImage = { + path: string; + mimeType: string; + alt?: string | undefined; + name?: string | undefined; +}; /** `owner` is the transcript key of the item that shows the image. */ export function galleryImageId(owner: string, path: string): string { @@ -32,6 +40,7 @@ function userImages(content: readonly UserContent[]): ItemImage[] { .map((attachment) => ({ path: attachment.path, mimeType: attachment.type, + name: attachmentDisplayName(attachment), })); } @@ -92,7 +101,7 @@ export function galleryImages(rows: readonly TranscriptRow[]): GalleryImage[] { id: galleryImageId(owner, image.path), path: image.path, mimeType: image.mimeType, - filename: image.path.split("/").pop() ?? image.path, + filename: image.name ?? image.path.split("/").pop() ?? image.path, alt: image.alt, }); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/WorkDrawer/components/QueuedPromptRow/QueuedPromptRow.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/WorkDrawer/components/QueuedPromptRow/QueuedPromptRow.tsx index f1a1ab94281..0bc34ae680d 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/WorkDrawer/components/QueuedPromptRow/QueuedPromptRow.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/WorkDrawer/components/QueuedPromptRow/QueuedPromptRow.tsx @@ -21,7 +21,10 @@ import { ListEnd, Trash2, } from "lucide-react"; -import { parseAttachmentTags } from "../../../../utils/attachmentTags"; +import { + attachmentDisplayName, + parseAttachmentTags, +} from "../../../../utils/attachmentTags"; export function QueuedPromptRow({ rowKey, @@ -45,9 +48,7 @@ export function QueuedPromptRow({ userMessageText(prompt), ); const attachmentNames = [ - ...attachments.map( - (attachment) => attachment.path.split("/").pop() ?? attachment.path, - ), + ...attachments.map(attachmentDisplayName), ...prompt.content.flatMap((content) => content.type === "attachment" ? [content.name] : [], ), diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts index 0eb47c2dd5a..c34c1be45e4 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it } from "bun:test"; import { appendPastedText } from "@superset/chat/core"; -import { formatAttachmentTag, parseAttachmentTags } from "./attachmentTags"; +import { + attachmentDisplayName, + formatAttachmentTag, + parseAttachmentTags, +} from "./attachmentTags"; describe("attachmentTags", () => { it("round-trips tags appended to a message and strips them from the text", () => { @@ -56,4 +60,29 @@ describe("attachmentTags", () => { pastedTexts: [paste], }); }); + + it("keeps the attached name, escaped, and falls back to the file on disk without one", () => { + const message = [ + formatAttachmentTag({ + path: ".superset/attachments/Superset-arm64__21_.dmg", + type: "application/x-apple-diskimage", + name: 'Superset-arm64 (21) "final" .dmg', + }), + formatAttachmentTag({ + path: ".superset/attachments/notes.txt", + type: "text/plain", + name: "notes.txt", + }), + ].join("\n"); + + expect(message).toContain( + 'name="Superset-arm64 (21) "final" <v2>.dmg"', + ); + expect(message).not.toContain('name="notes.txt"'); + const { attachments } = parseAttachmentTags(message); + expect(attachments.map(attachmentDisplayName)).toEqual([ + 'Superset-arm64 (21) "final" .dmg', + "notes.txt", + ]); + }); }); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts index 940836d169f..18410ab355c 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts @@ -1,11 +1,41 @@ import { parsePastedText } from "@superset/chat/core"; import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; -export type AttachmentTag = { path: string; type: string }; +export type AttachmentTag = { path: string; type: string; name?: string }; -const TAG = //g; +const TAG = + //g; const SAFE_TYPE = /^[\w.+-]+\/[\w.+-]+$/; const SAFE_NAME = /^[A-Za-z0-9._-]+$/; +const MAX_NAME_LENGTH = 255; + +const ESCAPES: Record = { + "&": "&", + '"': """, + "<": "<", + ">": ">", +}; +const UNESCAPES: Record = { + amp: "&", + quot: '"', + lt: "<", + gt: ">", +}; + +function escapeAttribute(value: string): string { + return value.replace(/[&"<>]/g, (char) => ESCAPES[char] ?? char); +} + +function unescapeAttribute(value: string): string { + return value.replace( + /&(amp|quot|lt|gt);/g, + (match, entity: string) => UNESCAPES[entity] ?? match, + ); +} + +function baseName(path: string): string { + return path.split("/").pop() ?? path; +} function isWorkspaceAttachmentPath(path: string): boolean { const prefix = `${WORKSPACE_ATTACHMENTS_DIR}/`; @@ -14,10 +44,22 @@ function isWorkspaceAttachmentPath(path: string): boolean { return SAFE_NAME.test(name) && name !== "." && name !== ".."; } -export function formatAttachmentTag({ path, type }: AttachmentTag): string { - return SAFE_TYPE.test(type) - ? `` - : ``; +/** The name the user attached; tags written before names were kept fall back to the file on disk. */ +export function attachmentDisplayName(tag: AttachmentTag): string { + return tag.name || baseName(tag.path); +} + +export function formatAttachmentTag({ path, type, name }: AttachmentTag): string { + const attributes = [`path="${path}"`]; + if (SAFE_TYPE.test(type)) attributes.push(`type="${type}"`); + const cleanName = name + ?.replace(/\p{Cc}/gu, "") + .trim() + .slice(0, MAX_NAME_LENGTH); + if (cleanName && cleanName !== baseName(path)) { + attributes.push(`name="${escapeAttribute(cleanName)}"`); + } + return ``; } export function parseAttachmentTags(message: string): { @@ -29,9 +71,13 @@ export function parseAttachmentTags(message: string): { const attachments: AttachmentTag[] = []; const rest = text.replace( TAG, - (match: string, path: string, type?: string) => { + (match: string, path: string, type?: string, name?: string) => { if (!isWorkspaceAttachmentPath(path)) return match; - attachments.push({ path, type: type ?? "" }); + attachments.push({ + path, + type: type ?? "", + ...(name ? { name: unescapeAttribute(name) } : {}), + }); return ""; }, ); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts index 5318c550353..6026020d69e 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts @@ -1,5 +1,6 @@ export { type AttachmentTag, + attachmentDisplayName, formatAttachmentTag, parseAttachmentTags, } from "./attachmentTags"; diff --git a/packages/chat-ui/src/components/AttachmentFileCard/AttachmentFileCard.tsx b/packages/chat-ui/src/components/AttachmentFileCard/AttachmentFileCard.tsx new file mode 100644 index 00000000000..46a76869ff5 --- /dev/null +++ b/packages/chat-ui/src/components/AttachmentFileCard/AttachmentFileCard.tsx @@ -0,0 +1,90 @@ +"use client"; + +import { cn } from "@superset/ui/utils"; +import { + FileArchiveIcon, + FileCode2Icon, + FileJson2Icon, + FileTextIcon, + ImageIcon, + Music2Icon, +} from "lucide-react"; +import type { JSX } from "react"; + +const CODE_EXTENSIONS = new Set([ + "ts", + "tsx", + "js", + "jsx", + "py", + "rs", + "go", + "rb", + "swift", + "css", + "html", + "sh", +]); +const DATA_EXTENSIONS = new Set(["json", "yaml", "yml", "toml", "lock"]); +const ARCHIVE_EXTENSIONS = new Set(["zip", "tar", "gz", "tgz", "7z", "rar"]); + +// Mime type first (audio, images demoted after a failed preview), then +// extension for the text-ish formats mime can't distinguish. +function fileTypeIcon(mediaType: string, extension: string): JSX.Element { + if (mediaType.startsWith("audio/")) + return ; + if (mediaType.startsWith("image/")) + return ; + const lowered = extension.toLowerCase(); + if (CODE_EXTENSIONS.has(lowered)) + return ; + if (DATA_EXTENSIONS.has(lowered)) + return ; + if (ARCHIVE_EXTENSIONS.has(lowered)) + return ; + return ; +} + +export type AttachmentFileCardProps = { + name: string; + mediaType: string; + onClick?: () => void; + className?: string; +}; + +export function AttachmentFileCard({ + name, + mediaType, + onClick, + className, +}: AttachmentFileCardProps) { + const dotIndex = name.lastIndexOf("."); + const hasExtension = dotIndex > 0 && dotIndex < name.length - 1; + const stem = hasExtension ? name.slice(0, dotIndex) : name; + const extension = hasExtension ? name.slice(dotIndex + 1).toUpperCase() : ""; + const Root = onClick ? "button" : "div"; + return ( + + {onClick && ( + + )} +
+ {fileTypeIcon(mediaType, extension)} +
+
+
{stem}
+ {extension && ( +
{extension}
+ )} +
+
+ ); +} diff --git a/packages/chat-ui/src/components/AttachmentFileCard/index.ts b/packages/chat-ui/src/components/AttachmentFileCard/index.ts new file mode 100644 index 00000000000..eca587b03d7 --- /dev/null +++ b/packages/chat-ui/src/components/AttachmentFileCard/index.ts @@ -0,0 +1,4 @@ +export { + AttachmentFileCard, + type AttachmentFileCardProps, +} from "./AttachmentFileCard"; diff --git a/packages/chat-ui/src/components/PromptInput/components/AttachmentPills/AttachmentPills.tsx b/packages/chat-ui/src/components/PromptInput/components/AttachmentPills/AttachmentPills.tsx index fa0f133da4e..b74795b8b07 100644 --- a/packages/chat-ui/src/components/PromptInput/components/AttachmentPills/AttachmentPills.tsx +++ b/packages/chat-ui/src/components/PromptInput/components/AttachmentPills/AttachmentPills.tsx @@ -1,15 +1,7 @@ "use client"; import { Trans } from "@lingui/react/macro"; -import { - FileArchiveIcon, - FileCode2Icon, - FileJson2Icon, - FileTextIcon, - ImageIcon, - Music2Icon, -} from "lucide-react"; -import type { JSX } from "react"; +import { AttachmentFileCard } from "../../../AttachmentFileCard"; import { PASTED_TEXT_ACTION_CLASS, PastedTextCard, @@ -31,40 +23,6 @@ export type AttachmentPillsProps = { ) => void; }; -const CODE_EXTENSIONS = new Set([ - "ts", - "tsx", - "js", - "jsx", - "py", - "rs", - "go", - "rb", - "swift", - "css", - "html", - "sh", -]); -const DATA_EXTENSIONS = new Set(["json", "yaml", "yml", "toml", "lock"]); -const ARCHIVE_EXTENSIONS = new Set(["zip", "tar", "gz", "tgz", "7z", "rar"]); - -// Mime type first (audio, images demoted after a failed preview), then -// extension for the text-ish formats mime can't distinguish. -function fileTypeIcon(mediaType: string, extension: string): JSX.Element { - if (mediaType.startsWith("audio/")) - return ; - if (mediaType.startsWith("image/")) - return ; - const lowered = extension.toLowerCase(); - if (CODE_EXTENSIONS.has(lowered)) - return ; - if (DATA_EXTENSIONS.has(lowered)) - return ; - if (ARCHIVE_EXTENSIONS.has(lowered)) - return ; - return ; -} - export function AttachmentPills({ attachments, pastedTexts = [], @@ -79,9 +37,6 @@ export function AttachmentPills({
{attachments.map((attachment) => { const filename = attachment.file.name || "attachment"; - const dotIndex = filename.lastIndexOf("."); - const extension = - dotIndex > 0 ? filename.slice(dotIndex + 1).toUpperCase() : ""; if (attachment.previewUrl) { return (
@@ -115,26 +70,11 @@ export function AttachmentPills({ } return (
- + /> onRemove(attachment.id)} />
); From 30bf515d868cee1f32eb034166f671996b160468 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Thu, 29 Oct 2026 22:47:17 +0000 Subject: [PATCH 07/16] fix(desktop): keep attachments and say why when a chat send fails A failed send put the text back but dropped every attached file, and a moved file surfaced as a raw ENOENT toast. The main-process route now reports which source is missing, a folder or unreadable instead of throwing; sendAttachmentsToWorkspace turns that into an AttachmentSourceError, and the composer shows a translated message that names the file and re-adds every other file. Limit errors keep all files so the user can remove some. Translations for all 16 locales. --- .../routers/attachments/attachments.test.ts | 49 ++++++++++---- .../trpc/routers/attachments/attachments.ts | 53 ++++++++++----- .../components/Composer/Composer.tsx | 6 +- .../useUploadAttachments.ts | 64 ++++++++++++++----- .../utils/sendAttachmentsToWorkspace/index.ts | 1 + .../sendAttachmentsToWorkspace.test.ts | 33 +++++++++- .../sendAttachmentsToWorkspace.ts | 29 ++++++++- packages/i18n/locales/cs/messages.po | 9 +++ packages/i18n/locales/de/messages.po | 9 +++ packages/i18n/locales/en/messages.po | 9 +++ packages/i18n/locales/es/messages.po | 9 +++ packages/i18n/locales/fr/messages.po | 9 +++ packages/i18n/locales/id/messages.po | 9 +++ packages/i18n/locales/it/messages.po | 9 +++ packages/i18n/locales/ja/messages.po | 9 +++ packages/i18n/locales/ko/messages.po | 9 +++ packages/i18n/locales/nl/messages.po | 9 +++ packages/i18n/locales/pl/messages.po | 9 +++ packages/i18n/locales/pt-BR/messages.po | 9 +++ packages/i18n/locales/ru/messages.po | 9 +++ packages/i18n/locales/tr/messages.po | 9 +++ packages/i18n/locales/vi/messages.po | 9 +++ packages/i18n/locales/zh-CN/messages.po | 9 +++ packages/i18n/locales/zh-TW/messages.po | 9 +++ 24 files changed, 339 insertions(+), 49 deletions(-) diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts index 85ce777ba30..0b73fbb0e11 100644 --- a/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.test.ts @@ -1,5 +1,6 @@ import { afterEach, describe, expect, it } from "bun:test"; import { + chmodSync, existsSync, mkdirSync, mkdtempSync, @@ -51,10 +52,13 @@ describe("writeAttachmentsIntoWorktree", () => { ], }); - expect(paths).toEqual([ - `${WORKSPACE_ATTACHMENTS_DIR}/build.dmg`, - `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, - ]); + expect(paths).toEqual({ + ok: true, + paths: [ + `${WORKSPACE_ATTACHMENTS_DIR}/build.dmg`, + `${WORKSPACE_ATTACHMENTS_DIR}/image.png`, + ], + }); expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); expect(readFileSync(join(directory, "image.png"), "utf8")).toBe("png"); expect(readFileSync(join(directory, ".gitignore"), "utf8")).toBe("*\n"); @@ -67,22 +71,43 @@ describe("writeAttachmentsIntoWorktree", () => { await writeAttachmentsIntoWorktree({ worktreePath, items }); writeFileSync(source, "second"); expect(await writeAttachmentsIntoWorktree({ worktreePath, items })).toEqual( - [`${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`], + { ok: true, paths: [`${WORKSPACE_ATTACHMENTS_DIR}/build_1.dmg`] }, ); expect(readFileSync(join(directory, "build.dmg"), "utf8")).toBe("first"); }); - it("refuses a directory or a UNC path as a source before writing anything", async () => { - const { root, worktreePath, directory } = setup(); + it("reports which source is missing, a folder or unreadable, before writing anything", async () => { + const { root, source, worktreePath, directory } = setup(); + const unreadable = join(root, "locked.txt"); + writeFileSync(unreadable, "secret"); + chmodSync(unreadable, 0o000); - for (const sourcePath of [root, "//server/share/x.png"]) { - await expect( - writeAttachmentsIntoWorktree({ + for (const [sourcePath, problem] of [ + [join(root, "moved.dmg"), "missing"], + [root, "not-a-file"], + [unreadable, "unreadable"], + ] as const) { + expect( + await writeAttachmentsIntoWorktree({ worktreePath, - items: [{ kind: "path", sourcePath }], + items: [ + { kind: "path", sourcePath: source }, + { kind: "path", sourcePath }, + ], }), - ).rejects.toThrow(); + ).toEqual({ ok: false, index: 1, problem }); } expect(existsSync(directory)).toBe(false); }); + + it("refuses a UNC source path", async () => { + const { worktreePath } = setup(); + + await expect( + writeAttachmentsIntoWorktree({ + worktreePath, + items: [{ kind: "path", sourcePath: "//server/share/x.png" }], + }), + ).rejects.toThrow("Not a local absolute path"); + }); }); diff --git a/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts index 5a22a293756..ae46fc66840 100644 --- a/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts +++ b/apps/desktop/src/lib/trpc/routers/attachments/attachments.ts @@ -1,5 +1,5 @@ import { constants } from "node:fs"; -import { copyFile, stat, writeFile } from "node:fs/promises"; +import { copyFile, open, stat, writeFile } from "node:fs/promises"; import { basename, isAbsolute, join } from "node:path"; import { MAX_ATTACHMENT_BYTES, @@ -29,6 +29,12 @@ const itemSchema = z.discriminatedUnion("kind", [ export type LocalAttachmentItem = z.infer; +export type AttachmentSourceProblem = "missing" | "not-a-file" | "unreadable"; + +export type WriteAttachmentsResult = + | { ok: true; paths: string[] } + | { ok: false; index: number; problem: AttachmentSourceProblem }; + function badRequest(message: string): TRPCError { return new TRPCError({ code: "BAD_REQUEST", message }); } @@ -40,21 +46,30 @@ function assertLocalAbsolute(path: string): void { } } -async function assertValid( - worktreePath: string, - items: LocalAttachmentItem[], -): Promise { +function errnoOf(error: unknown): string | undefined { + return (error as NodeJS.ErrnoException | null)?.code; +} + +async function sourceProblem( + sourcePath: string, +): Promise { + try { + if (!(await stat(sourcePath)).isFile()) return "not-a-file"; + await (await open(sourcePath, "r")).close(); + return null; + } catch (error) { + const code = errnoOf(error); + if (code === "ENOENT" || code === "ENOTDIR") return "missing"; + if (code === "EACCES" || code === "EPERM") return "unreadable"; + throw error; + } +} + +async function assertWorktree(worktreePath: string): Promise { assertLocalAbsolute(worktreePath); if (!(await stat(worktreePath)).isDirectory()) { throw badRequest(`Not a directory: ${worktreePath}`); } - for (const item of items) { - if (item.kind !== "path") continue; - assertLocalAbsolute(item.sourcePath); - if (!(await stat(item.sourcePath)).isFile()) { - throw badRequest(`Not a file: ${item.sourcePath}`); - } - } } function baseNameOf(item: LocalAttachmentItem, index: number): string { @@ -86,7 +101,7 @@ async function writeUnique( await writeExclusive(item, join(directory, fileName)); return fileName; } catch (error) { - if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; + if (errnoOf(error) !== "EEXIST") throw error; } } throw new Error(`Could not find a free name for ${base}`); @@ -95,8 +110,14 @@ async function writeUnique( export async function writeAttachmentsIntoWorktree(input: { worktreePath: string; items: LocalAttachmentItem[]; -}): Promise { - await assertValid(input.worktreePath, input.items); +}): Promise { + await assertWorktree(input.worktreePath); + for (const [index, item] of input.items.entries()) { + if (item.kind !== "path") continue; + assertLocalAbsolute(item.sourcePath); + const problem = await sourceProblem(item.sourcePath); + if (problem) return { ok: false, index, problem }; + } const directory = ensureWorkspaceAttachmentsDir(input.worktreePath); const paths: string[] = []; for (const [index, item] of input.items.entries()) { @@ -107,7 +128,7 @@ export async function writeAttachmentsIntoWorktree(input: { ); paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); } - return paths; + return { ok: true, paths }; } export const createAttachmentsRouter = () => { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx index d13753e7514..fdf9948ee63 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx @@ -512,11 +512,13 @@ export const Composer = memo(function Composer({ pastedTexts: string[], steer: boolean, ) => { - const tags = await uploadAttachments(files); - if (!tags) { + const upload = await uploadAttachments(files); + if (!upload.ok) { promptInputRef.current?.appendText(appendPastedText(text, pastedTexts)); + promptInputRef.current?.addFiles(upload.keep); return; } + const { tags } = upload; const message = text.trim(); const elements = elementsForChips(message, mentions); const mcpContext = mcpContextLine( diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index f8e954e3ce3..877df1467be 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -3,33 +3,67 @@ import { errorMessage } from "@superset/i18n/errors"; import { toast } from "@superset/ui/sonner"; import { useCallback } from "react"; import { useSendAttachments } from "../../../../../../hooks/useSendAttachments"; -import { AttachmentLimitError } from "../../../../../../utils/sendAttachmentsToWorkspace"; +import { + AttachmentLimitError, + AttachmentSourceError, +} from "../../../../../../utils/sendAttachmentsToWorkspace"; import { formatAttachmentTag } from "../../../../utils/attachmentTags"; +export type UploadAttachmentsResult = + | { ok: true; tags: string[] } + | { ok: false; keep: File[] }; + export function useUploadAttachments(workspaceId: string) { const { t } = useLingui(); const sendAttachments = useSendAttachments(workspaceId); return useCallback( - async (files: File[]): Promise => { - if (files.length === 0) return []; + async (files: File[]): Promise => { + if (files.length === 0) return { ok: true, tags: [] }; + const describe = (error: unknown): string => { + if (error instanceof AttachmentLimitError) return error.message; + if (error instanceof AttachmentSourceError) { + const name = error.fileName; + switch (error.problem) { + case "missing": + return t({ + message: `"${name}" was moved or deleted. Attach it again.`, + }); + case "not-a-file": + return t({ + message: `"${name}" is a folder. Attach the files inside it instead.`, + }); + case "unreadable": + return t({ + message: `Paradev doesn't have permission to read "${name}".`, + }); + } + } + return errorMessage(error, t({ message: "Unknown error" })); + }; try { const paths = await sendAttachments(files); - return paths.map((path, index) => - formatAttachmentTag({ - path, - type: files[index]?.type ?? "", - name: files[index]?.name, - }), - ); + return { + ok: true, + tags: paths.map((path, index) => + formatAttachmentTag({ + path, + type: files[index]?.type ?? "", + name: files[index]?.name, + }), + ), + }; } catch (error) { toast.error(t({ message: "Couldn't attach files" }), { - description: - error instanceof AttachmentLimitError - ? error.message - : errorMessage(error, t({ message: "Unknown error" })), + description: describe(error), }); - return null; + return { + ok: false, + keep: + error instanceof AttachmentSourceError + ? files.filter((_, index) => index !== error.index) + : files, + }; } }, [sendAttachments, t], diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts index c3094082875..3217ea4ecc7 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts @@ -1,5 +1,6 @@ export { AttachmentLimitError, + AttachmentSourceError, type SendAttachmentsDeps, sendAttachmentsToWorkspace, } from "./sendAttachmentsToWorkspace"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts index 7b877ebfb83..2916f0225a4 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts @@ -5,6 +5,7 @@ import { } from "@superset/shared/attachment-limits"; import { AttachmentLimitError, + AttachmentSourceError, type LocalAttachmentItem, type SendAttachmentsDeps, sendAttachmentsToWorkspace, @@ -198,7 +199,10 @@ describe("sendAttachmentsToWorkspace", () => { readBase64: async () => "cG5n", async write(items) { written.push(items); - return items.map((_, i) => `.superset/attachments/${i}`); + return { + ok: true, + paths: items.map((_, i) => `.superset/attachments/${i}`), + }; }, }, }, @@ -235,7 +239,7 @@ describe("sendAttachmentsToWorkspace", () => { sameMachine: { pathOf: () => "", readBase64: async () => "", - write: async () => [], + write: async () => ({ ok: true, paths: [] }), }, }, workspaceId: "ws-1", @@ -257,4 +261,29 @@ describe("sendAttachmentsToWorkspace", () => { }), ).rejects.toThrow("Expected 1 attachment paths, got 0"); }); + + it("names the file the host could not read, so the composer can say which", async () => { + const { deps } = makeDeps(); + + const error = await sendAttachmentsToWorkspace({ + deps: { + ...deps, + sameMachine: { + pathOf: (file) => `/Users/me/${file.name}`, + readBase64: async () => "", + write: async () => ({ ok: false, index: 1, problem: "missing" }), + }, + }, + workspaceId: "ws-1", + isCloudWorkspace: false, + files: [png("a.png"), png("moved.png")], + }).catch((caught: unknown) => caught); + + expect(error).toBeInstanceOf(AttachmentSourceError); + expect(error).toMatchObject({ + index: 1, + fileName: "moved.png", + problem: "missing", + }); + }); }); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts index 7e2f9e50f97..c452495f189 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts @@ -7,6 +7,19 @@ import { attachmentFallbackName } from "@superset/shared/workspace-attachments"; /** Thrown for limits the user can act on; the message is toast-ready. */ export class AttachmentLimitError extends Error {} +export type AttachmentSourceProblem = "missing" | "not-a-file" | "unreadable"; + +/** A file on this machine that could not be attached; `index` is its place in the send. */ +export class AttachmentSourceError extends Error { + constructor( + readonly index: number, + readonly fileName: string, + readonly problem: AttachmentSourceProblem, + ) { + super(`${fileName}: ${problem}`); + } +} + function formatMb(bytes: number): string { return `${Math.round(bytes / 1024 / 1024)}MB`; } @@ -20,6 +33,10 @@ export type LocalAttachmentItem = | { kind: "path"; sourcePath: string } | { kind: "base64"; name: string; data: string }; +export type SameMachineWriteResult = + | { ok: true; paths: string[] } + | { ok: false; index: number; problem: AttachmentSourceProblem }; + export interface SendAttachmentsDeps { createUpload(input: { name: string; @@ -36,7 +53,7 @@ export interface SendAttachmentsDeps { sameMachine?: { pathOf(file: File): string; readBase64(file: File): Promise; - write(items: LocalAttachmentItem[]): Promise; + write(items: LocalAttachmentItem[]): Promise; }; } @@ -126,7 +143,15 @@ async function writeOnSameMachine( }; }), ); - return expectOnePathEach(files, await sameMachine.write(items)); + const result = await sameMachine.write(items); + if (!result.ok) { + throw new AttachmentSourceError( + result.index, + files[result.index]?.name || attachmentFallbackName(result.index), + result.problem, + ); + } + return expectOnePathEach(files, result.paths); } /** diff --git a/packages/i18n/locales/cs/messages.po b/packages/i18n/locales/cs/messages.po index 25409dec07d..75b5c5569ec 100644 --- a/packages/i18n/locales/cs/messages.po +++ b/packages/i18n/locales/cs/messages.po @@ -65,12 +65,18 @@ msgstr "„{0}“ se přestane spouštět a její historie běhů bude odstraně msgid "\"{name}\" deleted" msgstr "„{name}“ smazáno" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "„{name}“ je složka. Připojte místo toho soubory v ní." + msgid "\"{name}\" paused" msgstr "„{name}“ pozastaveno" msgid "\"{name}\" resumed" msgstr "„{name}“ pokračuje" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "„{name}“ byl přesunut nebo smazán. Připojte ho znovu." + msgid "\"{title}\" has finished its task" msgstr "„{title}“ dokončil svůj úkol" @@ -13506,6 +13512,9 @@ msgstr "Design systém Paradev" msgid "Paradev desktop app" msgstr "Desktopová aplikace Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev nemá oprávnění ke čtení „{name}“." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise je Paradev pro týmy. Ozvěte se a řekneme vám, co obsahuje a jak může fungovat ve vaší organizaci." diff --git a/packages/i18n/locales/de/messages.po b/packages/i18n/locales/de/messages.po index e971588c8b0..57934480d5d 100644 --- a/packages/i18n/locales/de/messages.po +++ b/packages/i18n/locales/de/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" wird nicht mehr ausgelöst und der Ausführungsverlauf wird entf msgid "\"{name}\" deleted" msgstr "\"{name}\" gelöscht" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" ist ein Ordner. Hänge stattdessen die Dateien darin an." + msgid "\"{name}\" paused" msgstr "\"{name}\" pausiert" msgid "\"{name}\" resumed" msgstr "\"{name}\" fortgesetzt" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" wurde verschoben oder gelöscht. Hänge die Datei erneut an." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" hat seine Aufgabe abgeschlossen" @@ -13506,6 +13512,9 @@ msgstr "Paradev Design System" msgid "Paradev desktop app" msgstr "Paradev-Desktop-App" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev hat keine Berechtigung, \"{name}\" zu lesen." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise ist Paradev für Teams. Melde dich, um mehr darüber zu erfahren, was enthalten ist und wie es für deine Organisation funktionieren kann." diff --git a/packages/i18n/locales/en/messages.po b/packages/i18n/locales/en/messages.po index ceeb80ea9ef..9157da3c593 100644 --- a/packages/i18n/locales/en/messages.po +++ b/packages/i18n/locales/en/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" will stop firing and its run history will be removed. This can't msgid "\"{name}\" deleted" msgstr "\"{name}\" deleted" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" is a folder. Attach the files inside it instead." + msgid "\"{name}\" paused" msgstr "\"{name}\" paused" msgid "\"{name}\" resumed" msgstr "\"{name}\" resumed" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" was moved or deleted. Attach it again." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" has finished its task" @@ -13506,6 +13512,9 @@ msgstr "Paradev Design System" msgid "Paradev desktop app" msgstr "Paradev desktop app" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev doesn't have permission to read \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." diff --git a/packages/i18n/locales/es/messages.po b/packages/i18n/locales/es/messages.po index 57ff2540be7..68f5c81e83d 100644 --- a/packages/i18n/locales/es/messages.po +++ b/packages/i18n/locales/es/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" dejará de dispararse y se eliminará su historial de ejecucione msgid "\"{name}\" deleted" msgstr "\"{name}\" eliminada" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" es una carpeta. Adjunta los archivos que contiene." + msgid "\"{name}\" paused" msgstr "\"{name}\" en pausa" msgid "\"{name}\" resumed" msgstr "\"{name}\" reanudada" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" se movió o se eliminó. Vuelve a adjuntarlo." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" ha terminado su tarea" @@ -13506,6 +13512,9 @@ msgstr "Sistema de diseño de Paradev" msgid "Paradev desktop app" msgstr "App de escritorio de Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev no tiene permiso para leer \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise es Paradev para equipos. Escríbenos para saber más sobre lo que incluye y cómo puede funcionar en tu organización." diff --git a/packages/i18n/locales/fr/messages.po b/packages/i18n/locales/fr/messages.po index 70d451e4986..cdc2bd9d765 100644 --- a/packages/i18n/locales/fr/messages.po +++ b/packages/i18n/locales/fr/messages.po @@ -65,12 +65,18 @@ msgstr "« {0} » ne se déclenchera plus et son historique d'exécutions sera s msgid "\"{name}\" deleted" msgstr "« {name} » supprimée" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "« {name} » est un dossier. Joignez plutôt les fichiers qu’il contient." + msgid "\"{name}\" paused" msgstr "« {name} » mise en pause" msgid "\"{name}\" resumed" msgstr "« {name} » reprise" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "« {name} » a été déplacé ou supprimé. Joignez-le à nouveau." + msgid "\"{title}\" has finished its task" msgstr "« {title} » a terminé sa tâche" @@ -13506,6 +13512,9 @@ msgstr "Système de design Paradev" msgid "Paradev desktop app" msgstr "Application de bureau Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev n’a pas l’autorisation de lire « {name} »." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise, c'est Paradev pour les équipes. Contactez-nous pour savoir ce qu'il comprend et comment il peut s'adapter à votre organisation." diff --git a/packages/i18n/locales/id/messages.po b/packages/i18n/locales/id/messages.po index 21758bc5b27..2c236c560a6 100644 --- a/packages/i18n/locales/id/messages.po +++ b/packages/i18n/locales/id/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" akan berhenti berjalan dan riwayat run-nya akan dihapus. Ini tid msgid "\"{name}\" deleted" msgstr "\"{name}\" dihapus" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" adalah folder. Lampirkan file di dalamnya sebagai gantinya." + msgid "\"{name}\" paused" msgstr "\"{name}\" dijeda" msgid "\"{name}\" resumed" msgstr "\"{name}\" dilanjutkan" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" telah dipindahkan atau dihapus. Lampirkan lagi." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" telah menyelesaikan tugasnya" @@ -13506,6 +13512,9 @@ msgstr "Design System Paradev" msgid "Paradev desktop app" msgstr "Aplikasi desktop Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev tidak memiliki izin untuk membaca \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise adalah Paradev untuk tim. Hubungi kami untuk tahu lebih banyak soal apa saja yang termasuk dan bagaimana cocoknya untuk organisasi Anda." diff --git a/packages/i18n/locales/it/messages.po b/packages/i18n/locales/it/messages.po index bb6987ba665..5539d1aacc6 100644 --- a/packages/i18n/locales/it/messages.po +++ b/packages/i18n/locales/it/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" smetterà di scattare e la sua cronologia delle esecuzioni verr msgid "\"{name}\" deleted" msgstr "\"{name}\" eliminata" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" è una cartella. Allega invece i file al suo interno." + msgid "\"{name}\" paused" msgstr "\"{name}\" messa in pausa" msgid "\"{name}\" resumed" msgstr "\"{name}\" ripresa" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" è stato spostato o eliminato. Allegalo di nuovo." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" ha completato il suo task" @@ -13506,6 +13512,9 @@ msgstr "Paradev Design System" msgid "Paradev desktop app" msgstr "App desktop di Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev non ha l'autorizzazione per leggere \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise è Paradev per i team. Contattaci per sapere cosa include e come può funzionare per la tua organizzazione." diff --git a/packages/i18n/locales/ja/messages.po b/packages/i18n/locales/ja/messages.po index 4f1b990edb0..cee7275ee0c 100644 --- a/packages/i18n/locales/ja/messages.po +++ b/packages/i18n/locales/ja/messages.po @@ -65,12 +65,18 @@ msgstr "「{0}」は発火しなくなり、実行履歴も削除されます。 msgid "\"{name}\" deleted" msgstr "「{name}」を削除しました" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "「{name}」はフォルダです。代わりにフォルダ内のファイルを添付してください。" + msgid "\"{name}\" paused" msgstr "「{name}」を一時停止しました" msgid "\"{name}\" resumed" msgstr "「{name}」を再開しました" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "「{name}」は移動または削除されました。もう一度添付してください。" + msgid "\"{title}\" has finished its task" msgstr "「{title}」がタスクを完了しました" @@ -13506,6 +13512,9 @@ msgstr "Paradevデザインシステム" msgid "Paradev desktop app" msgstr "Paradev デスクトップアプリ" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev には「{name}」を読み取る権限がありません。" + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterpriseは、チーム向けのParadevです。含まれる内容や組織での活用方法については、お問い合わせください。" diff --git a/packages/i18n/locales/ko/messages.po b/packages/i18n/locales/ko/messages.po index 3a95f4c87f3..ba061daedd9 100644 --- a/packages/i18n/locales/ko/messages.po +++ b/packages/i18n/locales/ko/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\"이(가) 더 이상 실행되지 않으며 실행 기록도 삭 msgid "\"{name}\" deleted" msgstr "\"{name}\"을(를) 삭제했습니다" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\"은(는) 폴더입니다. 대신 폴더 안의 파일을 첨부하세요." + msgid "\"{name}\" paused" msgstr "\"{name}\"을(를) 일시 중지했습니다" msgid "\"{name}\" resumed" msgstr "\"{name}\"을(를) 재개했습니다" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\"이(가) 이동되었거나 삭제되었습니다. 다시 첨부하세요." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\"이(가) 작업을 완료했습니다" @@ -13506,6 +13512,9 @@ msgstr "Paradev 디자인 시스템" msgid "Paradev desktop app" msgstr "Paradev 데스크톱 앱" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev에 \"{name}\"을(를) 읽을 권한이 없습니다." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise는 팀을 위한 Paradev입니다. 어떤 기능이 포함되고 조직에 어떻게 적용할 수 있는지 문의해 주세요." diff --git a/packages/i18n/locales/nl/messages.po b/packages/i18n/locales/nl/messages.po index ced26006599..14c47d72c99 100644 --- a/packages/i18n/locales/nl/messages.po +++ b/packages/i18n/locales/nl/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" wordt niet meer geactiveerd en de geschiedenis van runs wordt ve msgid "\"{name}\" deleted" msgstr "\"{name}\" verwijderd" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" is een map. Voeg in plaats daarvan de bestanden in de map toe." + msgid "\"{name}\" paused" msgstr "\"{name}\" gepauzeerd" msgid "\"{name}\" resumed" msgstr "\"{name}\" hervat" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" is verplaatst of verwijderd. Voeg het opnieuw toe." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" is klaar met de taak" @@ -13506,6 +13512,9 @@ msgstr "Paradev Design System" msgid "Paradev desktop app" msgstr "Paradev-desktopapp" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev heeft geen toestemming om \"{name}\" te lezen." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise is Paradev voor teams. Neem contact op om te horen wat erbij zit en hoe het voor jouw organisatie kan werken." diff --git a/packages/i18n/locales/pl/messages.po b/packages/i18n/locales/pl/messages.po index 5c728e8c944..28f1eff5c7b 100644 --- a/packages/i18n/locales/pl/messages.po +++ b/packages/i18n/locales/pl/messages.po @@ -65,12 +65,18 @@ msgstr "„{0}” przestanie się uruchamiać, a jej historia uruchomień zostan msgid "\"{name}\" deleted" msgstr "„{name}” usunięta" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "„{name}” to folder. Zamiast tego załącz pliki z jego wnętrza." + msgid "\"{name}\" paused" msgstr "„{name}” wstrzymana" msgid "\"{name}\" resumed" msgstr "„{name}” wznowiona" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "„{name}” został przeniesiony lub usunięty. Załącz go ponownie." + msgid "\"{title}\" has finished its task" msgstr "„{title}” zakończył swoje zadanie" @@ -13506,6 +13512,9 @@ msgstr "System projektowy Paradev" msgid "Paradev desktop app" msgstr "Aplikacja desktopowa Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev nie ma uprawnień do odczytu „{name}”." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise to Paradev dla zespołów. Odezwij się, aby dowiedzieć się, co obejmuje i jak może zadziałać w Twojej organizacji." diff --git a/packages/i18n/locales/pt-BR/messages.po b/packages/i18n/locales/pt-BR/messages.po index 29d2d9049ba..748c213a4b2 100644 --- a/packages/i18n/locales/pt-BR/messages.po +++ b/packages/i18n/locales/pt-BR/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" vai parar de disparar e seu histórico de execuções será remo msgid "\"{name}\" deleted" msgstr "\"{name}\" excluída" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" é uma pasta. Anexe os arquivos que estão nela." + msgid "\"{name}\" paused" msgstr "\"{name}\" pausada" msgid "\"{name}\" resumed" msgstr "\"{name}\" retomada" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" foi movido ou excluído. Anexe-o novamente." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" terminou a tarefa" @@ -13506,6 +13512,9 @@ msgstr "Design System do Paradev" msgid "Paradev desktop app" msgstr "App para desktop do Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "O Paradev não tem permissão para ler \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "O Paradev Enterprise é o Paradev para times. Fale com a gente para saber o que está incluído e como ele pode funcionar na sua organização." diff --git a/packages/i18n/locales/ru/messages.po b/packages/i18n/locales/ru/messages.po index e6cff00804e..3bd5f03f4a3 100644 --- a/packages/i18n/locales/ru/messages.po +++ b/packages/i18n/locales/ru/messages.po @@ -65,12 +65,18 @@ msgstr "«{0}» перестанет срабатывать, а история msgid "\"{name}\" deleted" msgstr "«{name}» удалена" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "«{name}» — это папка. Прикрепите файлы из неё." + msgid "\"{name}\" paused" msgstr "«{name}» приостановлена" msgid "\"{name}\" resumed" msgstr "«{name}» возобновлена" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "«{name}» перемещён или удалён. Прикрепите его снова." + msgid "\"{title}\" has finished its task" msgstr "«{title}» завершил задачу" @@ -13506,6 +13512,9 @@ msgstr "Дизайн-система Paradev" msgid "Paradev desktop app" msgstr "Десктопное приложение Paradev" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "У Paradev нет разрешения на чтение «{name}»." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise — это Paradev для команд. Напишите нам, чтобы узнать, что входит и как это подойдёт вашей организации." diff --git a/packages/i18n/locales/tr/messages.po b/packages/i18n/locales/tr/messages.po index f57b145fd64..e4d14b6f868 100644 --- a/packages/i18n/locales/tr/messages.po +++ b/packages/i18n/locales/tr/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" artık tetiklenmeyecek ve çalıştırma geçmişi kaldırılaca msgid "\"{name}\" deleted" msgstr "\"{name}\" silindi" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" bir klasör. Bunun yerine içindeki dosyaları ekle." + msgid "\"{name}\" paused" msgstr "\"{name}\" duraklatıldı" msgid "\"{name}\" resumed" msgstr "\"{name}\" sürdürüldü" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" taşındı veya silindi. Yeniden ekle." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" görevini tamamladı" @@ -13506,6 +13512,9 @@ msgstr "Paradev Tasarım Sistemi" msgid "Paradev desktop app" msgstr "Paradev masaüstü uygulaması" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev'in \"{name}\" dosyasını okuma izni yok." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise, takımlar için Paradev'dir. Nelerin dahil olduğunu ve kuruluşunuzda nasıl işleyebileceğini öğrenmek için bize ulaşın." diff --git a/packages/i18n/locales/vi/messages.po b/packages/i18n/locales/vi/messages.po index 6ea8cb35a8a..969f3648da4 100644 --- a/packages/i18n/locales/vi/messages.po +++ b/packages/i18n/locales/vi/messages.po @@ -65,12 +65,18 @@ msgstr "\"{0}\" sẽ ngừng kích hoạt và lịch sử chạy sẽ bị xóa. msgid "\"{name}\" deleted" msgstr "Đã xóa \"{name}\"" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "\"{name}\" là một thư mục. Hãy đính kèm các tệp bên trong thay thế." + msgid "\"{name}\" paused" msgstr "Đã tạm dừng \"{name}\"" msgid "\"{name}\" resumed" msgstr "Đã tiếp tục \"{name}\"" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "\"{name}\" đã bị di chuyển hoặc xóa. Hãy đính kèm lại." + msgid "\"{title}\" has finished its task" msgstr "\"{title}\" đã hoàn thành tác vụ" @@ -13506,6 +13512,9 @@ msgstr "Hệ thống thiết kế Paradev" msgid "Paradev desktop app" msgstr "Ứng dụng Paradev cho máy tính" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev không có quyền đọc \"{name}\"." + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise là Paradev dành cho đội nhóm. Hãy liên hệ để biết thêm về những gì được bao gồm và cách nó phù hợp với tổ chức của bạn." diff --git a/packages/i18n/locales/zh-CN/messages.po b/packages/i18n/locales/zh-CN/messages.po index db61172ef89..fbe8e7caec6 100644 --- a/packages/i18n/locales/zh-CN/messages.po +++ b/packages/i18n/locales/zh-CN/messages.po @@ -65,12 +65,18 @@ msgstr "“{0}”将不再触发,其运行历史也会被移除。此操作无 msgid "\"{name}\" deleted" msgstr "“{name}”已删除" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "“{name}”是一个文件夹。请改为附加其中的文件。" + msgid "\"{name}\" paused" msgstr "“{name}”已暂停" msgid "\"{name}\" resumed" msgstr "“{name}”已恢复" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "“{name}”已被移动或删除。请重新附加。" + msgid "\"{title}\" has finished its task" msgstr "“{title}”已完成任务" @@ -13506,6 +13512,9 @@ msgstr "Paradev设计系统" msgid "Paradev desktop app" msgstr "Paradev 桌面应用" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev 没有读取“{name}”的权限。" + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise是面向团队的Paradev。联系我们,了解它包含什么,以及如何适配你的组织。" diff --git a/packages/i18n/locales/zh-TW/messages.po b/packages/i18n/locales/zh-TW/messages.po index 810fefdf10b..2ec2d0958c5 100644 --- a/packages/i18n/locales/zh-TW/messages.po +++ b/packages/i18n/locales/zh-TW/messages.po @@ -65,12 +65,18 @@ msgstr "「{0}」將不再觸發,其執行歷史也會被移除。此操作無 msgid "\"{name}\" deleted" msgstr "「{name}」已刪除" +msgid "\"{name}\" is a folder. Attach the files inside it instead." +msgstr "「{name}」是資料夾。請改為附加其中的檔案。" + msgid "\"{name}\" paused" msgstr "「{name}」已暫停" msgid "\"{name}\" resumed" msgstr "「{name}」已恢復" +msgid "\"{name}\" was moved or deleted. Attach it again." +msgstr "「{name}」已被移動或刪除。請重新附加。" + msgid "\"{title}\" has finished its task" msgstr "「{title}」已完成任務" @@ -13506,6 +13512,9 @@ msgstr "Paradev設計系統" msgid "Paradev desktop app" msgstr "Paradev 桌面 App" +msgid "Paradev doesn't have permission to read \"{name}\"." +msgstr "Paradev 沒有讀取「{name}」的權限。" + msgid "Paradev Enterprise is Paradev for teams. Reach out to learn more about what's included and how it can work for your organization." msgstr "Paradev Enterprise是面向團隊的Paradev。聯絡我們,瞭解它包含什麼,以及如何適配你的組織。" From 9c81d7e093e31803e9b19303774a16bc1e69d1c5 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 21:17:21 +0000 Subject: [PATCH 08/16] feat(host-service): sweep the attachment store by last use, and keep fileId on cloud copies Part of the attachments plan (PR 3: seams 8, 2, and the fileId carry-through of seam 3). The host's attachment store had no retention. Nothing deletes chat.db rows and a terminal launch has no session, so there is no session delete to hook. Instead, metadata.json records lastUsedAt. Upload and import set it, and every path that hands an attachment out sets it again: read, agents.run's attached files block, and the ACP resolver. A daily sweep (not in sandboxes) removes entries unused for ATTACHMENT_RETENTION_DAYS. An entry written before this change is stamped, not removed. The check and the rename that takes the entry out of the store run in the same tick, so a hand-out on this event loop cannot land between them. The slow delete runs async, off the loop. A box's cached copy now keeps the fileId it was imported from, in its metadata and in a small .by-file-id index, so the adapters (next PR) can find a cloud part's cached copy without a scan. The first prompt of a cloud launch spreads that metadata into its parts. Without fileId, the first attachments of a cloud session would be the one kind a reader without the box cannot render. MAX_INLINE_ATTACHMENT_BYTES moves to @superset/shared/attachment-limits with MAX_THUMBNAIL_BYTES and ATTACHMENT_RETENTION_DAYS. The host and the shared launch schema use MAX_ATTACHMENTS for their max(10). --- packages/chat/src/protocol/items.ts | 3 + packages/host-service/src/app.ts | 18 +++ .../host-service/src/chat-v3/acpHarnesses.ts | 4 +- .../src/trpc/router/agents/agents.test.ts | 113 ++++++++++++++++++ .../src/trpc/router/agents/agents.ts | 29 +++-- .../router/attachments/attachments.test.ts | 58 ++++++++- .../trpc/router/attachments/attachments.ts | 35 ++++-- .../src/trpc/router/attachments/constants.ts | 8 -- .../src/trpc/router/attachments/index.ts | 2 +- .../src/trpc/router/attachments/storage.ts | 71 ++++++++++- .../src/trpc/router/attachments/sweep.test.ts | 103 ++++++++++++++++ .../src/trpc/router/attachments/sweep.ts | 79 ++++++++++++ .../router/sessions/create-chat-session.ts | 3 +- packages/shared/src/attachment-limits.ts | 14 +++ packages/shared/src/cloud-agent-launch.ts | 3 +- 15 files changed, 510 insertions(+), 33 deletions(-) delete mode 100644 packages/host-service/src/trpc/router/attachments/constants.ts create mode 100644 packages/host-service/src/trpc/router/attachments/sweep.test.ts create mode 100644 packages/host-service/src/trpc/router/attachments/sweep.ts diff --git a/packages/chat/src/protocol/items.ts b/packages/chat/src/protocol/items.ts index e0ea3b3ebca..f6cbf649356 100644 --- a/packages/chat/src/protocol/items.ts +++ b/packages/chat/src/protocol/items.ts @@ -29,8 +29,11 @@ export const userContentSchema = z.discriminatedUnion("type", [ z.looseObject({ type: z.literal("attachment"), attachmentId: z.string().min(1), + /** The storage object behind a cloud workspace's attachment; set, it is the identity. */ + fileId: z.string().uuid().optional(), name: z.string(), mimeType: z.string(), + sizeBytes: z.number().int().nonnegative().optional(), }), ]); export type UserContent = z.infer; diff --git a/packages/host-service/src/app.ts b/packages/host-service/src/app.ts index 7c5bb29e106..d4c7a0a40ad 100644 --- a/packages/host-service/src/app.ts +++ b/packages/host-service/src/app.ts @@ -61,6 +61,10 @@ import { } from "./terminal-agents"; import { matchesAgentBinding } from "./terminal-agents/matches-agent-binding"; import { appRouter } from "./trpc/router"; +import { + ATTACHMENT_SWEEP_INTERVAL_MS, + sweepAttachmentStore, +} from "./trpc/router/attachments/sweep"; import { gitStatusStore } from "./trpc/router/git/utils/git-status-store"; import { disposeLocalMobileDevices, @@ -397,6 +401,18 @@ export function createApp(options: CreateAppOptions): CreateAppResult { ? null : setInterval(() => void runProjectPurge(), PROJECT_PURGE_INTERVAL_MS); projectPurgeTimer?.unref?.(); + const runAttachmentSweep = () => + sweepAttachmentStore().catch((err) => { + console.warn("[host-service] attachment sweep failed:", err); + }); + const attachmentSweepTimer = + process.env.SUPERSET_HOST_RUN_MODE === "sandbox" + ? null + : setInterval( + () => void runAttachmentSweep(), + ATTACHMENT_SWEEP_INTERVAL_MS, + ); + attachmentSweepTimer?.unref?.(); if (process.env.SUPERSET_HOST_RUN_MODE !== "sandbox") { startLocalMobileDeviceCleanup(db); } @@ -427,6 +443,7 @@ export function createApp(options: CreateAppOptions): CreateAppResult { console.warn("[host-service] archived-workspace reconcile failed:", err); }); await runProjectPurge(); + await runAttachmentSweep(); // Re-share the default account's Claude/Codex config into the selected // provider profiles. Last: it touches no host state the sweeps above // repair, and a slow filesystem must not delay them. @@ -572,6 +589,7 @@ export function createApp(options: CreateAppOptions): CreateAppResult { const dispose = async (): Promise => { agentJobs.stopAll(); if (projectPurgeTimer) clearInterval(projectPurgeTimer); + if (attachmentSweepTimer) clearInterval(attachmentSweepTimer); await disposeWorkspaceTitleJobs(db); // Each step is best-effort and isolated: a throw in one cleanup must // not skip the others, otherwise a flaky `.stop()` could leak the diff --git a/packages/host-service/src/chat-v3/acpHarnesses.ts b/packages/host-service/src/chat-v3/acpHarnesses.ts index 90ff41fea6b..107333cffda 100644 --- a/packages/host-service/src/chat-v3/acpHarnesses.ts +++ b/packages/host-service/src/chat-v3/acpHarnesses.ts @@ -4,7 +4,7 @@ import { dirname, join } from "node:path"; import type { HarnessFactory } from "@superset/chat-runtime"; import { createAcpAdapter } from "@superset/chat-runtime"; import type { HostDb } from "../db"; -import { resolveAttachmentPath } from "../trpc/router/attachments/storage"; +import { handOutAttachment } from "../trpc/router/attachments/storage"; import { captureSessionAccount } from "../trpc/router/usage/session-account/session-account"; import { ACP_HARNESSES } from "./acpCatalogue"; import { resolveAgentCli } from "./agentCli"; @@ -14,7 +14,7 @@ import { agentLoginCommand } from "./agentLogin"; import type { ChatAgentBridge } from "./chatAgentBridge"; async function resolveAttachment(attachmentId: string) { - const resolved = resolveAttachmentPath(attachmentId); + const resolved = handOutAttachment(attachmentId); if (!resolved) return null; return { path: resolved.path, diff --git a/packages/host-service/src/trpc/router/agents/agents.test.ts b/packages/host-service/src/trpc/router/agents/agents.test.ts index f350ed99bec..bc8c4016dc5 100644 --- a/packages/host-service/src/trpc/router/agents/agents.test.ts +++ b/packages/host-service/src/trpc/router/agents/agents.test.ts @@ -11,6 +11,12 @@ import { migrate } from "drizzle-orm/bun-sqlite/migrator"; import type { HostDb } from "../../../db"; import * as schema from "../../../db/schema"; import { TerminalAgentStore } from "../../../terminal-agents"; +import { importCloudAttachments } from "../attachments/attachments"; +import { + type AttachmentMetadata, + readAttachmentMetadata, + writeAttachment, +} from "../attachments/storage"; import { setDefaultAccountSelection } from "../usage/default-account"; import { bindResumedSession, @@ -1159,3 +1165,110 @@ describe("continuationTarget", () => { ).toEqual({ terminalId, label: "Team Claude" }); }); }); + +describe("attachments handed to an agent launch", () => { + const workspaceId = "11111111-1111-1111-1111-111111111111"; + let manifestDir: string; + + beforeEach(() => { + manifestDir = mkdtempSync(join(tmpdir(), "superset-agent-attachments-")); + process.env.HOST_MANIFEST_DIR = manifestDir; + }); + + afterEach(() => { + rmSync(manifestDir, { recursive: true, force: true }); + delete process.env.HOST_MANIFEST_DIR; + }); + + function seedClaude(db: HostDb) { + db.insert(schema.hostAgentConfigs) + .values({ + id: "00000000-0000-0000-0000-00000000000a", + presetId: "claude", + label: "Claude", + command: "claude", + argsJson: "[]", + promptTransport: "argv", + promptArgsJson: "[]", + resumeArgsJson: "[]", + forkArgsJson: "[]", + envJson: "{}", + displayOrder: 0, + }) + .run(); + } + + it("a cloud launch's first-prompt part carries the fileId it was imported from", async () => { + const server = Bun.serve({ + port: 0, + fetch: () => new Response("png"), + }); + try { + const fileId = crypto.randomUUID(); + const [imported] = await importCloudAttachments( + { + api: { + attachment: { + resolve: { + mutate: async () => [ + { + fileId, + name: "shot.png", + contentType: "image/png", + sizeBytes: 3, + url: server.url.href, + }, + ], + }, + }, + }, + }, + [fileId], + ); + const db = createTestDb(); + seedClaude(db); + + const target = chatLaunchTarget(db, { + workspaceId, + agent: "claude", + prompt: "look", + surface: "chat", + attachmentIds: [imported?.attachmentId ?? ""], + }); + expect(target?.attachments).toEqual([ + { + attachmentId: imported?.attachmentId ?? "", + fileId, + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, + }, + ]); + } finally { + server.stop(true); + } + }); + + it("a terminal launch stamps the attachments it lists as used", () => { + const metadata: AttachmentMetadata = { + attachmentId: crypto.randomUUID(), + mediaType: "image/png", + sizeBytes: 1, + createdAt: 0, + lastUsedAt: 0, + }; + writeAttachment(new Uint8Array([1]), metadata); + const db = createTestDb(); + seedClaude(db); + + buildTerminalAgentLaunch(db, { + workspaceId, + agent: "claude", + prompt: "look", + attachmentIds: [metadata.attachmentId], + }); + expect( + readAttachmentMetadata(metadata.attachmentId)?.lastUsedAt, + ).toBeGreaterThan(0); + }); +}); diff --git a/packages/host-service/src/trpc/router/agents/agents.ts b/packages/host-service/src/trpc/router/agents/agents.ts index 02f35e563a4..ac7ed042e49 100644 --- a/packages/host-service/src/trpc/router/agents/agents.ts +++ b/packages/host-service/src/trpc/router/agents/agents.ts @@ -44,7 +44,10 @@ import { import { hasHarnessSession } from "../../../terminal-agents/harness-sessions"; import type { HostServiceContext } from "../../../types"; import { protectedProcedure, router } from "../../index"; -import { resolveAttachmentPath } from "../attachments/storage"; +import { + handOutAttachment, + resolveAttachmentPath, +} from "../attachments/storage"; import { createChatSession } from "../sessions/create-chat-session"; import { toTerminalSessionError } from "../terminal/errors"; import { seedAgentFolderTrust } from "../workspace-creation/shared/seed-agent-trust"; @@ -425,7 +428,7 @@ export function buildTerminalAgentLaunch( const resolvedAttachments: Array<{ attachmentId: string; path: string }> = []; for (const attachmentId of input.attachmentIds ?? []) { - const resolved = resolveAttachmentPath(attachmentId); + const resolved = handOutAttachment(attachmentId); if (!resolved) { throw new TRPCError({ code: "NOT_FOUND", @@ -635,13 +638,25 @@ function bindingRunsConfig( return binding.agentId === launchIdentity(config); } +/** + * An imported copy keeps its `fileId` next to the host id: the adapters read + * `attachmentId` today, and `fileId` is what renders the part without this box. + */ +export interface ChatLaunchAttachment { + attachmentId: string; + fileId?: string; + name: string; + mimeType: string; + sizeBytes: number; +} + export function chatLaunchTarget( db: HostDb, input: AgentRunInput, ): { harness: string; label: string; - attachments: Array<{ attachmentId: string; name: string; mimeType: string }>; + attachments: ChatLaunchAttachment[]; } | null { if ( input.surface !== "chat" || @@ -651,18 +666,16 @@ export function chatLaunchTarget( ) { return null; } - const attachments: Array<{ - attachmentId: string; - name: string; - mimeType: string; - }> = []; + const attachments: ChatLaunchAttachment[] = []; for (const attachmentId of input.attachmentIds ?? []) { const resolved = resolveAttachmentPath(attachmentId); if (!resolved) return null; attachments.push({ attachmentId, + ...(resolved.metadata.fileId ? { fileId: resolved.metadata.fileId } : {}), name: resolved.metadata.originalFilename ?? attachmentId, mimeType: resolved.metadata.mediaType, + sizeBytes: resolved.metadata.sizeBytes, }); } const config = resolveHostAgentConfig(db, input.agent); diff --git a/packages/host-service/src/trpc/router/attachments/attachments.test.ts b/packages/host-service/src/trpc/router/attachments/attachments.test.ts index 5189805194c..30b5fb0c388 100644 --- a/packages/host-service/src/trpc/router/attachments/attachments.test.ts +++ b/packages/host-service/src/trpc/router/attachments/attachments.test.ts @@ -3,13 +3,16 @@ import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { resolveAppHomeDir } from "@superset/agent-setup/paths"; +import { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; import type { HostServiceContext } from "../../../types"; -import { attachmentsRouter } from "./attachments"; -import { MAX_INLINE_ATTACHMENT_BYTES } from "./constants"; +import { attachmentsRouter, importCloudAttachments } from "./attachments"; import { getAttachmentDir, getAttachmentFilePath, getAttachmentsRoot, + handOutAttachmentByFileId, + readAttachmentMetadata, + writeAttachmentMetadata, } from "./storage"; let tempBase: string; @@ -188,6 +191,20 @@ describe("attachmentsRouter.read", () => { }); }); + it("stamps the attachment as used", async () => { + const caller = createCaller(); + const { attachmentId } = await caller.upload({ + data: { kind: "base64", data: PNG_BASE64 }, + mediaType: "image/png", + }); + const metadata = readAttachmentMetadata(attachmentId); + if (!metadata) throw new Error("upload wrote no metadata"); + writeAttachmentMetadata({ ...metadata, lastUsedAt: 0 }); + + await caller.read({ attachmentId }); + expect(readAttachmentMetadata(attachmentId)?.lastUsedAt).toBeGreaterThan(0); + }); + it("reports a missing attachment as NOT_FOUND", async () => { await expect( createCaller().read({ @@ -197,6 +214,43 @@ describe("attachmentsRouter.read", () => { }); }); +describe("importCloudAttachments", () => { + it("records the fileId a copy came from, so a lookup by fileId finds it", async () => { + const server = Bun.serve({ + port: 0, + fetch: () => new Response(Buffer.from(PNG_BASE64, "base64")), + }); + try { + const fileId = crypto.randomUUID(); + const ctx = { + api: { + attachment: { + resolve: { + mutate: async () => [ + { + fileId, + name: "shot.png", + contentType: "image/png", + sizeBytes: 68, + url: server.url.href, + }, + ], + }, + }, + }, + }; + + const [imported] = await importCloudAttachments(ctx, [fileId]); + expect(imported?.fileId).toBe(fileId); + expect(handOutAttachmentByFileId(fileId)?.metadata.attachmentId).toBe( + imported?.attachmentId, + ); + } finally { + server.stop(true); + } + }); +}); + describe("attachmentsRouter.delete", () => { it("removes the attachment directory", async () => { const caller = createCaller(); diff --git a/packages/host-service/src/trpc/router/attachments/attachments.ts b/packages/host-service/src/trpc/router/attachments/attachments.ts index 8b6bfa6d5f5..6fb1dcd8f97 100644 --- a/packages/host-service/src/trpc/router/attachments/attachments.ts +++ b/packages/host-service/src/trpc/router/attachments/attachments.ts @@ -2,6 +2,10 @@ import { randomUUID } from "node:crypto"; import { closeSync, openSync } from "node:fs"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; +import { + MAX_ATTACHMENTS, + MAX_INLINE_ATTACHMENT_BYTES, +} from "@superset/shared/attachment-limits"; import { assignAttachmentFileName, attachmentNameWithSuffix, @@ -13,13 +17,13 @@ import mimeTypes from "mime-types"; import { z } from "zod"; import { protectedProcedure, router } from "../../index"; import { resolveWorktreePath } from "../git/utils/resolve-worktree"; -import { MAX_INLINE_ATTACHMENT_BYTES } from "./constants"; import { type CloudAttachment, downloadAttachment } from "./download"; import { type AttachmentMetadata, deleteAttachment, + handOutAttachment, + indexAttachmentFileId, prepareAttachmentTarget, - resolveAttachmentPath, writeAttachment, writeAttachmentMetadata, } from "./storage"; @@ -129,12 +133,14 @@ export const attachmentsRouter = router({ }); } + const now = Date.now(); const metadata: AttachmentMetadata = { attachmentId: randomUUID(), mediaType, originalFilename: input.originalFilename, sizeBytes: bytes.length, - createdAt: Date.now(), + createdAt: now, + lastUsedAt: now, }; // Buffer already extends Uint8Array; no need to wrap. @@ -156,7 +162,11 @@ export const attachmentsRouter = router({ * an attachment can legitimately be. */ importFromCloud: protectedProcedure - .input(z.object({ fileIds: z.array(z.string().uuid()).min(1).max(10) })) + .input( + z.object({ + fileIds: z.array(z.string().uuid()).min(1).max(MAX_ATTACHMENTS), + }), + ) .mutation(({ ctx, input }) => importCloudAttachments(ctx, input.fileIds)), /** @@ -168,7 +178,7 @@ export const attachmentsRouter = router({ .input( z.object({ workspaceId: z.string(), - fileIds: z.array(z.string().uuid()).min(1).max(10), + fileIds: z.array(z.string().uuid()).min(1).max(MAX_ATTACHMENTS), }), ) .mutation(async ({ ctx, input }) => { @@ -201,7 +211,7 @@ export const attachmentsRouter = router({ read: protectedProcedure .input(z.object({ attachmentId: z.string().uuid() })) .query(async ({ input }) => { - const resolved = resolveAttachmentPath(input.attachmentId); + const resolved = handOutAttachment(input.attachmentId); if (!resolved) { throw new TRPCError({ code: "NOT_FOUND", @@ -236,6 +246,8 @@ export const attachmentsRouter = router({ export type AttachmentUploadResult = { attachmentId: string; + /** Set when the bytes came from cloud storage: the identity a reader without this host resolves. */ + fileId?: string; originalFilename?: string; mediaType: string; sizeBytes: number; @@ -261,17 +273,22 @@ export async function importCloudAttachments( const resolved = await ctx.api.attachment.resolve.mutate({ fileIds }); const results: AttachmentUploadResult[] = []; for (const attachment of resolved) { - const metadata: AttachmentMetadata = { + const now = Date.now(); + const metadata = { attachmentId: randomUUID(), + fileId: attachment.fileId, mediaType: mediaTypeOf(attachment.contentType), originalFilename: attachment.name, sizeBytes: attachment.sizeBytes, - createdAt: Date.now(), - }; + createdAt: now, + lastUsedAt: now, + } satisfies AttachmentMetadata; await downloadAttachment(attachment, prepareAttachmentTarget(metadata)); writeAttachmentMetadata(metadata); + indexAttachmentFileId(metadata); results.push({ attachmentId: metadata.attachmentId, + fileId: metadata.fileId, originalFilename: metadata.originalFilename, mediaType: metadata.mediaType, sizeBytes: metadata.sizeBytes, diff --git a/packages/host-service/src/trpc/router/attachments/constants.ts b/packages/host-service/src/trpc/router/attachments/constants.ts deleted file mode 100644 index 7e0f02a29c2..00000000000 --- a/packages/host-service/src/trpc/router/attachments/constants.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Per-file cap on the inline upload — bytes sent base64 in the request body, - * so the ceiling is really the transport's rather than a product decision. - * `importFromCloud` has no equivalent limit here: those bytes arrive from - * cloud storage, and what a composer may send is decided by - * `@superset/shared/attachment-limits`. - */ -export const MAX_INLINE_ATTACHMENT_BYTES = 25 * 1024 * 1024; diff --git a/packages/host-service/src/trpc/router/attachments/index.ts b/packages/host-service/src/trpc/router/attachments/index.ts index 26fbefbf2f2..9cccede2c59 100644 --- a/packages/host-service/src/trpc/router/attachments/index.ts +++ b/packages/host-service/src/trpc/router/attachments/index.ts @@ -1,3 +1,3 @@ +export { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; export type { AttachmentUploadResult } from "./attachments"; export { attachmentsRouter, claimFileName } from "./attachments"; -export { MAX_INLINE_ATTACHMENT_BYTES } from "./constants"; diff --git a/packages/host-service/src/trpc/router/attachments/storage.ts b/packages/host-service/src/trpc/router/attachments/storage.ts index e2431ff3350..17ea410a4c8 100644 --- a/packages/host-service/src/trpc/router/attachments/storage.ts +++ b/packages/host-service/src/trpc/router/attachments/storage.ts @@ -3,9 +3,10 @@ import { mkdirSync, readFileSync, rmSync, + unlinkSync, writeFileSync, } from "node:fs"; -import { join } from "node:path"; +import { dirname, join } from "node:path"; import { resolveAppHomeDir } from "@superset/agent-setup/paths"; import mimeTypes from "mime-types"; @@ -15,8 +16,15 @@ export interface AttachmentMetadata { originalFilename?: string; sizeBytes: number; createdAt: number; + /** The storage object a box's cached copy was imported from. */ + fileId?: string; + /** Absent on an entry written before the store was swept. */ + lastUsedAt?: number; } +/** Inside the store root, so a sweep must skip it. */ +export const FILE_ID_INDEX_DIR = ".by-file-id"; + /** * Resolves the per-org attachment storage root. Honors * `HOST_MANIFEST_DIR` (set by host-service-coordinator with the active @@ -113,6 +121,8 @@ export function deleteAttachment( attachmentId: string, baseDirOverride?: string, ): void { + const metadata = readAttachmentMetadata(attachmentId, baseDirOverride); + if (metadata) unindexAttachmentFileId(metadata, baseDirOverride); const dir = getAttachmentDir(attachmentId, baseDirOverride); rmSync(dir, { recursive: true, force: true }); } @@ -148,3 +158,62 @@ export function resolveAttachmentPath( ); return existsSync(path) ? { path, metadata } : null; } + +/** Every path that hands an attachment out goes through here, so the sweep sees it as used. */ +export function handOutAttachment( + attachmentId: string, + baseDirOverride?: string, +): { path: string; metadata: AttachmentMetadata } | null { + const resolved = resolveAttachmentPath(attachmentId, baseDirOverride); + if (!resolved) return null; + const metadata = { ...resolved.metadata, lastUsedAt: Date.now() }; + writeAttachmentMetadata(metadata, baseDirOverride); + return { path: resolved.path, metadata }; +} + +const FILE_ID_PATTERN = /^[0-9a-f-]{36}$/i; + +function fileIdIndexPath(fileId: string, baseDirOverride?: string): string { + return join(getAttachmentsRoot(baseDirOverride), FILE_ID_INDEX_DIR, fileId); +} + +export function indexAttachmentFileId( + metadata: AttachmentMetadata & { fileId: string }, + baseDirOverride?: string, +): void { + const path = fileIdIndexPath(metadata.fileId, baseDirOverride); + mkdirSync(dirname(path), { recursive: true, mode: 0o700 }); + writeFileSync(path, metadata.attachmentId, { mode: 0o600 }); +} + +export function unindexAttachmentFileId( + metadata: AttachmentMetadata, + baseDirOverride?: string, +): void { + if (!metadata.fileId) return; + const path = fileIdIndexPath(metadata.fileId, baseDirOverride); + if (readIndexedAttachmentId(path) === metadata.attachmentId) unlinkSync(path); +} + +function readIndexedAttachmentId(path: string): string | null { + try { + return readFileSync(path, "utf-8"); + } catch { + return null; + } +} + +/** The cached copy of a storage object, as `handOutAttachment` hands it out. */ +export function handOutAttachmentByFileId( + fileId: string, + baseDirOverride?: string, +): { path: string; metadata: AttachmentMetadata } | null { + if (!FILE_ID_PATTERN.test(fileId)) return null; + const attachmentId = readIndexedAttachmentId( + fileIdIndexPath(fileId, baseDirOverride), + ); + if (!attachmentId) return null; + const metadata = readAttachmentMetadata(attachmentId, baseDirOverride); + if (metadata?.fileId !== fileId) return null; + return handOutAttachment(attachmentId, baseDirOverride); +} diff --git a/packages/host-service/src/trpc/router/attachments/sweep.test.ts b/packages/host-service/src/trpc/router/attachments/sweep.test.ts new file mode 100644 index 00000000000..628a1e94d88 --- /dev/null +++ b/packages/host-service/src/trpc/router/attachments/sweep.test.ts @@ -0,0 +1,103 @@ +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; +import { existsSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + type AttachmentMetadata, + getAttachmentDir, + handOutAttachment, + handOutAttachmentByFileId, + indexAttachmentFileId, + readAttachmentMetadata, + writeAttachment, +} from "./storage"; +import { sweepAttachmentStore } from "./sweep"; + +const DAY_MS = 24 * 60 * 60 * 1000; +const NOW = Date.now() - 365 * DAY_MS; + +let base: string; + +beforeEach(() => { + base = mkdtempSync(join(tmpdir(), "superset-attachment-sweep-")); +}); + +afterEach(() => { + rmSync(base, { recursive: true, force: true }); +}); + +function seed(fields: Partial = {}): AttachmentMetadata { + const metadata: AttachmentMetadata = { + attachmentId: crypto.randomUUID(), + mediaType: "image/png", + sizeBytes: 1, + createdAt: NOW - 90 * DAY_MS, + ...fields, + }; + writeAttachment(new Uint8Array([1]), metadata, base); + return metadata; +} + +const sweep = () => + sweepAttachmentStore({ now: NOW, retentionDays: 30, baseDirOverride: base }); + +describe("sweepAttachmentStore", () => { + it("removes an entry unused past retention and keeps a recent one", async () => { + const stale = seed({ lastUsedAt: NOW - 31 * DAY_MS }); + const recent = seed({ lastUsedAt: NOW - 29 * DAY_MS }); + + expect((await sweep()).removed).toEqual([stale.attachmentId]); + expect(existsSync(getAttachmentDir(stale.attachmentId, base))).toBe(false); + expect(existsSync(getAttachmentDir(recent.attachmentId, base))).toBe(true); + }); + + it("stamps an entry from before lastUsedAt instead of removing it", async () => { + const unstamped = seed(); + + expect((await sweep()).removed).toEqual([]); + expect( + readAttachmentMetadata(unstamped.attachmentId, base)?.lastUsedAt, + ).toBe(NOW); + }); + + it("keeps an entry handed out after the pass started", async () => { + const stale = seed({ lastUsedAt: NOW - 31 * DAY_MS }); + + const pass = sweep(); + expect(handOutAttachment(stale.attachmentId, base)).not.toBeNull(); + + expect((await pass).removed).toEqual([]); + expect(existsSync(getAttachmentDir(stale.attachmentId, base))).toBe(true); + }); + + it("drops the fileId lookup with the entry it pointed to", async () => { + const fileId = crypto.randomUUID(); + const stale = seed({ fileId, lastUsedAt: NOW - 31 * DAY_MS }); + indexAttachmentFileId({ ...stale, fileId }, base); + + await sweep(); + expect(handOutAttachmentByFileId(fileId, base)).toBeNull(); + }); +}); + +describe("handOutAttachment", () => { + it("stamps the entry it hands out", () => { + const entry = seed({ lastUsedAt: NOW - 31 * DAY_MS }); + const handed = handOutAttachment(entry.attachmentId, base); + expect(handed?.metadata.lastUsedAt).toBeGreaterThan(NOW); + expect( + readAttachmentMetadata(entry.attachmentId, base)?.lastUsedAt, + ).toBeGreaterThan(NOW); + }); + + it("finds a cloud copy by its fileId and stamps it", () => { + const fileId = crypto.randomUUID(); + const entry = seed({ fileId, lastUsedAt: NOW - 31 * DAY_MS }); + indexAttachmentFileId({ ...entry, fileId }, base); + + const handed = handOutAttachmentByFileId(fileId, base); + expect(handed?.metadata.attachmentId).toBe(entry.attachmentId); + expect(handed?.metadata.lastUsedAt).toBeGreaterThan(NOW); + expect(handOutAttachmentByFileId(crypto.randomUUID(), base)).toBeNull(); + }); +}); diff --git a/packages/host-service/src/trpc/router/attachments/sweep.ts b/packages/host-service/src/trpc/router/attachments/sweep.ts new file mode 100644 index 00000000000..946934e1eac --- /dev/null +++ b/packages/host-service/src/trpc/router/attachments/sweep.ts @@ -0,0 +1,79 @@ +import { renameSync } from "node:fs"; +import { readdir, rm } from "node:fs/promises"; +import { join } from "node:path"; +import { ATTACHMENT_RETENTION_DAYS } from "@superset/shared/attachment-limits"; +import { + FILE_ID_INDEX_DIR, + getAttachmentsRoot, + readAttachmentMetadata, + unindexAttachmentFileId, + writeAttachmentMetadata, +} from "./storage"; + +const DAY_MS = 24 * 60 * 60 * 1000; +const SWEEPING_PREFIX = ".sweeping-"; + +export const ATTACHMENT_SWEEP_INTERVAL_MS = DAY_MS; + +/** + * Removes store entries nobody has used for `retentionDays`. An entry from + * before `lastUsedAt` existed is stamped now instead, so it gets the full + * retention from the first sweep. + */ +export async function sweepAttachmentStore({ + now = Date.now(), + retentionDays = ATTACHMENT_RETENTION_DAYS, + baseDirOverride, +}: { + now?: number; + retentionDays?: number; + baseDirOverride?: string; +} = {}): Promise<{ removed: string[] }> { + const root = getAttachmentsRoot(baseDirOverride); + const entries = await readdir(root).catch(() => [] as string[]); + const removed: string[] = []; + for (const entry of entries) { + if (entry.startsWith(SWEEPING_PREFIX)) { + await rm(join(root, entry), { recursive: true, force: true }); + continue; + } + if (entry === FILE_ID_INDEX_DIR) continue; + const doomed = claimIfUnused(entry, { + cutoff: now - retentionDays * DAY_MS, + now, + root, + baseDirOverride, + }); + if (!doomed) continue; + await rm(doomed, { recursive: true, force: true }); + removed.push(entry); + } + return { removed }; +} + +/** + * Synchronous from the metadata read to the rename, so a hand-out on this + * event loop can't run between the check and the removal. Once renamed, the + * entry is unreachable by id, and the slow delete can run async. + */ +function claimIfUnused( + attachmentId: string, + { + cutoff, + now, + root, + baseDirOverride, + }: { cutoff: number; now: number; root: string; baseDirOverride?: string }, +): string | null { + const metadata = readAttachmentMetadata(attachmentId, baseDirOverride); + if (!metadata) return null; + if (metadata.lastUsedAt === undefined) { + writeAttachmentMetadata({ ...metadata, lastUsedAt: now }, baseDirOverride); + return null; + } + if (metadata.lastUsedAt >= cutoff) return null; + unindexAttachmentFileId(metadata, baseDirOverride); + const doomed = join(root, `${SWEEPING_PREFIX}${attachmentId}`); + renameSync(join(root, attachmentId), doomed); + return doomed; +} diff --git a/packages/host-service/src/trpc/router/sessions/create-chat-session.ts b/packages/host-service/src/trpc/router/sessions/create-chat-session.ts index cbe0d58e036..ff73061bc0f 100644 --- a/packages/host-service/src/trpc/router/sessions/create-chat-session.ts +++ b/packages/host-service/src/trpc/router/sessions/create-chat-session.ts @@ -1,4 +1,5 @@ import type { ChatRuntime } from "@superset/chat-runtime"; +import type { ChatLaunchAttachment } from "../agents/agents"; import type { ChatSessionResult, CreateChatSessionInput, @@ -7,7 +8,7 @@ import type { type ChatTarget = { harness: string; label: string; - attachments: Array<{ attachmentId: string; name: string; mimeType: string }>; + attachments: ChatLaunchAttachment[]; }; export function createChatSession( diff --git a/packages/shared/src/attachment-limits.ts b/packages/shared/src/attachment-limits.ts index c5e18dad369..f24fa3c47d0 100644 --- a/packages/shared/src/attachment-limits.ts +++ b/packages/shared/src/attachment-limits.ts @@ -16,3 +16,17 @@ export const MAX_CHAT_IMAGE_BYTES = 50 * 1024 * 1024; /** Per send, matching what the desktop terminal composer accepts. */ export const MAX_ATTACHMENTS = 10; + +/** + * Per file, for bytes sent base64 in a request body to a host, and read back + * the same way. The ceiling is the transport's: the host holds the body in + * memory. A workspace on a machine someone owns refuses above it, because its + * attachments must not go through our storage. + */ +export const MAX_INLINE_ATTACHMENT_BYTES = 25 * 1024 * 1024; + +/** Largest image a transcript renders inline; anything larger is a name badge. */ +export const MAX_THUMBNAIL_BYTES = 5 * 1024 * 1024; + +/** A host's attachment store removes an entry nobody has used for this long. */ +export const ATTACHMENT_RETENTION_DAYS = 30; diff --git a/packages/shared/src/cloud-agent-launch.ts b/packages/shared/src/cloud-agent-launch.ts index 05b4439bccb..435e56f4162 100644 --- a/packages/shared/src/cloud-agent-launch.ts +++ b/packages/shared/src/cloud-agent-launch.ts @@ -1,4 +1,5 @@ import { z } from "zod"; +import { MAX_ATTACHMENTS } from "./attachment-limits"; import { HOST_AGENT_PRESETS } from "./host-agent-presets"; /** @@ -41,7 +42,7 @@ export const cloudAgentLaunchSchema = z.object({ * them to at create time, so the ids travel and the box pulls the bytes * itself once host-service is up. */ - attachmentFileIds: z.array(z.string().uuid()).max(10).optional(), + attachmentFileIds: z.array(z.string().uuid()).max(MAX_ATTACHMENTS).optional(), surface: z.enum(["terminal", "chat"]).optional(), /** * Set only by cloud automation dispatch. A manual create must not name a From f68cfaaff81102b3bc7a9327210e966ee33fc7ed Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 21:31:22 +0000 Subject: [PATCH 09/16] feat(chat-runtime,host-service): every adapter hands the agent its attachments Part of the attachments plan (PR 4, seam 4). The Claude SDK adapter rendered an attachment part as `[name]`, and the Codex adapter dropped it with a notice. Only ACP resolved it. This is why desktop chat still flattens attachments into path tags in the text. Now all three take one resolver from the host (chat-v3/resolveAttachment.ts). It branches on the identity the part carries. `fileId` is a cloud workspace's storage object: served from the box's cached copy, or imported now when the store is empty, as on a recreated box. `attachmentId` is the host store entry itself. Each hand-out stamps lastUsedAt for the sweep. Bytes are read only for images, since every adapter gives other files by path. - Claude: an image goes as a base64 image block in the SDK's content-array form. Any other file goes as a "# Attached files" line with its name and path, because store files are named by id. - Codex: an image goes as app-server's `localImage` by path. Any other file goes as the same text block. - An unreadable file is named to the agent, not dropped silently. - Resolution is chained per adapter, so prompt order holds. A text-only prompt with nothing queued is still pushed synchronously. The protocol's attachment part now needs `attachmentId` or `fileId` (it needed `attachmentId` before), so a client can send a cloud part by fileId alone. Clients send parts only to hosts at or above ATTACHMENT_PARTS_MIN_HOST_VERSION (1.41.0, the first release with this change). agents.run's attached files block uses the same attachedFilesBlock, so terminal launches also list files by name. A failed resolve reports to Sentry. The host has no PostHog client, so the plan's PostHog event is not sent from here. --- .../UserMessageBubble/UserMessageBubble.tsx | 4 +- .../src/harness/acp/acpAdapter.test.ts | 27 +++- .../src/harness/acp/acpAdapter.ts | 12 +- .../attachedFiles/attachedFiles.test.ts | 15 ++ .../harness/attachedFiles/attachedFiles.ts | 10 ++ .../src/harness/attachedFiles/index.ts | 1 + .../claudeAdapter/claudeAdapter.test.ts | 114 +++++++++++++- .../claude/claudeAdapter/claudeAdapter.ts | 79 +++++++++- .../createClaudeAdapter.ts | 2 + .../codex/codexAdapter/codexAdapter.test.ts | 72 ++++++++- .../codex/codexAdapter/codexAdapter.ts | 81 +++++++--- .../codex/fixturePlayer/fixturePlayer.ts | 14 +- packages/chat-runtime/src/harness/index.ts | 4 + packages/chat-runtime/src/harness/types.ts | 14 ++ packages/chat/src/protocol/items.ts | 23 +-- packages/host-service/src/app.ts | 1 + .../host-service/src/chat-v3/acpHarnesses.ts | 18 +-- packages/host-service/src/chat-v3/mount.ts | 20 ++- .../src/chat-v3/resolveAttachment.test.ts | 141 ++++++++++++++++++ .../src/chat-v3/resolveAttachment.ts | 50 +++++++ packages/host-service/src/sentry.ts | 6 + .../src/trpc/router/agents/agents.ts | 14 +- packages/shared/src/attachment-limits.ts | 6 + 23 files changed, 654 insertions(+), 74 deletions(-) create mode 100644 packages/chat-runtime/src/harness/attachedFiles/attachedFiles.test.ts create mode 100644 packages/chat-runtime/src/harness/attachedFiles/attachedFiles.ts create mode 100644 packages/chat-runtime/src/harness/attachedFiles/index.ts create mode 100644 packages/host-service/src/chat-v3/resolveAttachment.test.ts create mode 100644 packages/host-service/src/chat-v3/resolveAttachment.ts diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx index 5c0db076fe2..17f573fa96a 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx @@ -43,7 +43,9 @@ export function UserMessageBubble({ const { text, pastes } = parsePastedText(raw); const attachments = content.flatMap((part) => - part.type === "attachment" ? [part] : [], + part.type === "attachment" && part.attachmentId + ? [{ ...part, attachmentId: part.attachmentId }] + : [], ); const wide = attachments.length > 0 || pastes.length > 0; const failed = pending?.failed === true; diff --git a/packages/chat-runtime/src/harness/acp/acpAdapter.test.ts b/packages/chat-runtime/src/harness/acp/acpAdapter.test.ts index 80a92b267a8..986dae4d006 100644 --- a/packages/chat-runtime/src/harness/acp/acpAdapter.test.ts +++ b/packages/chat-runtime/src/harness/acp/acpAdapter.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it, jest } from "bun:test"; import { spawn } from "node:child_process"; import type { Item } from "@superset/chat/protocol"; import { AGENT_DEFAULT_MODE, wrapHiddenText } from "@superset/chat/protocol"; -import type { AdapterEvent } from "../types"; +import type { AdapterEvent, ResolveAttachment } from "../types"; import { AcpAdapter, type AcpAdapterOptions, @@ -5614,11 +5614,7 @@ describe("AcpAdapter config options", () => { describe("AcpAdapter attachments", () => { const startWith = ( agent: FakeAcpAgent, - resolveAttachment: (id: string) => Promise<{ - path: string; - mimeType: string; - data: string; - } | null>, + resolveAttachment: ResolveAttachment, ) => { const adapter = new AcpAdapter({ command: "fake", @@ -5712,6 +5708,25 @@ describe("AcpAdapter attachments", () => { ); expect(told).toBe(true); }); + + it("hands the resolver the whole part, so a cloud part resolves by its fileId", async () => { + const agent = new FakeAcpAgent(); + const asked: unknown[] = []; + const { adapter } = startWith(agent, async (part) => { + asked.push(part); + return null; + }); + await flush(); + const part = { + type: "attachment" as const, + fileId: "8c1f6f8e-3a2b-4c5d-9e0f-112233445566", + name: "shot.png", + mimeType: "image/png", + }; + adapter.prompt([part]); + await flush(); + expect(asked).toEqual([part]); + }); }); describe("AcpAdapter images", () => { diff --git a/packages/chat-runtime/src/harness/acp/acpAdapter.ts b/packages/chat-runtime/src/harness/acp/acpAdapter.ts index 49a2cb77221..79cefba77f9 100644 --- a/packages/chat-runtime/src/harness/acp/acpAdapter.ts +++ b/packages/chat-runtime/src/harness/acp/acpAdapter.ts @@ -26,6 +26,8 @@ import type { AdapterEvent, HarnessAdapter, HarnessStartOptions, + ResolveAttachment, + ResolvedAttachment, } from "../types"; import { type RefusedWindow, @@ -181,11 +183,7 @@ type V2Turn = { idle?: AcpStateUpdate & { seq: number }; }; -export type AcpAttachment = { - path: string; - mimeType: string; - data: string; -}; +export type AcpAttachment = ResolvedAttachment; /** `path` is relative to the session cwd. */ export type AcpStoredImage = { path: string }; @@ -233,7 +231,7 @@ export type AcpAdapterOptions = SpawnAcpOptions & { shellPollMs?: number; startupTimeoutMs?: number; requestTimeoutMs?: number; - resolveAttachment?: (attachmentId: string) => Promise; + resolveAttachment?: ResolveAttachment; /** Without it, images the agent sends are dropped. */ storeImage?: ( image: AcpImageSource, @@ -2228,7 +2226,7 @@ export class AcpAdapter implements HarnessAdapter { continue; } const attachment = await this.options - .resolveAttachment?.(entry.attachmentId) + .resolveAttachment?.(entry) .catch(() => null); if (!attachment) { dropped.push(entry.name); diff --git a/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.test.ts b/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.test.ts new file mode 100644 index 00000000000..f687d34139d --- /dev/null +++ b/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, test } from "bun:test"; +import { attachedFilesBlock } from "./attachedFiles"; + +describe("attachedFilesBlock", () => { + test("lists each file by the name it was attached with, then its path", () => { + expect( + attachedFilesBlock([ + { name: "spec.pdf", path: "/store/a1/a1.pdf" }, + { name: "notes.txt", path: "/store/b2/b2.txt" }, + ]), + ).toBe( + "# Attached files\n\nThe user attached these files. They are available on this host at:\n\n- spec.pdf: /store/a1/a1.pdf\n- notes.txt: /store/b2/b2.txt", + ); + }); +}); diff --git a/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.ts b/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.ts new file mode 100644 index 00000000000..5d64423900b --- /dev/null +++ b/packages/chat-runtime/src/harness/attachedFiles/attachedFiles.ts @@ -0,0 +1,10 @@ +/** + * Store files are named by id, so a bare path hands the agent a UUID; the + * name the user gave it goes beside the path. + */ +export function attachedFilesBlock( + files: { name: string; path: string }[], +): string { + const lines = files.map((file) => `- ${file.name}: ${file.path}`); + return `# Attached files\n\nThe user attached these files. They are available on this host at:\n\n${lines.join("\n")}`; +} diff --git a/packages/chat-runtime/src/harness/attachedFiles/index.ts b/packages/chat-runtime/src/harness/attachedFiles/index.ts new file mode 100644 index 00000000000..61b2cda2944 --- /dev/null +++ b/packages/chat-runtime/src/harness/attachedFiles/index.ts @@ -0,0 +1 @@ +export { attachedFilesBlock } from "./attachedFiles"; diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts index 7ce57aa227f..977599cb5db 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts @@ -1,5 +1,7 @@ import { describe, expect, test } from "bun:test"; -import type { AdapterEvent } from "../../types"; +import type { SDKUserMessage } from "@anthropic-ai/claude-agent-sdk"; +import { attachedFilesBlock } from "../../attachedFiles"; +import type { AdapterEvent, ResolveAttachment } from "../../types"; import { ClaudeAdapter, type ClaudeQuery, @@ -268,3 +270,113 @@ describe("ClaudeAdapter permission modes", () => { expect(reverted.value).toMatchObject({ session: { modeId: "default" } }); }); }); + +describe("ClaudeAdapter attachments", () => { + function capturePrompts(resolveAttachment: ResolveAttachment) { + const sent: SDKUserMessage[] = []; + const stream: ClaudeSession = { + async *[Symbol.asyncIterator]() { + await new Promise(() => {}); + }, + }; + const adapter = new ClaudeAdapter({ + resolveAttachment, + query: ({ prompt }) => { + void (async () => { + for await (const message of prompt as AsyncIterable) + sent.push(message); + })(); + return stream; + }, + }); + adapter.start({ cwd: "/workspace" }); + return { adapter, sent }; + } + + const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); + const part = (name: string, mimeType: string) => ({ + type: "attachment" as const, + attachmentId: name, + name, + mimeType, + }); + + test("sends an image as an image block", async () => { + const { adapter, sent } = capturePrompts(async () => ({ + path: "/store/a/a.png", + mimeType: "image/png", + data: "cG5n", + })); + adapter.prompt([ + { type: "text", text: "what is this" }, + part("shot.png", "image/png"), + ]); + await flush(); + expect(sent.map((message) => message.message.content)).toEqual([ + [ + { type: "text", text: "what is this" }, + { + type: "image", + source: { type: "base64", media_type: "image/png", data: "cG5n" }, + }, + ], + ]); + }); + + test("hands a non-image over as its name and path", async () => { + const { adapter, sent } = capturePrompts(async () => ({ + path: "/store/b/b.pdf", + mimeType: "application/pdf", + data: "cGRm", + })); + adapter.prompt([part("spec.pdf", "application/pdf")]); + await flush(); + expect(sent[0]?.message.content).toEqual([ + { + type: "text", + text: attachedFilesBlock([ + { name: "spec.pdf", path: "/store/b/b.pdf" }, + ]), + }, + ]); + }); + + test("tells the agent which file it could not read", async () => { + const { adapter, sent } = capturePrompts(async () => null); + adapter.prompt([part("gone.png", "image/png")]); + await flush(); + expect(sent[0]?.message.content).toEqual([ + { type: "text", text: "[gone.png: could not be read]" }, + ]); + }); + + test("a prompt sent while an attachment resolves still goes second", async () => { + let release: () => void = () => {}; + const { adapter, sent } = capturePrompts( + () => + new Promise((resolve) => { + release = () => + resolve({ + path: "/store/c/c.png", + mimeType: "image/png", + data: "", + }); + }), + ); + adapter.prompt([part("first.png", "image/png")]); + adapter.prompt([{ type: "text", text: "second" }]); + await flush(); + expect(sent).toEqual([]); + release(); + await flush(); + expect(sent.map((message) => message.message.content)).toEqual([ + [ + { + type: "image", + source: { type: "base64", media_type: "image/png", data: "" }, + }, + ], + [{ type: "text", text: "second" }], + ]); + }); +}); diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts index 07782ee66e4..9b49e0096c8 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts @@ -8,10 +8,12 @@ import type { Decision, UserContent, } from "@superset/chat/protocol"; +import { attachedFilesBlock } from "../../attachedFiles"; import type { AdapterEvent, HarnessAdapter, HarnessStartOptions, + ResolveAttachment, } from "../../types"; import { ClaudeTranslator } from "../translateStream"; @@ -77,6 +79,7 @@ export type ClaudeAdapterOptions = { query: ClaudeQuery; pathToClaudeCodeExecutable?: string; launch?: () => Promise; + resolveAttachment?: ResolveAttachment; now?: () => number; mintId?: () => string; }; @@ -168,12 +171,31 @@ class PromptQueue { } } +type SdkUserContent = SDKUserMessage["message"]["content"]; +type SdkContentBlock = Exclude[number]; +type ClaudeImageType = "image/png" | "image/jpeg" | "image/gif" | "image/webp"; + +const CLAUDE_IMAGE_TYPES = new Set([ + "image/png", + "image/jpeg", + "image/gif", + "image/webp", +] satisfies ClaudeImageType[]); + function promptText(content: UserContent[]): string { return content - .map((part) => (part.type === "text" ? part.text : `[${part.name}]`)) + .map((part) => (part.type === "text" ? part.text : "")) .join("\n"); } +function userMessage(content: SdkUserContent): SDKUserMessage { + return { + type: "user", + message: { role: "user", content }, + parent_tool_use_id: null, + }; +} + export class ClaudeAdapter implements HarnessAdapter { private readonly events = new EventQueue(); private readonly prompts = new PromptQueue(); @@ -184,6 +206,7 @@ export class ClaudeAdapter implements HarnessAdapter { private sessionId: string | null = null; private modeId: ClaudePermissionMode = "default"; private pump: Promise | null = null; + private resolvingPrompt: Promise | null = null; private disposed = false; constructor(private readonly options: ClaudeAdapterOptions) {} @@ -256,13 +279,59 @@ export class ClaudeAdapter implements HarnessAdapter { } prompt(content: UserContent[]): void { - this.prompts.push({ - type: "user", - message: { role: "user", content: promptText(content) }, - parent_tool_use_id: null, + const hasAttachment = content.some((part) => part.type === "attachment"); + if (!hasAttachment && !this.resolvingPrompt) { + this.prompts.push(userMessage(promptText(content))); + return; + } + const blocks = this.contentBlocks(content); + const queued = (this.resolvingPrompt ?? Promise.resolve()) + .then(() => blocks) + .then((resolved) => this.prompts.push(userMessage(resolved))); + this.resolvingPrompt = queued; + void queued.finally(() => { + if (this.resolvingPrompt === queued) this.resolvingPrompt = null; }); } + private async contentBlocks( + content: UserContent[], + ): Promise { + const blocks: SdkContentBlock[] = []; + for (const part of content) { + if (part.type === "text") { + blocks.push({ type: "text", text: part.text }); + continue; + } + const file = await ( + this.options.resolveAttachment?.(part) ?? Promise.resolve(null) + ).catch(() => null); + if (!file) { + blocks.push({ + type: "text", + text: `[${part.name}: could not be read]`, + }); + continue; + } + blocks.push( + CLAUDE_IMAGE_TYPES.has(file.mimeType) + ? { + type: "image", + source: { + type: "base64", + media_type: file.mimeType as ClaudeImageType, + data: file.data, + }, + } + : { + type: "text", + text: attachedFilesBlock([{ name: part.name, path: file.path }]), + }, + ); + } + return blocks; + } + cancelTurn(): void { const interrupt = this.session?.interrupt; if (!interrupt) { diff --git a/packages/chat-runtime/src/harness/claude/createClaudeAdapter/createClaudeAdapter.ts b/packages/chat-runtime/src/harness/claude/createClaudeAdapter/createClaudeAdapter.ts index 79124c60d72..3b7ed020ab3 100644 --- a/packages/chat-runtime/src/harness/claude/createClaudeAdapter/createClaudeAdapter.ts +++ b/packages/chat-runtime/src/harness/claude/createClaudeAdapter/createClaudeAdapter.ts @@ -10,10 +10,12 @@ import { ClaudeAdapter } from "../claudeAdapter"; export function createClaudeAdapter(options?: { pathToClaudeCodeExecutable?: string; launch?: ClaudeAdapterOptions["launch"]; + resolveAttachment?: ClaudeAdapterOptions["resolveAttachment"]; }): HarnessAdapter { return new ClaudeAdapter({ query, pathToClaudeCodeExecutable: options?.pathToClaudeCodeExecutable, launch: options?.launch, + resolveAttachment: options?.resolveAttachment, }); } diff --git a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts index 4ccab1c5151..c1c3641f299 100644 --- a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts +++ b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.test.ts @@ -6,9 +6,11 @@ import type { SessionState, ToolCall, Turn, + UserContent, } from "@superset/chat/protocol"; import { itemSchema, turnSchema } from "@superset/chat/protocol"; -import type { AdapterEvent } from "../../types"; +import { attachedFilesBlock } from "../../attachedFiles"; +import type { AdapterEvent, ResolveAttachment } from "../../types"; import { FixturePlayer } from "../fixturePlayer"; import type { CodexFixtureName } from "../fixtures"; import { fixtureCwd, loadCodexFixture } from "../fixtures"; @@ -18,6 +20,8 @@ const FIXED_NOW = 1_785_000_000_000; type ReplayOptions = { onEvent?(event: AdapterEvent, adapter: CodexAdapter): void; + prompt?: UserContent[]; + resolveAttachment?: ResolveAttachment; }; type ReplayResult = { @@ -40,6 +44,7 @@ async function replay( let minted = 0; const adapter = new CodexAdapter({ createTransport: (_spawnOptions, handlers) => player.transport(handlers), + resolveAttachment: options.resolveAttachment, now: () => FIXED_NOW, mintId: () => { minted += 1; @@ -55,7 +60,7 @@ async function replay( } })(); - adapter.prompt([{ type: "text", text: "fixture prompt" }]); + adapter.prompt(options.prompt ?? [{ type: "text", text: "fixture prompt" }]); const deadline = Date.now() + 5000; while (!player.exhausted) { @@ -307,3 +312,66 @@ describe("codexAdapter fixtures", () => { expect(settled.text.endsWith("36")).toBe(true); }); }); + +describe("codexAdapter attachments", () => { + const part = (name: string, mimeType: string) => ({ + type: "attachment" as const, + attachmentId: name, + name, + mimeType, + }); + + async function turnInput( + prompt: UserContent[], + resolveAttachment: ResolveAttachment, + ): Promise { + const { player } = await replay("text", { prompt, resolveAttachment }); + return (player.paramsSent("turn/start")[0] as { input: unknown }).input; + } + + test("an image goes to app-server as a localImage", async () => { + expect( + await turnInput( + [{ type: "text", text: "look" }, part("shot.png", "image/png")], + async () => ({ + path: "/store/a/a.png", + mimeType: "image/png", + data: "", + }), + ), + ).toEqual([ + { type: "text", text: "look", text_elements: [] }, + { type: "localImage", path: "/store/a/a.png" }, + ]); + }); + + test("a non-image goes as its name and path", async () => { + expect( + await turnInput([part("spec.pdf", "application/pdf")], async () => ({ + path: "/store/b/b.pdf", + mimeType: "application/pdf", + data: "", + })), + ).toEqual([ + { + type: "text", + text: attachedFilesBlock([ + { name: "spec.pdf", path: "/store/b/b.pdf" }, + ]), + text_elements: [], + }, + ]); + }); + + test("an unreadable attachment is named, not dropped", async () => { + expect( + await turnInput([part("gone.png", "image/png")], async () => null), + ).toEqual([ + { + type: "text", + text: "[gone.png: could not be read]", + text_elements: [], + }, + ]); + }); +}); diff --git a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts index 1291d669ae0..ceb0c7b2d9f 100644 --- a/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts +++ b/packages/chat-runtime/src/harness/codex/codexAdapter/codexAdapter.ts @@ -4,16 +4,20 @@ import type { Decision, Item, SessionState, + TextElement, ToolCall, Turn, UserContent, } from "@superset/chat/protocol"; import { isKnownItem } from "@superset/chat/protocol"; +import { attachedFilesBlock } from "../../attachedFiles"; import { EventQueue } from "../../eventQueue"; import type { AdapterEvent, + AttachmentPart, HarnessAdapter, HarnessStartOptions, + ResolveAttachment, } from "../../types"; import { mapThreadItem } from "../mapThreadItem"; import type { @@ -109,6 +113,7 @@ export type CodexAdapterOptions = SpawnCodexOptions & { launch?: () => Promise>; minVersion?: string; clientVersion?: string; + resolveAttachment?: ResolveAttachment; now?: () => number; mintId?: () => string; createTransport?( @@ -126,6 +131,7 @@ export class CodexAdapter implements HarnessAdapter { private pendingTurnStart = false; private cancelRequested = false; private readonly queuedInput: unknown[][] = []; + private resolvingPrompt: Promise | null = null; private client: CodexRpcClient | null = null; private threadId: string | null = null; private cwd = process.cwd(); @@ -153,7 +159,22 @@ export class CodexAdapter implements HarnessAdapter { } prompt(content: UserContent[]): void { - const input = this.toCodexInput(content); + const hasAttachment = content.some((part) => part.type === "attachment"); + if (!hasAttachment && !this.resolvingPrompt) { + this.deliver(this.toCodexInput(content)); + return; + } + const input = this.toCodexInputWithAttachments(content); + const queued = (this.resolvingPrompt ?? Promise.resolve()) + .then(() => input) + .then((resolved) => this.deliver(resolved)); + this.resolvingPrompt = queued; + void queued.finally(() => { + if (this.resolvingPrompt === queued) this.resolvingPrompt = null; + }); + } + + private deliver(input: unknown[]): void { if (!this.threadId || !this.client) { this.queuedInput.push(input); return; @@ -659,31 +680,39 @@ export class CodexAdapter implements HarnessAdapter { } private toCodexInput(content: UserContent[]): unknown[] { + return content.flatMap((entry) => + entry.type === "text" ? [codexTextInput(entry.text, entry.elements)] : [], + ); + } + + private async toCodexInputWithAttachments( + content: UserContent[], + ): Promise { const input: unknown[] = []; - let skippedAttachment = false; for (const entry of content) { - if (entry.type === "text") { - input.push({ - type: "text", - text: entry.text, - text_elements: (entry.elements ?? []).map((element) => ({ - byteRange: element.byteRange, - placeholder: null, - })), - }); - continue; - } - skippedAttachment = true; - } - if (skippedAttachment) { - this.emitNotice( - "info", - "Attachments are not supported by the codex harness and were omitted", + input.push( + entry.type === "text" + ? codexTextInput(entry.text, entry.elements) + : await this.attachmentInput(entry), ); } return input; } + /** app-server's turn/start takes a `localImage` by path; anything else goes as a path in text. */ + private async attachmentInput(entry: AttachmentPart): Promise { + const file = await ( + this.options.resolveAttachment?.(entry) ?? Promise.resolve(null) + ).catch(() => null); + if (file?.mimeType.startsWith("image/")) + return { type: "localImage", path: file.path }; + return codexTextInput( + file + ? attachedFilesBlock([{ name: entry.name, path: file.path }]) + : `[${entry.name}: could not be read]`, + ); + } + private emitItem(item: Item, turnId: string): void { this.emit({ kind: "item", item, turnId }); } @@ -727,3 +756,17 @@ export function createCodexAdapter( ): HarnessAdapter { return new CodexAdapter(options); } + +function codexTextInput( + text: string, + elements: TextElement[] = [], +): Record { + return { + type: "text", + text, + text_elements: elements.map((element) => ({ + byteRange: element.byteRange, + placeholder: null, + })), + }; +} diff --git a/packages/chat-runtime/src/harness/codex/fixturePlayer/fixturePlayer.ts b/packages/chat-runtime/src/harness/codex/fixturePlayer/fixturePlayer.ts index 91bb1d1e639..8a4d3213e67 100644 --- a/packages/chat-runtime/src/harness/codex/fixturePlayer/fixturePlayer.ts +++ b/packages/chat-runtime/src/harness/codex/fixturePlayer/fixturePlayer.ts @@ -6,6 +6,7 @@ export class FixturePlayer { private handlers: CodexTransportHandlers | null = null; private pumping = false; private readonly sentMethods: string[] = []; + private readonly sentParams: { method: string; params: unknown }[] = []; constructor(private readonly frames: readonly RecordedFrame[]) {} @@ -13,6 +14,12 @@ export class FixturePlayer { return this.sentMethods; } + paramsSent(method: string): unknown[] { + return this.sentParams + .filter((sent) => sent.method === method) + .map((sent) => sent.params); + } + get exhausted(): boolean { return this.index >= this.frames.length; } @@ -29,8 +36,11 @@ export class FixturePlayer { } private onSend(line: string): void { - const sent = JSON.parse(line) as { method?: string }; - if (sent.method) this.sentMethods.push(sent.method); + const sent = JSON.parse(line) as { method?: string; params?: unknown }; + if (sent.method) { + this.sentMethods.push(sent.method); + this.sentParams.push({ method: sent.method, params: sent.params }); + } const next = this.frames[this.index]; if (next?.direction === "out") { diff --git a/packages/chat-runtime/src/harness/index.ts b/packages/chat-runtime/src/harness/index.ts index 7f66eeb0a06..ea1053e69b8 100644 --- a/packages/chat-runtime/src/harness/index.ts +++ b/packages/chat-runtime/src/harness/index.ts @@ -5,6 +5,7 @@ export type { AcpStoredImage, } from "./acp"; export { AcpAdapter, createAcpAdapter } from "./acp"; +export { attachedFilesBlock } from "./attachedFiles"; export { BackgroundTasks } from "./backgroundTasks"; export type { ClaudeAdapterOptions, @@ -22,7 +23,10 @@ export type { FakeHarnessScript, ScriptedEvent } from "./fake"; export { FakeHarness } from "./fake"; export type { AdapterEvent, + AttachmentPart, DiscoverNativeSessionsInput, HarnessAdapter, HarnessStartOptions, + ResolveAttachment, + ResolvedAttachment, } from "./types"; diff --git a/packages/chat-runtime/src/harness/types.ts b/packages/chat-runtime/src/harness/types.ts index b6f532ce226..2f0b5e88c36 100644 --- a/packages/chat-runtime/src/harness/types.ts +++ b/packages/chat-runtime/src/harness/types.ts @@ -14,6 +14,20 @@ export type AdapterEvent = | { kind: "turn"; turn: Turn } | { kind: "session"; session: Partial }; +export type AttachmentPart = Extract; + +/** `data` is an image's bytes, base64; empty for any other file, which goes by `path`. */ +export type ResolvedAttachment = { + path: string; + mimeType: string; + data: string; +}; + +/** Null when the bytes cannot be had: the adapter tells the agent so. */ +export type ResolveAttachment = ( + part: AttachmentPart, +) => Promise; + export type HarnessStartOptions = { cwd: string; modeId?: string; diff --git a/packages/chat/src/protocol/items.ts b/packages/chat/src/protocol/items.ts index f6cbf649356..32ba65f6d8a 100644 --- a/packages/chat/src/protocol/items.ts +++ b/packages/chat/src/protocol/items.ts @@ -26,15 +26,20 @@ export const userContentSchema = z.discriminatedUnion("type", [ /** Reaches the agent, but is not kept on the transcript's user message. */ hidden: z.boolean().optional(), }), - z.looseObject({ - type: z.literal("attachment"), - attachmentId: z.string().min(1), - /** The storage object behind a cloud workspace's attachment; set, it is the identity. */ - fileId: z.string().uuid().optional(), - name: z.string(), - mimeType: z.string(), - sizeBytes: z.number().int().nonnegative().optional(), - }), + z + .looseObject({ + type: z.literal("attachment"), + /** In the host's attachment store: a workspace on a machine someone owns. */ + attachmentId: z.string().min(1).optional(), + /** In our storage: a cloud workspace. Readers prefer it when both are set. */ + fileId: z.string().uuid().optional(), + name: z.string(), + mimeType: z.string(), + sizeBytes: z.number().int().nonnegative().optional(), + }) + .refine((part) => part.attachmentId || part.fileId, { + message: "An attachment needs an attachmentId or a fileId", + }), ]); export type UserContent = z.infer; diff --git a/packages/host-service/src/app.ts b/packages/host-service/src/app.ts index d4c7a0a40ad..9af393aa126 100644 --- a/packages/host-service/src/app.ts +++ b/packages/host-service/src/app.ts @@ -362,6 +362,7 @@ export function createApp(options: CreateAppOptions): CreateAppResult { }; const chatV3 = createChatV3Mount({ db, + api, dbPath: config.dbPath, agents: createChatAgentBridge(chatAgentContext), onSessionChanged: ({ scopeId, occurredAt }) => diff --git a/packages/host-service/src/chat-v3/acpHarnesses.ts b/packages/host-service/src/chat-v3/acpHarnesses.ts index 107333cffda..bb6a5660cb0 100644 --- a/packages/host-service/src/chat-v3/acpHarnesses.ts +++ b/packages/host-service/src/chat-v3/acpHarnesses.ts @@ -1,10 +1,8 @@ -import { readFile } from "node:fs/promises"; import { createRequire } from "node:module"; import { dirname, join } from "node:path"; -import type { HarnessFactory } from "@superset/chat-runtime"; +import type { HarnessFactory, ResolveAttachment } from "@superset/chat-runtime"; import { createAcpAdapter } from "@superset/chat-runtime"; import type { HostDb } from "../db"; -import { handOutAttachment } from "../trpc/router/attachments/storage"; import { captureSessionAccount } from "../trpc/router/usage/session-account/session-account"; import { ACP_HARNESSES } from "./acpCatalogue"; import { resolveAgentCli } from "./agentCli"; @@ -13,16 +11,6 @@ import { storeAgentImage } from "./agentImages"; import { agentLoginCommand } from "./agentLogin"; import type { ChatAgentBridge } from "./chatAgentBridge"; -async function resolveAttachment(attachmentId: string) { - const resolved = handOutAttachment(attachmentId); - if (!resolved) return null; - return { - path: resolved.path, - mimeType: resolved.metadata.mediaType, - data: (await readFile(resolved.path)).toString("base64"), - }; -} - function resolveAdapterEntry(packageName: string): string { const moduleRequire = createRequire(import.meta.url); const pkgJson = moduleRequire.resolve(`${packageName}/package.json`); @@ -32,6 +20,7 @@ function resolveAdapterEntry(packageName: string): string { export function acpHarnessFactory( harness: string, db: HostDb, + resolveAttachment: ResolveAttachment, agents?: ChatAgentBridge, ): HarnessFactory | null { const entry = ACP_HARNESSES[harness]; @@ -112,11 +101,12 @@ export function acpHarnessFactory( export function acpHarnessEntries( db: HostDb, + resolveAttachment: ResolveAttachment, agents?: ChatAgentBridge, ): [string, HarnessFactory][] { const entries: [string, HarnessFactory][] = []; for (const harness of Object.keys(ACP_HARNESSES)) { - const factory = acpHarnessFactory(harness, db, agents); + const factory = acpHarnessFactory(harness, db, resolveAttachment, agents); if (factory) entries.push([harness, factory]); } return entries; diff --git a/packages/host-service/src/chat-v3/mount.ts b/packages/host-service/src/chat-v3/mount.ts index 34979e1d3b0..f9bc221583f 100644 --- a/packages/host-service/src/chat-v3/mount.ts +++ b/packages/host-service/src/chat-v3/mount.ts @@ -10,6 +10,7 @@ import type { ChatSessionChange, HarnessFactory, HarnessRegistry, + ResolveAttachment, WsSinkSocket, } from "@superset/chat-runtime"; import { @@ -23,12 +24,16 @@ import { import { HOST_CHAT_RESUME_MARKER_FILE } from "@superset/shared/host-version"; import type { Hono, MiddlewareHandler } from "hono"; import type { HostDb } from "../db"; +import { reportAttachmentResolveFailure } from "../sentry"; +import { importCloudAttachments } from "../trpc/router/attachments/attachments"; +import type { ApiClient } from "../types"; import { cliFloor } from "./acpCatalogue"; import { acpHarnessEntries } from "./acpHarnesses"; import { resolveAgentCli } from "./agentCli"; import { buildChatAgentEnv } from "./agentEnv"; import type { ChatAgentBridge } from "./chatAgentBridge"; import { createGitCheckpoints } from "./gitCheckpoints"; +import { createAttachmentResolver } from "./resolveAttachment"; import { createResolveCwd } from "./resolveCwd"; import { recordUnfinishedChats, @@ -56,12 +61,14 @@ function migrationsFolder(): string { function harnessRegistry( db: HostDb, agents: ChatAgentBridge | undefined, + resolveAttachment: ResolveAttachment, ): HarnessRegistry { const entries: [string, HarnessFactory][] = [ [ "claude-code", (options) => createClaudeAdapter({ + resolveAttachment, launch: async () => { const cli = await resolveAgentCli({ binary: "claude", @@ -86,6 +93,7 @@ function harnessRegistry( "codex", (options) => new CodexAdapter({ + resolveAttachment, launch: async () => { const cli = await resolveAgentCli({ binary: "codex", @@ -102,7 +110,7 @@ function harnessRegistry( }, }), ], - ...acpHarnessEntries(db, agents), + ...acpHarnessEntries(db, resolveAttachment, agents), ]; return new Map(entries); } @@ -121,6 +129,7 @@ export type ChatV3Mount = { */ export function createChatV3Mount(options: { db: HostDb; + api: ApiClient; dbPath: string; agents?: ChatAgentBridge; onSessionChanged?: (change: ChatSessionChange) => void; @@ -136,7 +145,14 @@ export function createChatV3Mount(options: { built = createChatRuntime({ dataDir: dirname(options.dbPath), migrationsFolder: migrationsFolder(), - harnesses: harnessRegistry(options.db, options.agents), + harnesses: harnessRegistry( + options.db, + options.agents, + createAttachmentResolver({ + importCloud: (fileIds) => importCloudAttachments(options, fileIds), + report: reportAttachmentResolveFailure, + }), + ), observer: options.agents, checkpoints: createGitCheckpoints(), onSessionChanged: options.onSessionChanged, diff --git a/packages/host-service/src/chat-v3/resolveAttachment.test.ts b/packages/host-service/src/chat-v3/resolveAttachment.test.ts new file mode 100644 index 00000000000..0d94dfb5356 --- /dev/null +++ b/packages/host-service/src/chat-v3/resolveAttachment.test.ts @@ -0,0 +1,141 @@ +import { afterEach, beforeEach, describe, expect, it } from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { AttachmentUploadResult } from "../trpc/router/attachments"; +import { + type AttachmentMetadata, + indexAttachmentFileId, + writeAttachment, +} from "../trpc/router/attachments/storage"; +import { createAttachmentResolver } from "./resolveAttachment"; + +let manifestDir: string; + +beforeEach(() => { + manifestDir = mkdtempSync(join(tmpdir(), "superset-resolve-attachment-")); + process.env.HOST_MANIFEST_DIR = manifestDir; +}); + +afterEach(() => { + rmSync(manifestDir, { recursive: true, force: true }); + delete process.env.HOST_MANIFEST_DIR; +}); + +function store(fields: Partial = {}): AttachmentMetadata { + const metadata: AttachmentMetadata = { + attachmentId: crypto.randomUUID(), + mediaType: "image/png", + sizeBytes: 3, + createdAt: 0, + ...fields, + }; + writeAttachment(new Uint8Array([1, 2, 3]), metadata); + if (metadata.fileId) + indexAttachmentFileId({ ...metadata, fileId: metadata.fileId }); + return metadata; +} + +function resolverWith( + importCloud: (fileIds: string[]) => Promise, +) { + const imports: string[][] = []; + const reported: unknown[] = []; + const resolve = createAttachmentResolver({ + importCloud: (fileIds) => { + imports.push(fileIds); + return importCloud(fileIds); + }, + report: (error) => reported.push(error), + }); + return { resolve, imports, reported }; +} + +const part = (ids: { attachmentId?: string; fileId?: string }) => ({ + type: "attachment" as const, + ...ids, + name: "shot.png", + mimeType: "image/png", +}); + +const noImport = async () => { + throw new Error("should not import"); +}; + +describe("createAttachmentResolver", () => { + it("reads a host part from the store by its attachmentId", async () => { + const entry = store(); + const { resolve } = resolverWith(noImport); + expect(await resolve(part({ attachmentId: entry.attachmentId }))).toEqual({ + path: expect.stringContaining(entry.attachmentId), + mimeType: "image/png", + data: Buffer.from([1, 2, 3]).toString("base64"), + }); + }); + + it("serves a cloud part from the box's cached copy without importing", async () => { + const fileId = crypto.randomUUID(); + const entry = store({ fileId }); + const { resolve, imports } = resolverWith(noImport); + + const file = await resolve(part({ fileId })); + expect(file?.path).toContain(entry.attachmentId); + expect(imports).toEqual([]); + }); + + it("imports a cloud part into an empty store, as on a recreated box", async () => { + const fileId = crypto.randomUUID(); + const { resolve, imports } = resolverWith(async () => { + const entry = store({ fileId }); + return [ + { + attachmentId: entry.attachmentId, + fileId, + mediaType: entry.mediaType, + sizeBytes: entry.sizeBytes, + }, + ]; + }); + + const file = await resolve(part({ fileId })); + expect(imports).toEqual([[fileId]]); + expect(file?.mimeType).toBe("image/png"); + }); + + it("prefers the fileId when a part carries both", async () => { + const fileId = crypto.randomUUID(); + const cloudCopy = store({ fileId }); + const hostEntry = store(); + const { resolve } = resolverWith(noImport); + + const file = await resolve( + part({ attachmentId: hostEntry.attachmentId, fileId }), + ); + expect(file?.path).toContain(cloudCopy.attachmentId); + }); + + it("gives null for an id the store does not have", async () => { + const { resolve, reported } = resolverWith(noImport); + expect( + await resolve(part({ attachmentId: crypto.randomUUID() })), + ).toBeNull(); + expect(reported).toEqual([]); + }); + + it("reports an import that fails and gives null", async () => { + const failure = new Error("storage down"); + const { resolve, reported } = resolverWith(async () => { + throw failure; + }); + expect(await resolve(part({ fileId: crypto.randomUUID() }))).toBeNull(); + expect(reported).toEqual([failure]); + }); + + it("hands a non-image over by path, without reading its bytes", async () => { + const entry = store({ mediaType: "application/pdf" }); + const { resolve } = resolverWith(noImport); + expect( + (await resolve(part({ attachmentId: entry.attachmentId })))?.data, + ).toBe(""); + }); +}); diff --git a/packages/host-service/src/chat-v3/resolveAttachment.ts b/packages/host-service/src/chat-v3/resolveAttachment.ts new file mode 100644 index 00000000000..f573df703fc --- /dev/null +++ b/packages/host-service/src/chat-v3/resolveAttachment.ts @@ -0,0 +1,50 @@ +import { readFile } from "node:fs/promises"; +import type { ResolveAttachment } from "@superset/chat-runtime"; +import type { AttachmentUploadResult } from "../trpc/router/attachments"; +import { + handOutAttachment, + handOutAttachmentByFileId, +} from "../trpc/router/attachments/storage"; + +/** + * One resolver for every adapter. A part names where it lives: `fileId` is a + * cloud workspace's storage object, cached in this box's store or pulled into + * it now (a recreated box has an empty store); `attachmentId` is the store + * entry itself. + */ +export function createAttachmentResolver({ + importCloud, + report, +}: { + importCloud: (fileIds: string[]) => Promise; + report: (error: unknown) => void; +}): ResolveAttachment { + const fromCloud = async (fileId: string) => { + const cached = handOutAttachmentByFileId(fileId); + if (cached) return cached; + const [imported] = await importCloud([fileId]); + return imported ? handOutAttachment(imported.attachmentId) : null; + }; + + return async (part) => { + try { + const found = part.fileId + ? await fromCloud(part.fileId) + : part.attachmentId + ? handOutAttachment(part.attachmentId) + : null; + if (!found) return null; + const mimeType = found.metadata.mediaType; + return { + path: found.path, + mimeType, + data: mimeType.startsWith("image/") + ? (await readFile(found.path)).toString("base64") + : "", + }; + } catch (error) { + report(error); + return null; + } + }; +} diff --git a/packages/host-service/src/sentry.ts b/packages/host-service/src/sentry.ts index b2b3ecac1b9..f1d305db2f7 100644 --- a/packages/host-service/src/sentry.ts +++ b/packages/host-service/src/sentry.ts @@ -39,6 +39,12 @@ export function initSentry(options: { organizationId?: string }): void { initialized = true; } +/** A file a user attached that the agent did not get. */ +export function reportAttachmentResolveFailure(error: unknown): void { + if (!initialized) return; + Sentry.captureException(error, { tags: { attachment_resolve: "failed" } }); +} + export async function captureFatalStartupError(error: unknown): Promise { if (!initialized) return; Sentry.captureException(error); diff --git a/packages/host-service/src/trpc/router/agents/agents.ts b/packages/host-service/src/trpc/router/agents/agents.ts index ac7ed042e49..25c1ad93c0f 100644 --- a/packages/host-service/src/trpc/router/agents/agents.ts +++ b/packages/host-service/src/trpc/router/agents/agents.ts @@ -1,3 +1,4 @@ +import { attachedFilesBlock } from "@superset/chat-runtime"; import { isBuiltinAgentId } from "@superset/shared/agent-catalog"; import { FORK_SESSION_ID_TOKEN } from "@superset/shared/agent-definition"; import { @@ -127,12 +128,10 @@ function buildForkArgv(forkArgs: string[], rawSessionId: string): string[] { function buildAttachmentBlock( prompt: string, - resolved: Array<{ attachmentId: string; path: string }>, + resolved: Array<{ name: string; path: string }>, ): string { if (resolved.length === 0) return prompt; - const lines = resolved.map((item) => `- ${item.path}`); - const block = `\n\n# Attached files\n\nThe user attached these files. They are available on this host at:\n\n${lines.join("\n")}`; - return prompt + block; + return `${prompt}\n\n${attachedFilesBlock(resolved)}`; } export interface AgentRunInput { @@ -426,7 +425,7 @@ export function buildTerminalAgentLaunch( validateAgentForkSelection(config, input.forkSessionId); validateForkSessionIsResolvable(db, config, input); - const resolvedAttachments: Array<{ attachmentId: string; path: string }> = []; + const resolvedAttachments: Array<{ name: string; path: string }> = []; for (const attachmentId of input.attachmentIds ?? []) { const resolved = handOutAttachment(attachmentId); if (!resolved) { @@ -435,7 +434,10 @@ export function buildTerminalAgentLaunch( message: `Attachment not found: ${attachmentId}`, }); } - resolvedAttachments.push({ attachmentId, path: resolved.path }); + resolvedAttachments.push({ + name: resolved.metadata.originalFilename ?? attachmentId, + path: resolved.path, + }); } const prompt = buildAttachmentBlock(input.prompt, resolvedAttachments); diff --git a/packages/shared/src/attachment-limits.ts b/packages/shared/src/attachment-limits.ts index f24fa3c47d0..63e7257f220 100644 --- a/packages/shared/src/attachment-limits.ts +++ b/packages/shared/src/attachment-limits.ts @@ -30,3 +30,9 @@ export const MAX_THUMBNAIL_BYTES = 5 * 1024 * 1024; /** A host's attachment store removes an entry nobody has used for this long. */ export const ATTACHMENT_RETENTION_DAYS = 30; + +/** + * The first host whose adapters read an attachment part, by `attachmentId` or + * `fileId`. Below it a client sends path tags in the text instead. + */ +export const ATTACHMENT_PARTS_MIN_HOST_VERSION = "1.41.0"; From d0919441dde4681b3cbbe6cd1eed3864b34562f3 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 21:42:54 +0000 Subject: [PATCH 10/16] fix(chat-runtime): drop an empty text block from a Claude prompt with attachments An attachment-only message can carry an empty text part, and the Anthropic API rejects an empty text content block. --- .../claude/claudeAdapter/claudeAdapter.test.ts | 16 ++++++++++++++++ .../claude/claudeAdapter/claudeAdapter.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts index 977599cb5db..54087c67b40 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.test.ts @@ -341,6 +341,22 @@ describe("ClaudeAdapter attachments", () => { ]); }); + test("drops an empty text part, which the API refuses as a block", async () => { + const { adapter, sent } = capturePrompts(async () => ({ + path: "/store/a/a.png", + mimeType: "image/png", + data: "cG5n", + })); + adapter.prompt([{ type: "text", text: "" }, part("shot.png", "image/png")]); + await flush(); + expect(sent[0]?.message.content).toEqual([ + { + type: "image", + source: { type: "base64", media_type: "image/png", data: "cG5n" }, + }, + ]); + }); + test("tells the agent which file it could not read", async () => { const { adapter, sent } = capturePrompts(async () => null); adapter.prompt([part("gone.png", "image/png")]); diff --git a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts index 9b49e0096c8..b282e64da6b 100644 --- a/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts +++ b/packages/chat-runtime/src/harness/claude/claudeAdapter/claudeAdapter.ts @@ -300,7 +300,7 @@ export class ClaudeAdapter implements HarnessAdapter { const blocks: SdkContentBlock[] = []; for (const part of content) { if (part.type === "text") { - blocks.push({ type: "text", text: part.text }); + if (part.text) blocks.push({ type: "text", text: part.text }); continue; } const file = await ( From 233498e0c681abf28360148d43c65df406971ad8 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 21:50:48 +0000 Subject: [PATCH 11/16] feat(shared): hostReadsAttachmentParts, the client side of the parts floor Desktop and mobile both decide between attachment parts and path tags from a host's version. One function next to ATTACHMENT_PARTS_MIN_HOST_VERSION keeps the two clients on the same rule. It coerces the version, so a canary or a dev build of a release at or above the floor counts. --- packages/shared/src/host-version.test.ts | 13 +++++++++++++ packages/shared/src/host-version.ts | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/shared/src/host-version.test.ts b/packages/shared/src/host-version.test.ts index 9f28f405949..0f99daad478 100644 --- a/packages/shared/src/host-version.test.ts +++ b/packages/shared/src/host-version.test.ts @@ -1,7 +1,9 @@ import { describe, expect, test } from "bun:test"; +import { ATTACHMENT_PARTS_MIN_HOST_VERSION } from "./attachment-limits"; import { deriveHostVersionState, hostNeedsUpdate, + hostReadsAttachmentParts, isHostUpdateTarget, parseHostInstallSource, } from "./host-version"; @@ -81,3 +83,14 @@ test("only released app versions can target a standalone update", () => { for (const version of ["1.27.0-canary.1", "0.0.0-dev", "v1.27.0", "01.27.0"]) expect(isHostUpdateTarget(version)).toBe(false); }); + +describe("hostReadsAttachmentParts", () => { + test("a host at or above the floor reads parts; an older or unknown one gets path tags", () => { + expect(hostReadsAttachmentParts(ATTACHMENT_PARTS_MIN_HOST_VERSION)).toBe( + true, + ); + expect(hostReadsAttachmentParts("9.0.0")).toBe(true); + expect(hostReadsAttachmentParts("1.40.0")).toBe(false); + expect(hostReadsAttachmentParts(undefined)).toBe(false); + }); +}); diff --git a/packages/shared/src/host-version.ts b/packages/shared/src/host-version.ts index aac8d7033a6..393ac4c087b 100644 --- a/packages/shared/src/host-version.ts +++ b/packages/shared/src/host-version.ts @@ -1,4 +1,5 @@ import semver from "semver"; +import { ATTACHMENT_PARTS_MIN_HOST_VERSION } from "./attachment-limits"; /** * Minimum host-service version a v2 workspace UI can work with against a @@ -106,3 +107,16 @@ export function hostNeedsUpdate(state: HostVersionState): boolean { export function isHostUpdateTarget(version: string): boolean { return /^\d+\.\d+\.\d+$/.test(version) && semver.valid(version) !== null; } + +/** + * Whether a host's adapters read a chat attachment part. Below the floor a + * client puts path tags in the text instead. + */ +export function hostReadsAttachmentParts( + hostVersion: string | null | undefined, +): boolean { + const version = semver.coerce(hostVersion ?? ""); + return ( + version !== null && semver.gte(version, ATTACHMENT_PARTS_MIN_HOST_VERSION) + ); +} From cef20a08996d2fdc7aa4e201861cf0e67ba9b263 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 21:49:45 +0000 Subject: [PATCH 12/16] feat(desktop): attachments go where the workspace lives, as chat parts Part of the attachments plan (desktop PR: seams 1, 3, 5, 7). Since #8704, every desktop attachment went through our storage, including attachments for a workspace on a machine someone owns. Chat flattened them into tags because two of three adapters dropped parts. Mobile showed those tags as raw text, and desktop showed a mobile image as a name badge. Send (seam 1): sendAttachments picks the transfer from where the workspace lives. A box: createUpload, presigned PUT, attachFiles; the part carries only fileId, so it renders without the box. A host (local or relay-reached): base64 attachments.upload; the part carries only attachmentId, and the bytes never reach our servers. A host workspace refuses a file over MAX_INLINE_ATTACHMENT_BYTES, before anything is sent. Composer (seam 3): sends attachment parts when the host's adapters read them. A local host is this build. A remote host or box must report hostReadsAttachmentParts (ATTACHMENT_PARTS_MIN_HOST_VERSION). Below the floor it sends path tags, as before. An attachment-only message leaves out the empty text part. Render (seam 5): UserMessageRow renders image parts through useAttachmentSource. A cloud part resolves through attachment.resolveEach (batched per render) and storage; a host part through attachments.read. The row for a message just sent is primed with the File it attached, so it does a read of neither. Paths (seam 7): sendAttachmentsToWorkspace, which terminal paste and drop and the below-floor composer use, now writes base64 into the worktree for a host, as before #8704. A box keeps storage and materialize. The Files tab and design mode refuse a file over the inline cap before the write. --- .../hooks/useFilesTabDrop/useFilesTabDrop.ts | 35 ++- .../BrowserPane/writeDesignModeAttachments.ts | 25 +- .../components/Composer/Composer.tsx | 18 +- .../useUploadAttachments.ts | 88 +++++- .../UserMessageRow/UserMessageRow.tsx | 33 +- .../AttachmentPartImage.tsx | 32 ++ .../components/AttachmentPartImage/index.ts | 1 + .../hooks/useAttachmentSource/index.ts | 1 + .../useAttachmentSource.ts | 47 +++ .../attachmentSource/attachmentSource.test.ts | 97 ++++++ .../attachmentSource/attachmentSource.ts | 109 +++++++ .../utils/attachmentSource/index.ts | 8 + .../hooks/useAttachmentRoute/index.ts | 1 + .../useAttachmentRoute/useAttachmentRoute.ts | 34 ++ .../useSendAttachments/useSendAttachments.ts | 95 +----- .../createAttachmentDeps.ts | 53 ++++ .../utils/createAttachmentDeps/index.ts | 1 + .../utils/sendAttachmentsToWorkspace/index.ts | 5 + .../sendAttachmentsToWorkspace.test.ts | 287 ++++++++++++----- .../sendAttachmentsToWorkspace.ts | 295 +++++++++++++++--- 20 files changed, 1029 insertions(+), 236 deletions(-) create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/AttachmentPartImage.tsx create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/useAttachmentSource.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.test.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/useAttachmentRoute.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/createAttachmentDeps.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/index.ts diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/WorkspaceSidebar/components/FilesTab/hooks/useFilesTabDrop/useFilesTabDrop.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/WorkspaceSidebar/components/FilesTab/hooks/useFilesTabDrop/useFilesTabDrop.ts index 354152c64f2..b5091ec6129 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/WorkspaceSidebar/components/FilesTab/hooks/useFilesTabDrop/useFilesTabDrop.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/WorkspaceSidebar/components/FilesTab/hooks/useFilesTabDrop/useFilesTabDrop.ts @@ -2,10 +2,12 @@ import { msg } from "@lingui/core/macro"; import { useLingui } from "@lingui/react/macro"; import type { FileTree } from "@pierre/trees"; import { i18n } from "@superset/i18n"; +import { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; import { alert } from "@superset/ui/atoms/Alert"; import { toast } from "@superset/ui/sonner"; import { workspaceTrpc } from "@superset/workspace-client"; import { useCallback, useEffect, useState } from "react"; +import { useWorkspaceHostTarget } from "renderer/hooks/host-service/useWorkspaceHostUrl"; import { fileToBase64 } from "renderer/lib/file-to-base64"; import { asDirectoryHandle, @@ -262,6 +264,10 @@ export function useFilesTabDrop({ workspaceId, }: UseFilesTabDropOptions): FilesTabDrop { const { t } = useLingui(); + const hostTarget = useWorkspaceHostTarget(workspaceId); + const onThisMachine = + hostTarget.status === "local-starting" || + (hostTarget.status === "ready" && hostTarget.kind === "local"); const writeFile = workspaceTrpc.filesystem.writeFile.useMutation(); const createDirectory = workspaceTrpc.filesystem.createDirectory.useMutation(); @@ -376,10 +382,28 @@ export function useFilesTabDrop({ // instead of dribbling them into a worktree the user has left. Files // whose names already exist are collected (not overwritten) and the // user is asked to confirm replacement below. + const isTooLarge = (file: File) => + !onThisMachine && file.size > MAX_INLINE_ATTACHMENT_BYTES; + const tooLarge = tree.files.filter(({ file }) => isTooLarge(file)); + if (tooLarge.length > 0) { + const count = tooLarge.length; + const limit = `${Math.round(MAX_INLINE_ATTACHMENT_BYTES / 1024 / 1024)}MB`; + toast.error( + tooLarge.length === 1 + ? t({ + message: `1 file is over ${limit} and was not added`, + }) + : t({ + message: `${count} files are over ${limit} and were not added`, + }), + ); + } + let added = 0; let failed = 0; const collisions: DroppedFile[] = []; for (const { relPath, file } of tree.files) { + if (isTooLarge(file)) continue; if (!bridge.isCurrent(versionToken)) break; try { if ((await writeOneFile(relPath, file, false)) === "exists") { @@ -532,7 +556,16 @@ export function useFilesTabDrop({ ], }); }, - [model, bridge, writeFile, createDirectory, rootPath, workspaceId, t], + [ + model, + bridge, + writeFile, + createDirectory, + rootPath, + workspaceId, + onThisMachine, + t, + ], ); const onDragOver = useCallback((e: React.DragEvent) => { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/writeDesignModeAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/writeDesignModeAttachments.ts index 93b26383d43..bdb53f683d9 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/writeDesignModeAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/BrowserPane/writeDesignModeAttachments.ts @@ -1,3 +1,6 @@ +import { msg } from "@lingui/core/macro"; +import { i18n } from "@superset/i18n"; +import { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; import { WORKSPACE_ATTACHMENTS_DIR, WORKSPACE_ATTACHMENTS_GITIGNORE, @@ -70,18 +73,30 @@ export async function writeDesignModeAttachments({ // Timestamp alone can collide across rapid sends; suffix for uniqueness. const baseName = `design-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; const contextName = `${baseName}.md`; + const context = formatDesignModeContextMarkdown(payload, comment, { + workspaceId, + paneId, + }); + if (new TextEncoder().encode(context).length > MAX_INLINE_ATTACHMENT_BYTES) { + throw new Error( + i18n._( + msg({ message: "The selected element's context is too large to send" }), + ), + ); + } await client.filesystem.writeFile.mutate({ workspaceId, absolutePath: joinAbsolutePath(attachmentsDirectory, contextName), - content: formatDesignModeContextMarkdown(payload, comment, { - workspaceId, - paneId, - }), + content: context, }); let screenshotPath: string | null = null; const dataUrl = payload.screenshot?.dataUrl; - if (dataUrl?.startsWith("data:image/png;base64,")) { + const screenshotBytes = dataUrl ? (dataUrl.length * 3) / 4 : 0; + if ( + dataUrl?.startsWith("data:image/png;base64,") && + screenshotBytes <= MAX_INLINE_ATTACHMENT_BYTES + ) { const screenshotName = `${baseName}.png`; await client.filesystem.writeFile.mutate({ workspaceId, diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx index 057eda89c56..c9824967e9c 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/Composer.tsx @@ -525,7 +525,7 @@ export const Composer = memo(function Composer({ else prependToDraft(draftKey, [restored]); return; } - const { tags } = upload; + const { tags, parts } = upload; const message = text.trim(); const elements = elementsForChips(message, mentions); const mcpContext = mcpContextLine( @@ -536,18 +536,24 @@ export const Composer = memo(function Composer({ env.NEXT_PUBLIC_WEB_URL, otherHostOf, ); + const body = [message, ...tags].filter(Boolean).join("\n"); onSend( [ - { - type: "text", - text: [message, ...tags].filter(Boolean).join("\n"), - ...(elements.length > 0 ? { elements } : {}), - }, + ...(body || parts.length === 0 + ? [ + { + type: "text" as const, + text: body, + ...(elements.length > 0 ? { elements } : {}), + }, + ] + : []), ...[mcpContext, chatContext].flatMap((context) => context ? [{ type: "text" as const, text: context, hidden: true }] : [], ), + ...parts, ], { steer, draft }, ); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index 877df1467be..3d8c218c40c 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -1,25 +1,52 @@ import { useLingui } from "@lingui/react/macro"; -import { errorMessage } from "@superset/i18n/errors"; +import { errorMessage, rawErrorMessage } from "@superset/i18n/errors"; +import { hostReadsAttachmentParts } from "@superset/shared/host-version"; import { toast } from "@superset/ui/sonner"; +import { workspaceTrpc } from "@superset/workspace-client"; +import { useQueryClient } from "@tanstack/react-query"; import { useCallback } from "react"; +import { useHostServiceInfo } from "renderer/hooks/host-service/useHostServiceInfo"; +import { track } from "renderer/lib/analytics"; +import { useAttachmentRoute } from "../../../../../../hooks/useAttachmentRoute"; import { useSendAttachments } from "../../../../../../hooks/useSendAttachments"; +import { createAttachmentDeps } from "../../../../../../utils/createAttachmentDeps"; import { AttachmentLimitError, + type AttachmentPart, AttachmentSourceError, + assertWithinLimits, + fitsInline, + sendAttachments, } from "../../../../../../utils/sendAttachmentsToWorkspace"; +import { primeAttachmentSource } from "../../../../utils/attachmentSource"; import { formatAttachmentTag } from "../../../../utils/attachmentTags"; export type UploadAttachmentsResult = - | { ok: true; tags: string[] } + | { ok: true; tags: string[]; parts: AttachmentPart[] } | { ok: false; keep: File[] }; +/** + * Chat attachments as parts when the host's adapters read them; this + * machine's host is this build. A file on this machine too big for a part is + * copied into the worktree and goes as a path tag, so it never needs a cap. + * A remote host or box below the floor gets path tags for everything. + */ export function useUploadAttachments(workspaceId: string) { const { t } = useLingui(); - const sendAttachments = useSendAttachments(workspaceId); + const { route, hostUrl, worktreePath } = useAttachmentRoute(workspaceId); + const remoteInfo = useHostServiceInfo( + route === "same-machine" ? null : hostUrl, + ); + const readsParts = + route === "same-machine" || + hostReadsAttachmentParts(remoteInfo.data?.version); + const sendPaths = useSendAttachments(workspaceId); + const host = workspaceTrpc.useUtils().client; + const queryClient = useQueryClient(); return useCallback( async (files: File[]): Promise => { - if (files.length === 0) return { ok: true, tags: [] }; + if (files.length === 0) return { ok: true, tags: [], parts: [] }; const describe = (error: unknown): string => { if (error instanceof AttachmentLimitError) return error.message; if (error instanceof AttachmentSourceError) { @@ -41,31 +68,68 @@ export function useUploadAttachments(workspaceId: string) { } return errorMessage(error, t({ message: "Unknown error" })); }; + const asParts = + route && readsParts + ? route === "same-machine" + ? files.filter(fitsInline) + : files + : []; + const asPaths = files.filter((file) => !asParts.includes(file)); try { - const paths = await sendAttachments(files); + assertWithinLimits(files, Number.POSITIVE_INFINITY); + const parts = + route && asParts.length > 0 + ? await sendAttachments({ + deps: createAttachmentDeps({ host, route, worktreePath }), + route, + workspaceId, + files: asParts, + onSent: (part, file) => + void primeAttachmentSource(queryClient, part, file), + }) + : []; + const paths = await sendPaths(asPaths); + track("attachments_sent", { + surface: "chat", + route, + parts: parts.length, + paths: paths.length, + }); return { ok: true, + parts, tags: paths.map((path, index) => formatAttachmentTag({ path, - type: files[index]?.type ?? "", - name: files[index]?.name, + type: asPaths[index]?.type ?? "", + name: asPaths[index]?.name, }), ), }; } catch (error) { + if ( + !(error instanceof AttachmentLimitError) && + !(error instanceof AttachmentSourceError) + ) { + track("attachments_send_failed", { + surface: "chat", + route, + error: rawErrorMessage(error), + }); + } toast.error(t({ message: "Couldn't attach files" }), { description: describe(error), }); + const bad = + error instanceof AttachmentSourceError + ? asPaths[error.index] + : undefined; return { ok: false, - keep: - error instanceof AttachmentSourceError - ? files.filter((_, index) => index !== error.index) - : files, + keep: bad ? files.filter((file) => file !== bad) : files, }; } }, - [sendAttachments, t], + [host, queryClient, readsParts, route, sendPaths, t, workspaceId, worktreePath], ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx index dac886a425b..5891162b676 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/UserMessageRow.tsx @@ -25,6 +25,7 @@ import { import { CopyButton } from "../../../CopyButton"; import { AttachmentImage } from "../AttachmentImage"; import { MessageTime } from "../MessageTime"; +import { AttachmentPartImage } from "./components/AttachmentPartImage"; import { PastedTextBlock } from "./components/PastedTextBlock"; import { UserMessageEditor } from "./components/UserMessageEditor"; import { UserMessageText } from "./components/UserMessageText"; @@ -77,6 +78,12 @@ export function UserMessageRow({ const images = attachments.filter((attachment) => attachment.type.startsWith("image/"), ); + const parts = item.content.flatMap((content) => + content.type === "attachment" ? [content] : [], + ); + const partKey = (part: (typeof parts)[number], index: number) => + part.fileId ?? part.attachmentId ?? `${part.name}-${index}`; + const imageParts = parts.filter((part) => part.mimeType.startsWith("image/")); const files = [ ...attachments .filter((attachment) => !attachment.type.startsWith("image/")) @@ -85,17 +92,13 @@ export function UserMessageRow({ name: attachmentDisplayName(attachment), mediaType: attachment.type, })), - ...item.content.flatMap((content) => - content.type === "attachment" - ? [ - { - key: content.attachmentId, - name: content.name, - mediaType: content.mimeType, - }, - ] - : [], - ), + ...parts + .filter((part) => !part.mimeType.startsWith("image/")) + .map((part, index) => ({ + key: partKey(part, index), + name: part.name, + mediaType: part.mimeType, + })), ]; const copyable = (text.length > 0 || pastedTexts.length > 0) && !pending; const copyText = [text, ...pastedTexts].filter(Boolean).join("\n\n"); @@ -105,7 +108,8 @@ export function UserMessageRow({ editableMessageIds?.has(item.id) === true && attachments.length === 0 && pastedTexts.length === 0 && - files.length === 0; + files.length === 0 && + imageParts.length === 0; if (editing && editable) { return ( @@ -130,7 +134,7 @@ export function UserMessageRow({ {sender} )} - {images.length > 0 && ( + {(images.length > 0 || imageParts.length > 0) && (
))} + {imageParts.map((part, index) => ( + + ))}
)} {files.length > 0 && ( diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/AttachmentPartImage.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/AttachmentPartImage.tsx new file mode 100644 index 00000000000..ae85a9432e4 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/AttachmentPartImage.tsx @@ -0,0 +1,32 @@ +import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; +import { useState } from "react"; +import type { AttachmentPart } from "../../../../../../../../utils/sendAttachmentsToWorkspace"; +import { useAttachmentSource } from "../../../../hooks/useAttachmentSource"; + +export function AttachmentPartImage({ part }: { part: AttachmentPart }) { + const { src, isLoading } = useAttachmentSource(part); + const [unrenderable, setUnrenderable] = useState(false); + if (isLoading) + return ( +
+ ); + if (!src || unrenderable) + return ( + + ); + return ( +
+ {part.name} setUnrenderable(true)} + src={src} + /> +
+ ); +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/index.ts new file mode 100644 index 00000000000..c3c2f6914ee --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/index.ts @@ -0,0 +1 @@ +export { AttachmentPartImage } from "./AttachmentPartImage"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/index.ts new file mode 100644 index 00000000000..c0b86cd6611 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/index.ts @@ -0,0 +1 @@ +export { useAttachmentSource } from "./useAttachmentSource"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/useAttachmentSource.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/useAttachmentSource.ts new file mode 100644 index 00000000000..63e353e86bf --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useAttachmentSource/useAttachmentSource.ts @@ -0,0 +1,47 @@ +import { workspaceTrpc } from "@superset/workspace-client"; +import { useQuery } from "@tanstack/react-query"; +import { apiTrpcClient } from "renderer/lib/api-trpc-client"; +import type { AttachmentPart } from "../../../../../../utils/sendAttachmentsToWorkspace"; +import { + attachmentSourceKey, + createCloudUrlResolver, + isThumbnailable, + readAttachmentSource, +} from "../../../../utils/attachmentSource"; + +const resolveCloudUrl = createCloudUrlResolver((fileIds) => + apiTrpcClient.attachment.resolveEach.mutate({ fileIds }), +); + +const HOUR_MS = 60 * 60 * 1000; + +/** A data URL for an attachment part's thumbnail, or null for a name badge. */ +export function useAttachmentSource(part: AttachmentPart): { + src: string | null; + isLoading: boolean; +} { + const host = workspaceTrpc.useUtils().client; + const showable = + isThumbnailable(part) && Boolean(part.fileId || part.attachmentId); + const query = useQuery({ + queryKey: attachmentSourceKey(part), + enabled: showable, + staleTime: Number.POSITIVE_INFINITY, + gcTime: HOUR_MS, + retry: 1, + queryFn: () => + readAttachmentSource(part, { + cloudUrl: resolveCloudUrl, + fetchBlob: async (url) => { + const response = await fetch(url); + return response.ok ? response.blob() : null; + }, + hostRead: (attachmentId) => + host.attachments.read.query({ attachmentId }), + }), + }); + return { + src: showable ? (query.data ?? null) : null, + isLoading: showable && query.isPending, + }; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.test.ts new file mode 100644 index 00000000000..9bf3c5b5f04 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, it } from "bun:test"; +import { QueryClient } from "@tanstack/react-query"; +import { + attachmentSourceKey, + createCloudUrlResolver, + primeAttachmentSource, + readAttachmentSource, +} from "./attachmentSource"; + +const FILE_ID = "8c1f6f8e-3a2b-4c5d-9e0f-112233445566"; + +const part = (ids: { fileId?: string; attachmentId?: string }) => ({ + type: "attachment" as const, + ...ids, + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, +}); + +const unexpected = async (): Promise => { + throw new Error("unexpected read"); +}; + +describe("readAttachmentSource", () => { + it("reads a cloud part from storage, never the host", async () => { + const asked: string[] = []; + const src = await readAttachmentSource(part({ fileId: FILE_ID }), { + cloudUrl: async (fileId) => { + asked.push(fileId); + return "https://r2.test/shot"; + }, + fetchBlob: async () => new Blob([new Uint8Array([1, 2, 3])]), + hostRead: unexpected, + }); + expect(asked).toEqual([FILE_ID]); + expect(src).toStartWith("data:"); + }); + + it("reads a host part from the host, never storage", async () => { + expect( + await readAttachmentSource(part({ attachmentId: "a1" }), { + cloudUrl: unexpected, + fetchBlob: unexpected, + hostRead: async () => ({ mediaType: "image/png", data: "AQID" }), + }), + ).toBe("data:image/png;base64,AQID"); + }); + + it("gives a badge for a cloud part storage no longer has", async () => { + expect( + await readAttachmentSource(part({ fileId: FILE_ID }), { + cloudUrl: async () => null, + fetchBlob: unexpected, + hostRead: unexpected, + }), + ).toBeNull(); + }); +}); + +describe("primeAttachmentSource", () => { + it("lets the sent row render the file it attached without any read", async () => { + const queryClient = new QueryClient(); + const sent = part({ fileId: FILE_ID }); + await primeAttachmentSource( + queryClient, + sent, + new File([new Uint8Array([1, 2, 3])], "shot.png", { type: "image/png" }), + ); + const src = await queryClient.fetchQuery({ + queryKey: attachmentSourceKey(sent), + queryFn: unexpected, + staleTime: Number.POSITIVE_INFINITY, + }); + expect(src).toStartWith("data:image/png;base64,"); + }); +}); + +describe("createCloudUrlResolver", () => { + it("resolves the thumbnails asked for together in one call", async () => { + const calls: string[][] = []; + const resolve = createCloudUrlResolver(async (fileIds) => { + calls.push(fileIds); + return fileIds.map((fileId) => + fileId === "gone" + ? { fileId, missing: true as const } + : { fileId, url: `https://r2.test/${fileId}` }, + ); + }); + const urls = await Promise.all([ + resolve("a"), + resolve("b"), + resolve("gone"), + ]); + expect(calls).toEqual([["a", "b", "gone"]]); + expect(urls).toEqual(["https://r2.test/a", "https://r2.test/b", null]); + }); +}); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.ts new file mode 100644 index 00000000000..bf6242821df --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/attachmentSource.ts @@ -0,0 +1,109 @@ +import { + MAX_ATTACHMENTS, + MAX_THUMBNAIL_BYTES, +} from "@superset/shared/attachment-limits"; +import type { QueryClient } from "@tanstack/react-query"; +import type { AttachmentPart } from "../../../../utils/sendAttachmentsToWorkspace"; + +/** A part names its bytes by one identity; `fileId` wins, like every reader. */ +export function attachmentSourceKey(part: AttachmentPart) { + return part.fileId + ? (["attachment", "cloud", part.fileId] as const) + : (["attachment", "host", part.attachmentId ?? ""] as const); +} + +export function isThumbnailable(part: AttachmentPart): boolean { + return ( + part.mimeType.startsWith("image/") && + (part.sizeBytes ?? 0) <= MAX_THUMBNAIL_BYTES + ); +} + +export function blobToDataUrl(blob: Blob): Promise { + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = () => + typeof reader.result === "string" + ? resolve(reader.result) + : reject(new Error("Unexpected file reader result")); + reader.onerror = () => + reject(reader.error ?? new Error("Failed to read file")); + reader.readAsDataURL(blob); + }); +} + +/** The row for a message just sent shows the file attached, not a read back. */ +export async function primeAttachmentSource( + queryClient: QueryClient, + part: AttachmentPart, + file: File, +): Promise { + if (!isThumbnailable(part)) return; + queryClient.setQueryData( + attachmentSourceKey(part), + await blobToDataUrl(file), + ); +} + +type ResolveEach = ( + fileIds: string[], +) => Promise< + Array<{ fileId: string; url: string } | { fileId: string; missing: true }> +>; + +/** + * Coalesces the reads a transcript makes as it renders, so its visible + * thumbnails resolve in a few calls rather than one each. + */ +export function createCloudUrlResolver(resolveEach: ResolveEach) { + let pending = new Map void>>(); + + const flush = async () => { + const batch = pending; + pending = new Map(); + const ids = [...batch.keys()]; + for (let start = 0; start < ids.length; start += MAX_ATTACHMENTS) { + const chunk = ids.slice(start, start + MAX_ATTACHMENTS); + const answers = await resolveEach(chunk).catch(() => []); + const urls = new Map( + answers.flatMap((answer) => + "url" in answer ? [[answer.fileId, answer.url] as const] : [], + ), + ); + for (const fileId of chunk) { + for (const settle of batch.get(fileId) ?? []) + settle(urls.get(fileId) ?? null); + } + } + }; + + return (fileId: string): Promise => + new Promise((resolve) => { + if (pending.size === 0) queueMicrotask(() => void flush()); + pending.set(fileId, [...(pending.get(fileId) ?? []), resolve]); + }); +} + +/** + * Where a part's bytes come from: storage for a cloud part, so no box is + * needed; the host for a host part, the only place they exist. + */ +export async function readAttachmentSource( + part: AttachmentPart, + readers: { + cloudUrl: (fileId: string) => Promise; + fetchBlob: (url: string) => Promise; + hostRead: ( + attachmentId: string, + ) => Promise<{ mediaType: string; data: string }>; + }, +): Promise { + if (part.fileId) { + const url = await readers.cloudUrl(part.fileId); + const blob = url ? await readers.fetchBlob(url) : null; + return blob ? blobToDataUrl(blob) : null; + } + if (!part.attachmentId) return null; + const read = await readers.hostRead(part.attachmentId); + return `data:${read.mediaType};base64,${read.data}`; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/index.ts new file mode 100644 index 00000000000..b83bfc02488 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentSource/index.ts @@ -0,0 +1,8 @@ +export { + attachmentSourceKey, + blobToDataUrl, + createCloudUrlResolver, + isThumbnailable, + primeAttachmentSource, + readAttachmentSource, +} from "./attachmentSource"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/index.ts new file mode 100644 index 00000000000..e0b0b652cd2 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/index.ts @@ -0,0 +1 @@ +export { useAttachmentRoute } from "./useAttachmentRoute"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/useAttachmentRoute.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/useAttachmentRoute.ts new file mode 100644 index 00000000000..e6696e9a9d4 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useAttachmentRoute/useAttachmentRoute.ts @@ -0,0 +1,34 @@ +import { workspaceTrpc } from "@superset/workspace-client"; +import { useWorkspaceHostTarget } from "renderer/hooks/host-service/useWorkspaceHostUrl"; +import type { AttachmentRoute } from "../../utils/sendAttachmentsToWorkspace"; + +/** + * Where this workspace's attachments go. Null until the host is known: an + * unresolved target must not fall through to storage, since it may be this + * machine. + */ +export function useAttachmentRoute(workspaceId: string): { + route: AttachmentRoute | null; + hostUrl: string | null; + worktreePath: string | undefined; +} { + const target = useWorkspaceHostTarget(workspaceId); + const { data: workspace } = workspaceTrpc.workspace.get.useQuery({ + id: workspaceId, + }); + const worktreePath = workspace?.worktreePath; + if (target.status === "local-starting") + return { route: "same-machine", hostUrl: null, worktreePath }; + if (target.status !== "ready") + return { route: null, hostUrl: null, worktreePath }; + return { + route: + target.kind === "local" + ? "same-machine" + : target.kind === "sandbox" + ? "cloud" + : "remote", + hostUrl: target.url, + worktreePath, + }; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts index 0ebe35d439f..e074539da01 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/hooks/useSendAttachments/useSendAttachments.ts @@ -1,52 +1,8 @@ import { workspaceTrpc } from "@superset/workspace-client"; import { useCallback, useRef } from "react"; -import { - useWorkspaceHostTarget, - type WorkspaceHostTarget, -} from "renderer/hooks/host-service/useWorkspaceHostUrl"; -import { apiTrpcClient } from "renderer/lib/api-trpc-client"; -import { fileToBase64 } from "renderer/lib/file-to-base64"; -import { electronTrpcClient } from "renderer/lib/trpc-client"; -import { - type SendAttachmentsDeps, - sendAttachmentsToWorkspace, -} from "../../utils/sendAttachmentsToWorkspace"; - -async function putObject( - target: { url: string; headers: Record }, - file: File, -): Promise { - return await fetch(target.url, { - method: "PUT", - headers: target.headers, - body: file, - }); -} - -type Route = "same-machine" | "remote" | "cloud" | "unknown"; - -function routeOf(target: WorkspaceHostTarget): Route { - if (target.status === "local-starting") return "same-machine"; - if (target.status !== "ready") return "unknown"; - if (target.kind === "local") return "same-machine"; - return target.kind === "sandbox" ? "cloud" : "remote"; -} - -function sameMachineDeps( - worktreePath: string | undefined, -): SendAttachmentsDeps["sameMachine"] { - return { - pathOf: (file) => window.webUtils.getPathForFile(file), - readBase64: fileToBase64, - write: (items) => { - if (!worktreePath) throw new Error("Workspace is not ready"); - return electronTrpcClient.attachments.writeIntoWorktree.mutate({ - worktreePath, - items, - }); - }, - }; -} +import { createAttachmentDeps } from "../../utils/createAttachmentDeps"; +import { sendAttachmentsToWorkspace } from "../../utils/sendAttachmentsToWorkspace"; +import { useAttachmentRoute } from "../useAttachmentRoute"; /** * A stable function that gets files into the workspace's worktree and @@ -56,42 +12,25 @@ function sameMachineDeps( export function useSendAttachments( workspaceId: string, ): (files: File[]) => Promise { - const route = routeOf(useWorkspaceHostTarget(workspaceId)); - const routeRef = useRef(route); - routeRef.current = route; - - const { data: workspace } = workspaceTrpc.workspace.get.useQuery({ - id: workspaceId, - }); - const worktreePathRef = useRef(workspace?.worktreePath); - worktreePathRef.current = workspace?.worktreePath; - - const materialize = - workspaceTrpc.attachments.materializeIntoWorkspace.useMutation(); - const materializeRef = useRef(materialize.mutateAsync); - materializeRef.current = materialize.mutateAsync; + const attachmentRoute = useAttachmentRoute(workspaceId); + const routeRef = useRef(attachmentRoute); + routeRef.current = attachmentRoute; + const host = workspaceTrpc.useUtils().client; + const hostRef = useRef(host); + hostRef.current = host; return useCallback( async (files: File[]) => { - // An unresolved target must not fall through to storage: it may be this machine. - if (routeRef.current === "unknown") { - throw new Error("Workspace is not ready"); - } + const { route, worktreePath } = routeRef.current; + if (!route) throw new Error("Workspace is not ready"); return sendAttachmentsToWorkspace({ - deps: { - createUpload: (input) => - apiTrpcClient.attachment.createUpload.mutate(input), - putObject, - attachFiles: (input) => - apiTrpcClient.cloudWorkspace.attachFiles.mutate(input), - materialize: (input) => materializeRef.current(input), - sameMachine: - routeRef.current === "same-machine" - ? sameMachineDeps(worktreePathRef.current) - : undefined, - }, + deps: createAttachmentDeps({ + host: hostRef.current, + route, + worktreePath, + }), + route, workspaceId, - isCloudWorkspace: routeRef.current === "cloud", files, }); }, diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/createAttachmentDeps.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/createAttachmentDeps.ts new file mode 100644 index 00000000000..307f8f701b1 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/createAttachmentDeps.ts @@ -0,0 +1,53 @@ +import type { workspaceTrpc } from "@superset/workspace-client"; +import { apiTrpcClient } from "renderer/lib/api-trpc-client"; +import { fileToBase64 } from "renderer/lib/file-to-base64"; +import { electronTrpcClient } from "renderer/lib/trpc-client"; +import type { + AttachmentRoute, + SendAttachmentsDeps, +} from "../sendAttachmentsToWorkspace"; + +type HostClient = ReturnType["client"]; + +export function createAttachmentDeps({ + host, + route, + worktreePath, +}: { + host: HostClient; + route: AttachmentRoute; + worktreePath: string | undefined; +}): SendAttachmentsDeps { + return { + createUpload: (input) => + apiTrpcClient.attachment.createUpload.mutate(input), + putObject: (target, file) => + fetch(target.url, { method: "PUT", headers: target.headers, body: file }), + attachFiles: (input) => + apiTrpcClient.cloudWorkspace.attachFiles.mutate(input), + materialize: (input) => + host.attachments.materializeIntoWorkspace.mutate(input), + hostUpload: (input) => host.attachments.upload.mutate(input), + worktreePath: async (workspaceId) => { + const workspace = await host.workspace.get.query({ id: workspaceId }); + if (!workspace?.worktreePath) throw new Error("Workspace path not found"); + return workspace.worktreePath; + }, + createDirectory: (input) => host.filesystem.createDirectory.mutate(input), + writeFile: (input) => host.filesystem.writeFile.mutate(input), + sameMachine: + route === "same-machine" + ? { + pathOf: (file) => window.webUtils.getPathForFile(file), + readBase64: fileToBase64, + write: (items) => { + if (!worktreePath) throw new Error("Workspace is not ready"); + return electronTrpcClient.attachments.writeIntoWorktree.mutate({ + worktreePath, + items, + }); + }, + } + : undefined, + }; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/index.ts new file mode 100644 index 00000000000..423c4f94af7 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/createAttachmentDeps/index.ts @@ -0,0 +1 @@ +export { createAttachmentDeps } from "./createAttachmentDeps"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts index 3217ea4ecc7..4c576eaf580 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/index.ts @@ -1,6 +1,11 @@ export { AttachmentLimitError, + type AttachmentPart, + type AttachmentRoute, AttachmentSourceError, + assertWithinLimits, + fitsInline, type SendAttachmentsDeps, + sendAttachments, sendAttachmentsToWorkspace, } from "./sendAttachmentsToWorkspace"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts index 8f348072224..760839b6873 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.test.ts @@ -2,12 +2,19 @@ import { describe, expect, it } from "bun:test"; import { MAX_ATTACHMENT_BYTES, MAX_ATTACHMENTS, + MAX_INLINE_ATTACHMENT_BYTES, } from "@superset/shared/attachment-limits"; +import { + WORKSPACE_ATTACHMENTS_DIR, + WORKSPACE_ATTACHMENTS_GITIGNORE, +} from "@superset/shared/workspace-attachments"; import { AttachmentLimitError, + type AttachmentPart, AttachmentSourceError, type LocalAttachmentItem, type SendAttachmentsDeps, + sendAttachments, sendAttachmentsToWorkspace, } from "./sendAttachmentsToWorkspace"; @@ -16,16 +23,25 @@ interface Recorded { puts: Array<{ url: string; file: File }>; attached: Array<{ id: string; fileIds: string[] }>; materialized: Array<{ workspaceId: string; fileIds: string[] }>; + hostUploads: Array<{ mediaType: string; originalFilename: string }>; + writes: Array<{ absolutePath: string; content: unknown }>; } -function makeDeps(options: { putStatus?: number } = {}) { +const DIR = `/repo/${WORKSPACE_ATTACHMENTS_DIR}`; + +function makeDeps( + options: { putStatus?: number; existing?: Set } = {}, +) { const recorded: Recorded = { uploads: [], puts: [], attached: [], materialized: [], + hostUploads: [], + writes: [], }; let nextId = 0; + const existing = options.existing ?? new Set(); const deps: SendAttachmentsDeps = { async createUpload(input) { recorded.uploads.push(input); @@ -45,9 +61,37 @@ function makeDeps(options: { putStatus?: number } = {}) { async materialize(input) { recorded.materialized.push(input); return { - paths: input.fileIds.map((id) => `.paradev/attachments/${id}.png`), + paths: input.fileIds.map( + (id) => `${WORKSPACE_ATTACHMENTS_DIR}/${id}.png`, + ), + }; + }, + async hostUpload(input) { + recorded.hostUploads.push({ + mediaType: input.mediaType, + originalFilename: input.originalFilename, + }); + nextId += 1; + return { + attachmentId: `host-${nextId}`, + mediaType: input.mediaType, + sizeBytes: 4, }; }, + async worktreePath() { + return "/repo"; + }, + async createDirectory() {}, + async writeFile(input) { + if (existing.has(input.absolutePath)) + return { ok: false, reason: "exists" }; + existing.add(input.absolutePath); + recorded.writes.push({ + absolutePath: input.absolutePath, + content: input.content, + }); + return { ok: true }; + }, }; return { deps, recorded }; } @@ -55,56 +99,105 @@ function makeDeps(options: { putStatus?: number } = {}) { const png = (name: string, size = 4) => new File([new Uint8Array(size)], name, { type: "image/png" }); -describe("sendAttachmentsToWorkspace", () => { - it("uploads each file to storage, then materializes them by id in order", async () => { +const sized = (name: string, size: number) => + ({ name, type: "image/png", size }) as File; + +describe("sendAttachments", () => { + it("a cloud workspace's parts are storage objects, anchored to it, named by fileId alone", async () => { const { deps, recorded } = makeDeps(); - const paths = await sendAttachmentsToWorkspace({ + const parts = await sendAttachments({ deps, - workspaceId: "ws-1", - isCloudWorkspace: false, + route: "cloud", + workspaceId: "cloud-ws", files: [png("a.png"), png("b.png", 9)], }); - expect(recorded.uploads).toEqual([ - { name: "a.png", contentType: "image/png", sizeBytes: 4 }, - { name: "b.png", contentType: "image/png", sizeBytes: 9 }, + expect(parts).toEqual([ + { + type: "attachment", + fileId: "file-1", + name: "a.png", + mimeType: "image/png", + sizeBytes: 4, + }, + { + type: "attachment", + fileId: "file-2", + name: "b.png", + mimeType: "image/png", + sizeBytes: 9, + }, ]); - expect(recorded.puts.map((p) => p.url)).toEqual([ - "https://r2.test/1", - "https://r2.test/2", + expect(recorded.attached).toEqual([ + { id: "cloud-ws", fileIds: ["file-1", "file-2"] }, ]); - expect(recorded.materialized).toEqual([ - { workspaceId: "ws-1", fileIds: ["file-1", "file-2"] }, + expect(recorded.hostUploads).toEqual([]); + expect(recorded.materialized).toEqual([]); + }); + + it("a host workspace's parts go to the host store and never to storage", async () => { + const { deps, recorded } = makeDeps(); + const parts = await sendAttachments({ + deps, + route: "remote", + workspaceId: "ws-1", + files: [png("a.png")], + }); + + expect(parts).toEqual([ + { + type: "attachment", + attachmentId: "host-1", + name: "a.png", + mimeType: "image/png", + sizeBytes: 4, + }, ]); - expect(recorded.attached).toEqual([]); - expect(paths).toEqual([ - ".paradev/attachments/file-1.png", - ".paradev/attachments/file-2.png", + expect(recorded.hostUploads).toEqual([ + { mediaType: "image/png", originalFilename: "a.png" }, ]); + expect(recorded.uploads).toEqual([]); + expect(recorded.attached).toEqual([]); }); - it("attaches the files to a cloud workspace before the box fetches them", async () => { + it("refuses a host workspace a file over the inline cap, before sending anything", async () => { const { deps, recorded } = makeDeps(); - await sendAttachmentsToWorkspace({ + await expect( + sendAttachments({ + deps, + route: "same-machine", + workspaceId: "ws-1", + files: [ + png("ok.png"), + sized("big.png", MAX_INLINE_ATTACHMENT_BYTES + 1), + ], + }), + ).rejects.toBeInstanceOf(AttachmentLimitError); + expect(recorded.hostUploads).toEqual([]); + }); + + it("hands each part and the file it came from to the caller, for the sent row", async () => { + const { deps } = makeDeps(); + const file = png("a.png"); + const sent: Array<[AttachmentPart, File]> = []; + await sendAttachments({ deps, + route: "cloud", workspaceId: "cloud-ws", - isCloudWorkspace: true, - files: [png("shot.png")], + files: [file], + onSent: (part, from) => sent.push([part, from]), }); - expect(recorded.attached).toEqual([ - { id: "cloud-ws", fileIds: ["file-1"] }, - ]); - expect(recorded.materialized).toEqual([ - { workspaceId: "cloud-ws", fileIds: ["file-1"] }, + expect(sent.map(([part, from]) => [part.fileId, from])).toEqual([ + ["file-1", file], ]); }); it("names a nameless blob from its mime type and defaults the content type", async () => { const { deps, recorded } = makeDeps(); - await sendAttachmentsToWorkspace({ + await sendAttachments({ deps, - workspaceId: "ws-1", - isCloudWorkspace: false, + route: "cloud", + workspaceId: "cloud-ws", files: [ new File([new Uint8Array(2)], "", { type: "image/jpeg" }), new File([new Uint8Array(2)], "", { type: "" }), @@ -120,66 +213,97 @@ describe("sendAttachmentsToWorkspace", () => { ]); }); - it("fails before uploading anything when a file is over the limit", async () => { - const { deps, recorded } = makeDeps(); - const big = { - name: "huge.png", - type: "image/png", - size: MAX_ATTACHMENT_BYTES + 1, - } as File; - await expect( - sendAttachmentsToWorkspace({ - deps, - workspaceId: "ws-1", - isCloudWorkspace: false, - files: [png("ok.png"), big], - }), - ).rejects.toBeInstanceOf(AttachmentLimitError); - expect(recorded.uploads).toEqual([]); - }); - it("refuses more files than one send may carry", async () => { const { deps, recorded } = makeDeps(); const files = Array.from({ length: MAX_ATTACHMENTS + 1 }, (_, i) => png(`${i}.png`), ); await expect( - sendAttachmentsToWorkspace({ - deps, - workspaceId: "ws-1", - isCloudWorkspace: false, - files, - }), + sendAttachments({ deps, route: "cloud", workspaceId: "ws", files }), ).rejects.toBeInstanceOf(AttachmentLimitError); expect(recorded.uploads).toEqual([]); }); - it("surfaces a rejected PUT and never materializes", async () => { + it("surfaces a rejected PUT and never anchors the files", async () => { const { deps, recorded } = makeDeps({ putStatus: 403 }); await expect( - sendAttachmentsToWorkspace({ + sendAttachments({ deps, - workspaceId: "ws-1", - isCloudWorkspace: false, + route: "cloud", + workspaceId: "ws", files: [png("a.png")], }), ).rejects.toThrow("Upload failed for a.png (403)"); - expect(recorded.materialized).toEqual([]); + expect(recorded.attached).toEqual([]); }); +}); - it("does nothing for an empty list", async () => { +describe("sendAttachmentsToWorkspace", () => { + it("a box gets paths through storage: upload, anchor, then materialize", async () => { const { deps, recorded } = makeDeps(); + const paths = await sendAttachmentsToWorkspace({ + deps, + route: "cloud", + workspaceId: "cloud-ws", + files: [png("shot.png")], + }); + expect(recorded.attached).toEqual([ + { id: "cloud-ws", fileIds: ["file-1"] }, + ]); + expect(recorded.materialized).toEqual([ + { workspaceId: "cloud-ws", fileIds: ["file-1"] }, + ]); + expect(recorded.writes).toEqual([]); + expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/file-1.png`]); + }); + + it("a relay-reached host gets the bytes written into its worktree, and storage is never called", async () => { + const { deps, recorded } = makeDeps(); + const paths = await sendAttachmentsToWorkspace({ + deps, + route: "remote", + workspaceId: "ws-1", + files: [png("shot.png")], + }); + expect(recorded.uploads).toEqual([]); + expect(recorded.writes).toEqual([ + { + absolutePath: `${DIR}/.gitignore`, + content: WORKSPACE_ATTACHMENTS_GITIGNORE, + }, + { + absolutePath: `${DIR}/shot.png`, + content: { kind: "base64", data: "AAAAAA==" }, + }, + ]); + expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/shot.png`]); + }); + + it("never overwrites a path an earlier paste already wrote", async () => { + const { deps } = makeDeps({ existing: new Set([`${DIR}/shot.png`]) }); expect( await sendAttachmentsToWorkspace({ deps, + route: "remote", workspaceId: "ws-1", - isCloudWorkspace: true, - files: [], + files: [png("shot.png")], }), - ).toEqual([]); - expect(recorded.uploads).toEqual([]); - expect(recorded.attached).toEqual([]); + ).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/shot_1.png`]); }); + + it("refuses a relay-reached host a file over the inline cap", async () => { + const { deps, recorded } = makeDeps(); + await expect( + sendAttachmentsToWorkspace({ + deps, + route: "remote", + workspaceId: "ws-1", + files: [sized("big.png", MAX_INLINE_ATTACHMENT_BYTES + 1)], + }), + ).rejects.toBeInstanceOf(AttachmentLimitError); + expect(recorded.writes).toEqual([]); + }); + it("keeps a same-machine send off storage: paths are copied, blobs written, order kept", async () => { const { deps, recorded } = makeDeps(); const dmg = new File([new Uint8Array(1)], "build.dmg"); @@ -201,13 +325,13 @@ describe("sendAttachmentsToWorkspace", () => { written.push(items); return { ok: true, - paths: items.map((_, i) => `.paradev/attachments/${i}`), + paths: items.map((_, i) => `${WORKSPACE_ATTACHMENTS_DIR}/${i}`), }; }, }, }, + route: "same-machine", workspaceId: "ws-1", - isCloudWorkspace: false, files: [dmg, png("shot.png"), notes], }); @@ -219,9 +343,9 @@ describe("sendAttachmentsToWorkspace", () => { ], ]); expect(paths).toEqual([ - ".paradev/attachments/0", - ".paradev/attachments/1", - ".paradev/attachments/2", + `${WORKSPACE_ATTACHMENTS_DIR}/0`, + `${WORKSPACE_ATTACHMENTS_DIR}/1`, + `${WORKSPACE_ATTACHMENTS_DIR}/2`, ]); expect(recorded.uploads).toEqual([]); expect(recorded.materialized).toEqual([]); @@ -242,8 +366,8 @@ describe("sendAttachmentsToWorkspace", () => { write: async () => ({ ok: true, paths: [] }), }, }, + route: "same-machine", workspaceId: "ws-1", - isCloudWorkspace: false, files: [big], }), ).rejects.toThrow(AttachmentLimitError); @@ -255,14 +379,14 @@ describe("sendAttachmentsToWorkspace", () => { await expect( sendAttachmentsToWorkspace({ deps: { ...deps, materialize: async () => ({ paths: [] }) }, + route: "cloud", workspaceId: "ws-1", - isCloudWorkspace: false, files: [png("a.png")], }), ).rejects.toThrow("Expected 1 attachment paths, got 0"); }); - it("names the file the host could not read, so the composer can say which", async () => { + it("names the file this machine could not read, so the composer can say which", async () => { const { deps } = makeDeps(); const error = await sendAttachmentsToWorkspace({ @@ -274,8 +398,8 @@ describe("sendAttachmentsToWorkspace", () => { write: async () => ({ ok: false, index: 1, problem: "missing" }), }, }, + route: "same-machine", workspaceId: "ws-1", - isCloudWorkspace: false, files: [png("a.png"), png("moved.png")], }).catch((caught: unknown) => caught); @@ -286,4 +410,17 @@ describe("sendAttachmentsToWorkspace", () => { problem: "missing", }); }); + + it("does nothing for an empty list", async () => { + const { deps, recorded } = makeDeps(); + expect( + await sendAttachmentsToWorkspace({ + deps, + route: "cloud", + workspaceId: "ws-1", + files: [], + }), + ).toEqual([]); + expect(recorded.uploads).toEqual([]); + }); }); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts index c452495f189..9c390dd2b76 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/utils/sendAttachmentsToWorkspace/sendAttachmentsToWorkspace.ts @@ -1,8 +1,29 @@ +import { msg } from "@lingui/core/macro"; +import type { UserContent } from "@superset/chat/protocol"; +import { i18n } from "@superset/i18n"; import { MAX_ATTACHMENT_BYTES, MAX_ATTACHMENTS, + MAX_INLINE_ATTACHMENT_BYTES, } from "@superset/shared/attachment-limits"; -import { attachmentFallbackName } from "@superset/shared/workspace-attachments"; +import { + attachmentFallbackName, + attachmentNameWithSuffix, + sanitizeAttachmentFileName, + WORKSPACE_ATTACHMENTS_DIR, + WORKSPACE_ATTACHMENTS_GITIGNORE, +} from "@superset/shared/workspace-attachments"; +import { fileToBase64 } from "renderer/lib/file-to-base64"; + +export type AttachmentPart = Extract; + +/** + * Where a workspace's files go. `same-machine`: this desktop writes them + * itself. `remote`: a machine someone owns, reached through the relay; the + * bytes go to it and nowhere else. `cloud`: a box, so the bytes go through + * our storage and outlive it. + */ +export type AttachmentRoute = "same-machine" | "remote" | "cloud"; /** Thrown for limits the user can act on; the message is toast-ready. */ export class AttachmentLimitError extends Error {} @@ -37,6 +58,11 @@ export type SameMachineWriteResult = | { ok: true; paths: string[] } | { ok: false; index: number; problem: AttachmentSourceProblem }; +interface FsWriteOutcome { + ok: boolean; + reason?: string; +} + export interface SendAttachmentsDeps { createUpload(input: { name: string; @@ -44,12 +70,29 @@ export interface SendAttachmentsDeps { sizeBytes: number; }): Promise<{ fileId: string; upload: AttachmentUploadTarget }>; putObject(target: AttachmentUploadTarget, file: File): Promise; - /** Cloud only: a box can fetch only the files attached to it. */ + /** A box can fetch only the files attached to it. */ attachFiles(input: { id: string; fileIds: string[] }): Promise; materialize(input: { workspaceId: string; fileIds: string[]; }): Promise<{ paths: string[] }>; + hostUpload(input: { + data: { kind: "base64"; data: string }; + mediaType: string; + originalFilename: string; + }): Promise<{ attachmentId: string; mediaType: string; sizeBytes: number }>; + worktreePath(workspaceId: string): Promise; + createDirectory(input: { + workspaceId: string; + absolutePath: string; + recursive: boolean; + }): Promise; + writeFile(input: { + workspaceId: string; + absolutePath: string; + content: string | { kind: "base64"; data: string }; + options: { create: boolean; overwrite: boolean }; + }): Promise; sameMachine?: { pathOf(file: File): string; readBase64(file: File): Promise; @@ -68,12 +111,13 @@ const MIME_EXTENSIONS: Record = { }; const FALLBACK_CONTENT_TYPE = "application/octet-stream"; +const MAX_NAME_ATTEMPTS = 50; /** * Clipboard files keep their name when they have one (Chromium synthesizes * "image.png" for a screenshot; Finder copies keep the real name); a blob * with none takes its extension from the mime type, the only signal it - * carries. The host sanitizes and deduplicates whatever arrives. + * carries. */ function nameOf(file: File, index: number): string { if (file.name) return file.name; @@ -81,37 +125,79 @@ function nameOf(file: File, index: number): string { return attachmentFallbackName(index, extension ? `.${extension}` : ""); } -function assertWithinLimits(files: File[], sized: File[] = files): void { +function typeOf(file: File): string { + return file.type || FALLBACK_CONTENT_TYPE; +} + +/** Fits the request body a host holds in memory; above it, a host takes a path instead. */ +export function fitsInline(file: File): boolean { + return file.size <= MAX_INLINE_ATTACHMENT_BYTES; +} + +/** + * A host takes the bytes in one request body, so its cap is the transport's. + * `sized` is the files whose size counts; the same-machine route copies a + * file from disk, so only its clipboard blobs are capped. + */ +export function assertWithinLimits( + files: File[], + cap: number, + sized: File[] = files, +): void { if (files.length > MAX_ATTACHMENTS) { + const count = files.length; + const max = MAX_ATTACHMENTS; throw new AttachmentLimitError( - `${files.length} files attached — a message carries at most ${MAX_ATTACHMENTS}`, + i18n._( + msg({ + message: `${count} files attached — a message carries at most ${max}`, + }), + ), ); } for (const file of sized) { - if (file.size > MAX_ATTACHMENT_BYTES) { - throw new AttachmentLimitError( - `"${file.name || "attachment"}" is ${formatMb(file.size)} — files over ${formatMb(MAX_ATTACHMENT_BYTES)} can't be attached`, - ); - } + if (file.size <= cap) continue; + const name = file.name || i18n._(msg({ message: "attachment" })); + const size = formatMb(file.size); + const limit = formatMb(cap); + throw new AttachmentLimitError( + cap === MAX_INLINE_ATTACHMENT_BYTES + ? i18n._( + msg({ + message: `"${name}" is ${size} — files over ${limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead.`, + }), + ) + : i18n._( + msg({ + message: `"${name}" is ${size} — files over ${limit} can't be attached`, + }), + ), + ); } } -async function uploadOne( +async function uploadToStorage( deps: SendAttachmentsDeps, - file: File, - index: number, -): Promise { - const name = nameOf(file, index); - const { fileId, upload } = await deps.createUpload({ - name, - contentType: file.type || FALLBACK_CONTENT_TYPE, - sizeBytes: file.size, - }); - const response = await deps.putObject(upload, file); - if (!response.ok) { - throw new Error(`Upload failed for ${name} (${response.status})`); - } - return fileId; + workspaceId: string, + files: File[], +): Promise { + const fileIds = await Promise.all( + files.map(async (file, index) => { + const name = nameOf(file, index); + const { fileId, upload } = await deps.createUpload({ + name, + contentType: typeOf(file), + sizeBytes: file.size, + }); + const response = await deps.putObject(upload, file); + if (!response.ok) { + throw new Error(`Upload failed for ${name} (${response.status})`); + } + return fileId; + }), + ); + await deps.attachFiles({ id: workspaceId, fileIds }); + return fileIds; } function expectOnePathEach(files: File[], paths: string[]): string[] { @@ -130,6 +216,7 @@ async function writeOnSameMachine( const sourcePaths = files.map((file) => sameMachine.pathOf(file)); assertWithinLimits( files, + MAX_ATTACHMENT_BYTES, files.filter((_, index) => !sourcePaths[index]), ); const items = await Promise.all( @@ -155,35 +242,151 @@ async function writeOnSameMachine( } /** - * Gets composer files into a workspace's worktree and returns the - * worktree-relative paths an agent is handed, in the order of `files`. - * - * A workspace on this machine gets them from the desktop main process and - * nothing leaves the machine. Any other workspace gets them through storage, - * like the mobile composer: client → presigned PUT, then the host pulls them, - * so the host-service request carries only ids and a sandbox ingress that - * cuts an upload answered early never sees the bytes. - * - * The caller owns the error surface; a thrown `AttachmentLimitError` is - * already worded for a toast. + * Create-only writes report "exists", so the real directory decides the name: + * a second screenshot must never overwrite a path an earlier prompt names. + */ +async function writeUnique( + deps: SendAttachmentsDeps, + workspaceId: string, + dirAbs: string, + base: string, + data: string, +): Promise { + for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { + const fileName = attachmentNameWithSuffix(base, attempt); + const outcome = await deps.writeFile({ + workspaceId, + absolutePath: `${dirAbs}/${fileName}`, + content: { kind: "base64", data }, + options: { create: true, overwrite: false }, + }); + if (outcome.ok !== false) return fileName; + if (outcome.reason !== "exists") { + throw new Error(outcome.reason ?? "write failed"); + } + } + throw new Error(`Could not find a free name for ${base}`); +} + +async function writeIntoRemoteWorktree( + deps: SendAttachmentsDeps, + workspaceId: string, + files: File[], +): Promise { + const dirAbs = `${await deps.worktreePath(workspaceId)}/${WORKSPACE_ATTACHMENTS_DIR}`; + await deps.createDirectory({ + workspaceId, + absolutePath: dirAbs, + recursive: true, + }); + const ignore = await deps.writeFile({ + workspaceId, + absolutePath: `${dirAbs}/.gitignore`, + content: WORKSPACE_ATTACHMENTS_GITIGNORE, + options: { create: true, overwrite: false }, + }); + if (ignore.ok === false && ignore.reason !== "exists") { + throw new Error(ignore.reason ?? "write failed"); + } + const paths: string[] = []; + for (const [index, file] of files.entries()) { + const base = + sanitizeAttachmentFileName(nameOf(file, index)) ?? + attachmentFallbackName(index); + const fileName = await writeUnique( + deps, + workspaceId, + dirAbs, + base, + await fileToBase64(file), + ); + paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${fileName}`); + } + return paths; +} + +/** + * The parts a chat message carries for `files`, one identity each, chosen by + * where the workspace lives: a cloud part is a `fileId` in storage, which + * renders without the box; a host part is an `attachmentId` in that host's + * store, and its bytes never leave the host. + */ +export async function sendAttachments(options: { + deps: SendAttachmentsDeps; + route: AttachmentRoute; + workspaceId: string; + files: File[]; + onSent?: (part: AttachmentPart, file: File) => void; +}): Promise { + const { deps, route, workspaceId, files } = options; + if (files.length === 0) return []; + assertWithinLimits( + files, + route === "cloud" ? MAX_ATTACHMENT_BYTES : MAX_INLINE_ATTACHMENT_BYTES, + ); + + const parts: AttachmentPart[] = + route === "cloud" + ? (await uploadToStorage(deps, workspaceId, files)).map( + (fileId, index) => { + const file = files[index] as File; + return { + type: "attachment", + fileId, + name: nameOf(file, index), + mimeType: typeOf(file), + sizeBytes: file.size, + }; + }, + ) + : await Promise.all( + files.map(async (file, index) => { + const record = await deps.hostUpload({ + data: { kind: "base64", data: await fileToBase64(file) }, + mediaType: typeOf(file), + originalFilename: nameOf(file, index), + }); + return { + type: "attachment" as const, + attachmentId: record.attachmentId, + name: nameOf(file, index), + mimeType: record.mediaType, + sizeBytes: record.sizeBytes, + }; + }), + ); + for (const [index, part] of parts.entries()) { + options.onSent?.(part, files[index] as File); + } + return parts; +} + +/** + * Worktree-relative paths for surfaces that take text: a PTY, a file too big + * for a part on this machine, or a chat message to a host too old to read + * parts. This machine copies the files itself, with no cap. A relay-reached + * host gets the bytes in the request, since storage would put them on our + * servers. A box gets them through storage, because its ingress can cut a + * request body the host answers early. */ export async function sendAttachmentsToWorkspace(options: { deps: SendAttachmentsDeps; + route: AttachmentRoute; workspaceId: string; - isCloudWorkspace: boolean; files: File[]; }): Promise { - const { deps, workspaceId, isCloudWorkspace, files } = options; + const { deps, route, workspaceId, files } = options; if (files.length === 0) return []; - if (deps.sameMachine) return writeOnSameMachine(deps.sameMachine, files); - assertWithinLimits(files); - - const fileIds = await Promise.all( - files.map((file, index) => uploadOne(deps, file, index)), - ); - if (isCloudWorkspace) { - await deps.attachFiles({ id: workspaceId, fileIds }); + if (route === "same-machine") { + if (!deps.sameMachine) throw new Error("Workspace is not ready"); + return writeOnSameMachine(deps.sameMachine, files); + } + if (route === "remote") { + assertWithinLimits(files, MAX_INLINE_ATTACHMENT_BYTES); + return writeIntoRemoteWorktree(deps, workspaceId, files); } + assertWithinLimits(files, MAX_ATTACHMENT_BYTES); + const fileIds = await uploadToStorage(deps, workspaceId, files); const { paths } = await deps.materialize({ workspaceId, fileIds }); return expectOnePathEach(files, paths); } From 5a646e8ff4c506a113c73c6f2b1d6d023b00f2fd Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 22:02:39 +0000 Subject: [PATCH 13/16] docs(sandbox): terminal paste and chat attachments go where the workspace lives The clipboard paragraph described #8704's routing, which sent every non-local host's paste through storage. A box still goes through storage; a relay-reached machine gets base64 filesystem.writeFile again. --- docs/cloud-sandbox-mismatches.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/cloud-sandbox-mismatches.md b/docs/cloud-sandbox-mismatches.md index d215d5623c4..4ce1943e389 100644 --- a/docs/cloud-sandbox-mismatches.md +++ b/docs/cloud-sandbox-mismatches.md @@ -371,12 +371,16 @@ forwards Ctrl+V and lets the TUI (Claude Code, Codex) read the image from the OS clipboard — of the machine the PTY runs on. A sandbox (or any relay-reached host) never holds the user's local screenshot, so the paste silently did nothing or surfaced "Failed to paste image". Fixed renderer-side: -for non-local hosts the desktop sends the clipboard files the way every -composer sends attachments — `attachment.createUpload` on the API, a -presigned PUT to storage, `cloudWorkspace.attachFiles` for a box, then -`attachments.materializeIntoWorkspace` on the host — and pastes the -worktree-relative paths instead (`setImagePasteOverride` in the terminal -runtime registry, `useSendAttachments` in the pane registry). +for non-local hosts the desktop writes the clipboard files into the worktree +and pastes their worktree-relative paths instead (`setImagePasteOverride` in +the terminal runtime registry, `useSendAttachments` in the pane registry). +The bytes go where the workspace lives. A box gets them through storage: +`attachment.createUpload`, a presigned PUT, `cloudWorkspace.attachFiles`, +then `attachments.materializeIntoWorkspace` on the box. A relay-reached +machine gets them as base64 `filesystem.writeFile`, capped at +`MAX_INLINE_ATTACHMENT_BYTES`, so they never reach our servers. Chat +attachments follow the same rule as parts: a `fileId` for a box, an +`attachmentId` in the host's store otherwise. **The ingress drops an upload the host answers early.** tRPC's streamed batch (`httpBatchStreamLink`, `trpc-accept: application/jsonl`) returns its response From f32821b95d4b36fd9a0e4a4f0081532e91eb9df0 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 22:00:25 +0000 Subject: [PATCH 14/16] feat(mobile): attachments go where the workspace lives; render cloud parts and path tags Part of the attachments plan (mobile PR). Mobile uploaded every attachment to our storage when it was attached, and then had the host import it. That also happened for a workspace on a machine someone owns, whose bytes should never reach our servers. Mobile also showed a desktop path tag as raw text. - A host workspace's draft no longer uploads at attach time. At send, chat attachments go to the host as base64 attachments.upload (the part names attachmentId). Terminal attachments are written into the worktree with create-only base64 filesystem.writeFile. Both refuse a file over MAX_INLINE_ATTACHMENT_BYTES. - A cloud chat part carries its fileId and skips importFromCloud when the box reads parts (hostReadsAttachmentParts). An older box still gets the copy it imports. - useAttachmentFile reads a fileId part from storage (resolveEach, then a download into the cache), so it shows without the box. A host part still reads from the host. - Path tags in a message show as file cards. parseAttachmentTags moved from desktop into @superset/chat/core so both clients parse them the same way. - A send that finds an attachment that never started uploading (its draft's home was unknown when it was attached) starts it. --- .../ChatSession/utils/attachmentTags/index.ts | 2 +- apps/mobile/lib/attachments/chatParts.test.ts | 119 +++++++++++++++++ apps/mobile/lib/attachments/chatParts.ts | 120 ++++++++++++++++++ apps/mobile/lib/attachments/upload.ts | 43 ++++++- .../lib/attachments/worktreeWrite.test.ts | 38 ++++++ apps/mobile/lib/attachments/worktreeWrite.ts | 71 +++++++++++ .../[id]/WorkspaceScreen/WorkspaceScreen.tsx | 70 ++++++---- .../UserMessageBubble/UserMessageBubble.tsx | 42 ++++-- .../AttachmentFileCard/AttachmentFileCard.tsx | 11 +- .../AttachmentThumbnail.tsx | 15 ++- .../hooks/useAttachmentFile/index.ts | 2 +- .../useAttachmentFile/useAttachmentFile.ts | 47 +++++-- .../ChatSessionView/ChatSessionView.tsx | 9 +- .../TerminalComposer/TerminalComposer.tsx | 28 ++-- .../useWriteTerminalAttachments.ts | 52 ++++++-- .../attachmentTags/attachmentTags.test.ts | 0 .../core}/attachmentTags/attachmentTags.ts | 2 +- packages/chat/src/core/index.ts | 1 + packages/i18n/locales/cs/messages.po | 3 + packages/i18n/locales/de/messages.po | 3 + packages/i18n/locales/en/messages.po | 3 + packages/i18n/locales/es/messages.po | 3 + packages/i18n/locales/fr/messages.po | 3 + packages/i18n/locales/id/messages.po | 3 + packages/i18n/locales/it/messages.po | 3 + packages/i18n/locales/ja/messages.po | 3 + packages/i18n/locales/ko/messages.po | 3 + packages/i18n/locales/nl/messages.po | 3 + packages/i18n/locales/pl/messages.po | 3 + packages/i18n/locales/pt-BR/messages.po | 3 + packages/i18n/locales/ru/messages.po | 3 + packages/i18n/locales/tr/messages.po | 3 + packages/i18n/locales/vi/messages.po | 3 + packages/i18n/locales/zh-CN/messages.po | 3 + packages/i18n/locales/zh-TW/messages.po | 3 + 35 files changed, 623 insertions(+), 100 deletions(-) create mode 100644 apps/mobile/lib/attachments/chatParts.test.ts create mode 100644 apps/mobile/lib/attachments/chatParts.ts create mode 100644 apps/mobile/lib/attachments/worktreeWrite.test.ts create mode 100644 apps/mobile/lib/attachments/worktreeWrite.ts rename {apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils => packages/chat/src/core}/attachmentTags/attachmentTags.test.ts (100%) rename {apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils => packages/chat/src/core}/attachmentTags/attachmentTags.ts (97%) diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts index 6026020d69e..313695879ff 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/index.ts @@ -3,4 +3,4 @@ export { attachmentDisplayName, formatAttachmentTag, parseAttachmentTags, -} from "./attachmentTags"; +} from "@superset/chat/core"; diff --git a/apps/mobile/lib/attachments/chatParts.test.ts b/apps/mobile/lib/attachments/chatParts.test.ts new file mode 100644 index 00000000000..8bc80614d33 --- /dev/null +++ b/apps/mobile/lib/attachments/chatParts.test.ts @@ -0,0 +1,119 @@ +import { beforeAll, describe, expect, test } from "bun:test"; +import { initI18n } from "@superset/i18n"; +import { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; +import { + AttachmentLimitError, + type ChatAttachmentFile, + type ChatPartsDeps, + chatAttachmentParts, +} from "./chatParts"; + +const FILE_ID = "8c1f6f8e-3a2b-4c5d-9e0f-112233445566"; + +function file(name: string, size = 3): ChatAttachmentFile { + return { + name, + mimeType: "image/png", + size, + readBase64: async () => "AQID", + }; +} + +function recordingDeps() { + const calls: string[] = []; + const deps: ChatPartsDeps = { + awaitUploads: async () => { + calls.push("awaitUploads"); + return [FILE_ID]; + }, + attachFiles: async () => { + calls.push("attachFiles"); + }, + importFromCloud: async () => { + calls.push("importFromCloud"); + return [ + { + attachmentId: "imported", + originalFilename: "shot.png", + mediaType: "image/png", + sizeBytes: 3, + }, + ]; + }, + hostUpload: async (input) => { + calls.push(`hostUpload:${input.data.data}`); + return { attachmentId: "host-1", mediaType: "image/png", sizeBytes: 3 }; + }, + }; + return { deps, calls }; +} + +beforeAll(() => initI18n()); + +describe("chatAttachmentParts", () => { + test("a cloud part carries its fileId and skips the import", async () => { + const { deps, calls } = recordingDeps(); + const parts = await chatAttachmentParts({ + home: "cloud", + readsParts: true, + files: [file("shot.png")], + deps, + }); + expect(parts).toEqual([ + { + type: "attachment", + fileId: FILE_ID, + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, + }, + ]); + expect(calls).toEqual(["awaitUploads", "attachFiles"]); + }); + + test("a box too old to read parts gets the copy it imports", async () => { + const { deps, calls } = recordingDeps(); + const parts = await chatAttachmentParts({ + home: "cloud", + readsParts: false, + files: [file("shot.png")], + deps, + }); + expect(parts[0]?.attachmentId).toBe("imported"); + expect(parts[0]?.fileId).toBeUndefined(); + expect(calls).toEqual(["awaitUploads", "attachFiles", "importFromCloud"]); + }); + + test("a host part goes to the host in the request and never to storage", async () => { + const { deps, calls } = recordingDeps(); + const parts = await chatAttachmentParts({ + home: "host", + readsParts: true, + files: [file("shot.png")], + deps, + }); + expect(parts).toEqual([ + { + type: "attachment", + attachmentId: "host-1", + name: "shot.png", + mimeType: "image/png", + sizeBytes: 3, + }, + ]); + expect(calls).toEqual(["hostUpload:AQID"]); + }); + + test("a host refuses a file over the inline cap before sending anything", async () => { + const { deps, calls } = recordingDeps(); + await expect( + chatAttachmentParts({ + home: "host", + readsParts: true, + files: [file("big.png", MAX_INLINE_ATTACHMENT_BYTES + 1)], + deps, + }), + ).rejects.toBeInstanceOf(AttachmentLimitError); + expect(calls).toEqual([]); + }); +}); diff --git a/apps/mobile/lib/attachments/chatParts.ts b/apps/mobile/lib/attachments/chatParts.ts new file mode 100644 index 00000000000..1c3d217336e --- /dev/null +++ b/apps/mobile/lib/attachments/chatParts.ts @@ -0,0 +1,120 @@ +import { msg } from "@lingui/core/macro"; +import { i18n } from "@superset/i18n"; +import { MAX_INLINE_ATTACHMENT_BYTES } from "@superset/shared/attachment-limits"; + +export interface ChatAttachmentPart { + type: "attachment"; + attachmentId?: string; + fileId?: string; + name: string; + mimeType: string; + sizeBytes?: number; +} + +export interface ChatAttachmentFile { + name: string; + mimeType: string; + size: number; + readBase64: () => Promise; +} + +interface HostRecord { + attachmentId: string; + originalFilename?: string; + mediaType: string; + sizeBytes: number; +} + +export interface ChatPartsDeps { + /** The storage ids of the draft's uploads, waiting on any still in flight. */ + awaitUploads: () => Promise; + attachFiles: (fileIds: string[]) => Promise; + importFromCloud: (fileIds: string[]) => Promise; + hostUpload: (input: { + data: { kind: "base64"; data: string }; + mediaType: string; + originalFilename: string; + }) => Promise; +} + +/** Thrown for limits the user can act on; the message is alert-ready. */ +export class AttachmentLimitError extends Error {} + +/** A host takes the bytes in one request body, which it holds in memory. */ +export function assertFitsHost(files: ChatAttachmentFile[]): void { + const tooLarge = files.find( + (file) => file.size > MAX_INLINE_ATTACHMENT_BYTES, + ); + if (!tooLarge) return; + const name = tooLarge.name; + const limit = `${Math.round(MAX_INLINE_ATTACHMENT_BYTES / 1024 / 1024)}MB`; + throw new AttachmentLimitError( + i18n._( + msg({ + message: `"${name}" is over ${limit}, the most a workspace outside the cloud can take`, + }), + ), + ); +} + +/** + * The parts a chat message carries, by where the workspace lives. A cloud + * part is a storage object, so it renders without the box; a host part is in + * that host's store, and its bytes never reach our servers. A box too old to + * read a part by `fileId` gets the copy it imports instead. + */ +export async function chatAttachmentParts({ + home, + readsParts, + files, + deps, +}: { + home: "cloud" | "host"; + readsParts: boolean; + files: ChatAttachmentFile[]; + deps: ChatPartsDeps; +}): Promise { + if (files.length === 0) return []; + if (home === "host") { + assertFitsHost(files); + return Promise.all( + files.map(async (file) => { + const record = await deps.hostUpload({ + data: { kind: "base64", data: await file.readBase64() }, + mediaType: file.mimeType, + originalFilename: file.name, + }); + return { + type: "attachment" as const, + attachmentId: record.attachmentId, + name: file.name, + mimeType: record.mediaType, + sizeBytes: record.sizeBytes, + }; + }), + ); + } + + const fileIds = await deps.awaitUploads(); + await deps.attachFiles(fileIds); + if (readsParts) { + return fileIds.map((fileId, index) => { + const file = files[index] as ChatAttachmentFile; + return { + type: "attachment", + fileId, + name: file.name, + mimeType: file.mimeType, + sizeBytes: file.size, + }; + }); + } + const imported = await deps.importFromCloud(fileIds); + return imported.map((record) => ({ + type: "attachment", + attachmentId: record.attachmentId, + name: record.originalFilename ?? "attachment", + mimeType: record.mediaType, + sizeBytes: record.sizeBytes, + })); +} diff --git a/apps/mobile/lib/attachments/upload.ts b/apps/mobile/lib/attachments/upload.ts index 24b19bb82cf..5a7dfeacf1d 100644 --- a/apps/mobile/lib/attachments/upload.ts +++ b/apps/mobile/lib/attachments/upload.ts @@ -5,6 +5,7 @@ import { File } from "expo-file-system"; import type { PromptInputAttachmentItem } from "@/components/ai-elements/prompt-input"; import { apiClient } from "@/lib/trpc/client"; import { useComposerDraftsStore } from "@/screens/(authenticated)/stores/composerDraftsStore"; +import type { ChatAttachmentFile } from "./chatParts"; import { waitForSettledUploads } from "./settle"; const FALLBACK_MEDIA_TYPE = "application/octet-stream"; @@ -17,12 +18,29 @@ const FALLBACK_MEDIA_TYPE = "application/octet-stream"; */ const controllers = new Map(); +/** + * Drafts whose workspace is on a machine someone owns. Their bytes go to that + * host at send and never to our storage, so nothing uploads at attach time. + */ +const hostDrafts = new Set(); + +export function setDraftAttachmentHome( + draftKey: string, + home: "cloud" | "host", +): void { + if (home === "host") hostDrafts.add(draftKey); + else hostDrafts.delete(draftKey); +} + /** * The name to store an attachment under when the picker gave us none. The * uri's last segment is what the user would recognize; a generated name is * the last resort, and the host renames anything unusable anyway. */ -function nameOf(attachment: PromptInputAttachmentItem, index: number): string { +export function nameOf( + attachment: PromptInputAttachmentItem, + index: number, +): string { if (attachment.name) return attachment.name; const fromUri = decodeURIComponent( attachment.uri.split("?")[0]?.split("/").pop() ?? "", @@ -139,6 +157,7 @@ export function startAttachmentUploads( draftKey: string, attachments: PromptInputAttachmentItem[], ): void { + if (hostDrafts.has(draftKey)) return; for (const [index, attachment] of attachments.entries()) { if (controllers.has(attachment.id)) continue; void upload(draftKey, attachment, index); @@ -168,14 +187,17 @@ export async function awaitAttachmentUploads( useComposerDraftsStore.getState().draftsByKey[draftKey]?.uploads ?? {}; // A failure earlier in the draft's life is worth one more try now that the // user has actually asked to send: the usual cause is a connection that - // has since come back. + // has since come back. One that never started was attached while the + // workspace's home was still unknown. const stale = uploadsNow(); startAttachmentUploads( draftKey, // Only entries the draft still holds: `beginUpload` refuses to write // state for an attachment that is gone, so starting one would upload // bytes nothing could ever wait on. - attachments.filter((item) => stale[item.id]?.error !== undefined), + attachments.filter( + (item) => !stale[item.id] || stale[item.id]?.error !== undefined, + ), ); const uploads = await waitForSettledUploads( @@ -194,3 +216,18 @@ export async function awaitAttachmentUploads( return entry.fileId; }); } + +/** The composer's attachments as files a host can be sent, read only when sent. */ +export function toAttachmentFiles( + attachments: PromptInputAttachmentItem[], +): ChatAttachmentFile[] { + return attachments.map((attachment, index) => { + const file = new File(attachment.uri); + return { + name: nameOf(attachment, index), + mimeType: attachment.mediaType ?? FALLBACK_MEDIA_TYPE, + size: file.size ?? 0, + readBase64: () => file.base64(), + }; + }); +} diff --git a/apps/mobile/lib/attachments/worktreeWrite.test.ts b/apps/mobile/lib/attachments/worktreeWrite.test.ts new file mode 100644 index 00000000000..0e4f0ffa940 --- /dev/null +++ b/apps/mobile/lib/attachments/worktreeWrite.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, test } from "bun:test"; +import { WORKSPACE_ATTACHMENTS_DIR } from "@superset/shared/workspace-attachments"; +import { writeAttachmentsToWorktree } from "./worktreeWrite"; + +const DIR = `/repo/${WORKSPACE_ATTACHMENTS_DIR}`; + +describe("writeAttachmentsToWorktree", () => { + test("writes the bytes into the worktree create-only and names around what is there", async () => { + const existing = new Set([`${DIR}/shot.png`]); + const written: string[] = []; + const paths = await writeAttachmentsToWorktree( + [ + { + name: "shot.png", + mimeType: "image/png", + size: 3, + readBase64: async () => "AQID", + }, + ], + { + worktreePath: async () => "/repo", + createDirectory: async () => {}, + writeFile: async ({ absolutePath }) => { + if (existing.has(absolutePath)) + return { ok: false, reason: "exists" }; + existing.add(absolutePath); + written.push(absolutePath); + return { ok: true }; + }, + }, + ); + expect(written).toEqual([ + `${DIR}/.gitignore`, + `${DIR}/shot_1.png`, + ]); + expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/shot_1.png`]); + }); +}); diff --git a/apps/mobile/lib/attachments/worktreeWrite.ts b/apps/mobile/lib/attachments/worktreeWrite.ts new file mode 100644 index 00000000000..94f7c90fd37 --- /dev/null +++ b/apps/mobile/lib/attachments/worktreeWrite.ts @@ -0,0 +1,71 @@ +import { + attachmentFallbackName, + attachmentNameWithSuffix, + sanitizeAttachmentFileName, + WORKSPACE_ATTACHMENTS_DIR, + WORKSPACE_ATTACHMENTS_GITIGNORE, +} from "@superset/shared/workspace-attachments"; +import { assertFitsHost, type ChatAttachmentFile } from "./chatParts"; + +const MAX_NAME_ATTEMPTS = 50; + +export interface WorktreeWriteDeps { + worktreePath: () => Promise; + createDirectory: (absolutePath: string) => Promise; + writeFile: (input: { + absolutePath: string; + content: string | { kind: "base64"; data: string }; + }) => Promise<{ ok: boolean; reason?: string }>; +} + +/** + * Worktree-relative paths for a PTY on a machine someone owns: the bytes go to + * that host in the request and are written create-only, so a later paste never + * overwrites a path an earlier prompt names. + */ +export async function writeAttachmentsToWorktree( + files: ChatAttachmentFile[], + deps: WorktreeWriteDeps, +): Promise { + assertFitsHost(files); + + const dir = `${await deps.worktreePath()}/${WORKSPACE_ATTACHMENTS_DIR}`; + await deps.createDirectory(dir); + const ignore = await deps.writeFile({ + absolutePath: `${dir}/.gitignore`, + content: WORKSPACE_ATTACHMENTS_GITIGNORE, + }); + if (!ignore.ok && ignore.reason !== "exists") { + throw new Error(ignore.reason ?? "write failed"); + } + + const paths: string[] = []; + for (const [index, file] of files.entries()) { + const base = + sanitizeAttachmentFileName(file.name) ?? attachmentFallbackName(index); + const data = await file.readBase64(); + const name = await writeUnique(deps, dir, base, data); + paths.push(`${WORKSPACE_ATTACHMENTS_DIR}/${name}`); + } + return paths; +} + +async function writeUnique( + deps: WorktreeWriteDeps, + dir: string, + base: string, + data: string, +): Promise { + for (let attempt = 0; attempt < MAX_NAME_ATTEMPTS; attempt++) { + const name = attachmentNameWithSuffix(base, attempt); + const outcome = await deps.writeFile({ + absolutePath: `${dir}/${name}`, + content: { kind: "base64", data }, + }); + if (outcome.ok) return name; + if (outcome.reason !== "exists") { + throw new Error(outcome.reason ?? "write failed"); + } + } + throw new Error(`Could not find a free name for ${base}`); +} diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/WorkspaceScreen/WorkspaceScreen.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/WorkspaceScreen/WorkspaceScreen.tsx index 4e750ac2bd5..33e5622f4c1 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/WorkspaceScreen/WorkspaceScreen.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/WorkspaceScreen/WorkspaceScreen.tsx @@ -7,7 +7,9 @@ import type { ComposerSessionTab, } from "@superset/composer"; import { i18n } from "@superset/i18n"; +import { rawErrorMessage } from "@superset/i18n/errors"; import { BRAND } from "@superset/shared/brand"; +import { hostReadsAttachmentParts } from "@superset/shared/host-version"; import { TitlePress } from "@superset/title-press"; import { useQueryClient } from "@tanstack/react-query"; import * as Clipboard from "expo-clipboard"; @@ -36,11 +38,18 @@ import { View, } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; +import type { PromptInputAttachmentItem } from "@/components/ai-elements/prompt-input"; import { Icon } from "@/components/ui/icon"; import { Text } from "@/components/ui/text"; import { useArchivedCloudWorkspaces } from "@/hooks/useArchivedCloudWorkspaces"; import { getHostWorkspacesQueryKey } from "@/hooks/useHostWorkspaces"; import { useWorkspaceHost } from "@/hooks/useWorkspaceHost"; +import { chatAttachmentParts } from "@/lib/attachments/chatParts"; +import { + awaitAttachmentUploads, + setDraftAttachmentHome, + toAttachmentFiles, +} from "@/lib/attachments/upload"; import { getChatTransport } from "@/lib/chat"; import { errorCopy } from "@/lib/errors"; import { @@ -414,6 +423,11 @@ export function WorkspaceScreen() { ? hostServiceUrl(host.organizationId, host.machineId) : null; const hostCompatibility = useHostCompatibility(hostUrl); + const hostVersion = hostCompatibility.hostVersion; + useEffect(() => { + if (!id || isResolving || !host) return; + setDraftAttachmentHome(workspaceDraftKey(id), cloud ? "cloud" : "host"); + }, [id, isResolving, host, cloud]); const { renameWorkspace, deleteWorkspace, copyId, copyLink, shareWorkspace } = useWorkspaceHeaderActions(workspace, host); const pinned = usePinnedWorkspacesStore((state) => @@ -770,35 +784,43 @@ export function WorkspaceScreen() { // separates and delays the Enter, and frames the text as a bracketed paste // only when the running program actually has that mode on. const handleSubmit = useCallback( - async (text: string, attachmentFileIds: string[] = []) => { + async (text: string, attachments: PromptInputAttachmentItem[] = []) => { if (!hostUrl || !activeTerminalId || !id) { throw new Error("Terminal is not connected"); } if (activeIsChat) { if (!chatRef.current) throw new Error("Chat is not connected"); - // A cloud box resolves only the files attached to it. - if (cloud && attachmentFileIds.length > 0) { - await apiClient.cloudWorkspace.attachFiles.mutate({ - id, - fileIds: attachmentFileIds, + const host = getHostServiceClientByUrl(hostUrl); + const home = cloud ? "cloud" : "host"; + const parts = await chatAttachmentParts({ + home, + readsParts: hostReadsAttachmentParts(hostVersion), + files: toAttachmentFiles(attachments), + deps: { + awaitUploads: () => + awaitAttachmentUploads(workspaceDraftKey(id), attachments), + // A cloud box resolves only the files attached to it. + attachFiles: (fileIds) => + apiClient.cloudWorkspace.attachFiles.mutate({ id, fileIds }), + importFromCloud: (fileIds) => + host.attachments.importFromCloud.mutate({ fileIds }), + hostUpload: (input) => host.attachments.upload.mutate(input), + }, + }).catch((error: unknown) => { + posthog.capture("attachments_send_failed", { + surface: "chat", + home, + error: rawErrorMessage(error), + }); + throw error; + }); + if (parts.length > 0) + posthog.capture("attachments_sent", { + surface: "chat", + home, + count: parts.length, }); - } - const imported = - attachmentFileIds.length > 0 - ? await getHostServiceClientByUrl( - hostUrl, - ).attachments.importFromCloud.mutate({ - fileIds: attachmentFileIds, - }) - : []; - await chatRef.current.send( - text, - imported.map((entry) => ({ - attachmentId: entry.attachmentId, - name: entry.originalFilename ?? "attachment", - mimeType: entry.mediaType, - })), - ); + await chatRef.current.send(text, parts); composerRef.current?.blur(); return; } @@ -808,7 +830,7 @@ export function WorkspaceScreen() { text, }); }, - [hostUrl, activeTerminalId, id, activeIsChat, cloud], + [hostUrl, activeTerminalId, id, activeIsChat, cloud, hostVersion], ); const handleQuickKey = useCallback( diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx index 17f573fa96a..cdf07812149 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/UserMessageBubble.tsx @@ -1,6 +1,7 @@ import { Trans } from "@lingui/react/macro"; import { - parsePastedText, + attachmentDisplayName, + parseAttachmentTags, readBookkeeping, userMessageText, } from "@superset/chat/core"; @@ -41,12 +42,31 @@ export function UserMessageBubble({ const note = pending ? null : readBookkeeping(harness, raw); if (note) return {note.label}; - const { text, pastes } = parsePastedText(raw); - const attachments = content.flatMap((part) => - part.type === "attachment" && part.attachmentId - ? [{ ...part, attachmentId: part.attachmentId }] - : [], - ); + const { + text, + attachments: tags, + pastedTexts: pastes, + } = parseAttachmentTags(raw); + const attachments = [ + ...content.flatMap((part) => + part.type === "attachment" && (part.fileId || part.attachmentId) + ? [ + { + key: part.fileId ?? part.attachmentId ?? part.name, + source: { fileId: part.fileId, attachmentId: part.attachmentId }, + name: part.name, + mimeType: part.mimeType, + }, + ] + : [], + ), + ...tags.map((tag) => ({ + key: tag.path, + source: {}, + name: attachmentDisplayName(tag), + mimeType: "", + })), + ]; const wide = attachments.length > 0 || pastes.length > 0; const failed = pending?.failed === true; return ( @@ -74,19 +94,19 @@ export function UserMessageBubble({ {attachments.map((attachment) => isViewableImage(attachment.mimeType) ? ( ) : ( ), )} diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentFileCard/AttachmentFileCard.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentFileCard/AttachmentFileCard.tsx index 9d2e4820425..51911e26645 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentFileCard/AttachmentFileCard.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentFileCard/AttachmentFileCard.tsx @@ -5,24 +5,27 @@ import { FileText } from "lucide-react-native"; import { View } from "react-native"; import { Icon } from "@/components/ui/icon"; import { Text } from "@/components/ui/text"; -import { useAttachmentFile } from "../../hooks/useAttachmentFile"; +import { + type AttachmentSource, + useAttachmentFile, +} from "../../hooks/useAttachmentFile"; const GLYPH_SIZE = 36; export function AttachmentFileCard({ - attachmentId, + source, hostUrl, name, mediaType, }: { - attachmentId: string; + source: AttachmentSource; hostUrl: string | undefined; name: string; mediaType: string; }) { const { data: uri } = useAttachmentFile({ hostUrl, - attachmentId, + source, name, mediaType, }); diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentThumbnail/AttachmentThumbnail.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentThumbnail/AttachmentThumbnail.tsx index ece7b1ba74c..acfe21e4038 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentThumbnail/AttachmentThumbnail.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatRowView/components/UserMessageBubble/components/AttachmentThumbnail/AttachmentThumbnail.tsx @@ -1,31 +1,34 @@ import { presentImage } from "@superset/composer"; import { Image } from "expo-image"; import { Pressable } from "react-native"; -import { useAttachmentFile } from "../../hooks/useAttachmentFile"; +import { + type AttachmentSource, + useAttachmentFile, +} from "../../hooks/useAttachmentFile"; import { AttachmentFileCard } from "../AttachmentFileCard"; export function AttachmentThumbnail({ - attachmentId, + source, hostUrl, name, mediaType, }: { - attachmentId: string; + source: AttachmentSource; hostUrl: string | undefined; name: string; mediaType: string; }) { const { data: uri, isError } = useAttachmentFile({ hostUrl, - attachmentId, + source, name, mediaType, }); - if (isError || !hostUrl) { + if (isError || (!source.fileId && !hostUrl)) { return ( { - if (!hostUrl) throw new Error("no host"); const directory = new Directory(Paths.cache, "sent-attachments"); directory.create({ idempotent: true }); const file = new File( directory, - `${attachmentId}.${extensionOf(name, mediaType)}`, + `${fileId ?? attachmentId}.${extensionOf(name, mediaType)}`, ); - if (!file.exists) { - const { data } = await getHostServiceClientByUrl( - hostUrl, - ).attachments.read.query({ attachmentId }); - file.write(data, { encoding: "base64" }); + if (file.exists) return file.uri; + if (fileId) { + const [resolved] = await apiClient.attachment.resolveEach.mutate({ + fileIds: [fileId], + }); + if (!resolved || !("url" in resolved)) + throw new Error("Attachment is gone"); + await File.downloadFileAsync(resolved.url, file); + return file.uri; } + if (!hostUrl || !attachmentId) throw new Error("no host"); + const { data } = await getHostServiceClientByUrl( + hostUrl, + ).attachments.read.query({ attachmentId }); + file.write(data, { encoding: "base64" }); return file.uri; }, }); diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatSessionView/ChatSessionView.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatSessionView/ChatSessionView.tsx index c885b24a5a4..bb0e2783d3e 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatSessionView/ChatSessionView.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/ChatSessionView/ChatSessionView.tsx @@ -28,6 +28,7 @@ import { import { ActionSheetIOS, Alert, Pressable, View } from "react-native"; import { Conversation } from "@/components/ai-elements/conversation"; import { Text } from "@/components/ui/text"; +import type { ChatAttachmentPart } from "@/lib/attachments/chatParts"; import { type ChatHost, createChatSessionClient, @@ -74,11 +75,7 @@ function activityTexts( ); } -export interface ChatAttachment { - attachmentId: string; - name: string; - mimeType: string; -} +export type ChatAttachment = Omit; export interface ChatSessionViewHandle { send: (text: string, attachments?: ChatAttachment[]) => Promise; @@ -346,8 +343,8 @@ export const ChatSessionView = forwardRef< chat.sendPrompt([ ...(text.trim() ? [{ type: "text" as const, text }] : []), ...attachments.map((attachment) => ({ - type: "attachment" as const, ...attachment, + type: "attachment" as const, })), ]); }, diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/TerminalComposer.tsx b/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/TerminalComposer.tsx index cdfda8b6d78..2a1b66ad47c 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/TerminalComposer.tsx +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/TerminalComposer.tsx @@ -11,8 +11,10 @@ import { import type { SlashCommand } from "@superset/shared/slash-commands"; import { forwardRef, useImperativeHandle, useRef, useState } from "react"; import { Alert, View } from "react-native"; -import type { PromptInputMessage } from "@/components/ai-elements/prompt-input"; -import { awaitAttachmentUploads } from "@/lib/attachments/upload"; +import type { + PromptInputAttachmentItem, + PromptInputMessage, +} from "@/components/ai-elements/prompt-input"; import { errorCopy } from "@/lib/errors"; import { posthog } from "@/lib/posthog"; import { useAttachmentsSheet } from "@/screens/(authenticated)/hooks/useAttachmentsSheet"; @@ -40,7 +42,10 @@ interface TerminalComposerProps { workspaceId: string; placeholder?: string; /** Submit the current draft to the PTY. Rejects if it never got there. */ - onSubmit: (text: string, attachmentFileIds?: string[]) => Promise; + onSubmit: ( + text: string, + attachments?: PromptInputAttachmentItem[], + ) => Promise; onQuickKey: (key: TerminalQuickKey) => void; /** Where attachments land; null while the workspace or host is unresolved. */ attachmentTarget: TerminalAttachmentTarget | null; @@ -95,7 +100,7 @@ interface TerminalComposerProps { onModeSelect?: (modeId: string) => void; canStop?: boolean; onStop?: () => void; - /** Hand attachments over as uploaded file ids, not as worktree paths. */ + /** Hand attachments over as they are, not as worktree paths. */ sendsAttachments?: boolean; } @@ -191,7 +196,7 @@ export const TerminalComposer = forwardRef< const submit = async ({ text, attachments: files }: PromptInputMessage) => { let body = text; - let attachmentFileIds: string[] | undefined; + let sent: PromptInputAttachmentItem[] | undefined; // The tray is shared across tabs, so files attached in an agent session // are still there after switching to a plain shell — which would execute // the paths rather than read them. `allowAttachments` has to gate the @@ -206,16 +211,7 @@ export const TerminalComposer = forwardRef< return; } if (sendsAttachments) { - attachmentFileIds = await awaitAttachmentUploads(draftKey, files).catch( - (cause: unknown) => { - Alert.alert( - t({ message: "Could not attach files" }), - errorCopy(cause), - ); - return undefined; - }, - ); - if (!attachmentFileIds) return; + sent = files; } else { // A PTY takes bytes, not files: the agent gets the attachments as // worktree-relative paths appended to the message. The hook alerts @@ -229,7 +225,7 @@ export const TerminalComposer = forwardRef< } setIsSubmitting(true); try { - await onSubmit(body, attachmentFileIds); + await onSubmit(body, sent); posthog.capture("terminal_rich_input_submitted", { workspace_id: workspaceId, message_length: text.trim().length, diff --git a/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/hooks/useWriteTerminalAttachments/useWriteTerminalAttachments.ts b/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/hooks/useWriteTerminalAttachments/useWriteTerminalAttachments.ts index f6eeba243d7..a259962d80f 100644 --- a/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/hooks/useWriteTerminalAttachments/useWriteTerminalAttachments.ts +++ b/apps/mobile/screens/(authenticated)/workspace/[id]/components/TerminalComposer/hooks/useWriteTerminalAttachments/useWriteTerminalAttachments.ts @@ -4,7 +4,11 @@ import { useMutation } from "@tanstack/react-query"; import { Alert } from "react-native"; import type { PromptInputAttachmentItem } from "@/components/ai-elements/prompt-input"; import { asAttachmentError } from "@/lib/attachments/errors"; -import { awaitAttachmentUploads } from "@/lib/attachments/upload"; +import { + awaitAttachmentUploads, + toAttachmentFiles, +} from "@/lib/attachments/upload"; +import { writeAttachmentsToWorktree } from "@/lib/attachments/worktreeWrite"; import { errorCopy } from "@/lib/errors"; import { getHostServiceClientByUrl } from "@/lib/host-service/client"; import { apiClient } from "@/lib/trpc/client"; @@ -28,30 +32,50 @@ interface WriteArgs { * worktree-relative paths. A live PTY only takes bytes, so paths are how an * attachment reaches the agent. * - * The bytes go device → cloud storage → host, never through the relay: a - * relay request body is buffered whole in a Cloudflare Worker and refused - * above 100 MB by its edge, which is what made a large attachment fail with - * an unparseable response rather than an error anyone could read. - * - * By here the upload is usually finished — it started when the file was - * attached — so this waits only on what the message outpaced. + * A box gets the bytes device → cloud storage → box; the upload started when + * the file was attached. A machine someone owns gets them in the request, + * under the inline cap, so they never reach our storage. */ export function useWriteTerminalAttachments() { return useMutation({ mutationFn: async ({ target, attachments }: WriteArgs) => { if (attachments.length === 0) return []; + if (!target.isCloud) { + const client = getHostServiceClientByUrl(target.hostUrl); + return writeAttachmentsToWorktree(toAttachmentFiles(attachments), { + worktreePath: async () => { + const workspace = await client.workspace.get.query({ + id: target.workspaceId, + }); + if (!workspace?.worktreePath) + throw new Error("Workspace path not found"); + return workspace.worktreePath; + }, + createDirectory: (absolutePath) => + client.filesystem.createDirectory.mutate({ + workspaceId: target.workspaceId, + absolutePath, + recursive: true, + }), + writeFile: async ({ absolutePath, content }) => + (await client.filesystem.writeFile.mutate({ + workspaceId: target.workspaceId, + absolutePath, + content, + options: { create: true, overwrite: false }, + })) ?? { ok: true }, + }); + } const fileIds = await awaitAttachmentUploads( target.draftKey, attachments, ); const client = getHostServiceClientByUrl(target.hostUrl); try { - if (target.isCloud) { - await apiClient.cloudWorkspace.attachFiles.mutate({ - id: target.workspaceId, - fileIds, - }); - } + await apiClient.cloudWorkspace.attachFiles.mutate({ + id: target.workspaceId, + fileIds, + }); const { paths } = await client.attachments.materializeIntoWorkspace.mutate({ workspaceId: target.workspaceId, diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts b/packages/chat/src/core/attachmentTags/attachmentTags.test.ts similarity index 100% rename from apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.test.ts rename to packages/chat/src/core/attachmentTags/attachmentTags.test.ts diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts b/packages/chat/src/core/attachmentTags/attachmentTags.ts similarity index 97% rename from apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts rename to packages/chat/src/core/attachmentTags/attachmentTags.ts index 7a3a40c52b4..9d8107270b4 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/utils/attachmentTags/attachmentTags.ts +++ b/packages/chat/src/core/attachmentTags/attachmentTags.ts @@ -1,8 +1,8 @@ -import { parsePastedText } from "@superset/chat/core"; import { LEGACY_WORKSPACE_ATTACHMENTS_DIR, WORKSPACE_ATTACHMENTS_DIR, } from "@superset/shared/workspace-attachments"; +import { parsePastedText } from "../pastedText/pastedText"; export type AttachmentTag = { path: string; type: string; name?: string }; diff --git a/packages/chat/src/core/index.ts b/packages/chat/src/core/index.ts index c16f640044e..d7dea344ba9 100644 --- a/packages/chat/src/core/index.ts +++ b/packages/chat/src/core/index.ts @@ -1,3 +1,4 @@ +export * from "./attachmentTags/attachmentTags"; export * from "./bookkeeping/readBookkeeping"; export * from "./coalesce"; export * from "./config/launchConfigSelections"; diff --git a/packages/i18n/locales/cs/messages.po b/packages/i18n/locales/cs/messages.po index b22fca8c8d4..5ad4c67596b 100644 --- a/packages/i18n/locales/cs/messages.po +++ b/packages/i18n/locales/cs/messages.po @@ -70,6 +70,9 @@ msgstr "„{0}“ se přestane spouštět a její historie běhů bude odstraně msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "„{name}“ je složka. Připojte místo toho soubory v ní." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "„{name}“ přesahuje {limit}, víc pracovní prostor mimo cloud nepřijme" + msgid "\"{name}\" paused" msgstr "„{name}“ pozastaveno" diff --git a/packages/i18n/locales/de/messages.po b/packages/i18n/locales/de/messages.po index 0b83b757d3a..09ce7e77fc0 100644 --- a/packages/i18n/locales/de/messages.po +++ b/packages/i18n/locales/de/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" wird nicht mehr ausgelöst und der Ausführungsverlauf wird entf msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" ist ein Ordner. Hänge stattdessen die Dateien darin an." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "„{name}“ ist größer als {limit} – mehr nimmt ein Arbeitsbereich außerhalb der Cloud nicht an" + msgid "\"{name}\" paused" msgstr "\"{name}\" pausiert" diff --git a/packages/i18n/locales/en/messages.po b/packages/i18n/locales/en/messages.po index 3fe23f92f79..7d8ceb8c59a 100644 --- a/packages/i18n/locales/en/messages.po +++ b/packages/i18n/locales/en/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" will stop firing and its run history will be removed. This can't msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" is a folder. Attach the files inside it instead." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" + msgid "\"{name}\" paused" msgstr "\"{name}\" paused" diff --git a/packages/i18n/locales/es/messages.po b/packages/i18n/locales/es/messages.po index b1309d127ed..6f8f1c81821 100644 --- a/packages/i18n/locales/es/messages.po +++ b/packages/i18n/locales/es/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" dejará de dispararse y se eliminará su historial de ejecucione msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" es una carpeta. Adjunta los archivos que contiene." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "«{name}» supera {limit}, el máximo que admite un espacio de trabajo fuera de la nube" + msgid "\"{name}\" paused" msgstr "\"{name}\" en pausa" diff --git a/packages/i18n/locales/fr/messages.po b/packages/i18n/locales/fr/messages.po index 4925a5ea9a5..7ac186ea7d7 100644 --- a/packages/i18n/locales/fr/messages.po +++ b/packages/i18n/locales/fr/messages.po @@ -70,6 +70,9 @@ msgstr "« {0} » ne se déclenchera plus et son historique d'exécutions sera s msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "« {name} » est un dossier. Joignez plutôt les fichiers qu’il contient." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "« {name} » dépasse {limit}, le maximum qu’accepte un espace de travail hors du cloud" + msgid "\"{name}\" paused" msgstr "« {name} » mise en pause" diff --git a/packages/i18n/locales/id/messages.po b/packages/i18n/locales/id/messages.po index 31480e941c0..2577070cf8b 100644 --- a/packages/i18n/locales/id/messages.po +++ b/packages/i18n/locales/id/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" akan berhenti berjalan dan riwayat run-nya akan dihapus. Ini tid msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" adalah folder. Lampirkan file di dalamnya sebagai gantinya." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" melebihi {limit}, batas maksimum untuk workspace di luar cloud" + msgid "\"{name}\" paused" msgstr "\"{name}\" dijeda" diff --git a/packages/i18n/locales/it/messages.po b/packages/i18n/locales/it/messages.po index 83b5da1e50d..75ffe655516 100644 --- a/packages/i18n/locales/it/messages.po +++ b/packages/i18n/locales/it/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" smetterà di scattare e la sua cronologia delle esecuzioni verr msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" è una cartella. Allega invece i file al suo interno." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" supera {limit}, il massimo che un workspace fuori dal cloud può ricevere" + msgid "\"{name}\" paused" msgstr "\"{name}\" messa in pausa" diff --git a/packages/i18n/locales/ja/messages.po b/packages/i18n/locales/ja/messages.po index 976704746f3..95b6c208706 100644 --- a/packages/i18n/locales/ja/messages.po +++ b/packages/i18n/locales/ja/messages.po @@ -70,6 +70,9 @@ msgstr "「{0}」は発火しなくなり、実行履歴も削除されます。 msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "「{name}」はフォルダです。代わりにフォルダ内のファイルを添付してください。" +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "「{name}」は {limit} を超えています。クラウド外のワークスペースが受け取れるのは {limit} までです" + msgid "\"{name}\" paused" msgstr "「{name}」を一時停止しました" diff --git a/packages/i18n/locales/ko/messages.po b/packages/i18n/locales/ko/messages.po index a5c91eef47e..55ff0210c80 100644 --- a/packages/i18n/locales/ko/messages.po +++ b/packages/i18n/locales/ko/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\"이(가) 더 이상 실행되지 않으며 실행 기록도 삭 msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\"은(는) 폴더입니다. 대신 폴더 안의 파일을 첨부하세요." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\"이(가) {limit}를 넘습니다. 클라우드 밖의 워크스페이스는 최대 {limit}까지 받을 수 있습니다" + msgid "\"{name}\" paused" msgstr "\"{name}\"을(를) 일시 중지했습니다" diff --git a/packages/i18n/locales/nl/messages.po b/packages/i18n/locales/nl/messages.po index 98d93701b08..dd37438e604 100644 --- a/packages/i18n/locales/nl/messages.po +++ b/packages/i18n/locales/nl/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" wordt niet meer geactiveerd en de geschiedenis van runs wordt ve msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" is een map. Voeg in plaats daarvan de bestanden in de map toe." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" is groter dan {limit}, het maximum dat een werkruimte buiten de cloud aankan" + msgid "\"{name}\" paused" msgstr "\"{name}\" gepauzeerd" diff --git a/packages/i18n/locales/pl/messages.po b/packages/i18n/locales/pl/messages.po index a348e87a6e9..08570a06e75 100644 --- a/packages/i18n/locales/pl/messages.po +++ b/packages/i18n/locales/pl/messages.po @@ -70,6 +70,9 @@ msgstr "„{0}” przestanie się uruchamiać, a jej historia uruchomień zostan msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "„{name}” to folder. Zamiast tego załącz pliki z jego wnętrza." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "„{name}” przekracza {limit} — tyle maksymalnie przyjmuje obszar roboczy poza chmurą" + msgid "\"{name}\" paused" msgstr "„{name}” wstrzymana" diff --git a/packages/i18n/locales/pt-BR/messages.po b/packages/i18n/locales/pt-BR/messages.po index 9ad72a19eac..c0cd320e68e 100644 --- a/packages/i18n/locales/pt-BR/messages.po +++ b/packages/i18n/locales/pt-BR/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" vai parar de disparar e seu histórico de execuções será remo msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" é uma pasta. Anexe os arquivos que estão nela." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" passa de {limit}, o máximo que uma área de trabalho fora da nuvem aceita" + msgid "\"{name}\" paused" msgstr "\"{name}\" pausada" diff --git a/packages/i18n/locales/ru/messages.po b/packages/i18n/locales/ru/messages.po index a9f046c6042..89bcaec21d6 100644 --- a/packages/i18n/locales/ru/messages.po +++ b/packages/i18n/locales/ru/messages.po @@ -70,6 +70,9 @@ msgstr "«{0}» перестанет срабатывать, а история msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "«{name}» — это папка. Прикрепите файлы из неё." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "«{name}» больше {limit} — это максимум для рабочей области вне облака" + msgid "\"{name}\" paused" msgstr "«{name}» приостановлена" diff --git a/packages/i18n/locales/tr/messages.po b/packages/i18n/locales/tr/messages.po index 2b88aa4cb19..4db34ab46c1 100644 --- a/packages/i18n/locales/tr/messages.po +++ b/packages/i18n/locales/tr/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" artık tetiklenmeyecek ve çalıştırma geçmişi kaldırılaca msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" bir klasör. Bunun yerine içindeki dosyaları ekle." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" {limit} sınırını aşıyor; bulut dışındaki bir çalışma alanı en fazla bu kadarını alabilir" + msgid "\"{name}\" paused" msgstr "\"{name}\" duraklatıldı" diff --git a/packages/i18n/locales/vi/messages.po b/packages/i18n/locales/vi/messages.po index 4030ca616c7..4d12dea35a8 100644 --- a/packages/i18n/locales/vi/messages.po +++ b/packages/i18n/locales/vi/messages.po @@ -70,6 +70,9 @@ msgstr "\"{0}\" sẽ ngừng kích hoạt và lịch sử chạy sẽ bị xóa. msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" là một thư mục. Hãy đính kèm các tệp bên trong thay thế." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "\"{name}\" vượt quá {limit}, mức tối đa mà một workspace ngoài cloud có thể nhận" + msgid "\"{name}\" paused" msgstr "Đã tạm dừng \"{name}\"" diff --git a/packages/i18n/locales/zh-CN/messages.po b/packages/i18n/locales/zh-CN/messages.po index 3df1f96a5c2..fd21282e2f8 100644 --- a/packages/i18n/locales/zh-CN/messages.po +++ b/packages/i18n/locales/zh-CN/messages.po @@ -70,6 +70,9 @@ msgstr "“{0}”将不再触发,其运行历史也会被移除。此操作无 msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "“{name}”是一个文件夹。请改为附加其中的文件。" +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "“{name}”超过 {limit},这是云端以外的工作区可接收的上限" + msgid "\"{name}\" paused" msgstr "“{name}”已暂停" diff --git a/packages/i18n/locales/zh-TW/messages.po b/packages/i18n/locales/zh-TW/messages.po index 2e3a6d524a8..fcd60230fc5 100644 --- a/packages/i18n/locales/zh-TW/messages.po +++ b/packages/i18n/locales/zh-TW/messages.po @@ -70,6 +70,9 @@ msgstr "「{0}」將不再觸發,其執行歷史也會被移除。此操作無 msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "「{name}」是資料夾。請改為附加其中的檔案。" +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "「{name}」超過 {limit},這是雲端以外的工作區可接收的上限" + msgid "\"{name}\" paused" msgstr "「{name}」已暫停" From cb1e6f209acc4e3bc0c69fb85321e892e981fe17 Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi Date: Sat, 24 Oct 2026 22:02:23 +0000 Subject: [PATCH 15/16] docs(sandbox): a box's host-service is as new as its last boot The doc assumed deployed boxes never update their host-service. They do: a bundle release repins every live environment, every wake builds the claim from it, and superset-boot installs the pin when its hash differs. Only a box that never boots again keeps an old bundle. Features gate on the version a box reports, like a remote host. --- docs/cloud-sandbox-mismatches.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/cloud-sandbox-mismatches.md b/docs/cloud-sandbox-mismatches.md index 4ce1943e389..b010b75d1f9 100644 --- a/docs/cloud-sandbox-mismatches.md +++ b/docs/cloud-sandbox-mismatches.md @@ -393,8 +393,9 @@ uplink loses it about half the time, a 1 MB body on a datacenter link wins it. This is why shipping file bytes through `filesystem.writeFile` broke (SUPER-2658) and why attachments go through storage instead. host-service now buffers every `/trpc/*` body before handing it to tRPC, so no response starts -before the request is complete; a sandbox provisioned before that bundle still -has the race for any request with a sizeable body. Reproducing with curl: pass +before the request is complete. A box runs that host-service from its next +boot (see "A box's host-service is as new as its last boot"); one that never +sleeps keeps the race until it restarts. Reproducing with curl: pass `-H "Expect:"` — curl's automatic `Expect: 100-continue` on bodies ≥1 MB masks the bug, and browsers never send it. @@ -444,6 +445,19 @@ missing. The CLI bundle has the same gap. ## Lifecycle +**A box's host-service is as new as its last boot.** A sandbox bundle release +repins every live environment (`environments.bundleSha`, set by +`packages/sandbox/src/environments/release.ts`), and every wake builds the +box's claim from its environment (`wakeCloudWorkspace` → `buildSandboxClaim`). +When the box boots, `superset-boot` installs the pinned bundle if its hash +differs from the one installed. So a cold wake, a restart or a new box runs +the current host-service. A warm resume does not boot, and a box that never +sleeps keeps the host-service it last booted with. Do not assume a box is +frozen at the bundle it was provisioned with, and do not assume it is current +either: gate a new host feature on the version the box reports +(`health.check`), the same way as a remote host. The chat attachment parts +floor (`ATTACHMENT_PARTS_MIN_HOST_VERSION`) works like this. + **A tag-filtered sandbox list is not sorted by status time. Fixed.** The usage reconciler scans by `statusUpdatedAt` and stops at entries older than the maximum session length. Vercel's server-side tag filter uses creation-order pagination From 635f21d54ba6017485db26fac80fd0f7d75d1f4a Mon Sep 17 00:00:00 2001 From: Harshith Mullapudi <17528887+harshithmullapudi@users.noreply.github.com> Date: Mon, 2 Nov 2026 20:20:58 +0000 Subject: [PATCH 16/16] fix(attachments): finish folding the attachments plan into #9064 - ATTACHMENT_PARTS_MIN_HOST_VERSION is 2.0.4. 2.0.0 to 2.0.3 shipped without the adapters that read attachment parts, so the old 1.41.0 floor let those hosts receive parts they would drop. - The Files tab caps a drop at MAX_INLINE_ATTACHMENT_BYTES only for a workspace on another machine. A local drop keeps no cap, as before. - The cloud workspace attachment inputs in packages/trpc and desktop's thumbnail limit come from @superset/shared/attachment-limits. The values are the same. - The clipboard paragraph in cloud-sandbox-mismatches covers the same-machine route. - Translations for the new messages, including the Ukrainian catalog that main added. --- .../useUploadAttachments.ts | 11 +++++++++- .../useChatImageSource/useChatImageSource.ts | 9 +++++--- .../lib/attachments/worktreeWrite.test.ts | 5 +---- docs/cloud-sandbox-mismatches.md | 9 +++++--- .../fix-login-67cf0731/README.md | 1 + .../fix-login-6aae80e0/README.md | 1 + .../fix-login-10cfe30e/README.md | 1 + .../team/fix-login-cd8985ff/README.md | 1 + .../fix-login-f02297e8/README.md | 1 + .../fix-login-b9ef52ac/README.md | 1 + .../fix-login-a9abda00/README.md | 1 + .../team/typed-branch/README.md | 1 + .../fix-login-1f3b8799/README.md | 1 + .../fix-login-ac841533/README.md | 1 + .../explicit-a/README.md | 1 + .../what-git-rebase-84dd146c/README.md | 1 + .../fix-login-957c17db/README.md | 1 + .../fix-login-b8e1150c/README.md | 1 + .../my-workspace/README.md | 1 + .../fix-login-2e40daa5/README.md | 1 + .../fix-login-9417ec7e/README.md | 1 + .../fix-login-c6c1cba2/README.md | 1 + .../fix-login-82c2b237/README.md | 1 + .../fix-login-9f382856/README.md | 1 + .../fix-login-596f3d35/README.md | 1 + .../fix-login-f935e51b/README.md | 1 + .../fix-login-e826c9cb/README.md | 1 + .../fix-login-ac1d6896/README.md | 1 + .../fix-login-c670e5a5/README.md | 1 + .../fix-login-73cfe67d/README.md | 1 + .../fix-login-2ce7ee03/README.md | 1 + .../fix-login-eb8529e7/README.md | 1 + .../fix-login-169943be/README.md | 1 + .../energetic-apparatus-15b2ff09/README.md | 1 + .../fix-login-4fc97805/README.md | 1 + .../fix-login-74110ff0/README.md | 1 + .../east-trust-b2813850/README.md | 1 + packages/i18n/locales/cs/messages.po | 18 ++++++++++++++++ packages/i18n/locales/de/messages.po | 18 ++++++++++++++++ packages/i18n/locales/en/messages.po | 18 ++++++++++++++++ packages/i18n/locales/es/messages.po | 18 ++++++++++++++++ packages/i18n/locales/fr/messages.po | 18 ++++++++++++++++ packages/i18n/locales/id/messages.po | 18 ++++++++++++++++ packages/i18n/locales/it/messages.po | 18 ++++++++++++++++ packages/i18n/locales/ja/messages.po | 18 ++++++++++++++++ packages/i18n/locales/ko/messages.po | 18 ++++++++++++++++ packages/i18n/locales/nl/messages.po | 18 ++++++++++++++++ packages/i18n/locales/pl/messages.po | 18 ++++++++++++++++ packages/i18n/locales/pt-BR/messages.po | 18 ++++++++++++++++ packages/i18n/locales/ru/messages.po | 18 ++++++++++++++++ packages/i18n/locales/tr/messages.po | 18 ++++++++++++++++ packages/i18n/locales/uk/messages.po | 21 +++++++++++++++++++ packages/i18n/locales/vi/messages.po | 18 ++++++++++++++++ packages/i18n/locales/zh-CN/messages.po | 18 ++++++++++++++++ packages/i18n/locales/zh-TW/messages.po | 18 ++++++++++++++++ packages/shared/src/attachment-limits.ts | 2 +- .../router/cloud-workspace/cloud-workspace.ts | 6 +++++- .../trpc/src/router/cloud-workspace/record.ts | 3 ++- 58 files changed, 391 insertions(+), 14 deletions(-) create mode 100644 packages/host-service/undefined/worktrees/01017e7a-7b49-4a96-9d2f-08194c589568/fix-login-67cf0731/README.md create mode 100644 packages/host-service/undefined/worktrees/066a7e6b-23b2-49e3-8286-1436201d840b/fix-login-6aae80e0/README.md create mode 100644 packages/host-service/undefined/worktrees/0ff2771a-67a7-4834-96f3-50f1add0a8c1/fix-login-10cfe30e/README.md create mode 100644 packages/host-service/undefined/worktrees/16702455-2a7a-436a-82d3-c6dd259165e9/team/fix-login-cd8985ff/README.md create mode 100644 packages/host-service/undefined/worktrees/17344ed2-1498-4c3e-bbd7-337b1af7c44e/fix-login-f02297e8/README.md create mode 100644 packages/host-service/undefined/worktrees/1b2c8bac-9da1-4898-bd94-cec5e01c8730/fix-login-b9ef52ac/README.md create mode 100644 packages/host-service/undefined/worktrees/2697d4a4-3f8d-4b9e-8999-e11a840406c9/fix-login-a9abda00/README.md create mode 100644 packages/host-service/undefined/worktrees/2fc79365-e18c-4879-98e3-cac5f33bf5e5/team/typed-branch/README.md create mode 100644 packages/host-service/undefined/worktrees/4f609fdf-a33c-4a12-bed1-05db2272b127/fix-login-1f3b8799/README.md create mode 100644 packages/host-service/undefined/worktrees/5203447c-fada-4798-aef5-34c05d3a65a1/fix-login-ac841533/README.md create mode 100644 packages/host-service/undefined/worktrees/54da92b3-79e9-4a68-b22b-ae2df3569e84/explicit-a/README.md create mode 100644 packages/host-service/undefined/worktrees/58ee600b-df7d-4589-b1b8-3808651b2a4b/what-git-rebase-84dd146c/README.md create mode 100644 packages/host-service/undefined/worktrees/59306363-b823-4997-8f3b-dda7cd0c6d40/fix-login-957c17db/README.md create mode 100644 packages/host-service/undefined/worktrees/83139903-2396-4c36-8e1b-7d2e311ea937/fix-login-b8e1150c/README.md create mode 100644 packages/host-service/undefined/worktrees/94c7f164-6f57-42e5-bf43-1630b3fb650d/my-workspace/README.md create mode 100644 packages/host-service/undefined/worktrees/94f2904e-772d-44d7-b00f-42228adaeeb5/fix-login-2e40daa5/README.md create mode 100644 packages/host-service/undefined/worktrees/957c7815-bc28-4611-b82d-ed875964a2b4/fix-login-9417ec7e/README.md create mode 100644 packages/host-service/undefined/worktrees/9609bb27-7c51-4ef6-9c73-c36e1656f5c9/fix-login-c6c1cba2/README.md create mode 100644 packages/host-service/undefined/worktrees/962e6d59-6f5f-4b77-b11d-17590ae306a6/fix-login-82c2b237/README.md create mode 100644 packages/host-service/undefined/worktrees/992d0d5f-2899-4b69-aaa4-c598ad3ce75a/fix-login-9f382856/README.md create mode 100644 packages/host-service/undefined/worktrees/a94215c5-06e7-4ddb-9256-e5f7b58cc0de/fix-login-596f3d35/README.md create mode 100644 packages/host-service/undefined/worktrees/ac072299-17ec-4b35-a337-d09ab65d05de/fix-login-f935e51b/README.md create mode 100644 packages/host-service/undefined/worktrees/ac1213d7-ec34-4d3c-9759-93d49c231f86/fix-login-e826c9cb/README.md create mode 100644 packages/host-service/undefined/worktrees/af23535c-042d-4e4d-8e79-129d1fe8beba/fix-login-ac1d6896/README.md create mode 100644 packages/host-service/undefined/worktrees/b34d48f9-1f26-4b2d-8922-9b8b3bced6c8/fix-login-c670e5a5/README.md create mode 100644 packages/host-service/undefined/worktrees/c131738e-7063-4be6-96bd-c453d11e830e/fix-login-73cfe67d/README.md create mode 100644 packages/host-service/undefined/worktrees/c1c7d29d-850a-413b-b46b-2249e3c581d2/fix-login-2ce7ee03/README.md create mode 100644 packages/host-service/undefined/worktrees/c7efbdcf-1750-462e-b7db-33d38b2ea1a6/fix-login-eb8529e7/README.md create mode 100644 packages/host-service/undefined/worktrees/ca100d58-27df-4bf8-958f-237f768aadfb/fix-login-169943be/README.md create mode 100644 packages/host-service/undefined/worktrees/eb94f4fe-2b6b-442b-a605-73b9ebc5d82c/energetic-apparatus-15b2ff09/README.md create mode 100644 packages/host-service/undefined/worktrees/f5a30032-56f1-4cb9-976f-22251cccbdc0/fix-login-4fc97805/README.md create mode 100644 packages/host-service/undefined/worktrees/fb3a5161-eefc-4660-81fa-23109f08e930/fix-login-74110ff0/README.md create mode 100644 packages/host-service/undefined/worktrees/fdf10566-c9f8-4bb8-a894-4b1317395277/east-trust-b2813850/README.md diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts index 3d8c218c40c..e6c12cf0965 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Composer/hooks/useUploadAttachments/useUploadAttachments.ts @@ -130,6 +130,15 @@ export function useUploadAttachments(workspaceId: string) { }; } }, - [host, queryClient, readsParts, route, sendPaths, t, workspaceId, worktreePath], + [ + host, + queryClient, + readsParts, + route, + sendPaths, + t, + workspaceId, + worktreePath, + ], ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useChatImageSource/useChatImageSource.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useChatImageSource/useChatImageSource.ts index a3e3a4bb4b2..bee05692299 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useChatImageSource/useChatImageSource.ts +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/hooks/useChatImageSource/useChatImageSource.ts @@ -1,9 +1,10 @@ -import { MAX_CHAT_IMAGE_BYTES } from "@superset/shared/attachment-limits"; +import { + MAX_CHAT_IMAGE_BYTES, + MAX_THUMBNAIL_BYTES, +} from "@superset/shared/attachment-limits"; import { workspaceTrpc } from "@superset/workspace-client"; import { useChatPaneActions } from "../../../../providers/ChatPaneActionsProvider"; -export const MAX_THUMBNAIL_BYTES = 5 * 1024 * 1024; - const READ_OPTIONS = { staleTime: Number.POSITIVE_INFINITY, retry: false, @@ -20,6 +21,8 @@ export type ChatImageSource = * capped; `full` follows it with the larger read for a file over the cap, so * a thumbnail and the overlay showing the same file share one cached read. */ +export { MAX_THUMBNAIL_BYTES }; + export function useChatImageSource( image: { path: string; mimeType: string } | null, { full }: { full: boolean }, diff --git a/apps/mobile/lib/attachments/worktreeWrite.test.ts b/apps/mobile/lib/attachments/worktreeWrite.test.ts index 0e4f0ffa940..ee6048e1438 100644 --- a/apps/mobile/lib/attachments/worktreeWrite.test.ts +++ b/apps/mobile/lib/attachments/worktreeWrite.test.ts @@ -29,10 +29,7 @@ describe("writeAttachmentsToWorktree", () => { }, }, ); - expect(written).toEqual([ - `${DIR}/.gitignore`, - `${DIR}/shot_1.png`, - ]); + expect(written).toEqual([`${DIR}/.gitignore`, `${DIR}/shot_1.png`]); expect(paths).toEqual([`${WORKSPACE_ATTACHMENTS_DIR}/shot_1.png`]); }); }); diff --git a/docs/cloud-sandbox-mismatches.md b/docs/cloud-sandbox-mismatches.md index b010b75d1f9..be60431554f 100644 --- a/docs/cloud-sandbox-mismatches.md +++ b/docs/cloud-sandbox-mismatches.md @@ -378,9 +378,12 @@ The bytes go where the workspace lives. A box gets them through storage: `attachment.createUpload`, a presigned PUT, `cloudWorkspace.attachFiles`, then `attachments.materializeIntoWorkspace` on the box. A relay-reached machine gets them as base64 `filesystem.writeFile`, capped at -`MAX_INLINE_ATTACHMENT_BYTES`, so they never reach our servers. Chat -attachments follow the same rule as parts: a `fileId` for a box, an -`attachmentId` in the host's store otherwise. +`MAX_INLINE_ATTACHMENT_BYTES`, so they never reach our servers. A workspace +on the same machine as the desktop gets them from the desktop's main process +(`attachments.writeIntoWorktree`), which copies a file from disk with no cap. +Chat attachments follow the same rule as parts: a `fileId` for a box, an +`attachmentId` in the host's store otherwise. A file on this machine too big +for a part goes as a copied path instead. **The ingress drops an upload the host answers early.** tRPC's streamed batch (`httpBatchStreamLink`, `trpc-accept: application/jsonl`) returns its response diff --git a/packages/host-service/undefined/worktrees/01017e7a-7b49-4a96-9d2f-08194c589568/fix-login-67cf0731/README.md b/packages/host-service/undefined/worktrees/01017e7a-7b49-4a96-9d2f-08194c589568/fix-login-67cf0731/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/01017e7a-7b49-4a96-9d2f-08194c589568/fix-login-67cf0731/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/066a7e6b-23b2-49e3-8286-1436201d840b/fix-login-6aae80e0/README.md b/packages/host-service/undefined/worktrees/066a7e6b-23b2-49e3-8286-1436201d840b/fix-login-6aae80e0/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/066a7e6b-23b2-49e3-8286-1436201d840b/fix-login-6aae80e0/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/0ff2771a-67a7-4834-96f3-50f1add0a8c1/fix-login-10cfe30e/README.md b/packages/host-service/undefined/worktrees/0ff2771a-67a7-4834-96f3-50f1add0a8c1/fix-login-10cfe30e/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/0ff2771a-67a7-4834-96f3-50f1add0a8c1/fix-login-10cfe30e/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/16702455-2a7a-436a-82d3-c6dd259165e9/team/fix-login-cd8985ff/README.md b/packages/host-service/undefined/worktrees/16702455-2a7a-436a-82d3-c6dd259165e9/team/fix-login-cd8985ff/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/16702455-2a7a-436a-82d3-c6dd259165e9/team/fix-login-cd8985ff/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/17344ed2-1498-4c3e-bbd7-337b1af7c44e/fix-login-f02297e8/README.md b/packages/host-service/undefined/worktrees/17344ed2-1498-4c3e-bbd7-337b1af7c44e/fix-login-f02297e8/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/17344ed2-1498-4c3e-bbd7-337b1af7c44e/fix-login-f02297e8/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/1b2c8bac-9da1-4898-bd94-cec5e01c8730/fix-login-b9ef52ac/README.md b/packages/host-service/undefined/worktrees/1b2c8bac-9da1-4898-bd94-cec5e01c8730/fix-login-b9ef52ac/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/1b2c8bac-9da1-4898-bd94-cec5e01c8730/fix-login-b9ef52ac/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/2697d4a4-3f8d-4b9e-8999-e11a840406c9/fix-login-a9abda00/README.md b/packages/host-service/undefined/worktrees/2697d4a4-3f8d-4b9e-8999-e11a840406c9/fix-login-a9abda00/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/2697d4a4-3f8d-4b9e-8999-e11a840406c9/fix-login-a9abda00/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/2fc79365-e18c-4879-98e3-cac5f33bf5e5/team/typed-branch/README.md b/packages/host-service/undefined/worktrees/2fc79365-e18c-4879-98e3-cac5f33bf5e5/team/typed-branch/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/2fc79365-e18c-4879-98e3-cac5f33bf5e5/team/typed-branch/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/4f609fdf-a33c-4a12-bed1-05db2272b127/fix-login-1f3b8799/README.md b/packages/host-service/undefined/worktrees/4f609fdf-a33c-4a12-bed1-05db2272b127/fix-login-1f3b8799/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/4f609fdf-a33c-4a12-bed1-05db2272b127/fix-login-1f3b8799/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/5203447c-fada-4798-aef5-34c05d3a65a1/fix-login-ac841533/README.md b/packages/host-service/undefined/worktrees/5203447c-fada-4798-aef5-34c05d3a65a1/fix-login-ac841533/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/5203447c-fada-4798-aef5-34c05d3a65a1/fix-login-ac841533/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/54da92b3-79e9-4a68-b22b-ae2df3569e84/explicit-a/README.md b/packages/host-service/undefined/worktrees/54da92b3-79e9-4a68-b22b-ae2df3569e84/explicit-a/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/54da92b3-79e9-4a68-b22b-ae2df3569e84/explicit-a/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/58ee600b-df7d-4589-b1b8-3808651b2a4b/what-git-rebase-84dd146c/README.md b/packages/host-service/undefined/worktrees/58ee600b-df7d-4589-b1b8-3808651b2a4b/what-git-rebase-84dd146c/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/58ee600b-df7d-4589-b1b8-3808651b2a4b/what-git-rebase-84dd146c/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/59306363-b823-4997-8f3b-dda7cd0c6d40/fix-login-957c17db/README.md b/packages/host-service/undefined/worktrees/59306363-b823-4997-8f3b-dda7cd0c6d40/fix-login-957c17db/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/59306363-b823-4997-8f3b-dda7cd0c6d40/fix-login-957c17db/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/83139903-2396-4c36-8e1b-7d2e311ea937/fix-login-b8e1150c/README.md b/packages/host-service/undefined/worktrees/83139903-2396-4c36-8e1b-7d2e311ea937/fix-login-b8e1150c/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/83139903-2396-4c36-8e1b-7d2e311ea937/fix-login-b8e1150c/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/94c7f164-6f57-42e5-bf43-1630b3fb650d/my-workspace/README.md b/packages/host-service/undefined/worktrees/94c7f164-6f57-42e5-bf43-1630b3fb650d/my-workspace/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/94c7f164-6f57-42e5-bf43-1630b3fb650d/my-workspace/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/94f2904e-772d-44d7-b00f-42228adaeeb5/fix-login-2e40daa5/README.md b/packages/host-service/undefined/worktrees/94f2904e-772d-44d7-b00f-42228adaeeb5/fix-login-2e40daa5/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/94f2904e-772d-44d7-b00f-42228adaeeb5/fix-login-2e40daa5/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/957c7815-bc28-4611-b82d-ed875964a2b4/fix-login-9417ec7e/README.md b/packages/host-service/undefined/worktrees/957c7815-bc28-4611-b82d-ed875964a2b4/fix-login-9417ec7e/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/957c7815-bc28-4611-b82d-ed875964a2b4/fix-login-9417ec7e/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/9609bb27-7c51-4ef6-9c73-c36e1656f5c9/fix-login-c6c1cba2/README.md b/packages/host-service/undefined/worktrees/9609bb27-7c51-4ef6-9c73-c36e1656f5c9/fix-login-c6c1cba2/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/9609bb27-7c51-4ef6-9c73-c36e1656f5c9/fix-login-c6c1cba2/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/962e6d59-6f5f-4b77-b11d-17590ae306a6/fix-login-82c2b237/README.md b/packages/host-service/undefined/worktrees/962e6d59-6f5f-4b77-b11d-17590ae306a6/fix-login-82c2b237/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/962e6d59-6f5f-4b77-b11d-17590ae306a6/fix-login-82c2b237/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/992d0d5f-2899-4b69-aaa4-c598ad3ce75a/fix-login-9f382856/README.md b/packages/host-service/undefined/worktrees/992d0d5f-2899-4b69-aaa4-c598ad3ce75a/fix-login-9f382856/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/992d0d5f-2899-4b69-aaa4-c598ad3ce75a/fix-login-9f382856/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/a94215c5-06e7-4ddb-9256-e5f7b58cc0de/fix-login-596f3d35/README.md b/packages/host-service/undefined/worktrees/a94215c5-06e7-4ddb-9256-e5f7b58cc0de/fix-login-596f3d35/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/a94215c5-06e7-4ddb-9256-e5f7b58cc0de/fix-login-596f3d35/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/ac072299-17ec-4b35-a337-d09ab65d05de/fix-login-f935e51b/README.md b/packages/host-service/undefined/worktrees/ac072299-17ec-4b35-a337-d09ab65d05de/fix-login-f935e51b/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/ac072299-17ec-4b35-a337-d09ab65d05de/fix-login-f935e51b/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/ac1213d7-ec34-4d3c-9759-93d49c231f86/fix-login-e826c9cb/README.md b/packages/host-service/undefined/worktrees/ac1213d7-ec34-4d3c-9759-93d49c231f86/fix-login-e826c9cb/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/ac1213d7-ec34-4d3c-9759-93d49c231f86/fix-login-e826c9cb/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/af23535c-042d-4e4d-8e79-129d1fe8beba/fix-login-ac1d6896/README.md b/packages/host-service/undefined/worktrees/af23535c-042d-4e4d-8e79-129d1fe8beba/fix-login-ac1d6896/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/af23535c-042d-4e4d-8e79-129d1fe8beba/fix-login-ac1d6896/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/b34d48f9-1f26-4b2d-8922-9b8b3bced6c8/fix-login-c670e5a5/README.md b/packages/host-service/undefined/worktrees/b34d48f9-1f26-4b2d-8922-9b8b3bced6c8/fix-login-c670e5a5/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/b34d48f9-1f26-4b2d-8922-9b8b3bced6c8/fix-login-c670e5a5/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/c131738e-7063-4be6-96bd-c453d11e830e/fix-login-73cfe67d/README.md b/packages/host-service/undefined/worktrees/c131738e-7063-4be6-96bd-c453d11e830e/fix-login-73cfe67d/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/c131738e-7063-4be6-96bd-c453d11e830e/fix-login-73cfe67d/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/c1c7d29d-850a-413b-b46b-2249e3c581d2/fix-login-2ce7ee03/README.md b/packages/host-service/undefined/worktrees/c1c7d29d-850a-413b-b46b-2249e3c581d2/fix-login-2ce7ee03/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/c1c7d29d-850a-413b-b46b-2249e3c581d2/fix-login-2ce7ee03/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/c7efbdcf-1750-462e-b7db-33d38b2ea1a6/fix-login-eb8529e7/README.md b/packages/host-service/undefined/worktrees/c7efbdcf-1750-462e-b7db-33d38b2ea1a6/fix-login-eb8529e7/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/c7efbdcf-1750-462e-b7db-33d38b2ea1a6/fix-login-eb8529e7/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/ca100d58-27df-4bf8-958f-237f768aadfb/fix-login-169943be/README.md b/packages/host-service/undefined/worktrees/ca100d58-27df-4bf8-958f-237f768aadfb/fix-login-169943be/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/ca100d58-27df-4bf8-958f-237f768aadfb/fix-login-169943be/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/eb94f4fe-2b6b-442b-a605-73b9ebc5d82c/energetic-apparatus-15b2ff09/README.md b/packages/host-service/undefined/worktrees/eb94f4fe-2b6b-442b-a605-73b9ebc5d82c/energetic-apparatus-15b2ff09/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/eb94f4fe-2b6b-442b-a605-73b9ebc5d82c/energetic-apparatus-15b2ff09/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/f5a30032-56f1-4cb9-976f-22251cccbdc0/fix-login-4fc97805/README.md b/packages/host-service/undefined/worktrees/f5a30032-56f1-4cb9-976f-22251cccbdc0/fix-login-4fc97805/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/f5a30032-56f1-4cb9-976f-22251cccbdc0/fix-login-4fc97805/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/fb3a5161-eefc-4660-81fa-23109f08e930/fix-login-74110ff0/README.md b/packages/host-service/undefined/worktrees/fb3a5161-eefc-4660-81fa-23109f08e930/fix-login-74110ff0/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/fb3a5161-eefc-4660-81fa-23109f08e930/fix-login-74110ff0/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/host-service/undefined/worktrees/fdf10566-c9f8-4bb8-a894-4b1317395277/east-trust-b2813850/README.md b/packages/host-service/undefined/worktrees/fdf10566-c9f8-4bb8-a894-4b1317395277/east-trust-b2813850/README.md new file mode 100644 index 00000000000..6d4d45bcd12 --- /dev/null +++ b/packages/host-service/undefined/worktrees/fdf10566-c9f8-4bb8-a894-4b1317395277/east-trust-b2813850/README.md @@ -0,0 +1 @@ +initial commit \ No newline at end of file diff --git a/packages/i18n/locales/cs/messages.po b/packages/i18n/locales/cs/messages.po index 5ad4c67596b..6d42b64e132 100644 --- a/packages/i18n/locales/cs/messages.po +++ b/packages/i18n/locales/cs/messages.po @@ -67,6 +67,12 @@ msgstr "„{0}“ bude sloučen do {1} pomocí {2}. Odsud to nelze vrátit zpět msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "„{0}“ se přestane spouštět a její historie běhů bude odstraněna. Tuto akci nelze vrátit zpět." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "„{name}“ má {size} — soubory nad {limit} nelze připojit" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "„{name}“ má {size} — soubory nad {limit} nelze k tomuto pracovnímu prostoru připojit. Pokud je soubor na počítači pracovního prostoru, uveďte místo toho jeho cestu." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "„{name}“ je složka. Připojte místo toho soubory v ní." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Použit # zdroj} few {Použity # zdroje} many {Pou msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {použit # nástroj} few {použity # nástroje} many {použito # nástroje} other {použito # nástrojů}}" +msgid "{count} files are over {limit} and were not added" +msgstr "Soubory nad {limit} nebyly přidány: {count}" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "Připojené soubory: {count} — zpráva může obsahovat nejvýše {max}" + msgid "{count} GitHub stars" msgstr "{count} hvězd na GitHubu" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — zapomenout tento web" +msgid "1 file is over {limit} and was not added" +msgstr "1 soubor přesahuje {limit} a nebyl přidán" + msgid "1 hr" msgstr "1 h" @@ -19169,6 +19184,9 @@ msgstr "Stejná aplikace běží na iPadu nativně, na výšku i na šířku." msgid "The scorecard" msgstr "Hodnocení" +msgid "The selected element's context is too large to send" +msgstr "Kontext vybraného prvku je příliš velký na odeslání" + msgid "The self-driving software factory" msgstr "Samořídící softwarová továrna" diff --git a/packages/i18n/locales/de/messages.po b/packages/i18n/locales/de/messages.po index 09ce7e77fc0..82f157781aa 100644 --- a/packages/i18n/locales/de/messages.po +++ b/packages/i18n/locales/de/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" wird per {2} in {1} zusammengeführt. Das lässt sich von hier a msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" wird nicht mehr ausgelöst und der Ausführungsverlauf wird entfernt. Das lässt sich nicht rückgängig machen." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "„{name}“ ist {size} groß – Dateien über {limit} können nicht angehängt werden" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "„{name}“ ist {size} groß – Dateien über {limit} können an diesen Arbeitsbereich nicht angehängt werden. Liegt die Datei auf dem Rechner des Arbeitsbereichs, gib stattdessen ihren Pfad an." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" ist ein Ordner. Hänge stattdessen die Dateien darin an." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {# Quelle genutzt} other {# Quellen genutzt}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {# Werkzeug verwendet} other {# Werkzeuge verwendet}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} Dateien sind größer als {limit} und wurden nicht hinzugefügt" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} Dateien angehängt – eine Nachricht kann höchstens {max} enthalten" + msgid "{count} GitHub stars" msgstr "{count} GitHub-Stars" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 Cookie — diese Website vergessen" +msgid "1 file is over {limit} and was not added" +msgstr "1 Datei ist größer als {limit} und wurde nicht hinzugefügt" + msgid "1 hr" msgstr "1 Std." @@ -19169,6 +19184,9 @@ msgstr "Dieselbe App läuft nativ auf dem iPad, im Hoch- und im Querformat." msgid "The scorecard" msgstr "Der Scorecard" +msgid "The selected element's context is too large to send" +msgstr "Der Kontext des ausgewählten Elements ist zu groß zum Senden" + msgid "The self-driving software factory" msgstr "Die selbstfahrende Softwarefabrik" diff --git a/packages/i18n/locales/en/messages.po b/packages/i18n/locales/en/messages.po index 7d8ceb8c59a..c57008cf0de 100644 --- a/packages/i18n/locales/en/messages.po +++ b/packages/i18n/locales/en/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" will be merged into {1} via {2}. This can't be undone from here. msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" will stop firing and its run history will be removed. This can't be undone." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" is {size} — files over {limit} can't be attached" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" is a folder. Attach the files inside it instead." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Used # source} other {Used # sources}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {used # tool} other {used # tools}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} files are over {limit} and were not added" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} files attached — a message carries at most {max}" + msgid "{count} GitHub stars" msgstr "{count} GitHub stars" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — forget this site" +msgid "1 file is over {limit} and was not added" +msgstr "1 file is over {limit} and was not added" + msgid "1 hr" msgstr "1 hr" @@ -19169,6 +19184,9 @@ msgstr "The same app runs natively on iPad, in portrait and landscape." msgid "The scorecard" msgstr "The scorecard" +msgid "The selected element's context is too large to send" +msgstr "The selected element's context is too large to send" + msgid "The self-driving software factory" msgstr "The self-driving software factory" diff --git a/packages/i18n/locales/es/messages.po b/packages/i18n/locales/es/messages.po index 6f8f1c81821..0dae73f39bf 100644 --- a/packages/i18n/locales/es/messages.po +++ b/packages/i18n/locales/es/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" se fusionará en {1} mediante {2}. No se puede deshacer desde aq msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" dejará de dispararse y se eliminará su historial de ejecuciones. Esto no se puede deshacer." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "«{name}» ocupa {size}: no se pueden adjuntar archivos de más de {limit}" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "«{name}» ocupa {size}: no se pueden adjuntar archivos de más de {limit} a este espacio de trabajo. Si el archivo está en la máquina del espacio de trabajo, menciona su ruta." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" es una carpeta. Adjunta los archivos que contiene." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Se usó # fuente} other {Se usaron # fuentes}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {usó # herramienta} other {usó # herramientas}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} archivos superan {limit} y no se añadieron" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} archivos adjuntos: un mensaje admite como máximo {max}" + msgid "{count} GitHub stars" msgstr "{count} estrellas en GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie: olvidar este sitio" +msgid "1 file is over {limit} and was not added" +msgstr "1 archivo supera {limit} y no se añadió" + msgid "1 hr" msgstr "1 h" @@ -19169,6 +19184,9 @@ msgstr "La misma app funciona de forma nativa en iPad, en vertical y en horizont msgid "The scorecard" msgstr "El cuadro de resultados" +msgid "The selected element's context is too large to send" +msgstr "El contexto del elemento seleccionado es demasiado grande para enviarlo" + msgid "The self-driving software factory" msgstr "La fábrica de software autónoma" diff --git a/packages/i18n/locales/fr/messages.po b/packages/i18n/locales/fr/messages.po index 7ac186ea7d7..053d20a69e7 100644 --- a/packages/i18n/locales/fr/messages.po +++ b/packages/i18n/locales/fr/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" sera fusionnée dans {1} via {2}. Cette action est irréversible msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "« {0} » ne se déclenchera plus et son historique d'exécutions sera supprimé. Cette action est irréversible." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "« {name} » fait {size} — les fichiers de plus de {limit} ne peuvent pas être joints" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "« {name} » fait {size} — les fichiers de plus de {limit} ne peuvent pas être joints à cet espace de travail. Si le fichier se trouve sur la machine de l’espace de travail, indiquez plutôt son chemin." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "« {name} » est un dossier. Joignez plutôt les fichiers qu’il contient." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {# source utilisée} other {# sources utilisées}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {# outil utilisé} other {# outils utilisés}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} fichiers dépassent {limit} et n’ont pas été ajoutés" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} fichiers joints — un message en contient au plus {max}" + msgid "{count} GitHub stars" msgstr "{count} étoiles GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — oublier ce site" +msgid "1 file is over {limit} and was not added" +msgstr "1 fichier dépasse {limit} et n’a pas été ajouté" + msgid "1 hr" msgstr "1 h" @@ -19169,6 +19184,9 @@ msgstr "La même app fonctionne nativement sur iPad, en portrait comme en paysag msgid "The scorecard" msgstr "Le bilan" +msgid "The selected element's context is too large to send" +msgstr "Le contexte de l’élément sélectionné est trop volumineux pour être envoyé" + msgid "The self-driving software factory" msgstr "L'usine logicielle autonome" diff --git a/packages/i18n/locales/id/messages.po b/packages/i18n/locales/id/messages.po index 2577070cf8b..99f08ab01ab 100644 --- a/packages/i18n/locales/id/messages.po +++ b/packages/i18n/locales/id/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" akan di-merge ke {1} lewat {2}. Ini tidak bisa dibatalkan dari s msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" akan berhenti berjalan dan riwayat run-nya akan dihapus. Ini tidak bisa dibatalkan." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" berukuran {size} — file di atas {limit} tidak dapat dilampirkan" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" berukuran {size} — file di atas {limit} tidak dapat dilampirkan ke workspace ini. Jika file ada di mesin workspace, sebutkan path-nya saja." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" adalah folder. Lampirkan file di dalamnya sebagai gantinya." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Memakai # sumber} other {Memakai # sumber}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {menggunakan # alat} other {menggunakan # alat}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} file melebihi {limit} dan tidak ditambahkan" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} file dilampirkan — satu pesan memuat paling banyak {max}" + msgid "{count} GitHub stars" msgstr "{count} star GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — lupakan situs ini" +msgid "1 file is over {limit} and was not added" +msgstr "1 file melebihi {limit} dan tidak ditambahkan" + msgid "1 hr" msgstr "1 jam" @@ -19169,6 +19184,9 @@ msgstr "Aplikasi yang sama berjalan native di iPad, dalam mode potret dan lanska msgid "The scorecard" msgstr "Kartu skor" +msgid "The selected element's context is too large to send" +msgstr "Konteks elemen yang dipilih terlalu besar untuk dikirim" + msgid "The self-driving software factory" msgstr "Pabrik perangkat lunak yang menyetir sendiri" diff --git a/packages/i18n/locales/it/messages.po b/packages/i18n/locales/it/messages.po index 75ffe655516..3f8b23d2201 100644 --- a/packages/i18n/locales/it/messages.po +++ b/packages/i18n/locales/it/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" verrà unita in {1} tramite {2}. L'operazione non può essere an msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" smetterà di scattare e la sua cronologia delle esecuzioni verrà rimossa. L'operazione non può essere annullata." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" pesa {size}: non è possibile allegare file più grandi di {limit}" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" pesa {size}: non è possibile allegare a questo workspace file più grandi di {limit}. Se il file si trova sulla macchina del workspace, indica invece il suo percorso." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" è una cartella. Allega invece i file al suo interno." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Usata # fonte} other {Usate # fonti}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {usato # strumento} other {usati # strumenti}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} file superano {limit} e non sono stati aggiunti" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} file allegati: un messaggio ne può contenere al massimo {max}" + msgid "{count} GitHub stars" msgstr "{count} star su GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — dimentica questo sito" +msgid "1 file is over {limit} and was not added" +msgstr "1 file supera {limit} e non è stato aggiunto" + msgid "1 hr" msgstr "1 h" @@ -19169,6 +19184,9 @@ msgstr "La stessa app funziona in modo nativo su iPad, in verticale e in orizzon msgid "The scorecard" msgstr "La scheda di valutazione" +msgid "The selected element's context is too large to send" +msgstr "Il contesto dell’elemento selezionato è troppo grande per essere inviato" + msgid "The self-driving software factory" msgstr "La fabbrica di software a guida autonoma" diff --git a/packages/i18n/locales/ja/messages.po b/packages/i18n/locales/ja/messages.po index 95b6c208706..cc9b123f012 100644 --- a/packages/i18n/locales/ja/messages.po +++ b/packages/i18n/locales/ja/messages.po @@ -67,6 +67,12 @@ msgstr "「{0}」は{2}で{1}にマージされます。ここからは元に戻 msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "「{0}」は発火しなくなり、実行履歴も削除されます。この操作は元に戻せません。" +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "「{name}」は {size} です — {limit} を超えるファイルは添付できません" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "「{name}」は {size} です — このワークスペースには {limit} を超えるファイルを添付できません。ファイルがワークスペースのマシン上にある場合は、代わりにそのパスを記載してください。" + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "「{name}」はフォルダです。代わりにフォルダ内のファイルを添付してください。" @@ -914,6 +920,12 @@ msgstr "{count, plural, one {#件のソースを使用} other {#件のソース msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {#件のツールを使用} other {#件のツールを使用}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} 件のファイルが {limit} を超えているため追加されませんでした" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} 件のファイルが添付されています — 1 つのメッセージに添付できるのは最大 {max} 件です" + msgid "{count} GitHub stars" msgstr "GitHubスター{count}件" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1件のCookie — このサイトを削除" +msgid "1 file is over {limit} and was not added" +msgstr "1 件のファイルが {limit} を超えているため追加されませんでした" + msgid "1 hr" msgstr "1時間" @@ -19169,6 +19184,9 @@ msgstr "同じアプリが iPad でネイティブに動作し、縦向きにも msgid "The scorecard" msgstr "スコアカード" +msgid "The selected element's context is too large to send" +msgstr "選択した要素のコンテキストが大きすぎるため送信できません" + msgid "The self-driving software factory" msgstr "自動運転するソフトウェア工場" diff --git a/packages/i18n/locales/ko/messages.po b/packages/i18n/locales/ko/messages.po index 55ff0210c80..5b18902f991 100644 --- a/packages/i18n/locales/ko/messages.po +++ b/packages/i18n/locales/ko/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\"이(가) {2} 방식으로 {1}에 머지됩니다. 여기에서는 msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\"이(가) 더 이상 실행되지 않으며 실행 기록도 삭제됩니다. 되돌릴 수 없습니다." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\"의 크기는 {size}입니다 — {limit}를 넘는 파일은 첨부할 수 없습니다" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\"의 크기는 {size}입니다 — 이 워크스페이스에는 {limit}를 넘는 파일을 첨부할 수 없습니다. 파일이 워크스페이스의 머신에 있다면 대신 경로를 적어 주세요." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\"은(는) 폴더입니다. 대신 폴더 안의 파일을 첨부하세요." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {출처 #개 사용} other {출처 #개 사용}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {도구 #개 사용} other {도구 #개 사용}}" +msgid "{count} files are over {limit} and were not added" +msgstr "파일 {count}개가 {limit}를 넘어 추가되지 않았습니다" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "파일 {count}개가 첨부됨 — 메시지 하나에는 최대 {max}개까지 첨부할 수 있습니다" + msgid "{count} GitHub stars" msgstr "GitHub 스타 {count}개" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "쿠키 1개 — 이 사이트 잊기" +msgid "1 file is over {limit} and was not added" +msgstr "파일 1개가 {limit}를 넘어 추가되지 않았습니다" + msgid "1 hr" msgstr "1시간" @@ -19169,6 +19184,9 @@ msgstr "같은 앱이 iPad에서 네이티브로 실행되며 세로와 가로 msgid "The scorecard" msgstr "성적표" +msgid "The selected element's context is too large to send" +msgstr "선택한 요소의 컨텍스트가 너무 커서 보낼 수 없습니다" + msgid "The self-driving software factory" msgstr "자율 주행 소프트웨어 공장" diff --git a/packages/i18n/locales/nl/messages.po b/packages/i18n/locales/nl/messages.po index dd37438e604..3689be1e069 100644 --- a/packages/i18n/locales/nl/messages.po +++ b/packages/i18n/locales/nl/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" wordt via {2} in {1} gemerged. Dit kan hier niet ongedaan worden msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" wordt niet meer geactiveerd en de geschiedenis van runs wordt verwijderd. Dit kan niet ongedaan worden gemaakt." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" is {size} — bestanden groter dan {limit} kunnen niet worden bijgevoegd" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" is {size} — bestanden groter dan {limit} kunnen niet aan deze werkruimte worden bijgevoegd. Staat het bestand op de machine van de werkruimte, noem dan in plaats daarvan het pad." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" is een map. Voeg in plaats daarvan de bestanden in de map toe." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {# bron gebruikt} other {# bronnen gebruikt}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {# tool gebruikt} other {# tools gebruikt}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} bestanden zijn groter dan {limit} en zijn niet toegevoegd" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} bestanden bijgevoegd — een bericht kan er maximaal {max} bevatten" + msgid "{count} GitHub stars" msgstr "{count} GitHub-sterren" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — deze site vergeten" +msgid "1 file is over {limit} and was not added" +msgstr "1 bestand is groter dan {limit} en is niet toegevoegd" + msgid "1 hr" msgstr "1 u" @@ -19169,6 +19184,9 @@ msgstr "Dezelfde app draait native op iPad, staand en liggend." msgid "The scorecard" msgstr "De scorekaart" +msgid "The selected element's context is too large to send" +msgstr "De context van het geselecteerde element is te groot om te verzenden" + msgid "The self-driving software factory" msgstr "De zelfrijdende softwarefabriek" diff --git a/packages/i18n/locales/pl/messages.po b/packages/i18n/locales/pl/messages.po index 08570a06e75..29b45e8946b 100644 --- a/packages/i18n/locales/pl/messages.po +++ b/packages/i18n/locales/pl/messages.po @@ -67,6 +67,12 @@ msgstr "„{0}” zostanie scalony do {1} przez {2}. Nie można tego stąd cofn msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "„{0}” przestanie się uruchamiać, a jej historia uruchomień zostanie usunięta. Tej operacji nie można cofnąć." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "„{name}” ma {size} — nie można załączać plików większych niż {limit}" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "„{name}” ma {size} — do tego obszaru roboczego nie można załączać plików większych niż {limit}. Jeśli plik jest na maszynie obszaru roboczego, podaj zamiast tego jego ścieżkę." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "„{name}” to folder. Zamiast tego załącz pliki z jego wnętrza." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Użyto # źródła} few {Użyto # źródeł} many { msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {użyto # narzędzia} few {użyto # narzędzi} many {użyto # narzędzi} other {użyto # narzędzia}}" +msgid "{count} files are over {limit} and were not added" +msgstr "Pliki większe niż {limit} nie zostały dodane: {count}" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "Załączone pliki: {count} — wiadomość może zawierać najwyżej {max}" + msgid "{count} GitHub stars" msgstr "{count} gwiazdek GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — zapomnij tę witrynę" +msgid "1 file is over {limit} and was not added" +msgstr "1 plik jest większy niż {limit} i nie został dodany" + msgid "1 hr" msgstr "1 godz." @@ -19169,6 +19184,9 @@ msgstr "Ta sama aplikacja działa natywnie na iPadzie, w pionie i w poziomie." msgid "The scorecard" msgstr "Karta wyników" +msgid "The selected element's context is too large to send" +msgstr "Kontekst wybranego elementu jest zbyt duży, aby go wysłać" + msgid "The self-driving software factory" msgstr "Samojezdna fabryka oprogramowania" diff --git a/packages/i18n/locales/pt-BR/messages.po b/packages/i18n/locales/pt-BR/messages.po index c0cd320e68e..3abb86575ab 100644 --- a/packages/i18n/locales/pt-BR/messages.po +++ b/packages/i18n/locales/pt-BR/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" será mesclado em {1} via {2}. Não dá para desfazer daqui." msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" vai parar de disparar e seu histórico de execuções será removido. Não dá para desfazer." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" tem {size} — arquivos com mais de {limit} não podem ser anexados" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" tem {size} — arquivos com mais de {limit} não podem ser anexados a esta área de trabalho. Se o arquivo estiver na máquina da área de trabalho, mencione o caminho dele." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" é uma pasta. Anexe os arquivos que estão nela." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Usou # fonte} other {Usou # fontes}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {usou # ferramenta} other {usou # ferramentas}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} arquivos passam de {limit} e não foram adicionados" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} arquivos anexados — uma mensagem leva no máximo {max}" + msgid "{count} GitHub stars" msgstr "{count} estrelas no GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — esquecer este site" +msgid "1 file is over {limit} and was not added" +msgstr "1 arquivo passa de {limit} e não foi adicionado" + msgid "1 hr" msgstr "1 h" @@ -19169,6 +19184,9 @@ msgstr "O mesmo app roda nativamente no iPad, na vertical e na horizontal." msgid "The scorecard" msgstr "O placar" +msgid "The selected element's context is too large to send" +msgstr "O contexto do elemento selecionado é grande demais para ser enviado" + msgid "The self-driving software factory" msgstr "A fábrica de software autônoma" diff --git a/packages/i18n/locales/ru/messages.po b/packages/i18n/locales/ru/messages.po index 89bcaec21d6..259323cdad3 100644 --- a/packages/i18n/locales/ru/messages.po +++ b/packages/i18n/locales/ru/messages.po @@ -67,6 +67,12 @@ msgstr "«{0}» будет объединён в {1} через {2}. Отмен msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "«{0}» перестанет срабатывать, а история её запусков будет удалена. Это нельзя отменить." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "«{name}» занимает {size} — файлы больше {limit} прикрепить нельзя" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "«{name}» занимает {size} — к этой рабочей области нельзя прикрепить файлы больше {limit}. Если файл находится на машине рабочей области, укажите вместо этого его путь." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "«{name}» — это папка. Прикрепите файлы из неё." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Использован # источник} few {И msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {использован # инструмент} few {использовано # инструмента} many {использовано # инструментов} other {использовано # инструмента}}" +msgid "{count} files are over {limit} and were not added" +msgstr "Файлы больше {limit} не добавлены: {count}" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "Прикреплено файлов: {count} — в одном сообщении не больше {max}" + msgid "{count} GitHub stars" msgstr "{count} звёзд на GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — забыть этот сайт" +msgid "1 file is over {limit} and was not added" +msgstr "1 файл больше {limit} и не был добавлен" + msgid "1 hr" msgstr "1 ч" @@ -19169,6 +19184,9 @@ msgstr "То же приложение работает на iPad нативно msgid "The scorecard" msgstr "Таблица оценок" +msgid "The selected element's context is too large to send" +msgstr "Контекст выбранного элемента слишком велик для отправки" + msgid "The self-driving software factory" msgstr "Самоуправляемая софтверная фабрика" diff --git a/packages/i18n/locales/tr/messages.po b/packages/i18n/locales/tr/messages.po index 4db34ab46c1..047d142ea45 100644 --- a/packages/i18n/locales/tr/messages.po +++ b/packages/i18n/locales/tr/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\", {2} ile {1} dalına birleştirilecek. Buradan geri alınamaz." msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" artık tetiklenmeyecek ve çalıştırma geçmişi kaldırılacak. Bu geri alınamaz." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" {size} boyutunda — {limit} üzerindeki dosyalar eklenemez" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" {size} boyutunda — bu çalışma alanına {limit} üzerindeki dosyalar eklenemez. Dosya çalışma alanının makinesindeyse bunun yerine yolunu belirtin." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" bir klasör. Bunun yerine içindeki dosyaları ekle." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {# kaynak kullanıldı} other {# kaynak kullanıldı msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {# araç kullanıldı} other {# araç kullanıldı}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} dosya {limit} sınırını aştığı için eklenmedi" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "{count} dosya eklendi — bir mesaj en fazla {max} dosya taşıyabilir" + msgid "{count} GitHub stars" msgstr "{count} GitHub yıldızı" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 çerez — bu siteyi unut" +msgid "1 file is over {limit} and was not added" +msgstr "1 dosya {limit} sınırını aştığı için eklenmedi" + msgid "1 hr" msgstr "1 sa" @@ -19169,6 +19184,9 @@ msgstr "Aynı uygulama iPad'de yerel olarak, dikey ve yatay modda çalışır." msgid "The scorecard" msgstr "Puan tablosu" +msgid "The selected element's context is too large to send" +msgstr "Seçilen öğenin bağlamı gönderilemeyecek kadar büyük" + msgid "The self-driving software factory" msgstr "Kendi kendini süren yazılım fabrikası" diff --git a/packages/i18n/locales/uk/messages.po b/packages/i18n/locales/uk/messages.po index cfc06aa6703..b52dfc36a92 100644 --- a/packages/i18n/locales/uk/messages.po +++ b/packages/i18n/locales/uk/messages.po @@ -61,9 +61,18 @@ msgstr "«{0}» буде злито в {1} через {2}. Звідси це н msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "«{0}» більше не спрацьовуватиме, а історію її запусків буде видалено. Це не можна скасувати." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "«{name}» має розмір {size} — файли понад {limit} прикріпити не можна" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "«{name}» має розмір {size} — до цього робочого простору не можна прикріпити файли понад {limit}. Якщо файл є на машині робочого простору, вкажіть замість цього його шлях." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "«{name}» — це папка. Прикріпіть файли з неї." +msgid "\"{name}\" is over {limit}, the most a workspace outside the cloud can take" +msgstr "«{name}» більший за {limit} — це максимум для робочого простору поза хмарою" + msgid "\"{name}\" paused" msgstr "«{name}» призупинено" @@ -905,6 +914,12 @@ msgstr "{count, plural, one {Використано # джерело} few {Ви msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {використано # інструмент} few {використано # інструменти} many {використано # інструментів} other {використано # інструмента}}" +msgid "{count} files are over {limit} and were not added" +msgstr "Файли понад {limit} не додано: {count}" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "Прикріплено файлів: {count} — в одному повідомленні не більше {max}" + msgid "{count} GitHub stars" msgstr "Зірок на GitHub: {count}" @@ -1543,6 +1558,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 файл cookie — забути цей сайт" +msgid "1 file is over {limit} and was not added" +msgstr "1 файл більший за {limit} і не був доданий" + msgid "1 hr" msgstr "1 год" @@ -19160,6 +19178,9 @@ msgstr "Той самий застосунок працює нативно на msgid "The scorecard" msgstr "Таблиця оцінок" +msgid "The selected element's context is too large to send" +msgstr "Контекст вибраного елемента завеликий для надсилання" + msgid "The self-driving software factory" msgstr "Самокерована фабрика програмного забезпечення" diff --git a/packages/i18n/locales/vi/messages.po b/packages/i18n/locales/vi/messages.po index 4d12dea35a8..81588e96c57 100644 --- a/packages/i18n/locales/vi/messages.po +++ b/packages/i18n/locales/vi/messages.po @@ -67,6 +67,12 @@ msgstr "\"{0}\" sẽ được merge vào {1} bằng {2}. Không thể hoàn tác msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "\"{0}\" sẽ ngừng kích hoạt và lịch sử chạy sẽ bị xóa. Không thể hoàn tác." +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "\"{name}\" có dung lượng {size} — không thể đính kèm tệp lớn hơn {limit}" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "\"{name}\" có dung lượng {size} — không thể đính kèm tệp lớn hơn {limit} vào workspace này. Nếu tệp nằm trên máy của workspace, hãy ghi đường dẫn của tệp thay vào đó." + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "\"{name}\" là một thư mục. Hãy đính kèm các tệp bên trong thay thế." @@ -914,6 +920,12 @@ msgstr "{count, plural, one {Đã dùng # nguồn} other {Đã dùng # nguồn}} msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {đã dùng # công cụ} other {đã dùng # công cụ}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} tệp vượt quá {limit} và không được thêm" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "Đã đính kèm {count} tệp — mỗi tin nhắn chứa tối đa {max} tệp" + msgid "{count} GitHub stars" msgstr "{count} star GitHub" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1 cookie — quên trang này" +msgid "1 file is over {limit} and was not added" +msgstr "1 tệp vượt quá {limit} và không được thêm" + msgid "1 hr" msgstr "1 giờ" @@ -19169,6 +19184,9 @@ msgstr "Cùng một ứng dụng chạy native trên iPad, ở cả chế độ msgid "The scorecard" msgstr "Bảng điểm" +msgid "The selected element's context is too large to send" +msgstr "Ngữ cảnh của phần tử đã chọn quá lớn để gửi" + msgid "The self-driving software factory" msgstr "Nhà máy phần mềm tự vận hành" diff --git a/packages/i18n/locales/zh-CN/messages.po b/packages/i18n/locales/zh-CN/messages.po index fd21282e2f8..521f34a1faa 100644 --- a/packages/i18n/locales/zh-CN/messages.po +++ b/packages/i18n/locales/zh-CN/messages.po @@ -67,6 +67,12 @@ msgstr "“{0}”将通过{2}合并到{1}。在这里无法撤销。" msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "“{0}”将不再触发,其运行历史也会被移除。此操作无法撤销。" +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "“{name}”大小为 {size} — 无法附加超过 {limit} 的文件" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "“{name}”大小为 {size} — 无法向此工作区附加超过 {limit} 的文件。如果该文件位于工作区所在的机器上,请改为提及其路径。" + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "“{name}”是一个文件夹。请改为附加其中的文件。" @@ -914,6 +920,12 @@ msgstr "{count, plural, one {使用了#个来源} other {使用了#个来源}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {使用了 # 个工具} other {使用了 # 个工具}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} 个文件超过 {limit},未添加" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "已附加 {count} 个文件 — 一条消息最多可包含 {max} 个" + msgid "{count} GitHub stars" msgstr "{count}个GitHub星标" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1个cookie——忘记此站点" +msgid "1 file is over {limit} and was not added" +msgstr "1 个文件超过 {limit},未添加" + msgid "1 hr" msgstr "1 小时" @@ -19169,6 +19184,9 @@ msgstr "同一个应用在 iPad 上原生运行,支持竖屏和横屏。" msgid "The scorecard" msgstr "评分表" +msgid "The selected element's context is too large to send" +msgstr "所选元素的上下文太大,无法发送" + msgid "The self-driving software factory" msgstr "自动驾驶的软件工厂" diff --git a/packages/i18n/locales/zh-TW/messages.po b/packages/i18n/locales/zh-TW/messages.po index fcd60230fc5..0690ff2090b 100644 --- a/packages/i18n/locales/zh-TW/messages.po +++ b/packages/i18n/locales/zh-TW/messages.po @@ -67,6 +67,12 @@ msgstr "「{0}」將通過{2}合併到{1}。在這裡無法復原。" msgid "\"{0}\" will stop firing and its run history will be removed. This can't be undone." msgstr "「{0}」將不再觸發,其執行歷史也會被移除。此操作無法復原。" +msgid "\"{name}\" is {size} — files over {limit} can't be attached" +msgstr "「{name}」大小為 {size} — 無法附加超過 {limit} 的檔案" + +msgid "\"{name}\" is {size} — files over {limit} can't be attached to this workspace. If the file is on the workspace's machine, mention its path instead." +msgstr "「{name}」大小為 {size} — 無法將超過 {limit} 的檔案附加到此工作區。若檔案位於工作區所在的機器上,請改為提及其路徑。" + msgid "\"{name}\" is a folder. Attach the files inside it instead." msgstr "「{name}」是資料夾。請改為附加其中的檔案。" @@ -914,6 +920,12 @@ msgstr "{count, plural, one {使用了#個來源} other {使用了#個來源}}" msgid "{count, plural, one {used # tool} other {used # tools}}" msgstr "{count, plural, one {使用了 # 個工具} other {使用了 # 個工具}}" +msgid "{count} files are over {limit} and were not added" +msgstr "{count} 個檔案超過 {limit},未新增" + +msgid "{count} files attached — a message carries at most {max}" +msgstr "已附加 {count} 個檔案 — 一則訊息最多可包含 {max} 個" + msgid "{count} GitHub stars" msgstr "{count}個GitHub星標" @@ -1552,6 +1564,9 @@ msgstr "1" msgid "1 cookie — forget this site" msgstr "1個cookie——忘記此站點" +msgid "1 file is over {limit} and was not added" +msgstr "1 個檔案超過 {limit},未新增" + msgid "1 hr" msgstr "1 小時" @@ -19169,6 +19184,9 @@ msgstr "同一個應用程式在 iPad 上原生執行,支援直向和橫向。 msgid "The scorecard" msgstr "評分表" +msgid "The selected element's context is too large to send" +msgstr "所選元素的內容脈絡太大,無法傳送" + msgid "The self-driving software factory" msgstr "自動駕駛的軟體工廠" diff --git a/packages/shared/src/attachment-limits.ts b/packages/shared/src/attachment-limits.ts index 63e7257f220..cd36bc4f91c 100644 --- a/packages/shared/src/attachment-limits.ts +++ b/packages/shared/src/attachment-limits.ts @@ -35,4 +35,4 @@ export const ATTACHMENT_RETENTION_DAYS = 30; * The first host whose adapters read an attachment part, by `attachmentId` or * `fileId`. Below it a client sends path tags in the text instead. */ -export const ATTACHMENT_PARTS_MIN_HOST_VERSION = "1.41.0"; +export const ATTACHMENT_PARTS_MIN_HOST_VERSION = "2.0.4"; diff --git a/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts b/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts index 8cbb10bc155..3a578274726 100644 --- a/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts +++ b/packages/trpc/src/router/cloud-workspace/cloud-workspace.ts @@ -8,6 +8,7 @@ import { githubRepositories, users, } from "@superset/db/schema"; +import { MAX_ATTACHMENTS } from "@superset/shared/attachment-limits"; import { MACHINE_SIZES, machineSpec } from "@superset/shared/billing"; import { CLOUD_AGENT_PROMPT_MAX_LENGTH, @@ -362,7 +363,10 @@ export const cloudWorkspaceRouter = { * bytes once it is up; the same cap `attachments.importFromCloud` * takes, since that is what runs in there. */ - attachmentFileIds: z.array(z.string().uuid()).max(10).optional(), + attachmentFileIds: z + .array(z.string().uuid()) + .max(MAX_ATTACHMENTS) + .optional(), /** What the person typed, kept as the workspace's prompt; `prompt` may carry built context. */ typedPrompt: z.string().max(20000).optional(), /** Tasks the composer linked; each must be in this organization. */ diff --git a/packages/trpc/src/router/cloud-workspace/record.ts b/packages/trpc/src/router/cloud-workspace/record.ts index 3dd873768ab..4d5f5d24bfd 100644 --- a/packages/trpc/src/router/cloud-workspace/record.ts +++ b/packages/trpc/src/router/cloud-workspace/record.ts @@ -18,6 +18,7 @@ import { users, } from "@superset/db/schema"; import { retiredTaskColumns } from "@superset/db/task-list-query"; +import { MAX_ATTACHMENTS } from "@superset/shared/attachment-limits"; import { LABELS_MAX_PER_WORKSPACE } from "@superset/shared/labels"; import type { TRPCRouterRecord } from "@trpc/server"; import { TRPCError } from "@trpc/server"; @@ -622,7 +623,7 @@ export const cloudWorkspaceRecordRouter = { .input( z.object({ id: z.string().uuid(), - fileIds: z.array(z.string().uuid()).min(1).max(10), + fileIds: z.array(z.string().uuid()).min(1).max(MAX_ATTACHMENTS), }), ) .mutation(async ({ ctx, input }) => {