Skip to content

Commit 4b12568

Browse files
committed
Context window display
1 parent 96eb075 commit 4b12568

3 files changed

Lines changed: 87 additions & 2 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use client'
2+
3+
import { useMemo } from 'react'
4+
import { Tooltip } from '@/components/emcn'
5+
6+
interface ContextUsageIndicatorProps {
7+
/** Usage percentage (0-100) */
8+
percentage: number
9+
/** Size of the indicator in pixels */
10+
size?: number
11+
/** Stroke width in pixels */
12+
strokeWidth?: number
13+
}
14+
15+
/**
16+
* Circular context usage indicator showing percentage of context window used.
17+
* Displays a progress ring that changes color based on usage level.
18+
*
19+
* @param props - Component props
20+
* @returns Rendered context usage indicator
21+
*/
22+
export function ContextUsageIndicator({
23+
percentage,
24+
size = 20,
25+
strokeWidth = 2,
26+
}: ContextUsageIndicatorProps) {
27+
const radius = (size - strokeWidth) / 2
28+
const circumference = radius * 2 * Math.PI
29+
const offset = circumference - (percentage / 100) * circumference
30+
31+
const color = useMemo(() => {
32+
if (percentage >= 90) return '#dc2626'
33+
if (percentage >= 75) return '#d97706'
34+
return '#6b7280'
35+
}, [percentage])
36+
37+
const displayPercentage = useMemo(() => {
38+
return Math.round(percentage)
39+
}, [percentage])
40+
41+
return (
42+
<Tooltip.Root delayDuration={100}>
43+
<Tooltip.Trigger asChild>
44+
<div
45+
className='flex cursor-pointer items-center justify-center transition-opacity hover:opacity-80'
46+
style={{ width: size, height: size }}
47+
>
48+
<svg width={size} height={size} className='rotate-[-90deg]'>
49+
<circle
50+
cx={size / 2}
51+
cy={size / 2}
52+
r={radius}
53+
stroke='currentColor'
54+
strokeWidth={strokeWidth}
55+
fill='none'
56+
className='text-muted-foreground/20'
57+
/>
58+
<circle
59+
cx={size / 2}
60+
cy={size / 2}
61+
r={radius}
62+
stroke={color}
63+
strokeWidth={strokeWidth}
64+
fill='none'
65+
strokeDasharray={circumference}
66+
strokeDashoffset={offset}
67+
className='transition-all duration-300 ease-in-out'
68+
strokeLinecap='round'
69+
/>
70+
</svg>
71+
</div>
72+
</Tooltip.Trigger>
73+
<Tooltip.Content side='top'>
74+
{displayPercentage}% context used
75+
</Tooltip.Content>
76+
</Tooltip.Root>
77+
)
78+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { AttachedFilesDisplay } from './attached-files-display/attached-files-display'
22
export { ContextPills } from './context-pills/context-pills'
3+
export { ContextUsageIndicator } from './context-usage-indicator/context-usage-indicator'
34
export { MentionMenu } from './mention-menu/mention-menu'
45
export { ModeSelector } from './mode-selector/mode-selector'
56
export { ModelSelector } from './model-selector/model-selector'

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel-new/components/copilot/components/user-input/user-input.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type { ChatContext } from '@/stores/panel-new/copilot/types'
2121
import {
2222
AttachedFilesDisplay,
2323
ContextPills,
24+
ContextUsageIndicator,
2425
MentionMenu,
2526
ModelSelector,
2627
ModeSelector,
@@ -97,7 +98,7 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
9798
const workspaceId = params.workspaceId as string
9899

99100
// Store hooks
100-
const { workflowId, selectedModel, setSelectedModel } = useCopilotStore()
101+
const { workflowId, selectedModel, setSelectedModel, contextUsage } = useCopilotStore()
101102

102103
// Internal state
103104
const [internalMessage, setInternalMessage] = useState('')
@@ -581,7 +582,7 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
581582
onDragOver={fileAttachments.handleDragOver}
582583
onDrop={fileAttachments.handleDrop}
583584
>
584-
{/* Top Row: @ Button + Context Pills + Context Usage Pill */}
585+
{/* Top Row: @ Button + Context Usage + Context Pills */}
585586
<div className='mb-[6px] flex flex-wrap items-center gap-[6px]'>
586587
<Badge
587588
variant='outline'
@@ -595,6 +596,11 @@ const UserInput = forwardRef<UserInputRef, UserInputProps>(
595596
<AtSign className='h-3 w-3' strokeWidth={1.75} />
596597
</Badge>
597598

599+
{/* Context Usage Indicator */}
600+
{contextUsage && contextUsage.percentage > 0 && (
601+
<ContextUsageIndicator percentage={contextUsage.percentage} size={18} strokeWidth={2.5} />
602+
)}
603+
598604
{/* Selected Context Pills */}
599605
<ContextPills
600606
contexts={contextManagement.selectedContexts}

0 commit comments

Comments
 (0)