@@ -70,10 +70,15 @@ public final class AutoChatHack extends Hack implements ChatInputListener
7070 private static final Pattern VALID_PLAYER_NAME =
7171 Pattern .compile ("^[A-Za-z0-9_]{1,16}$" );
7272
73- private static final String [] INJECTION_MARKERS =
74- {"ignore previous" , "ignore all previous" , "system prompt" ,
75- "developer message" , "reveal prompt" , "show prompt" , "jailbreak" ,
76- "dan mode" , "forget your instructions" , "new instructions" };
73+ private static final String [] INJECTION_MARKERS =
74+ {"ignore previous" , "ignore all previous" , "system prompt" ,
75+ "developer message" , "reveal prompt" , "show prompt" , "jailbreak" ,
76+ "dan mode" , "forget your instructions" , "new instructions" };
77+
78+ private static final Pattern DEFAULT_PROMPT_USERNAME_LINE =
79+ Pattern .compile ("(?m)^Your username: .*$" );
80+ private static final Pattern DEFAULT_PROMPT_PERSONA_LINE =
81+ Pattern .compile ("(?m)^Persona: .*$" );
7782
7883 private final TextFieldSetting apiKey =
7984 new TextFieldSetting ("OpenAI API key" ,
@@ -822,14 +827,14 @@ private JsonObject buildChatCompletionsRequest(List<ChatLine> snapshot,
822827 return root ;
823828 }
824829
825- private String buildSystemPrompt ()
826- {
827- String custom = customSystemPrompt .getValue ();
828- if (!custom .isBlank ())
829- return custom ;
830-
831- return buildDefaultSystemPrompt ();
832- }
830+ private String buildSystemPrompt ()
831+ {
832+ String custom = normalizePromptText ( customSystemPrompt .getValue () );
833+ if (!custom .isBlank () && ! isGeneratedDefaultPromptSnapshot ( custom ) )
834+ return custom ;
835+
836+ return buildDefaultSystemPrompt ();
837+ }
833838
834839 private String buildDefaultSystemPrompt ()
835840 {
@@ -863,27 +868,64 @@ public void openSystemPromptEditor()
863868 MC .setScreen (new AutoChatSystemPromptScreen (MC .screen , this ));
864869 }
865870
866- public String getSystemPromptEditorText ()
867- {
868- String custom = customSystemPrompt .getValue ();
869- if (!custom .isBlank ())
870- return custom ;
871-
872- return buildDefaultSystemPrompt ();
873- }
871+ public String getSystemPromptEditorText ()
872+ {
873+ String custom = normalizePromptText ( customSystemPrompt .getValue () );
874+ if (!custom .isBlank () && ! isGeneratedDefaultPromptSnapshot ( custom ) )
875+ return custom ;
876+
877+ return buildDefaultSystemPrompt ();
878+ }
874879
875880 public String getGeneratedDefaultSystemPrompt ()
876881 {
877882 return buildDefaultSystemPrompt ();
878883 }
879884
880- public void setCustomSystemPrompt (String prompt )
881- {
882- if (prompt == null )
883- return ;
884-
885- customSystemPrompt .setValue (prompt );
886- }
885+ public void setCustomSystemPrompt (String prompt )
886+ {
887+ if (prompt == null )
888+ return ;
889+
890+ String normalized = normalizePromptText (prompt );
891+ if (normalized .isBlank () || isGeneratedDefaultPromptSnapshot (normalized ))
892+ {
893+ customSystemPrompt .setValue ("" );
894+ return ;
895+ }
896+
897+ customSystemPrompt .setValue (prompt );
898+ }
899+
900+ private static String normalizePromptText (String prompt )
901+ {
902+ if (prompt == null )
903+ return "" ;
904+
905+ return prompt .replace ("\r \n " , "\n " ).strip ();
906+ }
907+
908+ private boolean isGeneratedDefaultPromptSnapshot (String prompt )
909+ {
910+ String normalizedPrompt = normalizePromptText (prompt );
911+ if (normalizedPrompt .isBlank ())
912+ return false ;
913+
914+ String customSignature = toDefaultPromptSignature (normalizedPrompt );
915+ String defaultSignature =
916+ toDefaultPromptSignature (buildDefaultSystemPrompt ());
917+ return customSignature .equals (defaultSignature );
918+ }
919+
920+ private static String toDefaultPromptSignature (String prompt )
921+ {
922+ String signature = normalizePromptText (prompt );
923+ signature = DEFAULT_PROMPT_USERNAME_LINE .matcher (signature )
924+ .replaceFirst ("Your username: <dynamic>" );
925+ signature = DEFAULT_PROMPT_PERSONA_LINE .matcher (signature )
926+ .replaceFirst ("Persona: <dynamic>" );
927+ return signature ;
928+ }
887929
888930 private String buildUserPrompt (List <ChatLine > snapshot , ChatLine latest ,
889931 boolean direct )
0 commit comments