Files
Vision/VisionAsist/Models/Core.cs
Egor ae0994409a
Some checks failed
Mirror to Gitea / git-sync (push) Has been cancelled
Yess
2026-03-28 00:26:55 +02:00

111 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using VisionAsist.SDK;
namespace VisionAsist.Models;
public class Core
{
public class LoadedModule
{
public string Name { get; set; } = string.Empty;
public IModule Module { get; set; } = null!;
public List<ToolDefinition> Tools { get; set; } = new();
}
public static List<LoadedModule> ModuleList = new();
static readonly string PluginPath = Path.Combine(AppContext.BaseDirectory, "Modules");
static Core()
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
if (!Directory.Exists(PluginPath)) Directory.CreateDirectory(PluginPath);
AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;
LoadModules();
}
private static Assembly? ResolveAssembly(object? sender, ResolveEventArgs args)
{
string assemblyName = new AssemblyName(args.Name).Name + ".dll";
foreach (var dir in Directory.GetDirectories(PluginPath))
{
string assemblyPath = Path.Combine(dir, assemblyName);
if (File.Exists(assemblyPath)) return Assembly.LoadFrom(assemblyPath);
}
return null;
}
private static void LoadModules()
{
if (!Directory.Exists(PluginPath)) return;
foreach (var dir in Directory.GetDirectories(PluginPath))
{
string folderName = Path.GetFileName(dir);
// DLL называется так же, как и папка
string dllPath = Path.Combine(dir, folderName + ".dll");
if (File.Exists(dllPath))
{
try
{
Assembly assembly = Assembly.LoadFrom(dllPath);
var type = assembly.GetTypes().FirstOrDefault(t =>
typeof(IModule).IsAssignableFrom(t) && !t.IsInterface && !t.IsAbstract);
if (type != null)
{
var module = (IModule)Activator.CreateInstance(type)!;
if (!ModuleList.Any(m => m.Name == module.Name))
{
ModuleList.Add(new LoadedModule
{
Name = module.Name,
Module = module,
Tools = module.GetTools()
});
Console.WriteLine($"[CORE]: Загружен модуль '{module.Name}' из {dllPath}");
}
}
}
catch (Exception ex)
{
Console.WriteLine($"[CORE ERROR]: Не удалось загрузить {dllPath}: {ex.Message}");
}
}
}
}
public static string GetToolsManifestJson()
{
var allTools = ModuleList.SelectMany(m => m.Tools.Select(t => new
{
type = "function",
function = new
{
name = t.Name,
description = t.Description,
parameters = JsonDocument.Parse(t.ParametersSchema).RootElement
}
})).ToList();
return JsonSerializer.Serialize(allTools, new JsonSerializerOptions { WriteIndented = true });
}
public static string CallTool(string toolName, string argsJson)
{
foreach (var module in ModuleList)
{
var tool = module.Tools.FirstOrDefault(t => t.Name == toolName);
if (tool != null) return module.Module.Execute(toolName, argsJson);
}
return "Ошибка: Инструмент не найден";
}
}