Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 1 addition & 93 deletions llm/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,17 @@ package llm

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"

http2 "github.com/snyk/code-client-go/http"
)

var (
completeStatus = "COMPLETE"
defaultEndpointURL = "http://localhost:10000/explain"
)

func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options ExplainOptions) (Explanations, error) {
span := d.instrumentor.StartSpan(ctx, "code.RunExplain")
defer span.Finish()

logger := d.logger.With().Str("method", "code.RunExplain").Logger()

logger.Debug().Msg("API: Retrieving explain for bundle")
defer logger.Debug().Msg("API: Retrieving explain done")

requestBody, err := d.explainRequestBody(&options)
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body")
return Explanations{}, err
}
logger.Debug().Str("payload body: %s\n", string(requestBody)).Msg("Marshaled payload")

u := options.Endpoint
if u == nil {
u, err = url.Parse(defaultEndpointURL)
if err != nil {
logger.Err(err).Send()
return Explanations{}, err
}
}

responseBody, err := d.submitRequest(span.Context(), u, requestBody, "", false)
if err != nil {
return Explanations{}, err
}

var response explainResponse
var explains Explanations
response.Status = completeStatus
err = json.Unmarshal(responseBody, &response)
if err != nil {
logger.Err(err).Str("responseBody", string(responseBody)).Msg("error unmarshalling")
return Explanations{}, err
}

explains = response.Explanation

return explains, nil
}
var completeStatus = "COMPLETE"

func (d *DeepCodeLLMBindingImpl) submitRequest(ctx context.Context, url *url.URL, requestBody []byte, orgId string, needsEncoding bool) ([]byte, error) {
logger := d.logger.With().Str("method", "submitRequest").Logger()
Expand Down Expand Up @@ -107,30 +58,6 @@ func (d *DeepCodeLLMBindingImpl) submitRequest(ctx context.Context, url *url.URL
return responseBody, nil
}

func (d *DeepCodeLLMBindingImpl) explainRequestBody(options *ExplainOptions) ([]byte, error) {
logger := d.logger.With().Str("method", "code.explainRequestBody").Logger()

var requestBody []byte
var marshalErr error
if len(options.Diffs) == 0 {
requestBody, marshalErr = json.Marshal(explainVulnerabilityRequest{
RuleId: options.RuleKey,
Derivation: options.Derivation,
RuleMessage: options.RuleMessage,
ExplanationLength: SHORT,
})
logger.Debug().Msg("payload for VulnExplanation")
} else {
requestBody, marshalErr = json.Marshal(explainFixRequest{
RuleId: options.RuleKey,
Diffs: prepareDiffs(options.Diffs),
ExplanationLength: SHORT,
})
logger.Debug().Msg("payload for FixExplanation")
}
return requestBody, marshalErr
}

var failed = AutofixStatus{Message: "FAILED"}

func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, options AutofixOptions) (AutofixResponse, AutofixStatus, error) {
Expand Down Expand Up @@ -247,22 +174,3 @@ func (d *DeepCodeLLMBindingImpl) autofixFeedbackRequestBody(options *AutofixFeed

return requestBody, err
}

func prepareDiffs(diffs []string) []string {
cleanedDiffs := make([]string, 0, len(diffs))
for _, diff := range diffs {
diffLines := strings.Split(diff, "\n")
cleanedLines := ""
for _, line := range diffLines {
if !strings.HasPrefix(line, "---") && !strings.HasPrefix(line, "+++") {
cleanedLines += line + "\n"
}
}
cleanedDiffs = append(cleanedDiffs, cleanedLines)
}
var encodedDiffs []string
for _, diff := range cleanedDiffs {
encodedDiffs = append(encodedDiffs, base64.StdEncoding.EncodeToString([]byte(diff)))
}
return encodedDiffs
}
Loading
Loading