@@ -416,34 +416,53 @@ export class Database {
416
416
return messages
417
417
}
418
418
419
+ // character counts for history and each message in history for telemetry purposes
420
+ calculateDetailedCharacterCount ( allMessages : Message [ ] ) : { total : number ; individualCounts : number [ ] } {
421
+ const individualCounts : number [ ] = [ ]
422
+ let total = 0
423
+
424
+ for ( const message of allMessages ) {
425
+ const messageCount = this . calculateMessageCharacterCount ( message )
426
+ individualCounts . push ( messageCount )
427
+ total += messageCount
428
+ }
429
+
430
+ this . logger . debug ( `Current history characters: ${ total } ` )
431
+ return { total, individualCounts }
432
+ }
433
+
419
434
private calculateCharacterCount ( allMessages : Message [ ] ) : number {
420
435
let count = 0
421
436
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 ) } ` )
437
+ count += this . calculateMessageCharacterCount ( message )
438
+ }
439
+ this . logger . debug ( `Current history characters: ${ count } ` )
440
+ return count
441
+ }
442
+
443
+ private calculateMessageCharacterCount ( message : Message ) : number {
444
+ let count = message . body . length
445
+
446
+ // Count characters in tool uses
447
+ if ( message . toolUses ) {
448
+ try {
449
+ for ( const toolUse of message . toolUses ) {
450
+ count += JSON . stringify ( toolUse ) . length
433
451
}
452
+ } catch ( e ) {
453
+ this . logger . error ( `Error counting toolUses: ${ String ( e ) } ` )
434
454
}
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 ) } ` )
455
+ }
456
+ // Count characters in tool results
457
+ if ( message . userInputMessageContext ?. toolResults ) {
458
+ try {
459
+ for ( const toolResul of message . userInputMessageContext . toolResults ) {
460
+ count += JSON . stringify ( toolResul ) . length
443
461
}
462
+ } catch ( e ) {
463
+ this . logger . error ( `Error counting toolResults: ${ String ( e ) } ` )
444
464
}
445
465
}
446
- this . logger . debug ( `Current history characters: ${ count } ` )
447
466
return count
448
467
}
449
468
0 commit comments