@@ -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 [ ] = [
0 commit comments