Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand;
import org.togetherjava.tjbot.features.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.features.messages.MessageCommand;
import org.togetherjava.tjbot.features.messages.RewriteCommand;
import org.togetherjava.tjbot.features.moderation.BanCommand;
import org.togetherjava.tjbot.features.moderation.KickCommand;
import org.togetherjava.tjbot.features.moderation.ModerationActionsStore;
Expand Down Expand Up @@ -207,6 +208,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new ChatGptCommand(chatGptService, helpSystemHelper));
features.add(new JShellCommand(jshellEval));
features.add(new MessageCommand());
features.add(new RewriteCommand(chatGptService));

FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
return blacklist.filterStream(features.stream(), Object::getClass).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public ChatGptService(Config config) {
boolean keyIsDefaultDescription = apiKey.startsWith("<") && apiKey.endsWith(">");
if (apiKey.isBlank() || keyIsDefaultDescription) {
isDisabled = true;
logger.warn("ChatGPT service is disabled: API key is not configured");
return;
}
openAIClient = OpenAIOkHttpClient.builder().apiKey(apiKey).timeout(TIMEOUT).build();
logger.info("ChatGPT service initialized successfully");
}

/**
Expand All @@ -56,10 +58,6 @@ public ChatGptService(Config config) {
* Tokens</a>.
*/
public Optional<String> ask(String question, @Nullable String context, ChatGptModel chatModel) {
if (isDisabled) {
return Optional.empty();
}

String contextText = context == null ? "" : ", Context: %s.".formatted(context);
String inputPrompt = """
For code supplied for review, refer to the old code supplied rather than
Expand All @@ -71,35 +69,71 @@ public Optional<String> ask(String question, @Nullable String context, ChatGptMo
Question: %s
""".formatted(contextText, question);

logger.debug("ChatGpt request: {}", inputPrompt);
return sendPrompt(inputPrompt, chatModel);
}

/**
* Prompt ChatGPT with a raw prompt and receive a response without any prefix wrapping.
* <p>
* Use this method when you need full control over the prompt structure without the service's
* opinionated formatting (e.g., for iterative refinement or specialized use cases).
*
* @param inputPrompt The raw prompt to send to ChatGPT. Max is {@value MAX_TOKENS} tokens.
* @param chatModel The AI model to use for this request.
* @return response from ChatGPT as a String.
* @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
* Tokens</a>.
*/
public Optional<String> askRaw(String inputPrompt, ChatGptModel chatModel) {
return sendPrompt(inputPrompt, chatModel);
}

/**
* Sends a prompt to the ChatGPT API and returns the response.
*
* @param prompt The prompt to send to ChatGPT.
* @param chatModel The AI model to use for this request.
* @return response from ChatGPT as a String.
*/
private Optional<String> sendPrompt(String prompt, ChatGptModel chatModel) {
if (isDisabled) {
logger.warn("ChatGPT request attempted but service is disabled");
return Optional.empty();
}

logger.debug("ChatGpt request: {}", prompt);

String response = null;
try {
ResponseCreateParams params = ResponseCreateParams.builder()
.model(chatModel.toChatModel())
.input(inputPrompt)
.input(prompt)
.maxOutputTokens(MAX_TOKENS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we're here now, something we've learned is that the MAX_TOKENS should not exceed 2000. Please can you update MAX_TOKENS to equal Message.MAX_CONTENT_LENGTH

.build();

Response chatGptResponse = openAIClient.responses().create(params);

response = chatGptResponse.output()
String response = chatGptResponse.output()
.stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.map(ResponseOutputText::text)
.collect(Collectors.joining("\n"));
} catch (RuntimeException runtimeException) {
logger.warn("There was an error using the OpenAI API: {}",
runtimeException.getMessage());
}

logger.debug("ChatGpt Response: {}", response);
if (response == null) {
logger.debug("ChatGpt Response: {}", response);

if (response.isBlank()) {
logger.warn("ChatGPT returned an empty response");
return Optional.empty();
}

logger.debug("ChatGpt response received successfully, length: {} characters",
response.length());
return Optional.of(response);
} catch (RuntimeException runtimeException) {
logger.error("Error communicating with OpenAI API: {}", runtimeException.getMessage(),
runtimeException);
return Optional.empty();
}

return Optional.of(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package org.togetherjava.tjbot.features.messages;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.features.CommandVisibility;
import org.togetherjava.tjbot.features.SlashCommandAdapter;
import org.togetherjava.tjbot.features.chatgpt.ChatGptModel;
import org.togetherjava.tjbot.features.chatgpt.ChatGptService;

import java.util.Arrays;
import java.util.Optional;

/**
* The implemented command is {@code /rewrite}, which allows users to have their message rewritten
* in a clearer, more professional, or better structured form using AI.
* <p>
* The rewritten message is shown as an ephemeral message visible only to the user who triggered the
* command.
* <p>
* Users can optionally specify a tone/style for the rewrite.
*/
public final class RewriteCommand extends SlashCommandAdapter {
private static final Logger logger = LoggerFactory.getLogger(RewriteCommand.class);
private static final String COMMAND_NAME = "rewrite";
private static final String MESSAGE_OPTION = "message";
private static final String TONE_OPTION = "tone";

private static final int MAX_MESSAGE_LENGTH = Message.MAX_CONTENT_LENGTH;
private static final int MIN_MESSAGE_LENGTH = 3;

private final ChatGptService chatGptService;

private static ChatGptModel selectAiModel(MessageTone tone) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we move all private static functions to the bottom of the class please? Naturally, we expect to see the constructor first.

return switch (tone) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: Just add the ChatGptModel to the enum and remove this entire function.

case CLEAR, PROFESSIONAL -> ChatGptModel.FASTEST;
case DETAILED, TECHNICAL -> ChatGptModel.HIGH_QUALITY;
};
}

private static String createAiPrompt(String userMessage, MessageTone tone) {
return """
You are rewriting a Discord text chat message for clarity and professionalism.
Keep it conversational and casual—NOT email or formal document format.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep it conversational and casual, not email or formal document format.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, let's move this out into a constant e.g.

private static final String AI_REWRITE_PROMPT_TEMPLATE = """
        You are rewriting a Discord text chat message for clarity and professionalism.
        Keep it conversational and casual, not email or formal document format.

        Tone: %s

        Rewrite the message to:
        - Improve clarity and structure
        - Maintain the original meaning
        - Avoid em-dashes (—)
        - Stay under %d characters (strict limit)

        If the message is already well-written, make only minor improvements.

        Message to rewrite:
        %s
        """.stripIndent();

...

private static String createAiPrompt(String userMessage, MessageTone tone) {
    return AI_REWRITE_PROMPT_TEMPLATE.formatted(
            tone.description,
            MAX_MESSAGE_LENGTH,
            userMessage
    );
}

Tone: %s
Rewrite the message to:
- Improve clarity and structure
- Maintain the original meaning
- Avoid em-dashes (—)
- Stay under %d characters (strict limit)
Copy link
Contributor

@tj-wazei tj-wazei Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stay under %d characters (strict limit) can be enforced via the MAX_TOKENS field in ChatGptService. Consider adding a parameter to the ask method. If doing so, ensure to validate that the value <= Message.MAX_TOKENS

If the message is already well-written, make only minor improvements.
Message to rewrite:
%s""".stripIndent().formatted(tone.description, MAX_MESSAGE_LENGTH, userMessage);
}

/**
* Creates the slash command definition and configures available options for rewriting messages.
*
* @param chatGptService service for interacting with ChatGPT
*/
public RewriteCommand(ChatGptService chatGptService) {
super(COMMAND_NAME, "Let AI rephrase and improve your message", CommandVisibility.GUILD);

this.chatGptService = chatGptService;

final OptionData messageOption =
new OptionData(OptionType.STRING, MESSAGE_OPTION, "The message you want to rewrite",
true)
.setMinLength(MIN_MESSAGE_LENGTH)
.setMaxLength(MAX_MESSAGE_LENGTH);

final OptionData toneOption = new OptionData(OptionType.STRING, TONE_OPTION,
"The tone/style for the rewritten message (default: "
+ MessageTone.CLEAR.displayName + ")",
false);

Arrays.stream(MessageTone.values())
.forEach(tone -> toneOption.addChoice(tone.displayName, tone.name()));

getData().addOptions(messageOption, toneOption);
}

@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {

final OptionMapping messageOption = event.getOption(MESSAGE_OPTION);

if (messageOption == null) {
throw new IllegalStateException("Required option '" + MESSAGE_OPTION + "' is missing");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: technically correct but semantically, I'd prefer IllegalArgumentException.

}

final String userMessage = messageOption.getAsString();
final MessageTone tone = parseTone(event.getOption(TONE_OPTION));

event.deferReply(true).queue();

Optional<String> rewrittenMessage = rewrite(userMessage, tone);

if (rewrittenMessage.isEmpty()) {
logger.debug("Failed to obtain a response for /{}, original message: '{}'",
COMMAND_NAME, userMessage);

event.getHook()
.editOriginal(
"An error occurred while processing your request. Please try again later.")
.queue();

return;
}

final String rewrittenText = rewrittenMessage.orElseThrow();

logger.debug("Rewrite successful; rewritten message length: {}", rewrittenText.length());

event.getHook().sendMessage(rewrittenText).setEphemeral(true).queue();
}

private MessageTone parseTone(@Nullable OptionMapping toneOption)
throws IllegalArgumentException {

if (toneOption == null) {
logger.debug("Tone option not provided, using default '{}'", MessageTone.CLEAR.name());
return MessageTone.CLEAR;
}

final String toneValue = toneOption.getAsString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be inlined i.e. return MessageTone.valueOf(toneOption.getAsString());


return MessageTone.valueOf(toneValue);
}

private Optional<String> rewrite(String userMessage, MessageTone tone) {

final ChatGptModel aiModel = selectAiModel(tone);

final String rewritePrompt = createAiPrompt(userMessage, tone);

Optional<String> attempt = chatGptService.askRaw(rewritePrompt, aiModel);

if (attempt.isEmpty()) {
return attempt;
}

final String response = attempt.get();

if (response.length() <= Message.MAX_CONTENT_LENGTH) {
return attempt;
}

logger.debug("Rewritten message exceeded {} characters; retrying with stricter constraint",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of this code can be removed if you use the max token parameter.

This method can then be:

private Optional<String> rewrite(String userMessage, MessageTone tone) {
  return chatGptService.askRaw(createAiPrompt(userMessage, tone), selectAiModel(tone)); // if updating askRaw pass the MAX_CONTENT_LENGTH as a parameter
}

MAX_MESSAGE_LENGTH);

final String shortenPrompt = rewritePrompt
+ "\n\nConstraint reminder: Your previous rewrite exceeded " + MAX_MESSAGE_LENGTH
+ " characters. Provide a revised rewrite strictly under " + MAX_MESSAGE_LENGTH
+ " characters while preserving meaning and tone.";

return chatGptService.askRaw(shortenPrompt, aiModel);
}

private enum MessageTone {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned earlier, you can add the ChatGptModel here and entirely remove the selectAiModel method

CLEAR("Clear", "Make it clear and easy to understand."),
PROFESSIONAL("Professional", "Use a professional and polished tone."),
DETAILED("Detailed", "Expand with more detail and explanation."),
TECHNICAL("Technical", "Use technical and specialized language where appropriate.");

private final String displayName;
private final String description;

MessageTone(String displayName, String description) {
this.displayName = displayName;
this.description = description;
}
}
}
Loading