Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain Hook does not receive the selected text #141

Closed
alivault opened this issue Jun 8, 2024 · 8 comments
Closed

Explain Hook does not receive the selected text #141

alivault opened this issue Jun 8, 2024 · 8 comments

Comments

@alivault
Copy link

alivault commented Jun 8, 2024

I have copied the Explain hook exactly as the docs show into my config. But when I select some code and run the explain command, I get a pop up that says it didn't receive any selected text, only the filepath:
image
Here is my current config:

return {
  "robitx/gp.nvim",
  config = function()
    require("gp").setup({
      openai_api_key = os.getenv("OPENAI_API_KEY"),
      agents = {
        {
          name = "GPT-4o",
          chat = true,
          command = true,
          model = { model = "gpt-4o", temperature = 1.1, top_p = 1 },
          system_prompt = "You are a helpful assistant. Keep your answers concise. When providing code, put the code in triple backticks for markdown formatting. Use 'typescript' for tsx and ts code blocks, never 'typescriptreact'.",
        },
      },
      hooks = {
        -- explains the selected code
        Explain = function(gp, params)
          local template = "I have the following code from {{filename}}:\n\n"
            .. "```{{filetype}}\n{{selection}}\n```\n\n"
            .. "Please respond by explaining the code above."
          local agent = gp.get_chat_agent()
          gp.Prompt(params, gp.Target.popup, nil, agent.model, template, agent.system_prompt)
        end,

        -- opens new chat with the entire current buffer as a context
        BufferChatNew = function(gp, _)
          -- call GpChatNew command in range mode on whole buffer
          vim.api.nvim_command("%" .. gp.config.cmd_prefix .. "ChatNew")
        end,
      },
      chat_confirm_delete = false,
    })
  end,
}

Sidenote, the BufferChatNew hook works perfectly. But the Explain hook is not receiving the selected text.

@Robitx
Copy link
Owner

Robitx commented Aug 1, 2024

@alivault Hey, are you still seeing this issue in the latest version? I've just tried running :'<,'>GpExplain on some selection and don't see any problems.

@alivault
Copy link
Author

alivault commented Aug 1, 2024

@Robitx I just updated to the latest version but now I get this error:
Screenshot 2024-08-01 at 3 03 36 PM
This is my config:

return {
 "robitx/gp.nvim",
 config = function()
   local conf = {
     providers = {
       openai = {
         endpoint = "https://api.openai.com/v1/chat/completions",
         secret = os.getenv("OPENAI_API_KEY"),
       },
     },
     agents = {
       {
         name = "GPT-4o",
         chat = true,
         command = true,
         model = { model = "gpt-4o", temperature = 1.1, top_p = 1 },
         system_prompt = "You are a helpful assistant. Keep your answers concise. When providing code snippets, always leave comments for lines changed or added. Provide only the relevant parts of the code unless explicitly asked for the complete code. Format code in triple backticks. Use 'typescript' for tsx and ts code, never 'typescriptreact'.",
       },
     },
     hooks = {
       -- explains the selected code
       Explain = function(gp, params)
         local template = "I have the following code from {{filename}}:\n\n"
           .. "```{{filetype}}\n{{selection}}\n```\n\n"
           .. "Please respond by explaining the code above."
         local agent = gp.get_chat_agent()
         gp.Prompt(params, gp.Target.popup, agent, template)
       end,

       -- opens new chat with the entire current buffer as a context
       BufferChatNew = function(gp, _)
         -- call GpChatNew command in range mode on whole buffer
         vim.api.nvim_command("%" .. gp.config.cmd_prefix .. "ChatNew")
       end,
     },
     chat_confirm_delete = false,
   }
   require("gp").setup(conf)

   require("which-key").add({
     -- VISUAL mode mappings
     -- s, x, v modes are handled the same way by which_key
     {
       mode = { "v" },
       nowait = true,
       remap = false,
       { "<C-g><C-t>", ":<C-u>'<,'>GpChatNew tabnew<cr>", desc = "ChatNew tabnew" },
       { "<C-g><C-v>", ":<C-u>'<,'>GpChatNew vsplit<cr>", desc = "ChatNew vsplit" },
       { "<C-g><C-x>", ":<C-u>'<,'>GpChatNew split<cr>", desc = "ChatNew split" },
       { "<C-g>a", ":<C-u>'<,'>GpAppend<cr>", desc = "Visual Append (after)" },
       { "<C-g>b", ":<C-u>'<,'>GpPrepend<cr>", desc = "Visual Prepend (before)" },
       { "<C-g>c", ":<C-u>'<,'>GpChatNew<cr>", desc = "Visual Chat New" },
       { "<C-g>g", group = "generate into new .." },
       { "<C-g>ge", ":<C-u>'<,'>GpEnew<cr>", desc = "Visual GpEnew" },
       { "<C-g>gn", ":<C-u>'<,'>GpNew<cr>", desc = "Visual GpNew" },
       { "<C-g>gp", ":<C-u>'<,'>GpPopup<cr>", desc = "Visual Popup" },
       { "<C-g>gt", ":<C-u>'<,'>GpTabnew<cr>", desc = "Visual GpTabnew" },
       { "<C-g>gv", ":<C-u>'<,'>GpVnew<cr>", desc = "Visual GpVnew" },
       { "<C-g>i", ":<C-u>'<,'>GpImplement<cr>", desc = "Implement selection" },
       { "<C-g>n", "<cmd>GpNextAgent<cr>", desc = "Next Agent" },
       { "<C-g>p", ":<C-u>'<,'>GpChatPaste<cr>", desc = "Visual Chat Paste" },
       { "<C-g>r", ":<C-u>'<,'>GpRewrite<cr>", desc = "Visual Rewrite" },
       { "<C-g>s", "<cmd>GpStop<cr>", desc = "GpStop" },
       { "<C-g>t", ":<C-u>'<,'>GpChatToggle<cr>", desc = "Visual Toggle Chat" },
       { "<C-g>w", group = "Whisper" },
       { "<C-g>wa", ":<C-u>'<,'>GpWhisperAppend<cr>", desc = "Whisper Append" },
       { "<C-g>wb", ":<C-u>'<,'>GpWhisperPrepend<cr>", desc = "Whisper Prepend" },
       { "<C-g>we", ":<C-u>'<,'>GpWhisperEnew<cr>", desc = "Whisper Enew" },
       { "<C-g>wn", ":<C-u>'<,'>GpWhisperNew<cr>", desc = "Whisper New" },
       { "<C-g>wp", ":<C-u>'<,'>GpWhisperPopup<cr>", desc = "Whisper Popup" },
       { "<C-g>wr", ":<C-u>'<,'>GpWhisperRewrite<cr>", desc = "Whisper Rewrite" },
       { "<C-g>wt", ":<C-u>'<,'>GpWhisperTabnew<cr>", desc = "Whisper Tabnew" },
       { "<C-g>wv", ":<C-u>'<,'>GpWhisperVnew<cr>", desc = "Whisper Vnew" },
       { "<C-g>ww", ":<C-u>'<,'>GpWhisper<cr>", desc = "Whisper" },
       { "<C-g>x", ":<C-u>'<,'>GpContext<cr>", desc = "Visual GpContext" },
       { "<C-g>e", ":<cmd>'<,'>GpExplain<cr>", desc = "Explain Selection" },
     },

     -- NORMAL mode mappings
     {
       mode = { "n" },
       nowait = true,
       remap = false,
       { "<C-g>", group = "gp-nvim" },
       { "<C-g><C-t>", "<cmd>GpChatNew tabnew<cr>", desc = "New Chat tabnew" },
       { "<C-g><C-v>", "<cmd>GpChatNew vsplit<cr>", desc = "New Chat vsplit" },
       { "<C-g><C-x>", "<cmd>GpChatNew split<cr>", desc = "New Chat split" },
       { "<C-g>a", "<cmd>GpAppend<cr>", desc = "Append (after)" },
       { "<C-g>b", "<cmd>GpPrepend<cr>", desc = "Prepend (before)" },
       { "<C-g>c", "<cmd>GpChatNew<cr>", desc = "New Chat" },
       { "<C-g>f", "<cmd>GpChatFinder<cr>", desc = "Chat Finder" },
       { "<C-g>g", group = "generate into new .." },
       { "<C-g>ge", "<cmd>GpEnew<cr>", desc = "GpEnew" },
       { "<C-g>gn", "<cmd>GpNew<cr>", desc = "GpNew" },
       { "<C-g>gp", "<cmd>GpPopup<cr>", desc = "Popup" },
       { "<C-g>gt", "<cmd>GpTabnew<cr>", desc = "GpTabnew" },
       { "<C-g>gv", "<cmd>GpVnew<cr>", desc = "GpVnew" },
       { "<C-g>n", "<cmd>GpNextAgent<cr>", desc = "Next Agent" },
       { "<C-g>r", "<cmd>GpRewrite<cr>", desc = "Inline Rewrite" },
       { "<C-g>s", "<cmd>GpStop<cr>", desc = "GpStop" },
       { "<C-g>t", "<cmd>GpChatToggle<cr>", desc = "Toggle Chat" },
       { "<C-g>w", group = "Whisper" },
       { "<C-g>wa", "<cmd>GpWhisperAppend<cr>", desc = "Whisper Append (after)" },
       { "<C-g>wb", "<cmd>GpWhisperPrepend<cr>", desc = "Whisper Prepend (before)" },
       { "<C-g>we", "<cmd>GpWhisperEnew<cr>", desc = "Whisper Enew" },
       { "<C-g>wn", "<cmd>GpWhisperNew<cr>", desc = "Whisper New" },
       { "<C-g>wp", "<cmd>GpWhisperPopup<cr>", desc = "Whisper Popup" },
       { "<C-g>wr", "<cmd>GpWhisperRewrite<cr>", desc = "Whisper Inline Rewrite" },
       { "<C-g>wt", "<cmd>GpWhisperTabnew<cr>", desc = "Whisper Tabnew" },
       { "<C-g>wv", "<cmd>GpWhisperVnew<cr>", desc = "Whisper Vnew" },
       { "<C-g>ww", "<cmd>GpWhisper<cr>", desc = "Whisper" },
       { "<C-g>x", "<cmd>GpContext<cr>", desc = "Toggle GpContext" },
       { "<C-g>gb", "<cmd>GpBufferChatNew<cr>", desc = "Buffer Chat New" },
     },

     -- INSERT mode mappings
     {
       mode = { "i" },
       nowait = true,
       remap = false,
       { "<C-g><C-t>", "<cmd>GpChatNew tabnew<cr>", desc = "New Chat tabnew" },
       { "<C-g><C-v>", "<cmd>GpChatNew vsplit<cr>", desc = "New Chat vsplit" },
       { "<C-g><C-x>", "<cmd>GpChatNew split<cr>", desc = "New Chat split" },
       { "<C-g>a", "<cmd>GpAppend<cr>", desc = "Append (after)" },
       { "<C-g>b", "<cmd>GpPrepend<cr>", desc = "Prepend (before)" },
       { "<C-g>c", "<cmd>GpChatNew<cr>", desc = "New Chat" },
       { "<C-g>f", "<cmd>GpChatFinder<cr>", desc = "Chat Finder" },
       { "<C-g>g", group = "generate into new .." },
       { "<C-g>ge", "<cmd>GpEnew<cr>", desc = "GpEnew" },
       { "<C-g>gn", "<cmd>GpNew<cr>", desc = "GpNew" },
       { "<C-g>gp", "<cmd>GpPopup<cr>", desc = "Popup" },
       { "<C-g>gt", "<cmd>GpTabnew<cr>", desc = "GpTabnew" },
       { "<C-g>gv", "<cmd>GpVnew<cr>", desc = "GpVnew" },
       { "<C-g>n", "<cmd>GpNextAgent<cr>", desc = "Next Agent" },
       { "<C-g>r", "<cmd>GpRewrite<cr>", desc = "Inline Rewrite" },
       { "<C-g>s", "<cmd>GpStop<cr>", desc = "GpStop" },
       { "<C-g>t", "<cmd>GpChatToggle<cr>", desc = "Toggle Chat" },
       { "<C-g>w", group = "Whisper" },
       { "<C-g>wa", "<cmd>GpWhisperAppend<cr>", desc = "Whisper Append (after)" },
       { "<C-g>wb", "<cmd>GpWhisperPrepend<cr>", desc = "Whisper Prepend (before)" },
       { "<C-g>we", "<cmd>GpWhisperEnew<cr>", desc = "Whisper Enew" },
       { "<C-g>wn", "<cmd>GpWhisperNew<cr>", desc = "Whisper New" },
       { "<C-g>wp", "<cmd>GpWhisperPopup<cr>", desc = "Whisper Popup" },
       { "<C-g>wr", "<cmd>GpWhisperRewrite<cr>", desc = "Whisper Inline Rewrite" },
       { "<C-g>wt", "<cmd>GpWhisperTabnew<cr>", desc = "Whisper Tabnew" },
       { "<C-g>wv", "<cmd>GpWhisperVnew<cr>", desc = "Whisper Vnew" },
       { "<C-g>ww", "<cmd>GpWhisper<cr>", desc = "Whisper" },
       { "<C-g>x", "<cmd>GpContext<cr>", desc = "Toggle GpContext" },
     },
   })
 end,
}

@Robitx
Copy link
Owner

Robitx commented Aug 1, 2024

@alivault thanks for the report, I've commited a line from current experiment by mistake. It's fixed in 3.6.1.

@alivault
Copy link
Author

alivault commented Aug 1, 2024

@Robitx Nice! I just updated and the error is gone. Also the explain hook is working for me now, but the pop up is being covered by the noice.nvim pop up:
Screenshot 2024-08-01 at 3 23 46 PM

@Robitx
Copy link
Owner

Robitx commented Aug 1, 2024

@alivault and thanks again, zindex in neovim is kinda mess, I've made it configurable in 3.6.0 and lowered default to 49. Latest 3.6.2 uses again the default value of 50.

@alivault
Copy link
Author

alivault commented Aug 1, 2024

@Robitx Looks like noice is set to 200 zindex:
Screenshot 2024-08-01 at 3 55 33 PM

I had to set zindex to 201 in my gp config and it is above noice now. Maybe set the default to 201? Noice is a popular plugin so might be a common issue.

The other issue is that the explanation pop up does not take focus. The focus stays in the command prompt. So when I try to move my cursor in the pop up, it is actually typing into the command prompt. See video:

Screen.Recording.2024-08-01.at.4.05.20.PM.mov

Does the command prompt need to take focus after running the hook?

@Robitx
Copy link
Owner

Robitx commented Aug 2, 2024

@alivault oh, I see where is the problem, the video example prompt me to check over your shortcuts. 🙂

Please try to change this:

       { "<C-g>e", ":<cmd>'<,'>GpExplain<cr>", desc = "Explain Selection" },

to

       { "<C-g>e", ":<C-u>'<,'>GpExplain<cr>", desc = "Explain Selection" },

<cmd> is "a better version of : " - having them together is kinda overdoing it and explains why is the Noice command window there stealing the focus from the popup.

This also makes the zindex 201 unnecessary (you might want to be able to call a command in a popup windows so the Noice prompt should be visible).

@alivault
Copy link
Author

alivault commented Aug 2, 2024

You're the man. Thanks for catching that. It works perfectly now.

@alivault alivault closed this as completed Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants