-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit: Add AI-powered commit message generation
Go commit message: ``` Add AI-powered commit message generation This commit adds AI-powered commit message generation using the OneAPI server. The commit message is generated using the OneAPI token and the AI_PROVIDER environment variable. Features: - AI-powered commit message generation - Supports OneAPI server Bugs fixed: - None Refs: - OneAPI: https://github.com/songquanpeng/one-api ```
- Loading branch information
1 parent
d4969c9
commit 6f43412
Showing
7 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"syscall" | ||
|
||
fakeruntime "github.com/linuxsuren/go-fake-runtime" | ||
"github.com/linuxsuren/gogit/pkg/oneapi" | ||
"github.com/spf13/cobra" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type commitOption struct { | ||
provider string | ||
token string | ||
flags []string | ||
runtime fakeruntime.Execer | ||
} | ||
|
||
func newCommitCmd() *cobra.Command { | ||
opt := &commitOption{ | ||
runtime: fakeruntime.NewDefaultExecer(), | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "commit", | ||
RunE: opt.runE, | ||
PreRunE: opt.preRunE, | ||
Short: "Commit the current changes with AI", | ||
Long: `Commit the current changes with AI. | ||
The AI provider is defined by the environment variable AI_PROVIDER, | ||
and the token is defined by the environment variable ONEAPI_TOKEN.`, | ||
} | ||
cmd.Flags().StringSliceVarP(&opt.flags, "flag", "", []string{}, "The flags of the git commit command") | ||
return cmd | ||
} | ||
|
||
func (o *commitOption) preRunE(cmd *cobra.Command, args []string) (err error) { | ||
o.provider = os.Getenv("AI_PROVIDER") | ||
if o.provider == "" { | ||
err = fmt.Errorf("AI_PROVIDER is not set") | ||
} | ||
o.token = os.Getenv("ONEAPI_TOKEN") | ||
if o.token == "" { | ||
err = errors.Join(err, fmt.Errorf("ONEAPI_TOKEN is not set")) | ||
Check failure on line 50 in cmd/msg.go GitHub Actions / Build
|
||
} | ||
return | ||
} | ||
|
||
func (o *commitOption) runE(cmd *cobra.Command, args []string) (err error) { | ||
var gitdiff string | ||
gitdiff, err = o.getGitDiff() | ||
|
||
payload := oneapi.NewChatPayload(fmt.Sprintf("Please write a git commit message for the following git diff:\n%s", gitdiff), "chatglm_std") | ||
|
||
var body []byte | ||
if body, err = json.Marshal(payload); err != nil { | ||
return | ||
} | ||
|
||
var req *http.Request | ||
req, err = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/chat/completions", o.provider), io.NopCloser(bytes.NewReader(body))) | ||
if err != nil { | ||
return | ||
} | ||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", o.token)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
var resp *http.Response | ||
resp, err = http.DefaultClient.Do(req) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if resp.StatusCode == http.StatusOK { | ||
// read the body and parse to oenapi.ChatResponse | ||
var chatResp oneapi.ChatResponse | ||
if err = json.NewDecoder(resp.Body).Decode(&chatResp); err != nil { | ||
return | ||
} | ||
|
||
var tempF *os.File | ||
if tempF, err = os.CreateTemp(os.TempDir(), "msg"); err != nil { | ||
return | ||
} | ||
|
||
content := chatResp.Choices[0].Message.Content | ||
// convert \n to new line | ||
content = strings.ReplaceAll(content, "\\n", "\n") | ||
if _, err = io.WriteString(tempF, content); err != nil { | ||
return | ||
} | ||
if err = tempF.Close(); err != nil { | ||
return | ||
} | ||
|
||
cmd.Println("start to commit with", tempF.Name()) | ||
|
||
var gitExe string | ||
if gitExe, err = o.runtime.LookPath("git"); err != nil { | ||
return | ||
} | ||
|
||
if err = o.runtime.RunCommand(gitExe, "add", "."); err != nil { | ||
return | ||
} | ||
|
||
opts := []string{"git", "commit", "--edit", "--file", tempF.Name()} | ||
for _, flag := range o.flags { | ||
opts = append(opts, flag) | ||
} | ||
|
||
err = syscall.Exec(gitExe, opts, append(os.Environ(), "GIT_EDITOR=vim")) | ||
} | ||
return | ||
} | ||
|
||
func (o *commitOption) getGitDiff() (diff string, err error) { | ||
// run command git diff and get the output | ||
diff, err = o.runtime.RunCommandAndReturn("git", ".", "diff") | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package oneapi | ||
|
||
type ChatMessage struct { | ||
Content string `json:"content"` | ||
Role string `json:"role"` | ||
} | ||
|
||
type ChatPayload struct { | ||
Messages []ChatMessage `json:"messages"` | ||
Model string `json:"model"` | ||
} | ||
|
||
type ChatResponse struct { | ||
Created int64 `json:"created"` | ||
ID string `json:"id"` | ||
Object string `json:"object"` | ||
Usage map[string]int `json:"usage"` | ||
Choices []ChatResponseChoice `json:"choices"` | ||
} | ||
|
||
type ChatResponseChoice struct { | ||
FinishReason string `json:"finish_reason"` | ||
Index int `json:"index"` | ||
Message ChatMessage `json:"message"` | ||
} | ||
|
||
func NewChatPayload(message string, model string) ChatPayload { | ||
return ChatPayload{ | ||
Messages: []ChatMessage{{Content: message, Role: "user"}}, | ||
Model: model, | ||
} | ||
} |