feat: add output to ToolUsageFinishedEvent

This commit is contained in:
Eduardo Chiarotti
2025-03-26 09:58:37 -03:00
parent ebb585e494
commit fbd9d832ef
2 changed files with 20 additions and 7 deletions

View File

@@ -117,7 +117,10 @@ class ToolUsage:
self._printer.print(content=f"\n\n{error}\n", color="red") self._printer.print(content=f"\n\n{error}\n", color="red")
return error return error
if isinstance(tool, CrewStructuredTool) and tool.name == self._i18n.tools("add_image")["name"]: # type: ignore if (
isinstance(tool, CrewStructuredTool)
and tool.name == self._i18n.tools("add_image")["name"]
): # type: ignore
try: try:
result = self._use(tool_string=tool_string, tool=tool, calling=calling) result = self._use(tool_string=tool_string, tool=tool, calling=calling)
return result return result
@@ -181,7 +184,9 @@ class ToolUsage:
if calling.arguments: if calling.arguments:
try: try:
acceptable_args = tool.args_schema.model_json_schema()["properties"].keys() # type: ignore acceptable_args = tool.args_schema.model_json_schema()[
"properties"
].keys() # type: ignore
arguments = { arguments = {
k: v k: v
for k, v in calling.arguments.items() for k, v in calling.arguments.items()
@@ -202,7 +207,7 @@ class ToolUsage:
error=e, tool=tool.name, tool_inputs=tool.description error=e, tool=tool.name, tool_inputs=tool.description
) )
error = ToolUsageErrorException( error = ToolUsageErrorException(
f'\n{error_message}.\nMoving on then. {self._i18n.slice("format").format(tool_names=self.tools_names)}' f"\n{error_message}.\nMoving on then. {self._i18n.slice('format').format(tool_names=self.tools_names)}"
).message ).message
self.task.increment_tools_errors() self.task.increment_tools_errors()
if self.agent.verbose: if self.agent.verbose:
@@ -244,6 +249,7 @@ class ToolUsage:
tool_calling=calling, tool_calling=calling,
from_cache=from_cache, from_cache=from_cache,
started_at=started_at, started_at=started_at,
result=result,
) )
if ( if (
@@ -380,7 +386,7 @@ class ToolUsage:
raise raise
else: else:
return ToolUsageErrorException( return ToolUsageErrorException(
f'{self._i18n.errors("tool_arguments_error")}' f"{self._i18n.errors('tool_arguments_error')}"
) )
if not isinstance(arguments, dict): if not isinstance(arguments, dict):
@@ -388,7 +394,7 @@ class ToolUsage:
raise raise
else: else:
return ToolUsageErrorException( return ToolUsageErrorException(
f'{self._i18n.errors("tool_arguments_error")}' f"{self._i18n.errors('tool_arguments_error')}"
) )
return ToolCalling( return ToolCalling(
@@ -416,7 +422,7 @@ class ToolUsage:
if self.agent.verbose: if self.agent.verbose:
self._printer.print(content=f"\n\n{e}\n", color="red") self._printer.print(content=f"\n\n{e}\n", color="red")
return ToolUsageErrorException( # type: ignore # Incompatible return value type (got "ToolUsageErrorException", expected "ToolCalling | InstructorToolCalling") return ToolUsageErrorException( # type: ignore # Incompatible return value type (got "ToolUsageErrorException", expected "ToolCalling | InstructorToolCalling")
f'{self._i18n.errors("tool_usage_error").format(error=e)}\nMoving on then. {self._i18n.slice("format").format(tool_names=self.tools_names)}' f"{self._i18n.errors('tool_usage_error').format(error=e)}\nMoving on then. {self._i18n.slice('format').format(tool_names=self.tools_names)}"
) )
return self._tool_calling(tool_string) return self._tool_calling(tool_string)
@@ -492,7 +498,12 @@ class ToolUsage:
crewai_event_bus.emit(self, ToolUsageErrorEvent(**{**event_data, "error": e})) crewai_event_bus.emit(self, ToolUsageErrorEvent(**{**event_data, "error": e}))
def on_tool_use_finished( def on_tool_use_finished(
self, tool: Any, tool_calling: ToolCalling, from_cache: bool, started_at: float self,
tool: Any,
tool_calling: ToolCalling,
from_cache: bool,
started_at: float,
result: Any,
) -> None: ) -> None:
finished_at = time.time() finished_at = time.time()
event_data = self._prepare_event_data(tool, tool_calling) event_data = self._prepare_event_data(tool, tool_calling)
@@ -501,6 +512,7 @@ class ToolUsage:
"started_at": datetime.datetime.fromtimestamp(started_at), "started_at": datetime.datetime.fromtimestamp(started_at),
"finished_at": datetime.datetime.fromtimestamp(finished_at), "finished_at": datetime.datetime.fromtimestamp(finished_at),
"from_cache": from_cache, "from_cache": from_cache,
"output": result,
} }
) )
crewai_event_bus.emit(self, ToolUsageFinishedEvent(**event_data)) crewai_event_bus.emit(self, ToolUsageFinishedEvent(**event_data))

View File

@@ -30,6 +30,7 @@ class ToolUsageFinishedEvent(ToolUsageEvent):
started_at: datetime started_at: datetime
finished_at: datetime finished_at: datetime
from_cache: bool = False from_cache: bool = False
output: Any
type: str = "tool_usage_finished" type: str = "tool_usage_finished"