CUB-54: implement AgentState entity, repository, and DI registration
- Add AgentState entity mapping to 'agents' table (snake_case columns) - Add IAgentStateRepository interface (GetAllAsync, GetBySessionKeyAsync, UpdateStatusAsync) - Add AgentStateRepository with EF Core implementation - Add ControlCenterDbContext with ApplyConfigurationsFromAssembly - Add AgentStateConfiguration with snake_case column mappings and indexes - Register DbContext (Npgsql) and repository in Program.cs DI - Add ConnectionStrings to appsettings.json - Add EF Core 9.0.7 and Npgsql.EntityFrameworkCore.PostgreSQL 9.0.4 packages
This commit is contained in:
62
backend/ControlCenter/Repositories/AgentStateRepository.cs
Normal file
62
backend/ControlCenter/Repositories/AgentStateRepository.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using ControlCenter.Data;
|
||||
using ControlCenter.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ControlCenter.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// EF Core implementation of <see cref="IAgentStateRepository"/>.
|
||||
///
|
||||
/// <para>Queries the <c>agents</c> table via <see cref="ControlCenterDbContext"/>
|
||||
/// using snake_case column mappings defined in the <see cref="Data.Configurations.AgentStateConfiguration"/>.
|
||||
/// All write operations call <c>SaveChangesAsync</c> immediately.</para>
|
||||
/// </summary>
|
||||
public class AgentStateRepository : IAgentStateRepository
|
||||
{
|
||||
private readonly ControlCenterDbContext _context;
|
||||
private readonly ILogger<AgentStateRepository> _logger;
|
||||
|
||||
public AgentStateRepository(
|
||||
ControlCenterDbContext context,
|
||||
ILogger<AgentStateRepository> logger)
|
||||
{
|
||||
_context = context;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IEnumerable<AgentState>> GetAllAsync()
|
||||
{
|
||||
_logger.LogDebug("Fetching all agent states");
|
||||
return await _context.AgentStates.ToListAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<AgentState?> GetBySessionKeyAsync(string sessionKey)
|
||||
{
|
||||
_logger.LogDebug("Looking up agent state by session key: {SessionKey}", sessionKey);
|
||||
return await _context.AgentStates
|
||||
.FirstOrDefaultAsync(a => a.SessionKey == sessionKey);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<AgentState?> UpdateStatusAsync(Guid id, string status)
|
||||
{
|
||||
_logger.LogDebug("Updating agent status: {Id} → {Status}", id, status);
|
||||
|
||||
var entity = await _context.AgentStates.FindAsync(id);
|
||||
if (entity is null)
|
||||
{
|
||||
_logger.LogWarning("Agent state not found: {Id}", id);
|
||||
return null;
|
||||
}
|
||||
|
||||
entity.Status = status;
|
||||
entity.LastActivity = DateTime.UtcNow;
|
||||
|
||||
_context.AgentStates.Update(entity);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
38
backend/ControlCenter/Repositories/IAgentStateRepository.cs
Normal file
38
backend/ControlCenter/Repositories/IAgentStateRepository.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using ControlCenter.Models;
|
||||
|
||||
namespace ControlCenter.Repositories;
|
||||
|
||||
/// <summary>
|
||||
/// Repository interface for querying and updating agent state.
|
||||
///
|
||||
/// <para>Provides the data-access contract used by controllers,
|
||||
/// background services, and SignalR hubs to read and mutate
|
||||
/// persistent agent state.</para>
|
||||
///
|
||||
/// <para>Implementation should use <see cref="Data.ControlCenterDbContext"/>
|
||||
/// via EF Core with PostgreSQL (snake_case columns).</para>
|
||||
/// </summary>
|
||||
public interface IAgentStateRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns all agent states from the database.
|
||||
/// </summary>
|
||||
/// <returns>A collection of all <see cref="AgentState"/> records.</returns>
|
||||
Task<IEnumerable<AgentState>> GetAllAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Finds an agent state by its session key.
|
||||
/// </summary>
|
||||
/// <param name="sessionKey">The full session key, e.g. "agent:dex:telegram:direct:...".</param>
|
||||
/// <returns>The matching <see cref="AgentState"/>, or null if not found.</returns>
|
||||
Task<AgentState?> GetBySessionKeyAsync(string sessionKey);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the status of an agent state record.
|
||||
/// Also updates <c>LastActivity</c> to <see cref="DateTime.UtcNow"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">The unique identifier of the agent state record.</param>
|
||||
/// <param name="status">The new status value ("active", "idle", "thinking", "error").</param>
|
||||
/// <returns>The updated <see cref="AgentState"/>, or null if the record was not found.</returns>
|
||||
Task<AgentState?> UpdateStatusAsync(Guid id, string status);
|
||||
}
|
||||
Reference in New Issue
Block a user