Skip to content

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

Merge pull request #3795 from jagtapazad/feat/plain-file-links

milind-sonic456fc599251
diff --cc server/message-file.ts
index 8051f479c7,3dd1fe26e4..369d21730c
--- a/server/message-file.ts
+++ b/server/message-file.ts
@@@ -8,13 -8,10 +8,14 @@@ import { fileURLToPath, pathToFileURL
import { fromMarkdown } from "mdast-util-from-markdown";
import { windowsPathDestinations } from "../shared/markdown-windows-paths.ts";
+ import { linkPlainPaths } from "../shared/markdown-plain-paths.ts";
+import { PREVIEW_MAX_BYTES } from "../shared/upload-limits.ts";
import { inSecretFolder, SECRET_FILE_REFUSAL } from "./secret-folder.ts";
+import { userErrorThrown } from "./user-errors.ts";
-export const MESSAGE_FILE_MAX_BYTES = 25 * 1024 * 1024;
+export const MESSAGE_FILE_MAX_BYTES = PREVIEW_MAX_BYTES;
+/** Saving a linked file streams it to the person, so it may be larger than anything read into memory or previewed. */
+export const MESSAGE_FILE_DOWNLOAD_MAX_BYTES = 500 * 1024 * 1024;
export interface OpenedMessageFile {
handle: FileHandle;
diff --cc src/components/ChatMarkdown.tsx
index dd5c0fa28d,b7a70c8425..fb13ff1a63
--- a/src/components/ChatMarkdown.tsx
+++ b/src/components/ChatMarkdown.tsx
@@@ -79,11 -77,17 +80,17 @@@ export function chatUrlTransform(value
}
/** Must parse link destinations exactly as the server's message-file parser does. */
+function remarkWindowsPathDestinations(this: { data(): object }) {
+ const data = this.data() as { fromMarkdownExtensions?: unknown[] };
+ (data.fromMarkdownExtensions ??= []).push(windowsPathDestinations);
+}
+
+ function remarkPlainPaths() {
+ return (tree: object, file: { value?: unknown }) => {
+ if (typeof file.value === "string") linkPlainPaths(tree, file.value);
+ };
+ }
+
-function remarkWindowsPathDestinations(this: { data(): object }) {
- const data = this.data() as { fromMarkdownExtensions?: unknown[] };
- (data.fromMarkdownExtensions ??= []).push(windowsPathDestinations);
-}
-
const gfmParseData: {
micromarkExtensions?: MarkdownParseOptions["extensions"];
fromMarkdownExtensions?: MarkdownParseOptions["mdastExtensions"];
@@@ -728,50 -699,13 +735,50 @@@ const MARKDOWN_COMPONENTS: Components
},
};
-const MAY_LINK_THREAD = /#|openmausbot/i;
+// A thread link is "#" directly before a title, or an app link. A heading's "## " never reaches the plugin as text.
+const MAY_LINK_THREAD = /#[^\s#]|openmausbot/i;
const NO_THREAD_REFS: ThreadRefsValue = { threads: [] };
-function ChatMarkdownComponent({ text, message, mentionPeers = NO_MENTION_PEERS, everyone = false, mentionPerson, delivered }: {
+// One plugin list for everything every message shares; a message adds mentions only when it has an "@" and thread
+// links only when it may name one, so most messages render the same way whatever the bot list holds.
- const BASE_REMARK_PLUGINS: NonNullable<MarkdownOptions["remarkPlugins"]> = [[remarkGfm, GFM_OPTIONS], remarkMath, remarkWindowsPathDestinations, unwrapLinkedImages, remarkSoftBreaks, remarkGithubRefs];
++const BASE_REMARK_PLUGINS: NonNullable<MarkdownOptions["remarkPlugins"]> = [[remarkGfm, GFM_OPTIONS], remarkMath, remarkWindowsPathDestinations, remarkPlainPaths, unwrapLinkedImages, remarkSoftBreaks, remarkGithubRefs];
+const NO_REHYPE_PLUGINS: NonNullable<MarkdownOptions["rehypePlugins"]> = [];
+let katexPlugins: { plugin: KatexPlugin; list: NonNullable<MarkdownOptions["rehypePlugins"]> } | null = null;
+const rehypePluginsFor = (math: KatexPlugin | null) => {
+ if (!math) return NO_REHYPE_PLUGINS;
+ if (katexPlugins?.plugin !== math) katexPlugins = { plugin: math, list: [math] };
+ return katexPlugins.list;
+};
+
+const markdownCache = createMarkdownRenderCache();
+const peerSignatures = new WeakMap<readonly MentionPeer[], string>();
+const peerSignature = (peers: readonly MentionPeer[]) => {
+ let signature = peerSignatures.get(peers);
+ if (signature === undefined) {
+ signature = peers.map((peer) => `${peer.name}\u0001${peer.hidden ? 1 : 0}${peer.person ? 1 : 0}${peer.color ?? ""}`).join("\u0002");
+ peerSignatures.set(peers, signature);
+ }
+ return signature;
+};
+// The thread list keeps its identity while its contents hold (ThreadRefsProvider), so a number per list stands for it.
+const threadListIds = new WeakMap<object, number>();
+let threadListCount = 0;
+const threadListId = (threads: object) => {
+ let id = threadListIds.get(threads);
+ if (id === undefined) threadListIds.set(threads, (id = ++threadListCount));
+ return id;
+};
+
+/** Test seam: drops every cached message. */
+export function clearChatMarkdownCache(): void {
+ markdownCache.clear();
+}
+
+function ChatMarkdownComponent({ text, message, mentionPeers = NO_MENTION_PEERS, everyone = false, mentionPerson, delivered, originals }: {
text: string; message?: MessageAttachmentContext;
mentionPeers?: readonly MentionPeer[]; everyone?: boolean; mentionPerson?: MentionPerson;
- delivered?: DeliveredFiles;
+ /** Delivered copies by file name, and the originals those copies came from (copy path -> original). */
+ delivered?: DeliveredFiles; originals?: DeliveredFiles;
}) {
// use() may be called conditionally: only a message that can hold a thread link subscribes to the thread list.
const { threads, currentBotId } = MAY_LINK_THREAD.test(text) ? use(ThreadRefsContext) : NO_THREAD_REFS;