Skip to content

Commit

Permalink
add processing of the final output_handler call
Browse files Browse the repository at this point in the history
  • Loading branch information
User committed Jun 28, 2024
1 parent 5a859a0 commit 60bec6b
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions motleycrew/agents/parent.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ def __init__(self, output: Any):
self.output = output


def _run_tool_direct_decorator(func: Callable):
"""Decorator of the tool's _run method, for intercepting a DirectOutput exception"""

def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
except DirectOutput as direct_exc:
return direct_exc
return result

return wrapper


def run_tool_direct_decorator(func: Callable):
"""Decorator of the tool's run method, for intercepting a DirectOutput exception"""

def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
if isinstance(result, DirectOutput):
raise result
return result

return wrapper


class MotleyAgentParent(MotleyAgentAbstractParent, Runnable):
def __init__(
self,
Expand Down Expand Up @@ -150,6 +175,16 @@ def handle_agent_output(*args, **kwargs):
args_schema=self.output_handler.args_schema,
)

object.__setattr__(
prepared_output_handler,
"_run",
_run_tool_direct_decorator(prepared_output_handler._run),
)

object.__setattr__(
prepared_output_handler, "run", run_tool_direct_decorator(prepared_output_handler.run)
)

return MotleyTool.from_langchain_tool(prepared_output_handler)

@staticmethod
Expand Down

0 comments on commit 60bec6b

Please sign in to comment.