Skip to content

Commit 93035cb

Browse files
authored
Merge pull request #90 from DEEIX-AI/metadata
refactor: improve conversation metadata generation logic
2 parents d4fdb9c + fd590bb commit 93035cb

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

backend/internal/application/conversation/service_metadata.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type conversationMetadataLLMResult struct {
5252
}
5353

5454
func (s *Service) maybeGenerateConversationMetadataAsync(conversation model.Conversation, userMsg model.Message, assistantMsg model.Message) {
55-
if conversation.MessageCount != 0 {
55+
if !shouldGenerateConversationMetadata(conversation) {
5656
return
5757
}
5858
if strings.TrimSpace(userMsg.Content) == "" && strings.TrimSpace(assistantMsg.Content) == "" {
@@ -117,7 +117,7 @@ func (s *Service) generateConversationMetadata(ctx context.Context, conversation
117117
}()
118118
}
119119

120-
if s.routeResolver != nil && s.llmClient != nil {
120+
if s.routeResolver != nil && s.llmClient != nil && conversationLabelsEmpty(conversation.LabelsJSON) {
121121
wg.Add(1)
122122
go func() {
123123
defer wg.Done()
@@ -455,6 +455,15 @@ func shouldAutoReplaceConversationTitle(title string) bool {
455455
}
456456
}
457457

458+
func shouldGenerateConversationMetadata(conversation model.Conversation) bool {
459+
return shouldAutoReplaceConversationTitle(conversation.Title) || conversationLabelsEmpty(conversation.LabelsJSON)
460+
}
461+
462+
func conversationLabelsEmpty(labelsJSON string) bool {
463+
value := strings.TrimSpace(strings.ToLower(labelsJSON))
464+
return value == "" || value == "null" || value == "[]"
465+
}
466+
458467
func truncateByEstimatedTokens(text string, maxTokens int64) string {
459468
if maxTokens <= 0 || estimateTokens(text) <= maxTokens {
460469
return text

backend/internal/application/conversation/service_metadata_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,27 @@ func TestConversationTitleFromFirstUserMessage(t *testing.T) {
7171
}
7272
}
7373
}
74+
75+
func TestShouldGenerateConversationMetadataAfterFailedFirstTurn(t *testing.T) {
76+
conversation := model.Conversation{
77+
Title: "新会话",
78+
LabelsJSON: "[]",
79+
MessageCount: 2,
80+
}
81+
82+
if !shouldGenerateConversationMetadata(conversation) {
83+
t.Fatal("expected placeholder metadata to be generated even when failed messages already exist")
84+
}
85+
}
86+
87+
func TestConversationLabelsEmpty(t *testing.T) {
88+
emptyCases := []string{"", "null", "[]", " [] "}
89+
for _, value := range emptyCases {
90+
if !conversationLabelsEmpty(value) {
91+
t.Fatalf("expected labels %q to be empty", value)
92+
}
93+
}
94+
if conversationLabelsEmpty(`["技术"]`) {
95+
t.Fatal("expected non-empty labels to be preserved")
96+
}
97+
}

0 commit comments

Comments
 (0)