File size: 22,272 Bytes
abfb9f0
dc9482e
 
 
 
 
 
 
 
abfb9f0
 
 
 
 
 
 
13b3a31
abfb9f0
 
dc9482e
abfb9f0
 
 
 
 
 
 
 
32fa524
dc9482e
 
 
abfb9f0
 
 
eb6c668
 
 
 
dc9482e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcd518f
dc9482e
 
 
fcd518f
dc9482e
 
 
fcd518f
dc9482e
 
 
 
 
 
 
 
 
 
 
fcd518f
dc9482e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcd518f
dc9482e
 
 
 
 
 
 
eb6c668
712c651
eb6c668
712c651
eb6c668
712c651
dc9482e
 
 
712c651
 
eb6c668
 
 
 
712c651
eb6c668
 
 
 
712c651
eb6c668
abfb9f0
 
712c651
abfb9f0
712c651
 
 
 
 
eb6c668
712c651
eb6c668
712c651
 
eb6c668
 
32fa524
712c651
32fa524
 
 
dc9482e
abfb9f0
 
 
dc9482e
eb6c668
dc9482e
 
 
 
abfb9f0
dc9482e
 
 
abfb9f0
dc9482e
 
abfb9f0
 
 
dc9482e
32fa524
 
abfb9f0
 
dc9482e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcd518f
dc9482e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712c651
dc9482e
 
 
712c651
13b3a31
 
32fa524
712c651
eb6c668
32fa524
dc9482e
 
eb6c668
abfb9f0
 
 
 
32fa524
abfb9f0
eb6c668
abfb9f0
 
eb6c668
abfb9f0
 
dc9482e
abfb9f0
 
 
 
32fa524
abfb9f0
32fa524
abfb9f0
eb6c668
 
 
712c651
abfb9f0
dc9482e
712c651
dc9482e
eb6c668
dc9482e
 
13b3a31
eb6c668
abfb9f0
 
dc9482e
abfb9f0
 
32fa524
712c651
 
dc9482e
 
712c651
 
dc9482e
712c651
 
 
 
 
dc9482e
712c651
dc9482e
 
712c651
 
dc9482e
eb6c668
abfb9f0
 
eb6c668
 
abfb9f0
 
eb6c668
dc9482e
eb6c668
 
dc9482e
eb6c668
abfb9f0
eb6c668
 
dc9482e
eb6c668
abfb9f0
32fa524
eb6c668
 
 
dc9482e
abfb9f0
 
 
 
712c651
eb6c668
dc9482e
abfb9f0
 
32fa524
abfb9f0
eb6c668
 
abfb9f0
 
dc9482e
 
 
 
eb6c668
abfb9f0
 
dc9482e
eb6c668
abfb9f0
 
dc9482e
abfb9f0
eb6c668
 
 
 
 
abfb9f0
712c651
 
 
dc9482e
 
 
712c651
dc9482e
712c651
 
eb6c668
dc9482e
 
 
 
 
 
 
 
 
 
 
abfb9f0
32fa524
eb6c668
abfb9f0
eb6c668
abfb9f0
 
 
eb6c668
abfb9f0
 
 
eb6c668
abfb9f0
 
dc9482e
 
 
 
eb6c668
 
 
dc9482e
 
abfb9f0
 
eb6c668
abfb9f0
 
dc9482e
abfb9f0
 
 
 
 
dc9482e
 
 
abfb9f0
dc9482e
 
712c651
eb6c668
 
dc9482e
 
abfb9f0
 
 
dc9482e
 
 
abfb9f0
 
 
 
 
eb6c668
32fa524
abfb9f0
 
dc9482e
abfb9f0
 
 
dc9482e
abfb9f0
 
 
dc9482e
 
13b3a31
 
dc9482e
 
 
 
13b3a31
 
dc9482e
 
 
 
 
 
 
 
13b3a31
abfb9f0
 
eb6c668
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
"""
THE Z AI โ€” Computer Mode Server v5 โ€” Smart Terminal
====================================================
ุงู„ู…ุดูƒู„ุฉ: curl ูŠูุดู„ ุฃุญูŠุงู†ุงู‹ุŒ ุงู„ุฐูƒุงุก ูŠุณุชุณู„ู…
ุงู„ุญู„ ููŠ ุงู„ุณูŠุฑูุฑ:
  - ุนู†ุฏู…ุง ูŠุฃุชูŠ ุฃู…ุฑ terminal โ†’ ู†ู†ูุฐู‡
  - ุฅุฐุง ูุดู„ ุฃูˆ ุฌุงุก ูุงุฑุบ โ†’ ู†ุฌุฑุจ ุจุฏุงุฆู„ ุชู„ู‚ุงุฆูŠุงู‹
  - ู†ุฑุฌุน ุงู„ู†ุชุงุฆุฌ ุงู„ุญู‚ูŠู‚ูŠุฉ ุฏุงุฆู…ุงู‹
  - ุฏุนู… 8+ ู…ุตุงุฏุฑ ุจุญุซ ุชู„ู‚ุงุฆูŠุฉ
"""

import asyncio
import base64
import io
import json
import os
import re
import subprocess
import time
import urllib.parse

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import uvicorn

DISPLAY = os.environ.get("DISPLAY", ":1")
os.environ["DISPLAY"] = DISPLAY

app = FastAPI(title="Z-Computer-Mode API v5")
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
                   allow_methods=["*"], allow_headers=["*"])

active_connections: list[WebSocket] = []
stream_active = False
stream_fps = 3
stream_quality = 60
stream_scale = 0.5

# โ”€โ”€โ”€ CURL HEADERS โ€” ูŠุนู…ู„ ู…ุน ุฌู…ูŠุน ุงู„ู…ูˆุงู‚ุน โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
CURL_HEADERS = (
    '-H "User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 '
    '(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" '
    '-H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" '
    '-H "Accept-Language: en-US,en;q=0.9,ar;q=0.8" '
    '-H "Accept-Encoding: gzip, deflate" '
    '--compressed '
    '--max-time 15 '
    '-L '  # follow redirects
    '-s '
)

# โ”€โ”€โ”€ ู…ุตุงุฏุฑ ุงู„ุจุญุซ โ€” 8 ู…ุตุงุฏุฑ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
def _build_search_sources(query: str) -> list[dict]:
    """ูŠุจู†ูŠ ู‚ุงุฆู…ุฉ ู…ุตุงุฏุฑ ุจุญุซ ุจุฏูŠู„ุฉ ู„ู„ูƒูŠูˆุฑู‰"""
    q = urllib.parse.quote_plus(query)
    q_raw = query.replace(' ', '+')
    return [
        {
            "name": "DuckDuckGo Instant",
            "cmd": f"curl -s 'https://api.duckduckgo.com/?q={q}&format=json&no_html=1&skip_disambig=1' | python3 -c \"import sys,json; d=json.load(sys.stdin); ans=d.get('AbstractText',''); rels=d.get('RelatedTopics',[]); print('ANSWER:',ans if ans else 'no direct answer'); [print('-',r.get('Text','')[:250]) for r in rels if isinstance(r,dict) and r.get('Text')]\""
        },
        {
            "name": "Google News RSS",
            "cmd": f"curl -sL {CURL_HEADERS} 'https://news.google.com/rss/search?q={q_raw}&hl=en&gl=US&ceid=US:en' | python3 -c \"import sys,re; xml=sys.stdin.read(); titles=re.findall(r'<title><!\\[CDATA\\[(.*?)\\]\\]></title>|<title>(.*?)</title>',xml); descs=re.findall(r'<description>(.*?)</description>',xml); clean=lambda s:re.sub('<[^>]+>','',s); results=[(a or b).strip() for a,b in titles if (a or b).strip()][1:9]; [print(str(i+1)+'. '+t[:180]) for i,t in enumerate(results)]\""
        },
        {
            "name": "Wikipedia English",
            "cmd": f"curl -s 'https://en.wikipedia.org/api/rest_v1/page/summary/{q}' | python3 -c \"import sys,json; d=json.load(sys.stdin); print(d.get('title','')+'\\n'+d.get('extract','')[:1500])\" 2>/dev/null || curl -s 'https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch={q}&format=json&srlimit=5' | python3 -c \"import sys,json; d=json.load(sys.stdin); [print(str(i+1)+'. '+r['title']+': '+re.sub('<[^>]+>','',r.get('snippet',''))[:200]) for i,r in enumerate(d.get('query',{{}}).get('search',[]))]\" 2>/dev/null"
        },
        {
            "name": "DuckDuckGo HTML",
            "cmd": f"curl -sL {CURL_HEADERS} 'https://html.duckduckgo.com/html/?q={q}' | python3 -c \"import sys,re; h=sys.stdin.read(); snippets=re.findall(r'class=.result__snippet[^>]*>(.*?)</a>',h,re.DOTALL); titles=re.findall(r'class=.result__title[^>]*>.*?<a[^>]*>(.*?)</a>',h,re.DOTALL); clean=lambda s:re.sub('<[^>]+>','',s).strip(); [print(str(i+1)+'. '+clean(titles[i] if i<len(titles) else '')+'\\n   '+clean(s)[:200]) for i,s in enumerate(snippets[:7])]\""
        },
        {
            "name": "Bing News RSS",
            "cmd": f"curl -sL {CURL_HEADERS} 'https://www.bing.com/news/search?q={q}&format=RSS' | python3 -c \"import sys,re; xml=sys.stdin.read(); items=re.findall(r'<item>(.*?)</item>',xml,re.DOTALL); [print(str(i+1)+'. '+re.sub('<[^>]+>','',re.search(r'<title>(.*?)</title>',it).group(1) if re.search(r'<title>',it) else '')+'\\n   '+re.sub('<[^>]+>','',re.search(r'<description>(.*?)</description>',it).group(1) if re.search(r'<description>',it) else '')[:150]) for i,it in enumerate(items[:6])]\""
        },
        {
            "name": "Reddit Search",
            "cmd": f"curl -sL {CURL_HEADERS} -H 'Accept: application/json' 'https://www.reddit.com/search.json?q={q}&sort=new&limit=8&type=link' | python3 -c \"import sys,json; d=json.load(sys.stdin); posts=d.get('data',{{}}).get('children',[]); [print(str(i+1)+'. '+p['data'].get('title','')+'\\n   r/'+p['data'].get('subreddit','')+'  Score:'+str(p['data'].get('score',''))+'\\n   '+p['data'].get('selftext','')[:200]) for i,p in enumerate(posts[:6])]\""
        },
        {
            "name": "HackerNews",
            "cmd": f"curl -s 'https://hn.algolia.com/api/v1/search?query={q}&hitsPerPage=8&tags=story' | python3 -c \"import sys,json; d=json.load(sys.stdin); hits=d.get('hits',[]); [print(str(i+1)+'. '+h.get('title','')+'\\n   Points:'+str(h.get('points','0'))+' | '+h.get('url','')[:80]) for i,h in enumerate(hits[:6])]\""
        },
        {
            "name": "ArXiv (academic)",
            "cmd": f"curl -s 'https://export.arxiv.org/api/query?search_query=all:{q}&start=0&max_results=5' | python3 -c \"import sys,re; xml=sys.stdin.read(); titles=re.findall(r'<title>(.*?)</title>',xml)[1:]; summaries=re.findall(r'<summary>(.*?)</summary>',xml,re.DOTALL); [print(str(i+1)+'. '+t.strip()+'\\n   '+summaries[i].strip()[:200] if i<len(summaries) else '') for i,t in enumerate(titles[:5])]\""
        },
    ]


def _extract_query_from_cmd(cmd: str) -> str:
    """ูŠุณุชุฎุฑุฌ ูƒู„ู…ุงุช ุงู„ุจุญุซ ู…ู† ุฃู…ุฑ curl"""
    patterns = [
        r'[?&]q=([^&\'"]+)',
        r'[?&]query=([^&\'"]+)',
        r'[?&]search_query=all:([^&\'"]+)',
        r"search\?q=([^&'\"\s]+)",
    ]
    for p in patterns:
        m = re.search(p, cmd)
        if m:
            q = m.group(1).replace('+', ' ').replace('%20', ' ')
            return urllib.parse.unquote(q)
    return ""


def _is_empty_result(stdout: str) -> bool:
    """ูŠุชุญู‚ู‚ ุฅุฐุง ูƒุงู†ุช ุงู„ู†ุชูŠุฌุฉ ูุงุฑุบุฉ ุฃูˆ ุจู„ุง ู…ุนู„ูˆู…ุงุช"""
    if not stdout or len(stdout.strip()) < 10:
        return True
    empty_signals = [
        "no direct answer",
        "no results",
        "0 results",
        "no items",
        "[]",
        "{}",
        "error",
        "not found",
        "answer: no",
    ]
    s = stdout.strip().lower()
    meaningful_lines = [l for l in s.split('\n') if l.strip() and not any(sig in l for sig in empty_signals)]
    if len(meaningful_lines) < 1:
        return True
    return False


# โ”€โ”€โ”€ Screen Capture โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def capture_screen(scale=0.6, quality=65) -> str:
    try:
        tmp = f"/tmp/zs_{int(time.time()*1000)}.png"
        r = subprocess.run(["scrot", "-q", "90", tmp],
                           env={**os.environ, "DISPLAY": DISPLAY},
                           timeout=8, capture_output=True)
        if r.returncode != 0 or not os.path.exists(tmp):
            return _capture_xwd(scale, quality)
        from PIL import Image
        img = Image.open(tmp)
        os.unlink(tmp)
        if scale < 1.0:
            img = img.resize((int(img.width*scale), int(img.height*scale)), Image.LANCZOS)
        buf = io.BytesIO()
        img.convert("RGB").save(buf, format="JPEG", quality=quality, optimize=True)
        return base64.b64encode(buf.getvalue()).decode()
    except Exception as e:
        print(f"[capture] {e}")
        return ""


def _capture_xwd(scale=0.6, quality=65) -> str:
    try:
        xwd = f"/tmp/zs_{int(time.time()*1000)}.xwd"
        png = xwd.replace(".xwd", ".png")
        subprocess.run(["xwd", "-root", "-silent", "-out", xwd],
                       env={**os.environ, "DISPLAY": DISPLAY}, timeout=8, capture_output=True)
        subprocess.run(["convert", xwd, png], timeout=8, capture_output=True)
        from PIL import Image
        if not os.path.exists(png):
            return ""
        img = Image.open(png)
        for f in [xwd, png]:
            try: os.unlink(f)
            except: pass
        if scale < 1.0:
            img = img.resize((int(img.width*scale), int(img.height*scale)), Image.LANCZOS)
        buf = io.BytesIO()
        img.convert("RGB").save(buf, format="JPEG", quality=quality, optimize=True)
        return base64.b64encode(buf.getvalue()).decode()
    except:
        return ""


# โ”€โ”€โ”€ Command Runner โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

def run_raw_command(cmd: str, timeout: int = 45) -> dict:
    """ูŠู†ูุฐ ุฃู…ุฑ bash ุฎุงู…"""
    env = {**os.environ, "DISPLAY": DISPLAY,
           "PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"}
    try:
        result = subprocess.run(cmd, shell=True, capture_output=True,
                                text=True, timeout=timeout, env=env,
                                executable="/bin/bash")
        return {
            "stdout": result.stdout[-15000:],
            "stderr": result.stderr[-3000:],
            "returncode": result.returncode,
        }
    except subprocess.TimeoutExpired:
        return {"stdout": "", "stderr": f"โฑ๏ธ Timeout {timeout}s", "returncode": -1}
    except Exception as e:
        return {"stdout": "", "stderr": str(e), "returncode": -1}


def run_command_smart(cmd: str, timeout: int = 45) -> dict:
    """
    ูŠู†ูุฐ ุงู„ุฃู…ุฑ โ€” ุฅุฐุง ูƒุงู† curl ุจุญุซ ูˆูุดู„/ูุฑุบ โ†’ ูŠุฌุฑุจ ู…ุตุงุฏุฑ ุจุฏูŠู„ุฉ ุชู„ู‚ุงุฆูŠุงู‹
    ูŠุฑุฌุน ุงู„ู†ุชูŠุฌุฉ ุงู„ุฃูˆู„ู‰ ุงู„ุชูŠ ุชุญุชูˆูŠ ู…ุนู„ูˆู…ุงุช ุญู‚ูŠู‚ูŠุฉ
    """
    res = run_raw_command(cmd, timeout=timeout)
    stdout = res["stdout"].strip()

    if res["returncode"] == 0 and not _is_empty_result(stdout):
        return res

    is_curl_search = "curl" in cmd and any(x in cmd for x in [
        "duckduckgo", "google", "bing", "wikipedia", "reddit",
        "hackernews", "hn.algolia", "arxiv", "news", "search"
    ])

    if not is_curl_search:
        return res

    query = _extract_query_from_cmd(cmd)
    if not query or len(query) < 3:
        words = [w for w in cmd.split() if len(w) > 3 and not w.startswith('-')
                 and 'http' not in w and 'python3' not in w and 'curl' not in w]
        query = ' '.join(words[:5]) if words else ""

    if not query:
        return res

    sources = _build_search_sources(query)
    tried_names = []
    all_results = []

    for source in sources:
        src_name = source["name"]
        if any(x in cmd.lower() for x in [src_name.lower().split()[0]]):
            tried_names.append(f"โญ๏ธ {src_name} (ู…ุฌุฑุจ)")
            continue

        tried_names.append(f"๐Ÿ” {src_name}")
        src_res = run_raw_command(source["cmd"], timeout=20)
        src_out = src_res["stdout"].strip()

        if not _is_empty_result(src_out):
            combined_header = (
                f"[ุชุนุฐุฑ ุงู„ุญุตูˆู„ ุนู„ู‰ ู†ุชุงุฆุฌ ู…ู† ุงู„ู…ุตุฏุฑ ุงู„ุฃุตู„ูŠ]\n"
                f"[ุงู„ุจุฏูŠู„ ุงู„ู…ุณุชุฎุฏู…: {src_name}]\n"
                f"{'='*50}\n"
            )
            all_results.append(src_out)
            for source2 in sources:
                if source2["name"] != src_name:
                    s2 = run_raw_command(source2["cmd"], timeout=15)
                    s2_out = s2["stdout"].strip()
                    if not _is_empty_result(s2_out):
                        all_results.append(f"\n[ู…ุตุฏุฑ ุฅุถุงููŠ: {source2['name']}]\n{s2_out}")
                        break

            final_out = combined_header + "\n\n".join(all_results)
            return {
                "stdout": final_out[:15000],
                "stderr": "",
                "returncode": 0,
                "_sources_tried": tried_names,
                "_fallback_used": src_name,
            }

    return {
        "stdout": f"[ู„ู… ุชูุฑุฌุน ุฃูŠ ู…ุตุงุฏุฑ ู†ุชุงุฆุฌ ู„ู€: {query}]\nุงู„ู…ุตุงุฏุฑ ุงู„ุชูŠ ุฌุฑุจุชู‡ุง: {', '.join(tried_names[:5])}",
        "stderr": res["stderr"],
        "returncode": -1,
        "_sources_tried": tried_names,
    }


def xdo(args: list, timeout=10) -> dict:
    r = subprocess.run(["xdotool"] + args,
                       env={**os.environ, "DISPLAY": DISPLAY},
                       timeout=timeout, capture_output=True, text=True)
    return {"rc": r.returncode, "out": r.stdout, "err": r.stderr}


async def broadcast(msg: dict):
    txt = json.dumps(msg, ensure_ascii=False)
    dead = []
    for ws in active_connections:
        try: await ws.send_text(txt)
        except: dead.append(ws)
    for ws in dead:
        if ws in active_connections:
            active_connections.remove(ws)


async def screen_stream_loop():
    global stream_active
    interval = 1.0 / max(1, stream_fps)
    while stream_active and active_connections:
        try:
            frame = capture_screen(scale=stream_scale, quality=stream_quality)
            if frame:
                await broadcast({"type": "frame", "data": frame})
        except: pass
        await asyncio.sleep(interval)
    stream_active = False


# โ”€โ”€โ”€ Action Handler โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

async def handle_action(ws: WebSocket, msg: dict):
    action = msg.get("action", "")
    data = msg.get("data", {})

    async def send(obj):
        await ws.send_text(json.dumps(obj, ensure_ascii=False))

    async def auto_shot(label="", delay=0.4):
        await asyncio.sleep(delay)
        frame = capture_screen(scale=0.7, quality=68)
        if frame:
            await send({"type": "screenshot", "data": frame,
                        "ts": int(time.time()*1000), "auto": True, "label": label})
        return frame

    if action == "screenshot":
        frame = capture_screen(scale=0.8, quality=75)
        await send({"type": "screenshot", "data": frame, "ts": int(time.time()*1000)})

    elif action == "terminal":
        cmd = data.get("cmd", "")
        timeout = int(data.get("timeout", 45))
        if not cmd:
            await send({"type": "terminal_result", "cmd": "", "stdout": "",
                        "stderr": "no command", "returncode": -1})
            return

        res = run_command_smart(cmd, timeout=timeout)

        await send({
            "type": "terminal_result",
            "cmd": cmd,
            "stdout": res["stdout"],
            "stderr": res.get("stderr", ""),
            "returncode": res["returncode"],
            "fallback_used": res.get("_fallback_used", None),
            "sources_tried": res.get("_sources_tried", []),
        })

        await auto_shot(f"ุจุนุฏ: {cmd[:45]}", delay=0.3)

    elif action == "mouse_move":
        x, y = int(data.get("x", 0)), int(data.get("y", 0))
        xdo(["mousemove", str(x), str(y)])
        await send({"type": "ack", "action": "mouse_move"})

    elif action == "mouse_click":
        x, y = int(data.get("x", 0)), int(data.get("y", 0))
        btn_num = {"left": "1", "middle": "2", "right": "3"}.get(data.get("button","left"), "1")
        xdo(["mousemove", str(x), str(y)])
        await asyncio.sleep(0.05)
        if data.get("double"):
            xdo(["click", "--repeat", "2", "--delay", "100", btn_num])
        else:
            xdo(["click", btn_num])
        await send({"type": "ack", "action": "mouse_click"})
        await auto_shot("ุจุนุฏ ุงู„ู†ู‚ุฑ", 0.4)

    elif action == "keyboard_type":
        text = data.get("text", "")
        if text:
            xdo(["type", "--clearmodifiers", "--delay", "30", text])
        await send({"type": "ack", "action": "keyboard_type"})
        await auto_shot("ุจุนุฏ ุงู„ูƒุชุงุจุฉ", 0.3)

    elif action == "keyboard_hotkey":
        keys = data.get("keys", [])
        if keys:
            xdo(["key", "--clearmodifiers", "+".join(keys)])
        await send({"type": "ack", "action": "keyboard_hotkey"})
        await auto_shot("ุจุนุฏ ุงู„ุงุฎุชุตุงุฑ", 0.4)

    elif action == "keyboard_press":
        key = data.get("key", "")
        if key:
            xdo(["key", "--clearmodifiers", key])
        await send({"type": "ack", "action": "keyboard_press"})

    elif action == "clipboard_write":
        proc = subprocess.Popen(["xclip", "-selection", "clipboard"],
                                stdin=subprocess.PIPE,
                                env={**os.environ, "DISPLAY": DISPLAY})
        proc.communicate(data.get("text","").encode())
        await send({"type": "ack", "action": "clipboard_write"})

    elif action == "clipboard_read":
        res = run_raw_command("xclip -selection clipboard -o", timeout=5)
        await send({"type": "clipboard_content", "text": res["stdout"]})

    elif action == "scroll":
        x, y = int(data.get("x", 0)), int(data.get("y", 0))
        clicks = int(data.get("clicks", 3))
        btn = "4" if clicks > 0 else "5"
        xdo(["mousemove", str(x), str(y)])
        for _ in range(abs(clicks)):
            xdo(["click", btn])
        await send({"type": "ack", "action": "scroll"})

    elif action == "open_app":
        app_cmd = data.get("cmd", "")
        if app_cmd:
            subprocess.Popen(app_cmd, shell=True,
                             env={**os.environ, "DISPLAY": DISPLAY},
                             stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
            await send({"type": "ack", "action": "open_app", "cmd": app_cmd})
            await auto_shot(f"ุจุนุฏ ูุชุญ: {app_cmd}", 3.0)
        else:
            await send({"type": "ack", "action": "open_app"})

    elif action == "mouse_drag":
        x1,y1 = int(data.get("x1",0)), int(data.get("y1",0))
        x2,y2 = int(data.get("x2",0)), int(data.get("y2",0))
        xdo(["mousemove", str(x1), str(y1)])
        xdo(["mousedown", "1"])
        await asyncio.sleep(0.1)
        xdo(["mousemove", str(x2), str(y2)])
        await asyncio.sleep(0.1)
        xdo(["mouseup", "1"])
        await send({"type": "ack", "action": "mouse_drag"})

    elif action == "start_stream":
        global stream_active, stream_fps, stream_quality, stream_scale
        stream_fps = int(data.get("fps", 3))
        stream_quality = int(data.get("quality", 60))
        stream_scale = float(data.get("scale", 0.5))
        if not stream_active:
            stream_active = True
            asyncio.create_task(screen_stream_loop())
        await send({"type": "ack", "action": "start_stream"})

    elif action == "stop_stream":
        stream_active = False
        await send({"type": "ack", "action": "stop_stream"})

    elif action == "screen_info":
        res = run_raw_command("xdotool getdisplaygeometry", timeout=5)
        try: parts = res["stdout"].strip().split(); w,h = int(parts[0]),int(parts[1])
        except: w,h = 1920,1080
        res2 = run_raw_command("xdotool getmouselocation", timeout=5)
        try:
            mx = int(re.search(r"x:(\d+)", res2["stdout"]).group(1))
            my = int(re.search(r"y:(\d+)", res2["stdout"]).group(1))
        except: mx,my = 0,0
        await send({"type":"screen_info","width":w,"height":h,"mouse_x":mx,"mouse_y":my})

    else:
        await send({"type": "error", "msg": f"Unknown action: {action}"})


# โ”€โ”€โ”€ WebSocket โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

@app.websocket("/ws")
async def websocket_endpoint(ws: WebSocket):
    await ws.accept()
    active_connections.append(ws)
    res = run_raw_command("xdotool getdisplaygeometry", timeout=5)
    try: parts = res["stdout"].strip().split(); w,h = int(parts[0]),int(parts[1])
    except: w,h = 1920,1080
    await ws.send_text(json.dumps({
        "type": "connected", "screen_width": w, "screen_height": h,
        "msg": "Z Computer Mode v5 โ€” Smart Terminal + Multi-Source Search"
    }, ensure_ascii=False))
    frame = capture_screen(scale=0.75, quality=72)
    if frame:
        await ws.send_text(json.dumps({"type":"screenshot","data":frame,
                                        "ts":int(time.time()*1000),"label":"ุงู„ุดุงุดุฉ ุงู„ุฃูˆู„ูŠุฉ"}))
    try:
        while True:
            raw = await ws.receive_text()
            await handle_action(ws, json.loads(raw))
    except WebSocketDisconnect: pass
    except Exception as e: print(f"[ws] {e}")
    finally:
        if ws in active_connections:
            active_connections.remove(ws)


# โ”€โ”€โ”€ REST โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

@app.get("/screenshot")
async def rest_screenshot():
    return JSONResponse({"image": capture_screen(0.75,70), "ts": int(time.time()*1000)})

@app.post("/terminal")
async def rest_terminal(body: dict):
    return JSONResponse(run_command_smart(body.get("cmd",""), body.get("timeout",45)))

@app.get("/health")
async def health():
    display = run_raw_command("xdotool getdisplaygeometry", 4)
    test = run_command_smart("curl -s 'https://api.duckduckgo.com/?q=test&format=json&no_html=1'", 15)
    return {
        "status": "ok",
        "version": "v5-smart-terminal",
        "display_working": display["returncode"] == 0,
        "search_working": test["returncode"] == 0,
        "fallback_used": test.get("_fallback_used"),
    }

@app.get("/search/{query}")
async def quick_search(query: str):
    res = run_command_smart(
        f"curl -s 'https://api.duckduckgo.com/?q={urllib.parse.quote_plus(query)}&format=json&no_html=1'",
        timeout=30
    )
    return JSONResponse(res)


if __name__ == "__main__":
    port = int(os.environ.get("PORT", 7860))
    uvicorn.run("app:app", host="0.0.0.0", port=port, log_level="info")