Spaces:
Runtime error
Runtime error
File size: 2,616 Bytes
63bcd5a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | # src/recommendation_engine/command_handler.py
import re
# =====================================================
# Command Detection (Improved)
# =====================================================
def is_command(text: str) -> bool:
"""
Detect if input is a system/terminal command
"""
if not text:
return False
text = text.strip().lower()
# =========================================
# Exact commands
# =========================================
if text in {"exit", "quit", "clear"}:
return True
# =========================================
# Command-like patterns (regex)
# =========================================
command_patterns = [
r"^python\s+\S+\.py", # python file.py
r"^pip\s+install", # pip install
r"^npm\s+install", # npm install
r"^node\s+\S+", # node file.js
r"^cd\s+.+", # cd folder
r"^ls\b", # ls
r"^dir\b", # dir
r"^git\s+.+", # git command
r"^sudo\s+.+", # sudo command
r".+\.py\s*$", # ends with file.py (ONLY if standalone)
]
for pattern in command_patterns:
if re.match(pattern, text):
return True
# =========================================
# Inline execution attempts
# =========================================
if "run" in text and re.search(r"\.py\b", text):
return True
return False
# =====================================================
# Command Handling
# =====================================================
def handle_command(text: str) -> str:
"""
Return safe response for command-like inputs
"""
text = text.lower().strip()
# exit commands
if text in {"exit", "quit"}:
return "👋 Session ended. Start a new chat anytime."
# python execution
if re.search(r"python\s+\S+\.py", text):
return (
"⚠️ This looks like a code execution command.\n"
"I only help with graduation project ideas and development."
)
# package installation
if "pip install" in text or "npm install" in text:
return (
"⚠️ Installation commands are outside my scope.\n"
"I can help you design your graduation project instead."
)
# generic fallback
return (
"⚠️ This looks like a system command.\n"
"Please ask about graduation projects (ideas, features, or system design)."
) |