add the code
Browse files- code_router.py +103 -0
code_router.py
CHANGED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from schemas import CodeTaskType
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
GENERATE_PATTERNS = [
|
| 6 |
+
r"\bcreate\b",
|
| 7 |
+
r"\bgenerate\b",
|
| 8 |
+
r"\bbuild\b",
|
| 9 |
+
r"\bwrite\b",
|
| 10 |
+
r"\bmake\b",
|
| 11 |
+
r"\bdevelop\b",
|
| 12 |
+
r"\bimplement\b",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
FIX_PATTERNS = [
|
| 16 |
+
r"\bfix\b",
|
| 17 |
+
r"\bsolve\b",
|
| 18 |
+
r"\bcorrect\b",
|
| 19 |
+
r"\brepair\b",
|
| 20 |
+
r"\bnot working\b",
|
| 21 |
+
r"\bissue\b",
|
| 22 |
+
r"\bbug\b",
|
| 23 |
+
r"\berror\b",
|
| 24 |
+
r"\bexception\b",
|
| 25 |
+
r"\bcrash\b",
|
| 26 |
+
r"\bfailed\b",
|
| 27 |
+
r"\bproblem\b",
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
EXPLAIN_PATTERNS = [
|
| 31 |
+
r"\bexplain\b",
|
| 32 |
+
r"\bwhat does this do\b",
|
| 33 |
+
r"\bhow does this work\b",
|
| 34 |
+
r"\bdescribe\b",
|
| 35 |
+
r"\bmeaning\b",
|
| 36 |
+
r"\bunderstand\b",
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
REFACTOR_PATTERNS = [
|
| 40 |
+
r"\brefactor\b",
|
| 41 |
+
r"\bclean\b",
|
| 42 |
+
r"\bimprove\b",
|
| 43 |
+
r"\boptimize\b",
|
| 44 |
+
r"\bbetter\b",
|
| 45 |
+
r"\bmake this cleaner\b",
|
| 46 |
+
r"\bmake this better\b",
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
REVIEW_PATTERNS = [
|
| 50 |
+
r"\breview\b",
|
| 51 |
+
r"\bcheck this code\b",
|
| 52 |
+
r"\baudit\b",
|
| 53 |
+
r"\binspect\b",
|
| 54 |
+
r"\bcode review\b",
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def normalize_text(text: str) -> str:
|
| 59 |
+
text = text or ""
|
| 60 |
+
text = text.strip().lower()
|
| 61 |
+
text = re.sub(r"\s+", " ", text)
|
| 62 |
+
return text
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def contains_pattern(text: str, patterns: list[str]) -> bool:
|
| 66 |
+
return any(re.search(pattern, text) for pattern in patterns)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def detect_task_type(
|
| 70 |
+
message: str,
|
| 71 |
+
code: str | None = None,
|
| 72 |
+
error_message: str | None = None,
|
| 73 |
+
mode_hint: CodeTaskType | None = None,
|
| 74 |
+
) -> CodeTaskType:
|
| 75 |
+
if mode_hint and mode_hint != CodeTaskType.UNKNOWN:
|
| 76 |
+
return mode_hint
|
| 77 |
+
|
| 78 |
+
normalized_message = normalize_text(message)
|
| 79 |
+
has_code = bool(code and code.strip())
|
| 80 |
+
has_error = bool(error_message and error_message.strip())
|
| 81 |
+
|
| 82 |
+
if has_error:
|
| 83 |
+
return CodeTaskType.FIX
|
| 84 |
+
|
| 85 |
+
if contains_pattern(normalized_message, FIX_PATTERNS):
|
| 86 |
+
return CodeTaskType.FIX
|
| 87 |
+
|
| 88 |
+
if contains_pattern(normalized_message, EXPLAIN_PATTERNS):
|
| 89 |
+
return CodeTaskType.EXPLAIN
|
| 90 |
+
|
| 91 |
+
if contains_pattern(normalized_message, REFACTOR_PATTERNS):
|
| 92 |
+
return CodeTaskType.REFACTOR
|
| 93 |
+
|
| 94 |
+
if contains_pattern(normalized_message, REVIEW_PATTERNS):
|
| 95 |
+
return CodeTaskType.REVIEW
|
| 96 |
+
|
| 97 |
+
if contains_pattern(normalized_message, GENERATE_PATTERNS):
|
| 98 |
+
return CodeTaskType.GENERATE
|
| 99 |
+
|
| 100 |
+
if has_code and not has_error:
|
| 101 |
+
return CodeTaskType.EXPLAIN
|
| 102 |
+
|
| 103 |
+
return CodeTaskType.UNKNOWN
|