# ================================================================ # ural_code_engine.py — MASTER CODE ENGINE v3.0 # Nâng cấp toàn diện để đạt Claude-level programming # # 12 CƠ CHẾ CỐT LÕI: # [1] Semantic Code Detector — nhận biết request thông minh # [2] Intent Decomposer — hiểu YÊU CẦU THẬT trước khi code # [3] Pre-Code Reasoning Engine — suy nghĩ trước, code sau # [4] Language & Framework Aware — context đúng ngôn ngữ/framework # [5] Anti-Hallucination Guard — không bịa API, không bịa syntax # [6] Self-Verification Layer — tự tìm bug trước khi trả lời # [7] Completeness Enforcer — không TODO, không placeholder # [8] Algorithm Master — biết thuật toán tối ưu cho bài toán # [9] Complexity Analyzer — phân tích Big-O trước khi code # [10] Architecture Advisor — design pattern đúng context # [11] Debug Pattern Matcher — nhận diện lỗi phổ biến # [12] Problem Decomposer — chia bài khó thành bước nhỏ # ================================================================ import os, sys, re, time, random, threading from typing import Optional # ══════════════════════════════════════════════════════════════════ # [1] SEMANTIC CODE DETECTOR # ══════════════════════════════════════════════════════════════════ def is_code_request(text: str) -> bool: t = text.lower().strip() if "```" in text or re.search(r"\bdef \w+\(|\bclass \w+[:(]|\bfunction\s+\w+\(", text): return True _action = r"(viết|tạo|làm|build|code|implement|xây dựng|thiết kế|lập trình|tối ưu|fix|sửa|debug|refactor)" _obj = r"(hàm|function|class|api|endpoint|bot|script|tool|app|website|game|module|plugin|component|server|database|query|thuật toán|algorithm|code|chương trình|ứng dụng)" if re.search(f"{_action}.{{0,30}}{_obj}", t) or re.search(f"{_obj}.{{0,20}}{_action}", t): return True if re.search(r"(debug|fix|sửa|repair).{0,20}(lỗi|bug|error|code|này|đây)", t): return True _langs = r"(python|javascript|typescript|html|css|sql|java|kotlin|swift|go|rust|php|c\+\+|c#|bash|shell|react|vue|angular|flutter|dart|nodejs|fastapi|django|flask)" _acts = r"(viết|code|làm|build|tạo|sử dụng|dùng|cài|deploy|chạy|run|debug|fix|test|implement)" if re.search(f"{_langs}.{{0,20}}{_acts}", t) or re.search(f"{_acts}.{{0,20}}{_langs}", t): return True if re.search(r"(error|exception|traceback|syntax error|runtime error|import error|typeerror|valueerror|keyerror|indexerror|attributeerror|nameerror)", t): return True if re.search(r"(dockerfile|requirements\.txt|package\.json|setup\.py|\.env|config\.yaml|ci/cd|github action|pipeline)", t): return True if re.search(r"(tối ưu|optimize|refactor|cải thiện performance|nhanh hơn|giảm độ phức tạp).{0,30}(code|hàm|thuật toán|query|loop)", t): return True if re.search(r"(sort|search|tree|graph|dp|dynamic.programming|recursion|memoization|backtracking|binary.search|hash|heap|trie|queue|stack|linked.list)", t): return True if re.search(r"(thuật toán|algorithm|data structure|cấu trúc dữ liệu|độ phức tạp|big.?o|time.complexity|space.complexity)", t): return True return False # ══════════════════════════════════════════════════════════════════ # [2] LANGUAGE DETECTOR + FRAMEWORK AWARE # ══════════════════════════════════════════════════════════════════ def detect_code_language(text: str) -> str: t = text.lower() lang_map = [ ("TypeScript", [r"typescript", r"\.tsx?", r"\bts\b", r"type\s+\w+\s*=", r"interface\s+\w+", r"generic", r""]), ("React/JSX", [r"react", r"jsx", r"usestate", r"useeffect", r"usememo", r"component", r"props\.", r"useState", r"redux", r"zustand"]), ("Vue.js", [r"vue", r"v-model", r"v-for", r"v-if", r"