From e9a01401bb51124dca832d0232e447120e9f4b88 Mon Sep 17 00:00:00 2001 From: Jan Andrle Date: Wed, 28 Feb 2024 11:22:33 +0100 Subject: [PATCH] =?UTF-8?q?:bug:=20Update=20`=C2=A7ai-commit.mjs`=20to=20w?= =?UTF-8?q?ork=20properly=20with=20new=20AI=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/§ai-commit.mjs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/bin/§ai-commit.mjs b/bin/§ai-commit.mjs index 6f02e0f..8cf2786 100755 --- a/bin/§ai-commit.mjs +++ b/bin/§ai-commit.mjs @@ -43,7 +43,7 @@ const conventional_desc= [ ]; $.api("", true) - .version("2024-02-22") + .version("2024-02-28") .describe([ "Utility to use ChatGPT to generate a commit message from COMMIT_EDITMSG file.", `Don't forget to set the token in ${token_file} file.`, @@ -67,8 +67,8 @@ $.api("", true) )()) .map(pipe( j=> j.choices[0].text.trim(), - t=> t.match(/\[[^\]]*\]/) ?? convertToJSONArray(t), - JSON.parse, + convertToArray, + j=> (console.log(j), j), format==="regular" ? i=> i : i=> gitmoji(i, format==="git3moji"), a=> a.join("\n") )) @@ -103,18 +103,25 @@ function diffToChunks(max_tokens){ return function(input){ .filter(chunk=> chunk.length < max_tokens); }); }; } -function convertToJSONArray(text){ - const arr= text.split("\n") - .filter(line=> line.trim().match(/^[1-3]\. /)) - .map(line=> line.slice(3)) - .map(line => line.startsWith('"') ? line : "\"" + ( "'`".slice("").includes(line[0]) ? line.slice(1, -1) : line ) + "\""); - return `[${arr.join(", ")}]`; +function convertToArray(text){ + // console.log(text); + return text.split("\n") + .map(line=> line.trim()) + .filter(line=> line.trim()) + .map(function(line){ + if(/^[0-9-].? /.test(line)) line= line.slice(line.indexOf(" ")+1); + if(/^["']/.test(line[0])) line= line.slice(1); + if(/["']$/.test(line[line.length-1])) line= line.slice(0, -1); + return line; + }) + ; } function questionChatGPT(format){ return function(diff){ const msg= [ [ "I would like to ask you to act like a git commit message writer.", - "I will enter a git diff, and your job is to convert it into a useful commit message and make 3 options as JSON array.", + "I will enter a git diff, and your job is to convert it into a useful commit message", + "Make 3 options, one option per line.", "Do not preface the commit with anything, use a concise, precise, present-tense, complete sentence.", "The length should be fewer than 50 characters if possible.", ].join(" ") //340chars★