feat: Added support for getting aichat api key from commands and files

This commit is contained in:
uttarayan21
2024-11-26 16:08:13 +05:30
parent 3d4a8244f1
commit 8c1e1a47d3
2 changed files with 86 additions and 28 deletions

View File

@@ -231,7 +231,6 @@ in {
shellAliases = shellAliases =
{ {
g = "git"; g = "git";
aichat = "op plugin run -- aichat";
} }
// lib.optionalAttrs pkgs.stdenv.isLinux { // lib.optionalAttrs pkgs.stdenv.isLinux {
kmpv = "mpv --vo-kitty-use-shm=yes --vo=kitty --really-quiet"; kmpv = "mpv --vo-kitty-use-shm=yes --vo=kitty --really-quiet";
@@ -244,15 +243,6 @@ in {
# ${pkgs.spotify-player}/bin/spotify_player generate fish | source # ${pkgs.spotify-player}/bin/spotify_player generate fish | source
interactiveShellInit = '' interactiveShellInit = ''
${pkgs.pfetch-rs}/bin/pfetch ${pkgs.pfetch-rs}/bin/pfetch
function _aichat_fish
set -l _old (commandline)
if test -n $_old
echo -n ""
commandline -f repaint
commandline (aichat -e $_old)
end
end
bind \co _aichat_fish
${lib.optionalString (device.isLinux && !device.isNix) "source /etc/profile.d/nix-daemon.fish"} ${lib.optionalString (device.isLinux && !device.isNix) "source /etc/profile.d/nix-daemon.fish"}
''; '';
}; };
@@ -352,20 +342,22 @@ in {
home-manager = {enable = true;}; home-manager = {enable = true;};
aichat = { aichat = {
enable = true; enable = true;
enableFishIntegration = true;
settings = { settings = {
clients = [ clients = [
{ {
type = "openai-compatible"; type = "openai-compatible";
name = "llama"; name = "llama";
api_base = "https://llama.darksailor.dev/api/v1"; api_base = "https://llama.darksailor.dev/api/v1";
api_key_cmd = "op item get llama-api --fields label=credential --reveal";
models = [ models = [
{ {
name = "chat"; name = "qwen_2_5_1";
} }
]; ];
} }
]; ];
model = "llama:chat"; model = "llama:qwen_2_5_1";
}; };
}; };
}; };

View File

@@ -7,21 +7,71 @@
with lib; let with lib; let
cfg = config.programs.aichat; cfg = config.programs.aichat;
yamlFormat = pkgs.formats.yaml {}; yamlFormat = pkgs.formats.yaml {};
# configDir = fishIntegration = ''
# if pkgs.stdenv.isDarwin then function _aichat_fish
# "${config.home.homeDirectory}Library/Application Support/aichat" set -l _old (commandline)
# else if test -n $_old
# "${config.xdg.configHome}/aichat"; echo -n ""
commandline -f repaint
commandline (aichat -e $_old)
end
end
bind \co _aichat_fish'';
nuIntegration = ''
def _aichat_nushell [] {
let _prev = (commandline)
if ($_prev != "") {
print ''
commandline edit -r (aichat -e $_prev)
}
}
$env.config.keybindings = ($env.config.keybindings | append {
name: aichat_integration
modifier: control
keycode: char_o
mode: [emacs, vi_insert]
event:[
{
send: executehostcommand,
cmd: "_aichat_nushell"
}
]
}
)
'';
bashIntegration = ''
_aichat_bash() {
if [[ -n "$READLINE_LINE" ]]; then
READLINE_LINE=$(aichat -e "$READLINE_LINE")
READLINE_POINT=''${#READLINE_LINE}
fi
}
bind -x '"\co": _aichat_bash'
'';
zshIntegration = ''
_aichat_zsh() {
if [[ -n "$BUFFER" ]]; then
local _old=$BUFFER
BUFFER+=""
zle -I && zle redisplay
BUFFER=$(aichat -e "$_old")
zle end-of-line
fi
}
zle -N _aichat_zsh
bindkey '\co' _aichat_zsh
'';
in { in {
options = { options = {
programs.aichat = { programs.aichat = {
enable = mkEnableOption "aichat"; enable = mkEnableOption "aichat";
package = mkOption { package = mkPackageOption pkgs "aichat" {};
type = types.package;
default = pkgs.aichat; enableFishIntegration = mkEnableOption "Fish integration" // {default = true;};
defaultText = literalExpression "pkgs.aichat"; enableBashIntegration = mkEnableOption "Bash integration" // {default = true;};
description = "The aichat package to install."; enableZshIntegration = mkEnableOption "Zsh integration" // {default = true;};
}; enableNushellIntegration = mkEnableOption "Nushell integration" // {default = true;};
settings = lib.mkOption { settings = lib.mkOption {
type = yamlFormat.type; type = yamlFormat.type;
@@ -30,13 +80,29 @@ in {
}; };
}; };
config = { config = let
home.packages = mkIf cfg.enable [cfg.package]; api_key_files = concatStringsSep " " (builtins.map (client: ''--run 'export ${lib.toUpper client.name}_API_KEY=`cat -v ${client.api_key_file}`' '') (builtins.filter (client: (builtins.hasAttr "api_key_file" client)) cfg.settings.clients));
api_key_cmds = concatStringsSep " " (builtins.map (client: ''--run 'export ${lib.toUpper client.name}_API_KEY=`${client.api_key_cmd}`' '') (builtins.filter (client: (builtins.hasAttr "api_key_cmd" client)) cfg.settings.clients));
aichat-wrapped = pkgs.symlinkJoin {
name = "aichat";
paths = [
cfg.package
];
buildInputs = [pkgs.makeWrapper];
postBuild = ''
wrapProgram $out/bin/aichat ${api_key_files} ${api_key_cmds}
'';
};
in {
home.packages = mkIf cfg.enable [aichat-wrapped];
programs.fish.interactiveShellInit = mkIf cfg.enableFishIntegration fishIntegration;
programs.bash.initExtra = mkIf cfg.enableBashIntegration bashIntegration;
programs.zsh.initExtra = mkIf cfg.enableZshIntegration zshIntegration;
programs.nushell.extraConfig = mkIf cfg.enableNushellIntegration nuIntegration;
xdg.configFile."aichat/config.yaml".source = xdg.configFile."aichat/config.yaml".source =
yamlFormat.generate "config.yaml" cfg.settings; yamlFormat.generate "config.yaml" cfg.settings;
# xdg.configFile = mkIf cfg.enable {
# # "aichat/config.yaml".text = generators.toYAML {} cfg.settings;
# };
}; };
} }