Spaces:
Sleeping
Sleeping
Update agency_brain.py
Browse files- agency_brain.py +135 -120
agency_brain.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
# ==============================================================================
|
| 2 |
-
# π€ autonomous_agency.py (
|
| 3 |
# ==============================================================================
|
| 4 |
-
#
|
| 5 |
-
# 1.
|
| 6 |
-
# 2.
|
| 7 |
-
# 3.
|
| 8 |
# ==============================================================================
|
| 9 |
|
| 10 |
import os
|
|
@@ -24,7 +24,7 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 24 |
REPO_ID = os.getenv("SPACE_ID")
|
| 25 |
BASE_URL = "https://integrate.api.nvidia.com/v1"
|
| 26 |
|
| 27 |
-
#
|
| 28 |
try:
|
| 29 |
from r2 import R2Service, AGENCY_CASE_FILE_KEY
|
| 30 |
except ImportError:
|
|
@@ -32,103 +32,60 @@ except ImportError:
|
|
| 32 |
AGENCY_CASE_FILE_KEY = "dev_agency_case_file.json"
|
| 33 |
|
| 34 |
# ==============================================================================
|
| 35 |
-
#
|
| 36 |
-
# ==============================================================================
|
| 37 |
-
class MemoryGateway:
|
| 38 |
-
def __init__(self):
|
| 39 |
-
self.client = OpenAI(base_url=BASE_URL, api_key=NVIDIA_API_KEY)
|
| 40 |
-
self.model = "nvidia/llama-3.2-nemoretriever-300m-embed-v2"
|
| 41 |
-
|
| 42 |
-
async def embed_text(self, text: str) -> List[float]:
|
| 43 |
-
try:
|
| 44 |
-
clean_text = text[:2048]
|
| 45 |
-
response = await asyncio.to_thread(
|
| 46 |
-
self.client.embeddings.create,
|
| 47 |
-
input=[clean_text],
|
| 48 |
-
model=self.model,
|
| 49 |
-
encoding_format="float",
|
| 50 |
-
extra_body={"input_type": "query", "truncate": "NONE"}
|
| 51 |
-
)
|
| 52 |
-
return response.data[0].embedding
|
| 53 |
-
except Exception as e:
|
| 54 |
-
print(f"β οΈ [Memory] Embed Error: {e}")
|
| 55 |
-
return []
|
| 56 |
-
|
| 57 |
-
# ==============================================================================
|
| 58 |
-
# π οΈ Hugging Face Operations (The Hands - Upgraded)
|
| 59 |
# ==============================================================================
|
| 60 |
class HuggingFaceOps:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def __init__(self):
|
| 62 |
self.api = HfApi(token=HF_TOKEN)
|
| 63 |
self.repo_id = REPO_ID
|
| 64 |
|
| 65 |
def _sanitize_path(self, path: str) -> str:
|
| 66 |
-
"""Cleans filename from LLM hallucinations
|
| 67 |
clean = path.strip().replace("`", "").replace("'", "").replace('"', "")
|
| 68 |
-
# Remove common prefixes if LLM adds them
|
| 69 |
if clean.startswith("/"): clean = clean[1:]
|
| 70 |
if clean.startswith("./"): clean = clean[2:]
|
| 71 |
return clean
|
| 72 |
|
| 73 |
def list_repo_files(self) -> List[str]:
|
| 74 |
-
"""Lists all
|
| 75 |
if not self.repo_id: return []
|
| 76 |
try:
|
| 77 |
files = self.api.list_repo_files(repo_id=self.repo_id, repo_type="space")
|
| 78 |
-
|
| 79 |
-
return [f for f in files if f.endswith(('.py', '.json'))]
|
| 80 |
except Exception as e:
|
| 81 |
-
print(f"β οΈ [HF Ops] List
|
| 82 |
return []
|
| 83 |
|
| 84 |
def read_file_content(self, file_path: str) -> str:
|
| 85 |
-
"""
|
| 86 |
if not self.repo_id: return "ERROR: No REPO_ID."
|
| 87 |
-
|
| 88 |
target_file = self._sanitize_path(file_path)
|
| 89 |
-
|
| 90 |
-
|
| 91 |
try:
|
| 92 |
-
# Direct
|
| 93 |
-
local_path = hf_hub_download(
|
| 94 |
-
repo_id=self.repo_id,
|
| 95 |
-
filename=target_file,
|
| 96 |
-
repo_type="space",
|
| 97 |
-
token=HF_TOKEN
|
| 98 |
-
)
|
| 99 |
with open(local_path, 'r', encoding='utf-8') as f:
|
| 100 |
return f.read()
|
| 101 |
-
except Exception
|
| 102 |
-
# Smart Retry
|
| 103 |
-
print(f"
|
| 104 |
all_files = self.list_repo_files()
|
| 105 |
-
|
| 106 |
-
# Simple fuzzy match (e.g., matching 'trade_manager.py' to 'ml_engine/trade_manager.py')
|
| 107 |
match = next((f for f in all_files if f.endswith(target_file)), None)
|
| 108 |
-
|
| 109 |
if match:
|
| 110 |
-
print(f" β
Found match: '{match}'. Reading...")
|
| 111 |
try:
|
| 112 |
local_path = hf_hub_download(repo_id=self.repo_id, filename=match, repo_type="space", token=HF_TOKEN)
|
| 113 |
with open(local_path, 'r', encoding='utf-8') as f:
|
| 114 |
return f.read()
|
| 115 |
-
except Exception as e:
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
return f"ERROR_FILE_NOT_FOUND: {target_file} | Detail: {str(first_error)}"
|
| 119 |
|
| 120 |
def update_file(self, file_path: str, content: str, commit_message: str):
|
| 121 |
-
"""
|
| 122 |
target_file = self._sanitize_path(file_path)
|
| 123 |
-
|
| 124 |
-
# Ensure we are not creating a duplicate file if it exists elsewhere
|
| 125 |
-
# (Optional safety: Check if file exists in a subdir and use that path)
|
| 126 |
-
all_files = self.list_repo_files()
|
| 127 |
-
existing_match = next((f for f in all_files if f.endswith(target_file) and f != target_file), None)
|
| 128 |
-
if existing_match:
|
| 129 |
-
print(f" β οΈ Redirecting write from '{target_file}' to existing '{existing_match}'")
|
| 130 |
-
target_file = existing_match
|
| 131 |
-
|
| 132 |
try:
|
| 133 |
print(f"π [HF Ops] Committing to: {target_file}...")
|
| 134 |
self.api.upload_file(
|
|
@@ -138,135 +95,193 @@ class HuggingFaceOps:
|
|
| 138 |
repo_type="space",
|
| 139 |
commit_message=commit_message
|
| 140 |
)
|
| 141 |
-
return {"status": "SUCCESS", "msg": f"File {target_file} updated.
|
| 142 |
except Exception as e:
|
| 143 |
return {"status": "ERROR", "error": str(e)}
|
| 144 |
|
| 145 |
# ==============================================================================
|
| 146 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# ==============================================================================
|
| 148 |
class BaseAgent:
|
| 149 |
-
def __init__(self, name: str, model: str,
|
| 150 |
self.name = name
|
| 151 |
self.model = model
|
|
|
|
| 152 |
self.temperature = temperature
|
| 153 |
-
self.max_tokens = max_tokens
|
| 154 |
-
self.top_p = top_p
|
| 155 |
self.client = OpenAI(base_url=BASE_URL, api_key=NVIDIA_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
def _format_response(self, content: str, reasoning: str = None) -> Dict[str, Any]:
|
| 158 |
-
return {
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
class OrchestratorAgent(BaseAgent):
|
| 166 |
-
def __init__(self):
|
| 167 |
-
super().__init__("Orchestrator", "qwen/qwen3-235b-a22b",
|
| 168 |
|
| 169 |
async def deliberate(self, context: str) -> Dict[str, Any]:
|
|
|
|
|
|
|
| 170 |
try:
|
| 171 |
completion = await asyncio.to_thread(
|
| 172 |
self.client.chat.completions.create,
|
| 173 |
model=self.model,
|
| 174 |
-
messages=[{"role": "user", "content":
|
| 175 |
-
temperature=self.temperature,
|
| 176 |
)
|
| 177 |
return self._format_response(completion.choices[0].message.content)
|
| 178 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 179 |
|
|
|
|
|
|
|
|
|
|
| 180 |
class ArchitectAgent(BaseAgent):
|
| 181 |
-
def __init__(self):
|
| 182 |
-
super().__init__("Architect", "qwen/qwen3-coder-480b-a35b-instruct",
|
| 183 |
-
self.hf_ops = HuggingFaceOps()
|
| 184 |
|
| 185 |
-
async def
|
| 186 |
-
|
| 187 |
-
return "\n".join(files) if files else "NO_FILES_FOUND"
|
| 188 |
-
|
| 189 |
-
async def fetch_code_context(self, filename: str) -> str:
|
| 190 |
-
return self.hf_ops.read_file_content(filename)
|
| 191 |
-
|
| 192 |
-
async def generate_fix(self, task_description: str) -> Dict[str, Any]:
|
| 193 |
-
print(f"ποΈ [{self.name}] Coding via {self.model}...")
|
| 194 |
try:
|
| 195 |
completion = await asyncio.to_thread(
|
| 196 |
self.client.chat.completions.create,
|
| 197 |
model=self.model,
|
| 198 |
-
messages=[{"role": "user", "content":
|
| 199 |
-
temperature=
|
| 200 |
)
|
| 201 |
return self._format_response(completion.choices[0].message.content)
|
| 202 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 203 |
|
|
|
|
| 204 |
def apply_fix_to_repo(self, file_path: str, code: str, reason: str):
|
| 205 |
-
return self.
|
| 206 |
|
|
|
|
|
|
|
|
|
|
| 207 |
class QuantAgent(BaseAgent):
|
| 208 |
-
def __init__(self):
|
| 209 |
-
super().__init__("Quant", "nvidia/llama-3.1-nemotron-ultra-253b-v1",
|
| 210 |
|
| 211 |
async def analyze_logs(self, logs_data: str) -> Dict[str, Any]:
|
|
|
|
| 212 |
try:
|
| 213 |
completion = await asyncio.to_thread(
|
| 214 |
self.client.chat.completions.create,
|
| 215 |
model=self.model,
|
| 216 |
-
messages=[
|
| 217 |
-
|
| 218 |
-
{"role": "user", "content": logs_data}
|
| 219 |
-
],
|
| 220 |
-
temperature=self.temperature, top_p=self.top_p, max_tokens=self.max_tokens, stream=False
|
| 221 |
)
|
| 222 |
return self._format_response(completion.choices[0].message.content)
|
| 223 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 224 |
|
|
|
|
|
|
|
|
|
|
| 225 |
class WatchdogAgent(BaseAgent):
|
| 226 |
-
def __init__(self):
|
| 227 |
-
super().__init__("Watchdog", "moonshotai/kimi-k2-thinking",
|
| 228 |
|
| 229 |
async def audit_decision(self, context: str) -> Dict[str, Any]:
|
|
|
|
| 230 |
try:
|
| 231 |
completion = await asyncio.to_thread(
|
| 232 |
self.client.chat.completions.create,
|
| 233 |
model=self.model,
|
| 234 |
-
messages=[{"role": "user", "content":
|
| 235 |
-
temperature=
|
| 236 |
)
|
| 237 |
return self._format_response(completion.choices[0].message.content)
|
| 238 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 239 |
|
|
|
|
|
|
|
|
|
|
| 240 |
class TradingGeneralAgent(BaseAgent):
|
| 241 |
-
def __init__(self):
|
| 242 |
-
super().__init__("TradingGeneral", "deepseek-ai/deepseek-v3.2",
|
| 243 |
|
| 244 |
async def evaluate_market(self, data: str) -> Dict[str, Any]:
|
|
|
|
| 245 |
try:
|
| 246 |
completion = await asyncio.to_thread(
|
| 247 |
self.client.chat.completions.create,
|
| 248 |
model=self.model,
|
| 249 |
-
messages=[{"role": "user", "content":
|
| 250 |
-
temperature=
|
| 251 |
)
|
| 252 |
return self._format_response(completion.choices[0].message.content)
|
| 253 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 254 |
|
| 255 |
# ==============================================================================
|
| 256 |
-
# π’ Agency Container
|
| 257 |
# ==============================================================================
|
| 258 |
class AutonomousAgency:
|
| 259 |
def __init__(self, r2_service: Optional[R2Service] = None):
|
| 260 |
self.r2 = r2_service
|
| 261 |
self.memory = MemoryGateway()
|
| 262 |
|
| 263 |
-
|
| 264 |
-
self.
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
print("ποΈ [Agency
|
| 270 |
|
| 271 |
async def get_case_file(self):
|
| 272 |
if self.r2: return await self.r2.get_agency_case_file_async()
|
|
|
|
| 1 |
# ==============================================================================
|
| 2 |
+
# π€ autonomous_agency.py (V5.0 - GEM-Architect: The Glass House Protocol)
|
| 3 |
# ==============================================================================
|
| 4 |
+
# Architecture Update:
|
| 5 |
+
# 1. UNIVERSAL VISION: All agents can list and read files/logs.
|
| 6 |
+
# 2. ROLE RESTRICTION: Only Architect can write/modify.
|
| 7 |
+
# 3. TEAM AWARENESS: Agents are context-aware of the Case File & their peers.
|
| 8 |
# ==============================================================================
|
| 9 |
|
| 10 |
import os
|
|
|
|
| 24 |
REPO_ID = os.getenv("SPACE_ID")
|
| 25 |
BASE_URL = "https://integrate.api.nvidia.com/v1"
|
| 26 |
|
| 27 |
+
# R2 Service Import Check
|
| 28 |
try:
|
| 29 |
from r2 import R2Service, AGENCY_CASE_FILE_KEY
|
| 30 |
except ImportError:
|
|
|
|
| 32 |
AGENCY_CASE_FILE_KEY = "dev_agency_case_file.json"
|
| 33 |
|
| 34 |
# ==============================================================================
|
| 35 |
+
# π οΈ Hugging Face Operations (The Shared Eyes)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# ==============================================================================
|
| 37 |
class HuggingFaceOps:
|
| 38 |
+
"""
|
| 39 |
+
Central Operations for File Vision & Manipulation.
|
| 40 |
+
Passed to ALL agents for reading, but restricted for writing.
|
| 41 |
+
"""
|
| 42 |
def __init__(self):
|
| 43 |
self.api = HfApi(token=HF_TOKEN)
|
| 44 |
self.repo_id = REPO_ID
|
| 45 |
|
| 46 |
def _sanitize_path(self, path: str) -> str:
|
| 47 |
+
"""Cleans filename from LLM hallucinations."""
|
| 48 |
clean = path.strip().replace("`", "").replace("'", "").replace('"', "")
|
|
|
|
| 49 |
if clean.startswith("/"): clean = clean[1:]
|
| 50 |
if clean.startswith("./"): clean = clean[2:]
|
| 51 |
return clean
|
| 52 |
|
| 53 |
def list_repo_files(self) -> List[str]:
|
| 54 |
+
"""Lists all relevant files (Code & Logs/JSON)."""
|
| 55 |
if not self.repo_id: return []
|
| 56 |
try:
|
| 57 |
files = self.api.list_repo_files(repo_id=self.repo_id, repo_type="space")
|
| 58 |
+
return [f for f in files if f.endswith(('.py', '.json', '.md'))]
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
print(f"β οΈ [HF Ops] List Error: {e}")
|
| 61 |
return []
|
| 62 |
|
| 63 |
def read_file_content(self, file_path: str) -> str:
|
| 64 |
+
"""Universal Read Capability."""
|
| 65 |
if not self.repo_id: return "ERROR: No REPO_ID."
|
|
|
|
| 66 |
target_file = self._sanitize_path(file_path)
|
| 67 |
+
|
|
|
|
| 68 |
try:
|
| 69 |
+
# Try Direct Read
|
| 70 |
+
local_path = hf_hub_download(repo_id=self.repo_id, filename=target_file, repo_type="space", token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
with open(local_path, 'r', encoding='utf-8') as f:
|
| 72 |
return f.read()
|
| 73 |
+
except Exception:
|
| 74 |
+
# Smart Retry (Fuzzy Match)
|
| 75 |
+
print(f" π [Vision] Fuzzy searching for '{target_file}'...")
|
| 76 |
all_files = self.list_repo_files()
|
|
|
|
|
|
|
| 77 |
match = next((f for f in all_files if f.endswith(target_file)), None)
|
|
|
|
| 78 |
if match:
|
|
|
|
| 79 |
try:
|
| 80 |
local_path = hf_hub_download(repo_id=self.repo_id, filename=match, repo_type="space", token=HF_TOKEN)
|
| 81 |
with open(local_path, 'r', encoding='utf-8') as f:
|
| 82 |
return f.read()
|
| 83 |
+
except Exception as e: return f"ERROR_READING_MATCH: {str(e)}"
|
| 84 |
+
return f"ERROR_FILE_NOT_FOUND: {target_file}"
|
|
|
|
|
|
|
| 85 |
|
| 86 |
def update_file(self, file_path: str, content: str, commit_message: str):
|
| 87 |
+
"""Restricted Write Capability (Architect Only)."""
|
| 88 |
target_file = self._sanitize_path(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
try:
|
| 90 |
print(f"π [HF Ops] Committing to: {target_file}...")
|
| 91 |
self.api.upload_file(
|
|
|
|
| 95 |
repo_type="space",
|
| 96 |
commit_message=commit_message
|
| 97 |
)
|
| 98 |
+
return {"status": "SUCCESS", "msg": f"File {target_file} updated."}
|
| 99 |
except Exception as e:
|
| 100 |
return {"status": "ERROR", "error": str(e)}
|
| 101 |
|
| 102 |
# ==============================================================================
|
| 103 |
+
# π§ Memory Gateway
|
| 104 |
+
# ==============================================================================
|
| 105 |
+
class MemoryGateway:
|
| 106 |
+
def __init__(self):
|
| 107 |
+
self.client = OpenAI(base_url=BASE_URL, api_key=NVIDIA_API_KEY)
|
| 108 |
+
self.model = "nvidia/llama-3.2-nemoretriever-300m-embed-v2"
|
| 109 |
+
|
| 110 |
+
async def embed_text(self, text: str) -> List[float]:
|
| 111 |
+
try:
|
| 112 |
+
response = await asyncio.to_thread(
|
| 113 |
+
self.client.embeddings.create,
|
| 114 |
+
input=[text[:2048]],
|
| 115 |
+
model=self.model,
|
| 116 |
+
encoding_format="float",
|
| 117 |
+
extra_body={"input_type": "query", "truncate": "NONE"}
|
| 118 |
+
)
|
| 119 |
+
return response.data[0].embedding
|
| 120 |
+
except: return []
|
| 121 |
+
|
| 122 |
+
# ==============================================================================
|
| 123 |
+
# 𧬠Base Agent (The Glass House Standard)
|
| 124 |
# ==============================================================================
|
| 125 |
class BaseAgent:
|
| 126 |
+
def __init__(self, name: str, model: str, ops: HuggingFaceOps, temperature: float = 0.3):
|
| 127 |
self.name = name
|
| 128 |
self.model = model
|
| 129 |
+
self.ops = ops # Universal Vision
|
| 130 |
self.temperature = temperature
|
|
|
|
|
|
|
| 131 |
self.client = OpenAI(base_url=BASE_URL, api_key=NVIDIA_API_KEY)
|
| 132 |
+
|
| 133 |
+
# π The Constitution (Rules of Engagement)
|
| 134 |
+
self.system_instruction = f"""
|
| 135 |
+
You are {name}, an advanced autonomous AI agent part of the 'Titan Sovereign' team.
|
| 136 |
+
|
| 137 |
+
[TEAM ROLES & BOUNDARIES]
|
| 138 |
+
1. DIRECTOR: Makes final decisions and routes tasks.
|
| 139 |
+
2. TRADER: Analyzes market & strategy. REQUESTS changes, never codes.
|
| 140 |
+
3. QUANT: Analyzes data logs & stats. Diagnoses issues.
|
| 141 |
+
4. ARCHITECT: The ONLY agent allowed to write/modify code.
|
| 142 |
+
5. WATCHDOG: Audits safety and risks.
|
| 143 |
+
|
| 144 |
+
[YOUR CAPABILITIES]
|
| 145 |
+
- You can SEE the entire file structure.
|
| 146 |
+
- You can READ any file or log to inform your decision.
|
| 147 |
+
- You CANNOT modify code directly (unless you are the Architect).
|
| 148 |
+
|
| 149 |
+
[PROTOCOL]
|
| 150 |
+
- If you see a flaw, analyze it using the files you see.
|
| 151 |
+
- Submit a detailed request to the DIRECTOR specifying WHAT to change and WHY.
|
| 152 |
+
- Do not hallucinate files; use your vision tools to verify existence.
|
| 153 |
+
"""
|
| 154 |
|
| 155 |
def _format_response(self, content: str, reasoning: str = None) -> Dict[str, Any]:
|
| 156 |
+
return {"agent": self.name, "response": content, "reasoning": reasoning}
|
| 157 |
+
|
| 158 |
+
# ποΈ Universal Vision Methods exposed to all agents
|
| 159 |
+
async def see_file_structure(self) -> str:
|
| 160 |
+
files = self.ops.list_repo_files()
|
| 161 |
+
return "\n".join(files) if files else "NO_FILES_VISIBLE"
|
| 162 |
|
| 163 |
+
async def read_specific_file(self, filename: str) -> str:
|
| 164 |
+
return self.ops.read_file_content(filename)
|
| 165 |
+
|
| 166 |
+
# ==============================================================================
|
| 167 |
+
# 1. The Orchestrator (Executive Director)
|
| 168 |
+
# ==============================================================================
|
| 169 |
class OrchestratorAgent(BaseAgent):
|
| 170 |
+
def __init__(self, ops: HuggingFaceOps):
|
| 171 |
+
super().__init__("Orchestrator", "qwen/qwen3-235b-a22b", ops)
|
| 172 |
|
| 173 |
async def deliberate(self, context: str) -> Dict[str, Any]:
|
| 174 |
+
# Inject Constitution into the prompt flow
|
| 175 |
+
full_context = f"{self.system_instruction}\n\n[CURRENT TASK]\n{context}"
|
| 176 |
try:
|
| 177 |
completion = await asyncio.to_thread(
|
| 178 |
self.client.chat.completions.create,
|
| 179 |
model=self.model,
|
| 180 |
+
messages=[{"role": "user", "content": full_context}],
|
| 181 |
+
temperature=self.temperature, stream=False
|
| 182 |
)
|
| 183 |
return self._format_response(completion.choices[0].message.content)
|
| 184 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 185 |
|
| 186 |
+
# ==============================================================================
|
| 187 |
+
# 2. The Architect (The Coder - Only one with write access)
|
| 188 |
+
# ==============================================================================
|
| 189 |
class ArchitectAgent(BaseAgent):
|
| 190 |
+
def __init__(self, ops: HuggingFaceOps):
|
| 191 |
+
super().__init__("Architect", "qwen/qwen3-coder-480b-a35b-instruct", ops, temperature=0.0)
|
|
|
|
| 192 |
|
| 193 |
+
async def generate_fix(self, task: str) -> Dict[str, Any]:
|
| 194 |
+
full_context = f"{self.system_instruction}\n\n[CODING TASK]\n{task}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
try:
|
| 196 |
completion = await asyncio.to_thread(
|
| 197 |
self.client.chat.completions.create,
|
| 198 |
model=self.model,
|
| 199 |
+
messages=[{"role": "user", "content": full_context}],
|
| 200 |
+
temperature=0.0, stream=False
|
| 201 |
)
|
| 202 |
return self._format_response(completion.choices[0].message.content)
|
| 203 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 204 |
|
| 205 |
+
# βοΈ Exclusive Write Capability
|
| 206 |
def apply_fix_to_repo(self, file_path: str, code: str, reason: str):
|
| 207 |
+
return self.ops.update_file(file_path, code, reason)
|
| 208 |
|
| 209 |
+
# ==============================================================================
|
| 210 |
+
# 3. The Quant (The Analyst)
|
| 211 |
+
# ==============================================================================
|
| 212 |
class QuantAgent(BaseAgent):
|
| 213 |
+
def __init__(self, ops: HuggingFaceOps):
|
| 214 |
+
super().__init__("Quant", "nvidia/llama-3.1-nemotron-ultra-253b-v1", ops)
|
| 215 |
|
| 216 |
async def analyze_logs(self, logs_data: str) -> Dict[str, Any]:
|
| 217 |
+
full_context = f"{self.system_instruction}\n\n[ANALYSIS TASK]\n{logs_data}"
|
| 218 |
try:
|
| 219 |
completion = await asyncio.to_thread(
|
| 220 |
self.client.chat.completions.create,
|
| 221 |
model=self.model,
|
| 222 |
+
messages=[{"role": "user", "content": full_context}],
|
| 223 |
+
temperature=0.3, stream=False
|
|
|
|
|
|
|
|
|
|
| 224 |
)
|
| 225 |
return self._format_response(completion.choices[0].message.content)
|
| 226 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 227 |
|
| 228 |
+
# ==============================================================================
|
| 229 |
+
# 4. The Watchdog (The Auditor)
|
| 230 |
+
# ==============================================================================
|
| 231 |
class WatchdogAgent(BaseAgent):
|
| 232 |
+
def __init__(self, ops: HuggingFaceOps):
|
| 233 |
+
super().__init__("Watchdog", "moonshotai/kimi-k2-thinking", ops, temperature=0.0)
|
| 234 |
|
| 235 |
async def audit_decision(self, context: str) -> Dict[str, Any]:
|
| 236 |
+
full_context = f"{self.system_instruction}\n\n[AUDIT TASK]\n{context}"
|
| 237 |
try:
|
| 238 |
completion = await asyncio.to_thread(
|
| 239 |
self.client.chat.completions.create,
|
| 240 |
model=self.model,
|
| 241 |
+
messages=[{"role": "user", "content": full_context}],
|
| 242 |
+
temperature=0.0, stream=False
|
| 243 |
)
|
| 244 |
return self._format_response(completion.choices[0].message.content)
|
| 245 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 246 |
|
| 247 |
+
# ==============================================================================
|
| 248 |
+
# 5. The Trader (The Strategist)
|
| 249 |
+
# ==============================================================================
|
| 250 |
class TradingGeneralAgent(BaseAgent):
|
| 251 |
+
def __init__(self, ops: HuggingFaceOps):
|
| 252 |
+
super().__init__("TradingGeneral", "deepseek-ai/deepseek-v3.2", ops, temperature=0.4)
|
| 253 |
|
| 254 |
async def evaluate_market(self, data: str) -> Dict[str, Any]:
|
| 255 |
+
full_context = f"{self.system_instruction}\n\n[STRATEGY TASK]\n{data}"
|
| 256 |
try:
|
| 257 |
completion = await asyncio.to_thread(
|
| 258 |
self.client.chat.completions.create,
|
| 259 |
model=self.model,
|
| 260 |
+
messages=[{"role": "user", "content": full_context}],
|
| 261 |
+
temperature=0.4, stream=False
|
| 262 |
)
|
| 263 |
return self._format_response(completion.choices[0].message.content)
|
| 264 |
except Exception as e: return {"status": "ERROR", "error": str(e)}
|
| 265 |
|
| 266 |
# ==============================================================================
|
| 267 |
+
# π’ The Agency Container
|
| 268 |
# ==============================================================================
|
| 269 |
class AutonomousAgency:
|
| 270 |
def __init__(self, r2_service: Optional[R2Service] = None):
|
| 271 |
self.r2 = r2_service
|
| 272 |
self.memory = MemoryGateway()
|
| 273 |
|
| 274 |
+
# ποΈ Initialize Shared Vision
|
| 275 |
+
self.shared_ops = HuggingFaceOps()
|
| 276 |
+
|
| 277 |
+
# π€ Initialize Agents with Universal Vision
|
| 278 |
+
self.orchestrator = OrchestratorAgent(self.shared_ops)
|
| 279 |
+
self.architect = ArchitectAgent(self.shared_ops)
|
| 280 |
+
self.quant = QuantAgent(self.shared_ops)
|
| 281 |
+
self.watchdog = WatchdogAgent(self.shared_ops)
|
| 282 |
+
self.trader = TradingGeneralAgent(self.shared_ops)
|
| 283 |
|
| 284 |
+
print("ποΈ [Agency V5.0] Glass House Protocol Active. All Agents have Vision.")
|
| 285 |
|
| 286 |
async def get_case_file(self):
|
| 287 |
if self.r2: return await self.r2.get_agency_case_file_async()
|