Fix update AgentName

This commit is contained in:
2025-09-24 11:35:40 +07:00
parent 68350e3c24
commit 44846a1817
6 changed files with 65 additions and 0 deletions

View File

@@ -17,11 +17,13 @@ namespace Managing.Application.Abstractions.Grains
/// Updates only the agent name without recalculating summary.
/// </summary>
/// <param name="agentName">The new agent name.</param>
[OneWay]
Task UpdateAgentNameAsync(string agentName);
/// <summary>
/// Generates a summary of the agent's stats for the AgentRegistryGrain.
/// </summary>
[OneWay]
Task UpdateSummary();
/// <summary>

View File

@@ -1,6 +1,8 @@
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Core;
using Managing.Domain.Statistics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Managing.Application.Agents;
@@ -9,17 +11,20 @@ public class AgentService : IAgentService
{
private readonly IAgentBalanceRepository _agentBalanceRepository;
private readonly IAgentSummaryRepository _agentSummaryRepository;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ICacheService _cacheService;
private readonly ILogger<AgentService> _logger;
public AgentService(
IAgentBalanceRepository agentBalanceRepository,
IAgentSummaryRepository agentSummaryRepository,
IServiceScopeFactory serviceScopeFactory,
ICacheService cacheService,
ILogger<AgentService> logger)
{
_agentBalanceRepository = agentBalanceRepository;
_agentSummaryRepository = agentSummaryRepository;
_serviceScopeFactory = serviceScopeFactory;
_cacheService = cacheService;
_logger = logger;
}
@@ -96,6 +101,11 @@ public class AgentService : IAgentService
{
try
{
if (string.IsNullOrEmpty(agentSummary.AgentName))
{
agentSummary.AgentName = await ServiceScopeHelpers.WithScopedService<IUserService, string>(_serviceScopeFactory,
async (userService) => (await userService.GetUserByIdAsync(agentSummary.UserId)).AgentName);
}
// Use the injected AgentSummaryRepository to save or update
await _agentSummaryRepository.SaveOrUpdateAsync(agentSummary);
@@ -120,4 +130,19 @@ public class AgentService : IAgentService
var agentSummaries = await _agentSummaryRepository.GetAllAgentWithRunningBots();
return agentSummaries.Select(a => a.AgentName);
}
public async Task UpdateAgentSummaryNameAsync(int userId, string agentName)
{
try
{
await _agentSummaryRepository.UpdateAgentNameAsync(userId, agentName);
_logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating agent name for user {UserId} to {AgentName}", userId, agentName);
throw;
}
}
}

View File

@@ -85,6 +85,9 @@ public class AgentGrain : Grain, IAgentGrain
{
_state.State.AgentName = agentName;
await _state.WriteStateAsync();
// Use the efficient method to update only the agent name in the summary
await _agentService.UpdateAgentSummaryNameAsync((int)this.GetPrimaryKeyLong(), agentName);
_logger.LogInformation("Agent {UserId} updated with name {AgentName}", this.GetPrimaryKeyLong(), agentName);
}