refactor: Backend deduplizieren – 13 Dateien, -528 Zeilen netto

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.
This commit is contained in:
Meleki committed 2026-03-09 09:18:49 +01:00
1 parent 0097ce69db
commit dcedbaa074
13 files changed
+447 -975

No files matched your search

+7 -58
View File
@@ -338,7 +338,7 @@ public class DatabaseService
foreach (var (agentId, avatar) in agentsWithoutThumb)
{
var small = ResizeDataUrlToWebP(avatar, smallSize, smallQuality);
var small = ConvertDataUrlToWebP(avatar, smallQuality, maxSide: smallSize);
if (small != null)
conn.Execute("UPDATE agents SET avatar_small = @small WHERE agent_id = @agentId", new { small, agentId });
}
@@ -350,7 +350,7 @@ public class DatabaseService
foreach (var (sessionId, image) in sessionsWithoutThumb)
{
var small = ResizeDataUrlToWebP(image, smallSize, smallQuality);
var small = ConvertDataUrlToWebP(image, smallQuality, maxSide: smallSize);
if (small != null)
conn.Execute("UPDATE sessions SET image_small = @small WHERE session_id = @sessionId", new { small, sessionId });
}
@@ -359,7 +359,10 @@ public class DatabaseService
Console.WriteLine($"[Migration] Generated {agentsWithoutThumb.Count} agent thumbnails and {sessionsWithoutThumb.Count} session thumbnails");
}
private static string ResizeDataUrlToWebP(string dataUrl, int maxSide, int quality)
/// <summary>
/// Konvertiert ein DataUrl-Bild zu WebP. Optional Resize wenn maxSide > 0.
/// </summary>
private static string ConvertDataUrlToWebP(string dataUrl, int quality, int maxSide = 0)
{
try
{
@@ -370,7 +373,7 @@ public class DatabaseService
var bytes = Convert.FromBase64String(base64);
using var image = Image.Load(bytes);
if (image.Width > maxSide || image.Height > maxSide)
if (maxSide > 0 && (image.Width > maxSide || image.Height > maxSide))
{
image.Mutate(x => x.Resize(new ResizeOptions
{
@@ -390,28 +393,6 @@ public class DatabaseService
}
}
private static string ConvertDataUrlToWebP(string dataUrl, int quality)
{
try
{
var commaIndex = dataUrl.IndexOf(',');
if (commaIndex < 0) return null;
var base64 = dataUrl.Substring(commaIndex + 1);
var bytes = Convert.FromBase64String(base64);
using var image = Image.Load(bytes);
using var ms = new MemoryStream();
image.Save(ms, new WebpEncoder { Quality = quality });
return "data:image/webp;base64," + Convert.ToBase64String(ms.ToArray());
}
catch
{
return null; // Beschädigte Bilder überspringen
}
}
private void TryAddColumn(SqliteConnection conn, string table, string column, string type)
{
try { conn.Execute($"ALTER TABLE {table} ADD COLUMN {column} {type}"); }
@@ -573,13 +554,6 @@ public class DatabaseService
return session;
}
public async Task<IEnumerable<SessionRecord>> GetSessions(string userId)
{
using var conn = CreateConnection();
return await conn.QueryAsync<SessionRecord>(
"SELECT * FROM sessions WHERE user_id = @userId AND is_deleted = 0 ORDER BY updated_at DESC", new { userId });
}
public async Task<List<SessionWithStats>> GetSessionsWithStats(string userId)
{
using var conn = CreateConnection();
@@ -1303,23 +1277,6 @@ public class DatabaseService
new { sessionId, chatId, now });
}
public async Task SetSessionTelegramChatId(string sessionId, string chatId)
{
using var conn = CreateConnection();
var now = DateTime.UtcNow.ToString("o");
await conn.ExecuteAsync(
"UPDATE sessions SET telegram_chat_id = @chatId, updated_at = @now WHERE session_id = @sessionId",
new { sessionId, chatId, now });
}
public async Task ClearSessionTelegramChatId(string sessionId)
{
using var conn = CreateConnection();
await conn.ExecuteAsync(
"UPDATE sessions SET telegram_chat_id = NULL, updated_at = @now WHERE session_id = @sessionId",
new { sessionId, now = DateTime.UtcNow.ToString("o") });
}
public async Task<IEnumerable<SessionRecord>> GetSessionsByTelegramBot(string botId)
{
using var conn = CreateConnection();
@@ -1345,14 +1302,6 @@ public class DatabaseService
new { botId });
}
public async Task<IEnumerable<SessionRecord>> GetUserSessions(string userId)
{
using var conn = CreateConnection();
return await conn.QueryAsync<SessionRecord>(
"SELECT * FROM sessions WHERE user_id = @userId AND is_deleted = 0 ORDER BY updated_at DESC",
new { userId });
}
// ── Cronjobs ──
public async Task<CronjobRecord> CreateCronjob(CronjobRecord cronjob)