🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(evals): polish editor search panel (#16886)
Tobias Wochinger5cdbf3b0f7d8
diff --git a/web/src/components/editor/CodeMirrorEditor.tsx b/web/src/components/editor/CodeMirrorEditor.tsxindex 7c7c294880..3d659d0328 100644--- a/web/src/components/editor/CodeMirrorEditor.tsx+++ b/web/src/components/editor/CodeMirrorEditor.tsx@@ -14,7 +14,7 @@ import {StateEffect,StateField,} from "@codemirror/state";-import { SearchQuery, search, setSearchQuery } from "@codemirror/search";+import { SearchQuery, setSearchQuery } from "@codemirror/search";import { json, jsonParseLinter } from "@codemirror/lang-json";import { linter, type Diagnostic } from "@codemirror/lint";import { useTheme } from "next-themes";@@ -37,6 +37,7 @@ import { lightTheme } from "@/src/components/editor/light-theme";import { darkTheme } from "@/src/components/editor/dark-theme";import { autoScrollOnSelectionDrag } from "@/src/components/editor/autoScrollOnSelectionDrag";import { createJsonMagicPasteExtension } from "@/src/components/editor/jsonMagicPaste";+import { codeMirrorSearchPanel } from "@/src/constants/codeMirrorSearchPanel";// Custom language mode for prompts that highlights mustache variables and prompt dependency tagsconst promptLanguage = StreamLanguage.define({@@ -478,7 +479,7 @@ export function CodeMirrorEditor({// mouseup capture listeners registering on every drag....(editable ? [autoScrollOnSelectionDrag()] : []),searchHighlightingSupport,- search(),+ codeMirrorSearchPanel,// RTL/bidi support - must be early for proper line decoration...bidiSupport,// Remove outline if field is focusseddiff --git a/web/src/constants/codeMirrorSearchPanel.ts b/web/src/constants/codeMirrorSearchPanel.tsnew file mode 100644index 0000000000..0cbe1083b2--- /dev/null+++ b/web/src/constants/codeMirrorSearchPanel.ts@@ -0,0 +1,170 @@+import { search } from "@codemirror/search";+import type { Extension } from "@codemirror/state";+import { EditorView, ViewPlugin } from "@uiw/react-codemirror";++const KEYBOARD_NAVIGATION_CLASS = "cm-keyboard-navigation";++const keyboardNavigation = ViewPlugin.fromClass(+ class {+ constructor(readonly view: EditorView) {+ view.dom.addEventListener("keydown", this.handleKeyDown, true);+ view.dom.addEventListener("mousedown", this.handleMouseDown, true);+ }++ private readonly handleKeyDown = () => {+ this.view.dom.classList.add(KEYBOARD_NAVIGATION_CLASS);+ };++ private readonly handleMouseDown = () => {+ this.view.dom.classList.remove(KEYBOARD_NAVIGATION_CLASS);+ };++ destroy() {+ this.view.dom.removeEventListener("keydown", this.handleKeyDown, true);+ this.view.dom.removeEventListener(+ "mousedown",+ this.handleMouseDown,+ true,+ );+ }+ },+);++const searchPanelTheme = EditorView.theme({+ ".cm-panels-top": {+ borderBottom: "1px solid hsl(var(--border))",+ },+ ".cm-panel.cm-search": {+ position: "relative",+ display: "flex",+ flexWrap: "wrap",+ alignItems: "center",+ gap: "4px",+ padding: "6px 40px 6px 8px",+ backgroundColor: "hsl(var(--secondary))",+ color: "hsl(var(--secondary-foreground))",+ fontFamily: "var(--font-sans)",+ fontSize: "var(--text-sm)",+ },+ ".cm-panel.cm-search br": {+ display: "none",+ },+ ".cm-panel.cm-search input[name=search], .cm-panel.cm-search input[name=replace]":+ {+ boxSizing: "border-box",+ width: "min(220px, 100%)",+ height: "32px",+ margin: "0",+ border: "1px solid hsl(var(--input))",+ borderRadius: "6px",+ backgroundColor: "hsl(var(--background))",+ color: "hsl(var(--foreground))",+ padding: "4px 8px",+ fontFamily: "var(--font-sans)",+ fontSize: "var(--text-sm)",+ outline: "none",+ },+ ".cm-panel.cm-search input[name=search]:focus, .cm-panel.cm-search input[name=replace]:focus":+ {+ borderColor: "hsl(var(--ring))",+ },+ ".cm-panel.cm-search button": {+ boxSizing: "border-box",+ height: "32px",+ margin: "0",+ border: "1px solid hsl(var(--border-contrast))",+ borderRadius: "6px",+ backgroundColor: "hsl(var(--background))",+ backgroundImage: "none",+ color: "hsl(var(--foreground))",+ padding: "0 12px",+ fontFamily: "var(--font-sans)",+ fontSize: "var(--text-sm)",+ lineHeight: "30px",+ textTransform: "capitalize",+ cursor: "pointer",+ },+ ".cm-panel.cm-search button:hover": {+ backgroundColor: "hsl(var(--accent))",+ color: "hsl(var(--accent-foreground))",+ },+ ".cm-panel.cm-search button:focus-visible": {+ outline: "2px solid hsl(var(--ring))",+ outlineOffset: "2px",+ },+ ".cm-panel.cm-search button:disabled": {+ cursor: "not-allowed",+ opacity: "0.5",+ },+ ".cm-panel.cm-search label": {+ display: "inline-flex",+ height: "32px",+ alignItems: "center",+ gap: "4px",+ margin: "0",+ borderRadius: "6px",+ color: "hsl(var(--muted-foreground))",+ padding: "0 4px",+ fontFamily: "var(--font-sans)",+ fontSize: "var(--text-xs)",+ whiteSpace: "nowrap",+ cursor: "pointer",+ },+ ".cm-panel.cm-search label:hover": {+ backgroundColor: "hsl(var(--accent))",+ color: "hsl(var(--accent-foreground))",+ },+ ".cm-panel.cm-search input[type=checkbox]": {+ boxSizing: "border-box",+ display: "inline-flex",+ width: "16px",+ height: "16px",+ flexShrink: "0",+ alignItems: "center",+ justifyContent: "center",+ margin: "0",+ appearance: "none",+ border: "1px solid hsl(var(--control-border))",+ borderRadius: "4px",+ backgroundColor: "transparent",+ outline: "none",+ cursor: "pointer",+ },+ "&.cm-keyboard-navigation .cm-panel.cm-search input[type=checkbox]:focus": {+ outline: "2px solid hsl(var(--ring))",+ outlineOffset: "2px",+ },+ ".cm-panel.cm-search input[type=checkbox]:checked": {+ borderColor: "hsl(var(--control-fill))",+ backgroundColor: "hsl(var(--control-fill))",+ },+ ".cm-panel.cm-search input[type=checkbox]:checked::after": {+ width: "12px",+ height: "12px",+ backgroundColor: "hsl(var(--primary-foreground))",+ content: "''",+ maskImage:+ "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M20 6 9 17l-5-5' fill='none' stroke='black' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E\")",+ maskPosition: "center",+ maskRepeat: "no-repeat",+ maskSize: "contain",+ },+ ".cm-panel.cm-search button[name=close]": {+ position: "absolute",+ top: "6px",+ right: "8px",+ width: "32px",+ borderColor: "transparent",+ backgroundColor: "transparent",+ padding: "0",+ fontSize: "16px",+ lineHeight: "30px",+ },+});++/** Shared top-mounted search panel for prompt and code editors. */+export const codeMirrorSearchPanel: Extension = [+ search({ top: true }),+ keyboardNavigation,+ searchPanelTheme,+];diff --git a/web/src/features/evals/components/code-eval-template-form-body.clienttest.tsx b/web/src/features/evals/components/code-eval-template-form-body.clienttest.tsxindex e34d409344..3394fb860d 100644--- a/web/src/features/evals/components/code-eval-template-form-body.clienttest.tsx+++ b/web/src/features/evals/components/code-eval-template-form-body.clienttest.tsx@@ -40,6 +40,32 @@ describe("CodeEvalTemplateFormBody", () => {vi.clearAllMocks();});+ it("opens the search panel above the code editor", async () => {+ const { container } = render(+ <CodeEvalTemplateFormBody+ sourceCode="return JSON.stringify(ctx)"+ sourceCodeLanguage="TYPESCRIPT"+ onSourceCodeChange={vi.fn()}+ editable+ validationResult={null}+ ctxSample={null}+ />,+ { wrapper: TestTooltipProvider },+ );++ const editorContent = container.querySelector<HTMLElement>(".cm-content");+ expect(editorContent).not.toBeNull();+ fireEvent.keyDown(editorContent!, {+ key: "f",+ code: "KeyF",+ ctrlKey: true,+ });++ await waitFor(() => {+ expect(container.querySelector(".cm-panels-top .cm-search")).toBeTruthy();+ });+ });+it("blocks evaluator source from PostHog session recordings", () => {const { container } = render(<CodeEvalTemplateFormBodydiff --git a/web/src/features/evals/components/code-eval-template-form-body.tsx b/web/src/features/evals/components/code-eval-template-form-body.tsxindex d91eac5297..bdf1f77a7c 100644--- a/web/src/features/evals/components/code-eval-template-form-body.tsx+++ b/web/src/features/evals/components/code-eval-template-form-body.tsx@@ -33,6 +33,7 @@ import {import { darkTheme } from "@/src/components/editor/dark-theme";import { lightTheme } from "@/src/components/editor/light-theme";import { autoScrollOnSelectionDrag } from "@/src/components/editor/autoScrollOnSelectionDrag";+import { codeMirrorSearchPanel } from "@/src/constants/codeMirrorSearchPanel";import { showErrorToast } from "@/src/features/notifications/showErrorToast";import {getCodeEvalHoverDocs,@@ -376,6 +377,7 @@ export function CodeEvalTemplateFormBody({: []),EditorView.lineWrapping,codeMirrorLayoutTheme,+ codeMirrorSearchPanel,],[codeEvalCompletionExtension,diff --git a/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.clienttest.tsx b/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.clienttest.tsxindex 46bedf035f..b8fad3f01d 100644--- a/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.clienttest.tsx+++ b/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.clienttest.tsx@@ -1,8 +1,59 @@-import { render } from "@testing-library/react";+import { fireEvent, render, waitFor } from "@testing-library/react";import { PromptVariableEditor } from "./PromptVariableEditor";describe("PromptVariableEditor", () => {+ it("opens the search panel above the prompt editor", async () => {+ const { container } = render(+ <PromptVariableEditor+ value="Return only valid JSON"+ onChange={vi.fn()}+ />,+ );++ const editorContent = container.querySelector<HTMLElement>(".cm-content");+ expect(editorContent).not.toBeNull();+ fireEvent.keyDown(editorContent!, {+ key: "f",+ code: "KeyF",+ ctrlKey: true,+ });++ await waitFor(() => {+ expect(container.querySelector(".cm-panels-top .cm-search")).toBeTruthy();+ });+ });++ it("uses a checkbox focus affordance only during keyboard navigation", async () => {+ const { container } = render(+ <PromptVariableEditor value="Search me" onChange={vi.fn()} />,+ );++ const editorContent = container.querySelector<HTMLElement>(".cm-content");+ const editor = container.querySelector<HTMLElement>(".cm-editor");+ expect(editorContent).not.toBeNull();+ expect(editor).not.toBeNull();++ fireEvent.keyDown(editorContent!, {+ key: "f",+ code: "KeyF",+ ctrlKey: true,+ });+ const checkbox = await waitFor(() => {+ const element = container.querySelector<HTMLInputElement>(+ '.cm-search input[type="checkbox"]',+ );+ expect(element).not.toBeNull();+ return element!;+ });++ expect(editor).toHaveClass("cm-keyboard-navigation");+ fireEvent.mouseDown(checkbox);+ expect(editor).not.toHaveClass("cm-keyboard-navigation");+ fireEvent.keyDown(checkbox, { key: "Tab", code: "Tab" });+ expect(editor).toHaveClass("cm-keyboard-navigation");+ });+it("keeps the interpolated preview aligned and theme-aware", () => {const { container } = render(<PromptVariableEditordiff --git a/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.stories.tsx b/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.stories.tsxindex 65d7f87c24..f5864aa2b6 100644--- a/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.stories.tsx+++ b/web/src/features/evals/v2/components/Evaluators/Judges/PromptVariableEditor/PromptVariableEditor.stories.tsx@@ -1,5 +1,5 @@import { ChevronDown, MoreVertical } from "lucide-react";-import { fn } from "storybook/test";+import { expect, fn, userEvent, within } from "storybook/test";import { Badge } from "@/src/components/ui/badge";import { Button } from "@/src/components/ui/button";@@ -190,3 +190,38 @@ export const ReadOnly = meta.story({readOnly: true,},});++export const SearchPanel = meta.story({+ name: "(Test) Search Panel",+ args: {+ value:+ "Return only valid JSON. Do not add markdown or an explanation around the JSON object.",+ onChange: fn(),+ showPreviewToggle: true,+ previewEnabled: false,+ onPreviewEnabledChange: fn(),+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);+ const editor = canvasElement.querySelector<HTMLElement>(".cm-content");+ if (!editor) throw new Error("Prompt editor not found");++ await userEvent.click(editor);+ const isMac = /Mac|iPhone|iPad/.test(navigator.platform);+ editor.dispatchEvent(+ new KeyboardEvent("keydown", {+ key: "f",+ code: "KeyF",+ metaKey: isMac,+ ctrlKey: !isMac,+ bubbles: true,+ }),+ );++ const findInput = await canvas.findByRole("textbox", { name: "Find" });+ await userEvent.type(findInput, "JSON");+ await expect(+ canvasElement.querySelector(".cm-panels-top .cm-search"),+ ).toBeInTheDocument();+ },+});