fix(agent-share): don't require rerank model for wiki-only agents

The share-time validation forced every agent bound to a knowledge base to
have a rerank model configured, which blocked Wiki-type agents
(`wiki_search` / `wiki_read_page` / …) from being shared even though they
never invoke the reranker at runtime.

Align the check with the runtime logic in `session_agent_qa.go`: only
require a rerank model when the agent's allowed_tools include
`knowledge_search` (or rely on the default tool set, which contains it).
Wiki-only agents can now be shared without a rerank model; hybrid / RAG
agents still enforce the requirement.

Also update the handler error message to reflect the new rule.
This commit is contained in:
wizardchen
2026-04-24 10:09:13 +08:00
committed by lyingbug
parent 220404348e
commit b8c45b173e
2 changed files with 34 additions and 5 deletions
+33 -4
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"time"
"github.com/Tencent/WeKnora/internal/agent/tools"
"github.com/Tencent/WeKnora/internal/application/repository"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
@@ -19,9 +20,35 @@ var (
ErrAgentNotFoundForShare = errors.New("agent not found")
ErrNotAgentOwner = errors.New("only agent owner can share")
ErrOrgRoleCannotShareAgent = errors.New("only editors and admins can share agents to this organization")
ErrAgentNotConfigured = errors.New("agent is not fully configured (missing required chat model or rerank model when using knowledge bases)")
ErrAgentNotConfigured = errors.New("agent is not fully configured (missing required chat model, or rerank model when the knowledge_search tool is enabled)")
)
// agentRequiresRerankModel returns true when the agent's configured tools
// will actually invoke the reranker at runtime. This mirrors the runtime
// check in session_agent_qa.go: only `knowledge_search` uses the reranker.
// Wiki-first agents (wiki_search / wiki_read_page / …) never call it and
// therefore don't need a rerank model configured, even when knowledge bases
// are attached.
//
// When AllowedTools is empty the runtime falls back to
// tools.DefaultAllowedTools(), which includes knowledge_search, so we treat
// that case as requiring the reranker.
func agentRequiresRerankModel(agent *types.CustomAgent) bool {
if agent == nil {
return false
}
allowed := agent.Config.AllowedTools
if len(allowed) == 0 {
allowed = tools.DefaultAllowedTools()
}
for _, t := range allowed {
if t == tools.ToolKnowledgeSearch {
return true
}
}
return false
}
// agentShareService implements AgentShareService interface
type agentShareService struct {
shareRepo interfaces.AgentShareRepository
@@ -60,12 +87,14 @@ func (s *agentShareService) ShareAgent(ctx context.Context, agentID string, orgI
return nil, ErrNotAgentOwner
}
// Require agent to be fully configured before sharing (same rules as for conversation)
// Require agent to be fully configured before sharing (same rules as for
// conversation — see session_agent_qa.go). Rerank model is only required
// when the `knowledge_search` tool is enabled; Wiki-only agents
// (wiki_search / wiki_read_page) don't call the reranker at runtime.
if agent.Config.ModelID == "" {
return nil, ErrAgentNotConfigured
}
usesKB := agent.Config.KBSelectionMode != "none" || len(agent.Config.KnowledgeBases) > 0
if usesKB && agent.Config.RerankModelID == "" {
if agentRequiresRerankModel(agent) && agent.Config.RerankModelID == "" {
return nil, ErrAgentNotConfigured
}