@@ -75,9 +75,7 @@ export class ViewProvider implements vscode.WebviewViewProvider {
7575
7676 public get_presets_config_key ( ) : string {
7777 const mode =
78- this . home_view_type === HOME_VIEW_TYPES . API
79- ? this . api_mode
80- : this . web_mode
78+ this . home_view_type == HOME_VIEW_TYPES . API ? this . api_mode : this . web_mode
8179 switch ( mode ) {
8280 case 'ask' :
8381 return 'chatPresetsForAskAboutContext'
@@ -252,68 +250,6 @@ export class ViewProvider implements vscode.WebviewViewProvider {
252250 } )
253251 }
254252
255- public calculate_token_count ( ) {
256- const active_editor = vscode . window . activeTextEditor
257-
258- const is_code_completions_mode =
259- ( this . home_view_type == HOME_VIEW_TYPES . WEB &&
260- this . web_mode == 'code-completions' ) ||
261- ( this . home_view_type == HOME_VIEW_TYPES . API &&
262- this . api_mode == 'code-completions' )
263-
264- Promise . all ( [
265- this . workspace_provider . get_checked_files_token_count ( {
266- exclude_file_path :
267- is_code_completions_mode && active_editor
268- ? active_editor . document . uri . fsPath
269- : undefined
270- } ) ,
271- this . websites_provider . get_checked_websites_token_count ( )
272- ] )
273- . then ( ( [ workspace_tokens , websites_tokens ] ) => {
274- let current_token_count = workspace_tokens + websites_tokens
275-
276- if ( active_editor && is_code_completions_mode ) {
277- const document = active_editor . document
278- const text = document . getText ( )
279- const file_path = document . uri . fsPath
280- const workspace_root =
281- this . workspace_provider . get_workspace_root_for_file ( file_path )
282- let content_xml = ''
283-
284- if ( ! workspace_root ) {
285- content_xml = `<file path="${ file_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
286- } else {
287- const relative_path = path . relative ( workspace_root , file_path )
288- if ( this . workspace_provider . getWorkspaceRoots ( ) . length > 1 ) {
289- const workspace_name =
290- this . workspace_provider . get_workspace_name ( workspace_root )
291- content_xml = `<file path="${ workspace_name } /${ relative_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
292- } else {
293- content_xml = `<file path="${ relative_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
294- }
295- }
296- const file_token_count = Math . floor ( content_xml . length / 4 )
297- current_token_count += file_token_count
298- }
299-
300- this . send_message < TokenCountMessage > ( {
301- command : 'TOKEN_COUNT_UPDATED' ,
302- token_count : current_token_count
303- } )
304- } )
305- . catch ( ( error ) => {
306- console . error ( 'Error calculating token count:' , error )
307- vscode . window . showErrorMessage (
308- `Error calculating token count: ${ error . message } `
309- )
310- this . send_message < TokenCountMessage > ( {
311- command : 'TOKEN_COUNT_UPDATED' ,
312- token_count : 0
313- } )
314- } )
315- }
316-
317253 public send_message < T > ( message : T ) {
318254 if ( this . _webview_view ) {
319255 this . _webview_view . webview . postMessage ( message )
@@ -429,6 +365,68 @@ export class ViewProvider implements vscode.WebviewViewProvider {
429365 this . send_presets_to_webview ( webview_view . webview )
430366 }
431367
368+ public calculate_token_count ( ) {
369+ const active_editor = vscode . window . activeTextEditor
370+
371+ const is_code_completions_mode =
372+ ( this . home_view_type == HOME_VIEW_TYPES . WEB &&
373+ this . web_mode == 'code-completions' ) ||
374+ ( this . home_view_type == HOME_VIEW_TYPES . API &&
375+ this . api_mode == 'code-completions' )
376+
377+ Promise . all ( [
378+ this . workspace_provider . get_checked_files_token_count ( {
379+ exclude_file_path :
380+ is_code_completions_mode && active_editor
381+ ? active_editor . document . uri . fsPath
382+ : undefined
383+ } ) ,
384+ this . websites_provider . get_checked_websites_token_count ( )
385+ ] )
386+ . then ( ( [ workspace_tokens , websites_tokens ] ) => {
387+ let current_token_count = workspace_tokens + websites_tokens
388+
389+ if ( active_editor && is_code_completions_mode ) {
390+ const document = active_editor . document
391+ const text = document . getText ( )
392+ const file_path = document . uri . fsPath
393+ const workspace_root =
394+ this . workspace_provider . get_workspace_root_for_file ( file_path )
395+ let content_xml = ''
396+
397+ if ( ! workspace_root ) {
398+ content_xml = `<file path="${ file_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
399+ } else {
400+ const relative_path = path . relative ( workspace_root , file_path )
401+ if ( this . workspace_provider . getWorkspaceRoots ( ) . length > 1 ) {
402+ const workspace_name =
403+ this . workspace_provider . get_workspace_name ( workspace_root )
404+ content_xml = `<file path="${ workspace_name } /${ relative_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
405+ } else {
406+ content_xml = `<file path="${ relative_path } ">\n<![CDATA[\n${ text } \n]]>\n</file>\n`
407+ }
408+ }
409+ const file_token_count = Math . floor ( content_xml . length / 4 )
410+ current_token_count += file_token_count
411+ }
412+
413+ this . send_message < TokenCountMessage > ( {
414+ command : 'TOKEN_COUNT_UPDATED' ,
415+ token_count : current_token_count
416+ } )
417+ } )
418+ . catch ( ( error ) => {
419+ console . error ( 'Error calculating token count:' , error )
420+ vscode . window . showErrorMessage (
421+ `Error calculating token count: ${ error . message } `
422+ )
423+ this . send_message < TokenCountMessage > ( {
424+ command : 'TOKEN_COUNT_UPDATED' ,
425+ token_count : 0
426+ } )
427+ } )
428+ }
429+
432430 public send_presets_to_webview ( _ : vscode . Webview ) {
433431 const config = vscode . workspace . getConfiguration ( 'codeWebChat' )
434432 const web_modes : WebMode [ ] = [
0 commit comments