mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-09-17 01:56:18 +00:00
* feat(frontend): render markdown artifacts in the new window The artifacts panel's "open in new window" action handed the browser the raw Gateway response. For markdown that is a `text/markdown` body the browser can only show as source, so the new window was a text dump rather than a reader. Route markdown artifacts to a new `/artifacts/view` page that renders them with the same components the panel uses (SafeStreamdown + the artifact rehype chain + citation links/panel), including the truncated-preview banner and its "load full file" action. Everything else keeps the raw Gateway URL — notably HTML/SVG, which the Gateway deliberately serves as a download so active content never executes in the application origin. - `core/artifacts/viewer.ts` centralizes which stored artifacts are markdown (`.skill` archives included, since they hold a SKILL.md), so the panel and the viewer route cannot drift. - `ArtifactFilePreview` and its siblings move out of `artifact-file-detail.tsx` into `artifact-file-preview.tsx`; otherwise the standalone route would pull the CodeMirror editor into its bundle. - The window title comes from the route's `generateMetadata`, not `document.title`, which the App Router overwrites after hydration. - The viewer reads content through `useStandaloneArtifactContent`, which shares `useArtifactContent`'s query key but not its `useThread` dependency, since a detached window has no thread context. Claude-Session: https://claude.ai/code/session_013AiCrC5SBc3HdFYNxsp1EC * fix(frontend): keep the artifact target across re-authentication Review found the standalone viewer unrecoverable from an expired session. The window's target lives entirely in `?path=...&thread_id=...`, and both auth paths dropped it: - The layout guard redirected to `/login` with no `next` at all. A layout cannot read `searchParams`, so the guard moves into the page, which can — and rebuilds the full viewer address for `next`. The layout loses its AuthProvider along the way: nothing under this route reads `useAuth`, and the guard now makes a single `getServerSideUser` call per request. - The shared fetch wrapper built `next` from `window.location.pathname`, which silently truncated the query string. It now carries `search` too, so any route holding state in the query survives a 401, not just this one. `validateAuthNextPath` already accepts a query string. `buildArtifactViewerURL` is split out of `resolveArtifactOpenURL`: the guard needs the route itself, never the Gateway fallback that the latter takes for non-markdown targets. Tests: the login round trip (unit — the rebuilt URL survives `validateAuthNextPath` and parses back to the same target), the fetch wrapper preserving the query on 401 (unit), and the expired-session window reaching `/login` with the artifact intact (E2E). The E2E asserts on the popup's navigation *requests*, since `(auth)/layout` answers `/login` with a server redirect under DEER_FLOW_AUTH_DISABLED and no navigation commits. `tests/unit/core/models/api.test.ts` stubbed `window.location` without `search`; a real Location always has it. Claude-Session: https://claude.ai/code/session_013AiCrC5SBc3HdFYNxsp1EC * fix(frontend): keep public showcase artifacts out of the auth gate Review found that the viewer's access check regressed `/showcase`. Those pages render with `isMock`, their artifacts are served by the unauthenticated demo route, and the raw artifact URL this window replaced stayed public — so gating the window unconditionally bounced every logged-out showcase visitor to /login for a document that is already public. `requiresAuthenticatedViewer` exempts a mock target only when `resolveStaticDemoArtifact` would actually serve it. The allowlist is the authority rather than the flag: `mock=true` is caller-supplied, so a target the demo route answers with 404 — a non-allowlisted path, or a thread that is not a demo thread — still needs a session. Covered in `tests/e2e-auth/`, since the default E2E config disables auth and cannot see this: a public showcase artifact renders without a session, while a non-allowlisted path and a missing mock flag both land on /login. Verified the positive case goes red without the exemption. Claude-Session: https://claude.ai/code/session_013AiCrC5SBc3HdFYNxsp1EC
125 lines
3.7 KiB
TypeScript
125 lines
3.7 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, rs } from "@rstest/core";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import { cleanup, render, screen, waitFor } from "@testing-library/react";
|
|
import type { PropsWithChildren } from "react";
|
|
|
|
rs.mock("@/core/artifacts/loader", () => ({
|
|
loadArtifactContent: rs.fn(),
|
|
loadArtifactContentFromToolCall: rs.fn(),
|
|
}));
|
|
|
|
import { ArtifactViewer } from "@/components/workspace/artifacts/artifact-viewer";
|
|
import { loadArtifactContent } from "@/core/artifacts/loader";
|
|
import { urlOfArtifact } from "@/core/artifacts/utils";
|
|
import { I18nProvider } from "@/core/i18n/context";
|
|
|
|
const mockedLoadArtifactContent = rs.mocked(loadArtifactContent);
|
|
const filepath = "/mnt/user-data/outputs/report.md";
|
|
const threadId = "7cfa5f8f-a2f8-47ad-acbd-da7137baf990";
|
|
|
|
function Wrapper({ children }: PropsWithChildren) {
|
|
const queryClient = new QueryClient({
|
|
defaultOptions: { queries: { retry: false } },
|
|
});
|
|
return (
|
|
<QueryClientProvider client={queryClient}>
|
|
<I18nProvider initialLocale="en-US">{children}</I18nProvider>
|
|
</QueryClientProvider>
|
|
);
|
|
}
|
|
|
|
function renderViewer(props?: { isMock?: boolean }) {
|
|
return render(
|
|
<Wrapper>
|
|
<ArtifactViewer filepath={filepath} threadId={threadId} {...props} />
|
|
</Wrapper>,
|
|
);
|
|
}
|
|
|
|
describe("ArtifactViewer", () => {
|
|
beforeEach(() => {
|
|
mockedLoadArtifactContent.mockResolvedValue({
|
|
content: "# Quarterly report\n\nRevenue is up.",
|
|
url: urlOfArtifact({ filepath, threadId }),
|
|
sha256: undefined,
|
|
truncated: false,
|
|
previewBytes: 36,
|
|
totalBytes: 36,
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
cleanup();
|
|
mockedLoadArtifactContent.mockReset();
|
|
});
|
|
|
|
it("renders the markdown artifact with the app's markdown renderer", async () => {
|
|
const { container } = renderViewer();
|
|
|
|
await waitFor(() => {
|
|
expect(container.querySelector("h1")?.textContent).toContain(
|
|
"Quarterly report",
|
|
);
|
|
});
|
|
expect(container.textContent).not.toContain("# Quarterly report");
|
|
expect(mockedLoadArtifactContent).toHaveBeenCalledWith({
|
|
filepath,
|
|
threadId,
|
|
isMock: false,
|
|
full: false,
|
|
});
|
|
});
|
|
|
|
it("loads the mock artifact source when opened from a mock thread", async () => {
|
|
renderViewer({ isMock: true });
|
|
|
|
await waitFor(() => {
|
|
expect(mockedLoadArtifactContent).toHaveBeenCalledWith({
|
|
filepath,
|
|
threadId,
|
|
isMock: true,
|
|
full: false,
|
|
});
|
|
});
|
|
});
|
|
|
|
it("fetches the whole file when a truncated preview is expanded", async () => {
|
|
mockedLoadArtifactContent.mockImplementation(async ({ full }) => ({
|
|
content: full ? "# Full report\n\nEverything." : "# Full rep",
|
|
url: urlOfArtifact({ filepath, threadId }),
|
|
sha256: undefined,
|
|
truncated: !full,
|
|
previewBytes: full ? 27 : 10,
|
|
totalBytes: 27,
|
|
}));
|
|
const { container } = renderViewer();
|
|
|
|
const loadFull = await screen.findByRole("button", {
|
|
name: "Load full file",
|
|
});
|
|
loadFull.click();
|
|
|
|
await waitFor(() => {
|
|
expect(container.textContent).toContain("Everything.");
|
|
});
|
|
expect(mockedLoadArtifactContent).toHaveBeenLastCalledWith({
|
|
filepath,
|
|
threadId,
|
|
isMock: false,
|
|
full: true,
|
|
});
|
|
expect(screen.queryByRole("button", { name: "Load full file" })).toBe(null);
|
|
});
|
|
|
|
it("offers a download when the artifact cannot be loaded", async () => {
|
|
mockedLoadArtifactContent.mockRejectedValue(new Error("boom"));
|
|
|
|
renderViewer();
|
|
|
|
const download = await screen.findByRole("link", { name: /download/i });
|
|
expect(download.getAttribute("href")).toBe(
|
|
urlOfArtifact({ filepath, threadId, download: true }),
|
|
);
|
|
});
|
|
});
|