Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions backend/internal/application/conversation/service_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type conversationMetadataLLMResult struct {
}

func (s *Service) maybeGenerateConversationMetadataAsync(conversation model.Conversation, userMsg model.Message, assistantMsg model.Message) {
if conversation.MessageCount != 0 {
if !shouldGenerateConversationMetadata(conversation) {
return
}
if strings.TrimSpace(userMsg.Content) == "" && strings.TrimSpace(assistantMsg.Content) == "" {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (s *Service) generateConversationMetadata(ctx context.Context, conversation
}()
}

if s.routeResolver != nil && s.llmClient != nil {
if s.routeResolver != nil && s.llmClient != nil && conversationLabelsEmpty(conversation.LabelsJSON) {
wg.Add(1)
go func() {
defer wg.Done()
Expand Down Expand Up @@ -455,6 +455,15 @@ func shouldAutoReplaceConversationTitle(title string) bool {
}
}

func shouldGenerateConversationMetadata(conversation model.Conversation) bool {
return shouldAutoReplaceConversationTitle(conversation.Title) || conversationLabelsEmpty(conversation.LabelsJSON)
}

func conversationLabelsEmpty(labelsJSON string) bool {
value := strings.TrimSpace(strings.ToLower(labelsJSON))
return value == "" || value == "null" || value == "[]"
}

func truncateByEstimatedTokens(text string, maxTokens int64) string {
if maxTokens <= 0 || estimateTokens(text) <= maxTokens {
return text
Expand Down
24 changes: 24 additions & 0 deletions backend/internal/application/conversation/service_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ func TestConversationTitleFromFirstUserMessage(t *testing.T) {
}
}
}

func TestShouldGenerateConversationMetadataAfterFailedFirstTurn(t *testing.T) {
conversation := model.Conversation{
Title: "新会话",
LabelsJSON: "[]",
MessageCount: 2,
}

if !shouldGenerateConversationMetadata(conversation) {
t.Fatal("expected placeholder metadata to be generated even when failed messages already exist")
}
}

func TestConversationLabelsEmpty(t *testing.T) {
emptyCases := []string{"", "null", "[]", " [] "}
for _, value := range emptyCases {
if !conversationLabelsEmpty(value) {
t.Fatalf("expected labels %q to be empty", value)
}
}
if conversationLabelsEmpty(`["技术"]`) {
t.Fatal("expected non-empty labels to be preserved")
}
}
Loading