@@ -79,8 +79,7 @@ import {
7979 handle_delete_api_configuration ,
8080 handle_update_last_used_web_configuration ,
8181 handle_request_return_home ,
82- handle_pick_tasks_workspace ,
83- handle_get_token_count
82+ handle_pick_tasks_workspace
8483} from './message-handlers'
8584import { handle_update_api_configuration } from './message-handlers/handle-update-api-configuration'
8685import { handle_pick_model_provider } from './message-handlers/handle-pick-model-provider'
@@ -114,6 +113,13 @@ import { SharedContextState } from '@/context/shared-context-state'
114113import { webview_html } from '@/views/shared/utils/webview-html'
115114import { get_selected_files } from '@/context/helpers/get-selected-files'
116115import { normalize_path } from '@/utils/normalize-path'
116+ import { replace_symbols } from './utils/symbols/replace-symbols'
117+ import {
118+ EDIT_FORMAT_INSTRUCTIONS_WHOLE ,
119+ EDIT_FORMAT_INSTRUCTIONS_TRUNCATED ,
120+ EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE ,
121+ EDIT_FORMAT_INSTRUCTIONS_DIFF
122+ } from '@/constants/edit-format-instructions'
117123
118124export class PromptViewProvider implements vscode . WebviewViewProvider {
119125 public readonly extension_uri : vscode . Uri
@@ -158,6 +164,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
158164 workspace_name ?: string
159165 } [ ] = [ ]
160166 public api_call_abort_controller : AbortController | null = null
167+ public current_selected_files_token_count : number = 0
161168 public prompt_view_api_calls_manager ! : PromptViewApiCallsManager
162169 public response_history : ResponseHistoryItem [ ] = [ ]
163170 public message_listeners : ( ( message : BackendMessage ) => void ) [ ] = [ ]
@@ -398,12 +405,9 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
398405 )
399406
400407 token_count_emitter . on ( 'token-count-updated' , ( token_count : number ) => {
408+ this . current_selected_files_token_count = token_count
401409 if ( this . webview_view ) {
402- this . send_message ( {
403- command : 'TOKEN_COUNT_UPDATED' ,
404- token_count
405- } )
406-
410+ this . send_token_count ( )
407411 this . send_selected_files ( )
408412 }
409413 } )
@@ -536,6 +540,50 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
536540 controllers . forEach ( ( item ) => item . controller . abort ( 'Preview finished.' ) )
537541 }
538542
543+ public async send_token_count ( ) {
544+ const edit_instructions_result = await replace_symbols ( {
545+ instruction : this . current_edit_files_instruction ,
546+ extension_context : this . extension_context ,
547+ workspace_provider : this . workspace_provider ,
548+ remove_images : true
549+ } )
550+ const ask_instructions_result = await replace_symbols ( {
551+ instruction : this . current_ask_about_context_instruction ,
552+ extension_context : this . extension_context ,
553+ workspace_provider : this . workspace_provider ,
554+ remove_images : true
555+ } )
556+
557+ let formatted_system_instructions = ''
558+ const edit_format_instructions = {
559+ whole : EDIT_FORMAT_INSTRUCTIONS_WHOLE ,
560+ truncated : EDIT_FORMAT_INSTRUCTIONS_TRUNCATED ,
561+ 'search-replace' : EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE ,
562+ diff : EDIT_FORMAT_INSTRUCTIONS_DIFF
563+ } [ this . edit_format ]
564+ if ( edit_format_instructions ) {
565+ formatted_system_instructions = `# Output formatting\n\n${ edit_format_instructions } `
566+ }
567+
568+ this . send_message ( {
569+ command : 'TOKEN_COUNT_UPDATED' ,
570+ selected_files_token_count : this . current_selected_files_token_count ,
571+ edit_instructions_token_count : Math . ceil (
572+ ( edit_instructions_result . instruction . length +
573+ edit_instructions_result . skill_definitions . length ) /
574+ 4
575+ ) ,
576+ ask_instructions_token_count : Math . ceil (
577+ ( ask_instructions_result . instruction . length +
578+ ask_instructions_result . skill_definitions . length ) /
579+ 4
580+ ) ,
581+ edit_format_instructions_token_count : Math . ceil (
582+ formatted_system_instructions . length / 4
583+ )
584+ } )
585+ }
586+
539587 public send_selected_files ( ) {
540588 const file_paths = get_selected_files ( {
541589 workspace_provider : this . workspace_provider ,
@@ -612,6 +660,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
612660 handle_get_instructions ( this )
613661 } else if ( message . command == 'SAVE_INSTRUCTIONS' ) {
614662 await handle_save_instructions ( this , message )
663+ this . send_token_count ( )
615664 } else if ( message . command == 'GET_CONNECTION_STATUS' ) {
616665 handle_get_connection_status ( this )
617666 } else if ( message . command == 'GET_WEB_CONFIGURATIONS' ) {
@@ -650,8 +699,10 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
650699 await handle_delete_web_configuration ( message )
651700 } else if ( message . command == 'UNDO' ) {
652701 await handle_undo ( this )
702+ this . send_token_count ( )
653703 } else if ( message . command == 'APPLY_RESPONSE_FROM_HISTORY' ) {
654704 await handle_apply_response_from_history ( message )
705+ this . send_token_count ( )
655706 } else if ( message . command == 'EXECUTE_COMMAND' ) {
656707 await vscode . commands . executeCommand ( message . command_id )
657708 } else if ( message . command == 'MAKE_API_CALL' ) {
@@ -800,7 +851,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
800851 } else if ( message . command == 'PICK_TASKS_WORKSPACE' ) {
801852 await handle_pick_tasks_workspace ( this , message )
802853 } else if ( message . command == 'GET_TOKEN_COUNT' ) {
803- await handle_get_token_count ( this )
854+ this . send_token_count ( )
804855 }
805856 } catch ( error : any ) {
806857 Logger . error ( {
@@ -907,5 +958,6 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
907958 edit_files : this . edit_files_instructions ,
908959 caret_position : this . caret_position
909960 } )
961+ this . send_token_count ( )
910962 }
911963}
0 commit comments