diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/AnthropicModelDiscoveryTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/AnthropicModelDiscoveryTests.cs new file mode 100644 index 00000000..fe65409d --- /dev/null +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/AnthropicModelDiscoveryTests.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Moq; +using TelegramSearchBot.Interface; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +using TelegramSearchBot.Service.AI.LLM; +using Xunit; + +namespace TelegramSearchBot.LLM.Test.Service.AI.LLM { + public class AnthropicModelDiscoveryTests { + private static readonly string[] ExpectedStaticModels = { + "claude-sonnet-4-20250514", + "claude-opus-4-20250514", + "claude-3-5-sonnet-20241022", + "claude-3-5-haiku-20241022", + "claude-3-opus-20240229", + "claude-3-sonnet-20240229", + "claude-3-haiku-20240307" + }; + + [Theory] + [InlineData("https://api.anthropic.com", "https://api.anthropic.com/v1")] + [InlineData("https://api.anthropic.com/", "https://api.anthropic.com/v1")] + [InlineData("https://api.anthropic.com/v1", "https://api.anthropic.com/v1")] + [InlineData("https://api.anthropic.com/v1/", "https://api.anthropic.com/v1")] + [InlineData("https://opencode.ai/zen/v1", "https://opencode.ai/zen/v1")] + [InlineData("https://opencode.ai/zen/go/v1/", "https://opencode.ai/zen/go/v1")] + public void BuildModelsBaseUrl_NormalizesV1ExactlyOnce(string gateway, string expected) { + Assert.Equal(expected, AnthropicModelApi.BuildModelsBaseUrl(gateway)); + } + + [Fact] + public async Task GetAllModels_UsesDiscoveredModelsAndSendsAuthHeaders() { + var handler = new StubHandler(HttpStatusCode.OK, "{\"data\":[{\"id\":\"claude-opus-5\"},{\"id\":\"claude-sonnet-5\"}]}"); + var service = CreateService(handler); + + var models = (await service.GetAllModels(CreateChannel("https://api.anthropic.com"))).ToArray(); + + Assert.Equal(new[] { "claude-opus-5", "claude-sonnet-5" }, models); + Assert.Equal("https://api.anthropic.com/v1/models?limit=100", handler.RequestUri?.AbsoluteUri); + Assert.Equal("test-key", handler.LastRequest?.Headers.GetValues("x-api-key").Single()); + Assert.Equal("2023-06-01", handler.LastRequest?.Headers.GetValues("anthropic-version").Single()); + } + + [Theory] + [InlineData(HttpStatusCode.InternalServerError, "upstream down")] + [InlineData(HttpStatusCode.OK, "{\"data\":[]}")] + [InlineData(HttpStatusCode.OK, "not json at all")] + public async Task GetAllModels_FallsBackToStaticSnapshot_WhenDiscoveryUnavailable(HttpStatusCode statusCode, string content) { + var handler = new StubHandler(statusCode, content); + var service = CreateService(handler); + + var models = (await service.GetAllModels(CreateChannel("https://api.anthropic.com"))).ToArray(); + + Assert.Equal(ExpectedStaticModels, models); + } + + [Fact] + public async Task GetAllModelsWithCapabilities_CoversDiscoveredModels() { + var handler = new StubHandler(HttpStatusCode.OK, "{\"data\":[{\"id\":\"claude-opus-5\"}]}"); + var service = CreateService(handler); + + var models = (await service.GetAllModelsWithCapabilities(CreateChannel("https://api.anthropic.com"))).ToArray(); + + var model = Assert.Single(models); + Assert.Equal("claude-opus-5", model.ModelName); + Assert.True(model.SupportsVision); + } + + private static AnthropicModelApi CreateService(StubHandler handler) { + var factory = new Mock(); + factory.Setup(x => x.CreateClient(It.IsAny())).Returns(() => new HttpClient(handler, false)); + return new AnthropicModelApi( + null!, + Mock.Of>(), + Mock.Of(), + factory.Object); + } + + private static LLMChannel CreateChannel(string gateway) { + return new LLMChannel { + Provider = LLMProvider.Anthropic, + Gateway = gateway, + ApiKey = "test-key" + }; + } + + private sealed class StubHandler : HttpMessageHandler { + private readonly HttpStatusCode _statusCode; + private readonly string _content; + + public StubHandler(HttpStatusCode statusCode, string content) { + _statusCode = statusCode; + _content = content; + } + + public Uri? RequestUri { get; private set; } + public HttpRequestMessage? LastRequest { get; private set; } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + RequestUri = request.RequestUri; + LastRequest = request; + return Task.FromResult(new HttpResponseMessage(_statusCode) { + Content = new StringContent(_content) + }); + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs index 58392013..fe473373 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/AnthropicModelApi.cs @@ -208,16 +208,72 @@ private void LogPromptCachingObservation( #region Models - public virtual Task> GetAllModels(LLMChannel channel) { - return Task.FromResult>(_anthropicModels); + public virtual async Task> GetAllModels(LLMChannel channel) { + var discovered = await TryDiscoverModelsAsync(channel); + if (discovered.Count > 0) { + return discovered; + } + + _logger.LogInformation("Anthropic 目录发现失败或为空,回退到内置静态快照({Count} 个模型)", _anthropicModels.Length); + return _anthropicModels; + } + + /// + /// Anthropic-compatible GET /v1/models 发现(官方 API 与 OpenCode 等兼容网关都提供); + /// 任何失败都返回空列表,由调用方回退到静态快照,刷新链条不会因单个网关不可用而中断。 + /// + private async Task> TryDiscoverModelsAsync(LLMChannel channel) { + var result = new List(); + if (channel == null || string.IsNullOrWhiteSpace(channel.Gateway)) { + return result; + } + + try { + var baseUrl = BuildModelsBaseUrl(channel.Gateway); + using var httpClient = _httpClientFactory.CreateClient(); + if (!string.IsNullOrWhiteSpace(channel.ApiKey)) { + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("x-api-key", channel.ApiKey); + } + httpClient.DefaultRequestHeaders.TryAddWithoutValidation("anthropic-version", "2023-06-01"); + OpencodeSessionHeaders.Apply(httpClient, channel.Gateway); + + using var response = await httpClient.GetAsync($"{baseUrl}/models?limit=100"); + if (!response.IsSuccessStatusCode) { + _logger.LogWarning("Anthropic 目录请求失败 {StatusCode} ({Url})", (int)response.StatusCode, baseUrl); + return result; + } + + var json = await response.Content.ReadAsStringAsync(); + using var doc = JsonDocument.Parse(json); + if (!doc.RootElement.TryGetProperty("data", out var data) || data.ValueKind != JsonValueKind.Array) { + return result; + } + + foreach (var item in data.EnumerateArray()) { + var id = item.TryGetProperty("id", out var idProperty) ? idProperty.GetString() : null; + if (!string.IsNullOrWhiteSpace(id)) { + result.Add(id!); + } + } + } catch (Exception ex) { + _logger.LogWarning(ex, "Anthropic 目录请求异常,回退到静态快照"); + } + + return result; + } + + /// 把渠道网关归一化为 GET /v1/models 的基地址(重复的 /v1 只保留一次)。 + internal static string BuildModelsBaseUrl(string gateway) { + var trimmed = gateway.TrimEnd('/'); + return trimmed.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) ? trimmed : trimmed + "/v1"; } - public virtual Task> GetAllModelsWithCapabilities(LLMChannel channel) { + public virtual async Task> GetAllModelsWithCapabilities(LLMChannel channel) { var results = new List(); - foreach (var modelName in _anthropicModels) { + foreach (var modelName in await GetAllModels(channel)) { results.Add(InferAnthropicModelCapabilities(modelName)); } - return Task.FromResult>(results); + return results; } private ModelWithCapabilities InferAnthropicModelCapabilities(string modelName) { diff --git a/TelegramSearchBot.Test/Service/Scheduler/ModelCatalogRefreshTaskTests.cs b/TelegramSearchBot.Test/Service/Scheduler/ModelCatalogRefreshTaskTests.cs new file mode 100644 index 00000000..7d1bfe66 --- /dev/null +++ b/TelegramSearchBot.Test/Service/Scheduler/ModelCatalogRefreshTaskTests.cs @@ -0,0 +1,60 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Moq; +using TelegramSearchBot.Interface.Manage; +using TelegramSearchBot.Service.Scheduler; +using Xunit; + +namespace TelegramSearchBot.Test.Service.Scheduler { + public class ModelCatalogRefreshTaskTests { + [Fact] + public async Task ExecuteAsync_RefreshesAllChannels_AndHeartbeats() { + var helper = new Mock(); + helper.Setup(h => h.RefreshAllChannel()).ReturnsAsync(3); + + var services = new ServiceCollection(); + services.AddSingleton(helper.Object); + services.AddLogging(); + using var provider = services.BuildServiceProvider(); + + var task = new ModelCatalogRefreshTask(provider, provider.GetRequiredService>()); + var heartbeats = 0; + task.SetHeartbeatCallback(() => { + heartbeats++; + return Task.CompletedTask; + }); + + await task.ExecuteAsync(); + + helper.Verify(h => h.RefreshAllChannel(), Times.Once); + Assert.Equal(2, heartbeats); + } + + [Fact] + public void TaskName_AndCron_AreStable() { + var task = new ModelCatalogRefreshTask(Mock.Of(), Mock.Of>()); + + Assert.Equal("ModelCatalogRefresh", task.TaskName); + Assert.Equal("0 */6 * * *", task.CronExpression); + } + + [Fact] + public async Task ExecuteAsync_WithoutHeartbeatCallback_DoesNotThrow() { + var helper = new Mock(); + helper.Setup(h => h.RefreshAllChannel()).ReturnsAsync(0); + + var services = new ServiceCollection(); + services.AddSingleton(helper.Object); + services.AddLogging(); + using var provider = services.BuildServiceProvider(); + + var task = new ModelCatalogRefreshTask(provider, provider.GetRequiredService>()); + + await task.ExecuteAsync(); + + helper.Verify(h => h.RefreshAllChannel(), Times.Once); + } + } +} diff --git a/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs b/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs index 3a769749..01f6644d 100644 --- a/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs +++ b/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs @@ -1,312 +1,327 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using LiteDB; -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -using Serilog; -using StackExchange.Redis; -using Telegram.Bot; -using Telegram.Bot.Exceptions; -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; -using TelegramSearchBot.Common; -using TelegramSearchBot.Executor; -using TelegramSearchBot.Extension; -using TelegramSearchBot.Helper; -using TelegramSearchBot.Interface.Controller; -using TelegramSearchBot.Interface.Mcp; -using TelegramSearchBot.Manager; -using TelegramSearchBot.Model; -using TelegramSearchBot.Model.AI; -using TelegramSearchBot.Model.Mcp; -using TelegramSearchBot.Service.AI.LLM; -using TelegramSearchBot.Service.BotAPI; -using TelegramSearchBot.Service.Scheduler; -using TelegramSearchBot.Service.Storage; -using TelegramSearchBot.Service.Tools; -using TelegramSearchBot.Service.Vector; -using TelegramSearchBot.View; - -namespace TelegramSearchBot.AppBootstrap { - public class GeneralBootstrap : AppBootstrap { - private static IServiceProvider service; - - /// - /// 等待 TCP 服务端口就绪 - /// - private static async Task WaitForTcpServiceReady(string host, int port, string serviceName, Process? process = null, int maxRetries = 20, int delayMs = 500) { - for (int i = 0; i < maxRetries; i++) { - if (process is { HasExited: true }) { - Log.Warning("{ServiceName} 进程在端口就绪前退出,退出码 {ExitCode}", serviceName, process.ExitCode); - return; - } - - try { - using var tcp = new System.Net.Sockets.TcpClient(); - await tcp.ConnectAsync(host, port); - Log.Information("{ServiceName} 服务已就绪 ({Host}:{Port}),耗时约 {ElapsedMs}ms", serviceName, host, port, i * delayMs); - return; - } catch { - await Task.Delay(delayMs); - } - } - Log.Warning("等待 {ServiceName} 服务就绪超时 ({Host}:{Port}),将继续启动", serviceName, host, port); - } - - private static bool TryGetLoopbackLocalBotApiUri(out Uri? uri) { - if (!Env.IsLocalAPI || !Uri.TryCreate(Env.BaseUrl, UriKind.Absolute, out uri)) { - uri = null; - return false; - } - - return uri.IsLoopback || string.Equals(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase); - } - - private static async Task StartEmbeddedLocalBotApiAsync() { - string botApiExePath = Path.Combine(AppContext.BaseDirectory, "telegram-bot-api.exe"); - if (!File.Exists(botApiExePath)) { - Log.Warning("未找到 telegram-bot-api 可执行文件 {Path},跳过内置本地 Bot API 启动", botApiExePath); - return; - } - - if (string.IsNullOrWhiteSpace(Env.TelegramBotApiId) || string.IsNullOrWhiteSpace(Env.TelegramBotApiHash)) { - Log.Warning("EnableLocalBotAPI 为 true,但 TelegramBotApiId 或 TelegramBotApiHash 未配置,跳过内置本地 Bot API 启动"); - return; - } - - var botApiDataDir = Path.Combine(Env.WorkDir, "telegram-bot-api"); - Directory.CreateDirectory(botApiDataDir); - - var botApiProcess = Fork(botApiExePath, [ - "--local", - $"--api-id={Env.TelegramBotApiId}", - $"--api-hash={Env.TelegramBotApiHash}", - $"--dir={botApiDataDir}", - $"--http-port={Env.LocalBotApiPort}" - ]); - - Log.Information("内置 telegram-bot-api 已启动,等待端口 {Port} 就绪...", Env.LocalBotApiPort); - await WaitForTcpServiceReady("127.0.0.1", Env.LocalBotApiPort, "telegram-bot-api", botApiProcess, maxRetries: 40); - } - - private static async Task WaitForExternalLocalBotApiIfNeededAsync() { - if (!TryGetLoopbackLocalBotApiUri(out var localBotApiUri) || localBotApiUri == null || Env.EnableLocalBotAPI) { - return; - } - - var port = localBotApiUri.IsDefaultPort - ? ( localBotApiUri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) ? 443 : 80 ) - : localBotApiUri.Port; - - Log.Information("使用外部本地 Bot API: {BaseUrl}", Env.BaseUrl); - await WaitForTcpServiceReady(localBotApiUri.Host, port, "external telegram-bot-api", maxRetries: 40); - } - - /// - /// 等待本地 telegram-bot-api 服务端口就绪,最多等待 20 秒 - /// - private static async Task WaitForLocalBotApiReady(int port, int maxRetries = 40, int delayMs = 500) { - for (int i = 0; i < maxRetries; i++) { - try { - using var tcp = new System.Net.Sockets.TcpClient(); - await tcp.ConnectAsync("127.0.0.1", port); - Log.Information("telegram-bot-api 服务已就绪 (端口 {Port}),耗时约 {ElapsedMs}ms", port, i * delayMs); - return; - } catch { - await Task.Delay(delayMs); - } - } - Log.Warning("等待 telegram-bot-api 服务就绪超时 (端口 {Port}),将继续启动", port); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .UseSerilog() - .ConfigureServices(services => { - services.ConfigureAllServices(); - }); - public static async Task Startup(string[] args) { - Utils.CheckExistsAndCreateDirectorys($"{Env.WorkDir}/logs"); - - Directory.SetCurrentDirectory(Env.WorkDir); - - - Env.SchedulerPort = Utils.GetRandomAvailablePort(); -#if DEBUG - Env.SchedulerPort = 6379; -#endif - var schedulerProcess = Fork(["Scheduler", $"{Env.SchedulerPort}"]); - - // 等待 Garnet 服务就绪,避免竞态条件导致 Redis 连接失败 - await WaitForTcpServiceReady("127.0.0.1", Env.SchedulerPort, "Garnet", schedulerProcess); - - if (Env.EnableLocalBotAPI) { - await StartEmbeddedLocalBotApiAsync(); - } else { - await WaitForExternalLocalBotApiIfNeededAsync(); - } - - // 如果启用了本地 telegram-bot-api,则在此启动它 - if (Env.EnableLocalBotAPI) { - string botApiExePath = Path.Combine(AppContext.BaseDirectory, "telegram-bot-api.exe"); - if (File.Exists(botApiExePath)) { - if (string.IsNullOrEmpty(Env.TelegramBotApiId) || string.IsNullOrEmpty(Env.TelegramBotApiHash)) { - Log.Warning("EnableLocalBotAPI 为 true,但 TelegramBotApiId 或 TelegramBotApiHash 未配置,跳过本地 Bot API 启动"); - } else { - var botApiDataDir = Path.Combine(Env.WorkDir, "telegram-bot-api"); - Directory.CreateDirectory(botApiDataDir); - // 使用 ArgumentList 以正确处理路径中的空格 - // --local 模式允许大文件上传下载并将文件存储在本地 dir 下 - var startInfo = new ProcessStartInfo { - FileName = botApiExePath, - UseShellExecute = false, - RedirectStandardOutput = true, - RedirectStandardError = true - }; - startInfo.ArgumentList.Add("--local"); - startInfo.ArgumentList.Add($"--api-id={Env.TelegramBotApiId}"); - startInfo.ArgumentList.Add($"--api-hash={Env.TelegramBotApiHash}"); - startInfo.ArgumentList.Add($"--dir={botApiDataDir}"); - startInfo.ArgumentList.Add($"--http-port={Env.LocalBotApiPort}"); - var botApiProcess = Process.Start(startInfo); - if (botApiProcess == null) { - Log.Warning("telegram-bot-api 进程启动失败"); - } else { - childProcessManager.AddProcess(botApiProcess); - Log.Information("telegram-bot-api 已启动,等待端口 {Port} 就绪...", Env.LocalBotApiPort); - await WaitForLocalBotApiReady(Env.LocalBotApiPort); - } - } - } else { - Log.Warning("未找到 telegram-bot-api 可执行文件 {Path},跳过本地 Bot API 启动", botApiExePath); - } - } - - IHost host = CreateHostBuilder(args) - //.ConfigureLogging(logging => { - // logging.ClearProviders(); - - // logging.AddSimpleConsole(options => - // { - // options.IncludeScopes = true; - // options.SingleLine = true; - // options.TimestampFormat = "[yyyy/MM/dd HH:mm:ss] "; - // }); - //}) - .Build(); - - var bot = host.Services.GetRequiredService(); - using CancellationTokenSource cts = new(); - service = host.Services; - - // SQLite 数据库初始化 - using (var serviceScope = service.GetService().CreateScope()) { - var context = serviceScope.ServiceProvider.GetRequiredService(); - var searchCacheContext = serviceScope.ServiceProvider.GetRequiredService(); - //context.Database.EnsureCreated(); - context.Database.Migrate(); - await searchCacheContext.Database.MigrateAsync(); - - var imageGenerationSettings = serviceScope.ServiceProvider.GetRequiredService(); - await imageGenerationSettings.InitializeToolVisibilityAsync(); - - var musicGenerationSettings = serviceScope.ServiceProvider.GetRequiredService(); - await musicGenerationSettings.InitializeToolVisibilityAsync(); - } - - var loggerFactory = service.GetRequiredService(); - var mcpLogger = loggerFactory.CreateLogger("McpToolHelperInitialization"); - var mainAssembly = typeof(GeneralBootstrap).Assembly; - var llmAssembly = typeof(McpToolHelper).Assembly; - McpToolHelper.EnsureInitialized(mainAssembly, llmAssembly, service, mcpLogger); - Log.Information("McpToolHelper has been initialized with built-in tools."); - - if (Env.EnableLlmWindowsSandbox) { - if (!OperatingSystem.IsWindows()) { - throw new PlatformNotSupportedException("EnableLlmWindowsSandbox requires Windows."); - } - RegisterWindowsSandboxTools(service); - Log.Information("Windows AppContainer LLM tool sandbox is enabled."); - } - - // Initialize external MCP tool servers - try { - var mcpServerManager = service.GetRequiredService(); - await mcpServerManager.InitializeAllServersAsync(); - - // Register external tools with McpToolHelper - RegisterExternalMcpTools(mcpServerManager); - Log.Information("External MCP servers initialized."); - } catch (Exception ex) { - Log.Warning(ex, "Failed to initialize external MCP servers. Continuing without them."); - } - - // Export tool definitions to Redis so agent processes can discover available tools - try { - var redis = service.GetRequiredService(); - await McpToolHelper.RefreshAgentToolDefsInRedisAsync(redis); - Log.Information("Exported tool definitions to Redis for agent discovery."); - } catch (Exception ex) { - Log.Warning(ex, "Failed to export tool definitions to Redis. Agent processes may have limited tools."); - } - - // 启动Host,SchedulerService作为HostedService会自动启动 - await host.StartAsync(); - Log.Information("Host已启动,定时任务调度器已作为后台服务启动"); - - // 接收消息的逻辑已迁移到 TelegramBotReceiverService (IHostedService) - // 机器人信息将在该服务启动时打印 - - // 保持程序运行 - await host.WaitForShutdownAsync(); - } - - /// - /// Register external MCP tools with McpToolHelper so they appear in the LLM prompt - /// and can be executed through the tool call system. - /// - private static void RegisterExternalMcpTools(IMcpServerManager mcpServerManager) { - McpToolHelper.RegisterExternalMcpTools(mcpServerManager); - } - - [System.Runtime.Versioning.SupportedOSPlatform("windows")] - private static void RegisterWindowsSandboxTools(IServiceProvider services) { - var sandboxService = services.GetRequiredService(); - McpToolHelper.RegisterProxyTools( - WindowsAppContainerToolHostService.GetToolDefinitions(), - async (toolName, arguments) => { - long chatId = 0, userId = 0, messageId = 0; - if (arguments.TryGetValue("__chatId", out var cid)) { - long.TryParse(cid, out chatId); - arguments.Remove("__chatId"); - } - if (arguments.TryGetValue("__userId", out var uid)) { - long.TryParse(uid, out userId); - arguments.Remove("__userId"); - } - if (arguments.TryGetValue("__messageId", out var mid)) { - long.TryParse(mid, out messageId); - arguments.Remove("__messageId"); - } - - if (chatId == 0) { - throw new InvalidOperationException("Sandboxed tool execution requires a valid __chatId context value."); - } - - return await sandboxService.ExecuteToolAsync(toolName, arguments, chatId, userId, messageId); - }, - allowLocalOverride: true); - } - } -} +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using LiteDB; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Serilog; +using StackExchange.Redis; +using Telegram.Bot; +using Telegram.Bot.Exceptions; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using TelegramSearchBot.Common; +using TelegramSearchBot.Executor; +using TelegramSearchBot.Extension; +using TelegramSearchBot.Helper; +using TelegramSearchBot.Interface.Controller; +using TelegramSearchBot.Interface.Manage; +using TelegramSearchBot.Interface.Mcp; +using TelegramSearchBot.Manager; +using TelegramSearchBot.Model; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Mcp; +using TelegramSearchBot.Service.AI.LLM; +using TelegramSearchBot.Service.BotAPI; +using TelegramSearchBot.Service.Scheduler; +using TelegramSearchBot.Service.Storage; +using TelegramSearchBot.Service.Tools; +using TelegramSearchBot.Service.Vector; +using TelegramSearchBot.View; + +namespace TelegramSearchBot.AppBootstrap { + public class GeneralBootstrap : AppBootstrap { + private static IServiceProvider service; + + /// + /// 等待 TCP 服务端口就绪 + /// + private static async Task WaitForTcpServiceReady(string host, int port, string serviceName, Process? process = null, int maxRetries = 20, int delayMs = 500) { + for (int i = 0; i < maxRetries; i++) { + if (process is { HasExited: true }) { + Log.Warning("{ServiceName} 进程在端口就绪前退出,退出码 {ExitCode}", serviceName, process.ExitCode); + return; + } + + try { + using var tcp = new System.Net.Sockets.TcpClient(); + await tcp.ConnectAsync(host, port); + Log.Information("{ServiceName} 服务已就绪 ({Host}:{Port}),耗时约 {ElapsedMs}ms", serviceName, host, port, i * delayMs); + return; + } catch { + await Task.Delay(delayMs); + } + } + Log.Warning("等待 {ServiceName} 服务就绪超时 ({Host}:{Port}),将继续启动", serviceName, host, port); + } + + private static bool TryGetLoopbackLocalBotApiUri(out Uri? uri) { + if (!Env.IsLocalAPI || !Uri.TryCreate(Env.BaseUrl, UriKind.Absolute, out uri)) { + uri = null; + return false; + } + + return uri.IsLoopback || string.Equals(uri.Host, "localhost", StringComparison.OrdinalIgnoreCase); + } + + private static async Task StartEmbeddedLocalBotApiAsync() { + string botApiExePath = Path.Combine(AppContext.BaseDirectory, "telegram-bot-api.exe"); + if (!File.Exists(botApiExePath)) { + Log.Warning("未找到 telegram-bot-api 可执行文件 {Path},跳过内置本地 Bot API 启动", botApiExePath); + return; + } + + if (string.IsNullOrWhiteSpace(Env.TelegramBotApiId) || string.IsNullOrWhiteSpace(Env.TelegramBotApiHash)) { + Log.Warning("EnableLocalBotAPI 为 true,但 TelegramBotApiId 或 TelegramBotApiHash 未配置,跳过内置本地 Bot API 启动"); + return; + } + + var botApiDataDir = Path.Combine(Env.WorkDir, "telegram-bot-api"); + Directory.CreateDirectory(botApiDataDir); + + var botApiProcess = Fork(botApiExePath, [ + "--local", + $"--api-id={Env.TelegramBotApiId}", + $"--api-hash={Env.TelegramBotApiHash}", + $"--dir={botApiDataDir}", + $"--http-port={Env.LocalBotApiPort}" + ]); + + Log.Information("内置 telegram-bot-api 已启动,等待端口 {Port} 就绪...", Env.LocalBotApiPort); + await WaitForTcpServiceReady("127.0.0.1", Env.LocalBotApiPort, "telegram-bot-api", botApiProcess, maxRetries: 40); + } + + private static async Task WaitForExternalLocalBotApiIfNeededAsync() { + if (!TryGetLoopbackLocalBotApiUri(out var localBotApiUri) || localBotApiUri == null || Env.EnableLocalBotAPI) { + return; + } + + var port = localBotApiUri.IsDefaultPort + ? ( localBotApiUri.Scheme.Equals(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase) ? 443 : 80 ) + : localBotApiUri.Port; + + Log.Information("使用外部本地 Bot API: {BaseUrl}", Env.BaseUrl); + await WaitForTcpServiceReady(localBotApiUri.Host, port, "external telegram-bot-api", maxRetries: 40); + } + + /// + /// 等待本地 telegram-bot-api 服务端口就绪,最多等待 20 秒 + /// + private static async Task WaitForLocalBotApiReady(int port, int maxRetries = 40, int delayMs = 500) { + for (int i = 0; i < maxRetries; i++) { + try { + using var tcp = new System.Net.Sockets.TcpClient(); + await tcp.ConnectAsync("127.0.0.1", port); + Log.Information("telegram-bot-api 服务已就绪 (端口 {Port}),耗时约 {ElapsedMs}ms", port, i * delayMs); + return; + } catch { + await Task.Delay(delayMs); + } + } + Log.Warning("等待 telegram-bot-api 服务就绪超时 (端口 {Port}),将继续启动", port); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .UseSerilog() + .ConfigureServices(services => { + services.ConfigureAllServices(); + }); + public static async Task Startup(string[] args) { + Utils.CheckExistsAndCreateDirectorys($"{Env.WorkDir}/logs"); + + Directory.SetCurrentDirectory(Env.WorkDir); + + + Env.SchedulerPort = Utils.GetRandomAvailablePort(); +#if DEBUG + Env.SchedulerPort = 6379; +#endif + var schedulerProcess = Fork(["Scheduler", $"{Env.SchedulerPort}"]); + + // 等待 Garnet 服务就绪,避免竞态条件导致 Redis 连接失败 + await WaitForTcpServiceReady("127.0.0.1", Env.SchedulerPort, "Garnet", schedulerProcess); + + if (Env.EnableLocalBotAPI) { + await StartEmbeddedLocalBotApiAsync(); + } else { + await WaitForExternalLocalBotApiIfNeededAsync(); + } + + // 如果启用了本地 telegram-bot-api,则在此启动它 + if (Env.EnableLocalBotAPI) { + string botApiExePath = Path.Combine(AppContext.BaseDirectory, "telegram-bot-api.exe"); + if (File.Exists(botApiExePath)) { + if (string.IsNullOrEmpty(Env.TelegramBotApiId) || string.IsNullOrEmpty(Env.TelegramBotApiHash)) { + Log.Warning("EnableLocalBotAPI 为 true,但 TelegramBotApiId 或 TelegramBotApiHash 未配置,跳过本地 Bot API 启动"); + } else { + var botApiDataDir = Path.Combine(Env.WorkDir, "telegram-bot-api"); + Directory.CreateDirectory(botApiDataDir); + // 使用 ArgumentList 以正确处理路径中的空格 + // --local 模式允许大文件上传下载并将文件存储在本地 dir 下 + var startInfo = new ProcessStartInfo { + FileName = botApiExePath, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + startInfo.ArgumentList.Add("--local"); + startInfo.ArgumentList.Add($"--api-id={Env.TelegramBotApiId}"); + startInfo.ArgumentList.Add($"--api-hash={Env.TelegramBotApiHash}"); + startInfo.ArgumentList.Add($"--dir={botApiDataDir}"); + startInfo.ArgumentList.Add($"--http-port={Env.LocalBotApiPort}"); + var botApiProcess = Process.Start(startInfo); + if (botApiProcess == null) { + Log.Warning("telegram-bot-api 进程启动失败"); + } else { + childProcessManager.AddProcess(botApiProcess); + Log.Information("telegram-bot-api 已启动,等待端口 {Port} 就绪...", Env.LocalBotApiPort); + await WaitForLocalBotApiReady(Env.LocalBotApiPort); + } + } + } else { + Log.Warning("未找到 telegram-bot-api 可执行文件 {Path},跳过本地 Bot API 启动", botApiExePath); + } + } + + IHost host = CreateHostBuilder(args) + //.ConfigureLogging(logging => { + // logging.ClearProviders(); + + // logging.AddSimpleConsole(options => + // { + // options.IncludeScopes = true; + // options.SingleLine = true; + // options.TimestampFormat = "[yyyy/MM/dd HH:mm:ss] "; + // }); + //}) + .Build(); + + var bot = host.Services.GetRequiredService(); + using CancellationTokenSource cts = new(); + service = host.Services; + + // SQLite 数据库初始化 + using (var serviceScope = service.GetService().CreateScope()) { + var context = serviceScope.ServiceProvider.GetRequiredService(); + var searchCacheContext = serviceScope.ServiceProvider.GetRequiredService(); + //context.Database.EnsureCreated(); + context.Database.Migrate(); + await searchCacheContext.Database.MigrateAsync(); + + var imageGenerationSettings = serviceScope.ServiceProvider.GetRequiredService(); + await imageGenerationSettings.InitializeToolVisibilityAsync(); + + var musicGenerationSettings = serviceScope.ServiceProvider.GetRequiredService(); + await musicGenerationSettings.InitializeToolVisibilityAsync(); + } + + var loggerFactory = service.GetRequiredService(); + var mcpLogger = loggerFactory.CreateLogger("McpToolHelperInitialization"); + var mainAssembly = typeof(GeneralBootstrap).Assembly; + var llmAssembly = typeof(McpToolHelper).Assembly; + McpToolHelper.EnsureInitialized(mainAssembly, llmAssembly, service, mcpLogger); + Log.Information("McpToolHelper has been initialized with built-in tools."); + + if (Env.EnableLlmWindowsSandbox) { + if (!OperatingSystem.IsWindows()) { + throw new PlatformNotSupportedException("EnableLlmWindowsSandbox requires Windows."); + } + RegisterWindowsSandboxTools(service); + Log.Information("Windows AppContainer LLM tool sandbox is enabled."); + } + + // Initialize external MCP tool servers + try { + var mcpServerManager = service.GetRequiredService(); + await mcpServerManager.InitializeAllServersAsync(); + + // Register external tools with McpToolHelper + RegisterExternalMcpTools(mcpServerManager); + Log.Information("External MCP servers initialized."); + } catch (Exception ex) { + Log.Warning(ex, "Failed to initialize external MCP servers. Continuing without them."); + } + + // Export tool definitions to Redis so agent processes can discover available tools + try { + var redis = service.GetRequiredService(); + await McpToolHelper.RefreshAgentToolDefsInRedisAsync(redis); + Log.Information("Exported tool definitions to Redis for agent discovery."); + } catch (Exception ex) { + Log.Warning(ex, "Failed to export tool definitions to Redis. Agent processes may have limited tools."); + } + + // 启动Host,SchedulerService作为HostedService会自动启动 + await host.StartAsync(); + Log.Information("Host已启动,定时任务调度器已作为后台服务启动"); + + // 启动后异步刷新一次渠道模型目录:ScheduledTask 只在 cron 到点才跑, + // 首次部署/重启后应尽快拿到供应商最新模型(失败由 6 小时定时任务兜底)。 + _ = Task.Run(async () => { + try { + await Task.Delay(TimeSpan.FromSeconds(30)); + using var scope = service.CreateScope(); + var helper = scope.ServiceProvider.GetRequiredService(); + var added = await helper.RefreshAllChannel(); + Log.Information("启动后模型目录刷新完成,新增/恢复 {Count} 个模型", added); + } catch (Exception ex) { + Log.Warning(ex, "启动后模型目录刷新失败,将由定时任务重试"); + } + }); + + // 接收消息的逻辑已迁移到 TelegramBotReceiverService (IHostedService) + // 机器人信息将在该服务启动时打印 + + // 保持程序运行 + await host.WaitForShutdownAsync(); + } + + /// + /// Register external MCP tools with McpToolHelper so they appear in the LLM prompt + /// and can be executed through the tool call system. + /// + private static void RegisterExternalMcpTools(IMcpServerManager mcpServerManager) { + McpToolHelper.RegisterExternalMcpTools(mcpServerManager); + } + + [System.Runtime.Versioning.SupportedOSPlatform("windows")] + private static void RegisterWindowsSandboxTools(IServiceProvider services) { + var sandboxService = services.GetRequiredService(); + McpToolHelper.RegisterProxyTools( + WindowsAppContainerToolHostService.GetToolDefinitions(), + async (toolName, arguments) => { + long chatId = 0, userId = 0, messageId = 0; + if (arguments.TryGetValue("__chatId", out var cid)) { + long.TryParse(cid, out chatId); + arguments.Remove("__chatId"); + } + if (arguments.TryGetValue("__userId", out var uid)) { + long.TryParse(uid, out userId); + arguments.Remove("__userId"); + } + if (arguments.TryGetValue("__messageId", out var mid)) { + long.TryParse(mid, out messageId); + arguments.Remove("__messageId"); + } + + if (chatId == 0) { + throw new InvalidOperationException("Sandboxed tool execution requires a valid __chatId context value."); + } + + return await sandboxService.ExecuteToolAsync(toolName, arguments, chatId, userId, messageId); + }, + allowLocalOverride: true); + } + } +} diff --git a/TelegramSearchBot/Service/Scheduler/ModelCatalogRefreshTask.cs b/TelegramSearchBot/Service/Scheduler/ModelCatalogRefreshTask.cs new file mode 100644 index 00000000..68787026 --- /dev/null +++ b/TelegramSearchBot/Service/Scheduler/ModelCatalogRefreshTask.cs @@ -0,0 +1,50 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Interface.Manage; + +namespace TelegramSearchBot.Service.Scheduler { + /// + /// 定期刷新所有渠道的模型目录与能力信息,让渠道模型表跟随供应商最新状态, + /// 不再依赖预设里写死的历史快照。与手动指令「刷新所有渠道」共用同一实现。 + /// + [Injectable(ServiceLifetime.Transient)] + public class ModelCatalogRefreshTask : IScheduledTask { + public string TaskName => "ModelCatalogRefresh"; + + /// 每 6 小时刷新一次:兼顾模型表时效与供应商目录 API 压力。 + public string CronExpression => "0 */6 * * *"; + + private readonly IServiceProvider _serviceProvider; + private readonly ILogger _logger; + private Func _heartbeatCallback; + + public ModelCatalogRefreshTask(IServiceProvider serviceProvider, ILogger logger) { + _serviceProvider = serviceProvider; + _logger = logger; + } + + public void SetHeartbeatCallback(Func heartbeatCallback) { + _heartbeatCallback = heartbeatCallback; + } + + public async Task ExecuteAsync() { + await HeartbeatAsync(); + try { + using var scope = _serviceProvider.CreateScope(); + var helper = scope.ServiceProvider.GetRequiredService(); + var count = await helper.RefreshAllChannel(); + _logger.LogInformation("模型目录刷新完成,新增/恢复 {Count} 个模型", count); + } catch (Exception ex) { + _logger.LogError(ex, "模型目录刷新失败"); + throw; + } finally { + await HeartbeatAsync(); + } + } + + private Task HeartbeatAsync() => _heartbeatCallback?.Invoke() ?? Task.CompletedTask; + } +}