forked from microsoft/chat-copilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceExtensions.cs
336 lines (294 loc) · 14.3 KB
/
ServiceExtensions.cs
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Reflection;
using CopilotChat.Shared;
using CopilotChat.WebApi.Auth;
using CopilotChat.WebApi.Models.Storage;
using CopilotChat.WebApi.Options;
using CopilotChat.WebApi.Services;
using CopilotChat.WebApi.Storage;
using CopilotChat.WebApi.Utilities;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Identity.Web;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.Diagnostics;
namespace CopilotChat.WebApi.Extensions;
/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>.
/// Add options and services for Chat Copilot.
/// </summary>
public static class CopilotChatServiceExtensions
{
/// <summary>
/// Parse configuration into options.
/// </summary>
public static IServiceCollection AddOptions(this IServiceCollection services, ConfigurationManager configuration)
{
// General configuration
AddOptions<ServiceOptions>(ServiceOptions.PropertyName);
// Authentication configuration
AddOptions<ChatAuthenticationOptions>(ChatAuthenticationOptions.PropertyName);
// Chat storage configuration
AddOptions<ChatStoreOptions>(ChatStoreOptions.PropertyName);
// Azure speech token configuration
AddOptions<AzureSpeechOptions>(AzureSpeechOptions.PropertyName);
AddOptions<DocumentMemoryOptions>(DocumentMemoryOptions.PropertyName);
// Chat prompt options
AddOptions<PromptsOptions>(PromptsOptions.PropertyName);
AddOptions<ContentSafetyOptions>(ContentSafetyOptions.PropertyName);
AddOptions<KernelMemoryConfig>(MemoryConfiguration.KernelMemorySection);
AddOptions<FrontendOptions>(FrontendOptions.PropertyName);
AddOptions<MsGraphOboPluginOptions>(MsGraphOboPluginOptions.PropertyName);
return services;
void AddOptions<TOptions>(string propertyName)
where TOptions : class
{
services.AddOptions<TOptions>(configuration.GetSection(propertyName));
}
}
internal static void AddOptions<TOptions>(this IServiceCollection services, IConfigurationSection section)
where TOptions : class
{
services.AddOptions<TOptions>()
.Bind(section)
.ValidateDataAnnotations()
.ValidateOnStart()
.PostConfigure(TrimStringProperties);
}
internal static IServiceCollection AddPlugins(this IServiceCollection services, IConfiguration configuration)
{
var plugins = configuration.GetSection("Plugins").Get<List<Plugin>>() ?? new List<Plugin>();
var logger = services.BuildServiceProvider().GetRequiredService<ILogger<Program>>();
logger.LogDebug("Found {0} plugins.", plugins.Count);
// Validate the plugins
Dictionary<string, Plugin> validatedPlugins = new();
foreach (Plugin plugin in plugins)
{
if (validatedPlugins.ContainsKey(plugin.Name))
{
logger.LogWarning("Plugin '{0}' is defined more than once. Skipping...", plugin.Name);
continue;
}
var pluginManifestUrl = PluginUtils.GetPluginManifestUri(plugin.ManifestDomain);
using var request = new HttpRequestMessage(HttpMethod.Get, pluginManifestUrl);
// Need to set the user agent to avoid 403s from some sites.
request.Headers.Add("User-Agent", Telemetry.HttpUserAgent);
try
{
logger.LogInformation("Adding plugin: {0}.", plugin.Name);
using var httpClient = new HttpClient();
var response = httpClient.SendAsync(request).Result;
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException($"Plugin '{plugin.Name}' at '{pluginManifestUrl}' returned status code '{response.StatusCode}'.");
}
validatedPlugins.Add(plugin.Name, plugin);
logger.LogInformation("Added plugin: {0}.", plugin.Name);
}
catch (Exception ex) when (ex is InvalidOperationException || ex is AggregateException)
{
logger.LogWarning(ex, "Plugin '{0}' at {1} responded with error. Skipping...", plugin.Name, pluginManifestUrl);
}
catch (Exception ex) when (ex is UriFormatException)
{
logger.LogWarning("Plugin '{0}' at {1} is not a valid URL. Skipping...", plugin.Name, pluginManifestUrl);
}
}
// Add the plugins
services.AddSingleton<IDictionary<string, Plugin>>(validatedPlugins);
return services;
}
internal static IServiceCollection AddMaintenanceServices(this IServiceCollection services)
{
// Inject action stub
services.AddSingleton<IReadOnlyList<IMaintenanceAction>>(
sp => (IReadOnlyList<IMaintenanceAction>)Array.Empty<IMaintenanceAction>());
return services;
}
/// <summary>
/// Add CORS settings.
/// </summary>
internal static IServiceCollection AddCorsPolicy(this IServiceCollection services, IConfiguration configuration)
{
string[] allowedOrigins = configuration.GetSection("AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
if (allowedOrigins.Length > 0)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.WithOrigins(allowedOrigins)
.WithMethods("POST", "GET", "PUT", "DELETE", "PATCH")
.AllowAnyHeader();
});
});
}
return services;
}
/// <summary>
/// Add persistent chat store services.
/// </summary>
public static IServiceCollection AddPersistentChatStore(this IServiceCollection services)
{
IStorageContext<ChatSession> chatSessionStorageContext;
ICopilotChatMessageStorageContext chatMessageStorageContext;
IStorageContext<MemorySource> chatMemorySourceStorageContext;
IStorageContext<ChatParticipant> chatParticipantStorageContext;
ChatStoreOptions chatStoreConfig = services.BuildServiceProvider().GetRequiredService<IOptions<ChatStoreOptions>>().Value;
switch (chatStoreConfig.Type)
{
case ChatStoreOptions.ChatStoreType.Volatile:
{
chatSessionStorageContext = new VolatileContext<ChatSession>();
chatMessageStorageContext = new VolatileCopilotChatMessageContext();
chatMemorySourceStorageContext = new VolatileContext<MemorySource>();
chatParticipantStorageContext = new VolatileContext<ChatParticipant>();
break;
}
case ChatStoreOptions.ChatStoreType.Filesystem:
{
if (chatStoreConfig.Filesystem == null)
{
throw new InvalidOperationException("ChatStore:Filesystem is required when ChatStore:Type is 'Filesystem'");
}
string fullPath = Path.GetFullPath(chatStoreConfig.Filesystem.FilePath);
string directory = Path.GetDirectoryName(fullPath) ?? string.Empty;
chatSessionStorageContext = new FileSystemContext<ChatSession>(
new FileInfo(Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(fullPath)}_sessions{Path.GetExtension(fullPath)}")));
chatMessageStorageContext = new FileSystemCopilotChatMessageContext(
new FileInfo(Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(fullPath)}_messages{Path.GetExtension(fullPath)}")));
chatMemorySourceStorageContext = new FileSystemContext<MemorySource>(
new FileInfo(Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(fullPath)}_memorysources{Path.GetExtension(fullPath)}")));
chatParticipantStorageContext = new FileSystemContext<ChatParticipant>(
new FileInfo(Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(fullPath)}_participants{Path.GetExtension(fullPath)}")));
break;
}
case ChatStoreOptions.ChatStoreType.Cosmos:
{
if (chatStoreConfig.Cosmos == null)
{
throw new InvalidOperationException("ChatStore:Cosmos is required when ChatStore:Type is 'Cosmos'");
}
#pragma warning disable CA2000 // Dispose objects before losing scope - objects are singletons for the duration of the process and disposed when the process exits.
chatSessionStorageContext = new CosmosDbContext<ChatSession>(
chatStoreConfig.Cosmos.ConnectionString, chatStoreConfig.Cosmos.Database, chatStoreConfig.Cosmos.ChatSessionsContainer);
chatMessageStorageContext = new CosmosDbCopilotChatMessageContext(
chatStoreConfig.Cosmos.ConnectionString, chatStoreConfig.Cosmos.Database, chatStoreConfig.Cosmos.ChatMessagesContainer);
chatMemorySourceStorageContext = new CosmosDbContext<MemorySource>(
chatStoreConfig.Cosmos.ConnectionString, chatStoreConfig.Cosmos.Database, chatStoreConfig.Cosmos.ChatMemorySourcesContainer);
chatParticipantStorageContext = new CosmosDbContext<ChatParticipant>(
chatStoreConfig.Cosmos.ConnectionString, chatStoreConfig.Cosmos.Database, chatStoreConfig.Cosmos.ChatParticipantsContainer);
#pragma warning restore CA2000 // Dispose objects before losing scope
break;
}
default:
{
throw new InvalidOperationException(
"Invalid 'ChatStore' setting 'chatStoreConfig.Type'.");
}
}
services.AddSingleton<ChatSessionRepository>(new ChatSessionRepository(chatSessionStorageContext));
services.AddSingleton<ChatMessageRepository>(new ChatMessageRepository(chatMessageStorageContext));
services.AddSingleton<ChatMemorySourceRepository>(new ChatMemorySourceRepository(chatMemorySourceStorageContext));
services.AddSingleton<ChatParticipantRepository>(new ChatParticipantRepository(chatParticipantStorageContext));
return services;
}
/// <summary>
/// Add authorization services
/// </summary>
public static IServiceCollection AddChatCopilotAuthorization(this IServiceCollection services)
{
return services.AddScoped<IAuthorizationHandler, ChatParticipantAuthorizationHandler>()
.AddAuthorizationCore(options =>
{
options.DefaultPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.AddPolicy(AuthPolicyName.RequireChatParticipant, builder =>
{
builder.RequireAuthenticatedUser()
.AddRequirements(new ChatParticipantRequirement());
});
});
}
/// <summary>
/// Add authentication services
/// </summary>
public static IServiceCollection AddChatCopilotAuthentication(this IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<IAuthInfo, AuthInfo>();
var config = services.BuildServiceProvider().GetRequiredService<IOptions<ChatAuthenticationOptions>>().Value;
switch (config.Type)
{
case ChatAuthenticationOptions.AuthenticationType.AzureAd:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(configuration.GetSection($"{ChatAuthenticationOptions.PropertyName}:AzureAd"));
break;
case ChatAuthenticationOptions.AuthenticationType.None:
services.AddAuthentication(PassThroughAuthenticationHandler.AuthenticationScheme)
.AddScheme<AuthenticationSchemeOptions, PassThroughAuthenticationHandler>(
authenticationScheme: PassThroughAuthenticationHandler.AuthenticationScheme,
configureOptions: null);
break;
default:
throw new InvalidOperationException($"Invalid authentication type '{config.Type}'.");
}
return services;
}
/// <summary>
/// Trim all string properties, recursively.
/// </summary>
private static void TrimStringProperties<T>(T options) where T : class
{
Queue<object> targets = new();
targets.Enqueue(options);
while (targets.Count > 0)
{
object target = targets.Dequeue();
Type targetType = target.GetType();
foreach (PropertyInfo property in targetType.GetProperties())
{
// Skip enumerations
if (property.PropertyType.IsEnum)
{
continue;
}
// Skip index properties
if (property.GetIndexParameters().Length == 0)
{
continue;
}
// Property is a built-in type, readable, and writable.
if (property.PropertyType.Namespace == "System" &&
property.CanRead &&
property.CanWrite)
{
// Property is a non-null string.
if (property.PropertyType == typeof(string) &&
property.GetValue(target) != null)
{
property.SetValue(target, property.GetValue(target)!.ToString()!.Trim());
}
}
else
{
// Property is a non-built-in and non-enum type - queue it for processing.
if (property.GetValue(target) != null)
{
targets.Enqueue(property.GetValue(target)!);
}
}
}
}
}
}