File size: 25,580 Bytes
f343084
 
 
aca7d3d
f343084
 
 
 
86a6197
8368b8b
f343084
86a6197
f343084
 
 
 
aca7d3d
9baa398
 
86a6197
f343084
 
9892596
 
 
 
 
 
 
 
f343084
 
bd2c875
9892596
f343084
aca7d3d
 
 
 
 
 
 
 
 
69eeb04
f343084
aca7d3d
f343084
 
69eeb04
f343084
69eeb04
aca7d3d
 
 
 
 
 
 
 
69eeb04
 
 
 
 
aca7d3d
 
f343084
 
 
 
69eeb04
 
 
f343084
 
aca7d3d
 
 
 
 
 
 
 
d04ae9a
 
69eeb04
 
d04ae9a
aca7d3d
69eeb04
aca7d3d
69eeb04
bd2c875
aca7d3d
69eeb04
bd2c875
69eeb04
 
 
 
bd2c875
69eeb04
bd2c875
aca7d3d
bd2c875
69eeb04
bd2c875
aca7d3d
 
 
 
 
 
 
 
 
 
 
69eeb04
 
 
 
 
 
 
 
 
aca7d3d
 
 
 
 
 
 
69eeb04
157b8da
69eeb04
157b8da
69eeb04
 
157b8da
69eeb04
 
 
 
 
 
 
 
 
aca7d3d
69eeb04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157b8da
 
 
 
 
 
 
 
 
69eeb04
157b8da
 
69eeb04
 
157b8da
69eeb04
 
 
157b8da
 
 
69eeb04
157b8da
69eeb04
157b8da
 
69eeb04
157b8da
 
69eeb04
 
 
157b8da
 
 
 
 
69eeb04
157b8da
 
69eeb04
 
157b8da
 
 
 
 
 
 
 
69eeb04
157b8da
69eeb04
 
 
 
 
157b8da
 
 
69eeb04
157b8da
 
 
 
aca7d3d
69eeb04
 
157b8da
 
 
69eeb04
 
 
157b8da
69eeb04
157b8da
69eeb04
157b8da
69eeb04
 
157b8da
 
 
 
69eeb04
157b8da
69eeb04
 
 
 
aca7d3d
 
157b8da
69eeb04
 
 
 
 
157b8da
 
69eeb04
 
 
 
 
157b8da
 
 
 
 
69eeb04
 
 
 
 
 
 
 
 
157b8da
69eeb04
 
d04ae9a
bd2c875
69eeb04
 
 
d04ae9a
69eeb04
9b00487
69eeb04
9baa398
69eeb04
9baa398
 
69eeb04
aca7d3d
86a6197
56ab5c2
69eeb04
 
9baa398
 
d04ae9a
69eeb04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca7d3d
9baa398
 
 
 
 
 
 
 
 
 
 
 
 
86a6197
69eeb04
aca7d3d
f343084
aca7d3d
69eeb04
f343084
9892596
69eeb04
 
d04ae9a
69eeb04
d04ae9a
9baa398
d04ae9a
f343084
69eeb04
 
f343084
 
8368b8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f343084
9892596
f343084
 
d04ae9a
 
 
69eeb04
 
 
 
 
d04ae9a
 
 
9892596
d04ae9a
 
 
 
 
 
 
9baa398
 
 
d04ae9a
9892596
d04ae9a
 
 
 
 
9892596
 
f343084
 
 
 
 
 
 
 
9b00487
f343084
9b00487
f343084
9b00487
 
 
69eeb04
9b00487
 
 
9892596
 
 
 
 
 
9b00487
f343084
 
 
 
 
 
6304f51
 
 
 
f343084
 
7a433fd
 
 
 
 
 
 
 
9892596
 
d04ae9a
 
9892596
 
 
 
9b00487
f343084
56ab5c2
 
9892596
 
 
9b00487
f343084
 
56ab5c2
 
9892596
 
 
9b00487
f343084
 
 
 
9b00487
f343084
9892596
f343084
 
8368b8b
 
 
 
 
 
f343084
 
 
 
 
9b00487
f343084
 
7a433fd
f343084
 
9b00487
f343084
9892596
 
 
 
 
 
bd2c875
f343084
9892596
69eeb04
9892596
 
f343084
9892596
9baa398
 
f343084
 
 
 
 
 
 
9b00487
f343084
 
 
 
 
 
 
 
 
 
 
9b00487
f343084
9892596
f343084
 
 
 
 
9892596
 
 
 
bd2c875
f343084
9892596
69eeb04
9892596
9b00487
 
9892596
9b00487
f343084
56ab5c2
 
 
 
 
 
9b00487
f343084
 
56ab5c2
 
f343084
9b00487
 
f343084
 
9892596
9baa398
 
f343084
 
 
 
 
 
 
 
56ab5c2
f343084
 
 
 
7a433fd
 
 
 
aca7d3d
 
9baa398
86a6197
 
 
9baa398
 
aca7d3d
 
86a6197
6304f51
86a6197
 
 
 
 
 
 
9baa398
 
8368b8b
86a6197
 
 
 
 
 
 
6304f51
f343084
 
7a433fd
 
 
8368b8b
 
 
 
 
7a433fd
 
 
86a6197
7a433fd
 
 
 
 
 
 
86a6197
 
 
 
f343084
 
d04ae9a
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
import os
import uuid
import time
import random
import asyncio
import threading
import json
import re
import psutil
import httpx
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, HTMLResponse

# ====================================================================
# Configuration
# ====================================================================
API_SECRET_KEY = os.getenv("API_SECRET_KEY", "change-me-secret")
POOL_SIZE      = int(os.getenv("POOL_SIZE", "5"))
MAX_REQUESTS   = int(os.getenv("MAX_REQUESTS", "30"))
QUEUE_TIMEOUT  = int(os.getenv("QUEUE_TIMEOUT", "300"))

DUCK_MODELS = {
    "gpt-5-mini":       "GPT-5 mini",
    "gpt-5":            "GPT-5",
    "gpt-4o-mini":      "GPT-4o mini",
    "o3-mini":          "o3 mini",
    "gpt-oss-120b":     "gpt-oss 120B",
    "claude-haiku-4-5": "Claude Haiku 4.5",
    "llama-4-scout":    "Llama 4 Scout",
    "mistral-small-4":  "Mistral Small 4",
}

ALL_MODELS    = list(DUCK_MODELS.keys())
DEFAULT_MODEL = "gpt-5-mini"

USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
]


# ====================================================================
# Browser Worker
# ====================================================================

class BrowserWorker:

    def __init__(self, worker_id: int, loop: asyncio.AbstractEventLoop):
        self.id             = worker_id
        self.loop           = loop
        self.context        = None
        self._browser       = None
        self.busy           = False
        self._lock          = asyncio.Lock()
        self._request_count = 0
        self._total_count   = 0

    def tag(self, msg: str) -> str:
        return f"[W{self.id}] {msg}"

    async def init(self, playwright):
        self._playwright = playwright
        self._browser = await playwright.chromium.launch(
            headless=True,
            channel="chrome",
            args=[
                "--disable-blink-features=AutomationControlled",
                "--no-sandbox", "--disable-gpu",
                "--disable-dev-shm-usage", "--disable-setuid-sandbox",
                "--single-process", "--no-zygote",
            ],
        )
        await self._create_context()
        await self._accept_terms()
        print(self.tag("Ready โœ“"))

    async def _create_context(self, ua: str = None):
        user_agent = ua or random.choice(USER_AGENTS)
        self.context = await self._browser.new_context(
            user_agent=user_agent,
            viewport={"width": 1920, "height": 1080},
        )
        await self.context.add_init_script(
            "Object.defineProperty(navigator,'webdriver',{get:()=>undefined})"
        )
        print(self.tag(f"Context created (UA: ...{user_agent[-30:]}) โœ“"))

    async def _accept_terms(self):
        page = await self.context.new_page()
        try:
            print(self.tag("Accepting terms..."))
            await page.goto("https://duckduckgo.com/aichat", wait_until="domcontentloaded")
            await asyncio.sleep(5)
            agree = page.locator('button:has-text("Agree and Continue")')
            await agree.wait_for(state="visible", timeout=15000)
            await agree.click()
            print(self.tag("Terms accepted โœ“"))
            await asyncio.sleep(3)
            await page.wait_for_selector('textarea[name="user-prompt"]', timeout=20000)
        except Exception as e:
            print(self.tag(f"Terms note: {e}"))
        finally:
            await page.close()

    async def _rotate_context(self):
        print(self.tag(f"Rotating context after {self._request_count} requests..."))
        try:
            await self.context.close()
        except Exception:
            pass
        await self._create_context(ua=random.choice(USER_AGENTS))
        await self._accept_terms()
        self._request_count = 0
        print(self.tag("Context rotated โœ“"))

    async def chat(self, model_label: str, prompt: str) -> str:
        async with self._lock:
            self.busy = True
            try:
                return await self._do_chat(model_label, prompt)
            finally:
                self.busy = False

    async def _do_chat(self, model_label: str, prompt: str) -> str:
        await asyncio.sleep(random.uniform(0.5, 2.0))

        self._request_count += 1
        self._total_count   += 1
        if self._request_count >= MAX_REQUESTS:
            await self._rotate_context()

        page = await self.context.new_page()
        try:
            page.set_default_timeout(180000)

            await page.goto("https://duckduckgo.com/aichat", wait_until="domcontentloaded")
            await asyncio.sleep(3)

            try:
                agree = page.locator('button:has-text("Agree and Continue")')
                if await agree.count() > 0 and await agree.first.is_visible():
                    await agree.first.click()
                    await asyncio.sleep(2)
            except Exception:
                pass

            await page.wait_for_selector('textarea[name="user-prompt"]')
            print(self.tag(f"Input ready (req #{self._total_count}) โœ“"))

            await self._select_model(page, model_label)
            await self._send(page, prompt)
            return await self._extract(page)

        except Exception as e:
            print(self.tag(f"Error: {e}"))
            raise RuntimeError(f"duck.ai error: {e}")
        finally:
            await page.close()

    async def _select_model(self, page, model_label: str):
        try:
            btn  = page.locator('button[data-testid="model-select-button"]')
            text = (await btn.inner_text()).strip()
            print(self.tag(f"Current model: {text}"))
            if model_label.lower() in text.lower():
                return

            await btn.click()
            await asyncio.sleep(2)

            option = page.locator(
                f'li:has-text("{model_label}"), '
                f'[role="option"]:has-text("{model_label}"), '
                f'button:has-text("{model_label}")'
            )
            if await option.count() > 0:
                await option.first.click()
                print(self.tag(f"Model โ†’ {model_label} โœ“"))
                await asyncio.sleep(1)

                start = page.locator(
                    'button:has-text("Start chat"), button:has-text("Start new chat")'
                )
                if await start.count() > 0:
                    await start.first.click()
                    print(self.tag("Start chat โœ“"))
                    await asyncio.sleep(2)

                await page.wait_for_selector(
                    'textarea[name="user-prompt"]', state="visible", timeout=15000
                )
                print(self.tag("Textarea ready โœ“"))
            else:
                await page.keyboard.press("Escape")
                print(self.tag("Model not found, using default"))

        except Exception as e:
            print(self.tag(f"Model select (non-fatal): {e}"))

    async def _send(self, page, prompt: str):
        await page.evaluate(
            """
            (text) => {
                const ta = document.querySelector('textarea[name="user-prompt"]');
                if (!ta) return;
                const setter = Object.getOwnPropertyDescriptor(
                    window.HTMLTextAreaElement.prototype, 'value'
                ).set;
                setter.call(ta, text);
                ta.dispatchEvent(new InputEvent('input', { bubbles: true }));
                ta.dispatchEvent(new Event('change', { bubbles: true }));
                ta.focus();
            }
            """,
            prompt
        )
        await asyncio.sleep(2)

        sent = False
        try:
            btn = page.locator('button[type="submit"][aria-label="Send"]')
            if await btn.get_attribute("disabled") is None:
                await btn.click()
                sent = True
                print(self.tag(f"Sent via button โœ“ ({len(prompt)} chars)"))
        except Exception:
            pass

        if not sent:
            ta = page.locator('textarea[name="user-prompt"]')
            await ta.click()
            await asyncio.sleep(0.3)
            await page.keyboard.press("Enter")
            print(self.tag("Sent via Enter โœ“"))

    async def _extract(self, page) -> str:
        await asyncio.sleep(3)

        try:
            copy = page.locator('button[data-copyairesponse="true"]')
            await copy.last.wait_for(state="visible", timeout=90000)
            print(self.tag("Response complete โœ“"))
        except Exception:
            for _ in range(75):
                await asyncio.sleep(2)
                if await page.locator(
                    'button[aria-label="Stop generating"]:not([disabled])'
                ).count() == 0:
                    print(self.tag("Response complete (fallback) โœ“"))
                    break

        await asyncio.sleep(1)

        text = await page.evaluate("""
            () => {
                const active = document.querySelector('[data-activeresponse="true"]');
                if (active) {
                    const sp = active.querySelector('.space-y-4');
                    if (sp && sp.innerText.trim().length > 0) return sp.innerText.trim();
                    const prose = active.querySelector('[class*="whitespace-normal"],[class*="prose"]');
                    if (prose && prose.innerText.trim().length > 0) return prose.innerText.trim();
                }
                const all = document.querySelectorAll('[data-activeresponse]');
                if (all.length > 0) {
                    const last = all[all.length - 1];
                    const sp = last.querySelector('.space-y-4');
                    if (sp) return sp.innerText.trim();
                    return last.innerText.trim();
                }
                const arts = document.querySelectorAll('article');
                if (arts.length > 0) return arts[arts.length - 1].innerText.trim();
                const divs = document.querySelectorAll('.space-y-4');
                for (let i = divs.length - 1; i >= 0; i--) {
                    if (divs[i].innerText.trim().length > 10) return divs[i].innerText.trim();
                }
                return '';
            }
        """)

        if not text or len(text.strip()) < 5:
            await asyncio.sleep(5)
            text = await page.evaluate("""
                () => {
                    const sp = document.querySelectorAll('.space-y-4');
                    if (sp.length > 0) return sp[sp.length-1].innerText.trim();
                    return document.body.innerText.slice(0, 5000);
                }
            """)

        print(self.tag(f"Extracted {len(text)} chars โœ“"))
        return text.strip()


# ====================================================================
# Browser Pool Manager
# ====================================================================

class BrowserPool:

    def __init__(self):
        self.loop            = asyncio.new_event_loop()
        self.workers: list[BrowserWorker] = []
        self.ready_event     = threading.Event()
        self._thread         = threading.Thread(target=self._run, daemon=True)
        self._queue: asyncio.Queue | None = None
        self._total_requests = 0
        self._rejected       = 0

    def start(self):
        self._thread.start()
        print(f"[POOL] Starting {POOL_SIZE} browsers "
              f"(rotation every {MAX_REQUESTS} req, queue timeout {QUEUE_TIMEOUT}s)...")

    def _run(self):
        asyncio.set_event_loop(self.loop)
        self.loop.run_until_complete(self._init_pool())
        self.ready_event.set()
        print(f"[POOL] All {POOL_SIZE} browsers ready! โœ“")
        self.loop.run_forever()

    async def _init_pool(self):
        from playwright.async_api import async_playwright
        self._queue = asyncio.Queue()
        pw = await async_playwright().start()
        workers = [BrowserWorker(i + 1, self.loop) for i in range(POOL_SIZE)]
        await asyncio.gather(*[w.init(pw) for w in workers])
        self.workers = workers
        for w in workers:
            await self._queue.put(w)

    async def _process(self, model_label: str, prompt: str) -> str:
        self._total_requests += 1
        try:
            worker: BrowserWorker = await asyncio.wait_for(
                self._queue.get(), timeout=QUEUE_TIMEOUT
            )
        except asyncio.TimeoutError:
            self._rejected += 1
            print(f"[POOL] โš ๏ธ Queue timeout! All {POOL_SIZE} workers busy. "
                  f"Rejected: {self._rejected}")
            raise RuntimeError(
                f"All {POOL_SIZE} workers are busy. "
                f"Please retry in a moment. (rejected total: {self._rejected})"
            )

        print(f"[POOL] Assigned W{worker.id} (total req: {self._total_requests}) โœ“")
        try:
            return await worker.chat(model_label, prompt)
        finally:
            await self._queue.put(worker)
            print(f"[POOL] W{worker.id} returned to pool โœ“")

    def process(self, model_label: str, prompt: str) -> str:
        if not self.ready_event.wait(timeout=180):
            raise RuntimeError("Pool not ready")
        future = asyncio.run_coroutine_threadsafe(
            self._process(model_label, prompt), self.loop
        )
        return future.result(timeout=QUEUE_TIMEOUT + 240)


pool = BrowserPool()
pool.start()


# ====================================================================
# Keep-Alive (ูŠู…ู†ุน HuggingFace ู…ู† ุฅูŠู‚ุงู Space ุจุณุจุจ ุนุฏู… ุงู„ู†ุดุงุท)
# ====================================================================

_keep_alive_count = 0

async def _keep_alive():
    global _keep_alive_count
    # ุงู†ุชุธุฑ ุญุชู‰ ูŠูƒูˆู† ุงู„ุณูŠุฑูุฑ ุฌุงู‡ุฒุงู‹
    await asyncio.sleep(30)
    print("[KEEP-ALIVE] Started โ€” ping every 4 minutes โœ“")
    while True:
        await asyncio.sleep(240)  # ูƒู„ 4 ุฏู‚ุงุฆู‚
        try:
            async with httpx.AsyncClient(timeout=10) as client:
                r = await client.get("http://localhost:7860/")
            _keep_alive_count += 1
            print(f"[KEEP-ALIVE] Ping #{_keep_alive_count} โ†’ {r.status_code} โœ“")
        except Exception as e:
            print(f"[KEEP-ALIVE] Ping failed (non-fatal): {e}")


# ====================================================================
# Helpers
# ====================================================================

def _extract_content(msg: dict) -> str:
    content = msg.get("content", "")
    if isinstance(content, list):
        return "\n".join(
            item.get("text", item.get("content", str(item)))
            if isinstance(item, dict) else str(item)
            for item in content
        )
    return str(content) if content else ""


def _build_prompt(messages: list) -> str:
    parts = []
    for msg in messages:
        role    = msg.get("role", "user")
        content = _extract_content(msg)
        if not content.strip():
            continue
        if role == "system":
            parts.append(
                f"=== SYSTEM INSTRUCTIONS ===\n{content}\n=== END INSTRUCTIONS ==="
            )
        elif role == "assistant":
            parts.append(f"[Assistant]: {content}")
        else:
            parts.append(content)
    return "\n\n".join(parts)


def _parse_tool_calls(text: str):
    cleaned = text.strip()
    if "```" in cleaned:
        m = re.search(r'```(?:json)?\s*\n?(.*?)\n?\s*```', cleaned, re.DOTALL)
        if m:
            cleaned = m.group(1).strip()
    candidates = [cleaned]
    m2 = re.search(r'\{[\s\S]*"tool_calls"[\s\S]*\}', cleaned)
    if m2:
        candidates.append(m2.group(0))
    for c in candidates:
        try:
            parsed = json.loads(c)
            if isinstance(parsed, dict) and "tool_calls" in parsed:
                raw = parsed["tool_calls"]
                if isinstance(raw, list) and raw:
                    return [{
                        "id": f"call_{uuid.uuid4().hex[:24]}",
                        "type": "function",
                        "function": {
                            "name": call.get("name", ""),
                            "arguments": (
                                json.dumps(call.get("arguments", {}), ensure_ascii=False)
                                if isinstance(call.get("arguments"), dict)
                                else str(call.get("arguments", "{}"))
                            ),
                        },
                    } for call in raw]
        except (json.JSONDecodeError, TypeError, KeyError):
            continue
    return None


def _auth(request: Request) -> bool:
    return (
        request.headers.get("authorization", "")
        .replace("Bearer ", "").strip() == API_SECRET_KEY
    )


def _auth_token(request: Request, token: str = "") -> bool:
    header_key = (
        request.headers.get("authorization", "")
        .replace("Bearer ", "").strip()
    )
    return header_key == API_SECRET_KEY or token == API_SECRET_KEY


def _get_model_label(model: str) -> str:
    return DUCK_MODELS.get(model, DUCK_MODELS[DEFAULT_MODEL])


def _make_completion(start_time, model, text, messages, tools=None):
    p  = sum(len(_extract_content(m).split()) for m in messages)
    c  = len(text.split())
    tc = _parse_tool_calls(text) if tools else None
    if tc:
        return {
            "id": f"chatcmpl-{uuid.uuid4().hex[:29]}", "object": "chat.completion",
            "created": int(start_time), "model": model,
            "choices": [{"index": 0, "message": {
                "role": "assistant", "content": None, "tool_calls": tc
            }, "finish_reason": "tool_calls"}],
            "usage": {"prompt_tokens": p, "completion_tokens": c, "total_tokens": p + c},
        }
    return {
        "id": f"chatcmpl-{uuid.uuid4().hex[:29]}", "object": "chat.completion",
        "created": int(start_time), "model": model,
        "choices": [{"index": 0, "message": {
            "role": "assistant", "content": text
        }, "finish_reason": "stop"}],
        "usage": {"prompt_tokens": p, "completion_tokens": c, "total_tokens": p + c},
    }


# ====================================================================
# FastAPI
# ====================================================================
app = FastAPI(title="Duck.ai API Server")


@app.on_event("startup")
async def startup_event():
    asyncio.create_task(_keep_alive())
    print("[APP] Keep-alive task registered โœ“")


@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
    try:
        data = await request.json()
    except Exception:
        return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON"}})
    if not _auth(request):
        return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})

    messages = data.get("messages", [])
    if not messages:
        return JSONResponse(status_code=400, content={"error": {"message": "messages required"}})

    model       = data.get("model", DEFAULT_MODEL)
    tools       = data.get("tools", None)
    start_time  = time.time()
    model_label = _get_model_label(model)
    prompt      = _build_prompt(messages)

    print(f"[API] /v1/chat/completions โ†’ {model} ({model_label})")
    try:
        text = await asyncio.get_event_loop().run_in_executor(
            None, pool.process, model_label, prompt
        )
        return _make_completion(start_time, model, text, messages, tools)
    except Exception as e:
        print(f"[API] ERROR: {e}")
        status = 503 if "busy" in str(e).lower() else 500
        return JSONResponse(status_code=status, content={"error": {"message": str(e)}})


@app.post("/v1/responses")
async def responses(request: Request):
    try:
        data = await request.json()
    except Exception:
        return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON"}})
    if not _auth(request):
        return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})

    input_data = data.get("input", "")
    if isinstance(input_data, str):
        messages = [{"role": "user", "content": input_data}]
    elif isinstance(input_data, list):
        messages = input_data
    else:
        messages = data.get("messages", [])
    if not messages:
        return JSONResponse(status_code=400, content={"error": {"message": "input required"}})

    model        = data.get("model", DEFAULT_MODEL)
    tools        = data.get("tools", None)
    instructions = data.get("instructions", "")
    if instructions:
        messages.insert(0, {"role": "system", "content": instructions})

    start_time  = time.time()
    model_label = _get_model_label(model)
    prompt      = _build_prompt(messages)

    print(f"[API] /v1/responses โ†’ {model} ({model_label})")
    try:
        text = await asyncio.get_event_loop().run_in_executor(
            None, pool.process, model_label, prompt
        )
        p  = sum(len(_extract_content(m).split()) for m in messages)
        c  = len(text.split())
        tc = _parse_tool_calls(text) if tools else None
        if tc:
            return {
                "id": f"resp-{uuid.uuid4().hex[:29]}", "object": "response",
                "created_at": int(start_time), "model": model, "status": "completed",
                "output": [{"type": "function_call", "id": t["id"], "call_id": t["id"],
                            "name": t["function"]["name"],
                            "arguments": t["function"]["arguments"],
                            "status": "completed"} for t in tc],
                "usage": {"input_tokens": p, "output_tokens": c, "total_tokens": p + c},
            }
        return {
            "id": f"resp-{uuid.uuid4().hex[:29]}", "object": "response",
            "created_at": int(start_time), "model": model, "status": "completed",
            "output": [{"type": "message", "role": "assistant",
                        "content": [{"type": "output_text", "text": text}]}],
            "usage": {"input_tokens": p, "output_tokens": c, "total_tokens": p + c},
        }
    except Exception as e:
        print(f"[API] ERROR: {e}")
        status = 503 if "busy" in str(e).lower() else 500
        return JSONResponse(status_code=status, content={"error": {"message": str(e)}})


@app.get("/v1/models")
async def list_models(request: Request):
    if not _auth(request):
        return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
    return {
        "object": "list",
        "data": [{"id": m, "object": "model", "owned_by": "duck.ai"} for m in ALL_MODELS],
    }


@app.get("/health")
async def health(request: Request, token: str = ""):
    if not _auth_token(request, token):
        return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})

    busy  = sum(1 for w in pool.workers if w.busy)
    stats = [
        {
            "id":                      w.id,
            "busy":                    w.busy,
            "total_requests":          w._total_count,
            "requests_until_rotation": MAX_REQUESTS - w._request_count,
        }
        for w in pool.workers
    ]
    mem = psutil.virtual_memory()
    return {
        "status":            "running",
        "message":           "Duck.ai API Pool Server is active!",
        "models":            ALL_MODELS,
        "pool_size":         POOL_SIZE,
        "workers_busy":      busy,
        "workers_free":      POOL_SIZE - busy,
        "total_requests":    pool._total_requests,
        "rejected_requests": pool._rejected,
        "queue_timeout_sec": QUEUE_TIMEOUT,
        "keep_alive_pings":  _keep_alive_count,
        "workers":           stats,
        "ram": {
            "used_gb":  round(mem.used  / 1024**3, 2),
            "total_gb": round(mem.total / 1024**3, 2),
            "percent":  mem.percent,
        },
        "cpu": psutil.cpu_percent(interval=None),
    }


@app.get("/")
async def root():
    return {
        "status":           "running",
        "message":          "Duck.ai API Pool Server is active!",
        "docs":             "/docs",
        "health":           "/health",
        "keep_alive_pings": _keep_alive_count,
    }


@app.get("/dashboard", response_class=HTMLResponse)
async def dashboard(request: Request, token: str = ""):
    if not _auth_token(request, token):
        return HTMLResponse(
            "<h1 style='font-family:sans-serif;text-align:center;margin-top:20%;"
            "color:#ef5350'>401 โ€” Unauthorized</h1>",
            status_code=401
        )
    with open("dashboard.html", "r", encoding="utf-8") as f:
        return HTMLResponse(f.read())


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)