|
| 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 | + ) |
0 commit comments