Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/llm-badges-click-ui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hyperdx/app': patch
---

feat: use the click-ui `Badge` for the model, provider, tool, agent, and chat
role badges in the LLM span panel, session drawer, and dashboard.
44 changes: 44 additions & 0 deletions packages/app/src/llm/__tests__/ChatMessageItem.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { screen } from '@testing-library/react';

import { ChatMessageItem } from '@/llm/components/ChatMessageItem';

// click-ui encodes state only in hashed CSS module class names, so the mock
// exposes the props as attributes to assert on.
jest.mock('@clickhouse/click-ui', () => ({
...jest.requireActual('@clickhouse/click-ui'),
Badge: ({
text,
state,
'data-testid': testId,
}: {
text: React.ReactNode;
state?: string;
'data-testid'?: string;
}) => (
<span data-testid={testId} data-state={state}>
{text}
</span>
),
}));

describe('ChatMessageItem', () => {
it.each([
['system', 'neutral'],
['developer', 'neutral'],
['user', 'info'],
['Assistant', 'success'],
['tool', 'warning'],
['function', 'warning'],
['other', 'neutral'],
])('renders the %s role with the %s state', (role, badgeState) => {
renderWithMantine(
<ChatMessageItem
message={{ id: 0, source: 'input', role, content: null }}
/>,
);

const badge = screen.getByTestId('llm-chat-message-role');
expect(badge).toHaveTextContent(role.toLowerCase());
expect(badge).toHaveAttribute('data-state', badgeState);
});
});
53 changes: 53 additions & 0 deletions packages/app/src/llm/__tests__/LLMSpanSubpanel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { screen } from '@testing-library/react';

import { LLMSpanSubpanel } from '@/llm/components/LLMSpanSubpanel';

// click-ui encodes state only in hashed CSS module class names, so the mock
// exposes the props as attributes to assert on.
jest.mock('@clickhouse/click-ui', () => ({
...jest.requireActual('@clickhouse/click-ui'),
Badge: ({ text, state }: { text: React.ReactNode; state?: string }) => (
<span data-testid="badge" data-state={state}>
{text}
</span>
),
}));

describe('LLMSpanSubpanel', () => {
it('renders a badge with a distinct state for each span attribute', () => {
renderWithMantine(
<LLMSpanSubpanel
info={{
model: 'claude-fable-5',
provider: 'anthropic',
operation: 'chat',
toolName: 'get_weather',
agentName: 'planner',
usage: {},
params: {},
}}
/>,
);

const badges = screen.getAllByTestId('badge');
expect(
badges.map(b => [b.textContent, b.getAttribute('data-state')]),
).toEqual([
['claude-fable-5', 'info'],
['anthropic', 'neutral'],
['chat', null],
['get_weather', 'warning'],
['planner', 'success'],
]);
});

it('omits badges for missing attributes', () => {
renderWithMantine(
<LLMSpanSubpanel
info={{ model: 'claude-fable-5', usage: {}, params: {} }}
/>,
);

expect(screen.getAllByTestId('badge')).toHaveLength(1);
});
});
31 changes: 15 additions & 16 deletions packages/app/src/llm/components/ChatMessageItem.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useState } from 'react';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import { Badge, Box, Code, Group, Paper, Text } from '@mantine/core';
import { Badge, BadgeProps } from '@clickhouse/click-ui';
import { Box, Code, Group, Paper, Text } from '@mantine/core';
import {
IconChevronDown,
IconChevronRight,
Expand All @@ -10,15 +11,15 @@ import {

import { ChatToolCall, ConversationMessage } from '@/llm/lib/types';

// Categorical role → badge color. Chat roles are categories, not statuses,
// so plain Mantine palette colors are appropriate here.
const ROLE_COLORS: Record<string, string> = {
system: 'gray',
developer: 'gray',
user: 'blue',
assistant: 'green',
tool: 'orange',
function: 'orange',
// Chat roles are categories, not statuses. click-ui only has semantic badge
// states, so these states are for color only and do not signal a problem.
const ROLE_BADGE_STATES: Record<string, BadgeProps['state']> = {
system: 'neutral',
developer: 'neutral',
user: 'info',
assistant: 'success',
tool: 'warning',
function: 'warning',
};

function tryPrettyJson(value: string): string {
Expand Down Expand Up @@ -76,13 +77,11 @@ export function ChatMessageItem({ message }: { message: ConversationMessage }) {
<Paper withBorder p="sm" radius="md" data-testid="llm-chat-message">
<Group gap="xs" mb={message.content != null ? 6 : 0}>
<Badge
size="xs"
variant="light"
color={ROLE_COLORS[role] ?? 'gray'}
size="sm"
text={role}
state={ROLE_BADGE_STATES[role] ?? 'neutral'}
data-testid="llm-chat-message-role"
>
{role}
</Badge>
/>
{message.name != null && (
<Text size="xs" c="dimmed" ff="monospace">
{message.name}
Expand Down
27 changes: 7 additions & 20 deletions packages/app/src/llm/components/LLMSpanSubpanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react';
import { Badge, Group, Stack, Table, Text } from '@mantine/core';
import { Badge } from '@clickhouse/click-ui';
import { Group, Stack, Table, Text } from '@mantine/core';
Comment thread
brandon-pereira marked this conversation as resolved.
Resolved

import { IS_LLM_COST_ENABLED } from '@/config';
import { resolveSpanCostUsd } from '@/llm/lib/cost';
Expand All @@ -26,30 +27,16 @@ export function LLMSpanSubpanel({ info }: { info: LLMSpanInfo }) {
return (
<Stack gap="sm" data-testid="llm-span-subpanel">
<Group gap="xs" wrap="wrap">
{info.model != null && (
<Badge size="sm" variant="light" color="violet" tt="none">
{info.model}
</Badge>
)}
{info.model != null && <Badge text={info.model} state="info" />}
{info.provider != null && (
<Badge size="sm" variant="light" color="gray" tt="none">
{info.provider}
</Badge>
)}
{info.operation != null && (
<Badge size="sm" variant="light" color="blue" tt="none">
{info.operation}
</Badge>
<Badge text={info.provider} state="neutral" />
)}
{info.operation != null && <Badge text={info.operation} />}
{info.toolName != null && (
<Badge size="sm" variant="light" color="orange" tt="none">
{info.toolName}
</Badge>
<Badge text={info.toolName} state="warning" />
)}
{info.agentName != null && (
<Badge size="sm" variant="light" color="teal" tt="none">
{info.agentName}
</Badge>
<Badge text={info.agentName} state="success" />
)}
</Group>

Expand Down
6 changes: 2 additions & 4 deletions packages/app/src/llm/dashboard/LLMDashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
useQueryStates,
} from 'nuqs';
import { useForm, useWatch } from 'react-hook-form';
import { Badge } from '@clickhouse/click-ui';
import { tcFromSource } from '@hyperdx/common-utils/dist/core/metadata';
import { SourceKind } from '@hyperdx/common-utils/dist/types';
import {
Anchor,
Badge,
Box,
Breadcrumbs,
Button,
Expand Down Expand Up @@ -222,9 +222,7 @@ function LLMDashboardPage() {
<Text fz="sm" c="dimmed">
LLM
</Text>
<Badge size="xs" color="blue" variant="light">
Beta
</Badge>
<Badge size="sm" text="Beta" state="info" />
<HoverCard
width={420}
shadow="md"
Expand Down
10 changes: 3 additions & 7 deletions packages/app/src/llm/dashboard/LLMSessionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { useCallback, useMemo, useState } from 'react';
import Link from 'next/link';
import { parseAsString, useQueryState } from 'nuqs';
import SqlString from 'sqlstring';
import { Badge } from '@clickhouse/click-ui';
import {
Accordion,
Anchor,
Badge,
Button,
Drawer,
Group,
Expand Down Expand Up @@ -75,14 +75,10 @@ function SessionSpanItem({
{row.spanName}
</Text>
{row.model != null && (
<Badge size="xs" variant="light" color="violet" tt="none">
{row.model}
</Badge>
<Badge size="sm" text={row.model} state="info" />
)}
{row.toolName != null && (
<Badge size="xs" variant="light" color="orange" tt="none">
{row.toolName}
</Badge>
<Badge size="sm" text={row.toolName} state="warning" />
)}
{row.totalTokens != null && row.totalTokens > 0 && (
<Text size="xs" c="dimmed" style={{ whiteSpace: 'nowrap' }}>
Expand Down
Loading