Skip to content

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

12.1 KB
Raw Blame
package tools

import (
	"context"
	"encoding/json"
	"fmt"
	"runtime/debug"
	"sort"
	"strings"
	"unicode/utf8"

	"github.com/Tencent/WeKnora/internal/common"
	"github.com/Tencent/WeKnora/internal/logger"
	"github.com/Tencent/WeKnora/internal/types"
)

// ToolRegistry manages the registration and retrieval of tools
type ToolRegistry struct {
	executionGuard    func(context.Context, types.Tool) error
	mcpCallGuard      MCPCallGuard
	tools             map[string]types.Tool
	deferred          map[string]bool
	mcpDirect         bool // Full exposure is an explicit compatibility path.
	mcpPrepared       bool
	maxToolOutputSize int // maximum chars for tool output (0 = use DefaultMaxToolOutput)
}

// outputLimitProvider is implemented by tools that expose a caller-configurable
// output budget with their own hard safety cap. It prevents the registry's
// generic limit from undoing that explicit bounded choice.
type outputLimitProvider interface {
	OutputLimitChars(args json.RawMessage) int
}

// NewToolRegistry creates a new tool registry
func NewToolRegistry() *ToolRegistry {
	return &ToolRegistry{
		tools:    make(map[string]types.Tool),
		deferred: make(map[string]bool),
	}
}

// SetMaxToolOutputSize sets the maximum character length for tool output.
// Values <= 0 will use DefaultMaxToolOutput.
func (r *ToolRegistry) SetMaxToolOutputSize(maxChars int) {
	r.maxToolOutputSize = maxChars
}

// getMaxToolOutput returns the effective max tool output size.
func (r *ToolRegistry) getMaxToolOutput() int {
	if r.maxToolOutputSize > 0 {
		return r.maxToolOutputSize
	}
	return DefaultMaxToolOutput
}

// RegisterTool adds a tool to the registry.
// If a tool with the same name is already registered, the existing one is kept
// (first-wins) to prevent tool execution hijacking via name collision (GHSA-67q9-58vj-32qx).
func (r *ToolRegistry) RegisterTool(tool types.Tool) {
	r.registerTool(tool, false)
}

// RegisterDeferredTool retains execution capability without advertising the
// full definition to the model. Registration is completed before execution.
func (r *ToolRegistry) RegisterDeferredTool(tool types.Tool) {
	r.registerTool(tool, true)
}

func (r *ToolRegistry) registerTool(tool types.Tool, deferred bool) {
	name := tool.Name()
	if _, exists := r.tools[name]; exists {
		logger.Warnf(context.Background(),
			"[ToolRegistry] Duplicate tool registration rejected: %s (first-wins policy)", name)
		return
	}
	r.tools[name] = tool
	if r.deferred == nil {
		r.deferred = make(map[string]bool)
	}
	r.deferred[name] = deferred
}

// RestrictTools intersects an already constructed registry with an authorized
// snapshot. Used by inherited children so newly available integrations cannot
// silently expand their parent's effective native tool set.
func (r *ToolRegistry) RestrictTools(names []string) {
	allowed := make(map[string]bool, len(names))
	for _, name := range names {
		allowed[name] = true
	}
	for name := range r.tools {
		if !allowed[name] {
			delete(r.tools, name)
			delete(r.deferred, name)
		}
	}
}

// GetTool retrieves a tool by name
func (r *ToolRegistry) GetTool(name string) (types.Tool, error) {
	tool, exists := r.tools[name]
	if !exists {
		return nil, fmt.Errorf("tool not found: %s", name)
	}
	return tool, nil
}

type sessionBinder interface {
	BindSession(id string)
}

// BindSession tells sandbox tools which session's layout to advertise in
// Description() and Parameters(). Host adapters refuse an empty session ID.
func (r *ToolRegistry) BindSession(id string) {
	if r == nil {
		return
	}
	id = strings.TrimSpace(id)
	for _, tool := range r.tools {
		if binder, ok := tool.(sessionBinder); ok {
			binder.BindSession(id)
		}
	}
}

// ListTools returns all registered tool names sorted alphabetically.
// Sorting keeps the order stable across calls — Go map iteration is
// intentionally randomized.
func (r *ToolRegistry) ListTools() []string {
	names := make([]string, 0, len(r.tools))
	for name := range r.tools {
		names = append(names, name)
	}
	sort.Strings(names)
	return names
}

// GetFunctionDefinitions returns function definitions for all registered tools.
// The slice is sorted by tool name so the serialized payload sent to the LLM
// is byte-identical across requests. Providers that key prompt caching on a
// byte-level prefix match (e.g. Qwen explicit caching) require this — map
// iteration order would otherwise reshuffle the tools block and break cache
// hits.
func (r *ToolRegistry) GetFunctionDefinitions() []types.FunctionDefinition {
	return r.functionDefinitions(false)
}

// GetModelFunctionDefinitions is the stable model-facing projection of the registry.
func (r *ToolRegistry) GetModelFunctionDefinitions() []types.FunctionDefinition {
	return r.functionDefinitions(true)
}

func (r *ToolRegistry) functionDefinitions(modelOnly bool) []types.FunctionDefinition {
	names := make([]string, 0, len(r.tools))
	for name := range r.tools {
		names = append(names, name)
	}
	sort.Strings(names)

	definitions := make([]types.FunctionDefinition, 0, len(names))
	for _, name := range names {
		if modelOnly && r.deferred[name] {
			continue
		}
		tool := r.tools[name]
		definitions = append(definitions, types.FunctionDefinition{
			Name:        tool.Name(),
			Description: tool.Description(),
			Parameters:  tool.Parameters(),
		})
	}
	return definitions
}

// ExecuteTool executes a tool by name with the given arguments
func (r *ToolRegistry) ExecuteTool(
	ctx context.Context,
	name string,
	args json.RawMessage,
) (*types.ToolResult, error) {
	if err := ctx.Err(); err != nil {
		return &types.ToolResult{Success: false, Error: err.Error()}, err
	}
	common.PipelineInfo(ctx, "AgentTool", "execute_start", map[string]interface{}{
		"tool": name,
		"args": args,
	})
	tool, err := r.GetTool(name)
	if err != nil {
		if msg := RetiredToolReplacement(name); msg != "" {
			common.PipelineWarn(ctx, "AgentTool", "retired_tool", map[string]interface{}{
				"tool":  name,
				"error": msg,
			})
			return &types.ToolResult{Success: false, Error: msg}, nil
		}
		common.PipelineError(ctx, "AgentTool", "execute_failed", map[string]interface{}{
			"tool":  name,
			"error": err.Error(),
		})
		return &types.ToolResult{
			Success: false,
			Error:   err.Error(),
		}, err
	}

	if direct, ok := tool.(*MCPRegisteredTool); ok {
		// Authorization precedes schema validation: even parameter-error details
		// must not expose another engine principal's registered tool definition.
		if err := direct.catalog.authorize(ctx); err != nil {
			return mcpDiscoveryFailure(err, "unavailable")
		}
	}
	return r.execute(ctx, tool, args)
}

// execute is shared by direct calls and catalog-resolved MCP calls. A proxy
// must validate the target schema and retain the original result, not just
// validate its outer arguments or bypass the execution pipeline.
func (r *ToolRegistry) execute(ctx context.Context, tool types.Tool, args json.RawMessage) (*types.ToolResult, error) {
	if err := ctx.Err(); err != nil {
		return &types.ToolResult{Success: false, Error: err.Error()}, err
	}
	if r.executionGuard != nil {
		if err := r.executionGuard(ctx, tool); err != nil {
			return &types.ToolResult{Success: false, Error: err.Error()}, err
		}
	}
	name := tool.Name()
	// Cast parameters to match expected schema types before execution.
	// This handles common LLM quirks like returning "true" instead of true.
	args = CastParams(args, tool.Parameters())

	// Validate parameters against the tool's JSON Schema before execution.
	// This catches invalid arguments early, avoiding a wasted tool execution + LLM round.
	var validationErrs []ValidationError
	if validator, ok := tool.(interface{ ValidateArguments(json.RawMessage) error }); ok {
		if err := validator.ValidateArguments(args); err != nil {
			validationErrs = []ValidationError{{Message: err.Error()}}
		}
	} else {
		validationErrs = ValidateParams(args, tool.Parameters())
	}
	if len(validationErrs) > 0 {
		errMsg := FormatValidationErrors(validationErrs)
		if name == ToolCallMCPTool {
			errMsg += mcpCallArgumentsHint
		}
		if name == ToolWriteSandboxFile {
			errMsg += writeSandboxMissingFieldHint
		}
		if name == ToolEditSandboxFile {
			errMsg += editSandboxMissingFieldHint
		}
		common.PipelineWarn(ctx, "AgentTool", "validation_failed", map[string]interface{}{
			"tool":   name,
			"errors": errMsg,
		})
		return &types.ToolResult{
			Success: false,
			Error:   errMsg,
		}, nil
	}

	// Publish the ceiling so budget-aware tools can shape a batched result
	// themselves; the truncation below stays as the fallback for the rest.
	maxOutput := r.getMaxToolOutput()
	if provider, ok := tool.(outputLimitProvider); ok {
		if toolLimit := provider.OutputLimitChars(args); toolLimit > maxOutput {
			maxOutput = toolLimit
		}
	}
	ctx = withMCPCallGuard(ctx, r.mcpCallGuard)
	result, execErr := executeRecovered(WithOutputBudget(ctx, maxOutput), tool, args)
	if result == nil {
		result = &types.ToolResult{Success: false, Error: "tool returned no result"}
	}
	if execErr != nil {
		result.Success = false
		if result.Error == "" {
			result.Error = execErr.Error()
		}
	}

	// Truncate large tool outputs to prevent context window poisoning. The
	// limit is counted in runes to match TruncateToolOutput; comparing bytes
	// here would leave CJK output effectively uncapped.
	if result != nil && utf8.RuneCountInString(result.Output) > maxOutput {
		result.Output = TruncateToolOutput(result.Output, maxOutput)
	}
	if utf8.RuneCountInString(result.Error) > maxOutput {
		result.Error = TruncateToolOutput(result.Error, maxOutput)
	}

	fields := map[string]interface{}{
		"tool": name,
		"args": args,
	}
	if result != nil {
		fields["success"] = result.Success
		if result.Error != "" {
			fields["error"] = result.Error
		}
	}
	if execErr != nil {
		fields["error"] = execErr.Error()
		common.PipelineError(ctx, "AgentTool", "execute_done", fields)
	} else if result != nil && !result.Success {
		common.PipelineWarn(ctx, "AgentTool", "execute_done", fields)
	} else {
		common.PipelineInfo(ctx, "AgentTool", "execute_done", fields)
	}

	return result, execErr
}

// executeRecovered runs one tool and turns a panic into a failed result. Tools
// run on errgroup goroutines when a round executes in parallel, and errgroup
// does not carry a panic back to Wait: an unrecovered one from any tool (MCP
// clients, sandboxes, third-party SDKs) would take down the whole server.
//
// recover only covers the goroutine that calls it, so this guards the
// tool.Execute call alone: a goroutine a tool starts for itself must install
// its own barrier (GoRecovered / RecoverGoroutine in goroutine.go).
func executeRecovered(
	ctx context.Context, tool types.Tool, args json.RawMessage,
) (result *types.ToolResult, err error) {
	defer func() {
		if r := recover(); r != nil {
			logger.Errorf(ctx, "[ToolRegistry] Tool %s panicked: %v\n%s", tool.Name(), r, debug.Stack())
			// The panic value can carry internals (paths, addresses); the
			// model and the user only need to know the call did not complete.
			err = fmt.Errorf("tool %s failed with an internal error", tool.Name())
			result = &types.ToolResult{Success: false, Error: err.Error()}
		}
	}()
	return tool.Execute(ctx, args)
}

// errorCleanable is Cleanup that reports a failure. types.Cleanable takes no
// error, and one method cannot satisfy both signatures. shell_exec_async uses
// the error form so a failed cursor update is visible.
type errorCleanable interface {
	Cleanup(ctx context.Context) error
}

// Cleanup cleans up all registered tools that implement types.Cleanable or
// errorCleanable. This is called at the end of agent sessions to release
// tool-specific resources, including shell-job output cursors.
func (r *ToolRegistry) Cleanup(ctx context.Context) {
	for name, tool := range r.tools {
		switch cleanable := tool.(type) {
		case types.Cleanable:
			logger.Infof(ctx, "[ToolRegistry] Cleaning up tool: %s", name)
			cleanable.Cleanup(ctx)
		case errorCleanable:
			logger.Infof(ctx, "[ToolRegistry] Cleaning up tool: %s", name)
			if err := cleanable.Cleanup(ctx); err != nil {
				logger.Warnf(ctx, "[ToolRegistry] cleanup %s: %v", name, err)
			}
		}
	}
}

// SetExecutionGuard installs a runtime authority fence before any direct or deferred tool.
func (r *ToolRegistry) SetExecutionGuard(guard func(context.Context, types.Tool) error) {
	r.executionGuard = guard
}