基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-04 b3bde921167ee0a7dc21cf7e3d642840a0fcc11e
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Text.Json.Serialization;
 
namespace Moli.Desktop.Llm;
 
public sealed record LlmSettingsDocument
{
    public int SchemaVersion { get; init; } = 1;
    public string? DefaultModel { get; init; }
    public List<LlmModelConfig> Models { get; init; } = [];
}
 
public sealed record LlmModelConfig
{
    public required string Id { get; init; }
    public required string Label { get; init; }
    public required string Provider { get; init; }
    public required string Endpoint { get; init; }
    public string? AuthRef { get; init; }
    public required string ApiModel { get; init; }
    public bool SupportsStreaming { get; init; } = true;
    public bool Enabled { get; init; }
    public LlmRequestDefaults RequestDefaults { get; init; } = new();
}
 
public sealed record LlmRequestDefaults
{
    public double Temperature { get; init; } = 0.7;
    public int MaxTokens { get; init; } = 2048;
    public int TimeoutMs { get; init; } = 60000;
}
 
public sealed record LlmModelSummary
{
    public required string Id { get; init; }
    public required string Label { get; init; }
    public required string Provider { get; init; }
    public bool SupportsStreaming { get; init; }
    public bool Enabled { get; init; }
}
 
public sealed record LlmChatRequest
{
    public required string RequestId { get; init; }
    public required string PluginId { get; init; }
    public required string InstanceId { get; init; }
    public required string ModelId { get; init; }
    public required IReadOnlyList<LlmMessage> Messages { get; init; }
    public LlmRequestOptions Options { get; init; } = new();
}
 
public sealed record LlmMessage
{
    public required string Role { get; init; }
    public required string Content { get; init; }
}
 
public sealed record LlmRequestOptions
{
    public double? Temperature { get; init; }
    public int? MaxTokens { get; init; }
    public int? TimeoutMs { get; init; }
}
 
public sealed record LlmProviderRequest
{
    public required string RequestId { get; init; }
    public required LlmModelConfig Model { get; init; }
    public required IReadOnlyList<LlmMessage> Messages { get; init; }
    public required LlmRequestOptions Options { get; init; }
    public string? ApiKey { get; init; }
}
 
[JsonPolymorphic(TypeDiscriminatorPropertyName = "kind")]
[JsonDerivedType(typeof(LlmDeltaEvent), "delta")]
[JsonDerivedType(typeof(LlmCompletedEvent), "completed")]
[JsonDerivedType(typeof(LlmErrorEvent), "error")]
public abstract record LlmStreamEvent
{
    public required string RequestId { get; init; }
}
 
public sealed record LlmDeltaEvent : LlmStreamEvent
{
    public required string Delta { get; init; }
}
 
public sealed record LlmCompletedEvent : LlmStreamEvent
{
    public LlmUsage? Usage { get; init; }
}
 
public sealed record LlmErrorEvent : LlmStreamEvent
{
    public required string Code { get; init; }
    public required string Message { get; init; }
}
 
public sealed record LlmUsage
{
    public int? InputTokens { get; init; }
    public int? OutputTokens { get; init; }
}
 
public sealed record LlmProviderChunk
{
    public string? Delta { get; init; }
    public bool IsCompleted { get; init; }
    public LlmUsage? Usage { get; init; }
}
 
public static class LlmErrorCodes
{
    public const string ModelNotFound = "LlmModelNotFound";
    public const string ProviderUnsupported = "LlmProviderUnsupported";
    public const string CredentialMissing = "LlmCredentialMissing";
    public const string EndpointUnavailable = "LlmEndpointUnavailable";
    public const string Unauthorized = "LlmUnauthorized";
    public const string RateLimited = "LlmRateLimited";
    public const string Timeout = "LlmTimeout";
    public const string StreamParseFailed = "LlmStreamParseFailed";
    public const string RequestInvalid = "LlmRequestInvalid";
}