From 967263080ea71c261478aed66af900ecf9d9bdc1 Mon Sep 17 00:00:00 2001 From: Kiet Ho Date: Tue, 10 Nov 2026 23:21:27 -0800 Subject: [PATCH] fix(desktop): let the chat transcript render without a workspace host (SUPER-3412) A cloud workspace opens on its stored chat while its box wakes. That screen draws the chat pane's transcript before WorkspaceProvider mounts, because the sandbox has no address yet. TurnChangesCard, AttachmentImage and AttachmentPartImage called workspaceTrpc hooks, which throw outside a provider even when their query is disabled, so the view showed "Unable to find tRPC Context" instead of the chat. The transcript now reads the host only where the pane gives it one: - TurnChangesCard takes worktreePath from ChatPaneActions (SessionView supplies it) instead of querying workspace.get. - AttachmentImage and AttachmentPartImage mount their host-reading part only when ChatPaneActions has a workspaceId. Otherwise they show file cards; cloud-stored attachments still load without a host. - DraftChat's ChatPaneActionsProvider wraps the whole pane, so its warm transcript keeps host reads. This removes the provider whose client failed every call, added to StoredCloudChat in #9621. A fake provider hid the missing host instead of the transcript stating it. Adds a StoredChatTranscript test that renders a stored chat with a changes card and image attachments and no workspace tRPC provider. It fails with the original error when the fix is reverted. --- .../StoredChatTranscript.test.tsx | 126 ++++++++++++++++++ .../StoredCloudChat/StoredCloudChat.tsx | 35 +---- .../components/DraftChat/DraftChat.tsx | 40 +++--- .../components/SessionView/SessionView.tsx | 5 + .../AttachmentImage/AttachmentImage.tsx | 65 ++------- .../HostAttachmentImage.tsx | 57 ++++++++ .../components/HostAttachmentImage/index.ts | 1 + .../TurnChangesCard/TurnChangesCard.tsx | 9 +- .../AttachmentPartImage.tsx | 34 +---- .../AttachmentThumbnail.tsx | 41 ++++++ .../components/AttachmentThumbnail/index.ts | 1 + .../HostAttachmentPartImage.tsx | 14 ++ .../HostAttachmentPartImage/index.ts | 1 + .../hooks/useAttachmentSource/index.ts | 5 +- .../useAttachmentSource.ts | 25 +++- .../ChatPaneActionsProvider.tsx | 6 + 16 files changed, 321 insertions(+), 144 deletions(-) create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredChatTranscript/StoredChatTranscript.test.tsx create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/HostAttachmentImage.tsx create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/AttachmentThumbnail.tsx create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/index.ts create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/HostAttachmentPartImage.tsx create mode 100644 apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/index.ts diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredChatTranscript/StoredChatTranscript.test.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredChatTranscript/StoredChatTranscript.test.tsx new file mode 100644 index 00000000000..77de22815fc --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredChatTranscript/StoredChatTranscript.test.tsx @@ -0,0 +1,126 @@ +import { afterAll, afterEach, beforeAll, expect, test } from "bun:test"; +import { + emptySnapshot, + formatAttachmentTag, + reduceMany, +} from "@superset/chat/core"; +import type { DurableEnvelope, DurableEvent } from "@superset/chat/protocol"; +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { cleanup, render } from "@testing-library/react"; +import type { ContextType } from "react"; +import { HostWorkspacesContext } from "renderer/routes/_authenticated/providers/HostWorkspacesProvider"; +import { LocalHostServiceContext } from "renderer/routes/_authenticated/providers/LocalHostServiceProvider"; +import { SandboxAccessContext } from "renderer/routes/_authenticated/providers/SandboxAccessProvider"; +import { StoredChatTranscript } from "./StoredChatTranscript"; + +// File icons resolve against the page URL, and the test DOM starts on about:blank. +const page = ( + window as unknown as { happyDOM: { setURL: (url: string) => void } } +).happyDOM; +beforeAll(() => page.setURL("http://localhost/")); +afterAll(() => page.setURL("about:blank")); +afterEach(cleanup); + +function journal(events: DurableEvent[]): DurableEnvelope[] { + return events.map((event, index) => ({ + v: 1, + sessionId: "s1", + ts: index, + cursor: { epoch: "e1", seq: index + 1 }, + event, + })); +} + +const snapshot = reduceMany( + emptySnapshot(), + journal([ + { + type: "item", + turnId: "t1", + item: { + id: "u1", + kind: "user_message", + clientId: "c1", + startedAtMs: 1, + content: [ + { + type: "text", + text: `fix it\n${formatAttachmentTag({ path: ".superset/attachments/shot.png", type: "image/png" })}`, + }, + { + type: "attachment", + attachmentId: "a1", + name: "pasted.png", + mimeType: "image/png", + }, + ], + }, + }, + { + type: "item", + turnId: "t1", + item: { + id: "tool-1", + kind: "tool_call", + title: "Edit", + toolKind: "edit", + toolName: "Edit", + status: "completed", + startedAtMs: 2, + content: [ + { + type: "diff", + path: "/workspace/src/app.ts", + oldText: "a\n", + newText: "b\n", + }, + ], + }, + }, + { + type: "turn", + turn: { id: "t1", status: "completed", startedAtMs: 1, completedAtMs: 3 }, + }, + ]), +); + +test("a cloud workspace's stored chat renders while its box wakes, with no workspace host to read from", () => { + const view = render( + + + > + } + > + + > + } + > + + + + + + , + ); + + expect(view.getByText("Edited 1 file")).toBeTruthy(); + expect(view.getByTitle("shot.png")).toBeTruthy(); + expect(view.getByTitle("pasted.png")).toBeTruthy(); +}); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredCloudChat/StoredCloudChat.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredCloudChat/StoredCloudChat.tsx index 35cb6694a46..fe94808db9b 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredCloudChat/StoredCloudChat.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/components/StoredCloudChat/StoredCloudChat.tsx @@ -1,14 +1,9 @@ import { useLingui } from "@lingui/react/macro"; -import type { AppRouter } from "@superset/host-service/trpc"; -import { workspaceTrpc } from "@superset/workspace-client"; import { keepPreviousData, - QueryClient, useQuery, useQueryClient, } from "@tanstack/react-query"; -import { TRPCClientError } from "@trpc/client"; -import { observable } from "@trpc/server/observable"; import { type ReactNode, useEffect } from "react"; import { cloudTrpc } from "renderer/lib/cloud-trpc"; import { electronQueryClient } from "renderer/providers/ElectronTRPCProvider"; @@ -28,21 +23,6 @@ const PREFETCHED_CHATS = 5; const STALE_MS = 60_000; const NO_PROMPTS: never[] = []; -/** Transcript rows call workspace hooks, which need a client even with no box to reach. */ -const noHostClient = workspaceTrpc.createClient({ - links: [ - () => () => - observable((observer) => - observer.error( - new TRPCClientError("The workspace has no host yet"), - ), - ), - ], -}); -const noHostQueryClient = new QueryClient({ - defaultOptions: { queries: { retry: false } }, -}); - /** * A cloud workspace's newest chat as the database holds it, while the * workspace waits for its box to answer, else `fallback`. It is cached under @@ -106,16 +86,11 @@ export function StoredCloudChat({ }) } /> - - - +
); diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/components/DraftChat/DraftChat.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/components/DraftChat/DraftChat.tsx index c576753ad55..5a55d7cad3d 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/components/DraftChat/DraftChat.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatPane/components/AcpChatPane/components/DraftChat/DraftChat.tsx @@ -50,25 +50,27 @@ export function DraftChat({ ); return ( - - {transcript ?? - (prompts.length > 0 ? ( - - ) : ( -
- ))} - composerRef.current?.returnFocus(exit === "history")} - paneRef={paneRef} - ref={drawerRef} - /> - + {transcript ?? + (prompts.length > 0 ? ( + + ) : ( +
+ ))} + + composerRef.current?.returnFocus(exit === "history") + } + paneRef={paneRef} + ref={drawerRef} + /> onQueue(content)} workspaceId={workspaceId} /> - - + + ); } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/SessionView/SessionView.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/SessionView/SessionView.tsx index 0359dfd6383..64bbb9b6437 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/SessionView/SessionView.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/SessionView/SessionView.tsx @@ -27,6 +27,7 @@ import { MessageScroller } from "@superset/chat-ui/MessageScroller"; import type { ComposerDraft } from "@superset/chat-ui/PromptInput"; import { ChatHistorySidebarScroller } from "@superset/ui/chat-history-sidebar"; import { toast } from "@superset/ui/sonner"; +import { workspaceTrpc } from "@superset/workspace-client"; import type { ReactNode } from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { env } from "renderer/env.renderer"; @@ -312,6 +313,9 @@ export function SessionView({ }), [client, workspaceId], ); + const worktreePath = workspaceTrpc.workspace.get.useQuery({ + id: workspaceId, + }).data?.worktreePath; const sessionUsage = useMemo( () => latestSessionUsage(session.snapshot.turns), [session.snapshot.turns], @@ -662,6 +666,7 @@ export function SessionView({ openLink={openLink} getOpenFilePaths={getOpenFilePaths} workspaceId={workspaceId} + worktreePath={worktreePath} > { - registerThumbnail(id, element); - return () => registerThumbnail(id, null); - }, - [id, registerThumbnail], - ); - const openPreview = () => open(id); - - if (source.status === "error" || unrenderable) - return ; - if (source.status === "loading") - return ( -
- ); - if (source.status === "too-large") +export function AttachmentImage( + props: ComponentProps, +) { + const { workspaceId } = useChatPaneActions(); + if (!workspaceId) return ( - + ); - return ( - setUnrenderable(true)} - ref={register} - src={source.src} - /> - ); + return ; } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/HostAttachmentImage.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/HostAttachmentImage.tsx new file mode 100644 index 00000000000..8d34dbe2eba --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/HostAttachmentImage.tsx @@ -0,0 +1,57 @@ +import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; +import { useCallback, useState } from "react"; +import { useChatImageSource } from "../../../../hooks/useChatImageSource"; +import { useChatImageGallery } from "../../../../providers/ChatImageGalleryProvider"; +import { galleryImageId } from "../../../../utils/galleryImages"; +import { LargeImageTile } from "../LargeImageTile"; +import { PreviewableImage } from "../PreviewableImage"; + +export function HostAttachmentImage({ + alt, + name: attachedName, + owner, + path, + type, +}: { + /** Transcript key of the item showing the image; pairs with `path` in the gallery. */ + owner: string; + path: string; + type: string; + alt?: string | undefined; + name?: string | undefined; +}) { + const { open, registerThumbnail } = useChatImageGallery(); + const id = galleryImageId(owner, path); + const name = attachedName || (path.split("/").pop() ?? path); + const source = useChatImageSource({ path, mimeType: type }, { full: false }); + const [unrenderable, setUnrenderable] = useState(false); + const register = useCallback( + (element: HTMLElement | null) => { + registerThumbnail(id, element); + return () => registerThumbnail(id, null); + }, + [id, registerThumbnail], + ); + const openPreview = () => open(id); + + if (source.status === "error" || unrenderable) + return ; + if (source.status === "loading") + return ( +
+ ); + if (source.status === "too-large") + return ( + + ); + return ( + setUnrenderable(true)} + ref={register} + src={source.src} + /> + ); +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/index.ts new file mode 100644 index 00000000000..759c1d045cc --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/AttachmentImage/components/HostAttachmentImage/index.ts @@ -0,0 +1 @@ +export { HostAttachmentImage } from "./HostAttachmentImage"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/TurnGroupSection/components/TurnChangesCard/TurnChangesCard.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/TurnGroupSection/components/TurnChangesCard/TurnChangesCard.tsx index 9dfd85a720a..433ab700556 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/TurnGroupSection/components/TurnChangesCard/TurnChangesCard.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/TurnGroupSection/components/TurnChangesCard/TurnChangesCard.tsx @@ -3,7 +3,6 @@ import { Trans, useLingui } from "@lingui/react/macro"; import { Button } from "@superset/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger } from "@superset/ui/tooltip"; import { cn } from "@superset/ui/utils"; -import { workspaceTrpc } from "@superset/workspace-client"; import { ChevronDown, FileDiff, Undo2 } from "lucide-react"; import { useMemo, useState } from "react"; import { DiffStatText } from "renderer/routes/_authenticated/_dashboard/components/DiffStatText"; @@ -49,12 +48,8 @@ export function TurnChangesCard({ turnId: string; }) { const { t } = useLingui(); - const { openDiff, openFile, turnChanges, workspaceId } = useChatPaneActions(); - const workspaceQuery = workspaceTrpc.workspace.get.useQuery( - { id: workspaceId ?? "" }, - { enabled: Boolean(workspaceId) }, - ); - const worktreePath = workspaceQuery.data?.worktreePath; + const { openDiff, openFile, turnChanges, worktreePath } = + useChatPaneActions(); const { changes, reload } = useTurnChanges(turnId, { anchorItemIds: toolCallIds, }); 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 index ae85a9432e4..a15db4288bb 100644 --- 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 @@ -1,32 +1,10 @@ -import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; -import { useState } from "react"; import type { AttachmentPart } from "../../../../../../../../utils/sendAttachmentsToWorkspace"; -import { useAttachmentSource } from "../../../../hooks/useAttachmentSource"; +import { useChatPaneActions } from "../../../../../../providers/ChatPaneActionsProvider"; +import { AttachmentThumbnail } from "./components/AttachmentThumbnail"; +import { HostAttachmentPartImage } from "./components/HostAttachmentPartImage"; 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} - /> -
- ); + const { workspaceId } = useChatPaneActions(); + if (!workspaceId) return ; + return ; } diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/AttachmentThumbnail.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/AttachmentThumbnail.tsx new file mode 100644 index 00000000000..f8a07abc21d --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/AttachmentThumbnail.tsx @@ -0,0 +1,41 @@ +import { AttachmentFileCard } from "@superset/chat-ui/AttachmentFileCard"; +import { useState } from "react"; +import type { AttachmentPart } from "../../../../../../../../../../utils/sendAttachmentsToWorkspace"; +import { + type HostAttachmentRead, + useAttachmentSource, +} from "../../../../../../hooks/useAttachmentSource"; + +export function AttachmentThumbnail({ + hostRead, + part, +}: { + hostRead: HostAttachmentRead | null; + part: AttachmentPart; +}) { + const { src, isLoading } = useAttachmentSource(part, hostRead); + 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/components/AttachmentThumbnail/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/index.ts new file mode 100644 index 00000000000..68e6e344966 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/AttachmentThumbnail/index.ts @@ -0,0 +1 @@ +export { AttachmentThumbnail } from "./AttachmentThumbnail"; diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/HostAttachmentPartImage.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/HostAttachmentPartImage.tsx new file mode 100644 index 00000000000..dd755b0f68c --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/HostAttachmentPartImage.tsx @@ -0,0 +1,14 @@ +import { workspaceTrpc } from "@superset/workspace-client"; +import { useCallback } from "react"; +import type { AttachmentPart } from "../../../../../../../../../../utils/sendAttachmentsToWorkspace"; +import type { HostAttachmentRead } from "../../../../../../hooks/useAttachmentSource"; +import { AttachmentThumbnail } from "../AttachmentThumbnail"; + +export function HostAttachmentPartImage({ part }: { part: AttachmentPart }) { + const host = workspaceTrpc.useUtils().client; + const hostRead = useCallback( + (attachmentId) => host.attachments.read.query({ attachmentId }), + [host], + ); + return ; +} diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/index.ts b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/index.ts new file mode 100644 index 00000000000..ae9082b49b3 --- /dev/null +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/components/Transcript/components/UserMessageRow/components/AttachmentPartImage/components/HostAttachmentPartImage/index.ts @@ -0,0 +1 @@ +export { HostAttachmentPartImage } from "./HostAttachmentPartImage"; 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 index c0b86cd6611..856c82e00bb 100644 --- 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 @@ -1 +1,4 @@ -export { useAttachmentSource } from "./useAttachmentSource"; +export { + type HostAttachmentRead, + 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 index 63e353e86bf..31d65f2c751 100644 --- 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 @@ -1,4 +1,3 @@ -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"; @@ -15,14 +14,24 @@ const resolveCloudUrl = createCloudUrlResolver((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): { +export type HostAttachmentRead = ( + attachmentId: string, +) => Promise<{ mediaType: string; data: string }>; + +/** + * A data URL for an attachment part's thumbnail, or null for a name badge. + * Without `hostRead`, only parts stored in the cloud can show. + */ +export function useAttachmentSource( + part: AttachmentPart, + hostRead: HostAttachmentRead | null, +): { src: string | null; isLoading: boolean; } { - const host = workspaceTrpc.useUtils().client; const showable = - isThumbnailable(part) && Boolean(part.fileId || part.attachmentId); + isThumbnailable(part) && + Boolean(part.fileId || (hostRead && part.attachmentId)); const query = useQuery({ queryKey: attachmentSourceKey(part), enabled: showable, @@ -36,8 +45,10 @@ export function useAttachmentSource(part: AttachmentPart): { const response = await fetch(url); return response.ok ? response.blob() : null; }, - hostRead: (attachmentId) => - host.attachments.read.query({ attachmentId }), + hostRead: (attachmentId) => { + if (!hostRead) throw new Error("No workspace host to read from"); + return hostRead(attachmentId); + }, }), }); return { diff --git a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/providers/ChatPaneActionsProvider/ChatPaneActionsProvider.tsx b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/providers/ChatPaneActionsProvider/ChatPaneActionsProvider.tsx index fad0c903989..042c2560418 100644 --- a/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/providers/ChatPaneActionsProvider/ChatPaneActionsProvider.tsx +++ b/apps/desktop/src/renderer/routes/_authenticated/_dashboard/v2-workspace/$workspaceId/hooks/usePaneRegistry/components/ChatSession/providers/ChatPaneActionsProvider/ChatPaneActionsProvider.tsx @@ -36,7 +36,10 @@ export type ChatPaneActions = { /** Absent where the chat has no workspace to show diffs in. */ openDiff?: OpenDiff | undefined; turnChanges?: TurnChangesSource | undefined; + /** Absent where no workspace host is mounted to read from, such as a cloud chat shown while its box wakes. */ workspaceId?: string | undefined; + /** Absent where the chat has no worktree on disk to resolve paths against. */ + worktreePath?: string | undefined; /** Absent where the chat has no workspace to open pages into. */ openPage?: OpenPage | undefined; openLink?: OpenLink | undefined; @@ -60,6 +63,7 @@ export function ChatPaneActionsProvider({ openDiff, turnChanges, workspaceId, + worktreePath, openPage, openLink, getOpenFilePaths, @@ -72,6 +76,7 @@ export function ChatPaneActionsProvider({ openDiff, turnChanges, workspaceId, + worktreePath, openPage, openLink, getOpenFilePaths, @@ -83,6 +88,7 @@ export function ChatPaneActionsProvider({ openDiff, turnChanges, workspaceId, + worktreePath, openPage, openLink, getOpenFilePaths,