Agents: CRUD, DB table, session binding, replace model picker
- agents table with name, avatar, description, model, persona, instructions, soft-delete - AgentEndpoint (GET/POST/PUT/DELETE /api/agents) - sessions + chains now reference agent_id - ChatEndpoint resolves agent from session (no agent = no chat) - system prompt built from agent persona + instructions - frontend: agent editor with avatar upload, agent cards on dashboard - session: agent dropdown replaces model picker, choice persisted
This commit is contained in:
1 parent
0cd2d51d32
commit
449c872296
16 files changed
+552
-46
No files matched your search
@@ -0,0 +1,87 @@
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Gott.Models;
|
||||
using Gott.Services;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
|
||||
namespace Gott.Endpoints;
|
||||
|
||||
public static class AgentEndpoint
|
||||
{
|
||||
public static void MapAgentEndpoints(this IEndpointRouteBuilder app)
|
||||
{
|
||||
app.MapGet("/api/agents", HandleList);
|
||||
app.MapGet("/api/agents/{agentId}", HandleGet);
|
||||
app.MapPost("/api/agents", HandleCreate);
|
||||
app.MapPut("/api/agents/{agentId}", HandleUpdate);
|
||||
app.MapDelete("/api/agents/{agentId}", HandleDelete);
|
||||
}
|
||||
|
||||
private static async Task HandleList(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var agents = await db.GetAgents();
|
||||
await ctx.Response.WriteAsJsonAsync(agents);
|
||||
}
|
||||
|
||||
private static async Task HandleGet(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var agentId = ctx.Request.RouteValues["agentId"]?.ToString();
|
||||
if (string.IsNullOrEmpty(agentId)) { ctx.Response.StatusCode = 400; return; }
|
||||
|
||||
var agent = await db.GetAgent(agentId);
|
||||
if (agent == null) { ctx.Response.StatusCode = 404; return; }
|
||||
|
||||
await ctx.Response.WriteAsJsonAsync(agent);
|
||||
}
|
||||
|
||||
private static async Task HandleCreate(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
||||
|
||||
var agent = new AgentRecord
|
||||
{
|
||||
Name = body.TryGetProperty("name", out var n) ? n.GetString() ?? "Neuer Agent" : "Neuer Agent",
|
||||
Avatar = body.TryGetProperty("avatar", out var av) ? av.GetString() : null,
|
||||
Description = body.TryGetProperty("description", out var d) ? d.GetString() : null,
|
||||
Model = body.TryGetProperty("model", out var m) ? m.GetString() ?? "sonnet" : "sonnet",
|
||||
Persona = body.TryGetProperty("persona", out var p) ? p.GetString() : null,
|
||||
Instructions = body.TryGetProperty("instructions", out var i) ? i.GetString() : null
|
||||
};
|
||||
|
||||
agent = await db.CreateAgent(agent);
|
||||
await ctx.Response.WriteAsJsonAsync(agent);
|
||||
}
|
||||
|
||||
private static async Task HandleUpdate(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var agentId = ctx.Request.RouteValues["agentId"]?.ToString();
|
||||
if (string.IsNullOrEmpty(agentId)) { ctx.Response.StatusCode = 400; return; }
|
||||
|
||||
var agent = await db.GetAgent(agentId);
|
||||
if (agent == null) { ctx.Response.StatusCode = 404; return; }
|
||||
|
||||
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
||||
|
||||
if (body.TryGetProperty("name", out var n)) agent.Name = n.GetString();
|
||||
if (body.TryGetProperty("avatar", out var av)) agent.Avatar = av.GetString();
|
||||
if (body.TryGetProperty("description", out var d)) agent.Description = d.GetString();
|
||||
if (body.TryGetProperty("model", out var m)) agent.Model = m.GetString();
|
||||
if (body.TryGetProperty("persona", out var p)) agent.Persona = p.GetString();
|
||||
if (body.TryGetProperty("instructions", out var i)) agent.Instructions = i.GetString();
|
||||
|
||||
await db.UpdateAgent(agent);
|
||||
agent = await db.GetAgent(agentId);
|
||||
await ctx.Response.WriteAsJsonAsync(agent);
|
||||
}
|
||||
|
||||
private static async Task HandleDelete(HttpContext ctx, DatabaseService db)
|
||||
{
|
||||
var agentId = ctx.Request.RouteValues["agentId"]?.ToString();
|
||||
if (string.IsNullOrEmpty(agentId)) { ctx.Response.StatusCode = 400; return; }
|
||||
|
||||
await db.DeleteAgent(agentId);
|
||||
ctx.Response.StatusCode = 204;
|
||||
}
|
||||
}
|
||||
+42
-13
@@ -39,7 +39,32 @@ public static class ChatEndpoint
|
||||
return;
|
||||
}
|
||||
|
||||
var modelId = body.Model switch
|
||||
var sessionId = body.SessionId;
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
{
|
||||
res.StatusCode = 400;
|
||||
await res.WriteAsJsonAsync(new { error = "sessionId required" });
|
||||
return;
|
||||
}
|
||||
|
||||
// Agent aus Session auflösen
|
||||
var session = await db.GetSession(sessionId);
|
||||
if (session == null || string.IsNullOrEmpty(session.AgentId))
|
||||
{
|
||||
res.StatusCode = 400;
|
||||
await res.WriteAsJsonAsync(new { error = "Kein Agent ausgewählt" });
|
||||
return;
|
||||
}
|
||||
|
||||
var agent = await db.GetAgent(session.AgentId);
|
||||
if (agent == null)
|
||||
{
|
||||
res.StatusCode = 400;
|
||||
await res.WriteAsJsonAsync(new { error = "Agent nicht gefunden" });
|
||||
return;
|
||||
}
|
||||
|
||||
var modelId = agent.Model switch
|
||||
{
|
||||
"opus" => "claude-opus-4-6",
|
||||
"sonnet" => "claude-sonnet-4-6",
|
||||
@@ -50,20 +75,13 @@ public static class ChatEndpoint
|
||||
res.Headers["Cache-Control"] = "no-cache";
|
||||
res.Headers["Connection"] = "keep-alive";
|
||||
|
||||
var sessionId = body.SessionId;
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
{
|
||||
res.StatusCode = 400;
|
||||
await res.WriteAsJsonAsync(new { error = "sessionId required" });
|
||||
return;
|
||||
}
|
||||
|
||||
var chainId = Guid.NewGuid().ToString("N");
|
||||
|
||||
await db.CreateChain(new ChainRecord
|
||||
{
|
||||
ChainId = chainId,
|
||||
SessionId = sessionId,
|
||||
AgentId = agent.AgentId,
|
||||
Model = modelId,
|
||||
SystemPrompt = body.SystemPrompt
|
||||
});
|
||||
@@ -89,13 +107,24 @@ public static class ChatEndpoint
|
||||
});
|
||||
}
|
||||
|
||||
var systemPrompt = new List<object>
|
||||
{
|
||||
new { type = "text", text = "You are Claude Code, Anthropic's official CLI for Claude." }
|
||||
};
|
||||
var systemPrompt = new List<object>();
|
||||
|
||||
// Agent Persona
|
||||
if (!string.IsNullOrWhiteSpace(agent.Persona))
|
||||
systemPrompt.Add(new { type = "text", text = agent.Persona });
|
||||
|
||||
// Agent Instructions
|
||||
if (!string.IsNullOrWhiteSpace(agent.Instructions))
|
||||
systemPrompt.Add(new { type = "text", text = agent.Instructions });
|
||||
|
||||
// Zusätzlicher System Prompt aus dem UI
|
||||
if (!string.IsNullOrWhiteSpace(body.SystemPrompt))
|
||||
systemPrompt.Add(new { type = "text", text = body.SystemPrompt });
|
||||
|
||||
// Fallback wenn gar nix gesetzt
|
||||
if (systemPrompt.Count == 0)
|
||||
systemPrompt.Add(new { type = "text", text = "Du bist ein hilfreicher Assistent." });
|
||||
|
||||
var anthropic = new AnthropicService(httpFactory, token, toolService, db);
|
||||
await anthropic.StreamChat(res, modelId, systemPrompt, messages, chainId);
|
||||
}
|
||||
|
||||
@@ -61,6 +61,10 @@ public static class SessionEndpoint
|
||||
{
|
||||
await db.UpdateSessionName(sessionId, nameEl.GetString());
|
||||
}
|
||||
if (body.TryGetProperty("agentId", out var agentEl))
|
||||
{
|
||||
await db.UpdateSessionAgent(sessionId, agentEl.GetString());
|
||||
}
|
||||
|
||||
var session = await db.GetSession(sessionId);
|
||||
await ctx.Response.WriteAsJsonAsync(session);
|
||||
|
||||
Reference in new issue
Block a user