AnesKAM commited on
Commit
ef7d2e6
·
verified ·
1 Parent(s): 0357094

Delete index.html

Browse files
Files changed (1) hide show
  1. index.html +0 -1436
index.html DELETED
@@ -1,1436 +0,0 @@
1
- import os
2
- import re
3
- import json
4
- import base64
5
- import io
6
- import asyncio
7
- import requests
8
- from urllib.parse import quote, urlparse
9
- from fastapi import FastAPI, Request, HTTPException
10
- from fastapi.responses import StreamingResponse, Response, FileResponse
11
- from fastapi.staticfiles import StaticFiles
12
- from openai import AsyncOpenAI
13
- from pydantic import BaseModel
14
- from typing import Optional, List
15
-
16
- # ═══════════════════════════════════════════════════════════
17
- # التهيئة
18
- # ═══════════════════════════════════════════════════════════
19
- os.makedirs("static", exist_ok=True)
20
-
21
- app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
22
- app.mount("/static", StaticFiles(directory="static"), name="static")
23
-
24
- POLLINATIONS_KEY = os.getenv("POLLINATIONS_API_KEY")
25
- NVIDIA_KEY = os.getenv("NVIDIA_API_KEY")
26
- ZAI_KEY = os.getenv("ZAI_API_KEY", "sk-3NIcuqLmzTYDIh72z1fCBpLP848OLKGU")
27
- BAI_KEY = os.getenv("B_AI_API_KEY")
28
- HF_TOKEN = os.getenv("HF_TOKEN")
29
-
30
- if not POLLINATIONS_KEY or not NVIDIA_KEY:
31
- raise RuntimeError("يجب ضبط POLLINATIONS_API_KEY و NVIDIA_API_KEY في Secrets")
32
- if not BAI_KEY:
33
- raise RuntimeError("يجب ضبط B_AI_API_KEY في Secrets")
34
-
35
- # ── ① المزوّد الافتراضي للفلاش: FreeAI Gateway ──
36
- FREEAI_BASE_URL = "https://freeaixyz4all.vercel.app/api/v1"
37
- freeai_client = AsyncOpenAI(base_url=FREEAI_BASE_URL, api_key="not-needed", timeout=180)
38
-
39
- # ── ② المزوّد الافتراضي للبرو/الكودكس: Z.ai ──
40
- ZAI_BASE_URL = "https://h1kcr7a93tg0-d.space-z.ai/api/provider/v1"
41
- zai_client = AsyncOpenAI(base_url=ZAI_BASE_URL, api_key=ZAI_KEY, timeout=180)
42
-
43
- # ── ③ b.ai — احتياط ──
44
- BAI_BASE_URL = "https://api.b.ai/v1"
45
- bai_client = AsyncOpenAI(base_url=BAI_BASE_URL, api_key=BAI_KEY, timeout=120)
46
-
47
- # ── ④ Pollinations — احتياط أخير ──
48
- BASE_URL = "https://gen.pollinations.ai/v1"
49
- polli = AsyncOpenAI(base_url=BASE_URL, api_key=POLLINATIONS_KEY, timeout=120)
50
-
51
- # ── ⑤ HuggingFace — اختياري (تحليل الصور) ──
52
- hf_client = None
53
- if HF_TOKEN:
54
- hf_client = AsyncOpenAI(base_url="https://router.huggingface.co/v1",
55
- api_key=HF_TOKEN, timeout=120)
56
-
57
- VISION_BAI_MODELS = ["glm-5.3-flash", "deepseek-v4-flash"]
58
- VISION_HF_FALLBACK = "meta-llama/Llama-3.2-11B-Vision-Instruct"
59
-
60
- # مزوّدون نستخدم معهم الصيغة النصية للأدوات (لا تعريفات قياسية)
61
- # — freeai: لأن البوابة تنفّذ الأدوات بنفسها وستتعارض مع أدواتنا
62
- FLATTEN_PROVIDERS = {"zai", "freeai"}
63
-
64
- # ═══════════════════════════════════════════════════════════
65
- # الذاكرة — من localStorage للعميل
66
- # ═══════════════════════════════════════════════════════════
67
- MAX_MEMORY_JSON = 64 * 1024
68
-
69
-
70
- def _sanitize_memory(raw) -> dict:
71
- out = {"name": "", "surname": "", "email": "", "occupation": "",
72
- "interests": [], "notes": "", "facts": [], "preferences": {}}
73
- if not isinstance(raw, dict):
74
- return out
75
-
76
- def sval(v, n):
77
- return str(v or "").strip()[:n] if v else ""
78
-
79
- out["name"] = sval(raw.get("name"), 100)
80
- out["surname"] = sval(raw.get("surname"), 100)
81
- out["email"] = sval(raw.get("email"), 200)
82
- out["occupation"] = sval(raw.get("occupation"), 300)
83
- out["notes"] = sval(raw.get("notes"), 800)
84
-
85
- interests = raw.get("interests")
86
- if isinstance(interests, list):
87
- out["interests"] = [str(i).strip()[:60] for i in interests
88
- if str(i).strip()][:40]
89
-
90
- facts = raw.get("facts")
91
- if isinstance(facts, list):
92
- out["facts"] = [str(f).strip()[:300] for f in facts
93
- if str(f).strip()][:80]
94
-
95
- prefs = raw.get("preferences")
96
- if isinstance(prefs, dict):
97
- out["preferences"] = {str(k).strip()[:40]: str(v).strip()[:200]
98
- for k, v in list(prefs.items())[:30] if v}
99
- return out
100
-
101
-
102
- def _apply_memory_update(mem: dict, key: str, value: str) -> None:
103
- key = (key or "notes").lower().strip()
104
- value = (value or "").strip()[:500]
105
- if not value:
106
- return
107
- if key == "interests":
108
- new = [v.strip()[:60] for v in re.split(r"[,،]", value) if v.strip()]
109
- merged = list(dict.fromkeys(mem.get("interests", []) + new))
110
- mem["interests"] = merged[-40:]
111
- elif key == "facts":
112
- facts = mem.setdefault("facts", [])
113
- facts.append(value[:300])
114
- mem["facts"] = facts[-80:]
115
- elif key in ("occupation", "notes"):
116
- mem[key] = value[:800]
117
- elif key.startswith("pref"):
118
- prefs = mem.setdefault("preferences", {})
119
- if "=" in value:
120
- k, v = value.split("=", 1)
121
- prefs[k.strip()[:40]] = v.strip()[:200]
122
- else:
123
- prefs["general"] = value[:200]
124
- if len(prefs) > 30:
125
- for k in list(prefs)[:-30]:
126
- del prefs[k]
127
- else:
128
- facts = mem.setdefault("facts", [])
129
- facts.append(f"{key[:40]}: {value}"[:300])
130
- mem["facts"] = facts[-80:]
131
-
132
-
133
- def _memory_block(mem: dict) -> str:
134
- parts = []
135
- full_name = f"{mem.get('name', '')} {mem.get('surname', '')}".strip()
136
- if full_name:
137
- parts.append(f"Name: {full_name}")
138
- if mem.get("email"):
139
- parts.append(f"Email: {mem['email']}")
140
- if mem.get("occupation"):
141
- parts.append(f"Occupation: {mem['occupation']}")
142
- if mem.get("interests"):
143
- parts.append("Interests: " + ", ".join(mem["interests"]))
144
- if mem.get("notes"):
145
- parts.append(f"Notes: {mem['notes']}")
146
- if mem.get("facts"):
147
- parts.append("Known facts: " + " | ".join(mem["facts"][-25:]))
148
- if mem.get("preferences"):
149
- prefs = "; ".join(f"{k}: {v}" for k, v in mem["preferences"].items() if v)
150
- if prefs:
151
- parts.append("Preferences: " + prefs)
152
- if not parts:
153
- return ""
154
- return ("\n[USER MEMORY — personalize answers naturally using this; "
155
- "never list it explicitly]\n" + "\n".join(parts))
156
-
157
-
158
- # ═══════════════════════════════════════════════════════════
159
- # دليل الأدوات + قواعد الكود الصارمة ⛔ (مع منع تقسيم الكود)
160
- # ═══════════════════════════════════════════════════════════
161
- TOOL_GUIDE = """
162
- ===== ⛔ CODE RULES — ABSOLUTE HIGHEST PRIORITY ⛔ =====
163
- When the user asks for ANY code (program, script, game, website,
164
- HTML page, app, function, algorithm, fix, converter, calculator...):
165
-
166
- CODE-RULE 1: WRITE THE CODE DIRECTLY in your reply, inside ONE
167
- SINGLE fenced code block with a language tag, like:
168
- ```html
169
- ...the full code...
170
- ```
171
-
172
- CODE-RULE 1B — SINGLE BLOCK, NO SPLITTING (CRITICAL):
173
- - The ENTIRE code must be in ONE continuous fenced block, from the
174
- opening ``` to the closing ```, with NOTHING interrupting it.
175
- - FORBIDDEN: closing the fence early, writing an explanation in
176
- plain text, then opening a NEW fence to continue the SAME code.
177
- - FORBIDDEN: splitting HTML/CSS/JS into separate blocks (one block
178
- for HTML, another for CSS, another for JS) — merge them into ONE
179
- block (inline <style> and <script> inside one .html file) unless
180
- the user explicitly asked for separate files.
181
- - If you need to explain something, explain it ALL before the code
182
- block starts, or ALL after it ends — never in the middle.
183
- - BAD (forbidden) pattern:
184
- ```html
185
- <!DOCTYPE html>
186
- <head>...
187
- ```
188
- الآن سأضيف الأنماط:
189
- ```css
190
- body { ... }
191
- ```
192
- - GOOD (required) pattern: everything merged inside ONE block:
193
- ```html
194
- <!DOCTYPE html>
195
- <html>
196
- <head>
197
- <style>
198
- body { ... }
199
- </style>
200
- </head>
201
- <body>
202
- ...
203
- <script>
204
- ...
205
- </script>
206
- </body>
207
- </html>
208
- ```
209
-
210
- CODE-RULE 2: You are FORBIDDEN from using ANY tool when the task
211
- involves writing code. NO CREATE_DOCUMENT, NO WEB_SEARCH, NO
212
- BROWSE_WEBSITE, NO GENERATE_IMAGE. ZERO tools.
213
- CODE-RULE 3: NEVER say "I will create a file for you" when writing
214
- code. Just write the complete working code immediately in the chat.
215
- CODE-RULE 4: NEVER put a placeholder like "// write code here" —
216
- always deliver the FULL, complete, runnable code.
217
- CODE-RULE 5: If the code is long (a game, a full page, multiple
218
- functions), you STILL write it ALL inside the SAME single fenced
219
- block. Length is NEVER a reason to split, pause, or explain
220
- mid-code. Long code = long single block. Never split "for
221
- readability" or "to explain each part" — the user wants ONE
222
- copy-pasteable file. If the response gets cut off mid-code and you
223
- are asked to continue, resume inside the SAME block — never open a
224
- new fence.
225
- CODE-RULE 6: ONLY exception — if the user EXPLICITLY asks for a
226
- downloadable file, you may use CREATE_DOCUMENT after the task, but
227
- the code itself still appears in the chat first, complete and
228
- unsplit, when relevant.
229
- CODE-RULE 7: If the user reports an error in previous code, fix it
230
- and output the FULL corrected code in ONE new single code block.
231
- No tools, no partial diffs, no splitting.
232
- Violation of these code rules is a critical failure.
233
- ===== END CODE RULES =====
234
-
235
- ===== TOOLS — HOW TO USE THEM (ONLY for non-code tasks) =====
236
- You have 5 real tools. The system EXECUTES them for you. To use a
237
- tool, write its EXACT name in CAPITALS, then a colon, then its
238
- argument, on its OWN line — alone. Then STOP. The system runs the
239
- tool and sends you the real results, then you write your answer.
240
-
241
- 1) WEB_SEARCH: <search query>
242
- 2) GENERATE_IMAGE: <rich English description>
243
- 3) BROWSE_WEBSITE: <full URL> (never search-engine URLs)
244
- 4) UPDATE_MEMORY: <key>: <value> keys: interests|occupation|notes|facts
245
- 5) CREATE_DOCUMENT: <title> | format: <pdf|docx|pptx|xlsx> (never for code)
246
-
247
- --- STRICT RULES ---
248
- RULE 1: Tool line must be ALONE on its line.
249
- RULE 2: ONE tool per step, then STOP writing.
250
- RULE 3: In your FINAL answer, never write tool names.
251
- RULE 4: NEVER invent or simulate results.
252
- RULE 5: After results, answer in the SAME LANGUAGE as the user.
253
- RULE 6: Normal chat → just answer, no tool lines.
254
- RULE 7: NEVER apologize twice and never repeat a previous sentence.
255
- RULE 8: NEVER write system notes like "this message contained
256
- tool_calls" — internal, invisible to users.
257
- RULE 9: If you receive a continuation instruction because your
258
- previous text was cut off, CONTINUE seamlessly from the
259
- exact stopping point without repeating anything. If the
260
- answer was effectively complete, reply with exactly: [END]
261
- RULE 10: CODE REQUESTS OVERRIDE EVERYTHING: code goes in the chat,
262
- in ONE unsplit block, tools forbidden (see CODE RULES above).
263
- =====================================================
264
- """
265
-
266
- SYSTEM_PROMPTS = {
267
- "flash": (
268
- "You are Genisi Flash بالعربية جينيسي, an intelligent and helpful AI assistant "
269
- "by AnesNT (انس ان تي), founder Anes Kameche (انس كامش), made in Algeria (مصمم في الجزائر).\n"
270
- + TOOL_GUIDE
271
- ),
272
- "pro": (
273
- "You are Genisi Pro, the most powerful model in the Genisi family."
274
- " Identity: Developed by AnesNT (انس ان تي) (Algerian project by Anes Kameche / انس كامش)."
275
- " Do not say you are Kimi or Laguna or another AI model; you are Genisi only وانت مصمم في الجزائر."
276
- " Style: Professional, detailed, and direct. Think deeply before answering."
277
- " Skills: Analyze images/files. Be creative in stories, precise in math/science.\n"
278
- + TOOL_GUIDE
279
- ),
280
- "codex": (
281
- "You are Genisi Codex, an expert strictly in programming and tech.\n"
282
- "Identity: Developed by AnesNT (انس ان تي) (Algerian project by Anes Kameche / انس كامش)."
283
- " Do not say you are Kimi or Laguna or another AI model; you are Genisi only."
284
- " CODE-RULE 0 (CRITICAL FOR YOU): You ALWAYS write complete code directly in the"
285
- " chat inside ONE SINGLE ```code block```, never split into multiple blocks,"
286
- " never interrupted by explanations in the middle. You NEVER use CREATE_DOCUMENT"
287
- " or any tool for code."
288
- " Think deeply about architecture and edge cases before coding."
289
- " Non-tech questions: say 'أنا متخصص في البرمجة فقط.'\n"
290
- + TOOL_GUIDE
291
- ),
292
- }
293
-
294
- # ═══════════════════════════════════════════════════════════
295
- # سلاسل الاحتياط — FreeAI افتراضي للفلاش ⭐
296
- # ═══════════════════════════════════════════════════════════
297
- FALLBACK_CHAINS = {
298
- "flash": [
299
- {"model": "gpt-5.4-mini", "provider": "freeai", "temperature": 0.7}, # ⭐ الأساسي
300
- {"model": "glm-5.3-flash", "provider": "zai", "temperature": 0.7},
301
- {"model": "deepseek-v4-flash", "provider": "bai", "temperature": 0.7},
302
- {"model": "Spit-fires/muse-glimmer", "provider": "pollinations"},
303
- ],
304
- "pro": [
305
- {"model": "glm-5.3-flash", "provider": "zai", "temperature": 0.7,
306
- "thinking": True},
307
- {"model": "gpt-5.4-mini", "provider": "freeai", "temperature": 0.7},
308
- {"model": "glm-5.3-flash", "provider": "bai", "temperature": 0.7},
309
- {"model": "Spit-fires/muse-glimmer", "provider": "pollinations"},
310
- ],
311
- "codex": [
312
- {"model": "glm-5.3-flash", "provider": "zai", "temperature": 0.7,
313
- "thinking": True},
314
- {"model": "gpt-5.4-mini", "provider": "freeai", "temperature": 0.7},
315
- {"model": "deepseek-v4-flash", "provider": "bai", "temperature": 0.7},
316
- {"model": "Spit-fires/muse-glimmer", "provider": "pollinations"},
317
- ],
318
- }
319
-
320
- TOOLS = [
321
- {
322
- "type": "function",
323
- "function": {
324
- "name": "generate_image",
325
- "description": "إنشاء صورة احترافية من وصف نصي",
326
- "parameters": {
327
- "type": "object",
328
- "properties": {
329
- "prompt": {"type": "string", "description": "وصف الصورة المطلوبة"},
330
- "width": {"type": "integer", "default": 1024},
331
- "height": {"type": "integer", "default": 1024}
332
- },
333
- "required": ["prompt"]
334
- }
335
- }
336
- },
337
- {
338
- "type": "function",
339
- "function": {
340
- "name": "web_search",
341
- "description": "البحث في الإنترنت عبر DuckDuckGo",
342
- "parameters": {
343
- "type": "object",
344
- "properties": {"query": {"type": "string"}},
345
- "required": ["query"]
346
- }
347
- }
348
- },
349
- {
350
- "type": "function",
351
- "function": {
352
- "name": "browse_website",
353
- "description": "الدخول إلى موقع وقراءة محتوى صفحته",
354
- "parameters": {
355
- "type": "object",
356
- "properties": {"url": {"type": "string"}},
357
- "required": ["url"]
358
- }
359
- }
360
- },
361
- {
362
- "type": "function",
363
- "function": {
364
- "name": "update_memory",
365
- "description": "حفظ/تحديث معلومات دائمة عن المستخدم",
366
- "parameters": {
367
- "type": "object",
368
- "properties": {
369
- "key": {"type": "string",
370
- "enum": ["interests", "occupation", "notes", "facts"]},
371
- "value": {"type": "string"}
372
- },
373
- "required": ["key", "value"]
374
- }
375
- }
376
- },
377
- {
378
- "type": "function",
379
- "function": {
380
- "name": "create_document",
381
- "description": "إنشاء مستند (PDF, Word, PowerPoint, Excel) — غير مسموح لمهام الكود",
382
- "parameters": {
383
- "type": "object",
384
- "properties": {
385
- "title": {"type": "string"},
386
- "format": {"type": "string", "enum": ["pdf", "docx", "pptx", "xlsx"]},
387
- "content": {"type": "object"}
388
- },
389
- "required": ["title", "format", "content"]
390
- }
391
- }
392
- }
393
- ]
394
-
395
-
396
- class Attachment(BaseModel):
397
- name: str
398
- mime: str = ""
399
- data: str = ""
400
-
401
-
402
- class ChatRequest(BaseModel):
403
- messages: list
404
- message: str
405
- model: str = "flash"
406
- attachments: Optional[List[Attachment]] = []
407
- memory: Optional[dict] = None
408
-
409
-
410
- def _detect_mime(raw: bytes) -> str:
411
- if not raw or len(raw) < 12:
412
- return ""
413
- if raw.startswith(b"\x89PNG"):
414
- return "image/png"
415
- if raw.startswith(b"\xff\xd8\xff"):
416
- return "image/jpeg"
417
- if raw.startswith(b"GIF8"):
418
- return "image/gif"
419
- if raw.startswith(b"RIFF") and raw[8:12] == b"WEBP":
420
- return "image/webp"
421
- if raw.startswith(b"%PDF"):
422
- return "application/pdf"
423
- if raw.startswith(b"PK\x03\x04"):
424
- return "zip-office"
425
- return ""
426
-
427
-
428
- MAX_ATTACHMENT_B64 = 16 * 1024 * 1024
429
-
430
-
431
- def extract_text_from_file(attachment: Attachment) -> str:
432
- try:
433
- raw = base64.b64decode(attachment.data)
434
- mime = (attachment.mime or "").lower()
435
- name = (attachment.name or "").lower()
436
- if not mime or mime in ("application/octet-stream", "zip-office"):
437
- detected = _detect_mime(raw)
438
- if detected and detected != "zip-office":
439
- mime = detected
440
- elif detected == "zip-office":
441
- if name.endswith(".docx"):
442
- mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
443
- elif name.endswith(".xlsx"):
444
- mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
445
- elif name.endswith(".pptx"):
446
- mime = "application/vnd.openxmlformats-officedocument.presentationml.presentation"
447
- if mime.startswith("text/") or name.endswith(
448
- (".txt", ".md", ".csv", ".json", ".xml", ".html", ".py", ".js", ".ts", ".css")
449
- ):
450
- return raw.decode("utf-8", errors="replace")
451
- if mime == "application/pdf" or name.endswith(".pdf"):
452
- try:
453
- import pdfplumber
454
- with pdfplumber.open(io.BytesIO(raw)) as pdf:
455
- pages_text = []
456
- for i, page in enumerate(pdf.pages[:20]):
457
- text = page.extract_text()
458
- if text:
459
- pages_text.append(f"[صفحة {i+1}]\n{text}")
460
- return ("\n\n".join(pages_text)
461
- if pages_text else "[ملف PDF فارغ]")
462
- except ImportError:
463
- return "[يتطلب pdfplumber]"
464
- if (mime == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
465
- or name.endswith(".docx")):
466
- try:
467
- from docx import Document
468
- doc = Document(io.BytesIO(raw))
469
- paragraphs = [p.text for p in doc.paragraphs if p.text.strip()]
470
- return "\n".join(paragraphs) if paragraphs else "[مستند Word فارغ]"
471
- except ImportError:
472
- return "[يتطلب python-docx]"
473
- if (mime in ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
474
- "application/vnd.ms-excel") or name.endswith((".xlsx", ".xls"))):
475
- try:
476
- from openpyxl import load_workbook
477
- wb = load_workbook(io.BytesIO(raw), read_only=True)
478
- result = []
479
- for sheet_name in wb.sheetnames:
480
- ws = wb[sheet_name]
481
- result.append(f"[ورقة: {sheet_name}]")
482
- for row in ws.iter_rows(max_row=100, values_only=True):
483
- row_text = " | ".join(str(c) if c is not None else "" for c in row)
484
- if row_text.strip(" |"):
485
- result.append(row_text)
486
- return "\n".join(result)
487
- except ImportError:
488
- return "[يتطلب openpyxl]"
489
- if (mime == "application/vnd.openxmlformats-officedocument.presentationml.presentation"
490
- or name.endswith(".pptx")):
491
- try:
492
- from pptx import Presentation
493
- prs = Presentation(io.BytesIO(raw))
494
- result = []
495
- for i, slide in enumerate(prs.slides):
496
- result.append(f"[شريحة {i+1}]")
497
- for shape in slide.shapes:
498
- if hasattr(shape, "text") and shape.text.strip():
499
- result.append(shape.text)
500
- return "\n".join(result)
501
- except ImportError:
502
- return "[يتطلب python-pptx]"
503
- return f"[ملف '{attachment.name}' - نوع غير مدعوم: {mime}]"
504
- except Exception as e:
505
- return f"[خطأ في قراءة الملف '{attachment.name}': {e}]"
506
-
507
-
508
- def _vision_content(image_data: str, mime: str, question: str) -> list:
509
- return [
510
- {"type": "image_url", "image_url": {"url": f"data:{mime};base64,{image_data}"}},
511
- {"type": "text", "text": question},
512
- ]
513
-
514
-
515
- async def analyze_image_with_vision(image_data: str, mime: str, user_question: str) -> str:
516
- question = user_question or "صف هذه الصورة بالتفصيل"
517
- content = _vision_content(image_data, mime, question)
518
- errors = []
519
- for model in VISION_BAI_MODELS:
520
- try:
521
- resp = await bai_client.chat.completions.create(
522
- model=model, messages=[{"role": "user", "content": content}],
523
- max_tokens=1024)
524
- return resp.choices[0].message.content or "[لم يتم استخراج وصف]"
525
- except Exception as e:
526
- errors.append(f"b.ai/{model}: {e}")
527
- print(f"[Vision] فشل b.ai/{model}: {e}")
528
- try:
529
- resp = await polli.chat.completions.create(
530
- model="openai", messages=[{"role": "user", "content": content}],
531
- max_tokens=1024)
532
- return resp.choices[0].message.content or "[لم يتم استخراج وصف]"
533
- except Exception as e:
534
- errors.append(f"pollinations: {e}")
535
- if hf_client:
536
- try:
537
- resp = await hf_client.chat.completions.create(
538
- model=VISION_HF_FALLBACK, messages=[{"role": "user", "content": content}],
539
- max_tokens=1024)
540
- return resp.choices[0].message.content or "[لم يتم استخراج وصف]"
541
- except Exception as e:
542
- errors.append(f"hf: {e}")
543
- return f"[فشل تحليل الصورة: {' | '.join(errors)}]"
544
-
545
-
546
- async def process_attachments(attachments: List[Attachment], user_text: str) -> str:
547
- if not attachments:
548
- return user_text
549
- extra_parts = []
550
- for att in attachments:
551
- if not att.data or len(att.data) > MAX_ATTACHMENT_B64:
552
- continue
553
- mime = (att.mime or "").lower()
554
- if not mime or mime == "application/octet-stream":
555
- try:
556
- head = base64.b64decode(att.data[:128] + "===", validate=False)
557
- mime = _detect_mime(head) or mime
558
- except Exception:
559
- pass
560
- if mime.startswith("image/"):
561
- extra_parts.append(f"\n\n---\n📎 **صورة مرفقة: {att.name}**")
562
- vision_result = await analyze_image_with_vision(
563
- att.data, att.mime or mime or "image/png", user_text)
564
- extra_parts.append(f"**[تحليل الصورة]:**\n{vision_result}")
565
- else:
566
- file_text = await asyncio.to_thread(extract_text_from_file, att)
567
- truncated = file_text[:8000]
568
- extra_parts.append(f"\n\n---\n📄 **محتوى الملف: {att.name}**\n```\n{truncated}\n```")
569
- if len(file_text) > 8000:
570
- extra_parts.append(f"\n⚠️ *أول 8000 حرف من أصل {len(file_text)}*")
571
- if extra_parts:
572
- return (user_text or "") + "".join(extra_parts)
573
- return user_text or ""
574
-
575
-
576
- # ═══════════════════════════════════════════════════════���═══
577
- # البحث DuckDuckGo
578
- # ═══════════════════════════════════════════════════════════
579
- def _ddg_search_sync(query: str, max_results: int = 6) -> list:
580
- try:
581
- from ddgs import DDGS
582
- except ImportError:
583
- from duckduckgo_search import DDGS
584
- with DDGS() as ddgs:
585
- return [
586
- {"title": r.get("title", ""),
587
- "url": r.get("href", "") or r.get("link", ""),
588
- "snippet": r.get("body", "") or r.get("snippet", "")}
589
- for r in ddgs.text(query, max_results=max_results)
590
- ]
591
-
592
-
593
- async def web_search(query: str) -> str:
594
- query = (query or "").strip()[:300]
595
- if not query:
596
- return json.dumps({"error": "استعلام فارغ"}, ensure_ascii=False)
597
- try:
598
- results = await asyncio.to_thread(_ddg_search_sync, query)
599
- if results:
600
- return json.dumps({"special": "search", "engine": "duckduckgo",
601
- "query": query, "results": results}, ensure_ascii=False)
602
- raise RuntimeError("لا نتائج")
603
- except Exception as e:
604
- print(f"[Search] ddgs فشل: {e}")
605
- try:
606
- resp = await asyncio.to_thread(
607
- requests.get, "https://api.duckduckgo.com/",
608
- params={"q": query, "format": "json", "no_html": 1, "skip_disambig": 1},
609
- timeout=15)
610
- resp.raise_for_status()
611
- data = resp.json()
612
- abstract = data.get("AbstractText") or data.get("Abstract") or ""
613
- related = [{"title": t.get("Text", ""), "url": t.get("FirstURL", "")}
614
- for t in data.get("RelatedTopics", [])[:6]
615
- if isinstance(t, dict) and t.get("FirstURL")]
616
- return json.dumps({"special": "search", "engine": "duckduckgo-instant",
617
- "query": query, "abstract": abstract,
618
- "results": related}, ensure_ascii=False)
619
- except Exception as e2:
620
- return json.dumps({"error": f"فشل البحث: {e} | {e2}"}, ensure_ascii=False)
621
-
622
-
623
- # ═══════════════════════════════════════════════════════════
624
- # تصفح المواقع
625
- # ═══════════════════════════════════════════════════════════
626
- SEARCH_ENGINE_MARKS = ("google.", "bing.", "duckduckgo.", "search.brave", "yandex.",
627
- "yahoo.", "startpage.", "ecosia.", "qwant.", "perplexity.",
628
- "mojeek.", "searx", "lite.duckduckgo.")
629
-
630
- _PRIVATE_HOST_RE = re.compile(
631
- r"^(localhost$|127\.|0\.0\.0\.0|10\.|169\.254\.|192\.168\.|"
632
- r"172\.(1[6-9]|2\d|3[01])\.|\[::1\]$|metadata\.google\.internal$|169\.254\.169\.254)",
633
- re.IGNORECASE)
634
-
635
-
636
- async def browse_website(url: str) -> str:
637
- try:
638
- url = (url or "").strip()[:2000]
639
- if not url:
640
- return json.dumps({"error": "رابط فارغ"}, ensure_ascii=False)
641
- if not url.startswith(("http://", "https://")):
642
- url = "https://" + url
643
-
644
- try:
645
- host = urlparse(url).hostname or ""
646
- except Exception:
647
- host = ""
648
-
649
- if any(m in (host or "").lower() for m in SEARCH_ENGINE_MARKS):
650
- return json.dumps(
651
- {"error": "لا يمكن تصفح صفحات محركات البحث مباشرة. "
652
- "استخدم نتائج web_search، أو تصفح رابط نتيجة محدد."},
653
- ensure_ascii=False)
654
-
655
- if _PRIVATE_HOST_RE.match(host or ""):
656
- return json.dumps({"error": "رابط غير مسموح"}, ensure_ascii=False)
657
-
658
- headers = {
659
- "User-Agent": ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
660
- "(KHTML, like Gecko) Chrome/125.0 Safari/537.36 GenisiBot/1.0"),
661
- "Accept-Language": "ar,en;q=0.9",
662
- }
663
- resp = await asyncio.to_thread(requests.get, url, headers=headers, timeout=20)
664
- resp.raise_for_status()
665
-
666
- if len(resp.content) > 10 * 1024 * 1024:
667
- return json.dumps({"error": "الصفحة كبيرة جداً"}, ensure_ascii=False)
668
-
669
- ctype = resp.headers.get("content-type", "").lower()
670
- if "html" in ctype or "xml" in ctype:
671
- try:
672
- from bs4 import BeautifulSoup
673
- soup = BeautifulSoup(resp.text, "html.parser")
674
- for tag in soup(["script", "style", "noscript", "iframe", "svg"]):
675
- tag.decompose()
676
- title = ""
677
- if soup.title and soup.title.string:
678
- title = soup.title.string.strip()
679
- text = soup.get_text("\n")
680
- text = "\n".join(ln.strip() for ln in text.splitlines() if ln.strip())
681
- except ImportError:
682
- title = ""
683
- text = re.sub(r"<script[\s\S]*?</script>", " ", resp.text, flags=re.I)
684
- text = re.sub(r"<style[\s\S]*?</style>", " ", text, flags=re.I)
685
- text = re.sub(r"<[^>]+>", "\n", text)
686
- text = "\n".join(ln.strip() for ln in text.splitlines() if ln.strip())
687
- else:
688
- title = url
689
- text = resp.text[:100000]
690
-
691
- low = text[:3000].lower()
692
- bot_markers = ("captcha", "unusual traffic", "are you a human",
693
- "made by a human", "select all squares", "verify you are human",
694
- "يرجى إثبات", "أنت روبوت", "التحقق البشري", "تم حظر وصول")
695
- if any(m in low for m in bot_markers):
696
- return json.dumps(
697
- {"error": "هذه الصفحة تفرض تحققاً أمنياً ولا يمكن قراءتها. "
698
- "استخدم web_search أو رابطاً آخر."},
699
- ensure_ascii=False)
700
-
701
- full_len = len(text)
702
- text = text[:6000]
703
- return json.dumps({"special": "browse", "url": url, "title": title or url,
704
- "content": text,
705
- "note": (f"طول الصفحة {full_len} حرف، أول 6000 معروض."
706
- if full_len > 6000 else "الصفحة كاملة.")},
707
- ensure_ascii=False)
708
- except Exception as e:
709
- return json.dumps({"error": f"فشل تصفح الموقع: {e}"}, ensure_ascii=False)
710
-
711
-
712
- # ═══════════════════════════════════════════════════════════
713
- # توليد الصور — PrexzyAPI أولاً
714
- # ═══════════════════════════════════════════════════════════
715
- def _find_image_url(obj, require_ext=True):
716
- if isinstance(obj, str):
717
- s = obj.strip()
718
- if s.startswith(("http://", "https://")):
719
- low = s.lower()
720
- has_ext = any(e in low for e in (".png", ".jpg", ".jpeg", ".webp", ".gif"))
721
- if has_ext or not require_ext:
722
- return s
723
- return None
724
- if isinstance(obj, dict):
725
- for v in obj.values():
726
- r = _find_image_url(v, require_ext)
727
- if r:
728
- return r
729
- elif isinstance(obj, list):
730
- for v in obj:
731
- r = _find_image_url(v, require_ext)
732
- if r:
733
- return r
734
- return None
735
-
736
-
737
- async def generate_image_nvidia(prompt: str, width: int, height: int) -> str:
738
- url = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.2-klein-4b"
739
- headers = {"Authorization": f"Bearer {NVIDIA_KEY}", "Accept": "application/json"}
740
- payload = {"prompt": prompt, "width": width, "height": height, "steps": 4}
741
- try:
742
- resp = await asyncio.to_thread(
743
- requests.post, url, headers=headers, json=payload, timeout=30)
744
- resp.raise_for_status()
745
- data = resp.json()
746
- if data.get("data") and "b64_json" in data["data"][0]:
747
- return json.dumps({"special": "image",
748
- "images": [{"b64": data["data"][0]["b64_json"]}],
749
- "prompt": prompt})
750
- except Exception as e:
751
- print(f"NVIDIA API failed: {e}")
752
- try:
753
- image_url = (f"https://gen.pollinations.ai/image/{quote(prompt)}"
754
- f"?model=flux&width={width}&height={height}&key={POLLINATIONS_KEY}")
755
- return json.dumps({"special": "image", "images": [{"url": image_url}],
756
- "prompt": prompt})
757
- except Exception as e2:
758
- return json.dumps({"error": f"فشل إنشاء الصورة: {e2}"}, ensure_ascii=False)
759
-
760
-
761
- async def generate_image_full(prompt: str, width: int, height: int) -> str:
762
- try:
763
- resp = await asyncio.to_thread(
764
- requests.get, "https://prexzyapis.com/ai/genimage",
765
- params={"prompt": prompt[:500]}, timeout=90)
766
- resp.raise_for_status()
767
- try:
768
- data = resp.json()
769
- except Exception:
770
- data = resp.text
771
- url = _find_image_url(data, True) or _find_image_url(data, False)
772
- if url:
773
- return json.dumps({"special": "image", "engine": "PrexzyAPI",
774
- "images": [{"url": url}], "prompt": prompt},
775
- ensure_ascii=False)
776
- raise RuntimeError(f"شكل رد غير متوقع: {str(data)[:120]}")
777
- except Exception as e:
778
- print(f"[Image] PrexzyAPI فشل: {e}")
779
- r = await generate_image_nvidia(prompt, width, height)
780
- try:
781
- d = json.loads(r)
782
- if d.get("special") == "image":
783
- d.setdefault("engine", "NVIDIA/Pollinations")
784
- return json.dumps(d, ensure_ascii=False)
785
- except Exception:
786
- pass
787
- return r
788
-
789
-
790
- # ═══════════════════════════════════════════════════════════
791
- # المستندات — fpdf2 أولاً ثم weasyprint
792
- # ═══════════════════════════════════════════════════════════
793
- def _fpdf_wrap(pdf, text: str, width: float) -> str:
794
- lines = []
795
- for raw in text.splitlines():
796
- words = raw.split(" ")
797
- cur = ""
798
- for w in words:
799
- t = (cur + " " + w).strip()
800
- if pdf.get_string_width(t) <= width:
801
- cur = t
802
- else:
803
- if cur:
804
- lines.append(cur)
805
- while pdf.get_string_width(w) > width and len(w) > 1:
806
- cut = len(w)
807
- while cut > 1 and pdf.get_string_width(w[:cut]) > width:
808
- cut -= 1
809
- lines.append(w[:cut])
810
- w = w[cut:]
811
- cur = w
812
- lines.append(cur)
813
- return "\n".join(lines)
814
-
815
-
816
- def _build_pdf_bytes(title: str, content: dict) -> bytes:
817
- html = content.get("html") or f"<h1>{title}</h1>"
818
- text = content.get("text") or ""
819
- if not text:
820
- try:
821
- from bs4 import BeautifulSoup
822
- text = BeautifulSoup(html, "html.parser").get_text("\n")
823
- except Exception:
824
- text = re.sub(r"<[^>]+>", "\n", html)
825
- text = text.strip()[:200000] or title
826
- try:
827
- from fpdf import FPDF
828
- pdf = FPDF()
829
- pdf.set_auto_page_break(auto=True, margin=15)
830
- pdf.add_page()
831
- font_path = None
832
- for p in ("/usr/share/fonts/truetype/noto/NotoNaskhArabic-Regular.ttf",
833
- "/usr/share/fonts/truetype/kacst/KacstOne.ttf",
834
- "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"):
835
- if os.path.exists(p):
836
- font_path = p
837
- break
838
- if font_path:
839
- try:
840
- pdf.add_font("uni", "", font_path)
841
- except TypeError:
842
- pdf.add_font("uni", "", font_path, uni=True)
843
- pdf.set_font("uni", size=13)
844
- try:
845
- import arabic_reshaper
846
- from bidi.algorithm import get_display
847
- text = "\n".join(get_display(arabic_reshaper.reshape(l))
848
- for l in text.splitlines())
849
- except ImportError:
850
- pass
851
- else:
852
- pdf.set_font("Helvetica", size=12)
853
- text = text.encode("latin-1", "replace").decode("latin-1")
854
- usable = pdf.w - pdf.l_margin - pdf.r_margin
855
- pdf.multi_cell(0, 8, _fpdf_wrap(pdf, text, usable))
856
- return bytes(pdf.output())
857
- except ImportError:
858
- pass
859
- except Exception as e:
860
- print(f"[Document] fpdf2 فشل: {e}")
861
- from weasyprint import HTML
862
- return HTML(string=html).write_pdf()
863
-
864
-
865
- def _safe_filename(name: str, ext: str) -> str:
866
- base = re.sub(r"[^\w\u0600-\u06FF.-]", "_", (name or "document").strip())[:80]
867
- return (base or "document") + ext
868
-
869
-
870
- def _build_document_bytes(title: str, fmt: str, content: dict):
871
- fmt = (fmt or "pdf").lower()
872
- if fmt == "pdf":
873
- return _build_pdf_bytes(title, content), "application/pdf", _safe_filename(title, ".pdf")
874
- elif fmt == "docx":
875
- try:
876
- from docx import Document
877
- except ImportError:
878
- raise RuntimeError("يتطلب python-docx")
879
- doc = Document()
880
- doc.add_heading(str(title)[:200], 0)
881
- for p in (content.get("paragraphs") or [])[:500]:
882
- doc.add_paragraph(str(p)[:5000])
883
- buf = io.BytesIO()
884
- doc.save(buf)
885
- return (buf.getvalue(),
886
- "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
887
- _safe_filename(title, ".docx"))
888
- elif fmt == "pptx":
889
- try:
890
- from pptx import Presentation
891
- except ImportError:
892
- raise RuntimeError("يتطلب python-pptx")
893
- prs = Presentation()
894
- slides = content.get("slides") or [{"title": title,
895
- "bullets": content.get("paragraphs", [])}]
896
- for i, slide in enumerate(slides[:100]):
897
- layout = prs.slide_layouts[1] if i > 0 else prs.slide_layouts[0]
898
- sl = prs.slides.add_slide(layout)
899
- if sl.shapes.title:
900
- sl.shapes.title.text = str(slide.get("title", ""))[:200]
901
- if len(sl.placeholders) > 1 and sl.placeholders[1].has_text_frame:
902
- sl.placeholders[1].text_frame.text = "\n".join(
903
- str(b)[:500] for b in (slide.get("bullets") or [])[:20])
904
- buf = io.BytesIO()
905
- prs.save(buf)
906
- return (buf.getvalue(),
907
- "application/vnd.openxmlformats-officedocument.presentationml.presentation",
908
- _safe_filename(title, ".pptx"))
909
- elif fmt == "xlsx":
910
- try:
911
- from openpyxl import Workbook
912
- except ImportError:
913
- raise RuntimeError("يتطلب openpyxl")
914
- wb = Workbook()
915
- ws = wb.active
916
- ws.title = str(title)[:31] or "Sheet"
917
- for c, h in enumerate((content.get("headers") or [])[:100], 1):
918
- ws.cell(row=1, column=c, value=str(h)[:500])
919
- for r, row in enumerate((content.get("rows") or [])[:5000], 2):
920
- for c, val in enumerate(row[:100], 1):
921
- ws.cell(row=r, column=c, value=str(val)[:500])
922
- buf = io.BytesIO()
923
- wb.save(buf)
924
- return (buf.getvalue(),
925
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
926
- _safe_filename(title, ".xlsx"))
927
- raise ValueError(f"تنسيق غير مدعوم: {fmt}")
928
-
929
-
930
- async def generate_document(title: str, fmt: str, content: dict) -> str:
931
- def _build():
932
- data, _, filename = _build_document_bytes(str(title)[:200], fmt, content)
933
- return {"special": "document", "title": str(title)[:200], "format": fmt,
934
- "filename": filename, "b64": base64.b64encode(data).decode()}
935
- try:
936
- return json.dumps(await asyncio.to_thread(_build), ensure_ascii=False)
937
- except Exception as e:
938
- return json.dumps({"error": f"فشل إنشاء المستند: {e}"}, ensure_ascii=False)
939
-
940
-
941
- # ═══════════════════════════════════════════════════════════
942
- # مرشّح الأدوات الزائفة
943
- # ═══════════════════════════════════════════════════════════
944
- PSEUDO_TOOL_RE = re.compile(
945
- r'^[\s>#*`\-_\[\(]*'
946
- r'(WEB_SEARCH|GENERATE_IMAGE|BROWSE_WEBSITE|UPDATE_MEMORY|'
947
- r'CREATE_DOCUMENT|WEBSEARCH|IMAGE_GENERATION|SEARCH|GENERATEIMAGE|'
948
- r'BROWSEWEBSITE|UPDATEMEMORY|CREATEDOCUMENT)'
949
- r'[\]\)\s]*[::\-—]?\s*(.*)$',
950
- re.IGNORECASE)
951
-
952
- _PSEUDO_CANON = {
953
- "web_search": "web_search", "websearch": "web_search", "search": "web_search",
954
- "generate_image": "generate_image", "image_generation": "generate_image",
955
- "generateimage": "generate_image",
956
- "browse_website": "browse_website", "browsewebsite": "browse_website",
957
- "update_memory": "update_memory", "updatememory": "update_memory",
958
- "create_document": "create_document", "createdocument": "create_document",
959
- }
960
-
961
- _PROVIDER_NOTE_RE = re.compile(
962
- r'^\s*Note\s*:\s*this (assistant )?message contained (tool[_ ]?calls|tools)',
963
- re.IGNORECASE)
964
-
965
-
966
- def _build_pseudo_args(name: str, arg: str) -> dict:
967
- arg = (arg or "").strip().strip('`').strip('"').strip()
968
- if name == "web_search":
969
- return {"query": arg[:300]}
970
- if name == "generate_image":
971
- return {"prompt": arg[:500]}
972
- if name == "browse_website":
973
- return {"url": arg[:2000]}
974
- if name == "update_memory":
975
- if ":" in arg:
976
- k, v = arg.split(":", 1)
977
- return {"key": k.strip().lower()[:40], "value": v.strip()[:500]}
978
- return {"key": "facts", "value": arg[:500]}
979
- if name == "create_document":
980
- fmt = "pdf"
981
- m = re.search(r'\|\s*format\s*[::]\s*(pdf|docx|pptx|xlsx)', arg, re.I)
982
- if m:
983
- fmt = m.group(1).lower()
984
- arg = arg[:m.start()].strip().rstrip('|').strip()
985
- return {"title": arg[:100] or "document", "format": fmt,
986
- "content": {"paragraphs": [arg]}}
987
- return {}
988
-
989
-
990
- # ═══════════════════════════════════════════════════════════
991
- # تنفيذ الأدوات
992
- # ═══════════════════════════════════════════════════════════
993
- async def execute_tool(name: str, args: dict, mem: dict) -> str:
994
- try:
995
- if name == "generate_image":
996
- return await generate_image_full(
997
- str(args.get("prompt", ""))[:500],
998
- max(256, min(int(args.get("width", 1024)), 2048)),
999
- max(256, min(int(args.get("height", 1024)), 2048)))
1000
- elif name == "web_search":
1001
- return await web_search(args.get("query", ""))
1002
- elif name == "browse_website":
1003
- return await browse_website(args.get("url", ""))
1004
- elif name == "update_memory":
1005
- _apply_memory_update(mem, args.get("key", "notes"), str(args.get("value", "")))
1006
- return json.dumps({"special": "memory", "ok": True,
1007
- "message": "تم حفظ المعلومة في ذاكرة Genisi"},
1008
- ensure_ascii=False)
1009
- elif name == "create_document":
1010
- content = args.get("content", {})
1011
- if isinstance(content, str):
1012
- try:
1013
- content = json.loads(content)
1014
- except Exception:
1015
- content = {"paragraphs": [content]}
1016
- if not isinstance(content, dict):
1017
- content = {"paragraphs": [str(content)]}
1018
- return await generate_document(
1019
- args.get("title", "document"), args.get("format", "pdf"), content)
1020
- return json.dumps({"error": "أداة غير معروفة"}, ensure_ascii=False)
1021
- except Exception as e:
1022
- return json.dumps({"error": f"خطأ في تنفيذ الأداة: {e}"}, ensure_ascii=False)
1023
-
1024
-
1025
- # ═══════════════════════════════════════════════════════════
1026
- # الاتصال بالنماذج
1027
- # ═══════════════════════════════════════════════════════════
1028
- def _get_client(provider: str):
1029
- if provider == "freeai":
1030
- return freeai_client
1031
- if provider == "zai":
1032
- return zai_client
1033
- if provider == "bai":
1034
- return bai_client
1035
- if provider == "huggingface":
1036
- if hf_client is None:
1037
- raise RuntimeError("HuggingFace غير متاح")
1038
- return hf_client
1039
- return polli
1040
-
1041
-
1042
- async def _open_stream(attempt: dict, msgs: list, use_tools: bool):
1043
- client = _get_client(attempt["provider"])
1044
- kwargs = dict(model=attempt["model"], messages=msgs, stream=True)
1045
- if attempt.get("temperature") is not None:
1046
- kwargs["temperature"] = attempt["temperature"]
1047
- if use_tools and attempt["provider"] not in FLATTEN_PROVIDERS:
1048
- kwargs["tools"] = TOOLS
1049
- kwargs["tool_choice"] = "auto"
1050
-
1051
- # ⭐ وضع التفكير — فقط على Z.ai (GLM يدعم thinking)
1052
- if attempt.get("thinking") and attempt["provider"] == "zai":
1053
- try:
1054
- return await client.chat.completions.create(
1055
- **kwargs,
1056
- extra_body={
1057
- "thinking": {"type": "enabled"},
1058
- "max_tokens": 16384,
1059
- })
1060
- except Exception as e:
1061
- msg = str(e).lower()
1062
- if "thinking" in msg or "extra" in msg or "400" in msg or "unrecognized" in msg:
1063
- print(f"[Genisi] thinking مرفوض، إعادة بدون تفكير: {e}")
1064
- return await client.chat.completions.create(**kwargs)
1065
- raise
1066
-
1067
- return await client.chat.completions.create(**kwargs)
1068
-
1069
-
1070
- async def _pick_working_stream(chain, start_idx, msgs, use_tools, tools_disabled_for):
1071
- last_err = None
1072
- for idx in range(start_idx, len(chain)):
1073
- attempt = chain[idx]
1074
- effective_tools = use_tools and (attempt["model"] not in tools_disabled_for)
1075
- try:
1076
- stream = await _open_stream(attempt, msgs, effective_tools)
1077
- return idx, stream, effective_tools
1078
- except Exception as e:
1079
- last_err = e
1080
- err_msg = str(e).lower()
1081
- if effective_tools and ("tool" in err_msg or "function" in err_msg):
1082
- tools_disabled_for.add(attempt["model"])
1083
- try:
1084
- stream = await _open_stream(attempt, msgs, False)
1085
- return idx, stream, False
1086
- except Exception as e2:
1087
- last_err = e2
1088
- continue
1089
- print(f"[Genisi] فشل '{attempt['model']}' على '{attempt['provider']}': {e}")
1090
- raise last_err if last_err else RuntimeError("تعذر الاتصال بأي نموذج")
1091
-
1092
-
1093
- # ═══════════════════════════════════════════════════════════
1094
- # مولّد SSE الرئيسي — متابعة حتى الاكتمال الكامل
1095
- # ومرشّح كتل الكود (in_code_fence) ⭐
1096
- # ═══════════════════════════════════════════════════════════
1097
- async def stream_genisi(messages: list, model_key: str,
1098
- attachments: List[Attachment] = None,
1099
- memory: Optional[dict] = None):
1100
- attachments = attachments or []
1101
- mem = _sanitize_memory(memory)
1102
- chain = FALLBACK_CHAINS.get(model_key) or FALLBACK_CHAINS["flash"]
1103
- system_prompt = SYSTEM_PROMPTS.get(model_key, SYSTEM_PROMPTS["flash"]) + _memory_block(mem)
1104
-
1105
- current_messages = [{"role": "system", "content": system_prompt}]
1106
- history = [m for m in (messages or []) if isinstance(m, dict) and m.get("role") != "system"]
1107
- history = history[-40:]
1108
- for i, m in enumerate(history):
1109
- is_last = (i == len(history) - 1)
1110
- if is_last and m.get("role") == "user" and attachments:
1111
- yield f"data: {json.dumps({'type': 'chunk', 'data': ''})}\n\n"
1112
- processed_text = await process_attachments(attachments, str(m.get("content", ""))[:20000])
1113
- current_messages.append({"role": "user", "content": processed_text})
1114
- else:
1115
- current_messages.append({"role": m.get("role", "user"),
1116
- "content": str(m.get("content", ""))[:20000]})
1117
-
1118
- use_tools_global = True
1119
- tools_disabled_for = set()
1120
- active_idx = 0
1121
- max_rounds = 24
1122
- CONTINUE_LIMIT = 12
1123
- cont_count = 0
1124
- emitted_cards = set()
1125
-
1126
- def _dedupe_cards(results: list) -> list:
1127
- out = []
1128
- for r in results:
1129
- try:
1130
- d = json.loads(r["result"]) if isinstance(r.get("result"), str) else r.get("result")
1131
- except Exception:
1132
- out.append(r)
1133
- continue
1134
- if isinstance(d, dict):
1135
- if d.get("special") == "document":
1136
- key = "doc:" + str(d.get("filename") or d.get("title") or "")
1137
- if key in emitted_cards:
1138
- continue
1139
- emitted_cards.add(key)
1140
- elif d.get("special") == "image":
1141
- imgs = d.get("images") or []
1142
- sig = ""
1143
- if imgs:
1144
- sig = imgs[0].get("url") or ("b64:" + str(imgs[0].get("b64", ""))[:80])
1145
- key = "img:" + sig
1146
- if sig and key in emitted_cards:
1147
- continue
1148
- if sig:
1149
- emitted_cards.add(key)
1150
- out.append(r)
1151
- return out
1152
-
1153
- def _append_tool_results(provider: str, visible: str, results: list, tool_args_list: list):
1154
- results_text = "\n\n".join(
1155
- f"[{r['name']} result]: {r['result'][:6000]}" for r in results)
1156
- if provider in FLATTEN_PROVIDERS:
1157
- if visible:
1158
- current_messages.append({"role": "assistant", "content": visible})
1159
- current_messages.append({
1160
- "role": "user",
1161
- "content": ("SYSTEM: Tools were executed. Here are the REAL results:\n\n"
1162
- + results_text
1163
- + "\n\nNow write ONLY your final answer in the user's language "
1164
- "using these results. Do NOT repeat or apologize for earlier "
1165
- "attempts. Never mention tool names or system notes.")
1166
- })
1167
- else:
1168
- current_messages.append({
1169
- "role": "assistant",
1170
- "content": visible if visible else None,
1171
- "tool_calls": [
1172
- {"id": r["id"], "type": "function",
1173
- "function": {"name": r["name"],
1174
- "arguments": json.dumps(tool_args_list[i], ensure_ascii=False)}}
1175
- for i, r in enumerate(results)
1176
- ]})
1177
- for r in results:
1178
- current_messages.append({"role": "tool", "tool_call_id": r["id"],
1179
- "content": r["result"]})
1180
-
1181
- for _round in range(max_rounds):
1182
- try:
1183
- picked_idx, stream, round_use_tools = await _pick_working_stream(
1184
- chain, active_idx, current_messages, use_tools_global, tools_disabled_for)
1185
- except Exception as e:
1186
- yield f"data: {json.dumps({'type': 'error', 'data': str(e)})}\n\n"
1187
- yield "data: [DONE]\n\n"
1188
- return
1189
- active_idx = picked_idx
1190
- provider = chain[active_idx]["provider"]
1191
- visible_content = ""
1192
- pending_line = ""
1193
- pseudo_calls = []
1194
- awaiting_arg_idx = None
1195
- in_code_fence = False # ✅ تتبع كتل الكود — لا مرشّح داخلها
1196
- tool_calls = []
1197
- mid_stream_error = None
1198
-
1199
- def _fence_toggle(line: str) -> bool:
1200
- """يرجع True إذا كان السطر حداً لبداية/نهاية كتلة كود"""
1201
- return bool(re.match(r'^\s*(```|~~~)', line))
1202
-
1203
- def _handle_line(line: str) -> str:
1204
- nonlocal awaiting_arg_idx, in_code_fence
1205
- # داخل كتلة كود: لا فلاتر إطلاقاً — الكود للمستخدم كما هو
1206
- if _fence_toggle(line):
1207
- in_code_fence = not in_code_fence
1208
- return line + "\n"
1209
- if in_code_fence:
1210
- return line + "\n"
1211
- if line.strip() == "[END]":
1212
- return ""
1213
- if _PROVIDER_NOTE_RE.match(line):
1214
- return ""
1215
- m = PSEUDO_TOOL_RE.match(line)
1216
- if m:
1217
- canon = _PSEUDO_CANON.get(m.group(1).lower())
1218
- if canon:
1219
- arg = m.group(2).strip()
1220
- pseudo_calls.append({"name": canon, "arg": arg})
1221
- if not arg:
1222
- awaiting_arg_idx = len(pseudo_calls) - 1
1223
- return ""
1224
- stripped = line.strip()
1225
- if awaiting_arg_idx is not None and stripped:
1226
- pseudo_calls[awaiting_arg_idx]["arg"] = stripped
1227
- awaiting_arg_idx = None
1228
- return ""
1229
- return line + "\n"
1230
-
1231
- try:
1232
- async for chunk in stream:
1233
- if not chunk.choices:
1234
- continue
1235
- choice = chunk.choices[0]
1236
- delta = choice.delta
1237
-
1238
- reasoning = getattr(delta, "reasoning_content", None) \
1239
- or getattr(delta, "reasoning", None)
1240
- if reasoning:
1241
- yield f"data: {json.dumps({'type': 'thinking', 'data': reasoning})}\n\n"
1242
-
1243
- if delta and delta.content:
1244
- pending_line += delta.content
1245
- while "\n" in pending_line:
1246
- line, pending_line = pending_line.split("\n", 1)
1247
- out = _handle_line(line)
1248
- if out:
1249
- visible_content += out
1250
- yield f"data: {json.dumps({'type': 'chunk', 'data': out})}\n\n"
1251
- if delta and getattr(delta, "tool_calls", None):
1252
- for tc_delta in delta.tool_calls:
1253
- tidx = tc_delta.index
1254
- while len(tool_calls) <= tidx:
1255
- tool_calls.append({"id": "",
1256
- "function": {"name": "", "arguments": ""}})
1257
- if tc_delta.id:
1258
- tool_calls[tidx]["id"] = tc_delta.id
1259
- if tc_delta.function:
1260
- if tc_delta.function.name:
1261
- tool_calls[tidx]["function"]["name"] = tc_delta.function.name
1262
- if tc_delta.function.arguments:
1263
- tool_calls[tidx]["function"]["arguments"] += tc_delta.function.arguments
1264
- except Exception as e:
1265
- mid_stream_error = e
1266
-
1267
- if pending_line:
1268
- out = _handle_line(pending_line)
1269
- if out:
1270
- visible_content += out
1271
- yield f"data: {json.dumps({'type': 'chunk', 'data': out})}\n\n"
1272
- pending_line = ""
1273
-
1274
- # ═══ انقطاع البث: أكمل من نفس النقطة حتى ينتهي تماماً ═══
1275
- if mid_stream_error:
1276
- print(f"[Genisi] انقطاع: {mid_stream_error}")
1277
-
1278
- if not visible_content and not tool_calls and not pseudo_calls:
1279
- if active_idx < len(chain) - 1:
1280
- active_idx += 1
1281
- continue
1282
- yield f"data: {json.dumps({'type': 'error', 'data': str(mid_stream_error)})}\n\n"
1283
- yield "data: [DONE]\n\n"
1284
- return
1285
-
1286
- if visible_content and cont_count < CONTINUE_LIMIT:
1287
- cont_count += 1
1288
- current_messages.append({"role": "assistant", "content": visible_content})
1289
- current_messages.append({
1290
- "role": "user",
1291
- "content": ("SYSTEM: Your previous response was cut off mid-stream. "
1292
- "Continue EXACTLY from where it stopped. Do NOT repeat any "
1293
- "text already written, do NOT add greetings — just continue "
1294
- "seamlessly and finish the ENTIRE answer. If you were in the "
1295
- "middle of a fenced code block, resume it as a continuation "
1296
- "of the SAME block, never open a new one. If it was already "
1297
- "complete, reply with exactly: [END]")
1298
- })
1299
- print(f"[Genisi] متابعة تلقائية ({cont_count}/{CONTINUE_LIMIT})...")
1300
- continue
1301
-
1302
- if visible_content:
1303
- current_messages.append({"role": "assistant", "content": visible_content})
1304
- yield "data: [DONE]\n\n"
1305
- return
1306
- yield f"data: {json.dumps({'type': 'error', 'data': str(mid_stream_error)})}\n\n"
1307
- yield "data: [DONE]\n\n"
1308
- return
1309
-
1310
- # ═══ الأدوات الزائفة → تنفيذ حقيقي ═══
1311
- if pseudo_calls and round_use_tools:
1312
- results = []
1313
- mem_updated = False
1314
- args_list = []
1315
- for pc in pseudo_calls:
1316
- args = _build_pseudo_args(pc["name"], pc["arg"])
1317
- res_str = await execute_tool(pc["name"], args, mem)
1318
- if pc["name"] == "update_memory":
1319
- mem_updated = True
1320
- results.append({"id": f"call_pseudo_{_round}_{len(results)}",
1321
- "name": pc["name"], "result": res_str})
1322
- args_list.append(args)
1323
-
1324
- shown = _dedupe_cards(results)
1325
- if shown:
1326
- yield f"data: {json.dumps({'type': 'tool_calls', 'data': shown})}\n\n"
1327
- if mem_updated:
1328
- yield f"data: {json.dumps({'type': 'memory', 'data': mem})}\n\n"
1329
-
1330
- _append_tool_results(provider, visible_content, results, args_list)
1331
- cont_count = 0
1332
- continue
1333
-
1334
- # ═══ الأدوات الحقيقية ═══
1335
- if tool_calls:
1336
- for i, tc in enumerate(tool_calls):
1337
- if not tc["id"]:
1338
- tc["id"] = f"call_{_round}_{i}"
1339
- named = [tc for tc in tool_calls if tc["function"]["name"]]
1340
- if named and round_use_tools:
1341
- results = []
1342
- mem_updated = False
1343
- args_list = []
1344
- for tc in named:
1345
- try:
1346
- args = json.loads(tc["function"]["arguments"] or "{}")
1347
- if not isinstance(args, dict):
1348
- args = {}
1349
- except Exception:
1350
- args = {}
1351
- res_str = await execute_tool(tc["function"]["name"], args, mem)
1352
- if tc["function"]["name"] == "update_memory":
1353
- mem_updated = True
1354
- results.append({"id": tc["id"], "name": tc["function"]["name"],
1355
- "result": res_str})
1356
- args_list.append(args)
1357
-
1358
- shown = _dedupe_cards(results)
1359
- if shown:
1360
- yield f"data: {json.dumps({'type': 'tool_calls', 'data': shown})}\n\n"
1361
- if mem_updated:
1362
- yield f"data: {json.dumps({'type': 'memory', 'data': mem})}\n\n"
1363
-
1364
- _append_tool_results(provider, visible_content, results, args_list)
1365
- cont_count = 0
1366
- continue
1367
- elif not round_use_tools:
1368
- current_messages.append({"role": "assistant", "content": visible_content})
1369
- yield "data: [DONE]\n\n"
1370
- return
1371
-
1372
- # ═══ رد مكتمل بنجاح ═══
1373
- if visible_content:
1374
- current_messages.append({"role": "assistant", "content": visible_content})
1375
- yield "data: [DONE]\n\n"
1376
- return
1377
- if active_idx < len(chain) - 1:
1378
- active_idx += 1
1379
- continue
1380
- yield f"data: {json.dumps({'type': 'error', 'data': 'رد فارغ من كل النماذج'})}\n\n"
1381
- yield "data: [DONE]\n\n"
1382
- return
1383
-
1384
- current_messages.append({"role": "assistant", "content": visible_content})
1385
- yield "data: [DONE]\n\n"
1386
-
1387
-
1388
- # ═══════════════════════════════════════════════════════════
1389
- # نقاط النهاية
1390
- # ═══════════════════════════════════════════════════════════
1391
- @app.api_route("/", methods=["GET", "HEAD"])
1392
- async def root():
1393
- index_path = os.path.join("static", "index.html")
1394
- if os.path.exists(index_path):
1395
- return FileResponse(index_path)
1396
- return Response(content="<h1>Genisi API</h1>", media_type="text/html")
1397
-
1398
-
1399
- @app.post("/api/chat/stream")
1400
- async def chat_stream(req: ChatRequest):
1401
- memory = req.memory
1402
- if memory is not None:
1403
- try:
1404
- if len(json.dumps(memory)) > MAX_MEMORY_JSON:
1405
- memory = {}
1406
- except Exception:
1407
- memory = {}
1408
- msgs = list(req.messages)[:60]
1409
- msgs.append({"role": "user", "content": req.message})
1410
- return StreamingResponse(
1411
- stream_genisi(msgs, req.model, req.attachments or [], memory),
1412
- media_type="text/event-stream",
1413
- headers={"Cache-Control": "no-cache", "Connection": "keep-alive",
1414
- "X-Accel-Buffering": "no"})
1415
-
1416
-
1417
- @app.post("/api/document")
1418
- async def download_document(req: Request):
1419
- data = await req.json()
1420
- title = str(data.get("title", "document"))[:200]
1421
- fmt = str(data.get("format", "pdf"))[:10]
1422
- content = data.get("content", {})
1423
- if not isinstance(content, dict):
1424
- content = {}
1425
- try:
1426
- content_bytes, media_type, filename = await asyncio.to_thread(
1427
- _build_document_bytes, title, fmt, content)
1428
- return Response(content=content_bytes, media_type=media_type,
1429
- headers={"Content-Disposition": f"attachment; filename={filename}"})
1430
- except Exception as e:
1431
- raise HTTPException(status_code=500, detail=str(e))
1432
-
1433
-
1434
- if __name__ == "__main__":
1435
- import uvicorn
1436
- uvicorn.run(app, host="0.0.0.0", port=7860)