diff --git a/agentops/client.py b/agentops/client.py index 544285ef..3ed8f1cb 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -75,6 +75,7 @@ def __init__(self, self._session = None self._worker = None + self._tags_for_future_session = None self._env_data_opt_out = os.getenv('AGENTOPS_ENV_DATA_OPT_OUT') and os.getenv('AGENTOPS_ENV_DATA_OPT_OUT').lower() == 'true' @@ -135,7 +136,8 @@ def record(self, event: Event | ErrorEvent): Args: event (Event): The event to record. """ - + if not event.end_timestamp or event.init_timestamp == event.end_timestamp: + event.end_timestamp = get_ISO_time() if self._session is not None and not self._session.has_ended: if isinstance(event, ErrorEvent): if event.trigger_event: @@ -270,13 +272,13 @@ def end_session(self, self._session.video = video self._session.end_session(end_state, end_state_reason) - token_cost = Decimal(self._worker.end_session(self._session)) + token_cost = self._worker.end_session(self._session) if token_cost == 'unknown': print('🖇 AgentOps: Could not determine cost of run.') else: - + token_cost_d = Decimal(token_cost) print('🖇 AgentOps: This run cost ${}'.format('{:.2f}'.format( - token_cost) if token_cost == 0 else '{:.6f}'.format(token_cost))) + token_cost_d) if token_cost_d == 0 else '{:.6f}'.format(token_cost_d))) self._session = None self._worker = None diff --git a/tests/test_events.py b/tests/test_events.py new file mode 100644 index 00000000..4e156bdf --- /dev/null +++ b/tests/test_events.py @@ -0,0 +1,29 @@ +import time +import requests_mock +import pytest +import agentops +from agentops import ActionEvent + + +@pytest.fixture +def mock_req(): + with requests_mock.Mocker() as m: + url = 'https://api.agentops.ai' + m.post(url + '/events', text='ok') + m.post(url + '/sessions', json={'status': 'success', 'token_cost': 5}) + yield m + +class TestEvents: + def setup_method(self): + self.api_key = "random_api_key" + self.event_type = 'test_event_type' + self.config = agentops.Configuration(api_key=self.api_key, max_wait_time=50, max_queue_size=1) + + def test_record_timestamp(self, mock_req): + agentops.init(api_key=self.api_key) + + event = ActionEvent() + time.sleep(0.15) + agentops.record(event) + + assert event.init_timestamp != event.end_timestamp \ No newline at end of file