基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 97df0fb5ed49e37d1c8e0e62057bb72ba5c9eb6a
src/Moli.Desktop/Llm/OpenAICompatibleAdapter.cs
@@ -48,7 +48,11 @@
        if (!response.IsSuccessStatusCode)
        {
            throw new LlmProviderException(MapStatusCode(response.StatusCode), $"LLM provider returned {(int)response.StatusCode}.");
            var providerMessage = await ReadProviderErrorMessageAsync(response, cancellationToken).ConfigureAwait(false);
            var message = string.IsNullOrWhiteSpace(providerMessage)
                ? $"LLM provider returned {(int)response.StatusCode}."
                : $"LLM provider returned {(int)response.StatusCode}: {providerMessage}";
            throw new LlmProviderException(MapStatusCode(response.StatusCode), message);
        }
        await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
@@ -125,6 +129,48 @@
        (HttpStatusCode)429 => LlmErrorCodes.RateLimited,
        _ => LlmErrorCodes.EndpointUnavailable
    };
    private async Task<string?> ReadProviderErrorMessageAsync(HttpResponseMessage response, CancellationToken cancellationToken)
    {
        var body = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
        if (string.IsNullOrWhiteSpace(body))
        {
            return null;
        }
        try
        {
            using var document = JsonDocument.Parse(body);
            var root = document.RootElement;
            if (root.TryGetProperty("error", out var error))
            {
                if (error.ValueKind == JsonValueKind.Object &&
                    error.TryGetProperty("message", out var message) &&
                    message.ValueKind == JsonValueKind.String)
                {
                    return message.GetString();
                }
                if (error.ValueKind == JsonValueKind.String)
                {
                    return error.GetString();
                }
            }
            if (root.TryGetProperty("message", out var rootMessage) &&
                rootMessage.ValueKind == JsonValueKind.String)
            {
                return rootMessage.GetString();
            }
        }
        catch (JsonException)
        {
            // Fall through to a trimmed body snippet for non-JSON provider errors.
        }
        var trimmed = body.Trim();
        return trimmed.Length <= 500 ? trimmed : trimmed[..500];
    }
}
public sealed class LlmProviderException : Exception