Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

43.1 KB
Raw Blame
package tools

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"math"
	"regexp"
	"sort"
	"strings"
	"sync"
	"unicode/utf8"

	"github.com/Tencent/WeKnora/internal/config"
	"github.com/Tencent/WeKnora/internal/logger"
	"github.com/Tencent/WeKnora/internal/models/rerank"
	"github.com/Tencent/WeKnora/internal/reranking"
	"github.com/Tencent/WeKnora/internal/searchutil"
	"github.com/Tencent/WeKnora/internal/types"
	"github.com/Tencent/WeKnora/internal/types/interfaces"
)

// Retrieval modes accepted by search_knowledge.
const (
	SearchModeHybrid   = "hybrid"
	SearchModeSemantic = "semantic"
	SearchModeKeyword  = "keyword"
)

const (
	searchKnowledgeDefaultLimit = 10
	searchKnowledgeMaxLimit     = 30
)

// errSearchPanicked reports a retrieval adapter that panicked. The barrier
// logs the stack and the knowledge base is reported as failed, exactly like a
// search that returned an error; the panic value itself is never surfaced.
var errSearchPanicked = errors.New("knowledge base search failed with an internal error")

var searchKnowledgeTool = BaseTool{
	name: ToolSearchKnowledge,
	description: "Search the knowledge bases in scope and return the most relevant chunks with their content.\n" +
		"mode \"hybrid\" (default) combines semantic and keyword retrieval. \"semantic\" ranks by meaning and " +
		"tolerates paraphrase. \"keyword\" matches the literal terms and is the right choice for identifiers, " +
		"error codes, product names and exact phrases. A base without the requested index is searched with the " +
		"index it has; the result reports that as a mode fallback.\n" +
		"In hybrid and semantic mode write query as one complete natural-language question or statement that " +
		"names the subject and what you want to know (\"Why does Echo use open-weight models to cut cost?\"), " +
		"not a keyword list (\"Echo open-weight cost\"): results are ranked by a relevance model that scores " +
		"meaning. In keyword mode write the exact terms. Pass knowledge_base_ids to focus on the bases whose " +
		"profile fits the question. When nothing passes the relevance check, rephrase with terms the documents " +
		"would use; repeating the same query in another mode usually does not help. For identifiers, error " +
		"codes or exact names retry with mode=keyword.\n" +
		"Every chunk carries a chunk://N handle and belongs to a doc://N " +
		"document. Use read_document(id=chunk://N, context=k) to " +
		"read the text around a chunk, and read_document(id=doc://N) to read a " +
		"document from its start. Retrieval " +
		"matches chunk text; the relevance model also " +
		"sees the document title. To find a document by its title or file name use list_documents(keyword=...).",
	schema: json.RawMessage(`{
  "type": "object",
  "properties": {
    "query": {
      "type": "string",
      "description": "A complete natural-language question, not a keyword list; exact terms when mode is keyword",
      "minLength": 1
    },
    "mode": {
      "type": "string",
      "enum": ["hybrid", "semantic", "keyword"],
      "description": "Retrieval mode (default hybrid)"
    },
    "knowledge_base_ids": {
      "type": "array",
      "description": "Optional kb://N knowledge-base handles from the runtime context to restrict the search",
      "items": { "type": "string" },
      "maxItems": 10
    },
    "limit": {
      "type": "integer",
      "description": "Maximum chunks to return (default 10, max 30)",
      "minimum": 1,
      "maximum": 30
    }
  },
  "required": ["query"]
}`),
}

// SearchKnowledgeInput defines the input parameters for search_knowledge.
type SearchKnowledgeInput struct {
	Query            string   `json:"query"`
	Mode             string   `json:"mode,omitempty"`
	KnowledgeBaseIDs []string `json:"knowledge_base_ids,omitempty"`
	Limit            int      `json:"limit,omitempty"`
}

// searchResultWithMeta wraps search result with metadata about which query matched it
type searchResultWithMeta struct {
	*types.SearchResult
	SourceQuery       string
	QueryType         string // retrieval path that produced the row
	KnowledgeBaseID   string // ID of the knowledge base this result came from
	KnowledgeBaseType string // Type of the knowledge base (document, faq, etc.)
}

// SearchKnowledgeTool searches chunk-indexed knowledge bases in semantic,
// keyword or hybrid mode and returns reranked, de-duplicated chunks.
type SearchKnowledgeTool struct {
	BaseTool
	knowledgeBaseService interfaces.KnowledgeBaseService
	knowledgeService     interfaces.KnowledgeService
	chunkService         interfaces.ChunkService
	searchTargets        types.SearchTargets // Pre-computed unified search targets
	rerankModel          rerank.Reranker
	config               *config.Config // Global config for fallback values
	// patternFilter, when set, keeps only results whose text matches it.
	// It is not exposed to the model; the MCP grep_chunks endpoint uses it
	// to keep grep semantics on top of index-backed retrieval.
	patternFilter *regexp.Regexp
	// contextImages attaches the images of results found by an image's own
	// vector, for a model that can see them; see WithContextImages.
	contextImages bool
}

// WithContextImages attaches to the result the images of the results that
// rest on an image matched by its own vector, at most
// searchutil.MaxContextImages. Enable it only for a vision model: the agent
// describes tool images to a model that cannot see them, and these already
// carry captions.
func (t *SearchKnowledgeTool) WithContextImages(enabled bool) *SearchKnowledgeTool {
	t.contextImages = enabled
	return t
}

// WithPatternFilter restricts results to chunks whose text matches re. The
// candidate pool is widened to the maximum limit before filtering.
func (t *SearchKnowledgeTool) WithPatternFilter(re *regexp.Regexp) *SearchKnowledgeTool {
	t.patternFilter = re
	return t
}

// NewSearchKnowledgeTool creates a new search_knowledge tool.
func NewSearchKnowledgeTool(
	knowledgeBaseService interfaces.KnowledgeBaseService,
	knowledgeService interfaces.KnowledgeService,
	chunkService interfaces.ChunkService,
	searchTargets types.SearchTargets,
	rerankModel rerank.Reranker,
	cfg *config.Config,
) *SearchKnowledgeTool {
	return &SearchKnowledgeTool{
		BaseTool:             searchKnowledgeTool,
		knowledgeBaseService: knowledgeBaseService,
		knowledgeService:     knowledgeService,
		chunkService:         chunkService,
		searchTargets:        searchTargets,
		rerankModel:          rerankModel,
		config:               cfg,
	}
}

// normalizeSearchMode validates the mode argument and applies the default.
func normalizeSearchMode(mode string) (string, error) {
	mode = strings.ToLower(strings.TrimSpace(mode))
	switch mode {
	case "":
		return SearchModeHybrid, nil
	case SearchModeHybrid, SearchModeSemantic, SearchModeKeyword:
		return mode, nil
	default:
		return "", fmt.Errorf("mode must be one of hybrid, semantic, keyword (got %q)", mode)
	}
}

// clampSearchLimit applies the default and upper bound for limit.
func clampSearchLimit(limit int) int {
	if limit <= 0 {
		return searchKnowledgeDefaultLimit
	}
	if limit > searchKnowledgeMaxLimit {
		return searchKnowledgeMaxLimit
	}
	return limit
}

// Execute executes the search.
func (t *SearchKnowledgeTool) Execute(ctx context.Context, args json.RawMessage) (*types.ToolResult, error) {
	var input SearchKnowledgeInput
	if err := json.Unmarshal(args, &input); err != nil {
		return &types.ToolResult{Success: false, Error: fmt.Sprintf("Failed to parse args: %v", err)}, err
	}

	query := strings.TrimSpace(input.Query)
	if query == "" {
		return &types.ToolResult{Success: false, Error: "query is required"}, fmt.Errorf("no query provided")
	}
	mode, err := normalizeSearchMode(input.Mode)
	if err != nil {
		return &types.ToolResult{Success: false, Error: err.Error()}, err
	}
	limit := clampSearchLimit(input.Limit)

	// Optional KB filter: reject hallucinated or out-of-scope handles.
	searchTargets := t.searchTargets
	if len(input.KnowledgeBaseIDs) > 0 {
		if err := validateKnowledgeBaseIDsInSearchTargets(t.searchTargets, input.KnowledgeBaseIDs); err != nil {
			return &types.ToolResult{Success: false, Error: err.Error()}, err
		}
		wanted := make(map[string]bool, len(input.KnowledgeBaseIDs))
		for _, kbID := range input.KnowledgeBaseIDs {
			wanted[kbID] = true
		}
		var filtered types.SearchTargets
		for _, target := range t.searchTargets {
			if target != nil && wanted[target.KnowledgeBaseID] {
				filtered = append(filtered, target)
			}
		}
		searchTargets = filtered
	}
	if len(searchTargets) == 0 {
		return &types.ToolResult{
			Success: false,
			Error:   "no knowledge base is in scope for this search",
		}, fmt.Errorf("no search targets available")
	}

	kbIDs := searchTargets.GetAllKnowledgeBaseIDs()
	kbList, err := t.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, kbIDs)
	if err != nil {
		logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to load knowledge bases %v: %v", kbIDs, err)
	}
	kbModes, msg := resolveKBSearchModes(mode, kbList)
	if msg != "" {
		return &types.ToolResult{Success: false, Error: msg}, fmt.Errorf("%s", msg)
	}
	kbTypeMap := make(map[string]string, len(kbList))
	for _, kb := range kbList {
		if kb != nil {
			kbTypeMap[kb.ID] = kb.Type
		}
	}

	retrievalLimit := limit
	if t.patternFilter != nil {
		retrievalLimit = searchKnowledgeMaxLimit
	}
	topK, vectorThreshold, keywordThreshold := t.retrievalParams(retrievalLimit)
	logger.Infof(ctx,
		"[Tool][SearchKnowledge] query=%q mode=%s limit=%d top_k=%d targets=%d kbs=%d",
		query, mode, limit, topK, len(searchTargets), len(kbIDs))

	allResults, searchFailures, searchCalls, embedFallbacks := t.concurrentSearchByTargets(ctx, query, mode,
		kbModes, searchTargets, kbList, topK, vectorThreshold, keywordThreshold, kbTypeMap)
	// A hybrid search that ran keyword-only is reported like any other mode
	// fallback, so an empty result is not read as "no semantic match".
	for kbID, reason := range embedFallbacks {
		kbModes[kbID] = kbSearchMode{mode: SearchModeKeyword, fallback: true, reason: reason}
	}
	// A failed search is not an empty one. Reporting "no matching chunks"
	// when the vector store timed out or the embedding call failed made the
	// model conclude the knowledge base has no answer.
	if searchCalls > 0 && len(searchFailures) >= searchCalls {
		msg := "Knowledge search failed: " + strings.Join(searchFailures, "; ") +
			". The knowledge bases were not searched, so this is not evidence that they lack the answer."
		return &types.ToolResult{Success: false, Output: msg, Error: msg}, nil
	}

	deduplicated := t.deduplicateResultsForRerank(allResults)

	ranked := deduplicated
	rerankRejected := 0
	if t.rerankModel != nil && len(deduplicated) > 0 {
		reranked, err := t.rerankResults(ctx, query, deduplicated, mode == SearchModeKeyword)
		if err != nil {
			logger.Warnf(ctx, "[Tool][SearchKnowledge] Rerank failed, using retrieval order: %v", err)
		} else {
			ranked = reranked
			if len(reranked) == 0 {
				rerankRejected = len(deduplicated)
			}
		}
	}

	// Results the rerank stage kept outside its top-k (pictorial images a
	// text reranker could not judge) skip MMR and the limit, and follow the
	// ranked results.
	ranked, keptOutside := splitKeptOutsideTopK(ranked)
	if len(ranked) > 0 {
		if selected := selectMMR(ctx, ranked, min(len(ranked), retrievalLimit)); len(selected) > 0 {
			ranked = selected
		}
	}

	final := t.deduplicateResults(ranked)
	sort.Slice(final, func(i, j int) bool {
		if final[i].Score != final[j].Score {
			return final[i].Score > final[j].Score
		}
		return final[i].KnowledgeID < final[j].KnowledgeID
	})
	if t.patternFilter != nil {
		final = t.matchingPattern(ctx, final)
		keptOutside = t.matchingPattern(ctx, keptOutside)
	}
	if len(final) > limit {
		final = final[:limit]
	}
	if len(keptOutside) > 0 {
		// A kept image whose caption hit made the list is a copy of it; the
		// caption hit stays and takes the image along.
		final = t.deduplicateResults(append(final, keptOutside...))
	}

	// Enrich image info for search results (lazy-loaded from child image chunks)
	if t.chunkService != nil && len(final) > 0 {
		byTenant := make(map[uint64][]*types.SearchResult)
		for _, r := range final {
			tid := t.searchTargets.GetTenantIDForKB(r.KnowledgeBaseID)
			if tid == 0 {
				continue
			}
			byTenant[tid] = append(byTenant[tid], r.SearchResult)
		}
		for tid, batch := range byTenant {
			searchutil.EnrichSearchResultsImageInfo(ctx, t.chunkService.GetRepository(), tid, batch)
		}
	}

	result, omitted := t.formatWithinBudget(ctx, final, kbIDs, query, mode)
	if omitted > 0 {
		result.Data["omitted_for_budget"] = omitted
	}
	t.attachContextImages(ctx, result, final[:len(final)-omitted])
	if len(searchFailures) > 0 {
		result.Data["partial_failures"] = searchFailures
	}
	annotateModeFallback(result.Data, mode, kbModes)
	if rerankRejected > 0 {
		result.Data["rerank_rejected"] = rerankRejected
	}
	if len(final) == 0 {
		result.Output = emptySearchStatement(query, result.Data, len(kbIDs))
	}
	return result, nil
}

// splitKeptOutsideTopK separates the results kept outside the rerank top-k
// from the ranked ones, each in input order.
func splitKeptOutsideTopK(results []*searchResultWithMeta) (ranked, kept []*searchResultWithMeta) {
	for _, r := range results {
		if r != nil && searchutil.IsKeptOutsideTopK(r.SearchResult) {
			kept = append(kept, r)
		} else {
			ranked = append(ranked, r)
		}
	}
	return ranked, kept
}

// matchingPattern keeps the results whose text matches patternFilter.
func (t *SearchKnowledgeTool) matchingPattern(
	ctx context.Context, results []*searchResultWithMeta,
) []*searchResultWithMeta {
	matched := results[:0]
	for _, r := range results {
		if t.patternFilter.MatchString(t.getEnrichedPassage(ctx, r.SearchResult)) {
			matched = append(matched, r)
		}
	}
	return matched
}

// attachContextImages adds to result the images of the shown results that
// rest on an image matched by its own vector, and names the chunks they
// belong to.
func (t *SearchKnowledgeTool) attachContextImages(
	ctx context.Context, result *types.ToolResult, shown []*searchResultWithMeta,
) {
	if !t.contextImages || t.knowledgeBaseService == nil || len(shown) == 0 {
		return
	}
	rows := make([]*types.SearchResult, len(shown))
	for i, r := range shown {
		rows[i] = r.SearchResult
	}
	images, positions := searchutil.ContextImages(ctx, rows, t.knowledgeBaseService.ReadChunkImage,
		searchutil.MaxContextImages)
	if len(images) == 0 {
		return
	}
	ids := make([]string, len(positions))
	for i, pos := range positions {
		ids[i] = rows[pos].ID
	}
	result.Images = images
	result.Data["context_images"] = ids
	result.Output += fmt.Sprintf("\n\nAttached %d retrieved image(s), in order, for chunk_id %s. "+
		"They were matched by what they show, which their captions may not describe.",
		len(images), strings.Join(ids, ", "))
}

// formatWithinBudget renders results, dropping the lowest-ranked ones until
// the rendering fits in four fifths of the tool output budget, and reports
// how many were dropped. The model reads a view rebuilt from Data, which the
// registry's output truncation does not reach, so 30 full chunks used to go
// into the context whatever the budget. The best result is always kept.
func (t *SearchKnowledgeTool) formatWithinBudget(
	ctx context.Context,
	results []*searchResultWithMeta,
	kbIDs []string,
	query, mode string,
) (*types.ToolResult, int) {
	budget := OutputBudget(ctx) * 4 / 5
	kept := results
	result := t.formatOutput(ctx, kept, kbIDs, query, mode)
	for len(kept) > 1 {
		size := utf8.RuneCountInString(result.Output)
		if size <= budget {
			break
		}
		// Shrink in proportion to the overshoot, at least one row per round.
		n := min(len(kept)-1, max(1, len(kept)*budget/size))
		kept = kept[:n]
		result = t.formatOutput(ctx, kept, kbIDs, query, mode)
	}
	return result, len(results) - len(kept)
}

// emptySearchStatement describes an empty result, including any mode
// fallback, so the model does not read "no semantic neighbours" as "the
// exact term does not occur". When retrieval found candidates and the rerank
// model rejected all of them, it says so: repeating the same query in
// another mode is usually wasted, but keyword mode can still surface a
// different candidate set for identifiers and exact terms.
func emptySearchStatement(query string, data map[string]interface{}, kbCount int) string {
	mode, _ := data["mode"].(string)
	msg := fmt.Sprintf("No matching chunks for %q (mode=%s) in %d knowledge base(s).", query, mode, kbCount)
	if rejected, _ := data["rerank_rejected"].(int); rejected > 0 {
		msg = fmt.Sprintf("No chunk passed the relevance check for %q (mode=%s) in %d knowledge base(s): "+
			"retrieval found %d candidate chunks but the relevance model scored none of them as answering the "+
			"query. Repeating the same query in another mode usually will not help; rephrase with terms the "+
			"documents would use.",
			query, mode, kbCount, rejected)
		if mode != SearchModeKeyword {
			msg += " For identifiers, error codes or exact names retry with mode=keyword."
		}
	} else if mode == SearchModeKeyword {
		msg += " No chunk contains these exact terms; try the terms the documents would use, or mode=hybrid " +
			"with a natural-language question."
	}
	fallbacks, _ := data["mode_fallbacks"].([]map[string]interface{})
	for _, fb := range fallbacks {
		msg += fmt.Sprintf(" Knowledge base %v was searched with mode=%v (%v).",
			fb["knowledge_base_id"], fb["mode"], fb["reason"])
	}
	// An empty result renders from this text alone, so a failed search in
	// part of the scope must be stated here, or it reads as "not found".
	if failures, _ := data["partial_failures"].([]string); len(failures) > 0 {
		msg += " Some knowledge bases could not be searched, so this is not evidence that they lack the " +
			"answer: " + strings.Join(failures, "; ") + "."
	}
	return msg
}

// kbSearchMode is the retrieval path used for one knowledge base.
type kbSearchMode struct {
	mode     string
	fallback bool   // mode differs from the requested one
	reason   string // why the requested mode could not be served
}

// resolveKBSearchModes decides, per knowledge base, which retrieval path
// serves the requested mode. A base that lacks the requested index falls
// back to the one it has (FAQ bases have no keyword index; vector-only bases
// have no keyword index; keyword-only bases have no vector index) instead of
// failing the whole call. Bases with no chunk index at all (wiki-only /
// graph-only) are left out. The call is refused only when no base in scope
// can be searched. An empty kbs list (lookup failed) resolves to nil, which
// callers treat as "use the requested mode everywhere".
func resolveKBSearchModes(mode string, kbs []*types.KnowledgeBase) (map[string]kbSearchMode, string) {
	if len(kbs) == 0 {
		return nil, ""
	}
	modes := make(map[string]kbSearchMode, len(kbs))
	for _, kb := range kbs {
		if kb == nil {
			continue
		}
		hasVector := kb.IsVectorEnabled()
		hasKeyword := kb.IsKeywordEnabled() && kb.Type != types.KnowledgeBaseTypeFAQ
		switch {
		case !hasVector && !hasKeyword:
			continue
		case mode == SearchModeKeyword && !hasKeyword:
			reason := "no keyword index"
			if kb.Type == types.KnowledgeBaseTypeFAQ {
				reason = "FAQ bases are indexed for semantic search only"
			}
			modes[kb.ID] = kbSearchMode{mode: SearchModeSemantic, fallback: true, reason: reason}
		case mode == SearchModeSemantic && !hasVector:
			modes[kb.ID] = kbSearchMode{mode: SearchModeKeyword, fallback: true, reason: "no vector index"}
		default:
			modes[kb.ID] = kbSearchMode{mode: mode}
		}
	}
	if len(modes) == 0 {
		return nil, "none of the selected knowledge bases has a chunk index; use a wiki or graph tool for this scope"
	}
	return modes, ""
}

// annotateModeFallback records in the result which bases were searched with
// a different mode than requested, so neither the model nor the UI mistakes
// a semantic fallback for literal keyword matches.
func annotateModeFallback(data map[string]interface{}, requested string, kbModes map[string]kbSearchMode) {
	if data == nil {
		return
	}
	data["requested_mode"] = requested
	var fallbacks []map[string]interface{}
	used := make(map[string]bool)
	ids := make([]string, 0, len(kbModes))
	for kbID := range kbModes {
		ids = append(ids, kbID)
	}
	sort.Strings(ids)
	for _, kbID := range ids {
		m := kbModes[kbID]
		used[m.mode] = true
		if m.fallback {
			fallbacks = append(fallbacks, map[string]interface{}{
				"knowledge_base_id": kbID, "mode": m.mode, "reason": m.reason,
			})
		}
	}
	if len(fallbacks) == 0 {
		return
	}
	data["mode_fallbacks"] = fallbacks
	if len(used) == 1 {
		for effective := range used {
			data["mode"] = effective
		}
	}
}

// retrievalParams resolves the candidate pool size and recall thresholds from
// the global configuration, with hard-coded fallbacks.
func (t *SearchKnowledgeTool) retrievalParams(limit int) (topK int, vectorThreshold, keywordThreshold float64) {
	if t.config != nil && t.config.Conversation != nil {
		topK = t.config.Conversation.EmbeddingTopK
		vectorThreshold = t.config.Conversation.VectorThreshold
		keywordThreshold = t.config.Conversation.KeywordThreshold
	}
	if topK <= 0 {
		topK = searchKnowledgeMaxLimit
	}
	if topK < limit {
		topK = limit
	}
	if vectorThreshold == 0 {
		vectorThreshold = 0.6
	}
	if keywordThreshold == 0 {
		keywordThreshold = 0.5
	}
	return topK, vectorThreshold, keywordThreshold
}

// concurrentSearchByTargets executes retrieval using pre-computed search targets.
// Targets sharing the same model name, endpoint and configured vector dimension
// are grouped so each group computes one query embedding and combines all
// full-KB targets into a single retrieval call.
func (t *SearchKnowledgeTool) concurrentSearchByTargets(
	ctx context.Context,
	query string,
	mode string,
	kbModes map[string]kbSearchMode,
	searchTargets types.SearchTargets,
	kbList []*types.KnowledgeBase,
	topK int,
	vectorThreshold, keywordThreshold float64,
	kbTypeMap map[string]string,
) (_ []*searchResultWithMeta, failures []string, calls int, embedFallbacks map[string]string) {
	// Filter out non-searchable KBs (wiki-only / graph-only). Feeding a
	// wiki-only KB into HybridSearch causes spurious "model ID cannot be
	// empty" errors because such KBs have no EmbeddingModelID configured.
	// KBs that we couldn't fetch (not in kbList) are kept so the downstream
	// HybridSearch path can still surface the real error.
	knownKBs := make(map[string]bool, len(kbList))
	for _, kb := range kbList {
		if kb != nil {
			knownKBs[kb.ID] = true
		}
	}
	modeFor := func(kbID string) (string, bool) {
		if m, ok := kbModes[kbID]; ok {
			return m.mode, true
		}
		// Unknown to the lookup: keep it so HybridSearch surfaces the real error.
		return mode, !knownKBs[kbID]
	}
	filteredTargets := make(types.SearchTargets, 0, len(searchTargets))
	for _, st := range searchTargets {
		if st == nil || st.KnowledgeBaseID == "" {
			continue
		}
		if _, ok := modeFor(st.KnowledgeBaseID); ok {
			filteredTargets = append(filteredTargets, st)
			continue
		}
		logger.Infof(ctx, "[Tool][SearchKnowledge] Skipping non-searchable KB %s (no vector/keyword index)",
			st.KnowledgeBaseID)
	}
	if len(filteredTargets) == 0 {
		return nil, nil, 0, nil
	}
	searchTargets = filteredTargets

	// Resolve actual model identities (name + endpoint + configured dimension)
	// for cross-tenant grouping
	modelKeyMap := t.knowledgeBaseService.ResolveEmbeddingModelKeys(ctx, kbList)

	groups := make(map[string][]*types.SearchTarget)
	for _, st := range searchTargets {
		key := modelKeyMap[st.KnowledgeBaseID]
		groups[key] = append(groups[key], st)
	}

	var wg sync.WaitGroup
	var mu sync.Mutex
	allResults := make([]*searchResultWithMeta, 0)
	// fail records one search that returned an error, or a search that
	// could not run; calls counts every search attempted.
	fail := func(kbIDs []string, err error) {
		mu.Lock()
		defer mu.Unlock()
		failures = append(failures, fmt.Sprintf("%v: %v", kbIDs, err))
	}
	attempt := func() {
		mu.Lock()
		defer mu.Unlock()
		calls++
	}
	// fallBack records a hybrid knowledge base searched by keyword only
	// because the query embedding failed.
	fallBack := func(kbID string, err error) {
		mu.Lock()
		defer mu.Unlock()
		if embedFallbacks == nil {
			embedFallbacks = make(map[string]string)
		}
		embedFallbacks[kbID] = fmt.Sprintf("query embedding failed, semantic search skipped: %v", err)
	}
	collect := func(rows []*types.SearchResult, usedMode string) {
		mu.Lock()
		defer mu.Unlock()
		for _, r := range rows {
			allResults = append(allResults, &searchResultWithMeta{
				SearchResult:      r,
				SourceQuery:       query,
				QueryType:         usedMode,
				KnowledgeBaseID:   r.KnowledgeBaseID,
				KnowledgeBaseType: kbTypeMap[r.KnowledgeBaseID],
			})
		}
	}

	for modelKey, targets := range groups {
		wg.Add(1)
		go func(modelKey string, targets []*types.SearchTarget) {
			defer wg.Done()
			// The embedding adapter runs here, before any target is searched: a
			// panic from it is contained and every target still pending in the
			// group is reported as failed instead of ending the process.
			defer RecoverGoroutine(ctx, "search knowledge embedding group", func() {
				ids := make([]string, 0, len(targets))
				for _, st := range targets {
					ids = append(ids, st.KnowledgeBaseID)
				}
				fail(ids, errSearchPanicked)
			})

			needsEmbedding := false
			for _, st := range targets {
				if m, _ := modeFor(st.KnowledgeBaseID); m != SearchModeKeyword {
					needsEmbedding = true
					break
				}
			}
			// Compute embedding once for this (model, query) pair
			var queryEmbedding []float32
			var embedErr error
			if modelKey != "" && needsEmbedding {
				emb, err := t.knowledgeBaseService.GetQueryEmbedding(ctx, targets[0].KnowledgeBaseID, query)
				if err != nil {
					logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to pre-compute embedding for model %s: %v",
						modelKey, err)
					embedErr = err
				} else {
					queryEmbedding = emb
				}
			}
			// Without a query embedding every vector search would re-embed
			// (with retries) and fail again. Hybrid targets fall back to
			// keyword search, as the chat pipeline does; vector-only targets
			// cannot run and are reported as failed.
			groupModeFor := func(kbID string) (string, bool) {
				m, ok := modeFor(kbID)
				if embedErr != nil && m == SearchModeHybrid {
					return SearchModeKeyword, ok
				}
				return m, ok
			}
			runnable := targets[:0:0]
			for _, st := range targets {
				if m, _ := groupModeFor(st.KnowledgeBaseID); embedErr != nil && m == SearchModeSemantic {
					attempt()
					fail([]string{st.KnowledgeBaseID}, fmt.Errorf("query embedding failed: %w", embedErr))
					continue
				}
				if m, _ := modeFor(st.KnowledgeBaseID); embedErr != nil && m == SearchModeHybrid {
					fallBack(st.KnowledgeBaseID, embedErr)
				}
				runnable = append(runnable, st)
			}
			targets = runnable

			// Separate full-KB targets (combinable per retrieval mode) from
			// specific-knowledge targets.
			fullKBIDsByMode := make(map[string][]string)
			var knowledgeTargets []*types.SearchTarget
			for _, st := range targets {
				if st.Type == types.SearchTargetTypeKnowledgeBase && len(st.TagIDs) == 0 {
					m, _ := groupModeFor(st.KnowledgeBaseID)
					fullKBIDsByMode[m] = append(fullKBIDsByMode[m], st.KnowledgeBaseID)
				} else {
					knowledgeTargets = append(knowledgeTargets, st)
				}
			}

			var innerWg sync.WaitGroup
			for usedMode, fullKBIDs := range fullKBIDsByMode {
				innerWg.Add(1)
				go func(usedMode string, fullKBIDs []string) {
					defer innerWg.Done()
					defer RecoverGoroutine(ctx, "search knowledge combined retrieval", func() {
						fail(fullKBIDs, errSearchPanicked)
					})
					attempt()
					kbResults, err := t.knowledgeBaseService.HybridSearch(ctx, fullKBIDs[0], types.SearchParams{
						QueryText:            query,
						QueryEmbedding:       queryEmbedding,
						KnowledgeBaseIDs:     fullKBIDs,
						MatchCount:           topK,
						VectorThreshold:      vectorThreshold,
						KeywordThreshold:     keywordThreshold,
						DisableVectorMatch:   usedMode == SearchModeKeyword,
						DisableKeywordsMatch: usedMode == SearchModeSemantic,
						// Neighbor, parent and related chunks came back as
						// extra rows that were never retrieved, tripling the
						// rerank input; read_document(chunk://N, context=k) serves
						// surrounding text on demand.
						SkipContextEnrichment: true,
					})
					if err != nil {
						logger.Warnf(ctx, "[Tool][SearchKnowledge] Combined search failed for KBs %v: %v",
							fullKBIDs, err)
						fail(fullKBIDs, err)
						return
					}
					collect(kbResults, usedMode)
				}(usedMode, fullKBIDs)
			}

			for _, target := range knowledgeTargets {
				st := target
				innerWg.Add(1)
				go func() {
					defer innerWg.Done()
					defer RecoverGoroutine(ctx, "search knowledge retrieval", func() {
						fail([]string{st.KnowledgeBaseID}, errSearchPanicked)
					})
					attempt()
					usedMode, _ := groupModeFor(st.KnowledgeBaseID)
					stVectorThreshold, stKeywordThreshold := st.RecallThresholds(vectorThreshold, keywordThreshold)
					kbResults, err := t.knowledgeBaseService.HybridSearch(ctx, st.KnowledgeBaseID, types.SearchParams{
						QueryText:            query,
						QueryEmbedding:       queryEmbedding,
						MatchCount:           topK,
						VectorThreshold:      stVectorThreshold,
						KeywordThreshold:     stKeywordThreshold,
						KnowledgeIDs:         st.KnowledgeIDs,
						TagIDs:               st.TagIDs,
						ScopeTagIDs:          st.ScopeTagIDs,
						DisableVectorMatch:   usedMode == SearchModeKeyword,
						DisableKeywordsMatch: usedMode == SearchModeSemantic,
						// Neighbor, parent and related chunks came back as
						// extra rows that were never retrieved, tripling the
						// rerank input; read_document(chunk://N, context=k) serves
						// surrounding text on demand.
						SkipContextEnrichment: true,
					})
					if err != nil {
						logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to search KB %s: %v", st.KnowledgeBaseID, err)
						fail([]string{st.KnowledgeBaseID}, err)
						return
					}
					collect(kbResults, usedMode)
				}()
			}
			innerWg.Wait()
		}(modelKey, targets)
	}
	wg.Wait()
	return allResults, failures, calls, embedFallbacks
}

// rerankResults scores all search results (including FAQ entries) with the
// configured rerank model through the shared rerank stage, keeping those
// that pass the threshold with composite scores, best first.
//
// A failed rerank call degrades to the raw retrieval order. An empty result
// is kept empty: the shared stage already preserves the top candidate down to
// its fallback floor, so reaching zero means even the best match is below it.
//
// In keyword mode the rerank model only orders the hits. The exact-term
// match is the evidence, and rerank models score a bare identifier such as
// ERR_4012 low against the chunk that defines it, so filtering rejected
// every hit and the empty result sent the model back to keyword mode again.
func (t *SearchKnowledgeTool) rerankResults(
	ctx context.Context,
	query string,
	results []*searchResultWithMeta,
	orderOnly bool,
) ([]*searchResultWithMeta, error) {
	if len(results) == 0 || t.rerankModel == nil {
		return results, nil
	}

	rows := make([]*types.SearchResult, len(results))
	for i, r := range results {
		rows[i] = r.SearchResult
	}
	threshold := t.rerankThreshold()
	if orderOnly {
		threshold = math.Inf(-1)
	}
	_, vectorThreshold, _ := t.retrievalParams(0)
	opts := reranking.Options{
		Threshold:        threshold,
		FallbackMinScore: reranking.FallbackMinScore(t.searchTargets.HasRecallThresholdOverride()),
		MaxCandidates:    reranking.DefaultMaxCandidates,
		ImageKeepScore:   reranking.ImageKeepScoreFor(vectorThreshold),
	}
	if t.knowledgeBaseService != nil {
		opts.LoadImage = t.knowledgeBaseService.ReadChunkImage
		if ids := types.SearchResultKnowledgeBaseIDs(rows); len(ids) > 0 {
			kbs, err := t.knowledgeBaseService.GetKnowledgeBasesByIDsOnly(ctx, ids)
			if err != nil {
				logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to read question generation config: %v", err)
			} else {
				types.ApplySkipGeneratedQuestions(rows, types.QuestionGenerationOffIDs(kbs))
			}
		}
	}
	res := reranking.Rerank(ctx, t.rerankModel, query, rows, opts)
	if res.Diagnostics.Outcome == types.RerankOutcomeModelError {
		logger.Warnf(ctx, "[Tool][SearchKnowledge] Rerank model failed, using raw retrieval results: %s",
			res.Diagnostics.Error)
		return results, nil
	}

	reranked := make([]*searchResultWithMeta, 0, len(res.Results))
	for i, row := range res.Results {
		withMeta := *results[res.Indices[i]]
		withMeta.SearchResult = row
		reranked = append(reranked, &withMeta)
	}
	logger.Infof(ctx, "[Tool][SearchKnowledge] Reranked %d/%d results above threshold %.2f (%s)",
		len(reranked), len(results), threshold, res.Diagnostics.Outcome)
	return reranked, nil
}

// faqMetadataFromResult parses FAQ metadata carried on the search result.
// Retrieval already loaded the chunk, including its metadata; re-reading it
// per hit cost one query each and, being tenant scoped, failed for FAQ KBs
// shared from another workspace, which then rendered as plain chunks without
// their answers.
func faqMetadataFromResult(result *types.SearchResult) (*types.FAQChunkMetadata, error) {
	if result == nil || len(result.ChunkMetadata) == 0 {
		return nil, nil
	}
	return (&types.Chunk{Metadata: result.ChunkMetadata}).FAQMetadata()
}

func (t *SearchKnowledgeTool) rerankThreshold() float64 {
	if t.config != nil && t.config.Conversation != nil && t.config.Conversation.RerankThreshold > 0 {
		return t.config.Conversation.RerankThreshold
	}
	return reranking.DefaultThreshold
}

// deduplicateResults removes duplicate chunks. Uses the chunk ID, the
// document position of text chunks and a content signature for near-duplicate
// detection, and returns the survivors best score first.
//
// Rows are visited best score first (stable), so of two duplicates the
// better-scored one survives rather than whichever search returned first.
// Sibling chunks under one parent are distinct text and are kept: keying on
// the parent dropped the child that held the answer whenever a sibling
// arrived first. The document-position key applies to text chunks only;
// image and FAQ chunks all carry chunk_index 0, so it collapsed every image
// chunk of a document, and the document's first text chunk, into one row.
func (t *SearchKnowledgeTool) deduplicateResults(results []*searchResultWithMeta) []*searchResultWithMeta {
	return t.deduplicateResultsWithImageCandidates(results, false)
}

// Image vectors and captions can have identical text but must be judged
// independently. Only collapse their text copies after reranking.
func (t *SearchKnowledgeTool) deduplicateResultsForRerank(results []*searchResultWithMeta) []*searchResultWithMeta {
	return t.deduplicateResultsWithImageCandidates(results, true)
}

func (t *SearchKnowledgeTool) deduplicateResultsWithImageCandidates(
	results []*searchResultWithMeta, beforeRerank bool,
) []*searchResultWithMeta {
	seen := make(map[string]bool)
	contentSig := make(map[string]*searchResultWithMeta)
	uniqueResults := make([]*searchResultWithMeta, 0, len(results))

	ordered := make([]*searchResultWithMeta, 0, len(results))
	for _, r := range results {
		if r != nil && r.SearchResult != nil {
			ordered = append(ordered, r)
		}
	}
	sort.SliceStable(ordered, func(i, j int) bool { return ordered[i].Score > ordered[j].Score })

	for _, r := range ordered {
		keys := []string{r.ID}
		isText := r.ChunkType == "" || r.ChunkType == string(types.ChunkTypeText)
		if r.KnowledgeID != "" && isText {
			keys = append(keys, fmt.Sprintf("kb:%s#%d", r.KnowledgeID, r.ChunkIndex))
		}
		dup := false
		for _, k := range keys {
			if seen[k] {
				dup = true
				break
			}
		}
		if dup {
			continue
		}
		sig := searchutil.BuildContentSignature(r.Content)
		if beforeRerank && searchutil.IsImageEvidence(r.SearchResult) {
			// Exact chunk-ID dedup above still applies. Distinct image hits
			// must not collapse even when their captions are identical.
			sig = ""
		}
		if sig != "" {
			if kept, ok := contentSig[sig]; ok {
				// An image hit and its caption hit read the same once merged;
				// the image goes with whichever copy stays.
				searchutil.InheritImageEvidence(kept.SearchResult, r.SearchResult)
				continue
			}
			contentSig[sig] = r
		}
		for _, k := range keys {
			seen[k] = true
		}
		uniqueResults = append(uniqueResults, r)
	}
	return uniqueResults
}

// writeKnowledgeMetadataHeader emits document-scoped metadata once per
// knowledge item. Chunk entries keep only chunk-specific content so repeated
// results from the same document do not waste context.
func writeKnowledgeMetadataHeader(ob *strings.Builder, results []*searchResultWithMeta) {
	seen := make(map[string]struct{}, len(results))
	hasMetadata := false
	var documents strings.Builder
	for _, result := range results {
		if result == nil || result.SearchResult == nil || result.KnowledgeID == "" ||
			result.KnowledgeCustomMetadata == "" {
			continue
		}
		if _, ok := seen[result.KnowledgeID]; ok {
			continue
		}
		seen[result.KnowledgeID] = struct{}{}
		hasMetadata = true
		fmt.Fprintf(&documents,
			"<document knowledge_id=\"%s\" knowledge_base_id=\"%s\" title=\"%s\">\n",
			xmlEscape(result.KnowledgeID),
			xmlEscape(result.KnowledgeBaseID),
			xmlEscape(result.KnowledgeTitle),
		)
		fmt.Fprintf(&documents, "<metadata>%s</metadata>\n", xmlEscape(result.KnowledgeCustomMetadata))
		documents.WriteString("</document>\n")
	}
	if !hasMetadata {
		return
	}
	ob.WriteString("<documents>\n")
	ob.WriteString(documents.String())
	ob.WriteString("</documents>\n")
}

// formatOutput builds the structured Data (the source of truth for the UI and
// the model rendering) and a compact XML Output for logs.
func (t *SearchKnowledgeTool) formatOutput(
	ctx context.Context,
	results []*searchResultWithMeta,
	kbsToSearch []string,
	query string,
	mode string,
) *types.ToolResult {
	data := map[string]interface{}{
		"knowledge_base_ids": kbsToSearch,
		"query":              query,
		"queries":            []string{query}, // legacy alias for older frontends
		"mode":               mode,
		"results":            []interface{}{},
		"count":              0,
		"display_type":       "search_results",
	}
	if len(results) == 0 {
		return &types.ToolResult{
			Success: true,
			Output: fmt.Sprintf("No matching chunks for %q (mode=%s) in %d knowledge base(s).",
				query, mode, len(kbsToSearch)),
			Data: data,
		}
	}

	kbCounts := make(map[string]int)
	for _, r := range results {
		kbCounts[r.KnowledgeBaseID]++
	}

	var ob strings.Builder
	fmt.Fprintf(&ob, "<search_results count=\"%d\" mode=\"%s\">\n", len(results), xmlEscape(mode))
	fmt.Fprintf(&ob, "<query>%s</query>\n", xmlEscape(query))
	writeKnowledgeMetadataHeader(&ob, results)

	formattedResults := make([]map[string]interface{}, 0, len(results))
	queries := []string{query}

	for i, result := range results {
		var faqMeta *types.FAQChunkMetadata
		if result.KnowledgeBaseType == types.KnowledgeBaseTypeFAQ {
			meta, err := faqMetadataFromResult(result.SearchResult)
			if err != nil {
				logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to load FAQ metadata for chunk %s: %v",
					result.ID, err)
			} else {
				faqMeta = meta
			}
		}
		isFAQ := faqMeta != nil

		snippet := ""
		if isFAQ {
			snippet = faqMatchSnippetFromQueries(faqMeta, queries)
		}
		if snippet == "" && t.patternFilter != nil {
			snippet = extractSnippetRegex(result.Content, []*regexp.Regexp{t.patternFilter})
		}
		if snippet == "" {
			snippet = extractSnippetForQueries(result.Content, queries)
		}

		tag := "chunk"
		idAttr := fmt.Sprintf("chunk_id=\"%s\" chunk_index=\"%d\" knowledge_id=\"%s\"",
			xmlEscape(result.ID), result.ChunkIndex, xmlEscape(result.KnowledgeID))
		if isFAQ {
			tag = "faq"
			idAttr = fmt.Sprintf("faq_id=\"%s\" index=\"%d\"", xmlEscape(result.ID), result.ChunkIndex)
		}
		fmt.Fprintf(&ob, "<%s rank=\"%d\" %s knowledge_base_id=\"%s\" "+
			"knowledge_title=\"%s\" chunk_type=\"%s\" score=\"%.3f\">\n",
			tag, i+1, idAttr, xmlEscape(result.KnowledgeBaseID),
			xmlEscape(result.KnowledgeTitle), xmlEscape(result.ChunkType), result.Score)
		if snippet != "" {
			fmt.Fprintf(&ob, "<match_snippet>%s</match_snippet>\n", xmlEscape(snippet))
		}
		fmt.Fprintf(&ob, "<content>%s</content>\n", result.Content)
		if result.ImageInfo != "" {
			var imageInfos []types.ImageInfo
			if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err == nil {
				for _, img := range imageInfos {
					if md := searchutil.BuildImageInfoMarkdownWithURL(img.URL, &img); md != "" {
						ob.WriteString(md)
						ob.WriteString("\n")
					}
				}
			}
		}
		if isFAQ {
			writeFAQFieldsXML(&ob, faqMeta)
		}
		fmt.Fprintf(&ob, "</%s>\n", tag)

		row := map[string]interface{}{
			"result_index":        i + 1,
			"content":             result.Content,
			"chunk_type":          result.ChunkType,
			"knowledge_id":        result.KnowledgeID,
			"knowledge_base_id":   result.KnowledgeBaseID,
			"knowledge_title":     result.KnowledgeTitle,
			"knowledge_metadata":  result.KnowledgeCustomMetadata,
			"match_type":          result.MatchType,
			"match_snippet":       snippet,
			"score":               result.Score,
			"source_query":        result.SourceQuery,
			"query_type":          result.QueryType,
			"knowledge_base_type": result.KnowledgeBaseType,
		}
		if images := chunkImageList(result.ImageInfo); len(images) > 0 {
			row["images"] = images
		}
		if isFAQ {
			row["faq_id"] = result.ID
			row["index"] = result.ChunkIndex
			if faqMeta.StandardQuestion != "" {
				row["faq_standard_question"] = faqMeta.StandardQuestion
			}
			appendSimilarQuestionsToChunkData(row, faqMeta.SimilarQuestions)
			if len(faqMeta.Answers) > 0 {
				row["faq_answers"] = faqMeta.Answers
			}
		} else {
			row["chunk_id"] = result.ID
			row["chunk_index"] = result.ChunkIndex
		}
		formattedResults = append(formattedResults, row)
	}
	ob.WriteString("</search_results>")

	data["results"] = formattedResults
	data["count"] = len(formattedResults)
	data["kb_counts"] = kbCounts
	return &types.ToolResult{Success: true, Output: ob.String(), Data: data}
}

// getEnrichedPassage merges the raw chunk content with image captions / OCR
// text. The pattern filter matches against it, so unlike the rerank passage
// it keeps markdown, URLs and other literal text a grep may target.
func (t *SearchKnowledgeTool) getEnrichedPassage(ctx context.Context, result *types.SearchResult) string {
	if result.ImageInfo == "" {
		return result.Content
	}
	var imageInfos []types.ImageInfo
	if err := json.Unmarshal([]byte(result.ImageInfo), &imageInfos); err != nil {
		logger.Warnf(ctx, "[Tool][SearchKnowledge] Failed to parse image info: %v", err)
		return result.Content
	}
	if len(imageInfos) == 0 {
		return result.Content
	}
	var imageTexts []string
	for _, img := range imageInfos {
		if img.Caption != "" {
			imageTexts = append(imageTexts, fmt.Sprintf("Image Caption: %s", img.Caption))
		}
		if img.OCRText != "" {
			imageTexts = append(imageTexts, fmt.Sprintf("Image Text: %s", img.OCRText))
		}
	}
	if len(imageTexts) == 0 {
		return result.Content
	}
	combinedText := result.Content
	if combinedText != "" {
		combinedText += "\n\n"
	}
	return combinedText + strings.Join(imageTexts, "\n")
}

// selectMMR reduces redundancy among results through the shared MMR stage.
func selectMMR(ctx context.Context, results []*searchResultWithMeta, k int) []*searchResultWithMeta {
	rows := make([]*types.SearchResult, len(results))
	for i, r := range results {
		rows[i] = r.SearchResult
	}
	picks := reranking.SelectMMR(ctx, rows, k, reranking.DefaultMMRLambda)
	if picks == nil {
		return nil
	}
	selected := make([]*searchResultWithMeta, len(picks))
	for i, p := range picks {
		selected[i] = results[p]
	}
	return selected
}