Skip to content

Commit a45eec9

Browse files
committed
Implement context utilization display logic and UI component to show token count relative to a configurable warning threshold
1 parent c82b54b commit a45eec9

13 files changed

Lines changed: 162 additions & 10 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@
6060

6161
.textarea {
6262
padding: var(--padding);
63-
font-family: inherit;
64-
font-size: inherit;
65-
line-height: inherit;
6663
background-color: transparent;
67-
color: var(--vscode-input-foreground);
6864
border: none;
6965
resize: none;
7066
position: relative;
@@ -113,7 +109,7 @@
113109
display: flex;
114110
bottom: 0;
115111
right: 0;
116-
padding: var(--padding-6px) var(--padding-8px) var(--padding-6px)
112+
padding: var(--padding-6px) var(--padding-10px) var(--padding-6px)
117113
var(--padding-6px);
118114
justify-content: space-between;
119115
overflow: hidden;
@@ -150,7 +146,7 @@
150146
flex-shrink: 0;
151147
display: flex;
152148
align-items: center;
153-
gap: var(--padding-8px);
149+
gap: var(--padding-10px);
154150
pointer-events: none;
155151

156152
&__edit-format {
@@ -199,7 +195,6 @@
199195
height: 20px;
200196
padding: 0 var(--padding-2px);
201197
border-radius: var(--border-radius-5px);
202-
color: var(--vscode-foreground);
203198
pointer-events: all;
204199

205200
> svg {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
.container {
2+
display: flex;
3+
align-items: center;
4+
gap: var(--padding-8px);
5+
padding: 0 var(--padding-10px);
6+
font-size: var(--font-size-11px);
7+
color: var(--vscode-descriptionForeground);
8+
}
9+
10+
.label {
11+
white-space: nowrap;
12+
}
13+
14+
.bar {
15+
width: 100%;
16+
height: 3px;
17+
background-color: var(--vscode-dropdown-border);
18+
border-radius: var(--border-radius-999px);
19+
overflow: hidden;
20+
21+
&__progress {
22+
height: 100%;
23+
background-color: var(--vscode-focusBorder);
24+
border-top-right-radius: var(--border-radius-999px);
25+
border-bottom-right-radius: var(--border-radius-999px);
26+
27+
&--warning {
28+
background-color: var(--vscode-editorWarning-foreground);
29+
}
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ContextUtilisation } from './ContextUtilisation'
2+
3+
export default {
4+
component: ContextUtilisation
5+
}
6+
7+
export const BelowThreshold = () => (
8+
<ContextUtilisation
9+
current_context_size={5000}
10+
context_size_warning_treshold={100000}
11+
/>
12+
)
13+
14+
export const AboveThreshold = () => (
15+
<ContextUtilisation
16+
current_context_size={120000}
17+
context_size_warning_treshold={100000}
18+
/>
19+
)
20+
21+
export const AtThreshold = () => (
22+
<ContextUtilisation
23+
current_context_size={100000}
24+
context_size_warning_treshold={100000}
25+
/>
26+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { FC } from 'react'
2+
import styles from './ContextUtilisation.module.scss'
3+
import cn from 'classnames'
4+
5+
type Props = {
6+
current_context_size: number
7+
context_size_warning_treshold: number
8+
}
9+
10+
const format_number = (num: number): string => {
11+
if (num < 1000) {
12+
return num.toLocaleString()
13+
}
14+
return `${Math.floor(num / 1000)}k`
15+
}
16+
17+
export const ContextUtilisation: FC<Props> = (props) => {
18+
const { current_context_size, context_size_warning_treshold } = props
19+
const is_above_threshold =
20+
current_context_size > context_size_warning_treshold
21+
const progress = Math.min(
22+
(current_context_size / context_size_warning_treshold) * 100,
23+
100
24+
)
25+
26+
return (
27+
<div className={styles.container}>
28+
<span className={styles.label}>
29+
{format_number(current_context_size)}
30+
</span>
31+
<div className={styles.bar}>
32+
<div
33+
className={cn(styles.bar__progress, {
34+
[styles['bar__progress--warning']]: is_above_threshold
35+
})}
36+
style={{ width: `${progress}%` }}
37+
/>
38+
</div>
39+
<span className={styles.label}>
40+
{format_number(context_size_warning_treshold)}
41+
</span>
42+
</div>
43+
)
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './ContextUtilisation'

‎packages/ui/src/styles/global.scss‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
body {
1212
padding: 0;
1313
font-size: var(--font-size-13px);
14+
color: var(--vscode-foreground);
1415
user-select: none;
1516
line-height: 1.4;
1617
}
@@ -25,6 +26,13 @@ button {
2526
line-height: inherit;
2627
}
2728

29+
textarea {
30+
color: inherit;
31+
font-family: inherit;
32+
font-size: inherit;
33+
line-height: inherit;
34+
}
35+
2836
button:not(:disabled) {
2937
cursor: pointer;
3038
}

‎packages/vscode/package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@
537537
"description": "The instructions used when generating a commit message.",
538538
"default": "Write a brief and precise summary for the changes, limited to a single sentence if possible and nothing else. Use an imperative tone to ensure clarity and focus on the primary change or purpose."
539539
},
540+
"codeWebChat.contextSizeWarningThreshold": {
541+
"type": "number",
542+
"default": 60000,
543+
"description": "Threshold for the context size to show a warning."
544+
},
540545
"codeWebChat.ignorePatterns": {
541546
"type": "array",
542547
"description": "List of glob patterns to ignore files and folders in the context.",

‎packages/vscode/src/views/panel/backend/panel-provider.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ export class ViewProvider implements vscode.WebviewViewProvider {
191191
handle_get_edit_format_instructions(this)
192192
}
193193

194+
if (
195+
event.affectsConfiguration('codeWebChat.contextSizeWarningThreshold')
196+
) {
197+
this.send_context_size_warning_threshold()
198+
}
199+
194200
const all_api_config_keys = [
195201
'codeWebChat.configurationsForEditContext',
196202
'codeWebChat.configurationsForCodeCompletions'
@@ -500,6 +506,7 @@ export class ViewProvider implements vscode.WebviewViewProvider {
500506
)
501507

502508
this.send_presets_to_webview(webview_view.webview)
509+
this.send_context_size_warning_threshold()
503510
// We need to wait until the webview fully initialized
504511
setTimeout(() => {
505512
this.send_message({
@@ -683,6 +690,16 @@ export class ViewProvider implements vscode.WebviewViewProvider {
683690
})
684691
}
685692

693+
public send_context_size_warning_threshold() {
694+
if (!this._webview_view) return
695+
const config = vscode.workspace.getConfiguration('codeWebChat')
696+
const threshold = config.get<number>('contextSizeWarningThreshold', 60000)
697+
this.send_message({
698+
command: 'CONTEXT_SIZE_WARNING_THRESHOLD',
699+
threshold
700+
})
701+
}
702+
686703
private _send_git_state() {
687704
try {
688705
const git_extension =

‎packages/vscode/src/views/panel/frontend/Main/Main.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Props = {
5151
chat_input_focus_and_select_key: number
5252
chat_input_focus_key: number
5353
commit_button_enabling_trigger_count: number
54+
context_size_warning_threshold: number
5455
}
5556

5657
export const Main: React.FC<Props> = (props) => {
@@ -655,6 +656,7 @@ export const Main: React.FC<Props> = (props) => {
655656
token_count={token_count}
656657
web_mode={props.web_mode}
657658
api_mode={props.api_mode}
659+
context_size_warning_threshold={props.context_size_warning_threshold}
658660
on_web_mode_change={props.on_web_mode_change}
659661
on_api_mode_change={props.on_api_mode_change}
660662
chat_edit_format={chat_edit_format}

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.module.scss‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@
226226
}
227227
}
228228

229-
.chat-input {
229+
.chat-input-container {
230+
display: flex;
231+
flex-direction: column;
232+
gap: var(--padding-6px);
230233
margin: 0 var(--padding-12px);
231234
}
232235

0 commit comments

Comments
 (0)