Skip to content

Commit cd1a549

Browse files
committed
Add panel mode state management and optimize token count calculation by moving it to context initialization
1 parent 04587f4 commit cd1a549

8 files changed

Lines changed: 30 additions & 111 deletions

File tree

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const CHAT_EDIT_FORMAT_STATE_KEY = 'chat-edit-format'
4444
export const API_EDIT_FORMAT_STATE_KEY = 'api-edit-format'
4545
export const WEB_MODE_STATE_KEY = 'web-mode'
4646
export const API_MODE_STATE_KEY = 'api-mode'
47+
export const PANEL_MODE_STATE_KEY = 'panel-mode'
4748

4849
export const INSTRUCTIONS_EDIT_CONTEXT_STATE_KEY = 'instructions-edit-context'
4950
export const INSTRUCTIONS_ASK_STATE_KEY = 'instructions-ask'

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
CONTEXT_CHECKED_URLS_STATE_KEY,
1717
CONTEXT_CHECKED_TIMESTAMPS_STATE_KEY,
1818
DUPLICATE_WORKSPACE_CONTEXT_STATE_KEY,
19-
SAVED_CONTEXTS_STATE_KEY,
2019
RANGES_STATE_KEY,
2120
type DuplicateWorkspaceContext
2221
} from '../constants/state-keys'
@@ -82,12 +81,6 @@ const restore_duplicated_workspace_context = async (
8281
duplicated_context.ranges
8382
)
8483
}
85-
if (duplicated_context.saved_contexts) {
86-
await context.workspaceState.update(
87-
SAVED_CONTEXTS_STATE_KEY,
88-
duplicated_context.saved_contexts
89-
)
90-
}
9184
if (duplicated_context.open_editors) {
9285
for (const editor_info of duplicated_context.open_editors) {
9386
try {
@@ -150,8 +143,12 @@ export const context_initialization = async (
150143
const update_view_badges = async () => {
151144
let context_token_count = 0
152145
if (context_provider && context_view) {
153-
context_token_count =
146+
const files_count =
154147
await workspace_provider.get_checked_files_token_count()
148+
const websites_count =
149+
websites_provider.get_checked_websites_token_count()
150+
context_token_count = files_count + websites_count
151+
155152
workspace_view.badge = {
156153
value: round_token_count_for_badge(context_token_count),
157154
tooltip: context_token_count
@@ -160,7 +157,7 @@ export const context_initialization = async (
160157
}
161158
context_view.badge = undefined
162159
}
163-
token_count_emitter.emit('token-count-updated')
160+
token_count_emitter.emit('token-count-updated', context_token_count)
164161
}
165162

166163
const shared_state = SharedFileState.get_instance()

packages/vscode/src/views/panel/backend/message-handlers/handle-save-home-view-type.ts renamed to packages/vscode/src/views/panel/backend/message-handlers/handle-save-mode.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { PanelProvider } from '@/views/panel/backend/panel-provider'
22
import { SaveModeMessage } from '@/views/panel/types/messages'
3+
import { PANEL_MODE_STATE_KEY } from '@/constants/state-keys'
34

45
export const handle_save_mode = async (
56
panel_provider: PanelProvider,
67
message: SaveModeMessage
78
): Promise<void> => {
89
panel_provider.mode = message.mode
9-
panel_provider.send_message({
10-
command: 'MODE',
11-
mode: message.mode
12-
})
10+
await panel_provider.context.workspaceState.update(
11+
PANEL_MODE_STATE_KEY,
12+
message.mode
13+
)
14+
await panel_provider.context.globalState.update(
15+
PANEL_MODE_STATE_KEY,
16+
message.mode
17+
)
1318
}

‎packages/vscode/src/views/panel/backend/message-handlers/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export * from './handle-show-prompt-template-quick-pick'
2121
export * from './handle-update-preset'
2222
export * from './handle-pick-chatbot'
2323
export * from './handle-pick-open-router-model'
24-
export * from './handle-save-home-view-type'
24+
export * from './handle-save-mode'
2525
export * from './handle-get-mode'
2626
export * from './handle-edit-context'
2727
export * from './handle-code-completion'

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

Lines changed: 13 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
handle_at_sign_quick_pick,
4444
handle_get_mode_web,
4545
handle_save_mode_web,
46+
handle_save_mode,
4647
handle_get_mode_api,
4748
handle_save_mode_api,
4849
handle_get_mode,
@@ -89,6 +90,7 @@ import {
8990
INSTRUCTIONS_NO_CONTEXT_STATE_KEY,
9091
LAST_SELECTED_CODE_COMPLETION_CONFIG_ID_STATE_KEY,
9192
LAST_SELECTED_EDIT_CONTEXT_CONFIG_ID_STATE_KEY,
93+
PANEL_MODE_STATE_KEY,
9294
WEB_MODE_STATE_KEY,
9395
get_recently_used_presets_or_groups_key
9496
} from '@/constants/state-keys'
@@ -210,6 +212,10 @@ export class PanelProvider implements vscode.WebviewViewProvider {
210212
this.context.workspaceState.get<EditFormat>(API_EDIT_FORMAT_STATE_KEY) ??
211213
this.context.globalState.get<EditFormat>(API_EDIT_FORMAT_STATE_KEY) ??
212214
'whole'
215+
this.mode =
216+
this.context.workspaceState.get<Mode>(PANEL_MODE_STATE_KEY) ??
217+
this.context.globalState.get<Mode>(PANEL_MODE_STATE_KEY) ??
218+
MODE.WEB
213219

214220
this.web_prompt_type = this.context.workspaceState.get<WebPromptType>(
215221
WEB_MODE_STATE_KEY,
@@ -282,9 +288,13 @@ export class PanelProvider implements vscode.WebviewViewProvider {
282288
}
283289
)
284290

285-
token_count_emitter.on('token-count-updated', () => {
291+
token_count_emitter.on('token-count-updated', (token_count: number) => {
286292
if (this._webview_view) {
287-
this.calculate_token_count()
293+
this.send_message({
294+
command: 'TOKEN_COUNT_UPDATED',
295+
token_count
296+
})
297+
288298
this.send_context_files()
289299
}
290300
})
@@ -347,13 +357,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
347357
vscode.window.activeTextEditor &&
348358
event.document === vscode.window.activeTextEditor.document
349359
) {
350-
if (
351-
(this.mode == MODE.WEB &&
352-
this.web_prompt_type == 'code-completions') ||
353-
(this.mode == MODE.API && this.api_prompt_type == 'code-completions')
354-
) {
355-
this.calculate_token_count()
356-
}
357360
this.send_currently_open_file_text()
358361
}
359362
})
@@ -507,8 +510,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
507510
handle_request_editor_state(this)
508511
} else if (message.command == 'REQUEST_EDITOR_SELECTION_STATE') {
509512
handle_request_editor_selection_state(this)
510-
} else if (message.command == 'GET_CURRENT_TOKEN_COUNT') {
511-
this.calculate_token_count()
512513
} else if (message.command == 'GET_CONTEXT_SIZE_WARNING_THRESHOLD') {
513514
this._send_context_size_warning_threshold()
514515
} else if (message.command == 'REQUEST_CURRENTLY_OPEN_FILE_TEXT') {
@@ -579,8 +580,7 @@ export class PanelProvider implements vscode.WebviewViewProvider {
579580
} else if (message.command == 'CARET_POSITION_CHANGED') {
580581
this.caret_position = message.caret_position
581582
} else if (message.command == 'SAVE_MODE') {
582-
this.mode = message.mode
583-
this.calculate_token_count()
583+
await handle_save_mode(this, message)
584584
} else if (message.command == 'GET_MODE') {
585585
handle_get_mode(this)
586586
} else if (message.command == 'GET_VERSION') {
@@ -671,78 +671,6 @@ export class PanelProvider implements vscode.WebviewViewProvider {
671671
)
672672
}
673673

674-
public calculate_token_count() {
675-
if (this.mode == MODE.WEB && this.web_prompt_type == 'no-context') {
676-
this.send_message({
677-
command: 'TOKEN_COUNT_UPDATED',
678-
token_count: 0
679-
})
680-
return
681-
}
682-
683-
const active_editor = vscode.window.activeTextEditor
684-
685-
const is_code_completions_mode =
686-
(this.mode == MODE.WEB && this.web_prompt_type == 'code-completions') ||
687-
(this.mode == MODE.API && this.api_prompt_type == 'code-completions')
688-
689-
Promise.all([
690-
this.workspace_provider.get_checked_files_token_count({
691-
exclude_file_path:
692-
is_code_completions_mode && active_editor
693-
? active_editor.document.uri.fsPath
694-
: undefined
695-
}),
696-
this.websites_provider.get_checked_websites_token_count()
697-
])
698-
.then(([workspace_tokens, websites_tokens]) => {
699-
let current_token_count = workspace_tokens + websites_tokens
700-
701-
if (active_editor && is_code_completions_mode) {
702-
const document = active_editor.document
703-
const text = document.getText()
704-
const file_path = document.uri.fsPath
705-
const workspace_root =
706-
this.workspace_provider.get_workspace_root_for_file(file_path)
707-
let content_xml = ''
708-
709-
if (!workspace_root) {
710-
content_xml = `<file path="${file_path}">\n<![CDATA[\n${text}\n]]>\n</file>\n`
711-
} else {
712-
const relative_path = path.relative(workspace_root, file_path)
713-
if (this.workspace_provider.get_workspace_roots().length > 1) {
714-
const workspace_name =
715-
this.workspace_provider.get_workspace_name(workspace_root)
716-
content_xml = `<file path="${workspace_name}/${relative_path}">\n<![CDATA[\n${text}\n]]>\n</file>\n`
717-
} else {
718-
content_xml = `<file path="${relative_path}">\n<![CDATA[\n${text}\n]]>\n</file>\n`
719-
}
720-
}
721-
const file_token_count = Math.floor(content_xml.length / 4)
722-
current_token_count += file_token_count
723-
}
724-
725-
this.send_message({
726-
command: 'TOKEN_COUNT_UPDATED',
727-
token_count: current_token_count
728-
})
729-
})
730-
.catch((error) => {
731-
Logger.error({
732-
function_name: 'calculate_token_count',
733-
message: 'Error calculating token count',
734-
data: error
735-
})
736-
vscode.window.showErrorMessage(
737-
dictionary.error_message.ERROR_CALCULATING_TOKEN_COUNT(error.message)
738-
)
739-
this.send_message({
740-
command: 'TOKEN_COUNT_UPDATED',
741-
token_count: 0
742-
})
743-
})
744-
}
745-
746674
public send_presets_to_webview(_: vscode.Webview) {
747675
const config = vscode.workspace.getConfiguration('codeWebChat')
748676
const web_modes: WebPromptType[] = [

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ export const Main: React.FC<Props> = (props) => {
160160
const initial_messages: FrontendMessage[] = [
161161
{ command: 'GET_PRESETS' },
162162
{ command: 'GET_HISTORY' },
163-
{ command: 'GET_CURRENT_TOKEN_COUNT' },
164163
{ command: 'GET_INSTRUCTIONS' },
165164
{ command: 'GET_EDIT_FORMAT' },
166165
{ command: 'GET_EDIT_FORMAT_INSTRUCTIONS' },

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,6 @@ export const use_panel = (vscode: any) => {
433433
command: 'SAVE_WEB_MODE',
434434
mode: new_mode
435435
})
436-
post_message(vscode, {
437-
command: 'GET_CURRENT_TOKEN_COUNT'
438-
})
439436
}
440437

441438
const handle_api_prompt_type_change = (new_mode: ApiPromptType) => {
@@ -446,9 +443,6 @@ export const use_panel = (vscode: any) => {
446443
command: 'SAVE_API_MODE',
447444
mode: new_mode
448445
})
449-
post_message(vscode, {
450-
command: 'GET_CURRENT_TOKEN_COUNT'
451-
})
452446
}
453447

454448
const handle_discard_user_changes_in_preview = (file: {

‎packages/vscode/src/views/panel/types/messages.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ export interface SaveHistoryMessage extends BaseMessage {
124124
mode: 'ask' | 'edit-context' | 'no-context' | 'code-completions'
125125
}
126126

127-
export interface GetCurrentTokenCountMessage extends BaseMessage {
128-
command: 'GET_CURRENT_TOKEN_COUNT'
129-
}
130-
131127
export interface GetContextSizeWarningThresholdMessage extends BaseMessage {
132128
command: 'GET_CONTEXT_SIZE_WARNING_THRESHOLD'
133129
}
@@ -469,7 +465,6 @@ export type FrontendMessage =
469465
| GetHistoryMessage
470466
| GetResponseHistoryMessage
471467
| SaveHistoryMessage
472-
| GetCurrentTokenCountMessage
473468
| GetContextSizeWarningThresholdMessage
474469
| UpdatePresetMessage
475470
| DeletePresetGroupOrSeparatorMessage

0 commit comments

Comments
 (0)