File size: 25,674 Bytes
22de727
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Pluggable agent interface and browser-use implementation.

AgentRunner is the protocol every agent must satisfy. The evaluation harness
only calls this interface -- it never touches browser-use (or any other library)
directly.

Built-in:
  - BrowserUseAgent: wraps the browser-use library with any LLM backend.
  - MockAgent: no LLM, no browser -- exercises the pipeline for free.
"""

from __future__ import annotations

import asyncio
import os
import json
import pathlib
import re
import shutil
import time
import urllib.error
import urllib.request
from dataclasses import dataclass, field
from html.parser import HTMLParser
from pathlib import Path
from typing import Protocol, runtime_checkable


@dataclass
class AgentResult:
    """Result returned by an agent after running a single task."""

    elapsed: float
    steps: int
    is_done: bool
    final_result: str | None
    errors: list[str] = field(default_factory=list)


@runtime_checkable
class AgentRunner(Protocol):
    """Interface that every agent implementation must satisfy."""

    async def setup(self, server_url: str) -> None:
        """One-time setup before running tasks (e.g. start browser)."""
        ...

    async def run(self, task: str, server_url: str, task_dir: Path) -> AgentResult:
        """Execute a single task. Save trajectory artifacts to *task_dir*."""
        ...

    async def teardown(self) -> None:
        """Release resources (e.g. close browser)."""
        ...


# -- Chrome flags to reduce per-instance resource usage under parallel load --
_EXTRA_CHROME_ARGS = [
    "--disable-gpu",
    "--disable-software-rasterizer",
    "--disable-extensions",
    "--no-sandbox",
    # OFFLINE: fail DNS for every host except localhost, so the agent physically
    # cannot reach the outside web (127.0.0.1 is an IP, not DNS-resolved, so it's
    # unaffected). Keeps the eval self-contained and reproducible.
    "--host-resolver-rules=MAP * ~NOTFOUND , EXCLUDE localhost",
]

_RETRYABLE_ERRORS = ("Timeout", "ReadTimeout", "ConnectionError", "WebSocket", "CDP")


def _is_retryable(exc: Exception) -> bool:
    text = f"{type(exc).__name__}: {exc}"
    return any(k in text for k in _RETRYABLE_ERRORS)


def _route_uploads_through_miniweb_picker():
    """Let browser-use click <input type=file>, so uploads go through MiniWeb's picker.

    browser-use refuses a click on a file input twice (the click handler and the CDP click both
    answer "use the upload function"). On MiniWeb that click is exactly how a person picks a file:
    src/miniweb/app/static/file-explorer.js catches it (capture phase, default prevented, so no native chooser
    opens) and shows the in-page Finder over the simulated file system. For file inputs only, the
    click handler runs with the node's type masked; everything else is unchanged. Must run before a
    BrowserSession starts: its watchdogs bind their handlers then. Idempotent."""
    import functools

    from browser_use.browser.watchdogs.default_action_watchdog import DefaultActionWatchdog as W
    if getattr(W, "_miniweb_file_clicks", False):
        return
    original = W.on_ClickElementEvent

    @functools.wraps(original)
    async def on_ClickElementEvent(self, event):
        node = event.node
        attrs = node.attributes if isinstance(getattr(node, "attributes", None), dict) else None
        is_file = ((getattr(node, "node_name", "") or "").upper() == "INPUT" and attrs is not None
                   and str(attrs.get("type", "")).lower() == "file")
        if not is_file:
            return await original(self, event)
        attrs["type"] = "miniweb-file"          # past both guards; the page still has a file input
        try:
            return await original(self, event)
        finally:
            attrs["type"] = "file"

    W.on_ClickElementEvent = on_ClickElementEvent
    W._miniweb_file_clicks = True


class BrowserUseAgent:
    """AgentRunner backed by the *browser-use* library."""

    def __init__(
        self,
        llm,
        *,
        use_vision: bool = False,
        max_steps: int = 50,
        timeout: int = 300,
        headless: bool = True,
        available_file_paths: list[str] | None = None,
        executable_path: str | None = None,
    ):
        self.llm = llm
        self.use_vision = use_vision
        self.max_steps = max_steps
        self.timeout = timeout
        self.headless = headless
        # Browser binary. Unset, browser-use picks the first Chromium it finds; on this
        # machine that is Playwright's Chrome for Testing 149 (build 1228), which never
        # completes a page load in headless mode (2026-09-26), while Chromium 125 does.
        # MINIWEB_BROWSER pins one for every run.
        self.executable_path = executable_path or os.environ.get("MINIWEB_BROWSER") or None
        # Uploads go through MiniWeb's own file picker (src/miniweb/app/static/file-explorer.js, the
        # in-page Finder over the simulated file system), as a person would pick a file:
        # browser-use's upload_file shortcut is removed and file inputs are clickable (see
        # _route_uploads_through_miniweb_picker). `available_file_paths` is accepted for
        # compatibility but no longer handed to the agent (2026-09-28).
        self.available_file_paths = available_file_paths or []
        self._session = None
        self._server_url: str | None = None

    async def _start_session(self, max_retries: int = 3) -> None:
        from browser_use import BrowserSession

        _route_uploads_through_miniweb_picker()         # before the session binds its handlers

        for attempt in range(1, max_retries + 1):
            self._session = BrowserSession(
                headless=self.headless,
                keep_alive=True,
                args=_EXTRA_CHROME_ARGS,
                **({"executable_path": self.executable_path} if self.executable_path else {}),
                # agent-level guard: only localhost is navigable (belt to the
                # host-resolver-rules braces β€” gives a clean refusal, not a DNS error)
                allowed_domains=["localhost", "127.0.0.1"],
            )
            try:
                await self._session.start()
                await self._enable_clipboard()
                return
            except Exception as e:
                if attempt < max_retries and _is_retryable(e):
                    print(
                        f"    [BrowserUseAgent] start attempt "
                        f"{attempt}/{max_retries} failed ({type(e).__name__}), retrying..."
                    )
                    try:
                        await self._session.kill()
                    except Exception:
                        pass
                    self._session = None
                    await asyncio.sleep(3 * attempt)
                else:
                    raise

    async def _enable_clipboard(self, agent=None) -> None:
        """Let the site's Copy buttons work headless. A headless page never has
        focus, and navigator.clipboard.writeText() rejects on an unfocused document
        ("Document is not focused"), so every copy failed and the recorder never
        saw a clipboard_write β€” 8 tasks could not be passed. Grant clipboard access
        browser-wide and emulate focus on the current tab; re-applied every step
        (on_step_start) so a tab the agent opens later is covered too."""
        if not self._session:
            return
        try:
            cdp = await self._session.get_or_create_cdp_session()
            if agent is None:
                await cdp.cdp_client.send.Browser.grantPermissions(
                    params={"permissions": ["clipboardReadWrite", "clipboardSanitizedWrite"]})
            await cdp.cdp_client.send.Emulation.setFocusEmulationEnabled(
                params={"enabled": True}, session_id=cdp.session_id)
        except Exception as e:
            print(f"    [BrowserUseAgent] clipboard/focus setup failed: {type(e).__name__}: {e}")

    async def setup(self, server_url: str) -> None:
        self._server_url = server_url
        await self._start_session()
        page = await self._session.get_current_page()
        # Explicitly reset this task's session (clears any overlay/login residue)
        # before starting β€” session-scoped, so parallel evals don't interfere.
        try:
            from urllib.parse import urlparse
            o = urlparse(server_url)
            await page.goto(f"{o.scheme}://{o.netloc}/_reset_data")
        except Exception:
            pass
        await page.goto(server_url)
        await asyncio.sleep(2)

    async def restart_session(self) -> None:
        old_dir = None
        if self._session:
            try:
                old_dir = getattr(self._session.browser_profile, "user_data_dir", None)
            except Exception:
                pass
            try:
                await self._session.kill()
            except Exception:
                pass
            self._session = None
        if old_dir:
            shutil.rmtree(str(old_dir), ignore_errors=True)

        await self._start_session()
        page = await self._session.get_current_page()
        await page.goto(self._server_url)
        await asyncio.sleep(2)

    async def run(self, task: str, server_url: str, task_dir: Path) -> AgentResult:
        from browser_use import Agent, Tools

        instruction = (
            f"You are interacting with a web application at {server_url}. "
            f"Your task: {task}"
        )

        agent = Agent(
            task=instruction,
            llm=self.llm,
            browser_session=self._session,
            use_vision=self.use_vision,
            use_judge=False,  # our verify_task is authoritative; skip browser-use's self-judge LLM call
            save_conversation_path=str(task_dir / "conversations"),
            max_steps=self.max_steps,
            # no upload_file shortcut: a file is chosen in MiniWeb's picker, which opens when the
            # file control is clicked
            tools=Tools(exclude_actions=["upload_file"]),
        )

        timed_out = False
        t0 = time.time()
        try:
            history = await asyncio.wait_for(agent.run(on_step_start=self._enable_clipboard), timeout=self.timeout)
        except asyncio.TimeoutError:
            timed_out = True
            history = agent.history
        elapsed = time.time() - t0

        # Save trajectory
        history.save_to_file(task_dir / "history.json")
        screenshots_dst = task_dir / "screenshots"
        for step_idx, path_str in enumerate(history.screenshot_paths()):
            if path_str and Path(path_str).exists():
                screenshots_dst.mkdir(parents=True, exist_ok=True)
                shutil.copy2(path_str, screenshots_dst / f"step_{step_idx}.png")

        if timed_out:
            raise asyncio.TimeoutError()

        return AgentResult(
            elapsed=round(elapsed, 1),
            steps=len(history.history),
            is_done=history.is_done(),
            final_result=history.final_result(),
            errors=history.errors(),
        )

    async def teardown(self) -> None:
        user_data_dir = None
        if self._session:
            try:
                user_data_dir = getattr(self._session.browser_profile, "user_data_dir", None)
            except Exception:
                pass
            try:
                await self._session.kill()
            except Exception:
                pass
            self._session = None
        if user_data_dir:
            shutil.rmtree(str(user_data_dir), ignore_errors=True)


# ── Mock agent (no LLM, no browser) ─────────────────────────────────────────

class _TextExtractor(HTMLParser):
    """Minimal HTML→text extractor."""

    def __init__(self):
        super().__init__()
        self._pieces: list[str] = []
        self._skip = False

    def handle_starttag(self, tag, attrs):
        self._skip = tag in ("script", "style", "noscript")

    def handle_endtag(self, tag):
        if tag in ("script", "style", "noscript"):
            self._skip = False

    def handle_data(self, data):
        if not self._skip:
            self._pieces.append(data)

    def get_text(self) -> str:
        return " ".join(self._pieces)


def _html_to_text(html: str) -> str:
    parser = _TextExtractor()
    parser.feed(html)
    return parser.get_text()


def _fetch(url: str, timeout: int = 10) -> str:
    try:
        with urllib.request.urlopen(url, timeout=timeout) as r:
            return r.read().decode("utf-8", errors="replace")
    except Exception:
        return ""


class MockAgent:
    """Pipeline-validation agent. No LLM, no browser.

    For each task it:
      1. GETs the site index page
      2. Follows obvious links (categories, search, detail pages) based on
         keywords in the instruction
      3. Extracts visible text and returns a best-guess answer

    This won't produce high pass rates but exercises the full eval pipeline
    (server management, task loading, verification, result aggregation)
    without any API calls or browser processes.
    """

    def __init__(self, *, use_vision=False, max_steps=20,
                 timeout=180, headless=True, available_file_paths=None):
        self._server_url: str | None = None
        self.timeout = timeout
        self.max_steps = max_steps

    async def setup(self, server_url: str) -> None:
        self._server_url = server_url
        # Verify the server is reachable
        _fetch(server_url)

    async def run(self, task: str, server_url: str, task_dir: Path) -> AgentResult:
        t0 = time.time()
        trajectory = []
        answer = None

        try:
            # Step 1: fetch the site index
            index_html = _fetch(server_url)
            index_text = _html_to_text(index_html)
            trajectory.append({"action": "navigate", "url": server_url})

            # Step 2: try to find relevant links from the instruction
            instruction_lower = task.lower()

            # Extract all <a href="..."> links from the page
            links = re.findall(r'href="([^"]*)"', index_html)
            # Normalise relative links
            links = [
                l if l.startswith("http") else server_url.rstrip("/") + "/" + l.lstrip("/")
                for l in links if not l.startswith("#") and not l.startswith("javascript")
            ]

            # Try keyword matching to pick a relevant link
            visited = {server_url}
            pages_text = [index_text]

            keywords = re.findall(r'\b[a-z]{4,}\b', instruction_lower)
            # Remove common stop words
            stop = {"that", "this", "with", "from", "have", "what", "your",
                    "navigate", "page", "using", "many", "listed", "find",
                    "click", "search", "filter", "sort", "select", "open",
                    "look", "check", "view", "show", "display", "which",
                    "does", "total", "number", "count", "name", "after",
                    "before", "into", "then", "also", "each", "when", "more",
                    "most", "about", "been", "would", "could", "should"}
            keywords = [k for k in keywords if k not in stop][:5]

            steps = 1
            for link in links[:20]:
                if steps >= self.max_steps:
                    break
                link_lower = link.lower()
                if any(kw in link_lower for kw in keywords) and link not in visited:
                    visited.add(link)
                    page_html = _fetch(link)
                    if page_html:
                        page_text = _html_to_text(page_html)
                        pages_text.append(page_text)
                        trajectory.append({"action": "navigate", "url": link})
                        steps += 1

                        # Extract sub-links and follow one level deeper
                        sub_links = re.findall(r'href="([^"]*)"', page_html)
                        for sl in sub_links[:5]:
                            if steps >= self.max_steps:
                                break
                            full = sl if sl.startswith("http") else server_url.rstrip("/") + "/" + sl.lstrip("/")
                            if full not in visited and any(kw in full.lower() for kw in keywords):
                                visited.add(full)
                                sub_html = _fetch(full)
                                if sub_html:
                                    pages_text.append(_html_to_text(sub_html))
                                    trajectory.append({"action": "navigate", "url": full})
                                    steps += 1

            # Step 3: try to extract a plausible answer
            all_text = "\n".join(pages_text)

            # Look for numbers if the task asks "how many"
            if "how many" in instruction_lower or "count" in instruction_lower:
                # Try to find a count from the page
                numbers = re.findall(r'\b(\d+)\b', all_text)
                if numbers:
                    answer = numbers[0]

            if answer is None:
                # Return first 200 chars of extracted text as a generic answer
                answer = all_text[:200].strip() or "done"

            # Save trajectory
            with open(task_dir / "trajectory.json", "w") as f:
                json.dump(trajectory, f, indent=2)

        except Exception as e:
            return AgentResult(
                elapsed=round(time.time() - t0, 1),
                steps=0,
                is_done=False,
                final_result=None,
                errors=[str(e)],
            )

        elapsed = round(time.time() - t0, 1)
        return AgentResult(
            elapsed=elapsed,
            steps=len(trajectory),
            is_done=True,
            final_result=answer,
            errors=[],
        )

    async def teardown(self) -> None:
        pass


# ── Shared-API LLM (drop-in for browser-use) ────────────────────────────────

class ChatLLM:
    """browser-use BaseChatModel backed by the shared model-routing helper.

    Implements the ainvoke() protocol so browser-use Agent can use it as its
    LLM. Each call goes through app.llm.call_llm, which routes the model
    name to its provider (Groq / OpenAI / Gemini). model="auto" uses the
    default model (LLM_MODEL env or app.llm.DEFAULT_MODEL).
    """

    _verified_api_keys: bool = True  # keys are resolved by app.llm

    def __init__(self, model: str = "auto", timeout: int = 120):
        self.model = model
        self._timeout = timeout

    @property
    def provider(self) -> str:
        return "miniweb-llm"

    @property
    def name(self) -> str:
        return self.model

    @property
    def model_name(self) -> str:
        return self.model

    @staticmethod
    def _serialize_messages(messages) -> str:
        """Convert browser-use message objects into a single text prompt."""
        parts = []
        for msg in messages:
            role = getattr(msg, "role", "user")
            content = getattr(msg, "content", "")
            if isinstance(content, list):
                # Multi-part content (text + images) β€” extract text parts
                text_parts = []
                for part in content:
                    if hasattr(part, "text"):
                        text_parts.append(part.text)
                    elif isinstance(part, dict) and "text" in part:
                        text_parts.append(part["text"])
                content = "\n".join(text_parts)
            if content:
                parts.append(f"[{role.upper()}]\n{content}")
        return "\n\n".join(parts)

    @staticmethod
    def _extract_json(text: str) -> str:
        """Strip markdown code fences from the LLM response."""
        text = text.strip()
        if text.startswith("```"):
            text = text.split("```")[1]
            if text.startswith("json"):
                text = text[4:]
        # Also handle trailing fence
        if "```" in text:
            text = text.split("```")[0]
        return text.strip()

    async def ainvoke(self, messages, output_format=None, **kwargs):
        from browser_use.llm.views import ChatInvokeCompletion, ChatInvokeUsage
        from browser_use.llm.exceptions import ModelProviderError

        prompt_text = self._serialize_messages(messages)

        if output_format is not None:
            schema = output_format.model_json_schema()
            prompt_text += (
                "\n\n---\nYou MUST respond with ONLY valid JSON (no markdown, no explanation) "
                "matching this schema:\n"
                + json.dumps(schema, indent=2)
            )

        try:
            import sys as _sys
            _root = str(pathlib.Path(__file__).resolve().parents[2])   # src/
            if _root not in _sys.path:
                _sys.path.insert(0, _root)
            from miniweb.app.llm import call_llm
            text = await asyncio.wait_for(
                asyncio.to_thread(
                    call_llm, prompt_text,
                    None,                       # system (already serialized)
                    4000,                       # max_tokens
                    0.3,                        # temperature
                    output_format is not None,  # json_mode
                    None if self.model == "auto" else self.model,
                ),
                timeout=self._timeout,
            )
            if not text:
                raise ModelProviderError(
                    message=f"LLM call failed (model={self.model})",
                    model=self.model,
                )
        except asyncio.TimeoutError:
            raise ModelProviderError(
                message=f"LLM call timed out after {self._timeout}s",
                model=self.model,
            )

        usage = ChatInvokeUsage(
            prompt_tokens=0, completion_tokens=0, total_tokens=0,
            prompt_cached_tokens=None, prompt_image_tokens=None,
            prompt_cache_creation_tokens=None,
        )

        if output_format is None:
            return ChatInvokeCompletion(
                completion=text, usage=usage, stop_reason="end_turn",
            )

        # Structured output: parse JSON into Pydantic model
        json_text = self._extract_json(text)
        try:
            parsed = output_format.model_validate_json(json_text)
        except Exception:
            # Retry: sometimes there's extra text around the JSON
            # Try to find the outermost { }
            start = json_text.find("{")
            end = json_text.rfind("}")
            if start >= 0 and end > start:
                json_text = json_text[start:end + 1]
                parsed = output_format.model_validate_json(json_text)
            else:
                raise ModelProviderError(
                    message=f"Failed to parse JSON from LLM response: {text[:200]}",
                    model=self.model,
                )

        return ChatInvokeCompletion(
            completion=parsed, usage=usage, stop_reason="end_turn",
        )


# ── unified agent factory (replaces the per-runner AGENT_FACTORIES) ───────────

def build_browser_llm(model: str, *, native: bool = False):
    """LLM object for the browser agent.

    Default: ChatLLM β€” routes every call through helpers.llm.LLMClient, so it
    works with any configured provider (incl. Vertex Gemini and Ollama) and its
    tokens land in LLMClient.GLOBAL. It serializes messages to text, so it drives
    the agent from the text DOM.

    native=True builds browser-use's provider-native class instead (needs that
    provider's own API key in env) β€” use it when you want true vision/multimodal.
    """
    if native:
        from helpers.llm import resolve_provider
        paths = {
            "openai": "browser_use.llm.openai.chat:ChatOpenAI",
            "gemini": "browser_use.llm.google.chat:ChatGoogle",
            "anthropic": "browser_use.llm.anthropic.chat:ChatAnthropic",
            "ollama": "browser_use.llm.ollama.chat:ChatOllama",
            "groq": "browser_use.llm.groq.chat:ChatGroq",
        }
        cp = paths.get(resolve_provider(model))
        if cp:
            mod, cls = cp.split(":")
            LLM = getattr(__import__(mod, fromlist=[cls]), cls)
            name = model.split("/", 1)[-1] if resolve_provider(model) == "ollama" else model
            return LLM(model=name)
    return ChatLLM(model)


# Friendly shorthands the older runners accepted, mapped to real model ids.
MODEL_ALIASES = {
    "gemini-flash": "gemini-3.5-flash", "gemini-pro": "gemini-2.5-pro",
    "gpt": "gpt-4o", "gpt-5.4": "gpt-5", "gpt-5.5": "gpt-5",
    "llm": "auto", "groq": "llama-3.3-70b-versatile", "groq-70b": "llama-3.3-70b-versatile",
}


def build_agent(model: str, *, native_llm: bool = False, harness: str = "browser-use", **opts):
    """Construct a browser-use agent runner for a model name (or an alias, or "mock").

    opts: use_vision, max_steps, timeout, headless, available_file_paths.
    """
    if model == "mock":
        return MockAgent(**opts)
    model = MODEL_ALIASES.get(model, model)
    if harness not in ("browser-use", "auto"):
        raise ValueError(f"harness {harness!r} is gone β€” only browser-use is supported")
    return BrowserUseAgent(build_browser_llm(model, native=native_llm), **opts)