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 Agent cited not show during chat (#19986)
Wang Qia46e64f53b27
diff --git a/internal/agent/component/agent.go b/internal/agent/component/agent.goindex aeb0e5677d..ac94a62fce 100644--- a/internal/agent/component/agent.go+++ b/internal/agent/component/agent.go@@ -28,6 +28,7 @@ import ("github.com/cloudwego/eino/schema""gorm.io/gorm"+ "ragflow/internal/agent/chat""ragflow/internal/agent/component/prompts""ragflow/internal/agent/runtime"agenttool "ragflow/internal/agent/tool"@@ -211,9 +212,9 @@ func runEinoReActAgent(ctx context.Context, p AgentParam) (*schema.Message, erro// graph releases its output stream, so agent.Stream does not return// until the model finishes. GetMessageStreams blocks on the future's// started signal (closed by the graph onStart callback), so starting- // the collector first lets thinking deltas stream out in real time+ // the collector first lets non-citation deltas stream out in real time// while the checker runs, instead of buffering the entire round.- emitDone := emitAgentModelStreams(ctx, future)+ emitDone := emitAgentModelStreams(ctx, future, p.Cite)stream, err := agent.Stream(ctx, input, opt)if err != nil {// Drain the collector so its goroutine exits before we return.@@ -345,7 +346,7 @@ func buildAgentInputMessages(ctx context.Context, p AgentParam) []*schema.Messagreturn input}-func emitAgentModelStreams(ctx context.Context, future react.MessageFuture) <-chan error {+func emitAgentModelStreams(ctx context.Context, future react.MessageFuture, cite bool) <-chan error {done := make(chan error, 1)go func() {var firstErr error@@ -386,6 +387,11 @@ func emitAgentModelStreams(ctx context.Context, future react.MessageFuture) <-chif msg.Content == "" && msg.ReasoningContent == "" {continue}+ // Retrieval tools can add chunks during this call. Hold the first+ // answer until we know whether citation grounding will run.+ if cite {+ continue+ }if runtime.AgentMessageEventsEmitted(ctx) && !runtime.HasDeferredAgentMessageSink(ctx) {continue}@@ -446,9 +452,9 @@ func addToolCallMemory(ctx context.Context, db *gorm.DB, p AgentParam, msg *sche// the model to insert [ID:N] tags into the assistant's final// content.//-// Returns the grounded content on success, the original content-// unchanged when no chunks are available or the call fails. Mirrors-// Python's `cite_letter` / `generate_with_citation` flow.+// Streams grounded deltas when the invoker supports streaming. Returns the+// original content when no chunks are available or the call fails before+// emitting anything.func applyCitationGrounding(ctx context.Context, db *gorm.DB, p AgentParam, content string, chunks []prompts.CitationSource) (string, error) {if !p.Cite {return content, nil@@ -461,7 +467,7 @@ func applyCitationGrounding(ctx context.Context, db *gorm.DB, p AgentParam, cont}systemPrompt, _ := prompts.CitationPlusPrompt(chunks)inv := getDefaultChatInvoker()- resp, err := inv.Invoke(ctx, db, ChatInvokeRequest{+ req := ChatInvokeRequest{Driver: p.Driver,ModelName: p.ModelID,APIKey: p.APIKey,@@ -471,18 +477,46 @@ func applyCitationGrounding(ctx context.Context, db *gorm.DB, p AgentParam, cont{Role: schema.User, Content: content},},TopP: p.TopP,- })+ }+ var resp *ChatInvokeResponse+ var err error+ if streamer, ok := inv.(chat.StreamingInvoker); ok {+ var leadingWhitespace strings.Builder+ emitting := false+ resp, err = streamer.Stream(ctx, db, req, func(delta string, isThink bool) error {+ if ctx.Err() != nil {+ return ctx.Err()+ }+ if isThink || delta == "" {+ return nil+ }+ if !emitting {+ leadingWhitespace.WriteString(delta)+ if strings.TrimSpace(leadingWhitespace.String()) == "" {+ return nil+ }+ delta = leadingWhitespace.String()+ emitting = true+ }+ runtime.EmitAgentMessage(ctx, delta, "")+ return nil+ })+ } else {+ resp, err = inv.Invoke(ctx, db, req)+ }if err != nil {// Grounding is best-effort. Return the original content// so the message still flows; the caller can decide// whether to surface the error.return content, err}- grounded := strings.TrimSpace(resp.Content)- if grounded == "" {+ if resp == nil {+ return content, fmt.Errorf("citation grounding returned no response")+ }+ if strings.TrimSpace(resp.Content) == "" {return content, nil}- return grounded, nil+ return resp.Content, nil}// chunksFromState extracts the recorded retrieval chunks from@@ -963,9 +997,8 @@ func (c *AgentComponent) invokeNow(ctx context.Context, db *gorm.DB, inputs map[// the canvas state has recorded retrieval chunks (populated// by the Retrieval tool during the ReAct loop), make a second// LLM call to insert [ID:N] tags into the final content. The- // grounding call is best-effort — on failure the original- // content is kept and the error is surfaced under- // outputs["grounding_error"].+ // grounding call is best-effort until it emits a visible delta. An+ // interrupted citation stream must not be followed by the first answer.// Diagnostic sentinel (temporary — see plan): log the post-// agentRunner state right before the `msg.Content` deref so a// subsequent panic shows whether the agent returned (nil, nil).@@ -995,6 +1028,9 @@ func (c *AgentComponent) invokeNow(ctx context.Context, db *gorm.DB, inputs map[content = groundedgroundingStatus = "applied"} else if gErr != nil {+ if ctx.Err() != nil || runtime.AgentMessageEventsEmitted(ctx) || runtime.DeferredAgentMessageEventsEmitted(ctx) {+ return nil, fmt.Errorf("component: Agent citation grounding stream: %w", gErr)+ }groundingStatus = "error: " + gErr.Error()}}diff --git a/internal/agent/component/agent_test.go b/internal/agent/component/agent_test.goindex e092d32146..ab2fc4031b 100644--- a/internal/agent/component/agent_test.go+++ b/internal/agent/component/agent_test.go@@ -306,7 +306,7 @@ func TestReactCheckerStreamsThinkingBeforeAgentReturns(t *testing.T) {})opt, future := react.WithMessageFuture()- emitDone := emitAgentModelStreams(ctx, future)+ emitDone := emitAgentModelStreams(ctx, future, false)streamCh := make(chan *schema.StreamReader[*schema.Message], 1)errCh := make(chan error, 1)diff --git a/internal/agent/component/grounding_test.go b/internal/agent/component/grounding_test.goindex 69194cf667..d9073d9139 100644--- a/internal/agent/component/grounding_test.go+++ b/internal/agent/component/grounding_test.go@@ -46,6 +46,26 @@ type groundingTestInvoker struct {calls int}+type groundingStreamingInvoker struct {+ groundingTestInvoker+ streamCalls int+}++func (g *groundingStreamingInvoker) Stream(ctx context.Context, db *gorm.DB, req ChatInvokeRequest, onDelta func(string, bool) error) (*ChatInvokeResponse, error) {+ g.streamCalls+++ resp, err := g.Invoke(ctx, db, req)+ if err != nil {+ return nil, err+ }+ mid := len(resp.Content) / 2+ for _, delta := range []string{resp.Content[:mid], resp.Content[mid:]} {+ if err := onDelta(delta, false); err != nil {+ return nil, err+ }+ }+ return resp, nil+}+func (g *groundingTestInvoker) Invoke(_ context.Context, _ *gorm.DB, req ChatInvokeRequest) (*ChatInvokeResponse, error) {g.mu.Lock()defer g.mu.Unlock()@@ -64,7 +84,7 @@ func (g *groundingTestInvoker) Invoke(_ context.Context, _ *gorm.DB, req ChatInv// TestGrounding_Applied: Cite=true + state has chunks → second// LLM call is made and the grounded content replaces the original.func TestGrounding_Applied(t *testing.T) {- inv := &groundingTestInvoker{responses: []string{"grounded answer [ID:0]"}}+ inv := &groundingStreamingInvoker{groundingTestInvoker: groundingTestInvoker{responses: []string{"grounded answer [ID:0]"}}}prev := getDefaultChatInvoker()SetDefaultChatInvoker(inv)defer SetDefaultChatInvoker(prev)@@ -78,6 +98,12 @@ func TestGrounding_Applied(t *testing.T) {{"id": "0", "content": "the source content"},})ctx := runtime.WithState(t.Context(), state)+ var streamed string+ var deltas int+ ctx = runtime.WithAgentMessageEmitter(ctx, func(content, _ string) {+ streamed += content+ deltas+++ })c := NewAgentComponent(AgentParam{ModelID: "stub", Cite: true})out, err := c.Invoke(ctx, nil, map[string]any{"user_prompt": "q"})@@ -93,6 +119,9 @@ func TestGrounding_Applied(t *testing.T) {if inv.calls != 1 {t.Errorf("expected 1 chat call, got %d", inv.calls)}+ if inv.streamCalls != 1 || deltas != 2 || streamed != "grounded answer [ID:0]" {+ t.Errorf("stream calls=%d, deltas=%d, streamed=%q, want 1 / 2 / grounded answer [ID:0]", inv.streamCalls, deltas, streamed)+ }// System message should contain the citation prompt + sources block.if got := inv.lastReq.Messages[0].Role; got != schema.System {t.Errorf("first message role=%v, want System", got)