Skip to content

decorators

hook

Source code in agenty/decorators.py
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
class hook:
    @staticmethod
    def input(
        func: Callable[HookParam, AgentInputT],
    ) -> Callable[HookParam, AgentInputT]:
        """Decorator to register a method as an input hook. It provides a simple way to designate methods that handle input processing.

        Args:
            func: The function to be registered as an input hook.

        Returns:
            The original function, marked as an input hook.

        Example:
            ```python
            class MyAgent(Agent[str, str]):
                @hook.input
                def process_input(self, input: str) -> str:
                    return input.upper()
            ```
        """
        setattr(func, "_is_hook_input", True)
        return func

    @staticmethod
    def output(
        func: Callable[HookParam, AgentOutputT],
    ) -> Callable[HookParam, AgentOutputT]:
        """Decorator to register a method as an output hook. It provides a simple way to designate methods that handle output processing.

        Args:
            func: The function to be registered as an output hook.

        Returns:
            The original function, marked as an output hook.

        Example:
            ```python
            class MyAgent(Agent[str, str]):
                @hook.output
                def process_output(self, output: str) -> str:
                    return output.lower()
            ```
        """
        setattr(func, "_is_hook_output", True)
        return func

input(func) staticmethod

Decorator to register a method as an input hook. It provides a simple way to designate methods that handle input processing.

Parameters:

Name Type Description Default
func Callable[HookParam, AgentInputT]

The function to be registered as an input hook.

required

Returns:

Type Description
Callable[HookParam, AgentInputT]

The original function, marked as an input hook.

Example
class MyAgent(Agent[str, str]):
    @hook.input
    def process_input(self, input: str) -> str:
        return input.upper()
Source code in agenty/decorators.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@staticmethod
def input(
    func: Callable[HookParam, AgentInputT],
) -> Callable[HookParam, AgentInputT]:
    """Decorator to register a method as an input hook. It provides a simple way to designate methods that handle input processing.

    Args:
        func: The function to be registered as an input hook.

    Returns:
        The original function, marked as an input hook.

    Example:
        ```python
        class MyAgent(Agent[str, str]):
            @hook.input
            def process_input(self, input: str) -> str:
                return input.upper()
        ```
    """
    setattr(func, "_is_hook_input", True)
    return func

output(func) staticmethod

Decorator to register a method as an output hook. It provides a simple way to designate methods that handle output processing.

Parameters:

Name Type Description Default
func Callable[HookParam, AgentOutputT]

The function to be registered as an output hook.

required

Returns:

Type Description
Callable[HookParam, AgentOutputT]

The original function, marked as an output hook.

Example
class MyAgent(Agent[str, str]):
    @hook.output
    def process_output(self, output: str) -> str:
        return output.lower()
Source code in agenty/decorators.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@staticmethod
def output(
    func: Callable[HookParam, AgentOutputT],
) -> Callable[HookParam, AgentOutputT]:
    """Decorator to register a method as an output hook. It provides a simple way to designate methods that handle output processing.

    Args:
        func: The function to be registered as an output hook.

    Returns:
        The original function, marked as an output hook.

    Example:
        ```python
        class MyAgent(Agent[str, str]):
            @hook.output
            def process_output(self, output: str) -> str:
                return output.lower()
        ```
    """
    setattr(func, "_is_hook_output", True)
    return func

tool(func)

Decorator to register a method as an agent tool.

This decorator marks a method as a tool that can be called by the agent during execution. It handles proper type casting and logging of tool invocations.

Parameters:

Name Type Description Default
func Callable[ToolParams, AgentOutputT]

The function to be registered as a tool. Must be a method of an Agent subclass with proper type annotations.

required

Returns:

Type Description
Callable[ToolParams, AgentOutputT]

A wrapped function that maintains the original function's type signature

Callable[ToolParams, AgentOutputT]

while providing tool registration and logging functionality.

Example
class MyAgent(Agent[str, str]):
    @tool
    def my_tool(self, param: str) -> str:
        return f"Processed {param}"
Source code in agenty/decorators.py
 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
def tool(
    func: Callable[ToolParams, AgentOutputT],
) -> Callable[ToolParams, AgentOutputT]:
    """Decorator to register a method as an agent tool.

    This decorator marks a method as a tool that can be called by the agent during
    execution. It handles proper type casting and logging of tool invocations.

    Args:
        func: The function to be registered as a tool. Must be a method of an Agent
             subclass with proper type annotations.

    Returns:
        A wrapped function that maintains the original function's type signature
        while providing tool registration and logging functionality.

    Example:
        ```python
        class MyAgent(Agent[str, str]):
            @tool
            def my_tool(self, param: str) -> str:
                return f"Processed {param}"
        ```
    """
    setattr(func, "_is_tool", True)

    @wraps(func)
    def wrapper(
        ctx: RunContext[Agent[AgentInputT, AgentOutputT]], *args: Any, **kwargs: Any
    ) -> AgentOutputT:
        self = ctx.deps
        _func = cast(
            Callable[
                Concatenate[Agent[AgentInputT, AgentOutputT], ToolParams],
                AgentOutputT,
            ],
            func,
        )
        result = _func(self, *args, **kwargs)
        logger.debug(
            {
                "tool": func.__name__,
                "result": result,
                "args": args,
                "kwargs": kwargs,
                "agent": type(self).__name__,
            }
        )
        return result

    return cast(Callable[ToolParams, AgentOutputT], wrapper)