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

feat: add on_complete_callback to gp.Prompt #145

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

1orZero
Copy link

@1orZero 1orZero commented Jul 6, 2024

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:

Use.case.demo.mov

@1orZero 1orZero changed the title feat: add on_complete_callback to query and Prompt feat: add on_complete_callback to gp.Prompt Jul 6, 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

Successfully merging this pull request may close these issues.

None yet

1 participant