🪢 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(web): "+N" score chip opens a table of all scores (#17644)
Trang Nguyenf25723d99a6e
diff --git a/web/src/components/GroupedScoreBadges.stories.tsx b/web/src/components/GroupedScoreBadges.stories.tsxindex f4c4d928df..98cb1fed0d 100644--- a/web/src/components/GroupedScoreBadges.stories.tsx+++ b/web/src/components/GroupedScoreBadges.stories.tsx@@ -1,5 +1,5 @@import { type LastUserScore } from "@langfuse/shared";-import { expect } from "storybook/test";+import { expect, userEvent, waitFor, within } from "storybook/test";import preview from "../../.storybook/preview";import { GroupedScoreBadges } from "./grouped-score-badge";@@ -47,6 +47,7 @@ export const Compact = meta.story({});export const WithOverflow = meta.story({+ name: "(Test) With Overflow",args: {scores: [...scores,@@ -59,6 +60,21 @@ export const WithOverflow = meta.story({],maxVisible: 2,},+ play: async ({ canvasElement }) => {+ const body = within(canvasElement.ownerDocument.body);++ await userEvent.click(+ body.getByRole("button", { name: "Show all 3 scores" }),+ );++ await waitFor(async () => {+ await expect(+ canvasElement.ownerDocument.body.querySelectorAll(+ '[role="dialog"][data-state="open"] li',+ ),+ ).toHaveLength(3);+ });+ },});export const DetailsInsideBadge = meta.story({diff --git a/web/src/components/grouped-score-badge.tsx b/web/src/components/grouped-score-badge.tsxindex c94439f1d9..60656da755 100644--- a/web/src/components/grouped-score-badge.tsx+++ b/web/src/components/grouped-score-badge.tsx@@ -1,24 +1,25 @@-import { useState } from "react";import { BadgeShell } from "@/src/components/design-system/Badge/Badge";import {- HoverCard,- HoverCardContent,- HoverCardTrigger,-} from "@/src/components/ui/hover-card";+ Popover,+ PopoverContent,+ PopoverTrigger,+} from "@/src/components/ui/popover";import { cn } from "@/src/utils/tailwind";import { type LastUserScore, type ScoreDomain } from "@langfuse/shared";import { type WithStringifiedMetadata } from "@/src/utils/clientSideDomainTypes";import { scoreLevelFromScore } from "@/src/components/score-tag";import { ScoreBadge } from "@/src/components/ScoreBadge/ScoreBadge";+type ChipScore = WithStringifiedMetadata<ScoreDomain> | LastUserScore;++const MAX_VISIBLE_SCORE_GROUPS = 2;+/*** Bucket scores by name, the way the badges group them. Exported so a caller that* has to RESERVE room for these badges buckets them identically — two copies of* the grouping rule are two chances to price a chip that never renders.*/-const groupScoresByName = <- T extends WithStringifiedMetadata<ScoreDomain> | LastUserScore,->(+const groupScoresByName = <T extends ChipScore>(scores: T[],): Record<string, T[]> =>scores.reduce<Record<string, T[]>>((groups, score) => {@@ -28,65 +29,66 @@ const groupScoresByName = <return groups;}, {});-const MAX_VISIBLE_SCORE_GROUPS = 3;--const partitionScores = <- T extends WithStringifiedMetadata<ScoreDomain> | LastUserScore,->(+const partitionScores = <T extends ChipScore>(scores: Record<string, T[]>,maxVisible: number,) => {const sortedScores = Object.entries(scores).sort(([a], [b]) =>a < b ? -1 : 1,);- const visibleScores = sortedScores.slice(0, maxVisible);- const hiddenScores = sortedScores.slice(maxVisible);- return { visibleScores, hiddenScores };+ return {+ visibleScores: sortedScores.slice(0, maxVisible),+ hiddenScores: sortedScores.slice(maxVisible),+ };+};++const formatScoreValue = (score: ChipScore) =>+ score.stringValue ?? score.value?.toFixed(2) ?? "";++const ScoreTable = <T extends ChipScore>({ scores }: { scores: T[] }) => {+ const groups = Object.entries(groupScoresByName(scores)).sort(([a], [b]) =>+ a.localeCompare(b),+ );++ return (+ <div className="p-2 text-xs">+ <div className="text-foreground mb-1 font-bold">Scores</div>+ <ul className="grid grid-cols-[auto_auto] gap-x-4 gap-y-1">+ {groups.map(([name, groupScores]) => (+ <li key={name} className="contents">+ <span className="text-muted-foreground whitespace-nowrap">+ {name}+ </span>+ <span className="text-foreground whitespace-nowrap">+ {groupScores.map(formatScoreValue).join(", ")}+ </span>+ </li>+ ))}+ </ul>+ </div>+ );};-export const GroupedScoreBadges = <- T extends WithStringifiedMetadata<ScoreDomain> | LastUserScore,->({+export const GroupedScoreBadges = <T extends ChipScore>({scores,maxVisible = MAX_VISIBLE_SCORE_GROUPS,compact,- expandable = true,}: {scores: T[];maxVisible?: number;compact?: boolean;- /**- * Whether "+N" expands the hidden chips IN PLACE. A caller that has measured a- * box for exactly `maxVisible` chips has to say no: expanding is unbounded by- * construction, so inside a clipping box it does not reveal the hidden scores,- * it cuts the visible ones. The hover preview stays either way, which is the- * part that actually shows them.- */- expandable?: boolean;}) => {const groupedScores = groupScoresByName(scores);- // Level tags only when this selection MIXES levels (LFE-10596): a row whose- // scores all share one level (the common case — e.g. a span's own- // observation-level scores) needs no per-chip disambiguation; a mixed row- // tags each group so the levels are tellable apart.+ // Level tags only when this selection MIXES levels: a row whose scores all+ // share one level needs no per-chip disambiguation; a mixed row (e.g. the+ // root carrying trace-level and observation-level scores) tags each group.const showLevels =new Set(scores.map((score) => scoreLevelFromScore(score))).size > 1;- // "+N" expands IN PLACE on click (hover still previews the hidden chips);- // the trailing "−" collapses back to the capped view.- const [expanded, setExpanded] = useState(false);- const overflows = Object.keys(groupedScores).length > maxVisible;-const { visibleScores, hiddenScores } = partitionScores(groupedScores,- expanded && expandable ? Number.POSITIVE_INFINITY : maxVisible,- );-- const overflowButtonClassName = cn(- expandable ? "cursor-pointer" : "cursor-default",- compact ? "px-0.5 py-0 leading-tight" : "px-1",- "text-xs font-bold",+ maxVisible,);return (@@ -101,8 +103,8 @@ export const GroupedScoreBadges = </>))}{Boolean(hiddenScores.length) && (- <HoverCard>- <HoverCardTrigger asChild>+ <Popover>+ <PopoverTrigger asChild><BadgeShellasChildcolor="neutral"@@ -110,55 +112,26 @@ export const GroupedScoreBadges = <><buttontype="button"- className={overflowButtonClassName}- // aria-label, not title: a native tooltip would stack on top of- // the hover-card preview.- aria-label={`Show ${hiddenScores.length} more score${hiddenScores.length === 1 ? "" : "s"}`}- // Chips render inside clickable rows (tree nodes, table rows) —- // expanding must not also select/navigate the row. Still swallowed- // when expansion is off, or the row would react to a click aimed at- // the preview.- onClick={(event) => {- event.stopPropagation();- if (expandable) setExpanded(true);- }}+ className={cn(+ "cursor-pointer self-center text-xs font-bold",+ compact ? "px-0.5 py-0 leading-tight" : "px-1",+ )}+ aria-label={`Show all ${Object.keys(groupedScores).length} scores`}+ // Chips render inside clickable rows; opening must not select the row.+ onClick={(event) => event.stopPropagation()}>+{hiddenScores.length}</button></BadgeShell>- </HoverCardTrigger>- {/* w-max overrides the fixed w-64 base so the card adapts to its- chips; the cap makes long selections wrap instead of clipping. */}- <HoverCardContent className="max-h-[300px] w-max max-w-[min(420px,90vw)] overflow-y-auto p-2">- <div className="flex flex-wrap gap-1">- {hiddenScores.map(([name, scores]) => (- <ScoreBadge- key={name}- name={name}- scores={scores}- compact={compact}- showLevels={showLevels}- />- ))}- </div>- </HoverCardContent>- </HoverCard>- )}- {expanded && overflows && (- <BadgeShell asChild color="neutral" size={compact ? "sm" : "default"}>- <button- type="button"- className={overflowButtonClassName}- title="Show fewer scores"- aria-label="Show fewer scores"- onClick={(event) => {- event.stopPropagation();- setExpanded(false);- }}+ </PopoverTrigger>+ <PopoverContent+ align="start"+ className="max-h-[320px] w-max max-w-[min(560px,90vw)] overflow-y-auto p-0"+ onClick={(event) => event.stopPropagation()}>- −- </button>- </BadgeShell>+ <ScoreTable scores={scores} />+ </PopoverContent>+ </Popover>)}</>);diff --git a/web/src/features/traces/components/SpanContent.tsx b/web/src/features/traces/components/SpanContent.tsxindex d3887a2dac..8080bf05d3 100644--- a/web/src/features/traces/components/SpanContent.tsx+++ b/web/src/features/traces/components/SpanContent.tsx@@ -33,10 +33,6 @@ import { useTraceData } from "@/src/features/traces/contexts/TraceDataContext";import { selectNodeScores } from "@/src/features/traces/fns/nodeScores";import type Decimal from "decimal.js";-// How many distinct score groups to show inline on a tree/search row before-// collapsing the rest into a "+N" pill. Keeps dense-score rows compact; the-// full set is always on the node's Scores tab. (The timeline caps at 3.)-interface SpanContentProps {node: TreeNode;parentTotalCost?: Decimal;@@ -216,9 +212,8 @@ export function SpanContent({</div>)}- {/* Scores row. Cap the inline badges and roll the rest into a "+N"- pill (hover to see them) so a node with many scores stays a compact- one/two-line row instead of a tall wrapping grid. */}+ {/* Scores row. Inline badges are capped; the rest roll into a "+N"+ pill that opens a table of all scores. */}{showScores && nodeScores.length > 0 && (<div className="flex flex-wrap gap-1"><GroupedScoreBadges compact scores={nodeScores} />