diff --git a/Program.cs b/Program.cs index dd291ab..02242ff 100644 --- a/Program.cs +++ b/Program.cs @@ -35,10 +35,13 @@ builder.Services.AddSingleton(chatService); // AnthropicService (Singleton – shared zwischen ChatEndpoint und TelegramService) var anthropicToken = builder.Configuration["AnthropicToken"] ?? ""; +var payloadLogging = builder.Configuration.GetValue("PayloadLogging", false); +var payloadLoggingGzip = builder.Configuration.GetValue("PayloadLoggingGzip", false); builder.Services.AddSingleton(sp => new AnthropicService( sp.GetRequiredService(), - anthropicToken, toolService, dbService, memoryService)); + anthropicToken, toolService, dbService, memoryService, + payloadLogging, payloadLoggingGzip)); // Telegram Bot (pro Session – Token aus Session-Config) var maxIterations = builder.Configuration.GetValue("MaxIterations", 100); diff --git a/Services/AnthropicService.cs b/Services/AnthropicService.cs index b6723f1..e78e39d 100644 --- a/Services/AnthropicService.cs +++ b/Services/AnthropicService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.IO.Compression; using System.Net.Http; using System.Net.Http.Headers; using System.Text; @@ -19,14 +20,18 @@ public class AnthropicService private readonly ToolService _toolService; private readonly DatabaseService _db; private readonly MemoryService _memoryService; + private readonly bool _payloadLogging; + private readonly bool _payloadLoggingGzip; - public AnthropicService(IHttpClientFactory httpFactory, string token, ToolService toolService, DatabaseService db, MemoryService memoryService = null) + public AnthropicService(IHttpClientFactory httpFactory, string token, ToolService toolService, DatabaseService db, MemoryService memoryService = null, bool payloadLogging = false, bool payloadLoggingGzip = false) { _httpFactory = httpFactory; _token = token; _toolService = toolService; _db = db; _memoryService = memoryService; + _payloadLogging = payloadLogging; + _payloadLoggingGzip = payloadLoggingGzip; } public async Task StreamChat( @@ -416,15 +421,28 @@ public class AnthropicService return finalText.Length > 0 ? finalText.ToString() : "(Keine Textantwort)"; } - private static void LogPayload(string json, string sessionId, string chainId, int iteration) + private void LogPayload(string json, string sessionId, string chainId, int iteration) { + if (!_payloadLogging) return; try { var logsDir = Path.Combine(AppContext.BaseDirectory, "data", "logs"); Directory.CreateDirectory(logsDir); var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); - var fileName = $"{timestamp}-{sessionId}-{chainId}-iter{iteration}-payload.json"; - File.WriteAllText(Path.Combine(logsDir, fileName), json); + var baseName = $"{timestamp}-{sessionId}-{chainId}-iter{iteration}-payload"; + + if (_payloadLoggingGzip) + { + var filePath = Path.Combine(logsDir, baseName + ".json.gz"); + using var fs = File.Create(filePath); + using var gz = new GZipStream(fs, CompressionLevel.Fastest); + using var writer = new StreamWriter(gz, Encoding.UTF8); + writer.Write(json); + } + else + { + File.WriteAllText(Path.Combine(logsDir, baseName + ".json"), json); + } } catch { /* Logging darf nie die App crashen */ } } diff --git a/appsettings.json b/appsettings.json index 309ec2f..7c6a5e3 100755 --- a/appsettings.json +++ b/appsettings.json @@ -7,5 +7,7 @@ }, "AllowedHosts": "*", "AnthropicToken": "", - "MaxIterations": 100 + "MaxIterations": 100, + "PayloadLogging": false, + "PayloadLoggingGzip": false } diff --git a/wwwroot/js/pages/chat-page.js b/wwwroot/js/pages/chat-page.js index 6c277e8..5f73be7 100644 --- a/wwwroot/js/pages/chat-page.js +++ b/wwwroot/js/pages/chat-page.js @@ -384,7 +384,6 @@ registerPage('chat', { _streamingPollInterval = null; } stopHistoryPolling(); - messages.length = 0; lastKnownMessageId = 0; loadedArchiveCount = 0; isStreaming = false;