mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2025-08-02 13:02:58 +00:00
Start plugins
This commit is contained in:
parent
a8663e46b9
commit
adcd199aff
@ -0,0 +1,6 @@
|
|||||||
|
namespace AIStudio.Tools.PluginSystem;
|
||||||
|
|
||||||
|
public interface IAvailablePlugin : IPluginMetadata
|
||||||
|
{
|
||||||
|
public string LocalPath { get; }
|
||||||
|
}
|
@ -10,18 +10,29 @@ namespace AIStudio.Tools.PluginSystem;
|
|||||||
public static partial class PluginFactory
|
public static partial class PluginFactory
|
||||||
{
|
{
|
||||||
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(nameof(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 List<IAvailablePlugin> AVAILABLE_PLUGINS = [];
|
private static readonly List<IAvailablePlugin> AVAILABLE_PLUGINS = [];
|
||||||
|
private static readonly List<PluginBase> RUNNING_PLUGINS = [];
|
||||||
|
|
||||||
private static bool IS_INITIALIZED;
|
private static bool IS_INITIALIZED;
|
||||||
private static string DATA_DIR = string.Empty;
|
private static string DATA_DIR = string.Empty;
|
||||||
private static string PLUGINS_ROOT = string.Empty;
|
private static string PLUGINS_ROOT = string.Empty;
|
||||||
private static string INTERNAL_PLUGINS_ROOT = string.Empty;
|
private static string INTERNAL_PLUGINS_ROOT = string.Empty;
|
||||||
private static FileSystemWatcher HOT_RELOAD_WATCHER = null!;
|
private static FileSystemWatcher HOT_RELOAD_WATCHER = null!;
|
||||||
|
private static ILanguagePlugin BASE_LANGUAGE_PLUGIN = NoPluginLanguage.INSTANCE;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A list of all available plugins.
|
/// A list of all available plugins.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static IReadOnlyCollection<IPluginMetadata> AvailablePlugins => AVAILABLE_PLUGINS;
|
public static IReadOnlyCollection<IPluginMetadata> AvailablePlugins => AVAILABLE_PLUGINS;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A list of all running plugins.
|
||||||
|
/// </summary>
|
||||||
|
public static IReadOnlyCollection<PluginBase> RunningPlugins => RUNNING_PLUGINS;
|
||||||
|
|
||||||
|
public static ILanguagePlugin BaseLanguage => BASE_LANGUAGE_PLUGIN;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set up the plugin factory. We will read the data directory from the settings manager.
|
/// Set up the plugin factory. We will read the data directory from the settings manager.
|
||||||
/// Afterward, we will create the plugins directory and the internal plugin directory.
|
/// Afterward, we will create the plugins directory and the internal plugin directory.
|
||||||
@ -109,6 +120,9 @@ public static partial class PluginFactory
|
|||||||
LOG.LogInformation($"Successfully loaded plugin: '{pluginMainFile}' (Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}', Authors='{string.Join(", ", plugin.Authors)}')");
|
LOG.LogInformation($"Successfully loaded plugin: '{pluginMainFile}' (Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}', Authors='{string.Join(", ", plugin.Authors)}')");
|
||||||
AVAILABLE_PLUGINS.Add(new PluginMetadata(plugin, pluginPath));
|
AVAILABLE_PLUGINS.Add(new PluginMetadata(plugin, pluginPath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start or restart all plugins:
|
||||||
|
await RestartAllPlugins(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<PluginBase> Load(string pluginPath, string code, CancellationToken cancellationToken = default)
|
private static async Task<PluginBase> Load(string pluginPath, string code, CancellationToken cancellationToken = default)
|
||||||
@ -160,6 +174,93 @@ public static partial class PluginFactory
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task RestartAllPlugins(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
LOG.LogInformation("Try to start or restart all plugins.");
|
||||||
|
RUNNING_PLUGINS.Clear();
|
||||||
|
|
||||||
|
//
|
||||||
|
// Get the base language plugin. This is the plugin that will be used to fill in missing keys.
|
||||||
|
//
|
||||||
|
var baseLanguagePluginId = InternalPlugin.LANGUAGE_EN_US.MetaData().Id;
|
||||||
|
var baseLanguagePluginMetaData = AVAILABLE_PLUGINS.FirstOrDefault(p => p.Id == baseLanguagePluginId);
|
||||||
|
if (baseLanguagePluginMetaData is null)
|
||||||
|
{
|
||||||
|
LOG.LogError($"Was not able to find the base language plugin: Id='{baseLanguagePluginId}'. Please check your installation.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var startedBasePlugin = await Start(baseLanguagePluginMetaData, cancellationToken);
|
||||||
|
if (startedBasePlugin is NoPlugin noPlugin)
|
||||||
|
{
|
||||||
|
LOG.LogError($"Was not able to start the base language plugin: Id='{baseLanguagePluginId}'. Reason: {noPlugin.Issues.First()}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (startedBasePlugin is PluginLanguage languagePlugin)
|
||||||
|
{
|
||||||
|
BASE_LANGUAGE_PLUGIN = languagePlugin;
|
||||||
|
LOG.LogInformation($"Successfully started the base language plugin: Id='{languagePlugin.Id}', Type='{languagePlugin.Type}', Name='{languagePlugin.Name}', Version='{languagePlugin.Version}'");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG.LogError($"Was not able to start the base language plugin: Id='{baseLanguagePluginId}'. Reason: {string.Join("; ", startedBasePlugin.Issues)}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Iterate over all available plugins and try to start them.
|
||||||
|
//
|
||||||
|
foreach (var availablePlugin in AVAILABLE_PLUGINS)
|
||||||
|
{
|
||||||
|
if(cancellationToken.IsCancellationRequested)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (availablePlugin.Id == baseLanguagePluginId)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (availablePlugin.IsInternal || SETTINGS_MANAGER.IsPluginEnabled(availablePlugin))
|
||||||
|
if(await Start(availablePlugin, cancellationToken) is { IsValid: true } plugin)
|
||||||
|
RUNNING_PLUGINS.Add(plugin);
|
||||||
|
|
||||||
|
// Inform all components that the plugins have been reloaded or started:
|
||||||
|
await MessageBus.INSTANCE.SendMessage<bool>(null, Event.PLUGINS_RELOADED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<PluginBase> Start(IAvailablePlugin meta, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
var pluginMainFile = Path.Join(meta.LocalPath, "plugin.lua");
|
||||||
|
if(!File.Exists(pluginMainFile))
|
||||||
|
{
|
||||||
|
LOG.LogError($"Was not able to start plugin: Id='{meta.Id}', Type='{meta.Type}', Name='{meta.Name}', Version='{meta.Version}'. Reason: The plugin file does not exist.");
|
||||||
|
return new NoPlugin($"The plugin file does not exist: {pluginMainFile}");
|
||||||
|
}
|
||||||
|
|
||||||
|
var code = await File.ReadAllTextAsync(pluginMainFile, Encoding.UTF8, cancellationToken);
|
||||||
|
var plugin = await Load(meta.LocalPath, code, cancellationToken);
|
||||||
|
if (plugin is NoPlugin noPlugin)
|
||||||
|
{
|
||||||
|
LOG.LogError($"Was not able to start plugin: Id='{meta.Id}', Type='{meta.Type}', Name='{meta.Name}', Version='{meta.Version}'. Reason: {noPlugin.Issues.First()}");
|
||||||
|
return noPlugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin.IsValid)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
// When this is a language plugin, we need to set the base language plugin.
|
||||||
|
//
|
||||||
|
if (plugin is PluginLanguage languagePlugin && BASE_LANGUAGE_PLUGIN != NoPluginLanguage.INSTANCE)
|
||||||
|
languagePlugin.SetBaseLanguage(BASE_LANGUAGE_PLUGIN);
|
||||||
|
|
||||||
|
LOG.LogInformation($"Successfully started plugin: Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}'");
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.LogError($"Was not able to start plugin: Id='{meta.Id}', Type='{meta.Type}', Name='{meta.Name}', Version='{meta.Version}'. Reasons: {string.Join("; ", plugin.Issues)}");
|
||||||
|
return new NoPlugin($"Was not able to start plugin: Id='{meta.Id}', Type='{meta.Type}', Name='{meta.Name}', Version='{meta.Version}'. Reasons: {string.Join("; ", plugin.Issues)}");
|
||||||
|
}
|
||||||
|
|
||||||
public static void Dispose()
|
public static void Dispose()
|
||||||
{
|
{
|
||||||
if(!IS_INITIALIZED)
|
if(!IS_INITIALIZED)
|
||||||
|
Loading…
Reference in New Issue
Block a user