From de9afe63eff88e6c3a21ebfdc68740dee84d5704 Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Wed, 23 Sep 2026 21:46:29 +0800 Subject: [PATCH] feat(llm): align built-in presets with pi provider catalogs Add the remaining pi presets and Mistral, Azure, Vertex, Bedrock, and Radius transports, and keep local specs drafts out of git. Co-authored-by: Cursor --- .gitignore | 9 +- Docs/Bot_Commands_User_Guide.md | 2 +- .../Model/AI/LLMProvider.cs | 29 +- .../Model/AI/LlmProtocol.cs | 7 +- .../AI/LLM/LlmProviderCatalogLoaderTests.cs | 2 +- .../AI/LLM/PiProviderAlignmentTests.cs | 157 ++++ .../Providers/providers.json | 757 +++++++++++++++++- .../Service/AI/LLM/LlmPresetEndpoints.cs | 74 ++ .../Service/AI/LLM/LlmProviderCatalog.cs | 19 +- .../Service/AI/LLM/LlmProviderRegistry.cs | 21 + .../AI/LLM/ProviderApi/FixedModelCatalog.cs | 38 + .../AI/LLM/Transports/AwsEventStream.cs | 141 ++++ .../LLM/Transports/AzureResponsesTransport.cs | 41 + .../AI/LLM/Transports/BedrockTransport.cs | 183 +++++ .../AI/LLM/Transports/MistralTransport.cs | 178 ++++ .../AI/LLM/Transports/PiMessagesTransport.cs | 182 +++++ .../AI/LLM/Transports/VertexTransport.cs | 165 ++++ TelegramSearchBot.LLMAgent/LLMAgentProgram.cs | 1 + .../Manage/EditLLMConfTest.cs | 39 + TelegramSearchBot/Model/AI/LLMConfState.cs | 9 + .../Service/Manage/EditLLMConfHelper.cs | 23 +- .../Service/Manage/EditLLMConfService.cs | 79 +- 22 files changed, 2108 insertions(+), 48 deletions(-) create mode 100644 TelegramSearchBot.LLM.Test/Service/AI/LLM/PiProviderAlignmentTests.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/LlmPresetEndpoints.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/FixedModelCatalog.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/AwsEventStream.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/AzureResponsesTransport.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/BedrockTransport.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/MistralTransport.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/PiMessagesTransport.cs create mode 100644 TelegramSearchBot.LLM/Service/AI/LLM/Transports/VertexTransport.cs diff --git a/.gitignore b/.gitignore index fa86a60b..2b4b4edd 100644 --- a/.gitignore +++ b/.gitignore @@ -366,6 +366,9 @@ external/moder-update/**/target/ # Moder.Update C# build artifacts external/moder-update/**/bin/ external/moder-update/**/obj/ - -# pi agent session state -.pi/ + +# pi agent session state +.pi/ + +# Local requirement drafts (requirements / design / tasks). Keep on disk, do not commit. +/specs/ diff --git a/Docs/Bot_Commands_User_Guide.md b/Docs/Bot_Commands_User_Guide.md index 0b9797c3..cc05d76a 100644 --- a/Docs/Bot_Commands_User_Guide.md +++ b/Docs/Bot_Commands_User_Guide.md @@ -147,7 +147,7 @@ * **LLM渠道配置** (在群组中由全局管理员发送,会触发交互式配置流程) * **说明**: 以下指令用于管理LLM API渠道。它们会启动一个多步骤的对话流程(状态机),机器人会依次询问配置所需的各项信息。请按照机器人的提示逐步回复。 - * **预制渠道**:从内置 Provider 预设目录一步创建渠道(Anthropic、OpenAI Chat/Responses、Gemini、MiniMax、Ollama、LM Studio、DeepSeek、Moonshot 等常见提供商已预置默认地址与默认模型列表)。交互流程: + * **预制渠道**:从内置 Provider 预设目录一步创建渠道。除原有 Anthropic、OpenAI Chat/Responses、Gemini、MiniMax、Ollama、LM Studio、DeepSeek、Moonshot、OpenCode Zen/Go 外,还包含 pi 对齐的 Groq、Cerebras、xAI(Responses)、OpenRouter、Together、Fireworks、Hugging Face、NVIDIA、Baseten、智谱 ZAI、通义 Token Plan、小米 MiMo、MiniMax 国际/中国 Anthropic 端点、Moonshot 国际、Kimi For Coding、GitHub Copilot、Mistral、Azure OpenAI Responses、Google Vertex、Amazon Bedrock、Radius、Vercel AI Gateway、Cloudflare Workers AI / AI Gateway、Ant Ling。网关需要手填的有:Cloudflare 两家、Azure(资源名或根地址)、Vertex(project 与 location)、Bedrock(region 与 bearer token)。目录超过 30 个模型的预设不自动插入模型行。交互流程: 1. 管理员发送: `预制渠道` 2. 机器人回复: 预设列表(含默认地址与默认模型) 3. 管理员发送预设编号 diff --git a/TelegramSearchBot.Common/Model/AI/LLMProvider.cs b/TelegramSearchBot.Common/Model/AI/LLMProvider.cs index 7a8dfe8f..20836857 100644 --- a/TelegramSearchBot.Common/Model/AI/LLMProvider.cs +++ b/TelegramSearchBot.Common/Model/AI/LLMProvider.cs @@ -1,12 +1,17 @@ -namespace TelegramSearchBot.Model.AI { - public enum LLMProvider { - None, - OpenAI, - Ollama, - Gemini, - MiniMax = 4, - LMStudio = 5, - Anthropic = 6, - ResponsesAPI = 7 - } -} +namespace TelegramSearchBot.Model.AI { + public enum LLMProvider { + None, + OpenAI, + Ollama, + Gemini, + MiniMax = 4, + LMStudio = 5, + Anthropic = 6, + ResponsesAPI = 7, + Mistral = 8, + AzureOpenAI = 9, + Vertex = 10, + Bedrock = 11, + Radius = 12 + } +} diff --git a/TelegramSearchBot.Common/Model/AI/LlmProtocol.cs b/TelegramSearchBot.Common/Model/AI/LlmProtocol.cs index 3cc83340..fd074391 100644 --- a/TelegramSearchBot.Common/Model/AI/LlmProtocol.cs +++ b/TelegramSearchBot.Common/Model/AI/LlmProtocol.cs @@ -8,6 +8,11 @@ public enum LlmProtocol { OpenAIResponses = 1, AnthropicMessages = 2, Ollama = 3, - Gemini = 4 + Gemini = 4, + MistralConversations = 5, + AzureOpenAIResponses = 6, + GoogleVertex = 7, + BedrockConverse = 8, + PiMessages = 9 } } diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmProviderCatalogLoaderTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmProviderCatalogLoaderTests.cs index 522b0b8c..f814389f 100644 --- a/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmProviderCatalogLoaderTests.cs +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/LlmProviderCatalogLoaderTests.cs @@ -22,7 +22,7 @@ public void BuiltInDocument_LoadsAllPresets_WhenOverrideMissing() { Assert.Equal("builtin", doc.Source); Assert.Contains("不存在", doc.Warning); - Assert.Equal(11, doc.Presets.Count); + Assert.Equal(43, doc.Presets.Count); Assert.NotNull(doc.GeneratedAt); Assert.NotNull(doc.Presets.FirstOrDefault(p => p.Id == "opencode-go")); } diff --git a/TelegramSearchBot.LLM.Test/Service/AI/LLM/PiProviderAlignmentTests.cs b/TelegramSearchBot.LLM.Test/Service/AI/LLM/PiProviderAlignmentTests.cs new file mode 100644 index 00000000..42c7e3b5 --- /dev/null +++ b/TelegramSearchBot.LLM.Test/Service/AI/LLM/PiProviderAlignmentTests.cs @@ -0,0 +1,157 @@ +using System.Net; +using System.Net.Http.Headers; +using System.Text; +using Microsoft.Extensions.Logging; +using Moq; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; +using TelegramSearchBot.Service.AI.LLM; +using TelegramSearchBot.Service.AI.LLM.Transports; +using Xunit; + +namespace TelegramSearchBot.LLM.Test.Service.AI.LLM { + public class PiProviderAlignmentTests { + [Fact] + public void BuiltInCatalog_ContainsPiProviders() { + Assert.Equal("https://api.groq.com/openai/v1", LlmProviderCatalog.FindById("groq")!.DefaultGateway); + Assert.Equal(LLMProvider.ResponsesAPI, LlmProviderCatalog.FindById("xai")!.Provider); + Assert.Equal(LLMProvider.Anthropic, LlmProviderCatalog.FindById("vercel-ai-gateway")!.Provider); + Assert.Equal(LLMProvider.Anthropic, LlmProviderCatalog.FindById("minimax-global")!.Provider); + Assert.Equal("https://api.minimax.io/anthropic", LlmProviderCatalog.FindById("minimax-global")!.DefaultGateway); + Assert.Equal("https://api.minimaxi.com/v1", LlmProviderCatalog.FindById("minimax")!.DefaultGateway); + Assert.Equal("vertex", LlmProviderCatalog.FindById("google-vertex")!.Setup); + Assert.Equal("bedrock", LlmProviderCatalog.FindById("amazon-bedrock")!.Setup); + Assert.Equal("azure", LlmProviderCatalog.FindById("azure-openai-responses")!.Setup); + Assert.Empty(LlmProviderCatalog.FindById("openrouter")!.DefaultModels); + Assert.Equal(LLMProvider.ResponsesAPI, LlmProviderCatalog.FindById("github-copilot")!.Provider); + Assert.Contains(LlmProviderCatalog.FindById("github-copilot")!.Bindings!, b => b.Id == "anthropic" && b.Protocol == LlmProtocol.AnthropicMessages); + Assert.Contains(LlmProviderCatalog.Presets, p => p.Id == "opencode-go"); + } + + [Theory] + [InlineData("my-resource", "https://my-resource.openai.azure.com/openai/v1")] + [InlineData("https://demo.openai.azure.com", "https://demo.openai.azure.com/openai/v1")] + [InlineData("https://demo.cognitiveservices.azure.com/openai/v1", "https://demo.cognitiveservices.azure.com/openai/v1")] + [InlineData("https://demo.ai.azure.com/openai/v1/responses", "https://demo.ai.azure.com/openai/v1")] + public void NormalizeAzure_CollapsesResourceRoots(string input, string expected) { + Assert.Equal(expected, LlmPresetEndpoints.NormalizeAzure(input)); + } + + [Fact] + public void AzureCreate_StoresNormalizedEndpoint() { + var channel = new LLMChannel { Provider = LLMProvider.AzureOpenAI, Gateway = "my-resource", ApiKey = "k" }; + var binding = new LLMApiBinding { Endpoint = "my-resource", Protocol = LlmProtocol.AzureOpenAIResponses, AuthProfile = LlmAuthProfile.Bearer }; + var bundle = AzureResponsesTransport.Create(channel, binding, "gpt-4o", false, false, + Mock.Of(), new ThrowingHttpClientFactory()); + + Assert.Equal("https://my-resource.openai.azure.com/openai/v1", bundle.Config.Endpoint); + Assert.Equal(LlmProtocol.AzureOpenAIResponses, bundle.Config.Protocol); + } + + [Fact] + public async Task AzureApiKeyHandler_MovesBearerToApiKeyHeader() { + var inner = new RecordingHandler(new StringContent("ok")); + var client = new HttpClient(new AzureApiKeyHandler { InnerHandler = inner }); + var request = new HttpRequestMessage(HttpMethod.Post, "https://example.com/openai/v1/responses"); + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "secret"); + + await client.SendAsync(request); + + Assert.Equal("secret", inner.Request!.Headers.GetValues("api-key").Single()); + Assert.Null(inner.Request.Headers.Authorization); + } + + [Fact] + public async Task MistralTransport_PostsChatCompletionsAndReadsText() { + var sse = "data: {\"choices\":[{\"delta\":{\"content\":\"hi\"}}]}\n\ndata: [DONE]\n"; + var handler = Client(sse); + var (events, request, body) = await Stream(new MistralTransport(new HttpClient(handler), "https://api.mistral.ai", "key"), handler, "mistral-small"); + Assert.Equal("https://api.mistral.ai/v1/chat/completions", request.RequestUri!.ToString()); + Assert.Equal("Bearer", request.Headers.Authorization!.Scheme); + Assert.Contains("mistral-small", body); + Assert.Contains("hello", body); + Assert.Contains(events, e => e is LlmStreamEvent.TextDelta delta && delta.Delta == "hi"); + } + + [Fact] + public async Task VertexTransport_PostsStreamGenerateContent() { + var sse = "data: {\"candidates\":[{\"content\":{\"parts\":[{\"text\":\"hi\"}]}}]}\n\n"; + var handler = Client(sse); + var (events, request, body) = await Stream(new VertexTransport(new HttpClient(handler), "https://us-central1-aiplatform.googleapis.com/v1/projects/p/locations/us-central1", "vk"), handler, "gemini-2.5-flash"); + Assert.Contains(":streamGenerateContent", request.RequestUri!.ToString()); + Assert.Equal("vk", request.Headers.GetValues("x-goog-api-key").Single()); + Assert.Contains("hello", body); + Assert.Contains(events, e => e is LlmStreamEvent.TextDelta delta && delta.Delta == "hi"); + } + + [Fact] + public async Task BedrockTransport_PostsConverseStream() { + var frame = AwsEventStream.Encode("contentBlockDelta", """{"contentBlockIndex":0,"delta":{"text":"hi"}}"""); + var handler = Client(frame); + var (events, request, body) = await Stream(new BedrockTransport(new HttpClient(handler), "https://bedrock-runtime.us-east-1.amazonaws.com", "bearer"), handler, "amazon.nova-lite-v1:0"); + Assert.EndsWith("/converse-stream", request.RequestUri!.AbsolutePath); + Assert.Contains("nova-lite", request.RequestUri.AbsolutePath); + Assert.Equal("Bearer", request.Headers.Authorization!.Scheme); + Assert.Contains("hello", body); + Assert.Contains(events, e => e is LlmStreamEvent.TextDelta delta && delta.Delta == "hi"); + } + + [Fact] + public async Task PiMessagesTransport_PostsMessages() { + var sse = "data: {\"type\":\"text_delta\",\"contentIndex\":0,\"delta\":\"hi\"}\n\n"; + var handler = Client(sse); + var (events, request, body) = await Stream(new PiMessagesTransport(new HttpClient(handler), "https://radius.pi.dev", "rk"), handler, "balanced"); + Assert.Equal("https://radius.pi.dev/messages", request.RequestUri!.ToString()); + Assert.Equal("Bearer", request.Headers.Authorization!.Scheme); + Assert.Contains("balanced", body); + Assert.Contains("hello", body); + Assert.Contains(events, e => e is LlmStreamEvent.TextDelta delta && delta.Delta == "hi"); + } + + [Fact] + public void AwsEventStream_RoundTripsEventType() { + var encoded = AwsEventStream.Encode("contentBlockDelta", """{"delta":{"text":"hi"}}"""); + var message = Assert.Single(AwsEventStream.Decode(encoded)); + Assert.Equal("contentBlockDelta", message.EventType); + Assert.Contains("hi", message.Payload); + } + + private static async Task<(List Events, HttpRequestMessage Request, string Body)> Stream(ILlmTransport transport, RecordingHandler handler, string model) { + var events = new List(); + await foreach (var evt in transport.StreamTurnAsync(new LlmTurnRequest { + SystemPrompt = "sys", + History = [LlmMessage.User("hello")], + Config = new LlmTransportConfig { ModelName = model } + })) { + events.Add(evt); + } + return (events, handler.Request!, handler.Body!); + } + + private static RecordingHandler Client(string sse) => + new(new StringContent(sse, Encoding.UTF8, "text/event-stream")); + + private static RecordingHandler Client(byte[] body) => + new(new ByteArrayContent(body)); + + private sealed class ThrowingHttpClientFactory : IHttpClientFactory { + public HttpClient CreateClient(string name) => throw new InvalidOperationException(); + } + + internal sealed class RecordingHandler : HttpMessageHandler { + private readonly HttpContent _response; + public HttpRequestMessage? Request { get; private set; } + public string? Body { get; private set; } + + public RecordingHandler(HttpContent response) { + _response = response; + } + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + Request = request; + Body = request.Content == null ? null : await request.Content.ReadAsStringAsync(cancellationToken); + return new HttpResponseMessage(HttpStatusCode.OK) { Content = _response }; + } + } + } +} diff --git a/TelegramSearchBot.LLM/Providers/providers.json b/TelegramSearchBot.LLM/Providers/providers.json index b9114b4b..82f7ea4e 100644 --- a/TelegramSearchBot.LLM/Providers/providers.json +++ b/TelegramSearchBot.LLM/Providers/providers.json @@ -1,12 +1,16 @@ { - "generatedAt": "2026-09-16T00:00:00Z", + "generatedAt": "2026-09-23T00:00:00Z", "presets": [ { "id": "anthropic", "displayName": "Anthropic 官方", "provider": "Anthropic", "defaultGateway": "https://api.anthropic.com", - "defaultModels": ["claude-opus-5", "claude-sonnet-5", "claude-haiku-4-5-20251001"], + "defaultModels": [ + "claude-opus-5", + "claude-sonnet-5", + "claude-haiku-4-5-20251001" + ], "requiresApiKey": true }, { @@ -14,7 +18,11 @@ "displayName": "OpenAI 官方 (Chat Completions)", "provider": "OpenAI", "defaultGateway": "https://api.openai.com/v1", - "defaultModels": ["gpt-6-astra", "gpt-5.6-sol", "gpt-5.6-terra"], + "defaultModels": [ + "gpt-6-astra", + "gpt-5.6-sol", + "gpt-5.6-terra" + ], "requiresApiKey": true }, { @@ -22,7 +30,10 @@ "displayName": "OpenAI 官方 (Responses API)", "provider": "ResponsesAPI", "defaultGateway": "https://api.openai.com/v1", - "defaultModels": ["gpt-6-astra", "gpt-5.6-sol"], + "defaultModels": [ + "gpt-6-astra", + "gpt-5.6-sol" + ], "requiresApiKey": true }, { @@ -30,7 +41,11 @@ "displayName": "Google Gemini", "provider": "Gemini", "defaultGateway": "https://generativelanguage.googleapis.com", - "defaultModels": ["gemini-3.8-flash", "gemini-3.1-pro-preview", "gemini-3.5-flash"], + "defaultModels": [ + "gemini-3.8-flash", + "gemini-3.1-pro-preview", + "gemini-3.5-flash" + ], "requiresApiKey": true }, { @@ -38,7 +53,10 @@ "displayName": "MiniMax", "provider": "MiniMax", "defaultGateway": "https://api.minimaxi.com/v1", - "defaultModels": ["MiniMax-M3", "MiniMax-M2.7"], + "defaultModels": [ + "MiniMax-M3", + "MiniMax-M2.7" + ], "requiresApiKey": true }, { @@ -64,7 +82,10 @@ "displayName": "DeepSeek (OpenAI 兼容)", "provider": "OpenAI", "defaultGateway": "https://api.deepseek.com/v1", - "defaultModels": ["deepseek-flash", "deepseek-v4-pro"], + "defaultModels": [ + "deepseek-flash", + "deepseek-v4-pro" + ], "requiresApiKey": true }, { @@ -72,7 +93,10 @@ "displayName": "Moonshot Kimi (OpenAI 兼容)", "provider": "OpenAI", "defaultGateway": "https://api.moonshot.cn/v1", - "defaultModels": ["kimi-k3", "kimi-k2.7-code"], + "defaultModels": [ + "kimi-k3", + "kimi-k2.7-code" + ], "requiresApiKey": true }, { @@ -84,14 +108,47 @@ "requiresApiKey": true, "notes": "多协议网关(claude-*→Anthropic、gpt-*/grok-*→Responses、gemini-*→Google、其余→Chat Completions);按量计费,目录不自动创建,授权模型请用 `添加模型`。", "bindings": [ - { "id": "anthropic", "protocol": "AnthropicMessages", "authProfile": "AnthropicApiKey", "endpointSuffix": "/v1" }, - { "id": "responses", "protocol": "OpenAIResponses", "authProfile": "Bearer", "endpointSuffix": "/v1" }, - { "id": "google", "protocol": "Gemini", "authProfile": "Bearer", "endpointSuffix": "/v1" } + { + "id": "anthropic", + "protocol": "AnthropicMessages", + "authProfile": "AnthropicApiKey", + "endpointSuffix": "/v1" + }, + { + "id": "responses", + "protocol": "OpenAIResponses", + "authProfile": "Bearer", + "endpointSuffix": "/v1" + }, + { + "id": "google", + "protocol": "Gemini", + "authProfile": "Bearer", + "endpointSuffix": "/v1" + } ], "modelBindingRules": [ - { "bindingId": "anthropic", "prefixes": ["claude-"] }, - { "bindingId": "responses", "prefixes": ["gpt-", "grok-", "o3", "o4"] }, - { "bindingId": "google", "prefixes": ["gemini-"] } + { + "bindingId": "anthropic", + "prefixes": [ + "claude-" + ] + }, + { + "bindingId": "responses", + "prefixes": [ + "gpt-", + "grok-", + "o3", + "o4" + ] + }, + { + "bindingId": "google", + "prefixes": [ + "gemini-" + ] + } ] }, { @@ -100,26 +157,676 @@ "provider": "OpenAI", "defaultGateway": "https://opencode.ai/zen/go/v1", "defaultModels": [ - "grok-4.6", "gpt-5.6-luna", - "glm-5.3-flash", "glm-5.3", "glm-5.2", "glm-5.1", - "kimi-k3", "kimi-k2.7-code", "kimi-k2.6", + "grok-4.6", + "gpt-5.6-luna", + "glm-5.3-flash", + "glm-5.3", + "glm-5.2", + "glm-5.1", + "kimi-k3", + "kimi-k2.7-code", + "kimi-k2.6", "longcat-2.0", - "deepseek-v4.1-flash", "deepseek-v4-pro", "deepseek-v4-flash", "deepseek-v4-flash-vision-exp", - "minimax-m3", "minimax-m2.7", - "mimo-v2.5", "mimo-v2.5-pro", - "qwen3.8-max", "qwen3.8-flash", "qwen3.7-max", "qwen3.7-plus", "qwen3.6-plus", - "muse-spark-1.3-contributor", "muse-spark-1.2-contributor", - "hy4-preview", "hy3" + "deepseek-v4.1-flash", + "deepseek-v4-pro", + "deepseek-v4-flash", + "deepseek-v4-flash-vision-exp", + "minimax-m3", + "minimax-m2.7", + "mimo-v2.5", + "mimo-v2.5-pro", + "qwen3.8-max", + "qwen3.8-flash", + "qwen3.7-max", + "qwen3.7-plus", + "qwen3.6-plus", + "muse-spark-1.3-contributor", + "muse-spark-1.2-contributor", + "hy4-preview", + "hy3" ], "requiresApiKey": true, "notes": "OpenAI 兼容订阅网关(/responses + /chat/completions),自动携带 x-opencode-session;订阅覆盖目录模型,刷新会同步目录。自建网关请用 `新建渠道`。", "bindings": [ - { "id": "responses", "protocol": "OpenAIResponses", "authProfile": "Bearer", "endpointSuffix": "/v1" } + { + "id": "responses", + "protocol": "OpenAIResponses", + "authProfile": "Bearer", + "endpointSuffix": "/v1" + } ], "modelBindingRules": [ - { "bindingId": "responses", "prefixes": ["grok-", "gpt-"] } + { + "bindingId": "responses", + "prefixes": [ + "grok-", + "gpt-" + ] + } ], "catalogIsEntitlement": true + }, + { + "id": "ant-ling", + "displayName": "Ant Ling", + "provider": "OpenAI", + "defaultGateway": "https://api.ant-ling.com/v1", + "defaultModels": [ + "Ling-2.6-1T", + "Ling-2.6-flash", + "Ring-2.6-1T" + ], + "requiresApiKey": true + }, + { + "id": "cerebras", + "displayName": "Cerebras", + "provider": "OpenAI", + "defaultGateway": "https://api.cerebras.ai/v1", + "defaultModels": [ + "gpt-oss-120b", + "qwen-3.8-27b" + ], + "requiresApiKey": true + }, + { + "id": "groq", + "displayName": "Groq", + "provider": "OpenAI", + "defaultGateway": "https://api.groq.com/openai/v1", + "defaultModels": [ + "llama-3.1-8b-instant", + "llama-3.3-70b-versatile", + "openai/gpt-oss-120b", + "openai/gpt-oss-20b", + "openai/gpt-oss-safeguard-20b", + "qwen/qwen3.6-27b", + "qwen/qwen3.8-27b" + ], + "requiresApiKey": true + }, + { + "id": "huggingface", + "displayName": "Hugging Face", + "provider": "OpenAI", + "defaultGateway": "https://router.huggingface.co/v1", + "defaultModels": [], + "requiresApiKey": true, + "notes": "模型目录超过 30 个,创建后用添加模型或刷新维护。" + }, + { + "id": "nvidia", + "displayName": "NVIDIA NIM", + "provider": "OpenAI", + "defaultGateway": "https://integrate.api.nvidia.com/v1", + "defaultModels": [ + "google/gemma-3-12b-it", + "google/gemma-3-4b-it", + "meta/llama-3.2-11b-vision-instruct", + "meta/llama-3.2-90b-vision-instruct", + "meta/muse-glimmer-30b", + "mistralai/mistral-7b-instruct-v0.3", + "moonshotai/kimi-k2.6", + "moonshotai/kimi-k3", + "nvidia/cosmos-reason2-8b", + "nvidia/llama-3.1-nemotron-70b-instruct", + "nvidia/llama-3.1-nemotron-ultra-253b-v1", + "nvidia/nemotron-3-nano-omni-30b-a3b-reasoning", + "nvidia/nemotron-3-super-120b-a12b", + "nvidia/nemotron-3-ultra-550b-a55b", + "nvidia/nemotron-3.5-lightning-30b-a3b", + "openai/gpt-oss-20b", + "poolside/laguna-xs-2.1", + "z-ai/glm-5.3", + "z-ai/glm-5.3-flash" + ], + "requiresApiKey": true + }, + { + "id": "together", + "displayName": "Together AI", + "provider": "OpenAI", + "defaultGateway": "https://api.together.ai/v1", + "defaultModels": [ + "MiniMaxAI/MiniMax-M2.7", + "MiniMaxAI/MiniMax-M3", + "Qwen/Qwen2.5-7B-Instruct-Turbo", + "Qwen/Qwen3.5-9B", + "Qwen/Qwen3.6-Plus", + "Qwen/Qwen3.7-Max", + "deepseek-ai/DeepSeek-V4-Flash-0731", + "deepseek-ai/DeepSeek-V4-Pro", + "deepseek-ai/DeepSeek-V4-Pro-0813", + "deepseek-ai/DeepSeek-V4.1-Flash", + "google/gemma-4-31B-it", + "meta-llama/Llama-3.3-70B-Instruct-Turbo", + "moonshotai/Kimi-K2.6", + "moonshotai/Kimi-K2.7-Code", + "moonshotai/Kimi-K3", + "nvidia/nemotron-3-ultra-550b-a55b", + "openai/gpt-oss-120b", + "openai/gpt-oss-20b", + "thinkingmachines/Inkling", + "zai-org/GLM-5.2", + "zai-org/GLM-5.3", + "zai-org/GLM-5.3-Flash" + ], + "requiresApiKey": true + }, + { + "id": "openrouter", + "displayName": "OpenRouter", + "provider": "OpenAI", + "defaultGateway": "https://openrouter.ai/api/v1", + "defaultModels": [], + "requiresApiKey": true, + "notes": "目录很大,不自动灌入模型。anthropic/ 前缀走 Anthropic,其余走 Chat Completions。", + "bindings": [ + { + "id": "anthropic", + "protocol": "AnthropicMessages", + "authProfile": "AnthropicApiKey", + "endpointSuffix": "" + } + ], + "modelBindingRules": [ + { + "bindingId": "anthropic", + "prefixes": [ + "anthropic/" + ] + } + ] + }, + { + "id": "vercel-ai-gateway", + "displayName": "Vercel AI Gateway", + "provider": "Anthropic", + "defaultGateway": "https://ai-gateway.vercel.sh", + "defaultModels": [], + "requiresApiKey": true, + "notes": "pi 当前目录全部走 Anthropic Messages。目录超过 30 个,创建后手工添加模型。" + }, + { + "id": "xai", + "displayName": "xAI", + "provider": "ResponsesAPI", + "defaultGateway": "https://api.x.ai/v1", + "defaultModels": [ + "grok-4.3", + "grok-4.5", + "grok-4.6", + "grok-4.7" + ], + "requiresApiKey": true, + "notes": "pi 的 xAI 模型走 OpenAI Responses,不是 Chat Completions。" + }, + { + "id": "xiaomi", + "displayName": "Xiaomi MiMo", + "provider": "OpenAI", + "defaultGateway": "https://api.xiaomimimo.com/v1", + "defaultModels": [ + "mimo-v2.5", + "mimo-v2.5-pro", + "mimo-v2.5-pro-ultraspeed", + "mimo-v2.6-flash", + "mimo-v2.6-pro", + "mimo-v2.6-pro-ultraspeed" + ], + "requiresApiKey": true + }, + { + "id": "xiaomi-token-plan-cn", + "displayName": "Xiaomi MiMo Token Plan(中国)", + "provider": "OpenAI", + "defaultGateway": "https://token-plan-cn.xiaomimimo.com/v1", + "defaultModels": [ + "mimo-v2.5", + "mimo-v2.5-pro", + "mimo-v2.6-flash", + "mimo-v2.6-pro" + ], + "requiresApiKey": true + }, + { + "id": "xiaomi-token-plan-ams", + "displayName": "Xiaomi MiMo Token Plan(阿姆斯特丹)", + "provider": "OpenAI", + "defaultGateway": "https://token-plan-ams.xiaomimimo.com/v1", + "defaultModels": [ + "mimo-v2.5", + "mimo-v2.5-pro", + "mimo-v2.6-flash", + "mimo-v2.6-pro" + ], + "requiresApiKey": true + }, + { + "id": "xiaomi-token-plan-sgp", + "displayName": "Xiaomi MiMo Token Plan(新加坡)", + "provider": "OpenAI", + "defaultGateway": "https://token-plan-sgp.xiaomimimo.com/v1", + "defaultModels": [ + "mimo-v2.5", + "mimo-v2.5-pro", + "mimo-v2.6-flash", + "mimo-v2.6-pro" + ], + "requiresApiKey": true + }, + { + "id": "qwen-token-plan", + "displayName": "Qwen Token Plan", + "provider": "OpenAI", + "defaultGateway": "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", + "defaultModels": [ + "MiniMax-M2.5", + "deepseek-v3.2", + "deepseek-v4-flash", + "deepseek-v4-flash-0731", + "deepseek-v4-pro", + "deepseek-v4-pro-0813", + "deepseek-v4.1-flash", + "glm-5", + "glm-5.1", + "glm-5.2", + "glm-5.3", + "kimi-k2.5", + "kimi-k2.6", + "kimi-k2.7-code", + "qwen3.6-flash", + "qwen3.6-plus", + "qwen3.7-max", + "qwen3.7-plus", + "qwen3.8-flash", + "qwen3.8-max" + ], + "requiresApiKey": true + }, + { + "id": "qwen-token-plan-individual", + "displayName": "Qwen Token Plan Individual", + "provider": "OpenAI", + "defaultGateway": "https://token-plan.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1", + "defaultModels": [ + "deepseek-v4-flash-0731", + "deepseek-v4-pro", + "deepseek-v4-pro-0813", + "glm-5.2", + "qwen3.6-flash", + "qwen3.7-max", + "qwen3.7-plus", + "qwen3.8-flash", + "qwen3.8-max" + ], + "requiresApiKey": true, + "notes": "与 Qwen Token Plan 同一网关,只预置 Individual 订阅模型。" + }, + { + "id": "qwen-token-plan-cn", + "displayName": "Qwen Token Plan(中国)", + "provider": "OpenAI", + "defaultGateway": "https://token-plan.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", + "defaultModels": [ + "MiniMax-M2.5", + "deepseek-v3.2", + "deepseek-v4-flash", + "deepseek-v4-flash-0731", + "deepseek-v4-pro", + "deepseek-v4-pro-0813", + "deepseek-v4.1-flash", + "glm-5", + "glm-5.1", + "glm-5.2", + "glm-5.3", + "kimi-k2.5", + "kimi-k2.6", + "kimi-k2.7-code", + "qwen3.6-flash", + "qwen3.6-plus", + "qwen3.7-max", + "qwen3.7-plus", + "qwen3.8-flash", + "qwen3.8-max" + ], + "requiresApiKey": true + }, + { + "id": "zai", + "displayName": "ZAI Coding Plan", + "provider": "OpenAI", + "defaultGateway": "https://api.z.ai/api/coding/paas/v4", + "defaultModels": [ + "glm-4.7", + "glm-5-turbo", + "glm-5.2", + "glm-5.2-highspeed", + "glm-5.3", + "glm-5.3-flash", + "glm-5.3-highspeed" + ], + "requiresApiKey": true + }, + { + "id": "zai-coding-cn", + "displayName": "ZAI Coding Plan(中国)", + "provider": "OpenAI", + "defaultGateway": "https://open.bigmodel.cn/api/coding/paas/v4", + "defaultModels": [ + "glm-4.6v", + "glm-5.3", + "glm-5.3-flash", + "glm-5.3-highspeed" + ], + "requiresApiKey": true + }, + { + "id": "baseten", + "displayName": "Baseten", + "provider": "OpenAI", + "defaultGateway": "https://inference.baseten.co/v1", + "defaultModels": [ + "deepseek-ai/DeepSeek-V4-Flash-0731", + "deepseek-ai/DeepSeek-V4-Pro", + "deepseek-ai/DeepSeek-V4-Pro-0813", + "deepseek-ai/DeepSeek-V4.1-Flash", + "moonshotai/Kimi-K2.5", + "moonshotai/Kimi-K2.6", + "moonshotai/Kimi-K2.7-Code", + "moonshotai/Kimi-K3", + "nvidia/NVIDIA-Nemotron-3-Ultra-550B-A55B", + "nvidia/Nemotron-120B-A12B", + "openai/gpt-oss-120b", + "thinkingmachines/inkling", + "thinkingmachines/inkling-small", + "zai-org/GLM-4.7", + "zai-org/GLM-5", + "zai-org/GLM-5.1", + "zai-org/GLM-5.2", + "zai-org/GLM-5.2-Fast", + "zai-org/GLM-5.3", + "zai-org/GLM-5.3-Fast", + "zai-org/GLM-5.3-Flash" + ], + "requiresApiKey": true + }, + { + "id": "cloudflare-workers-ai", + "displayName": "Cloudflare Workers AI", + "provider": "OpenAI", + "defaultGateway": null, + "defaultModels": [ + "@cf/deepseek-ai/deepseek-v4-flash-0731", + "@cf/deepseek-ai/deepseek-v4-pro-0813", + "@cf/google/gemma-4-26b-a4b-it", + "@cf/ibm-granite/granite-4.0-h-micro", + "@cf/meta/llama-3.3-70b-instruct-fp8-fast", + "@cf/meta/llama-4-scout-17b-16e-instruct", + "@cf/mistralai/mistral-small-3.1-24b-instruct", + "@cf/moonshotai/kimi-k2.6", + "@cf/moonshotai/kimi-k2.7-code", + "@cf/nvidia/nemotron-3-120b-a12b", + "@cf/openai/gpt-oss-120b", + "@cf/openai/gpt-oss-20b", + "@cf/qwen/qwen3-30b-a3b-fp8", + "@cf/qwen/qwen3.8-27b", + "@cf/zai-org/glm-4.7-flash", + "@cf/zai-org/glm-5.2", + "@cf/zai-org/glm-5.3", + "@cf/zai-org/glm-5.3-flash" + ], + "requiresApiKey": true, + "notes": "创建时输入完整网关(路径含 account id)。" + }, + { + "id": "fireworks", + "displayName": "Fireworks", + "provider": "Anthropic", + "defaultGateway": "https://api.fireworks.ai/inference", + "defaultModels": [], + "requiresApiKey": true, + "notes": "默认 Anthropic Messages。glm/kimi 模型走 Chat Completions binding。目录 33 个,超过 30 不预置。", + "bindings": [ + { + "id": "chat", + "protocol": "OpenAIChat", + "authProfile": "Bearer", + "endpointSuffix": "/v1" + } + ], + "modelBindingRules": [ + { + "bindingId": "chat", + "prefixes": [ + "accounts/fireworks/models/glm", + "accounts/fireworks/models/kimi", + "accounts/fireworks/routers/glm" + ] + } + ] + }, + { + "id": "cloudflare-ai-gateway", + "displayName": "Cloudflare AI Gateway", + "provider": "OpenAI", + "defaultGateway": null, + "defaultModels": [], + "requiresApiKey": true, + "notes": "创建时输入完整网关(含 account id 与 gateway id)。claude-* 走 Anthropic,gpt-* 走 Responses,workers-ai/ 走 Chat。", + "bindings": [ + { + "id": "anthropic", + "protocol": "AnthropicMessages", + "authProfile": "AnthropicApiKey", + "endpointSuffix": "" + }, + { + "id": "responses", + "protocol": "OpenAIResponses", + "authProfile": "Bearer", + "endpointSuffix": "" + } + ], + "modelBindingRules": [ + { + "bindingId": "anthropic", + "prefixes": [ + "claude-" + ] + }, + { + "bindingId": "responses", + "prefixes": [ + "gpt-" + ] + } + ] + }, + { + "id": "minimax-global", + "displayName": "MiniMax(国际,Anthropic 协议)", + "provider": "Anthropic", + "defaultGateway": "https://api.minimax.io/anthropic", + "defaultModels": [ + "MiniMax-M2.7", + "MiniMax-M2.7-highspeed", + "MiniMax-M3" + ], + "requiresApiKey": true, + "notes": "与现有 MiniMax OpenAI 兼容中国区预设并存,不改已有渠道。" + }, + { + "id": "minimax-cn-anthropic", + "displayName": "MiniMax(中国,Anthropic 协议)", + "provider": "Anthropic", + "defaultGateway": "https://api.minimaxi.com/anthropic", + "defaultModels": [ + "MiniMax-M2.7", + "MiniMax-M2.7-highspeed", + "MiniMax-M3" + ], + "requiresApiKey": true + }, + { + "id": "moonshot-global", + "displayName": "Moonshot(国际)", + "provider": "OpenAI", + "defaultGateway": "https://api.moonshot.ai/v1", + "defaultModels": [ + "kimi-k2.6", + "kimi-k2.7-code", + "kimi-k2.7-code-highspeed", + "kimi-k3" + ], + "requiresApiKey": true, + "notes": "中国区预设 moonshot 保持 https://api.moonshot.cn/v1。" + }, + { + "id": "kimi-coding", + "displayName": "Kimi For Coding", + "provider": "Anthropic", + "defaultGateway": "https://api.kimi.com/coding", + "defaultModels": [ + "k3", + "k3-256k", + "kimi-for-coding", + "kimi-for-coding-highspeed" + ], + "requiresApiKey": true, + "notes": "粘贴 API Key。不实现订阅 OAuth。" + }, + { + "id": "github-copilot", + "displayName": "GitHub Copilot", + "provider": "ResponsesAPI", + "defaultGateway": "https://api.individual.githubcopilot.com", + "defaultModels": [], + "requiresApiKey": true, + "notes": "粘贴 Copilot token。默认 Responses;claude-* 走 Anthropic,gemini-*/kimi-* 走 Chat。目录超过 30 个,不预置。", + "bindings": [ + { + "id": "anthropic", + "protocol": "AnthropicMessages", + "authProfile": "AnthropicApiKey", + "endpointSuffix": "" + }, + { + "id": "chat", + "protocol": "OpenAIChat", + "authProfile": "Bearer", + "endpointSuffix": "" + } + ], + "modelBindingRules": [ + { + "bindingId": "anthropic", + "prefixes": [ + "claude-" + ] + }, + { + "bindingId": "chat", + "prefixes": [ + "gemini-", + "kimi-" + ] + } + ] + }, + { + "id": "mistral", + "displayName": "Mistral", + "provider": "Mistral", + "defaultGateway": "https://api.mistral.ai", + "defaultModels": [], + "requiresApiKey": true, + "notes": "线协议名 mistral-conversations;pi 当前实现实际请求 POST /v1/chat/completions。目录 33 个,不预置。" + }, + { + "id": "azure-openai-responses", + "displayName": "Azure OpenAI (Responses)", + "provider": "AzureOpenAI", + "defaultGateway": null, + "defaultModels": [], + "requiresApiKey": true, + "notes": "创建时输入资源名或资源根地址,系统规范成 /openai/v1。模型名即 deployment 名。", + "setup": "azure" + }, + { + "id": "google-vertex", + "displayName": "Google Vertex AI", + "provider": "Vertex", + "defaultGateway": null, + "defaultModels": [ + "gemini-2.5-flash", + "gemini-2.5-flash-lite", + "gemini-2.5-pro", + "gemini-3-flash-preview", + "gemini-3.1-flash-lite", + "gemini-3.1-pro-preview", + "gemini-3.1-pro-preview-customtools", + "gemini-3.5-flash", + "gemini-3.5-flash-lite", + "gemini-3.6-flash", + "gemini-3.7-flash", + "gemini-3.8-flash", + "gemini-flash-latest", + "gemini-flash-lite-latest" + ], + "requiresApiKey": true, + "notes": "创建时输入 project、location 和 API Key。不支持 ADC。", + "setup": "vertex" + }, + { + "id": "amazon-bedrock", + "displayName": "Amazon Bedrock", + "provider": "Bedrock", + "defaultGateway": null, + "defaultModels": [], + "requiresApiKey": true, + "notes": "创建时输入 region 和 bearer token。不支持 AWS profile / IAM。目录很大,不预置模型。", + "setup": "bedrock" + }, + { + "id": "radius", + "displayName": "Radius", + "provider": "Radius", + "defaultGateway": "https://radius.pi.dev", + "defaultModels": [ + "balanced", + "cheap", + "claude-fable-5", + "claude-fable-5-1", + "claude-haiku-4-5", + "claude-opus-4-5", + "claude-opus-4-8", + "claude-opus-5", + "claude-opus-5-5", + "claude-sonnet-4-5", + "claude-sonnet-5", + "deepseek-v4-flash", + "deepseek-v4-pro", + "deepseek-v4.1-flash", + "glm-5.2", + "glm-5.3", + "glm-5.3-flash", + "gpt-5.3-codex", + "gpt-5.4", + "gpt-5.4-mini", + "gpt-5.5", + "gpt-5.6-luna", + "gpt-5.6-sol", + "gpt-5.6-terra", + "gpt-6-astra", + "gpt-6-luna", + "gpt-6-sol", + "kimi-k2.7-code", + "kimi-k3", + "precise" + ], + "requiresApiKey": true, + "notes": "pi-messages 网关。粘贴 API Key,不实现 OAuth。" } ] } diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmPresetEndpoints.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmPresetEndpoints.cs new file mode 100644 index 00000000..19289f98 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmPresetEndpoints.cs @@ -0,0 +1,74 @@ +using System; +using System.Text.RegularExpressions; + +namespace TelegramSearchBot.Service.AI.LLM { + /// Turns preset setup answers into the Gateway stored on LLMChannel. No new columns. + public static class LlmPresetEndpoints { + private static readonly Regex Token = new("^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$", RegexOptions.Compiled); + private static readonly Regex Region = new("^[a-z0-9-]{1,40}$", RegexOptions.Compiled); + + public static string Compose(LlmProviderPreset preset, string redisData) { + var parts = (redisData ?? string.Empty).Split('|'); + return preset.Setup switch { + "vertex" => BuildVertex(Part(parts, 1), Part(parts, 2)), + "bedrock" => BuildBedrock(Part(parts, 1)), + "azure" => Part(parts, 1), + _ => parts.Length > 1 && !string.IsNullOrWhiteSpace(parts[1]) + ? parts[1] + : preset.DefaultGateway ?? throw new ArgumentException("预设没有默认网关") + }; + } + + public static string BuildVertex(string project, string location) { + if (!Token.IsMatch(project ?? string.Empty)) { + throw new ArgumentException("project ID 只能包含字母、数字、点、下划线和连字符"); + } + if (!Region.IsMatch(location ?? string.Empty)) { + throw new ArgumentException("location 只能包含小写字母、数字和连字符,例如 us-central1"); + } + return $"https://{location}-aiplatform.googleapis.com/v1/projects/{project}/locations/{location}"; + } + + public static string BuildBedrock(string region) { + if (!Region.IsMatch(region ?? string.Empty)) { + throw new ArgumentException("region 只能包含小写字母、数字和连字符,例如 us-east-1"); + } + return $"https://bedrock-runtime.{region}.amazonaws.com"; + } + + /// + /// Accepts a resource name or a root URL on the Azure OpenAI hosts and returns https://host/openai/v1. + /// + public static string NormalizeAzure(string input) { + var trimmed = (input ?? string.Empty).Trim().TrimEnd('/'); + if (string.IsNullOrEmpty(trimmed)) { + throw new ArgumentException("请输入 Azure 资源名或资源根地址"); + } + + if (!trimmed.Contains("://", StringComparison.Ordinal)) { + if (!Token.IsMatch(trimmed)) { + throw new ArgumentException("资源名只能包含字母、数字、点、下划线和连字符"); + } + trimmed = $"https://{trimmed}.openai.azure.com"; + } + + if (!Uri.TryCreate(trimmed, UriKind.Absolute, out var uri) || + (uri.Scheme != Uri.UriSchemeHttps && uri.Scheme != Uri.UriSchemeHttp)) { + throw new ArgumentException("请输入有效的 Azure 资源根地址"); + } + + var host = uri.Host; + var allowed = host.EndsWith(".openai.azure.com", StringComparison.OrdinalIgnoreCase) + || host.EndsWith(".cognitiveservices.azure.com", StringComparison.OrdinalIgnoreCase) + || host.EndsWith(".ai.azure.com", StringComparison.OrdinalIgnoreCase); + if (!allowed) { + throw new ArgumentException("Azure 地址主机须为 openai.azure.com、cognitiveservices.azure.com 或 ai.azure.com"); + } + + return $"{uri.Scheme}://{host}/openai/v1"; + } + + private static string Part(string[] parts, int index) => + index < parts.Length ? parts[index] : string.Empty; + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs index bbe0929a..e8b3c674 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderCatalog.cs @@ -22,7 +22,12 @@ public sealed record LlmProviderPreset( /// True when the gateway catalog IS the entitlement (subscription, e.g. OpenCode Go): refresh may add /// discovered models. False for pay-per-token catalogs (e.g. OpenCode Zen) where listing ≠ authorization. /// - bool CatalogIsEntitlement = false); + bool CatalogIsEntitlement = false, + /// + /// Extra prompts before the API key: vertex (project, location), bedrock (region), + /// azure (resource root or name). Null uses the fixed gateway or the generic gateway prompt. + /// + string? Setup = null); /// Extra API binding a preset creates: same gateway, different wire protocol/auth. public sealed record LlmPresetBinding( @@ -95,6 +100,18 @@ public static class LlmProviderCatalog { return null; } + /// 按预设默认网关精确匹配(忽略大小写与末尾斜杠)。网关要手填的预设不参与。 + public static LlmProviderPreset? FindForGateway(string? gateway) { + if (string.IsNullOrWhiteSpace(gateway)) { + return null; + } + + var normalized = gateway.Trim().TrimEnd('/'); + return Presets.FirstOrDefault(p => + !string.IsNullOrWhiteSpace(p.DefaultGateway) && + string.Equals(p.DefaultGateway.Trim().TrimEnd('/'), normalized, StringComparison.OrdinalIgnoreCase)); + } + /// 把用户输入的网关补成带协议路径后缀的 binding 端点(后缀已存在时不重复追加)。 public static string BuildBindingEndpoint(string? gateway, string? suffix) { var trimmed = (gateway ?? string.Empty).TrimEnd('/'); diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs index 64afb254..36c2074a 100644 --- a/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs +++ b/TelegramSearchBot.LLM/Service/AI/LLM/LlmProviderRegistry.cs @@ -32,6 +32,8 @@ public virtual ILlmModelCatalog GetCatalog(LLMProvider provider) { LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + LLMProvider.Mistral or LLMProvider.AzureOpenAI or LLMProvider.Vertex or LLMProvider.Bedrock or LLMProvider.Radius + => _serviceProvider.GetRequiredService(), _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } @@ -46,6 +48,8 @@ public virtual ILlmEmbeddings GetEmbeddings(LLMProvider provider) { LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + LLMProvider.Mistral or LLMProvider.AzureOpenAI or LLMProvider.Vertex or LLMProvider.Bedrock or LLMProvider.Radius + => _serviceProvider.GetRequiredService(), _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } @@ -60,6 +64,8 @@ public virtual ILlmVision GetVision(LLMProvider provider) { LLMProvider.LMStudio => _serviceProvider.GetRequiredService(), LLMProvider.Anthropic => _serviceProvider.GetRequiredService(), LLMProvider.ResponsesAPI => _serviceProvider.GetRequiredService(), + LLMProvider.Mistral or LLMProvider.AzureOpenAI or LLMProvider.Vertex or LLMProvider.Bedrock or LLMProvider.Radius + => _serviceProvider.GetRequiredService(), _ => throw new KeyNotFoundException($"No LLM provider registered for provider {provider}.") }; } @@ -80,6 +86,11 @@ public virtual async Task GetTransport(LLMChannel channel, L LLMProvider.Anthropic => LlmProtocol.AnthropicMessages, LLMProvider.Ollama => LlmProtocol.Ollama, LLMProvider.Gemini => LlmProtocol.Gemini, + LLMProvider.Mistral => LlmProtocol.MistralConversations, + LLMProvider.AzureOpenAI => LlmProtocol.AzureOpenAIResponses, + LLMProvider.Vertex => LlmProtocol.GoogleVertex, + LLMProvider.Bedrock => LlmProtocol.BedrockConverse, + LLMProvider.Radius => LlmProtocol.PiMessages, _ => LlmProtocol.OpenAIChat }; @@ -96,6 +107,16 @@ public virtual async Task GetTransport(LLMChannel channel, L return Transports.GeminiTransport.Create(channel, binding, modelName, supportsVision, logger, httpClientFactory); case LlmProtocol.Ollama: return await Transports.OllamaTransport.CreateAsync(channel, binding, modelName, systemPrompt, logger, httpClientFactory); + case LlmProtocol.MistralConversations: + return Transports.MistralTransport.Create(channel, binding, modelName, httpClientFactory); + case LlmProtocol.AzureOpenAIResponses: + return Transports.AzureResponsesTransport.Create(channel, binding, modelName, promptCachingEnabled, supportsVision, logger, httpClientFactory); + case LlmProtocol.GoogleVertex: + return Transports.VertexTransport.Create(channel, binding, modelName, httpClientFactory); + case LlmProtocol.BedrockConverse: + return Transports.BedrockTransport.Create(channel, binding, modelName, httpClientFactory); + case LlmProtocol.PiMessages: + return Transports.PiMessagesTransport.Create(channel, binding, modelName, httpClientFactory); case LlmProtocol.OpenAIChat: default: return Transports.OpenAiChatTransport.Create(channel, binding, modelName, chatId, diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/FixedModelCatalog.cs b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/FixedModelCatalog.cs new file mode 100644 index 00000000..368a110c --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/ProviderApi/FixedModelCatalog.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using TelegramSearchBot.Attributes; +using TelegramSearchBot.Interface.AI.LLM; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM { + /// + /// Catalog for providers that do not have a shared model-list HTTP API here. + /// Returns the preset's seeded model ids and never calls the network, so refresh cannot wipe manual rows. + /// + [Injectable(ServiceLifetime.Transient)] + public class FixedModelCatalog : ILlmModelCatalog, ILlmEmbeddings, ILlmVision { + public Task IsHealthyAsync(LLMChannel channel, LLMApiBinding binding) => + Task.FromResult(channel != null && !string.IsNullOrWhiteSpace(channel.ApiKey)); + + public Task> GetAllModels(LLMChannel channel) { + if (channel == null) return Task.FromResult>([]); + var names = LlmProviderCatalog.Presets + .Where(preset => preset.Provider == channel.Provider) + .SelectMany(preset => preset.DefaultModels) + .Distinct(StringComparer.OrdinalIgnoreCase) + .ToList(); + return Task.FromResult>(names); + } + + public async Task> GetAllModelsWithCapabilities(LLMChannel channel) { + var models = await GetAllModels(channel); + return models.Select(name => new ModelWithCapabilities { ModelName = name }); + } + + public Task GenerateEmbeddingsAsync(string text, string modelName, LLMChannel channel) => + throw new NotSupportedException($"{channel?.Provider} does not provide embeddings."); + + public Task AnalyzeImageAsync(string photoPath, string modelName, LLMChannel channel, string prompt = null) => + throw new NotSupportedException($"{channel?.Provider} does not provide vision analysis."); + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AwsEventStream.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AwsEventStream.cs new file mode 100644 index 00000000..8b42d8c4 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AwsEventStream.cs @@ -0,0 +1,141 @@ +using System.Text; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// Minimal Amazon Event Stream codec (prelude + string headers + payload). Used by Bedrock Converse Stream. + internal static class AwsEventStream { + internal readonly record struct Message(string EventType, string Payload); + + public static byte[] Encode(string eventType, string jsonPayload) { + var headers = EncodeHeaders(eventType); + var payload = Encoding.UTF8.GetBytes(jsonPayload); + var total = 12 + headers.Length + payload.Length + 4; + using var ms = new MemoryStream(); + WriteInt(ms, total); + WriteInt(ms, headers.Length); + var prelude = ms.ToArray(); + WriteInt(ms, unchecked((int)Crc32(prelude))); + ms.Write(headers); + ms.Write(payload); + var withPayload = ms.ToArray(); + WriteInt(ms, unchecked((int)Crc32(withPayload))); + return ms.ToArray(); + } + + public static List Decode(ReadOnlySpan bytes) { + var messages = new List(); + var offset = 0; + while (offset + 12 <= bytes.Length) { + var total = ReadInt(bytes, offset); + if (total < 16 || offset + total > bytes.Length) { + break; + } + var headersLength = ReadInt(bytes, offset + 4); + var preludeCrc = unchecked((uint)ReadInt(bytes, offset + 8)); + if (Crc32(bytes.Slice(offset, 8)) != preludeCrc) { + throw new InvalidOperationException("Bedrock event stream prelude CRC mismatch"); + } + var headerStart = offset + 12; + var payloadStart = headerStart + headersLength; + var payloadLength = total - 12 - headersLength - 4; + if (headersLength < 0 || payloadLength < 0 || payloadStart + payloadLength + 4 > offset + total) { + throw new InvalidOperationException("Bedrock event stream frame is truncated"); + } + var messageCrc = unchecked((uint)ReadInt(bytes, payloadStart + payloadLength)); + if (Crc32(bytes.Slice(offset, total - 4)) != messageCrc) { + throw new InvalidOperationException("Bedrock event stream message CRC mismatch"); + } + var eventType = ReadEventType(bytes.Slice(headerStart, headersLength)); + var payload = Encoding.UTF8.GetString(bytes.Slice(payloadStart, payloadLength)); + messages.Add(new Message(eventType, payload)); + offset += total; + } + return messages; + } + + private static byte[] EncodeHeaders(string eventType) { + using var ms = new MemoryStream(); + WriteStringHeader(ms, ":event-type", eventType); + WriteStringHeader(ms, ":content-type", "application/json"); + WriteStringHeader(ms, ":message-type", "event"); + return ms.ToArray(); + } + + private static void WriteStringHeader(Stream stream, string name, string value) { + var nameBytes = Encoding.UTF8.GetBytes(name); + var valueBytes = Encoding.UTF8.GetBytes(value); + stream.WriteByte((byte)nameBytes.Length); + stream.Write(nameBytes); + stream.WriteByte(7); + stream.WriteByte((byte)(valueBytes.Length >> 8)); + stream.WriteByte((byte)(valueBytes.Length & 0xFF)); + stream.Write(valueBytes); + } + + private static string ReadEventType(ReadOnlySpan headers) { + var offset = 0; + while (offset < headers.Length) { + var nameLength = headers[offset]; + offset++; + if (offset + nameLength + 1 > headers.Length) break; + var name = Encoding.UTF8.GetString(headers.Slice(offset, nameLength)); + offset += nameLength; + var valueType = headers[offset]; + offset++; + if (valueType == 7) { + if (offset + 2 > headers.Length) break; + var valueLength = (headers[offset] << 8) | headers[offset + 1]; + offset += 2; + if (offset + valueLength > headers.Length) break; + var value = Encoding.UTF8.GetString(headers.Slice(offset, valueLength)); + if (name == ":event-type") return value; + offset += valueLength; + } else { + offset += ValueLength(headers, ref offset, valueType); + } + } + return string.Empty; + } + + private static int ValueLength(ReadOnlySpan headers, ref int offset, byte valueType) { + return valueType switch { + 0 or 1 => 0, + 2 => 1, + 3 => 2, + 4 => 4, + 5 or 8 => 8, + 6 or 7 => ConsumeCounted(headers, ref offset), + 9 => 16, + _ => 0 + }; + } + + private static int ConsumeCounted(ReadOnlySpan headers, ref int offset) { + if (offset + 2 > headers.Length) return 0; + var length = (headers[offset] << 8) | headers[offset + 1]; + offset += 2; + return length; + } + + private static void WriteInt(Stream stream, int value) { + stream.WriteByte((byte)(value >> 24)); + stream.WriteByte((byte)(value >> 16)); + stream.WriteByte((byte)(value >> 8)); + stream.WriteByte((byte)value); + } + + private static int ReadInt(ReadOnlySpan bytes, int offset) => + (bytes[offset] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3]; + + private static uint Crc32(ReadOnlySpan data) { + uint crc = 0xFFFFFFFF; + foreach (var b in data) { + crc ^= b; + for (var i = 0; i < 8; i++) { + var mask = (uint)-(int)(crc & 1); + crc = (crc >> 1) ^ (0xEDB88320 & mask); + } + } + return ~crc; + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AzureResponsesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AzureResponsesTransport.cs new file mode 100644 index 00000000..2ce35bc7 --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/AzureResponsesTransport.cs @@ -0,0 +1,41 @@ +using Microsoft.Extensions.Logging; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// + /// Azure OpenAI Responses. The body matches ; the base URL is normalized + /// to /openai/v1 and the SDK bearer token is copied to the api-key header. + /// + public static class AzureResponsesTransport { + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, + bool promptCachingEnabled, bool supportsVision, ILogger logger, IHttpClientFactory httpClientFactory) { + var endpoint = LlmPresetEndpoints.NormalizeAzure(LlmBindingSupport.ResolveEndpoint(channel, binding)); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var bundle = ResponsesTransport.Create(channel, binding, modelName, endpoint, apiKey, + promptCachingEnabled, supportsVision, logger, new AzureHeaderHttpClientFactory()); + bundle.Config.Protocol = LlmProtocol.AzureOpenAIResponses; + bundle.Config.Endpoint = endpoint; + return bundle; + } + } + + internal sealed class AzureHeaderHttpClientFactory : IHttpClientFactory { + + public HttpClient CreateClient(string name) { + var handler = new AzureApiKeyHandler { InnerHandler = new HttpClientHandler() }; + return new HttpClient(handler, disposeHandler: true); + } + } + + internal sealed class AzureApiKeyHandler : DelegatingHandler { + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + if (request.Headers.Authorization?.Parameter is { Length: > 0 } token) { + request.Headers.Remove("api-key"); + request.Headers.TryAddWithoutValidation("api-key", token); + request.Headers.Authorization = null; + } + return base.SendAsync(request, cancellationToken); + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/BedrockTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/BedrockTransport.cs new file mode 100644 index 00000000..d335ac5f --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/BedrockTransport.cs @@ -0,0 +1,183 @@ +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// Bedrock Converse Stream with a bearer token. AWS credential chains are out of scope. + public sealed class BedrockTransport : ILlmTransport { + private readonly HttpClient _http; + private readonly string _endpoint; + private readonly string _apiKey; + + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, IHttpClientFactory httpClientFactory) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var transport = new BedrockTransport(httpClientFactory.CreateClient(), endpoint, apiKey); + return new LlmTransportBundle(transport, new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Protocol = LlmProtocol.BedrockConverse, + Binding = binding, + Channel = channel + }); + } + + internal BedrockTransport(HttpClient http, string endpoint, string apiKey) { + _http = http; + _endpoint = endpoint; + _apiKey = apiKey; + } + + public bool SupportsNativeTools => true; + + internal static HttpRequestMessage BuildRequest(string endpoint, string apiKey, LlmTurnRequest request) { + var model = Uri.EscapeDataString(request.Config.ModelName); + var url = $"{endpoint.TrimEnd('/')}/model/{model}/converse-stream"; + var message = new HttpRequestMessage(HttpMethod.Post, url); + message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.amazon.eventstream")); + message.Content = new StringContent(BuildBody(request), Encoding.UTF8, "application/json"); + return message; + } + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var httpRequest = BuildRequest(_endpoint, _apiKey, request); + using var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + if (!response.IsSuccessStatusCode) { + var error = await response.Content.ReadAsStringAsync(cancellationToken); + throw new InvalidOperationException($"Bedrock HTTP {(int)response.StatusCode}: {error}"); + } + + var bytes = await response.Content.ReadAsByteArrayAsync(cancellationToken); + var text = new StringBuilder(); + var thinking = new StringBuilder(); + var tools = new Dictionary(); + var toolArgs = new Dictionary(); + foreach (var frame in AwsEventStream.Decode(bytes)) { + if (frame.EventType == "exception" || frame.EventType.Contains("Exception", StringComparison.Ordinal)) { + throw new InvalidOperationException($"Bedrock {frame.EventType}: {frame.Payload}"); + } + if (frame.EventType is not ("contentBlockStart" or "contentBlockDelta")) continue; + using var doc = JsonDocument.Parse(string.IsNullOrWhiteSpace(frame.Payload) ? "{}" : frame.Payload); + var root = doc.RootElement; + var index = root.TryGetProperty("contentBlockIndex", out var indexElement) ? indexElement.GetInt32() : 0; + if (frame.EventType == "contentBlockStart" && + root.TryGetProperty("start", out var start) && + start.TryGetProperty("toolUse", out var toolUse)) { + tools[index] = new LlmToolCall { + Id = toolUse.TryGetProperty("toolUseId", out var id) ? id.GetString() ?? string.Empty : string.Empty, + Name = toolUse.TryGetProperty("name", out var name) ? name.GetString() ?? string.Empty : string.Empty, + ArgumentsJson = "{}" + }; + toolArgs[index] = new StringBuilder(); + } + if (!root.TryGetProperty("delta", out var delta)) continue; + if (delta.TryGetProperty("text", out var textElement) && textElement.ValueKind == JsonValueKind.String) { + var piece = textElement.GetString() ?? string.Empty; + text.Append(piece); + yield return new LlmStreamEvent.TextDelta(piece); + } + if (delta.TryGetProperty("reasoningContent", out var reasoning) && + reasoning.TryGetProperty("text", out var reasoningText) && + reasoningText.ValueKind == JsonValueKind.String) { + var piece = reasoningText.GetString() ?? string.Empty; + thinking.Append(piece); + yield return new LlmStreamEvent.ThinkingDelta(piece); + } + if (delta.TryGetProperty("toolUse", out var toolDelta) && + toolDelta.TryGetProperty("input", out var input) && + input.ValueKind == JsonValueKind.String) { + if (!toolArgs.TryGetValue(index, out var builder)) { + builder = new StringBuilder(); + toolArgs[index] = builder; + tools.TryAdd(index, new LlmToolCall()); + } + builder.Append(input.GetString()); + } + } + + foreach (var pair in toolArgs) { + if (tools.TryGetValue(pair.Key, out var call) && pair.Value.Length > 0) { + call.ArgumentsJson = pair.Value.ToString(); + } + } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = text.ToString(), + Reasoning = thinking.ToString(), + ToolCalls = tools.OrderBy(pair => pair.Key).Select(pair => pair.Value).Where(call => call.Name.Length > 0 || call.Id.Length > 0).ToList(), + StreamedAny = text.Length > 0 || thinking.Length > 0 + }); + } + + private static string BuildBody(LlmTurnRequest request) { + var messages = new JsonArray(); + foreach (var message in request.History.Where(item => item.Role is LlmRole.User or LlmRole.Assistant or LlmRole.Tool)) { + if (message.Role == LlmRole.Assistant) { + var content = new JsonArray(); + if (!string.IsNullOrEmpty(message.Text)) content.Add(new JsonObject { ["text"] = message.Text }); + if (message.ToolCalls != null) { + foreach (var call in message.ToolCalls) { + content.Add(new JsonObject { + ["toolUse"] = new JsonObject { + ["toolUseId"] = call.Id, + ["name"] = call.Name, + ["input"] = ParseObject(call.ArgumentsJson) + } + }); + } + } + messages.Add(new JsonObject { ["role"] = "assistant", ["content"] = content }); + } else if (message.Role == LlmRole.Tool) { + messages.Add(new JsonObject { + ["role"] = "user", + ["content"] = new JsonArray { + new JsonObject { + ["toolResult"] = new JsonObject { + ["toolUseId"] = message.ToolCallId ?? string.Empty, + ["content"] = new JsonArray { new JsonObject { ["text"] = message.Text ?? string.Empty } } + } + } + } + }); + } else { + messages.Add(new JsonObject { + ["role"] = "user", + ["content"] = new JsonArray { new JsonObject { ["text"] = message.Text ?? string.Empty } } + }); + } + } + + var body = new JsonObject { ["messages"] = messages }; + if (!string.IsNullOrWhiteSpace(request.SystemPrompt)) { + body["system"] = new JsonArray { new JsonObject { ["text"] = request.SystemPrompt } }; + } + if (request.Tools is { Count: > 0 }) { + body["toolConfig"] = new JsonObject { + ["tools"] = new JsonArray(request.Tools.Select(tool => new JsonObject { + ["toolSpec"] = new JsonObject { + ["name"] = tool.Name, + ["description"] = tool.Description, + ["inputSchema"] = new JsonObject { ["json"] = ParseObject(tool.ParametersJson) } + } + }).ToArray()) + }; + } + return body.ToJsonString(); + } + + private static JsonNode ParseObject(string? json) { + try { + return JsonNode.Parse(string.IsNullOrWhiteSpace(json) ? "{}" : json) ?? new JsonObject(); + } catch (JsonException) { + return new JsonObject(); + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/MistralTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/MistralTransport.cs new file mode 100644 index 00000000..20ecc4ed --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/MistralTransport.cs @@ -0,0 +1,178 @@ +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// + /// pi's mistral-conversations adapter. The current pi module posts to Chat Completions. + /// + public sealed class MistralTransport : ILlmTransport { + private readonly HttpClient _http; + private readonly string _endpoint; + private readonly string _apiKey; + + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, IHttpClientFactory httpClientFactory) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var transport = new MistralTransport(httpClientFactory.CreateClient(), endpoint, apiKey); + return new LlmTransportBundle(transport, BundleConfig(channel, binding, modelName, endpoint, apiKey)); + } + + internal MistralTransport(HttpClient http, string endpoint, string apiKey) { + _http = http; + _endpoint = endpoint; + _apiKey = apiKey; + } + + public bool SupportsNativeTools => true; + + internal static HttpRequestMessage BuildRequest(string endpoint, string apiKey, LlmTurnRequest request) { + var url = endpoint.TrimEnd('/'); + url = url.EndsWith("/v1", StringComparison.OrdinalIgnoreCase) + ? url + "/chat/completions" + : url + "/v1/chat/completions"; + var message = new HttpRequestMessage(HttpMethod.Post, url); + message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + message.Content = new StringContent(BuildBody(request), Encoding.UTF8, "application/json"); + return message; + } + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var httpRequest = BuildRequest(_endpoint, _apiKey, request); + using var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + if (!response.IsSuccessStatusCode) { + var error = await response.Content.ReadAsStringAsync(cancellationToken); + throw new InvalidOperationException($"Mistral HTTP {(int)response.StatusCode}: {error}"); + } + + var text = new StringBuilder(); + var toolCalls = new List(); + var toolArgs = new Dictionary(); + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + using var reader = new StreamReader(stream); + while (await reader.ReadLineAsync(cancellationToken) is { } line) { + if (!line.StartsWith("data:", StringComparison.Ordinal)) continue; + var data = line[5..].Trim(); + if (data.Length == 0 || data == "[DONE]") continue; + using var doc = JsonDocument.Parse(data); + if (!doc.RootElement.TryGetProperty("choices", out var choices) || choices.GetArrayLength() == 0) continue; + var delta = choices[0].GetProperty("delta"); + if (delta.TryGetProperty("content", out var content) && content.ValueKind == JsonValueKind.String) { + var piece = content.GetString() ?? string.Empty; + if (piece.Length > 0) { + text.Append(piece); + yield return new LlmStreamEvent.TextDelta(piece); + } + } + if (!delta.TryGetProperty("tool_calls", out var calls)) continue; + foreach (var call in calls.EnumerateArray()) { + var index = call.TryGetProperty("index", out var indexElement) ? indexElement.GetInt32() : toolCalls.Count; + while (toolCalls.Count <= index) { + toolCalls.Add(new LlmToolCall()); + toolArgs[toolCalls.Count - 1] = new StringBuilder(); + } + if (call.TryGetProperty("id", out var id) && id.ValueKind == JsonValueKind.String) { + toolCalls[index].Id = id.GetString() ?? toolCalls[index].Id; + } + if (call.TryGetProperty("function", out var function)) { + if (function.TryGetProperty("name", out var name) && name.ValueKind == JsonValueKind.String) { + toolCalls[index].Name += name.GetString(); + } + if (function.TryGetProperty("arguments", out var args) && args.ValueKind == JsonValueKind.String) { + toolArgs[index].Append(args.GetString()); + } + } + } + } + + foreach (var pair in toolArgs) { + toolCalls[pair.Key].ArgumentsJson = pair.Value.Length == 0 ? "{}" : pair.Value.ToString(); + } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = text.ToString(), + ToolCalls = toolCalls.Where(call => !string.IsNullOrEmpty(call.Name) || !string.IsNullOrEmpty(call.Id)).ToList(), + StreamedAny = text.Length > 0 + }); + } + + private static string BuildBody(LlmTurnRequest request) { + var messages = new JsonArray(); + if (!string.IsNullOrWhiteSpace(request.SystemPrompt)) { + messages.Add(new JsonObject { ["role"] = "system", ["content"] = request.SystemPrompt }); + } + foreach (var message in request.History) { + switch (message.Role) { + case LlmRole.System: + messages.Add(new JsonObject { ["role"] = "system", ["content"] = message.Text ?? string.Empty }); + break; + case LlmRole.Assistant: + var assistant = new JsonObject { ["role"] = "assistant", ["content"] = message.Text ?? string.Empty }; + if (message.ToolCalls is { Count: > 0 }) { + assistant["tool_calls"] = new JsonArray(message.ToolCalls.Select(call => new JsonObject { + ["id"] = call.Id, + ["type"] = "function", + ["function"] = new JsonObject { + ["name"] = call.Name, + ["arguments"] = string.IsNullOrWhiteSpace(call.ArgumentsJson) ? "{}" : call.ArgumentsJson + } + }).ToArray()); + } + messages.Add(assistant); + break; + case LlmRole.Tool: + messages.Add(new JsonObject { + ["role"] = "tool", + ["tool_call_id"] = message.ToolCallId ?? string.Empty, + ["content"] = message.Text ?? string.Empty + }); + break; + default: + messages.Add(new JsonObject { ["role"] = "user", ["content"] = message.Text ?? string.Empty }); + break; + } + } + + var body = new JsonObject { + ["model"] = request.Config.ModelName, + ["stream"] = true, + ["messages"] = messages + }; + if (request.Tools is { Count: > 0 }) { + body["tools"] = new JsonArray(request.Tools.Select(tool => new JsonObject { + ["type"] = "function", + ["function"] = new JsonObject { + ["name"] = tool.Name, + ["description"] = tool.Description, + ["parameters"] = ParseArguments(tool.ParametersJson) + } + }).ToArray()); + } + return body.ToJsonString(); + } + + private static JsonNode ParseArguments(string? json) { + try { + return JsonNode.Parse(string.IsNullOrWhiteSpace(json) ? "{}" : json) ?? new JsonObject(); + } catch (JsonException) { + return new JsonObject(); + } + } + + private static LlmTransportConfig BundleConfig(LLMChannel channel, LLMApiBinding binding, string modelName, string endpoint, string apiKey) => + new() { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Protocol = LlmProtocol.MistralConversations, + Binding = binding, + Channel = channel + }; + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/PiMessagesTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/PiMessagesTransport.cs new file mode 100644 index 00000000..62b17e1c --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/PiMessagesTransport.cs @@ -0,0 +1,182 @@ +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// pi-messages: POST {gateway}/messages and read assistant-message SSE events. + public sealed class PiMessagesTransport : ILlmTransport { + private readonly HttpClient _http; + private readonly string _endpoint; + private readonly string _apiKey; + + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, IHttpClientFactory httpClientFactory) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var transport = new PiMessagesTransport(httpClientFactory.CreateClient(), endpoint, apiKey); + return new LlmTransportBundle(transport, new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Protocol = LlmProtocol.PiMessages, + Binding = binding, + Channel = channel + }); + } + + internal PiMessagesTransport(HttpClient http, string endpoint, string apiKey) { + _http = http; + _endpoint = endpoint; + _apiKey = apiKey; + } + + public bool SupportsNativeTools => true; + + internal static HttpRequestMessage BuildRequest(string endpoint, string apiKey, LlmTurnRequest request) { + var message = new HttpRequestMessage(HttpMethod.Post, endpoint.TrimEnd('/') + "/messages"); + message.Headers.Authorization = new AuthenticationHeaderValue("Bearer", apiKey); + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + message.Content = new StringContent(BuildBody(request), Encoding.UTF8, "application/json"); + return message; + } + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var httpRequest = BuildRequest(_endpoint, _apiKey, request); + using var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + if (!response.IsSuccessStatusCode) { + var error = await response.Content.ReadAsStringAsync(cancellationToken); + throw new InvalidOperationException($"pi-messages HTTP {(int)response.StatusCode}: {error}"); + } + + var text = new StringBuilder(); + var thinking = new StringBuilder(); + var tools = new Dictionary(); + var toolArgs = new Dictionary(); + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + using var reader = new StreamReader(stream); + while (await reader.ReadLineAsync(cancellationToken) is { } line) { + if (!line.StartsWith("data:", StringComparison.Ordinal)) continue; + var data = line[5..].Trim(); + if (data.Length == 0 || data == "[DONE]") continue; + using var doc = JsonDocument.Parse(data); + var root = doc.RootElement; + var type = root.TryGetProperty("type", out var typeElement) ? typeElement.GetString() : null; + var index = root.TryGetProperty("contentIndex", out var indexElement) ? indexElement.GetInt32() : 0; + switch (type) { + case "text_delta": + var textPiece = root.TryGetProperty("delta", out var textDelta) ? textDelta.GetString() ?? string.Empty : string.Empty; + text.Append(textPiece); + yield return new LlmStreamEvent.TextDelta(textPiece); + break; + case "thinking_delta": + var thought = root.TryGetProperty("delta", out var thoughtDelta) ? thoughtDelta.GetString() ?? string.Empty : string.Empty; + thinking.Append(thought); + yield return new LlmStreamEvent.ThinkingDelta(thought); + break; + case "toolcall_start": + tools[index] = new LlmToolCall { + Id = root.TryGetProperty("id", out var id) ? id.GetString() ?? string.Empty : string.Empty, + Name = root.TryGetProperty("toolName", out var toolName) ? toolName.GetString() ?? string.Empty : string.Empty + }; + toolArgs[index] = new StringBuilder(); + break; + case "toolcall_delta": + if (!toolArgs.TryGetValue(index, out var builder)) { + builder = new StringBuilder(); + toolArgs[index] = builder; + tools.TryAdd(index, new LlmToolCall()); + } + builder.Append(root.TryGetProperty("delta", out var argDelta) ? argDelta.GetString() : string.Empty); + break; + case "error": + var reason = root.TryGetProperty("errorMessage", out var errorMessage) ? errorMessage.GetString() : data; + throw new InvalidOperationException($"pi-messages error: {reason}"); + } + } + + foreach (var pair in toolArgs) { + if (tools.TryGetValue(pair.Key, out var call)) { + call.ArgumentsJson = pair.Value.Length == 0 ? "{}" : pair.Value.ToString(); + } + } + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = text.ToString(), + Reasoning = thinking.ToString(), + ToolCalls = tools.OrderBy(pair => pair.Key).Select(pair => pair.Value).ToList(), + StreamedAny = text.Length > 0 || thinking.Length > 0 + }); + } + + private static string BuildBody(LlmTurnRequest request) { + var messages = new JsonArray(); + if (!string.IsNullOrWhiteSpace(request.SystemPrompt)) { + messages.Add(Message("system", request.SystemPrompt)); + } + foreach (var message in request.History) { + switch (message.Role) { + case LlmRole.Assistant: + var content = new JsonArray(); + if (!string.IsNullOrEmpty(message.Text)) content.Add(new JsonObject { ["type"] = "text", ["text"] = message.Text }); + if (message.ToolCalls != null) { + foreach (var call in message.ToolCalls) { + content.Add(new JsonObject { + ["type"] = "toolCall", + ["id"] = call.Id, + ["name"] = call.Name, + ["arguments"] = ParseObject(call.ArgumentsJson) + }); + } + } + messages.Add(new JsonObject { ["role"] = "assistant", ["content"] = content }); + break; + case LlmRole.Tool: + messages.Add(new JsonObject { + ["role"] = "toolResult", + ["toolCallId"] = message.ToolCallId ?? string.Empty, + ["toolName"] = message.Thinking ?? string.Empty, + ["content"] = new JsonArray { new JsonObject { ["type"] = "text", ["text"] = message.Text ?? string.Empty } } + }); + break; + case LlmRole.System: + messages.Add(Message("system", message.Text ?? string.Empty)); + break; + default: + messages.Add(Message("user", message.Text ?? string.Empty)); + break; + } + } + + var context = new JsonObject { ["messages"] = messages }; + if (request.Tools is { Count: > 0 }) { + context["tools"] = new JsonArray(request.Tools.Select(tool => new JsonObject { + ["name"] = tool.Name, + ["description"] = tool.Description, + ["parameters"] = ParseObject(tool.ParametersJson) + }).ToArray()); + } + return new JsonObject { + ["model"] = request.Config.ModelName, + ["context"] = context, + ["options"] = new JsonObject() + }.ToJsonString(); + } + + private static JsonObject Message(string role, string text) => new() { + ["role"] = role, + ["content"] = new JsonArray { new JsonObject { ["type"] = "text", ["text"] = text } } + }; + + private static JsonNode ParseObject(string? json) { + try { + return JsonNode.Parse(string.IsNullOrWhiteSpace(json) ? "{}" : json) ?? new JsonObject(); + } catch (JsonException) { + return new JsonObject(); + } + } + } +} diff --git a/TelegramSearchBot.LLM/Service/AI/LLM/Transports/VertexTransport.cs b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/VertexTransport.cs new file mode 100644 index 00000000..18f1d93b --- /dev/null +++ b/TelegramSearchBot.LLM/Service/AI/LLM/Transports/VertexTransport.cs @@ -0,0 +1,165 @@ +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using TelegramSearchBot.Model.AI; +using TelegramSearchBot.Model.Data; + +namespace TelegramSearchBot.Service.AI.LLM.Transports { + /// Vertex AI streamGenerateContent via API key. ADC is out of scope. + public sealed class VertexTransport : ILlmTransport { + private readonly HttpClient _http; + private readonly string _endpoint; + private readonly string _apiKey; + + public static LlmTransportBundle Create(LLMChannel channel, LLMApiBinding binding, string modelName, IHttpClientFactory httpClientFactory) { + var endpoint = LlmBindingSupport.ResolveEndpoint(channel, binding); + var apiKey = LlmBindingSupport.ResolveApiKey(channel, binding); + var transport = new VertexTransport(httpClientFactory.CreateClient(), endpoint, apiKey); + return new LlmTransportBundle(transport, new LlmTransportConfig { + ModelName = modelName, + Endpoint = endpoint, + ApiKey = apiKey, + Provider = channel.Provider, + Protocol = LlmProtocol.GoogleVertex, + Binding = binding, + Channel = channel + }); + } + + internal VertexTransport(HttpClient http, string endpoint, string apiKey) { + _http = http; + _endpoint = endpoint; + _apiKey = apiKey; + } + + public bool SupportsNativeTools => true; + + internal static HttpRequestMessage BuildRequest(string endpoint, string apiKey, LlmTurnRequest request) { + var model = Uri.EscapeDataString(request.Config.ModelName); + var url = $"{endpoint.TrimEnd('/')}/publishers/google/models/{model}:streamGenerateContent?alt=sse"; + var message = new HttpRequestMessage(HttpMethod.Post, url); + message.Headers.TryAddWithoutValidation("x-goog-api-key", apiKey); + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); + message.Content = new StringContent(BuildBody(request), Encoding.UTF8, "application/json"); + return message; + } + + public async IAsyncEnumerable StreamTurnAsync( + LlmTurnRequest request, + [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default) { + using var httpRequest = BuildRequest(_endpoint, _apiKey, request); + using var response = await _http.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken); + if (!response.IsSuccessStatusCode) { + var error = await response.Content.ReadAsStringAsync(cancellationToken); + throw new InvalidOperationException($"Vertex HTTP {(int)response.StatusCode}: {error}"); + } + + var text = new StringBuilder(); + var thinking = new StringBuilder(); + var toolCalls = new List(); + await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); + using var reader = new StreamReader(stream); + while (await reader.ReadLineAsync(cancellationToken) is { } line) { + if (!line.StartsWith("data:", StringComparison.Ordinal)) continue; + var data = line[5..].Trim(); + if (data.Length == 0) continue; + using var doc = JsonDocument.Parse(data); + if (!doc.RootElement.TryGetProperty("candidates", out var candidates) || candidates.GetArrayLength() == 0) continue; + if (!candidates[0].TryGetProperty("content", out var content) || !content.TryGetProperty("parts", out var parts)) continue; + foreach (var part in parts.EnumerateArray()) { + if (part.TryGetProperty("text", out var textElement) && textElement.ValueKind == JsonValueKind.String) { + var piece = textElement.GetString() ?? string.Empty; + var thought = part.TryGetProperty("thought", out var thoughtElement) && thoughtElement.ValueKind == JsonValueKind.True; + if (thought) { + thinking.Append(piece); + yield return new LlmStreamEvent.ThinkingDelta(piece); + } else if (piece.Length > 0) { + text.Append(piece); + yield return new LlmStreamEvent.TextDelta(piece); + } + } + if (part.TryGetProperty("functionCall", out var functionCall)) { + var name = functionCall.TryGetProperty("name", out var nameElement) ? nameElement.GetString() ?? string.Empty : string.Empty; + var args = functionCall.TryGetProperty("args", out var argsElement) ? argsElement.GetRawText() : "{}"; + toolCalls.Add(new LlmToolCall { Id = name, Name = name, ArgumentsJson = args }); + } + } + } + + yield return new LlmStreamEvent.TurnCompleted(new LlmTurnResult { + Text = text.ToString(), + Reasoning = thinking.ToString(), + ToolCalls = toolCalls, + StreamedAny = text.Length > 0 || thinking.Length > 0 + }); + } + + private static string BuildBody(LlmTurnRequest request) { + var contents = new JsonArray(); + foreach (var message in request.History) { + if (message.Role == LlmRole.Assistant) { + var parts = new JsonArray(); + if (!string.IsNullOrEmpty(message.Text)) { + parts.Add(new JsonObject { ["text"] = message.Text }); + } + if (message.ToolCalls != null) { + foreach (var call in message.ToolCalls) { + parts.Add(new JsonObject { + ["functionCall"] = new JsonObject { + ["name"] = call.Name, + ["args"] = ParseObject(call.ArgumentsJson) + } + }); + } + } + contents.Add(new JsonObject { ["role"] = "model", ["parts"] = parts }); + } else if (message.Role == LlmRole.Tool) { + contents.Add(new JsonObject { + ["role"] = "user", + ["parts"] = new JsonArray { + new JsonObject { + ["functionResponse"] = new JsonObject { + ["name"] = message.Thinking ?? message.ToolCallId ?? string.Empty, + ["response"] = new JsonObject { ["result"] = message.Text ?? string.Empty } + } + } + } + }); + } else if (message.Role != LlmRole.System) { + contents.Add(new JsonObject { + ["role"] = "user", + ["parts"] = new JsonArray { new JsonObject { ["text"] = message.Text ?? string.Empty } } + }); + } + } + + var body = new JsonObject { ["contents"] = contents }; + if (!string.IsNullOrWhiteSpace(request.SystemPrompt)) { + body["systemInstruction"] = new JsonObject { + ["parts"] = new JsonArray { new JsonObject { ["text"] = request.SystemPrompt } } + }; + } + if (request.Tools is { Count: > 0 }) { + body["tools"] = new JsonArray { + new JsonObject { + ["functionDeclarations"] = new JsonArray(request.Tools.Select(tool => new JsonObject { + ["name"] = tool.Name, + ["description"] = tool.Description, + ["parameters"] = ParseObject(tool.ParametersJson) + }).ToArray()) + } + }; + } + return body.ToJsonString(); + } + + private static JsonNode ParseObject(string? json) { + try { + return JsonNode.Parse(string.IsNullOrWhiteSpace(json) ? "{}" : json) ?? new JsonObject(); + } catch (JsonException) { + return new JsonObject(); + } + } + } +} diff --git a/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs b/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs index 6db2cd90..bc07c521 100644 --- a/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs +++ b/TelegramSearchBot.LLMAgent/LLMAgentProgram.cs @@ -106,6 +106,7 @@ private static ServiceProvider BuildServices(int port) { services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddTransient(); // LlmServiceProxy resolves LlmChatRunner, which needs the registry (same lifetimes as the main process DI scan). services.AddSingleton(); services.AddSingleton(); diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs index f6c9efc2..b1245669 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs @@ -613,5 +613,44 @@ public async Task ExecuteAsync_EditChannelBindings_AddsNewBinding() { Assert.Contains("绑定创建成功(ID: 55", created.Item2); helperMock.Verify(h => h.EnsureBinding(1, "https://opencode.ai/zen/v1", LlmProtocol.OpenAIResponses, LlmAuthProfile.Bearer), Times.Once); } + + [Fact] + public async Task ExecuteAsync_PresetVertex_ComposesGatewayFromProjectAndLocation() { + long chatId = 123; + var stateKey = $"llmconf:{chatId}:state"; + var dataKey = $"llmconf:{chatId}:data"; + var index = LlmProviderCatalog.Presets.ToList().FindIndex(p => p.Id == "google-vertex") + 1; + + _dbMock.SetupSequence(d => d.StringGetAsync(stateKey, It.IsAny())) + .ReturnsAsync(RedisValue.Null) + .ReturnsAsync("awaiting_preset_selection") + .ReturnsAsync("awaiting_preset_project") + .ReturnsAsync("awaiting_preset_location") + .ReturnsAsync("awaiting_preset_apikey"); + _dbMock.SetupSequence(d => d.StringGetAsync(dataKey, It.IsAny())) + .ReturnsAsync("google-vertex") + .ReturnsAsync("google-vertex|my-project") + .ReturnsAsync("google-vertex|my-project|us-central1"); + helperMock.Setup(h => h.AddChannel(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(9); + helperMock.Setup(h => h.AddModelWithChannel(It.IsAny(), It.IsAny>())) + .ReturnsAsync(true); + + Assert.Contains("Vertex", (await _service.ExecuteAsync("预制渠道", chatId)).Item2); + Assert.Contains("project", (await _service.ExecuteAsync(index.ToString(), chatId)).Item2); + Assert.Contains("location", (await _service.ExecuteAsync("my-project", chatId)).Item2); + Assert.Contains("API Key", (await _service.ExecuteAsync("us-central1", chatId)).Item2); + var created = await _service.ExecuteAsync("vertex-key", chatId); + + Assert.True(created.Item1); + Assert.Contains("渠道创建成功", created.Item2); + helperMock.Verify(h => h.AddChannel( + It.IsAny(), + "https://us-central1-aiplatform.googleapis.com/v1/projects/my-project/locations/us-central1", + "vertex-key", + LLMProvider.Vertex, + 1, + 0), Times.Once); + } } } diff --git a/TelegramSearchBot/Model/AI/LLMConfState.cs b/TelegramSearchBot/Model/AI/LLMConfState.cs index 723d9954..01d722cb 100644 --- a/TelegramSearchBot/Model/AI/LLMConfState.cs +++ b/TelegramSearchBot/Model/AI/LLMConfState.cs @@ -26,6 +26,15 @@ public enum LLMConfState { [Description("awaiting_preset_apikey")] AwaitingPresetApiKey, + [Description("awaiting_preset_project")] + AwaitingPresetProject, + + [Description("awaiting_preset_location")] + AwaitingPresetLocation, + + [Description("awaiting_preset_region")] + AwaitingPresetRegion, + [Description("awaiting_name")] AwaitingName, diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs index d50a5b8e..d2cdaa91 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs @@ -269,6 +269,15 @@ private async Task RefreshOpenCodeEntitlementCatalogAsync(LLMChannel channel, LL } } + private async Task ResolveManualModelBindingAsync(int channelId, string modelName, int? defaultBindingId) { + var channel = await DataContext.LLMChannels.FindAsync(channelId); + var preset = LlmProviderCatalog.FindForGateway(channel?.Gateway); + if (preset == null || channel == null) { + return defaultBindingId; + } + return await ResolvePresetBindingAsync(channelId, channel.Gateway, preset, modelName) ?? defaultBindingId; + } + /// 按预设的模型前缀规则解析并补建 binding;未命中规则返回 null(用渠道默认 binding)。 private async Task ResolvePresetBindingAsync(int channelId, string? gateway, LlmProviderPreset preset, string modelName) { var ruleBindingId = preset.ModelBindingRules.ResolveBindingId(modelName); @@ -427,7 +436,7 @@ await DataContext.ChannelsWithModel.AddAsync(new ChannelWithModel { LLMChannelId = channelId, ModelName = modelName, IsDeleted = false, - ApiBindingId = defaultBinding?.Id + ApiBindingId = await ResolveManualModelBindingAsync(channelId, modelName, defaultBinding?.Id) }); } } @@ -452,7 +461,7 @@ await DataContext.ChannelsWithModel.AddAsync(new ChannelWithModel { LLMChannelId = channelId, ModelName = modelName, IsDeleted = false, - ApiBindingId = defaultBinding?.Id + ApiBindingId = await ResolveManualModelBindingAsync(channelId, modelName, defaultBinding?.Id) }); } } @@ -877,6 +886,11 @@ internal static (LlmProtocol Protocol, LlmAuthProfile AuthProfile) MapProviderTo LLMProvider.LMStudio => (LlmProtocol.OpenAIChat, LlmAuthProfile.Bearer), LLMProvider.Anthropic => (LlmProtocol.AnthropicMessages, LlmAuthProfile.AnthropicApiKey), LLMProvider.ResponsesAPI => (LlmProtocol.OpenAIResponses, LlmAuthProfile.Bearer), + LLMProvider.Mistral => (LlmProtocol.MistralConversations, LlmAuthProfile.Bearer), + LLMProvider.AzureOpenAI => (LlmProtocol.AzureOpenAIResponses, LlmAuthProfile.Bearer), + LLMProvider.Vertex => (LlmProtocol.GoogleVertex, LlmAuthProfile.Bearer), + LLMProvider.Bedrock => (LlmProtocol.BedrockConverse, LlmAuthProfile.Bearer), + LLMProvider.Radius => (LlmProtocol.PiMessages, LlmAuthProfile.Bearer), _ => (LlmProtocol.OpenAIChat, LlmAuthProfile.Bearer) }; } @@ -891,6 +905,11 @@ internal static LLMProvider MapProtocolToProvider(LlmProtocol protocol) { LlmProtocol.AnthropicMessages => LLMProvider.Anthropic, LlmProtocol.Ollama => LLMProvider.Ollama, LlmProtocol.Gemini => LLMProvider.Gemini, + LlmProtocol.MistralConversations => LLMProvider.Mistral, + LlmProtocol.AzureOpenAIResponses => LLMProvider.AzureOpenAI, + LlmProtocol.GoogleVertex => LLMProvider.Vertex, + LlmProtocol.BedrockConverse => LLMProvider.Bedrock, + LlmProtocol.PiMessages => LLMProvider.Radius, _ => LLMProvider.OpenAI }; } diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs index 41d341bd..46e19b53 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs @@ -53,6 +53,9 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService { LLMConfState.AwaitingPresetSelection.GetDescription(), HandleAwaitingPresetSelectionAsync }, { LLMConfState.AwaitingPresetGateway.GetDescription(), HandleAwaitingPresetGatewayAsync }, { LLMConfState.AwaitingPresetApiKey.GetDescription(), HandleAwaitingPresetApiKeyAsync }, + { LLMConfState.AwaitingPresetProject.GetDescription(), HandleAwaitingPresetProjectAsync }, + { LLMConfState.AwaitingPresetLocation.GetDescription(), HandleAwaitingPresetLocationAsync }, + { LLMConfState.AwaitingPresetRegion.GetDescription(), HandleAwaitingPresetRegionAsync }, { LLMConfState.AwaitingGateway.GetDescription(), HandleAwaitingGatewayAsync }, { LLMConfState.AwaitingProvider.GetDescription(), HandleAwaitingProviderAsync }, { LLMConfState.AwaitingParallel.GetDescription(), HandleAwaitingParallelAsync }, @@ -110,6 +113,21 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService } var preset = LlmProviderCatalog.Presets[presetIndex - 1]; + if (preset.Setup == "vertex") { + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetProject.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 Google Cloud project ID:"); + } + if (preset.Setup == "bedrock") { + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetRegion.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 AWS region(例如 us-east-1):"); + } + if (preset.Setup == "azure") { + await redis.SetDataAsync(preset.Id); + await redis.SetStateAsync(LLMConfState.AwaitingPresetGateway.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的资源名或资源根地址(例如 my-resource 或 https://my-resource.openai.azure.com):"); + } if (preset.DefaultGateway == null) { await redis.SetDataAsync(preset.Id); await redis.SetStateAsync(LLMConfState.AwaitingPresetGateway.GetDescription()); @@ -133,7 +151,13 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService } var gateway = command.Trim(); - if (!Uri.TryCreate(gateway, UriKind.Absolute, out var uri) || + if (preset.Setup == "azure") { + try { + gateway = LlmPresetEndpoints.NormalizeAzure(gateway); + } catch (ArgumentException ex) { + return (false, ex.Message); + } + } else if (!Uri.TryCreate(gateway, UriKind.Absolute, out var uri) || ( uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps )) { return (false, "请输入有效的网关地址(以 http:// 或 https:// 开头)"); } @@ -153,10 +177,61 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService } var apiKey = command.Trim().Equals("-", StringComparison.OrdinalIgnoreCase) ? string.Empty : command.Trim(); - var gateway = parts.Length > 1 ? parts[1] : preset.DefaultGateway!; + string gateway; + try { + gateway = LlmPresetEndpoints.Compose(preset, data); + } catch (ArgumentException ex) { + return (false, ex.Message); + } return await CreatePresetChannelAsync(redis, preset, apiKey, gateway); } + private async Task<(bool, string)> HandleAwaitingPresetProjectAsync(EditLLMConfRedisHelper redis, string command) { + var preset = LlmProviderCatalog.FindById(await redis.GetDataAsync()); + if (preset == null) { + await redis.DeleteKeysAsync(); + return (false, "预设状态丢失,请重新发送 预制渠道"); + } + var project = command.Trim(); + if (project.Contains('|')) { + return (false, "project ID 不能包含 |"); + } + await redis.SetDataAsync($"{preset.Id}|{project}"); + await redis.SetStateAsync(LLMConfState.AwaitingPresetLocation.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 location(例如 us-central1):"); + } + + private async Task<(bool, string)> HandleAwaitingPresetLocationAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync() ?? string.Empty; + var preset = LlmProviderCatalog.FindById(data.Split('|')[0]); + if (preset == null) { + await redis.DeleteKeysAsync(); + return (false, "预设状态丢失,请重新发送 预制渠道"); + } + var location = command.Trim(); + if (location.Contains('|')) { + return (false, "location 不能包含 |"); + } + await redis.SetDataAsync($"{data}|{location}"); + await redis.SetStateAsync(LLMConfState.AwaitingPresetApiKey.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 API Key:"); + } + + private async Task<(bool, string)> HandleAwaitingPresetRegionAsync(EditLLMConfRedisHelper redis, string command) { + var preset = LlmProviderCatalog.FindById(await redis.GetDataAsync()); + if (preset == null) { + await redis.DeleteKeysAsync(); + return (false, "预设状态丢失,请重新发送 预制渠道"); + } + var region = command.Trim(); + if (region.Contains('|')) { + return (false, "region 不能包含 |"); + } + await redis.SetDataAsync($"{preset.Id}|{region}"); + await redis.SetStateAsync(LLMConfState.AwaitingPresetApiKey.GetDescription()); + return (true, $"请输入 {preset.DisplayName} 的 bearer token:"); + } + private async Task<(bool, string)> CreatePresetChannelAsync(EditLLMConfRedisHelper redis, LlmProviderPreset preset, string apiKey, string gateway) { var channelId = await Helper.AddChannel( preset.DisplayName,