tool

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AskPermissionFunc

type AskPermissionFunc func(toolName string, input string) bool

AskPermissionFunc 权限询问回调(阻塞直到用户回答)

type CheckResult

type CheckResult int

CheckResult 权限检查三态结果

const (
	ResultAllow   CheckResult = iota // 允许执行
	ResultDeny                       // 拒绝执行
	ResultNeedAsk                    // 需要询问用户
)

type ExecuteResult

type ExecuteResult struct {
	ToolUseID string
	Result    *Result
}

ExecuteResult 单个工具执行结果

type HookRunner

type HookRunner interface {
	// RunPreToolUse 执行 preToolUse 钩子,返回 false 表示阻止
	RunPreToolUse(ctx context.Context, toolName, toolInput string) (bool, error)
	// RunPostToolUse 执行 postToolUse 钩子
	RunPostToolUse(ctx context.Context, toolName, toolOutput string, isError bool)
}

HookRunner 钩子执行接口(避免循环依赖)

type PermissionChecker

type PermissionChecker interface {
	Check(toolName string, isReadOnly bool) (CheckResult, string)
}

PermissionChecker 权限检查接口

type PermissionMode

type PermissionMode string

PermissionMode 权限模式

const (
	ModeBypass      PermissionMode = "bypass"
	ModeAcceptEdits PermissionMode = "acceptEdits"
	ModeDefault     PermissionMode = "default"
	ModePlan        PermissionMode = "plan"
	ModeInteractive PermissionMode = "interactive"
)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry 工具注册表

func NewRegistry

func NewRegistry() *Registry

NewRegistry 创建工具注册表

func (*Registry) All

func (r *Registry) All() []Tool

All 返回所有工具

func (*Registry) ExecuteBatch

func (r *Registry) ExecuteBatch(ctx context.Context, calls []*provider.ToolCall, checker PermissionChecker, askFn AskPermissionFunc, hooks ...HookRunner) []ExecuteResult

ExecuteBatch 批量执行工具调用 只读工具并发执行,写入工具顺序执行

func (*Registry) ExecuteTool

func (r *Registry) ExecuteTool(ctx context.Context, call *provider.ToolCall) *Result

ExecuteTool 执行单个工具调用

func (*Registry) ExecuteWithPermission

func (r *Registry) ExecuteWithPermission(ctx context.Context, call *provider.ToolCall, checker PermissionChecker, askFn AskPermissionFunc, hooks ...HookRunner) *Result

ExecuteWithPermission 执行单个工具调用(带权限检查 + 用户询问 + 钩子)

func (*Registry) Get

func (r *Registry) Get(name string) (Tool, bool)

Get 获取工具

func (*Registry) Register

func (r *Registry) Register(t Tool)

Register 注册工具

func (*Registry) ToolDefs

func (r *Registry) ToolDefs() []provider.ToolDef

ToolDefs 返回所有工具的 Provider ToolDef 格式(传给 API)

type Result

type Result struct {
	Content string
	IsError bool
}

Result 工具执行结果

type SimplePermissionChecker

type SimplePermissionChecker struct {
	Mode PermissionMode
}

SimplePermissionChecker 基于模式的权限检查器

func (*SimplePermissionChecker) Check

func (c *SimplePermissionChecker) Check(toolName string, isReadOnly bool) (CheckResult, string)

type Tool

type Tool interface {
	Name() string
	Description() string
	InputSchema() map[string]any
	IsReadOnly() bool
	Execute(ctx context.Context, input json.RawMessage) (*Result, error)
}

Tool 工具接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL