diff --git a/Docs/Bot_Commands_User_Guide.md b/Docs/Bot_Commands_User_Guide.md index 12ff7d20..0b9797c3 100644 --- a/Docs/Bot_Commands_User_Guide.md +++ b/Docs/Bot_Commands_User_Guide.md @@ -182,7 +182,7 @@ 1. 管理员发送: `编辑渠道` 2. 机器人回复: (列出现有渠道) `请选择要编辑的渠道ID:` 3. 管理员发送: (选择一个渠道ID,例如 `1`) - 4. 机器人回复: (列出可编辑字段) `请选择要编辑的字段:\n1. 名称 (当前值)\n2. 地址 (当前值)\n3. 类型 (当前值)\n4. API Key\n5. 最大并行数量 (当前值)\n6. 优先级 (当前值)` + 4. 机器人回复: (列出可编辑字段) `请选择要编辑的字段:\n1. 名称 (当前值)\n2. 地址 (当前值)\n3. 类型 (当前值)\n4. API Key\n5. 最大并行数量 (当前值)\n6. 优先级 (当前值)\n7. 协议绑定` 5. 管理员发送: (选择字段编号) 6. **如果选择 `1`, `2`, `4`, `5`, `6`**: - 机器人回复: `请输入新的值:` @@ -191,9 +191,14 @@ 6. **如果选择 `3` (类型)**: - 机器人回复: `请选择渠道类型:\n1. OpenAI\n2. Ollama` - 管理员发送: (输入选项编号,例如 `1`) + 6. **如果选择 `7` (协议绑定)**(多协议网关用,如 OpenCode Zen/Go): + - 机器人回复: (列出绑定) `渠道 x 的协议绑定:\n3. https://opencode.ai/zen/v1 (OpenAIChat/Bearer) [默认]\n...` + - 管理员发送: 绑定ID(设为默认)或 `0`(新增绑定) + - 新增时依次输入:端点地址 → 线协议(OpenAIChat/OpenAIResponses/AnthropicMessages/Ollama/Gemini)→ 认证方式(Bearer/AnthropicApiKey/None) + - 机器人回复: `绑定创建成功(ID: x,协议/认证)` 7. 管理员发送: (输入新值或选择编号) 8. 机器人回复: `更新成功` (或失败信息) - * **`移除模型` 指令的交互流程示例:** + * **`移除模型` 指令的交互流程示例:**(按模型行删除:多 binding 下同名模型会带 `[渠道/binding/协议]` 标注,删错行不会发生) 1. 管理员发送: `移除模型` 2. 机器人回复: (列出现有渠道) `请选择要移除模型的渠道ID:` 3. 管理员发送: (选择一个渠道ID,例如 `1`) diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs index 705436b4..1a5f94c0 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfHelperTest.cs @@ -496,9 +496,8 @@ private async Task SeedChannelWithDefaultBinding(int id, string n [Fact] public async Task RefreshAllChannel_OpenCodeBinding_CreatesNoRowsAndSoftDeletesNothing() { - // Arrange: OpenCode 默认 binding(opencode.ai/zen/* 空间)——目录不是授权快照, - // 刷新不得创建行、不得软删/复活任何行(blueprint §四.1/.5) - const string openCodeEndpoint = "https://opencode.ai/zen/go/v1/chat/completions"; + // Arrange: OpenCode Zen 是按量计费目录(非授权快照),刷新不得创建行、不得软删/复活任何行(blueprint §四.1/.5) + const string openCodeEndpoint = "https://opencode.ai/zen/v1/chat/completions"; var binding = await SeedChannelWithDefaultBinding(20, "OpenCode", LLMProvider.OpenAI, gateway: openCodeEndpoint, endpoint: openCodeEndpoint); await _context.ChannelsWithModel.AddRangeAsync(new[] { @@ -738,6 +737,103 @@ public async Task SetModelPreferred_UnknownRowRejected() { Assert.False(ok); } + /// OpenCode Go 是订阅网关(目录即授权):只增不删;既有行/手工行不被覆盖或删除。 + [Fact] + public async Task RefreshAllChannel_OpenCodeGo_SyncsCatalogAddOnly() { + // Arrange + const string goEndpoint = "https://opencode.ai/zen/go/v1"; + var binding = await SeedChannelWithDefaultBinding(40, "OpenCode Go", LLMProvider.OpenAI, + gateway: goEndpoint, endpoint: goEndpoint); + await _context.ChannelsWithModel.AddRangeAsync(new[] { + new ChannelWithModel { LLMChannelId = 40, ModelName = "existing-manual", IsDeleted = false, AuthorizationSource = AuthorizationSource.Manual }, + new ChannelWithModel { LLMChannelId = 40, ModelName = "restored-discovered", IsDeleted = true, AuthorizationSource = AuthorizationSource.Discovered, ApiBindingId = binding.Id }, + new ChannelWithModel { LLMChannelId = 40, ModelName = "catalog-dropped", IsDeleted = false, AuthorizationSource = AuthorizationSource.Discovered, ApiBindingId = binding.Id } + }); + await _context.SaveChangesAsync(); + + _openAIServiceMock.Setup(o => o.GetAllModels(It.IsAny(), It.IsAny())) + .ReturnsAsync(new List { "new-model", "existing-manual", "restored-discovered" }); + + // Act + await _helper.RefreshAllChannel(); + + // Assert: 新模型以 Discovered 行加入;既有行不重复;软删行恢复;目录里没有的行不被软删 + var rows = await _context.ChannelsWithModel.Where(m => m.LLMChannelId == 40).ToListAsync(); + Assert.Single(rows.Where(r => r.ModelName == "new-model")); + Assert.Single(rows.Where(r => r.ModelName == "existing-manual")); + Assert.False(rows.Single(r => r.ModelName == "restored-discovered").IsDeleted); + Assert.False(rows.Single(r => r.ModelName == "catalog-dropped").IsDeleted); + } + + [Fact] + public async Task PromoteBinding_SwitchesDefaultAndMirrorsChannel() { + // Arrange + var defaultBinding = await SeedChannelWithDefaultBinding(41, "Multi Prot", LLMProvider.OpenAI, gateway: "http://a", endpoint: "http://a"); + var responsesBinding = new LLMApiBinding { + LLMChannelId = 41, + Endpoint = "http://b", + Protocol = LlmProtocol.OpenAIResponses, + AuthProfile = LlmAuthProfile.Bearer, + IsDefault = false + }; + await _context.LLMApiBindings.AddAsync(responsesBinding); + await _context.SaveChangesAsync(); + + // Act + var ok = await _helper.PromoteBinding(41, responsesBinding.Id); + + // Assert + Assert.True(ok); + Assert.True((await _context.LLMApiBindings.FindAsync(responsesBinding.Id))!.IsDefault); + Assert.False((await _context.LLMApiBindings.FindAsync(defaultBinding.Id))!.IsDefault); + var channel = await _context.LLMChannels.FindAsync(41); + Assert.Equal("http://b", channel.Gateway); + Assert.Equal(LLMProvider.ResponsesAPI, channel.Provider); + } + + [Fact] + public async Task GetModelRowsByChannelId_QualifiesDuplicateNamesAndRemoveModelRowKeepsTheOther() { + // Arrange + var bindingA = await SeedChannelWithDefaultBinding(42, "Multi Model", LLMProvider.OpenAI, gateway: "http://a", endpoint: "http://a"); + var bindingB = new LLMApiBinding { LLMChannelId = 42, Endpoint = "http://b", Protocol = LlmProtocol.OpenAIResponses, AuthProfile = LlmAuthProfile.Bearer, IsDefault = false }; + await _context.LLMApiBindings.AddAsync(bindingB); + await _context.SaveChangesAsync(); + + var rowA = new ChannelWithModel { LLMChannelId = 42, ModelName = "shared-model", ApiBindingId = bindingA.Id }; + var rowB = new ChannelWithModel { LLMChannelId = 42, ModelName = "shared-model", ApiBindingId = bindingB.Id }; + var rowC = new ChannelWithModel { LLMChannelId = 42, ModelName = "single-model", ApiBindingId = bindingA.Id }; + await _context.ChannelsWithModel.AddRangeAsync(rowA, rowB, rowC); + await _context.SaveChangesAsync(); + + // Act + var rows = await _helper.GetModelRowsByChannelId(42); + + // Assert: 重名行带协议标注,唯一行保持原名 + Assert.Equal(3, rows.Count); + Assert.Equal(2, rows.Count(r => r.Display.Contains("shared-model ["))); + Assert.Contains(rows, r => r.Display == "single-model"); + + // 按行 Id 删除只影响目标行 + var rowToRemove = rows.First(r => r.RowId == rowB.Id); + Assert.True(await _helper.RemoveModelRow(rowToRemove.RowId)); + var remaining = await _context.ChannelsWithModel.Where(m => m.LLMChannelId == 42).ToListAsync(); + Assert.Equal(2, remaining.Count); + Assert.DoesNotContain(remaining, r => r.Id == rowB.Id); + Assert.Contains(remaining, r => r.Id == rowA.Id); + } + + [Fact] + public async Task GetBindings_ReturnsChannelBindingsOrderedById() { + await SeedChannelWithDefaultBinding(43, "Bindings", LLMProvider.OpenAI, gateway: "http://a", endpoint: "http://a"); + await _context.LLMApiBindings.AddAsync(new LLMApiBinding { LLMChannelId = 43, Endpoint = "http://b", Protocol = LlmProtocol.OpenAIResponses, AuthProfile = LlmAuthProfile.Bearer }); + await _context.SaveChangesAsync(); + + var bindings = await _helper.GetBindings(43); + + Assert.Equal(2, bindings.Count); + Assert.True(bindings[0].Id < bindings[1].Id); + } + [Fact] public async Task UpdateChannel_GatewayAndProviderChange_SyncsDefaultBinding() { // Arrange diff --git a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs index e02de206..f6c9efc2 100644 --- a/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs +++ b/TelegramSearchBot.Test/Manage/EditLLMConfTest.cs @@ -255,61 +255,43 @@ public async Task ExecuteAsync_RemoveModel_CompleteFlow() { ApiKey = "test-key", Provider = LLMProvider.OpenAI }; - await _context.LLMChannels.AddAsync(channel); - await _context.ChannelsWithModel.AddAsync(new ChannelWithModel { - LLMChannelId = 1, - ModelName = "model1" - }); - // Add model2 to the in-memory database - await _context.ChannelsWithModel.AddAsync(new ChannelWithModel { - LLMChannelId = 1, - ModelName = "model2" - }); - await _context.SaveChangesAsync(); var stateKey = $"llmconf:{chatId}:state"; var dataKey = $"llmconf:{chatId}:data"; - // Setup state transitions + // 1. initial state, 2. state after "移除模型", 3. state after channel input, 4. row id payload _dbMock.SetupSequence(d => d.StringGetAsync(It.IsAny(), It.IsAny())) - .ReturnsAsync(RedisValue.Null) // 1. Initial state before "移除模型" - .ReturnsAsync("removing_model_select_channel") // 2. State after "移除模型" command - .ReturnsAsync("removing_model_select") // 3. State after channel ID input - .ReturnsAsync("1|model1,model2"); // 4. Data read by HandleRemovingModelSelectAsync - - // Setup helper mock to return channel - helperMock.Setup(h => h.GetChannelById(1)) - .ReturnsAsync(channel); - - // Setup helper mock to return a channel in GetAllChannels, which should trigger GetModelsByChannelId in service (but service doesn't call helper here) - // This setup is still needed for the initial "移除模型" command which lists channels. - helperMock.Setup(h => h.GetAllChannels()) - .ReturnsAsync(new List { channel }); + .ReturnsAsync(RedisValue.Null) + .ReturnsAsync("removing_model_select_channel") + .ReturnsAsync("removing_model_select") + .ReturnsAsync("1|101,102"); + + helperMock.Setup(h => h.GetChannelById(1)).ReturnsAsync(channel); + helperMock.Setup(h => h.GetAllChannels()).ReturnsAsync(new List { channel }); + helperMock.Setup(h => h.GetModelRowsByChannelId(1)).ReturnsAsync(new List<(long, string, string)> { + (101, "model1", "model1"), + (102, "model2", "model2") + }); + helperMock.Setup(h => h.RemoveModelRow(It.IsAny())).ReturnsAsync(true); // Act & Assert var result1 = await _service.ExecuteAsync("移除模型", chatId); Assert.True(result1.Item1); Assert.Contains("请选择要移除模型的渠道ID:", result1.Item2); - // Verify the in-memory database contains the expected models - var modelsInDb = await _context.ChannelsWithModel.Where(m => m.LLMChannelId == 1).ToListAsync(); - Assert.NotNull(modelsInDb); - Assert.Equal(2, modelsInDb.Count); - Assert.Contains(modelsInDb, m => m.ModelName == "model1"); - Assert.Contains(modelsInDb, m => m.ModelName == "model2"); - - var result2 = await _service.ExecuteAsync("1", chatId); // User inputs channel ID + var result2 = await _service.ExecuteAsync("1", chatId); Assert.True(result2.Item1); - // Verify the response lists models Assert.Contains("请选择要移除的模型:", result2.Item2); Assert.Contains("1. model1", result2.Item2); + Assert.Contains("2. model2", result2.Item2); - var result3 = await _service.ExecuteAsync("1", chatId); // User inputs model index (1 for model1) + var result3 = await _service.ExecuteAsync("1", chatId); // 选择第一行 Assert.True(result3.Item1); Assert.Equal("模型移除成功", result3.Item2); - // Verify model was removed by helper (as service calls helper) - helperMock.Verify(h => h.RemoveModelFromChannel(1, "model1"), Times.Once); + // 按行 Id 删除(不是按模型名,多 binding 下不会删错行) + helperMock.Verify(h => h.RemoveModelRow(101), Times.Once); + helperMock.Verify(h => h.RemoveModelRow(It.IsAny()), Times.Once); // Verify Redis keys are deleted _dbMock.Verify(d => d.KeyDeleteAsync(stateKey, It.IsAny()), Times.Once); @@ -553,5 +535,83 @@ public async Task ExecuteAsync_PresetOpenCodeGo_CreatesResponseBindingAndAssigns helperMock.Verify(h => h.AssignModelBinding(7, "glm-5.3", It.IsAny()), Times.Never); helperMock.Verify(h => h.AssignModelBinding(7, It.IsAny(), It.IsAny()), Times.Exactly(2)); } + + [Fact] + public async Task ExecuteAsync_EditChannelBindings_PromotesSelectedBinding() { + // Arrange + long chatId = 123; + var channel = new LLMChannel { Id = 1, Name = "Test Channel", Provider = LLMProvider.OpenAI, Gateway = "http://test.com" }; + var binding = new LLMApiBinding { Id = 5, LLMChannelId = 1, Endpoint = "http://test.com/v1", Protocol = LlmProtocol.OpenAIResponses, AuthProfile = LlmAuthProfile.Bearer }; + + _dbMock.SetupSequence(d => d.StringGetAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(RedisValue.Null) + .ReturnsAsync("editing_select_channel") + .ReturnsAsync("editing_select_field") + .ReturnsAsync("1") + .ReturnsAsync("editing_select_binding") + .ReturnsAsync("1|7"); + + helperMock.Setup(h => h.GetAllChannels()).ReturnsAsync(new List { channel }); + helperMock.Setup(h => h.GetChannelById(1)).ReturnsAsync(channel); + helperMock.Setup(h => h.GetBindings(1)).ReturnsAsync(new List { binding }); + helperMock.Setup(h => h.PromoteBinding(1, 5)).ReturnsAsync(true); + + // Act + var result1 = await _service.ExecuteAsync("编辑渠道", chatId); + Assert.True(result1.Item1); + + var result2 = await _service.ExecuteAsync("1", chatId); + Assert.Contains("7. 协议绑定", result2.Item2); + + var result3 = await _service.ExecuteAsync("7", chatId); + Assert.True(result3.Item1); + Assert.Contains("5. http://test.com/v1 (OpenAIResponses/Bearer)", result3.Item2); + Assert.Contains("0 新增绑定", result3.Item2); + + var result4 = await _service.ExecuteAsync("5", chatId); + + // Assert + Assert.True(result4.Item1); + Assert.Contains("设为默认绑定", result4.Item2); + helperMock.Verify(h => h.PromoteBinding(1, 5), Times.Once); + } + + [Fact] + public async Task ExecuteAsync_EditChannelBindings_AddsNewBinding() { + // Arrange + long chatId = 123; + var existing = new LLMApiBinding { Id = 3, LLMChannelId = 1, Endpoint = "https://opencode.ai/zen/v1", Protocol = LlmProtocol.OpenAIChat, AuthProfile = LlmAuthProfile.Bearer, IsDefault = true }; + + _dbMock.SetupSequence(d => d.StringGetAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync("editing_select_field").ReturnsAsync("1") + .ReturnsAsync("editing_select_binding").ReturnsAsync("1|7") + .ReturnsAsync("editing_binding_endpoint").ReturnsAsync("1|new") + .ReturnsAsync("editing_binding_protocol").ReturnsAsync("1|https://opencode.ai/zen/v1") + .ReturnsAsync("editing_binding_auth").ReturnsAsync("1|https://opencode.ai/zen/v1|OpenAIResponses"); + + helperMock.Setup(h => h.GetBindings(1)).ReturnsAsync(new List { existing }); + helperMock.Setup(h => h.EnsureBinding(1, "https://opencode.ai/zen/v1", LlmProtocol.OpenAIResponses, LlmAuthProfile.Bearer)).ReturnsAsync(55); + + // Act + var openBindings = await _service.ExecuteAsync("7", chatId); + Assert.Contains("3. https://opencode.ai/zen/v1", openBindings.Item2); + + var askEndpoint = await _service.ExecuteAsync("0", chatId); + Assert.Contains("请输入新绑定的端点地址", askEndpoint.Item2); + + var askProtocol = await _service.ExecuteAsync("https://opencode.ai/zen/v1", chatId); + Assert.Contains("请选择线协议", askProtocol.Item2); + Assert.Contains("2. OpenAIResponses", askProtocol.Item2); + + var askAuth = await _service.ExecuteAsync("2", chatId); + Assert.Contains("请选择认证方式", askAuth.Item2); + + var created = await _service.ExecuteAsync("1", chatId); + + // Assert + Assert.True(created.Item1); + Assert.Contains("绑定创建成功(ID: 55", created.Item2); + helperMock.Verify(h => h.EnsureBinding(1, "https://opencode.ai/zen/v1", LlmProtocol.OpenAIResponses, LlmAuthProfile.Bearer), Times.Once); + } } } diff --git a/TelegramSearchBot/Interface/Manage/IEditLLMConfHelper.cs b/TelegramSearchBot/Interface/Manage/IEditLLMConfHelper.cs index 528e5545..d3740f14 100644 --- a/TelegramSearchBot/Interface/Manage/IEditLLMConfHelper.cs +++ b/TelegramSearchBot/Interface/Manage/IEditLLMConfHelper.cs @@ -37,5 +37,20 @@ public interface IEditLLMConfHelper { /// 把模型行改挂到指定 binding,并标记为模型级协议覆盖;成功返回 true。 Task AssignModelBinding(int channelId, string modelName, int bindingId); + + /// 渠道下的全部协议绑定(按 Id 升序)。 + Task> GetBindings(int channelId); + + /// 把指定 binding 设为渠道默认(其余降级),并镜像 channel.Gateway/Provider;成功返回 true。 + Task PromoteBinding(int channelId, int bindingId); + + /// + /// 渠道下的模型行(按行 Id),展示格式与 GetModelsByChannelId 一致(多 binding 时带 [channel/binding/protocol]); + /// 移除模型按行 Id 删除,避免多 binding 下删错行。 + /// + Task> GetModelRowsByChannelId(long channelId); + + /// 按行 Id 删除模型行;行不存在返回 false。 + Task RemoveModelRow(long rowId); } } diff --git a/TelegramSearchBot/Model/AI/LLMConfState.cs b/TelegramSearchBot/Model/AI/LLMConfState.cs index 1734008a..723d9954 100644 --- a/TelegramSearchBot/Model/AI/LLMConfState.cs +++ b/TelegramSearchBot/Model/AI/LLMConfState.cs @@ -32,6 +32,18 @@ public enum LLMConfState { [Description("editing_select_channel")] EditingSelectChannel, + [Description("editing_select_binding")] + EditingSelectBinding, + + [Description("editing_binding_endpoint")] + EditingBindingEndpoint, + + [Description("editing_binding_protocol")] + EditingBindingProtocol, + + [Description("editing_binding_auth")] + EditingBindingAuth, + [Description("adding_model_select_channel")] AddingModelSelectChannel, diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs index 9604212e..d50a5b8e 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfHelper.cs @@ -761,6 +761,82 @@ public async Task AssignModelBinding(int channelId, string modelName, int } } + /// 渠道下的全部协议绑定(按 Id 升序)。 + public async Task> GetBindings(int channelId) { + try { + return await DataContext.LLMApiBindings + .Where(b => b.LLMChannelId == channelId) + .OrderBy(b => b.Id) + .ToListAsync(); + } catch { + return new List(); + } + } + + /// 把指定 binding 设为渠道默认(其余降级),并镜像 channel.Gateway/Provider(blueprint §七)。 + public async Task PromoteBinding(int channelId, int bindingId) { + try { + var channel = await DataContext.LLMChannels.Include(c => c.Bindings).FirstOrDefaultAsync(c => c.Id == channelId); + if (channel == null) { + return false; + } + + var target = channel.Bindings.FirstOrDefault(b => b.Id == bindingId); + if (target == null) { + _logger.LogWarning("设置默认 binding 失败:渠道 {ChannelId} 不存在 binding {BindingId}", channelId, bindingId); + return false; + } + + foreach (var binding in channel.Bindings) { + binding.IsDefault = binding.Id == target.Id; + } + channel.Gateway = target.Endpoint; + channel.Provider = MapProtocolToProvider(target.Protocol); + await DataContext.SaveChangesAsync(); + return true; + } catch (Exception ex) { + _logger.LogError(ex, "把 binding {BindingId} 设为渠道 {ChannelId} 默认失败", bindingId, channelId); + return false; + } + } + + /// 渠道下的模型行(按行 Id),展示格式与 GetModelsByChannelId 一致(多 binding 带协议标注)。 + public async Task> GetModelRowsByChannelId(long channelId) { + var rows = await DataContext.ChannelsWithModel + .Include(c => c.ApiBinding) + .Include(c => c.LLMChannel) + .Where(c => c.LLMChannelId == channelId && !c.IsDeleted) + .OrderBy(c => c.ModelName).ThenBy(c => c.Id) + .ToListAsync(); + + var multiBindingNames = rows + .GroupBy(r => r.ModelName, StringComparer.OrdinalIgnoreCase) + .Where(g => g.Count() > 1) + .Select(g => g.Key) + .ToHashSet(StringComparer.OrdinalIgnoreCase); + + return rows + .Select(r => ((long)r.Id, r.ModelName, multiBindingNames.Contains(r.ModelName) ? FormatModelDisplay(r) : r.ModelName)) + .ToList(); + } + + /// 按行 Id 删除模型行(多 binding 下不会误删同名的其它行)。 + public async Task RemoveModelRow(long rowId) { + try { + var row = await DataContext.ChannelsWithModel.FirstOrDefaultAsync(m => m.Id == rowId); + if (row == null) { + return false; + } + + DataContext.ChannelsWithModel.Remove(row); + await DataContext.SaveChangesAsync(); + return true; + } catch (Exception ex) { + _logger.LogError(ex, "删除模型行 {RowId} 失败", rowId); + return false; + } + } + /// /// channel Gateway/Provider 变更时同步默认 binding(镜像规则,blueprint §七)。 /// 无默认 binding 时不创建(补建由 EnsureDefaultBinding 在 AddChannel/RefreshAllChannel 负责)。 diff --git a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs index 2a268d48..41d341bd 100644 --- a/TelegramSearchBot/Service/Manage/EditLLMConfService.cs +++ b/TelegramSearchBot/Service/Manage/EditLLMConfService.cs @@ -61,6 +61,10 @@ MusicGenerationToolSettingsService musicGenerationToolSettingsService { LLMConfState.SettingAltPhotoModel.GetDescription(), HandleSettingAltPhotoModelAsync }, { LLMConfState.EditingSelectChannel.GetDescription(), HandleEditingSelectChannelAsync }, { LLMConfState.EditingSelectField.GetDescription(), HandleEditingSelectFieldAsync }, + { LLMConfState.EditingSelectBinding.GetDescription(), HandleEditingSelectBindingAsync }, + { LLMConfState.EditingBindingEndpoint.GetDescription(), HandleEditingBindingEndpointAsync }, + { LLMConfState.EditingBindingProtocol.GetDescription(), HandleEditingBindingProtocolAsync }, + { LLMConfState.EditingBindingAuth.GetDescription(), HandleEditingBindingAuthAsync }, { LLMConfState.AddingModelSelectChannel.GetDescription(), HandleAddingModelSelectChannelAsync }, { LLMConfState.AddingModelInput.GetDescription(), HandleAddingModelInputAsync }, { LLMConfState.RemovingModelSelectChannel.GetDescription(), HandleRemovingModelSelectChannelAsync }, @@ -341,7 +345,7 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration await redis.SetDataAsync(channelId.ToString()); await redis.SetStateAsync(LLMConfState.EditingSelectField.GetDescription()); - return (true, $"请选择要编辑的字段:\n1. 名称 ({channel.Name})\n2. 地址 ({channel.Gateway})\n3. 类型 ({channel.Provider})\n4. API Key\n5. 最大并行数量 ({channel.Parallel})\n6. 优先级 ({channel.Priority})"); + return (true, $"请选择要编辑的字段:\n1. 名称 ({channel.Name})\n2. 地址 ({channel.Gateway})\n3. 类型 ({channel.Provider})\n4. API Key\n5. 最大并行数量 ({channel.Parallel})\n6. 优先级 ({channel.Priority})\n7. 协议绑定"); } private async Task<(bool, string)> HandleEditingSelectFieldAsync(EditLLMConfRedisHelper redis, string command) { @@ -349,6 +353,10 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration var editChannelId = int.Parse(value); await redis.SetDataAsync($"{editChannelId}|{command}"); + if (command == "7") { + return await ShowBindingSelectionAsync(redis, editChannelId); + } + if (command == "3") { await redis.SetStateAsync(LLMConfState.EditingInputValue.GetDescription()); @@ -369,6 +377,135 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration } } + /// 列出渠道的协议绑定并询问操作(输入编号设为默认,0 新增)。 + private async Task<(bool, string)> ShowBindingSelectionAsync(EditLLMConfRedisHelper redis, int channelId) { + var bindings = await Helper.GetBindings(channelId); + if (bindings.Count == 0) { + await redis.DeleteKeysAsync(); + return (true, "该渠道没有任何协议绑定"); + } + + var sb = new StringBuilder($"渠道 {channelId} 的协议绑定:\n"); + foreach (var binding in bindings) { + var marker = binding.IsDefault ? " [默认]" : string.Empty; + sb.AppendLine($"{binding.Id}. {binding.Endpoint} ({binding.Protocol}/{binding.AuthProfile}){marker}"); + } + sb.Append("请输入要设为默认的绑定ID,或发送 0 新增绑定:"); + + await redis.SetStateAsync(LLMConfState.EditingSelectBinding.GetDescription()); + return (true, sb.ToString()); + } + + private async Task<(bool, string)> HandleEditingSelectBindingAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync(); + var parts = data.Split('|'); + if (parts.Length < 2 || !int.TryParse(parts[0], out var channelId)) { + await redis.DeleteKeysAsync(); + return (true, "内部错误:渠道状态丢失"); + } + + var bindings = await Helper.GetBindings(channelId); + var cmd = command.Trim(); + + if (cmd == "0" || cmd.Equals("新增", StringComparison.OrdinalIgnoreCase)) { + await redis.SetDataAsync($"{channelId}|new"); + await redis.SetStateAsync(LLMConfState.EditingBindingEndpoint.GetDescription()); + return (true, "请输入新绑定的端点地址(例如 https://opencode.ai/zen/v1):"); + } + + if (!int.TryParse(cmd, out var bindingId) || bindings.All(b => b.Id != bindingId)) { + return (false, "请输入列表中的绑定ID,或发送 0 新增绑定"); + } + + if (!await Helper.PromoteBinding(channelId, bindingId)) { + await redis.DeleteKeysAsync(); + return (true, "设置为默认绑定失败"); + } + + await redis.DeleteKeysAsync(); + return (true, $"已将 binding {bindingId} 设为默认绑定(渠道地址与类型已同步)。"); + } + + private async Task<(bool, string)> HandleEditingBindingEndpointAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync(); + var parts = data.Split('|'); + if (parts.Length < 2 || !int.TryParse(parts[0], out var channelId)) { + await redis.DeleteKeysAsync(); + return (true, "内部错误:渠道状态丢失"); + } + + var endpoint = command.Trim(); + if (!Uri.TryCreate(endpoint, UriKind.Absolute, out var uri) || + ( uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps )) { + return (false, "请输入有效的端点地址(以 http:// 或 https:// 开头)"); + } + + await redis.SetDataAsync($"{channelId}|{endpoint}"); + await redis.SetStateAsync(LLMConfState.EditingBindingProtocol.GetDescription()); + return (true, FormatProtocolOptions()); + } + + private async Task<(bool, string)> HandleEditingBindingProtocolAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync(); + var parts = data.Split('|'); + if (parts.Length < 2 || !int.TryParse(parts[0], out var channelId)) { + await redis.DeleteKeysAsync(); + return (true, "内部错误:渠道状态丢失"); + } + + var protocols = Enum.GetValues(); + if (!int.TryParse(command.Trim(), out var index) || index < 1 || index > protocols.Length) { + return (false, $"请输入 1 到 {protocols.Length} 之间的数字"); + } + + await redis.SetDataAsync($"{channelId}|{parts[1]}|{protocols[index - 1]}"); + await redis.SetStateAsync(LLMConfState.EditingBindingAuth.GetDescription()); + return (true, FormatAuthOptions()); + } + + private async Task<(bool, string)> HandleEditingBindingAuthAsync(EditLLMConfRedisHelper redis, string command) { + var data = await redis.GetDataAsync(); + var parts = data.Split('|', 3); + if (parts.Length < 3 || !int.TryParse(parts[0], out var channelId)) { + await redis.DeleteKeysAsync(); + return (true, "内部错误:绑定状态丢失"); + } + + var authProfiles = Enum.GetValues(); + if (!int.TryParse(command.Trim(), out var index) || index < 1 || index > authProfiles.Length) { + return (false, $"请输入 1 到 {authProfiles.Length} 之间的数字"); + } + + if (!Enum.TryParse(parts[2], out var protocol)) { + await redis.DeleteKeysAsync(); + return (true, "内部错误:协议解析失败"); + } + + var bindingId = await Helper.EnsureBinding(channelId, parts[1], protocol, authProfiles[index - 1]); + await redis.DeleteKeysAsync(); + return bindingId > 0 + ? (true, $"绑定创建成功(ID: {bindingId},{protocol}/{authProfiles[index - 1]})。可用 `编辑渠道` 第 7 项把它设为默认。") + : (true, "绑定创建失败"); + } + + private static string FormatProtocolOptions() { + var protocols = Enum.GetValues(); + var sb = new StringBuilder("请选择线协议:\n"); + for (var i = 0; i < protocols.Length; i++) { + sb.AppendLine($"{i + 1}. {protocols[i]}"); + } + return sb.ToString().TrimEnd(); + } + + private static string FormatAuthOptions() { + var authProfiles = Enum.GetValues(); + var sb = new StringBuilder("请选择认证方式:\n"); + for (var i = 0; i < authProfiles.Length; i++) { + sb.AppendLine($"{i + 1}. {authProfiles[i]}"); + } + return sb.ToString().TrimEnd(); + } + private async Task<(bool, string)> HandleAddingModelSelectChannelAsync(EditLLMConfRedisHelper redis, string command) { if (!int.TryParse(command, out var addModelChannelId)) { return (false, "请输入有效的渠道ID"); @@ -404,23 +541,20 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration return (true, "找不到指定的渠道"); } - // 获取该渠道下的所有模型 - var models = await DataContext.ChannelsWithModel - .Where(m => m.LLMChannelId == removeModelChannelId && !m.IsDeleted) - .Select(m => m.ModelName) - .ToListAsync(); + // 获取该渠道下的所有模型行(多 binding 时带协议标注;按行 Id 删除避免误删同名行) + var modelRows = await Helper.GetModelRowsByChannelId(removeModelChannelId); - if (models.Count == 0) { + if (modelRows.Count == 0) { return (true, "该渠道下没有可移除的模型"); } var sb = new StringBuilder(); sb.AppendLine("请选择要移除的模型:"); - for (int i = 0; i < models.Count; i++) { - sb.AppendLine($"{i + 1}. {models[i]}"); + for (int i = 0; i < modelRows.Count; i++) { + sb.AppendLine($"{i + 1}. {modelRows[i].Display}"); } - await redis.SetDataAsync($"{removeModelChannelId}|{string.Join(",", models)}"); + await redis.SetDataAsync($"{removeModelChannelId}|{string.Join(",", modelRows.Select(r => r.RowId))}"); await redis.SetStateAsync(LLMConfState.RemovingModelSelect.GetDescription()); return (true, sb.ToString()); } @@ -433,23 +567,21 @@ await DataContext.AppConfigurationItems.AddAsync(new Model.Data.AppConfiguration return (false, "内部错误:模型数据格式不正确"); } - var removeChannelId = int.Parse(parts[0]); - var modelList = parts[1].Split(','); + var rowIds = parts[1].Split(',', StringSplitOptions.RemoveEmptyEntries); - if (modelList.Length == 0 || ( modelList.Length == 1 && string.IsNullOrEmpty(modelList[0]) )) { + if (rowIds.Length == 0) { return (true, "该渠道下没有可移除的模型"); } - if (!int.TryParse(command, out var modelIndex) || modelIndex < 1 || modelIndex > modelList.Length) { + if (!int.TryParse(command, out var modelIndex) || modelIndex < 1 || modelIndex > rowIds.Length) { return (true, "请输入有效的模型序号"); } - if (modelIndex - 1 < 0 || modelIndex - 1 >= modelList.Length) { - return (true, "内部错误:无效的模型序号"); + if (!long.TryParse(rowIds[modelIndex - 1], out var rowId)) { + return (true, "内部错误:模型行ID无效"); } - var modelName = modelList[modelIndex - 1]; - var removeResult = await Helper.RemoveModelFromChannel(removeChannelId, modelName); + var removeResult = await Helper.RemoveModelRow(rowId); // 清理状态 await redis.DeleteKeysAsync();