Merge pull request 'CUB-54: Implement Agent State Repository (EF Core)' (#23) from agent/dex/CUB-54-agent-state-repository into dev

Reviewed-on: #23
This commit was merged in pull request #23.
This commit is contained in:
2026-04-27 17:42:57 -04:00
5 changed files with 135 additions and 3 deletions

View File

@@ -6,13 +6,19 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<!-- Exclude the separate ControlCenter sub-project from this project's compilation -->
<ItemGroup>
<Compile Remove="ControlCenter/**/*.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,19 @@
namespace ControlCenter.Api.Models;
/// <summary>
/// Read-only model representing an agent's current state.
/// Used as the return type from the Agent State Repository
/// to decouple consumers from the persistence layer.
/// </summary>
public class AgentState
{
public Guid Id { get; set; }
public string Status { get; set; } = string.Empty;
public string? Task { get; set; }
public int? Progress { get; set; }
public string SessionKey { get; set; } = string.Empty;
public string Channel { get; set; } = string.Empty;
public DateTime LastActivity { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}

View File

@@ -1,5 +1,6 @@
using ControlCenter.Api.Data;
using ControlCenter.Api.Hubs;
using ControlCenter.Api.Repositories;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
@@ -10,6 +11,9 @@ builder.Services.AddOpenApi();
// Register SignalR for real-time agent status updates
builder.Services.AddSignalR();
// Register Agent State Repository
builder.Services.AddScoped<IAgentStateRepository, AgentStateRepository>();
// Register DbContext with PostgreSQL
builder.Services.AddDbContext<AppDbContext>(options =>
{

View File

@@ -0,0 +1,76 @@
using ControlCenter.Api.Data;
using ControlCenter.Api.Entities;
using ControlCenter.Api.Models;
using Microsoft.EntityFrameworkCore;
namespace ControlCenter.Api.Repositories;
/// <summary>
/// EF Core implementation of the Agent State Repository.
/// Maps between the persisted <see cref="Agent"/> entity and the
/// read-oriented <see cref="AgentState"/> model.
/// </summary>
public class AgentStateRepository : IAgentStateRepository
{
private readonly AppDbContext _db;
public AgentStateRepository(AppDbContext db)
{
_db = db;
}
/// <inheritdoc />
public async Task<IReadOnlyList<AgentState>> GetAllAsync(CancellationToken ct = default)
{
var agents = await _db.Agents
.AsNoTracking()
.OrderByDescending(a => a.LastActivity)
.ToListAsync(ct);
return agents.Select(ToModel).ToList();
}
/// <inheritdoc />
public async Task<AgentState?> GetBySessionKeyAsync(string sessionKey, CancellationToken ct = default)
{
var agent = await _db.Agents
.AsNoTracking()
.FirstOrDefaultAsync(a => a.SessionKey == sessionKey, ct);
return agent is null ? null : ToModel(agent);
}
/// <inheritdoc />
public async Task<bool> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default)
{
if (!Enum.TryParse<AgentStatus>(status, ignoreCase: true, out var parsedStatus))
return false;
var agent = await _db.Agents.FindAsync([id], ct);
if (agent is null)
return false;
agent.Status = parsedStatus;
agent.UpdatedAt = DateTime.UtcNow;
agent.LastActivity = DateTime.UtcNow;
await _db.SaveChangesAsync(ct);
return true;
}
/// <summary>
/// Maps a persisted <see cref="Agent"/> entity to a <see cref="AgentState"/> model.
/// </summary>
private static AgentState ToModel(Agent agent) => new()
{
Id = agent.Id,
Status = agent.Status.ToString(),
Task = agent.Task,
Progress = agent.Progress,
SessionKey = agent.SessionKey,
Channel = agent.Channel,
LastActivity = agent.LastActivity,
CreatedAt = agent.CreatedAt,
UpdatedAt = agent.UpdatedAt,
};
}

View File

@@ -0,0 +1,27 @@
using ControlCenter.Api.Models;
namespace ControlCenter.Api.Repositories;
/// <summary>
/// Repository interface for accessing and mutating Agent State.
/// Provides a clean abstraction over the EF Core data access layer.
/// </summary>
public interface IAgentStateRepository
{
/// <summary>
/// Retrieve all agent states.
/// </summary>
Task<IReadOnlyList<AgentState>> GetAllAsync(CancellationToken ct = default);
/// <summary>
/// Retrieve a single agent state by its session key.
/// Returns null if no agent is found with the given session key.
/// </summary>
Task<AgentState?> GetBySessionKeyAsync(string sessionKey, CancellationToken ct = default);
/// <summary>
/// Update the status of an agent by its primary key.
/// Returns true if the agent was found and updated, false otherwise.
/// </summary>
Task<bool> UpdateStatusAsync(Guid id, string status, CancellationToken ct = default);
}