Stop throwing for not-found; add Guard and request validation

Not-found lookups return null/false instead of throwing NotFoundException
across all services — a missing row is expected control flow, not an
exceptional condition. NotFoundException stays for embedded precondition
checks inside mutations (missing parent, invalid foreign reference).

Guard (copied from mic-check) enforces required arguments at the top of
every service method. A ported IModelValidator<T> framework validates
every request DTO at the API layer via a new ValidationEndpointFilter,
returning a 400 with field-level messages; services re-run the same
validator and throw for direct callers that bypass the API.

Endpoints translate null/false into 404 via a new ToApiResult() helper.
The agent toolset boundary translates the same nullable/bool results
into the tool-error text the model already expected.
This commit is contained in:
James Wampler
2026-08-06 15:13:36 -07:00
parent 04917fa09e
commit 40f93e40a8
45 changed files with 1523 additions and 377 deletions
+14 -14
View File
@@ -5,16 +5,16 @@ namespace Novelly.Api.Agent;
/// <summary>A chat thread between the writer and the embedded agent, scoped to one project.</summary>
public class AgentConversation
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid ProjectId { get; set; }
public Project? Project { get; set; }
public Guid Id { get; init; } = Guid.NewGuid();
public Guid ProjectId { get; init; }
public Project? Project { get; init; }
public string Title { get; set; } = "New conversation";
public string Title { get; init; } = "New conversation";
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset CreatedAt { get; init; } = DateTimeOffset.UtcNow;
public DateTimeOffset UpdatedAt { get; set; } = DateTimeOffset.UtcNow;
public List<AgentMessage> Messages { get; set; } = [];
public List<AgentMessage> Messages { get; init; } = [];
}
/// <summary>
@@ -24,26 +24,26 @@ public class AgentConversation
/// </summary>
public class AgentMessage
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid ConversationId { get; set; }
public AgentConversation? Conversation { get; set; }
public Guid Id { get; init; } = Guid.NewGuid();
public Guid ConversationId { get; init; }
public AgentConversation? Conversation { get; init; }
public AgentRole Role { get; set; }
public AgentRole Role { get; init; }
/// <summary>
/// Position in the conversation, 0-based. Timestamps are not enough to order a
/// transcript: a fast turn can produce two messages inside the same tick.
/// </summary>
public int Sequence { get; set; }
public int Sequence { get; init; }
/// <summary>The visible text of the turn.</summary>
public string Content { get; set; } = string.Empty;
public string Content { get; init; } = string.Empty;
/// <summary>
/// JSON array of <c>{ name, input, result }</c> objects describing tool calls made
/// during this turn. Null on user turns and on assistant turns that used no tools.
/// </summary>
public string? ToolCallsJson { get; set; }
public string? ToolCallsJson { get; init; }
public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow;
public DateTimeOffset CreatedAt { get; init; } = DateTimeOffset.UtcNow;
}