Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(preview): carry the decoded path through table buttons and linked file chips

laijsg145efecb88021
diff --git a/src/components/AttachmentGallery.test.ts b/src/components/AttachmentGallery.test.ts
index c072f3233a..46846aa463 100644
--- a/src/components/AttachmentGallery.test.ts
+++ b/src/components/AttachmentGallery.test.ts
@@ -140,16 +140,16 @@ describe("message gallery", () => {
"Plain prose /workspace/private.txt",
].join("\n");
expect(collectMessageFiles(text, [image.path])).toEqual([
- { path: "./report.pdf", name: "report.pdf", linked: true },
- { path: "./demo.mp4", name: "demo.mp4", linked: true },
+ { path: "./report.pdf", shownPath: "./report.pdf", name: "report.pdf", linked: true },
+ { path: "./demo.mp4", shownPath: "./demo.mp4", name: "demo.mp4", linked: true },
]);
});
it("handles Windows and encoded paths while keeping a safe filename", () => {
expect(collectMessageFiles("[release](file:///C:/work/release%20notes.pdf) [video](./demo.MP4#section)"))
.toEqual([
- { path: "file:///C:/work/release%20notes.pdf", name: "release notes.pdf", linked: true },
- { path: "./demo.MP4#section", name: "demo.MP4", linked: true },
+ { path: "file:///C:/work/release%20notes.pdf", shownPath: "C:/work/release notes.pdf", name: "release notes.pdf", linked: true },
+ { path: "./demo.MP4#section", shownPath: "./demo.MP4", name: "demo.MP4", linked: true },
]);
expect(isVideoAttachment("./demo.MP4#section")).toBe(true);
expect(isVideoAttachment("./movie.mp4.html")).toBe(false);
@@ -157,16 +157,16 @@ describe("message gallery", () => {
it("sends a Windows path with its backslash before punctuation intact", () => {
expect(collectMessageFiles("[report](C:\\Users\\Maus\\.openmausbot\\workspaces\\bot\\report.md)")).toEqual([
- { path: "C:\\Users\\Maus\\.openmausbot\\workspaces\\bot\\report.md", name: "report.md", linked: true },
+ { path: "C:\\Users\\Maus\\.openmausbot\\workspaces\\bot\\report.md", shownPath: "C:\\Users\\Maus\\.openmausbot\\workspaces\\bot\\report.md", name: "report.md", linked: true },
]);
});
it("does not decode file URLs twice or mistake filename characters for URL suffixes", () => {
expect(collectMessageFiles("[video](file:///work/demo%23one.mp4) [percent](file:///work/r%2520.pdf) [space](file:///work/r%20.pdf)"))
.toEqual([
- { path: "file:///work/demo%23one.mp4", name: "demo#one.mp4", linked: true },
- { path: "file:///work/r%2520.pdf", name: "r%20.pdf", linked: true },
- { path: "file:///work/r%20.pdf", name: "r .pdf", linked: true },
+ { path: "file:///work/demo%23one.mp4", shownPath: "/work/demo#one.mp4", name: "demo#one.mp4", linked: true },
+ { path: "file:///work/r%2520.pdf", shownPath: "/work/r%20.pdf", name: "r%20.pdf", linked: true },
+ { path: "file:///work/r%20.pdf", shownPath: "/work/r .pdf", name: "r .pdf", linked: true },
]);
expect(isVideoAttachment("file:///work/demo%23one.mp4")).toBe(true);
});
diff --git a/src/components/AttachmentGallery.tsx b/src/components/AttachmentGallery.tsx
index f6f453a544..088c805805 100644
--- a/src/components/AttachmentGallery.tsx
+++ b/src/components/AttachmentGallery.tsx
@@ -24,6 +24,7 @@ import { useEditSources } from "@/lib/use-edit-sources";
export interface GalleryFile extends TranscriptFileAttachment {
linked?: boolean;
+ shownPath?: string;
}
type MarkdownNode = {
@@ -62,7 +63,7 @@ export function collectMessageFiles(text: string, existingPaths: readonly string
if (seen.has(identity)) return [];
seen.add(identity);
// Security: send the authored spelling; the server owns the one decode at its authorization boundary.
- return [{ path: href, name: safeDownloadFilename(attachmentBasename(identity)), linked: true }];
+ return [{ path: href, shownPath: identity, name: safeDownloadFilename(attachmentBasename(identity)), linked: true }];
});
}
diff --git a/src/components/AttachmentPreview.tsx b/src/components/AttachmentPreview.tsx
index a313107cd3..8bfda619d1 100644
--- a/src/components/AttachmentPreview.tsx
+++ b/src/components/AttachmentPreview.tsx
@@ -416,8 +416,8 @@ export function MessageFolderFiles({ folderPath, listing, message }: {
);
}
-const REVEALABLE_PATH = /^(?:\/(?!\/)|[a-zA-Z]:[\\/])/;
-function revealBridge(): NonNullable<Window["ogb"]>["revealInFolder"] {
+export const REVEALABLE_PATH = /^(?:\/(?!\/)|[a-zA-Z]:[\\/])/;
+export function revealBridge(): NonNullable<Window["ogb"]>["revealInFolder"] {
const ogb = typeof window === "undefined" ? undefined : window.ogb;
return ogb?.remoteClient?.active ? undefined : ogb?.revealInFolder;
}
@@ -892,14 +892,14 @@ export function MarkdownImagePreview({
}
export function AttachedFileChip({ file, message, linked = false, compact = false, className }: {
- file: TranscriptFileAttachment;
+ file: TranscriptFileAttachment & { shownPath?: string };
message?: MessageAttachmentContext;
linked?: boolean;
compact?: boolean;
className?: string;
}) {
const save = useLocalFileSave(file.path, file.name, message);
- const preview = useHtmlPreview(file.path, file.name, message);
+ const preview = useHtmlPreview(file.path, file.name, message, file.shownPath);
const failed = save.state === "failed";
if (compact) return <CompactFileChip file={file} message={message} linked={linked} save={save} preview={preview} className={className} />;
if (!message || (!file.private && !linked)) {
@@ -934,7 +934,7 @@ export function AttachedFileChip({ file, message, linked = false, compact = fals
<Download size={13} className="shrink-0" />
)}
</button>
- {preview.opens ? <SaveIconButton name={file.name} save={save} retryable={save.retryable} /> : <FilePreviewButton path={file.path} name={file.name} message={message} />}
+ {preview.opens ? <SaveIconButton name={file.name} save={save} retryable={save.retryable} /> : <FilePreviewButton path={file.path} shownPath={file.shownPath} name={file.name} message={message} />}
{preview.dialog}
</div>
{save.state !== "idle" && (
@@ -952,7 +952,7 @@ export function AttachedFileChip({ file, message, linked = false, compact = fals
: saveFailureText(save)}
{failed && save.outsideWorkspace && (
<div className="mt-1 flex flex-wrap items-center gap-x-1.5">
- <OutsideWorkspaceFile filePath={file.path} />
+ <OutsideWorkspaceFile filePath={file.shownPath ?? file.path} />
</div>
)}
</div>
@@ -964,7 +964,7 @@ export function AttachedFileChip({ file, message, linked = false, compact = fals
const FILE_CHIP = "inline-flex h-7 max-w-[16rem] items-center rounded-full bg-ink/[0.06] text-[13px] leading-none text-ink-secondary";
function CompactFileChip({ file, message, linked, save, preview, className }: {
- file: TranscriptFileAttachment;
+ file: TranscriptFileAttachment & { shownPath?: string };
message?: MessageAttachmentContext;
linked: boolean;
save: ReturnType<typeof useLocalFileSave>;
@@ -1005,14 +1005,14 @@ function CompactFileChip({ file, message, linked, save, preview, className }: {
<Download size={13} className="shrink-0" aria-hidden="true" />
)}
</button>
- {preview.opens ? <SaveIconButton name={file.name} save={save} retryable={save.retryable} /> : <FilePreviewButton path={file.path} name={file.name} message={message} />}
+ {preview.opens ? <SaveIconButton name={file.name} save={save} retryable={save.retryable} /> : <FilePreviewButton path={file.path} shownPath={file.shownPath} name={file.name} message={message} />}
{preview.dialog}
</span>
{save.state !== "idle" && save.state !== "saved" && (
<span role={failed ? "alert" : "status"} className={cn("px-2.5 text-[12px]", failed ? "text-danger" : "text-ink-secondary")}>
{save.state === "saving" ? t("attach.downloading") : saveFailureText(save)}
{failed && save.outsideWorkspace && (
- <span className="ms-1.5 inline-flex flex-wrap items-center gap-x-1.5"><OutsideWorkspaceFile filePath={file.path} /></span>
+ <span className="ms-1.5 inline-flex flex-wrap items-center gap-x-1.5"><OutsideWorkspaceFile filePath={file.shownPath ?? file.path} /></span>
)}
</span>
)}
diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx
index cbe59f908e..4f1162e93e 100644
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@ -379,7 +379,7 @@ function LocalFileLink({ filePath, shownPath, shownName, name, source, children,
{revealMenu.menu}
{preview.opens
? <SaveIconButton name={shownName} save={save} retryable={save.retryable} />
- : <FilePreviewButton path={filePath} name={shownName} message={message} />}
+ : <FilePreviewButton path={filePath} shownPath={shownPath} name={shownName} message={message} />}
<button
type="button"
onClick={preview.opens ? preview.show : () => void save.save()}
diff --git a/src/components/FilePreviewButton.tsx b/src/components/FilePreviewButton.tsx
index c1f7a2c7bd..836a457527 100644
--- a/src/components/FilePreviewButton.tsx
+++ b/src/components/FilePreviewButton.tsx
@@ -2,7 +2,7 @@ import type { MessageAttachmentContext } from "./AttachmentPreview";
import { HtmlFileButton, isHtmlFile } from "./HtmlFilePreview";
import { TableFileButton } from "./TableFilePreview";
-export function FilePreviewButton({ path, name, message }: { path: string; name: string; message: MessageAttachmentContext }) {
- if (isHtmlFile(name) || isHtmlFile(path)) return <HtmlFileButton path={path} name={name} message={message} />;
- return <TableFileButton path={path} name={name} message={message} />;
+export function FilePreviewButton({ path, shownPath, name, message }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext }) {
+ if (isHtmlFile(name) || isHtmlFile(path)) return <HtmlFileButton path={path} shownPath={shownPath} name={name} message={message} />;
+ return <TableFileButton path={path} shownPath={shownPath} name={name} message={message} />;
}
diff --git a/src/components/FilePreviewContent.outside.dom.test.ts b/src/components/FilePreviewContent.outside.dom.test.ts
index 8cf51a8c31..1177130e1f 100644
--- a/src/components/FilePreviewContent.outside.dom.test.ts
+++ b/src/components/FilePreviewContent.outside.dom.test.ts
@@ -3,6 +3,8 @@ import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { setLocale } from "@/lib/i18n";
+import { MessageAttachmentGallery } from "./AttachmentGallery";
+import { ChatMarkdown } from "./ChatMarkdown";
import { FilePreviewContent } from "./HtmlFilePreview";
const message = { threadId: "t", messageId: "m" };
@@ -44,6 +46,7 @@ describe("a previewed file outside the chat's folders", () => {
const fetch = await show(kind, extension, outside);
expect(host.querySelector("[role=alert]")!.textContent).toBe("This file is outside this chat's working folder, so it can't be opened here");
expect(buttons()).toEqual([]);
+ expect(host.textContent).toContain("Find this file at this path on the computer running");
expect(host.querySelector("code")?.textContent).toBe(`${onDisk}${extension}`);
expect(fetch).toHaveBeenCalledTimes(1);
});
@@ -53,6 +56,7 @@ describe("a previewed file outside the chat's folders", () => {
Object.assign(window, { ogb: { revealInFolder } });
await show(kind, extension, outside);
expect(buttons()).toEqual(["Show in folder"]);
+ expect(host.textContent).not.toContain("Find this file at this path");
await act(async () => host.querySelector("button")!.click());
expect(revealInFolder).toHaveBeenCalledWith(`${onDisk}${extension}`);
});
@@ -70,3 +74,34 @@ describe("a previewed file outside the chat's folders", () => {
expect(host.querySelector("[role=alert]")!.textContent).toBe("Diese Datei liegt außerhalb des Arbeitsordners dieses Chats und kann hier nicht geöffnet werden");
});
});
+
+describe("a linked file outside the chat's folders, opened from the chat", () => {
+ async function openFrom(element: ReturnType<typeof createElement>, label: string) {
+ const fetch = vi.fn(async (_url: string, _init?: RequestInit) => outside());
+ const revealInFolder = vi.fn(async () => "shown");
+ vi.stubGlobal("fetch", fetch);
+ Object.assign(window, { ogb: { revealInFolder } });
+ await act(async () => root.render(element));
+ await act(async () => document.body.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`)!.click());
+ const show = await vi.waitFor(() => {
+ const button = [...document.body.querySelectorAll("button")].find((candidate) => candidate.textContent === "Show in folder");
+ expect(button).toBeDefined();
+ return button!;
+ }, { timeout: 5_000 });
+ await act(async () => show.click());
+ const asked = JSON.parse(String(fetch.mock.calls[0]![1]!.body)) as { path: string };
+ return { asked: asked.path, revealed: revealInFolder.mock.calls[0] };
+ }
+
+ it("asks for a table by the link as written and shows it in its folder by its real path", async () => {
+ const opened = await openFrom(createElement(ChatMarkdown, { text: `Report: [Q3 report.csv](${linked}.csv)`, message }), "Preview table: Q3 report.csv");
+ expect(opened.asked).toBe(`${linked}.csv`);
+ expect(opened.revealed).toEqual([`${onDisk}.csv`]);
+ });
+
+ it("shows a file:// link from the files under a reply in its folder by its real path", async () => {
+ const opened = await openFrom(createElement(MessageAttachmentGallery, { text: `Report: [Q3](file://${linked}.md)`, message }), "Preview: Q3 report.md");
+ expect(opened.asked).toBe(`file://${linked}.md`);
+ expect(opened.revealed).toEqual([`${onDisk}.md`]);
+ });
+});
diff --git a/src/components/HtmlFilePreview.tsx b/src/components/HtmlFilePreview.tsx
index 3b1676a161..fbca93fc98 100644
--- a/src/components/HtmlFilePreview.tsx
+++ b/src/components/HtmlFilePreview.tsx
@@ -89,8 +89,8 @@ export function FilePreviewContent({ kind, path, shownPath = path, name, message
return <MarkdownPreviewBoundary><Suspense fallback={<div role="status" className="flex items-center justify-center gap-2 p-12 text-sm text-ink-secondary"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</div>}><MarkdownFileContent key={key} path={path} shownPath={shownPath} name={name} message={message} fill={fill} /></Suspense></MarkdownPreviewBoundary>;
}
-export function HtmlFileButton({ path, name, message }: { path: string; name: string; message: MessageAttachmentContext }) {
- const preview = useHtmlPreview(path, name, message);
+export function HtmlFileButton({ path, shownPath, name, message }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext }) {
+ const preview = useHtmlPreview(path, name, message, shownPath);
return <>
<button type="button" className="table-action" title={preview.label} aria-label={preview.label} onClick={preview.show}><Eye size={14} /></button>
{preview.dialog}
diff --git a/src/components/OutsideWorkspacePreview.tsx b/src/components/OutsideWorkspacePreview.tsx
index 35b5cc4475..fbe657322f 100644
--- a/src/components/OutsideWorkspacePreview.tsx
+++ b/src/components/OutsideWorkspacePreview.tsx
@@ -1,13 +1,14 @@
-import { OutsideWorkspaceFile } from "./AttachmentPreview";
+import { OutsideWorkspaceFile, REVEALABLE_PATH, revealBridge } from "./AttachmentPreview";
import { t } from "@/lib/i18n";
export const refusedAsOutsideWorkspace = (error: unknown): boolean => (error as { code?: unknown } | null)?.code === "outside_workspace";
-/** A file preview the server refused as outside the chat's folders: saving or retrying would be refused the same way. */
export function OutsideWorkspacePreview({ filePath }: { filePath: string }) {
+ const revealable = Boolean(revealBridge()) && REVEALABLE_PATH.test(filePath);
return (
<div className="p-8 text-center text-sm">
<p role="alert" className="mb-3 text-ink-secondary">{t("attach.previewOutsideWorkspace")}</p>
+ {!revealable && <p className="mb-1 text-[12px] text-ink-secondary">{t("attach.outsideWorkspacePath")}</p>}
<div className="flex flex-wrap items-center justify-center gap-x-1.5 text-[12px]"><OutsideWorkspaceFile filePath={filePath} /></div>
</div>
);
diff --git a/src/components/TableFilePreview.tsx b/src/components/TableFilePreview.tsx
index 677d97b79a..ea34cb6027 100644
--- a/src/components/TableFilePreview.tsx
+++ b/src/components/TableFilePreview.tsx
@@ -37,18 +37,18 @@ export async function readTableResponse(response: Response, signal: AbortSignal,
}
}
-export function TableFileButton({ path, name, message }: { path: string; name: string; message: MessageAttachmentContext }) {
+export function TableFileButton({ path, shownPath, name, message }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext }) {
const [open, setOpen] = useState(false);
const trigger = useRef<HTMLButtonElement>(null);
const beside = useFileBesideChat();
const delimiter = tableDelimiter(name) ?? tableDelimiter(path);
if (!delimiter) return null;
const show = () => {
- if (!beside({ kind: "table", path, name, message, opener: trigger.current })) setOpen(true);
+ if (!beside({ kind: "table", path, shownPath, name, message, opener: trigger.current })) setOpen(true);
};
return <>
<button ref={trigger} type="button" className="table-action" title={t("table.preview", { name })} aria-label={t("table.preview", { name })} onClick={show}><Table2 size={14} /></button>
- {open && <TableDialog title={name} returnFocus={trigger.current} onClose={() => setOpen(false)}><TableFileContent key={`${message.threadId}:${message.messageId}:${path}`} path={path} name={name} message={message} delimiter={delimiter} /></TableDialog>}
+ {open && <TableDialog title={name} returnFocus={trigger.current} onClose={() => setOpen(false)}><TableFileContent key={`${message.threadId}:${message.messageId}:${path}`} path={path} shownPath={shownPath} name={name} message={message} delimiter={delimiter} /></TableDialog>}
</>;
}