Skip to content

Commit 1e8cd5f

Browse files
committed
update
1 parent 49c251e commit 1e8cd5f

11 files changed

Lines changed: 300 additions & 129 deletions

File tree

src/backend/ai-agent-api/Agents/AIAgentBase.cs renamed to src/backend/ai-agent-api/Agents/AgentConfigBase.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
namespace AIAgent.API.Agents
22
{
3-
public abstract class AIAgentConfigBase
3+
public interface IAgentConfig
4+
{
5+
string AgentName { get; }
6+
string GetAgentDisplayName();
7+
string GetSystemMessage();
8+
string GetDescription();
9+
}
10+
11+
public abstract class AgentConfigBase : IAgentConfig
412
{
513
private string _agentName;
614
private string _systemMessage;
715

8-
protected AIAgentConfigBase(
16+
protected AgentConfigBase(
917
string agentName)
1018
{
1119
_agentName = agentName;
@@ -26,7 +34,7 @@ public static string DefaultSystemMessage(string agentName = null)
2634
return $"You are an AI assistant named {agentName}. Help the user by providing accurate and helpful information.";
2735
}
2836

29-
public virtual string GetAgentSystemMessage()
37+
public virtual string GetSystemMessage()
3038
{
3139
if (string.IsNullOrEmpty(_systemMessage))
3240
{
@@ -35,7 +43,7 @@ public virtual string GetAgentSystemMessage()
3543
return _systemMessage;
3644
}
3745

38-
public virtual string GetAgentDescription()
46+
public virtual string GetDescription()
3947
{
4048
return $"This is a generic AI agent named {_agentName}. " +
4149
$"It is designed to assist users with various tasks and provide information based on the context of the conversation.";
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace AIAgent.API.Agents
2+
{
3+
public static class AgentFactory
4+
{
5+
public static IAgentConfig GetAgent(string agentName)
6+
{
7+
// For now, only TechSupportAgent is supported. Extend this as more agents are added.
8+
if (string.IsNullOrEmpty(agentName) || agentName == "TechSupportAgent")
9+
{
10+
return new TechSupportAgentConfig();
11+
}
12+
// Add more agent types here as needed.
13+
throw new ArgumentException($"Unknown agentId: {agentName}");
14+
}
15+
}
16+
}

src/backend/ai-agent-api/Agents/TechSupportAgentConfig.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace AIAgent.API.Agents
22
{
3-
public class TechSupportAgentConfig : AIAgentConfigBase
3+
public class TechSupportAgentConfig : AgentConfigBase
44
{
55
public TechSupportAgentConfig()
66
: base("TechSupportAgent")
@@ -12,14 +12,14 @@ public override string GetAgentDisplayName()
1212
return "Technical Support Agent";
1313
}
1414

15-
public override string GetAgentDescription()
15+
public override string GetDescription()
1616
{
1717
return "This agent provides IT and technical support for the company. " +
1818
"It assists users with troubleshooting, technical queries, and problem resolution. " +
1919
"The agent utilizes the `TechSupportTools` to effectively address and resolve technical issues.";
2020
}
2121

22-
public override string GetAgentSystemMessage()
22+
public override string GetSystemMessage()
2323
{
2424
return "You are a technical support agent. " +
2525
"Your role is to assist users with IT and technical issues, providing solutions and troubleshooting steps. " +
Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,87 @@
11
using AIAgent.API.Agents;
2-
using AIAgent.API.KernelTools;
32
using AIAgent.API.Models;
4-
using Azure.AI.Agents.Persistent;
5-
using Azure.Identity;
3+
using AIAgent.API.Services;
64
using Microsoft.AspNetCore.Mvc;
7-
using Microsoft.SemanticKernel;
8-
using Microsoft.SemanticKernel.Agents.AzureAI;
95

106
namespace AIAgent.API.Controllers
117
{
128
[ApiController]
139
[Route("api")]
1410
public class ChatController : ControllerBase
1511
{
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;
1815

19-
public ChatController()
16+
public ChatController(IAzureAIAgentService azureAIAgentService, ILogger<ChatController> logger)
2017
{
21-
var projectEndpoint = Config.AZURE_AI_PROJECT_ENDPOINT;
22-
_projectClient = AzureAIAgent.CreateAgentsClient(projectEndpoint, new DefaultAzureCredential());
18+
_azureAIAgentService = azureAIAgentService;
19+
_logger = logger;
2320
}
2421

22+
/// <summary>
23+
/// Gets information about the chat agent.
24+
/// </summary>
2525
// GET /agent
2626
[HttpGet("agent")]
2727
public IActionResult GetChatAgent()
2828
{
29+
_logger.LogInformation("Fetching chat agent info.");
2930
var agentInfo = new
3031
{
3132
Name = _techSupportAgentConfig.AgentName,
3233
DisplayName = _techSupportAgentConfig.GetAgentDisplayName(),
33-
Description = _techSupportAgentConfig.GetAgentDescription(),
34+
Description = _techSupportAgentConfig.GetDescription(),
3435
};
3536
return Ok(agentInfo);
3637
}
3738

39+
/// <summary>
40+
/// Gets the chat message history for a given thread.
41+
/// </summary>
3842
// GET /chat/history
3943
[HttpGet("chat/history")]
4044
public async Task<IActionResult> History([FromQuery] string? agentId, [FromQuery] string? threadId)
4145
{
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+
}
4757
}
4858

59+
/// <summary>
60+
/// Sends a chat message and returns the agent thread id.
61+
/// </summary>
4962
[HttpPost("chat/send")]
5063
public async Task<IActionResult> ChatSend([FromBody] ChatRequest request)
5164
{
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
5667
{
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);
6370

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
7472
{
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+
};
8276

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);
9079
}
91-
92-
if (agentDefinition == null)
80+
catch (Exception ex)
9381
{
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.");
10084
}
101-
102-
var agent = new AzureAIAgent(agentDefinition, _projectClient);
103-
agent.Kernel.ImportPluginFromObject(new TechSupportTools(), "TechSupportTools");
104-
return agent;
10585
}
10686
}
10787
}

src/backend/ai-agent-api/Controllers/ChatUtils.cs

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/backend/ai-agent-api/Models/ChatRequest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace AIAgent.API.Models
22
{
33
public class ChatRequest
44
{
5+
public string AgentName { get; set; }
56
public string? AgentId { get; set; }
67
public string? ThreadId { get; set; }
78
public string Message { get; set; }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AIAgent.API.Models
2+
{
3+
public class SendMessageResult
4+
{
5+
public string ThreadId { get; set; }
6+
public string AgentId { get; set; }
7+
}
8+
}

src/backend/ai-agent-api/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AIAgent.API;
2+
using AIAgent.API.Services;
23
using Microsoft.OpenApi.Models;
34

45
var builder = WebApplication.CreateBuilder(args);
@@ -14,6 +15,8 @@
1415
// Register AppConfig as a singleton
1516
builder.Services.AddSingleton<AppConfig>(sp => new AppConfig(sp.GetRequiredService<IConfiguration>()));
1617

18+
builder.Services.AddSingleton<IAzureAIAgentService, AzureAIAgentService>();
19+
1720
// Add CORS policy
1821
builder.Services.AddCors(options =>
1922
{

0 commit comments

Comments
 (0)