55 "context"
66 "encoding/base64"
77 "encoding/json"
8+ "errors"
89 "io"
910 "net/http"
1011 "net/url"
@@ -40,18 +41,42 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
4041 return Explanations {}, err
4142 }
4243 }
43- req , err := http .NewRequestWithContext (ctx , http .MethodPost , u .String (), bytes .NewBuffer (requestBody ))
44+
45+ responseBody , err := d .submitRequest (ctx , u , requestBody )
4446 if err != nil {
45- logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error creating request" )
4647 return Explanations {}, err
4748 }
4849
50+ var response explainResponse
51+ var explains Explanations
52+ response .Status = completeStatus
53+ err = json .Unmarshal (responseBody , & response )
54+ if err != nil {
55+ logger .Err (err ).Str ("responseBody" , string (responseBody )).Msg ("error unmarshalling" )
56+ return Explanations {}, err
57+ }
58+
59+ explains = response .Explanation
60+
61+ return explains , nil
62+ }
63+
64+ func (d * DeepCodeLLMBindingImpl ) submitRequest (ctx context.Context , url * url.URL , requestBody []byte ) ([]byte , error ) {
65+ logger := d .logger .With ().Str ("method" , "submitRequest" ).Logger ()
66+ logger .Trace ().Str ("payload body: %s\n " , string (requestBody )).Msg ("Marshaled payload" )
67+
68+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , url .String (), bytes .NewBuffer (requestBody ))
69+ if err != nil {
70+ logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error creating request" )
71+ return nil , err
72+ }
73+
4974 d .addDefaultHeaders (req )
5075
5176 resp , err := d .httpClientFunc ().Do (req ) //nolint:bodyclose // this seems to be a false positive
5277 if err != nil {
5378 logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error getting response" )
54- return Explanations {} , err
79+ return nil , err
5580 }
5681 defer func (Body io.ReadCloser ) {
5782 bodyCloseErr := Body .Close ()
@@ -64,21 +89,11 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
6489 responseBody , err := io .ReadAll (resp .Body )
6590 if err != nil {
6691 logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error reading all response" )
67- return Explanations {} , err
92+ return nil , err
6893 }
6994 logger .Debug ().Str ("response body: %s\n " , string (responseBody )).Msg ("Got the response" )
70- var response explainResponse
71- var explains Explanations
72- response .Status = completeStatus
73- err = json .Unmarshal (responseBody , & response )
74- if err != nil {
75- logger .Err (err ).Str ("responseBody" , string (responseBody )).Msg ("error unmarshalling" )
76- return Explanations {}, err
77- }
78-
79- explains = response .Explanation
8095
81- return explains , nil
96+ return responseBody , nil
8297}
8398
8499func (d * DeepCodeLLMBindingImpl ) explainRequestBody (options * ExplainOptions ) ([]byte , error ) {
@@ -105,6 +120,111 @@ func (d *DeepCodeLLMBindingImpl) explainRequestBody(options *ExplainOptions) ([]
105120 return requestBody , marshalErr
106121}
107122
123+ var failed = AutofixStatus {Message : "FAILED" }
124+
125+ func (d * DeepCodeLLMBindingImpl ) runAutofix (ctx context.Context , requestId string , options AutofixOptions ) (AutofixResponse , AutofixStatus , error ) {
126+ span := d .instrumentor .StartSpan (ctx , "code.RunAutofix" )
127+ defer span .Finish ()
128+
129+ logger := d .logger .With ().Str ("method" , "code.RunAutofix" ).Str ("requestId" , requestId ).Logger ()
130+
131+ requestBody , err := d .autofixRequestBody (& options )
132+ if err != nil {
133+ logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error creating request body" )
134+ return AutofixResponse {}, failed , err
135+ }
136+
137+ logger .Info ().Msg ("Started obtaining autofix Response" )
138+ responseBody , err := d .submitRequest (ctx , options .Endpoint , requestBody )
139+ logger .Info ().Msg ("Finished obtaining autofix Response" )
140+
141+ if err != nil {
142+ logger .Err (err ).Str ("responseBody" , string (responseBody )).Msg ("error response from autofix" )
143+ return AutofixResponse {}, failed , err
144+ }
145+
146+ var response AutofixResponse
147+ err = json .Unmarshal (responseBody , & response )
148+ if err != nil {
149+ logger .Err (err ).Str ("responseBody" , string (responseBody )).Msg ("error unmarshalling" )
150+ return AutofixResponse {}, failed , err
151+ }
152+
153+ logger .Debug ().Msgf ("Status: %s" , response .Status )
154+
155+ if response .Status == failed .Message {
156+ errMsg := "autofix failed"
157+ logger .Error ().Str ("responseStatus" , response .Status ).Msg (errMsg )
158+ return response , failed , errors .New (errMsg )
159+ }
160+
161+ if response .Status == "" {
162+ errMsg := "unknown response status (empty)"
163+ logger .Error ().Str ("responseStatus" , response .Status ).Msg (errMsg )
164+ return response , failed , errors .New (errMsg )
165+ }
166+
167+ status := AutofixStatus {Message : response .Status }
168+ if response .Status != completeStatus {
169+ return response , status , nil
170+ }
171+
172+ return response , status , nil
173+ }
174+
175+ func (d * DeepCodeLLMBindingImpl ) autofixRequestBody (options * AutofixOptions ) ([]byte , error ) {
176+ request := AutofixRequest {
177+ Key : AutofixRequestKey {
178+ Type : "file" ,
179+ Hash : options .BundleHash ,
180+ FilePath : options .FilePath ,
181+ RuleId : options .RuleID ,
182+ LineNum : options .LineNum ,
183+ },
184+ AnalysisContext : options .CodeRequestContext ,
185+ IdeExtensionDetails : options .IdeExtensionDetails ,
186+ }
187+ if len (options .ShardKey ) > 0 {
188+ request .Key .Shard = options .ShardKey
189+ }
190+
191+ requestBody , err := json .Marshal (request )
192+ return requestBody , err
193+ }
194+
195+ func (d * DeepCodeLLMBindingImpl ) submitAutofixFeedback (ctx context.Context , requestId string , options AutofixFeedbackOptions ) error {
196+ span := d .instrumentor .StartSpan (ctx , "code.SubmitAutofixFeedback" )
197+ defer span .Finish ()
198+
199+ logger := d .logger .With ().Str ("method" , "code.SubmitAutofixFeedback" ).Str ("requestId" , requestId ).Logger ()
200+
201+ requestBody , err := d .autofixFeedbackRequestBody (& options )
202+ if err != nil {
203+ logger .Err (err ).Str ("requestBody" , string (requestBody )).Msg ("error creating request body" )
204+ return err
205+ }
206+
207+ logger .Info ().Msg ("Started obtaining autofix Response" )
208+ _ , err = d .submitRequest (ctx , options .Endpoint , requestBody )
209+ logger .Info ().Msg ("Finished obtaining autofix Response" )
210+
211+ return err
212+ }
213+
214+ func (d * DeepCodeLLMBindingImpl ) autofixFeedbackRequestBody (options * AutofixFeedbackOptions ) ([]byte , error ) {
215+ request := AutofixUserEvent {
216+ Channel : "IDE" ,
217+ EventType : options .Result ,
218+ EventDetails : AutofixEventDetails {FixId : options .FixID },
219+ AnalysisContext : options .CodeRequestContext ,
220+ IdeExtensionDetails : options .IdeExtensionDetails ,
221+ }
222+
223+ requestBody , err := json .Marshal (request )
224+
225+ return requestBody , err
226+ }
227+
108228func prepareDiffs (diffs []string ) []string {
109229 cleanedDiffs := make ([]string , 0 , len (diffs ))
110230 for _ , diff := range diffs {
0 commit comments