135 lines
4.8 KiB
C#
135 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
namespace Gott.Endpoints;
|
|
|
|
public static class SessionEndpoint
|
|
{
|
|
public static void MapSessionEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
app.MapGet("/api/sessions", HandleList);
|
|
app.MapGet("/api/sessions/{sessionId}", HandleGet);
|
|
app.MapPost("/api/sessions", HandleCreate);
|
|
app.MapPut("/api/sessions/{sessionId}", HandleUpdate);
|
|
app.MapDelete("/api/sessions/{sessionId}", HandleDelete);
|
|
app.MapGet("/api/sessions/{sessionId}/system-prompt", HandleSystemPrompt);
|
|
}
|
|
|
|
private static async Task HandleList(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessions = await db.GetSessions();
|
|
await ctx.Response.WriteAsJsonAsync(sessions);
|
|
}
|
|
|
|
private static async Task HandleGet(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
var session = await db.GetSession(sessionId);
|
|
if (session == null)
|
|
{
|
|
ctx.Response.StatusCode = 404;
|
|
return;
|
|
}
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleCreate(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
|
|
var name = body.TryGetProperty("name", out var n) ? n.GetString() : "Neue Session";
|
|
var description = body.TryGetProperty("description", out var d) ? d.GetString() : null;
|
|
var image = body.TryGetProperty("image", out var img) ? img.GetString() : null;
|
|
var agentId = body.TryGetProperty("agentId", out var a) ? a.GetString() : null;
|
|
var workspace = body.TryGetProperty("workspace", out var w) ? w.GetString() : null;
|
|
var context = body.TryGetProperty("context", out var ctxEl) ? ctxEl.GetString() : null;
|
|
|
|
var session = await db.CreateSession(name, description, image, agentId, workspace, context);
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleUpdate(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
await db.UpdateSession(sessionId, body);
|
|
|
|
var session = await db.GetSession(sessionId);
|
|
await ctx.Response.WriteAsJsonAsync(session);
|
|
}
|
|
|
|
private static async Task HandleDelete(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId)) { ctx.Response.StatusCode = 400; return; }
|
|
|
|
await db.DeleteSession(sessionId);
|
|
ctx.Response.StatusCode = 204;
|
|
}
|
|
|
|
private static async Task HandleSystemPrompt(HttpContext ctx, DatabaseService db)
|
|
{
|
|
var sessionId = ctx.Request.RouteValues["sessionId"]?.ToString();
|
|
if (string.IsNullOrEmpty(sessionId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
return;
|
|
}
|
|
|
|
var session = await db.GetSession(sessionId);
|
|
if (session == null)
|
|
{
|
|
ctx.Response.StatusCode = 404;
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(session.AgentId))
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
await ctx.Response.WriteAsJsonAsync(new { error = "Kein Agent ausgewählt" });
|
|
return;
|
|
}
|
|
|
|
var agent = await db.GetAgent(session.AgentId);
|
|
if (agent == null)
|
|
{
|
|
ctx.Response.StatusCode = 400;
|
|
await ctx.Response.WriteAsJsonAsync(new { error = "Agent nicht gefunden" });
|
|
return;
|
|
}
|
|
|
|
var promptParts = new List<string>();
|
|
promptParts.Add($"Du bist {agent.Name}.");
|
|
if (!string.IsNullOrWhiteSpace(agent.Persona))
|
|
promptParts.Add($"## Persona\n{agent.Persona}");
|
|
if (!string.IsNullOrWhiteSpace(agent.Instructions))
|
|
promptParts.Add($"## Instructions\n{agent.Instructions}");
|
|
if (!string.IsNullOrWhiteSpace(session.Workspace))
|
|
promptParts.Add($"## Workspace\n{session.Workspace}");
|
|
if (!string.IsNullOrWhiteSpace(session.Context))
|
|
promptParts.Add($"## Kontext\n{session.Context}");
|
|
|
|
var systemPrompt = string.Join("\n\n", promptParts);
|
|
|
|
ctx.Response.ContentType = "text/plain; charset=utf-8";
|
|
await ctx.Response.WriteAsync(systemPrompt);
|
|
}
|
|
}
|