Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: store <think>content</think> to conversation_message (#19836)

Wang Qid8fd3703dbc1
diff --git a/internal/dao/conversation_history_migration_test.go b/internal/dao/conversation_history_migration_test.go
index 10d5fff024..f84fb3e3de 100644
--- a/internal/dao/conversation_history_migration_test.go
+++ b/internal/dao/conversation_history_migration_test.go
@@ -109,7 +109,7 @@ func writeDatabaseVersion(t *testing.T, db *gorm.DB, version string) {
}
func TestMigrateConversationHistorySplitsPayloads(t *testing.T) {
- message := `[{"role":"user","content":"hi","id":"m1"},{"role":"assistant","content":"hello","id":"m2","reference":[{"chunk-0":{"content":"chunk"}}]}]`
+ message := `[{"role":"user","content":"hi","id":"m1"},{"role":"assistant","content":"<think>reasoning</think>hello","id":"m2","reference":[{"chunk-0":{"content":"chunk"}}]}]`
reference := `[{"chunks":["a"],"doc_aggs":[]},{"chunks":["b"],"doc_aggs":[]}]`
db := setupConversationHistoryMigrationDB(t, true)
insertLegacyConversation(t, db, "conversation", "c1", message, reference)
@@ -131,12 +131,17 @@ func TestMigrateConversationHistorySplitsPayloads(t *testing.T) {
}
}
- var msg entity.ConversationMessage
- if err := db.Table(conversationMessageTable).Where("conversation_id = ? AND position = ?", "c1", 1).Take(&msg).Error; err != nil {
- t.Fatalf("load message: %v", err)
- }
- if derefString(msg.Role) != "assistant" || derefString(msg.Content) != "hello" || derefString(msg.MessageID) != "m2" {
- t.Fatalf("unexpected assistant message: %+v", msg)
+ for _, tc := range []struct{ table, conversationID string }{
+ {conversationMessageTable, "c1"},
+ {apiConversationMessageTable, "a1"},
+ } {
+ var msg entity.ConversationMessage
+ if err := db.Table(tc.table).Where("conversation_id = ? AND position = ?", tc.conversationID, 1).Take(&msg).Error; err != nil {
+ t.Fatalf("load message from %s: %v", tc.table, err)
+ }
+ if derefString(msg.Role) != "assistant" || derefString(msg.Content) != "<think>reasoning</think>hello" || derefString(msg.MessageID) != "m2" {
+ t.Fatalf("unexpected assistant message in %s: %+v", tc.table, msg)
+ }
}
var storedReference string
diff --git a/internal/service/chat_session.go b/internal/service/chat_session.go
index 2cc3c8f2ef..e3d5bafda3 100644
--- a/internal/service/chat_session.go
+++ b/internal/service/chat_session.go
@@ -1502,9 +1502,10 @@ func (s *ChatSessionService) ChatCompletions(
if result.Final {
failed := strings.Contains(result.Answer, "**ERROR**")
if session != nil && !failed {
- content := result.Answer
+ // Store with <think>thinking content</think>
+ content := fullAnswer.String()
if content == "" {
- content = fullAnswer.String()
+ content = result.Answer
}
s.appendAssistantToSession(session, content, messageID)
if ctx.Err() == nil {
diff --git a/internal/service/chat_session_test.go b/internal/service/chat_session_test.go
index 68873139a4..cbd31d42a0 100644
--- a/internal/service/chat_session_test.go
+++ b/internal/service/chat_session_test.go
@@ -1112,7 +1112,10 @@ func TestChatCompletionsStreamFinalCarriesDecoratedReference(t *testing.T) {
}
pipeline := &fakePipeline{
resultChan: makeResultChan(
- AsyncChatResult{Answer: "Marigold", Reference: map[string]interface{}{"chunks": []interface{}{}}, Final: false},
+ AsyncChatResult{StartToThink: true, Reference: map[string]interface{}{"chunks": []interface{}{}}, Final: false},
+ AsyncChatResult{Reasoning: "checking sources", Reference: map[string]interface{}{"chunks": []interface{}{}}, Final: false},
+ AsyncChatResult{EndToThink: true, Reference: map[string]interface{}{"chunks": []interface{}{}}, Final: false},
+ AsyncChatResult{Answer: "Marigold is a depth-estimation model.", Reference: map[string]interface{}{"chunks": []interface{}{}}, Final: false},
AsyncChatResult{
Answer: "Marigold is a depth-estimation model. [ID:0]",
Reference: finalReference,
@@ -1191,6 +1194,10 @@ func TestChatCompletionsStreamFinalCarriesDecoratedReference(t *testing.T) {
if got := ref["total"]; got != float64(1) {
t.Fatalf("final reference total = %v", got)
}
+ stored := parseMessages(store.sessions["session-1"].Message)
+ if got := stored[len(stored)-1]["content"]; got != "<think>checking sources</think>Marigold is a depth-estimation model." {
+ t.Fatalf("stored assistant content = %q, want tagged reasoning and visible answer", got)
+ }
}
func TestChatCompletionsModelIDOverrideUsesModelResolver(t *testing.T) {