@@ -23,6 +23,7 @@ function sleep(ms) {
2323
2424
2525function splitStringByTokens ( str , maxTokens ) {
26+ console . error ( 'splitStringByTokens' ) ;
2627 const words = str . split ( ' ' ) ;
2728 const result = [ ] ;
2829 let currentLine = '' ;
@@ -51,30 +52,23 @@ function chunkDiffByFiles(diffContent) {
5152 const lines = diffContent . split ( '\n' ) ;
5253 let currentChunk = '' ;
5354 let currentFile = '' ;
55+ let tokenCount = 0 ;
5456
5557 for ( const line of lines ) {
5658 // Check if this is a new file header
5759 console . error ( `Line is estimated at ${ estimateTokens ( line ) } tokens` ) ;
60+ tokenCount += estimateTokens ( line ) ;
61+ console . error ( `Total tokens for this chunk is ${ tokenCount } ` ) ;
5862 if ( line . startsWith ( 'diff --git' ) || line . startsWith ( '+++' ) || line . startsWith ( '---' ) ) {
5963 // If we have content and it's getting large, save current chunk
60- if ( currentChunk && estimateTokens ( currentChunk + '\n' + line ) > MAX_TOKENS_PER_REQUEST ) {
61- if ( estimateTokens ( currentChunk ) > MAX_TOKENS_PER_REQUEST ) {
62- const split_chunk = splitStringByTokens ( currentChunk , MAX_TOKENS_PER_REQUEST ) ;
63- split_chunk . forEach ( ( chunk ) => {
64- fileChunks . push ( {
65- content : chunk . trim ( ) ,
66- file : currentFile ,
67- type : 'file-chunk'
68- } ) ;
69- } )
70- } else {
71- fileChunks . push ( {
72- content : currentChunk . trim ( ) ,
73- file : currentFile ,
74- type : 'file-chunk'
75- } ) ;
76- }
64+ if ( currentChunk && tokenCount > MAX_TOKENS_PER_REQUEST ) {
65+ fileChunks . push ( {
66+ content : currentChunk . trim ( ) ,
67+ file : currentFile ,
68+ type : 'file-chunk'
69+ } ) ;
7770 currentChunk = '' ;
71+ tokenCount = 0 ;
7872 }
7973
8074 // Start new chunk
@@ -85,7 +79,7 @@ function chunkDiffByFiles(diffContent) {
8579 if ( line . startsWith ( '+++' ) ) {
8680 currentFile = line . replace ( '+++ b/' , '' ) . replace ( '+++ a/' , '' ) ;
8781 }
88- if ( estimateTokens ( currentChunk ) > MAX_TOKENS_PER_REQUEST ) {
82+ if ( tokenCount > MAX_TOKENS_PER_REQUEST ) {
8983 const split_chunk = splitStringByTokens ( currentChunk , MAX_TOKENS_PER_REQUEST ) ;
9084 currentChunk = split_chunk [ split_chunk . length - 1 ] ;
9185 for ( let i = 0 ; i < split_chunk . length - 1 ; i ++ ) {
0 commit comments