Skip to content

Commit 8b25d2a

Browse files
author
Jesus Terrazas
committed
Add User-Agent Header
1 parent 494a17a commit 8b25d2a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

libraries/microsoft-agents-a365-runtime/microsoft_agents_a365/runtime/utility.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@
88
"""
99

1010
from __future__ import annotations
11+
from importlib.metadata import version
1112

13+
import platform
1214
import uuid
1315
from typing import Any, Optional
1416

1517
import jwt
1618

1719

1820
class Utility:
21+
_cached_version = None
22+
1923
"""
2024
Utility class providing common runtime operations for Agent 365.
2125
@@ -80,3 +84,19 @@ def resolve_agent_identity(context: Any, auth_token: Optional[str]) -> str:
8084

8185
# Fallback to extracting App ID from the auth token
8286
return Utility.get_app_id_from_token(auth_token)
87+
88+
@staticmethod
89+
def get_user_agent_header(orchestrator: str = "") -> str:
90+
if Utility._cached_version is None:
91+
try:
92+
Utility._cached_version = version("microsoft-agents-a365-runtime")
93+
print("Successfully retrieved version")
94+
except Exception:
95+
Utility._cached_version = "unknown"
96+
97+
print(f"Utility._cached_version: {Utility._cached_version}")
98+
99+
orchestrator_part = f"; {orchestrator}" if orchestrator else ""
100+
os_type = platform.system()
101+
python_version = platform.python_version()
102+
return f"Agent365SDK/{Utility._cached_version} ({os_type}; Python/{python_version}{orchestrator_part})"

tests/runtime/test_utility.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
import jwt
1010
import pytest
11+
from pytest_mock import mocker
1112
from microsoft_agents_a365.runtime.utility import Utility
1213

14+
import platform
1315

1416
# Fixtures (Mocks and Helpers)
1517
@pytest.fixture
@@ -124,3 +126,21 @@ def test_resolve_agent_identity_exception_handling(create_test_jwt, mock_context
124126

125127
result = Utility.resolve_agent_identity(context, token)
126128
assert result == "token-app-id"
129+
130+
def test_get_user_agent_header_default():
131+
"""Test get_user_agent_header returns expected format with default orchestrator."""
132+
# Patch version to a known value
133+
Utility._cached_version = "1.2.3"
134+
result = Utility.get_user_agent_header()
135+
os_type = platform.system()
136+
py_version = platform.python_version()
137+
assert result.startswith(f"Agent365SDK/1.2.3 ({os_type}; Python/{py_version}")
138+
assert ";" not in result.split("Python/")[1] # No orchestrator
139+
140+
def test_get_user_agent_header_with_orchestrator():
141+
"""Test get_user_agent_header includes orchestrator when provided."""
142+
Utility._cached_version = "2.0.0"
143+
orchestrator = "TestOrchestrator"
144+
result = Utility.get_user_agent_header(orchestrator)
145+
assert f"; {orchestrator}" in result
146+
assert result.startswith("Agent365SDK/2.0.0 (")

0 commit comments

Comments
 (0)