Skip to content

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

Use the shared preview size limit for HTML and Markdown files

guylfeb8445d7f2276
diff --git a/src/components/HtmlFilePreview.dom.test.ts b/src/components/HtmlFilePreview.dom.test.ts
index aef7ac5f64..4b7da0be1d 100644
--- a/src/components/HtmlFilePreview.dom.test.ts
+++ b/src/components/HtmlFilePreview.dom.test.ts
@@ -34,8 +34,8 @@ describe("HTML file preview errors", () => {
expect(alertText()).toContain("Check your connection");
});
- it("opens a page the server sends", async () => {
- await open(async () => new Response("<h1>Report</h1>"));
+ it("opens an HTML page larger than 2 MiB", async () => {
+ await open(async () => new Response(" ".repeat(2 * 1024 * 1024) + "<h1>Report</h1>"));
expect(alertText()).toBe("");
expect(document.body.querySelector("iframe")?.getAttribute("srcdoc")).toContain("<h1>Report</h1>");
});
diff --git a/src/components/HtmlFilePreview.test.ts b/src/components/HtmlFilePreview.test.ts
index b68e160e6b..0e3a323962 100644
--- a/src/components/HtmlFilePreview.test.ts
+++ b/src/components/HtmlFilePreview.test.ts
@@ -2,7 +2,8 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, describe, expect, it, vi } from "vitest";
import { AttachedFileChip } from "./AttachmentPreview";
-import { HTML_PREVIEW_MAX_BYTES, HtmlPreviewFrame, OpenInBrowserButton, htmlPreviewDocument, isHtmlFile } from "./HtmlFilePreview";
+import { HtmlPreviewFrame, OpenInBrowserButton, htmlPreviewDocument, isHtmlFile } from "./HtmlFilePreview";
+import { PREVIEW_MAX_BYTES } from "../../shared/upload-limits";
import { readTableResponse } from "./TableFilePreview";
const message = { threadId: "t", messageId: "m" };
@@ -95,7 +96,7 @@ describe("OpenInBrowserButton", () => {
describe("bounded read", () => {
it("stops an HTML page at its own cap", async () => {
const signal = new AbortController().signal;
- expect(await readTableResponse(new Response("<p>ok</p>"), signal, HTML_PREVIEW_MAX_BYTES)).toBe("<p>ok</p>");
- await expect(readTableResponse(new Response(new Uint8Array(HTML_PREVIEW_MAX_BYTES + 1)), signal, HTML_PREVIEW_MAX_BYTES)).rejects.toThrow("size");
+ expect(await readTableResponse(new Response("<p>ok</p>"), signal, PREVIEW_MAX_BYTES)).toBe("<p>ok</p>");
+ await expect(readTableResponse(new Response(new Uint8Array(PREVIEW_MAX_BYTES + 1)), signal, PREVIEW_MAX_BYTES)).rejects.toThrow("size");
});
});
diff --git a/src/components/HtmlFilePreview.tsx b/src/components/HtmlFilePreview.tsx
index de54cb40b1..ef50f36ad1 100644
--- a/src/components/HtmlFilePreview.tsx
+++ b/src/components/HtmlFilePreview.tsx
@@ -4,8 +4,7 @@ import { requestMessageFile, useLocalFileSave, type MessageAttachmentContext } f
import { TableDialog } from "./RichTable";
import { readTableResponse } from "./TableFilePreview";
import { t } from "@/lib/i18n";
-
-export const HTML_PREVIEW_MAX_BYTES = 2 * 1024 * 1024;
+import { PREVIEW_MAX_BYTES } from "../../shared/upload-limits";
/** Security: the frame may draw what the file carries inline, and fetch nothing at all. */
export const HTML_PREVIEW_CSP = "default-src 'none'; img-src data:; style-src 'unsafe-inline'; font-src data:; media-src data:";
@@ -130,7 +129,7 @@ function HtmlFileContent({ path, name, message }: { path: string; name: string;
try {
const response = await requestMessageFile(path, message, controller.signal);
answered = true;
- const text = await readTableResponse(response, controller.signal, HTML_PREVIEW_MAX_BYTES);
+ const text = await readTableResponse(response, controller.signal, PREVIEW_MAX_BYTES);
if (!controller.signal.aborted) setSource(text);
} catch (reason) {
if (controller.signal.aborted) return;
@@ -141,7 +140,7 @@ function HtmlFileContent({ path, name, message }: { path: string; name: string;
})();
return () => controller.abort();
}, [path, message.threadId, message.messageId, attempt]);
- if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{typeof error === "object" ? t("html.refused", { reason: error.reason }) : t(error === "size" ? "html.tooLarge" : "html.loadFailed")}</p><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>;
+ if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="mb-3 text-ink-secondary">{typeof error === "object" ? t("html.refused", { reason: error.reason }) : t(error === "size" ? "attach.fileTooLarge" : "html.loadFailed")}</p><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>;
if (source === null) return <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("html.loading")}</div>;
return <div className="flex flex-col gap-2">
<div className="flex items-center justify-end gap-1">
diff --git a/src/components/MarkdownFilePreview.dom.test.ts b/src/components/MarkdownFilePreview.dom.test.ts
index d9eb9f123c..9b3c4bf554 100644
--- a/src/components/MarkdownFilePreview.dom.test.ts
+++ b/src/components/MarkdownFilePreview.dom.test.ts
@@ -31,6 +31,13 @@ describe("Markdown file preview errors and encodings", () => {
expect(host.textContent).toContain("Ação concluída");
});
+ it("previews Markdown larger than 2 MiB", async () => {
+ const source = "a".repeat(2 * 1024 * 1024) + "\n\n# Final heading";
+ await open(async () => new Response(source));
+ expect(host.querySelector("pre")?.textContent).toBe(source);
+ expect(host.querySelector('[aria-pressed="true"]')).toBeTruthy();
+ });
+
it("still opens UTF-8 files as UTF-8", async () => {
await open(async () => new Response(new TextEncoder().encode("# Relatório\n\nAção")));
expect(host.textContent).toContain("Relatório");
diff --git a/src/components/MarkdownFilePreview.test.ts b/src/components/MarkdownFilePreview.test.ts
index 3d5c8412a4..8d30e4fa90 100644
--- a/src/components/MarkdownFilePreview.test.ts
+++ b/src/components/MarkdownFilePreview.test.ts
@@ -3,7 +3,8 @@ import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { AttachedFileChip } from "./AttachmentPreview";
import { isMarkdownFile } from "./HtmlFilePreview";
-import MarkdownFileContent, { MARKDOWN_PREVIEW_MAX_BYTES, MarkdownFileView } from "./MarkdownFilePreview";
+import MarkdownFileContent, { MarkdownFileView } from "./MarkdownFilePreview";
+import { PREVIEW_MAX_BYTES } from "../../shared/upload-limits";
import { readTableResponse } from "./TableFilePreview";
const message = { threadId: "t", messageId: "m" };
@@ -56,13 +57,13 @@ describe("MarkdownFileView (#4765)", () => {
it("stops a file at its cap", async () => {
const signal = new AbortController().signal;
- await expect(readTableResponse(new Response(new Uint8Array(MARKDOWN_PREVIEW_MAX_BYTES + 1)), signal, MARKDOWN_PREVIEW_MAX_BYTES)).rejects.toThrow("size");
+ await expect(readTableResponse(new Response(new Uint8Array(PREVIEW_MAX_BYTES + 1)), signal, PREVIEW_MAX_BYTES)).rejects.toThrow("size");
});
it("still reports the cap when the oversized body already errored", async () => {
const body = new ReadableStream({ start(controller) { controller.error(new Error("network")); } });
- const response = new Response(body, { headers: { "content-length": String(MARKDOWN_PREVIEW_MAX_BYTES + 1) } });
- await expect(readTableResponse(response, new AbortController().signal, MARKDOWN_PREVIEW_MAX_BYTES)).rejects.toThrow("size");
+ const response = new Response(body, { headers: { "content-length": String(PREVIEW_MAX_BYTES + 1) } });
+ await expect(readTableResponse(response, new AbortController().signal, PREVIEW_MAX_BYTES)).rejects.toThrow("size");
});
it("keeps Save and Retry on screen while the file is still loading, wrapping for long names", () => {
diff --git a/src/components/MarkdownFilePreview.tsx b/src/components/MarkdownFilePreview.tsx
index fe236e9370..7984161211 100644
--- a/src/components/MarkdownFilePreview.tsx
+++ b/src/components/MarkdownFilePreview.tsx
@@ -4,8 +4,9 @@ import { requestMessageFile, useLocalFileSave, type MessageAttachmentContext } f
import { ChatMarkdown } from "./ChatMarkdown";
import { readTableResponse } from "./TableFilePreview";
import { t } from "@/lib/i18n";
+import { PREVIEW_MAX_BYTES } from "../../shared/upload-limits";
-export const MARKDOWN_PREVIEW_MAX_BYTES = 2 * 1024 * 1024;
+const AUTO_RENDER_MAX_CHARS = 2 * 1024 * 1024;
/** A Markdown file in a message, drawn the way the chat draws a reply. Security: no message context is passed on, so
* paths written in the file stay plain text instead of becoming links that fetch other files. */
@@ -25,8 +26,11 @@ export default function MarkdownFileContent({ path, name, message }: { path: str
const response = await requestMessageFile(path, message, controller.signal);
answered = true;
// Notes saved by Windows apps are often Windows-1252, not UTF-8.
- const text = await readTableResponse(response, controller.signal, MARKDOWN_PREVIEW_MAX_BYTES, "windows-1252");
- if (!controller.signal.aborted) setSource(text);
+ const text = await readTableResponse(response, controller.signal, PREVIEW_MAX_BYTES, "windows-1252");
+ if (!controller.signal.aborted) {
+ setShowSource(text.length > AUTO_RENDER_MAX_CHARS);
+ setSource(text);
+ }
} catch (reason) {
if (controller.signal.aborted) return;
// A refusal from the server says why; only a request that got no answer (TypeError) is the connection.
@@ -38,7 +42,7 @@ export default function MarkdownFileContent({ path, name, message }: { path: str
}, [path, message.threadId, message.messageId, attempt]);
// Save and Retry stay on screen while loading too, so a stalled request never leaves only a spinner; they wrap so a long name can't push Retry off a narrow dialog.
const recovery = <><div className="mt-3 flex flex-wrap justify-center gap-2"><button type="button" className="inline-flex max-w-full min-w-0 items-center gap-1.5 rounded-lg border border-hairline px-3 py-2 hover:bg-raised" disabled={save.state === "saving"} onClick={() => void save.save()}><Download size={14} className="shrink-0" /><span className="min-w-0 break-all">{t("attach.saveAria", { name })}</span></button><button type="button" className="rounded-lg border border-hairline px-3 py-2 hover:bg-raised" onClick={() => setAttempt(attempt + 1)}>{t("chat.retry")}</button></div>{save.state === "failed" && <p role="alert" className="mt-2 text-[12px] text-danger">{save.reason}</p>}</>;
- if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="text-ink-secondary">{typeof error === "object" ? t("markdown.refused", { reason: error.reason }) : t(error === "size" ? "markdown.tooLarge" : error === "encoding" ? "markdown.notText" : "markdown.loadFailed")}</p>{recovery}</div>;
+ if (error) return <div className="p-8 text-center text-sm"><p role="alert" className="text-ink-secondary">{typeof error === "object" ? t("markdown.refused", { reason: error.reason }) : t(error === "size" ? "attach.fileTooLarge" : error === "encoding" ? "markdown.notText" : "markdown.loadFailed")}</p>{recovery}</div>;
if (source === null) return <div className="p-12 text-center text-sm text-ink-secondary"><span role="status" className="inline-flex items-center gap-2"><LoaderCircle size={16} className="animate-spin" />{t("markdown.loading")}</span>{recovery}</div>;
return <div className="flex flex-col gap-2">
<div className="flex items-center justify-end gap-1">