File size: 29,205 Bytes
5627f55
 
 
 
 
 
fd7f17a
df568bc
5627f55
 
 
 
 
 
df568bc
 
5627f55
11a4e6e
 
7738e45
5627f55
 
7738e45
5627f55
7738e45
5627f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df568bc
 
 
 
fd7f17a
df568bc
 
 
 
 
 
 
 
 
 
fd7f17a
 
 
 
 
 
 
df568bc
fd7f17a
df568bc
fd7f17a
 
 
 
 
 
 
df568bc
fd7f17a
 
 
 
 
 
 
 
 
 
5627f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd7f17a
5627f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd7f17a
5627f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df568bc
5627f55
 
df568bc
5627f55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
"""
Custom Gradio UI for the AWM environment.
"""

from __future__ import annotations

import asyncio
import inspect
import json
import os
from typing import Any

import gradio as gr

from openenv.core.env_server.serialization import serialize_observation

from .data_loader import AWMDataLoader
from .prompts import DEFAULT_SYSTEM_PROMPT
from .web_agent import AwmAgent
from .config import DEFAULT_REWARD_CONFIG


# Keep in sync with DEFAULT_REWARD_CONFIG in config.py.
_DEFAULT_REWARD_JSON = json.dumps(
    DEFAULT_REWARD_CONFIG, indent=2
)


def _format_obs_md(payload: dict | None) -> str:
    if not payload:
        return "*No observation yet.*"
    obs = payload.get("observation") if isinstance(payload, dict) else None
    if obs is None:
        obs = payload
    reward = payload.get("reward") if isinstance(payload, dict) else None
    done = payload.get("done") if isinstance(payload, dict) else None

    lines: list[str] = []
    if reward is not None:
        lines.append(f"**reward**: `{reward}`")
    if done is not None:
        lines.append(f"**done**: `{done}`")
    if isinstance(obs, dict):
        for key in (
            "reward_type",
            "scenario",
            "task",
            "task_idx",
            "num_tools",
            "tool_name",
            "error",
            "warning",
        ):
            v = obs.get(key)
            if v is not None and v != "":
                lines.append(f"**{key}**: `{v}`")
        if obs.get("tool_result") is not None:
            tr = obs["tool_result"]
            tr_text = (
                tr if isinstance(tr, str) else json.dumps(tr, indent=2, default=str)
            )
            if len(tr_text) > 2000:
                tr_text = tr_text[:2000] + "\n... (truncated)"
            lines.append("\n**tool_result:**")
            lines.append(f"```\n{tr_text}\n```")
        if obs.get("verify_result"):
            vr = obs["verify_result"]
            vr_text = json.dumps(vr, indent=2, default=str)
            if len(vr_text) > 2000:
                vr_text = vr_text[:2000] + "\n... (truncated)"
            lines.append("\n**verify_result:**")
            lines.append(f"```json\n{vr_text}\n```")
        if obs.get("trajectory_path"):
            lines.append(f"\n**trajectory_path**: `{obs['trajectory_path']}`")
    return "\n\n".join(lines) if lines else "*Empty observation.*"


def _make_args_template(input_schema: dict | None) -> str:
    if not input_schema or not isinstance(input_schema, dict):
        return "{}"
    props = input_schema.get("properties") or {}
    template: dict[str, Any] = {}
    for name, info in props.items():
        ty = (info or {}).get("type", "string")
        template[name] = {
            "string": "",
            "integer": 0,
            "number": 0.0,
            "boolean": False,
            "array": [],
            "object": {},
        }.get(ty, None)
    return json.dumps(template, indent=2)


def build_awm_gradio_app(
    web_manager: Any,
    action_fields: list[dict] | None = None,
    metadata: Any = None,
    is_chat_env: bool = False,
    title: str = "AWM Environment",
    quick_start_md: str | None = None,
) -> gr.Blocks:
    data_loader = AWMDataLoader(cache_dir=os.environ.get("AWM_DATA_DIR"))

    readme_md = ""
    if metadata is not None and getattr(metadata, "readme_content", None):
        readme_md = metadata.readme_content

    # openenv-core 0.2.3 added a ``reset_kwargs`` parameter to
    # ``WebInterfaceManager.reset_environment``. PyPI's 0.2.1 takes no args,
    # which silently drops scenario/task_idx β€” fall back to calling env.reset
    # directly and replicate the episode-state updates the manager would do.
    _reset_env_supports_kwargs = (
        len(
            [
                p
                for p in inspect.signature(
                    web_manager.reset_environment
                ).parameters.values()
                if p.name != "self"
            ]
        )
        > 0
    )

    async def _safe_reset(reset_kwargs: dict[str, Any]) -> dict[str, Any]:
        if _reset_env_supports_kwargs:
            return await web_manager.reset_environment(reset_kwargs)

        env = web_manager.env
        params = inspect.signature(env.reset).parameters
        has_var_kw = any(
            p.kind == inspect.Parameter.VAR_KEYWORD for p in params.values()
        )
        valid = {k: v for k, v in reset_kwargs.items() if has_var_kw or k in params}
        loop = asyncio.get_event_loop()
        observation = await loop.run_in_executor(None, lambda: env.reset(**valid))
        serialized = serialize_observation(observation)

        es = web_manager.episode_state
        es.episode_id = env.state.episode_id
        es.step_count = 0
        es.current_observation = serialized["observation"]
        es.action_logs = []
        es.is_reset = True
        try:
            await web_manager._send_state_update()
        except Exception:
            pass
        return serialized

    async def _do_reset(
        scenario: str,
        task_idx: int,
        llm_base_url: str,
        llm_api_key: str,
        llm_model: str,
        reward_config_json: str,
    ):
        if not scenario:
            return (
                "❌ Pick a scenario first.",
                "",
                "*Reset failed.*",
                "{}",
                gr.update(choices=[], value=None),
                "{}",
            )

        reset_kwargs: dict[str, Any] = {
            "scenario": scenario,
            "task_idx": int(task_idx),
        }
        if llm_base_url:
            reset_kwargs["llm_base_url"] = llm_base_url
        if llm_api_key:
            reset_kwargs["llm_api_key"] = llm_api_key
        if llm_model:
            reset_kwargs["llm_model"] = llm_model
        if reward_config_json and reward_config_json.strip():
            try:
                reset_kwargs["reward_config"] = json.loads(reward_config_json)
            except json.JSONDecodeError as e:
                return (
                    f"❌ Invalid reward_config JSON: {e}",
                    "",
                    "*Reset failed.*",
                    "{}",
                    gr.update(choices=[], value=None),
                    "{}",
                )

        try:
            result = await _safe_reset(reset_kwargs)
        except Exception as e:
            return (
                f"❌ Reset error: {e}",
                "",
                f"*Reset failed: {e}*",
                "{}",
                gr.update(choices=[], value=None),
                "{}",
            )

        obs = result.get("observation", {}) or {}
        rt = obs.get("reward_type")
        ok = rt in ("reset_ok", "reset_warning")
        status = "βœ… Reset OK." if ok else f"❌ Reset failed: {obs.get('error') or rt}"
        task_md = f"**Task** (`{scenario}`, idx={task_idx}):\n\n{obs.get('task') or '*(no task description)*'}"

        tool_names: list[str] = []
        tool_lookup: dict[str, dict] = {}
        if ok:
            try:
                tools_result = await web_manager.step_environment(
                    {"type": "list_tools"}
                )
                tools = (tools_result.get("observation", {}) or {}).get("tools") or []
                for t in tools:
                    if isinstance(t, dict):
                        n = t.get("name", "")
                        tool_names.append(n)
                        tool_lookup[n] = t
                    else:
                        n = getattr(t, "name", "")
                        tool_names.append(n)
                        tool_lookup[n] = {
                            "name": n,
                            "description": getattr(t, "description", ""),
                            "input_schema": getattr(t, "input_schema", {}),
                        }
            except Exception as e:
                status += f" (list_tools warning: {e})"

        tool_choice = gr.update(
            choices=tool_names,
            value=(tool_names[0] if tool_names else None),
        )
        return (
            status,
            task_md,
            _format_obs_md(result),
            json.dumps(result, indent=2, default=str),
            tool_choice,
            json.dumps(tool_lookup),
        )

    async def _refresh_scenarios():
        try:
            scens = data_loader.list_scenarios()
            names = sorted(s["name"] for s in scens)
            return gr.update(
                choices=names,
                value=(names[0] if names else None),
            ), f"Loaded {len(names)} scenarios."
        except Exception as e:
            return gr.update(choices=[]), f"❌ Failed to load scenarios: {e}"

    async def _on_tool_change(tool_name: str, tool_lookup_json: str):
        try:
            lookup = json.loads(tool_lookup_json or "{}")
        except json.JSONDecodeError:
            lookup = {}
        if not tool_name or tool_name not in lookup:
            return "{}", ""
        meta = lookup[tool_name]
        schema = meta.get("input_schema") or meta.get("inputSchema") or {}
        return _make_args_template(schema), meta.get("description", "")

    async def _do_call_tool(tool_name: str, args_json: str):
        if not tool_name:
            return "*Pick a tool first.*", "{}"
        try:
            args = json.loads(args_json) if args_json.strip() else {}
        except json.JSONDecodeError as e:
            return f"❌ Invalid args JSON: {e}", "{}"
        try:
            result = await web_manager.step_environment(
                {"type": "call_tool", "tool_name": tool_name, "arguments": args}
            )
        except Exception as e:
            return f"❌ step error: {e}", "{}"
        return _format_obs_md(result), json.dumps(result, indent=2, default=str)

    async def _do_list_tools():
        try:
            result = await web_manager.step_environment({"type": "list_tools"})
        except Exception as e:
            return f"❌ {e}", "{}"
        return _format_obs_md(result), json.dumps(result, indent=2, default=str)

    async def _do_verify(verifier_mode: Any, final_answer: str):
        if isinstance(verifier_mode, dict):
            verifier_mode = next(iter(verifier_mode.keys()), "code")
        verifier_mode = str(verifier_mode).strip().lower() or "code"
        if verifier_mode not in ("code", "sql"):
            verifier_mode = "code"
        args: dict[str, Any] = {"verifier_mode": verifier_mode}
        if final_answer:
            args["final_answer"] = final_answer
        try:
            result = await web_manager.step_environment(
                {"type": "call_tool", "tool_name": "verify", "arguments": args}
            )
        except Exception as e:
            return f"❌ verify error: {e}", "{}"
        return _format_obs_md(result), json.dumps(result, indent=2, default=str)

    async def _do_done(keep_session: bool):
        try:
            result = await web_manager.step_environment(
                {
                    "type": "call_tool",
                    "tool_name": "done",
                    "arguments": {"keep_session": bool(keep_session)},
                }
            )
        except Exception as e:
            return f"❌ done error: {e}", "{}", None
        obs = result.get("observation", {}) or {}
        traj_path = obs.get("trajectory_path")
        return (
            _format_obs_md(result),
            json.dumps(result, indent=2, default=str),
            traj_path if traj_path and os.path.exists(traj_path) else None,
        )

    async def _do_list_scenarios_via_tool():
        try:
            result = await web_manager.step_environment(
                {
                    "type": "call_tool",
                    "tool_name": "__list_scenarios__",
                    "arguments": {},
                }
            )
        except Exception as e:
            return f"❌ {e}", "{}"
        return _format_obs_md(result), json.dumps(result, indent=2, default=str)

    agent_state: dict[str, AwmAgent | None] = {"agent": None, "stop": False}

    async def _do_run_agent(
        scenario: str,
        task_idx: int,
        verifier_mode: Any,
        llm_base_url: str,
        llm_api_key: str,
        llm_model: str,
        system_prompt: str,
        max_iter: int,
        temperature: float,
        max_tokens: int,
        auto_verify: bool,
        auto_done: bool,
    ):
        if isinstance(verifier_mode, dict):
            verifier_mode = next(iter(verifier_mode.keys()), "code")
        verifier_mode = str(verifier_mode).strip().lower() or "code"
        if verifier_mode not in ("code", "sql"):
            verifier_mode = "code"
        if not scenario:
            yield "❌ Pick a scenario first.", None
            return
        if not (llm_base_url and llm_api_key and llm_model):
            yield (
                "❌ LLM config required for Agent mode (base_url + api_key + model).",
                None,
            )
            return

        try:
            reset_result = await _safe_reset(
                {
                    "scenario": scenario,
                    "task_idx": int(task_idx),
                    "llm_base_url": llm_base_url,
                    "llm_api_key": llm_api_key,
                    "llm_model": llm_model,
                }
            )
        except Exception as e:
            yield f"❌ Reset failed: {e}", None
            return
        obs = reset_result.get("observation", {}) or {}
        if obs.get("reward_type") not in ("reset_ok", "reset_warning"):
            yield (
                f"❌ Reset returned reward_type={obs.get('reward_type')}, error={obs.get('error')}",
                None,
            )
            return
        task_text = obs.get("task") or ""

        agent = AwmAgent(
            web_manager=web_manager,
            llm_base_url=llm_base_url,
            llm_api_key=llm_api_key,
            llm_model=llm_model,
            system_prompt=system_prompt or DEFAULT_SYSTEM_PROMPT,
            max_iterations=int(max_iter),
            temperature=float(temperature),
            max_tokens=int(max_tokens),
        )
        agent_state["agent"] = agent
        agent_state["stop"] = False

        log_lines: list[str] = [
            "### Agent run started",
            f"**scenario**: `{scenario}`    **task_idx**: `{task_idx}`",
            f"**task**: {task_text}",
            "",
        ]
        # Two outputs: (log_markdown, trajectory_file_path_or_None)
        yield "\n".join(log_lines), None

        traj_path: str | None = None
        async for ev in agent.run(
            task=task_text,
            verifier_mode=verifier_mode,
            auto_verify=auto_verify,
            auto_done=auto_done,
        ):
            if agent_state["stop"]:
                agent.request_stop()

            if ev.kind == "info":
                log_lines.append(f"_ℹ️ {ev.text}_")
            elif ev.kind == "llm_response":
                step = ev.payload.get("step", "?")
                log_lines.append(f"\n**[Step {step}] LLM response:**")
                log_lines.append(f"```\n{ev.text[:4000]}\n```")
            elif ev.kind == "tool_call":
                log_lines.append(f"β†’ **tool_call**: {ev.text}")
            elif ev.kind == "tool_result":
                log_lines.append("← **tool_result:**")
                log_lines.append(f"```\n{ev.text[:1500]}\n```")
            elif ev.kind == "verify":
                log_lines.append(f"\nπŸ§ͺ **Verify**: {ev.text}")
                if ev.payload.get("verify_result"):
                    vr = json.dumps(ev.payload["verify_result"], indent=2, default=str)[
                        :1500
                    ]
                    log_lines.append(f"```json\n{vr}\n```")
            elif ev.kind == "done":
                log_lines.append(f"\n🏁 **Done**: {ev.text}")
                # Capture the trajectory path for download
                p = ev.payload.get("trajectory_path")
                if p and os.path.exists(p):
                    traj_path = p
            elif ev.kind == "error":
                log_lines.append(f"\n❌ **Error**: {ev.text}")

            yield "\n\n".join(log_lines), traj_path

        log_lines.append("\n_Agent run finished._")
        yield "\n\n".join(log_lines), traj_path

    def _do_stop_agent():
        agent_state["stop"] = True
        a = agent_state["agent"]
        if a is not None:
            a.request_stop()
        return "πŸ›‘ Stop requested. The agent will exit before its next iteration."

    async def _load_trajectory_from_state():
        try:
            logs = web_manager.episode_state.action_logs
        except Exception:
            return [], None
        rows: list[list[Any]] = []
        for i, log in enumerate(logs, 1):
            action = getattr(log, "action", {}) or {}
            obs = getattr(log, "observation", {}) or {}
            tool_name = action.get("tool_name") if isinstance(action, dict) else ""
            atype = action.get("type") if isinstance(action, dict) else ""
            rt = obs.get("reward_type") if isinstance(obs, dict) else ""
            preview = ""
            if isinstance(obs, dict):
                tr = obs.get("tool_result")
                if isinstance(tr, str):
                    preview = tr[:200]
                elif tr is not None:
                    preview = json.dumps(tr, default=str)[:200]
                elif obs.get("error"):
                    preview = f"ERROR: {obs['error']}"[:200]
            rows.append(
                [
                    i,
                    atype or "",
                    tool_name or "",
                    rt or "",
                    getattr(log, "reward", None),
                    preview,
                ]
            )
        return rows, None

    with gr.Blocks(title=f"AWM β€” {title}") as blocks:
        tools_state = gr.State("{}")

        gr.Markdown("# πŸ€– Agent World Model β€” Web Console")
        gr.Markdown(
            "Pick a scenario, set LLM credentials (only needed for SQL verifier "
            "or Agent mode), then explore via Human or Agent mode."
        )

        with gr.Group():
            gr.Markdown("## βš™οΈ Setup")
            with gr.Row():
                scenario_dd = gr.Dropdown(
                    choices=[],
                    value=None,
                    label="Scenario",
                    info="1,000 scenarios β€” click 'Load' first",
                    elem_id="awm_scenario_dd",
                    interactive=True,
                    allow_custom_value=False,
                )
                load_scen_btn = gr.Button(
                    "πŸ”„ Load scenarios", scale=0, elem_id="awm_load_scen"
                )
                task_idx_slider = gr.Slider(
                    minimum=0,
                    maximum=9,
                    step=1,
                    value=0,
                    label="Task idx (0-9)",
                    elem_id="awm_task_idx",
                )
                verifier_mode_radio = gr.Textbox(
                    value="code",
                    label="Verifier mode (code or sql)",
                    elem_id="awm_verifier_mode",
                    info="Type 'code' or 'sql'. SQL mode requires LLM config above.",
                )
            with gr.Accordion(
                "LLM config (for SQL verifier and Agent mode)", open=False
            ):
                llm_base_url_in = gr.Textbox(
                    label="LLM base_url",
                    placeholder="https://...",
                    value="",
                    elem_id="awm_llm_base_url",
                )
                llm_api_key_in = gr.Textbox(
                    label="LLM api_key",
                    type="password",
                    value="",
                    elem_id="awm_llm_api_key",
                )
                llm_model_in = gr.Textbox(
                    label="LLM model",
                    placeholder="gpt-4.1, gpt-5, ...",
                    value="",
                    elem_id="awm_llm_model",
                )
            with gr.Accordion("Reward config (advanced)", open=False):
                reward_cfg_in = gr.Code(
                    language="json",
                    value=_DEFAULT_REWARD_JSON,
                    label="reward_config (JSON)",
                    elem_id="awm_reward_cfg",
                )
            with gr.Row():
                reset_btn = gr.Button(
                    "πŸ”„ Reset", variant="primary", elem_id="awm_reset_btn"
                )
                status_box = gr.Markdown("Status: *idle*", elem_id="awm_status_box")
            task_md = gr.Markdown("*No task loaded yet.*", elem_id="awm_task_md")

        with gr.Tabs():
            with gr.Tab("πŸ‘€ Human Mode"):
                with gr.Row():
                    list_tools_btn = gr.Button(
                        "πŸ“‹ List Tools", elem_id="awm_human_list_tools"
                    )
                    list_scenarios_btn = gr.Button(
                        "🌐 List Scenarios", elem_id="awm_human_list_scenarios"
                    )
                with gr.Row():
                    tool_dd = gr.Dropdown(
                        choices=[],
                        value=None,
                        label="Tool",
                        elem_id="awm_human_tool_dd",
                        interactive=True,
                        allow_custom_value=False,
                    )
                tool_desc_md = gr.Markdown("", elem_id="awm_human_tool_desc")
                tool_args_in = gr.Code(
                    language="json",
                    value="{}",
                    label="Tool arguments (JSON)",
                    elem_id="awm_human_tool_args",
                )
                with gr.Row():
                    call_tool_btn = gr.Button(
                        "▢️ Call Tool", variant="primary", elem_id="awm_human_call_tool"
                    )
                with gr.Group():
                    gr.Markdown("### Episode controls")
                    final_answer_in = gr.Textbox(
                        label="Final answer (optional, used in code-mode verify)",
                        value="",
                        elem_id="awm_human_final_answer",
                    )
                    with gr.Row():
                        verify_btn = gr.Button("πŸ§ͺ Verify", elem_id="awm_human_verify")
                        keep_session_cb = gr.Checkbox(
                            value=True,
                            label="keep_session on done",
                            elem_id="awm_human_keep_session",
                        )
                        done_btn = gr.Button(
                            "🏁 Done", variant="stop", elem_id="awm_human_done"
                        )
                gr.Markdown("### Latest observation")
                obs_md = gr.Markdown("*No action yet.*", elem_id="awm_human_obs_md")
                with gr.Accordion("Raw JSON", open=False):
                    obs_json = gr.Code(
                        language="json",
                        value="{}",
                        elem_id="awm_human_obs_json",
                    )
                trajectory_file_dl = gr.File(
                    label="trajectory.json (after Done)",
                    interactive=False,
                    elem_id="awm_human_traj_dl",
                )

            with gr.Tab("πŸ€– Agent Mode"):
                gr.Markdown(
                    "Drives an LLM agent through the env. Reset is done"
                    " automatically using the scenario/task_idx selected above."
                )
                system_prompt_in = gr.Textbox(
                    label="System prompt",
                    value=DEFAULT_SYSTEM_PROMPT,
                    lines=8,
                    max_lines=20,
                    elem_id="awm_agent_system_prompt",
                )
                with gr.Row():
                    max_iter_slider = gr.Slider(
                        minimum=1,
                        maximum=30,
                        step=1,
                        value=10,
                        label="Max iterations",
                        elem_id="awm_agent_max_iter",
                    )
                    temperature_slider = gr.Slider(
                        minimum=0.0,
                        maximum=2.0,
                        step=0.1,
                        value=1.0,
                        label="Temperature",
                        elem_id="awm_agent_temperature",
                    )
                    max_tokens_slider = gr.Slider(
                        minimum=256,
                        maximum=8192,
                        step=128,
                        value=2048,
                        label="Max tokens / call",
                        elem_id="awm_agent_max_tokens",
                    )
                with gr.Row():
                    auto_verify_cb = gr.Checkbox(
                        value=True,
                        label="Auto verify at end",
                        elem_id="awm_agent_auto_verify",
                    )
                    auto_done_cb = gr.Checkbox(
                        value=True,
                        label="Auto done at end (keep_session=True)",
                        elem_id="awm_agent_auto_done",
                    )
                with gr.Row():
                    start_agent_btn = gr.Button(
                        "▢️ Start Agent", variant="primary", elem_id="awm_agent_start"
                    )
                    stop_agent_btn = gr.Button(
                        "⏹ Stop", variant="stop", elem_id="awm_agent_stop"
                    )
                stop_status = gr.Markdown("", elem_id="awm_agent_stop_status")
                agent_log_md = gr.Markdown(
                    "_Agent log will appear here._", elem_id="awm_agent_log_md"
                )
                agent_traj_dl = gr.File(
                    label="trajectory.json (auto-populated when agent finishes)",
                    interactive=False,
                    elem_id="awm_agent_traj_dl",
                )

            with gr.Tab("πŸ“œ Trajectory"):
                gr.Markdown(
                    "Step-by-step history of the current episode. "
                    "Click **Refresh** after actions to update."
                )
                refresh_traj_btn = gr.Button("πŸ”„ Refresh", elem_id="awm_traj_refresh")
                traj_table = gr.Dataframe(
                    headers=["#", "type", "tool", "reward_type", "reward", "preview"],
                    datatype=["number", "str", "str", "str", "number", "str"],
                    interactive=False,
                    elem_id="awm_traj_table",
                )

            if readme_md:
                with gr.Tab("πŸ“– README"):
                    gr.Markdown(readme_md)

        load_scen_btn.click(
            _refresh_scenarios, inputs=None, outputs=[scenario_dd, status_box]
        )
        reset_btn.click(
            _do_reset,
            inputs=[
                scenario_dd,
                task_idx_slider,
                llm_base_url_in,
                llm_api_key_in,
                llm_model_in,
                reward_cfg_in,
            ],
            outputs=[status_box, task_md, obs_md, obs_json, tool_dd, tools_state],
        )
        list_tools_btn.click(_do_list_tools, inputs=None, outputs=[obs_md, obs_json])
        list_scenarios_btn.click(
            _do_list_scenarios_via_tool, inputs=None, outputs=[obs_md, obs_json]
        )
        tool_dd.change(
            _on_tool_change,
            inputs=[tool_dd, tools_state],
            outputs=[tool_args_in, tool_desc_md],
        )
        call_tool_btn.click(
            _do_call_tool,
            inputs=[tool_dd, tool_args_in],
            outputs=[obs_md, obs_json],
        )
        verify_btn.click(
            _do_verify,
            inputs=[verifier_mode_radio, final_answer_in],
            outputs=[obs_md, obs_json],
        )
        done_btn.click(
            _do_done,
            inputs=[keep_session_cb],
            outputs=[obs_md, obs_json, trajectory_file_dl],
        )
        start_agent_btn.click(
            _do_run_agent,
            inputs=[
                scenario_dd,
                task_idx_slider,
                verifier_mode_radio,
                llm_base_url_in,
                llm_api_key_in,
                llm_model_in,
                system_prompt_in,
                max_iter_slider,
                temperature_slider,
                max_tokens_slider,
                auto_verify_cb,
                auto_done_cb,
            ],
            outputs=[agent_log_md, agent_traj_dl],
        )
        stop_agent_btn.click(_do_stop_agent, inputs=None, outputs=[stop_status])
        refresh_traj_btn.click(
            _load_trajectory_from_state,
            inputs=None,
            outputs=[traj_table, trajectory_file_dl],
        )

    return blocks