ChatContext: 3 identische Context-Klassen (PreparerContext, ReviewContext, FinalizerContext) zu einer SubAgentContext-Klasse zusammengefasst. ChatService: 3x40 Zeilen Sub-Agent-Setup durch BuildSubAgentContext() ersetzt. AnthropicService: 5 Hilfsmethoden extrahiert – ExtractWebSearchSources(), TruncateToolResult(), AddToolsToPayload(), ParseServerToolUse(), TrackUsage(). WorkspaceEndpoint: 5 Session/Agent-Endpoint-Paare durch generische Core-Handler mit basePath-Parameter ersetzt. Ungenutzte rmdir-Endpoints entfernt. AgentEndpoint: Doppeltes Agent-DTO-Mapping durch MapAgentList() ersetzt. SessionEndpoint: Doppelte Session/Agent-Auflösung durch ResolvePromptContext() ersetzt. DatabaseService: Toten Code entfernt (SetSessionTelegramChatId, ClearSessionTelegramChatId, GetSessions, GetUserSessions). ConvertDataUrlToWebP und ResizeDataUrlToWebP zu einer Methode zusammengefasst. TelegramEndpoint: Bot-DTO-Mapping durch MapBotResponse() dedupliziert. KreaEndpoint + SkillEndpoint: Thumbnail-Generierung in ImageService.GenerateThumbnail() extrahiert, SixLabors-Imports aus Endpoints entfernt. CronjobEndpoint: 7x ctx.Items[UserId] durch ctx.GetUserId() ersetzt.
185 lines
7.8 KiB
C#
185 lines
7.8 KiB
C#
using System;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using Gott.Models;
|
|
using Gott.Extensions;
|
|
using Gott.Services;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Routing;
|
|
|
|
namespace Gott.Endpoints;
|
|
|
|
public static class CronjobEndpoint
|
|
{
|
|
public static void MapCronjobEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
app.MapGet("/api/cronjobs", async (HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var cronjobs = await db.GetCronjobs(userId);
|
|
return Results.Ok(cronjobs);
|
|
});
|
|
|
|
app.MapGet("/api/cronjobs/{id}", async (string id, HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var cronjob = await db.GetCronjob(id);
|
|
if (cronjob == null || cronjob.UserId != userId) return Results.NotFound();
|
|
return Results.Ok(cronjob);
|
|
});
|
|
|
|
app.MapPost("/api/cronjobs", async (HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
|
|
var name = body.TryGetProperty("name", out var n) ? n.GetString() ?? "" : "";
|
|
var sessionId = body.TryGetProperty("sessionId", out var s) ? s.GetString() ?? "" : "";
|
|
var cronExpression = body.TryGetProperty("cronExpression", out var c) ? c.GetString() ?? "" : "";
|
|
var prompt = body.TryGetProperty("prompt", out var p) ? p.GetString() ?? "" : "";
|
|
var notifySessionId = body.TryGetProperty("notifySessionId", out var ns) ? ns.GetString() ?? "" : "";
|
|
|
|
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(sessionId) ||
|
|
string.IsNullOrWhiteSpace(cronExpression) || string.IsNullOrWhiteSpace(prompt))
|
|
return Results.BadRequest(new { error = "Name, Session, Cron-Expression und Prompt sind Pflichtfelder" });
|
|
|
|
// Cron-Expression validieren
|
|
var nextRun = CronjobService.CalculateNextRun(cronExpression);
|
|
if (string.IsNullOrEmpty(nextRun))
|
|
return Results.BadRequest(new { error = "Ungültige Cron-Expression" });
|
|
|
|
// Session-Zugehörigkeit prüfen
|
|
var session = await db.GetSession(sessionId);
|
|
if (session == null || session.UserId != userId)
|
|
return Results.BadRequest(new { error = "Session nicht gefunden" });
|
|
|
|
// Notify-Session prüfen (optional)
|
|
if (!string.IsNullOrEmpty(notifySessionId))
|
|
{
|
|
var notifySession = await db.GetSession(notifySessionId);
|
|
if (notifySession == null || notifySession.UserId != userId)
|
|
return Results.BadRequest(new { error = "Benachrichtigungs-Session nicht gefunden" });
|
|
}
|
|
|
|
var cronjob = new CronjobRecord
|
|
{
|
|
UserId = userId,
|
|
SessionId = sessionId,
|
|
Name = name,
|
|
CronExpression = cronExpression,
|
|
Prompt = prompt,
|
|
NotifySessionId = notifySessionId,
|
|
IsEnabled = true,
|
|
NextRunAt = nextRun
|
|
};
|
|
|
|
var created = await db.CreateCronjob(cronjob);
|
|
return Results.Ok(created);
|
|
});
|
|
|
|
app.MapPut("/api/cronjobs/{id}", async (string id, HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var existing = await db.GetCronjob(id);
|
|
if (existing == null || existing.UserId != userId) return Results.NotFound();
|
|
|
|
var body = await JsonSerializer.DeserializeAsync<JsonElement>(ctx.Request.Body);
|
|
|
|
if (body.TryGetProperty("name", out var n)) existing.Name = n.GetString() ?? existing.Name;
|
|
if (body.TryGetProperty("sessionId", out var s))
|
|
{
|
|
var newSessionId = s.GetString() ?? "";
|
|
if (!string.IsNullOrEmpty(newSessionId))
|
|
{
|
|
var session = await db.GetSession(newSessionId);
|
|
if (session == null || session.UserId != userId)
|
|
return Results.BadRequest(new { error = "Session nicht gefunden" });
|
|
existing.SessionId = newSessionId;
|
|
}
|
|
}
|
|
if (body.TryGetProperty("cronExpression", out var c))
|
|
{
|
|
var expr = c.GetString() ?? "";
|
|
if (!string.IsNullOrEmpty(expr))
|
|
{
|
|
var nextRun = CronjobService.CalculateNextRun(expr);
|
|
if (string.IsNullOrEmpty(nextRun))
|
|
return Results.BadRequest(new { error = "Ungültige Cron-Expression" });
|
|
existing.CronExpression = expr;
|
|
existing.NextRunAt = nextRun;
|
|
}
|
|
}
|
|
if (body.TryGetProperty("prompt", out var p)) existing.Prompt = p.GetString() ?? existing.Prompt;
|
|
if (body.TryGetProperty("notifySessionId", out var ns))
|
|
{
|
|
var notifyId = ns.GetString() ?? "";
|
|
if (!string.IsNullOrEmpty(notifyId))
|
|
{
|
|
var notifySession = await db.GetSession(notifyId);
|
|
if (notifySession == null || notifySession.UserId != userId)
|
|
return Results.BadRequest(new { error = "Benachrichtigungs-Session nicht gefunden" });
|
|
}
|
|
existing.NotifySessionId = notifyId;
|
|
}
|
|
if (body.TryGetProperty("isEnabled", out var e))
|
|
{
|
|
existing.IsEnabled = e.GetBoolean();
|
|
if (existing.IsEnabled)
|
|
existing.NextRunAt = CronjobService.CalculateNextRun(existing.CronExpression);
|
|
}
|
|
|
|
await db.UpdateCronjob(existing);
|
|
return Results.Ok(existing);
|
|
});
|
|
|
|
app.MapDelete("/api/cronjobs/{id}", async (string id, HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var existing = await db.GetCronjob(id);
|
|
if (existing == null || existing.UserId != userId) return Results.NotFound();
|
|
|
|
await db.DeleteCronjob(id, userId);
|
|
return Results.Ok();
|
|
});
|
|
|
|
app.MapPost("/api/cronjobs/{id}/toggle", async (string id, HttpContext ctx, DatabaseService db) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var existing = await db.GetCronjob(id);
|
|
if (existing == null || existing.UserId != userId) return Results.NotFound();
|
|
|
|
existing.IsEnabled = !existing.IsEnabled;
|
|
if (existing.IsEnabled)
|
|
existing.NextRunAt = CronjobService.CalculateNextRun(existing.CronExpression);
|
|
|
|
await db.UpdateCronjob(existing);
|
|
return Results.Ok(existing);
|
|
});
|
|
|
|
app.MapPost("/api/cronjobs/{id}/run", async (string id, HttpContext ctx, DatabaseService db, CronjobService cronjobService) =>
|
|
{
|
|
var userId = ctx.GetUserId();
|
|
var existing = await db.GetCronjob(id);
|
|
if (existing == null || existing.UserId != userId) return Results.NotFound();
|
|
|
|
try
|
|
{
|
|
await cronjobService.ExecuteCronjob(existing);
|
|
|
|
var nextRun = CronjobService.CalculateNextRun(existing.CronExpression);
|
|
await db.UpdateCronjobRun(existing.CronjobId, "success", null, nextRun);
|
|
|
|
return Results.Ok(new { status = "success" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var nextRun = CronjobService.CalculateNextRun(existing.CronExpression);
|
|
await db.UpdateCronjobRun(existing.CronjobId, "error", ex.Message, nextRun);
|
|
|
|
return Results.Ok(new { status = "error", error = ex.Message });
|
|
}
|
|
});
|
|
}
|
|
}
|