-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DO NOT MERGE] Refactoring core session management #486
Open
teocns
wants to merge
36
commits into
AgentOps-AI:main
Choose a base branch
from
teocns:core-major-refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+966
−317
Open
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b7ddf3a
refactor(config.py): simplify Configuration class by using dataclass
teocns 70c229a
feat(config.py): add graceful_shutdown_wait_time attribute to Configu…
teocns d70d6fe
refactor(session): restructure session management and threading
teocns e1c9b91
chore(pyproject): add "-s" to pytest addopts
teocns 28709a0
fixup! refactor(session): restructure session management and threading
teocns e991472
Remove EventsCounter
teocns eb15582
feat(session): add debug logging for session events and state
teocns 85b18dd
fix construction (use dataclass); adjust initialization order
teocns 7078637
refactor(session): change SessionStruct to SessionDict
teocns 3831e1e
privatize locks attr
teocns eaad74b
clean
teocns d36440f
Assign `config` as object-level member
teocns ff399f9
streamline setting of `jwt` in SessionApi - reflect Session's
teocns f5d53ea
Correct SessionApi initialization
teocns 2044f73
refactor(session): update batch method to use api.batch()
teocns df3fd74
style(session.py): format code and import statements
teocns a350785
Fix event_type accessor
teocns 009eddd
Remove exc suppression
teocns b43a45f
improve SessionDict init
teocns 4218243
fix Sesssion self access
teocns 066715d
misc queue fixes
teocns 266347e
clean imports
teocns 1c71408
feat(session): add active property to Session class
teocns fed537b
refactor(session): streamline type hints and properties
teocns 47a934f
refactor(session): streamline exception handling logic
teocns 8838b52
make the threads stop properly when requested and prevent the infinit…
teocns c91f52b
misc
teocns 6bdf714
refactor(session): remove deprecated active property and clean up
teocns 7aca074
improve logging
teocns 00c2319
feat(session): add active property for backward compatibility
teocns 1f00391
refactor(session): clean up end_session and stop_session logic
teocns ce45bc9
weakset, reference tracking, etc
teocns 561dd2a
SessionsCollection: add custom iterator for sorted sessions
teocns c73e93e
introduce SessionsCollection
teocns a7e527f
refactor(client): convert active_sessions to a list
teocns bfca31a
fixes test_session.TestSingleSessions
teocns File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,74 +1,53 @@ | ||
from typing import List, Optional | ||
from dataclasses import dataclass, field | ||
from typing import Optional, Set | ||
from uuid import UUID | ||
|
||
from .log_config import logger | ||
|
||
# TODO: Use annotations to clarify the purpose of each attribute. | ||
# Details are defined in a docstrings found in __init__.py, but | ||
# it's good to have those right on the fields at class definition | ||
|
||
@dataclass | ||
class Configuration: | ||
def __init__(self): | ||
self.api_key: Optional[str] = None | ||
self.parent_key: Optional[str] = None | ||
self.endpoint: str = "https://api.agentops.ai" | ||
self.max_wait_time: int = 5000 | ||
self.max_queue_size: int = 512 | ||
self.default_tags: set[str] = set() | ||
self.instrument_llm_calls: bool = True | ||
self.auto_start_session: bool = True | ||
self.skip_auto_end_session: bool = False | ||
self.env_data_opt_out: bool = False | ||
api_key: Optional[str] = None | ||
parent_key: Optional[str] = None | ||
endpoint: str = "https://api.agentops.ai" | ||
max_wait_time: int = 5000 | ||
max_queue_size: int = 512 | ||
graceful_shutdown_wait_time: int = 2000 | ||
default_tags: Set[str] = field(default_factory=set) | ||
instrument_llm_calls: bool = True | ||
auto_start_session: bool = True | ||
skip_auto_end_session: bool = False | ||
env_data_opt_out: bool = False | ||
|
||
def configure( | ||
self, | ||
client, | ||
api_key: Optional[str] = None, | ||
parent_key: Optional[str] = None, | ||
endpoint: Optional[str] = None, | ||
max_wait_time: Optional[int] = None, | ||
max_queue_size: Optional[int] = None, | ||
default_tags: Optional[List[str]] = None, | ||
instrument_llm_calls: Optional[bool] = None, | ||
auto_start_session: Optional[bool] = None, | ||
skip_auto_end_session: Optional[bool] = None, | ||
env_data_opt_out: Optional[bool] = None, | ||
**kwargs | ||
): | ||
if api_key is not None: | ||
try: | ||
UUID(api_key) | ||
self.api_key = api_key | ||
except ValueError: | ||
message = f"API Key is invalid: {{{api_key}}}.\n\t Find your API key at https://app.agentops.ai/settings/projects" | ||
client.add_pre_init_warning(message) | ||
logger.error(message) | ||
|
||
if parent_key is not None: | ||
try: | ||
UUID(parent_key) | ||
self.parent_key = parent_key | ||
except ValueError: | ||
message = f"Parent Key is invalid: {parent_key}" | ||
client.add_pre_init_warning(message) | ||
logger.warning(message) | ||
|
||
if endpoint is not None: | ||
self.endpoint = endpoint | ||
|
||
if max_wait_time is not None: | ||
self.max_wait_time = max_wait_time | ||
|
||
if max_queue_size is not None: | ||
self.max_queue_size = max_queue_size | ||
|
||
if default_tags is not None: | ||
self.default_tags.update(default_tags) | ||
|
||
if instrument_llm_calls is not None: | ||
self.instrument_llm_calls = instrument_llm_calls | ||
|
||
if auto_start_session is not None: | ||
self.auto_start_session = auto_start_session | ||
|
||
if skip_auto_end_session is not None: | ||
self.skip_auto_end_session = skip_auto_end_session | ||
|
||
if env_data_opt_out is not None: | ||
self.env_data_opt_out = env_data_opt_out | ||
# Special handling for keys that need UUID validation | ||
for key_name in ['api_key', 'parent_key']: | ||
if key_name in kwargs and kwargs[key_name] is not None: | ||
try: | ||
UUID(kwargs[key_name]) | ||
setattr(self, key_name, kwargs[key_name]) | ||
except ValueError: | ||
message = ( | ||
f"API Key is invalid: {{{kwargs[key_name]}}}.\n\t Find your API key at https://app.agentops.ai/settings/projects" | ||
if key_name == 'api_key' | ||
else f"Parent Key is invalid: {kwargs[key_name]}" | ||
) | ||
client.add_pre_init_warning(message) | ||
logger.error(message) if key_name == 'api_key' else logger.warning(message) | ||
kwargs.pop(key_name) | ||
|
||
# Special handling for default_tags which needs update() instead of assignment | ||
if 'default_tags' in kwargs and kwargs['default_tags'] is not None: | ||
self.default_tags.update(kwargs.pop('default_tags')) | ||
|
||
# Handle all other attributes | ||
for key, value in kwargs.items(): | ||
if value is not None and hasattr(self, key): | ||
setattr(self, key, value) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Performance Improvement
Ensure Explicit List Conversion for Robustness
The change from
active_sessions
tolist(active_sessions)
ensures that_sessions
is explicitly a list, which is crucial ifactive_sessions
is a mutable sequence like a set or another iterable. This prevents unintended side effects from external modifications toactive_sessions
, enhancing the robustness of session management.Commitable Code Suggestion: