From 57560cbbeac7fed3f6736800f089b7b3069db88c Mon Sep 17 00:00:00 2001 From: Meleki Date: Mon, 9 Mar 2026 07:03:47 +0100 Subject: [PATCH] refactor: MaxHistory + ReminderInterval in appsettings auslagern MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - MaxHistory (Default: 120): Max History-Messages pro API-Request - ReminderInterval (Default: 30): System-Prompt Reminder alle N Messages (0 = deaktiviert) - Beide Werte über Server-Settings-UI konfigurierbar - Hardcoded Werte in ChatService durch konfigurierbare Felder ersetzt --- Endpoints/ServerSettingsEndpoint.cs | 2 ++ Program.cs | 4 +++- Services/ChatService.cs | 18 +++++++++++------- appsettings.json | 2 ++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Endpoints/ServerSettingsEndpoint.cs b/Endpoints/ServerSettingsEndpoint.cs index ad05311..874a203 100644 --- a/Endpoints/ServerSettingsEndpoint.cs +++ b/Endpoints/ServerSettingsEndpoint.cs @@ -25,6 +25,8 @@ public static class ServerSettingsEndpoint new SettingDefinition("Port", "int", 19000, "Server-Port (erfordert Neustart)", "number"), new SettingDefinition("AnthropicToken", "string", "", "Anthropic API Key", "password"), new SettingDefinition("MaxIterations", "int", 100, "Max Tool-Loop Iterationen pro Chat", "number"), + new SettingDefinition("MaxHistory", "int", 120, "Max History-Messages pro Request", "number"), + new SettingDefinition("ReminderInterval", "int", 30, "System-Prompt Reminder alle N Messages (0 = deaktiviert)", "number"), new SettingDefinition("PayloadLogging", "bool", false, "API-Payloads loggen (Debug)", "toggle"), new SettingDefinition("PayloadLoggingGzip", "bool", false, "Payload-Logs als .gz komprimieren", "toggle"), new SettingDefinition("KreaToken", "string", "", "Krea API Token", "password"), diff --git a/Program.cs b/Program.cs index b856c03..db16e3b 100644 --- a/Program.cs +++ b/Program.cs @@ -30,7 +30,9 @@ builder.Services.AddSingleton(skillService); // Shared Services var toolService = new ToolService(); builder.Services.AddSingleton(toolService); -var chatService = new ChatService(dbService, toolService, memoryService, skillService); +var maxHistory = builder.Configuration.GetValue("MaxHistory", 120); +var reminderInterval = builder.Configuration.GetValue("ReminderInterval", 30); +var chatService = new ChatService(dbService, toolService, memoryService, skillService, maxHistory, reminderInterval); builder.Services.AddSingleton(chatService); // AnthropicService (Singleton – shared zwischen ChatEndpoint und TelegramService) diff --git a/Services/ChatService.cs b/Services/ChatService.cs index 244d653..afa7b72 100644 --- a/Services/ChatService.cs +++ b/Services/ChatService.cs @@ -14,13 +14,18 @@ public class ChatService private readonly ToolService _toolService; private readonly MemoryService _memoryService; private readonly SkillService _skillService; + private readonly int _maxHistory; + private readonly int _reminderInterval; - public ChatService(DatabaseService db, ToolService toolService, MemoryService memoryService, SkillService skillService) + public ChatService(DatabaseService db, ToolService toolService, MemoryService memoryService, SkillService skillService, + int maxHistory = 120, int reminderInterval = 30) { _db = db; _toolService = toolService; _memoryService = memoryService; _skillService = skillService; + _maxHistory = maxHistory; + _reminderInterval = reminderInterval; } public static string ResolveModelId(string model) @@ -478,14 +483,13 @@ Für welche Plattform soll die App entwickelt werden? Web, Mobile oder Desktop? } } - // History auf maximal 120 Messages begrenzen (älteste entfernen) - const int maxHistory = 120; - if (history.Count > maxHistory) - history.RemoveRange(0, history.Count - maxHistory); + // History auf maximal N Messages begrenzen (älteste entfernen) + if (history.Count > _maxHistory) + history.RemoveRange(0, history.Count - _maxHistory); // Reminder einfügen: alle ~30 user/assistant-Paare ein Reminder-Paar einfügen var reminder = SystemPromptBuilder.BuildReminder(promptBlocks); - if (!string.IsNullOrWhiteSpace(reminder) && history.Count > 30) + if (!string.IsNullOrWhiteSpace(reminder) && _reminderInterval > 0 && history.Count > _reminderInterval) { var reminderUser = new Dictionary { @@ -504,7 +508,7 @@ Für welche Plattform soll die App entwickelt werden? Web, Mobile oder Desktop? for (int i = 0; i < history.Count; i++) { messageCount++; - if (messageCount > 0 && messageCount % 30 == 0) + if (messageCount > 0 && messageCount % _reminderInterval == 0) { // Position nach dem aktuellen Element – aber nur einfügen wenn es eine // assistant-Message ist (damit user/assistant-Alternierung nicht bricht) diff --git a/appsettings.json b/appsettings.json index 841c762..b8f5b68 100755 --- a/appsettings.json +++ b/appsettings.json @@ -9,6 +9,8 @@ "Port": 19000, "AnthropicToken": "", "MaxIterations": 100, + "MaxHistory": 120, + "ReminderInterval": 30, "PayloadLogging": false, "PayloadLoggingGzip": false, "KreaToken": "",