Implemented indexer, cleanup

This commit is contained in:
EzFeDezy
2025-05-26 15:53:18 +02:00
parent 85141c879f
commit f6a4a75e4f
22 changed files with 791 additions and 50 deletions

View File

@@ -0,0 +1,12 @@
namespace Indexer.Models;
public interface IScriptable
{
ScriptToolSet ToolSet { get; set; }
void Init();
void Update(ICallbackInfos callbackInfos);
bool IsScript(string filePath);
}
public interface ICallbackInfos { }

View File

@@ -0,0 +1,94 @@
using System.Text.Json;
using System.Timers;
using embeddingsearch;
using Python.Runtime;
namespace Indexer.Models;
public class PythonScriptable : IScriptable
{
public ScriptToolSet ToolSet { get; set; }
public PyObject? pyToolSet;
public PyModule scope;
public dynamic sys;
public string source;
public PythonScriptable(ScriptToolSet toolSet)
{
Runtime.PythonDLL = @"libpython3.12.so";
if (!PythonEngine.IsInitialized)
{
PythonEngine.Initialize();
PythonEngine.BeginAllowThreads();
}
ToolSet = toolSet;
source = File.ReadAllText(ToolSet.filePath);
string fullPath = Path.GetFullPath(ToolSet.filePath);
string? scriptDir = Path.GetDirectoryName(fullPath);
using (Py.GIL())
{
scope = Py.CreateScope();
sys = Py.Import("sys");
if (scriptDir is not null)
{
sys.path.append(scriptDir);
}
}
Init();
}
public void Init()
{
using (Py.GIL())
{
pyToolSet = ToolSet.ToPython();
scope.Set("toolset", pyToolSet);
scope.Exec(source);
scope.Exec("init(toolset)");
}
}
public void Update(ICallbackInfos callbackInfos)
{
PythonEngine.Initialize();
using (Py.GIL())
{
ToolSet.callbackInfos = callbackInfos;
pyToolSet = ToolSet.ToPython();
scope.Set("toolset", pyToolSet);
scope.Exec("update(toolset)");
}
PythonEngine.Shutdown();
}
public bool IsScript(string fileName)
{
return fileName.EndsWith(".py");
}
}
/*
TODO Add the following languages
- Javascript
- Golang (reconsider)
*/
public class ScriptToolSet
{
public string filePath;
public Client.Client client;
public ICallbackInfos? callbackInfos;
// IConfiguration - Access to connection strings, ollama, etc. maybe?
public ScriptToolSet(string filePath, Client.Client client)
{
this.filePath = filePath;
this.client = client;
}
}
public class IntervalCallbackInfos : ICallbackInfos
{
public object? sender;
public required ElapsedEventArgs e;
}

View File

@@ -0,0 +1,45 @@
namespace Indexer.Models;
public class WorkerCollection
{
public List<Worker> Workers;
public List<Type> types;
public WorkerCollection()
{
Workers = [];
types = [typeof(PythonScriptable)];
}
}
public class Worker
{
public WorkerConfig Config { get; set; }
public IScriptable Scriptable { get; set; }
public Worker(WorkerConfig workerConfig, IScriptable scriptable)
{
this.Config = workerConfig;
this.Scriptable = scriptable;
}
}
public class WorkerCollectionConfig
{
public required List<WorkerConfig> Worker { get; set; }
}
public class WorkerConfig
{
public required string Name { get; set; }
public required List<string> Searchdomains { get; set; }
public required string Script { get; set; }
public required List<Call> Calls { get; set; }
}
public class Call
{
public required string Type { get; set; }
public long? Interval { get; set; } // For Type: Interval
public string? Path { get; set; } // For Type: FileSystemWatcher
}