import inspect
from typing import Dict, List, Literal, Optional
from langchain_core.messages import ToolCall
from langchain_core.runnables import RunnableConfig
from langchain_core.tools import BaseTool, tool
from cuga.backend.browser_env.tools.providers import BrowserToolImplProvider
from cuga.backend.cuga_graph.nodes.browser.action_agent.tools.alert import Alert
# ----------------------------------------------------------------------------
# Original public API (tool functions)
# ----------------------------------------------------------------------------
def get_params_and_values_except(func, exclude_param, *args, **kwargs):
# Retrieve the signature of the function
sig = inspect.signature(func)
# Bind the provided arguments to the function's parameters
bound_args = sig.bind(*args, **kwargs)
# Apply default values for missing arguments
bound_args.apply_defaults()
# Create a dictionary of parameter names and their values, excluding the specified one
params_and_values = {k: v for k, v in bound_args.arguments.items() if k != exclude_param}
return params_and_values
# ----------------------------------------------------------
# Delegation helper – choose implementation set at import time
# ----------------------------------------------------------
def _retrieve_impl(config: RunnableConfig, tool_name: str):
impl: BrowserToolImplProvider = config['configurable']['tool_impl']
tool_impl = impl.implementations()[tool_name]
return tool_impl
@tool
async def go_back(config: RunnableConfig | None = None):
"""
Go back to previous page.
Examples:
"""
_go_back = _retrieve_impl(config, 'go_back')
await _go_back(config)
@tool
async def open_app(
app_name: Literal[
"wikipedia",
"map",
"reddit",
"gitlab",
"shopping",
"shopping_admin",
],
config: RunnableConfig = None,
):
"""
Open an application
Examples:
open_app('reddit')
"""
_open_app = _retrieve_impl(config, 'open_app')
return await _open_app(app_name=app_name, config=config)
@tool
async def click(
bid: str,
button: Literal["left", "middle", "right"] = "left",
modifiers: Optional[List[Literal["Alt", "Control", "Meta", "Shift"]]] = [],
config: RunnableConfig = None,
) -> Optional[Alert]:
"""
Click an element.
Examples:
click('a51')
click('b22', button="right")
click('48', button="middle", modifiers=["Shift"])
"""
_click = _retrieve_impl(config, 'click')
return await _click(bid=bid, button=button, modifiers=modifiers or [], config=config)
@tool
async def select_option(bid: str, options: str | list[str], config: RunnableConfig = None) -> Optional[Alert]:
"""
Select one or more options in a dropdown – delegate to implementation.
"""
_select_option = _retrieve_impl(config, 'select_option')
return await _select_option(bid=bid, options=options, config=config)
@tool
async def type(bid: str, value: str, press_enter: bool, config: RunnableConfig) -> Optional[Alert]:
"""
Fill out a form field. It focuses the element and triggers an input event with the entered text.
It works for ,