fix(webui): check explicit file links before enabling previews (#6483)
This commit is contained in:
1 parent
d1451e30e9
commit
69d2fd87b0
4 files changed
+37
-21
No files matched your search
@@ -157,6 +157,7 @@ for (const narrow of [false, true]) {
|
||||
await expect(sentPrefix).toHaveCSS("font-weight", "400");
|
||||
const sentCommand = page.locator("[data-user-text-bubble]").last().locator("p");
|
||||
expect(await sentCommand.textContent()).toBe(shellCommand.trim());
|
||||
await sentCommand.click();
|
||||
await sentCommand.evaluate(node => {
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(node);
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
useEffect,
|
||||
useMemo,
|
||||
useState,
|
||||
type ComponentProps,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@@ -86,13 +87,11 @@ type AvailabilityResult = {
|
||||
resolve: FilePreviewAvailabilityResolver;
|
||||
};
|
||||
|
||||
function InferredFileReferenceChip({
|
||||
path,
|
||||
function AvailableFileReferenceChip({
|
||||
onOpen,
|
||||
}: {
|
||||
path: string;
|
||||
onOpen?: (path: string) => void;
|
||||
}) {
|
||||
...props
|
||||
}: ComponentProps<typeof FileReferenceChip>) {
|
||||
const path = props.previewPath || props.tooltipPath || props.path;
|
||||
const resolve = useFilePreviewAvailabilityResolver();
|
||||
const [result, setResult] = useState<AvailabilityResult | null>(null);
|
||||
|
||||
@@ -118,7 +117,7 @@ function InferredFileReferenceChip({
|
||||
);
|
||||
return (
|
||||
<FileReferenceChip
|
||||
path={path}
|
||||
{...props}
|
||||
onOpen={onOpen && resolvedAvailable ? onOpen : undefined}
|
||||
/>
|
||||
);
|
||||
@@ -557,7 +556,7 @@ export default function MarkdownTextRenderer({
|
||||
const raw = String(kids).replace(/\n$/, "");
|
||||
if (!documentPath && isLikelyFilePath(raw)) {
|
||||
return (
|
||||
<InferredFileReferenceChip
|
||||
<AvailableFileReferenceChip
|
||||
path={raw}
|
||||
onOpen={onOpenFilePreview}
|
||||
/>
|
||||
@@ -644,7 +643,7 @@ export default function MarkdownTextRenderer({
|
||||
if (filePath) {
|
||||
const label = nodeText(markdownChildren).trim();
|
||||
return (
|
||||
<FileReferenceChip
|
||||
<AvailableFileReferenceChip
|
||||
path={label || filePath}
|
||||
tooltipPath={filePath}
|
||||
previewPath={filePath}
|
||||
|
||||
@@ -136,20 +136,26 @@ describe("MarkdownTextRenderer", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps unavailable inferred inline file paths non-interactive", async () => {
|
||||
it.each([
|
||||
{ kind: "inferred", markdown: "Future file: `notes/missing.md`", path: "notes/missing.md", label: "missing.md" },
|
||||
{ kind: "explicit", markdown: "[Future report](notes/missing.md)", path: "notes/missing.md", label: "Future report" },
|
||||
{ kind: "malformed explicit", markdown: "[Python code](/home/example/workspace/model_check.py)", path: "/home/example/workspace/model_check.py", label: "Python code" },
|
||||
])("keeps unavailable $kind file references non-interactive", async ({ markdown, path, label }) => {
|
||||
const onOpenFilePreview = vi.fn();
|
||||
const resolve = vi.fn().mockResolvedValue(false);
|
||||
render(
|
||||
<FilePreviewAvailabilityProvider resolve={resolve}>
|
||||
<MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>
|
||||
{"Future file: `notes/missing.md`"}
|
||||
{markdown}
|
||||
</MarkdownTextRenderer>
|
||||
</FilePreviewAvailabilityProvider>,
|
||||
);
|
||||
|
||||
const reference = screen.getByTestId("inline-file-path");
|
||||
expect(reference).toHaveTextContent("missing.md");
|
||||
await waitFor(() => expect(resolve).toHaveBeenCalledWith("notes/missing.md"));
|
||||
expect(reference).toHaveTextContent(label);
|
||||
expect(reference).not.toHaveAttribute("role");
|
||||
expect(reference).toHaveClass("text-muted-foreground");
|
||||
await waitFor(() => expect(resolve).toHaveBeenCalledWith(path));
|
||||
expect(reference).not.toHaveAttribute("role");
|
||||
expect(reference).not.toHaveAttribute("tabindex");
|
||||
|
||||
@@ -158,7 +164,7 @@ describe("MarkdownTextRenderer", () => {
|
||||
expect(onOpenFilePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("keeps inferred inline file paths non-interactive when availability lookup fails", async () => {
|
||||
it.each(["inferred", "explicit"])("keeps %s file references non-interactive when availability lookup fails", async (kind) => {
|
||||
const onOpenFilePreview = vi.fn();
|
||||
let rejectAvailability!: (reason?: unknown) => void;
|
||||
const resolve = vi.fn(() => new Promise<boolean>((_resolve, reject) => {
|
||||
@@ -167,7 +173,7 @@ describe("MarkdownTextRenderer", () => {
|
||||
render(
|
||||
<FilePreviewAvailabilityProvider resolve={resolve}>
|
||||
<MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>
|
||||
{"Unreadable file: `notes/locked.md`"}
|
||||
{kind === "inferred" ? "Unreadable file: `notes/locked.md`" : "[Unreadable report](notes/locked.md)"}
|
||||
</MarkdownTextRenderer>
|
||||
</FilePreviewAvailabilityProvider>,
|
||||
);
|
||||
@@ -185,18 +191,25 @@ describe("MarkdownTextRenderer", () => {
|
||||
expect(onOpenFilePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("makes available inferred inline file paths previewable", async () => {
|
||||
it.each(["inferred", "explicit"])("makes available %s file references previewable after the probe succeeds", async (kind) => {
|
||||
const onOpenFilePreview = vi.fn();
|
||||
const resolve = vi.fn().mockResolvedValue(true);
|
||||
let resolveAvailability!: (available: boolean) => void;
|
||||
const resolve = vi.fn(() => new Promise<boolean>((settle) => {
|
||||
resolveAvailability = settle;
|
||||
}));
|
||||
render(
|
||||
<FilePreviewAvailabilityProvider resolve={resolve}>
|
||||
<MarkdownTextRenderer onOpenFilePreview={onOpenFilePreview}>
|
||||
{"Existing file: `notes/ready.md`"}
|
||||
{kind === "inferred" ? "Existing file: `notes/ready.md`" : "[Existing report](notes/ready.md)"}
|
||||
</MarkdownTextRenderer>
|
||||
</FilePreviewAvailabilityProvider>,
|
||||
);
|
||||
|
||||
const reference = screen.getByTestId("inline-file-path");
|
||||
expect(reference).not.toHaveAttribute("role");
|
||||
expect(reference).toHaveClass("text-muted-foreground");
|
||||
await waitFor(() => expect(resolve).toHaveBeenCalledWith("notes/ready.md"));
|
||||
await act(async () => resolveAvailability(true));
|
||||
await waitFor(() => expect(reference).toHaveAttribute("role", "button"));
|
||||
expect(reference).toHaveAttribute("tabindex", "0");
|
||||
|
||||
|
||||
@@ -1112,7 +1112,10 @@ describe("ThreadShell", () => {
|
||||
portal.remove();
|
||||
});
|
||||
|
||||
it("keeps inferred file paths non-interactive when the availability probe fails", async () => {
|
||||
it.each([
|
||||
{ kind: "inferred", content: "Unreadable file: `prompts/dream.md`", path: "prompts/dream.md" },
|
||||
{ kind: "explicit", content: "[Python code](/home/example/workspace/model_check.py)", path: "/home/example/workspace/model_check.py" },
|
||||
])("keeps $kind file references non-interactive when the availability probe fails", async ({ content, path }) => {
|
||||
await preloadMarkdownText();
|
||||
const client = makeClient();
|
||||
let resolveProbe!: (value: Response) => void;
|
||||
@@ -1123,7 +1126,7 @@ describe("ThreadShell", () => {
|
||||
const url = String(input);
|
||||
if (url.includes("websocket%3Apreview-error/webui-thread")) {
|
||||
return Promise.resolve(httpJson(transcriptFromSimpleMessages([
|
||||
{ role: "assistant", content: "Unreadable file: `prompts/dream.md`" },
|
||||
{ role: "assistant", content },
|
||||
])));
|
||||
}
|
||||
if (url.includes("websocket%3Apreview-error/file-preview?")) return probe;
|
||||
@@ -1146,7 +1149,7 @@ describe("ThreadShell", () => {
|
||||
|
||||
const reference = await screen.findByTestId("inline-file-path");
|
||||
await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(
|
||||
expect.stringContaining("file-preview?path=prompts%2Fdream.md&probe=1"),
|
||||
expect.stringContaining(`file-preview?path=${encodeURIComponent(path)}&probe=1`),
|
||||
expect.anything(),
|
||||
));
|
||||
await act(async () => {
|
||||
|
||||
Reference in new issue
Block a user