NiviruIns commited on
Commit
1ae0af0
·
verified ·
1 Parent(s): 1a06520

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -17
app.py CHANGED
@@ -23,23 +23,31 @@ except Exception as e:
23
 
24
  def get_smart_fallback(diff_text, filename):
25
  """
26
- If AI fails, look at the code to see WHICH function was touched.
 
27
  """
28
- # Look for function definitions or modifications
29
- # Regex matches: function name(), const name =, class Name
30
- patterns = [
31
- r'function\s+([a-zA-Z0-9_]+)',
32
- r'const\s+([a-zA-Z0-9_]+)\s*=',
33
- r'let\s+([a-zA-Z0-9_]+)\s*=',
34
- r'class\s+([a-zA-Z0-9_]+)',
35
- r'def\s+([a-zA-Z0-9_]+)'
36
- ]
37
-
 
 
 
 
 
 
 
 
38
  for pattern in patterns:
39
  match = re.search(pattern, diff_text)
40
  if match:
41
- func_name = match.group(1)
42
- return f"Refactor '{func_name}' in {filename}"
43
 
44
  return f"Update logic in {filename}"
45
 
@@ -50,8 +58,8 @@ def preprocess_diff(diff_text):
50
  for line in lines:
51
  if (line.startswith('+') or line.startswith('-')):
52
  if line.startswith('+++') or line.startswith('---'): continue
53
- if "import " in line or "require(" in line: continue
54
- if len(line.strip()) < 5: continue
55
  cleaned_lines.append(line.strip())
56
  return "\n".join(cleaned_lines)
57
 
@@ -65,14 +73,13 @@ def sanitize_summary(summary, diff_text, filename):
65
  if match:
66
  ticket = match.group()
67
  if ticket not in diff_text:
68
- print(f"⚠️ Hallucination Killed: '{ticket}' -> Switching to Smart Fallback")
69
  return get_smart_fallback(diff_text, filename)
70
 
71
  # 2. Catch Linguistic Nonsense
72
  forbidden_words = ["transitive verb", "intransitive", "adjective"]
73
  for word in forbidden_words:
74
  if word in summary_clean.lower():
75
- print(f"⚠️ Nonsense Killed: '{word}' -> Switching to Smart Fallback")
76
  return get_smart_fallback(diff_text, filename)
77
 
78
  return summary_clean
 
23
 
24
  def get_smart_fallback(diff_text, filename):
25
  """
26
+ Analyzes the CODE itself to generate a specific message
27
+ when the AI model fails or hallucinates.
28
  """
29
+ # 1. Detect Logging / Debugging
30
+ if "console.log" in diff_text or "outputChannel.append" in diff_text or "print(" in diff_text:
31
+ return f"Add debug logging to {filename}"
32
+
33
+ # 2. Detect Error Handling
34
+ if "try" in diff_text and "catch" in diff_text:
35
+ return f"Improve error handling in {filename}"
36
+
37
+ # 3. Detect Timing / Async Logic
38
+ if "setTimeout" in diff_text or "debounce" in diff_text or "await" in diff_text:
39
+ return f"Update async/debounce logic in {filename}"
40
+
41
+ # 4. Detect Import Changes
42
+ if "import " in diff_text or "require(" in diff_text:
43
+ return f"Update imports in {filename}"
44
+
45
+ # 5. Last Resort: Find the function name
46
+ patterns = [r'function\s+([a-zA-Z0-9_]+)', r'const\s+([a-zA-Z0-9_]+)\s*=']
47
  for pattern in patterns:
48
  match = re.search(pattern, diff_text)
49
  if match:
50
+ return f"Refactor '{match.group(1)}' logic"
 
51
 
52
  return f"Update logic in {filename}"
53
 
 
58
  for line in lines:
59
  if (line.startswith('+') or line.startswith('-')):
60
  if line.startswith('+++') or line.startswith('---'): continue
61
+ # Don't strip imports here, we might need them for context
62
+ if len(line.strip()) < 4: continue
63
  cleaned_lines.append(line.strip())
64
  return "\n".join(cleaned_lines)
65
 
 
73
  if match:
74
  ticket = match.group()
75
  if ticket not in diff_text:
76
+ print(f"⚠️ Hallucination Killed: '{ticket}' -> Using Smart Fallback")
77
  return get_smart_fallback(diff_text, filename)
78
 
79
  # 2. Catch Linguistic Nonsense
80
  forbidden_words = ["transitive verb", "intransitive", "adjective"]
81
  for word in forbidden_words:
82
  if word in summary_clean.lower():
 
83
  return get_smart_fallback(diff_text, filename)
84
 
85
  return summary_clean