Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bboynton97 committed Jun 22, 2024
1 parent 688a883 commit f1f10de
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 25 deletions.
2 changes: 0 additions & 2 deletions agentops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ def record(event: Union[Event, ErrorEvent]):
Client().record(event)


@check_init
def add_tags(tags: List[str]):
"""
Append to session tags at runtime.
Expand All @@ -187,7 +186,6 @@ def add_tags(tags: List[str]):
Client().add_tags(tags)


@check_init
def set_tags(tags: List[str]):
"""
Replace session tags at runtime.
Expand Down
10 changes: 7 additions & 3 deletions agentops/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ def new_init(self, *args, **kwargs):
original_init(self, *args, **kwargs)
self.agent_ops_agent_id = str(uuid4())

if kwargs.get("session_id", None):
self.agent_ops_session_id = kwargs.get("session_id")
session_id = None

Check warning on line 22 in agentops/agent.py

View check run for this annotation

Codecov / codecov/patch

agentops/agent.py#L22

Added line #L22 was not covered by tests
if kwargs.get("session", None):
session_id = kwargs.get("session").session_id
self.agent_ops_session_id = session_id

Check warning on line 25 in agentops/agent.py

View check run for this annotation

Codecov / codecov/patch

agentops/agent.py#L24-L25

Added lines #L24 - L25 were not covered by tests

Client().create_agent(
name=self.agent_ops_agent_name, agent_id=self.agent_ops_agent_id
name=self.agent_ops_agent_name,
agent_id=self.agent_ops_agent_id,
session_id=session_id,
)
except AttributeError as e:
logger.warning(
Expand Down
40 changes: 33 additions & 7 deletions agentops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
get_partner_frameworks,
singleton,
conditional_singleton,
safe_serialize,
)
from .http_client import HttpClient
from .session import Session
from .host_env import get_host_env
from .log_config import logger
Expand Down Expand Up @@ -193,10 +195,12 @@ def set_tags(self, tags: List[str]) -> None:
Args:
tags (List[str]): The list of tags to set.
"""
self._tags_for_future_session = tags

session = self._safe_get_session()
session.set_tags(tags=tags)
try:
session = self._safe_get_session()
session.set_tags(tags=tags)

Check warning on line 201 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L201

Added line #L201 was not covered by tests
except ValueError:
self._tags_for_future_session = tags

def record(self, event: Union[Event, ErrorEvent]) -> None:
"""
Expand Down Expand Up @@ -433,12 +437,34 @@ def end_session(

self._sessions.remove(session)

def create_agent(self, name: str, agent_id: Optional[str] = None):
def create_agent(
self,
name: str,
agent_id: Optional[str] = None,
session: Optional[Session] = None,
):
if agent_id is None:
agent_id = str(uuid4())
if self._worker:
self._worker.create_agent(name=name, agent_id=agent_id)
return agent_id

# if a session is passed in, use multi-session logic
if session:
return session.create_agent(name=name, agent_id=agent_id)

Check warning on line 451 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L451

Added line #L451 was not covered by tests
else:
# if no session passed, assume single session
session = self._safe_get_session()
payload = {

Check warning on line 455 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L454-L455

Added lines #L454 - L455 were not covered by tests
"id": agent_id,
"name": name,
}

serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(

Check warning on line 461 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L460-L461

Added lines #L460 - L461 were not covered by tests
f"{self.config.endpoint}/v2/create_agent",
serialized_payload,
jwt=session.jwt,
)

return agent_id

Check warning on line 467 in agentops/client.py

View check run for this annotation

Codecov / codecov/patch

agentops/client.py#L467

Added line #L467 was not covered by tests

def _handle_unclean_exits(self):
def cleanup(end_state: str = "Fail", end_state_reason: Optional[str] = None):
Expand Down
18 changes: 17 additions & 1 deletion agentops/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .config import ClientConfiguration
from .helpers import get_ISO_time, filter_unjsonable, safe_serialize
from typing import Optional, List, Union
from uuid import UUID
from uuid import UUID, uuid4

from .http_client import HttpClient
from .worker import Worker
Expand Down Expand Up @@ -224,3 +224,19 @@ def run(self) -> None:
time.sleep(self.config.max_wait_time / 1000)
if self.queue:
self.flush_queue()

def create_agent(self, name, agent_id):
if agent_id is None:
agent_id = str(uuid4())

Check warning on line 230 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L230

Added line #L230 was not covered by tests

payload = {

Check warning on line 232 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L232

Added line #L232 was not covered by tests
"id": agent_id,
"name": name,
}

serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(

Check warning on line 238 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L237-L238

Added lines #L237 - L238 were not covered by tests
f"{self.config.endpoint}/v2/create_agent", serialized_payload, jwt=self.jwt
)

return agent_id

Check warning on line 242 in agentops/session.py

View check run for this annotation

Codecov / codecov/patch

agentops/session.py#L242

Added line #L242 was not covered by tests
12 changes: 0 additions & 12 deletions agentops/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,6 @@ def flush_queue(self) -> None:
logger.debug(serialized_payload)
logger.debug("</AGENTOPS_DEBUG_OUTPUT>\n")

Check warning on line 66 in agentops/worker.py

View check run for this annotation

Codecov / codecov/patch

agentops/worker.py#L63-L66

Added lines #L63 - L66 were not covered by tests

def create_agent(self, agent_id, name, session_id: str):
payload = {
"id": agent_id,
"name": name,
"session_id": session_id,
}

serialized_payload = safe_serialize(payload).encode("utf-8")
HttpClient.post(
f"{self.config.endpoint}/v2/create_agent", serialized_payload, jwt=self.jwt
)

def run(self) -> None:
while not self.stop_flag.is_set():
time.sleep(self.config.max_wait_time / 1000)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ def test_inherit_session_id(self, mock_req):

agentops.end_all_sessions()

def test_add_tags_before_session(self, mock_req):
agentops.add_tags(["pre-session-tag"])
agentops.start_session(config=self.config)

request_json = mock_req.last_request.json()
assert request_json["session"]["tags"] == ["pre-session-tag"]

def test_set_tags_before_session(self, mock_req):
agentops.set_tags(["pre-session-tag"])
agentops.start_session(config=self.config)

request_json = mock_req.last_request.json()
assert request_json["session"]["tags"] == ["pre-session-tag"]


class TestMultiSessions:
def setup_method(self):
Expand Down

0 comments on commit f1f10de

Please sign in to comment.