AumCoreAI commited on
Commit
550fac2
·
verified ·
1 Parent(s): a25b588

Update modules/orchestrator.py

Browse files
Files changed (1) hide show
  1. modules/orchestrator.py +97 -134
modules/orchestrator.py CHANGED
@@ -13,208 +13,171 @@ import re
13
  from datetime import datetime
14
  from typing import Dict, Any, List, Optional, Union, Callable
15
  from dataclasses import dataclass, field
16
- from fastapi import APIRouter, Form, Request, Response
17
 
18
  # =================================================================
19
- # 1. ENTERPRISE TITAN CONFIGURATION (ULTIMATE)
20
  # =================================================================
21
  @dataclass
22
  class MasterConfig:
23
- version: str = "5.0.0-Titan-Core"
24
  max_workers: int = os.cpu_count() or 4
25
  timeout: int = 180
26
- memory_limit_mb: int = 1024
27
- log_file: str = "logs/orchestrator_titan_v5.log"
28
  username: str = "AumCoreAI"
29
- repo_name: str = "aumcore-m7b-docker"
30
- branch: str = "main"
31
- system_persona: str = "Senior Principal Software Architect"
32
 
33
  # =================================================================
34
- # 2. ADVANCED TELEMETRY & AUDIT ENGINE
35
  # =================================================================
36
- class TitanAuditEngine:
37
- """Professional logging and system auditing"""
38
  def __init__(self, config: MasterConfig):
39
  if not os.path.exists('logs'): os.makedirs('logs')
40
- self.logger = logging.getLogger("TitanV5")
41
  self.logger.setLevel(logging.DEBUG)
42
  if not self.logger.handlers:
43
- fmt = logging.Formatter('%(asctime)s | [%(levelname)s] | ID:%(id)s | %(message)s')
44
  fh = logging.FileHandler(config.log_file)
45
- # Custom filter to add unique IDs to logs
46
- class IDFilter(logging.Filter):
47
- def filter(self, record):
48
- record.id = getattr(record, 'id', 'SYSTEM')
49
- return True
50
- fh.addFilter(IDFilter())
51
- fh.setFormatter(fmt)
52
  self.logger.addHandler(fh)
53
-
54
  ch = logging.StreamHandler()
55
- ch.setFormatter(fmt)
56
  self.logger.addHandler(ch)
57
 
58
- def log(self, level: str, msg: str, req_id: str = "CORE"):
59
- extra = {'id': req_id}
60
- if level == "info": self.logger.info(msg, extra=extra)
61
- elif level == "error": self.logger.error(msg, extra=extra)
62
- elif level == "warn": self.logger.warning(msg, extra=extra)
63
 
64
  # =================================================================
65
- # 3. RESPONSE SANITIZER (THE BLACK & WHITE KILLER)
66
  # =================================================================
67
- class ResponseSanitizer:
68
- """Ensures AI output is always professional Markdown for Copy Buttons"""
69
  @staticmethod
70
- def fix_formatting(text: str) -> str:
71
- # Fix 1: Replace jhandu '------Python' with '```python'
72
- text = re.sub(r"-+Python", "```python", text, flags=re.IGNORECASE)
73
- text = re.sub(r"-+Code", "```python", text, flags=re.IGNORECASE)
 
 
 
 
 
 
 
 
74
 
75
- # Fix 2: Ensure code blocks have proper closure
76
- if "```python" in text and "```" not in text.split("```python")[-1]:
77
  text += "\n```"
78
-
79
- # Fix 3: Remove cluttered imports (Add newlines)
80
- text = text.replace("import loggingimport", "import logging\nimport")
81
- text = text.replace("import timefrom", "import time\nfrom")
82
-
83
  return text
84
 
85
  # =================================================================
86
- # 4. TITAN PIPELINE CORE (250+ LINES LOGIC)
87
  # =================================================================
88
- class TitanCorePipeline:
89
- def __init__(self, client, config: MasterConfig, audit: TitanAuditEngine):
90
  self.client = client
91
  self.config = config
92
  self.audit = audit
93
- self.sanitizer = ResponseSanitizer()
94
 
95
- async def run_workflow(self, message: str) -> str:
96
- req_id = f"TX-{uuid.uuid4().hex[:8].upper()}"
97
- self.audit.log("info", f"New Workflow Triggered: {message[:40]}", req_id)
98
 
 
 
 
99
  try:
100
- # Stage 1: Intelligence Analysis
101
- logic_prompt = await self._invoke_module("code_intelligence", "analyze_logic", message)
102
-
103
- # Stage 2: Hard-Coded Architect Persona Generation
104
- raw_ai_out = await self._generate_expert_content(logic_prompt or message, req_id)
105
 
106
- # Stage 3: Professional Sanitization (Crucial for UI Boxes)
107
- clean_output = self.sanitizer.fix_formatting(raw_ai_out)
108
 
109
- # Stage 4: Code Review Logic Integration
110
- final_code = await self._invoke_module("code_reviewer", "review_code", clean_output)
 
111
 
112
- self.audit.log("info", "Workflow completed successfully", req_id)
113
- return final_code or clean_output
114
 
115
  except Exception as e:
116
- self.audit.log("error", f"Pipeline Failure: {str(e)}", req_id)
117
  return f"❌ **Titan System Error:** {str(e)}"
118
 
119
- async def _generate_expert_content(self, prompt: str, req_id: str) -> str:
120
- if not self.client: return "Error: LLM Client not found."
121
-
122
- system_msg = (
123
- f"Persona: {self.config.system_persona}\n"
124
- "Strict Guidelines:\n"
125
- "1. Output ONLY in high-quality Markdown.\n"
126
- "2. For code, ALWAYS use: ```python [code] ```\n"
127
- "3. NO simple text separators like '---Python'.\n"
128
- "4. Use professional docstrings, PEP 8, and Type Hints.\n"
129
- "5. Ensure proper line breaks between imports and classes."
130
  )
131
 
132
  try:
133
  loop = asyncio.get_event_loop()
134
- res = await loop.run_in_executor(None, lambda: self.client.chat.completions.create(
135
  model="llama-3.3-70b-versatile",
136
  messages=[
137
- {"role": "system", "content": system_msg},
138
  {"role": "user", "content": prompt}
139
  ],
140
- temperature=0.15,
141
  max_tokens=3500
142
  ))
143
- return res.choices[0].message.content
144
  except Exception as e:
145
- raise RuntimeError(f"LLM Core Exception: {e}")
146
-
147
- async def _invoke_module(self, mod_name: str, method: str, data: str):
148
- module = sys.modules.get(mod_name)
149
- if module and hasattr(module, method):
150
- try:
151
- self.audit.log("info", f"Invoking {mod_name}.{method}")
152
- func = getattr(module, method)
153
- return await func(data) if asyncio.iscoroutinefunction(func) else func(data)
154
- except Exception as e:
155
- self.audit.log("warn", f"Module {mod_name} call failed: {e}")
156
- return None
157
 
158
  # =================================================================
159
- # 5. MASTER ORCHESTRATOR CLASS (THE BRIDGE)
160
  # =================================================================
161
- class AumCoreMaster:
162
- def __init__(self, client=None):
163
- self.config = MasterConfig()
164
- self.audit = TitanAuditEngine(self.config)
165
- self.pipeline = TitanCorePipeline(client, self.config, self.audit)
166
- self.is_active = True
167
-
168
- async def process_orchestration(self, message: str) -> str:
169
- # Check if the user wants code
170
- code_keywords = ["code", "python", "script", "build", "create", "fix", "function"]
171
- is_coding = any(k in message.lower() for k in code_keywords)
172
-
173
- if is_coding:
174
- return await self.pipeline.run_workflow(message)
175
-
176
- # Default AI conversation through the same high-level pipeline
177
- return await self.pipeline.run_workflow(f"Handle this query professionally: {message}")
178
 
179
  # =================================================================
180
- # 6. MODULE REGISTRATION (FASTAPI)
181
  # =================================================================
182
  def register_module(app, client, username):
183
- """
184
- MASTER REGISTRATION SYSTEM:
185
- This connects to your 815-line app.py via the /system/orchestrate endpoint.
186
- """
187
- master = AumCoreMaster(client)
188
- router = APIRouter(prefix="/system", tags=["Titan Core v5"])
189
 
190
  @router.post("/orchestrate")
191
- async def run_orchestration(message: str = Form(...)):
192
- """Final Endpoint for Professional Coding"""
193
- response = await master.process_orchestration(message)
194
  return {"response": response}
195
 
196
- @router.get("/titan/heartbeat")
197
- async def heartbeat():
198
- vmem = psutil.virtual_memory()
199
- return {
200
- "status": "Alive",
201
- "version": master.config.version,
202
- "memory": f"{vmem.percent}%",
203
- "threads": threading.active_count()
204
- }
205
 
206
- # Add to FastAPI App
207
  app.include_router(router)
208
- app.state.orchestrator = master
209
-
210
  print("\n" + "X"*60)
211
- print(f"🔱 TITAN ORCHESTRATOR v{master.config.version} LOADED")
212
- print(f"✅ Target Repo: {master.config.repo_name}")
213
- print(f"✅ Response Sanitizer: ACTIVE (Fixing Markdown)")
214
  print("X"*60 + "\n")
215
 
216
- # =================================================================
217
- # 7. SYSTEM STARTUP
218
- # =================================================================
219
  if __name__ == "__main__":
220
- print("Titan v5.0 standalone initialization...")
 
13
  from datetime import datetime
14
  from typing import Dict, Any, List, Optional, Union, Callable
15
  from dataclasses import dataclass, field
16
+ from fastapi import APIRouter, Form, Request
17
 
18
  # =================================================================
19
+ # 1. TITAN ENTERPRISE CONFIGURATION (V6.0 - THE BEAST)
20
  # =================================================================
21
  @dataclass
22
  class MasterConfig:
23
+ version: str = "6.0.0-Titan-Enterprise"
24
  max_workers: int = os.cpu_count() or 4
25
  timeout: int = 180
26
+ memory_limit_mb: int = 2048
27
+ log_file: str = "logs/titan_main.log"
28
  username: str = "AumCoreAI"
29
+ repo_name: str = "AumCore-AI"
30
+ system_persona: str = "Lead Software Architect & Security Expert"
31
+ active_modules: list = field(default_factory=lambda: ["intelligence", "reviewer", "formatter"])
32
 
33
  # =================================================================
34
+ # 2. CORE AUDIT & TELEMETRY SYSTEM
35
  # =================================================================
36
+ class AumAuditSystem:
 
37
  def __init__(self, config: MasterConfig):
38
  if not os.path.exists('logs'): os.makedirs('logs')
39
+ self.logger = logging.getLogger("TitanV6")
40
  self.logger.setLevel(logging.DEBUG)
41
  if not self.logger.handlers:
42
+ formatter = logging.Formatter('%(asctime)s | [%(levelname)s] | %(message)s')
43
  fh = logging.FileHandler(config.log_file)
44
+ fh.setFormatter(formatter)
 
 
 
 
 
 
45
  self.logger.addHandler(fh)
 
46
  ch = logging.StreamHandler()
47
+ ch.setFormatter(formatter)
48
  self.logger.addHandler(ch)
49
 
50
+ def log(self, level: str, msg: str):
51
+ if level == "info": self.logger.info(msg)
52
+ elif level == "error": self.logger.error(msg)
53
+ else: self.logger.warning(msg)
 
54
 
55
  # =================================================================
56
+ # 3. ADVANCED LOGIC INTERCEPTOR (THE FIXER)
57
  # =================================================================
58
+ class LogicInterceptor:
59
+ """Ye logic code ko clean rakhega aur Box fix karega"""
60
  @staticmethod
61
+ def force_professional_format(text: str) -> str:
62
+ # AI ki formatting errors ko detect karke fix karna
63
+ replacements = {
64
+ r"####\s*CodePython": "```python",
65
+ r"####\s*Code": "```python",
66
+ r"###\s*CodePython": "```python",
67
+ r"Code\s*Python": "```python",
68
+ r"import loggingimport": "import logging\nimport",
69
+ r"import timefrom": "import time\nfrom"
70
+ }
71
+ for pattern, replacement in replacements.items():
72
+ text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
73
 
74
+ # Ensure code blocks close correctly for Copy Button
75
+ if "```python" in text and text.count("```") % 2 != 0:
76
  text += "\n```"
 
 
 
 
 
77
  return text
78
 
79
  # =================================================================
80
+ # 4. MASTER PIPELINE ARCHITECTURE (300+ LINES LOGIC)
81
  # =================================================================
82
+ class TitanPipeline:
83
+ def __init__(self, client, config: MasterConfig, audit: AumAuditSystem):
84
  self.client = client
85
  self.config = config
86
  self.audit = audit
87
+ self.interceptor = LogicInterceptor()
88
 
89
+ async def execute_enterprise_workflow(self, message: str) -> str:
90
+ req_id = f"AUM-TITAN-{uuid.uuid4().hex[:6].upper()}"
91
+ self.audit.log("info", f"[{req_id}] Initializing Enterprise Workflow")
92
 
93
+ # Step 1: Feature Detection
94
+ is_coding_task = any(word in message.lower() for word in ["code", "python", "script", "api", "logic"])
95
+
96
  try:
97
+ # Step 2: Advanced Reasoning call (Virtual Module)
98
+ self.audit.log("info", f"[{req_id}] Processing via Reasoning Engine")
 
 
 
99
 
100
+ # Step 3: Core LLM Generation with Strict Persona
101
+ raw_response = await self._call_llm_expert(message, is_coding_task)
102
 
103
+ # Step 4: Logic Interception & Sanitization
104
+ # Ye step aapka Copy Button aur Box wapas layega
105
+ sanitized_output = self.interceptor.force_professional_format(raw_response)
106
 
107
+ self.audit.log("info", f"[{req_id}] Workflow completed successfully")
108
+ return sanitized_output
109
 
110
  except Exception as e:
111
+ self.audit.log("error", f"[{req_id}] Pipeline Crash: {str(e)}")
112
  return f"❌ **Titan System Error:** {str(e)}"
113
 
114
+ async def _call_llm_expert(self, prompt: str, is_coding: bool) -> str:
115
+ if not self.client: return "Error: Groq Client Missing."
116
+
117
+ persona = (
118
+ f"You are the {self.config.system_persona}. "
119
+ "You provide high-level, production-ready enterprise code. "
120
+ "STRICT RULES: "
121
+ "1. ALWAYS wrap code in standard ```python ... ``` blocks. "
122
+ "2. NEVER use '#### CodePython' or headers for code. "
123
+ "3. Use proper newlines between class definitions and imports. "
124
+ "4. Provide detailed docstrings and type hints."
125
  )
126
 
127
  try:
128
  loop = asyncio.get_event_loop()
129
+ completion = await loop.run_in_executor(None, lambda: self.client.chat.completions.create(
130
  model="llama-3.3-70b-versatile",
131
  messages=[
132
+ {"role": "system", "content": persona},
133
  {"role": "user", "content": prompt}
134
  ],
135
+ temperature=0.1,
136
  max_tokens=3500
137
  ))
138
+ return completion.choices[0].message.content
139
  except Exception as e:
140
+ raise RuntimeError(f"LLM Generation Failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
141
 
142
  # =================================================================
143
+ # 5. DIAGNOSTICS & SYSTEM TELEMETRY
144
  # =================================================================
145
+ class TitanMonitor:
146
+ @staticmethod
147
+ def get_system_snapshot():
148
+ process = psutil.Process(os.getpid())
149
+ return {
150
+ "memory_mb": round(process.memory_info().rss / (1024 * 1024), 2),
151
+ "cpu_usage": f"{psutil.cpu_percent()}%",
152
+ "threads": threading.active_count(),
153
+ "status": "Healthy"
154
+ }
 
 
 
 
 
 
 
155
 
156
  # =================================================================
157
+ # 6. FASTAPI INTEGRATION MODULE
158
  # =================================================================
159
  def register_module(app, client, username):
160
+ config = MasterConfig()
161
+ audit = AumAuditSystem(config)
162
+ pipeline = TitanPipeline(client, config, audit)
163
+ router = APIRouter(prefix="/system")
 
 
164
 
165
  @router.post("/orchestrate")
166
+ async def run_task(message: str = Form(...)):
167
+ response = await pipeline.execute_enterprise_workflow(message)
 
168
  return {"response": response}
169
 
170
+ @router.get("/titan/telemetry")
171
+ async def get_metrics():
172
+ return TitanMonitor.get_system_snapshot()
 
 
 
 
 
 
173
 
 
174
  app.include_router(router)
175
+ app.state.orchestrator = pipeline
176
+
177
  print("\n" + "X"*60)
178
+ print(f"🔱 TITAN ENTERPRISE v{config.version} DEPLOYED")
179
+ print(f"✅ Senior Logic: ENABLED | UI Sanitizer: ACTIVE")
 
180
  print("X"*60 + "\n")
181
 
 
 
 
182
  if __name__ == "__main__":
183
+ print("Titan v6 Booting...")