diff --git a/README.md b/README.md index c077327..387c874 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,11 @@ We just finished a full course on retrieval-augmented generation and built a sma ## Try it -You need Python 3.12+, [uv](https://docs.astral.sh/uv/) and [Ollama](https://ollama.com/). +**On Windows, without installing anything else:** download Open RAG, the desktop app, from the +[latest release](https://github.com/Carthavate/open-rag-engine/releases/latest). Put the `.exe` and its `.bin` parts in +one folder and run the `.exe`. It brings its own models and works offline. + +**From source:** you need Python 3.12+, [uv](https://docs.astral.sh/uv/) and [Ollama](https://ollama.com/). ```bash uv sync @@ -88,5 +92,7 @@ We review each other's work through pull requests. See [CONTRIBUTING.md](CONTRIB ## Team -- [Wadi](https://github.com/TWadi) -- [Ghassen](https://github.com/GhassenJamoussi99) +Open RAG is developed by: + +- Wadi Touil: [GitHub](https://github.com/TWadi) · [LinkedIn](https://www.linkedin.com/in/wadi-touil-117472319/) +- Ghassen Jamoussi: [GitHub](https://github.com/GhassenJamoussi99) · [LinkedIn](https://www.linkedin.com/in/ghassen-jamoussi/) diff --git a/desktop/README.md b/desktop/README.md index bf6dbc0..61def12 100644 --- a/desktop/README.md +++ b/desktop/README.md @@ -53,3 +53,8 @@ npm test - **Against a real engine in the browser:** start `ORE_API_TOKEN=<32+ chars> uv run ore serve --origin http://localhost:5176`, then open `http://localhost:5176/?api=http://127.0.0.1:&token=`. - **Live reload inside the app (Debug builds only):** set `OPENRAG_DEV_URL=http://localhost:5176` before starting `OpenRag.exe`. + +## Installer + +`installer/build.py` builds `OpenRag-Setup.exe`, a self-contained setup that brings its own Python, models and +llama.cpp server, so it needs no internet after installing. See [installer/README.md](../installer/README.md). diff --git a/desktop/src/OpenRag.App/MainWindow.xaml.cs b/desktop/src/OpenRag.App/MainWindow.xaml.cs index 952cf71..8aee7fa 100644 --- a/desktop/src/OpenRag.App/MainWindow.xaml.cs +++ b/desktop/src/OpenRag.App/MainWindow.xaml.cs @@ -21,6 +21,7 @@ public partial class MainWindow : Window private const string DevUrlVariable = "OPENRAG_DEV_URL"; private static readonly TimeSpan EngineStartTimeout = TimeSpan.FromMinutes(3); + private static readonly TimeSpan HealthCheckTimeout = TimeSpan.FromSeconds(2); private static readonly JsonSerializerOptions Json = new(JsonSerializerDefaults.Web); private static readonly string WebRoot = Path.Combine(AppContext.BaseDirectory, "wwwroot"); @@ -32,7 +33,8 @@ public partial class MainWindow : Window private readonly HttpClient _http = new(new SocketsHttpHandler { UseProxy = false }) { Timeout = Timeout.InfiniteTimeSpan }; private readonly SemaphoreSlim _lifecycle = new(1, 1); private readonly Dictionary _asks = []; - private EngineHost? _engine; + private ChildHost? _engine; + private ChildHost? _llm; private EngineProxy? _proxy; private object _engineState = new { type = "engine", state = "starting" }; private string _origin = $"https://{AppHost}"; @@ -115,44 +117,77 @@ private async Task LaunchEngineAsync() } try { - var engine = new EngineHost(command, command.ServeArguments(_paths.Data, Environment.ProcessId), _paths.EngineLog); - engine.Started += process => _job?.Add(process); - // Only the current engine may report a crash; a late exit from a replaced one is ignored. - engine.Exited += code => Dispatcher.BeginInvoke(() => + var environment = new Dictionary { ["ORE_API_TOKEN"] = _token }; + string? llmUrl = null; + Task llmReady = Task.CompletedTask; + if (command.Llm is { } bundle) { - if (!ReferenceEquals(engine, _engine)) - { - return; - } - _proxy = null; - SetEngineState("failed", $"The engine stopped unexpectedly (exit code {code}). Details are in {_paths.EngineLog}."); - }); - _engine = engine; - var connection = await engine.StartAsync(new Dictionary { ["ORE_API_TOKEN"] = _token }, EngineStartTimeout); + // The language model loads while the engine warms up; both must be ready before the first question. + var port = LlmBundle.FreePort(); + var key = Token.New(); + llmUrl = $"http://127.0.0.1:{port}/v1"; + environment["ORE_LLM_API_KEY"] = key; + _llm = Watch(new ChildHost(new EngineCommand(bundle.ServerPath, [], Path.GetDirectoryName(bundle.ServerPath)!, null), + bundle.Arguments(port), _paths.LlmLog, "language model"), _paths.LlmLog); + llmReady = _llm.StartAsync(new Dictionary { [LlmBundle.ApiKeyVariable] = key }, + cancel => IsHealthyAsync(port, cancel), EngineStartTimeout); + } + _engine = Watch(new ChildHost(command, command.ServeArguments(_paths.Data, Environment.ProcessId, llmUrl), _paths.EngineLog), + _paths.EngineLog); + var connection = await _engine.StartAsync(environment, EngineStartTimeout); + await llmReady; _proxy = new EngineProxy(_http, connection.BaseUrl, _token); SetEngineState("ready"); } catch (EngineStartException e) { - SetEngineState("failed", $"{e.Message}\n\nThe full log is in {_paths.EngineLog}."); + await StopEngineAsync(); + SetEngineState("failed", $"{e.Message}\n\nThe logs are in {_paths.Logs}."); } catch (Exception e) { // Anything else (no permission to create the log folder, a blocked program) must not leave "starting" forever. LogError(e); + await StopEngineAsync(); SetEngineState("failed", $"Open RAG couldn't start its engine: {e.Message}\n\nDetails are in {_paths.ErrorLog}."); } } + /// Puts a helper process in the job object and reports a crash, but only while it is still the current one: + /// a late exit from a replaced engine or model is ignored. + private ChildHost Watch(ChildHost host, string log) + { + host.Started += process => _job?.Add(process); + host.Exited += code => Dispatcher.BeginInvoke(() => + { + if (!ReferenceEquals(host, _engine) && !ReferenceEquals(host, _llm)) + { + return; + } + _proxy = null; + var what = ReferenceEquals(host, _llm) ? "language model" : "engine"; + SetEngineState("failed", $"The {what} stopped unexpectedly (exit code {code}). Details are in {log}."); + }); + return host; + } + + /// llama-server answers /health with 200 once the model is loaded (503 while loading). + private async Task IsHealthyAsync(int port, CancellationToken cancel) + { + using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancel); + timeout.CancelAfter(HealthCheckTimeout); + using var response = await _http.GetAsync($"http://127.0.0.1:{port}/health", timeout.Token); + return response.IsSuccessStatusCode; + } + private async Task StopEngineAsync() { _proxy = null; var engine = _engine; + var llm = _llm; _engine = null; - if (engine is not null) - { - await engine.DisposeAsync(); - } + _llm = null; + await Task.WhenAll(engine?.DisposeAsync().AsTask() ?? Task.CompletedTask, llm?.DisposeAsync().AsTask() ?? Task.CompletedTask); } private void LogError(Exception e) @@ -262,6 +297,9 @@ private async void OnWebMessage(object? sender, CoreWebView2WebMessageReceivedEv case "restartEngine": await StartEngineAsync(restart: true); break; + case "openLink": + OpenLink(message.GetProperty("url").GetString()); + break; case "api": await ForwardAsync(message); break; @@ -420,6 +458,23 @@ private async Task OpenSourceAsync(string? path, bool reveal) } } + /// Opens one of the app's own links (the credits, the source code) in the default browser. + private void OpenLink(string? url) + { + if (!ExternalLinks.IsAllowed(url)) + { + return; + } + try + { + Process.Start(new ProcessStartInfo(url!) { UseShellExecute = true }); + } + catch (System.ComponentModel.Win32Exception ex) + { + Post(new { type = "notice", message = $"Couldn't open your browser: {ex.Message}" }); + } + } + private async Task ShutdownAsync() { SystemEvents.UserPreferenceChanged -= OnUserPreferenceChanged; diff --git a/desktop/src/OpenRag.App/OpenRag.App.csproj b/desktop/src/OpenRag.App/OpenRag.App.csproj index 8685a25..b55fc03 100644 --- a/desktop/src/OpenRag.App/OpenRag.App.csproj +++ b/desktop/src/OpenRag.App/OpenRag.App.csproj @@ -17,6 +17,9 @@ OpenRag Open RAG Open RAG + Wadi Touil; Ghassen Jamoussi + Carthavate + Copyright (C) 2026 Wadi Touil and Ghassen Jamoussi 0.1.0 Assets\OpenRag.ico $(MSBuildThisFileDirectory)..\..\web\ diff --git a/desktop/src/OpenRag.Core/ChildHost.cs b/desktop/src/OpenRag.Core/ChildHost.cs new file mode 100644 index 0000000..ca998ca --- /dev/null +++ b/desktop/src/OpenRag.Core/ChildHost.cs @@ -0,0 +1,238 @@ +using System.Diagnostics; + +namespace OpenRag.Core; + +public sealed record EngineConnection(Uri BaseUrl); + +public sealed class EngineStartException(string message) : Exception(message); + +/// Runs a helper program (the engine, or the bundled language model server) as a hidden child process, writes +/// its output to a log, and tells when it is ready: either from a line it prints, or from a health check. +public sealed class ChildHost(EngineCommand command, IReadOnlyList arguments, string logPath, string name = "engine") + : IAsyncDisposable +{ + private const int TailLines = 12; + private static readonly TimeSpan ProbeInterval = TimeSpan.FromMilliseconds(500); + + private readonly Queue _tail = new(); + private readonly Lock _log = new(); + private readonly TaskCompletionSource _readyLine = new(TaskCreationOptions.RunContinuationsAsynchronously); + private readonly TaskCompletionSource _exited = new(TaskCreationOptions.RunContinuationsAsynchronously); + private Process? _process; + private volatile bool _stopping; + private volatile bool _ready; + + /// Raised when the process exits after it was ready, unless we stopped it ourselves. + public event Action? Exited; + + /// Lets the app put the process in its job object, so it dies with the app. + public event Action? Started; + + /// Starts the process and waits for its ORE_READY port=N line. + public async Task StartAsync(IReadOnlyDictionary environment, TimeSpan timeout, + CancellationToken cancel = default) + { + Start(environment); + return await WaitAsync(_readyLine.Task, timeout, cancel); + } + + /// Starts the process and waits until says it is ready (for example a health check). + public async Task StartAsync(IReadOnlyDictionary environment, Func> probe, + TimeSpan timeout, CancellationToken cancel = default) + { + Start(environment); + await WaitAsync(Probe(probe), timeout, cancel); + } + + private async Task Probe(Func> probe) + { + using var stop = new CancellationTokenSource(); + _ = _exited.Task.ContinueWith(_ => stop.Cancel(), TaskScheduler.Default); + while (!stop.IsCancellationRequested) + { + try + { + if (await probe(stop.Token)) + { + return true; + } + await Task.Delay(ProbeInterval, stop.Token); + } + catch (Exception e) when (e is OperationCanceledException or HttpRequestException) + { + // Not up yet, or it exited; WaitAsync reports which. + } + } + return false; + } + + /// Above this size the log moves to .old when the process next starts, so it can't grow forever. + public const long MaxLogBytes = 5 * 1024 * 1024; + + private void Start(IReadOnlyDictionary environment) + { + Directory.CreateDirectory(Path.GetDirectoryName(logPath)!); + RotateLog(); + var info = new ProcessStartInfo(command.FileName) + { + WorkingDirectory = command.WorkingDirectory, + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + StandardOutputEncoding = System.Text.Encoding.UTF8, + StandardErrorEncoding = System.Text.Encoding.UTF8, + }; + foreach (var argument in arguments) + { + info.ArgumentList.Add(argument); + } + info.Environment["PYTHONUTF8"] = "1"; + foreach (var (key, value) in environment) + { + info.Environment[key] = value; + } + + var process = new Process { StartInfo = info, EnableRaisingEvents = true }; + process.OutputDataReceived += (_, e) => + { + Write(e.Data); + if (ReadyLine.TryParsePort(e.Data) is { } port) + { + _readyLine.TrySetResult(new EngineConnection(new Uri($"http://127.0.0.1:{port}/"))); + } + }; + process.ErrorDataReceived += (_, e) => Write(e.Data); + process.Exited += (_, _) => + { + var code = SafeExitCode(process); + _exited.TrySetResult(code); + // Before it was ready, the waiting start reports the failure. A stop we asked for is not a crash. + if (_ready && !_stopping) + { + Exited?.Invoke(code); + } + }; + + try + { + process.Start(); + } + catch (System.ComponentModel.Win32Exception e) + { + throw new EngineStartException($"Couldn't start the {name} ({command.FileName}): {e.Message}"); + } + _process = process; + Started?.Invoke(process); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + } + + private async Task WaitAsync(Task ready, TimeSpan timeout, CancellationToken cancel) + { + using var timer = CancellationTokenSource.CreateLinkedTokenSource(cancel); + timer.CancelAfter(timeout); + try + { + var first = await Task.WhenAny(ready, _exited.Task).WaitAsync(timer.Token); + if (first == ready && !_exited.Task.IsCompleted && (ready.Result is not bool ok || ok)) + { + _ready = true; + return ready.Result; + } + throw new EngineStartException(FailureMessage(await _exited.Task)); + } + catch (OperationCanceledException) when (!cancel.IsCancellationRequested) + { + Stop(); + throw new EngineStartException($"The {name} didn't start within {timeout.TotalSeconds:0} seconds. {Tail()}"); + } + } + + public void Stop() + { + _stopping = true; + try + { + if (_process is { HasExited: false } process) + { + process.Kill(entireProcessTree: true); + } + } + catch (Exception e) when (e is InvalidOperationException or System.ComponentModel.Win32Exception) + { + // Already gone. + } + } + + public async ValueTask DisposeAsync() + { + Stop(); + if (_process is not null) + { + await _process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(5)).ContinueWith(_ => { }); + _process.Dispose(); + } + } + + private void Write(string? line) + { + if (line is null) + { + return; + } + lock (_log) + { + _tail.Enqueue(line); + while (_tail.Count > TailLines) + { + _tail.Dequeue(); + } + try + { + File.AppendAllText(logPath, $"{DateTime.Now:HH:mm:ss} {line}{Environment.NewLine}"); + } + catch (IOException) + { + // The log is a convenience; a locked file must not stop the process. + } + } + } + + private void RotateLog() + { + try + { + if (File.Exists(logPath) && new FileInfo(logPath).Length > MaxLogBytes) + { + File.Move(logPath, logPath + ".old", overwrite: true); + } + } + catch (IOException) + { + // Rotation is housekeeping; the process starts either way. + } + } + + private string Tail() + { + lock (_log) + { + return _tail.Count == 0 ? "" : "Last output:\n" + string.Join("\n", _tail); + } + } + + private string FailureMessage(int code) => $"The {name} stopped (exit code {code}) before it was ready. {Tail()}".Trim(); + + private static int SafeExitCode(Process process) + { + try + { + return process.ExitCode; + } + catch (InvalidOperationException) + { + return -1; + } + } +} diff --git a/desktop/src/OpenRag.Core/Engine.cs b/desktop/src/OpenRag.Core/Engine.cs index 0a743d2..c062e48 100644 --- a/desktop/src/OpenRag.Core/Engine.cs +++ b/desktop/src/OpenRag.Core/Engine.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System.Net; +using System.Net.Sockets; using System.Security.Cryptography; using System.Text.RegularExpressions; @@ -14,6 +15,7 @@ public sealed record AppPaths(string Root) public string WebView => Path.Combine(Root, "WebView2"); public string Logs => Path.Combine(Root, "logs"); public string EngineLog => Path.Combine(Logs, "engine.log"); + public string LlmLog => Path.Combine(Logs, "llm.log"); public string ErrorLog => Path.Combine(Logs, "errors.log"); } @@ -56,12 +58,42 @@ public static bool IsLocalDocumentPath(string? path) => public static bool CanOpen(string? path) => IsLocalDocumentPath(path) && File.Exists(path); } -/// How to start the engine: a program, its base arguments, where to run it, and an optional config file. -public sealed record EngineCommand(string FileName, IReadOnlyList Arguments, string WorkingDirectory, string? ConfigPath) +/// The language model an installed app brings along: llama.cpp's server and a GGUF model file. +public sealed record LlmBundle(string ServerPath, string ModelPath) { - public IReadOnlyList ServeArguments(string dataDirectory, int parentProcessId) + public const string ModelAlias = "qwen3-4b-instruct"; + + /// llama-server reads its key from here, so the key never shows on its command line. + public const string ApiKeyVariable = "LLAMA_API_KEY"; + + /// Loopback only, with its own key; the context matches the engine's default (8192 tokens). + public IReadOnlyList Arguments(int port) => + ["-m", ModelPath, "--host", "127.0.0.1", "--port", port.ToString(), "--ctx-size", "8192", "--jinja", + "--alias", ModelAlias, "--no-webui"]; + + public static int FreePort() + { + var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + var port = ((IPEndPoint)listener.LocalEndpoint).Port; + listener.Stop(); + return port; + } +} + +/// How to start the engine: a program, its base arguments, where to run it, an optional config file, and the +/// bundled language model when there is one. +public sealed record EngineCommand(string FileName, IReadOnlyList Arguments, string WorkingDirectory, string? ConfigPath, + LlmBundle? Llm = null) +{ + /// The bundled language model's server, when the app runs one (an OpenAI-compatible /v1 URL). + public IReadOnlyList ServeArguments(string dataDirectory, int parentProcessId, string? llmUrl = null) { var args = new List(Arguments) { "--data-dir", dataDirectory, "--parent-pid", parentProcessId.ToString(), "--warm" }; + if (llmUrl is not null) + { + args.AddRange(["--llm-url", llmUrl]); + } if (ConfigPath is not null) { args.AddRange(["--config", ConfigPath]); @@ -84,7 +116,10 @@ public static class EngineLocator if (File.Exists(python)) { var config = Path.Combine(engine, "ore.toml"); - return new EngineCommand(python, ["-m", "open_rag_engine", "serve"], engine, File.Exists(config) ? config : null); + var server = Path.Combine(engine, "llm", "llama-server.exe"); + var model = Path.Combine(engine, "llm", "model.gguf"); + var llm = File.Exists(server) && File.Exists(model) ? new LlmBundle(server, model) : null; + return new EngineCommand(python, ["-m", "open_rag_engine", "serve"], engine, File.Exists(config) ? config : null, llm); } if (!allowCheckout) { @@ -112,166 +147,3 @@ private static bool IsCheckout(string dir) return File.Exists(pyproject) && File.ReadAllText(pyproject).Contains("name = \"open-rag-engine\"", StringComparison.Ordinal); } } - -public sealed record EngineConnection(Uri BaseUrl); - -public sealed class EngineStartException(string message) : Exception(message); - -/// Runs the engine as a hidden child process, writes its output to a log, and reports the port it listens on. -public sealed class EngineHost(EngineCommand command, IReadOnlyList arguments, string logPath) : IAsyncDisposable -{ - private const int TailLines = 12; - private readonly Queue _tail = new(); - private readonly Lock _log = new(); - private Process? _process; - private volatile bool _stopping; - - /// Raised when the engine exits after it was ready (a crash, or killed from outside). - public event Action? Exited; - - /// Lets the app put the process in its job object, so the engine dies with the app. - public event Action? Started; - - public async Task StartAsync(IReadOnlyDictionary environment, TimeSpan timeout, - CancellationToken cancel = default) - { - Directory.CreateDirectory(Path.GetDirectoryName(logPath)!); - var info = new ProcessStartInfo(command.FileName) - { - WorkingDirectory = command.WorkingDirectory, - UseShellExecute = false, - CreateNoWindow = true, - RedirectStandardOutput = true, - RedirectStandardError = true, - StandardOutputEncoding = System.Text.Encoding.UTF8, - StandardErrorEncoding = System.Text.Encoding.UTF8, - }; - foreach (var argument in arguments) - { - info.ArgumentList.Add(argument); - } - info.Environment["PYTHONUTF8"] = "1"; - foreach (var (key, value) in environment) - { - info.Environment[key] = value; - } - - var ready = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var process = new Process { StartInfo = info, EnableRaisingEvents = true }; - process.OutputDataReceived += (_, e) => - { - Write(e.Data); - if (ReadyLine.TryParsePort(e.Data) is { } port) - { - ready.TrySetResult(new EngineConnection(new Uri($"http://127.0.0.1:{port}/"))); - } - }; - process.ErrorDataReceived += (_, e) => Write(e.Data); - process.Exited += (_, _) => - { - var code = SafeExitCode(process); - // A stop we asked for is not a crash, so it isn't reported. - if (!ready.TrySetException(new EngineStartException(FailureMessage(code))) && !_stopping) - { - Exited?.Invoke(code); - } - }; - - try - { - process.Start(); - } - catch (System.ComponentModel.Win32Exception e) - { - throw new EngineStartException($"Couldn't start the engine ({command.FileName}): {e.Message}"); - } - _process = process; - Started?.Invoke(process); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - - using var timer = CancellationTokenSource.CreateLinkedTokenSource(cancel); - timer.CancelAfter(timeout); - try - { - return await ready.Task.WaitAsync(timer.Token); - } - catch (OperationCanceledException) when (!cancel.IsCancellationRequested) - { - Stop(); - throw new EngineStartException($"The engine didn't start within {timeout.TotalSeconds:0} seconds. {Tail()}"); - } - } - - public void Stop() - { - _stopping = true; - try - { - if (_process is { HasExited: false } process) - { - process.Kill(entireProcessTree: true); - } - } - catch (Exception e) when (e is InvalidOperationException or System.ComponentModel.Win32Exception) - { - // Already gone. - } - } - - public async ValueTask DisposeAsync() - { - Stop(); - if (_process is not null) - { - await _process.WaitForExitAsync().WaitAsync(TimeSpan.FromSeconds(5)).ContinueWith(_ => { }); - _process.Dispose(); - } - } - - private void Write(string? line) - { - if (line is null) - { - return; - } - lock (_log) - { - _tail.Enqueue(line); - while (_tail.Count > TailLines) - { - _tail.Dequeue(); - } - try - { - File.AppendAllText(logPath, $"{DateTime.Now:HH:mm:ss} {line}{Environment.NewLine}"); - } - catch (IOException) - { - // The log is a convenience; a locked file must not stop the engine. - } - } - } - - private string Tail() - { - lock (_log) - { - return _tail.Count == 0 ? "" : "Last output:\n" + string.Join("\n", _tail); - } - } - - private string FailureMessage(int code) => $"The engine stopped (exit code {code}) before it was ready. {Tail()}".Trim(); - - private static int SafeExitCode(Process process) - { - try - { - return process.ExitCode; - } - catch (InvalidOperationException) - { - return -1; - } - } -} diff --git a/desktop/src/OpenRag.Core/ExternalLinks.cs b/desktop/src/OpenRag.Core/ExternalLinks.cs new file mode 100644 index 0000000..1de1982 --- /dev/null +++ b/desktop/src/OpenRag.Core/ExternalLinks.cs @@ -0,0 +1,17 @@ +namespace OpenRag.Core; + +/// The only web pages the app opens, in the default browser: its authors' profiles and its source code. The +/// page asks by URL; anything not on this list is ignored, so even a compromised page can't open other sites. +public static class ExternalLinks +{ + public static IReadOnlySet Allowed { get; } = new HashSet(StringComparer.Ordinal) + { + "https://github.com/Carthavate/open-rag-engine", + "https://github.com/TWadi", + "https://www.linkedin.com/in/wadi-touil-117472319/", + "https://github.com/GhassenJamoussi99", + "https://www.linkedin.com/in/ghassen-jamoussi/", + }; + + public static bool IsAllowed(string? url) => url is not null && Allowed.Contains(url); +} diff --git a/desktop/tests/OpenRag.Core.Tests/EngineTests.cs b/desktop/tests/OpenRag.Core.Tests/EngineTests.cs index e72f73b..aeff025 100644 --- a/desktop/tests/OpenRag.Core.Tests/EngineTests.cs +++ b/desktop/tests/OpenRag.Core.Tests/EngineTests.cs @@ -104,6 +104,23 @@ public void Prefers_the_bundled_engine_next_to_the_app() Assert.Equal(config, command.ConfigPath); } + [Fact] + public void An_installed_app_brings_its_language_model() + { + using var dir = new TempDir(); + dir.Make("engine", "python", "python.exe"); + var server = dir.Make("engine", "llm", "llama-server.exe"); + var model = dir.Make("engine", "llm", "model.gguf"); + var command = EngineLocator.Find(dir.Path, Env(), allowCheckout: false)!; + Assert.Equal(new LlmBundle(server, model), command.Llm); + var args = command.Llm!.Arguments(4242); + Assert.Equal("127.0.0.1", args[args.ToList().IndexOf("--host") + 1]); + Assert.Equal("4242", args[args.ToList().IndexOf("--port") + 1]); + } + + [Fact] + public void Free_ports_are_real_loopback_ports() => Assert.InRange(LlmBundle.FreePort(), 1024, 65535); + [Fact] public void Finds_a_development_checkout_above_the_app() { @@ -150,9 +167,18 @@ public void Serve_arguments_add_data_dir_parent_and_config() Assert.Equal(["-m", "open_rag_engine", "serve", "--data-dir", "C:/data", "--parent-pid", "42", "--warm", "--config", "C:/app/ore.toml"], args); } + + [Fact] + public void Serve_arguments_point_the_engine_at_the_bundled_model() + { + var command = new EngineCommand("python.exe", ["-m", "open_rag_engine", "serve"], "C:/app", null); + var args = command.ServeArguments("C:/data", 42, "http://127.0.0.1:5000/v1"); + Assert.Equal(["-m", "open_rag_engine", "serve", "--data-dir", "C:/data", "--parent-pid", "42", "--warm", + "--llm-url", "http://127.0.0.1:5000/v1"], args); + } } -public class EngineHostTests +public class ChildHostTests { private static EngineCommand Cmd(string script) => new("cmd.exe", ["/c", script], Environment.CurrentDirectory, null); @@ -160,7 +186,7 @@ public class EngineHostTests public async Task Start_returns_the_port_the_engine_announces() { using var dir = new TempDir(); - await using var host = new EngineHost(Cmd("echo starting & echo ORE_READY port=4321 & ping -n 20 127.0.0.1 >nul"), + await using var host = new ChildHost(Cmd("echo starting & echo ORE_READY port=4321 & ping -n 20 127.0.0.1 >nul"), ["/c", "echo starting & echo ORE_READY port=4321 & ping -n 20 127.0.0.1 >nul"], Path.Combine(dir.Path, "engine.log")); var connection = await host.StartAsync(new Dictionary { ["ORE_API_TOKEN"] = "x" }, TimeSpan.FromSeconds(20)); Assert.Equal(new Uri("http://127.0.0.1:4321/"), connection.BaseUrl); @@ -172,7 +198,7 @@ public async Task A_deliberate_stop_is_not_reported_as_a_crash() { using var dir = new TempDir(); var script = "echo ORE_READY port=4322 & ping -n 30 127.0.0.1 >nul"; - await using var host = new EngineHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "engine.log")); + await using var host = new ChildHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "engine.log")); var crashes = 0; host.Exited += _ => crashes++; await host.StartAsync(new Dictionary(), TimeSpan.FromSeconds(20)); @@ -186,18 +212,41 @@ public async Task A_start_that_times_out_stops_the_process() { using var dir = new TempDir(); var script = "ping -n 30 127.0.0.1 >nul"; - await using var host = new EngineHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "engine.log")); + await using var host = new ChildHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "engine.log")); System.Diagnostics.Process? started = null; host.Started += p => started = p; await Assert.ThrowsAsync(() => host.StartAsync(new Dictionary(), TimeSpan.FromSeconds(1))); Assert.True(started!.WaitForExit(5000)); } + [Fact] + public async Task A_health_probe_decides_when_a_server_is_ready() + { + using var dir = new TempDir(); + var script = "echo loading & ping -n 30 127.0.0.1 >nul"; + await using var host = new ChildHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "llm.log"), "language model"); + var checks = 0; + await host.StartAsync(new Dictionary(), _ => Task.FromResult(++checks >= 3), TimeSpan.FromSeconds(20)); + Assert.Equal(3, checks); + } + + [Fact] + public async Task A_server_that_exits_while_loading_is_reported() + { + using var dir = new TempDir(); + var script = "echo failed to load model 1>&2 & exit 1"; + await using var host = new ChildHost(Cmd(script), ["/c", script], Path.Combine(dir.Path, "llm.log"), "language model"); + var error = await Assert.ThrowsAsync(() => + host.StartAsync(new Dictionary(), _ => Task.FromResult(false), TimeSpan.FromSeconds(20))); + Assert.Contains("failed to load model", error.Message); + Assert.Contains("language model", error.Message); + } + [Fact] public async Task A_crash_before_ready_reports_the_last_output() { using var dir = new TempDir(); - await using var host = new EngineHost(Cmd("echo no module named open_rag_engine 1>&2 & exit 3"), + await using var host = new ChildHost(Cmd("echo no module named open_rag_engine 1>&2 & exit 3"), ["/c", "echo no module named open_rag_engine 1>&2 & exit 3"], Path.Combine(dir.Path, "engine.log")); var error = await Assert.ThrowsAsync(() => host.StartAsync(new Dictionary(), TimeSpan.FromSeconds(20))); diff --git a/desktop/tests/OpenRag.Core.Tests/ExternalLinksTests.cs b/desktop/tests/OpenRag.Core.Tests/ExternalLinksTests.cs new file mode 100644 index 0000000..be68cf4 --- /dev/null +++ b/desktop/tests/OpenRag.Core.Tests/ExternalLinksTests.cs @@ -0,0 +1,46 @@ +using System.Text.RegularExpressions; +using OpenRag.Core; + +namespace OpenRag.Core.Tests; + +public class ExternalLinksTests +{ + [Theory] + [InlineData("https://github.com/TWadi")] + [InlineData("https://www.linkedin.com/in/ghassen-jamoussi/")] + [InlineData("https://github.com/Carthavate/open-rag-engine")] + public void Opens_the_credits_and_the_source(string url) => Assert.True(ExternalLinks.IsAllowed(url)); + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("http://github.com/TWadi")] + [InlineData("https://github.com/TWadi/../evil")] + [InlineData("https://github.com/TWadi.evil.example")] + [InlineData("https://GITHUB.com/TWadi")] + [InlineData("file:///C:/Windows/System32/calc.exe")] + [InlineData("javascript:alert(1)")] + public void Refuses_anything_else(string? url) => Assert.False(ExternalLinks.IsAllowed(url)); + + /// The Credits sheet (desktop/web/src/lib/credits.ts) must only show links the window will open. + [Fact] + public void Every_link_in_the_credits_sheet_opens() + { + var credits = File.ReadAllText(Path.Combine(DesktopFolder(), "web", "src", "lib", "credits.ts")); + var links = Regex.Matches(credits, "\"(https://[^\"]+)\"").Select(m => m.Groups[1].Value).ToList(); + Assert.Equal(5, links.Count); + Assert.All(links, link => Assert.True(ExternalLinks.IsAllowed(link), $"{link} isn't in ExternalLinks.Allowed")); + } + + private static string DesktopFolder() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) + { + if (File.Exists(Path.Combine(dir.FullName, "OpenRag.slnx"))) + { + return dir.FullName; + } + } + throw new DirectoryNotFoundException("desktop/OpenRag.slnx not found above the test output."); + } +} diff --git a/desktop/web/src/components/Credits.tsx b/desktop/web/src/components/Credits.tsx new file mode 100644 index 0000000..fa4d46f --- /dev/null +++ b/desktop/web/src/components/Credits.tsx @@ -0,0 +1,88 @@ +import { ExternalLink } from "lucide-react"; +import { useState, type ReactNode } from "react"; +import { CREDITS, SOURCE_URL, type Credit } from "../lib/credits"; +import { host } from "../lib/host"; +import { Sheet } from "./Dialogs"; +import { Button, LogoMark } from "./primitives"; + +function Initials({ credit, className = "" }: { credit: Credit; className?: string }) { + return ( + + ); +} + +function LinkButton({ href, label, children }: { href: string; label: string; children: ReactNode }) { + return ( + + ); +} + +/** The sidebar's "Developed by" line; it opens the Credits sheet with both authors' GitHub and LinkedIn. */ +export function Credits() { + const [open, setOpen] = useState(false); + const first = (c: Credit) => c.name.split(" ")[0]; + return ( + <> + + + setOpen(false)}> +
+ +

Credits

+
+

+ Open RAG is developed by Wadi and Ghassen, from the RAG course we made for the AI Engineering Arena. +

+
    + {CREDITS.map((c) => ( +
  • +
    + +
    +

    {c.name}

    +

    {c.role}

    +
    +
    +
    + GitHub + LinkedIn +
    +
  • + ))} +
+

+ Free and open source (AGPL-3.0).{" "} + +

+
+ +
+
+ + ); +} diff --git a/desktop/web/src/components/Dialogs.tsx b/desktop/web/src/components/Dialogs.tsx index 70e2072..92d5b7d 100644 --- a/desktop/web/src/components/Dialogs.tsx +++ b/desktop/web/src/components/Dialogs.tsx @@ -2,7 +2,7 @@ import { AnimatePresence, motion } from "motion/react"; import { useEffect, useRef, useState, type ReactNode } from "react"; import { Button } from "./primitives"; -function Sheet({ open, onClose, children }: { open: boolean; onClose: () => void; children: ReactNode }) { +export function Sheet({ open, onClose, children }: { open: boolean; onClose: () => void; children: ReactNode }) { useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose(); diff --git a/desktop/web/src/components/Sidebar.tsx b/desktop/web/src/components/Sidebar.tsx index d34611a..7d8c0cc 100644 --- a/desktop/web/src/components/Sidebar.tsx +++ b/desktop/web/src/components/Sidebar.tsx @@ -1,6 +1,7 @@ import { AlertCircle, BookOpen, ChevronRight, FolderClosed, Pencil, Plus, ShieldCheck, Trash2 } from "lucide-react"; import { useState } from "react"; import type { Category, Library, Scope, Section } from "../lib/types"; +import { Credits } from "./Credits"; import { IconButton, Spinner } from "./primitives"; type Props = { @@ -38,9 +39,12 @@ export function Sidebar(props: Props) {

)} -