@@ -21,6 +21,7 @@ import path from 'path'
2121import { fs } from '../../fs/fs'
2222import { getLogger } from '../../logger/logger'
2323import { ChatMessage , ToolResultStatus } from '@amzn/codewhisperer-streaming'
24+ import { CWCTelemetryHelper } from '../../../codewhispererChat/controllers/chat/telemetryHelper'
2425
2526// Maximum number of characters to keep in history
2627const MaxConversationHistoryCharacters = 600_000
@@ -275,6 +276,7 @@ export class Database {
275276 ? message . body
276277 : tabData ?. title || 'Amazon Q Chat'
277278 message = this . formatChatHistoryMessage ( message )
279+ message . characterCount = this . calculateMessageCharacterCount ( message )
278280 if ( tabData ) {
279281 this . logger . info ( `Found existing tab data, updating conversations` )
280282 tabData . conversations = updateOrCreateConversation ( tabData . conversations , conversationId , message )
@@ -292,6 +294,7 @@ export class Database {
292294 conversations : [ { conversationId, clientType : ClientType . VSCode , messages : [ message ] } ] ,
293295 } )
294296 }
297+ CWCTelemetryHelper . instance . record_TODO ( tabId , conversationId , message )
295298 }
296299 }
297300
@@ -419,31 +422,35 @@ export class Database {
419422 private calculateCharacterCount ( allMessages : Message [ ] ) : number {
420423 let count = 0
421424 for ( const message of allMessages ) {
422- // Count characters of all message text
423- count += message . body . length
424-
425- // Count characters in tool uses
426- if ( message . toolUses ) {
427- try {
428- for ( const toolUse of message . toolUses ) {
429- count += JSON . stringify ( toolUse ) . length
430- }
431- } catch ( e ) {
432- this . logger . error ( `Error counting toolUses: ${ String ( e ) } ` )
425+ count += message . characterCount ?? 0
426+ }
427+ this . logger . debug ( `Current history characters: ${ count } ` )
428+ return count
429+ }
430+
431+ private calculateMessageCharacterCount ( message : Message ) : number {
432+ let count = message . body . length
433+
434+ // Count characters in tool uses
435+ if ( message . toolUses ) {
436+ try {
437+ for ( const toolUse of message . toolUses ) {
438+ count += JSON . stringify ( toolUse ) . length
433439 }
440+ } catch ( e ) {
441+ this . logger . error ( `Error counting toolUses: ${ String ( e ) } ` )
434442 }
435- // Count characters in tool results
436- if ( message . userInputMessageContext ?. toolResults ) {
437- try {
438- for ( const toolResul of message . userInputMessageContext . toolResults ) {
439- count += JSON . stringify ( toolResul ) . length
440- }
441- } catch ( e ) {
442- this . logger . error ( `Error counting toolResults: ${ String ( e ) } ` )
443+ }
444+ // Count characters in tool results
445+ if ( message . userInputMessageContext ?. toolResults ) {
446+ try {
447+ for ( const toolResul of message . userInputMessageContext . toolResults ) {
448+ count += JSON . stringify ( toolResul ) . length
443449 }
450+ } catch ( e ) {
451+ this . logger . error ( `Error counting toolResults: ${ String ( e ) } ` )
444452 }
445453 }
446- this . logger . debug ( `Current history characters: ${ count } ` )
447454 return count
448455 }
449456
0 commit comments