Skip to content

Commit

Permalink
0.1.5 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski authored Jan 10, 2023
1 parent 7dc600f commit 89ced21
Show file tree
Hide file tree
Showing 23 changed files with 1,435 additions and 590 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

## [Unreleased]

## [0.1.5]
### Added
- Added dropdown model selection
- Improved comment parsing
- Added functions to create new list items and table rows and columns (markdown)

## [0.1.4]

### Added
Expand Down
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ We can also iterate and use the AI to refactor the code using custom edits:

![aicoder_editresult.png](docs/aicoder_editresult.png)

---

## Template ToDo list
- [x] Create a new [IntelliJ Platform Plugin Template][template] project.
- [x] Get familiar with the [template documentation][template].
- [x] Verify the [pluginGroup](./gradle.properties), [plugin ID](./src/main/resources/META-INF/plugin.xml) and [sources package](./src/main/kotlin).
- [x] Review the [Legal Agreements](https://plugins.jetbrains.com/docs/marketplace/legal-agreements.html?from=IJPluginTemplate).
- [x] [Publish a plugin manually](https://plugins.jetbrains.com/docs/intellij/publishing-plugin.html?from=IJPluginTemplate) for the first time.
- [x] Set the Plugin ID in the above README badges.
- [ ] Set the [Plugin Signing](https://plugins.jetbrains.com/docs/intellij/plugin-signing.html?from=IJPluginTemplate) related [secrets](https://github.com/JetBrains/intellij-platform-plugin-template#environment-variables).
- [ ] Set the [Deployment Token](https://plugins.jetbrains.com/docs/marketplace/plugin-upload.html?from=IJPluginTemplate).
- [ ] Click the <kbd>Watch</kbd> button on the top of the [IntelliJ Platform Plugin Template][template] to be notified about releases containing new features and fixes.

---
Plugin based on the [IntelliJ Platform Plugin Template][template].

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pluginGroup = com.github.simiacryptus
pluginName = intellij-aicoder
pluginRepositoryUrl = https://github.com/SimiaCryptus/intellij-aicoder
# SemVer format -> https://semver.org
pluginVersion = 0.1.4
pluginVersion = 0.1.5

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
Expand Down
391 changes: 0 additions & 391 deletions src/main/java/com/github/simiacryptus/aicoder/AICoderMainMenu.java

This file was deleted.

397 changes: 334 additions & 63 deletions src/main/java/com/github/simiacryptus/aicoder/ComputerLanguage.java

Large diffs are not rendered by default.

666 changes: 666 additions & 0 deletions src/main/java/com/github/simiacryptus/aicoder/EditorMenu.java

Large diffs are not rendered by default.

68 changes: 0 additions & 68 deletions src/main/java/com/github/simiacryptus/aicoder/StringTools.java

This file was deleted.

32 changes: 26 additions & 6 deletions src/main/java/com/github/simiacryptus/aicoder/StyleUtil.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.simiacryptus.aicoder;

import com.github.simiacryptus.aicoder.config.AppSettingsState;
import com.github.simiacryptus.aicoder.openai.*;
import com.github.simiacryptus.aicoder.openai.ModerationException;
import com.github.simiacryptus.aicoder.text.IndentedText;
import com.github.simiacryptus.aicoder.text.StringTools;
import com.intellij.openapi.diagnostic.Logger;

import javax.swing.*;
Expand Down Expand Up @@ -90,8 +91,12 @@ public class StyleUtil {
"Yoda-Speak"
);

// This here code is gonna pick two random styles from a list of 'em and combine 'em with a random dialect
// It'll then return a string with the combination of the three
/**
*
* This method will generate a random combination of a dialect and style
*
* @return A string in the format of "Dialect - Casual, Inspirational"
*/
public static String randomStyle() {
String dialect = dialectKeywords.get(new Random().nextInt(dialectKeywords.size()));
String style1 = styleKeywords.get(new Random().nextInt(styleKeywords.size()));
Expand All @@ -112,26 +117,41 @@ public static void demoStyle(String style) {
"String randomItem = items.get(randomIndex);");
}

/**
* Demonstrates the description of a given code snippet using a given style.
*
* @param style The style to describe with.
* @param language The language of the code snippet.
* @param code The code snippet to be described.
*/
public static void demoStyle(String style, ComputerLanguage language, String code) {
String codeDescription = describeTest(style, language, code);
String message = String.format("This code:\n %s\nwas described as:\n %s", code.replace("\n", "\n "), codeDescription.replace("\n", "\n "));
JOptionPane.showMessageDialog(null, message, "Style Demo", JOptionPane.INFORMATION_MESSAGE);
}

public static String describeTest(String style, ComputerLanguage language, String inputString) {
/**
* Describes some test code in the specified style and language.
*
* @param style The style of the description.
* @param language The language of the test.
* @param code The code.
* @return A description of the test in the specified style and language.
*/
public static String describeTest(String style, ComputerLanguage language, String code) {
AppSettingsState settings = AppSettingsState.getInstance();
try {
return StringTools.lineWrapping(settings.createTranslationRequest()
.setInstruction(String.format("Explain this %s in %s (%s)", language.name(), settings.humanLanguage, style))
.setInputText(IndentedText.fromString(inputString).textBlock.trim())
.setInputText(IndentedText.fromString(code).getTextBlock().trim())
.setInputType(language.name())
.setInputAttribute("type", "code")
.setOutputType(settings.humanLanguage)
.setOutputAttrute("type", "description")
.setOutputAttrute("style", style)
.buildCompletionRequest()
.complete("")
.trim());
.trim(), 120);
} catch (ModerationException e) {
return e.getMessage();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void actionPerformed(@NotNull final AnActionEvent e) {
editor.getDocument().replaceString(primaryCaret.getSelectionStart(), primaryCaret.getSelectionEnd(), newText);
});
} catch (ModerationException | IOException ex) {
AICoderMainMenu.handle(ex);
EditorMenu.handle(ex);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.github.simiacryptus.aicoder.config;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.simiacryptus.aicoder.StyleUtil;
import com.github.simiacryptus.aicoder.openai.OpenAI;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBPasswordField;
Expand All @@ -13,12 +17,31 @@
import java.util.Arrays;

public class AppSettingsComponent extends SimpleSettingsComponent<AppSettingsState> {
private static final Logger log = Logger.getInstance(AppSettingsComponent.class);
@Name("API Base")
public final JBTextField apiBase = new JBTextField();
@Name("API Key")
public final JBPasswordField apiKey = new JBPasswordField();
@Name("Model")
public final JBTextField model = new JBTextField();
public final JComponent model = getModelSelector();

@NotNull
private static JComponent getModelSelector() {
try {
ObjectNode engines = OpenAI.INSTANCE.getEngines();
JsonNode data = engines.get("data");
String[] items = new String[data.size()];
for (int i = 0; i < data.size(); i++) {
items[i] = data.get(i).get("id").asText();
}
Arrays.sort(items);
return new ComboBox<String>(items);
} catch (Throwable e) {
log.warn(e);
return new JBTextField();
}
}

@Name("Style")
public final JBTextField style = new JBTextField();
@Name("Human Language")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.github.simiacryptus.aicoder.config;

import com.github.simiacryptus.aicoder.AICoderMainMenu;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import java.util.ArrayList;
import java.util.Arrays;

import static com.github.simiacryptus.aicoder.StringTools.stripPrefix;
import static com.github.simiacryptus.aicoder.StringTools.stripUnbalancedTerminators;
import static com.github.simiacryptus.aicoder.text.StringTools.stripPrefix;
import static com.github.simiacryptus.aicoder.text.StringTools.stripUnbalancedTerminators;

/**
* The CompletionRequest class is used to create a request for completion of a given prompt.
Expand All @@ -34,34 +34,40 @@ public CompletionRequest(String prompt, double temperature, int max_tokens, Inte
this.echo = echo;
}

@Nullable
@NotNull
public String complete(String indent) throws IOException, ModerationException {
return getCompletionText(OpenAI.INSTANCE.complete(this), indent);
}

@Nullable
public String getCompletionText(CompletionResponse response, String indent) {
CompletionResponse response = OpenAI.INSTANCE.complete(this);
return response
.getFirstChoice()
.map(completion -> stripPrefix(completion, this.prompt))
.map(String::trim)
.map(completion -> stripPrefix(completion, this.prompt.trim()))
.map(String::trim)
.map(completion -> stripUnbalancedTerminators(completion))
.map(IndentedText::fromString)
.map(indentedText -> indentedText.withIndent(indent))
.map(IndentedText::toString)
.map(indentedText -> indent + indentedText)
.orElse(null);
.orElse("");
}

public @NotNull CompletionRequest appendPrompt(String prompt) {
this.prompt = this.prompt + prompt;
return this;
}

public @NotNull CompletionRequest addStops(String @NotNull [] stop) {
public @NotNull CompletionRequest addStops(@NotNull String... newStops) {
ArrayList<String> stops = new ArrayList<>();
Arrays.stream(this.stop).forEach(stops::add);
Arrays.stream(stop).forEach(stops::add);
this.stop = stops.stream().distinct().toArray(String[]::new);
for (String x : newStops) {
if (x != null) {
if (!x.isEmpty()) {
stops.add(x);
}
}
}
if (!stops.isEmpty()) {
Arrays.stream(this.stop).forEach(stops::add);
this.stop = stops.stream().distinct().toArray(String[]::new);
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.IOException;
import java.util.Map;

import static com.github.simiacryptus.aicoder.StringTools.stripPrefix;
import static com.github.simiacryptus.aicoder.text.StringTools.stripPrefix;

public class OpenAI {

Expand Down Expand Up @@ -63,9 +63,8 @@ public CompletionResponse complete(@NotNull CompletionRequest completionRequest)
throw new IOException(errorMessage);
}
CompletionResponse completionResponse = getMapper().readValue(result, CompletionResponse.class);
String completionResult = stripPrefix(completionResponse.getFirstChoice().orElse(""), completionRequest.prompt).replace("\n", "\n\t");
log(settings.apiLogLevel, String.format("Text Completion Request\nPrefix:\n%s\n\nCompletion:\n%s", completionRequest.prompt.replace("\n", "\n\t"), completionResult));
//writeRequestLog(String.format("Request:\n%s\n\nResponse:\n%s", request, result));
String completionResult = stripPrefix(completionResponse.getFirstChoice().orElse("").trim(), completionRequest.prompt.trim());
log(settings.apiLogLevel, String.format("Text Completion Request\nPrefix:\n\t%s\n\nCompletion:\n\t%s", completionRequest.prompt.replace("\n", "\n\t"), completionResult.replace("\n", "\n\t")));
return completionResponse;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down Expand Up @@ -168,7 +167,7 @@ protected void authorize(@NotNull HttpRequestBase request) throws IOException {
/**
* Gets the response from the given URL.
*
* @param url The URL to get the response from.
* @param url The URL to GET the response from.
* @return The response from the given URL.
* @throws IOException If an I/O error occurs.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.simiacryptus.aicoder.text;
package com.github.simiacryptus.aicoder.psi;

import com.github.simiacryptus.aicoder.StringTools;
import com.github.simiacryptus.aicoder.text.StringTools;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.simiacryptus.aicoder.text;
package com.github.simiacryptus.aicoder.psi;

import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
Expand Down
Loading

0 comments on commit 89ced21

Please sign in to comment.