@@ -21,6 +21,7 @@ import path from 'path'
21
21
import { fs } from '../../fs/fs'
22
22
import { getLogger } from '../../logger/logger'
23
23
import { ChatMessage , ToolResultStatus } from '@amzn/codewhisperer-streaming'
24
+ import { CWCTelemetryHelper } from '../../../codewhispererChat/controllers/chat/telemetryHelper'
24
25
25
26
// Maximum number of characters to keep in history
26
27
const MaxConversationHistoryCharacters = 600_000
@@ -275,6 +276,7 @@ export class Database {
275
276
? message . body
276
277
: tabData ?. title || 'Amazon Q Chat'
277
278
message = this . formatChatHistoryMessage ( message )
279
+ message . characterCount = this . calculateMessageCharacterCount ( message )
278
280
if ( tabData ) {
279
281
this . logger . info ( `Found existing tab data, updating conversations` )
280
282
tabData . conversations = updateOrCreateConversation ( tabData . conversations , conversationId , message )
@@ -292,6 +294,7 @@ export class Database {
292
294
conversations : [ { conversationId, clientType : ClientType . VSCode , messages : [ message ] } ] ,
293
295
} )
294
296
}
297
+ CWCTelemetryHelper . instance . record_TODO ( )
295
298
}
296
299
}
297
300
@@ -419,31 +422,35 @@ export class Database {
419
422
private calculateCharacterCount ( allMessages : Message [ ] ) : number {
420
423
let count = 0
421
424
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
433
439
}
440
+ } catch ( e ) {
441
+ this . logger . error ( `Error counting toolUses: ${ String ( e ) } ` )
434
442
}
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
443
449
}
450
+ } catch ( e ) {
451
+ this . logger . error ( `Error counting toolResults: ${ String ( e ) } ` )
444
452
}
445
453
}
446
- this . logger . debug ( `Current history characters: ${ count } ` )
447
454
return count
448
455
}
449
456
0 commit comments