基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-17 9b5711d3124622b1f1b18500d442c76492da877a
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Net;
using System.Text;
using Moli.Desktop.Llm;
 
namespace Moli.Desktop.Tests.Llm;
 
public class OpenAICompatibleAdapterTests
{
    [Fact]
    public async Task StreamAsync_PostsToBaseUrlChatCompletions()
    {
        var handler = new RecordingHandler(HttpStatusCode.OK, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n");
        using var httpClient = new HttpClient(handler);
        var adapter = new OpenAICompatibleAdapter(httpClient);
 
        var chunks = await adapter.StreamAsync(CreateRequest(baseUrl: "https://api.deepseek.com")).ToListAsync();
 
        Assert.Equal("https://api.deepseek.com/chat/completions", handler.LastRequest?.RequestUri?.ToString());
        Assert.Contains(chunks, chunk => chunk.Delta == "ok");
    }
 
    [Fact]
    public async Task StreamAsync_AcceptsLegacyChatCompletionsEndpoint()
    {
        var handler = new RecordingHandler(HttpStatusCode.OK, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\ndata: [DONE]\n");
        using var httpClient = new HttpClient(handler);
        var adapter = new OpenAICompatibleAdapter(httpClient);
 
        await adapter.StreamAsync(CreateRequest(endpoint: "https://api.deepseek.com/chat/completions")).ToListAsync();
 
        Assert.Equal("https://api.deepseek.com/chat/completions", handler.LastRequest?.RequestUri?.ToString());
    }
 
    [Fact]
    public async Task StreamAsync_IncludesProviderErrorMessageWhenRequestFails()
    {
        using var httpClient = new HttpClient(new RecordingHandler(
            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(baseUrl: "https://api.deepseek.com")).ToListAsync();
        });
 
        Assert.Equal(LlmErrorCodes.EndpointUnavailable, exception.Code);
        Assert.Contains("LLM provider returned 400", exception.Message);
        Assert.Contains("Invalid max_tokens value", exception.Message);
    }
 
    [Fact]
    public async Task ListModelIdsAsync_ReadsOpenAiDataIds()
    {
        var handler = new RecordingHandler(
            HttpStatusCode.OK,
            """{"data":[{"id":"deepseek-v4-flash"},{"id":"deepseek-chat"}]}""");
        using var httpClient = new HttpClient(handler);
        var adapter = new OpenAICompatibleAdapter(httpClient);
 
        var ids = await adapter.ListModelIdsAsync("https://api.deepseek.com", "secret-key");
 
        Assert.Equal("https://api.deepseek.com/models", handler.LastRequest?.RequestUri?.ToString());
        Assert.Equal("secret-key", handler.LastRequest?.Headers.Authorization?.Parameter);
        Assert.Equal(["deepseek-v4-flash", "deepseek-chat"], ids);
    }
 
    private static LlmProviderRequest CreateRequest(string? baseUrl = null, string? endpoint = null)
        => new()
        {
            RequestId = "req-1",
            Model = new LlmModelConfig
            {
                Id = "deepseek-v4-flash",
                Label = "DeepSeek",
                Provider = "deepseek",
                BaseUrl = baseUrl,
                Endpoint = endpoint,
                ApiModel = "deepseek-v4-flash",
                Enabled = true
            },
            Messages = [new LlmMessage { Role = "user", Content = "ping" }],
            Options = new LlmRequestOptions(),
            ApiKey = "test-key"
        };
 
    private sealed class RecordingHandler(HttpStatusCode statusCode, string content) : HttpMessageHandler
    {
        public HttpRequestMessage? LastRequest { get; private set; }
 
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            LastRequest = request;
            return Task.FromResult(new HttpResponseMessage(statusCode)
            {
                Content = new StringContent(content, Encoding.UTF8, "application/json")
            });
        }
    }
}