hashan-7 commited on
Commit
6c33fb1
·
verified ·
1 Parent(s): 86be2ba

Update code

Browse files
Files changed (1) hide show
  1. response_formatter.py +89 -12
response_formatter.py CHANGED
@@ -36,7 +36,11 @@ def extract_code_block(text: str) -> Optional[str]:
36
 
37
  def extract_explanation(text: str, task_type: CodeTaskType) -> Optional[str]:
38
  if task_type == CodeTaskType.FIX:
39
- return extract_section(text, "Explanation") or extract_section(text, "Root Cause")
 
 
 
 
40
 
41
  if task_type == CodeTaskType.REVIEW:
42
  review = extract_section(text, "Review")
@@ -122,6 +126,78 @@ def build_review_summary(review_text: str) -> str:
122
  return "Code review completed."
123
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def build_main_answer(
126
  task_type: CodeTaskType,
127
  raw_text: str,
@@ -186,16 +262,6 @@ def build_main_answer(
186
  return "Request processed successfully."
187
 
188
 
189
- def should_include_code_output(task_type: CodeTaskType, code_output: Optional[str]) -> Optional[str]:
190
- if not code_output:
191
- return None
192
-
193
- if task_type == CodeTaskType.EXPLAIN:
194
- return None
195
-
196
- return code_output
197
-
198
-
199
  def build_response(
200
  task_type: CodeTaskType,
201
  model_output: str,
@@ -204,18 +270,29 @@ def build_response(
204
  retrieval_used: bool = False,
205
  source_count: int = 0,
206
  processing_time_ms: Optional[int] = None,
 
207
  ) -> CodeXResponse:
208
  cleaned_output = clean_text(model_output)
209
 
210
  raw_code_output = extract_code_block(cleaned_output)
211
  explanation = extract_explanation(cleaned_output, task_type)
212
- code_output = should_include_code_output(task_type, raw_code_output)
 
 
 
 
213
  answer = build_main_answer(task_type, cleaned_output, explanation, code_output)
214
 
215
  warnings = []
216
  if not cleaned_output:
217
  warnings.append("Model returned an empty response.")
218
 
 
 
 
 
 
 
219
  return CodeXResponse(
220
  task_type=task_type,
221
  answer=answer,
 
36
 
37
  def extract_explanation(text: str, task_type: CodeTaskType) -> Optional[str]:
38
  if task_type == CodeTaskType.FIX:
39
+ root_cause = extract_section(text, "Root Cause")
40
+ explanation = extract_section(text, "Explanation")
41
+ if root_cause and explanation:
42
+ return explanation
43
+ return explanation or root_cause
44
 
45
  if task_type == CodeTaskType.REVIEW:
46
  review = extract_section(text, "Review")
 
126
  return "Code review completed."
127
 
128
 
129
+ def extract_original_callable_name(raw_text: str) -> Optional[str]:
130
+ patterns = [
131
+ r"\bdef\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(",
132
+ r"\bclass\s+([A-Za-z_][A-Za-z0-9_]*)\s*[:(]",
133
+ r"\bfunction\s+([A-Za-z_][A-Za-z0-9_]*)\s*\(",
134
+ ]
135
+ for pattern in patterns:
136
+ match = re.search(pattern, raw_text)
137
+ if match:
138
+ return match.group(1)
139
+ return None
140
+
141
+
142
+ def replace_callable_name(code_output: str, old_name: str, new_name: str) -> str:
143
+ if not code_output or not old_name or not new_name or old_name == new_name:
144
+ return code_output
145
+
146
+ patterns = [
147
+ (rf"(\bdef\s+){re.escape(old_name)}(\s*\()", rf"\1{new_name}\2"),
148
+ (rf"(\bclass\s+){re.escape(old_name)}(\b)", rf"\1{new_name}\2"),
149
+ (rf"(\bfunction\s+){re.escape(old_name)}(\s*\()", rf"\1{new_name}\2"),
150
+ ]
151
+
152
+ updated = code_output
153
+ for pattern, replacement in patterns:
154
+ updated = re.sub(pattern, replacement, updated, count=1)
155
+ return updated
156
+
157
+
158
+ def align_refactor_names_with_input(raw_text: str, code_output: Optional[str]) -> Optional[str]:
159
+ if not code_output:
160
+ return None
161
+
162
+ original_name = extract_original_callable_name(raw_text)
163
+ new_name = extract_original_callable_name(code_output)
164
+
165
+ if original_name and new_name and original_name != new_name:
166
+ return replace_callable_name(code_output, new_name, original_name)
167
+
168
+ return code_output
169
+
170
+
171
+ def should_include_code_output(
172
+ task_type: CodeTaskType,
173
+ code_output: Optional[str],
174
+ explanation: Optional[str],
175
+ ) -> Optional[str]:
176
+ if not code_output:
177
+ return None
178
+
179
+ if task_type == CodeTaskType.EXPLAIN:
180
+ return None
181
+
182
+ if task_type == CodeTaskType.REVIEW:
183
+ if explanation:
184
+ lowered = explanation.lower()
185
+ review_only_signals = [
186
+ "consider",
187
+ "suggest",
188
+ "add a check",
189
+ "type hints",
190
+ "docstring",
191
+ "could improve",
192
+ "readability",
193
+ "maintainability",
194
+ ]
195
+ if any(signal in lowered for signal in review_only_signals):
196
+ return None
197
+
198
+ return code_output
199
+
200
+
201
  def build_main_answer(
202
  task_type: CodeTaskType,
203
  raw_text: str,
 
262
  return "Request processed successfully."
263
 
264
 
 
 
 
 
 
 
 
 
 
 
265
  def build_response(
266
  task_type: CodeTaskType,
267
  model_output: str,
 
270
  retrieval_used: bool = False,
271
  source_count: int = 0,
272
  processing_time_ms: Optional[int] = None,
273
+ original_code: Optional[str] = None,
274
  ) -> CodeXResponse:
275
  cleaned_output = clean_text(model_output)
276
 
277
  raw_code_output = extract_code_block(cleaned_output)
278
  explanation = extract_explanation(cleaned_output, task_type)
279
+
280
+ if task_type == CodeTaskType.REFACTOR and original_code:
281
+ raw_code_output = align_refactor_names_with_input(original_code, raw_code_output)
282
+
283
+ code_output = should_include_code_output(task_type, raw_code_output, explanation)
284
  answer = build_main_answer(task_type, cleaned_output, explanation, code_output)
285
 
286
  warnings = []
287
  if not cleaned_output:
288
  warnings.append("Model returned an empty response.")
289
 
290
+ if task_type == CodeTaskType.REFACTOR and original_code and raw_code_output and code_output:
291
+ original_name = extract_original_callable_name(original_code)
292
+ final_name = extract_original_callable_name(code_output)
293
+ if original_name and final_name and original_name != final_name:
294
+ warnings.append("Refactor output may have changed original naming unexpectedly.")
295
+
296
  return CodeXResponse(
297
  task_type=task_type,
298
  answer=answer,