Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
"""

from __future__ import annotations
from importlib.metadata import version

import platform
import uuid
from typing import Any, Optional
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated

import jwt


class Utility:
_cached_version = None
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated

"""
Utility class providing common runtime operations for Agent 365.

Expand Down Expand Up @@ -80,3 +84,19 @@ def resolve_agent_identity(context: Any, auth_token: Optional[str]) -> str:

# Fallback to extracting App ID from the auth token
return Utility.get_app_id_from_token(auth_token)

@staticmethod
def get_user_agent_header(orchestrator: str = "") -> str:
Comment thread
JesuTerraz marked this conversation as resolved.
if Utility._cached_version is None:
try:
Utility._cached_version = version("microsoft-agents-a365-runtime")
print("Successfully retrieved version")
except Exception:
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated
Utility._cached_version = "unknown"
Comment thread
JesuTerraz marked this conversation as resolved.

print(f"Utility._cached_version: {Utility._cached_version}")
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated

orchestrator_part = f"; {orchestrator}" if orchestrator else ""
os_type = platform.system()
python_version = platform.python_version()
return f"Agent365SDK/{Utility._cached_version} ({os_type}; Python/{python_version}{orchestrator_part})"
19 changes: 19 additions & 0 deletions tests/runtime/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest
from microsoft_agents_a365.runtime.utility import Utility
Comment thread
JesuTerraz marked this conversation as resolved.

import platform

Comment thread
JesuTerraz marked this conversation as resolved.
# Fixtures (Mocks and Helpers)
@pytest.fixture
Expand Down Expand Up @@ -124,3 +125,21 @@ def test_resolve_agent_identity_exception_handling(create_test_jwt, mock_context

result = Utility.resolve_agent_identity(context, token)
assert result == "token-app-id"

def test_get_user_agent_header_default():
"""Test get_user_agent_header returns expected format with default orchestrator."""
# Patch version to a known value
Utility._cached_version = "1.2.3"
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated
result = Utility.get_user_agent_header()
os_type = platform.system()
py_version = platform.python_version()
assert result.startswith(f"Agent365SDK/1.2.3 ({os_type}; Python/{py_version}")
assert ";" not in result.split("Python/")[1] # No orchestrator
Comment thread
JesuTerraz marked this conversation as resolved.
Outdated

def test_get_user_agent_header_with_orchestrator():
"""Test get_user_agent_header includes orchestrator when provided."""
Utility._cached_version = "2.0.0"
orchestrator = "TestOrchestrator"
result = Utility.get_user_agent_header(orchestrator)
assert f"; {orchestrator}" in result
assert result.startswith("Agent365SDK/2.0.0 (")
Loading