-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEndpointRouteBuilderExtensions.cs
More file actions
205 lines (178 loc) · 7.93 KB
/
EndpointRouteBuilderExtensions.cs
File metadata and controls
205 lines (178 loc) · 7.93 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using System.Text.Json;
using SQLAgent.Hosting.Dto;
using SQLAgent.Hosting.Services;
namespace SQLAgent.Hosting.Extensions;
/// <summary>
/// 端点路由构建器扩展方法
/// </summary>
public static class EndpointRouteBuilderExtensions
{
/// <summary>
/// 映射连接管理 API
/// </summary>
public static IEndpointRouteBuilder MapConnectionApis(this IEndpointRouteBuilder app)
{
app.MapGet("/api/connections",
async (ConnectionService service, bool includeDisabled = false) =>
await service.GetAllAsync(includeDisabled))
.WithName("GetAllConnections");
app.MapGet("/api/connections/{id}",
async (string id, ConnectionService service) =>
await service.GetByIdAsync(id))
.WithName("GetConnectionById");
app.MapPost("/api/connections",
async (CreateConnectionRequest request, ConnectionService service) =>
await service.CreateAsync(request))
.WithName("CreateConnection");
app.MapPut("/api/connections/{id}",
async (string id, UpdateConnectionRequest request, ConnectionService service) =>
await service.UpdateAsync(id, request))
.WithName("UpdateConnection");
app.MapDelete("/api/connections/{id}",
async (string id, ConnectionService service) =>
await service.DeleteAsync(id))
.WithName("DeleteConnection");
app.MapPost("/api/connections/{id}/test",
async (string id, ConnectionService service) =>
await service.TestAsync(id))
.WithName("TestConnection");
return app;
}
/// <summary>
/// 映射 AI 提供商管理 API
/// </summary>
public static IEndpointRouteBuilder MapProviderApis(this IEndpointRouteBuilder app)
{
app.MapGet("/api/providers",
async (ProvidersService service) =>
await service.GetAllAsync())
.WithName("GetAllProviders");
app.MapGet("/api/providers/{id}",
async (string id, ProvidersService service) =>
await service.GetAsync(id))
.WithName("GetProviderById");
app.MapPost("/api/providers",
async (AIProviderInput input, ProvidersService service) =>
await service.CreateAsync(input))
.WithName("CreateProvider");
app.MapPut("/api/providers/{id}",
async (string id, AIProviderInput input, ProvidersService service) =>
await service.UpdateAsync(id, input))
.WithName("UpdateProvider");
app.MapDelete("/api/providers/{id}",
async (string id, ProvidersService service) =>
await service.DeleteAsync(id))
.WithName("DeleteProvider");
app.MapGet("/api/providers/{id}/models",
async (string id, ProvidersService service) =>
await service.GetModelsAsync(id))
.WithName("GetProviderModels");
return app;
}
/// <summary>
/// 映射聊天 API
/// </summary>
public static IEndpointRouteBuilder MapChatApis(this IEndpointRouteBuilder app)
{
app.MapPost("/api/chat/completion",
async (HttpContext context, CompletionInput input, ChatService service) =>
await service.CompletionAsync(context, input))
.WithName("ChatCompletion");
return app;
}
/// <summary>
/// 映射系统设置 API
/// </summary>
public static IEndpointRouteBuilder MapSettingsApis(this IEndpointRouteBuilder app)
{
// GET /api/settings - 返回内存设置;如存在 settings.json,则加载并合并到内存实例后返回
app.MapGet("/api/settings", async ([FromServices] SystemSettings settings, [FromServices] IWebHostEnvironment env) =>
{
try
{
var filePath = Path.Combine(env.ContentRootPath, "settings.json");
if (File.Exists(filePath))
{
var json = await File.ReadAllTextAsync(filePath);
var fileSettings = JsonSerializer.Deserialize<SystemSettings>(json);
if (fileSettings != null)
{
settings.EmbeddingProviderId = fileSettings.EmbeddingProviderId;
settings.EmbeddingModel = fileSettings.EmbeddingModel;
settings.VectorDbPath = fileSettings.VectorDbPath;
settings.VectorCollection = fileSettings.VectorCollection;
settings.AutoCreateCollection = fileSettings.AutoCreateCollection;
settings.VectorCacheExpireMinutes = fileSettings.VectorCacheExpireMinutes;
settings.DefaultChatProviderId = fileSettings.DefaultChatProviderId;
settings.DefaultChatModel = fileSettings.DefaultChatModel;
}
}
}
catch
{
// 忽略读取失败,返回内存中的设置
}
return Results.Ok(settings);
})
.WithName("GetSettings");
// PUT /api/settings - 更新内存设置并持久化到 settings.json
app.MapPut("/api/settings", async ([FromBody] SystemSettings payload, [FromServices] SystemSettings settings, [FromServices] IWebHostEnvironment env) =>
{
settings.EmbeddingProviderId = payload.EmbeddingProviderId;
settings.EmbeddingModel = payload.EmbeddingModel;
settings.VectorDbPath = payload.VectorDbPath;
settings.VectorCollection = payload.VectorCollection;
settings.AutoCreateCollection = payload.AutoCreateCollection;
settings.VectorCacheExpireMinutes = payload.VectorCacheExpireMinutes;
settings.DefaultChatProviderId = payload.DefaultChatProviderId;
settings.DefaultChatModel = payload.DefaultChatModel;
try
{
var filePath = Path.Combine(env.ContentRootPath, "settings.json");
var options = new JsonSerializerOptions { WriteIndented = true };
var json = JsonSerializer.Serialize(settings, options);
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
await File.WriteAllTextAsync(filePath, json);
}
catch (Exception ex)
{
return Results.Problem($"保存设置失败: {ex.Message}");
}
return Results.Ok(settings);
})
.WithName("UpdateSettings");
return app;
}
/// <summary>
/// 映射连接 Agent API
/// </summary>
public static IEndpointRouteBuilder MapConnectionAgentApis(this IEndpointRouteBuilder app)
{
app.MapPost("/api/connections/{id}/agent/generate",
async (string id, ConnectionAgentService service) =>
await service.GenerateAgentAsync(id))
.WithName("GenerateConnectionAgent");
app.MapGet("/api/connections/{id}/agent/status",
(string id, ConnectionAgentService service) =>
{
var status = service.GetGenerationStatus(id);
return Results.Ok(status);
})
.WithName("GetConnectionAgentStatus");
return app;
}
/// <summary>
/// 映射所有 API 端点
/// </summary>
public static IEndpointRouteBuilder MapAllApis(this IEndpointRouteBuilder app)
{
app.MapConnectionApis();
app.MapProviderApis();
app.MapChatApis();
app.MapSettingsApis();
app.MapConnectionAgentApis();
return app;
}
}