Skip to content

Commit 0041725

Browse files
fixed bug #175 Predefined prompts lookup fails if two prompts have key with same prefix (#176)
* fixed bug #175 Predefined prompts lookup fails if two prompts have key with same prefix * fixed project info concatenated to the prompt without any separation
1 parent b850533 commit 0041725

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/main/java/io/github/jeddict/ai/hints/AssistantChatManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
import java.io.IOException;
8888
import java.lang.ref.WeakReference;
8989
import java.util.ArrayList;
90+
import java.util.Comparator;
9091
import java.util.HashSet;
9192
import java.util.List;
9293
import java.util.Map;
@@ -737,14 +738,23 @@ public void actionPerformed(ActionEvent ae) {
737738
String question = questionPane.getText();
738739
Map<String, String> prompts = PreferencesManager.getInstance().getPrompts();
739740

740-
for (Map.Entry<String, String> entry : prompts.entrySet()) {
741-
String promptKey = entry.getKey();
742-
String promptValue = entry.getValue();
741+
//
742+
// To make sure the longest matching shortcut matches (i.e.
743+
// 'shortcutlong' is 'shortcut' is defined as well) let's
744+
// sort the shurtcuts in descending order; this guarantees
745+
// 'shortcut2' is matched before "shortcut" in the for loop
746+
//
747+
ArrayList<String> promptKeys = new ArrayList();
748+
promptKeys.addAll(prompts.keySet());
749+
promptKeys.sort(Comparator.reverseOrder());
743750

744-
String toReplace = "/" + promptKey;
751+
for (String key: promptKeys) {
752+
String prompt = prompts.get(key);
753+
754+
String toReplace = "/" + key;
745755

746756
if (question.contains(toReplace)) {
747-
question = question.replace(toReplace, promptValue);
757+
question = question.replace(toReplace, prompt);
748758
}
749759
}
750760
if (!question.isEmpty()) {

src/main/java/io/github/jeddict/ai/lang/JeddictChatModelBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private String generateInternal(Project project, String prompt, List<String> ima
226226
JOptionPane.ERROR_MESSAGE);
227227
}
228228
if (project != null) {
229-
prompt = prompt + ProjectMetadataInfo.get(project);
229+
prompt = prompt + "\n" + ProjectMetadataInfo.get(project);
230230
}
231231
String systemMessage = null;
232232
String globalRules = PreferencesManager.getInstance().getGlobalRules();

0 commit comments

Comments
 (0)