Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(editors): support RTL languages, also in prompts (#10993)

Nimarfc879d108e24
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 8e13d1afc9..36f464fc4d 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -59,7 +59,7 @@ importers:
next-auth:
specifier: ^4.24.12
zod:
specifier: ^3.25.62
version: 3.25.62
@@ -248,7 +248,7 @@ importers:
version: 4.1.1
next-auth:
specifier: ^4.24.12
nodemailer:
specifier: ^7.0.11
version: 7.0.11
@@ -358,6 +358,9 @@ importers:
'@codemirror/lint':
specifier: ^6.9.2
version: 6.9.2
+ '@codemirror/state':
+ specifier: ^6.5.2
+ version: 6.5.2
'@dnd-kit/core':
specifier: ^6.1.0
@@ -657,7 +660,7 @@ importers:
next-auth:
specifier: ^4.24.12
next-query-params:
specifier: ^5.1.0
@@ -18155,7 +18158,7 @@ snapshots:
dependencies:
dependencies:
@@ -22222,7 +22225,7 @@ snapshots:
eslint-import-resolver-typescript: 3.6.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])([email protected])
eslint-plugin-eslint-comments: 3.2.0([email protected])
- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])
eslint-plugin-jsx-a11y: 6.8.0([email protected])
@@ -24464,7 +24467,7 @@ snapshots:
dependencies:
- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])
dependencies:
@@ -24495,7 +24498,7 @@ snapshots:
enhanced-resolve: 5.17.1
eslint: 8.57.0
- eslint-plugin-import: 2.29.1(@typescript-eslint/[email protected]([email protected])([email protected]))([email protected])
fast-glob: 3.3.3
get-tsconfig: 4.10.1
is-core-module: 2.15.1
@@ -24528,16 +24531,6 @@ snapshots:
transitivePeerDependencies:
- supports-color
- dependencies:
- debug: 3.2.7
- optionalDependencies:
- '@typescript-eslint/parser': 8.48.1([email protected])([email protected])
- eslint: 8.57.0
- eslint-import-resolver-node: 0.3.9
- transitivePeerDependencies:
- - supports-color
-
dependencies:
'@eslint-community/eslint-utils': 4.9.0([email protected])
@@ -24551,7 +24544,7 @@ snapshots:
eslint: 8.57.0
ignore: 5.3.2
dependencies:
array-includes: 3.1.7
array.prototype.findlastindex: 1.2.4
@@ -24561,7 +24554,7 @@ snapshots:
doctrine: 2.1.0
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
hasown: 2.0.2
is-core-module: 2.15.1
is-glob: 4.0.3
@@ -24572,7 +24565,7 @@ snapshots:
semver: 6.3.1
tsconfig-paths: 3.15.0
optionalDependencies:
- '@typescript-eslint/parser': 8.48.1([email protected])([email protected])
+ '@typescript-eslint/parser': 7.12.0([email protected])([email protected])
transitivePeerDependencies:
- eslint-import-resolver-typescript
- eslint-import-resolver-webpack
@@ -24999,7 +24992,7 @@ snapshots:
dependencies:
- debug: 4.3.4
+ debug: 4.4.3
get-stream: 5.2.0
yauzl: 2.10.0
optionalDependencies:
@@ -27843,7 +27836,7 @@ snapshots:
dependencies:
type-fest: 2.19.0
dependencies:
'@babel/runtime': 7.28.4
'@panva/hkdf': 1.2.1
diff --git a/web/package.json b/web/package.json
index cd3000a796..5277d0b0b9 100644
--- a/web/package.json
+++ b/web/package.json
@@ -29,6 +29,7 @@
"@codemirror/lang-json": "^6.0.2",
"@codemirror/language": "^6.11.3",
"@codemirror/lint": "^6.9.2",
+ "@codemirror/state": "^6.5.2",
"@dnd-kit/core": "^6.1.0",
"@dnd-kit/modifiers": "^7.0.0",
"@dnd-kit/sortable": "^8.0.0",
diff --git a/web/src/components/editor/CodeMirrorEditor.tsx b/web/src/components/editor/CodeMirrorEditor.tsx
index 89b4c70836..78e3903037 100644
--- a/web/src/components/editor/CodeMirrorEditor.tsx
+++ b/web/src/components/editor/CodeMirrorEditor.tsx
@@ -1,7 +1,12 @@
import CodeMirror, {
EditorView,
type ReactCodeMirrorRef,
+ Decoration,
+ type DecorationSet,
+ ViewPlugin,
+ type ViewUpdate,
} from "@uiw/react-codemirror";
+import { RangeSetBuilder } from "@codemirror/state";
import { json, jsonParseLinter } from "@codemirror/lang-json";
import { linter, type Diagnostic } from "@codemirror/lint";
import { useTheme } from "next-themes";
@@ -122,6 +127,38 @@ const promptLinter = linter((view) => {
// Create a language support instance that combines the language and its configuration
const promptSupport = new LanguageSupport(promptLanguage);
+// RTL/bidirectional text support
+const dirAutoDecoration = Decoration.line({ attributes: { dir: "auto" } });
+
+const bidiSupport = [
+ EditorView.perLineTextDirection.of(true),
+ ViewPlugin.fromClass(
+ class {
+ decorations: DecorationSet;
+ constructor(view: EditorView) {
+ this.decorations = this.build(view);
+ }
+ update(update: ViewUpdate) {
+ if (update.docChanged || update.viewportChanged) {
+ this.decorations = this.build(update.view);
+ }
+ }
+ build(view: EditorView) {
+ const builder = new RangeSetBuilder<Decoration>();
+ for (const { from, to } of view.visibleRanges) {
+ for (let pos = from; pos <= to; ) {
+ const line = view.state.doc.lineAt(pos);
+ builder.add(line.from, line.from, dirAutoDecoration);
+ pos = line.to + 1;
+ }
+ }
+ return builder.finish();
+ }
+ },
+ { decorations: (v) => v.decorations },
+ ),
+];
+
export function CodeMirrorEditor({
value,
onChange,
@@ -167,6 +204,8 @@ export function CodeMirrorEditor({
}}
lang={mode === "json" ? "json" : undefined}
extensions={[
+ // RTL/bidi support - must be early for proper line decoration
+ ...bidiSupport,
// Remove outline if field is focussed
EditorView.theme({
"&.cm-focused": {
diff --git a/web/src/components/ui/CodeJsonViewer.tsx b/web/src/components/ui/CodeJsonViewer.tsx
index f05d7e6516..43990047b5 100644
--- a/web/src/components/ui/CodeJsonViewer.tsx
+++ b/web/src/components/ui/CodeJsonViewer.tsx
@@ -102,7 +102,11 @@ export function JSONView(props: {
{props.isLoading ? (
<Skeleton className="h-3 w-3/4" />
) : props.projectIdForPromptButtons ? (
- <code className="whitespace-pre-wrap break-words">
+ <code
+ className="whitespace-pre-wrap break-words"
+ dir="auto"
+ style={{ unicodeBidi: "plaintext" }}
+ >
{renderRichPromptContent(
props.projectIdForPromptButtons,
String(parsedJson),
@@ -274,10 +278,12 @@ export function CodeView(props: {
)}
<code
className={cn(
- "relative flex-1 whitespace-pre-wrap break-all px-4 py-3 font-mono text-xs",
+ "relative flex-1 whitespace-pre-wrap break-words px-4 py-3 font-mono text-xs",
isCollapsed ? `line-clamp-6` : "block",
props.scrollable ? "overflow-y-auto" : "",
)}
+ dir="auto"
+ style={{ unicodeBidi: "plaintext" }}
>
{props.content}
</code>
diff --git a/web/src/features/prompts/components/prompt-content-utils.tsx b/web/src/features/prompts/components/prompt-content-utils.tsx
index 29855e198e..a1b880b37d 100644
--- a/web/src/features/prompts/components/prompt-content-utils.tsx
+++ b/web/src/features/prompts/components/prompt-content-utils.tsx
@@ -12,6 +12,8 @@ import { FileCode } from "lucide-react";
const PromptVar = ({ name, isValid }: { name: string; isValid: boolean }) => (
<span
+ dir="ltr"
+ style={{ unicodeBidi: "isolate" }}
className={cn(
isValid ? "text-primary-accent" : "text-destructive",
"whitespace-nowrap",
@@ -46,6 +48,7 @@ const PromptReference = ({
variant="outline"
size="sm"
className="inline-flex items-center gap-1.5 rounded-sm border-dashed bg-muted/50 px-2 py-0.5 align-[-3px] text-xs font-medium transition-colors hover:bg-muted"
+ dir="ltr"
onClick={() => window.open(getPromptUrl(projectId, promptRef), "_blank")}
title={`Open prompt: ${promptRef.name}${promptRef.type === "version" ? ` (v${promptRef.version})` : promptRef.label ? ` (${promptRef.label})` : ""}`}
>
@@ -75,6 +78,17 @@ export const renderRichPromptContent = (
): React.ReactNode[] => {
if (!content) return [];
+ const createTextNode = (text: string, key: string) => (
+ <span
+ key={key}
+ dir="auto"
+ style={{ unicodeBidi: "plaintext" }}
+ className="whitespace-pre-wrap break-words"
+ >
+ {text}
+ </span>
+ );
+
const parts: React.ReactNode[] = [];
let lastIndex = 0;
@@ -93,7 +107,9 @@ export const renderRichPromptContent = (
// Add any text before this match
if (index > lastIndex) {
const textBefore = content.substring(lastIndex, index);
- if (textBefore) parts.push(textBefore);
+ if (textBefore) {
+ parts.push(createTextNode(textBefore, `text-${lastIndex}-${index}`));
+ }
}
// Determine type based on which capture group matched and process directly
@@ -131,7 +147,7 @@ export const renderRichPromptContent = (
</React.Fragment>,
);
} else {
- parts.push(fullMatch);
+ parts.push(createTextNode(fullMatch, `raw-${index}`));
}
} else if (match[2] !== undefined) {
// Second capture group = variable
@@ -150,7 +166,9 @@ export const renderRichPromptContent = (
// Add any remaining text
if (lastIndex < content.length) {
const remainingText = content.substring(lastIndex);
- if (remainingText) parts.push(remainingText);
+ if (remainingText) {
+ parts.push(createTextNode(remainingText, `text-${lastIndex}-end`));
+ }
}
return parts;