@@ -70,15 +70,17 @@ 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" };
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: .*$" );
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: .*$" );
82+ private static final Pattern SYSTEM_PROMPT_PERSONA_CAPTURE =
83+ Pattern .compile ("(?m)^Persona:\\ s*(.*)$" );
8284
8385 private final TextFieldSetting apiKey =
8486 new TextFieldSetting ("OpenAI API key" ,
@@ -196,6 +198,8 @@ public final class AutoChatHack extends Hack implements ChatInputListener
196198 private int inFlightRequests ;
197199 private volatile long lastReplyTime ;
198200 private volatile long lastUnsolicitedReplyTime ;
201+ private String lastPersonaSnapshot = "" ;
202+ private String lastCustomPromptSnapshot = "" ;
199203
200204 public AutoChatHack ()
201205 {
@@ -223,6 +227,9 @@ public AutoChatHack()
223227 addSetting (wordsPerMinute );
224228 addSetting (maxConcurrentRequests );
225229 addSetting (debugMode );
230+ lastPersonaSnapshot = persona .getValue ();
231+ lastCustomPromptSnapshot =
232+ normalizePromptText (customSystemPrompt .getValue ());
226233
227234 }
228235
@@ -827,14 +834,16 @@ private JsonObject buildChatCompletionsRequest(List<ChatLine> snapshot,
827834 return root ;
828835 }
829836
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- }
837+ private String buildSystemPrompt ()
838+ {
839+ syncPersonaAndPrompt ();
840+
841+ String custom = normalizePromptText (customSystemPrompt .getValue ());
842+ if (!custom .isBlank () && !isGeneratedDefaultPromptSnapshot (custom ))
843+ return custom ;
844+
845+ return buildDefaultSystemPrompt ();
846+ }
838847
839848 private String buildDefaultSystemPrompt ()
840849 {
@@ -868,64 +877,132 @@ public void openSystemPromptEditor()
868877 MC .setScreen (new AutoChatSystemPromptScreen (MC .screen , this ));
869878 }
870879
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- }
880+ public String getSystemPromptEditorText ()
881+ {
882+ syncPersonaAndPrompt ();
883+
884+ String custom = normalizePromptText (customSystemPrompt .getValue ());
885+ if (!custom .isBlank () && !isGeneratedDefaultPromptSnapshot (custom ))
886+ return custom ;
887+
888+ return buildDefaultSystemPrompt ();
889+ }
879890
880891 public String getGeneratedDefaultSystemPrompt ()
881892 {
882893 return buildDefaultSystemPrompt ();
883894 }
884895
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- }
896+ public void setCustomSystemPrompt (String prompt )
897+ {
898+ if (prompt == null )
899+ return ;
900+
901+ String normalized = normalizePromptText (prompt );
902+ if (normalized .isBlank () || isGeneratedDefaultPromptSnapshot (normalized ))
903+ {
904+ customSystemPrompt .setValue ("" );
905+ syncPersonaAndPrompt ();
906+ return ;
907+ }
908+
909+ customSystemPrompt .setValue (prompt );
910+ syncPersonaAndPrompt ();
911+ }
912+
913+ private void syncPersonaAndPrompt ()
914+ {
915+ String currentPersona = persona .getValue ();
916+ String currentPrompt =
917+ normalizePromptText (customSystemPrompt .getValue ());
918+ if (currentPrompt .isBlank ())
919+ {
920+ lastPersonaSnapshot = currentPersona ;
921+ lastCustomPromptSnapshot = currentPrompt ;
922+ return ;
923+ }
924+
925+ String promptPersona = extractPersonaFromPrompt (currentPrompt );
926+ if (promptPersona == null )
927+ {
928+ lastPersonaSnapshot = currentPersona ;
929+ lastCustomPromptSnapshot = currentPrompt ;
930+ return ;
931+ }
932+
933+ boolean personaChanged = !currentPersona .equals (lastPersonaSnapshot );
934+ boolean promptChanged = !currentPrompt .equals (lastCustomPromptSnapshot );
935+
936+ if (promptChanged && !personaChanged )
937+ {
938+ if (!currentPersona .equals (promptPersona ))
939+ persona .setValue (promptPersona );
940+ }else if (personaChanged && !promptChanged )
941+ {
942+ if (!promptPersona .equals (currentPersona ))
943+ {
944+ String synced =
945+ replacePersonaInPrompt (currentPrompt , currentPersona );
946+ customSystemPrompt .setValue (synced );
947+ }
948+ }else if (promptChanged && personaChanged )
949+ {
950+ if (!currentPersona .equals (promptPersona ))
951+ persona .setValue (promptPersona );
952+ }else if (!currentPersona .equals (promptPersona ))
953+ persona .setValue (promptPersona );
954+
955+ lastPersonaSnapshot = persona .getValue ();
956+ lastCustomPromptSnapshot =
957+ normalizePromptText (customSystemPrompt .getValue ());
958+ }
959+
960+ private static String extractPersonaFromPrompt (String prompt )
961+ {
962+ Matcher matcher =
963+ SYSTEM_PROMPT_PERSONA_CAPTURE .matcher (normalizePromptText (prompt ));
964+ if (!matcher .find ())
965+ return null ;
966+
967+ return matcher .group (1 ).strip ();
968+ }
969+
970+ private static String replacePersonaInPrompt (String prompt , String persona )
971+ {
972+ return SYSTEM_PROMPT_PERSONA_CAPTURE
973+ .matcher (normalizePromptText (prompt ))
974+ .replaceFirst ("Persona: " + Matcher .quoteReplacement (persona ));
975+ }
976+
977+ private static String normalizePromptText (String prompt )
978+ {
979+ if (prompt == null )
980+ return "" ;
981+
982+ return prompt .replace ("\r \n " , "\n " ).strip ();
983+ }
984+
985+ private boolean isGeneratedDefaultPromptSnapshot (String prompt )
986+ {
987+ String normalizedPrompt = normalizePromptText (prompt );
988+ if (normalizedPrompt .isBlank ())
989+ return false ;
990+
991+ String customSignature = toDefaultPromptSignature (normalizedPrompt );
992+ String defaultSignature =
993+ toDefaultPromptSignature (buildDefaultSystemPrompt ());
994+ return customSignature .equals (defaultSignature );
995+ }
996+
997+ private static String toDefaultPromptSignature (String prompt )
998+ {
999+ String signature = normalizePromptText (prompt );
1000+ signature = DEFAULT_PROMPT_USERNAME_LINE .matcher (signature )
1001+ .replaceFirst ("Your username: <dynamic>" );
1002+ signature = DEFAULT_PROMPT_PERSONA_LINE .matcher (signature )
1003+ .replaceFirst ("Persona: <dynamic>" );
1004+ return signature ;
1005+ }
9291006
9301007 private String buildUserPrompt (List <ChatLine > snapshot , ChatLine latest ,
9311008 boolean direct )
0 commit comments