Skip to content

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

12.0 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.
//

// BotService is the shared service layer for the public
// chatbot/agentbot endpoints (api/v1/chatbots/...,
// api/v1/agentbots/...) plus the agent attachment download. It is
// intentionally a thin aggregator — it sequences permission checks and DAO
// lookups, and delegates the heavy work (LLM call, canvas run) to the existing
// services.
package service

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"

	"ragflow/internal/agent/canvas"
	"ragflow/internal/agent/dsl"
	"ragflow/internal/common"
	"ragflow/internal/dao"
	"ragflow/internal/engine/kvrocks"
	"ragflow/internal/entity"
	"ragflow/internal/permission"
	permissionresponse "ragflow/internal/permission/response"
)

// BotService coordinates chatbot + agentbot reads and the matching
// completion paths. Mirrors the Python
// `api/db/services/conversation_service.py::async_iframe_completion`
// + `api/db/services/canvas_service.py::completion` flow but stays
// stateless — it does not own the LLM or canvas runner; it just
// sequences them.
type BotService struct {
	chatDAO             *dao.ChatSessionDAO
	canvasDAO           *dao.UserCanvasDAO
	api4ConversationDAO *dao.API4ConversationDAO
	agentService        *AgentService
	pipeline            *ChatPipelineService
}

// NewBotService wires a fresh BotService. agentSvc is required for
// AgentbotCompletion and is nullable in unit tests.
func NewBotService(agentSvc *AgentService) *BotService {
	return &BotService{
		chatDAO:             dao.NewChatSessionDAO(),
		canvasDAO:           dao.NewUserCanvasDAO(),
		api4ConversationDAO: dao.NewAPI4ConversationDAO(),
		agentService:        agentSvc,
		pipeline:            NewChatPipelineService(),
	}
}

// ChatbotInfo returns the public metadata of a chatbot dialog.
func (s *BotService) ChatbotInfo(ctx context.Context, dialogID string) (
	title, avatar, prologue, llmID string, hasWebSearch bool, ec common.ErrorCode, err error,
) {
	if err = permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindChat, ID: dialogID}, permission.OperationRead); err != nil {
		ec, err = permissionresponse.NormalizeHidden(err)
		return "", "", "", "", false, ec, err
	}

	dialog, err := s.chatDAO.GetDialogByID(ctx, dao.DB, dialogID)
	if err != nil {
		if dao.IsNotFoundErr(err) {
			ec, err = permissionresponse.NormalizeHidden(permission.ErrResourceNotFound)
			return "", "", "", "", false, ec, err
		}
		return "", "", "", "", false, common.CodeServerError, err
	}
	if dialog == nil {
		ec, err = permissionresponse.NormalizeHidden(permission.ErrResourceNotFound)
		return "", "", "", "", false, ec, err
	}
	pc := dialog.PromptConfig
	// Defensive lookups mirroring python's
	// dialog.prompt_config.get("prologue", "") and
	// resolveWebSearchProvider(dialog.prompt_config) != nil
	// semantics. A hard type assertion here would panic on a missing
	// or non-string prologue field — this endpoint is public over
	// persisted JSON config and the schema is not guaranteed.
	prologue = stringFromMap(pc, "prologue")
	return botDerefStr(dialog.Name), botDerefStr(dialog.Icon), prologue,
		dialog.LLMID, resolveWebSearchProvider(pc) != nil, common.CodeSuccess, nil
}

// AgentbotInputs returns the public metadata of an agentbot canvas.
//
// Mirrors the python `bot_api.py::agentbot_inputs` handler. The
// authorisation check is the same IDOR guard the production
// AgentService uses (canvas must be visible to the requesting user).
func (s *BotService) AgentbotInputs(ctx context.Context, agentID string) (
	title, avatar, prologue, mode string, inputs map[string]any,
	ec common.ErrorCode, err error,
) {
	cv, err := s.loadCanvas(ctx, agentID, permission.OperationRead)
	if err != nil {
		return "", "", "", "", nil, common.CodeDataError, err
	}
	dslMap := canvasDSLMap(cv)
	// Resolve the begin component ID first, then pass that ID to
	// ExtractComponentInputForm. ExtractComponentInputForm is keyed
	// by component ID, NOT component name — passing the literal
	// "begin" would only succeed when the canvas happens to use
	// "begin" as the component ID.
	beginID, idErr := dsl.FindBeginComponentID(dslMap)
	if idErr != nil {
		// No begin component (or malformed DSL). Degrade gracefully —
		// empty prologue / mode / inputs, matching the Python
		// behaviour when Canvas.get_component_input_form returns an
		// empty dict.
		return botDerefStr(cv.Title), botDerefStr(cv.Avatar), "", "", nil, common.CodeSuccess, nil
	}
	inputs, _ = dsl.ExtractComponentInputForm(dslMap, beginID)
	prologue, _ = dsl.ExtractPrologue(dslMap)
	mode, _ = dsl.ExtractMode(dslMap)
	return botDerefStr(cv.Title), botDerefStr(cv.Avatar), prologue, mode, inputs, common.CodeSuccess, nil
}

// AgentbotCompletion is a thin wrapper around AgentService.RunAgent
// for the /api/v1/agentbots/<agent_id>/completions endpoint.
//
// Defence-in-depth (security H2): the IDOR guard runs BEFORE the
// delegate so an unauthorised caller can never trigger canvas
// compile/invoke (which would spend LLM tokens + emit canvas
// telemetry even for "not found" paths). RunAgent re-runs the
// same guard internally — this is intentional; the upstream check
// is the cheap fast-fail that costs a single DAO roundtrip
// instead of a full canvas compile.
func (s *BotService) AgentbotCompletion(
	ctx context.Context, agentID string, req AgentbotCompletionRequest,
) (<-chan canvas.RunEvent, common.ErrorCode, error) {
	question, err := ResolveCompletionQuestion(req.Question, req.Query, req.Messages)
	if err != nil {
		return nil, common.CodeArgumentError, err
	}
	req.Question = question
	if s.agentService == nil {
		return nil, common.CodeServerError, fmt.Errorf("bot: agent service not wired")
	}
	if _, err := s.loadCanvas(ctx, agentID, permission.OperationRun); err != nil {
		return nil, common.CodeDataError, err
	}
	userInput := NormalizeAgentInput(question, req.UserInput)
	var ch <-chan canvas.RunEvent
	if req.Release != nil && *req.Release {
		ch, err = s.agentService.runReleasedAgent(ctx, agentID,
			req.SessionID, userInput, req.Files)
	} else {
		ch, err = s.agentService.RunAgent(ctx, agentID,
			req.SessionID, "", userInput, req.Files)
	}
	if err != nil {
		return nil, common.CodeDataError, err
	}
	return ch, common.CodeSuccess, nil
}

// AgentbotLogs returns the stored execution timeline for an agentbot run.
// Access is scoped to the caller's accessible tenants, matching the Python
// agent_bot_logs endpoint.
func (s *BotService) AgentbotLogs(ctx context.Context, agentID, messageID string) (map[string]any, common.ErrorCode, error) {
	if _, err := s.loadCanvas(ctx, agentID, permission.OperationRead); err != nil {
		return nil, common.CodeDataError, err
	}
	payload, err := kvrocks.Get().Get(ctx, fmt.Sprintf("%s-%s-logs", agentID, messageID))
	if err != nil {
		return nil, common.CodeServerError, errors.New("failed to read agent logs")
	}
	data := map[string]any{}
	if payload == "" {
		return data, common.CodeSuccess, nil
	}
	if err := json.Unmarshal([]byte(payload), &data); err != nil {
		return nil, common.CodeServerError, errors.New("failed to decode agent logs")
	}
	return data, common.CodeSuccess, nil
}

// AgentbotCompletionRequest is the request body for
// /api/v1/agentbots/<agent_id>/completions. We intentionally accept
// the same fields the production /agents/chat/completions handler
// accepts; the URL-bound agent_id is the authoritative canvas id
// (matches python bot_api.py:159).
type AgentbotCompletionRequest struct {
	Messages  []map[string]interface{} `json:"messages,omitempty"`
	SessionID string                   `json:"session_id"`
	UserID    string                   `json:"user_id"`
	Stream    bool                     `json:"stream"`
	// Query is the free-text chat question. The shared/embedded chat
	// page sends `query` (the Python completion reads it before
	// `question`).
	Query string `json:"query"`
	// UserInput carries the begin-form field values the canvas run
	// expects when the flow starts from a form instead of free text.
	UserInput map[string]any           `json:"inputs"`
	Question  string                   `json:"question"`
	Files     []map[string]interface{} `json:"files"`
	Release   *bool                    `json:"release,omitempty"`
}

// ChatbotCompletionRequest is the request body for
// /api/v1/chatbots/<dialog_id>/completions. Mirrors the python
// `async_iframe_completion` body shape (session_id, question,
// tts (unused) and a freeform dict).
type ChatbotCompletionRequest struct {
	Query     string                   `json:"query,omitempty"`
	Messages  []map[string]interface{} `json:"messages,omitempty"`
	SessionID string                   `json:"session_id"`
	Question  string                   `json:"question"`
	Stream    bool                     `json:"stream"`
	Inputs    map[string]any           `json:"inputs"`
	// Quote controls citation generation. Nil means "absent" —
	// python bot_api.py defaults it to False for chatbot
	// completions, so the service layer mirrors that.
	Quote *bool `json:"quote"`
	// Reasoning / Internet arrive as bool OR 0/1 number depending on the
	// widget. Internet is normalised by normalizeInternetFlag before reaching
	// the pipeline (chat_pipeline.go); Reasoning is passed through verbatim as
	// the 0..4 agentic-RAG level so medium/high/ultra levels are not collapsed
	// to a bool — resolveReasoningLevel reads it directly.
	Reasoning any `json:"reasoning"`
	Internet  any `json:"internet"`
	// DocIDs is an optional comma-separated document filter,
	// same shape as the regular chat completion kwargs.
	DocIDs string `json:"doc_ids"`
}

// loadCanvas authorizes a bot operation before loading its canvas. Keeping this
// guard here prevents public bot endpoints from depending on AgentService.
func (s *BotService) loadCanvas(ctx context.Context, agentID string, operation permission.Operation) (*entity.UserCanvas, error) {
	if agentID == "" {
		return nil, dao.ErrUserCanvasNotFound
	}
	if err := permission.NewDatabaseChecker(dao.DB).CheckResource(ctx, permission.ResourceRef{Kind: permission.ResourceKindCanvas, ID: agentID}, operation); err != nil {
		if errors.Is(err, permission.ErrResourceNotFound) || errors.Is(err, permission.ErrPermissionDenied) || errors.Is(err, permission.ErrUnauthenticated) {
			return nil, dao.ErrUserCanvasNotFound
		}
		return nil, fmt.Errorf("bot: check canvas %s permission: %w", agentID, err)
	}
	return s.canvasDAO.GetByID(ctx, dao.DB, agentID)
}

// canvasDSLMap projects a UserCanvas.DSL JSONMap into a
// map[string]any. Returns an empty map (not nil) on miss so
// downstream dsl helpers can still scan it.
func canvasDSLMap(cv *entity.UserCanvas) map[string]any {
	if cv == nil {
		return map[string]any{}
	}
	// cv.DSL is entity.JSONMap (alias for map[string]interface{}).
	// We must return a fresh map[string]any because the dsl
	// helpers expect that concrete type.
	return map[string]any(cv.DSL)
}

// botDerefStr returns *s or "" if nil. Used to read pointer-string
// fields on entities (Name, Icon, Title, Avatar). Prefixed with bot
// to avoid colliding with the test-only botDerefStr in
// openai_chat_test.go.
func botDerefStr(s *string) string {
	if s == nil {
		return ""
	}
	return *s
}

// stringFromMap returns m[key] as a string. Returns "" if the key is
// absent or the value is not a string. Used for defensive reads
// over JSONMap-shaped fields (dialog.prompt_config) where a hard
// type assertion would panic.
func stringFromMap(m entity.JSONMap, key string) string {
	if m == nil {
		return ""
	}
	v, ok := m[key]
	if !ok || v == nil {
		return ""
	}
	if s, ok := v.(string); ok {
		return s
	}
	return ""
}