Skip to content

Commit 64a6d1a

Browse files
committed
Introduce a warning notification when the total context token count exceeds the configured size threshold
1 parent 41e39c2 commit 64a6d1a

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +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 and costs.`,
8587
FAILED_TO_CREATE_FILE: (file_path: string) =>
8688
`Failed to create file: ${file_path}`,
8789
FAILED_TO_APPLY_CHANGES_TO_FILE: (file_path: string) =>

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export function context_initialization(context: vscode.ExtensionContext): {
1818
open_editors_provider: OpenEditorsProvider
1919
websites_provider: WebsitesProvider
2020
} {
21+
let was_above_threshold = false
22+
2123
const workspace_folders = vscode.workspace.workspaceFolders ?? []
2224

2325
let workspace_view: vscode.TreeView<FileItem>
@@ -42,8 +44,9 @@ export function context_initialization(context: vscode.ExtensionContext): {
4244
)
4345

4446
const update_view_badges = async () => {
47+
let context_token_count = 0
4548
if (context_provider && context_view) {
46-
const context_token_count =
49+
context_token_count =
4750
await workspace_provider.get_checked_files_token_count()
4851
context_view.badge = {
4952
value: context_token_count,
@@ -53,8 +56,9 @@ export function context_initialization(context: vscode.ExtensionContext): {
5356
}
5457
}
5558

59+
let websites_token_count = 0
5660
if (websites_provider && websites_view) {
57-
const websites_token_count =
61+
websites_token_count =
5862
websites_provider.get_checked_websites_token_count()
5963
websites_view.badge = {
6064
value: websites_token_count,
@@ -64,6 +68,22 @@ export function context_initialization(context: vscode.ExtensionContext): {
6468
}
6569
}
6670
token_count_emitter.emit('token-count-updated')
71+
72+
const total_token_count = context_token_count + websites_token_count
73+
const config = vscode.workspace.getConfiguration('codeWebChat')
74+
const threshold = config.get<number>('contextSizeWarningThreshold')
75+
76+
if (threshold && threshold > 0) {
77+
const is_above_threshold = total_token_count > threshold
78+
if (is_above_threshold && !was_above_threshold) {
79+
vscode.window.showWarningMessage(
80+
dictionary.warning_message.CONTEXT_SIZE_WARNING(threshold)
81+
)
82+
}
83+
was_above_threshold = is_above_threshold
84+
} else {
85+
was_above_threshold = false
86+
}
6787
}
6888

6989
const shared_state = SharedFileState.get_instance()

0 commit comments

Comments
 (0)