Add novel-writing app: .NET 10 API, React front end, agent and MCP server
Builds out the vertical slice for planning and writing a novel. Three front ends — the React UI, an embedded Claude agent, and an MCP stdio server — all go through one REST API, so an edit made from Claude Code and one made in the browser are the same edit. Layout: Domain entities and enums, no dependencies Application services, DTOs, the agent tool-use loop and its 15 tools Infrastructure EF Core 10 + SQLite, Anthropic SDK client Api ASP.NET Core 10 minimal APIs, OpenAPI, ProblemDetails Mcp MCP stdio server, 21 tools over the same REST API Web React 19 + Vite + TanStack Query + Tailwind v4 Data model is Project > Characters / OutlineNodes / Chapters > Scenes, plus agent conversations. The outline is a self-nesting tree so acts, sequences and beats can be arranged however the book wants; scenes carry goal/conflict/outcome because that is what the agent drafts prose from. Notes on a few choices: - Conversation history replays to the model as text only. The agent re-reads current state through its tools rather than trusting a record of edits that may since have changed in the UI. - The user's turn is persisted before the tool loop runs, so a question is recorded even when the model call fails. Turn order uses an explicit sequence column; timestamps tie when a turn completes inside one tick. - Tool failures return is_error results rather than throwing, so the model can read the message and correct itself. MCP tools do the same via CallToolResult, which keeps the API's own message instead of a generic SDK error. - The Anthropic client is constructed lazily. It is injected into the agent service, which also serves read-only endpoints, and those should keep working on an install with no key. Sending without one returns 503, not 400. - DateTimeOffset is stored as UTC ticks. SQLite refuses to ORDER BY the default text form, which every "recently updated first" listing depends on. Tests run against real in-memory SQLite rather than the EF in-memory provider so they exercise the cascade deletes and query translation that actually ship. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01S56bfZMGe1hnhpWP4CjjNw
This commit is contained in:
co-authored by
Claude Opus 5
parent
3c85bab4a4
commit
0d7b7a6f30
@@ -0,0 +1,136 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NovelSoftware.Application.Dtos;
|
||||
using NovelSoftware.Domain.Entities;
|
||||
|
||||
namespace NovelSoftware.Application.Services;
|
||||
|
||||
public class CharacterService(INovelDbContext db)
|
||||
{
|
||||
public async Task<IReadOnlyList<CharacterDto>> ListAsync(Guid projectId, CancellationToken ct = default)
|
||||
{
|
||||
var characters = await Query()
|
||||
.Where(c => c.ProjectId == projectId)
|
||||
.OrderBy(c => c.Role)
|
||||
.ThenBy(c => c.Name)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return [.. characters.Select(c => c.ToDto())];
|
||||
}
|
||||
|
||||
public async Task<CharacterDto> GetAsync(Guid id, CancellationToken ct = default) =>
|
||||
(await FindAsync(id, ct)).ToDto();
|
||||
|
||||
public async Task<CharacterDto> CreateAsync(Guid projectId, CreateCharacterRequest request, CancellationToken ct = default)
|
||||
{
|
||||
await EnsureProjectExists(projectId, ct);
|
||||
|
||||
var character = new Character
|
||||
{
|
||||
ProjectId = projectId,
|
||||
Name = request.Name,
|
||||
Role = request.Role,
|
||||
Age = request.Age,
|
||||
Pronouns = request.Pronouns,
|
||||
Occupation = request.Occupation,
|
||||
Appearance = request.Appearance,
|
||||
Personality = request.Personality,
|
||||
Backstory = request.Backstory,
|
||||
Want = request.Want,
|
||||
Need = request.Need,
|
||||
InternalConflict = request.InternalConflict,
|
||||
ExternalConflict = request.ExternalConflict,
|
||||
ArcSummary = request.ArcSummary,
|
||||
Voice = request.Voice,
|
||||
Notes = request.Notes
|
||||
};
|
||||
|
||||
db.Characters.Add(character);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return character.ToDto();
|
||||
}
|
||||
|
||||
public async Task<CharacterDto> UpdateAsync(Guid id, UpdateCharacterRequest request, CancellationToken ct = default)
|
||||
{
|
||||
var character = await FindAsync(id, ct);
|
||||
|
||||
character.Name = Patch.Apply(character.Name, request.Name) ?? character.Name;
|
||||
character.Role = request.Role ?? character.Role;
|
||||
character.Age = Patch.Apply(character.Age, request.Age);
|
||||
character.Pronouns = Patch.Apply(character.Pronouns, request.Pronouns);
|
||||
character.Occupation = Patch.Apply(character.Occupation, request.Occupation);
|
||||
character.Appearance = Patch.Apply(character.Appearance, request.Appearance);
|
||||
character.Personality = Patch.Apply(character.Personality, request.Personality);
|
||||
character.Backstory = Patch.Apply(character.Backstory, request.Backstory);
|
||||
character.Want = Patch.Apply(character.Want, request.Want);
|
||||
character.Need = Patch.Apply(character.Need, request.Need);
|
||||
character.InternalConflict = Patch.Apply(character.InternalConflict, request.InternalConflict);
|
||||
character.ExternalConflict = Patch.Apply(character.ExternalConflict, request.ExternalConflict);
|
||||
character.ArcSummary = Patch.Apply(character.ArcSummary, request.ArcSummary);
|
||||
character.Voice = Patch.Apply(character.Voice, request.Voice);
|
||||
character.Notes = Patch.Apply(character.Notes, request.Notes);
|
||||
character.UpdatedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
return character.ToDto();
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
var character = await FindAsync(id, ct);
|
||||
db.Characters.Remove(character);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<CharacterDto> AddRelationshipAsync(
|
||||
Guid characterId, CreateRelationshipRequest request, CancellationToken ct = default)
|
||||
{
|
||||
var character = await FindAsync(characterId, ct);
|
||||
|
||||
var related = await db.Characters
|
||||
.FirstOrDefaultAsync(c => c.Id == request.RelatedCharacterId, ct)
|
||||
?? throw new NotFoundException(nameof(Character), request.RelatedCharacterId);
|
||||
|
||||
if (related.ProjectId != character.ProjectId)
|
||||
{
|
||||
throw new InvalidOperationException("Characters must belong to the same project to be related.");
|
||||
}
|
||||
|
||||
db.CharacterRelationships.Add(new CharacterRelationship
|
||||
{
|
||||
CharacterId = characterId,
|
||||
RelatedCharacterId = request.RelatedCharacterId,
|
||||
RelationshipType = request.RelationshipType,
|
||||
Description = request.Description
|
||||
});
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
return (await FindAsync(characterId, ct)).ToDto();
|
||||
}
|
||||
|
||||
public async Task RemoveRelationshipAsync(Guid relationshipId, CancellationToken ct = default)
|
||||
{
|
||||
var relationship = await db.CharacterRelationships
|
||||
.FirstOrDefaultAsync(r => r.Id == relationshipId, ct)
|
||||
?? throw new NotFoundException(nameof(CharacterRelationship), relationshipId);
|
||||
|
||||
db.CharacterRelationships.Remove(relationship);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
private IQueryable<Character> Query() =>
|
||||
db.Characters
|
||||
.Include(c => c.Relationships)
|
||||
.ThenInclude(r => r.RelatedCharacter);
|
||||
|
||||
private async Task<Character> FindAsync(Guid id, CancellationToken ct) =>
|
||||
await Query().FirstOrDefaultAsync(c => c.Id == id, ct)
|
||||
?? throw new NotFoundException(nameof(Character), id);
|
||||
|
||||
private async Task EnsureProjectExists(Guid projectId, CancellationToken ct)
|
||||
{
|
||||
if (!await db.Projects.AnyAsync(p => p.Id == projectId, ct))
|
||||
{
|
||||
throw new NotFoundException(nameof(Project), projectId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user