from __future__ import annotations
import functools
from typing import Any, Callable, Dict, Optional, TypeVar
from autogen_core.tools import FunctionTool
from .client import A1Client
F = TypeVar("F", bound=Callable[..., Any])
def build_a1_function_tool(
fn: F,
chain: Any,
executor_pk_hex: str,
intent_name: str,
intent_params: Optional[Dict[str, str]] = None,
gateway_url: Optional[str] = None,
name: Optional[str] = None,
description: Optional[str] = None,
) -> FunctionTool:
client = A1Client(gateway_url)
@functools.wraps(fn)
def wrapper(*args: Any, **kwargs: Any) -> Any:
client.authorize(
chain=chain,
intent_name=intent_name,
executor_pk_hex=executor_pk_hex,
intent_params=intent_params,
)
return fn(*args, **kwargs)
return FunctionTool(
wrapper,
name=name or fn.__name__,
description=description or fn.__doc__ or "Authorized tool",
)