Centralize message role invariants
This commit is contained in:
78
internal/domain/message.go
Normal file
78
internal/domain/message.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
RoleDeveloper = "developer"
|
||||
RoleSystem = "system"
|
||||
RoleUser = "user"
|
||||
RoleAssistant = "assistant"
|
||||
)
|
||||
|
||||
// NormalizeMessageRole validates and canonicalizes a provider-bound chat role.
|
||||
func NormalizeMessageRole(role string) (string, error) {
|
||||
if !utf8.ValidString(role) {
|
||||
return "", errors.New("message role must be valid UTF-8")
|
||||
}
|
||||
|
||||
normalized := strings.ToLower(strings.TrimSpace(role))
|
||||
switch normalized {
|
||||
case RoleDeveloper, RoleSystem, RoleUser, RoleAssistant:
|
||||
return normalized, nil
|
||||
default:
|
||||
return "", errors.New("message role must be developer, system, user, or assistant")
|
||||
}
|
||||
}
|
||||
|
||||
// NormalizeCacheControl validates, canonicalizes, and copies cache metadata.
|
||||
func NormalizeCacheControl(control *CacheControl) (*CacheControl, error) {
|
||||
if control == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if !utf8.ValidString(string(control.Type)) {
|
||||
return nil, errors.New("cache control type must be valid UTF-8")
|
||||
}
|
||||
if !utf8.ValidString(control.TTL) {
|
||||
return nil, errors.New("cache control ttl must be valid UTF-8")
|
||||
}
|
||||
|
||||
cacheType := strings.TrimSpace(string(control.Type))
|
||||
if cacheType == "" {
|
||||
return nil, errors.New("cache control type is required")
|
||||
}
|
||||
if CacheControlType(cacheType) != CacheControlEphemeral {
|
||||
return nil, errors.New("unsupported type")
|
||||
}
|
||||
|
||||
ttl := strings.TrimSpace(control.TTL)
|
||||
if ttl != "" && ttl != "1h" {
|
||||
return nil, errors.New("unsupported ttl")
|
||||
}
|
||||
|
||||
return &CacheControl{Type: CacheControlType(cacheType), TTL: ttl}, nil
|
||||
}
|
||||
|
||||
// CloneRenderedMessages returns a deep copy of rendered messages.
|
||||
func CloneRenderedMessages(messages []RenderedMessage) []RenderedMessage {
|
||||
cloned := make([]RenderedMessage, len(messages))
|
||||
for index, message := range messages {
|
||||
cloned[index] = message
|
||||
if message.CacheControl != nil {
|
||||
cacheControl := *message.CacheControl
|
||||
cloned[index].CacheControl = &cacheControl
|
||||
}
|
||||
}
|
||||
return cloned
|
||||
}
|
||||
|
||||
// ConcatRenderedMessages returns an independently owned concatenation of messages.
|
||||
func ConcatRenderedMessages(prefix, suffix []RenderedMessage) []RenderedMessage {
|
||||
messages := make([]RenderedMessage, 0, len(prefix)+len(suffix))
|
||||
messages = append(messages, CloneRenderedMessages(prefix)...)
|
||||
messages = append(messages, CloneRenderedMessages(suffix)...)
|
||||
return messages
|
||||
}
|
||||
121
internal/domain/message_test.go
Normal file
121
internal/domain/message_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeMessageRole(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "developer", input: RoleDeveloper, want: RoleDeveloper},
|
||||
{name: "system", input: RoleSystem, want: RoleSystem},
|
||||
{name: "user", input: RoleUser, want: RoleUser},
|
||||
{name: "assistant", input: RoleAssistant, want: RoleAssistant},
|
||||
{name: "surrounding whitespace and mixed case", input: " \u2003UsEr\u2003 ", want: RoleUser},
|
||||
{name: "blank", input: " \t\n ", wantErr: true},
|
||||
{name: "tool", input: "tool", wantErr: true},
|
||||
{name: "function", input: "function", wantErr: true},
|
||||
{name: "custom", input: "custom-role", wantErr: true},
|
||||
{name: "invalid UTF-8", input: string([]byte{0xff}), wantErr: true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := NormalizeMessageRole(test.input)
|
||||
if test.wantErr {
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
if strings.Contains(err.Error(), test.input) {
|
||||
t.Fatalf("error exposed the input: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("NormalizeMessageRole() error = %v", err)
|
||||
}
|
||||
if got != test.want {
|
||||
t.Fatalf("NormalizeMessageRole() = %q, want %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeCacheControl(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input *CacheControl
|
||||
want *CacheControl
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "nil", input: nil, want: nil},
|
||||
{
|
||||
name: "canonical values",
|
||||
input: &CacheControl{Type: CacheControlEphemeral, TTL: "1h"},
|
||||
want: &CacheControl{Type: CacheControlEphemeral, TTL: "1h"},
|
||||
},
|
||||
{
|
||||
name: "trims values",
|
||||
input: &CacheControl{Type: " ephemeral ", TTL: " 1h\t"},
|
||||
want: &CacheControl{Type: CacheControlEphemeral, TTL: "1h"},
|
||||
},
|
||||
{name: "empty type", input: &CacheControl{}, wantErr: true},
|
||||
{name: "unsupported type", input: &CacheControl{Type: "persistent"}, wantErr: true},
|
||||
{name: "unsupported ttl", input: &CacheControl{Type: CacheControlEphemeral, TTL: "5m"}, wantErr: true},
|
||||
{name: "invalid type UTF-8", input: &CacheControl{Type: CacheControlType(string([]byte{0xff}))}, wantErr: true},
|
||||
{name: "invalid ttl UTF-8", input: &CacheControl{Type: CacheControlEphemeral, TTL: string([]byte{0xff})}, wantErr: true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := NormalizeCacheControl(test.input)
|
||||
if test.wantErr {
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("NormalizeCacheControl() error = %v", err)
|
||||
}
|
||||
if got == nil || test.want == nil {
|
||||
if got != test.want {
|
||||
t.Fatalf("NormalizeCacheControl() = %#v, want %#v", got, test.want)
|
||||
}
|
||||
return
|
||||
}
|
||||
if *got != *test.want {
|
||||
t.Fatalf("NormalizeCacheControl() = %#v, want %#v", got, test.want)
|
||||
}
|
||||
if got == test.input {
|
||||
t.Fatal("normalized cache control aliases its input")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderedMessageCloning(t *testing.T) {
|
||||
prefix := []RenderedMessage{{Role: RoleSystem, Content: "prefix", CacheControl: &CacheControl{Type: CacheControlEphemeral, TTL: "1h"}}}
|
||||
suffix := []RenderedMessage{{Role: RoleUser, Content: " suffix "}, {Role: RoleAssistant, Content: "", CacheControl: &CacheControl{Type: CacheControlEphemeral}}}
|
||||
|
||||
cloned := CloneRenderedMessages(prefix)
|
||||
combined := ConcatRenderedMessages(prefix, suffix)
|
||||
if len(combined) != 3 || combined[0].Content != "prefix" || combined[1].Content != " suffix " || combined[2].Content != "" {
|
||||
t.Fatalf("unexpected combined messages: %#v", combined)
|
||||
}
|
||||
if cloned[0].CacheControl == prefix[0].CacheControl || combined[0].CacheControl == prefix[0].CacheControl || combined[2].CacheControl == suffix[1].CacheControl {
|
||||
t.Fatal("cloned cache controls alias their inputs")
|
||||
}
|
||||
|
||||
prefix[0].Content = "changed"
|
||||
prefix[0].CacheControl.TTL = ""
|
||||
suffix[1].CacheControl.Type = "changed"
|
||||
if cloned[0].Content != "prefix" || cloned[0].CacheControl.TTL != "1h" || combined[0].Content != "prefix" || combined[0].CacheControl.TTL != "1h" || combined[2].CacheControl.Type != CacheControlEphemeral {
|
||||
t.Fatalf("cloned messages changed with their inputs: cloned=%#v combined=%#v", cloned, combined)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user