Skip to content

Commit c6a6fb6

Browse files
committed
Refactor context size warning logic to include the percentage over the threshold and update UI components to display token utilization details, including hover titles and formatted token counts
1 parent 4dbb297 commit c6a6fb6

4 files changed

Lines changed: 41 additions & 16 deletions

File tree

‎packages/shared/src/constants/dictionary.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ export const dictionary = {
8282
TYPE_SOMETHING_TO_USE_GROUP: 'Type something to use this group.',
8383
GROUP_HAS_NO_SELECTED_PRESETS:
8484
'The chosen group has no selected presets to run.',
85-
CONTEXT_SIZE_WARNING: (threshold: number) =>
86-
`Context is now larger than set threshold of ${threshold} tokens. Unreasonably large context affects quality of responses, usage limits/costs.`,
85+
CONTEXT_SIZE_WARNING: (threshold: string, percentage: number) =>
86+
`Context is now larger than set threshold of ${threshold} tokens by ${percentage}%. Unreasonably large context may degrade response quality, hit allowed usage limit faster or increase costs.`,
8787
FAILED_TO_CREATE_FILE: (file_path: string) =>
8888
`Failed to create file: ${file_path}`,
8989
FAILED_TO_APPLY_CHANGES_TO_FILE: (file_path: string) =>

‎packages/ui/src/components/editor/panel/ContextUtilisation/ContextUtilisation.module.scss‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
display: flex;
33
align-items: center;
44
gap: var(--padding-8px);
5-
padding: 0 13px;
5+
padding: 0 var(--padding-10px);
66
color: var(--vscode-descriptionForeground);
77
}
88

‎packages/ui/src/components/editor/panel/ContextUtilisation/ContextUtilisation.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,19 @@ export const ContextUtilisation: React.FC<Props> = (props) => {
3131
? `⚠ ${formatted_current_size}`
3232
: formatted_current_size
3333

34+
let title_text = ''
3435
const context_text = is_above_threshold
3536
? `${display_current_size} tokens in context`
3637
: `${display_current_size}/${formatted_threshold} tokens in context`
3738

39+
if (!is_above_threshold) {
40+
const remaining_tokens =
41+
props.context_size_warning_threshold - props.current_context_size
42+
title_text = `${remaining_tokens} tokens remaining until threshold (${formatted_threshold})`
43+
} else {
44+
title_text = `Context size (${formatted_current_size}) exceeds threshold (${formatted_threshold})`
45+
}
46+
3847
return (
3948
<div className={styles.container}>
4049
<div className={styles.bar}>
@@ -45,7 +54,9 @@ export const ContextUtilisation: React.FC<Props> = (props) => {
4554
style={{ width: `${progress}%` }}
4655
/>
4756
</div>
48-
<span className={styles.label}>{context_text}</span>
57+
<span className={styles.label} title={title_text}>
58+
{context_text}
59+
</span>
4960
</div>
5061
)
5162
}

‎packages/vscode/src/context/context-initialization.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,20 @@ import { ContextProvider } from './providers/context-provider'
1313

1414
export const token_count_emitter = new EventEmitter()
1515

16-
export function context_initialization(context: vscode.ExtensionContext): {
16+
const round_token_count_for_badge = (count: number): number => {
17+
if (count < 1000) {
18+
return count
19+
}
20+
return Math.floor(count / 1000) * 1000
21+
}
22+
23+
export const context_initialization = (
24+
context: vscode.ExtensionContext
25+
): {
1726
workspace_provider: WorkspaceProvider
1827
open_editors_provider: OpenEditorsProvider
1928
websites_provider: WebsitesProvider
20-
} {
29+
} => {
2130
let was_above_threshold = false
2231

2332
const workspace_folders = vscode.workspace.workspaceFolders ?? []
@@ -48,24 +57,19 @@ export function context_initialization(context: vscode.ExtensionContext): {
4857
if (context_provider && context_view) {
4958
context_token_count =
5059
await workspace_provider.get_checked_files_token_count()
51-
context_view.badge = {
52-
value: context_token_count,
60+
workspace_view.badge = {
61+
value: round_token_count_for_badge(context_token_count),
5362
tooltip: context_token_count
5463
? `About ${context_token_count} tokens in context`
5564
: ''
5665
}
66+
context_view.badge = undefined
5767
}
5868

5969
let websites_token_count = 0
60-
if (websites_provider && websites_view) {
70+
if (websites_provider) {
6171
websites_token_count =
6272
websites_provider.get_checked_websites_token_count()
63-
websites_view.badge = {
64-
value: websites_token_count,
65-
tooltip: websites_token_count
66-
? `About ${websites_token_count} tokens in context`
67-
: ''
68-
}
6973
}
7074
token_count_emitter.emit('token-count-updated')
7175

@@ -76,8 +80,18 @@ export function context_initialization(context: vscode.ExtensionContext): {
7680
if (threshold && threshold > 0) {
7781
const is_above_threshold = total_token_count > threshold
7882
if (is_above_threshold && !was_above_threshold) {
83+
const percentage_over = Math.round(
84+
((total_token_count - threshold) / threshold) * 100
85+
)
86+
const formatted_threshold =
87+
threshold >= 1000
88+
? `${Math.round(threshold / 1000)}k`
89+
: `${threshold}`
7990
vscode.window.showWarningMessage(
80-
dictionary.warning_message.CONTEXT_SIZE_WARNING(threshold)
91+
dictionary.warning_message.CONTEXT_SIZE_WARNING(
92+
formatted_threshold,
93+
percentage_over
94+
)
8195
)
8296
}
8397
was_above_threshold = is_above_threshold

0 commit comments

Comments
 (0)