🪢 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): improve code evaluator formatting (#13897)
Tobias Wochingerd393d6bea76c
diff --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 6ede6fa8bf..452f49b252 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@@ -2,9 +2,8 @@ import CodeMirror, {EditorView,ExternalChange,hoverTooltip,- keymap,} from "@uiw/react-codemirror";-import { EditorState } from "@codemirror/state";+import { EditorState, Prec } from "@codemirror/state";import { linter, type Diagnostic } from "@codemirror/lint";import { StreamLanguage, type StringStream } from "@codemirror/language";import { python } from "@codemirror/lang-python";@@ -28,6 +27,7 @@ import {type CodeEvalHoverDocs,} from "@/src/features/evals/utils/code-eval-template-hover-docs";import {+ formatPythonCodeEvalSourceWithRuff,TYPESCRIPT_CODE_EVAL_CONTRACT,type CodeEvalSourceCodeLanguage,type CodeEvalValidationResult,@@ -54,7 +54,6 @@ type ContractRanges = {const PYTHON_EVALUATE_SIGNATURE_PATTERN =/(?:^|\n)def evaluate\s*\(\s*ctx\s*:\s*EvaluationContext\s*\)\s*->\s*EvaluationResult\s*:/;-const FORMAT_SHORTCUT_KEY = "Shift-Alt-f";const FORMAT_SHORTCUT_ARIA = "Alt+Shift+F";const TYPESCRIPT_KEYWORDS = new Set(["async",@@ -259,6 +258,19 @@ async function formatTypeScriptSource(source: string) {return `${source.slice(0, ranges.prelude.to)}\n${formattedEditableSource.trimStart()}`;}+async function formatPythonSource(source: string) {+ const ranges = findPythonContractRanges(source);+ if (!ranges?.prelude) {+ return formatPythonCodeEvalSourceWithRuff(source);+ }++ const formattedEditableSource = await formatPythonCodeEvalSourceWithRuff(+ source.slice(ranges.prelude.to),+ );++ return `${source.slice(0, ranges.prelude.to).trimEnd()}\n\n\n${formattedEditableSource.trimStart()}`;+}+function scrollCodeMirrorToBottom(view: EditorView) {if (typeof window === "undefined") return;@@ -287,9 +299,7 @@ export function CodeEvalTemplateFormBody({sourceCodeLanguage === EvalTemplateSourceCodeLanguage.PYTHON? "Python": "TypeScript";- const canFormatSource =- sourceCodeLanguage === EvalTemplateSourceCodeLanguage.TYPESCRIPT;- const shouldShowFormatButton = editable && canFormatSource;+ const shouldShowFormatButton = editable;const handleCreateEditor = useCallback((view: EditorView) => {codeMirrorViewRef.current = view;@@ -309,18 +319,27 @@ export function CodeEvalTemplateFormBody({);const formatSource = useCallback(async () => {- if (!editable || isFormatting || !canFormatSource) return;+ if (!editable || isFormatting) return;setIsFormatting(true);try {- const formatted = await formatTypeScriptSource(sourceCode);+ const formatted =+ sourceCodeLanguage === EvalTemplateSourceCodeLanguage.PYTHON+ ? await formatPythonSource(sourceCode)+ : await formatTypeScriptSource(sourceCode);onSourceCodeChange(formatted);} catch (error) {console.error(error);} finally {setIsFormatting(false);}- }, [editable, isFormatting, canFormatSource, onSourceCodeChange, sourceCode]);+ }, [+ editable,+ isFormatting,+ onSourceCodeChange,+ sourceCode,+ sourceCodeLanguage,+ ]);const linterExtension = useMemo(() =>@@ -338,15 +357,27 @@ export function CodeEvalTemplateFormBody({);const formatShortcutExtension = useMemo(() =>- keymap.of([- {- key: FORMAT_SHORTCUT_KEY,- run: () => {- void formatSource();- return true;+ Prec.highest(+ EditorView.domEventHandlers({+ keydown: (event) => {+ // CodeMirror keymaps intentionally don't bind macOS Option combos+ // that type special characters, so match the physical F key here.+ if (+ event.code === "KeyF" &&+ event.shiftKey &&+ event.altKey &&+ !event.ctrlKey &&+ !event.metaKey+ ) {+ event.preventDefault();+ void formatSource();+ return true;+ }++ return false;},- },- ]),+ }),+ ),[formatSource],);const languageExtension = useMemo(