refactor: 清理无用代码并统一网关处理逻辑

- 移除未使用的 using 和多余空行,优化代码可读性
- 统一 yi- 前缀模型名处理逻辑,减少重复代码
- 使用 EnsureSuccessStatusCode 简化图片上传错误处理流程
- 不影响现有功能,仅做代码结构和规范优化
This commit is contained in:
ccnetcore
2026-01-03 14:07:04 +08:00
parent 88fae0cdc2
commit 3447e2dc5d

View File

@@ -1,6 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
@@ -93,14 +92,12 @@ public class AiGateWayManager : DomainService
{
throw new UserFriendlyException($"【{modelId}】模型当前版本【{modelApiType}】格式不支持");
}
// ✅ 统一处理 yi- 后缀(网关层模型规范化)
if (!string.IsNullOrEmpty(aiModelDescribe.ModelId) &&
aiModelDescribe.ModelId.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
{
aiModelDescribe.ModelId = aiModelDescribe.ModelId[3..];
}
return aiModelDescribe;
}
@@ -546,11 +543,11 @@ public class AiGateWayManager : DomainService
{
request.Model = request.Model[3..];
}
var chatService =
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier);
if (userId is not null)
@@ -620,7 +617,7 @@ public class AiGateWayManager : DomainService
{
request.Model = request.Model[3..];
}
var completeChatResponse = chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken);
ThorUsageResponse? tokenUsage = null;
StringBuilder backupSystemContent = new StringBuilder();
@@ -980,7 +977,6 @@ public class AiGateWayManager : DomainService
}
private const string ImageStoreHost = "http://localhost:19001/api/app";
/// <summary>
/// Gemini 生成(Image)-非流式-缓存处理
/// 返回图片绝对路径
@@ -1017,13 +1013,7 @@ public class AiGateWayManager : DomainService
var uploadUrl = $"{ImageStoreHost}/ai-image/upload-base64";
var content = new StringContent(JsonSerializer.Serialize(imagePrefixBase64), Encoding.UTF8, "application/json");
var uploadResponse = await httpClient.PostAsync(uploadUrl, content, cancellationToken);
// uploadResponse.EnsureSuccessStatusCode();
if (uploadResponse.StatusCode != HttpStatusCode.OK)
{
var errorMessage = await uploadResponse.Content.ReadAsStringAsync(cancellationToken);
throw new UserFriendlyException($"{errorMessage}");
}
uploadResponse.EnsureSuccessStatusCode();
var storeUrl = await uploadResponse.Content.ReadAsStringAsync(cancellationToken);
var tokenUsage = new ThorUsageResponse