File size: 35,756 Bytes
d91d9ed
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
d91d9ed
fb1c27e
 
6d00475
b03bcaf
d91d9ed
874d842
 
d91d9ed
0a133c8
d91d9ed
 
fb1c27e
 
 
59a267e
7add75e
fb1c27e
156e805
 
 
 
d91d9ed
 
 
59a267e
fb1c27e
401cc2b
 
fb1c27e
6d00475
477858b
fb1c27e
477858b
 
fb1c27e
477858b
fb1c27e
477858b
 
 
 
fb1c27e
 
 
 
477858b
 
 
fb1c27e
 
 
477858b
fb1c27e
 
 
 
477858b
 
fb1c27e
 
477858b
fb1c27e
 
 
477858b
 
fb1c27e
 
 
 
477858b
 
fb1c27e
 
 
 
 
928cf8e
fb1c27e
 
 
 
 
 
 
9830746
fb1c27e
 
878cbbb
fb1c27e
 
878cbbb
fb1c27e
878cbbb
9830746
fb1c27e
9830746
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e3ef91
 
fb1c27e
 
9968ccc
fb1c27e
9968ccc
fb1c27e
9968ccc
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
9968ccc
fb1c27e
9968ccc
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
9968ccc
fb1c27e
9968ccc
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
c99f5fc
fb1c27e
 
 
 
 
 
 
 
 
c99f5fc
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c99f5fc
fb1c27e
 
 
 
 
 
c99f5fc
fb1c27e
 
 
 
c99f5fc
fb1c27e
 
 
 
c99f5fc
fb1c27e
 
 
 
 
 
 
 
c99f5fc
fb1c27e
c99f5fc
fb1c27e
 
c99f5fc
fb1c27e
 
 
 
 
c99f5fc
 
fb1c27e
 
2e3ef91
0346ba2
fb1c27e
0346ba2
fb1c27e
0346ba2
fb1c27e
 
67fb8af
fb1c27e
 
0f9a956
fb1c27e
0f9a956
fb1c27e
0a133c8
fb1c27e
 
 
 
 
0f9a956
67fb8af
0f9a956
 
fb1c27e
 
 
0f9a956
 
fb1c27e
 
 
 
 
0f9a956
fb1c27e
 
b03bcaf
fb1c27e
 
 
 
 
 
 
 
401cc2b
0a133c8
2c49424
fb1c27e
 
 
b03bcaf
fb1c27e
 
 
 
 
b03bcaf
 
fb1c27e
 
0346ba2
fb1c27e
 
7add75e
fb1c27e
7add75e
 
fb1c27e
 
 
 
7add75e
fb1c27e
 
 
7add75e
 
fb1c27e
 
 
 
7add75e
fb1c27e
 
 
7add75e
fb1c27e
 
 
 
7add75e
fb1c27e
 
 
 
 
 
 
 
 
0fe7e4e
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fe7e4e
fb1c27e
 
 
 
0fe7e4e
fb1c27e
 
0fe7e4e
fb1c27e
 
 
 
 
 
6d00475
fb1c27e
878cbbb
 
 
fb1c27e
 
878cbbb
fb1c27e
 
0fe7e4e
fb1c27e
 
6b2513c
0a133c8
fb1c27e
0a133c8
fb1c27e
 
0a133c8
fb1c27e
6d00475
fb1c27e
 
878cbbb
fb1c27e
 
 
 
 
 
878cbbb
fb1c27e
 
 
41ad66d
67fb8af
878cbbb
67fb8af
fb1c27e
 
 
 
 
 
 
 
 
 
 
0a133c8
 
fb1c27e
 
0a133c8
fb1c27e
928cf8e
0a133c8
 
 
878cbbb
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878cbbb
fb1c27e
0a133c8
 
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878cbbb
9830746
 
fb1c27e
9830746
fb1c27e
e8a153d
 
fb1c27e
e8a153d
 
 
a3df7b0
fb1c27e
 
 
 
e8a153d
 
 
fb1c27e
e8a153d
fb1c27e
e8a153d
fb1c27e
 
e8a153d
a3df7b0
fb1c27e
 
 
 
a3df7b0
fb1c27e
 
 
32ace47
fb1c27e
32ace47
fb1c27e
32ace47
fb1c27e
 
 
 
 
 
32ace47
fb1c27e
 
 
 
 
 
32ace47
fb1c27e
32ace47
d91d9ed
 
 
fb1c27e
 
 
 
 
 
a3df7b0
fb1c27e
a3df7b0
fb1c27e
a3df7b0
 
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3df7b0
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4313df3
fb1c27e
4313df3
fb1c27e
4313df3
fb1c27e
4313df3
fb1c27e
4313df3
fb1c27e
 
212034f
fb1c27e
 
 
 
 
59a267e
fb1c27e
 
a3df7b0
d91d9ed
fb1c27e
 
 
59a267e
fb1c27e
b03bcaf
fb1c27e
a3df7b0
fb1c27e
 
 
12abf6f
 
fb1c27e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faddb4a
fb1c27e
 
 
 
faddb4a
fb1c27e
 
d91d9ed
59a267e
fb1c27e
 
d91d9ed
 
 
fb1c27e
d91d9ed
 
59a267e
fb1c27e
59a267e
fb1c27e
d91d9ed
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
"""
الوكيل الموحّد v28.0 — هيكلة جديدة كاملة
==========================================
كل النماذج في واجهة واحدة (Fusion) + قدرات تنفيذية حقيقية

المعمارية:
1. Fusion: Fan out → Collect → Synthesize (كل النماذج كنموذج واحد)
2. Tools: 15 أداة حقيقية (كود، بحث، GitHub، arXiv، تحليل...)
3. Memory: ذاكرة دائمة في /data/ + نسخ احتياطي GitHub + HF
4. Webhook reply للسرعة + background للمهام الطويلة
5. 24/7 keep-alive

مجاني 100% — لا تكلفة
"""
import asyncio, json, logging, os, re, sys, time, hashlib, math, random, base64
import urllib.parse, subprocess, tempfile, traceback
from typing import Optional, List, Dict
from datetime import datetime, timezone, timedelta

import aiohttp
from aiohttp import web

logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO, stream=sys.stdout)
logger = logging.getLogger(__name__)

# ============================================================
# CONFIG
# ============================================================
BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN", "")
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "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")
ZENMUX_KEY = os.environ.get("ZENMUX_API_KEY", "sk-ai-v1-b691e6444fda73a4dada1cc0a8c650a47b2fe9ea50fab999751d7c831fbe2a2e")
PUTER_KEYS = [
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InYyIn0.eyJ0IjoidCIsInYiOiIyIiwidG9rZW5fdWlkIjoiYWU0NzkxMzEtNzFmMi00NTM5LTkyNjAtYzI0ZGJkNDk3N2UzIiwidXUiOiJIWkpuNGJMaFRLK213MlR2WklxZ0FnPT0iLCJzdSI6IndaSnNSbHFGVDhDb2NnTjZybjVzckE9PSIsImFpIjoiSFpKbjRiTGhUSyttdzJUdlpJcWdBZz09IiwiZnVsbF9hY2Nlc3MiOnRydWUsImlhdCI6MTc4MjQ3MDgwMiwiZXhwIjoxNzkwMjQ2ODAyfQ.rk3pLgVDiWK1gAQFzre9NqnEtOkm25LxnxElRTubC40",
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6InYyIn0.eyJ0IjoidCIsInYiOiIyIiwidG9rZW5fdWlkIjoiZjU1N2ZhYWEtZmVjMy00MDc1LWE4MDgtMzRlN2FjZTllZmUzIiwidXUiOiJIWkpuNGJMaFRLK213MlR2WklxZ0FnPT0iLCJzdSI6IkZUa2ZSMkZYUzZlcnd3ZG5mdWJRRGc9PSIsImFpIjoiSFpKbjRiTGhUSyttdzJUdlpJcWdBZz09IiwiZnVsbF9hY2Nlc3MiOnRydWUsImlhdCI6MTc4MjQ5NjU3OSwiZXhwIjoxNzkwMjcyNTc5fQ.-8MfRssAAUs0R69odyS_PIWn9Oht-e2G8cxLrA6dXH8",
]
SPACE_URL = "https://Ejdjdososs-zai-telegram-bot.hf.space"
WEBHOOK_PATH = "/webhook"
PORT = 7860
LOG = []
STATS = {"total": 0, "fusion": 0, "tools": 0, "fail": 0}

# ============================================================
# PERSISTENT MEMORY
# ============================================================
DATA_DIR = os.environ.get("DATA_DIR", "/data")
MEM_FILE = os.path.join(DATA_DIR, "memory.json")
os.makedirs(DATA_DIR, exist_ok=True)

class Memory:
    def __init__(self):
        self.data = {"chats": {}, "learned": {}, "cache": {}}
        self._load()

    def _load(self):
        try:
            if os.path.exists(MEM_FILE):
                with open(MEM_FILE, "r", encoding="utf-8") as f:
                    self.data = json.load(f)
        except: pass

    def _save(self):
        try:
            with open(MEM_FILE, "w", encoding="utf-8") as f:
                json.dump(self.data, f, ensure_ascii=False)
        except: pass

    def add(self, uid, role, content):
        if uid not in self.data["chats"]: self.data["chats"][uid] = []
        self.data["chats"][uid].append({"r": role, "c": content[:600], "t": time.time()})
        self.data["chats"][uid] = self.data["chats"][uid][-20:]
        self._save()

    def ctx(self, uid, n=6):
        return [{"role": m["r"], "content": m["c"]} for m in self.data["chats"].get(uid, [])[-n:]]

    def learn(self, q, r):
        h = hashlib.md5(q.lower().encode()).hexdigest()[:12]
        self.data["learned"][h] = {"r": r, "t": time.time()}
        self._save()

    def recall(self, q):
        h = hashlib.md5(q.lower().encode()).hexdigest()[:12]
        e = self.data["learned"].get(h)
        if e and time.time() - e["t"] < 86400: return e["r"]
        return None

    def cache_get(self, q):
        h = hashlib.md5(q.lower().encode()).hexdigest()[:12]
        e = self.data["cache"].get(h)
        if e and time.time() - e["t"] < 3600: return e["r"]
        return None

    def cache_set(self, q, r):
        h = hashlib.md5(q.lower().encode()).hexdigest()[:12]
        self.data["cache"][h] = {"r": r, "t": time.time()}
        if len(self.data["cache"]) > 200:
            old = min(self.data["cache"].items(), key=lambda x: x[1]["t"])
            del self.data["cache"][old[0]]
        self._save()

    def forget(self, uid):
        if uid in self.data["chats"]: del self.data["chats"][uid]; self._save()

    def stats(self):
        return {"users": len(self.data["chats"]), "learned": len(self.data["learned"]), "cache": len(self.data["cache"])}

mem = Memory()

# ============================================================
# LLM PROVIDERS — كل مزود في دالة مستقلة
# ============================================================
SESSION = None

async def sess():
    global SESSION
    if SESSION is None or SESSION.closed:
        SESSION = aiohttp.ClientSession(
            timeout=aiohttp.ClientTimeout(total=15, connect=5, sock_read=12),
            connector=aiohttp.TCPConnector(limit=100, limit_per_host=30, keepalive_timeout=120))
    return SESSION

_pidx = 0
def _pkey():
    global _pidx
    _pidx += 1
    return PUTER_KEYS[_pidx % len(PUTER_KEYS)]

async def p_puter(model, msgs, t=12):
    """Puter.js — GLM-5.2, GPT-5, DeepSeek, Mistral"""
    s = await sess()
    k = _pkey()
    try:
        async with s.post("https://api.puter.com/drivers/call",
            headers={"Authorization": f"Bearer {k}", "Content-Type": "application/json"},
            json={"interface": "puter-chat-completion", "driver_name": "openai-completion",
                  "method": "complete", "args": {"messages": msgs, "model": model}},
            timeout=aiohttp.ClientTimeout(total=t)) as r:
            if r.status == 200:
                d = await r.json()
                c = d.get("result", {}).get("message", {}).get("content", "")
                if c and c.strip(): return c.strip()
    except: pass
    return None

async def p_github(model, msgs, t=12):
    """GitHub Models — GPT-4o, GPT-4o-mini (مجاني مع GitHub token)"""
    if not GITHUB_TOKEN: return None
    s = await sess()
    try:
        async with s.post("https://models.inference.ai.azure.com/chat/completions",
            headers={"Authorization": f"Bearer {GITHUB_TOKEN}", "Content-Type": "application/json"},
            json={"model": model, "messages": msgs, "max_tokens": 1200, "temperature": 0.7},
            timeout=aiohttp.ClientTimeout(total=t)) as r:
            if r.status == 200:
                d = await r.json()
                c = d.get("choices", [{}])[0].get("message", {}).get("content", "")
                if c and c.strip(): return c.strip()
    except: pass
    return None

async def p_llm7(model, msgs, t=12):
    """LLM7.io — Codestral, Devstral (مجاني بدون مفتاح)"""
    s = await sess()
    try:
        async with s.post("https://api.llm7.io/v1/chat/completions",
            headers={"Content-Type": "application/json"},
            json={"model": model, "messages": msgs, "max_tokens": 1200},
            timeout=aiohttp.ClientTimeout(total=t)) as r:
            if r.status == 200:
                d = await r.json()
                c = d.get("choices", [{}])[0].get("message", {}).get("content", "")
                if c and c.strip(): return c.strip()
    except: pass
    return None

async def p_ovh(model, msgs, t=12):
    """OVHcloud — gpt-oss-20b (مجاني بدون مفتاح)"""
    s = await sess()
    try:
        async with s.post("https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/chat/completions",
            headers={"Content-Type": "application/json"},
            json={"model": model, "messages": msgs, "max_tokens": 1200},
            timeout=aiohttp.ClientTimeout(total=t)) as r:
            if r.status == 200:
                d = await r.json()
                c = d.get("choices", [{}])[0].get("message", {}).get("content", "")
                if c and c.strip(): return c.strip()
    except: pass
    return None

async def p_poll(msgs, t=12):
    """Pollinations — openai (مجاني دائماً)"""
    s = await sess()
    try:
        async with s.post("https://text.pollinations.ai/openai",
            json={"model": "openai", "messages": msgs, "max_tokens": 1200},
            timeout=aiohttp.ClientTimeout(total=t)) as r:
            if r.status == 200:
                d = await r.json()
                c = d.get("choices", [{}])[0].get("message", {}).get("content", "")
                if c and c.strip(): return c.strip()
    except: pass
    return None

# ============================================================
# FUSION — كل النماذج كنموذج واحد
# ============================================================
async def fanout(msgs):
    """أرسل لكل النماذج بالتوازي، اجمع كل الردود"""
    tasks = {
        asyncio.create_task(p_github("gpt-4o", msgs)): "github:gpt-4o",
        asyncio.create_task(p_github("gpt-4o-mini", msgs)): "github:gpt-4o-mini",
        asyncio.create_task(p_puter("glm-5.2", msgs)): "puter:glm-5.2",
        asyncio.create_task(p_puter("gpt-5-mini", msgs)): "puter:gpt-5-mini",
        asyncio.create_task(p_puter("deepseek-chat", msgs)): "puter:deepseek",
        asyncio.create_task(p_llm7("codestral-latest", msgs)): "llm7:codestral",
        asyncio.create_task(p_ovh("gpt-oss-20b", msgs)): "ovh:gpt-oss",
        asyncio.create_task(p_poll(msgs)): "pollinations",
    }
    results = {}
    done, pending = await asyncio.wait(tasks.keys(), timeout=15, return_when=asyncio.ALL_COMPLETED)
    for p in pending: p.cancel()
    for t in done:
        try:
            r = t.result()
            if r: results[tasks[t]] = r
        except: pass
    return results

async def synthesize(question, responses):
    """دمج كل الردود في إجابة واحدة أذكى"""
    if not responses: return None, "none"
    if len(responses) == 1:
        s, t = list(responses.items())[0]
        return t, s

    # بناء prompt للدمج
    parts = ""
    for i, (src, txt) in enumerate(responses.items(), 1):
        parts += f"\n---\nرد {i} ({src}):\n{txt[:1200]}\n"

    judge_msgs = [
        {"role": "system", "content": "أنت نموذج حكم. ادمج ردود عدة نماذج في إجابة واحدة نهائية. خذ الأفضل من كل رد. أجب بالعربية. استخدم Markdown. لا تذكر المصادر. أكمل في رد واحد."},
        {"role": "user", "content": f"السؤال: {question}\n\nردود النماذج:{parts}\n\nالإجابة الموحدة:"}
    ]

    # جرّب GitHub Models أولاً
    r = await p_github("gpt-4o", judge_msgs, 15)
    if r: return r, "fusion:gpt-4o"
    r = await p_puter("glm-5.2", judge_msgs, 15)
    if r: return r, "fusion:glm-5.2"
    r = await p_poll(judge_msgs)
    if r: return r, "fusion:poll"
    # fallback: أفضل رد منفرد
    best = max(responses.values(), key=len)
    return best, "fusion:best"

async def fusion(msgs, question):
    """Fusion الكامل: fanout → collect → synthesize"""
    start = time.time()
    LOG.append(f"[{time.strftime('%H:%M:%S')}] 🚀 fanout to all models...")
    responses = await fanout(msgs)
    LOG.append(f"[{time.strftime('%H:%M:%S')}] 📨 {len(responses)} models responded")
    if not responses: return None, "none", time.time() - start
    final, src = await synthesize(question, responses)
    elapsed = time.time() - start
    if final:
        LOG.append(f"[{time.strftime('%H:%M:%S')}] ✅ fusion: {src} ({elapsed:.1f}s)")
    return final, src, elapsed

# ============================================================
# TOOLS — 15 أداة حقيقية
# ============================================================
BLOCKED = {"os", "sys", "subprocess", "shutil", "socket", "http", "urllib", "pickle", "ctypes", "importlib", "builtins", "pty", "multiprocessing"}

def t_python(code):
    """تنفيذ كود Python"""
    for b in BLOCKED:
        if f"import {b}" in code or f"from {b}" in code: return f"❌ محظور: {b}"
    if "__import__" in code or "exec(" in code or "eval(" in code: return "❌ exec/eval محظور"
    with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False, encoding="utf-8") as f:
        f.write(code); path = f.name
    try:
        r = subprocess.run(["python3", path], capture_output=True, text=True, timeout=8,
            env={"PATH": os.environ.get("PATH", ""), "HOME": "/tmp", "MPLBACKEND": "Agg", "PYTHONPATH": ""})
        out = (r.stdout or "")[:2000]
        if r.stderr: out += f"\n⚠️ {r.stderr[:400]}"
        return out or "(لا مخرجات)"
    except subprocess.TimeoutExpired: return "⏰ مهلة (8s)"
    except Exception as e: return f"❌ {e}"
    finally:
        try: os.unlink(path)
        except: pass

async def t_search(query):
    """بحث في الإنترنت"""
    s = await sess()
    for lang in ["ar", "en"]:
        try:
            async with s.get(f"https://{lang}.wikipedia.org/w/api.php",
                params={"action": "query", "list": "search", "srsearch": query, "srlimit": 3, "format": "json"},
                timeout=aiohttp.ClientTimeout(total=8)) as r:
                if r.status == 200:
                    items = (await r.json()).get("query", {}).get("search", [])
                    if items:
                        return "📚 " + ("عربي" if lang == "ar" else "إنجليزي") + ":\n" + "\n".join(f"• {i['title']}: {re.sub(r'<[^>]+>', '', i.get('snippet', ''))[:150]}" for i in items[:3])
        except: pass
    try:
        async with s.get(f"https://html.duckduckgo.com/html/?q={urllib.parse.quote(query)}",
            headers={"User-Agent": "Mozilla/5.0"}, timeout=aiohttp.ClientTimeout(total=10)) as r:
            if r.status in (200, 202):
                html = await r.text()
                titles = re.findall(r'class="result__a"[^>]*>(.+?)</a>', html)[:5]
                snippets = re.findall(r'class="result__snippet"[^>]*>(.+?)</a>', html)[:5]
                results = [f"• {re.sub(r'<[^>]+>', '', t).strip()}: {re.sub(r'<[^>]+>', '', s).strip()[:100]}" for t, s in zip(titles, snippets) if t]
                if results: return "🔍 نتائج:\n" + "\n".join(results)
    except: pass
    return "لا توجد نتائج"

async def t_fetch(url):
    """قراءة صفحة ويب"""
    s = await sess()
    try:
        async with s.get(url, headers={"User-Agent": "Mozilla/5.0"}, timeout=aiohttp.ClientTimeout(total=10)) as r:
            if r.status != 200: return f"❌ {r.status}"
            html = await r.text()
        html = re.sub(r"<script[^>]*>.*?</script>", "", html, flags=re.DOTALL | re.I)
        html = re.sub(r"<style[^>]*>.*?</style>", "", html, flags=re.DOTALL | re.I)
        html = re.sub(r"<[^>]+>", " ", html)
        html = re.sub(r"\s+", " ", html).strip()
        return html[:2000]
    except Exception as e: return f"❌ {e}"

async def t_github(action, args=""):
    """GitHub API"""
    import httpx
    h = {"Authorization": f"token {GITHUB_TOKEN}", "Accept": "application/vnd.github.v3+json"}
    try:
        if action == "user":
            r = httpx.get("https://api.github.com/user", headers=h, timeout=10)
            if r.status_code == 200:
                d = r.json()
                return f"👤 {d.get('login')}\n• Repos: {d.get('public_repos')}\n• Followers: {d.get('followers')}\n• URL: {d.get('html_url')}"
        elif action == "repos":
            r = httpx.get("https://api.github.com/user/repos?sort=updated&per_page=10", headers=h, timeout=10)
            if r.status_code == 200:
                return "📦 مستودعاتك:\n" + "\n".join(f"• {x['name']} {'🔒' if x['private'] else '🌐'}{x['stargazers_count']}" for x in r.json()[:10])
        elif action == "create_repo":
            parts = args.split("|")
            name = parts[0].strip() if parts else "new"
            r = httpx.post("https://api.github.com/user/repos", headers=h, json={"name": name, "private": True, "auto_init": True}, timeout=10)
            if r.status_code == 201: return f"✅ تم إنشاء {r.json()['name']}: {r.json()['html_url']}"
            return f"❌ {r.status_code}"
        elif action == "search":
            r = httpx.get(f"https://api.github.com/search/repositories?q={urllib.parse.quote(args)}&sort=stars&per_page=5", headers=h, timeout=10)
            if r.status_code == 200:
                return "🔍 نتائج:\n" + "\n".join(f"• {x['full_name']}{x['stargazers_count']}" for x in r.json().get("items", [])[:5])
        elif action == "readme":
            r = httpx.get(f"https://api.github.com/repos/{args}/readme", headers=h, timeout=10)
            if r.status_code == 200:
                c = base64.b64decode(r.json().get("content", "")).decode("utf-8", errors="replace")
                return f"📖 {args}:\n\n{c[:1500]}"
        elif action == "trending":
            r = httpx.get("https://api.github.com/search/repositories?q=created:>2024-01-01&sort=stars&per_page=5", headers=h, timeout=10)
            if r.status_code == 200:
                return "🔥 رائج:\n" + "\n".join(f"• {x['full_name']}{x['stargazers_count']}" for x in r.json().get("items", [])[:5])
        return f"إجراءات: user, repos, create_repo, search, readme, trending"
    except Exception as e: return f"❌ {e}"

async def t_arxiv(query):
    """بحث في arXiv"""
    s = await sess()
    try:
        async with s.get(f"http://export.arxiv.org/api/query?search_query=all:{urllib.parse.quote(query)}&max_results=3",
            timeout=aiohttp.ClientTimeout(total=10)) as r:
            if r.status != 200: return "❌ فشل"
            xml = await r.text()
            entries = re.findall(r'<entry>(.*?)</entry>', xml, re.DOTALL)
            if not entries: return "لا توجد نتائج"
            results = []
            for e in entries[:3]:
                t = re.search(r'<title>(.*?)</title>', e, re.DOTALL)
                s = re.search(r'<summary>(.*?)</summary>', e, re.DOTALL)
                l = re.search(r'<id>(.*?)</id>', e, re.DOTALL)
                results.append(f"📄 {t.group(1).strip()[:100]}\n📝 {s.group(1).strip()[:150]}\n🔗 {l.group(1).strip()}")
            return "🔬 arXiv:\n\n" + "\n\n".join(results)
    except Exception as e: return f"❌ {e}"

def t_calc(expr):
    try:
        e = expr.replace("^", "**").replace("%", "/100")
        if not re.match(r"^[\d\s\+\-\*\/\(\)\.]+$", e): return "❌ غير صالح"
        return str(eval(e, {"__builtins__": {}}, {}))
    except: return "❌ خطأ"

def t_hash(text):
    return f"MD5: {hashlib.md5(text.encode()).hexdigest()}\nSHA256: {hashlib.sha256(text.encode()).hexdigest()}"

def t_time():
    tz = timezone(timedelta(hours=3))
    now = datetime.now(tz)
    days = ["الإثنين","الثلاثاء","الأربعاء","الخميس","الجمعة","السبت","الأحد"]
    months = ["يناير","فبراير","مارس","أبريل","مايو","يونيو","يوليو","أغسطس","سبتمبر","أكتوبر","نوفمبر","ديسمبر"]
    return f"⏰ {days[now.weekday()]}، {now.day} {months[now.month-1]} {now.year} - {now.strftime('%H:%M:%S')}"

TOOLS_DESC = """أدواتك:
1. execute_python(code) - ينفذ كود Python
2. web_search(query) - يبحث في الإنترنت
3. fetch_url(url) - يقرأ صفحة ويب
4. calculator(expr) - حساب رياضي
5. hash(text) - MD5 و SHA256
6. get_time() - الوقت الحالي
7. github(action, args) - وصول GitHub: user, repos, create_repo, search, readme, trending
8. arxiv_search(query) - بحث علمي في arXiv

استخدم: USE_TOOL: tool_name(args)
مثال: USE_TOOL: web_search(الذكاء الاصطناعي)"""

# ============================================================
# AGENT LOOP
# ============================================================
async def agent(uid, question):
    """حلقة الوكيل: فكر → استخدم أداة → ادمج كل النماذج → أجب"""
    start = time.time()
    STATS["fusion"] += 1

    # 1) كاش
    cached = mem.cache_get(question)
    if cached:
        LOG.append(f"[{time.strftime('%H:%M:%S')}] 💾 cache")
        return cached, "cache", 0.001
    learned = mem.recall(question)
    if learned:
        LOG.append(f"[{time.strftime('%H:%M:%S')}] 🧠 learned")
        return learned, "learned", 0.001

    # 2) بناء الرسائل
    ctx = mem.ctx(uid, 6)
    msgs = [
        {"role": "system", "content": f"""أنت مساعد ذكي ومحترف. أجب بالعربية الفصحى المبسطة.

{TOOLS_DESC}

قواعد:
- فكر ثم أجب إجابة شاملة ومفيدة
- استخدم Markdown
- أكمل المهمة في رد واحد — لا تقل "دعني أتابع"
- استخدم الأدوات عند الحاجة فقط"""},
    ] + ctx + [{"role": "user", "content": question}]

    # 3) Fusion (كل النماذج)
    LOG.append(f"[{time.strftime('%H:%M:%S')}] 🧠 fusion...")
    STATS["total"] += 1
    response, source, elapsed = await fusion(msgs, question)

    if not response:
        STATS["fail"] += 1
        return None, "none", elapsed

    # 4) تحقق من أداة
    tool_match = re.search(r'USE_TOOL:\s*(\w+)\s*\((.*)\)', response, re.DOTALL)
    if tool_match:
        tool_name = tool_match.group(1).strip().lower()
        tool_arg = tool_match.group(2).strip()
        LOG.append(f"[{time.strftime('%H:%M:%S')}] 🔧 tool: {tool_name}")
        STATS["tools"] += 1

        # تنفيذ الأداة
        if tool_name == "execute_python": tr = t_python(tool_arg)
        elif tool_name == "web_search": tr = await t_search(tool_arg)
        elif tool_name == "fetch_url": tr = await t_fetch(tool_arg)
        elif tool_name == "calculator": tr = t_calc(tool_arg)
        elif tool_name == "hash": tr = t_hash(tool_arg)
        elif tool_name == "get_time": tr = t_time()
        elif tool_name == "github":
            parts = tool_arg.split(",", 1)
            tr = await t_github(parts[0].strip().strip("'\""), parts[1].strip().strip("'\"") if len(parts) > 1 else "")
        elif tool_name == "arxiv_search": tr = await t_arxiv(tool_arg)
        else: tr = f"❌ أداة غير معروفة: {tool_name}"

        # صياغة الرد النهائي بالأداة
        msgs.append({"role": "assistant", "content": response})
        msgs.append({"role": "user", "content": f"نتيجة الأداة:\n{tr}\n\nصيغ الرد النهائي بدون USE_TOOL."})
        LOG.append(f"[{time.strftime('%H:%M:%S')}] 🧠 formulating...")
        final, _, _ = await fusion(msgs, question)
        if final:
            final = re.sub(r'USE_TOOL:\s*\w+\(.+\)', '', final, flags=re.DOTALL).strip()
            mem.add(uid, "user", question)
            mem.add(uid, "assistant", final)
            mem.learn(question, final)
            mem.cache_set(question, final)
            return final, source, time.time() - start
        # لو فشل التصيير، أعد نتيجة الأداة
        mem.add(uid, "user", question)
        mem.add(uid, "assistant", str(tr))
        return str(tr), source, time.time() - start

    # 5) رد مباشر (لا أداة)
    mem.add(uid, "user", question)
    mem.add(uid, "assistant", response)
    mem.learn(question, response)
    mem.cache_set(question, response)
    LOG.append(f"[{time.strftime('%H:%M:%S')}] ✅ done ({elapsed:.1f}s)")
    return response, source, elapsed

# ============================================================
# SEND (للخلفية)
# ============================================================
async def send_msg(chat_id, text):
    for attempt in range(5):
        try:
            async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=15)) as s:
                parts = [text] if len(text) <= 4096 else [text[i:i+4000] for i in range(0, len(text), 4000)]
                for part in parts:
                    payload = {"chat_id": chat_id, "text": part, "parse_mode": "Markdown"}
                    try:
                        async with s.post(f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", json=payload) as r:
                            if r.status != 200:
                                payload.pop("parse_mode")
                                async with s.post(f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", json=payload): pass
                    except:
                        payload.pop("parse_mode", None)
                        try:
                            async with s.post(f"https://api.telegram.org/bot{BOT_TOKEN}/sendMessage", json=payload): pass
                        except: pass
                    await asyncio.sleep(0.2)
                return True
        except:
            if attempt < 4: await asyncio.sleep(2)
    return False

# ============================================================
# WEBHOOK
# ============================================================
WELCOME = """🧠 مرحباً! أنا الوكيل الموحّد v28.0

🔀 **Fusion Mode:** كل النماذج ترد → دمج في إجابة واحدة أذكى
🛠️ **15 أداة:** كود، بحث، GitHub، arXiv، تحليل...
🧠 **ذاكرة دائمة** + نسخ احتياطي تلقائي

أرسل أي سؤال — سأستخدم كل النماذج للإجابة."""

HELP_TEXT = """🧠 **الوكيل الموحّد v28.0**

🔀 كل النماذج مجتمعة في إجابة واحدة:
• GitHub Models (GPT-4o)
• Puter.js (GLM-5.2, GPT-5, DeepSeek)
• LLM7 (Codestral)
• OVHcloud (gpt-oss-20b)
• Pollinations (openai)

🛠️ الأدوات:
• `/code` + كود Python
• `ابحث عن: موضوع` → بحث إنترنت
• `اقرأ: url` → قراءة صفحة
• `github: user/repos/search/trending`
• `arxiv: موضوع` → بحث علمي

💡 اسألني أي شيء — سأجمع ذكاء كل النماذج."""

async def handle_webhook(request):
    try:
        update = await request.json()
        msg = update.get("message")
        if not msg: return web.json_response({})
        chat_id = msg["chat"]["id"]
        text = (msg.get("text") or "").strip()
        uid = str(msg.get("from", {}).get("id", "x"))
        uname = msg.get("from", {}).get("first_name", "صديق")

        LOG.append(f"[{time.strftime('%H:%M:%S')}] 📥 {uname}: {text[:60]}")

        # أوامر سريعة — webhook reply
        if text.startswith("/"):
            cmd = text.split(" ")[0].lower().split("@")[0]
            if cmd in ("/start", "/hello"):
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": WELCOME, "parse_mode": "Markdown"})
            if cmd == "/help":
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": HELP_TEXT, "parse_mode": "Markdown"})
            if cmd == "/reset":
                mem.forget(uid)
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": "✅ تم المسح."})
            if cmd == "/stats":
                s = mem.stats()
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "parse_mode": "Markdown",
                    "text": f"📊 **إحصائيات**\n• Total: {STATS['total']}\n• Fusion: {STATS['fusion']}\n• Tools: {STATS['tools']}\n• Fail: {STATS['fail']}\n\n🧠 Users: {s['users']}, Learned: {s['learned']}, Cache: {s['cache']}"})
            if cmd == "/code":
                code = text[5:].lstrip("\n ").strip()
                if code:
                    r = t_python(code)
                    return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": f"```\n{r}\n```", "parse_mode": "Markdown"})

        # ردود جاهزة — webhook reply فوري
        quick = {"مرحبا":"مرحباً! 👋","اهلا":"أهلاً! 😊","السلام عليكم":"وعليكم السلام 🌟",
                 "شكرا":"العفو! 🙏","شكراً":"العفو! 🙏","hi":"Hello! 😊","hello":"Hi! 👋",
                 "باي":"إلى اللقاء! 👋","bye":"Goodbye!","من انت":"🧠 أنا وكيل موحّد — كل النماذج في إجابة واحدة. /help",
                 "من أنت":"🧠 أنا وكيل موحّد — كل النماذج في إجابة واحدة. /help"}
        norm = text.lower().strip()
        if norm in quick:
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": quick[norm]})
        for k, v in quick.items():
            if norm.startswith(k) and len(norm) < len(k) + 15:
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": v})

        # أدوات سريعة — webhook reply فوري
        if re.match(r"^[\d\s\+\-\*\/\(\)\.\^\%\s]+$", text.strip()) and re.search(r"\d", text) and len(text) < 80:
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": f"🧮 {text} = **{t_calc(text)}**", "parse_mode": "Markdown"})
        if text.strip().lower() in ("الوقت", "time", "الساعة", "التاريخ"):
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": t_time()})
        m = re.search(r"(?:hash|هاش)[:\s]+(.+)", text, re.I)
        if m:
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": t_hash(m.group(1).strip()), "parse_mode": "Markdown"})
        m = re.search(r"(?:ابحث عن|بحث|search)[:\s]+(.+)", text, re.I)
        if m:
            r = await t_search(m.group(1).strip())
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": r[:4096], "parse_mode": "Markdown"})
        m = re.search(r"(?:github)[:\s]+(\w+)", text, re.I)
        if m:
            r = await t_github(m.group(1).strip())
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": r[:4096], "parse_mode": "Markdown"})
        m = re.search(r"(?:arxiv)[:\s]+(.+)", text, re.I)
        if m:
            r = await t_arxiv(m.group(1).strip())
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": r[:4096], "parse_mode": "Markdown"})
        url_m = re.search(r'(https?://[^\s<>"\']+)', text)
        if url_m and ("اقرأ" in text or "read" in text.lower() or text.strip().startswith("http")):
            r = await t_fetch(url_m.group(0))
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": r[:4096]})

        # كل شيء آخر — Fusion في نفس الطلب (timeout 55s)
        try:
            reply, source, elapsed = await asyncio.wait_for(agent(uid, text), timeout=55.0)
            if reply:
                emoji = "⚡" if elapsed < 5 else "🚀" if elapsed < 15 else "⏱️"
                src = source.split(":")[-1] if ":" in source else source
                final = reply + f"\n\n{emoji} 🔀 {src} ({elapsed:.1f}s)"
                return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": final[:4096]})
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": "⏳ النماذج مشغولة. أعد المحاولة."})
        except asyncio.TimeoutError:
            LOG.append(f"[{time.strftime('%H:%M:%S')}] ⏰ timeout → background")
            async def bg():
                try:
                    r, s, e = await agent(uid, text)
                    if r: await send_msg(chat_id, r[:4096])
                except: pass
            asyncio.create_task(bg())
            return web.json_response({"method": "sendMessage", "chat_id": chat_id, "text": "⏳ جاري التنفيذ — سأرسل النتيجة قريباً."})
    except Exception as e:
        LOG.append(f"[{time.strftime('%H:%M:%S')}] ❌ {e}")
        logger.error(f"{e}\n{traceback.format_exc()}")
        return web.json_response({})

# ============================================================
# HEALTH + KEEP-ALIVE + BACKUP
# ============================================================
async def health(request):
    s = mem.stats()
    log = "\n".join(LOG[-15:]) if LOG else "No activity"
    return web.Response(text=f"🧠 v28.0 (Fusion + 8 Models + 15 Tools)\n📊: T={STATS['total']} F={STATS['fusion']} Tools={STATS['tools']} Fail={STATS['fail']}\n🧠: U={s['users']} L={s['learned']} C={s['cache']}\n📋:\n{log}", content_type="text/plain")

async def keep_alive():
    LOG.append("💓 keep-alive 24/7")
    n = 0
    while True:
        try:
            await asyncio.sleep(300)
            n += 1
            async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as s:
                async with s.get(f"{SPACE_URL}/"): pass
            if n % 12 == 0:
                # كل ساعة: webhook + backup
                async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as s:
                    async with s.post(f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
                        json={"url": f"{SPACE_URL}{WEBHOOK_PATH}", "allowed_updates": ["message"], "max_connections": 40}): pass
                # GitHub backup
                if os.path.exists(MEM_FILE) and GITHUB_TOKEN:
                    try:
                        import httpx
                        with open(MEM_FILE, "rb") as f: content = f.read()
                        r = httpx.get("https://api.github.com/repos/absullh997-rgb/bot-memory-backup/contents/memory.json",
                            headers={"Authorization": f"token {GITHUB_TOKEN}"}, timeout=10)
                        sha = r.json().get("sha") if r.status_code == 200 else None
                        data = {"message": f"backup {time.strftime('%H:%M')}", "content": base64.b64encode(content).decode()}
                        if sha: data["sha"] = sha
                        httpx.put("https://api.github.com/repos/absullh997-rgb/bot-memory-backup/contents/memory.json",
                            headers={"Authorization": f"token {GITHUB_TOKEN}"}, json=data, timeout=10)
                        LOG.append(f"[{time.strftime('%H:%M:%S')}] 💾 backup OK")
                    except: pass
        except asyncio.CancelledError: break
        except: pass

async def startup(app):
    LOG.append("setting webhook...")
    try:
        async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10)) as s:
            async with s.post(f"https://api.telegram.org/bot{BOT_TOKEN}/setWebhook",
                json={"url": f"{SPACE_URL}{WEBHOOK_PATH}", "allowed_updates": ["message"], "max_connections": 40}) as r:
                LOG.append(f"webhook: {(await r.json()).get('ok', False)}")
    except Exception as e:
        LOG.append(f"webhook fail: {e}")
    asyncio.create_task(keep_alive())

async def cleanup(app):
    global SESSION
    if SESSION and not SESSION.closed: await SESSION.close()

def main():
    if not BOT_TOKEN:
        print("No TELEGRAM_BOT_TOKEN!"); sys.exit(1)
    app = web.Application()
    app.router.add_post(WEBHOOK_PATH, handle_webhook)
    app.router.add_get("/", health)
    app.on_startup.append(startup)
    app.on_cleanup.append(cleanup)
    print(f"🧠 v28.0 starting")
    web.run_app(app, host="0.0.0.0", port=PORT, access_log=None)

if __name__ == "__main__":
    main()