Added dependency on babashka.fs and babashka.cli.
Introduced functions to find git repository and read/write commit message. Modified response processing to append to COMMIT_EDITMSG and open in editor.
This commit is contained in:
parent
5fb26e6220
commit
4e9a32f501
19
aicommit.clj
Normal file → Executable file
19
aicommit.clj
Normal file → Executable file
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
(require '[babashka.curl :as curl]
|
(require '[babashka.curl :as curl]
|
||||||
'[babashka.process :as p]
|
'[babashka.process :as p]
|
||||||
'[cheshire.core :as json])
|
'[cheshire.core :as json]
|
||||||
|
'[babashka.fs :as fs]
|
||||||
|
'[babashka.cli :as cli])
|
||||||
|
|
||||||
(def url "https://api.groq.com/openai/v1/chat/completions")
|
(def url "https://api.groq.com/openai/v1/chat/completions")
|
||||||
|
|
||||||
@ -16,6 +18,17 @@ Only reply with the commit message and nothing else.
|
|||||||
|
|
||||||
(def diff (:out (p/check (p/sh "git" "diff" "--cached"))))
|
(def diff (:out (p/check (p/sh "git" "diff" "--cached"))))
|
||||||
|
|
||||||
|
(defn find-git
|
||||||
|
[dir]
|
||||||
|
(loop [d dir]
|
||||||
|
(if (fs/directory? (fs/path d ".git"))
|
||||||
|
(fs/path d ".git")
|
||||||
|
(recur (fs/parent d)))))
|
||||||
|
|
||||||
|
(def commit-message-path (fs/unixify (fs/path (find-git (fs/cwd)) "COMMIT_EDITMSG")))
|
||||||
|
|
||||||
|
(def commit-message (slurp commit-message-path))
|
||||||
|
|
||||||
(def message (json/generate-string
|
(def message (json/generate-string
|
||||||
{:messages [{:role "system" :content system-msg}
|
{:messages [{:role "system" :content system-msg}
|
||||||
{:role "user" :content diff}]
|
{:role "user" :content diff}]
|
||||||
@ -34,4 +47,6 @@ Only reply with the commit message and nothing else.
|
|||||||
|
|
||||||
(def content (get-in (:choices (json/parse-string (:body response) true)) [0 :message :content]))
|
(def content (get-in (:choices (json/parse-string (:body response) true)) [0 :message :content]))
|
||||||
|
|
||||||
(println content)
|
(spit commit-message-path (str content "\n" commit-message))
|
||||||
|
|
||||||
|
(p/shell (System/getenv "EDITOR") (first *command-line-args*))
|
||||||
|
Loading…
Reference in New Issue
Block a user