Skip to content

Commit d5e635a

Browse files
feat(agent/version): surface per-version usage metrics in agent detail view to improve visibility.
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 871eb5c commit d5e635a

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use client'
2+
3+
import { useQuery } from '@tanstack/react-query'
4+
import { Badge } from '@/components/ui/badge'
5+
import { Skeleton } from '@/components/ui/skeleton'
6+
7+
interface VersionUsageBadgeProps {
8+
publisherId: string
9+
agentId: string
10+
version: string
11+
}
12+
13+
interface AgentData {
14+
id: string
15+
publisher: {
16+
id: string
17+
}
18+
version_stats?: Record<
19+
string,
20+
{
21+
total_invocations: number
22+
}
23+
>
24+
}
25+
26+
const formatUsageCount = (count?: number) => {
27+
if (!count) return '0'
28+
if (count >= 1000000) return `${(count / 1000000).toFixed(1)}M`
29+
if (count >= 1000) return `${(count / 1000).toFixed(1)}K`
30+
return count.toString()
31+
}
32+
33+
export const VersionUsageBadge = ({
34+
publisherId,
35+
agentId,
36+
version,
37+
}: VersionUsageBadgeProps) => {
38+
const { data: agents, isLoading } = useQuery<AgentData[]>({
39+
queryKey: ['agents'],
40+
queryFn: async () => {
41+
const response = await fetch('/api/agents')
42+
if (!response.ok) {
43+
throw new Error('Failed to fetch agents')
44+
}
45+
return await response.json()
46+
},
47+
})
48+
49+
const agent = agents?.find(
50+
(agent) => agent.id === agentId && agent.publisher.id === publisherId
51+
)
52+
53+
const totalRuns = agent?.version_stats?.[version]?.total_invocations || 0
54+
55+
if (isLoading) {
56+
return <Skeleton className="h-4 w-8" />
57+
}
58+
59+
if (totalRuns === 0) {
60+
return null
61+
}
62+
63+
return (
64+
<Badge variant="secondary" className="text-xs px-1.5 py-0 ml-2">
65+
{formatUsageCount(totalRuns)} runs
66+
</Badge>
67+
)
68+
}

0 commit comments

Comments
 (0)