tools

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChromiumDataDir = "./data/chromium-browser"

ChromiumDataDir 下载的 Chromium 存放目录,可在 init 时修改

View Source
var (
	ConfigDBPath = "./user_config.db" // 可在 init 时修改
)

Functions

func CommandExec added in v1.1.0

func CommandExec(ctx context.Context, args map[string]any) (any, error)

func FileEdit added in v1.1.0

func FileEdit(ctx context.Context, args map[string]any) (any, error)

func FileList added in v1.1.0

func FileList(ctx context.Context, args map[string]any) (any, error)

func FileRead added in v1.1.0

func FileRead(ctx context.Context, args map[string]any) (any, error)

func FileSearch added in v1.1.0

func FileSearch(ctx context.Context, args map[string]any) (any, error)

func FileWrite added in v1.1.0

func FileWrite(ctx context.Context, args map[string]any) (any, error)

func GetWorkDir

func GetWorkDir() string

GetWorkDir 获取当前工作目录

func GetWorkDirTool

func GetWorkDirTool(ctx context.Context, args map[string]any) (any, error)

GetWorkDirTool 工具:返回当前工作目录

func RegisterAll added in v1.1.0

func RegisterAll(registry *ToolRegistry) []string

RegisterAll 一键注册所有内置工具 自动检测环境,跳过不可用的工具

func RegisterAllWithBrowser added in v1.1.0

func RegisterAllWithBrowser(registry *ToolRegistry) []string

RegisterAllWithBrowser 强制注册所有工具(包括浏览器工具) 即使 Chrome 不可用也注册,首次使用时会自动下载

func RegisterCommandTools added in v1.1.0

func RegisterCommandTools(registry *ToolRegistry)

func RegisterEnvTools

func RegisterEnvTools(registry *ToolRegistry)

RegisterEnvTools 注册环境工具

func RegisterFileEditTools added in v1.1.0

func RegisterFileEditTools(registry *ToolRegistry)

func RegisterFileTools added in v1.1.0

func RegisterFileTools(registry *ToolRegistry)

func RegisterUserConfigTools

func RegisterUserConfigTools(registry *ToolRegistry)

func RegisterWebTools

func RegisterWebTools(registry *ToolRegistry)

func UserConfig

func UserConfig(ctx context.Context, args map[string]any) (any, error)

func WebBrowse

func WebBrowse(ctx context.Context, args map[string]any) (any, error)

func WebFetch

func WebFetch(ctx context.Context, args map[string]any) (any, error)

Types

type ChromiumManager

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

ChromiumManager 管理 Chrome/Chromium 二进制文件 查找优先级:系统已安装 > 已缓存下载 > 自动下载(需校验)

func NewChromiumManager

func NewChromiumManager(dataDir string) *ChromiumManager

func (*ChromiumManager) ChromePath

func (m *ChromiumManager) ChromePath() (string, error)

ChromePath 获取可用的 Chrome/Chromium 可执行文件路径

type Config

type Config struct {
	ID        uint   `gorm:"primaryKey;autoIncrement"`
	Type      string `gorm:"type:text;not null;uniqueIndex:idx_type_key,priority:1"`
	Key       string `gorm:"type:text;not null;uniqueIndex:idx_type_key,priority:2"`
	Value     string `gorm:"type:text;not null"`
	UpdatedAt int64  `gorm:"not null"`
}

func (Config) TableName

func (Config) TableName() string

type HookID

type HookID int

HookID 钩子标识

type PromptLoader

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

PromptLoader 通用 Markdown 配置加载器 支持从目录加载任意 .md 文件,以及业界标准的 AGENTS.md

func NewPromptLoader

func NewPromptLoader(configDir string) *PromptLoader

func (*PromptLoader) LoadAGENTS added in v1.1.0

func (pl *PromptLoader) LoadAGENTS() (string, error)

LoadAGENTS 加载 AGENTS.md(业界标准的 Agent 指令文件) 搜索顺序:configDir/AGENTS.md

func (*PromptLoader) LoadAll added in v1.1.0

func (pl *PromptLoader) LoadAll() (string, []string, error)

LoadAll 加载所有可用的指令文件,合并为一个 prompt 搜索优先级:AGENTS.md > CLAUDE.md > .cursor/rules/ 返回合并后的内容和实际加载到的文件名列表

func (*PromptLoader) LoadByName added in v1.1.0

func (pl *PromptLoader) LoadByName(name string) (string, error)

LoadByName 按名称加载(自动加 .md 后缀)

func (*PromptLoader) LoadCLAUDE added in v1.1.0

func (pl *PromptLoader) LoadCLAUDE() (string, error)

LoadCLAUDE 加载 CLAUDE.md(Claude Code 标准指令文件)

func (*PromptLoader) LoadCursorRules added in v1.1.0

func (pl *PromptLoader) LoadCursorRules() (string, error)

LoadCursorRules 加载 .cursor/rules/ 目录下的规则文件

func (*PromptLoader) LoadFile added in v1.1.0

func (pl *PromptLoader) LoadFile(filename string) (string, error)

LoadFile 加载指定文件名的内容

func (*PromptLoader) LoadWithDefaults added in v1.1.0

func (pl *PromptLoader) LoadWithDefaults() (string, []string, error)

LoadWithDefaults 加载所有可用指令文件,附加工作目录变量替换

func (*PromptLoader) ReplaceVariables

func (pl *PromptLoader) ReplaceVariables(content string, vars map[string]string) string

ReplaceVariables 替换模板变量(如 {{work_dir}})

type RegisteredTool

type RegisteredTool struct {
	Metadata     ToolMetadata
	Handler      ToolHandler
	RegisteredAt time.Time
	UpdatedAt    time.Time
	UseCount     int64
}

RegisteredTool 注册后的工具

type ToolHandler

type ToolHandler func(ctx context.Context, args map[string]any) (any, error)

ToolHandler 工具执行函数签名

type ToolMetadata

type ToolMetadata struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  any            `json:"parameters"`
	Permission  ToolPermission `json:"permission"`
	Category    string         `json:"category"`
	Version     string         `json:"version"`
	Author      string         `json:"author"`
	Tags        []string       `json:"tags"`
	Requires    []string       `json:"requires,omitempty"`
	Timeout     time.Duration  `json:"timeout"`
	Enabled     bool           `json:"enabled"`
	Config      map[string]any `json:"config,omitempty"`
}

ToolMetadata 工具元数据

type ToolOption

type ToolOption func(*ToolMetadata)

ToolOption 工具配置选项

func WithAuthor

func WithAuthor(author string) ToolOption

WithAuthor 设置作者

func WithCategory

func WithCategory(cat string) ToolOption

WithCategory 设置分类

func WithParameters

func WithParameters(params map[string]any) ToolOption

WithParameters 设置参数定义

func WithPermission

func WithPermission(perm ToolPermission) ToolOption

WithPermission 设置权限

func WithTags

func WithTags(tags ...string) ToolOption

WithTags 设置标签

func WithTimeout

func WithTimeout(timeout time.Duration) ToolOption

WithTimeout 设置超时

func WithVersion

func WithVersion(version string) ToolOption

WithVersion 设置版本

type ToolPermission

type ToolPermission int

ToolPermission 工具权限级别

const (
	// PermReadOnly 只读权限(安全,无副作用)
	PermReadOnly ToolPermission = iota
	// PermReadWrite 读写权限(可能修改状态)
	PermReadWrite
	// PermDangerous 危险权限(可能破坏数据/系统)
	PermDangerous
)

func (ToolPermission) String

func (p ToolPermission) String() string

type ToolRegistry

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

ToolRegistry 动态工具注册中心

func NewToolRegistry

func NewToolRegistry() *ToolRegistry

NewToolRegistry 创建工具注册中心

func (*ToolRegistry) AddAfterExecuteHook

func (r *ToolRegistry) AddAfterExecuteHook(hook func(ctx context.Context, toolName string, result schema.ToolResult, duration time.Duration)) HookID

AddAfterExecuteHook 注册 afterExecute 钩子,返回 ID

func (*ToolRegistry) AddBeforeExecuteHook

func (r *ToolRegistry) AddBeforeExecuteHook(hook func(ctx context.Context, toolName string, args map[string]any) error) HookID

AddBeforeExecuteHook 注册 beforeExecute 钩子,返回 ID

func (*ToolRegistry) AddOnRegisterHook

func (r *ToolRegistry) AddOnRegisterHook(hook func(tool *RegisteredTool)) HookID

AddOnRegisterHook 注册 onRegister 钩子,返回 ID

func (*ToolRegistry) AddOnUnregisterHook

func (r *ToolRegistry) AddOnUnregisterHook(hook func(toolName string)) HookID

AddOnUnregisterHook 注册 onUnregister 钩子,返回 ID

func (*ToolRegistry) Disable

func (r *ToolRegistry) Disable(toolName string) error

Disable 禁用工具

func (*ToolRegistry) Enable

func (r *ToolRegistry) Enable(toolName string) error

Enable 启用工具

func (*ToolRegistry) Execute

Execute 执行工具(带生命周期钩子)

func (*ToolRegistry) ExecuteBatch

func (r *ToolRegistry) ExecuteBatch(ctx context.Context, calls []schema.ToolCall) []schema.ToolResult

ExecuteBatch 批量执行 ExecuteBatch 并行执行多个工具调用(带并发限制)

func (*ToolRegistry) Get

func (r *ToolRegistry) Get(toolName string) (*RegisteredTool, bool)

Get 获取工具

func (*ToolRegistry) GetAllTools

func (r *ToolRegistry) GetAllTools() []*RegisteredTool

GetAllTools 获取所有工具

func (*ToolRegistry) GetByCategory

func (r *ToolRegistry) GetByCategory(category string) []*RegisteredTool

GetByCategory 按分类获取

func (*ToolRegistry) GetByPermission

func (r *ToolRegistry) GetByPermission(perm ToolPermission) []*RegisteredTool

GetByPermission 按权限级别获取

func (*ToolRegistry) GetByTag

func (r *ToolRegistry) GetByTag(tag string) []*RegisteredTool

GetByTag 按标签搜索

func (*ToolRegistry) GetEnabledTools

func (r *ToolRegistry) GetEnabledTools() []schema.Tool

GetEnabledTools 获取所有启用的工具 Schema(发给模型用)

func (*ToolRegistry) MustRegister

func (r *ToolRegistry) MustRegister(meta ToolMetadata, handler ToolHandler)

MustRegister 注册工具,失败 panic

func (*ToolRegistry) Register

func (r *ToolRegistry) Register(meta ToolMetadata, handler ToolHandler) error

Register 动态注册工具

func (*ToolRegistry) RegisterSimple added in v1.1.0

func (r *ToolRegistry) RegisterSimple(name, description string, handler ToolHandler, opts ...ToolOption) error

RegisterSimple 简化注册:只需 name、description、handler,其余用默认值 默认:PermReadOnly、category="dynamic"、timeout=30s

func (*ToolRegistry) RemoveAfterExecuteHook

func (r *ToolRegistry) RemoveAfterExecuteHook(id HookID)

RemoveAfterExecuteHook 移除 afterExecute 钩子

func (*ToolRegistry) RemoveBeforeExecuteHook

func (r *ToolRegistry) RemoveBeforeExecuteHook(id HookID)

RemoveBeforeExecuteHook 移除 beforeExecute 钩子

func (*ToolRegistry) RemoveOnRegisterHook

func (r *ToolRegistry) RemoveOnRegisterHook(id HookID)

RemoveOnRegisterHook 移除 onRegister 钩子

func (*ToolRegistry) RemoveOnUnregisterHook

func (r *ToolRegistry) RemoveOnUnregisterHook(id HookID)

RemoveOnUnregisterHook 移除 onUnregister 钩子

func (*ToolRegistry) ToToolMessages

func (r *ToolRegistry) ToToolMessages(results []schema.ToolResult) []*schema.Message

ToToolMessages 将 ToolResult 转成 Message

func (*ToolRegistry) Unregister

func (r *ToolRegistry) Unregister(toolName string) error

Unregister 注销工具

func (*ToolRegistry) Update

func (r *ToolRegistry) Update(toolName string, meta ToolMetadata, handler ToolHandler) error

Update 更新工具

Jump to

Keyboard shortcuts

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