Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: show right-to-left chat messages right to left (#16348)

Michael Hanf8a860964e97
diff --git a/studio/frontend/src/components/assistant-ui/markdown-text.tsx b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
index 8e2dce44905..40f67989b42 100644
--- a/studio/frontend/src/components/assistant-ui/markdown-text.tsx
+++ b/studio/frontend/src/components/assistant-ui/markdown-text.tsx
@@ -121,6 +121,7 @@ import {
import { MARKDOWN_TABLE_ICONS, MarkdownTable } from "./markdown-table";
import { SearchImageElement, SearchImagesContext } from "./search-image";
import { useSandboxImage } from "./use-sandbox-image";
+import { rehypeAutoDirection } from "./rehype-auto-direction";
import { rehypeSandboxImages } from "./rehype-sandbox-images";
import { unslothDarkTheme, unslothLightTheme } from "./code-themes";
import { stabilizeStreamingMarkdown } from "./streaming-markdown";
@@ -1405,6 +1406,7 @@ function MarkdownTextRenderer({
// Streamdown caches processors by plugin name and serialized options.
withDataImageSupport(STREAMDOWN_ALLOWED_TAGS, [
[rehypeSandboxImages, { threadId, projectId }],
+ rehypeAutoDirection,
]),
[threadId, projectId],
);
diff --git a/studio/frontend/src/components/assistant-ui/rehype-auto-direction.ts b/studio/frontend/src/components/assistant-ui/rehype-auto-direction.ts
new file mode 100644
index 00000000000..4ec7ae9a6b2
--- /dev/null
+++ b/studio/frontend/src/components/assistant-ui/rehype-auto-direction.ts
@@ -0,0 +1,98 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import type { Element, Root, RootContent } from "hast";
+import type { Plugin } from "unified";
+
+const TEXT_BLOCKS = new Set([
+ "p",
+ "h1",
+ "h2",
+ "h3",
+ "h4",
+ "h5",
+ "h6",
+ "td",
+ "th",
+ "dt",
+ "dd",
+]);
+// dir="auto" skips descendants that set their own dir, so it would resolve these from no text at all.
+const CONTAINERS = new Set(["ul", "ol", "blockquote"]);
+const BLOCKS = new Set([
+ ...TEXT_BLOCKS,
+ ...CONTAINERS,
+ "li",
+ "pre",
+ "div",
+ "table",
+ "hr",
+]);
+const LETTER = /\p{L}/u;
+const RTL_LETTER = /[\u0590-\u08FF\uFB1D-\uFDFF\uFE70-\uFEFF]/;
+
+function firstLetter(node: Element | RootContent): string | null {
+ if (node.type === "text") {
+ for (const char of node.value) {
+ if (LETTER.test(char)) return char;
+ }
+ return null;
+ }
+ if (
+ node.type !== "element" ||
+ node.tagName === "pre" ||
+ node.tagName === "code"
+ ) {
+ return null;
+ }
+ for (const child of node.children) {
+ const found = firstLetter(child);
+ if (found) return found;
+ }
+ return null;
+}
+
+// A tight item has no paragraph to carry dir; the item itself keeps the list's side for its marker.
+function wrapTightItemText(item: Element): void {
+ let end = 0;
+ while (end < item.children.length) {
+ const child = item.children[end];
+ if (child.type === "element" && BLOCKS.has(child.tagName)) break;
+ end += 1;
+ }
+ const inline = item.children.slice(0, end);
+ if (!inline.some((child) => firstLetter(child))) return;
+ const wrapper: Element = {
+ type: "element",
+ tagName: "span",
+ properties: { dir: "auto" },
+ children: inline as Element["children"],
+ };
+ item.children.splice(0, end, wrapper);
+}
+
+export const rehypeAutoDirection: Plugin<[], Root> =
+ function rehypeAutoDirection() {
+ return (tree) => {
+ function walk(node: Root | RootContent): void {
+ if (node.type === "element" && node.tagName === "li") {
+ wrapTightItemText(node);
+ }
+ if (node.type === "element" && node.properties.dir === undefined) {
+ if (TEXT_BLOCKS.has(node.tagName)) {
+ node.properties.dir = "auto";
+ } else if (CONTAINERS.has(node.tagName)) {
+ // Explicit ltr too, or an English list nested in an RTL one inherits rtl.
+ const letter = firstLetter(node);
+ if (letter) {
+ node.properties.dir = RTL_LETTER.test(letter) ? "rtl" : "ltr";
+ }
+ }
+ }
+ if ("children" in node) {
+ node.children.forEach(walk);
+ }
+ }
+ walk(tree);
+ };
+ };
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 6df06a4c17b..594f12166e8 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -8905,7 +8905,9 @@ const UserMessage: FC = () => {
<div className="aui-user-message-content-wrapper flex w-full min-w-0 flex-col items-end">
{hasContent ? (
- <div className="aui-user-message-content wrap-break-word w-fit max-w-[80%] rounded-xl bg-[#f5f5f5] px-4 py-2.5 text-[#0f1114] dark:text-foreground dark:bg-card">
+ <div
+ dir="auto"
+ className="aui-user-message-content wrap-break-word w-fit max-w-[80%] rounded-xl bg-[#f5f5f5] px-4 py-2.5 text-[#0f1114] dark:text-foreground dark:bg-card">
<MessagePrimitive.Parts />
</div>
) : null}
diff --git a/studio/frontend/src/index.css b/studio/frontend/src/index.css
index 88fb8894186..d58161ab584 100644
--- a/studio/frontend/src/index.css
+++ b/studio/frontend/src/index.css
@@ -4605,7 +4605,7 @@ html[data-chat-font] .aui-root {
[data-streamdown="unordered-list"] {
list-style-type: disc;
list-style-position: outside;
- padding-left: calc(1.25rem * var(--ui-space-scale, 1));
+ padding-inline-start: calc(1.25rem * var(--ui-space-scale, 1));
margin-block: calc(0.5rem * var(--ui-space-scale, 1));
}
@@ -4613,7 +4613,7 @@ html[data-chat-font] .aui-root {
[data-streamdown="ordered-list"] {
list-style-type: decimal;
list-style-position: outside;
- padding-left: 1.6em;
+ padding-inline-start: 1.6em;
margin-block: calc(0.5rem * var(--ui-space-scale, 1));
}
@@ -4622,17 +4622,23 @@ html[data-chat-font] .aui-root {
:has(> li:nth-child(10)),
[start]:not([start="1"], [start="2"], [start="3"], [start="4"], [start="5"], [start="6"], [start="7"], [start="8"], [start="9"])
) {
- padding-left: 2.2em;
+ padding-inline-start: 2.2em;
}
[data-streamdown="ordered-list"]:has(> li:nth-child(100)) {
- padding-left: 2.8em;
+ padding-inline-start: 2.8em;
}
[data-streamdown="list-item"] {
display: list-item;
}
+ /* Code stays LTR inside a reply or list that rehype-auto-direction made RTL. */
+ :is([data-streamdown="code-block"], [data-streamdown="inline-code"], .aui-reasoning-code-fragment) {
+ direction: ltr;
+ unicode-bidi: isolate;
+ }
+
.aui-reasoning-code-fragment {
padding-inline: calc(1rem * var(--ui-space-scale, 1));
background: var(--color-background);
@@ -4949,6 +4955,11 @@ html[data-chat-font] .aui-root {
max-width: calc(100% + 1.25rem * var(--ui-space-scale, 1));
}
+ .aui-thread-root [dir="rtl"] > [data-streamdown="list-item"] > [data-streamdown="code-block"] {
+ margin-left: 0;
+ margin-right: calc(-1.25rem * var(--ui-space-scale, 1));
+ }
+
.dark .aui-thread-root [data-streamdown="code-block"] {
/* Streamdown `pre` sets its own dark bg; keep one surface on the outer shell. */
--shiki-dark-bg: transparent;
diff --git a/studio/frontend/tests/rehype-auto-direction.test.ts b/studio/frontend/tests/rehype-auto-direction.test.ts
new file mode 100644
index 00000000000..971088bc201
--- /dev/null
+++ b/studio/frontend/tests/rehype-auto-direction.test.ts
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { Streamdown } from "streamdown";
+
+import { rehypeAutoDirection } from "../src/components/assistant-ui/rehype-auto-direction.ts";
+import { withDataImageSupport } from "../src/lib/markdown-data-images.ts";
+
+function render(markdown: string): string {
+ return renderToStaticMarkup(
+ createElement(Streamdown, {
+ mode: "static",
+ children: markdown,
+ rehypePlugins: withDataImageSupport({}, [rehypeAutoDirection]),
+ }),
+ );
+}
+
+test("text blocks get dir=auto through the chat's sanitize and harden pipeline", () => {
+ const html = render(
+ "## سرفصل\n\nبرای Unsloth راست به چپ\n\n| ستون |\n| --- |\n| خانه |",
+ );
+ for (const tag of ["h2", "p", "th", "td"]) {
+ assert.match(html, new RegExp(`<${tag}[^>]* dir="auto"`), tag);
+ }
+});
+
+test("lists and quotes flip only when their text starts right to left", () => {
+ const rtl = render(
+ "1. **مورد** اول\n2. دوم\n\n- مورد\n\n با پاراگراف\n\n> نقل",
+ );
+ for (const tag of ["ol", "ul", "blockquote"]) {
+ assert.match(rtl, new RegExp(`<${tag}[^>]* dir="rtl"`), tag);
+ }
+ const ltr = render("- `کد` item\n\n> quote سلام");
+ assert.match(ltr, /<ul[^>]* dir="ltr"/);
+ assert.match(ltr, /<blockquote[^>]* dir="ltr"/);
+});
+
+test("an English list nested in an RTL list stays left to right", () => {
+ const html = render("- سلام\n - English item.");
+ assert.match(html, /<ul[^>]* dir="rtl"[\s\S]*<ul[^>]* dir="ltr"/);
+});
+
+test("a tight item's text gets its own direction, the item keeps the list's", () => {
+ const html = render("- سلام\n- English item.\n - nested");
+ assert.match(html, /<ul[^>]* dir="rtl"/);
+ assert.match(html, /<li[^>]*><span dir="auto">English item\.\s*<\/span>/);
+ assert.doesNotMatch(html, /<li[^>]* dir=/);
+});
+
+test("code is pinned LTR in the stylesheet", () => {
+ const css = readFileSync(
+ new URL("../src/index.css", import.meta.url),
+ "utf8",
+ );
+ assert.match(
+ css,
+ /\[data-streamdown="code-block"\], \[data-streamdown="inline-code"\][^{]*\{\s*direction: ltr;\s*unicode-bidi: isolate;/,
+ );
+});
+
+test("code keeps its own direction", () => {
+ const html = render("```python\nprint('سلام')\n```\n\nمتن `code` متن");
+ assert.doesNotMatch(html, /<(pre|code)[^>]* dir=/);
+});