Enha: do not remove tag

This commit is contained in:
Grail Finder
2026-01-17 12:28:19 +03:00
parent ec2d1c05ac
commit fd84dd5826
2 changed files with 136 additions and 131 deletions

26
bot.go
View File

@@ -71,9 +71,9 @@ var (
// parseKnownToTag extracts known_to list from content using configured tag. // parseKnownToTag extracts known_to list from content using configured tag.
// Returns cleaned content and list of character names. // Returns cleaned content and list of character names.
func parseKnownToTag(content string) (string, []string) { func parseKnownToTag(content string) []string {
if cfg == nil || !cfg.CharSpecificContextEnabled { if cfg == nil || !cfg.CharSpecificContextEnabled {
return content, nil return nil
} }
tag := cfg.CharSpecificContextTag tag := cfg.CharSpecificContextTag
if tag == "" { if tag == "" {
@@ -84,17 +84,15 @@ func parseKnownToTag(content string) (string, []string) {
re := regexp.MustCompile(pattern) re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(content, -1) matches := re.FindAllStringSubmatch(content, -1)
if len(matches) == 0 { if len(matches) == 0 {
return content, nil return nil
} }
// There may be multiple tags; we combine all. // There may be multiple tags; we combine all.
var knownTo []string var knownTo []string
cleaned := content
for _, match := range matches { for _, match := range matches {
if len(match) < 2 { if len(match) < 2 {
continue continue
} }
// Remove the entire matched tag from content // Remove the entire matched tag from content
cleaned = strings.Replace(cleaned, match[0], "", 1)
list := strings.TrimSpace(match[1]) list := strings.TrimSpace(match[1])
if list == "" { if list == "" {
continue continue
@@ -108,7 +106,7 @@ func parseKnownToTag(content string) (string, []string) {
} }
} }
// Also remove any leftover trailing "__" that might be orphaned? Not needed. // Also remove any leftover trailing "__" that might be orphaned? Not needed.
return strings.TrimSpace(cleaned), knownTo return knownTo
} }
// processMessageTag processes a message for known_to tag and sets KnownTo field. // processMessageTag processes a message for known_to tag and sets KnownTo field.
@@ -120,11 +118,8 @@ func processMessageTag(msg models.RoleMsg) models.RoleMsg {
} }
// If KnownTo already set, assume tag already processed (content cleaned). // If KnownTo already set, assume tag already processed (content cleaned).
// However, we still check for new tags (maybe added later). // However, we still check for new tags (maybe added later).
cleaned, knownTo := parseKnownToTag(msg.Content) knownTo := parseKnownToTag(msg.Content)
logger.Info("processing tags", "msg", msg.Content, "known_to", knownTo) logger.Info("processing tags", "msg", msg.Content, "known_to", knownTo)
if cleaned != msg.Content {
msg.Content = cleaned
}
// If tag found, replace KnownTo with new list (merge with existing?) // If tag found, replace KnownTo with new list (merge with existing?)
// For simplicity, if knownTo is not nil, replace. // For simplicity, if knownTo is not nil, replace.
if knownTo != nil { if knownTo != nil {
@@ -789,10 +784,17 @@ out:
if resume { if resume {
chatBody.Messages[len(chatBody.Messages)-1].Content += respText.String() chatBody.Messages[len(chatBody.Messages)-1].Content += respText.String()
// lastM.Content = lastM.Content + respText.String() // lastM.Content = lastM.Content + respText.String()
// Process the updated message to check for known_to tags in resumed response
updatedMsg := chatBody.Messages[len(chatBody.Messages)-1]
processedMsg := processMessageTag(updatedMsg)
chatBody.Messages[len(chatBody.Messages)-1] = processedMsg
} else { } else {
chatBody.Messages = append(chatBody.Messages, models.RoleMsg{ newMsg := models.RoleMsg{
Role: botPersona, Content: respText.String(), Role: botPersona, Content: respText.String(),
}) }
// Process the new message to check for known_to tags in LLM response
newMsg = processMessageTag(newMsg)
chatBody.Messages = append(chatBody.Messages, newMsg)
} }
logger.Debug("chatRound: before cleanChatBody", "messages_before_clean", len(chatBody.Messages)) logger.Debug("chatRound: before cleanChatBody", "messages_before_clean", len(chatBody.Messages))
for i, msg := range chatBody.Messages { for i, msg := range chatBody.Messages {

View File

@@ -387,13 +387,7 @@ func TestParseKnownToTag(t *testing.T) {
CharSpecificContextTag: tt.tag, CharSpecificContextTag: tt.tag,
} }
cfg = testCfg cfg = testCfg
knownTo := parseKnownToTag(tt.content)
cleaned, knownTo := parseKnownToTag(tt.content)
if cleaned != tt.wantCleaned {
t.Errorf("parseKnownToTag() cleaned = %q, want %q", cleaned, tt.wantCleaned)
}
if len(knownTo) != len(tt.wantKnownTo) { if len(knownTo) != len(tt.wantKnownTo) {
t.Errorf("parseKnownToTag() knownTo length = %v, want %v", len(knownTo), len(tt.wantKnownTo)) t.Errorf("parseKnownToTag() knownTo length = %v, want %v", len(knownTo), len(tt.wantKnownTo))
t.Logf("got: %v", knownTo) t.Logf("got: %v", knownTo)
@@ -488,6 +482,21 @@ func TestProcessMessageTag(t *testing.T) {
KnownTo: []string{"Bob", "Alice"}, KnownTo: []string{"Bob", "Alice"},
}, },
}, },
{
name: "example from real use",
msg: models.RoleMsg{
Role: "Alice",
Content: "I'll start with a simple one! The word is 'banana'. (ooc: __known_to_chars__Bob__)",
KnownTo: []string{"Alice"}, // from previous processing
},
enabled: true,
tag: "__known_to_chars__",
wantMsg: models.RoleMsg{
Role: "Alice",
Content: "I'll start with a simple one! The word is 'banana'. (ooc: __known_to_chars__Bob__)",
KnownTo: []string{"Bob", "Alice"},
},
},
} }
for _, tt := range tests { for _, tt := range tests {
@@ -497,13 +506,7 @@ func TestProcessMessageTag(t *testing.T) {
CharSpecificContextTag: tt.tag, CharSpecificContextTag: tt.tag,
} }
cfg = testCfg cfg = testCfg
got := processMessageTag(tt.msg) got := processMessageTag(tt.msg)
if got.Content != tt.wantMsg.Content {
t.Errorf("processMessageTag() content = %q, want %q", got.Content, tt.wantMsg.Content)
}
if len(got.KnownTo) != len(tt.wantMsg.KnownTo) { if len(got.KnownTo) != len(tt.wantMsg.KnownTo) {
t.Errorf("processMessageTag() KnownTo length = %v, want %v", len(got.KnownTo), len(tt.wantMsg.KnownTo)) t.Errorf("processMessageTag() KnownTo length = %v, want %v", len(got.KnownTo), len(tt.wantMsg.KnownTo))
t.Logf("got: %v", got.KnownTo) t.Logf("got: %v", got.KnownTo)