Robitx / gp.nvim

Gp.nvim (GPT prompt) Neovim AI plugin: ChatGPT sessions & Instructable text/code operations & Speech to text [OpenAI, Ollama, Anthropic, ..]
MIT License
789 stars 67 forks source link

feat: add on_complete_callback to gp.Prompt #145

Closed 1orZero closed 2 months ago

1orZero commented 3 months ago

I am using the power of gp.nvim to generate a Git commit message after the response is complete.

To do this, I added the on_complete_callback argument to the M.Prompt and M.query functions.

I am a new Neovim user and very new to Lua script. This is my 1st PR. Feedback is welcome.

Below is an example of how I setup the git commit function.

hooks = {
    GitCommitStaged = function(gp, params)
      local system_prompt =
        "You are a commit title generator. You use the GPT-4 version of OpenAI's GPT models. You must only provide the commit title. Rules to follows: Write commit title for the change with commitizen convention. Make sure the title has maximum 50 characters. You must not provide additional explination/description other than the git commit title."
      local prompt =
        "Below is the diff of the changes. Please write a commit title for the change. Do not provide any additional information other than the git commit title. Write the commit title."

      local cmd = "git diff --no-color --no-ext-diff --staged"
      local handle = io.popen(cmd)
      if not handle then
        return nil
      end

      local diff = handle:read("*a")
      handle:close()

      if not diff or diff == "" then
        return nil
      end
      local template = prompt .. "\n\n" .. diff

      local agent = gp.get_command_agent()
      gp.info("Generating commit message for staged changes with agent: " .. agent.name)

      local function commit_callback(response)
        vim.notify(response, vim.log.levels.INFO, {
          title = "Git Commit Staged",
          timeout = 5000,
        })
        vim.fn.setreg("+", response)
        vim.notify("Copied commit message to clipboard")
        -- Open up Lazygit and its commit view
        LazyVim.lazygit({ cwd = LazyVim.root.git() })

        -- Wait for 500ms to ensure Lazygit is open
        vim.defer_fn(function()
          -- Run c to open the commit view
          vim.api.nvim_input("c")
          -- Wait another 500ms to ensure the commit view is open
          vim.defer_fn(function()
            -- Use vim.fn.system() to execute a shell command that pastes the commit message
            vim.fn.system('echo -n "' .. vim.fn.getreg("+") .. '" | pbcopy')
            vim.fn.system('osascript -e \'tell application "System Events" to keystroke "v" using command down\'')
          end, 100)
        end, 100)
      end
      gp.Prompt(params, gp.Target.popup, nil, agent.model, template, system_prompt, nil, commit_callback)
    end,
  }

Demo:

https://github.com/Robitx/gp.nvim/assets/53516684/9aeebdad-5317-48be-8119-01217fb6971e

Robitx commented 2 months ago

@1orZero Hey, sorry for keeping you waiting. Yes, the callback for the Prompt is a good and useful idea.

I've recently made some changes to the signature of M.Prompt so the PR requires some adjustments. Can't push changes to your branch so I made a new PR #157 and pull your commits into it.

1orZero commented 2 months ago

@1orZero Hey, sorry for keeping you waiting. Yes, the callback for the Prompt is a good and useful idea.

I've recently made some changes to the signature of M.Prompt so the PR requires some adjustments. Can't push changes to your branch so I made a new PR #157 and pull your commits into it.

All good man! Thanks for the merge!