forked from crewAIInc/crewAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96427c1
commit 30e26c9
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from typing import Any, Dict | ||
from pydantic import BaseModel | ||
from datetime import datetime | ||
|
||
|
||
class ToolUsageEvent(BaseModel): | ||
agent_key: str | ||
agent_role: str | ||
tool_name: str | ||
tool_args: Dict[str, Any] | ||
tool_class: str | ||
run_attempts: int | None = None | ||
delegations: int | None = None | ||
|
||
|
||
class ToolUsageFinished(ToolUsageEvent): | ||
started_at: datetime | ||
finished_at: datetime | ||
from_cache: bool = False | ||
|
||
|
||
class ToolUsageError(ToolUsageEvent): | ||
error: str |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any, Callable, Generic, List, Dict, Type, TypeVar | ||
from functools import wraps | ||
from pydantic import BaseModel | ||
|
||
|
||
T = TypeVar("T") | ||
EVT = TypeVar("EVT", bound=BaseModel) | ||
|
||
|
||
class Emitter(Generic[T, EVT]): | ||
_listeners: Dict[Type[EVT], List[Callable]] = {} | ||
|
||
def on(self, event_type: Type[EVT]): | ||
def decorator(func: Callable): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
self._listeners.setdefault(event_type, []).append(wrapper) | ||
return wrapper | ||
|
||
return decorator | ||
|
||
def emit(self, source: T, event: EVT) -> None: | ||
event_type = type(event) | ||
for func in self._listeners.get(event_type, []): | ||
func(source, event) | ||
|
||
|
||
default_emitter = Emitter[Any, BaseModel]() | ||
|
||
|
||
def emit(source: Any, event: BaseModel, raise_on_error: bool = False) -> None: | ||
try: | ||
default_emitter.emit(source, event) | ||
except Exception as e: | ||
if raise_on_error: | ||
raise e | ||
else: | ||
print(f"Error emitting event: {e}") | ||
|
||
|
||
def on(event_type: Type[BaseModel]) -> Callable: | ||
return default_emitter.on(event_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters