Refactor text functionalities and constants A new `joinWithSpace` text method has been created in TextUtils to segregate this functionality from ChatGptPrompt* classes. Additionally, the class constants SPACE and DOT have been transferred to TextUtils. Change-Id: I29cf22dc53c7fbc5201284b24e8155799181a517 Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java index 8553a6b..bd991f1 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/prompt/ChatGptPrompt.java
@@ -17,9 +17,6 @@ @Slf4j public class ChatGptPrompt { - public static final String SPACE = " "; - public static final String DOT = ". "; - // Reply attributes public static final String ATTRIBUTE_ID = "id"; public static final String ATTRIBUTE_REPLY = "reply"; @@ -68,10 +65,12 @@ } public static String getCommentRequestUserPrompt(int commentPropertiesSize) { - return DEFAULT_GPT_PROMPT_FORCE_JSON_FORMAT + SPACE + - buildFieldSpecifications(REQUEST_REPLY_ATTRIBUTES) + SPACE + - DEFAULT_GPT_REPLIES_PROMPT_INLINE + SPACE + - String.format(DEFAULT_GPT_REPLIES_PROMPT_ENFORCE_RESPONSE_CHECK, commentPropertiesSize); + return joinWithSpace(new ArrayList<>(List.of( + DEFAULT_GPT_PROMPT_FORCE_JSON_FORMAT, + buildFieldSpecifications(REQUEST_REPLY_ATTRIBUTES), + DEFAULT_GPT_REPLIES_PROMPT_INLINE, + String.format(DEFAULT_GPT_REPLIES_PROMPT_ENFORCE_RESPONSE_CHECK, commentPropertiesSize) + ))); } public static String getReviewPromptCommitMessages() { @@ -129,8 +128,7 @@ attributes.remove(ATTRIBUTE_SCORE); } updateRelevanceDescription(); - return buildFieldSpecifications(attributes) + SPACE + - DEFAULT_GPT_REPLIES_PROMPT_INLINE; + return buildFieldSpecifications(attributes) + SPACE + DEFAULT_GPT_REPLIES_PROMPT_INLINE; } private void updateScoreDescription() {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java index 2cf6d2b..9a16f7c 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/prompt/ChatGptPromptStateful.java
@@ -63,7 +63,7 @@ instructions.add(getReviewPromptCommitMessages()); } } - return String.join(SPACE, instructions); + return joinWithSpace(instructions); } public String getDefaultGptThreadReviewMessage(String patchSet) { @@ -89,7 +89,7 @@ DEFAULT_GPT_ASSISTANT_INSTRUCTIONS_DONT_GUESS_CODE, DEFAULT_GPT_ASSISTANT_INSTRUCTIONS_HISTORY )), - RULE_NUMBER_PREFIX, COLON + RULE_NUMBER_PREFIX, COLON_SPACE ))); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java index b63256e..b466c91 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateless/client/prompt/ChatGptPromptStateless.java
@@ -9,9 +9,7 @@ import java.util.Arrays; import java.util.List; -import static com.googlesource.gerrit.plugins.chatgpt.utils.StringUtils.concatenate; -import static com.googlesource.gerrit.plugins.chatgpt.utils.TextUtils.getNumberedListString; -import static com.googlesource.gerrit.plugins.chatgpt.utils.TextUtils.joinWithNewLine; +import static com.googlesource.gerrit.plugins.chatgpt.utils.TextUtils.*; @Slf4j public class ChatGptPromptStateless extends ChatGptPrompt { @@ -33,20 +31,22 @@ } public static String getDefaultGptReviewSystemPrompt() { - return DEFAULT_GPT_SYSTEM_PROMPT + DOT + - DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION + SPACE + - DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION_REVIEW; + return joinWithSpace(new ArrayList<>(List.of( + DEFAULT_GPT_SYSTEM_PROMPT + DOT, + DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION, + DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION_REVIEW + ))); } public String getGptSystemPrompt() { List<String> prompt = new ArrayList<>(Arrays.asList( - config.getString(Configuration.KEY_GPT_SYSTEM_PROMPT, DEFAULT_GPT_SYSTEM_PROMPT), DOT, + config.getString(Configuration.KEY_GPT_SYSTEM_PROMPT, DEFAULT_GPT_SYSTEM_PROMPT) + DOT, ChatGptPromptStateless.DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION )); if (!isCommentEvent) { - prompt.addAll(Arrays.asList(SPACE, ChatGptPromptStateless.DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION_REVIEW)); + prompt.add(ChatGptPromptStateless.DEFAULT_GPT_SYSTEM_PROMPT_INPUT_DESCRIPTION_REVIEW); } - return concatenate(prompt); + return joinWithSpace(prompt); } public String getGptUserPrompt(ChangeSetData changeSetData, String patchSet) { @@ -88,12 +88,13 @@ } private List<String> getReviewSteps() { - List<String> steps = new ArrayList<>(){}; - steps.add( - DEFAULT_GPT_REVIEW_PROMPT_REVIEW + SPACE + - DEFAULT_GPT_PROMPT_FORCE_JSON_FORMAT + SPACE + - getPatchSetReviewUserPrompt() - ); + List<String> steps = new ArrayList<>(List.of( + joinWithSpace(new ArrayList<>(List.of( + DEFAULT_GPT_REVIEW_PROMPT_REVIEW, + DEFAULT_GPT_PROMPT_FORCE_JSON_FORMAT, + getPatchSetReviewUserPrompt() + ))) + )); if (config.getGptReviewCommitMessages()) { steps.add(getReviewPromptCommitMessages()); }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/TextUtils.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/TextUtils.java index a78470f..e4f41e8 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/TextUtils.java +++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/utils/TextUtils.java
@@ -14,10 +14,11 @@ public static final String CODE_DELIMITER = "```"; public static final String CODE_DELIMITER_BEGIN ="\n\n" + CODE_DELIMITER + "\n"; public static final String CODE_DELIMITER_END ="\n" + CODE_DELIMITER + "\n"; - public static final String COLON = ": "; - - private static final String COMMA = ", "; - private static final String SEMICOLON = "; "; + public static final String SPACE = " "; + public static final String DOT = "."; + public static final String COMMA_SPACE = ", "; + public static final String COLON_SPACE = ": "; + public static final String SEMICOLON_SPACE = "; "; public static String parseOutOfDelimiters(String body, String splitDelim, Function<String, String> processMessage, String leftDelimReplacement, String rightDelimReplacement) { @@ -48,12 +49,16 @@ return String.join("\n\n", components); } + public static String joinWithSpace(List<String> components) { + return String.join(SPACE, components); + } + public static String joinWithComma(Set<String> components) { - return String.join(COMMA, components); + return String.join(COMMA_SPACE, components); } public static String joinWithSemicolon(List<String> components) { - return String.join(SEMICOLON, components); + return String.join(SEMICOLON_SPACE, components); } public static List<String> getNumberedList(List<String> components, String prefix, String postfix) {