|
1 | 1 | using AIAgent.API.Agents; |
2 | | -using AIAgent.API.KernelTools; |
3 | 2 | using AIAgent.API.Models; |
4 | | -using Azure.AI.Agents.Persistent; |
5 | | -using Azure.Identity; |
| 3 | +using AIAgent.API.Services; |
6 | 4 | using Microsoft.AspNetCore.Mvc; |
7 | | -using Microsoft.SemanticKernel; |
8 | | -using Microsoft.SemanticKernel.Agents.AzureAI; |
9 | 5 |
|
10 | 6 | namespace AIAgent.API.Controllers |
11 | 7 | { |
12 | 8 | [ApiController] |
13 | 9 | [Route("api")] |
14 | 10 | public class ChatController : ControllerBase |
15 | 11 | { |
16 | | - private readonly PersistentAgentsClient _projectClient; |
17 | | - private TechSupportAgentConfig _techSupportAgentConfig = new TechSupportAgentConfig(); |
| 12 | + private readonly IAzureAIAgentService _azureAIAgentService; |
| 13 | + private readonly TechSupportAgentConfig _techSupportAgentConfig = new(); |
| 14 | + private readonly ILogger<ChatController> _logger; |
18 | 15 |
|
19 | | - public ChatController() |
| 16 | + public ChatController(IAzureAIAgentService azureAIAgentService, ILogger<ChatController> logger) |
20 | 17 | { |
21 | | - var projectEndpoint = Config.AZURE_AI_PROJECT_ENDPOINT; |
22 | | - _projectClient = AzureAIAgent.CreateAgentsClient(projectEndpoint, new DefaultAzureCredential()); |
| 18 | + _azureAIAgentService = azureAIAgentService; |
| 19 | + _logger = logger; |
23 | 20 | } |
24 | 21 |
|
| 22 | + /// <summary> |
| 23 | + /// Gets information about the chat agent. |
| 24 | + /// </summary> |
25 | 25 | // GET /agent |
26 | 26 | [HttpGet("agent")] |
27 | 27 | public IActionResult GetChatAgent() |
28 | 28 | { |
| 29 | + _logger.LogInformation("Fetching chat agent info."); |
29 | 30 | var agentInfo = new |
30 | 31 | { |
31 | 32 | Name = _techSupportAgentConfig.AgentName, |
32 | 33 | DisplayName = _techSupportAgentConfig.GetAgentDisplayName(), |
33 | | - Description = _techSupportAgentConfig.GetAgentDescription(), |
| 34 | + Description = _techSupportAgentConfig.GetDescription(), |
34 | 35 | }; |
35 | 36 | return Ok(agentInfo); |
36 | 37 | } |
37 | 38 |
|
| 39 | + /// <summary> |
| 40 | + /// Gets the chat message history for a given thread. |
| 41 | + /// </summary> |
38 | 42 | // GET /chat/history |
39 | 43 | [HttpGet("chat/history")] |
40 | 44 | public async Task<IActionResult> History([FromQuery] string? agentId, [FromQuery] string? threadId) |
41 | 45 | { |
42 | | - if (string.IsNullOrEmpty(threadId)) |
43 | | - return Ok(new List<ChatMessageHistory>()); // No history for this thread |
44 | | - |
45 | | - var messages = await ChatUtils.GetChatMessageHistoryAsync(threadId, _projectClient); |
46 | | - return Ok(messages); |
| 46 | + _logger.LogInformation("Fetching chat history for threadId: {ThreadId}", threadId); |
| 47 | + try |
| 48 | + { |
| 49 | + var messages = await _azureAIAgentService.GetChatMessageHistoryAsync(threadId).ConfigureAwait(false); |
| 50 | + return Ok(messages); |
| 51 | + } |
| 52 | + catch (Exception ex) |
| 53 | + { |
| 54 | + _logger.LogError(ex, "Error fetching chat history for threadId: {ThreadId}", threadId); |
| 55 | + return StatusCode(500, "An error occurred while fetching chat history."); |
| 56 | + } |
47 | 57 | } |
48 | 58 |
|
| 59 | + /// <summary> |
| 60 | + /// Sends a chat message and returns the agent thread id. |
| 61 | + /// </summary> |
49 | 62 | [HttpPost("chat/send")] |
50 | 63 | public async Task<IActionResult> ChatSend([FromBody] ChatRequest request) |
51 | 64 | { |
52 | | - var agent = await GetOrCreateAgentAsync(request.AgentId); |
53 | | - var agentThread = await ChatUtils.GetOrCreateAgentThreadAsync(request.ThreadId, _projectClient); |
54 | | - await ChatUtils.InvokeAgent(request.Message, agent, agentThread); |
55 | | - var response = new ChatCompletionResponse |
| 65 | + _logger.LogInformation("Sending chat message for agentId: {AgentId}, threadId: {ThreadId}", request.AgentId, request.ThreadId); |
| 66 | + try |
56 | 67 | { |
57 | | - AgentId = agent.Id, |
58 | | - ThreadId = agentThread.Id ?? string.Empty |
59 | | - }; |
60 | | - |
61 | | - return Ok(response); |
62 | | - } |
| 68 | + var result = await _azureAIAgentService.SendMessage |
| 69 | + (request.Message, request.AgentName, request.AgentId, request.ThreadId); |
63 | 70 |
|
64 | | - private async Task<AzureAIAgent> GetOrCreateAgentAsync(string? agentId) |
65 | | - { |
66 | | - PersistentAgent agentDefinition = null; |
67 | | - var modelId = Config.AZURE_OPENAI_DEPLOYMENT_NAME; |
68 | | - var agentInstructions = _techSupportAgentConfig.GetAgentSystemMessage(); |
69 | | - var agentName = _techSupportAgentConfig.AgentName; |
70 | | - |
71 | | - if (!string.IsNullOrEmpty(agentId)) |
72 | | - { |
73 | | - try |
| 71 | + var response = new ChatCompletionResponse |
74 | 72 | { |
75 | | - agentDefinition = await _projectClient.Administration.GetAgentAsync(agentId); |
76 | | - } |
77 | | - catch (Azure.RequestFailedException) |
78 | | - { |
79 | | - // Agent not found, will create a new one |
80 | | - } |
81 | | - } |
| 73 | + AgentId = result.AgentId, |
| 74 | + ThreadId = result.ThreadId |
| 75 | + }; |
82 | 76 |
|
83 | | - if (agentDefinition == null) |
84 | | - { |
85 | | - await foreach (var agentDefn in _projectClient.Administration.GetAgentsAsync()) |
86 | | - { |
87 | | - if (agentDefn.Name == agentName) |
88 | | - agentDefinition = agentDefn; |
89 | | - } |
| 77 | + _logger.LogInformation("Message sent successfully. agentId: {AgentId}, threadId: {ThreadId}", result.AgentId, result.ThreadId); |
| 78 | + return Ok(response); |
90 | 79 | } |
91 | | - |
92 | | - if (agentDefinition == null) |
| 80 | + catch (Exception ex) |
93 | 81 | { |
94 | | - agentDefinition = await _projectClient.Administration.CreateAgentAsync( |
95 | | - modelId, |
96 | | - name: agentName, |
97 | | - instructions: agentInstructions, |
98 | | - tools: [], |
99 | | - toolResources: null); |
| 82 | + _logger.LogError(ex, "Error sending chat message for agentId: {AgentId}, threadId: {ThreadId}", request.AgentId, request.ThreadId); |
| 83 | + return StatusCode(500, "An error occurred while sending the chat message."); |
100 | 84 | } |
101 | | - |
102 | | - var agent = new AzureAIAgent(agentDefinition, _projectClient); |
103 | | - agent.Kernel.ImportPluginFromObject(new TechSupportTools(), "TechSupportTools"); |
104 | | - return agent; |
105 | 85 | } |
106 | 86 | } |
107 | 87 | } |
0 commit comments