File size: 21,690 Bytes
f343084
 
 
 
 
 
 
 
9b00487
f343084
 
 
 
 
 
 
9892596
 
 
 
 
 
 
 
f343084
 
bd2c875
9892596
f343084
 
9b00487
f343084
 
 
 
 
bd2c875
 
 
 
 
f343084
 
 
 
 
bd2c875
f343084
 
 
 
bd2c875
f343084
 
 
 
 
 
 
 
 
 
 
 
 
 
d04ae9a
bd2c875
 
9b00487
 
 
 
 
d04ae9a
 
bd2c875
d04ae9a
 
bd2c875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9892596
d04ae9a
 
 
bd2c875
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d04ae9a
bd2c875
d04ae9a
bd2c875
 
 
 
 
 
 
9892596
d04ae9a
 
 
bd2c875
 
 
 
 
9b00487
bd2c875
9892596
bd2c875
 
 
9892596
bd2c875
9892596
 
 
bd2c875
 
 
 
 
9892596
 
 
bd2c875
9892596
 
bd2c875
9892596
bd2c875
9892596
bd2c875
9892596
d04ae9a
 
 
 
9892596
 
 
bd2c875
9b00487
9892596
d04ae9a
9b00487
9892596
 
 
9b00487
bd2c875
9892596
 
bd2c875
 
9892596
 
 
 
bd2c875
 
 
 
 
9892596
 
bd2c875
9892596
bd2c875
9b00487
 
bd2c875
9892596
 
 
9b00487
d04ae9a
bd2c875
9892596
bd2c875
 
9892596
 
 
bd2c875
 
9892596
 
 
 
 
9b00487
 
bd2c875
9892596
 
 
 
 
 
9b00487
9892596
bd2c875
 
 
9892596
9b00487
 
 
 
bd2c875
9892596
 
9b00487
d04ae9a
9892596
 
 
d04ae9a
9892596
d04ae9a
 
 
9892596
9b00487
d04ae9a
 
 
9892596
f343084
 
 
bd2c875
9892596
bd2c875
 
d04ae9a
9892596
d04ae9a
 
 
f343084
 
 
 
 
 
9892596
f343084
 
d04ae9a
 
 
 
 
 
 
 
 
 
 
 
 
9892596
d04ae9a
 
 
 
 
 
 
9892596
d04ae9a
9892596
d04ae9a
 
 
 
 
9892596
 
f343084
 
 
 
 
 
 
 
9b00487
f343084
9b00487
f343084
9b00487
 
 
bd2c875
9b00487
 
 
9892596
 
 
 
 
 
9b00487
f343084
 
 
 
 
 
9892596
 
f343084
 
9892596
 
d04ae9a
 
9892596
 
 
 
9b00487
f343084
9892596
 
 
 
 
 
 
9b00487
f343084
 
9892596
 
 
 
 
 
 
9b00487
f343084
 
 
 
9b00487
f343084
9892596
f343084
 
 
 
 
 
 
9b00487
9892596
f343084
 
 
 
 
9b00487
f343084
9892596
 
 
 
 
 
bd2c875
f343084
 
9892596
 
 
 
f343084
9892596
f343084
 
 
 
 
 
 
 
9b00487
9892596
f343084
 
 
 
 
 
 
 
 
 
9892596
f343084
9b00487
f343084
9892596
f343084
 
 
 
 
9892596
 
 
 
bd2c875
9892596
f343084
9892596
 
 
f343084
9b00487
 
9892596
9b00487
 
f343084
bd2c875
 
 
 
 
9892596
bd2c875
 
 
 
9892596
bd2c875
9892596
9b00487
f343084
9892596
f343084
bd2c875
 
 
 
 
f343084
9b00487
 
f343084
9892596
f343084
9892596
f343084
 
 
 
 
 
 
 
 
9892596
 
 
 
f343084
 
 
 
 
9b00487
f343084
9892596
 
 
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
import os
import uuid
import time
import asyncio
import threading
import json
import re
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse

# ====================================================================
# Configuration
# ====================================================================
API_SECRET_KEY = os.getenv("API_SECRET_KEY", "change-me-secret")

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"

# ====================================================================
# Browser Engine
# ====================================================================

class AsyncBrowserThread(threading.Thread):
    def __init__(self):
        super().__init__(daemon=True)
        self.loop               = asyncio.new_event_loop()
        self.ready_event        = threading.Event()
        self.browser            = None
        self.playwright         = None
        self.persistent_context = None

    def run(self):
        asyncio.set_event_loop(self.loop)
        self.loop.run_until_complete(self._start_browser())
        self.ready_event.set()
        print("[SERVER] Browser + Duck.ai ready!")
        self.loop.run_forever()

    async def _start_browser(self):
        from playwright.async_api import async_playwright
        print("[SERVER] Launching Chrome...")
        self.playwright = await async_playwright().start()
        self.browser = await self.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",
            ],
        )

        # โ”€โ”€ Context ุฏุงุฆู…: cookies ุชูุญูุธ ุจูŠู† ุงู„ู€ requests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        self.persistent_context = await self.browser.new_context(
            user_agent=(
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
                "AppleWebKit/537.36 (KHTML, like Gecko) "
                "Chrome/124.0.0.0 Safari/537.36"
            ),
            viewport={"width": 1920, "height": 1080},
        )
        await self.persistent_context.add_init_script(
            "Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
        )

        # โ”€โ”€ ู‚ุจูˆู„ ุดุฑูˆุท duck.ai ู…ุฑุฉ ูˆุงุญุฏุฉ ุนู†ุฏ ุงู„ุฅู‚ู„ุงุน โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
        setup_page = await self.persistent_context.new_page()
        try:
            print("[SERVER] Opening duck.ai for first-time setup...")
            await setup_page.goto(
                "https://duckduckgo.com/aichat", wait_until="domcontentloaded"
            )
            await asyncio.sleep(5)

            # ุฒุฑ "Agree and Continue" ู…ู† ุงู„ุตูˆุฑุฉ ุงู„ุญู‚ูŠู‚ูŠุฉ
            agree_btn = setup_page.locator('button:has-text("Agree and Continue")')
            await agree_btn.wait_for(state="visible", timeout=12000)
            await agree_btn.click()
            print("[SERVER] Duck.ai terms accepted โœ“")
            await asyncio.sleep(3)

            # ุงู†ุชุธุฑ ุธู‡ูˆุฑ ุตู†ุฏูˆู‚ ุงู„ูƒุชุงุจุฉ = ุงู„ู…ูˆู‚ุน ุฌุงู‡ุฒ
            await setup_page.wait_for_selector(
                'textarea[name="user-prompt"]', timeout=20000
            )
            print("[SERVER] Duck.ai chat interface ready โœ“")

        except Exception as e:
            print(f"[SERVER] Setup note (non-fatal): {e}")
        finally:
            await setup_page.close()

    # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    # ุงู„ุฏุงู„ุฉ ุงู„ุฑุฆูŠุณูŠุฉ: ุฅุฑุณุงู„ prompt ูˆุงุณุชู‚ุจุงู„ ุงู„ุฑุฏ
    # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    async def _chat(self, model_label: str, prompt: str) -> str:
        # ูƒู„ request ูŠุฃุฎุฐ page ู…ู†ูุตู„ุฉ ู…ู† ู†ูุณ ุงู„ู€ context ุงู„ู…ุญููˆุธ
        page = await self.persistent_context.new_page()

        try:
            page.set_default_timeout(120000)

            # โ”€โ”€ 1. ูุชุญ ุตูุญุฉ ุฌุฏูŠุฏุฉ ู†ุธูŠูุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            await page.goto(
                "https://duckduckgo.com/aichat", wait_until="domcontentloaded"
            )
            await asyncio.sleep(3)

            # โ”€โ”€ 2. ุฅุบู„ุงู‚ ุฃูŠ popup/banner ุฅุถุงููŠ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            # Agree and Continue ุฅู† ุธู‡ุฑ ู…ุฌุฏุฏุงู‹ (ุงุญุชูŠุงุท)
            try:
                agree_btn = page.locator('button:has-text("Agree and Continue")')
                if await agree_btn.count() > 0:
                    is_visible = await agree_btn.first.is_visible()
                    if is_visible:
                        await agree_btn.first.click()
                        print("[DUCK] Terms re-accepted โœ“")
                        await asyncio.sleep(2)
            except Exception:
                pass

            # ุฅุบู„ุงู‚ ุฅุดุนุงุฑ PDF
            try:
                close_btn = page.locator(
                    'button.XkSxBJ8ofSQsZmGZs6qx, '
                    'li.HmVD0odzmaobZhTx3jzd button[aria-label="Close"], '
                    'button[aria-label="Close"]'
                )
                if await close_btn.count() > 0:
                    await close_btn.first.click()
                    await asyncio.sleep(1)
            except Exception:
                pass

            # โ”€โ”€ 3. ุงู†ุชุธุงุฑ ุตู†ุฏูˆู‚ ุงู„ูƒุชุงุจุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            await page.wait_for_selector(
                'textarea[name="user-prompt"]', timeout=30000
            )
            print("[DUCK] Input ready โœ“")

            # โ”€โ”€ 4. ุชุบูŠูŠุฑ ุงู„ู†ู…ูˆุฐุฌ ุฅุฐุง ู„ุฒู… โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            try:
                model_btn    = page.locator('button[data-testid="model-select-button"]')
                current_text = await model_btn.inner_text()
                print(f"[DUCK] Current model: {current_text.strip()}")

                if model_label.lower() not in current_text.lower():
                    await model_btn.click()
                    await asyncio.sleep(1.5)

                    option = page.locator(
                        f"li:has-text('{model_label}'), "
                        f"button:has-text('{model_label}'), "
                        f"[role='option']:has-text('{model_label}')"
                    )
                    if await option.count() > 0:
                        await option.first.click()
                        await asyncio.sleep(1)
                        print(f"[DUCK] Model โ†’ {model_label} โœ“")
                    else:
                        await page.keyboard.press("Escape")
                        print(f"[DUCK] Model '{model_label}' not found, using default")
            except Exception as e:
                print(f"[DUCK] Model select (non-fatal): {e}")

            # โ”€โ”€ 5. ูƒุชุงุจุฉ ูˆุฅุฑุณุงู„ ุงู„ุฑุณุงู„ุฉ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            textarea = page.locator('textarea[name="user-prompt"]')
            await textarea.click()
            await textarea.fill(prompt)
            await asyncio.sleep(0.5)

            send_btn = page.locator('button[type="submit"][aria-label="Send"]')
            await send_btn.wait_for(state="enabled", timeout=10000)
            await send_btn.click()
            print(f"[DUCK] Sent ({len(prompt)} chars) โœ“")

            # โ”€โ”€ 6. ุงู†ุชุธุงุฑ ุจุฏุก ุงู„ุฑุฏ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            await asyncio.sleep(3)
            try:
                stop_btn = page.locator('button[aria-label="Stop generating"]')
                await stop_btn.wait_for(state="visible", timeout=20000)
                print("[DUCK] Response started โœ“")
            except Exception:
                print("[DUCK] Stop button not detected, continuing...")

            # โ”€โ”€ 7. ุงู†ุชุธุงุฑ ุงูƒุชู…ุงู„ ุงู„ุฑุฏ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            # ุงู„ุฑุฏ ุงูƒุชู…ู„ = ุฒุฑ "Stop generating" ุงุฎุชูู‰ ุฃูˆ ุฃุตุจุญ disabled
            max_wait = 120
            elapsed  = 0
            while elapsed < max_wait:
                await asyncio.sleep(2)
                elapsed += 2
                stop_active = await page.locator(
                    'button[aria-label="Stop generating"]:not([disabled])'
                ).count()
                if stop_active == 0:
                    print(f"[DUCK] Response complete (~{elapsed}s) โœ“")
                    break

            await asyncio.sleep(1.5)

            # โ”€โ”€ 8. ุงุณุชุฎุฑุงุฌ ุงู„ู†ุต โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            response_text = await page.evaluate("""
                () => {
                    // ุทุฑูŠู‚ุฉ 1: article elements
                    const articles = document.querySelectorAll('article');
                    if (articles.length > 0) {
                        return articles[articles.length - 1].innerText.trim();
                    }

                    // ุทุฑูŠู‚ุฉ 2: divs ุชุญุชูˆูŠ ุนู„ู‰ "message" ููŠ class
                    const msgDivs = document.querySelectorAll(
                        '[class*="message"]:not([class*="user"])' +
                        ':not([class*="User"]):not([class*="input"])'
                    );
                    if (msgDivs.length > 0) {
                        const textDivs = [...msgDivs].filter(el =>
                            el.innerText &&
                            el.innerText.trim().length > 20 &&
                            !el.querySelector('textarea')
                        );
                        if (textDivs.length > 0) {
                            return textDivs[textDivs.length - 1].innerText.trim();
                        }
                    }

                    // ุทุฑูŠู‚ุฉ 3: ุฃุทูˆู„ div ุจุฏูˆู† textarea
                    const allDivs = [...document.querySelectorAll('div')].filter(el =>
                        el.children.length < 10 &&
                        el.innerText &&
                        el.innerText.trim().length > 50 &&
                        !el.querySelector('textarea') &&
                        !el.querySelector('button[type="submit"]')
                    );
                    if (allDivs.length > 0) {
                        return allDivs.sort(
                            (a, b) => b.innerText.length - a.innerText.length
                        )[0].innerText.trim();
                    }
                    return '';
                }
            """)

            # โ”€โ”€ 9. fallback ุฅุฐุง ูƒุงู† ุงู„ู†ุต ูุงุฑุบุงู‹ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
            if not response_text or len(response_text.strip()) < 10:
                await asyncio.sleep(5)
                response_text = await page.evaluate("""
                    () => {
                        const articles = document.querySelectorAll('article');
                        if (articles.length > 0) {
                            return articles[articles.length - 1].innerText.trim();
                        }
                        return document.body.innerText.slice(0, 5000);
                    }
                """)

            print(f"[DUCK] Extracted {len(response_text)} chars โœ“")
            return response_text.strip()

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

    # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    def process(self, model_label: str, prompt: str) -> str:
        if not self.ready_event.wait(timeout=120):
            raise RuntimeError("Browser not ready after 120s")
        future = asyncio.run_coroutine_threadsafe(
            self._chat(model_label, prompt), self.loop
        )
        return future.result(timeout=180)


browser_engine = AsyncBrowserThread()
browser_engine.start()


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

def _extract_content(msg: dict) -> str:
    content = msg.get("content", "")
    if isinstance(content, list):
        parts = []
        for item in content:
            if isinstance(item, dict):
                parts.append(item.get("text", item.get("content", str(item))))
            else:
                parts.append(str(item))
        return "\n".join(parts)
    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:
    token = request.headers.get("authorization", "").replace("Bearer ", "").strip()
    return 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.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, browser_engine.process, model_label, prompt
        )
        return _make_completion(start_time, model, text, messages, tools)
    except Exception as e:
        print(f"[API] ERROR: {e}")
        return JSONResponse(status_code=500, 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, browser_engine.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}")
        return JSONResponse(status_code=500, 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")
@app.get("/")
async def health():
    return {
        "status":  "running",
        "message": "Duck.ai API Server is active!",
        "models":  ALL_MODELS,
    }


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