-
Notifications
You must be signed in to change notification settings - Fork 220
/
cli.go
129 lines (117 loc) · 3.62 KB
/
cli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ai
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-cli/utils/cliutils"
"github.com/jfrog/jfrog-client-go/http/httpclient"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
"io"
"net/http"
"strings"
)
type ApiCommand string
const (
cliAiApiPath = "https://cli-ai.jfrog.info/"
questionApi ApiCommand = "ask"
feedbackApi ApiCommand = "feedback"
)
type questionBody struct {
Question string `json:"question"`
}
type feedbackBody struct {
questionBody
LlmAnswer string `json:"llm_answer"`
IsAccurate bool `json:"is_accurate"`
ExpectedAnswer string `json:"expected_answer"`
}
func HowCmd(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() < 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
args := cliutils.ExtractCommand(c)
question := questionBody{Question: fmt.Sprintf("How %s", strings.Join(args, " "))}
llmAnswer, err := askQuestion(question)
if err != nil {
return err
}
log.Output("AI generated JFrog CLI command:")
err = coreutils.PrintTable("", "", coreutils.PrintTitle(llmAnswer), false)
if err != nil {
return err
}
feedback := feedbackBody{questionBody: question, LlmAnswer: llmAnswer}
feedback.getUserFeedback()
if err = sendFeedback(feedback); err != nil {
return err
}
log.Output("Thank you for your feedback!")
return nil
}
func (fb *feedbackBody) getUserFeedback() {
fb.IsAccurate = coreutils.AskYesNo(coreutils.PrintLink("Is the provided command accurate?"), true)
if !fb.IsAccurate {
ioutils.ScanFromConsole("Please provide the exact command you expected (Example: 'jf rt u ...')", &fb.ExpectedAnswer, "")
}
}
func askQuestion(question questionBody) (response string, err error) {
return sendRequestToCliAiServer(question, questionApi)
}
func sendFeedback(feedback feedbackBody) (err error) {
_, err = sendRequestToCliAiServer(feedback, feedbackApi)
return
}
func sendRequestToCliAiServer(content interface{}, apiCommand ApiCommand) (response string, err error) {
contentBytes, err := json.Marshal(content)
if errorutils.CheckError(err) != nil {
return
}
client, err := httpclient.ClientBuilder().Build()
if errorutils.CheckError(err) != nil {
return
}
req, err := http.NewRequest(http.MethodPost, cliAiApiPath+string(apiCommand), bytes.NewBuffer(contentBytes))
if errorutils.CheckError(err) != nil {
return
}
req.Header.Set("Content-Type", "application/json")
log.Debug(fmt.Sprintf("Sending HTTP %s request to: %s", req.Method, req.URL))
resp, err := client.GetClient().Do(req)
if errorutils.CheckError(err) != nil {
return
}
if resp == nil {
err = errorutils.CheckErrorf("received empty response from server")
return
}
if err = errorutils.CheckResponseStatus(resp, http.StatusOK); err != nil {
if resp.StatusCode == http.StatusInternalServerError {
err = errorutils.CheckErrorf("AI model Endpoint is not available.\n" + err.Error())
} else if resp.StatusCode == http.StatusNotFound {
err = errorutils.CheckErrorf("CLI-AI app server is no available. Note that the this command is supported while inside JFrog's internal network only.\n" + err.Error())
}
return
}
if apiCommand == questionApi {
defer func() {
if resp.Body != nil {
err = errors.Join(err, errorutils.CheckError(resp.Body.Close()))
}
}()
var body []byte
body, err = io.ReadAll(resp.Body)
if errorutils.CheckError(err) != nil {
return
}
response = string(body)
}
return
}