Implement configuration system with embedded TOML resources for agent and provider definitions, custom IConfigurationSource/Provider for agent and provider options, and DI extension methods. Includes config models for agents, providers, sessions, and Telegram integration.
73 lines
2.6 KiB
C#
73 lines
2.6 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Luna.Configuration.Extensions;
|
|
|
|
public static class ServiceCollectionExtensions
|
|
{
|
|
extension(IServiceCollection services)
|
|
{
|
|
public IServiceCollection AddProviderOptions(string? name)
|
|
{
|
|
services.AddOptions<ProviderOptions>(name)
|
|
.BindConfiguration($"Providers:{name}")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.ApiKey),
|
|
$"{name} options missing ApiKey")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.ApiUrl),
|
|
$"{name} options missing ApiUrl")
|
|
.Validate(
|
|
options => options.Models is { Length: > 0 },
|
|
$"{name} options missing Model definitions")
|
|
.ValidateOnStart();
|
|
|
|
return services;
|
|
}
|
|
|
|
public IServiceCollection AddAgentOptions(string? name)
|
|
{
|
|
services.AddOptions<AgentOptions>(name)
|
|
.BindConfiguration($"Agents:{name}")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.Provider),
|
|
$"{name} options missing Provider")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.ModelId),
|
|
$"{name} options missing ModelId")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.Instructions),
|
|
$"{name} options missing Instructions")
|
|
.Validate(
|
|
options => options.MaxContextTokens > 0,
|
|
$"{name} options must have MaxContextTokens greater than 0")
|
|
.ValidateOnStart();
|
|
|
|
return services;
|
|
}
|
|
|
|
public IServiceCollection AddSessionOptions()
|
|
{
|
|
services.AddOptions<SessionOptions>()
|
|
.BindConfiguration("Session")
|
|
.Validate(
|
|
options => options.ContextTokenThreshold > 0,
|
|
"Session options must have ContextTokenThreshold greater than 0")
|
|
.ValidateOnStart();
|
|
|
|
return services;
|
|
}
|
|
|
|
public IServiceCollection AddTelegramOptions()
|
|
{
|
|
services.AddOptions<TelegramOptions>()
|
|
.BindConfiguration("Channels:Telegram")
|
|
.Validate(
|
|
options => !string.IsNullOrWhiteSpace(options.BotToken),
|
|
"Telegram options missing BotToken")
|
|
.ValidateOnStart();
|
|
|
|
return services;
|
|
}
|
|
}
|
|
}
|