diff --git a/agentops/event.py b/agentops/event.py index c6200aca1..1ed356bfb 100644 --- a/agentops/event.py +++ b/agentops/event.py @@ -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 @@ -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) { ... @@ -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 @@ -105,7 +108,7 @@ class ToolEvent(Event): @dataclass -class ErrorEvent: +class ErrorEvent(Event): """ For recording any errors e.g. ones related to agent execution @@ -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