基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-07-29 cf40cded1708bb303be4a404b7aceddd196e1d46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Net;
using System.Text;
using Moli.Desktop.Llm;
 
namespace Moli.Desktop.Tests.Llm;
 
public class OpenAICompatibleAdapterTests
{
    [Fact]
    public async Task StreamAsync_IncludesProviderErrorMessageWhenRequestFails()
    {
        using var httpClient = new HttpClient(new StaticResponseHandler(
            HttpStatusCode.BadRequest,
            """{"error":{"message":"Invalid max_tokens value, the valid range of max_tokens is [1, 393216]"}}"""));
        var adapter = new OpenAICompatibleAdapter(httpClient);
 
        var exception = await Assert.ThrowsAsync<LlmProviderException>(async () =>
        {
            await adapter.StreamAsync(CreateRequest()).ToListAsync();
        });
 
        Assert.Equal(LlmErrorCodes.EndpointUnavailable, exception.Code);
        Assert.Contains("LLM provider returned 400", exception.Message);
        Assert.Contains("Invalid max_tokens value", exception.Message);
    }
 
    private static LlmProviderRequest CreateRequest()
        => new()
        {
            RequestId = "req-1",
            Model = new LlmModelConfig
            {
                Id = "deepseek-v4-flash",
                Label = "DeepSeek",
                Provider = "deepseek",
                Endpoint = "https://api.deepseek.com/chat/completions",
                ApiModel = "deepseek-v4-flash",
                Enabled = true
            },
            Messages = [new LlmMessage { Role = "user", Content = "ping" }],
            Options = new LlmRequestOptions(),
            ApiKey = "test-key"
        };
 
    private sealed class StaticResponseHandler(HttpStatusCode statusCode, string content) : HttpMessageHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
            => Task.FromResult(new HttpResponseMessage(statusCode)
            {
                Content = new StringContent(content, Encoding.UTF8, "application/json")
            });
    }
}