This repository was archived by the owner on Aug 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
fix(tasks): expose artifact downloads in task conversations #3788
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b60aa24
fix(tasks): expose artifact downloads in task conversations
tatoalo 0dfbf00
chore(tasks): remove artifact UI documentation
tatoalo 5b1d661
fix(tasks): address artifact UI quality checks
tatoalo 078738e
fix(tasks): fetch cloud artifact manifests for downloads
tatoalo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/ui/src/features/sessions/components/CloudArtifactDownloads.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { Theme } from "@radix-ui/themes"; | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { CloudArtifactDownloads } from "./CloudArtifactDownloads"; | ||
|
|
||
| const getCloudAttachmentPreviewUrl = vi.fn(); | ||
| const fetchedArtifacts = [ | ||
| { | ||
| id: "output-1", | ||
| name: "report.pdf", | ||
| type: "output", | ||
| size: 12_000, | ||
| storage_path: "tasks/run-1/report.pdf", | ||
| }, | ||
| { | ||
| id: "internal-1", | ||
| name: "handoff.pack", | ||
| type: "artifact", | ||
| storage_path: "tasks/run-1/handoff.pack", | ||
| }, | ||
| ]; | ||
|
|
||
| vi.mock("@posthog/core/sessions/sessionService", () => ({ | ||
| SESSION_SERVICE: Symbol("SESSION_SERVICE"), | ||
| })); | ||
|
|
||
| vi.mock("@posthog/di/react", () => ({ | ||
| useService: () => ({ getCloudAttachmentPreviewUrl }), | ||
| })); | ||
|
|
||
| vi.mock("@posthog/ui/features/sessions/sessionStore", () => ({ | ||
| useSessionSelector: () => undefined, | ||
| })); | ||
|
|
||
| vi.mock("@posthog/ui/features/auth/store", () => ({ | ||
| getAuthIdentity: () => "auth-1", | ||
| useAuthStateValue: () => "auth-1", | ||
| })); | ||
|
|
||
| vi.mock("@tanstack/react-query", () => ({ | ||
| useQuery: () => ({ data: fetchedArtifacts }), | ||
| })); | ||
|
|
||
| const task = { | ||
| id: "task-1", | ||
| latest_run: { | ||
| id: "run-1", | ||
| status: "completed", | ||
| }, | ||
| } as never; | ||
|
|
||
| describe("CloudArtifactDownloads", () => { | ||
| beforeEach(() => { | ||
| getCloudAttachmentPreviewUrl.mockReset(); | ||
| }); | ||
|
|
||
| it("shows output artifacts and opens their download URL", async () => { | ||
| getCloudAttachmentPreviewUrl.mockResolvedValue( | ||
| "https://files.example/report.pdf", | ||
| ); | ||
| const open = vi.spyOn(window, "open").mockImplementation(() => null); | ||
|
|
||
| render( | ||
| <Theme> | ||
| <CloudArtifactDownloads taskId="task-1" task={task} /> | ||
| </Theme>, | ||
| ); | ||
|
|
||
| expect(screen.getByText("report.pdf")).toBeInTheDocument(); | ||
| expect(screen.getByText("12 KB")).toBeInTheDocument(); | ||
| expect(screen.queryByText("handoff.pack")).not.toBeInTheDocument(); | ||
|
|
||
| fireEvent.click(screen.getByRole("button", { name: "Download" })); | ||
|
|
||
| await waitFor(() => | ||
| expect(open).toHaveBeenCalledWith( | ||
| "https://files.example/report.pdf", | ||
| "_blank", | ||
| "noopener,noreferrer", | ||
| ), | ||
| ); | ||
| expect(getCloudAttachmentPreviewUrl).toHaveBeenCalledWith( | ||
| "task-1", | ||
| "run-1", | ||
| "output-1", | ||
| ); | ||
| }); | ||
| }); |
135 changes: 135 additions & 0 deletions
135
packages/ui/src/features/sessions/components/CloudArtifactDownloads.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { DownloadSimple } from "@phosphor-icons/react"; | ||
| import { | ||
| SESSION_SERVICE, | ||
| type SessionService, | ||
| } from "@posthog/core/sessions/sessionService"; | ||
| import { useService } from "@posthog/di/react"; | ||
| import { Button } from "@posthog/quill"; | ||
| import type { TaskRunArtifact } from "@posthog/shared"; | ||
| import { isTerminalStatus, type Task } from "@posthog/shared/domain-types"; | ||
| import { | ||
| getAuthIdentity, | ||
| useAuthStateValue, | ||
| } from "@posthog/ui/features/auth/store"; | ||
| import { useSessionSelector } from "@posthog/ui/features/sessions/sessionStore"; | ||
| import { FileIcon } from "@posthog/ui/primitives/FileIcon"; | ||
| import { toast } from "@posthog/ui/primitives/toast"; | ||
| import { Box, Flex, Text } from "@radix-ui/themes"; | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { useCallback, useMemo, useState } from "react"; | ||
|
|
||
| function formatFileSize(size: number | undefined): string | null { | ||
| if (size === undefined) return null; | ||
| if (size < 1_000) return `${size} B`; | ||
| if (size < 1_000_000) return `${Math.round(size / 1_000)} KB`; | ||
| return `${(size / 1_000_000).toFixed(1)} MB`; | ||
| } | ||
|
|
||
| export function CloudArtifactDownloads({ | ||
| taskId, | ||
| task, | ||
| }: { | ||
| taskId: string | undefined; | ||
| task: Task | undefined; | ||
| }) { | ||
| const sessionService = useService<SessionService>(SESSION_SERVICE); | ||
| const sessionArtifacts = useSessionSelector( | ||
| taskId, | ||
| (session) => session?.cloudArtifacts, | ||
| ); | ||
| const cloudStatus = useSessionSelector( | ||
| taskId, | ||
| (session) => session?.cloudStatus, | ||
| ); | ||
| const authIdentity = useAuthStateValue(getAuthIdentity); | ||
| const [downloadingId, setDownloadingId] = useState<string | null>(null); | ||
| const runId = task?.latest_run?.id; | ||
| const { data: fetchedArtifacts } = useQuery({ | ||
| queryKey: ["cloudRunArtifacts", authIdentity, taskId, runId], | ||
| queryFn: () => | ||
| sessionService.getCloudRunArtifacts(taskId ?? "", runId ?? ""), | ||
| enabled: | ||
| authIdentity !== null && | ||
| taskId !== undefined && | ||
| runId !== undefined && | ||
| isTerminalStatus(cloudStatus ?? task?.latest_run?.status), | ||
| retry: false, | ||
| staleTime: Infinity, | ||
| }); | ||
| const artifacts = useMemo( | ||
| () => | ||
| ( | ||
| fetchedArtifacts ?? | ||
| sessionArtifacts ?? | ||
| task?.latest_run?.artifacts ?? | ||
| [] | ||
| ).filter((artifact) => artifact.type === "output"), | ||
| [fetchedArtifacts, sessionArtifacts, task?.latest_run?.artifacts], | ||
| ); | ||
|
|
||
| const downloadArtifact = useCallback( | ||
| async (artifact: TaskRunArtifact): Promise<void> => { | ||
| if (!taskId || !runId || !artifact.id) return; | ||
| setDownloadingId(artifact.id); | ||
| try { | ||
| const url = await sessionService.getCloudAttachmentPreviewUrl( | ||
| taskId, | ||
| runId, | ||
| artifact.id, | ||
| ); | ||
| if (!url) { | ||
| toast.error("This file is no longer available"); | ||
| return; | ||
| } | ||
| window.open(url, "_blank", "noopener,noreferrer"); | ||
| } catch { | ||
| toast.error("Couldn't download file"); | ||
| } finally { | ||
| setDownloadingId(null); | ||
| } | ||
| }, | ||
| [runId, sessionService, taskId], | ||
| ); | ||
|
|
||
| if (!runId || artifacts.length === 0) return null; | ||
|
|
||
| return ( | ||
| <Box className="mb-3 rounded-lg border border-gray-4 bg-gray-2 p-3"> | ||
| <Text className="mb-2 block font-medium text-[13px]">Files</Text> | ||
| <Flex direction="column" gap="1"> | ||
| {artifacts.map((artifact) => { | ||
| const size = formatFileSize(artifact.size); | ||
| const canDownload = Boolean(artifact.id); | ||
| return ( | ||
| <Flex | ||
| key={artifact.id ?? artifact.storage_path ?? artifact.name} | ||
| align="center" | ||
| justify="between" | ||
| gap="3" | ||
| className="min-w-0 rounded-md bg-background px-2 py-1.5" | ||
| > | ||
| <Flex align="center" gap="2" className="min-w-0"> | ||
| <FileIcon filename={artifact.name} size={16} /> | ||
| <Text className="truncate text-[13px]">{artifact.name}</Text> | ||
| {size !== null && ( | ||
| <Text color="gray" className="shrink-0 text-[12px]"> | ||
| {size} | ||
| </Text> | ||
| )} | ||
| </Flex> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| disabled={!canDownload || downloadingId === artifact.id} | ||
| onClick={() => void downloadArtifact(artifact)} | ||
| > | ||
| <DownloadSimple size={14} /> | ||
| {downloadingId === artifact.id ? "Opening..." : "Download"} | ||
| </Button> | ||
| </Flex> | ||
| ); | ||
| })} | ||
| </Flex> | ||
| </Box> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.