using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Gott.Endpoints;
using Gott.Models;
using Microsoft.AspNetCore.Http;
namespace Gott.Services;
public partial class AnthropicService
{
///
/// Optionen für AddToolsToPayload – ersetzt die 12 bool-Parameter.
///
public record ToolPayloadOptions
{
public string Workspace { get; init; }
public string AgentId { get; init; }
public bool UseMemory { get; init; }
public string WebSearchVersion { get; init; }
public string ToolChoice { get; init; }
public int Iteration { get; init; }
public bool AllowUserChoice { get; init; }
public bool PlanMode { get; init; }
public bool IncludeRemote { get; init; }
public string RemoteOs { get; init; }
public bool IncludeGitHubSearch { get; init; }
public bool IncludeSerperSearch { get; init; }
}
private readonly IHttpClientFactory _httpFactory;
private readonly ToolService _toolService;
private readonly DatabaseService _db;
private readonly MemoryService _memoryService;
private readonly ServerSettingsService _settings;
private readonly VectorSearchService _vectorSearch;
private readonly EmbeddingBackgroundService _embeddingBg;
private readonly SteeringQueue _steeringQueue;
private readonly WorkflowProgressTracker _workflowTracker;
private ChatService _chatService;
private TelegramService _telegramService;
private ClaudeCliService _cliService;
public AnthropicService(IHttpClientFactory httpFactory, ToolService toolService, DatabaseService db, MemoryService memoryService = null, ServerSettingsService settings = null, VectorSearchService vectorSearch = null, EmbeddingBackgroundService embeddingBg = null, SteeringQueue steeringQueue = null, WorkflowProgressTracker workflowTracker = null)
{
_httpFactory = httpFactory;
_toolService = toolService;
_db = db;
_memoryService = memoryService;
_settings = settings;
_vectorSearch = vectorSearch;
_embeddingBg = embeddingBg;
_steeringQueue = steeringQueue;
_workflowTracker = workflowTracker;
}
public void SetCliService(ClaudeCliService cliService)
{
_cliService = cliService;
}
///
/// Prüft ob die Claude CLI verfügbar ist.
///
public bool IsCliAvailable => _cliService?.IsAvailable ?? false;
///
/// Wird nach der Konstruktion gesetzt um zirkuläre Abhängigkeit zu vermeiden
/// (ChatService → AnthropicService → ChatService).
///
public void SetChatService(ChatService chatService)
{
_chatService = chatService;
}
public void SetTelegramService(TelegramService telegramService)
{
_telegramService = telegramService;
}
private OpenAiService _openAiService;
public void SetOpenAiService(OpenAiService openAiService)
{
_openAiService = openAiService;
}
///
/// Liest Usage-Header aus einer Anthropic-Response und aktualisiert die api_keys-Tabelle.
/// Fire-and-forget – Fehler werden stillschweigend ignoriert.
///
private void TryUpdateKeyMeta(HttpResponseMessage httpResp, string apiToken)
{
try
{
var meta = AnthropicKeyChecker.ExtractMetaFromHeaders(httpResp.Headers);
if (meta == null || string.IsNullOrEmpty(apiToken)) return;
_ = _db.UpdateApiKeyMetaByToken(apiToken, meta.Status, meta.Plan, meta.Usage7d, meta.Usage5h);
}
catch { /* silent */ }
}
// ── Shared Helpers ──
// TruncateToolResult → SseHelper.cs
internal static string TruncateToolResult(string result) => SseHelper.TruncateToolResult(result);
///
/// Extrahiert den Text der letzten User-Message aus der Message-Liste.
///
private static string ExtractLastUserText(List