Skip to content

Commit

Permalink
Merge pull request #91 from devoxx/issue-88
Browse files Browse the repository at this point in the history
Feat #88: Use TextArea for prompts
  • Loading branch information
stephanj authored May 31, 2024
2 parents 2f25b95 + 64221b3 commit be75b69
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public StreamingChatLanguageModel createStreamingChatModel(@NotNull ChatModel ch

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getAnthropicKey();
return SettingsStateService.getInstance().getAnthropicKey().trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public StreamingChatLanguageModel createStreamingChatModel(@NotNull ChatModel ch

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getDeepInfraKey();
return SettingsStateService.getInstance().getDeepInfraKey().trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ChatLanguageModel createChatModel(@NotNull ChatModel chatModel) {

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getGeminiKey();
return SettingsStateService.getInstance().getGeminiKey().trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ChatLanguageModel createChatModel(@NotNull ChatModel chatModel) {

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getGroqKey();
return SettingsStateService.getInstance().getGroqKey().trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public StreamingChatLanguageModel createStreamingChatModel(@NotNull ChatModel ch

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getMistralKey();
return SettingsStateService.getInstance().getMistralKey().trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public StreamingChatLanguageModel createStreamingChatModel(@NotNull ChatModel ch

@Override
public String getApiKey() {
return SettingsStateService.getInstance().getOpenAIKey();
return SettingsStateService.getInstance().getOpenAIKey().trim();
}

@Override
Expand Down
75 changes: 58 additions & 17 deletions src/main/java/com/devoxx/genie/ui/DevoxxGenieSettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.messages.MessageBus;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nls;
Expand Down Expand Up @@ -63,10 +64,11 @@ public class DevoxxGenieSettingsManager implements Configurable {
private JCheckBox astReferenceFieldCheckBox;
private JCheckBox astReferenceClassesCheckBox;

private JTextField testPromptField;
private JTextField explainPromptField;
private JTextField reviewPromptField;
private JTextField customPromptField;
private JTextArea testPromptField;
private JTextArea explainPromptField;
private JTextArea reviewPromptField;
private JTextArea customPromptField;

private JTextField maxOutputTokensField;

public DevoxxGenieSettingsManager() {
Expand Down Expand Up @@ -147,10 +149,10 @@ public JComponent createComponent() {

setTitle("Predefined Command Prompts", settingsPanel, gbc);

testPromptField = addTextFieldWithLabel(settingsPanel, gbc, "Test prompt :", settings.getTestPrompt());
explainPromptField = addTextFieldWithLabel(settingsPanel, gbc, "Explain prompt :", settings.getExplainPrompt());
reviewPromptField = addTextFieldWithLabel(settingsPanel, gbc, "Review prompt :", settings.getReviewPrompt());
customPromptField = addTextFieldWithLabel(settingsPanel, gbc, "Custom prompt :", settings.getCustomPrompt());
testPromptField = addTextAreaWithLabel(settingsPanel, gbc, "Test prompt :", settings.getTestPrompt());
explainPromptField = addTextAreaWithLabel(settingsPanel, gbc, "Explain prompt :", settings.getExplainPrompt());
reviewPromptField = addTextAreaWithLabel(settingsPanel, gbc, "Review prompt :", settings.getReviewPrompt());
customPromptField = addTextAreaWithLabel(settingsPanel, gbc, "Custom prompt :", settings.getCustomPrompt());
return settingsPanel;
}

Expand Down Expand Up @@ -183,7 +185,6 @@ private void setTitle(String title,

/**
* Add a text field with label
*
* @param panel the panel
* @param gbc the gridbag constraints
* @param label the label
Expand All @@ -202,6 +203,26 @@ private void setTitle(String title,
return textField;
}

/**
* Add a text area with label
* @param panel the panel
* @param gbc the gridbag constraints
* @param label the label
* @param value the value
* @return the text field
*/
private @NotNull JTextArea addTextAreaWithLabel(@NotNull JPanel panel,
GridBagConstraints gbc,
String label,
String value) {
panel.add(new JLabel(label), gbc);
gbc.gridx++;
JTextArea textArea = new JTextArea(value, 3, 80);
panel.add(textArea, gbc);
resetGbc(gbc);
return textArea;
}

/**
* Add a field with label and a link button
*
Expand Down Expand Up @@ -369,10 +390,13 @@ public boolean isModified() {
isModified |= isFieldModified(maxOutputTokensField, settings.getMaxOutputTokens());
isModified |= isFieldModified(retryField, settings.getMaxRetries());
isModified |= isFieldModified(chatMemorySizeField, settings.getChatMemorySize());
isModified |= isFieldModified(testPromptField, settings.getTestPrompt());
isModified |= isFieldModified(explainPromptField, settings.getExplainPrompt());
isModified |= isFieldModified(reviewPromptField, settings.getReviewPrompt());
isModified |= isFieldModified(customPromptField, settings.getCustomPrompt());

isModified |= !StringUtil.equals(testPromptField.getText(), settings.getTestPrompt());
isModified |= !StringUtil.equals(explainPromptField.getText(), settings.getExplainPrompt());
isModified |= !StringUtil.equals(reviewPromptField.getText(), settings.getReviewPrompt());

isModified |= !StringUtil.equals(customPromptField.getText(), settings.getCustomPrompt());

isModified |= isFieldModified(openAiKeyField, settings.getOpenAIKey());
isModified |= isFieldModified(mistralKeyField, settings.getMistralKey());
isModified |= isFieldModified(anthropicKeyField, settings.getAnthropicKey());
Expand Down Expand Up @@ -430,10 +454,12 @@ public void apply() {
updateSettingIfModified(timeoutField, settings.getTimeout(), value -> settings.setTimeout(safeCastToInteger(value)));
updateSettingIfModified(retryField, settings.getMaxRetries(), value -> settings.setMaxRetries(safeCastToInteger(value)));
updateSettingIfModified(maxOutputTokensField, settings.getMaxOutputTokens(), settings::setMaxOutputTokens);
updateSettingIfModified(testPromptField, settings.getTestPrompt(), settings::setTestPrompt);
updateSettingIfModified(explainPromptField, settings.getExplainPrompt(), settings::setExplainPrompt);
updateSettingIfModified(reviewPromptField, settings.getReviewPrompt(), settings::setReviewPrompt);
updateSettingIfModified(customPromptField, settings.getCustomPrompt(), settings::setCustomPrompt);

updateTextAreaIfModified(testPromptField, settings.getTestPrompt(), settings::setTestPrompt);
updateTextAreaIfModified(explainPromptField, settings.getExplainPrompt(), settings::setExplainPrompt);
updateTextAreaIfModified(reviewPromptField, settings.getReviewPrompt(), settings::setReviewPrompt);
updateTextAreaIfModified(customPromptField, settings.getCustomPrompt(), settings::setCustomPrompt);

updateSettingIfModified(streamModeCheckBox, settings.getStreamMode(), value -> settings.setStreamMode(Boolean.parseBoolean(value)));

updateSettingIfModified(astModeCheckBox, settings.getAstMode(), value -> settings.setAstMode(Boolean.parseBoolean(value)));
Expand All @@ -447,6 +473,21 @@ public void apply() {
}
}

/**
* Update the text area if the value has changed
* @param textArea the text area
* @param currentValue the current value
* @param updateAction the update action
*/
public void updateTextAreaIfModified(@NotNull JTextArea textArea,
Object currentValue,
Consumer<String> updateAction) {
String newValue = textArea.getText();
if (newValue != null && !newValue.equals(currentValue)) {
updateAction.accept(newValue);
}
}

/**
* Update the setting if the field value has changed
* @param field the field
Expand Down

0 comments on commit be75b69

Please sign in to comment.