Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chat): count words, skip technical tokens, keep addresses off RTL text
Chidambaram56840a5f6869
diff --git a/docs/verification/bidi.md b/docs/verification/bidi.mdindex 28aa9c5b30..90f69f8aac 100644--- a/docs/verification/bidi.md+++ b/docs/verification/bidi.md@@ -25,9 +25,10 @@ these checks. What is being verified is content direction, per block.2. In the Arabic reply, the heading, paragraphs, list and quote all readright-to-left — bullets on the right, the quote's rule on the right. Thatincludes the paragraph opening on "Chrome 158" and the list whose items- open on "Node 24" and "pnpm 10": the script with more letters decides a- block, so a Latin name at the start does not turn it left-to-right, and a- bare address is not counted.+ open on "Node 24" and "pnpm 10": the script with more words decides a+ block, so a Latin name at the start does not turn it left-to-right. Bare+ addresses and technical tokens (versions, paths, flags, camelCase names)+ are not counted.3. Its table's columns run right-to-left, and every header sits over its owndata. A header drifting to the opposite edge from its column means cellsare resolving individually instead of inheriting the table.diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.tsindex 71236c0cc9..d2fd01dfce 100644--- a/src/components/ChatMarkdown.test.ts+++ b/src/components/ChatMarkdown.test.ts@@ -1290,7 +1290,7 @@ describe("a fence's wrap directive", () => {describe("bidi: message content carries its own direction", () => {const ARABIC = "مرحبا بالعالم";- it("goes with the script that has more letters, so an Arabic answer opening on a Latin name stays right-to-left (#6541)", () => {+ it("goes with the script that has more words, so an Arabic answer opening on a Latin name stays right-to-left", () => {expect(textDirection("Chrome 158 هو الإصدار الأحدث من المتصفح")).toBe("rtl");expect(textDirection("Node.js بيئة تشغيل لجافاسكريبت على الخادم")).toBe("rtl");expect(textDirection("OpenMausBot v0.1.169 صدر في 2026-11-18 مع إصلاحات كثيرة")).toBe("rtl");@@ -1299,13 +1299,36 @@ describe("bidi: message content carries its own direction", () => {});it("does not count a bare address, which is Latin in any language", () => {- // Counted, the address's letters would outnumber the Arabic ones.expect(textDirection("راجع https://github.com/milind-soni/OpenMausBot/issues/6541 للتفاصيل")).toBe("rtl");expect(textDirection("https://openmausbot.com/download هو رابط التحميل الرسمي")).toBe("rtl");expect(textDirection("انظر www.example.com/a/very/long/path/to/the/release/notes هنا")).toBe("rtl");expect(textDirection("https://example.com/page")).toBe("ltr");});+ it("keeps an Arabic sentence right-to-left when it holds long technical Latin tokens", () => {+ expect(textDirection("شغّل pnpm install --frozen-lockfile بعدين")).toBe("rtl");+ expect(textDirection("استخدم TypeScriptLanguageServer")).toBe("rtl");+ expect(textDirection("راجع pnpm.io/installation/and/more/details/here للتفاصيل")).toBe("rtl");+ });++ it("keeps an English sentence left-to-right when it holds an Arabic name", () => {+ expect(textDirection("Meeting with محمد عبد الرحمن at 5")).toBe("ltr");+ expect(textDirection("Set process.env.NODE_ENV in config.ts for مرحبا")).toBe("ltr");+ });++ it("does not let an address swallow the RTL text glued to it", () => {+ expect(textDirection("API https://x.io/aوالتفاصيل هنا")).toBe("rtl");+ expect(textDirection("التوثيق (https://pnpm.io/installation) فيه كل التفاصيل")).toBe("rtl");+ expect(textDirection("مرحبا welcome https://example.com،والتفاصيل")).toBe("rtl");+ });++ it("keeps a link label and the Arabic prose right after it apart when judging the block", () => {+ const html = renderToStaticMarkup(createElement(ChatMarkdown, {+ text: "مرحبا welcome [https://example.com](https://example.com)،والتفاصيل",+ }));+ expect(html).toMatch(/<p dir="rtl"/);+ });+it("gives a list whose items open on Latin names the direction of its text", () => {const html = renderToStaticMarkup(createElement(ChatMarkdown, {text: [diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsxindex fb42684b64..af19c14ee0 100644--- a/src/components/ChatMarkdown.tsx+++ b/src/components/ChatMarkdown.tsx@@ -155,22 +155,25 @@ function unwrapLinkedImages() {// directions falls back to LTR. JS regexes cannot match Bidi_Class, so these are the blocks Unicode reserves for// right-to-left letters.const RTL_LETTER = /[\u0590-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF\u{10800}-\u{10FFF}\u{1E800}-\u{1EFFF}]/u;-/** A bare address is Latin whatever language the sentence around it is in, so its letters are not counted. */-const ADDRESS = /(?:[a-z][a-z0-9+.-]*:\/\/|www\.)\S+/giu;+const ADDRESS = /(?:[a-z][a-z0-9+.-]*:\/\/|www\.)[^\s\u0590-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF()]+/giu;+const TECHNICAL = /[\d._/\\@=]|^-|\p{Ll}\p{Lu}/u;-/** The script with more letters decides; the first letter only breaks a tie. An Arabic answer often opens on a name- * ("Chrome 158 \u0647\u0648 \u0627\u0644\u0625\u0635\u062F\u0627\u0631 \u0627\u0644\u0623\u062D\u062F\u062B", "**Node.js** \u0628\u064A\u0626\u0629 \u062A\u0634\u063A\u064A\u0644"), and the first letter alone laid that whole paragraph or- * list out left-to-right: the Arabic words, numbers and Latin names in it reordered, and its bullets went left (#6541). */export function textDirection(value: string): "rtl" | "ltr" {+ const text = value.replace(ADDRESS, " ");let rtl = 0, ltr = 0, first: "rtl" | "ltr" | null = null;- for (const [letter] of value.replace(ADDRESS, " ").matchAll(/\p{Letter}/gu)) {- const direction = RTL_LETTER.test(letter) ? "rtl" : "ltr";+ for (const word of text.split(/\s+/)) {+ const direction = RTL_LETTER.test(word) ? "rtl"+ : /\p{Letter}/u.test(word) && !TECHNICAL.test(word) ? "ltr"+ : null;+ if (!direction) continue;first ??= direction;if (direction === "rtl") rtl += 1;else ltr += 1;}if (rtl !== ltr) return rtl > ltr ? "rtl" : "ltr";- return first ?? "ltr";+ if (first) return first;+ const strong = /\p{Letter}/u.exec(text);+ return strong && RTL_LETTER.test(strong[0]) ? "rtl" : "ltr";}interface HastNode {