Skip to content

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

Open a text file a bot links in a reply in the side preview

AdityaUmale0209cfbae01d
diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx
index 8274b346ca..051f9d06b1 100644
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@ -363,10 +363,11 @@ export function MermaidDiagram({ code }: MermaidDiagramProps) {
// source: the original a delivered copy came from, offered on right-click (Show in Finder) like in the gallery.
function LocalFileLink({ filePath, shownPath, shownName, name, source, children, message }: { filePath: string; shownPath: string; shownName: string; name?: string; source?: string; children?: ReactNode; message?: MessageAttachmentContext }) {
const save = useLocalFileSave(filePath, name, message);
- const preview = useHtmlPreview(filePath, shownName, message, shownPath);
const revealMenu = useRevealOriginalMenu();
const fileOutsideWorkspace = useMessageFileOutsideWorkspace(filePath, message);
const revealHere = typeof window !== "undefined" && Boolean(window.ogb?.revealInFolder) && !window.ogb?.remoteClient?.active;
+ // A text file outside the chat's folder keeps Show in folder, so where the desktop app checks, the preview waits for it.
+ const preview = useHtmlPreview(filePath, shownName, message, shownPath, fileOutsideWorkspace === false || !(revealHere && canRevealHostPath(filePath)));
const revealOnClick = fileOutsideWorkspace === true && canRevealHostPath(shownPath) && revealHere;
const [revealProblem, setRevealProblem] = useState("");
if (!message) {
diff --git a/src/components/FilePreviewPanel.dom.test.ts b/src/components/FilePreviewPanel.dom.test.ts
index 1deaec20ff..51dd9e06f0 100644
--- a/src/components/FilePreviewPanel.dom.test.ts
+++ b/src/components/FilePreviewPanel.dom.test.ts
@@ -146,3 +146,44 @@ describe("a file that keeps the dialog", () => {
expect(fixture.dispatched).toEqual([]);
});
});
+
+describe("a text file a bot links in a reply", () => {
+ const NOTES = "Shopping list\n- apples\n# bread";
+ async function renderReply(text: string) {
+ await act(async () => root.render(createElement(Fragment, null,
+ createElement("main", { "data-look": "chat" }, createElement(ChatMarkdown, { text, message })),
+ createElement(FilePreviewPanel, { threadId: "t1" }),
+ )));
+ }
+
+ it("opens beside the conversation from its name, as the file's own text", async () => {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(NOTES)));
+ await renderReply("I wrote the notes: [notes.txt](notes.txt)");
+ await act(async () => host.querySelector<HTMLButtonElement>("main button[title='Preview: notes.txt']")!.click());
+ await vi.waitFor(() => expect(pane()?.querySelector("pre")?.textContent).toBe(NOTES));
+ expect(pane()?.getAttribute("data-file-preview")).toBe("notes.txt");
+ expect(pane()?.querySelector("button[aria-label='Source']")).toBeNull();
+ expect(host.querySelector("main button[aria-label='Save a copy of notes.txt']")).not.toBeNull();
+ });
+
+ it("still shows one outside the chat's folder in its folder on the desktop app", async () => {
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ error: "the linked file is outside this conversation's workspace", code: "outside_workspace" }), { status: 403, headers: { "content-type": "application/json" } })));
+ const revealInFolder = vi.fn(async () => "shown");
+ Object.assign(window, { ogb: { revealInFolder } });
+ await renderReply("Log: [run.log](/var/log/run.log)");
+ const show = await vi.waitFor(() => {
+ const button = host.querySelector<HTMLButtonElement>("main button[title='Show in folder']");
+ expect(button).not.toBeNull();
+ return button!;
+ });
+ await act(async () => show.click());
+ expect(revealInFolder).toHaveBeenCalledWith("/var/log/run.log");
+ expect(pane()).toBeNull();
+ });
+
+ it("is still saved from its name when attached instead", async () => {
+ await act(async () => root.render(createElement("main", { "data-look": "chat" }, createElement(AttachedFileChip, { file: { path: "/workspace/notes.txt", name: "notes.txt", private: true }, message }))));
+ expect(host.querySelector("main button[aria-label='Save a copy of notes.txt']")).not.toBeNull();
+ expect(host.querySelector("main button[aria-label='Preview: notes.txt']")).toBeNull();
+ });
+});
diff --git a/src/components/HtmlFilePreview.tsx b/src/components/HtmlFilePreview.tsx
index d9cefd40f5..71f965f0ce 100644
--- a/src/components/HtmlFilePreview.tsx
+++ b/src/components/HtmlFilePreview.tsx
@@ -21,6 +21,10 @@ export function isMarkdownFile(name: string): boolean {
return /\.(md|markdown)$/i.test(name);
}
+export function isPlainTextFile(name: string): boolean {
+ return /\.(txt|text|log)$/i.test(name);
+}
+
// Lazy, so the chat's Markdown renderer (which uses this file's chips) is not imported back into it.
const MarkdownFileContent = lazy(() => import("./MarkdownFilePreview"));
const WhatChangedContent = lazy(() => import("./WhatChangedView"));
@@ -52,17 +56,18 @@ export function htmlBrowserBridge(): ((source: string, name: string) => Promise<
}
/** The preview for an HTML or Markdown file in a message, opened by the eye button or by the file's name: beside the chat
- * when the window has room for it, else in a dialog. */
-export function useHtmlPreview(path: string, name: string, message?: MessageAttachmentContext, shownPath = path) {
+ * when the window has room for it, else in a dialog. plainText: a plain text file opens it too. */
+export function useHtmlPreview(path: string, name: string, message?: MessageAttachmentContext, shownPath = path, plainText = false) {
const [open, setOpen] = useState(false);
const opener = useRef<HTMLElement | null>(null);
const beside = useFileBesideChat();
const kind = message === undefined ? null
: isHtmlFile(name) || isHtmlFile(path) ? "html"
- : isMarkdownFile(name) || isMarkdownFile(path) ? "markdown" : null;
+ : isMarkdownFile(name) || isMarkdownFile(path) ? "markdown"
+ : plainText && (isPlainTextFile(name) || isPlainTextFile(path)) ? "text" : null;
return {
opens: kind !== null,
- label: t(kind === "markdown" ? "markdown.preview" : "html.preview", { name }),
+ label: t(kind === "html" ? "html.preview" : "markdown.preview", { name }),
show: (event: { currentTarget: HTMLElement }) => {
opener.current = event.currentTarget;
if (kind && message && beside({ kind, path, shownPath, name, message, opener: event.currentTarget })) return;
@@ -85,7 +90,7 @@ export function FilePreviewContent({ kind, path, shownPath = path, name, message
const delimiter = tableDelimiter(name) ?? tableDelimiter(path);
return delimiter && <TableFileContent key={key} path={path} shownPath={shownPath} name={name} message={message} delimiter={delimiter} />;
}
- 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>;
+ 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} plain={kind === "text"} /></Suspense></MarkdownPreviewBoundary>;
}
export function HtmlFileButton({ path, shownPath, name, message }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext }) {
diff --git a/src/components/MarkdownFilePreview.tsx b/src/components/MarkdownFilePreview.tsx
index aa504450bf..4f9566a3f2 100644
--- a/src/components/MarkdownFilePreview.tsx
+++ b/src/components/MarkdownFilePreview.tsx
@@ -13,8 +13,9 @@ import { PREVIEW_MAX_BYTES } from "../../shared/upload-limits";
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. */
-export default function MarkdownFileContent({ path, shownPath = path, name, message, fill = false }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext; fill?: boolean }) {
+ * paths written in the file stay plain text instead of becoming links that fetch other files. plain: a plain text
+ * file, always shown as its raw text. */
+export default function MarkdownFileContent({ path, shownPath = path, name, message, fill = false, plain = false }: { path: string; shownPath?: string; name: string; message: MessageAttachmentContext; fill?: boolean; plain?: boolean }) {
const [attempt, setAttempt] = useState(0);
const [source, setSource] = useState<string | null>(null);
const [error, setError] = useState<"size" | "load" | "encoding" | "outside" | { reason: string } | null>(null);
@@ -32,7 +33,7 @@ export default function MarkdownFileContent({ path, shownPath = path, name, mess
// Notes saved by Windows apps are often Windows-1252, not UTF-8.
const text = await readTableResponse(response, controller.signal, PREVIEW_MAX_BYTES, "windows-1252");
if (!controller.signal.aborted) {
- setShowSource(text.length > AUTO_RENDER_MAX_CHARS);
+ setShowSource(plain || text.length > AUTO_RENDER_MAX_CHARS);
setSource(text);
}
} catch (reason) {
@@ -52,7 +53,7 @@ export default function MarkdownFileContent({ path, shownPath = path, name, mess
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={cn("flex flex-col gap-2", fill && "min-h-0 flex-1")}>
<div className="flex items-center justify-end gap-1">
- <button type="button" className="table-action" aria-pressed={showSource} aria-label={t(showSource ? "markdown.rendered" : "html.source")} title={t(showSource ? "markdown.rendered" : "html.source")} disabled={source.length > AUTO_RENDER_MAX_CHARS} onClick={() => setShowSource(!showSource)}>{showSource ? <Eye size={14} /> : <Code2 size={14} />}</button>
+ {!plain && <button type="button" className="table-action" aria-pressed={showSource} aria-label={t(showSource ? "markdown.rendered" : "html.source")} title={t(showSource ? "markdown.rendered" : "html.source")} disabled={source.length > AUTO_RENDER_MAX_CHARS} onClick={() => setShowSource(!showSource)}>{showSource ? <Eye size={14} /> : <Code2 size={14} />}</button>}
<button type="button" className="table-action" aria-label={t("html.reload")} title={t("html.reload")} onClick={() => setAttempt(attempt + 1)}><RotateCcw size={14} /></button>
<button type="button" className="table-action" aria-label={t("attach.saveAria", { name })} title={t("attach.saveAria", { name })} disabled={save.state === "saving"} onClick={() => void save.save()}><Download size={14} /></button>
</div>
diff --git a/src/lib/file-preview.ts b/src/lib/file-preview.ts
index 49ad44ac5e..0e854ae01c 100644
--- a/src/lib/file-preview.ts
+++ b/src/lib/file-preview.ts
@@ -8,7 +8,7 @@ import { SIDEBAR_AND_PANEL_FIT } from "./use-media-query";
/** A file from a message, shown beside the chat. Renderer-only: nothing about it is stored or sent anywhere. */
export interface FilePreview {
- kind: "html" | "markdown" | "table" | "changes";
+ kind: "html" | "markdown" | "text" | "table" | "changes";
path: string;
shownPath?: string;
name: string;