RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: honor Go chat citation settings and preserve final streamed citations (#19797)
adws52cf6fe586ea
diff --git a/internal/service/chat_pipeline.go b/internal/service/chat_pipeline.goindex e161f06b02..f4a8c6f686 100644--- a/internal/service/chat_pipeline.go+++ b/internal/service/chat_pipeline.go@@ -409,6 +409,15 @@ func (s *ChatPipelineService) AsyncChat(// === Phase 6: SQL Retrieval ===// Retrieve field_map for SQL retrieval (preferred over vector search)promptConfig := chat.PromptConfig+ // Either the chat setting or the request can disable citations. Resolve+ // this once before any retrieval path can return early.+ quote := true+ if v, ok := kwargs["quote"].(bool); ok {+ quote = v+ }+ if promptConfigQuote, ok := promptConfig["quote"].(bool); ok {+ quote = quote && promptConfigQuote+ }fieldMap, fmErr := s.kbDAO.GetFieldMap(ctx, dao.DB, kbIDStrings(kbs))if fmErr != nil {common.Warn("get_field_map failed; proceeding without field_map", zap.Error(fmErr))@@ -421,11 +430,6 @@ func (s *ChatPipelineService) AsyncChat(if len(fieldMap) > 0 && chatModel != nil && len(kbs) > 0 {common.Info("Phase 6: Use SQL to retrieval")common.Debug("field_map retrieved", zap.Any("field_map", fieldMap))- quote := true- if v, ok := promptConfig["quote"].(bool); ok {- quote = v- }-ans, sqlErr := s.useSQL(ctx, chat, kbs, questions[len(questions)-1], chatModel, fieldMap, quote,)@@ -693,16 +697,10 @@ func (s *ChatPipelineService) AsyncChat(// When false, the entire block is skipped.if hasKnowledgeParam {if useReasoning && chatModel != nil && len(kbs) > 0 {- // Reasoning chat (level 1..4): drive the agentic-RAG harness at- // the corresponding mode, mirroring Python's dialog_service →- // RAGTools → harness/*.- //- // The harness 'rag' tool composes the final cited answer itself- // (terminal tool, mirroring Python). When it returns a non-empty- // Answer we emit that answer directly (decorated per Python's- // decorate_answer: references resolved, prompt empty) and stop —- // there is no second-generation pass in Phase 10/11. Otherwise- // we keep its evidence as kbinfos and fall through.+ // The harness collects evidence and composes the answer for+ // reasoning levels 1..4. Apply citation visibility to its stream+ // and final answer. If no answer is returned, keep the evidence+ // and continue through the regular generation path.thinkingMode := harnessModeForLevel(reasoningLevel)question := strings.Join(questions, " ")// Stream the answer as the harness composes it (Python@@ -735,16 +733,17 @@ func (s *ChatPipelineService) AsyncChat(// Engine progress (B) is pushed from concurrent research-slot// goroutines while the final compose runs on the main goroutine, so// serialize the think-framing state machine and the out<-send.+ var answerFilter, reasoningFilter citationStreamFiltervar sinkMu sync.Mutex+ send := func(ev AsyncChatResult) {+ select {+ case out <- ev:+ case <-ctx.Done():+ }+ }sink := func(delta string, isThink bool) {sinkMu.Lock()defer sinkMu.Unlock()- send := func(ev AsyncChatResult) {- select {- case out <- ev:- case <-ctx.Done():- }- }if isThink {if answerStreamed {// Never reopen a thought block after the answer: drop the@@ -760,6 +759,9 @@ func (s *ChatPipelineService) AsyncChat(StartToThink: true,})}+ if !quote {+ delta = reasoningFilter.write(delta)+ }if delta != "" {send(AsyncChatResult{Reasoning: delta,@@ -771,6 +773,9 @@ func (s *ChatPipelineService) AsyncChat(return}if harnessThinking {+ if text := reasoningFilter.flush(); text != "" {+ send(AsyncChatResult{Reasoning: text, Reference: map[string]interface{}{}, CreatedAt: float64(time.Now().Unix())})+ }harnessThinking = falsesend(AsyncChatResult{Reference: map[string]interface{}{},@@ -781,6 +786,11 @@ func (s *ChatPipelineService) AsyncChat(}if delta != "" {answerStreamed = true+ }+ if !quote {+ delta = answerFilter.write(delta)+ }+ if delta != "" {send(AsyncChatResult{Answer: delta,Reference: map[string]interface{}{},@@ -836,6 +846,11 @@ func (s *ChatPipelineService) AsyncChat(// EndToThink after the Phase 10/11 answer or the Final below.// No-op when no block is open.sink("", false)+ sinkMu.Lock()+ if text := answerFilter.flush(); text != "" {+ send(AsyncChatResult{Answer: text, Reference: map[string]interface{}{}, CreatedAt: float64(time.Now().Unix())})+ }+ sinkMu.Unlock()if hErr != nil {common.Warn("harness retrieval failed", zap.Error(hErr))} else {@@ -843,7 +858,7 @@ func (s *ChatPipelineService) AsyncChat(if harnessAnswer != "" {common.Info("harness produced final cited answer; short-circuiting",zap.Int("answer_chars", len(harnessAnswer)))- final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs)+ final := s.decorateHarnessAnswer(harnessAnswer, kbinfos, slotCites, citeChunkIDs, quote)final.Final = trueout <- finalreturn@@ -1031,13 +1046,17 @@ func (s *ChatPipelineService) AsyncChat(// otherwise see an empty reply.if emptyResponseApplies(len(knowledges), attachments, hasImageAttachments) {if emptyResp, ok := promptConfig["empty_response"].(string); ok && emptyResp != "" {+ finalReference := kbinfos+ if !quote {+ finalReference = map[string]interface{}{}+ }out <- AsyncChatResult{Answer: emptyResp,Reference: map[string]interface{}{},}out <- AsyncChatResult{Answer: emptyResp,- Reference: kbinfos,+ Reference: finalReference,AudioBinary: s.synthesizeTTS(ctx, ttsModel, emptyResp),Prompt: fmt.Sprintf("\n\n### Query:\n%s", strings.Join(questions, " ")),Final: true,@@ -1075,13 +1094,6 @@ func (s *ChatPipelineService) AsyncChat(// Build citation prompt if quoting is enabled.prompt4citation := ""- quote := true- if v, ok := kwargs["quote"].(bool); ok {- quote = v- }- if promptConfigQuote, ok := promptConfig["quote"].(bool); ok {- quote = quote && promptConfigQuote- }if len(knowledges) > 0 && quote {// Python's citation_prompt() (generator.py:226) renders// citation_prompt.md — the full rules with examples, which the agent@@ -1253,6 +1265,34 @@ func (s *ChatPipelineService) AsyncChat(// Streaming path: accumulate answer, emit deltas.var fullAnswer stringthinkState := &ThinkStreamState{}+ var answerFilter, reasoningFilter citationStreamFilter+ emit := func(result AsyncChatResult) {+ if !result.Final && result.Answer != "" {+ result.AudioBinary = s.synthesizeTTS(ctx, ttsModel, result.Answer)+ }+ out <- result+ }+ send := func(result AsyncChatResult) {+ if !quote {+ // Flush before a boundary so buffered text stays in its section.+ if result.StartToThink || result.EndToThink || result.Final {+ if text := reasoningFilter.flush(); text != "" {+ emit(AsyncChatResult{Reasoning: text, Reference: map[string]interface{}{}, CreatedAt: float64(time.Now().Unix())})+ }+ if text := answerFilter.flush(); text != "" {+ emit(AsyncChatResult{Answer: text, Reference: map[string]interface{}{}, CreatedAt: float64(time.Now().Unix())})+ }+ }+ if !result.Final && (result.Answer != "" || result.Reasoning != "") {+ result.Answer = answerFilter.write(result.Answer)+ result.Reasoning = reasoningFilter.write(result.Reasoning)+ if result.Answer == "" && result.Reasoning == "" {+ return+ }+ }+ }+ emit(result)+ }// Tool routing: use tool-loop method when tools are bound.var driverErr error@@ -1271,48 +1311,47 @@ func (s *ChatPipelineService) AsyncChat(if text == "<think>" {inThink = true- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,StartToThink: true,- }+ })return nil}if text == "</think>" {inThink = false- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,EndToThink: true,- }+ })return nil}if inThink {// Reasoning text — route to Reasoning field so// the SSE handler maps it to// `delta.reasoning_content`.- out <- AsyncChatResult{+ send(AsyncChatResult{Reasoning: text,Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,- }+ })} else {// Regular answer content- out <- AsyncChatResult{- Answer: text,- Reference: map[string]interface{}{},- AudioBinary: s.synthesizeTTS(ctx, ttsModel, text),- CreatedAt: float64(time.Now().Unix()),- Final: false,- }+ send(AsyncChatResult{+ Answer: text,+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ Final: false,+ })}return nil})@@ -1322,26 +1361,25 @@ func (s *ChatPipelineService) AsyncChat(func(answer *string, reason *string) error {if reason != nil && *reason != "" {if thinkState.EnterReasoning() {- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,StartToThink: true,- }+ })}deltas := NextThinkDelta(thinkState, *reason, 16)for _, d := range deltas {if d.Kind == ThinkDeltaText && d.Value != "" {fullAnswer += d.Value- out <- AsyncChatResult{- Answer: d.Value,- Reference: map[string]interface{}{},- AudioBinary: s.synthesizeTTS(ctx, ttsModel, d.Value),- CreatedAt: float64(time.Now().Unix()),- Final: false,- }+ send(AsyncChatResult{+ Answer: d.Value,+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ Final: false,+ })}}}@@ -1350,35 +1388,33 @@ func (s *ChatPipelineService) AsyncChat(for _, d := range FlushRemaining(thinkState) {if d.Kind == ThinkDeltaText && d.Value != "" {fullAnswer += d.Value- out <- AsyncChatResult{- Answer: d.Value,- Reference: map[string]interface{}{},- AudioBinary: s.synthesizeTTS(ctx, ttsModel, d.Value),- CreatedAt: float64(time.Now().Unix()),- Final: false,- }+ send(AsyncChatResult{+ Answer: d.Value,+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ Final: false,+ })}}- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,EndToThink: true,- }+ })}fullAnswer += *answerdeltas := BufferAnswerDelta(thinkState, *answer, 16)for _, d := range deltas {if d.Kind == ThinkDeltaText && d.Value != "" {- out <- AsyncChatResult{- Answer: d.Value,- Reference: map[string]interface{}{},- AudioBinary: s.synthesizeTTS(ctx, ttsModel, d.Value),- CreatedAt: float64(time.Now().Unix()),- Final: false,- }+ send(AsyncChatResult{+ Answer: d.Value,+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ Final: false,+ })}}}@@ -1387,10 +1423,10 @@ func (s *ChatPipelineService) AsyncChat()}if driverErr != nil {- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", driverErr.Error()),Final: true,- }+ })return}@@ -1401,36 +1437,35 @@ func (s *ChatPipelineService) AsyncChat(for _, d := range FlushRemaining(thinkState) {if d.Kind == ThinkDeltaMarker && d.Value == "</think>" {hadThinkClose = true- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,EndToThink: true,- }+ })} else if d.Kind == ThinkDeltaText && d.Value != "" {- out <- AsyncChatResult{- Answer: d.Value,- Reference: map[string]interface{}{},- AudioBinary: s.synthesizeTTS(ctx, ttsModel, d.Value),- CreatedAt: float64(time.Now().Unix()),- Final: false,- }+ send(AsyncChatResult{+ Answer: d.Value,+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ Final: false,+ })}}// Close reasoning if the stream ended while still in reasoning mode// (e.g. model returned only reasoning chunks with no content delta).// Skip when FlushRemaining already emitted a </think> marker.if !hadThinkClose && thinkState.ExitReasoning() {- out <- AsyncChatResult{+ send(AsyncChatResult{Answer: "",Reference: map[string]interface{}{},AudioBinary: nil,CreatedAt: float64(time.Now().Unix()),Final: false,EndToThink: true,- }+ })}// Decorate and yield the final answer.@@ -1443,7 +1478,7 @@ func (s *ChatPipelineService) AsyncChat(final.Final = truefinal.AudioBinary = niltimer.Exit(common.PhaseGenerateAnswer)- out <- final+ send(final)} else {// Non-streaming: get the answer synchronously.var answer string@@ -3072,6 +3107,10 @@ func (s *ChatPipelineService) decorateAnswer(ans = strings.TrimSpace(parts[1])}}+ if !quote {+ think = stripCitations(think)+ ans = stripCitations(ans)+ }var citationIdx map[int]struct{}var refs map[string]interface{}@@ -3178,10 +3217,10 @@ func (s *ChatPipelineService) decorateAnswer(}}- // Build refs only when the answer contains a citation that resolves to a- // chunk. Retrieved evidence without a citation must not be exposed as a- // document reference.- if hasKnowledges && len(citationIdx) > 0 {+ // Include sources only when citations are enabled and at least one citation+ // actually resolves to a chunk, stripping chunk vectors. Retrieved evidence+ // without a citation must not be exposed as a document reference.+ if hasKnowledges && quote && len(citationIdx) > 0 {refs = make(map[string]interface{})for k, v := range kbinfos {refs[k] = v@@ -3200,6 +3239,8 @@ func (s *ChatPipelineService) decorateAnswer(}refs["chunks"] = chunksFormat(newChunks)}+ } else if !quote {+ refs = map[string]interface{}{}}// Check for invalid API key errors (outside knowledges guard).@@ -3276,13 +3317,13 @@ func (s *ChatPipelineService) decorateAnswer(}}-// decorateHarnessAnswer mirrors Python's rag_agent decorate_answer used on the-// reasoning>=1 path (dialog_service.py:2092-2137). The harness 'rag' tool-// already composed the final answer WITH its own [ID:N] citations, so — unlike-// decorateAnswer for the native async_chat path — we never run insert_citations-// here. We only resolve the existing markers, repair bad formats, filter-// doc_aggs to the cited docs, and build the reference from the harness citation-// pool (chunks stripped of their vectors). Prompt stays empty, matching Python.+// decorateHarnessAnswer formats the reasoning chat's final answer. The harness+// 'rag' tool already composed the answer WITH its own [ID:N] citations, so —+// unlike decorateAnswer for the native async_chat path — we never run+// insert_citations here. We only resolve the existing markers, repair bad+// formats, filter doc_aggs to the cited docs, and build the reference from the+// harness citation pool (chunks stripped of their vectors). Prompt stays empty,+// matching Python.//// The answer's [ID:n] markers index the evidence list the compose rendered for// the model — harness.Kbinfos.CiteChunkIDs, passed in as citeChunkIDs. That list@@ -3297,11 +3338,9 @@ func (s *ChatPipelineService) decorateAnswer(// returned whenever the pool is non-empty, so an answer the model composed without// markers still carries its sources.//-// Precondition for marker n to open passage n: the rendered blocks resolve to pool-// chunks in order. A block whose id the pool does not know keeps its slot — an empty-// entry the reference carries — so the later blocks keep their numbers; its own-// citations are dropped, since the user could not open them.-func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[string]interface{}, slotCitations map[string][]string, citeChunkIDs []string) AsyncChatResult {+// When quote is false the citations are removed entirely and the reference is+// cleared (see stripCitations).+func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[string]interface{}, slotCitations map[string][]string, citeChunkIDs []string, quote bool) AsyncChatResult {think := ""ans := answerif strings.Contains(answer, "</think>") {@@ -3311,6 +3350,20 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[s}}+ // Invalid-key hint (dialog_service.py:2134-2135).+ if strings.Contains(strings.ToLower(ans), "invalid key") ||+ strings.Contains(strings.ToLower(ans), "invalid api") {+ ans += " Please set LLM API-Key in 'User Setting -> Model providers -> API-Key'"+ }++ if !quote {+ return AsyncChatResult{+ Answer: stripCitations(think + ans),+ Reference: map[string]interface{}{},+ CreatedAt: float64(time.Now().Unix()),+ }+ }+chunksRaw, _ := kbinfos["chunks"].([]map[string]interface{})// The markers exactly as the model wrote them (before any repair), for the// citation observability log below.@@ -3411,12 +3464,6 @@ func (s *ChatPipelineService) decorateHarnessAnswer(answer string, kbinfos map[srefs = ref}- // Invalid-key hint (dialog_service.py:2134-2135).- if strings.Contains(strings.ToLower(ans), "invalid key") ||- strings.Contains(strings.ToLower(ans), "invalid api") {- ans += " Please set LLM API-Key in 'User Setting -> Model providers -> API-Key'"- }-return AsyncChatResult{Answer: think + ans,Reference: refs,@@ -3770,23 +3817,6 @@ Please correct the error and write SQL again using the exact field names above,//// - err: non-nil when something went wrong; caller should log and fall// through.-//-// StructuredQuery is the exported narrow entrypoint for the agentic-search-// structured_query tool: translate a natural-language question to SQL over the-// given tabular KBs and return the answer + reference chunks. It forwards to the-// internal useSQL with quote=false (the agent tool returns the answer directly,-// not a cited natural-language response).-func (s *ChatPipelineService) StructuredQuery(- ctx context.Context,- chat *entity.Chat,- kbs []*entity.Knowledgebase,- question string,- chatModel *modelModule.ChatModel,- fieldMap map[string]interface{},-) (ans map[string]interface{}, err error) {- return s.useSQL(ctx, chat, kbs, question, chatModel, fieldMap, false)-}-func (s *ChatPipelineService) useSQL(ctx context.Context,chat *entity.Chat,@@ -3888,7 +3918,7 @@ func (s *ChatPipelineService) useSQL(// repair doesn't yield source columns, fall through to the// best-effort answer (matches Python's `returning best-effort// answer` log at line 1221).- if !isAggregateSQL(sqlText) && !hasSourceColumns(rows) {+ if quote && !isAggregateSQL(sqlText) && !hasSourceColumns(rows) {common.Debug("SQL retrieval: result missing source columns; attempting repair",zap.String("sql", sqlText))expectedCol := expectedDocNameColumn(engineName)@@ -3924,7 +3954,7 @@ func (s *ChatPipelineService) useSQL(// and best-effort empty refs.answerStr, ref := s.buildSQLReference(ctx, docEngine, tableName, sqlText, rows,- sysPrompt, engineName, kbs, fieldMap,+ sysPrompt, engineName, kbs, fieldMap, quote,)return map[string]interface{}{"answer": answerStr,@@ -4638,8 +4668,12 @@ func (s *ChatPipelineService) buildSQLReference(sysPrompt, engineName string,kbs []*entity.Knowledgebase,fieldMap map[string]interface{},+ quote bool,) (string, map[string]interface{}) {if len(rows) == 0 {+ if !quote {+ return "No results.", map[string]interface{}{}+ }return "No results.", map[string]interface{}{"chunks": []map[string]interface{}{},"doc_aggs": []interface{}{},@@ -4650,6 +4684,9 @@ func (s *ChatPipelineService) buildSQLReference(// Scalar shortcut — matches the previous renderSQLAnswer behavior.if len(rows) == 1 && len(rows[0]) == 1 {for _, v := range rows[0] {+ if !quote {+ return cleanCellValue(v), map[string]interface{}{}+ }return cleanCellValue(v), map[string]interface{}{"chunks": []map[string]interface{}{},"doc_aggs": []interface{}{},@@ -4662,6 +4699,7 @@ func (s *ChatPipelineService) buildSQLReference(docIDIdx, docNameIdx, kbIDIdx, columns := extractSourceColumnIndexes(rows)expectedCol := expectedDocNameColumn(engineName)hasSrc := len(docIDIdx) > 0 && len(docNameIdx) > 0+ showSource := quote && hasSrc// Build the set of "display column" indices (everything except// doc_id, docnm*, kb_id*). Python uses set subtraction at@@ -4690,13 +4728,13 @@ func (s *ChatPipelineService) buildSQLReference(header.WriteString(mapColumnName(columns[i], fieldMap))header.WriteString("|")}- if hasSrc {+ if showSource {header.WriteString("Source|")}// --- Separator (Python L1285) ---sep := strings.Repeat("|------", len(displayCols)) + "|"- if hasSrc {+ if showSource {sep += "------|"}@@ -4709,7 +4747,7 @@ func (s *ChatPipelineService) buildSQLReference(cells.WriteString(cleanCellValue(r[columns[i]]))cells.WriteString("|")}- if hasSrc {+ if showSource {cells.WriteString(fmt.Sprintf(" ##%d$$|", rowIdx))}// Skip rows that are entirely empty/whitespace (Python's@@ -4722,6 +4760,9 @@ func (s *ChatPipelineService) buildSQLReference(rowsJoined := stripISOTimestamps(strings.Join(bodyRows, "\n"))answer := strings.Join([]string{header.String(), sep, rowsJoined}, "\n")+ if !quote {+ return answer, map[string]interface{}{}+ }// --- Reference: chunks + doc_aggs ---ref := map[string]interface{}{diff --git a/internal/service/chat_pipeline_test.go b/internal/service/chat_pipeline_test.goindex b009e30e7b..c64e5ae6b4 100644--- a/internal/service/chat_pipeline_test.go+++ b/internal/service/chat_pipeline_test.go@@ -235,8 +235,8 @@ func TestDecorateAnswer_InvalidKeySuffix(t *testing.T) {}// TestDecorateAnswer_LeavesCanonicalMarkers covers P0: the decorator-// passes canonical [ID:N] markers through unchanged when there are-// no chunks to cite (so insertCitations is skipped).+// passes canonical [ID:N] markers through unchanged when quoting is enabled+// and there are no chunks to cite (so insertCitations is skipped).func TestDecorateAnswer_LeavesCanonicalMarkers(t *testing.T) {s := &ChatPipelineService{}timer, _ := newTimerAndPrompt()@@ -248,7 +248,7 @@ func TestDecorateAnswer_LeavesCanonicalMarkers(t *testing.T) {[]string{"q"},0,timer,- nil, 0.0, false,+ nil, 0.0, true,nil,"",nil,@@ -262,14 +262,14 @@ func TestDecorateAnswer_LeavesCanonicalMarkers(t *testing.T) {}// TestDecorateAnswer_RepairNotRunWhenNoQuote covers P0.10: when-// quote=false, the citation-repair branch is gated off and the-// answer is preserved verbatim.+// quote=false, citation markers are removed without rewriting ordinary+// bracketed numbers.func TestDecorateAnswer_RepairNotRunWhenNoQuote(t *testing.T) {s := &ChatPipelineService{}timer, _ := newTimerAndPrompt()result := s.decorateAnswer(t.Context(),- "see (ID: 12) for details",+ "<think>Checking[ID:0]##0$$</think>see (ID: 12)[ID:0]##1$$ in [2024] for details",map[string]interface{}{"chunks": []interface{}{}, "doc_aggs": []interface{}{}},"system prompt",[]string{"q"},@@ -283,8 +283,8 @@ func TestDecorateAnswer_RepairNotRunWhenNoQuote(t *testing.T) {nil,false,)- if result.Answer != "see (ID: 12) for details" {- t.Errorf("quote=false must not repair, got %q", result.Answer)+ if result.Answer != "<think>Checking</think>see in [2024] for details" {+ t.Errorf("quote=false citation cleanup = %q", result.Answer)}}@@ -649,7 +649,7 @@ func TestBuildSQLReference_Scalar(t *testing.T) {ans, ref := s.buildSQLReference(t.Context(), nil, "", "",[]map[string]interface{}{{"count": 42.0}},- "", "", nil, nil,+ "", "", nil, nil, true,)if ans != "42" {t.Errorf("buildSQLReference scalar answer = %q, want %q", ans, "42")@@ -676,7 +676,7 @@ func TestBuildSQLReference_MultiRowTable(t *testing.T) {ans, ref := s.buildSQLReference(t.Context(), nil, "", "select id, name from t",rows,- "sys", "elasticsearch", nil, nil,+ "sys", "elasticsearch", nil, nil, true,)// No source columns → empty chunks/doc_aggs.if chunks, _ := ref["chunks"].([]map[string]interface{}); len(chunks) != 0 {@@ -792,6 +792,23 @@ func TestDecorateAnswer_VectorStrippedFromReference(t *testing.T) {}}+func TestDecorateAnswer_NoReferencesWhenQuoteDisabled(t *testing.T) {+ s := &ChatPipelineService{}+ timer, _ := newTimerAndPrompt()+ kb := map[string]interface{}{+ "chunks": []map[string]interface{}{{"chunk_id": "c1", "content_with_weight": "Evidence", "doc_id": "d1"}},+ "doc_aggs": []interface{}{map[string]interface{}{"doc_id": "d1", "doc_name": "Source"}},+ }+ result := s.decorateAnswer(t.Context(), "Answer", kb, "", nil, 0, timer,+ nil, 0, false, nil, "", nil, "", nil, true)+ if result.Reference == nil || len(result.Reference) != 0 {+ t.Fatalf("disabled citations must explicitly clear references: %#v", result.Reference)+ }+ if result.Answer != "Answer" {+ t.Fatalf("answer changed: %q", result.Answer)+ }+}+// --- normalizeInternetFlag / shouldUseWebSearch parity with Python ---// TestNormalizeInternetFlag_PythonParity pins the three-state return of@@ -1405,7 +1422,7 @@ func TestBuildSQLReference_EmptyRows(t *testing.T) {s := &ChatPipelineService{}ans, ref := s.buildSQLReference(t.Context(), nil, "", "", nil,- "", "", nil, nil,+ "", "", nil, nil, true,)if ans != "No results." {t.Errorf("ans = %q, want %q", ans, "No results.")@@ -1427,7 +1444,7 @@ func TestBuildSQLReference_NonAggregateWithSourceColumns(t *testing.T) {s := &ChatPipelineService{}ans, ref := s.buildSQLReference(t.Context(), nil, "t", "select doc_id, docnm_kwd, title from t",- rows, "", "elasticsearch", kbs, nil,+ rows, "", "elasticsearch", kbs, nil, true,)if !strings.Contains(ans, "Source|") {t.Errorf("expected Source column in answer, got:\n%s", ans)@@ -1449,6 +1466,17 @@ func TestBuildSQLReference_NonAggregateWithSourceColumns(t *testing.T) {t.Errorf("chunks[%d].dataset_id = %v, want kb_a", i, cm["dataset_id"])}}++ ans, ref = s.buildSQLReference(+ t.Context(), nil, "t", "select doc_id, docnm_kwd, title from t",+ rows, "", "elasticsearch", kbs, nil, false,+ )+ if strings.Contains(ans, "Source|") || strings.Contains(ans, "##0$$") {+ t.Errorf("quote=false leaked SQL citations in answer:\n%s", ans)+ }+ if len(ref) != 0 {+ t.Errorf("quote=false reference = %#v, want empty", ref)+ }}// TestBuildSQLReference_AggregateMissingSourceColumnsSecondaryFetch@@ -1475,7 +1503,7 @@ func TestBuildSQLReference_AggregateMissingSourceColumnsSecondaryFetch(t *testinans, ref := s.buildSQLReference(t.Context(), sqlEngine, "t","select count(*) from t where x = 1",- rows, "", "elasticsearch", kbs, nil,+ rows, "", "elasticsearch", kbs, nil, true,)// Multi-cell aggregate → renders as a table, not a scalar.if !strings.Contains(ans, "|42|") {@@ -1498,7 +1526,7 @@ func TestBuildSQLReference_NonAggregateMissingSourceEmptyRefs(t *testing.T) {s := &ChatPipelineService{}ans, ref := s.buildSQLReference(t.Context(), nil, "t", "select title from t",- rows, "", "elasticsearch", nil, nil,+ rows, "", "elasticsearch", nil, nil, true,)if !strings.Contains(ans, "T1") || !strings.Contains(ans, "T2") {t.Errorf("expected table data in answer, got:\n%s", ans)@@ -1526,7 +1554,7 @@ func TestBuildSQLReference_DisplayNameTranslation(t *testing.T) {s := &ChatPipelineService{}ans, _ := s.buildSQLReference(t.Context(), nil, "t", "select doc_id, docnm_kwd, title from t",- rows, "", "elasticsearch", nil, fieldMap,+ rows, "", "elasticsearch", nil, fieldMap, true,)if !strings.Contains(ans, "|My Title|") {t.Errorf("expected translated column name, got:\n%s", ans)@@ -1545,7 +1573,7 @@ func TestBuildSQLReference_ISOTimestampStripped(t *testing.T) {s := &ChatPipelineService{}ans, _ := s.buildSQLReference(t.Context(), nil, "t", "select doc_id, docnm_kwd, created_at from t",- rows, "", "elasticsearch", nil, nil,+ rows, "", "elasticsearch", nil, nil, true,)if strings.Contains(ans, "T13:24:55") {t.Errorf("expected ISO timestamp stripped, got:\n%s", ans)@@ -1864,7 +1892,7 @@ func TestDecorateHarnessAnswerReferenceUsesClientChunkShape(t *testing.T) {}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos, nil, nil)+ res := s.decorateHarnessAnswer("The answer [ID:0]", kbinfos, nil, nil, true)if res.Reference == nil {t.Fatal("a cited answer must carry a reference")}@@ -1960,6 +1988,7 @@ func TestDecorateHarnessAnswerRewritesSlotCitations(t *testing.T) {kbinfos,map[string][]string{"0": {"c9"}},nil,+ true,)if strings.Contains(res.Answer, "[ID:Slot") {t.Fatalf("final answer still carries the internal slot citation: %q", res.Answer)@@ -1981,6 +2010,114 @@ func TestDecorateHarnessAnswerRewritesSlotCitations(t *testing.T) {}}+func TestAsyncChatHarnessQuote(t *testing.T) {+ db := setupChatPipelineVisionTestDB(t)+ seedChatPipelineVisionTenant(t, db, "glm-4-flash@ZHIPU-AI")+ if err := db.AutoMigrate(&entity.Knowledgebase{}); err != nil {+ t.Fatal(err)+ }+ if err := db.Create(&entity.Knowledgebase{ID: "kb-1", TenantID: "tenant-1", Name: "Knowledge"}).Error; err != nil {+ t.Fatal(err)+ }+ previousHarness := harnessRetriever+ t.Cleanup(func() { harnessRetriever = previousHarness })+ harnessRetriever = func(_ context.Context, req HarnessRequest) (HarnessResult, error) {+ req.AnswerSink("Research[ID:", true)+ req.AnswerSink("0] done", true)+ for _, delta := range []string{"Answer[", "ID:0", "] end"} {+ req.AnswerSink(delta, false)+ }+ return HarnessResult{+ Answer: "Answer[ID:0] end",+ Chunks: []map[string]interface{}{{"chunk_id": "c1", "content_with_weight": "Evidence", "doc_id": "d1"}},+ DocAggs: []map[string]interface{}{{"doc_id": "d1", "doc_name": "Source"}},+ }, nil+ }+ for _, tt := range []struct {+ name string+ config, request any+ wantQuote bool+ }{+ {"default enabled", nil, nil, true},+ {"chat disabled", false, true, false},+ {"request disabled", true, false, false},+ } {+ t.Run(tt.name, func(t *testing.T) {+ chat := dialForTest("glm-4-flash@ZHIPU-AI")+ chat.KBIDs = entity.JSONSlice{"kb-1"}+ chat.PromptConfig["system"] = "Answer using {knowledge}"+ chat.PromptConfig["quote"] = tt.config+ results, err := NewChatPipelineService().AsyncChat(t.Context(), "user-1", chat,+ []map[string]interface{}{{"role": "user", "content": "Question"}}, true,+ map[string]interface{}{"reasoning": 1, "quote": tt.request})+ if err != nil {+ t.Fatal(err)+ }+ var streamed, reasoning strings.Builder+ var final AsyncChatResult+ want := "Answer end"+ if tt.wantQuote {+ want = "Answer[ID:0] end"+ }+ for result := range results {+ reasoning.WriteString(result.Reasoning)+ if result.Final {+ final = result+ } else {+ streamed.WriteString(result.Answer)+ if !strings.HasPrefix(want, streamed.String()) {+ t.Fatalf("stream leaked citation text: %q", streamed.String())+ }+ }+ }+ if !final.Final || final.Answer != want || streamed.String() != want {+ t.Fatalf("stream=%q final=%+v, want %q", streamed.String(), final, want)+ }+ if (len(final.Reference) > 0) != tt.wantQuote {+ t.Fatalf("references=%#v, quote=%v", final.Reference, tt.wantQuote)+ }+ if !tt.wantQuote && (final.Reference == nil || strings.Contains(reasoning.String(), "[ID:")) {+ t.Fatal("disabled citations must be removed from reasoning and explicitly clear references")+ }+ })+ }+}++func TestAsyncChatEmptyResponseQuoteDisabled(t *testing.T) {+ db := setupChatPipelineVisionTestDB(t)+ seedChatPipelineVisionTenant(t, db, "glm-4-flash@ZHIPU-AI")+ if err := db.AutoMigrate(&entity.Knowledgebase{}); err != nil {+ t.Fatal(err)+ }+ if err := db.Create(&entity.Knowledgebase{ID: "kb-1", TenantID: "tenant-1", Name: "Knowledge"}).Error; err != nil {+ t.Fatal(err)+ }++ chat := dialForTest("glm-4-flash@ZHIPU-AI")+ chat.KBIDs = entity.JSONSlice{"kb-1"}+ chat.PromptConfig["system"] = "Answer using {knowledge}"+ chat.PromptConfig["empty_response"] = "Nothing found."+ results, err := NewChatPipelineService().AsyncChat(t.Context(), "user-1", chat,+ []map[string]interface{}{{"role": "user", "content": "Question"}}, true,+ map[string]interface{}{"quote": false})+ if err != nil {+ t.Fatal(err)+ }++ var final AsyncChatResult+ for result := range results {+ if result.Final {+ final = result+ }+ }+ if !final.Final || final.Answer != "Nothing found." {+ t.Fatalf("final=%+v, want empty-response result", final)+ }+ if final.Reference == nil || len(final.Reference) != 0 {+ t.Fatalf("quote=false reference = %#v, want explicit empty object", final.Reference)+ }+}+// TestThinkEventWireKeys pins the JSON keys of a fully populated step: they ARE// the client contract (the SSE `think_event` field), so renaming or dropping a tag// breaks every client while neither the compiler nor any Go caller notices.@@ -2061,7 +2198,7 @@ func TestDecorateHarnessAnswerUncitedOmitsReference(t *testing.T) {}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("小狼的颜色是灰色的。", kbinfos, nil, nil)+ res := s.decorateHarnessAnswer("小狼的颜色是灰色的。", kbinfos, nil, nil, true)if res.Reference != nil {t.Fatalf("uncited answer must not carry a reference, got %#v", res.Reference)}@@ -2085,7 +2222,7 @@ func TestDecorateHarnessAnswerCitesThinkBlock(t *testing.T) {}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("<think>Supported by [ID:0].</think>小狼的颜色是灰色的。", kbinfos, nil, []string{"c1"})+ res := s.decorateHarnessAnswer(" thinkingSupported by [ID:0]. response小狼的颜色是灰色的。", kbinfos, nil, []string{"c1"}, true)if res.Reference == nil {t.Fatal("a think-block citation must carry a reference")}@@ -2115,7 +2252,7 @@ func TestDecorateHarnessAnswerResolvesRenderedPosition(t *testing.T) {}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:0]。", kbinfos, nil, []string{"c1"})+ res := s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:0]。", kbinfos, nil, []string{"c1"}, true)if !strings.Contains(res.Answer, "[ID:0]") {t.Fatalf("the marker is the client's index and must survive, got %q", res.Answer)}@@ -2129,7 +2266,7 @@ func TestDecorateHarnessAnswerResolvesRenderedPosition(t *testing.T) {// The old numbering (a 1-based position) names no entry in a one-passage// reference, so it must be dropped rather than shipped as a dead marker.- res = s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:1]。", kbinfos, nil, []string{"c1"})+ res = s.decorateHarnessAnswer("小狼的颜色是灰色的 [ID:1]。", kbinfos, nil, []string{"c1"}, true)if strings.Contains(res.Answer, "[ID:1]") {t.Fatalf("an unresolvable marker must be dropped, got %q", res.Answer)}@@ -2163,6 +2300,7 @@ func TestDecorateHarnessAnswerOrdersReferenceByRenderedOrder(t *testing.T) {kbinfos,nil,[]string{"c3", "c1"},+ true,)chunks, _ := res.Reference["chunks"].([]map[string]interface{})if len(chunks) != 3 {@@ -2203,6 +2341,7 @@ func TestDecorateHarnessAnswerDropsOnlyCanonicalMarkers(t *testing.T) {kbinfos,nil,[]string{"c1"},+ true,)if !strings.Contains(res.Answer, "[2024]") {t.Errorf("prose brackets must survive, got %q", res.Answer)@@ -2239,6 +2378,7 @@ func TestDecorateHarnessAnswerKeepsPositionsAfterUnknownBlock(t *testing.T) {kbinfos,nil,[]string{"c1", "gone", "c3"},+ true,)if !strings.Contains(res.Answer, "[ID:2]") {t.Fatalf("a marker past the unknown block must survive, got %q", res.Answer)diff --git a/internal/service/citation.go b/internal/service/citation.goindex b4a145bc34..0c4a26429b 100644--- a/internal/service/citation.go+++ b/internal/service/citation.go@@ -477,6 +477,42 @@ func ExpandRangeCitations(answer string, poolSize int) string {return b.String()}+// Keep an unfinished citation until its closing delimiter arrives. Retaining+// the trailing word also preserves the word boundaries used by refN markers.+var citationStreamTailPattern = regexp.MustCompile(`(?i)[\[(【][\s*idslot::0-9\x{0660}-\x{0669}\x{06F0}-\x{06F9}–—~~-]*$|#{1,2}(?:[0-9]+\$?)?$|\bref\s*[0-9\x{0660}-\x{0669}\x{06F0}-\x{06F9}]*$|\w+$`)++func stripCitations(text string) string {+ text = cleanCitationMarkers(text)+ text = canonicalIDMarkerPattern.ReplaceAllString(text, "")+ text = slotCitationPattern.ReplaceAllString(text, "")+ text = rangeCitationPattern.ReplaceAllString(text, "")+ for _, pattern := range badCitationPatterns {+ text = pattern.ReplaceAllString(text, "")+ }+ return text+}++type citationStreamFilter struct {+ pending string+}++func (f *citationStreamFilter) write(delta string) string {+ f.pending += delta+ end := len(f.pending)+ if tail := citationStreamTailPattern.FindStringIndex(f.pending); tail != nil {+ end = tail[0]+ }+ text := stripCitations(f.pending[:end])+ f.pending = f.pending[end:]+ return text+}++func (f *citationStreamFilter) flush() string {+ text := stripCitations(f.pending)+ f.pending = ""+ return text+}+// ResolveCitationMarkers drops the answer's canonical [ID:n] markers that name// nothing and returns the pool positions of the markers that do resolve.//@@ -529,7 +565,7 @@ func ResolveCitationMarkers(answer string, citeIdx []int) (string, []int) {// "ID:" prefix — the form the citation rules prescribe, and the only one that can// be removed from the answer without risking ordinary text like "[2024]".var canonicalIDMarkerPattern = regexp.MustCompile(- `(?i)^\[\s*ID\s*[:: ]*\s*[0-9\x{0660}-\x{0669}\x{06F0}-\x{06F9}]+\s*\]$`)+ `(?i)\[\s*ID\s*[:: ]*\s*[0-9\x{0660}-\x{0669}\x{06F0}-\x{06F9}]+\s*\]`)// rawCitationMarkers returns the citation numbers the model wrote, in order and// including duplicates — the repetition is what the citation observability log indiff --git a/internal/service/citation_test.go b/internal/service/citation_test.goindex 8cfc0808a7..30e4a87945 100644--- a/internal/service/citation_test.go+++ b/internal/service/citation_test.go@@ -495,7 +495,7 @@ func TestDecorateHarnessAnswerExpandsRangeCitations(t *testing.T) {},}s := &ChatPipelineService{}- res := s.decorateHarnessAnswer("The range claim holds [ID:1-3].", kbinfos, nil, nil)+ res := s.decorateHarnessAnswer("The range claim holds [ID:1-3].", kbinfos, nil, nil, true)if strings.Contains(res.Answer, "1-3") {t.Fatalf("final answer still carries the range citation: %q", res.Answer)@@ -513,3 +513,19 @@ func TestDecorateHarnessAnswerExpandsRangeCitations(t *testing.T) {t.Fatalf("reference doc_aggs = %#v, want the single cited doc", res.Reference["doc_aggs"])}}++func TestCitationStreamFilter(t *testing.T) {+ var filter citationStreamFilter+ want := "Answer\n## Heading\n[2024][guide](https://example.com) end"+ got := ""+ for _, delta := range []string{"Answer[", "ID:0][ID:1-", "3][ID:Slot 0](ID: 2)#", "#0", "$", "$\n#", "# Heading\n[2024][guide](https://example.com) end"} {+ got += filter.write(delta)+ if !strings.HasPrefix(want, got) {+ t.Fatalf("stream leaked citation text: %q", got)+ }+ }+ got += filter.flush()+ if got != want {+ t.Fatalf("filtered answer=%q, want %q", got, want)+ }+}diff --git a/web/src/components/markdown-content/index.tsx b/web/src/components/markdown-content/index.tsxindex b9c80102c7..507fa31040 100644--- a/web/src/components/markdown-content/index.tsx+++ b/web/src/components/markdown-content/index.tsx@@ -41,7 +41,6 @@ import 'katex/dist/katex.min.css'; // `rehype-katex` does not import the CSS forimport { useFetchDocumentThumbnailsByIds } from '@/hooks/use-document-request';import { useLoadingPause } from '@/hooks/use-loading-pause';import {- currentReg,escapeUnmatchedAngleBrackets,parseCitationIndex,preprocessLaTeX,@@ -66,6 +65,7 @@ import { sanitizeHtmlWithImagesAsText } from '@/utils/dom-util';import { SafeImg } from '@/components/safe-img';const getChunkIndex = (match: string) => parseCitationIndex(match);+const ReferenceMarkerReg = /(\[(?:ID:)?[0-9\u0660-\u0669\u06F0-\u06F9]+\])/g;// Wraps every text node so citation markers can be replaced by React elements.// Defined at module scope: react-markdown rebuilds its whole processor whenever@@ -326,26 +326,40 @@ const MarkdownContent = ({const renderReference = useCallback((text: string) => {- const replacedText = reactStringReplace(text, currentReg, (match, i) => {- const chunkIndex = getChunkIndex(match) as number;+ const replacedText = reactStringReplace(+ text,+ ReferenceMarkerReg,+ (match, i) => {+ const chunkIndex = getChunkIndex(match);+ if (typeof chunkIndex !== 'number') {+ return match;+ }+ const hasReference = !!reference?.chunks?.[chunkIndex];+ // Explicit markers can render while their sources are still arriving.+ if (!hasReference && !(loading && match.startsWith('[ID:'))) {+ return match;+ }- return (- <HoverCard key={i}>- <HoverCardTrigger>- <bdi className="text-text-secondary bg-bg-card rounded-2xl px-1 mx-1 text-nowrap inline-block">- [{chunkIndex + 1}]- </bdi>- </HoverCardTrigger>- <HoverCardContent className="max-w-3xl">- {getPopoverContent(chunkIndex)}- </HoverCardContent>- </HoverCard>- );- });+ return (+ <HoverCard key={i}>+ <HoverCardTrigger>+ <bdi className="text-text-secondary bg-bg-card rounded-2xl px-1 mx-1 text-nowrap inline-block">+ [{chunkIndex + 1}]+ </bdi>+ </HoverCardTrigger>+ {hasReference && (+ <HoverCardContent className="max-w-3xl">+ {getPopoverContent(chunkIndex)}+ </HoverCardContent>+ )}+ </HoverCard>+ );+ },+ );return replacedText;},- [getPopoverContent],+ [getPopoverContent, loading, reference?.chunks],);const dir = getDirAttribute(content.replace(citationMarkerReg, ''));diff --git a/web/src/hooks/logic-hooks.ts b/web/src/hooks/logic-hooks.tsindex 7c9da6d2c5..2e775d97b2 100644--- a/web/src/hooks/logic-hooks.ts+++ b/web/src/hooks/logic-hooks.ts@@ -32,7 +32,7 @@ import { IKnowledgeFile } from '@/interfaces/database/dataset';import { changeLanguageAsync } from '@/locales/config';import api from '@/utils/api';import { getAuthorization } from '@/utils/authorization-util';-import { buildMessageUuid } from '@/utils/chat';+import { buildMessageUuid, mergeAnswerChunk } from '@/utils/chat';import {consumeListDeletionMarker,discardListDeletionMarker,@@ -365,31 +365,9 @@ export const useSendMessageWithSse = () => {const d = val?.data;if (typeof d !== 'boolean') {setAnswer((prev) => {- const prevAnswer = prev.answer || '';- // Skip final-chunk answer only when prior stream chunks exist (avoids duplicate).- // Empty-response and other single-shot answers arrive with final=true only.- // const currentAnswer = d.final ? '' : d.answer || '';- const currentAnswer =- d.final && prevAnswer ? '' : d.answer || '';-- let newAnswer: string;- if (prevAnswer && currentAnswer.startsWith(prevAnswer)) {- newAnswer = currentAnswer;- } else {- newAnswer = prevAnswer + currentAnswer;- }-- if (d.start_to_think === true) {- newAnswer = newAnswer + '<think>';- }-- if (d.end_to_think === true) {- newAnswer = newAnswer + '</think>';- }-return {...d,- answer: newAnswer,+ answer: mergeAnswerChunk(prev.answer || '', d),conversationId: body?.session_id ?? body?.conversation_id,chatBoxId: body.chatBoxId,};diff --git a/web/src/pages/next-chats/chat-stream/run-stream.ts b/web/src/pages/next-chats/chat-stream/run-stream.tsindex 25c2227c8d..628e38e5ec 100644--- a/web/src/pages/next-chats/chat-stream/run-stream.ts+++ b/web/src/pages/next-chats/chat-stream/run-stream.ts@@ -10,9 +10,9 @@ import {readJsonSafely,requestChatCompletionStream,} from '@/services/chat-completion-stream';+import { mergeAnswerChunk } from '@/utils/chat';import { AnswerFlushIntervalMs } from './constants';import { useChatStreamStore } from './store';-import { mergeAnswerChunk } from './utils';export type RunChatCompletionStreamParams = {conversationId: string;diff --git a/web/src/pages/next-chats/chat-stream/utils.ts b/web/src/pages/next-chats/chat-stream/utils.tsindex f01e3fcc7c..ec6f3447e8 100644--- a/web/src/pages/next-chats/chat-stream/utils.ts+++ b/web/src/pages/next-chats/chat-stream/utils.ts@@ -2,68 +2,8 @@ import { MessageType } from '@/constants/chat';import { IAnswer, IMessage } from '@/interfaces/database/chat';import { buildMessageUuid } from '@/utils/chat';import { omit } from 'lodash';-import { CompletionChunk } from '@/services/chat-completion-stream';import { PrologueMessageIdPrefix } from './constants';-/**- * Accumulates a streamed answer chunk onto the previously accumulated text.- * Ported verbatim from the legacy `setAnswer` reducer in- * `useSendMessageWithSse` (src/hooks/logic-hooks.ts) to preserve behaviour:- * the final chunk is skipped only when earlier chunks exist (single-shot- * answers arrive with `final: true` only), a chunk that already contains the- * previous text replaces it instead of being appended, and think markers are- * injected inline.- */-export function mergeAnswerChunk(- previousAnswer: string,- chunk: CompletionChunk,-): string {- // A final chunk carries the COMPLETE answer (citations inserted, references- // attached server-side). It replaces the accumulated ANSWER — but NOT the- // thinking accumulated so far, which is the only record of the ReAct- // trajectory the user watched unfold (the final chunk ships the deliverable- // alone). A naive final is self-contained (`think + decorated answer`), so- // it replaces everything wholesale instead of duplicating the think span.- if (chunk.final === true && chunk.answer) {- if (chunk.answer.includes('<think>')) {- return chunk.answer;- }- const closeTag = '</think>';- const closeIdx = previousAnswer.lastIndexOf(closeTag);- if (closeIdx !== -1) {- return (- previousAnswer.slice(0, closeIdx + closeTag.length) +- '\n\n' +- chunk.answer- );- }- return chunk.answer;- }-- const currentAnswer = chunk.final && previousAnswer ? '' : chunk.answer || '';-- let nextAnswer: string;- if (previousAnswer && currentAnswer.startsWith(previousAnswer)) {- nextAnswer = currentAnswer;- } else {- nextAnswer = previousAnswer + currentAnswer;- }-- if (chunk.start_to_think === true) {- nextAnswer = nextAnswer + '<think>';- }-- if (chunk.end_to_think === true) {- // The closing marker must be followed by a blank line: the answer's first- // line lands right after it, and a heading (`## Candidate Matrix`) glued- // onto '</think>' stops being a heading — markdown only parses `#` at- // line begin.- nextAnswer = nextAnswer + '</think>\n\n';- }-- return nextAnswer;-}-/** Builds the assistant message that replaces the trailing placeholder. */export function buildAssistantMessageFromAnswer(answer: IAnswer): IMessage {return {diff --git a/web/src/utils/__tests__/chat.test.ts b/web/src/utils/__tests__/chat.test.tsindex 085147e033..ff4aa6aacb 100644--- a/web/src/utils/__tests__/chat.test.ts+++ b/web/src/utils/__tests__/chat.test.ts@@ -1,4 +1,56 @@-import { preprocessLaTeX, replaceThinkToSection } from '../chat';+import {+ mergeAnswerChunk,+ preprocessLaTeX,+ replaceThinkToSection,+} from '../chat';++describe('mergeAnswerChunk', () => {+ it.each([+ ['First fact. Second fact.', 'First fact.[ID:0] Second fact.[ID:1]'],+ ['A fact.[ID:0-1]', 'A fact.[ID:0][ID:1]'],+ ])('replaces %s with the final cited answer', (previous, answer) => {+ expect(mergeAnswerChunk(previous, { answer, final: true })).toBe(answer);+ });++ it('keeps streamed thinking when replacing the visible answer', () => {+ const streamed = [+ { start_to_think: true },+ { answer: 'Checking sources.' },+ { end_to_think: true },+ { answer: '## First fact. ' },+ { answer: 'Second fact.' },+ ].reduce(mergeAnswerChunk, '');+ expect(streamed).toBe(+ '<think>Checking sources.</think>\n\n## First fact. Second fact.',+ );+ const answer = mergeAnswerChunk(streamed, {+ answer: '## First fact.[ID:0] Second fact.[ID:1]',+ final: true,+ });++ expect(answer).toBe(+ '<think>Checking sources.</think>\n\n## First fact.[ID:0] Second fact.[ID:1]',+ );+ expect(mergeAnswerChunk(answer, { answer, final: true })).toBe(answer);+ });++ it.each(['', undefined])(+ 'keeps the answer for an empty final chunk (%s)',+ (answer) => {+ const previous = '<think>Checking sources.</think>A fact.[ID:0]';+ expect(mergeAnswerChunk(previous, { answer, final: true })).toBe(+ previous,+ );+ },+ );++ it('accepts a single-shot final answer without duplicating a repeated one', () => {+ const chunk = { answer: 'A fact.[ID:0]', final: true };+ const answer = mergeAnswerChunk('', chunk);+ expect(answer).toBe(chunk.answer);+ expect(mergeAnswerChunk(answer, chunk)).toBe(answer);+ });+});describe('preprocessLaTeX', () => {it('converts block \\[ \\] to $$ $$', () => {diff --git a/web/src/utils/chat.ts b/web/src/utils/chat.tsindex 7041624fb1..382b6ab24e 100644--- a/web/src/utils/chat.ts+++ b/web/src/utils/chat.ts@@ -19,6 +19,7 @@ import {EmptyConversationId,} from '@/constants/chat';import { IMessage, Message } from '@/interfaces/database/chat';+import type { CompletionChunk } from '@/services/chat-completion-stream';import { omit } from 'lodash';import { v4 as uuid } from 'uuid';import {@@ -58,6 +59,43 @@ export const buildMessageUuidWithRole = (return `${message.role}_${message.id}`;};+/** Merges streamed deltas, replacing the visible text with a final answer. */+export function mergeAnswerChunk(+ previousAnswer: string,+ chunk: CompletionChunk,+): string {+ const currentAnswer = chunk.answer || '';++ let nextAnswer: string;+ if (chunk.final && currentAnswer) {+ // Final answers can add or repair citations anywhere in the text.+ // Keep streamed thinking when the final payload only contains the answer.+ const thinkEnd = previousAnswer.lastIndexOf('</think>');+ const thinking =+ thinkEnd >= 0 &&+ !currentAnswer.includes('<think>') &&+ !currentAnswer.includes('</think>')+ ? previousAnswer.slice(0, thinkEnd + '</think>'.length) + '\n\n'+ : '';+ nextAnswer = thinking + currentAnswer;+ } else if (previousAnswer && currentAnswer.startsWith(previousAnswer)) {+ nextAnswer = currentAnswer;+ } else {+ nextAnswer = previousAnswer + currentAnswer;+ }++ if (chunk.start_to_think === true) {+ nextAnswer = nextAnswer + '<think>';+ }++ if (chunk.end_to_think === true) {+ // Keep the answer's first Markdown heading on its own line.+ nextAnswer = nextAnswer + '</think>\n\n';+ }++ return nextAnswer;+}+// Preprocess LaTeX equations to be rendered by KaTeX// ref: https://github.com/remarkjs/react-markdown/issues/785//