@@ -35,33 +35,25 @@ public final class RewriteCommand extends SlashCommandAdapter {
3535 private static final int MAX_MESSAGE_LENGTH = Message .MAX_CONTENT_LENGTH ;
3636 private static final int MIN_MESSAGE_LENGTH = 3 ;
3737
38- private final ChatGptService chatGptService ;
39-
40- private static ChatGptModel selectAiModel (MessageTone tone ) {
41- return switch (tone ) {
42- case CLEAR , PROFESSIONAL -> ChatGptModel .FASTEST ;
43- case DETAILED , TECHNICAL -> ChatGptModel .HIGH_QUALITY ;
44- };
45- }
38+ private static final String AI_REWRITE_PROMPT_TEMPLATE = """
39+ You are rewriting a Discord text chat message for clarity and professionalism.
40+ Keep it conversational and casual, not email or formal document format.
4641
47- private static String createAiPrompt (String userMessage , MessageTone tone ) {
48- return """
49- You are rewriting a Discord text chat message for clarity and professionalism.
50- Keep it conversational and casual—NOT email or formal document format.
42+ Tone: %s
5143
52- Tone: %s
44+ Rewrite the message to:
45+ - Improve clarity and structure
46+ - Maintain the original meaning
47+ - Avoid em-dashes (—)
48+ - Stay under %d characters (strict limit)
5349
54- Rewrite the message to:
55- - Improve clarity and structure
56- - Maintain the original meaning
57- - Avoid em-dashes (—)
58- - Stay under %d characters (strict limit)
50+ If the message is already well-written, make only minor improvements.
5951
60- If the message is already well-written, make only minor improvements.
52+ Message to rewrite:
53+ %s
54+ """ .stripIndent ();
6155
62- Message to rewrite:
63- %s""" .stripIndent ().formatted (tone .description , MAX_MESSAGE_LENGTH , userMessage );
64- }
56+ private final ChatGptService chatGptService ;
6557
6658 /**
6759 * Creates the slash command definition and configures available options for rewriting messages.
@@ -73,13 +65,13 @@ public RewriteCommand(ChatGptService chatGptService) {
7365
7466 this .chatGptService = chatGptService ;
7567
76- final OptionData messageOption =
68+ OptionData messageOption =
7769 new OptionData (OptionType .STRING , MESSAGE_OPTION , "The message you want to rewrite" ,
7870 true )
7971 .setMinLength (MIN_MESSAGE_LENGTH )
8072 .setMaxLength (MAX_MESSAGE_LENGTH );
8173
82- final OptionData toneOption = new OptionData (OptionType .STRING , TONE_OPTION ,
74+ OptionData toneOption = new OptionData (OptionType .STRING , TONE_OPTION ,
8375 "The tone/style for the rewritten message (default: "
8476 + MessageTone .CLEAR .displayName + ")" ,
8577 false );
@@ -93,14 +85,15 @@ public RewriteCommand(ChatGptService chatGptService) {
9385 @ Override
9486 public void onSlashCommand (SlashCommandInteractionEvent event ) {
9587
96- final OptionMapping messageOption = event .getOption (MESSAGE_OPTION );
88+ OptionMapping messageOption = event .getOption (MESSAGE_OPTION );
9789
9890 if (messageOption == null ) {
99- throw new IllegalStateException ("Required option '" + MESSAGE_OPTION + "' is missing" );
91+ throw new IllegalArgumentException (
92+ "Required option '" + MESSAGE_OPTION + "' is missing" );
10093 }
10194
102- final String userMessage = messageOption .getAsString ();
103- final MessageTone tone = parseTone (event .getOption (TONE_OPTION ));
95+ String userMessage = messageOption .getAsString ();
96+ MessageTone tone = parseTone (event .getOption (TONE_OPTION ));
10497
10598 event .deferReply (true ).queue ();
10699
@@ -118,7 +111,7 @@ public void onSlashCommand(SlashCommandInteractionEvent event) {
118111 return ;
119112 }
120113
121- final String rewrittenText = rewrittenMessage .orElseThrow ();
114+ String rewrittenText = rewrittenMessage .orElseThrow ();
122115
123116 logger .debug ("Rewrite successful; rewritten message length: {}" , rewrittenText .length ());
124117
@@ -133,52 +126,59 @@ private MessageTone parseTone(@Nullable OptionMapping toneOption)
133126 return MessageTone .CLEAR ;
134127 }
135128
136- final String toneValue = toneOption .getAsString ();
137-
138- return MessageTone .valueOf (toneValue );
129+ return MessageTone .valueOf (toneOption .getAsString ());
139130 }
140131
141132 private Optional <String > rewrite (String userMessage , MessageTone tone ) {
142133
143- final ChatGptModel aiModel = selectAiModel ( tone );
134+ String rewritePrompt = createAiPrompt ( userMessage , tone );
144135
145- final String rewritePrompt = createAiPrompt ( userMessage , tone ) ;
136+ ChatGptModel aiModel = tone . model ;
146137
147138 Optional <String > attempt = chatGptService .askRaw (rewritePrompt , aiModel );
148139
149140 if (attempt .isEmpty ()) {
150141 return attempt ;
151142 }
152143
153- final String response = attempt .get ();
144+ String response = attempt .get ();
154145
155- if (response .length () <= Message . MAX_CONTENT_LENGTH ) {
146+ if (response .length () <= MAX_MESSAGE_LENGTH ) {
156147 return attempt ;
157148 }
158149
159150 logger .debug ("Rewritten message exceeded {} characters; retrying with stricter constraint" ,
160151 MAX_MESSAGE_LENGTH );
161152
162- final String shortenPrompt = rewritePrompt
153+ String shortenPrompt = rewritePrompt
163154 + "\n \n Constraint reminder: Your previous rewrite exceeded " + MAX_MESSAGE_LENGTH
164155 + " characters. Provide a revised rewrite strictly under " + MAX_MESSAGE_LENGTH
165156 + " characters while preserving meaning and tone." ;
166157
167158 return chatGptService .askRaw (shortenPrompt , aiModel );
168159 }
169160
161+ private static String createAiPrompt (String userMessage , MessageTone tone ) {
162+ return AI_REWRITE_PROMPT_TEMPLATE .formatted (tone .description , MAX_MESSAGE_LENGTH ,
163+ userMessage );
164+ }
165+
170166 private enum MessageTone {
171- CLEAR ("Clear" , "Make it clear and easy to understand." ),
172- PROFESSIONAL ("Professional" , "Use a professional and polished tone." ),
173- DETAILED ("Detailed" , "Expand with more detail and explanation." ),
174- TECHNICAL ("Technical" , "Use technical and specialized language where appropriate." );
167+ CLEAR ("Clear" , "Make it clear and easy to understand." , ChatGptModel .FASTEST ),
168+ PROFESSIONAL ("Professional" , "Use a professional and polished tone." , ChatGptModel .FASTEST ),
169+ DETAILED ("Detailed" , "Expand with more detail and explanation." , ChatGptModel .HIGH_QUALITY ),
170+ TECHNICAL ("Technical" , "Use technical and specialized language where appropriate." ,
171+ ChatGptModel .HIGH_QUALITY );
175172
176173 private final String displayName ;
177174 private final String description ;
175+ private final ChatGptModel model ;
178176
179- MessageTone (String displayName , String description ) {
177+ MessageTone (String displayName , String description , ChatGptModel model ) {
180178 this .displayName = displayName ;
181179 this .description = description ;
180+ this .model = model ;
182181 }
183182 }
183+
184184}
0 commit comments