browser_dialog_tool.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Agent-facing tool: respond to a native JS dialog captured by the CDP supervisor.
  2. Response-only: the agent calls ``browser_dialog(action=...)`` for a dialog the
  3. supervisor holds; with several queued, the error names the candidate ids. Gated on ``_browser_cdp_check`` so it
  4. appears together with ``browser_cdp``. Design: ``website/docs/developer-guide/browser-supervisor.md``.
  5. """
  6. from __future__ import annotations
  7. import json
  8. from typing import Any, Dict, Optional
  9. from tools.browser_supervisor import SUPERVISOR_REGISTRY
  10. from tools.registry import registry
  11. BROWSER_DIALOG_SCHEMA: dict[str, Any] = {
  12. "name": "browser_dialog",
  13. "description": (
  14. "Accept or dismiss a native JS dialog (alert/confirm/prompt) blocking the page.\n\n"
  15. "**Workflow:** when a page is blocked by a native dialog the CDP "
  16. "supervisor captured, call this tool with ``action='accept'`` or "
  17. "``action='dismiss'``.\n\n"
  18. "**Prompt dialogs:** pass ``prompt_text`` to supply the response "
  19. "string. Ignored for alert/confirm/beforeunload.\n\n"
  20. "**Multiple dialogs:** if more than one dialog is queued (rare — "
  21. "happens when a second dialog fires while the first is still open), "
  22. "pass ``dialog_id`` (the error lists the candidates) to disambiguate.\n\n"
  23. "**Availability:** only present when a CDP endpoint is attached — "
  24. "a local Chromium-family browser via ``/browser connect``, or "
  25. "``browser.cdp_url`` in config.yaml."
  26. ),
  27. "parameters": {
  28. "type": "object",
  29. "properties": {
  30. "action": {
  31. "type": "string",
  32. "enum": ["accept", "dismiss"],
  33. "description": (
  34. "'accept' clicks OK / returns the prompt text. "
  35. "'dismiss' clicks Cancel / returns null from prompt(). "
  36. "For ``beforeunload`` dialogs: 'accept' allows the "
  37. "navigation, 'dismiss' keeps the page."
  38. ),
  39. },
  40. "prompt_text": {
  41. "type": "string",
  42. "description": (
  43. "Response string for a ``prompt()`` dialog. Ignored for "
  44. "other dialog types. Defaults to empty string."
  45. ),
  46. },
  47. "dialog_id": {
  48. "type": "string",
  49. "description": (
  50. "Specific dialog to respond to, from "
  51. "the candidate ids a previous call listed. Required "
  52. "only when multiple dialogs are queued."
  53. ),
  54. },
  55. },
  56. "required": ["action"],
  57. },
  58. }
  59. def browser_dialog(
  60. action: str,
  61. prompt_text: Optional[str] = None,
  62. dialog_id: Optional[str] = None,
  63. task_id: Optional[str] = None,
  64. ) -> str:
  65. """Respond to a pending dialog on the active task's CDP supervisor."""
  66. supervisor = SUPERVISOR_REGISTRY.get(task_id or "default")
  67. if supervisor is None:
  68. return json.dumps({
  69. "success": False,
  70. "error": (
  71. "No CDP supervisor is attached to this task: no browser "
  72. "session has been started yet. Call browser_exec or "
  73. "/browser connect first."
  74. ),
  75. })
  76. result = supervisor.respond_to_dialog(action=action, prompt_text=prompt_text, dialog_id=dialog_id)
  77. if result.get("ok"):
  78. return json.dumps({"success": True, "action": action, "dialog": result.get("dialog", {})})
  79. return json.dumps({"success": False, "error": result.get("error", "unknown error")})
  80. def _browser_dialog_check() -> bool:
  81. """Gate: same as ``browser_cdp`` so the two tools appear/disappear together."""
  82. from tools.browser_cdp_tool import _browser_cdp_check
  83. return _browser_cdp_check()
  84. registry.register(
  85. name="browser_dialog",
  86. toolset="browser-cdp",
  87. schema=BROWSER_DIALOG_SCHEMA,
  88. handler=lambda args, **kw: browser_dialog(action=args.get("action", ""), prompt_text=args.get("prompt_text"),
  89. dialog_id=args.get("dialog_id"), task_id=kw.get("task_id")),
  90. check_fn=_browser_dialog_check,
  91. emoji="💬",
  92. )