Skip to content

Commit

Permalink
feat: add auto apply integration (CodeGPT)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlrobertoh committed Oct 25, 2024
1 parent 339a1f0 commit 488583e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 7 deletions.
15 changes: 13 additions & 2 deletions src/main/java/ee/carlrobert/llm/client/DeserializationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import ee.carlrobert.llm.client.codegpt.response.CodeGPTException;
import java.io.IOException;
import okhttp3.Response;

Expand All @@ -15,10 +16,20 @@ private DeserializationUtil() {

public static <T> T mapResponse(Response response, Class<T> clazz) {
var body = response.body();
if (body == null) {
throw new RuntimeException("Response body is null");
}

String json = "";
try {
return OBJECT_MAPPER.readValue(body.string(), clazz);
json = body.string();
return OBJECT_MAPPER.readValue(json, clazz);
} catch (IOException ex) {
throw new RuntimeException("Could not deserialize response", ex);
try {
throw OBJECT_MAPPER.readValue(json, CodeGPTException.class);
} catch (IOException e) {
throw new RuntimeException("Could not deserialize response", ex);
}
}
}
}
29 changes: 24 additions & 5 deletions src/main/java/ee/carlrobert/llm/client/codegpt/CodeGPTClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import ee.carlrobert.llm.PropertiesLoader;
import ee.carlrobert.llm.client.DeserializationUtil;
import ee.carlrobert.llm.client.codegpt.request.AutoApplyRequest;
import ee.carlrobert.llm.client.codegpt.request.CodeCompletionRequest;
import ee.carlrobert.llm.client.codegpt.request.chat.ChatCompletionRequest;
import ee.carlrobert.llm.client.codegpt.response.AutoApplyResponse;
import ee.carlrobert.llm.client.openai.completion.ErrorDetails;
import ee.carlrobert.llm.client.openai.completion.OpenAIChatCompletionEventSourceListener;
import ee.carlrobert.llm.client.openai.completion.OpenAITextCompletionEventSourceListener;
Expand Down Expand Up @@ -42,10 +44,7 @@ public CodeGPTClient(String apiKey, OkHttpClient.Builder httpClientBuilder) {
}

public CodeGPTUserDetails getUserDetails(String apiKey) {
try (var response = new OkHttpClient.Builder().build()
.newCall(buildUserDetailsRequest(apiKey))
.execute()) {

try (var response = httpClient.newCall(buildUserDetailsRequest(apiKey)).execute()) {
return DeserializationUtil.mapResponse(response, CodeGPTUserDetails.class);
} catch (IOException e) {
throw new RuntimeException("Unable to fetch user details", e);
Expand All @@ -72,7 +71,15 @@ public OpenAIChatCompletionResponse getChatCompletion(ChatCompletionRequest requ
try (var response = httpClient.newCall(buildChatCompletionRequest(request)).execute()) {
return DeserializationUtil.mapResponse(response, OpenAIChatCompletionResponse.class);
} catch (IOException e) {
throw new RuntimeException(e);
throw new RuntimeException("Unable to get chat completion", e);
}
}

public AutoApplyResponse applySuggestedChanges(AutoApplyRequest request) {
try (var response = httpClient.newCall(buildAutoApplyRequest(request)).execute()) {
return DeserializationUtil.mapResponse(response, AutoApplyResponse.class);
} catch (IOException e) {
throw new RuntimeException("Unable to apply suggested changes", e);
}
}

Expand Down Expand Up @@ -116,6 +123,18 @@ private Request buildUserDetailsRequest(String apiKey) {
.build();
}

private Request buildAutoApplyRequest(AutoApplyRequest request) {
try {
return new Request.Builder()
.url(BASE_URL + "/v1/files/apply-changes")
.header("Authorization", "Bearer " + apiKey)
.post(RequestBody.create(OBJECT_MAPPER.writeValueAsString(request), APPLICATION_JSON))
.build();
} catch (JsonProcessingException e) {
throw new RuntimeException("Unable to build file diff request", e);
}
}

private Map<String, String> getRequiredHeaders() {
var headers = new HashMap<>(Map.of(
"X-LLM-Application-Tag", "codegpt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ee.carlrobert.llm.client.codegpt.request;

public class AutoApplyRequest {

private String suggestedChanges;
private String fileContent;

public String getSuggestedChanges() {
return suggestedChanges;
}

public void setSuggestedChanges(String suggestedChanges) {
this.suggestedChanges = suggestedChanges;
}

public String getFileContent() {
return fileContent;
}

public void setFileContent(String fileContent) {
this.fileContent = fileContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ee.carlrobert.llm.client.codegpt.response;

public class AutoApplyResponse {

private String modifiedFileContent;

public String getModifiedFileContent() {
return modifiedFileContent;
}

public void setModifiedFileContent(String modifiedFileContent) {
this.modifiedFileContent = modifiedFileContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ee.carlrobert.llm.client.codegpt.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class CodeGPTException extends RuntimeException {

private String title;
private int status;
private String detail;
private String instance;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}

public String getInstance() {
return instance;
}

public void setInstance(String instance) {
this.instance = instance;
}
}

0 comments on commit 488583e

Please sign in to comment.