Adding providers can now be disabled using config plugins (#522)
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-apple-darwin, osx-x64, macos-latest, x86_64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-pc-windows-msvc.exe, win-x64, windows-latest, x86_64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-x86_64-unknown-linux-gnu, linux-x64, ubuntu-22.04, x86_64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Prepare & create release (push) Blocked by required conditions
Build and Release / Read metadata (push) Waiting to run
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-apple-darwin, osx-arm64, macos-latest, aarch64-apple-darwin, dmg updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-pc-windows-msvc.exe, win-arm64, windows-latest, aarch64-pc-windows-msvc, nsis updater) (push) Blocked by required conditions
Build and Release / Build app (${{ matrix.dotnet_runtime }}) (-aarch64-unknown-linux-gnu, linux-arm64, ubuntu-22.04-arm, aarch64-unknown-linux-gnu, appimage deb updater) (push) Blocked by required conditions
Build and Release / Publish release (push) Blocked by required conditions

Co-authored-by: Thorsten Sommer <mail@tsommer.org>
This commit is contained in:
Peer Schüttandtsommer authored and GitHub committed 2025-08-09 19:29:43 +02:00
1 parent 6116c03f7c
commit 035412f7ff
47 files changed
+663 -219

No files matched your search

@@ -1,6 +1,5 @@
using AIStudio.Provider;
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
using Lua;
@@ -13,24 +12,27 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
{
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(PluginConfiguration).Namespace, nameof(PluginConfiguration));
private static readonly ILogger<PluginConfiguration> LOGGER = Program.LOGGER_FACTORY.CreateLogger<PluginConfiguration>();
private static readonly SettingsLocker SETTINGS_LOCKER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsLocker>();
private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
public async Task InitializeAsync()
public async Task InitializeAsync(bool dryRun)
{
if(!this.TryProcessConfiguration(out var issue))
if(!this.TryProcessConfiguration(dryRun, out var issue))
this.pluginIssues.Add(issue);
await SETTINGS_MANAGER.StoreSettings();
await MessageBus.INSTANCE.SendMessage<bool>(null, Event.CONFIGURATION_CHANGED);
if (!dryRun)
{
await SETTINGS_MANAGER.StoreSettings();
await MessageBus.INSTANCE.SendMessage<bool>(null, Event.CONFIGURATION_CHANGED);
}
}
/// <summary>
/// Tries to initialize the UI text content of the plugin.
/// </summary>
/// <param name="dryRun">When true, the method will not apply any changes, but only check if the configuration can be read.</param>
/// <param name="message">The error message, when the UI text content could not be read.</param>
/// <returns>True, when the UI text content could be read successfully.</returns>
private bool TryProcessConfiguration(out string message)
private bool TryProcessConfiguration(bool dryRun, out string message)
{
// Ensure that the main CONFIG table exists and is a valid Lua table:
if (!this.state.Environment["CONFIG"].TryRead<LuaTable>(out var mainTable))
@@ -40,19 +42,21 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
}
//
// ===========================================
// Configured settings
// ===========================================
//
if (!mainTable.TryGetValue("SETTINGS", out var settingsValue) || !settingsValue.TryRead<LuaTable>(out var settingsTable))
{
message = TB("The SETTINGS table does not exist or is not a valid table.");
return false;
}
if (settingsTable.TryGetValue(SettingsManager.ToSettingName<DataApp>(x => x.UpdateBehavior), out var updateBehaviorValue) && updateBehaviorValue.TryRead<string>(out var updateBehaviorText) && Enum.TryParse<UpdateBehavior>(updateBehaviorText, true, out var updateBehavior))
{
SETTINGS_LOCKER.Register<DataApp>(x => x.UpdateBehavior, this.Id);
SETTINGS_MANAGER.ConfigurationData.App.UpdateBehavior = updateBehavior;
}
// Check for updates, and if so, how often?
ManagedConfiguration.TryProcessConfiguration(x => x.App, x => x.UpdateBehavior, this.Id, settingsTable, dryRun);
// Allow the user to add providers?
ManagedConfiguration.TryProcessConfiguration(x => x.App, x => x.AllowUserToAddProvider, this.Id, settingsTable, dryRun);
//
// Configured providers
@@ -16,10 +16,21 @@ public static partial class PluginFactory
try
{
HOT_RELOAD_WATCHER.IncludeSubdirectories = true;
HOT_RELOAD_WATCHER.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
HOT_RELOAD_WATCHER.Filter = "*.lua";
HOT_RELOAD_WATCHER.NotifyFilter = NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Size;
HOT_RELOAD_WATCHER.Changed += HotReloadEventHandler;
HOT_RELOAD_WATCHER.Deleted += HotReloadEventHandler;
HOT_RELOAD_WATCHER.Created += HotReloadEventHandler;
HOT_RELOAD_WATCHER.Renamed += HotReloadEventHandler;
HOT_RELOAD_WATCHER.Error += (_, args) =>
{
LOG.LogError(args.GetException(), "Error in hot reload watcher.");
};
HOT_RELOAD_WATCHER.EnableRaisingEvents = true;
}
catch (Exception e)
@@ -39,13 +50,13 @@ public static partial class PluginFactory
var changeType = args.ChangeType.ToString().ToLowerInvariant();
if (!await HOT_RELOAD_SEMAPHORE.WaitAsync(0))
{
LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Already processing another change.");
LOG.LogInformation($"File changed '{args.FullPath}' (event={changeType}). Already processing another change.");
return;
}
try
{
LOG.LogInformation($"File changed ({changeType}): {args.FullPath}. Reloading plugins...");
LOG.LogInformation($"File changed '{args.FullPath}' (event={changeType}). Reloading plugins...");
if (File.Exists(HOT_RELOAD_LOCK_FILE))
{
LOG.LogInformation("Hot reload lock file exists. Waiting for it to be released before proceeding with the reload.");
@@ -1,5 +1,6 @@
using System.Text;
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
using Lua;
@@ -120,8 +121,10 @@ public static partial class PluginFactory
}
//
// =========================================================
// Next, we have to clean up our settings. It is possible that a configuration plugin was removed.
// We have to remove the related settings as well:
// =========================================================
//
var wasConfigurationChanged = false;
@@ -150,23 +153,18 @@ public static partial class PluginFactory
#pragma warning restore MWAIS0001
//
// ==========================================================
// Check all possible settings:
// ==========================================================
//
if (SETTINGS_LOCKER.GetConfigurationPluginId<DataApp>(x => x.UpdateBehavior) is var updateBehaviorPluginId && updateBehaviorPluginId != Guid.Empty)
{
var sourcePlugin = AVAILABLE_PLUGINS.FirstOrDefault(plugin => plugin.Id == updateBehaviorPluginId);
if (sourcePlugin is null)
{
// Remove the locked state:
SETTINGS_LOCKER.Remove<DataApp>(x => x.UpdateBehavior);
// Reset the setting to the default value:
SETTINGS_MANAGER.ConfigurationData.App.UpdateBehavior = UpdateBehavior.HOURLY;
LOG.LogWarning($"The configured update behavior is based on a plugin that is not available anymore. Resetting the setting to the default value: {SETTINGS_MANAGER.ConfigurationData.App.UpdateBehavior}.");
wasConfigurationChanged = true;
}
}
// Check for updates, and if so, how often?
if(ManagedConfiguration.IsConfigurationLeftOver<DataApp, UpdateBehavior>(x => x.App, x => x.UpdateBehavior, AVAILABLE_PLUGINS))
wasConfigurationChanged = true;
// Allow the user to add providers?
if(ManagedConfiguration.IsConfigurationLeftOver<DataApp, bool>(x => x.App, x => x.AllowUserToAddProvider, AVAILABLE_PLUGINS))
wasConfigurationChanged = true;
if (wasConfigurationChanged)
{
@@ -225,7 +223,7 @@ public static partial class PluginFactory
case PluginType.CONFIGURATION:
var configPlug = new PluginConfiguration(isInternal, state, type);
await configPlug.InitializeAsync();
await configPlug.InitializeAsync(true);
return configPlug;
default:
@@ -103,7 +103,7 @@ public static partial class PluginFactory
languagePlugin.SetBaseLanguage(BASE_LANGUAGE_PLUGIN);
if(plugin is PluginConfiguration configPlugin)
await configPlugin.InitializeAsync();
await configPlugin.InitializeAsync(false);
LOG.LogInformation($"Successfully started plugin: Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}'");
return plugin;
@@ -6,7 +6,6 @@ public static partial class PluginFactory
{
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(nameof(PluginFactory));
private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
private static readonly SettingsLocker SETTINGS_LOCKER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsLocker>();
private static bool IS_INITIALIZED;
private static string DATA_DIR = string.Empty;