Add Luna.Configuration with TOML-based agent and provider config
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.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Luna.Configuration.Services;
|
||||
|
||||
public sealed class EmbeddedResourceLoader
|
||||
{
|
||||
private readonly Assembly? assembly = Assembly.GetExecutingAssembly();
|
||||
|
||||
public ImmutableHashSet<string> ListResources(string? pattern = null)
|
||||
{
|
||||
if (assembly is null)
|
||||
throw new InvalidOperationException("Unable to determine the entry assembly.");
|
||||
|
||||
var resourceNames = assembly.GetManifestResourceNames();
|
||||
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
return resourceNames.ToImmutableHashSet();
|
||||
|
||||
var regex = new Regex(Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".?"), RegexOptions.IgnoreCase);
|
||||
return resourceNames
|
||||
.Where(r => regex.IsMatch(r))
|
||||
.ToImmutableHashSet();
|
||||
}
|
||||
|
||||
public async Task<string> LoadAsync(string name)
|
||||
{
|
||||
if (assembly is null)
|
||||
throw new InvalidOperationException("Unable to determine the entry assembly.");
|
||||
|
||||
var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(r => r.EndsWith(name, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (resourceName == null)
|
||||
throw new FileNotFoundException($"Embedded resource '{name}' not found.");
|
||||
|
||||
await using var stream = assembly.GetManifestResourceStream(resourceName);
|
||||
if (stream == null)
|
||||
throw new FileNotFoundException($"Unable to load embedded resource '{name}'.");
|
||||
|
||||
using var reader = new StreamReader(stream);
|
||||
return await reader.ReadToEndAsync();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user