diff --git a/internal/application/service/message.go b/internal/application/service/message.go index 85a721f2..26074725 100644 --- a/internal/application/service/message.go +++ b/internal/application/service/message.go @@ -384,6 +384,12 @@ func (s *messageService) IndexMessageToKB(ctx context.Context, userQuery string, cfg := s.getChatHistoryConfig(ctx) if cfg == nil { + // Say why, otherwise an operator who enabled indexing in the UI sees + // "indexed messages: 0" with nothing in the logs to explain it: the + // stats endpoint only checks Enabled, while indexing additionally + // requires an embedding model and an auto-created KB. + logger.Infof(ctx, "Skipping message index for message %s: %s", + messageID, describeChatHistorySkip(ctx)) return } @@ -410,6 +416,30 @@ func (s *messageService) IndexMessageToKB(ctx context.Context, userQuery string, logger.Infof(ctx, "Message indexed to chat history KB: knowledge_id=%s, message_id=%s", knowledge.ID, messageID) } +// describeChatHistorySkip reports which chat-history precondition is missing, +// for the log line emitted when indexing is skipped. ChatHistoryConfig. +// IsConfigured requires Enabled plus a selected embedding model plus an +// auto-created KB, so "enabled" alone is not enough to index. +func describeChatHistorySkip(ctx context.Context) string { + tenant, ok := types.TenantInfoFromContext(ctx) + if !ok { + return "no tenant in context" + } + cfg := tenant.ChatHistoryConfig + switch { + case cfg == nil: + return "chat history config not set" + case !cfg.Enabled: + return "chat history indexing disabled" + case cfg.EmbeddingModelID == "": + return "enabled but no embedding model selected" + case cfg.KnowledgeBaseID == "": + return "enabled but chat history knowledge base not created yet" + default: + return "chat history config incomplete" + } +} + // DeleteMessageKnowledge deletes the Knowledge entry associated with a message from the chat history KB. func (s *messageService) DeleteMessageKnowledge(ctx context.Context, knowledgeID string) { if knowledgeID == "" { diff --git a/internal/application/service/message_index_skip_test.go b/internal/application/service/message_index_skip_test.go new file mode 100644 index 00000000..2586d0f3 --- /dev/null +++ b/internal/application/service/message_index_skip_test.go @@ -0,0 +1,98 @@ +package service + +import ( + "context" + "testing" + + "github.com/Tencent/WeKnora/internal/types" +) + +// The stats endpoint reports the feature as enabled based on +// ChatHistoryConfig.Enabled alone, but IndexMessageToKB only indexes when +// IsConfigured() holds - which also requires an embedding model and an +// auto-created KB. When those disagree the operator sees "indexed messages: 0" +// with no log line explaining it. These cases pin the reason string so each +// skip is attributable from the logs. +func TestDescribeChatHistorySkip(t *testing.T) { + tests := []struct { + name string + ctx func() context.Context + want string + }{ + { + name: "no tenant in context", + ctx: func() context.Context { return context.Background() }, + want: "no tenant in context", + }, + { + name: "config not set", + ctx: func() context.Context { + return contextWithTenant(&types.Tenant{}) + }, + want: "chat history config not set", + }, + { + name: "disabled", + ctx: func() context.Context { + return contextWithTenant(&types.Tenant{ + ChatHistoryConfig: &types.ChatHistoryConfig{Enabled: false}, + }) + }, + want: "chat history indexing disabled", + }, + { + name: "enabled without embedding model", + ctx: func() context.Context { + return contextWithTenant(&types.Tenant{ + ChatHistoryConfig: &types.ChatHistoryConfig{ + Enabled: true, + KnowledgeBaseID: "kb-1", + }, + }) + }, + want: "enabled but no embedding model selected", + }, + { + name: "enabled without knowledge base", + ctx: func() context.Context { + return contextWithTenant(&types.Tenant{ + ChatHistoryConfig: &types.ChatHistoryConfig{ + Enabled: true, + EmbeddingModelID: "emb-1", + }, + }) + }, + want: "enabled but chat history knowledge base not created yet", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := describeChatHistorySkip(tt.ctx()); got != tt.want { + t.Errorf("describeChatHistorySkip() = %q, want %q", got, tt.want) + } + }) + } +} + +// A fully configured tenant must not be reported as a skip reason: it is the +// one case where IndexMessageToKB proceeds, so the fallback string here would +// mean the helper and IsConfigured() disagree. +func TestDescribeChatHistorySkipFullyConfigured(t *testing.T) { + cfg := &types.ChatHistoryConfig{ + Enabled: true, + EmbeddingModelID: "emb-1", + KnowledgeBaseID: "kb-1", + } + if !cfg.IsConfigured() { + t.Fatal("fixture is not fully configured; test cannot check the agreement") + } + got := describeChatHistorySkip(contextWithTenant(&types.Tenant{ChatHistoryConfig: cfg})) + if got != "chat history config incomplete" { + t.Errorf("unexpected reason for a configured tenant: %q", got) + } +} + +func contextWithTenant(tenant *types.Tenant) context.Context { + return context.WithValue(context.Background(), types.TenantInfoContextKey, tenant) +}