Skip to content

Commit 090cfa9

Browse files
Add AgentSettingsService with template and instance management
Co-authored-by: sergioescalera <8428450+sergioescalera@users.noreply.github.com>
1 parent 24bdb54 commit 090cfa9

5 files changed

Lines changed: 674 additions & 1 deletion

File tree

libraries/microsoft-agents-a365-runtime/README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,58 @@ pip install microsoft-agents-a365-runtime
1313

1414
## Usage
1515

16-
For usage examples and detailed documentation, see the [Microsoft Agent 365 Developer documentation](https://learn.microsoft.com/microsoft-agent-365/developer/?tabs=python) on Microsoft Learn.
16+
### Agent Settings Service
17+
18+
The Agent Settings Service provides methods to manage agent settings templates and instance-specific settings:
19+
20+
```python
21+
import asyncio
22+
from microsoft_agents_a365.runtime import (
23+
AgentSettingsService,
24+
AgentSettingTemplate,
25+
AgentSettings,
26+
PowerPlatformApiDiscovery,
27+
)
28+
29+
# Initialize the service
30+
api_discovery = PowerPlatformApiDiscovery("prod")
31+
tenant_id = "your-tenant-id"
32+
service = AgentSettingsService(api_discovery, tenant_id)
33+
34+
async def main():
35+
access_token = "your-access-token"
36+
37+
# Get agent setting template by agent type
38+
template = await service.get_agent_setting_template(
39+
"my-agent-type",
40+
access_token
41+
)
42+
43+
# Set agent setting template
44+
new_template = AgentSettingTemplate(
45+
agent_type="my-agent-type",
46+
settings={"key1": "value1", "key2": "value2"}
47+
)
48+
await service.set_agent_setting_template(new_template, access_token)
49+
50+
# Get agent settings by instance
51+
settings = await service.get_agent_settings(
52+
"agent-instance-id",
53+
access_token
54+
)
55+
56+
# Set agent settings by instance
57+
new_settings = AgentSettings(
58+
agent_instance_id="agent-instance-id",
59+
agent_type="my-agent-type",
60+
settings={"instanceKey": "instanceValue"}
61+
)
62+
await service.set_agent_settings(new_settings, access_token)
63+
64+
asyncio.run(main())
65+
```
66+
67+
For more usage examples and detailed documentation, see the [Microsoft Agent 365 Developer documentation](https://learn.microsoft.com/microsoft-agent-365/developer/?tabs=python) on Microsoft Learn.
1768

1869
## Support
1970

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3+
from .agent_settings_service import (
4+
AgentSettings,
5+
AgentSettingsService,
6+
AgentSettingTemplate,
7+
)
38
from .environment_utils import get_observability_authentication_scope
49
from .power_platform_api_discovery import ClusterCategory, PowerPlatformApiDiscovery
510
from .utility import Utility
611

712
__all__ = [
13+
"AgentSettings",
14+
"AgentSettingsService",
15+
"AgentSettingTemplate",
816
"get_observability_authentication_scope",
917
"PowerPlatformApiDiscovery",
1018
"ClusterCategory",
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
"""Service for managing agent settings templates and instance-specific settings."""
4+
5+
from dataclasses import dataclass
6+
from typing import Any
7+
from urllib.parse import quote
8+
9+
import httpx
10+
11+
from .power_platform_api_discovery import PowerPlatformApiDiscovery
12+
13+
14+
@dataclass
15+
class AgentSettingTemplate:
16+
"""Represents an agent setting template.
17+
18+
Attributes:
19+
agent_type: The agent type identifier.
20+
settings: The settings template as a key-value dictionary.
21+
metadata: Optional metadata about the template.
22+
"""
23+
24+
agent_type: str
25+
settings: dict[str, Any]
26+
metadata: dict[str, Any] | None = None
27+
28+
29+
@dataclass
30+
class AgentSettings:
31+
"""Represents agent settings for a specific instance.
32+
33+
Attributes:
34+
agent_instance_id: The agent instance identifier.
35+
agent_type: The agent type identifier.
36+
settings: The settings as a key-value dictionary.
37+
metadata: Optional metadata about the settings.
38+
"""
39+
40+
agent_instance_id: str
41+
agent_type: str
42+
settings: dict[str, Any]
43+
metadata: dict[str, Any] | None = None
44+
45+
46+
class AgentSettingsService:
47+
"""Service for managing agent settings templates and instance-specific settings."""
48+
49+
def __init__(self, api_discovery: PowerPlatformApiDiscovery, tenant_id: str) -> None:
50+
"""Creates a new instance of AgentSettingsService.
51+
52+
Args:
53+
api_discovery: The Power Platform API discovery service.
54+
tenant_id: The tenant identifier.
55+
"""
56+
self.api_discovery = api_discovery
57+
self.tenant_id = tenant_id
58+
59+
def _get_base_endpoint(self) -> str:
60+
"""Gets the base endpoint for agent settings API.
61+
62+
Returns:
63+
The base endpoint URL.
64+
"""
65+
tenant_endpoint = self.api_discovery.get_tenant_endpoint(self.tenant_id)
66+
return f"https://{tenant_endpoint}/agents/v1.0"
67+
68+
def get_agent_setting_template_endpoint(self, agent_type: str) -> str:
69+
"""Gets the endpoint for agent setting templates.
70+
71+
Args:
72+
agent_type: The agent type identifier.
73+
74+
Returns:
75+
The endpoint URL for the agent type template.
76+
"""
77+
return f"{self._get_base_endpoint()}/settings/templates/{quote(agent_type, safe='')}"
78+
79+
def get_agent_settings_endpoint(self, agent_instance_id: str) -> str:
80+
"""Gets the endpoint for agent instance settings.
81+
82+
Args:
83+
agent_instance_id: The agent instance identifier.
84+
85+
Returns:
86+
The endpoint URL for the agent instance settings.
87+
"""
88+
return f"{self._get_base_endpoint()}/settings/instances/{quote(agent_instance_id, safe='')}"
89+
90+
async def get_agent_setting_template(
91+
self, agent_type: str, access_token: str
92+
) -> AgentSettingTemplate:
93+
"""Retrieves an agent setting template by agent type.
94+
95+
Args:
96+
agent_type: The agent type identifier.
97+
access_token: The access token for authentication.
98+
99+
Returns:
100+
The agent setting template.
101+
102+
Raises:
103+
httpx.HTTPStatusError: If the API request fails.
104+
"""
105+
endpoint = self.get_agent_setting_template_endpoint(agent_type)
106+
107+
async with httpx.AsyncClient() as client:
108+
response = await client.get(
109+
endpoint,
110+
headers={
111+
"Authorization": f"Bearer {access_token}",
112+
"Content-Type": "application/json",
113+
},
114+
)
115+
116+
if not response.is_success:
117+
raise httpx.HTTPStatusError(
118+
f"Failed to get agent setting template for type '{agent_type}': "
119+
f"{response.status_code} {response.reason_phrase}",
120+
request=response.request,
121+
response=response,
122+
)
123+
124+
data = response.json()
125+
return AgentSettingTemplate(
126+
agent_type=data["agentType"],
127+
settings=data["settings"],
128+
metadata=data.get("metadata"),
129+
)
130+
131+
async def set_agent_setting_template(
132+
self, template: AgentSettingTemplate, access_token: str
133+
) -> AgentSettingTemplate:
134+
"""Sets an agent setting template for a specific agent type.
135+
136+
Args:
137+
template: The agent setting template to set.
138+
access_token: The access token for authentication.
139+
140+
Returns:
141+
The updated agent setting template.
142+
143+
Raises:
144+
httpx.HTTPStatusError: If the API request fails.
145+
"""
146+
endpoint = self.get_agent_setting_template_endpoint(template.agent_type)
147+
148+
payload = {
149+
"agentType": template.agent_type,
150+
"settings": template.settings,
151+
}
152+
if template.metadata is not None:
153+
payload["metadata"] = template.metadata
154+
155+
async with httpx.AsyncClient() as client:
156+
response = await client.put(
157+
endpoint,
158+
headers={
159+
"Authorization": f"Bearer {access_token}",
160+
"Content-Type": "application/json",
161+
},
162+
json=payload,
163+
)
164+
165+
if not response.is_success:
166+
raise httpx.HTTPStatusError(
167+
f"Failed to set agent setting template for type '{template.agent_type}': "
168+
f"{response.status_code} {response.reason_phrase}",
169+
request=response.request,
170+
response=response,
171+
)
172+
173+
data = response.json()
174+
return AgentSettingTemplate(
175+
agent_type=data["agentType"],
176+
settings=data["settings"],
177+
metadata=data.get("metadata"),
178+
)
179+
180+
async def get_agent_settings(
181+
self, agent_instance_id: str, access_token: str
182+
) -> AgentSettings:
183+
"""Retrieves agent settings for a specific agent instance.
184+
185+
Args:
186+
agent_instance_id: The agent instance identifier.
187+
access_token: The access token for authentication.
188+
189+
Returns:
190+
The agent settings.
191+
192+
Raises:
193+
httpx.HTTPStatusError: If the API request fails.
194+
"""
195+
endpoint = self.get_agent_settings_endpoint(agent_instance_id)
196+
197+
async with httpx.AsyncClient() as client:
198+
response = await client.get(
199+
endpoint,
200+
headers={
201+
"Authorization": f"Bearer {access_token}",
202+
"Content-Type": "application/json",
203+
},
204+
)
205+
206+
if not response.is_success:
207+
raise httpx.HTTPStatusError(
208+
f"Failed to get agent settings for instance '{agent_instance_id}': "
209+
f"{response.status_code} {response.reason_phrase}",
210+
request=response.request,
211+
response=response,
212+
)
213+
214+
data = response.json()
215+
return AgentSettings(
216+
agent_instance_id=data["agentInstanceId"],
217+
agent_type=data["agentType"],
218+
settings=data["settings"],
219+
metadata=data.get("metadata"),
220+
)
221+
222+
async def set_agent_settings(
223+
self, settings: AgentSettings, access_token: str
224+
) -> AgentSettings:
225+
"""Sets agent settings for a specific agent instance.
226+
227+
Args:
228+
settings: The agent settings to set.
229+
access_token: The access token for authentication.
230+
231+
Returns:
232+
The updated agent settings.
233+
234+
Raises:
235+
httpx.HTTPStatusError: If the API request fails.
236+
"""
237+
endpoint = self.get_agent_settings_endpoint(settings.agent_instance_id)
238+
239+
payload = {
240+
"agentInstanceId": settings.agent_instance_id,
241+
"agentType": settings.agent_type,
242+
"settings": settings.settings,
243+
}
244+
if settings.metadata is not None:
245+
payload["metadata"] = settings.metadata
246+
247+
async with httpx.AsyncClient() as client:
248+
response = await client.put(
249+
endpoint,
250+
headers={
251+
"Authorization": f"Bearer {access_token}",
252+
"Content-Type": "application/json",
253+
},
254+
json=payload,
255+
)
256+
257+
if not response.is_success:
258+
raise httpx.HTTPStatusError(
259+
f"Failed to set agent settings for instance '{settings.agent_instance_id}': "
260+
f"{response.status_code} {response.reason_phrase}",
261+
request=response.request,
262+
response=response,
263+
)
264+
265+
data = response.json()
266+
return AgentSettings(
267+
agent_instance_id=data["agentInstanceId"],
268+
agent_type=data["agentType"],
269+
settings=data["settings"],
270+
metadata=data.get("metadata"),
271+
)

libraries/microsoft-agents-a365-runtime/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ license = {text = "MIT"}
2626
keywords = ["observability", "telemetry", "tracing", "opentelemetry", "monitoring", "ai", "agents"]
2727
dependencies = [
2828
"PyJWT >= 2.8.0",
29+
"httpx >= 0.27.0",
2930
]
3031

3132
[project.urls]

0 commit comments

Comments
 (0)