Skip to content

Commit

Permalink
Refactored ErrorEvent to inherit from Event class
Browse files Browse the repository at this point in the history
- Added `session_id` field to `Event` class for session tracking;
- Improved error handling and timestamp management
- Maintained backward compatibility through `timestamp` property

Signed-off-by: Teo <[email protected]>
  • Loading branch information
teocns committed Jan 16, 2025
1 parent fa49968 commit ef4b5c4
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions agentops/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
Event: Represents discrete events to be recorded.
"""

import traceback
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Sequence, Union
from .helpers import get_ISO_time, check_call_stack_for_agent_id
from .enums import EventType
from uuid import UUID, uuid4
import traceback

from .enums import EventType
from .helpers import check_call_stack_for_agent_id, get_ISO_time


@dataclass
Expand All @@ -25,6 +26,7 @@ class Event:
end_timestamp(str): A timestamp indicating when the event ended. Defaults to the time when this Event was instantiated.
agent_id(UUID, optional): The unique identifier of the agent that triggered the event.
id(UUID): A unique identifier for the event. Defaults to a new UUID.
session_id(UUID, optional): The unique identifier of the session that the event belongs to.
foo(x=1) {
...
Expand All @@ -43,6 +45,7 @@ class Event:
end_timestamp: Optional[str] = None
agent_id: Optional[UUID] = field(default_factory=check_call_stack_for_agent_id)
id: UUID = field(default_factory=uuid4)
session_id: Optional[UUID] = None


@dataclass
Expand Down Expand Up @@ -105,7 +108,7 @@ class ToolEvent(Event):


@dataclass
class ErrorEvent:
class ErrorEvent(Event):
"""
For recording any errors e.g. ones related to agent execution
Expand All @@ -115,21 +118,31 @@ class ErrorEvent:
code(str, optional): A code that can be used to identify the error e.g. 501.
details(str, optional): Detailed information about the error.
logs(str, optional): For detailed information/logging related to the error.
timestamp(str): A timestamp indicating when the error occurred. Defaults to the time when this ErrorEvent was instantiated.
"""

# Inherit common Event fields
event_type: str = field(default=EventType.ERROR.value)

# Error-specific fields
trigger_event: Optional[Event] = None
exception: Optional[BaseException] = None
error_type: Optional[str] = None
code: Optional[str] = None
details: Optional[Union[str, Dict[str, str]]] = None
logs: Optional[str] = field(default_factory=traceback.format_exc)
timestamp: str = field(default_factory=get_ISO_time)

def __post_init__(self):
self.event_type = EventType.ERROR.value
"""Process exception if provided"""
if self.exception:
self.error_type = self.error_type or type(self.exception).__name__
self.details = self.details or str(self.exception)
self.exception = None # removes exception from serialization

# Ensure end timestamp is set
if not self.end_timestamp:
self.end_timestamp = get_ISO_time()

@property
def timestamp(self) -> str:
"""Maintain backward compatibility with old code expecting timestamp"""
return self.init_timestamp

0 comments on commit ef4b5c4

Please sign in to comment.