magicalne / nvim.ai

Inspired by Zed AI, it allows you to chat with your buffers, insert code with an inline assistant, and leverage various LLM providers for context-aware AI assistance.
Apache License 2.0
102 stars 2 forks source link

how i can add it with lazy vim #11

Open d7manDev opened 2 weeks ago

d7manDev commented 2 weeks ago

hi really thankful for your contribution i just would like to know how to add the plugin with lazy.vim

magicalne commented 2 weeks ago

Thanks for taking time with this project. I just tried with config below:

-- Setup lazy.nvim
require("lazy").setup({
  spec = {
    -- add your plugins here
    {
      "hrsh7th/nvim-cmp",
      event = "InsertEnter",
      dependencies = {
        "hrsh7th/cmp-nvim-lsp",
        "hrsh7th/cmp-buffer",
      },
      config = function()
        local cmp = require'cmp'
        cmp.setup({
          snippet = {
            -- REQUIRED by nvim-cmp. get rid of it once we can
            expand = function(args)
              vim.fn["vsnip#anonymous"](args.body)
            end,
          },
          mapping = {
          sources = cmp.config.sources({
            { name = 'nvimai_cmp_source' }, -- This is optional but recommended

          }
        })

      end,
    },
    {"nvim-treesitter/nvim-treesitter", build = ":TSUpdate"}, -- nvim.ai depends on treesitter
    {
      "magicalne/nvim.ai",
      dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-treesitter/nvim-treesitter",
      },
      opts = {
        provider = "anthropic", -- You can configure your provider, model or keymaps here.
      }
    },

  },
  -- ...
})
d7manDev commented 2 weeks ago

could you please provide commands that the tool used for keymaps cuz i'm using nvchad but it seems that i have to add the command with the plugin as well as i need to reset the keymap

magicalne commented 2 weeks ago

Sure thing. You can find the default keymaps in here. You can reset the keymaps in the config.

-- Keymaps
  keymaps = {
    toggle = "<leader>c",       -- Toggle chat dialog
    send = "<CR>",               -- Send message in normal mode
    close = "q",                 -- Close chat dialog
    clear = "<C-l>",             -- Clear chat history
    inline_assist = "<leader>i", -- Run InlineAssist command with prompt
    accept_code = "<leader>ia",
    reject_code = "<leader>ij",
  },
d7manDev commented 2 weeks ago

this command need to be add dose't work form me with out


{
    cmd = "NvimAIToggleChatDialog",
    callback = function()
      ChatDialog.toggle()
    end,
    opts = {
      desc = "Insert code or rewrite a section",
    },
  },
  {
    cmd = "NvimAIInlineAssist",
    callback = function(opts)
      Assistant.inline(opts.args)
    end,
    opts = {
      desc = "Insert code or rewrite a section",
      range = true,
      nargs = "*",
    },
  },
  {
    cmd = "NvimAIAcceptCode",
    callback = function(opts)
      Assistant.accept_code()
    end,
    opts = {
      desc = "Accept generated code",
      range = true,
      nargs = "*",
    },
  },
  {
    cmd = "NvimAIRejectCode",
    callback = function(opts)
      Assistant.reject_code()
    end,
    opts = {
      desc = "Reject generated code",
      range = true,
      nargs = "*",
    },
  }
}```
magicalne commented 2 weeks ago

Could you please elaborate your question? Are you saying those commands are not working?

d7manDev commented 2 weeks ago

yah when enter them it says (the commnd ) not an editor command, so it's must add when add the plugin in lazy file E.x : by the way i'm using nvim with NvChad


 {"nvim-treesitter/nvim-treesitter", build = ":TSUpdate"}, -- nvim.ai depends on treesitter
    {
      "magicalne/nvim.ai",
      dependencies = {
        "nvim-lua/plenary.nvim",
        "nvim-treesitter/nvim-treesitter",
      },
      opts = {
        provider = "anthropic", -- You can configure your provider, model or keymaps here.
      }
cmd = {"NvimAIToggleChatDialog", "NvimAIAcceptCode"},
}
magicalne commented 2 weeks ago

This is tricky. :disappointed: I'm unable to reproduce this issue, but I'm glad you were able to find a workaround. I'll take another look at this later.

codr1 commented 6 days ago

Hey. So I am not folling d7manDev, but I was having a similar issue, but I resolved it as I was typing this comment. (its always skills issues...)

In my case - Lazy installed the plugin, but it wasn't loading.
nvim.ai is in the Not Loaded section image

There are two things you can do. Manually load it like so: Lazy load nvim.ai

Add lazy=false to the config which will load it as soon as we start nvim up. As a special bonus to my NVChad homies, I have added a couple of keymaps that avoid a conflict with the NVChad keymaps. ac - AI Chat, and ai - AI Insert.

Like so.

    {
        "magicalne/nvim.ai",
        lazy = false,
        dependencies = {
            "nvim-lua/plenary.nvim",
            "nvim-treesitter/nvim-treesitter",
        },
        opts = {
            provider = "anthropic", -- You can configure your provider, model or keymaps here.
            keymaps = {
                toggle = "<leader>ac", -- Toggle chat dialog
                inline_assist = "<leader>ai", -- Run InlineAssist command with prompt
            },
        },
    },

This did the trick for now.

codr1 commented 6 days ago

... So this is all good. And I have an environmental variable like this echo $ANTHROPIC_API_KEY ...which I am using in other similar plugins. But when I try to send a message to Anthropic, I get nothing.
You can see my config above. What am I missing? This is what I see image

codr1 commented 6 days ago

... I spent a really long time trying to troubleshoot. Where can i see the logs? I fee like I am not communicating with the model somehow.

This is latest config. What am I doing wrong?

    -- nvim.ai
    {
        "magicalne/nvim.ai",
        lazy = false,
        dependencies = {
            "nvim-lua/plenary.nvim",
            "nvim-treesitter/nvim-treesitter",
        },
        config = function()
            local api_key = os.getenv "ANTHROPIC_API_KEY"
            local opts = {
                provider = "anthropic", -- You can configure your provider, model or keymaps here.
                anthropic = {
                    max_tokens = 100000,
                },
                ANTHROPIC_API_KEY = api_key,
                keymaps = {
                    toggle = "<leader>ac", -- Toggle chat dialog
                    inline_assist = "<leader>ai", -- Run InlineAssist command with prompt
                },
            }
            require("ai").setup(opts)
        end,
    },
magicalne commented 6 days ago

Sorry for any inconvenience caused. I've added debug logs in the HTTP response callback. You can enable debugging by setting:

local opts = {
  debug = true,
  provider = "anthropic",
  -- the rest...
}

Please send some message in the sidebar and see if there is any log. To check out these logs, use :message.

I didn't test with nvchad. Not sure about what's going on.

Actually, I recommend you guys try with codecompanion. As the slash command pr got merged last week. This project is basically a subset of codecompanion. Codecompanion supports all the features that this project does.