feat(audit): persist message source and token usage
This commit is contained in:
@@ -150,6 +150,10 @@ func (m *MessageAttachments) Scan(value interface{}) error {
|
||||
// MentionedItems is a slice of MentionedItem for database storage
|
||||
type MentionedItems []MentionedItem
|
||||
|
||||
// MessageKnowledgeBaseIDs stores the knowledge-base scope searched for a message.
|
||||
// It is kept even when retrieval returns no references so the request remains auditable.
|
||||
type MessageKnowledgeBaseIDs []string
|
||||
|
||||
// Value implements the driver.Valuer interface for database serialization
|
||||
func (m MentionedItems) Value() (driver.Value, error) {
|
||||
if m == nil {
|
||||
@@ -216,6 +220,22 @@ type Message struct {
|
||||
RenderedContent string `json:"-" gorm:"type:text;column:rendered_content;default:''"`
|
||||
// Channel indicates the source channel of this message (e.g., "web", "api", "im")
|
||||
Channel string `json:"channel,omitempty" gorm:"type:varchar(50);default:''"`
|
||||
// SourcePlatform is the concrete external platform for IM messages (for example, dingtalk).
|
||||
SourcePlatform string `json:"source_platform,omitempty" gorm:"type:varchar(20);default:''"`
|
||||
// SourceChannelID identifies the configured WeKnora IM channel that received the message.
|
||||
SourceChannelID string `json:"source_channel_id,omitempty" gorm:"type:varchar(36);default:''"`
|
||||
// SourceMessageID is the platform message identifier used for end-to-end correlation.
|
||||
SourceMessageID string `json:"source_message_id,omitempty" gorm:"type:varchar(128);default:''"`
|
||||
// SourceUserID records the platform user for this individual message.
|
||||
SourceUserID string `json:"source_user_id,omitempty" gorm:"type:varchar(128);default:''"`
|
||||
// SourceChatID records the platform conversation or group identifier.
|
||||
SourceChatID string `json:"source_chat_id,omitempty" gorm:"type:varchar(128);default:''"`
|
||||
// SourceThreadID records the platform thread identifier when present.
|
||||
SourceThreadID string `json:"source_thread_id,omitempty" gorm:"type:varchar(128);default:''"`
|
||||
// SearchedKnowledgeBaseIDs records the request scope even when retrieval has zero hits.
|
||||
SearchedKnowledgeBaseIDs MessageKnowledgeBaseIDs `json:"searched_knowledge_base_ids,omitempty" gorm:"serializer:json;type:jsonb;column:searched_knowledge_base_ids"`
|
||||
// TokenUsage records answer-generation model usage for the assistant message.
|
||||
TokenUsage *TokenUsage `json:"token_usage,omitempty" gorm:"serializer:json;type:jsonb;column:token_usage"`
|
||||
// KnowledgeID links this message to a Knowledge entry in the chat history knowledge base
|
||||
// Used for vector search indexing: when set, the message content has been indexed as a Knowledge passage
|
||||
KnowledgeID string `json:"knowledge_id,omitempty" gorm:"type:varchar(36);index"`
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMessageAuditMigrationCoversModelFields(t *testing.T) {
|
||||
upPath := "../../migrations/versioned/000064_message_audit.up.sql"
|
||||
downPath := "../../migrations/versioned/000064_message_audit.down.sql"
|
||||
|
||||
up, err := os.ReadFile(upPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read up migration: %v", err)
|
||||
}
|
||||
down, err := os.ReadFile(downPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read down migration: %v", err)
|
||||
}
|
||||
|
||||
columns := []string{
|
||||
"source_platform",
|
||||
"source_channel_id",
|
||||
"source_message_id",
|
||||
"source_user_id",
|
||||
"source_chat_id",
|
||||
"source_thread_id",
|
||||
"searched_knowledge_base_ids",
|
||||
"token_usage",
|
||||
}
|
||||
upSQL := strings.ToLower(string(up))
|
||||
downSQL := strings.ToLower(string(down))
|
||||
for _, column := range columns {
|
||||
if !strings.Contains(upSQL, column) {
|
||||
t.Errorf("up migration missing column %q", column)
|
||||
}
|
||||
if !strings.Contains(downSQL, column) {
|
||||
t.Errorf("down migration missing column %q", column)
|
||||
}
|
||||
}
|
||||
|
||||
indexes := []string{
|
||||
"idx_messages_request_role",
|
||||
"idx_messages_source_channel",
|
||||
"idx_messages_source_message",
|
||||
"idx_messages_source_user",
|
||||
}
|
||||
for _, index := range indexes {
|
||||
if !strings.Contains(upSQL, index) {
|
||||
t.Errorf("up migration missing index %q", index)
|
||||
}
|
||||
if !strings.Contains(downSQL, index) {
|
||||
t.Errorf("down migration missing index %q", index)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMessageCarriesAuditMetadata(t *testing.T) {
|
||||
original := Message{
|
||||
ID: "message-1",
|
||||
SourcePlatform: "dingtalk",
|
||||
SourceChannelID: "channel-1",
|
||||
SourceMessageID: "external-message-1",
|
||||
SourceUserID: "user-1",
|
||||
SourceChatID: "chat-1",
|
||||
SourceThreadID: "thread-1",
|
||||
SearchedKnowledgeBaseIDs: MessageKnowledgeBaseIDs{"kb-1", "kb-2"},
|
||||
TokenUsage: &TokenUsage{
|
||||
PromptTokens: 100,
|
||||
CompletionTokens: 20,
|
||||
TotalTokens: 120,
|
||||
CachedTokens: 40,
|
||||
},
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(original)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal() error = %v", err)
|
||||
}
|
||||
|
||||
var decoded Message
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("json.Unmarshal() error = %v", err)
|
||||
}
|
||||
|
||||
if decoded.SourcePlatform != original.SourcePlatform ||
|
||||
decoded.SourceChannelID != original.SourceChannelID ||
|
||||
decoded.SourceMessageID != original.SourceMessageID ||
|
||||
decoded.SourceUserID != original.SourceUserID ||
|
||||
decoded.SourceChatID != original.SourceChatID ||
|
||||
decoded.SourceThreadID != original.SourceThreadID {
|
||||
t.Fatalf("source metadata was not preserved: got %+v", decoded)
|
||||
}
|
||||
if !reflect.DeepEqual(decoded.SearchedKnowledgeBaseIDs, original.SearchedKnowledgeBaseIDs) {
|
||||
t.Fatalf("searched knowledge bases = %#v, want %#v", decoded.SearchedKnowledgeBaseIDs, original.SearchedKnowledgeBaseIDs)
|
||||
}
|
||||
if !reflect.DeepEqual(decoded.TokenUsage, original.TokenUsage) {
|
||||
t.Fatalf("token usage = %#v, want %#v", decoded.TokenUsage, original.TokenUsage)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user