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

View File

@@ -118,34 +118,34 @@ func TestConsolidateConsecutiveAssistantMessages(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := consolidateConsecutiveAssistantMessages(tt.input)
if len(result) != len(tt.expected) {
t.Errorf("Expected %d messages, got %d", len(tt.expected), len(result))
t.Logf("Result: %+v", result)
t.Logf("Expected: %+v", tt.expected)
return
}
for i, expectedMsg := range tt.expected {
if i >= len(result) {
t.Errorf("Result has fewer messages than expected at index %d", i)
continue
}
actualMsg := result[i]
if actualMsg.Role != expectedMsg.Role {
t.Errorf("Message %d: expected role '%s', got '%s'", i, expectedMsg.Role, actualMsg.Role)
}
if actualMsg.Content != expectedMsg.Content {
t.Errorf("Message %d: expected content '%s', got '%s'", i, expectedMsg.Content, actualMsg.Content)
}
if actualMsg.ToolCallID != expectedMsg.ToolCallID {
t.Errorf("Message %d: expected ToolCallID '%s', got '%s'", i, expectedMsg.ToolCallID, actualMsg.ToolCallID)
}
}
// Additional check: ensure no messages were lost
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("Result does not match expected:\nResult: %+v\nExpected: %+v", result, tt.expected)
@@ -290,92 +290,92 @@ func TestConvertJSONToMapStringString(t *testing.T) {
func TestParseKnownToTag(t *testing.T) {
tests := []struct {
name string
content string
enabled bool
tag string
wantCleaned string
wantKnownTo []string
name string
content string
enabled bool
tag string
wantCleaned string
wantKnownTo []string
}{
{
name: "feature disabled returns original",
content: "Hello __known_to_chars__Alice__",
enabled: false,
tag: "__known_to_chars__",
wantCleaned: "Hello __known_to_chars__Alice__",
wantKnownTo: nil,
name: "feature disabled returns original",
content: "Hello __known_to_chars__Alice__",
enabled: false,
tag: "__known_to_chars__",
wantCleaned: "Hello __known_to_chars__Alice__",
wantKnownTo: nil,
},
{
name: "no tag returns original",
content: "Hello Alice",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello Alice",
wantKnownTo: nil,
name: "no tag returns original",
content: "Hello Alice",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello Alice",
wantKnownTo: nil,
},
{
name: "single tag with one char",
content: "Hello __known_to_chars__Alice__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
name: "single tag with one char",
content: "Hello __known_to_chars__Alice__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
},
{
name: "single tag with two chars",
content: "Secret __known_to_chars__Alice,Bob__ message",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Secret message",
wantKnownTo: []string{"Alice", "Bob"},
name: "single tag with two chars",
content: "Secret __known_to_chars__Alice,Bob__ message",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Secret message",
wantKnownTo: []string{"Alice", "Bob"},
},
{
name: "tag at beginning",
content: "__known_to_chars__Alice__ Hello",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
name: "tag at beginning",
content: "__known_to_chars__Alice__ Hello",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
},
{
name: "tag at end",
content: "Hello __known_to_chars__Alice__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
name: "tag at end",
content: "Hello __known_to_chars__Alice__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Hello",
wantKnownTo: []string{"Alice"},
},
{
name: "multiple tags",
content: "First __known_to_chars__Alice__ then __known_to_chars__Bob__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "First then",
wantKnownTo: []string{"Alice", "Bob"},
name: "multiple tags",
content: "First __known_to_chars__Alice__ then __known_to_chars__Bob__",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "First then",
wantKnownTo: []string{"Alice", "Bob"},
},
{
name: "custom tag",
content: "Secret __secret__Alice,Bob__ message",
enabled: true,
tag: "__secret__",
wantCleaned: "Secret message",
wantKnownTo: []string{"Alice", "Bob"},
name: "custom tag",
content: "Secret __secret__Alice,Bob__ message",
enabled: true,
tag: "__secret__",
wantCleaned: "Secret message",
wantKnownTo: []string{"Alice", "Bob"},
},
{
name: "empty list",
content: "Secret __known_to_chars____",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Secret",
wantKnownTo: nil,
name: "empty list",
content: "Secret __known_to_chars____",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "Secret",
wantKnownTo: nil,
},
{
name: "whitespace around commas",
content: "__known_to_chars__ Alice , Bob , Carl __",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "",
wantKnownTo: []string{"Alice", "Bob", "Carl"},
name: "whitespace around commas",
content: "__known_to_chars__ Alice , Bob , Carl __",
enabled: true,
tag: "__known_to_chars__",
wantCleaned: "",
wantKnownTo: []string{"Alice", "Bob", "Carl"},
},
}
@@ -387,13 +387,7 @@ func TestParseKnownToTag(t *testing.T) {
CharSpecificContextTag: tt.tag,
}
cfg = testCfg
cleaned, knownTo := parseKnownToTag(tt.content)
if cleaned != tt.wantCleaned {
t.Errorf("parseKnownToTag() cleaned = %q, want %q", cleaned, tt.wantCleaned)
}
knownTo := parseKnownToTag(tt.content)
if len(knownTo) != len(tt.wantKnownTo) {
t.Errorf("parseKnownToTag() knownTo length = %v, want %v", len(knownTo), len(tt.wantKnownTo))
t.Logf("got: %v", knownTo)
@@ -411,11 +405,11 @@ func TestParseKnownToTag(t *testing.T) {
func TestProcessMessageTag(t *testing.T) {
tests := []struct {
name string
msg models.RoleMsg
enabled bool
tag string
wantMsg models.RoleMsg
name string
msg models.RoleMsg
enabled bool
tag string
wantMsg models.RoleMsg
}{
{
name: "feature disabled returns unchanged",
@@ -488,6 +482,21 @@ func TestProcessMessageTag(t *testing.T) {
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 {
@@ -497,13 +506,7 @@ func TestProcessMessageTag(t *testing.T) {
CharSpecificContextTag: tt.tag,
}
cfg = testCfg
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) {
t.Errorf("processMessageTag() KnownTo length = %v, want %v", len(got.KnownTo), len(tt.wantMsg.KnownTo))
t.Logf("got: %v", got.KnownTo)
@@ -530,7 +533,7 @@ func TestProcessMessageTag(t *testing.T) {
func TestFilterMessagesForCharacter(t *testing.T) {
messages := []models.RoleMsg{
{Role: "system", Content: "System message", KnownTo: nil}, // visible to all
{Role: "Alice", Content: "Hello everyone", KnownTo: nil}, // visible to all
{Role: "Alice", Content: "Hello everyone", KnownTo: nil}, // visible to all
{Role: "Alice", Content: "Secret for Bob", KnownTo: []string{"Alice", "Bob"}},
{Role: "Bob", Content: "Reply to Alice", KnownTo: []string{"Alice", "Bob"}},
{Role: "Alice", Content: "Private to Carl", KnownTo: []string{"Alice", "Carl"}},
@@ -538,46 +541,46 @@ func TestFilterMessagesForCharacter(t *testing.T) {
}
tests := []struct {
name string
enabled bool
character string
name string
enabled bool
character string
wantIndices []int // indices from original messages that should be included
}{
{
name: "feature disabled returns all",
enabled: false,
character: "Alice",
wantIndices: []int{0,1,2,3,4,5},
name: "feature disabled returns all",
enabled: false,
character: "Alice",
wantIndices: []int{0, 1, 2, 3, 4, 5},
},
{
name: "character empty returns all",
enabled: true,
character: "",
wantIndices: []int{0,1,2,3,4,5},
name: "character empty returns all",
enabled: true,
character: "",
wantIndices: []int{0, 1, 2, 3, 4, 5},
},
{
name: "Alice sees all including Carl-private",
enabled: true,
character: "Alice",
wantIndices: []int{0,1,2,3,4,5},
name: "Alice sees all including Carl-private",
enabled: true,
character: "Alice",
wantIndices: []int{0, 1, 2, 3, 4, 5},
},
{
name: "Bob sees Alice-Bob secrets and all public",
enabled: true,
character: "Bob",
wantIndices: []int{0,1,2,3,5},
name: "Bob sees Alice-Bob secrets and all public",
enabled: true,
character: "Bob",
wantIndices: []int{0, 1, 2, 3, 5},
},
{
name: "Carl sees Alice-Carl secret and public",
enabled: true,
character: "Carl",
wantIndices: []int{0,1,4,5},
name: "Carl sees Alice-Carl secret and public",
enabled: true,
character: "Carl",
wantIndices: []int{0, 1, 4, 5},
},
{
name: "David sees only public messages",
enabled: true,
character: "David",
wantIndices: []int{0,1,5},
name: "David sees only public messages",
enabled: true,
character: "David",
wantIndices: []int{0, 1, 5},
},
}
@@ -604,4 +607,4 @@ func TestFilterMessagesForCharacter(t *testing.T) {
}
})
}
}
}