Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,13 +7,14 @@ import time
|
|
| 7 |
from datetime import datetime
|
| 8 |
import logging
|
| 9 |
import re
|
|
|
|
| 10 |
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
app = FastAPI(title="Windows AI Controller - الخادم المركزي الحقيقي")
|
| 15 |
|
| 16 |
-
# تمكين CORS للسماح بالاتصال من
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
| 19 |
allow_origins=["*"],
|
|
@@ -22,11 +23,18 @@ app.add_middleware(
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# تخزين البيانات
|
| 26 |
pending_commands = {}
|
| 27 |
client_results = {}
|
| 28 |
connected_clients = {}
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
class WindowsCommand(BaseModel):
|
| 31 |
command_type: str
|
| 32 |
target_game: Optional[str] = None
|
|
@@ -60,6 +68,12 @@ class ClientRegistration(BaseModel):
|
|
| 60 |
timestamp: str
|
| 61 |
agent_type: str
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def update_client_activity(client_id: str, activity: str = "chat"):
|
| 64 |
"""تحديث نشاط العميل"""
|
| 65 |
connected_clients[client_id] = {
|
|
@@ -70,15 +84,157 @@ def update_client_activity(client_id: str, activity: str = "chat"):
|
|
| 70 |
}
|
| 71 |
logger.info(f"📊 تم تحديث نشاط العميل {client_id}: {activity}")
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
@app.get("/")
|
| 74 |
async def root():
|
|
|
|
| 75 |
return {
|
| 76 |
"status": "running",
|
| 77 |
-
"service": "Windows AI Controller Server - النسخة الحقيقية",
|
| 78 |
"timestamp": datetime.now().isoformat(),
|
| 79 |
"connected_clients": len(connected_clients),
|
| 80 |
"pending_commands": len(pending_commands),
|
| 81 |
-
"active_clients": list(connected_clients.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
|
| 84 |
@app.post("/api/chat")
|
|
@@ -88,6 +244,13 @@ async def chat_with_ai(request: ChatRequest):
|
|
| 88 |
logger.info(f"💬 طلب محادثة من العميل {request.client_id}: {request.message}")
|
| 89 |
update_client_activity(request.client_id, "chat")
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
user_message = request.message.lower()
|
| 92 |
|
| 93 |
# تحليل الطلب وتحديد الإجراءات الحقيقية
|
|
@@ -107,6 +270,8 @@ async def chat_with_ai(request: ChatRequest):
|
|
| 107 |
return await handle_tool_installation(request)
|
| 108 |
elif "مرحبا" in user_message or "اهلا" in user_message or "hello" in user_message:
|
| 109 |
return await handle_welcome(request)
|
|
|
|
|
|
|
| 110 |
else:
|
| 111 |
return await handle_general_request(request)
|
| 112 |
|
|
@@ -120,14 +285,15 @@ async def handle_scan_programs(request: ChatRequest):
|
|
| 120 |
|
| 121 |
response = ChatResponse(
|
| 122 |
thinking_process=thinking,
|
| 123 |
-
message="🔍 **جاري مسح البرامج المثبتة على جهازك...**\n\nسيقوم الوكيل التنفيذي بمسح شامل لجميع البرامج المثبتة
|
| 124 |
actions=[
|
| 125 |
{
|
| 126 |
"type": "scan_installed_programs",
|
| 127 |
"description": "مسح شامل للبرامج المثبتة على النظام",
|
| 128 |
"parameters": {
|
| 129 |
"scan_type": "comprehensive",
|
| 130 |
-
"include_versions": True
|
|
|
|
| 131 |
}
|
| 132 |
}
|
| 133 |
]
|
|
@@ -140,14 +306,15 @@ async def handle_scan_processes(request: ChatRequest):
|
|
| 140 |
|
| 141 |
response = ChatResponse(
|
| 142 |
thinking_process=thinking,
|
| 143 |
-
message="⚡ **جاري مسح العمليات
|
| 144 |
actions=[
|
| 145 |
{
|
| 146 |
"type": "scan_running_processes",
|
| 147 |
"description": "مسح العمليات النشطة على النظام",
|
| 148 |
"parameters": {
|
| 149 |
"include_memory_usage": True,
|
| 150 |
-
"detect_games": True
|
|
|
|
| 151 |
}
|
| 152 |
}
|
| 153 |
]
|
|
@@ -174,25 +341,30 @@ async def handle_reverse_engineering(request: ChatRequest):
|
|
| 174 |
]
|
| 175 |
)
|
| 176 |
else:
|
|
|
|
|
|
|
|
|
|
| 177 |
response = ChatResponse(
|
| 178 |
thinking_process=thinking + f" تم تحديد المسار: {file_path}. جاري إعداد أدوات الهندسة العكسية.",
|
| 179 |
-
message=f"🔬 **جاري تحضير الهندسة العكسية لـ**: `{file_path}`\n\nسيقوم النظام بتحليل الملف بشكل كامل واستخراج جميع المعلومات
|
| 180 |
actions=[
|
| 181 |
{
|
| 182 |
"type": "reverse_engineer",
|
| 183 |
-
"description": f"هندسة عكسية متقدمة للملف {
|
| 184 |
"parameters": {
|
| 185 |
"file_path": file_path,
|
| 186 |
"analysis_depth": "deep",
|
| 187 |
"extract_resources": True,
|
| 188 |
-
"analyze_imports": True
|
|
|
|
| 189 |
}
|
| 190 |
},
|
| 191 |
{
|
| 192 |
"type": "analyze_game_file",
|
| 193 |
"description": f"تحليل بنية ملف اللعبة",
|
| 194 |
"parameters": {
|
| 195 |
-
"file_path": file_path
|
|
|
|
| 196 |
}
|
| 197 |
}
|
| 198 |
]
|
|
@@ -221,8 +393,8 @@ async def handle_bot_development(request: ChatRequest):
|
|
| 221 |
)
|
| 222 |
else:
|
| 223 |
response = ChatResponse(
|
| 224 |
-
thinking_process=thinking + f" تطوير بوت {bot_type} للعبة {game_name}.",
|
| 225 |
-
message=f"🚀 **بدء تطوير بوت {bot_type} للعبة {game_name}**\n\nجاري:\n• تصميم هيكل البوت\n• إعداد مكتبات الأتمتة\n• تطوير الخوارزميات الأساسية\n• إنشاء كود البوت
|
| 226 |
actions=[
|
| 227 |
{
|
| 228 |
"type": "develop_bot",
|
|
@@ -230,14 +402,16 @@ async def handle_bot_development(request: ChatRequest):
|
|
| 230 |
"parameters": {
|
| 231 |
"game_name": game_name,
|
| 232 |
"bot_type": bot_type,
|
| 233 |
-
"features": ["auto_detection", "memory_control", "image_recognition", "screen_capture"]
|
|
|
|
| 234 |
}
|
| 235 |
},
|
| 236 |
{
|
| 237 |
"type": "install_tool",
|
| 238 |
"description": "تثبيت أدوات تطوير البوت",
|
| 239 |
"parameters": {
|
| 240 |
-
"tool_name": "python"
|
|
|
|
| 241 |
}
|
| 242 |
}
|
| 243 |
]
|
|
@@ -247,17 +421,18 @@ async def handle_bot_development(request: ChatRequest):
|
|
| 247 |
|
| 248 |
async def handle_screenshot(request: ChatRequest):
|
| 249 |
"""معالجة طلب تصوير الشاشة"""
|
| 250 |
-
thinking = "🤔 **التفكير الاستراتيجي**: المستخدم يطلب تصوير شاشة جهازه. إعداد أمر التقاط
|
| 251 |
|
| 252 |
response = ChatResponse(
|
| 253 |
thinking_process=thinking,
|
| 254 |
-
message="📸 **جاري التقاط صورة لشاشة جهازك...**\n\nسيتم حفظ الصورة في مجلد
|
| 255 |
actions=[
|
| 256 |
{
|
| 257 |
"type": "take_screenshot",
|
| 258 |
"description": "التقاط صورة للشاشة الحالية",
|
| 259 |
"parameters": {
|
| 260 |
-
"save_path": f"screenshots/screen_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
|
|
|
|
| 261 |
}
|
| 262 |
}
|
| 263 |
]
|
|
@@ -278,15 +453,16 @@ async def handle_memory_scan(request: ChatRequest):
|
|
| 278 |
)
|
| 279 |
else:
|
| 280 |
response = ChatResponse(
|
| 281 |
-
thinking_process=thinking + f" مسح ذاكرة العملية: {process_name}",
|
| 282 |
-
message=f"🔍 **جاري مسح ذاكرة اللعبة**: {process_name}\n\nالبحث عن القيم القابلة للتعديل مثل المال، الصحة، الذخيرة...",
|
| 283 |
actions=[
|
| 284 |
{
|
| 285 |
"type": "scan_game_memory",
|
| 286 |
"description": f"مسح ذاكرة العملية {process_name}",
|
| 287 |
"parameters": {
|
| 288 |
"process_name": process_name,
|
| 289 |
-
"scan_for": ["money", "health", "ammo", "experience"]
|
|
|
|
| 290 |
}
|
| 291 |
}
|
| 292 |
]
|
|
@@ -308,14 +484,15 @@ async def handle_tool_installation(request: ChatRequest):
|
|
| 308 |
)
|
| 309 |
else:
|
| 310 |
response = ChatResponse(
|
| 311 |
-
thinking_process=thinking + f" تثبيت الأداة: {tool_name}",
|
| 312 |
-
message=f"📥 **جاري تثبيت الأداة**: {tool_name}\n\nسيتم تحضير البيئة اللازمة
|
| 313 |
actions=[
|
| 314 |
{
|
| 315 |
"type": "install_tool",
|
| 316 |
"description": f"تثبيت أداة {tool_name}",
|
| 317 |
"parameters": {
|
| 318 |
-
"tool_name": tool_name
|
|
|
|
| 319 |
}
|
| 320 |
}
|
| 321 |
]
|
|
@@ -325,11 +502,27 @@ async def handle_tool_installation(request: ChatRequest):
|
|
| 325 |
|
| 326 |
async def handle_welcome(request: ChatRequest):
|
| 327 |
"""معالجة رسالة الترحيب"""
|
| 328 |
-
thinking = "🤔 **التفكير الاستراتيجي**: المستخدم يرسل تحية. الترحيب به وعرض الميزات
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
response = ChatResponse(
|
| 331 |
thinking_process=thinking,
|
| 332 |
-
message="🧠 **مرحباً بك في النظام الحقيقي
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
actions=[
|
| 334 |
{
|
| 335 |
"type": "welcome",
|
|
@@ -340,26 +533,74 @@ async def handle_welcome(request: ChatRequest):
|
|
| 340 |
)
|
| 341 |
return response
|
| 342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
async def handle_general_request(request: ChatRequest):
|
| 344 |
"""معالجة الطلبات العامة"""
|
| 345 |
-
thinking = "🤔 **التفكير الاستراتيجي**: تحليل الطلب العام للمستخدم وتحديد أفضل الإجراءات
|
| 346 |
|
| 347 |
response = ChatResponse(
|
| 348 |
thinking_process=thinking,
|
| 349 |
-
message="🧠 **فهمت طلبك!**\n\nسأقوم بمساعدتك في هذه المهمة باستخدام النظام الحقيقي
|
| 350 |
actions=[
|
| 351 |
{
|
| 352 |
"type": "general_analysis",
|
| 353 |
"description": "تحليل المهمة العامة وتحديد المتطلبات",
|
| 354 |
"parameters": {
|
| 355 |
"user_message": request.message,
|
| 356 |
-
"analysis_type": "comprehensive"
|
|
|
|
| 357 |
}
|
| 358 |
}
|
| 359 |
]
|
| 360 |
)
|
| 361 |
return response
|
| 362 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
def extract_file_path(message: str) -> str:
|
| 364 |
"""استخراج مسار الملف من الرسالة"""
|
| 365 |
# البحث عن أنماط المسارات في النص
|
|
@@ -446,6 +687,154 @@ def extract_tool_name(message: str) -> str:
|
|
| 446 |
|
| 447 |
return ""
|
| 448 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
@app.post("/api/register-client")
|
| 450 |
async def register_client(registration: ClientRegistration):
|
| 451 |
"""تسجيل عميل جديد"""
|
|
@@ -460,6 +849,14 @@ async def register_client(registration: ClientRegistration):
|
|
| 460 |
"message_count": 0
|
| 461 |
}
|
| 462 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
logger.info(f"🆕 عميل جديد مسجل: {registration.client_id} - {registration.machine_name}")
|
| 464 |
|
| 465 |
return {
|
|
@@ -533,6 +930,14 @@ async def submit_result(result: ClientResult):
|
|
| 533 |
|
| 534 |
update_client_activity(result.client_id, "result")
|
| 535 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 536 |
logger.info(f"📊 تم استلام نتيجة من العميل {result.client_id}")
|
| 537 |
|
| 538 |
return {"success": True, "message": "تم استلام النتيجة بنجاح"}
|
|
@@ -544,12 +949,15 @@ async def submit_result(result: ClientResult):
|
|
| 544 |
async def client_status(client_id: str):
|
| 545 |
"""الحصول على حالة العميل"""
|
| 546 |
client_data = connected_clients.get(client_id, {})
|
|
|
|
|
|
|
| 547 |
return {
|
| 548 |
"success": True,
|
| 549 |
"client_id": client_id,
|
| 550 |
"status": client_data.get("status", "offline"),
|
| 551 |
"last_seen": client_data.get("last_seen"),
|
| 552 |
"message_count": client_data.get("message_count", 0),
|
|
|
|
| 553 |
"timestamp": datetime.now().isoformat()
|
| 554 |
}
|
| 555 |
|
|
@@ -573,6 +981,8 @@ async def system_status():
|
|
| 573 |
"active_clients": active_clients,
|
| 574 |
"pending_commands": len(pending_commands),
|
| 575 |
"total_results": len(client_results),
|
|
|
|
|
|
|
| 576 |
"server_time": datetime.now().isoformat()
|
| 577 |
}
|
| 578 |
|
|
@@ -581,6 +991,10 @@ async def debug_clients():
|
|
| 581 |
"""تصحيح الأخطاء - عرض جميع العملاء"""
|
| 582 |
return {
|
| 583 |
"connected_clients": connected_clients,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 584 |
"total": len(connected_clients)
|
| 585 |
}
|
| 586 |
|
|
|
|
| 7 |
from datetime import datetime
|
| 8 |
import logging
|
| 9 |
import re
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
logging.basicConfig(level=logging.INFO)
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
app = FastAPI(title="Windows AI Controller - الخادم المركزي الحقيقي")
|
| 16 |
|
| 17 |
+
# تمكين CORS للسماح بالاتصال من تطبيبات Windows
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
| 20 |
allow_origins=["*"],
|
|
|
|
| 23 |
allow_headers=["*"],
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# تخزين البيانات - نظام الذاكرة الكامل
|
| 27 |
pending_commands = {}
|
| 28 |
client_results = {}
|
| 29 |
connected_clients = {}
|
| 30 |
|
| 31 |
+
# نظام الذاكرة الجديد
|
| 32 |
+
client_memories = {} # ذاكرة المحادثات لكل عميل
|
| 33 |
+
game_analysis_memory = {} # ذاكرة تحليل الألعاب
|
| 34 |
+
system_info_memory = {} # ذاكرة معلومات النظام
|
| 35 |
+
installed_programs_memory = {} # ذاكرة البرامج المثبتة
|
| 36 |
+
action_history_memory = {} # سجل الإجراءات
|
| 37 |
+
|
| 38 |
class WindowsCommand(BaseModel):
|
| 39 |
command_type: str
|
| 40 |
target_game: Optional[str] = None
|
|
|
|
| 68 |
timestamp: str
|
| 69 |
agent_type: str
|
| 70 |
|
| 71 |
+
class MemoryEntry(BaseModel):
|
| 72 |
+
timestamp: str
|
| 73 |
+
type: str # conversation, game_analysis, system_info, action_result, installed_programs
|
| 74 |
+
data: Dict[str, Any]
|
| 75 |
+
client_id: str
|
| 76 |
+
|
| 77 |
def update_client_activity(client_id: str, activity: str = "chat"):
|
| 78 |
"""تحديث نشاط العميل"""
|
| 79 |
connected_clients[client_id] = {
|
|
|
|
| 84 |
}
|
| 85 |
logger.info(f"📊 تم تحديث نشاط العميل {client_id}: {activity}")
|
| 86 |
|
| 87 |
+
def save_to_memory(client_id: str, memory_type: str, data: Dict[str, Any]):
|
| 88 |
+
"""حفظ البيانات في الذاكرة"""
|
| 89 |
+
if client_id not in client_memories:
|
| 90 |
+
client_memories[client_id] = []
|
| 91 |
+
|
| 92 |
+
memory_entry = {
|
| 93 |
+
"timestamp": datetime.now().isoformat(),
|
| 94 |
+
"type": memory_type,
|
| 95 |
+
"data": data,
|
| 96 |
+
"client_id": client_id
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
client_memories[client_id].append(memory_entry)
|
| 100 |
+
|
| 101 |
+
# حفظ فقط آخر 1000 إدخال لكل عميل لمنع استهلاك الذاكرة
|
| 102 |
+
if len(client_memories[client_id]) > 1000:
|
| 103 |
+
client_memories[client_id] = client_memories[client_id][-1000:]
|
| 104 |
+
|
| 105 |
+
logger.info(f"💾 تم حفظ بيانات في الذاكرة للعميل {client_id}: {memory_type}")
|
| 106 |
+
|
| 107 |
+
def get_client_memory(client_id: str, memory_type: str = None, limit: int = 10):
|
| 108 |
+
"""استرجاع الذاكرة للعميل"""
|
| 109 |
+
if client_id not in client_memories:
|
| 110 |
+
return []
|
| 111 |
+
|
| 112 |
+
memories = client_memories[client_id]
|
| 113 |
+
|
| 114 |
+
if memory_type:
|
| 115 |
+
memories = [m for m in memories if m["type"] == memory_type]
|
| 116 |
+
|
| 117 |
+
return memories[-limit:]
|
| 118 |
+
|
| 119 |
+
def save_game_analysis(client_id: str, game_name: str, analysis_data: Dict[str, Any]):
|
| 120 |
+
"""حفظ تحليل اللعبة في الذاكرة"""
|
| 121 |
+
if client_id not in game_analysis_memory:
|
| 122 |
+
game_analysis_memory[client_id] = {}
|
| 123 |
+
|
| 124 |
+
if game_name not in game_analysis_memory[client_id]:
|
| 125 |
+
game_analysis_memory[client_id][game_name] = []
|
| 126 |
+
|
| 127 |
+
analysis_entry = {
|
| 128 |
+
"timestamp": datetime.now().isoformat(),
|
| 129 |
+
"analysis_data": analysis_data,
|
| 130 |
+
"client_id": client_id
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
game_analysis_memory[client_id][game_name].append(analysis_entry)
|
| 134 |
+
|
| 135 |
+
# أيضا حفظ في الذاكرة العامة
|
| 136 |
+
save_to_memory(client_id, "game_analysis", {
|
| 137 |
+
"game_name": game_name,
|
| 138 |
+
"analysis_data": analysis_data
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
logger.info(f"🎮 تم حفظ تحليل اللعبة {game_name} للعميل {client_id}")
|
| 142 |
+
|
| 143 |
+
def get_game_analysis(client_id: str, game_name: str = None):
|
| 144 |
+
"""استرجاع تحليل الألعاب"""
|
| 145 |
+
if client_id not in game_analysis_memory:
|
| 146 |
+
return {}
|
| 147 |
+
|
| 148 |
+
if game_name:
|
| 149 |
+
return game_analysis_memory[client_id].get(game_name, [])
|
| 150 |
+
else:
|
| 151 |
+
return game_analysis_memory[client_id]
|
| 152 |
+
|
| 153 |
+
def save_system_info(client_id: str, system_info: Dict[str, Any]):
|
| 154 |
+
"""حفظ معلومات النظام"""
|
| 155 |
+
system_info_memory[client_id] = {
|
| 156 |
+
"timestamp": datetime.now().isoformat(),
|
| 157 |
+
"system_info": system_info
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
# أيضا حفظ في الذاكرة العامة
|
| 161 |
+
save_to_memory(client_id, "system_info", system_info)
|
| 162 |
+
|
| 163 |
+
logger.info(f"💻 تم حفظ معلومات النظام للعميل {client_id}")
|
| 164 |
+
|
| 165 |
+
def get_system_info(client_id: str):
|
| 166 |
+
"""استرجاع معلومات النظام"""
|
| 167 |
+
return system_info_memory.get(client_id, {})
|
| 168 |
+
|
| 169 |
+
def save_installed_programs(client_id: str, programs_data: Dict[str, Any]):
|
| 170 |
+
"""حفظ البرامج المثبتة"""
|
| 171 |
+
installed_programs_memory[client_id] = {
|
| 172 |
+
"timestamp": datetime.now().isoformat(),
|
| 173 |
+
"programs": programs_data
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
# أيضا حفظ في الذاكرة العامة
|
| 177 |
+
save_to_memory(client_id, "installed_programs", programs_data)
|
| 178 |
+
|
| 179 |
+
logger.info(f"📦 تم حفظ بيانات البرامج المثبتة للعميل {client_id}")
|
| 180 |
+
|
| 181 |
+
def get_installed_programs(client_id: str):
|
| 182 |
+
"""استرجاع البرامج المثبتة"""
|
| 183 |
+
return installed_programs_memory.get(client_id, {})
|
| 184 |
+
|
| 185 |
+
def save_action_history(client_id: str, action_data: Dict[str, Any]):
|
| 186 |
+
"""حفظ سجل الإجراءات"""
|
| 187 |
+
if client_id not in action_history_memory:
|
| 188 |
+
action_history_memory[client_id] = []
|
| 189 |
+
|
| 190 |
+
action_entry = {
|
| 191 |
+
"timestamp": datetime.now().isoformat(),
|
| 192 |
+
"action_data": action_data
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
action_history_memory[client_id].append(action_entry)
|
| 196 |
+
|
| 197 |
+
# أيضا حفظ في الذاكرة العامة
|
| 198 |
+
save_to_memory(client_id, "action_history", action_data)
|
| 199 |
+
|
| 200 |
+
logger.info(f"📝 تم حفظ إجراء في السجل للعميل {client_id}: {action_data.get('action_type', 'unknown')}")
|
| 201 |
+
|
| 202 |
+
def get_action_history(client_id: str, limit: int = 20):
|
| 203 |
+
"""استرجاع سجل الإجراءات"""
|
| 204 |
+
if client_id not in action_history_memory:
|
| 205 |
+
return []
|
| 206 |
+
|
| 207 |
+
return action_history_memory[client_id][-limit:]
|
| 208 |
+
|
| 209 |
+
def extract_file_name(file_path: str) -> str:
|
| 210 |
+
"""استخراج اسم الملف من المسار - الإصلاح هنا"""
|
| 211 |
+
try:
|
| 212 |
+
if '/' in file_path:
|
| 213 |
+
return file_path.split('/')[-1]
|
| 214 |
+
elif '\\' in file_path:
|
| 215 |
+
return file_path.split('\\')[-1]
|
| 216 |
+
else:
|
| 217 |
+
return file_path
|
| 218 |
+
except Exception:
|
| 219 |
+
return file_path
|
| 220 |
+
|
| 221 |
@app.get("/")
|
| 222 |
async def root():
|
| 223 |
+
total_memories = sum(len(memories) for memories in client_memories.values())
|
| 224 |
return {
|
| 225 |
"status": "running",
|
| 226 |
+
"service": "Windows AI Controller Server - النسخة الحقيقية مع الذاكرة",
|
| 227 |
"timestamp": datetime.now().isoformat(),
|
| 228 |
"connected_clients": len(connected_clients),
|
| 229 |
"pending_commands": len(pending_commands),
|
| 230 |
+
"active_clients": list(connected_clients.keys()),
|
| 231 |
+
"total_memories": total_memories,
|
| 232 |
+
"memory_stats": {
|
| 233 |
+
"conversations": total_memories,
|
| 234 |
+
"game_analyses": sum(len(analyses) for analyses in game_analysis_memory.values()),
|
| 235 |
+
"system_info": len(system_info_memory),
|
| 236 |
+
"installed_programs": len(installed_programs_memory)
|
| 237 |
+
}
|
| 238 |
}
|
| 239 |
|
| 240 |
@app.post("/api/chat")
|
|
|
|
| 244 |
logger.info(f"💬 طلب محادثة من العميل {request.client_id}: {request.message}")
|
| 245 |
update_client_activity(request.client_id, "chat")
|
| 246 |
|
| 247 |
+
# حفظ المحادثة في الذاكرة
|
| 248 |
+
save_to_memory(request.client_id, "conversation", {
|
| 249 |
+
"user_message": request.message,
|
| 250 |
+
"timestamp": request.timestamp,
|
| 251 |
+
"direction": "incoming"
|
| 252 |
+
})
|
| 253 |
+
|
| 254 |
user_message = request.message.lower()
|
| 255 |
|
| 256 |
# تحليل الطلب وتحديد الإجراءات الحقيقية
|
|
|
|
| 270 |
return await handle_tool_installation(request)
|
| 271 |
elif "مرحبا" in user_message or "اهلا" in user_message or "hello" in user_message:
|
| 272 |
return await handle_welcome(request)
|
| 273 |
+
elif "الذاكرة" in user_message or "memory" in user_message:
|
| 274 |
+
return await handle_memory_query(request)
|
| 275 |
else:
|
| 276 |
return await handle_general_request(request)
|
| 277 |
|
|
|
|
| 285 |
|
| 286 |
response = ChatResponse(
|
| 287 |
thinking_process=thinking,
|
| 288 |
+
message="🔍 **جاري مسح البرامج المثبتة على جهازك...**\n\nسيقوم الوكيل التنفيذي بمسح شامل لجميع البرامج المثبتة وحفظها في الذاكرة الدائمة.",
|
| 289 |
actions=[
|
| 290 |
{
|
| 291 |
"type": "scan_installed_programs",
|
| 292 |
"description": "مسح شامل للبرامج المثبتة على النظام",
|
| 293 |
"parameters": {
|
| 294 |
"scan_type": "comprehensive",
|
| 295 |
+
"include_versions": True,
|
| 296 |
+
"save_to_memory": True
|
| 297 |
}
|
| 298 |
}
|
| 299 |
]
|
|
|
|
| 306 |
|
| 307 |
response = ChatResponse(
|
| 308 |
thinking_process=thinking,
|
| 309 |
+
message="⚡ **جاري مسح العمليات النشطة على جهازك...**\n\nعرض جميع العمليات الجارية حالياً مع التركيز على عمليات الألعاب وحفظها في الذاكرة.",
|
| 310 |
actions=[
|
| 311 |
{
|
| 312 |
"type": "scan_running_processes",
|
| 313 |
"description": "مسح العمليات النشطة على النظام",
|
| 314 |
"parameters": {
|
| 315 |
"include_memory_usage": True,
|
| 316 |
+
"detect_games": True,
|
| 317 |
+
"save_to_memory": True
|
| 318 |
}
|
| 319 |
}
|
| 320 |
]
|
|
|
|
| 341 |
]
|
| 342 |
)
|
| 343 |
else:
|
| 344 |
+
# استخراج اسم الملف فقط للعرض - الإصلاح هنا
|
| 345 |
+
file_name = extract_file_name(file_path)
|
| 346 |
+
|
| 347 |
response = ChatResponse(
|
| 348 |
thinking_process=thinking + f" تم تحديد المسار: {file_path}. جاري إعداد أدوات الهندسة العكسية.",
|
| 349 |
+
message=f"🔬 **جاري تحضير الهندسة العكسية لـ**: `{file_path}`\n\nسيقوم النظام بتحليل الملف بشكل كامل واستخراج جميع المعلومات الممكنة وحفظها في الذاكرة.",
|
| 350 |
actions=[
|
| 351 |
{
|
| 352 |
"type": "reverse_engineer",
|
| 353 |
+
"description": f"هندسة عكسية متقدمة للملف {file_name}",
|
| 354 |
"parameters": {
|
| 355 |
"file_path": file_path,
|
| 356 |
"analysis_depth": "deep",
|
| 357 |
"extract_resources": True,
|
| 358 |
+
"analyze_imports": True,
|
| 359 |
+
"save_to_memory": True
|
| 360 |
}
|
| 361 |
},
|
| 362 |
{
|
| 363 |
"type": "analyze_game_file",
|
| 364 |
"description": f"تحليل بنية ملف اللعبة",
|
| 365 |
"parameters": {
|
| 366 |
+
"file_path": file_path,
|
| 367 |
+
"save_to_memory": True
|
| 368 |
}
|
| 369 |
}
|
| 370 |
]
|
|
|
|
| 393 |
)
|
| 394 |
else:
|
| 395 |
response = ChatResponse(
|
| 396 |
+
thinking_process=thinking + f" تطوير بوت {bot_type} للعبة {game_name}. سيتم حفظ تفاصيل البوت في الذاكرة.",
|
| 397 |
+
message=f"🚀 **بدء تطوير بوت {bot_type} للعبة {game_name}**\n\nجاري:\n• تصميم هيكل البوت\n• إعداد مكتبات الأتمتة\n• تطوير الخوارزميات الأساسية\n• إنشاء كود البوت الكامل\n• حفظ التفاصيل في الذاكرة الدائمة",
|
| 398 |
actions=[
|
| 399 |
{
|
| 400 |
"type": "develop_bot",
|
|
|
|
| 402 |
"parameters": {
|
| 403 |
"game_name": game_name,
|
| 404 |
"bot_type": bot_type,
|
| 405 |
+
"features": ["auto_detection", "memory_control", "image_recognition", "screen_capture"],
|
| 406 |
+
"save_to_memory": True
|
| 407 |
}
|
| 408 |
},
|
| 409 |
{
|
| 410 |
"type": "install_tool",
|
| 411 |
"description": "تثبيت أدوات تطوير البوت",
|
| 412 |
"parameters": {
|
| 413 |
+
"tool_name": "python",
|
| 414 |
+
"save_to_memory": True
|
| 415 |
}
|
| 416 |
}
|
| 417 |
]
|
|
|
|
| 421 |
|
| 422 |
async def handle_screenshot(request: ChatRequest):
|
| 423 |
"""معالجة طلب تصوير الشاشة"""
|
| 424 |
+
thinking = "🤔 **التفكير الاستراتيجي**: المستخدم يطلب تصوير شاشة جهازه. إعداد أمر التقاط الصورة وحفظها في الذاكرة."
|
| 425 |
|
| 426 |
response = ChatResponse(
|
| 427 |
thinking_process=thinking,
|
| 428 |
+
message="📸 **جاري التقاط صورة لشاشة جهازك...**\n\nسيتم حفظ الصورة في مجلد البوتات وتخزين معلوماتها في الذاكرة.",
|
| 429 |
actions=[
|
| 430 |
{
|
| 431 |
"type": "take_screenshot",
|
| 432 |
"description": "التقاط صورة للشاشة الحالية",
|
| 433 |
"parameters": {
|
| 434 |
+
"save_path": f"screenshots/screen_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png",
|
| 435 |
+
"save_to_memory": True
|
| 436 |
}
|
| 437 |
}
|
| 438 |
]
|
|
|
|
| 453 |
)
|
| 454 |
else:
|
| 455 |
response = ChatResponse(
|
| 456 |
+
thinking_process=thinking + f" مسح ذاكرة العملية: {process_name}. سيتم حفظ النتائج في الذاكرة.",
|
| 457 |
+
message=f"🔍 **جاري مسح ذاكرة اللعبة**: {process_name}\n\nالبحث عن القيم القابلة للتعديل مثل المال، الصحة، الذخيرة... وحفظ النتائج في الذاكرة الدائمة.",
|
| 458 |
actions=[
|
| 459 |
{
|
| 460 |
"type": "scan_game_memory",
|
| 461 |
"description": f"مسح ذاكرة العملية {process_name}",
|
| 462 |
"parameters": {
|
| 463 |
"process_name": process_name,
|
| 464 |
+
"scan_for": ["money", "health", "ammo", "experience"],
|
| 465 |
+
"save_to_memory": True
|
| 466 |
}
|
| 467 |
}
|
| 468 |
]
|
|
|
|
| 484 |
)
|
| 485 |
else:
|
| 486 |
response = ChatResponse(
|
| 487 |
+
thinking_process=thinking + f" تثبيت الأداة: {tool_name}. سيتم حفظ حالة التثبيت في الذاكرة.",
|
| 488 |
+
message=f"📥 **جاري تثبيت الأداة**: {tool_name}\n\nسيتم تحضير البيئة اللازمة للعمل وحفظ حالة التثبيت في الذاكرة.",
|
| 489 |
actions=[
|
| 490 |
{
|
| 491 |
"type": "install_tool",
|
| 492 |
"description": f"تثبيت أداة {tool_name}",
|
| 493 |
"parameters": {
|
| 494 |
+
"tool_name": tool_name,
|
| 495 |
+
"save_to_memory": True
|
| 496 |
}
|
| 497 |
}
|
| 498 |
]
|
|
|
|
| 502 |
|
| 503 |
async def handle_welcome(request: ChatRequest):
|
| 504 |
"""معالجة رسالة الترحيب"""
|
| 505 |
+
thinking = "🤔 **التفكير الاستراتيجي**: المستخدم يرسل تحية. الترحيب به وعرض الميزات الحقيقية ونظام الذاكرة."
|
| 506 |
+
|
| 507 |
+
# استرجاع إحصائيات الذاكرة للعميل
|
| 508 |
+
memory_stats = await get_memory_stats_for_client(request.client_id)
|
| 509 |
|
| 510 |
response = ChatResponse(
|
| 511 |
thinking_process=thinking,
|
| 512 |
+
message=f"🧠 **مرحباً بك في النظام الحقيقي المتكامل مع الذاكرة الدائمة!**\n\n"
|
| 513 |
+
f"💾 **حالة ذاكرتك الحالية:**\n"
|
| 514 |
+
f"• المحادثات: {memory_stats['conversations']}\n"
|
| 515 |
+
f"• تحليلات الألعاب: {memory_stats['game_analyses']}\n"
|
| 516 |
+
f"• الإجراءات: {memory_stats['actions']}\n\n"
|
| 517 |
+
f"🚀 **الميزات الحقيقية الجاهزة للتنفيذ على جهازك:**\n\n"
|
| 518 |
+
f"• 🔍 **مسح شامل** للبرامج والعمليات\n"
|
| 519 |
+
f"• 🎮 **تحليل الذاكرة** للألعاب والتعديل\n"
|
| 520 |
+
f"• 🤖 **تطوير بوتات** كاملة وتجربتها\n"
|
| 521 |
+
f"• 🔬 **هندسة عكسية** متقدمة للملفات\n"
|
| 522 |
+
f"• 📸 **تصوير الشاشة** والتسجيل\n"
|
| 523 |
+
f"• ⚡ **تثبيت أدوات** متخصصة\n\n"
|
| 524 |
+
f"💡 **كل شيء يتم حفظه تلقائياً في الذاكرة الدائمة!**\n\n"
|
| 525 |
+
f"ما المهمة التي تريد البدء بها؟",
|
| 526 |
actions=[
|
| 527 |
{
|
| 528 |
"type": "welcome",
|
|
|
|
| 533 |
)
|
| 534 |
return response
|
| 535 |
|
| 536 |
+
async def handle_memory_query(request: ChatRequest):
|
| 537 |
+
"""معالجة استعلامات الذاكرة"""
|
| 538 |
+
thinking = "🤔 **التفكير الاستراتيجي**: المستخدم يطلب معلومات عن الذاكرة. تجهيز إحصائيات الذاكرة."
|
| 539 |
+
|
| 540 |
+
memory_stats = await get_memory_stats_for_client(request.client_id)
|
| 541 |
+
|
| 542 |
+
response = ChatResponse(
|
| 543 |
+
thinking_process=thinking,
|
| 544 |
+
message=f"💾 **نظام الذاكرة الدائمة - إحصائياتك:**\n\n"
|
| 545 |
+
f"📊 **التفاصيل:**\n"
|
| 546 |
+
f"• المحادثات المحفوظة: {memory_stats['conversations']}\n"
|
| 547 |
+
f"• تحليلات الألعاب: {memory_stats['game_analyses']}\n"
|
| 548 |
+
f"• معلومات النظام: {memory_stats['system_info']}\n"
|
| 549 |
+
f"• البرامج المثبتة: {memory_stats['installed_programs']}\n"
|
| 550 |
+
f"• سجل الإجراءات: {memory_stats['actions']}\n\n"
|
| 551 |
+
f"🔍 **آخر 5 محادثات:**\n{memory_stats['recent_conversations']}\n\n"
|
| 552 |
+
f"🎮 **الألعاب التي تم تحليلها:** {memory_stats['games_analyzed']}",
|
| 553 |
+
actions=[]
|
| 554 |
+
)
|
| 555 |
+
return response
|
| 556 |
+
|
| 557 |
async def handle_general_request(request: ChatRequest):
|
| 558 |
"""معالجة الطلبات العامة"""
|
| 559 |
+
thinking = "🤔 **التفكير الاستراتيجي**: تحليل الطلب العام للمستخدم وتحديد أفضل الإجراءات المناسبة مع حفظ النتائج في الذاكرة."
|
| 560 |
|
| 561 |
response = ChatResponse(
|
| 562 |
thinking_process=thinking,
|
| 563 |
+
message="🧠 **فهمت طلبك!**\n\nسأقوم بمساعدتك في هذه المهمة باستخدام النظام الحقيقي المتكامل مع الذاكرة الدائمة.\n\n💡 **جاري تحليل متطلباتك وإعداد الحل المناسب مع حفظ كل التفاصيل في الذاكرة...**",
|
| 564 |
actions=[
|
| 565 |
{
|
| 566 |
"type": "general_analysis",
|
| 567 |
"description": "تحليل المهمة العامة وتحديد المتطلبات",
|
| 568 |
"parameters": {
|
| 569 |
"user_message": request.message,
|
| 570 |
+
"analysis_type": "comprehensive",
|
| 571 |
+
"save_to_memory": True
|
| 572 |
}
|
| 573 |
}
|
| 574 |
]
|
| 575 |
)
|
| 576 |
return response
|
| 577 |
|
| 578 |
+
async def get_memory_stats_for_client(client_id: str):
|
| 579 |
+
"""الحصول على إحصائيات الذاكرة للعميل"""
|
| 580 |
+
conversations = len(get_client_memory(client_id, "conversation"))
|
| 581 |
+
game_analyses = len(get_client_memory(client_id, "game_analysis"))
|
| 582 |
+
system_info = 1 if client_id in system_info_memory else 0
|
| 583 |
+
installed_programs = 1 if client_id in installed_programs_memory else 0
|
| 584 |
+
actions = len(get_action_history(client_id))
|
| 585 |
+
|
| 586 |
+
# آخر 5 محادثات
|
| 587 |
+
recent_convos = get_client_memory(client_id, "conversation", 5)
|
| 588 |
+
recent_text = "\n".join([f"• {conv['data'].get('user_message', '')[:50]}..." for conv in recent_convos])
|
| 589 |
+
|
| 590 |
+
# الألعاب التي تم تحليلها
|
| 591 |
+
games_analyzed = list(get_game_analysis(client_id).keys())
|
| 592 |
+
games_text = ", ".join(games_analyzed) if games_analyzed else "لا توجد"
|
| 593 |
+
|
| 594 |
+
return {
|
| 595 |
+
"conversations": conversations,
|
| 596 |
+
"game_analyses": game_analyses,
|
| 597 |
+
"system_info": system_info,
|
| 598 |
+
"installed_programs": installed_programs,
|
| 599 |
+
"actions": actions,
|
| 600 |
+
"recent_conversations": recent_text,
|
| 601 |
+
"games_analyzed": games_text
|
| 602 |
+
}
|
| 603 |
+
|
| 604 |
def extract_file_path(message: str) -> str:
|
| 605 |
"""استخراج مسار الملف من الرسالة"""
|
| 606 |
# البحث عن أنماط المسارات في النص
|
|
|
|
| 687 |
|
| 688 |
return ""
|
| 689 |
|
| 690 |
+
# نقاط النهاية الجديدة للذاكرة
|
| 691 |
+
@app.get("/api/memory/{client_id}")
|
| 692 |
+
async def get_client_memory_endpoint(client_id: str, memory_type: str = None, limit: int = 10):
|
| 693 |
+
"""الحصول على ذاكرة العميل"""
|
| 694 |
+
try:
|
| 695 |
+
memories = get_client_memory(client_id, memory_type, limit)
|
| 696 |
+
return {
|
| 697 |
+
"success": True,
|
| 698 |
+
"client_id": client_id,
|
| 699 |
+
"memories": memories,
|
| 700 |
+
"total": len(memories)
|
| 701 |
+
}
|
| 702 |
+
except Exception as e:
|
| 703 |
+
logger.error(f"❌ خطأ في استرجاع الذاكرة: {e}")
|
| 704 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 705 |
+
|
| 706 |
+
@app.get("/api/game-analysis/{client_id}")
|
| 707 |
+
async def get_game_analysis_endpoint(client_id: str, game_name: str = None):
|
| 708 |
+
"""الحصول على تحليل الألعاب"""
|
| 709 |
+
try:
|
| 710 |
+
analysis = get_game_analysis(client_id, game_name)
|
| 711 |
+
return {
|
| 712 |
+
"success": True,
|
| 713 |
+
"client_id": client_id,
|
| 714 |
+
"game_analysis": analysis
|
| 715 |
+
}
|
| 716 |
+
except Exception as e:
|
| 717 |
+
logger.error(f"❌ خطأ في استرجاع تحليل الألعاب: {e}")
|
| 718 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 719 |
+
|
| 720 |
+
@app.post("/api/save-system-info/{client_id}")
|
| 721 |
+
async def save_system_info_endpoint(client_id: str, system_info: Dict[str, Any]):
|
| 722 |
+
"""حفظ معلومات النظام"""
|
| 723 |
+
try:
|
| 724 |
+
save_system_info(client_id, system_info)
|
| 725 |
+
return {
|
| 726 |
+
"success": True,
|
| 727 |
+
"message": "تم حفظ معلومات النظام بنجاح"
|
| 728 |
+
}
|
| 729 |
+
except Exception as e:
|
| 730 |
+
logger.error(f"❌ خطأ في حفظ معلومات النظام: {e}")
|
| 731 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 732 |
+
|
| 733 |
+
@app.get("/api/system-info/{client_id}")
|
| 734 |
+
async def get_system_info_endpoint(client_id: str):
|
| 735 |
+
"""الحصول على معلومات النظام"""
|
| 736 |
+
try:
|
| 737 |
+
system_info = get_system_info(client_id)
|
| 738 |
+
return {
|
| 739 |
+
"success": True,
|
| 740 |
+
"client_id": client_id,
|
| 741 |
+
"system_info": system_info
|
| 742 |
+
}
|
| 743 |
+
except Exception as e:
|
| 744 |
+
logger.error(f"❌ خطأ في استرجاع معلومات النظام: {e}")
|
| 745 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 746 |
+
|
| 747 |
+
@app.post("/api/save-installed-programs/{client_id}")
|
| 748 |
+
async def save_installed_programs_endpoint(client_id: str, programs_data: Dict[str, Any]):
|
| 749 |
+
"""حفظ البرامج المثبتة"""
|
| 750 |
+
try:
|
| 751 |
+
save_installed_programs(client_id, programs_data)
|
| 752 |
+
return {
|
| 753 |
+
"success": True,
|
| 754 |
+
"message": "تم حفظ بيانات البرامج المثبتة بنجاح"
|
| 755 |
+
}
|
| 756 |
+
except Exception as e:
|
| 757 |
+
logger.error(f"❌ خطأ في حفظ البرامج المثبتة: {e}")
|
| 758 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 759 |
+
|
| 760 |
+
@app.get("/api/installed-programs/{client_id}")
|
| 761 |
+
async def get_installed_programs_endpoint(client_id: str):
|
| 762 |
+
"""الحصول على البرامج المثبتة"""
|
| 763 |
+
try:
|
| 764 |
+
programs = get_installed_programs(client_id)
|
| 765 |
+
return {
|
| 766 |
+
"success": True,
|
| 767 |
+
"client_id": client_id,
|
| 768 |
+
"installed_programs": programs
|
| 769 |
+
}
|
| 770 |
+
except Exception as e:
|
| 771 |
+
logger.error(f"❌ خطأ في استرجاع البرامج المثبتة: {e}")
|
| 772 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 773 |
+
|
| 774 |
+
@app.post("/api/save-action-result/{client_id}")
|
| 775 |
+
async def save_action_result_endpoint(client_id: str, action_result: Dict[str, Any]):
|
| 776 |
+
"""حفظ نتيجة الإجراء في الذاكرة"""
|
| 777 |
+
try:
|
| 778 |
+
save_action_history(client_id, action_result)
|
| 779 |
+
return {
|
| 780 |
+
"success": True,
|
| 781 |
+
"message": "تم حفظ نتيجة الإجراء بنجاح"
|
| 782 |
+
}
|
| 783 |
+
except Exception as e:
|
| 784 |
+
logger.error(f"❌ خطأ في حفظ نتيجة الإجراء: {e}")
|
| 785 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 786 |
+
|
| 787 |
+
@app.get("/api/action-history/{client_id}")
|
| 788 |
+
async def get_action_history_endpoint(client_id: str, limit: int = 20):
|
| 789 |
+
"""الحصول على سجل الإجراءات"""
|
| 790 |
+
try:
|
| 791 |
+
history = get_action_history(client_id, limit)
|
| 792 |
+
return {
|
| 793 |
+
"success": True,
|
| 794 |
+
"client_id": client_id,
|
| 795 |
+
"action_history": history,
|
| 796 |
+
"total": len(history)
|
| 797 |
+
}
|
| 798 |
+
except Exception as e:
|
| 799 |
+
logger.error(f"❌ خطأ في استرجاع سجل الإجراءات: {e}")
|
| 800 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 801 |
+
|
| 802 |
+
@app.get("/api/memory-stats")
|
| 803 |
+
async def get_memory_stats_endpoint():
|
| 804 |
+
"""إحصائيات الذاكرة"""
|
| 805 |
+
total_memories = sum(len(memories) for memories in client_memories.values())
|
| 806 |
+
total_clients = len(client_memories)
|
| 807 |
+
total_game_analyses = sum(len(analyses) for analyses in game_analysis_memory.values())
|
| 808 |
+
total_system_info = len(system_info_memory)
|
| 809 |
+
total_installed_programs = len(installed_programs_memory)
|
| 810 |
+
|
| 811 |
+
return {
|
| 812 |
+
"success": True,
|
| 813 |
+
"total_memories": total_memories,
|
| 814 |
+
"total_clients": total_clients,
|
| 815 |
+
"total_game_analyses": total_game_analyses,
|
| 816 |
+
"total_system_info": total_system_info,
|
| 817 |
+
"total_installed_programs": total_installed_programs,
|
| 818 |
+
"clients_with_memory": list(client_memories.keys()),
|
| 819 |
+
"timestamp": datetime.now().isoformat()
|
| 820 |
+
}
|
| 821 |
+
|
| 822 |
+
@app.get("/api/client-memory-stats/{client_id}")
|
| 823 |
+
async def get_client_memory_stats_endpoint(client_id: str):
|
| 824 |
+
"""إحصائيات ذاكرة عميل محدد"""
|
| 825 |
+
try:
|
| 826 |
+
stats = await get_memory_stats_for_client(client_id)
|
| 827 |
+
return {
|
| 828 |
+
"success": True,
|
| 829 |
+
"client_id": client_id,
|
| 830 |
+
"memory_stats": stats,
|
| 831 |
+
"timestamp": datetime.now().isoformat()
|
| 832 |
+
}
|
| 833 |
+
except Exception as e:
|
| 834 |
+
logger.error(f"❌ خطأ في إحصائيات الذاكرة: {e}")
|
| 835 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 836 |
+
|
| 837 |
+
# نقاط النهاية القديمة (محفوظة للتوافق)
|
| 838 |
@app.post("/api/register-client")
|
| 839 |
async def register_client(registration: ClientRegistration):
|
| 840 |
"""تسجيل عميل جديد"""
|
|
|
|
| 849 |
"message_count": 0
|
| 850 |
}
|
| 851 |
|
| 852 |
+
# حفظ معلومات النظام الأولية في الذاكرة
|
| 853 |
+
save_system_info(registration.client_id, {
|
| 854 |
+
"machine_name": registration.machine_name,
|
| 855 |
+
"os_version": registration.os_version,
|
| 856 |
+
"agent_type": registration.agent_type,
|
| 857 |
+
"first_seen": registration.timestamp
|
| 858 |
+
})
|
| 859 |
+
|
| 860 |
logger.info(f"🆕 عميل جديد مسجل: {registration.client_id} - {registration.machine_name}")
|
| 861 |
|
| 862 |
return {
|
|
|
|
| 930 |
|
| 931 |
update_client_activity(result.client_id, "result")
|
| 932 |
|
| 933 |
+
# حفظ نتيجة الإجراء في الذاكرة
|
| 934 |
+
save_action_history(result.client_id, {
|
| 935 |
+
"command_id": result.command_id,
|
| 936 |
+
"result": result.result,
|
| 937 |
+
"status": result.status,
|
| 938 |
+
"type": "action_result"
|
| 939 |
+
})
|
| 940 |
+
|
| 941 |
logger.info(f"📊 تم استلام نتيجة من العميل {result.client_id}")
|
| 942 |
|
| 943 |
return {"success": True, "message": "تم استلام النتيجة بنجاح"}
|
|
|
|
| 949 |
async def client_status(client_id: str):
|
| 950 |
"""الحصول على حالة العميل"""
|
| 951 |
client_data = connected_clients.get(client_id, {})
|
| 952 |
+
memory_stats = await get_memory_stats_for_client(client_id)
|
| 953 |
+
|
| 954 |
return {
|
| 955 |
"success": True,
|
| 956 |
"client_id": client_id,
|
| 957 |
"status": client_data.get("status", "offline"),
|
| 958 |
"last_seen": client_data.get("last_seen"),
|
| 959 |
"message_count": client_data.get("message_count", 0),
|
| 960 |
+
"memory_stats": memory_stats,
|
| 961 |
"timestamp": datetime.now().isoformat()
|
| 962 |
}
|
| 963 |
|
|
|
|
| 981 |
"active_clients": active_clients,
|
| 982 |
"pending_commands": len(pending_commands),
|
| 983 |
"total_results": len(client_results),
|
| 984 |
+
"total_memories": sum(len(memories) for memories in client_memories.values()),
|
| 985 |
+
"memory_system": "active",
|
| 986 |
"server_time": datetime.now().isoformat()
|
| 987 |
}
|
| 988 |
|
|
|
|
| 991 |
"""تصحيح الأخطاء - عرض جميع العملاء"""
|
| 992 |
return {
|
| 993 |
"connected_clients": connected_clients,
|
| 994 |
+
"client_memories": {client_id: len(memories) for client_id, memories in client_memories.items()},
|
| 995 |
+
"game_analysis_memory": {client_id: len(analyses) for client_id, analyses in game_analysis_memory.items()},
|
| 996 |
+
"system_info_memory": list(system_info_memory.keys()),
|
| 997 |
+
"installed_programs_memory": list(installed_programs_memory.keys()),
|
| 998 |
"total": len(connected_clients)
|
| 999 |
}
|
| 1000 |
|