-
Notifications
You must be signed in to change notification settings - Fork 5
Add Agent Settings Service with template and instance management #130
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { PowerPlatformApiDiscovery } from './power-platform-api-discovery'; | ||
|
|
||
| /** | ||
| * Represents an agent setting template. | ||
| */ | ||
| export interface AgentSettingTemplate { | ||
| /** | ||
| * The agent type identifier. | ||
| */ | ||
| agentType: string; | ||
|
|
||
| /** | ||
| * The settings template as a key-value dictionary. | ||
| */ | ||
| settings: Record<string, unknown>; | ||
|
|
||
| /** | ||
| * Optional metadata about the template. | ||
| */ | ||
| metadata?: Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
| * Represents agent settings for a specific instance. | ||
| */ | ||
| export interface AgentSettings { | ||
| /** | ||
| * The agent instance identifier. | ||
| */ | ||
| agentInstanceId: string; | ||
|
|
||
| /** | ||
| * The agent type identifier. | ||
| */ | ||
| agentType: string; | ||
|
|
||
| /** | ||
| * The settings as a key-value dictionary. | ||
| */ | ||
| settings: Record<string, unknown>; | ||
|
|
||
| /** | ||
| * Optional metadata about the settings. | ||
| */ | ||
| metadata?: Record<string, unknown>; | ||
| } | ||
|
|
||
| /** | ||
| * Service for managing agent settings templates and instance-specific settings. | ||
| */ | ||
| export class AgentSettingsService { | ||
| private readonly apiDiscovery: PowerPlatformApiDiscovery; | ||
| private readonly tenantId: string; | ||
|
|
||
| /** | ||
| * Creates a new instance of AgentSettingsService. | ||
| * @param apiDiscovery The Power Platform API discovery service. | ||
| * @param tenantId The tenant identifier. | ||
| */ | ||
| constructor(apiDiscovery: PowerPlatformApiDiscovery, tenantId: string) { | ||
| this.apiDiscovery = apiDiscovery; | ||
| this.tenantId = tenantId; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the base endpoint for agent settings API. | ||
| * @returns The base endpoint URL. | ||
| */ | ||
| private getBaseEndpoint(): string { | ||
| const tenantEndpoint = this.apiDiscovery.getTenantEndpoint(this.tenantId); | ||
| return `https://${tenantEndpoint}/agents/v1.0`; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the endpoint for agent setting templates. | ||
| * @param agentType The agent type identifier. | ||
| * @returns The endpoint URL for the agent type template. | ||
| */ | ||
| public getAgentSettingTemplateEndpoint(agentType: string): string { | ||
| return `${this.getBaseEndpoint()}/settings/templates/${encodeURIComponent(agentType)}`; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the endpoint for agent instance settings. | ||
| * @param agentInstanceId The agent instance identifier. | ||
| * @returns The endpoint URL for the agent instance settings. | ||
| */ | ||
| public getAgentSettingsEndpoint(agentInstanceId: string): string { | ||
| return `${this.getBaseEndpoint()}/settings/instances/${encodeURIComponent(agentInstanceId)}`; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves an agent setting template by agent type. | ||
| * @param agentType The agent type identifier. | ||
| * @param accessToken The access token for authentication. | ||
| * @returns A promise that resolves to the agent setting template. | ||
| */ | ||
| public async getAgentSettingTemplate( | ||
| agentType: string, | ||
| accessToken: string | ||
| ): Promise<AgentSettingTemplate> { | ||
| const endpoint = this.getAgentSettingTemplateEndpoint(agentType); | ||
|
|
||
| const response = await fetch(endpoint, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Authorization': `Bearer ${accessToken}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get agent setting template for type '${agentType}': ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
|
||
| /** | ||
| * Sets an agent setting template for a specific agent type. | ||
| * @param template The agent setting template to set. | ||
| * @param accessToken The access token for authentication. | ||
| * @returns A promise that resolves to the updated agent setting template. | ||
| */ | ||
| public async setAgentSettingTemplate( | ||
| template: AgentSettingTemplate, | ||
| accessToken: string | ||
| ): Promise<AgentSettingTemplate> { | ||
| const endpoint = this.getAgentSettingTemplateEndpoint(template.agentType); | ||
|
|
||
| const response = await fetch(endpoint, { | ||
| method: 'PUT', | ||
| headers: { | ||
| 'Authorization': `Bearer ${accessToken}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(template), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to set agent setting template for type '${template.agentType}': ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
Comment on lines
+136
to
+152
|
||
|
|
||
| /** | ||
| * Retrieves agent settings for a specific agent instance. | ||
| * @param agentInstanceId The agent instance identifier. | ||
| * @param accessToken The access token for authentication. | ||
| * @returns A promise that resolves to the agent settings. | ||
| */ | ||
| public async getAgentSettings( | ||
| agentInstanceId: string, | ||
| accessToken: string | ||
| ): Promise<AgentSettings> { | ||
| const endpoint = this.getAgentSettingsEndpoint(agentInstanceId); | ||
|
|
||
| const response = await fetch(endpoint, { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Authorization': `Bearer ${accessToken}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to get agent settings for instance '${agentInstanceId}': ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
Comment on lines
+166
to
+181
|
||
|
|
||
| /** | ||
| * Sets agent settings for a specific agent instance. | ||
| * @param settings The agent settings to set. | ||
| * @param accessToken The access token for authentication. | ||
| * @returns A promise that resolves to the updated agent settings. | ||
| */ | ||
| public async setAgentSettings( | ||
| settings: AgentSettings, | ||
| accessToken: string | ||
| ): Promise<AgentSettings> { | ||
| const endpoint = this.getAgentSettingsEndpoint(settings.agentInstanceId); | ||
|
|
||
| const response = await fetch(endpoint, { | ||
| method: 'PUT', | ||
| headers: { | ||
| 'Authorization': `Bearer ${accessToken}`, | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(settings), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to set agent settings for instance '${settings.agentInstanceId}': ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
|
|
||
| return await response.json(); | ||
| } | ||
|
Comment on lines
+195
to
+211
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export * from './power-platform-api-discovery'; | ||
| export * from './agentic-authorization-service'; | ||
| export * from './environment-utils'; | ||
| export * from './utility'; | ||
| export * from './utility'; | ||
| export * from './agent-settings-service'; |
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.
The fetch call lacks error handling for network failures and JSON parsing errors. If the network request fails (e.g., network timeout, DNS failure) or if the response body is not valid JSON, an unhandled exception will be thrown. Consider wrapping the fetch and json parsing in a try-catch block to provide more informative error messages to callers.