🪢 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
feat(corrections): support diff viewer between actual vs. corrected output (#11556)
marliessophie5d319e1fe353
diff --git a/web/src/components/trace2/components/IOPreview/components/CorrectedOutputDiffDialog.tsx b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputDiffDialog.tsxnew file mode 100644index 0000000000..88aaf33aeb--- /dev/null+++ b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputDiffDialog.tsx@@ -0,0 +1,111 @@+import React from "react";+import { Button } from "@/src/components/ui/button";+import {+ Dialog,+ DialogBody,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/src/components/ui/dialog";+import DiffViewer from "@/src/components/DiffViewer";++type CorrectedOutputDiffDialogProps = {+ isOpen: boolean;+ setIsOpen: (open: boolean) => void;+ actualOutput?: unknown;+ correctedOutput: string;+ strictJsonMode: boolean;+};++/**+ * Formats output for diff display+ * @param output - The output to format+ * @param strictJsonMode - Whether to enforce JSON formatting+ * @returns Formatted string for display+ */+const formatOutputForDiff = (+ output: unknown,+ strictJsonMode: boolean,+): string => {+ if (output === null || output === undefined) {+ return "";+ }++ // If strict JSON mode, try to format as JSON+ if (strictJsonMode) {+ try {+ // If it's already a string, try to parse it first+ if (typeof output === "string") {+ const parsed = JSON.parse(output);+ return JSON.stringify(parsed, null, 2);+ }+ // Otherwise just stringify the object+ return JSON.stringify(output, null, 2);+ } catch {+ // If JSON formatting fails, fall back to string representation+ return typeof output === "string" ? output : JSON.stringify(output);+ }+ }++ // Non-strict mode: convert to string+ return typeof output === "string" ? output : JSON.stringify(output, null, 2);+};++export const CorrectedOutputDiffDialog: React.FC<+ CorrectedOutputDiffDialogProps+> = ({ isOpen, setIsOpen, actualOutput, correctedOutput, strictJsonMode }) => {+ // Format both outputs for comparison+ const formattedActualOutput = formatOutputForDiff(+ actualOutput,+ strictJsonMode,+ );+ const formattedCorrectedOutput = formatOutputForDiff(+ correctedOutput,+ strictJsonMode,+ );++ // Check if there's no original output to compare+ const hasNoOriginalOutput =+ actualOutput === null || actualOutput === undefined;++ return (+ <Dialog open={isOpen} onOpenChange={setIsOpen}>+ <DialogContent size="xl">+ <DialogHeader>+ <DialogTitle>Output Correction Diff</DialogTitle>+ <DialogDescription>+ Compare the original output with the corrected version+ </DialogDescription>+ </DialogHeader>++ <DialogBody>+ {hasNoOriginalOutput ? (+ <div className="flex flex-col items-center justify-center p-8 text-center">+ <div className="text-muted-foreground">+ <p className="text-lg font-medium">No original output</p>+ <p className="mt-2 text-sm">+ There is no original output to compare with the correction.+ </p>+ </div>+ </div>+ ) : (+ <div className="space-y-4">+ <DiffViewer+ oldString={formattedActualOutput}+ newString={formattedCorrectedOutput}+ oldLabel="Original Output"+ newLabel="Corrected Output"+ />+ </div>+ )}+ </DialogBody>++ <DialogFooter>+ <Button onClick={() => setIsOpen(false)}>Close</Button>+ </DialogFooter>+ </DialogContent>+ </Dialog>+ );+};diff --git a/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsxindex 4dc492fc74..f0532adf26 100644--- a/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx+++ b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx@@ -1,15 +1,16 @@-import { Pencil, Trash } from "lucide-react";+import { Pencil, Trash, FileDiff } from "lucide-react";import { cn } from "@/src/utils/tailwind";import { Button } from "@/src/components/ui/button";import { type ScoreDomain } from "@langfuse/shared";import { useCorrectionData } from "./hooks/useCorrectionData";import { useCorrectionMutations } from "./hooks/useCorrectionMutations";import { useCorrectionEditor } from "./hooks/useCorrectionEditor";-import { useMemo } from "react";+import { useMemo, useState } from "react";import { CodeMirrorEditor } from "@/src/components/editor/CodeMirrorEditor";import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";import { Switch } from "@/src/components/ui/switch";import useLocalStorage from "@/src/components/useLocalStorage";+import { CorrectedOutputDiffDialog } from "./CorrectedOutputDiffDialog";interface CorrectedOutputFieldProps {projectId: string;@@ -36,6 +37,9 @@ export function CorrectedOutputField({false,);+ // Diff dialog state+ const [isDiffDialogOpen, setIsDiffDialogOpen] = useState(false);+// Merge cache + server dataconst { effectiveCorrection, correctionValue } = useCorrectionData(existingCorrection,@@ -54,16 +58,26 @@ export function CorrectedOutputField({});// Manage editor state & debouncing- const { isEditing, value, isValidJson, handleEdit, handleChange } =- useCorrectionEditor({- correctionValue,- actualOutput,- onSave: handleSave,- setSaveStatus,- strictJsonMode,- });+ const {+ isEditing,+ setIsEditing,+ value,+ isValidJson,+ handleEdit,+ handleChange,+ } = useCorrectionEditor({+ correctionValue,+ actualOutput,+ onSave: handleSave,+ setSaveStatus,+ strictJsonMode,+ });- const hasContent = value.trim().length > 0;+ // When not editing, use correctionValue (source of truth from cache/server)+ // When editing, use value (local state)+ const hasContent = isEditing+ ? value.trim().length > 0+ : correctionValue.trim().length > 0;// Format JSON for displayconst displayValue = useMemo(() => {@@ -81,101 +95,118 @@ export function CorrectedOutputField({handleChange(newValue);};+ const handleDeleteWithExitEdit = () => {+ handleDelete();+ setIsEditing(false);+ };+return (- <div className="px-2">- <div className="group relative rounded-md">- <div className="flex items-center justify-between py-1.5">- <span className="text-sm font-medium">Corrected Output (Beta)</span>- <div className="-mr-1 flex items-center">- <div className="flex items-center -space-x-1 opacity-0 transition-opacity group-hover:opacity-100">- {!isValidJson && isEditing && hasContent && (- <span className="mr-2 text-xs text-red-500">- {strictJsonMode- ? "Invalid JSON - fix to save"- : "Cannot save empty content"}- </span>- )}- {isValidJson &&- saveStatus === "idle" &&- isEditing &&- hasContent && (- <span className="mr-2 text-xs text-muted-foreground">- DRAFT - Type any character to save+ <>+ <CorrectedOutputDiffDialog+ isOpen={isDiffDialogOpen}+ setIsOpen={setIsDiffDialogOpen}+ actualOutput={actualOutput}+ correctedOutput={value}+ strictJsonMode={strictJsonMode}+ />+ <div className="px-2">+ <div className="group relative rounded-md">+ <div className="flex items-center justify-between py-1.5">+ <span className="text-sm font-medium">Corrected Output (Beta)</span>+ <div className="-mr-1 flex items-center">+ <div className="flex items-center -space-x-1 opacity-0 transition-opacity group-hover:opacity-100">+ {!isValidJson && isEditing && hasContent && (+ <span className="mr-2 text-xs text-red-500">+ {strictJsonMode+ ? "Invalid JSON - fix to save"+ : "Cannot save empty content"}</span>)}- {isValidJson && saveStatus === "saving" && (- <span className="mr-2 w-fit text-xs text-muted-foreground">- Saving...- </span>- )}- {isValidJson && saveStatus === "saved" && (- <span className="mr-2 w-fit text-xs">Saved ✓</span>- )}- {hasContent && !isEditing && (- <>- <Button- size="icon-xs"- variant="ghost"- onClick={handleEdit}- disabled={!hasAccess}- className="hover:bg-border"- title="Edit corrected output"- >- <Pencil className="h-3 w-3" />- </Button>- <Button- size="icon-xs"- variant="ghost"- onClick={handleDelete}- disabled={!hasAccess}- className="hover:bg-border"- title="Delete corrected output"- >- <Trash className="h-3 w-3" />- </Button>- </>- )}- </div>- <div className="flex items-center">- <Switch- checked={strictJsonMode}- onCheckedChange={setStrictJsonMode}- className="scale-75"- />- <span className="text-xs text-muted-foreground">JSON</span>+ {isValidJson && saveStatus === "saving" && (+ <span className="mr-2 w-fit text-xs text-muted-foreground">+ Saving...+ </span>+ )}+ {isValidJson && saveStatus === "saved" && (+ <span className="mr-2 w-fit text-xs">Saved ✓</span>+ )}+ {hasContent && (+ <>+ <Button+ size="icon-xs"+ variant="ghost"+ onClick={() => setIsDiffDialogOpen(true)}+ className="hover:bg-border"+ title={"View diff between original and corrected output"}+ >+ <FileDiff className="h-3 w-3" />+ </Button>+ {!isEditing && (+ <Button+ size="icon-xs"+ variant="ghost"+ onClick={handleEdit}+ disabled={!hasAccess}+ className="hover:bg-border"+ title="Edit corrected output"+ >+ <Pencil className="h-3 w-3" />+ </Button>+ )}+ <Button+ size="icon-xs"+ variant="ghost"+ onClick={handleDeleteWithExitEdit}+ disabled={!hasAccess}+ className="hover:bg-border"+ title="Delete corrected output"+ >+ <Trash className="h-3 w-3" />+ </Button>+ </>+ )}+ </div>+ <div className="flex items-center">+ <Switch+ checked={strictJsonMode}+ onCheckedChange={setStrictJsonMode}+ className="scale-75"+ />+ <span className="text-xs text-muted-foreground">JSON</span>+ </div></div></div>- </div>- {!hasContent && !isEditing ? (- <button- onClick={handleEdit}- disabled={!hasAccess}- className={cn(- "w-full cursor-pointer rounded-md border px-3 py-4 text-center text-xs text-muted-foreground transition-colors hover:bg-muted/50",- )}- >- Click to add corrected output- </button>- ) : isEditing ? (- <CodeMirrorEditor- value={displayValue}- onChange={handleEditorChange}- mode={strictJsonMode ? "json" : "text"}- minHeight={200}- placeholder="Enter corrected output..."- className="bg-accent-light-green"- />- ) : (- <CodeMirrorEditor- value={displayValue}- mode={strictJsonMode ? "json" : "text"}- minHeight={200}- editable={false}- className="bg-accent-light-green"- />- )}+ {!hasContent && !isEditing ? (+ <button+ onClick={handleEdit}+ disabled={!hasAccess}+ className={cn(+ "w-full cursor-pointer rounded-md border px-3 py-4 text-center text-xs text-muted-foreground transition-colors hover:bg-muted/50",+ )}+ >+ Click to add corrected output+ </button>+ ) : isEditing ? (+ <CodeMirrorEditor+ value={displayValue}+ onChange={handleEditorChange}+ mode={strictJsonMode ? "json" : "text"}+ minHeight={200}+ placeholder="Enter corrected output..."+ className="bg-accent-light-green"+ />+ ) : (+ <CodeMirrorEditor+ value={displayValue}+ mode={strictJsonMode ? "json" : "text"}+ minHeight={200}+ editable={false}+ className="bg-accent-light-green"+ />+ )}+ </div></div>- </div>+ </>);}diff --git a/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts b/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.tsindex eed803a6d1..e0e8d40335 100644--- a/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts+++ b/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts@@ -62,13 +62,42 @@ export function useCorrectionEditor({? actualOutput: JSON.stringify(actualOutput);setValue(jsonValue);++ // Validate the prefilled value+ let valid = false;+ if (strictJsonMode) {+ try {+ if (jsonValue.trim()) {+ JSON.parse(jsonValue);+ valid = true;+ }+ } catch {+ valid = false;+ }+ } else {+ valid = jsonValue.trim().length > 0;+ }+ setIsValidJson(valid);++ // Auto-save prefilled value if valid+ if (valid) {+ setSaveStatus("saving");+ onSave(jsonValue);+ }} catch {- setValue(String(actualOutput));+ const stringValue = String(actualOutput);+ setValue(stringValue);+ const valid = stringValue.trim().length > 0;+ setIsValidJson(valid);+ if (valid) {+ setSaveStatus("saving");+ onSave(stringValue);+ }}}// Focus textarea after it renderssetTimeout(() => textareaRef.current?.focus(), 0);- }, [value, actualOutput]);+ }, [value, actualOutput, strictJsonMode, onSave, setSaveStatus]);const handleChange = useCallback((newValue: string) => {@@ -119,6 +148,7 @@ export function useCorrectionEditor({return {isEditing,+ setIsEditing,value,isValidJson,handleEdit,