df94ed3cd4
- GatewayBridgeController: MCP-artiger Kommando-Adapter für Agent-zu-Backend - TaskBridgeService + LiveUpdateService: SSE Live-Sync + Bridge-Kommandos - FlowBoard.vue: Board-first orchestration dashboard panel - live-sync.ts store + live.ts service: SSE-basierte Live-Updates - Nullability-Warnung in HealthController.cs gefixt - nginx.conf: SSE-Proxy + CORS für Bridge-Endpunkte - .gitignore: pnpm/corepack local caches ausgeschlossen - docs: architecture-board-first-orchestration.md hinzugefügt - README: Backend Bridge API dokumentiert
169 lines
6.7 KiB
C#
169 lines
6.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Nexus.Api.Data;
|
|
using Nexus.Api.DTOs;
|
|
using Nexus.Api.Models;
|
|
using Nexus.Api.Services;
|
|
|
|
namespace Nexus.Api.Controllers;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/v1/tasks")]
|
|
public class TasksController(ITaskService taskService, IAgentService agentService) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<IResult> GetAll(CancellationToken ct)
|
|
=> Results.Ok(await taskService.GetAllAsync(ct));
|
|
|
|
[HttpPost]
|
|
public async Task<IResult> Create([FromBody] CreateTaskRequest request, CancellationToken ct)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(request.Title))
|
|
return Results.ValidationProblem(new Dictionary<string, string[]> { ["title"] = ["Title is required."] });
|
|
|
|
var task = await taskService.CreateAsync(request, ct);
|
|
return Results.Created($"/api/v1/tasks/{task.Id}", task);
|
|
}
|
|
|
|
[HttpGet("pending-approval")]
|
|
public async Task<IResult> GetPendingApproval(CancellationToken ct)
|
|
{
|
|
var pending = await taskService.GetPendingApprovalAsync(ct);
|
|
return Results.Ok(pending.Select(x => new { x.Id, x.Title, x.State, x.Priority, x.ProjectId, x.UpdatedAt }));
|
|
}
|
|
|
|
[HttpPost("{id:guid}/approve")]
|
|
public async Task<IResult> Approve(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await taskService.ApproveAsync(id, ct);
|
|
return result.Outcome switch
|
|
{
|
|
TaskOperationOutcome.NotFound => Results.NotFound(),
|
|
TaskOperationOutcome.InvalidState => Results.Problem(
|
|
title: "Approval denied",
|
|
detail: "Only tasks in 'In progress' or 'Blocked' state can be approved.",
|
|
statusCode: StatusCodes.Status403Forbidden),
|
|
_ => Results.Ok(result.Task)
|
|
};
|
|
}
|
|
|
|
[HttpPost("{id:guid}/reject")]
|
|
public async Task<IResult> Reject(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await taskService.RejectAsync(id, ct);
|
|
return result.Outcome switch
|
|
{
|
|
TaskOperationOutcome.NotFound => Results.NotFound(),
|
|
TaskOperationOutcome.InvalidState => Results.Problem(
|
|
title: "Rejection denied",
|
|
detail: "Only tasks in 'In progress' or 'Blocked' state can be rejected.",
|
|
statusCode: StatusCodes.Status403Forbidden),
|
|
_ => Results.Ok(result.Task)
|
|
};
|
|
}
|
|
|
|
[HttpPatch("{id:guid}/state")]
|
|
public async Task<IResult> UpdateState(Guid id, [FromBody] UpdateTaskStateRequest request, CancellationToken ct)
|
|
{
|
|
if (!TaskStateHelper.IsValidState(request.State))
|
|
return Results.ValidationProblem(new Dictionary<string, string[]> { ["state"] = ["Unsupported task state."] });
|
|
|
|
var result = await taskService.UpdateStateAsync(id, request.State, ct);
|
|
return result.Outcome switch
|
|
{
|
|
TaskOperationOutcome.NotFound => Results.NotFound(),
|
|
TaskOperationOutcome.InvalidState => Results.Problem(
|
|
title: "Action denied",
|
|
detail: "Statusänderungen sind nur Iris und Bao vorbehalten. Sub-Agenten können Tasks nicht verschieben.",
|
|
statusCode: StatusCodes.Status403Forbidden),
|
|
_ => Results.Ok(result.Task)
|
|
};
|
|
}
|
|
|
|
[HttpPatch("{id:guid}")]
|
|
public async Task<IResult> Update(Guid id, [FromBody] UpdateTaskRequest request, CancellationToken ct)
|
|
{
|
|
var result = await taskService.UpdateAsync(id, request, ct);
|
|
return result.Outcome switch
|
|
{
|
|
TaskOperationOutcome.NotFound => Results.NotFound(),
|
|
_ => Results.Ok(result.Task)
|
|
};
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<IResult> Delete(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await taskService.DeleteAsync(id, ct);
|
|
return result.Outcome switch
|
|
{
|
|
TaskOperationOutcome.NotFound => Results.NotFound(),
|
|
TaskOperationOutcome.InvalidState => Results.Problem(
|
|
title: "Task deletion denied",
|
|
detail: "Only tasks in 'Done' or 'Backlog' state can be deleted.",
|
|
statusCode: StatusCodes.Status403Forbidden),
|
|
_ => Results.NoContent()
|
|
};
|
|
}
|
|
|
|
// ── Board & Stale-Reset (für Iris Autonomous Worker) ──
|
|
|
|
/// <summary>
|
|
/// Gibt das Task-Board zurück (gruppiert nach Status, priorisiert sortiert).
|
|
/// Wird vom Iris Autonomous Worker genutzt.
|
|
///
|
|
/// SICHERHEIT: Erfordert X-Agent-Id Header (bel. erkannter Agent) ODER
|
|
/// X-Nexus-Api-Key / JWT. Kein [AllowAnonymous] mehr.
|
|
/// Für Agent-zu-Agent-Kommunikation den /api/bridge/board Endpunkt nutzen.
|
|
/// </summary>
|
|
[HttpGet("board")]
|
|
public async Task<IResult> GetBoard(CancellationToken ct)
|
|
{
|
|
// Erfordert mindestens einen identifizierbaren Agent-Aufrufer
|
|
var agentHeader = await GetAllowedAgentHeaderAsync(ct);
|
|
var isApiKey = HttpContext.User.IsInRole("Service");
|
|
var isAuth = HttpContext.User.Identity?.IsAuthenticated == true;
|
|
|
|
if (string.IsNullOrWhiteSpace(agentHeader) && !isApiKey && !isAuth)
|
|
return Results.Unauthorized();
|
|
|
|
return Results.Ok(await taskService.GetBoardAsync(ct));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Setzt stale Tasks (InProgress, älter als N Stunden) zurück auf Backlog.
|
|
/// Wird vom Iris Autonomous Worker genutzt.
|
|
///
|
|
/// SICHERHEIT: Erfordert X-Agent-Id Header (nur iris) ODER
|
|
/// X-Nexus-Api-Key / JWT-authenticated user.
|
|
/// Für Agent-zu-Agent-Kommunikation den /api/bridge Endpunkt nutzen.
|
|
/// </summary>
|
|
[HttpPost("reset-stale")]
|
|
public async Task<IResult> ResetStale([FromBody] ResetStaleRequest request, CancellationToken ct)
|
|
{
|
|
var agentHeader = await GetAllowedAgentHeaderAsync(ct);
|
|
var isApiKey = HttpContext.User.IsInRole("Service");
|
|
var isAuth = HttpContext.User.Identity?.IsAuthenticated == true;
|
|
|
|
// Nur iris, nexus-system (ApiKey) oder JWT-authenticated user
|
|
var isIris = string.Equals(agentHeader, "iris", StringComparison.OrdinalIgnoreCase);
|
|
if (!isIris && !isApiKey && !isAuth)
|
|
return Results.Unauthorized();
|
|
|
|
var count = await taskService.ResetStaleAsync(request.StaleHours, ct);
|
|
return Results.Ok(new ResetStaleResponse(count));
|
|
}
|
|
|
|
private async Task<string?> GetAllowedAgentHeaderAsync(CancellationToken ct)
|
|
{
|
|
var headerValue = HttpContext.Request.Headers["X-Agent-Id"].FirstOrDefault();
|
|
if (string.IsNullOrWhiteSpace(headerValue))
|
|
return null;
|
|
|
|
var normalized = headerValue.Trim().ToLowerInvariant();
|
|
var allowed = await agentService.GetAllowedAgentIdsAsync(ct);
|
|
return allowed.Contains(normalized) ? normalized : null;
|
|
}
|
|
}
|