@@ -7,9 +7,16 @@ impl SmartReviewPromptBuilder {
77 pub fn build_enhanced_review_prompt (
88 diff : & UnifiedDiff ,
99 context_chunks : & [ LLMContextChunk ] ,
10+ max_context_chars : usize ,
11+ max_diff_chars : usize ,
1012 ) -> Result < ( String , String ) > {
1113 let system_prompt = Self :: build_smart_review_system_prompt ( ) ;
12- let user_prompt = Self :: build_smart_review_user_prompt ( diff, context_chunks) ?;
14+ let user_prompt = Self :: build_smart_review_user_prompt (
15+ diff,
16+ context_chunks,
17+ max_context_chars,
18+ max_diff_chars,
19+ ) ?;
1320
1421 Ok ( ( system_prompt, user_prompt) )
1522 }
@@ -72,10 +79,13 @@ TAGS: [comma-separated relevant tags]
7279 fn build_smart_review_user_prompt (
7380 diff : & UnifiedDiff ,
7481 context_chunks : & [ LLMContextChunk ] ,
82+ max_context_chars : usize ,
83+ max_diff_chars : usize ,
7584 ) -> Result < String > {
7685 let mut prompt = String :: new ( ) ;
7786 let mut context_chars = 0usize ;
78- const MAX_CONTEXT_CHARS : usize = 20000 ;
87+ let mut diff_chars = 0usize ;
88+ let mut diff_truncated = false ;
7989
8090 prompt. push_str ( & format ! (
8191 "Please review the following code changes in file: {}\n \n " ,
@@ -104,7 +114,9 @@ TAGS: [comma-separated relevant tags]
104114 . collect:: <Vec <_>>( )
105115 . join( "\n " )
106116 ) ;
107- if context_chars. saturating_add ( block. len ( ) ) > MAX_CONTEXT_CHARS {
117+ if max_context_chars > 0
118+ && context_chars. saturating_add ( block. len ( ) ) > max_context_chars
119+ {
108120 prompt. push_str ( "[Context truncated]\n \n " ) ;
109121 break ;
110122 }
@@ -117,15 +129,26 @@ TAGS: [comma-separated relevant tags]
117129
118130 // Format the diff with line numbers and change indicators
119131 for hunk in & diff. hunks {
120- prompt . push_str ( & format ! (
132+ let hunk_header = format ! (
121133 "### Hunk: Lines {}-{} (was {}-{})\n \n " ,
122134 hunk. new_start,
123135 hunk. new_start + hunk. new_lines,
124136 hunk. old_start,
125137 hunk. old_start + hunk. old_lines
126- ) ) ;
138+ ) ;
139+ if max_diff_chars > 0 && diff_chars. saturating_add ( hunk_header. len ( ) ) > max_diff_chars {
140+ diff_truncated = true ;
141+ break ;
142+ }
143+ prompt. push_str ( & hunk_header) ;
144+ diff_chars = diff_chars. saturating_add ( hunk_header. len ( ) ) ;
127145
146+ if max_diff_chars > 0 && diff_chars. saturating_add ( "```diff\n " . len ( ) ) > max_diff_chars {
147+ diff_truncated = true ;
148+ break ;
149+ }
128150 prompt. push_str ( "```diff\n " ) ;
151+ diff_chars = diff_chars. saturating_add ( "```diff\n " . len ( ) ) ;
129152 let mut line_num = hunk. new_start ;
130153
131154 for line in & hunk. changes {
@@ -135,7 +158,14 @@ TAGS: [comma-separated relevant tags]
135158 crate :: core:: diff_parser:: ChangeType :: Context => " " ,
136159 } ;
137160
138- prompt. push_str ( & format ! ( "{}{:4} {}\n " , prefix, line_num, line. content) ) ;
161+ let rendered = format ! ( "{}{:4} {}\n " , prefix, line_num, line. content) ;
162+ if max_diff_chars > 0 && diff_chars. saturating_add ( rendered. len ( ) ) > max_diff_chars
163+ {
164+ diff_truncated = true ;
165+ break ;
166+ }
167+ prompt. push_str ( & rendered) ;
168+ diff_chars = diff_chars. saturating_add ( rendered. len ( ) ) ;
139169
140170 if !matches ! (
141171 line. change_type,
@@ -146,6 +176,15 @@ TAGS: [comma-separated relevant tags]
146176 }
147177
148178 prompt. push_str ( "```\n \n " ) ;
179+ diff_chars = diff_chars. saturating_add ( "```\n \n " . len ( ) ) ;
180+
181+ if diff_truncated {
182+ break ;
183+ }
184+ }
185+
186+ if diff_truncated {
187+ prompt. push_str ( "[Diff truncated]\n \n " ) ;
149188 }
150189
151190 prompt. push_str ( "## Review Instructions\n \n " ) ;
0 commit comments