# alpaca: ollama llm client
# - super simple, easy UI
#
# shortcomings (as of 6.1.7, 2025-07-23):
# - doesn't seem to do any prompt tuning;
#   inherits all the pathologies of the underlying model (e.g. makes up citations)
#
# it creates a config dir, `~/.config/com.jeffser.Alpaca`, but apparently empty
#
# TODO: configure ollama connection details statically
# - until then, on first run:
#   - select the non-"managed" ollama option.
#   - connect to http://10.0.10.22:11434
# TODO: update the nix package 6.1.7 -> 7.5.2
# - i.e. review <https://github.com/NixOS/nixpkgs/pull/420698>
{ pkgs, ... }:
{
  sane.programs.alpaca = {
    packageUnwrapped = (pkgs.alpaca.override {
      # ollama is only added to `PATH`; since i'm using it via http, remove it here.
      # fixes cross compilation & simplifies closure.
      ollama = null;
      python3Packages = pkgs.python3Packages // {
        # XXX(2025-07-23): does not cross compile (markitdown -> pydub -> ... -> opencv)
        markitdown = null;
      };
    }).overrideAttrs (upstream: {
      postPatch = (upstream.postPatch or "") + ''
        # for nulled dependencies (above), patch so the application only errors
        # at runtime, on first attempted use.
        substituteInPlace src/widgets/attachments.py \
          --replace-fail 'from markitdown' '# from markitdown'
      '';
    });
    buildCost = 2;  #< liable to break cross during updates; not important enough to block deploy over

    sandbox.net = "all";  # maybe only needs wireguard, actually
    sandbox.whitelistWayland = true;
    sandbox.mesaCacheDir = ".cache/com.jeffser.Alpaca/mesa";

    sandbox.whitelistDbus.user.own = [ "com.jeffser.Alpaca" ];
    sandbox.whitelistPortal = [
      "OpenURI"
    ];
    sandbox.whitelistSendNotifications = true;

    persist.byStore.ephemeral = [
      ".cache/com.jeffser.Alpaca"  #< ?
    ];

    persist.byStore.private = [
      # alpaca.db: sqlite3 database with the following tables:
      # - attachment
      # - chat
      # - instances
      # - message
      # - model_preferences
      # - preferences
      # - tool_parameters
      ".local/share/com.jeffser.Alpaca"
    ];
  };
}
