|
| 1 | +package org.togetherjava.tjbot.features.messages; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.Message; |
| 4 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 5 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 6 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 7 | +import net.dv8tion.jda.api.interactions.commands.build.OptionData; |
| 8 | +import org.jetbrains.annotations.Nullable; |
| 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; |
| 11 | + |
| 12 | +import org.togetherjava.tjbot.features.CommandVisibility; |
| 13 | +import org.togetherjava.tjbot.features.SlashCommandAdapter; |
| 14 | +import org.togetherjava.tjbot.features.chatgpt.ChatGptModel; |
| 15 | +import org.togetherjava.tjbot.features.chatgpt.ChatGptService; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | + |
| 19 | +/** |
| 20 | + * The implemented command is {@code /rewrite}, which allows users to have their message rewritten |
| 21 | + * in a clearer, more professional, or better structured form using AI. |
| 22 | + * <p> |
| 23 | + * The rewritten message is shown as an ephemeral message visible only to the user who triggered the |
| 24 | + * command. |
| 25 | + * <p> |
| 26 | + * Users can optionally specify a tone/style for the rewrite. |
| 27 | + */ |
| 28 | +public final class RewriteCommand extends SlashCommandAdapter { |
| 29 | + private static final Logger logger = LoggerFactory.getLogger(RewriteCommand.class); |
| 30 | + private static final String COMMAND_NAME = "rewrite"; |
| 31 | + private static final String MESSAGE_OPTION = "message"; |
| 32 | + private static final String TONE_OPTION = "tone"; |
| 33 | + |
| 34 | + private static final int MAX_MESSAGE_LENGTH = Message.MAX_CONTENT_LENGTH; |
| 35 | + private static final int MIN_MESSAGE_LENGTH = 3; |
| 36 | + |
| 37 | + private static final String AI_REWRITE_PROMPT_TEMPLATE = """ |
| 38 | + You are rewriting a Discord text chat message for clarity and professionalism. |
| 39 | + Keep it conversational and casual, not email or formal document format. |
| 40 | +
|
| 41 | + Tone: %s |
| 42 | +
|
| 43 | + Rewrite the message to: |
| 44 | + - Improve clarity and structure |
| 45 | + - Maintain the original meaning |
| 46 | + - Avoid em-dashes (—) |
| 47 | + - Stay under %d characters (strict limit) |
| 48 | +
|
| 49 | + If the message is already well-written, make only minor improvements. |
| 50 | +
|
| 51 | + Reply with ONLY the rewritten message, nothing else (greetings, preamble, etc). |
| 52 | +
|
| 53 | + Message to rewrite: |
| 54 | + %s |
| 55 | + """.stripIndent(); |
| 56 | + |
| 57 | + private final ChatGptService chatGptService; |
| 58 | + |
| 59 | + /** |
| 60 | + * Creates the slash command definition and configures available options for rewriting messages. |
| 61 | + * |
| 62 | + * @param chatGptService service for interacting with ChatGPT |
| 63 | + */ |
| 64 | + public RewriteCommand(ChatGptService chatGptService) { |
| 65 | + super(COMMAND_NAME, "Let AI rephrase and improve your message", CommandVisibility.GUILD); |
| 66 | + |
| 67 | + this.chatGptService = chatGptService; |
| 68 | + |
| 69 | + OptionData messageOption = |
| 70 | + new OptionData(OptionType.STRING, MESSAGE_OPTION, "The message you want to rewrite", |
| 71 | + true) |
| 72 | + .setMinLength(MIN_MESSAGE_LENGTH) |
| 73 | + .setMaxLength(MAX_MESSAGE_LENGTH); |
| 74 | + |
| 75 | + OptionData toneOption = new OptionData(OptionType.STRING, TONE_OPTION, |
| 76 | + "The tone/style for the rewritten message (default: " |
| 77 | + + MessageTone.CLEAR.displayName + ")", |
| 78 | + false); |
| 79 | + |
| 80 | + Arrays.stream(MessageTone.values()) |
| 81 | + .forEach(tone -> toneOption.addChoice(tone.displayName, tone.name())); |
| 82 | + |
| 83 | + getData().addOptions(messageOption, toneOption); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onSlashCommand(SlashCommandInteractionEvent event) { |
| 88 | + |
| 89 | + OptionMapping messageOption = event.getOption(MESSAGE_OPTION); |
| 90 | + |
| 91 | + if (messageOption == null) { |
| 92 | + throw new IllegalArgumentException( |
| 93 | + "Required option '" + MESSAGE_OPTION + "' is missing"); |
| 94 | + } |
| 95 | + |
| 96 | + String userMessage = messageOption.getAsString(); |
| 97 | + |
| 98 | + MessageTone tone = parseTone(event.getOption(TONE_OPTION)); |
| 99 | + |
| 100 | + event.deferReply(true).queue(); |
| 101 | + |
| 102 | + String rewrittenMessage = rewrite(userMessage, tone); |
| 103 | + |
| 104 | + if (rewrittenMessage.isEmpty()) { |
| 105 | + logger.debug("Failed to obtain a response for /{}, original message: '{}'", |
| 106 | + COMMAND_NAME, userMessage); |
| 107 | + |
| 108 | + event.getHook() |
| 109 | + .editOriginal( |
| 110 | + "An error occurred while processing your request. Please try again later.") |
| 111 | + .queue(); |
| 112 | + |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + logger.debug("Rewrite successful; rewritten message length: {}", rewrittenMessage.length()); |
| 117 | + |
| 118 | + event.getHook().sendMessage(rewrittenMessage).setEphemeral(true).queue(); |
| 119 | + } |
| 120 | + |
| 121 | + private MessageTone parseTone(@Nullable OptionMapping toneOption) |
| 122 | + throws IllegalArgumentException { |
| 123 | + |
| 124 | + if (toneOption == null) { |
| 125 | + logger.debug("Tone option not provided, using default '{}'", MessageTone.CLEAR.name()); |
| 126 | + return MessageTone.CLEAR; |
| 127 | + } |
| 128 | + |
| 129 | + return MessageTone.valueOf(toneOption.getAsString()); |
| 130 | + } |
| 131 | + |
| 132 | + private String rewrite(String userMessage, MessageTone tone) { |
| 133 | + |
| 134 | + String rewritePrompt = createAiPrompt(userMessage, tone); |
| 135 | + |
| 136 | + ChatGptModel aiModel = tone.model; |
| 137 | + |
| 138 | + String attempt = askAi(rewritePrompt, aiModel); |
| 139 | + |
| 140 | + if (attempt.length() <= MAX_MESSAGE_LENGTH) { |
| 141 | + return attempt; |
| 142 | + } |
| 143 | + |
| 144 | + logger.debug("Rewritten message exceeded {} characters; retrying with stricter constraint", |
| 145 | + MAX_MESSAGE_LENGTH); |
| 146 | + |
| 147 | + String shortenPrompt = |
| 148 | + """ |
| 149 | + %s |
| 150 | +
|
| 151 | + Constraint reminder: Your previous rewrite exceeded %d characters. |
| 152 | + Provide a revised rewrite strictly under %d characters while preserving meaning and tone. |
| 153 | + """ |
| 154 | + .formatted(rewritePrompt, MAX_MESSAGE_LENGTH, MAX_MESSAGE_LENGTH); |
| 155 | + |
| 156 | + return askAi(shortenPrompt, aiModel); |
| 157 | + } |
| 158 | + |
| 159 | + private String askAi(String shortenPrompt, ChatGptModel aiModel) { |
| 160 | + return chatGptService.askRaw(shortenPrompt, aiModel).orElse(""); |
| 161 | + } |
| 162 | + |
| 163 | + private static String createAiPrompt(String userMessage, MessageTone tone) { |
| 164 | + return AI_REWRITE_PROMPT_TEMPLATE.formatted(tone.description, MAX_MESSAGE_LENGTH, |
| 165 | + userMessage); |
| 166 | + } |
| 167 | + |
| 168 | + private enum MessageTone { |
| 169 | + CLEAR("Clear", "Make it clear and easy to understand.", ChatGptModel.FASTEST), |
| 170 | + PROFESSIONAL("Professional", "Use a professional and polished tone.", ChatGptModel.FASTEST), |
| 171 | + DETAILED("Detailed", "Expand with more detail and explanation.", ChatGptModel.HIGH_QUALITY), |
| 172 | + TECHNICAL("Technical", "Use technical and specialized language where appropriate.", |
| 173 | + ChatGptModel.HIGH_QUALITY); |
| 174 | + |
| 175 | + private final String displayName; |
| 176 | + private final String description; |
| 177 | + private final ChatGptModel model; |
| 178 | + |
| 179 | + MessageTone(String displayName, String description, ChatGptModel model) { |
| 180 | + this.displayName = displayName; |
| 181 | + this.description = description; |
| 182 | + this.model = model; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | +} |
0 commit comments