Skip to content

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

21.6 KB
Raw Blame
//
//  Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

package service

import (
	"context"
	"fmt"
	"strings"
	"time"

	"github.com/cloudwego/eino/schema"
	"go.uber.org/zap"

	"ragflow/internal/agent/runtime"
	"ragflow/internal/agentic_rag"
	"ragflow/internal/common"
	"ragflow/internal/entity"
	modelModule "ragflow/internal/entity/models"
	"ragflow/internal/permission"
	"ragflow/internal/tokenizer"
)

// agenticRunTimeout is the wall-clock budget of one agentic turn. The ReAct
// loop is bounded by MaxIterations on its own, but a provider that answers
// every call just slowly enough can otherwise hold a request open indefinitely.
const agenticRunTimeout = 15 * time.Minute

// defaultAgenticTemplateID is the agent a reasoning-level-selected turn runs.
// It must name a template in conf/agentic_rag.yaml; resolveTemplateFor fails
// the turn loudly if it does not, which is the intended behaviour for an
// operator who removed or renamed the template.
const defaultAgenticTemplateID = "smart-reasoning"

func resolveChatAgenticTemplate(chat *entity.Chat, kwargs map[string]interface{}) (agentic_rag.Template, error) {
	mode, _ := kwargs["agent_mode"].(string)
	if mode == "" {
		mode = defaultAgenticTemplateID
	}
	override, err := agentic_rag.ParseOverride(chat.PromptConfig["agentic_rag"])
	if err != nil {
		return agentic_rag.Template{}, err
	}
	return agentic_rag.ResolveTemplate(mode, override)
}

// agenticRag drives ONE conversation turn through the smart-reasoning agent
// (internal/agentic_rag): an eino-ADK ReAct explorer whose toolset is the
// corpus itself (grep / lexical / semantic locate, list_chunks deep read).
// Dispatched from AsyncChat before the retrieval phases when the request names
// kwargs["agent_mode"] or asks for reasoning level reasoningLevelAgentic - the
// agent runs its own retrieval, so the pipeline's search and generation phases
// do not apply.
//
// The streamed shape matches the naive pipeline's: thinking deltas framed by
// StartToThink/EndToThink, and exactly one Final result carrying the answer
// with its reference payload built from the answer's own chunk_id citations
// (see buildAgenticReference).
// defaultAgenticTokenBudget is the per-run token ceiling. It bounds the cost of
// a runaway search; 0 disables the ceiling.
const defaultAgenticTokenBudget = 500_000

func (s *ChatPipelineService) agenticRag(
	ctx context.Context,
	chat *entity.Chat,
	messages []map[string]interface{},
	stream bool,
	kwargs map[string]interface{},
	quote bool,
) (<-chan AsyncChatResult, error) {
	userID := permission.UserIDFromContext(ctx)
	tmpl, err := resolveChatAgenticTemplate(chat, kwargs)
	if err != nil {
		return nil, err
	}
	out := make(chan AsyncChatResult, 16)
	// Wire the document-metadata resolver the metadata_search tool needs. The
	// eino agentic_rag package must not import service (service imports it), so
	// the concrete *service.MetadataService is injected here, per turn.
	agentic_rag.SetMetadataService(NewMetadataService())
	// agent_mode selects the template id for this run (validated non-empty by
	// AsyncChat before dispatch). Resolved per-run so conf/agentic_rag.yaml
	// edits take effect without restart.
	//
	// A turn selected by reasoning level reasoningLevelAgentic names no
	// template — the level is a retrieval strategy, not a config id — so it
	// lands on the one agent this build ships. An explicit agent_mode still
	// wins and is still validated strictly, so a bad id from a direct API
	// caller fails loudly rather than silently running a different agent.
	//
	// AsyncChat only dispatches here with a non-empty KB scope, so the
	// citation lookup below is always kb_id-bounded.
	mode := tmpl.ID

	go func() {
		defer close(out)

		runCtx, cancel := context.WithTimeoutCause(ctx, agenticRunTimeout, fmt.Errorf("agentic run timed out after %s", agenticRunTimeout))
		defer cancel()
		// The run's evidence registry: every tool result stamps its passages with
		// the [ID:n] handle the model cites, and buildAgenticReference resolves
		// those handles against the same numbering.
		registry := agentic_rag.NewEvidenceRegistry()
		runCtx = agentic_rag.WithEvidenceRegistry(runCtx, registry)
		// The turn's question, published on the run context because the
		// sufficiency filter needs it per passage while the retrieval tools are
		// never given it.
		query := lastUserQuestion(messages)
		runCtx = agentic_rag.WithQuery(runCtx, query)
		// The run's token ceiling. Exceeding it cancels this run and the
		// terminating fallback answers from the evidence already gathered, so a
		// runaway search costs a bounded amount instead of an unbounded one.
		if limit := common.GetEnvInt(common.EnvAgenticTokenBudget, defaultAgenticTokenBudget); limit > 0 {
			runCtx = common.WithTokenBudget(runCtx, int64(limit))
			defer common.LogTokenBudget(runCtx)
		}
		// Per-turn token accounting. Every LLM call inside the run records its
		// usage into this sink (models.EinoChatModel does it for both the
		// non-streaming and streaming paths). Its totals are emitted on the final
		// result and stored with the assistant message.
		runCtx = tokenizer.WithRunUsage(runCtx)
		runCtx = tokenizer.WithRunAttrs(runCtx, &tokenizer.RunAttrs{UserID: userID})
		turnStarted := time.Now()
		// The retrieval tools authorize their datasets as the CALLER of the run,
		// not as the chat's tenant: NLPRetrievalAdapter.Search reads that identity
		// from the run's runtime state (state.Sys["user_id"]) and treats a missing
		// state as an empty subject, which fails every dataset permission check and
		// kills the vector-only locate tool. The canvas path gets this state from
		// its compile step; the agentic path has no canvas, so attach it here.
		runState := runtime.NewCanvasState("", chat.ID)
		if userID != "" {
			runState.SetSys("user_id", userID)
			runState.SetSys("tenant_id", chat.TenantID)
		}
		runCtx = runtime.WithState(runCtx, runState)
		// A result is abandoned only when the CALLER is gone. runCtx is not the
		// delivery signal: the token budget cancels it precisely so that the
		// terminating fallback can answer from the evidence already gathered, so
		// treating that cancellation as "nobody is listening" dropped the very
		// answer the run had just built and shipped an empty reply. Delivery is
		// tried first and unconditionally, because out is buffered and the
		// consumer always drains it.
		sendResult := func(result AsyncChatResult) bool {
			select {
			case out <- result:
				return true
			default:
			}
			select {
			case out <- result:
				return true
			case <-ctx.Done():
				return false
			}
		}
		var answerFilter, reasoningFilter citationStreamFilter
		emitResult := func(result AsyncChatResult) bool {
			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 != "" {
						if !sendResult(AsyncChatResult{Reasoning: text}) {
							return false
						}
					}
					if text := answerFilter.flush(); text != "" {
						if !sendResult(AsyncChatResult{Answer: text}) {
							return false
						}
					}
				}
				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 true
					}
				}
			}
			return sendResult(result)
		}

		chain, chainErr := s.agenticModelChain(runCtx, chat)
		if chainErr != nil {
			common.ErrorCtx(runCtx, "agentic_rag: resolve chat model", chainErr)
			emitResult(AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", chainErr.Error()), Final: true})
			return
		}
		docIDs := s.chatMetadataDocIDs(runCtx, chat, lastUserQuestion(messages), chain[0], nil, chatDatasetIDs(chat))
		modelType := "chat"
		if info := chain[0].Info(); info != nil && entity.ModelTypeFromStrings(info.ModelTypes).Has(entity.ModelTypeImage2Text) {
			modelType = "image2text"
		}
		agentMessages := s.convertMessagesToEino(runCtx, modelType, messages)
		// Image-bearing turns cannot fail over to a text-only model.
		for _, msg := range agentMessages {
			if len(msg.UserInputMultiContent) > 0 {
				visionChain := chain[:0]
				for _, candidate := range chain {
					if info := candidate.Info(); info != nil && entity.ModelTypeFromStrings(info.ModelTypes).Has(entity.ModelTypeImage2Text) {
						visionChain = append(visionChain, candidate)
					}
				}
				chain = visionChain
				break
			}
		}

		// Apply the dialog's LLM settings with per-request overrides exactly
		// like the regular AsyncChat path does, then let the template pin its
		// sampling when it declares one (smart-reasoning pins 0.5).
		chatCfg := BuildChatConfig(chat, kwargs)
		if temp := tmpl.Temperature; temp != nil {
			pinned := *chatCfg
			pinned.Temperature = temp
			chatCfg = &pinned
		}
		// Keep agent and synthesis on the ONE resolved model set, so a failover
		// that moved the chain's cursor mid-turn cannot leave the researcher
		// and the summariser on different providers.
		//
		// A single-member chain is wrapped by the same failover constructor: it
		// is the identity case (one entry, nothing to fail over to) and keeps
		// one construction path instead of branching on len(chain).
		model, chainErr := modelModule.NewFailoverEinoChatModel(chain, chatCfg)
		if chainErr != nil {
			common.ErrorCtx(runCtx, "agentic_rag: build chat model", chainErr)
			emitResult(AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", chainErr.Error()), Final: true})
			return
		}
		synth, synthErr := modelModule.NewFailoverEinoChatModel(chain, chatCfg)
		if synthErr != nil {
			common.ErrorCtx(runCtx, "agentic_rag: build synthesis model", synthErr)
			emitResult(AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", synthErr.Error()), Final: true})
			return
		}

		// Per-turn tool accounting, logged (not shipped on the result: the
		// channel contract here is the naive pipeline's answer/reference one).
		toolCounts := map[string]int{}
		toolErrors := map[string]int{}

		// thinking tracks whether we are inside the think block so the
		// StartToThink marker is emitted once and EndToThink fires when the
		// agent's final answer arrives.
		thinking := false
		var reasoning strings.Builder
		final, runErr := agentic_rag.Run(runCtx, agentic_rag.Input{
			Model:           model,
			SynthModel:      synth,
			Messages:        agentMessages,
			TemplateID:      mode,
			Template:        &tmpl,
			TenantID:        chat.TenantID,
			UserID:          userID,
			DatasetIDs:      chatDatasetIDs(chat),
			DocIDs:          docIDs,
			ConversationKey: chat.ID,
			Stream:          stream,
			ToolCallCounts:  toolCounts,
			ToolCallErrors:  toolErrors,
			OnDelta: func(contentDelta, thinkingDelta string) {
				reasoning.WriteString(thinkingDelta)
				startToThink, endToThink := false, false
				if thinkingDelta != "" {
					if !thinking {
						startToThink = true
						thinking = true
					}
				} else if thinking {
					endToThink = true
					thinking = false
				}
				// Markers travel on their own chunks: the frontend appends
				// '<think>' / '</think>' AFTER the chunk's answer text, so a
				// marker riding on a content chunk would strand that text on
				// the wrong side of the think section.
				if startToThink {
					emitResult(AsyncChatResult{Final: false, StartToThink: true})
				}
				if endToThink {
					emitResult(AsyncChatResult{Final: false, EndToThink: true})
				}
				if contentDelta != "" || thinkingDelta != "" {
					emitResult(AsyncChatResult{
						Answer:    contentDelta,
						Reasoning: thinkingDelta,
						Final:     false,
					})
				}
			},
		})
		if thinking {
			emitResult(AsyncChatResult{Final: false, EndToThink: true})
		}
		usageFields := agenticUsageFields(runCtx, tokenizer.GetRunUsage(runCtx))
		if runErr != nil {
			common.ErrorCtx(runCtx, "agentic_rag: run", runErr)
			// A failed turn still spent tokens; log them under their own event so
			// the cost of timeouts and tool failures is visible instead of
			// disappearing with the error.
			common.InfoCtx(ctx, "agentic turn failed", append([]zap.Field{
				zap.String("mode", mode),
				zap.Bool("stream", stream),
				zap.Int64("elapsed_ms", time.Since(turnStarted).Milliseconds()),
				zap.Any("tool_calls", toolCounts),
				zap.Any("tool_errors", toolErrors),
				zap.String("error", runErr.Error()),
			}, usageFields...)...)
			emitResult(AsyncChatResult{Answer: fmt.Sprintf("**ERROR**: %s", runtime.MarkdownSafeErrorText(runErr)), Final: true})
			return
		}
		common.InfoCtx(ctx, "agentic turn done", append([]zap.Field{
			zap.String("mode", mode),
			zap.Bool("stream", stream),
			zap.Int64("elapsed_ms", time.Since(turnStarted).Milliseconds()),
			zap.Any("tool_calls", toolCounts),
			zap.Any("tool_errors", toolErrors),
			zap.Int("final_bytes", len(final)),
		}, usageFields...)...)

		// Citations off means neither the [ID:N] markers nor the reference
		// payload, matching what the regular pipeline ships when quote is
		// false.
		answer := final
		if reasoning.Len() > 0 {
			answer = agentSessionMessageContent("\n\n"+final, reasoning.String())
		}
		reference := map[string]interface{}{}
		if quote {
			reference, answer = s.buildAgenticReference(ctx, chat.TenantID, chatDatasetIDs(chat), answer, registry)
		} else {
			if len(agentic_rag.CitedIDsFromMarkers(answer, registry)) > 0 || len(agentic_rag.ExtractCitedChunkIDs(answer)) > 0 {
				common.InfoCtx(ctx, "agentic citations suppressed by quote=false")
			}
			answer = stripCitations(answer)
		}
		// What the turn cost, on the wire for the same reason the log line
		// exists: nothing else tells the client. The chat page renders it under
		// the answer, and a caller comparing runs reads it instead of grepping
		// the server log.
		promptTokens, completionTokens, totalTokens, _ := tokenizer.GetRunUsage(runCtx).Snapshot()

		// Persist the same thinking and final answer the stream renders, with
		// the final answer's citation handles compacted against its reference.
		emitResult(AsyncChatResult{
			Answer:                answer,
			Reference:             reference,
			Final:                 true,
			AnswerIsAuthoritative: true,
			Usage: &ChatTurnUsage{
				PromptTokens:     promptTokens,
				CompletionTokens: completionTokens,
				TotalTokens:      totalTokens,
			},
			ElapsedMs: float64(time.Since(turnStarted).Milliseconds()),
		})
	}()

	return out, nil
}

// agenticUsageFields renders a turn's LLM cost as structured log fields.
//
// The final response and stored message carry token totals. The log also records
// call counts and per-phase totals for benchmarks and support investigations.
// The per-call breakdown goes to the debug log to keep the INFO line one line
// per turn.
//
// usage_missing_calls counts calls whose provider reported no usable split.
// They are counted rather than silently folded into the totals, so "cheap"
// turns that actually lost usage are distinguishable from genuinely cheap ones.
func agenticUsageFields(ctx context.Context, usage *tokenizer.RunUsage) []zap.Field {
	if usage == nil {
		return []zap.Field{zap.Bool("usage_tracked", false)}
	}
	promptTokens, completionTokens, totalTokens, calls := usage.Snapshot()
	records := usage.CallSnapshot()
	missing := 0
	byModel := map[string]int{}
	byPhase := map[string]int{}
	topModel, topTokens := "", 0
	for _, call := range records {
		if call.TotalTokens <= 0 {
			missing++
		}
		if call.Model != "" {
			byModel[call.Model] += call.TotalTokens
			if byModel[call.Model] > topTokens {
				topModel, topTokens = call.Model, byModel[call.Model]
			}
		}
		phase := call.Phase
		if phase == "" {
			phase = "unspecified"
		}
		byPhase[phase] += call.TotalTokens
		common.DebugCtx(ctx, "agentic_rag: llm call",
			zap.Int("seq", call.Seq),
			zap.Float64("at_seconds", call.AtSeconds),
			zap.String("model", call.Model),
			zap.String("phase", call.Phase),
			zap.Int("prompt_tokens", call.PromptTokens),
			zap.Int("completion_tokens", call.CompletionTokens),
			zap.Int("total_tokens", call.TotalTokens))
	}

	fields := []zap.Field{
		zap.Int("prompt_tokens", promptTokens),
		zap.Int("completion_tokens", completionTokens),
		zap.Int("total_tokens", totalTokens),
		zap.Int("llm_calls", calls),
		zap.Int("usage_missing_calls", missing),
		zap.Any("tokens_by_phase", byPhase),
		zap.String("top_model", topModel),
	}
	if calls > 0 {
		fields = append(fields, zap.Int("tokens_per_call", totalTokens/calls))
	}
	if len(byModel) > 1 {
		fields = append(fields, zap.Any("tokens_by_model", byModel))
	}
	if missing > 0 {
		fields = append(fields, zap.Bool("usage_incomplete", true))
	}
	return fields
}

// agenticModelChain resolves only the model explicitly selected by the dialog
// (or the tenant default when no model is selected), plus any failover members
// the dialog configures. Agentic RAG must not silently broaden the dialog's
// model choice to every chat model owned by the tenant: a failover chain is
// exactly the list the dialog's author chose, and nothing else.
func (s *ChatPipelineService) agenticModelChain(ctx context.Context, chat *entity.Chat) ([]*modelModule.ChatModel, error) {
	ctx = permission.WithTenantID(ctx, chat.TenantID)
	primary, err := s.agenticPrimaryModel(ctx, chat)
	if err != nil {
		return nil, err
	}

	chain := []*modelModule.ChatModel{primary}
	// Failover members are best-effort per entry: a member that no longer
	// resolves (deleted, deactivated, or re-typed) must not take the whole turn
	// down when a working primary exists. Skipping it keeps the surviving
	// members usable instead of collapsing to a hard error.
	for _, llmID := range agenticFailoverModelIDs(chat) {
		if llmID == "" || llmID == chat.LLMID {
			continue
		}
		model, resolveErr := s.ModelFactory.NewChatModel(ctx, llmID)
		if resolveErr != nil || model == nil {
			common.WarnCtx(ctx, "agentic_rag: skipping unresolvable failover model",
				zap.String("llm_id", llmID), zap.Error(resolveErr))
			continue
		}
		chain = append(chain, model)
	}
	common.InfoCtx(ctx, "agentic model chain resolved",
		zap.Int("chain", len(chain)), zap.String("primary", chat.LLMID))
	return chain, nil
}

// agenticPrimaryModel resolves the dialog's own model, or the tenant default
// when it selected none.
func (s *ChatPipelineService) agenticPrimaryModel(ctx context.Context, chat *entity.Chat) (*modelModule.ChatModel, error) {
	var (
		model *modelModule.ChatModel
		err   error
	)
	if chat.LLMID == "" {
		model, err = s.ModelFactory.NewDefaultChatModel(ctx)
	} else {
		model, err = s.ModelFactory.NewChatModel(ctx, chat.LLMID)
	}
	if err != nil {
		return nil, fmt.Errorf("resolve chat model: %w", err)
	}
	if model == nil {
		return nil, fmt.Errorf("resolve chat model: no chat model resolved for tenant %s", chat.TenantID)
	}
	return model, nil
}

// agenticFailoverModelIDs reads the dialog's ordered failover list from
// llm_setting. Order is the author's priority: EinoChatModel walks the chain in
// order and the sticky cursor keeps a healthy member in front, so a member that
// failed once is not re-probed on every call of a long turn.
//
// The list is per conversation and lives in llm_setting rather than a tenant
// group table, so it needs no cross-entity join and cannot outlive its dialog.
func agenticFailoverModelIDs(chat *entity.Chat) []string {
	if chat == nil || chat.LLMSetting == nil {
		return nil
	}
	raw, ok := chat.LLMSetting["failover_llm_ids"]
	if !ok {
		return nil
	}
	items, ok := raw.([]interface{})
	if !ok {
		return nil
	}
	out := make([]string, 0, len(items))
	for _, item := range items {
		if id, ok := item.(string); ok && id != "" {
			out = append(out, id)
		}
	}
	return out
}

// convertMessagesToEino converts pre-filtered user/assistant messages into
// eino schema messages, resolving uploaded attachments throughout the history.
func (s *ChatPipelineService) convertMessagesToEino(ctx context.Context, modelType string, messages []map[string]interface{}) []*schema.Message {
	out := make([]*schema.Message, 0, len(messages))
	for _, m := range messages {
		role, _ := m["role"].(string)
		content, _ := m["content"].(string)
		switch role {
		case "user":
			text, images := s.splitChatAttachments(ctx, m["files"])
			if text != "" {
				content += "\n\n" + text
			}
			msg := schema.UserMessage(content)
			images, _ = gateImageAttachments("", modelType, images)
			if len(images) > 0 {
				msg.UserInputMultiContent = append(msg.UserInputMultiContent, schema.MessageInputPart{Type: schema.ChatMessagePartTypeText, Text: content})
				for _, uri := range images {
					msg.UserInputMultiContent = append(msg.UserInputMultiContent, schema.MessageInputPart{
						Type:  schema.ChatMessagePartTypeImageURL,
						Image: &schema.MessageInputImage{MessagePartCommon: schema.MessagePartCommon{URL: new(uri)}},
					})
				}
			}
			out = append(out, msg)
		case "assistant":
			out = append(out, schema.AssistantMessage(content, nil))
		default:
			// system messages are stripped upstream; skip anything else.
			continue
		}
	}
	return out
}

// chatDatasetIDs reads the conversation's bound dataset ids.
func chatDatasetIDs(chat *entity.Chat) []string {
	if chat == nil {
		return nil
	}
	ids := make([]string, 0, len(chat.KBIDs))
	for _, raw := range chat.KBIDs {
		if id, ok := raw.(string); ok && id != "" {
			ids = append(ids, id)
		}
	}
	return ids
}