From fc48b84f80121fd3379b77488fa54d24ce00c999 Mon Sep 17 00:00:00 2001 From: zzh <1368735060@qq.com> Date: Wed, 28 Oct 2026 10:46:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20AI=20=E5=AF=B9=E8=AF=9D=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E5=A4=9A=E6=A8=A1=E5=9E=8B=20JSON=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E5=8F=96=20defaultModelId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 兼容 apiSecret 纯模型名与 {defaultModelId,models} 结构,避免多模型配置后请求失败。 Co-authored-by: Cursor --- src/Oms.Application/Shared/AiLlmClient.cs | 84 ++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Oms.Application/Shared/AiLlmClient.cs b/src/Oms.Application/Shared/AiLlmClient.cs index 0400994..eb263af 100644 --- a/src/Oms.Application/Shared/AiLlmClient.cs +++ b/src/Oms.Application/Shared/AiLlmClient.cs @@ -80,7 +80,11 @@ public class AiLlmClient throw new BusinessException($"请先配置 {provider.DisplayName} 的 API Key"); } - var model = modelOverride ?? provider.ApiSecretEnc; + var model = modelOverride; + if (string.IsNullOrWhiteSpace(model)) + { + model = ResolveConfiguredModelId(provider.ApiSecretEnc); + } if (string.IsNullOrWhiteSpace(model)) { model = DefaultModelForProviderType(provider.ProviderType); @@ -273,6 +277,84 @@ public class AiLlmClient "zhipu" => "glm-5.1", _ => null }; + + /// + /// 解析 ApiSecret:兼容纯模型名,或多模型 JSON + /// {"defaultModelId":"...","models":[{"modelId":"...","role":"文字"}]}。 + /// + private static string? ResolveConfiguredModelId(string? apiSecret) + { + var secret = (apiSecret ?? string.Empty).Trim(); + if (string.IsNullOrWhiteSpace(secret)) + { + return null; + } + + if (!secret.StartsWith('{')) + { + return secret; + } + + try + { + using var doc = JsonDocument.Parse(secret); + var root = doc.RootElement; + if (root.TryGetProperty("defaultModelId", out var defaultModel) && + defaultModel.ValueKind == JsonValueKind.String) + { + var id = defaultModel.GetString()?.Trim(); + if (!string.IsNullOrWhiteSpace(id)) + { + return id; + } + } + + if (root.TryGetProperty("models", out var models) && + models.ValueKind == JsonValueKind.Array) + { + foreach (var item in models.EnumerateArray()) + { + if (!item.TryGetProperty("modelId", out var modelIdEl) || + modelIdEl.ValueKind != JsonValueKind.String) + { + continue; + } + + var modelId = modelIdEl.GetString()?.Trim(); + if (string.IsNullOrWhiteSpace(modelId)) + { + continue; + } + + if (item.TryGetProperty("role", out var roleEl) && + roleEl.ValueKind == JsonValueKind.String && + string.Equals(roleEl.GetString(), "文字", StringComparison.Ordinal)) + { + return modelId; + } + } + + foreach (var item in models.EnumerateArray()) + { + if (item.TryGetProperty("modelId", out var modelIdEl) && + modelIdEl.ValueKind == JsonValueKind.String) + { + var modelId = modelIdEl.GetString()?.Trim(); + if (!string.IsNullOrWhiteSpace(modelId)) + { + return modelId; + } + } + } + } + } + catch (JsonException) + { + return secret; + } + + return null; + } } public static class AiLlmServiceExtensions