92 lines
3.8 KiB
C#
92 lines
3.8 KiB
C#
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, ImageService imageService)
|
|
{
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
|
|
var avatarRaw = body.TryGetProperty("avatar", out var av) ? av.GetString() : null;
|
|
|
|
var agent = new AgentRecord
|
|
{
|
|
Name = body.TryGetProperty("name", out var n) ? n.GetString() ?? "Neuer Agent" : "Neuer Agent",
|
|
Avatar = imageService.ResizeDataUrl(avatarRaw),
|
|
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,
|
|
ToolChoice = body.TryGetProperty("toolChoice", out var tc) ? tc.GetString() ?? "any" : "any"
|
|
};
|
|
|
|
agent = await db.CreateAgent(agent);
|
|
await ctx.Response.WriteAsJsonAsync(agent);
|
|
}
|
|
|
|
private static async Task HandleUpdate(HttpContext ctx, DatabaseService db, ImageService imageService)
|
|
{
|
|
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 = imageService.ResizeDataUrl(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();
|
|
if (body.TryGetProperty("toolChoice", out var tc)) agent.ToolChoice = tc.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;
|
|
}
|
|
}
|