From 1ade6dd1d93d6409e493b93439cba7edc4ee2b80 Mon Sep 17 00:00:00 2001 From: Hassan El Mghari Date: Sun, 12 Feb 2023 00:15:46 -0500 Subject: [PATCH] Refactor aicommit.mjs to include verbose output and confirmation prompt for AI commit message --- aicommit.mjs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/aicommit.mjs b/aicommit.mjs index 9123de6..40ae8f4 100644 --- a/aicommit.mjs +++ b/aicommit.mjs @@ -1,10 +1,12 @@ #!/usr/bin/env zx +$.verbose = false; + +console.log(chalk.white("▲ ") + chalk.green("Welcome to AICommit!")); let { OPENAI_API_KEY } = await fs.readJson("./.env.json"); let diff = await quiet($`git diff --cached`); -let prompt = `Write one detailed commit message based on the following commit: ${diff}`; -echo(prompt); +let prompt = `Write one detailed commit message based on the following commit. Do not preface the commit with anything, write it right away: ${diff}`; const payload = { model: "text-davinci-003", @@ -18,6 +20,10 @@ const payload = { n: 1, }; +console.log( + chalk.white("▲ ") + chalk.black("Generating your AI commit message...") +); + const response = await fetch("https://api.openai.com/v1/completions", { headers: { "Content-Type": "application/json", @@ -28,4 +34,19 @@ const response = await fetch("https://api.openai.com/v1/completions", { }); const json = await response.json(); -echo(json.choices[0].text); +const aiCommit = json.choices[0].text; + +echo(aiCommit); + +let confirmationMessage = await question( + "\nWould you like to use this commit message? " + chalk.yellow("(Y/n) "), + { + choices: ["Y", "n"], + } +); + +$.verbose = true; + +if (confirmationMessage !== "n") { + await $`git commit -m ${aiCommit}`; +}