8000 feat: updated AI Explain API interface for new API definition [IDE-954] by DariusZdroba · Pull Request #78 · snyk/code-client-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: updated AI Explain API interface for new API definition [IDE-954] #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 28, 2025
Merged
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
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/snyk/code-client-go

go 1.23.0

toolchain go1.23.6
go 1.23.6

require (
github.com/go-git/go-git/v5 v5.14.0
Expand Down
32 changes: 17 additions & 15 deletions llm/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@ import (
"bytes"
"context"
"encoding/json"
"github.com/snyk/code-client-go/observability"
"io"
"net/http"
"net/url"

"github.com/snyk/code-client-go/observability"
)

const (
var (
completeStatus = "COMPLETE"
failedToObtainRequestIdString = "Failed to obtain request id. "
defaultEndpointURL = "http://localhost:10000/explain"
)

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

requestId, err := observability.GetTraceId(ctx)
logger := d.logger.With().Str("method", "code.RunExplain").Str("requestId", requestId).Logger()
if err != nil {
logger.Err(err).Msg(failedToObtainRequestIdString + err.Error())
return explainResponse{}, err
return Explanations{}, err
}

logger.Debug().Msg("API: Retrieving explain for bundle")
Expand All @@ -34,7 +33,7 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
requestBody, err := d.explainRequestBody(&options)
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body")
return explainResponse{}, err
return Explanations{}, err
}
logger.Debug().Str("payload body: %s\n", string(requestBody)).Msg("Marshaled payload")

Expand All @@ -43,21 +42,21 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
u, err = url.Parse(defaultEndpointURL)
if err != nil {
logger.Err(err).Send()
return explainResponse{}, err
return Explanations{}, err
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), bytes.NewBuffer(requestBody))
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request")
return explainResponse{}, err
return Explanations{}, err
}

d.addDefaultHeaders(req, requestId)

resp, err := d.httpClientFunc().Do(req) //nolint:bodyclose // this seems to be a false positive
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error getting response")
return explainResponse{}, err
return Explanations{}, err
}
defer func(Body io.ReadCloser) {
bodyCloseErr := Body.Close()
Expand All @@ -70,25 +69,28 @@ func (d *DeepCodeLLMBindingImpl) runExplain(ctx context.Context, options Explain
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
logger.Err(err).Str("requestBody", string(requestBody)).Msg("error reading all response")
return explainResponse{}, err
return Explanations{}, err
}
logger.Debug().Str("response body: %s\n", string(responseBody)).Msg("Got the response")

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 explainResponse{}, err
return E 8000 xplanations{}, err
}
return response, nil

explains = response.Explanation

return explains, nil
}

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

var request explainRequest
if options.Diff == "" {
if len(options.Diffs) == 0 {
request.VulnExplanation = &explainVulnerabilityRequest{
RuleId: options.RuleKey,
Derivation: options.Derivation,
Expand All @@ -99,7 +101,7 @@ func (d *DeepCodeLLMBindingImpl) explainRequestBody(options *ExplainOptions) ([]
} else {
request.FixExplanation = &explainFixRequest{
RuleId: options.RuleKey,
Diff: options.Diff,
Diffs: options.Diffs,
ExplanationLength: SHORT,
}
logger.Debug().Msg("payload for FixExplanation")
Expand Down
22 changes: 8 additions & 14 deletions llm/api_client_test.go