diff --git a/llm/api_client.go b/llm/api_client.go index 5ce396b..9b6f3dc 100644 --- a/llm/api_client.go +++ b/llm/api_client.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "encoding/json" "errors" + "fmt" "io" "net/http" "net/url" @@ -128,6 +129,12 @@ func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, requestId strin logger := d.logger.With().Str("method", "code.RunAutofix").Str("requestId", requestId).Logger() + endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/suggestions", options.Host)) + if err != nil { + logger.Err(err).Str("host", options.Host).Msg("error creating endpoint URL") + return AutofixResponse{}, failed, err + } + requestBody, err := d.autofixRequestBody(&options) if err != nil { logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body") @@ -135,7 +142,7 @@ func (d *DeepCodeLLMBindingImpl) runAutofix(ctx context.Context, requestId strin } logger.Info().Msg("Started obtaining autofix Response") - responseBody, err := d.submitRequest(ctx, options.Endpoint, requestBody) + responseBody, err := d.submitRequest(ctx, endpoint, requestBody) logger.Info().Msg("Finished obtaining autofix Response") if err != nil { @@ -198,6 +205,12 @@ func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, requ logger := d.logger.With().Str("method", "code.SubmitAutofixFeedback").Str("requestId", requestId).Logger() + endpoint, err := url.Parse(fmt.Sprintf("%s/autofix/event", options.Host)) + if err != nil { + logger.Err(err).Str("host", options.Host).Msg("error creating endpoint URL") + return err + } + requestBody, err := d.autofixFeedbackRequestBody(&options) if err != nil { logger.Err(err).Str("requestBody", string(requestBody)).Msg("error creating request body") @@ -205,7 +218,7 @@ func (d *DeepCodeLLMBindingImpl) submitAutofixFeedback(ctx context.Context, requ } logger.Info().Msg("Started obtaining autofix Response") - _, err = d.submitRequest(ctx, options.Endpoint, requestBody) + _, err = d.submitRequest(ctx, endpoint, requestBody) logger.Info().Msg("Finished obtaining autofix Response") return err diff --git a/llm/api_client_test.go b/llm/api_client_test.go index b950cf1..4bf169b 100644 --- a/llm/api_client_test.go +++ b/llm/api_client_test.go @@ -283,3 +283,71 @@ func TestAddDefaultHeadersWithExistingHeaders(t *testing.T) { t.Errorf("Expected Existing-Header to be 'existing-value', got %s", existingHeader) } } + +func TestAutofixRequestBody(t *testing.T) { + d := &DeepCodeLLMBindingImpl{} + + const testBundleHash = "0123456789abcdef" + const testBaseDir = "basedir" + const testFilePath = "/path/to/file" + const testLineNumber0Based = 0 + const testRuleId = "rule_id" + const testShardKey = "shard_key" + const testHost = "http://api.test.snyk.io" + const testIdeName = "my IDE" + const testIdeVersion = "1.0.0" + const testExtensionName = "my extension" + const testExtensionVersion = "1.2.3" + + options := AutofixOptions{ + RuleID: testRuleId, + BundleHash: testBundleHash, + ShardKey: testShardKey, + Host: testHost, + BaseDir: testBaseDir, + FilePath: testFilePath, + LineNum: testLineNumber0Based, + CodeRequestContext: CodeRequestContext{ + Initiator: "", + Flow: "", + Org: CodeRequestContextOrg{}, + }, + IdeExtensionDetails: AutofixIdeExtensionDetails{ + IdeName: testIdeName, + IdeVersion: testIdeVersion, + ExtensionName: testExtensionName, + ExtensionVersion: testExtensionVersion, + }, + } + + jsonBody, err := d.autofixRequestBody(&options) + assert.NoError(t, err) + + expectedBody := AutofixRequest{ + Key: AutofixRequestKey{ + Type: "file", + Hash: testBundleHash, + Shard: testShardKey, + FilePath: testFilePath, + RuleId: testRuleId, + LineNum: testLineNumber0Based, + }, + AnalysisContext: CodeRequestContext{ + Initiator: "", + Flow: "", + Org: CodeRequestContextOrg{}, + }, + IdeExtensionDetails: AutofixIdeExtensionDetails{ + IdeName: testIdeName, + IdeVersion: testIdeVersion, + ExtensionName: testExtensionName, + ExtensionVersion: testExtensionVersion, + }, + } + + var body AutofixRequest + err = json.Unmarshal(jsonBody, &body) + assert.NoError(t, err) + + assert.Equal(t, expectedBody, body) +} diff --git a/llm/types.go b/llm/types.go index b402e21..d7cf6f2 100644 --- a/llm/types.go +++ b/llm/types.go @@ -133,7 +133,7 @@ type AutofixOptions struct { FilePath string LineNum int - Endpoint *url.URL + Host string CodeRequestContext CodeRequestContext IdeExtensionDetails AutofixIdeExtensionDetails } @@ -142,7 +142,7 @@ type AutofixFeedbackOptions struct { FixID string Result string - Endpoint *url.URL + Host string CodeRequestContext CodeRequestContext IdeExtensionDetails AutofixIdeExtensionDetails }