Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
2 changes: 1 addition & 1 deletion Docs/Bot_Commands_User_Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. 管理员发送预设编号
Expand Down
29 changes: 17 additions & 12 deletions TelegramSearchBot.Common/Model/AI/LLMProvider.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
7 changes: 6 additions & 1 deletion TelegramSearchBot.Common/Model/AI/LlmProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
157 changes: 157 additions & 0 deletions TelegramSearchBot.LLM.Test/Service/AI/LLM/PiProviderAlignmentTests.cs
Original file line number Diff line number Diff line change
@@ -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<ILogger>(), 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<LlmStreamEvent> Events, HttpRequestMessage Request, string Body)> Stream(ILlmTransport transport, RecordingHandler handler, string model) {
var events = new List<LlmStreamEvent>();
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<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
Request = request;
Body = request.Content == null ? null : await request.Content.ReadAsStringAsync(cancellationToken);
return new HttpResponseMessage(HttpStatusCode.OK) { Content = _response };
}
}
}
}
Loading
Loading