Documentation
¶
Index ¶
- Variables
- func CommandExec(ctx context.Context, args map[string]any) (any, error)
- func FileList(ctx context.Context, args map[string]any) (any, error)
- func FileRead(ctx context.Context, args map[string]any) (any, error)
- func FileWrite(ctx context.Context, args map[string]any) (any, error)
- func GetWorkDir() string
- func GetWorkDirTool(ctx context.Context, args map[string]any) (any, error)
- func RegisterAll(registry *ToolRegistry)
- func RegisterCommandTools(registry *ToolRegistry)
- func RegisterEnvTools(registry *ToolRegistry)
- func RegisterFileTools(registry *ToolRegistry)
- func RegisterUserConfigTools(registry *ToolRegistry)
- func RegisterWebTools(registry *ToolRegistry)
- func SetDefaultSearchProvider(provider SearchProvider)
- func UserConfig(ctx context.Context, args map[string]any) (any, error)
- func WebSearch(ctx context.Context, args map[string]any) (any, error)
- type Config
- type DynamicToolLoader
- type HookID
- type PromptLoader
- func (pl *PromptLoader) LoadAllDefaultPrompt() (string, error)
- func (pl *PromptLoader) LoadRules() (string, error)
- func (pl *PromptLoader) LoadSafetyRules() (string, error)
- func (pl *PromptLoader) LoadSystemPrompt() (string, error)
- func (pl *PromptLoader) ReplaceVariables(content string, vars map[string]string) string
- type RegisteredTool
- type SearchOptions
- type SearchProvider
- type SearchResult
- type SearchResultItem
- type SerperConfig
- type SerperProvider
- type ToolHandler
- type ToolMetadata
- type ToolOption
- func WithAuthor(author string) ToolOption
- func WithCategory(cat string) ToolOption
- func WithParameters(params map[string]any) ToolOption
- func WithPermission(perm ToolPermission) ToolOption
- func WithTags(tags ...string) ToolOption
- func WithTimeout(timeout time.Duration) ToolOption
- func WithVersion(version string) ToolOption
- type ToolPermission
- type ToolRegistry
- func (r *ToolRegistry) AddAfterExecuteHook(hook func(ctx context.Context, toolName string, result schema.ToolResult, ...)) HookID
- func (r *ToolRegistry) AddBeforeExecuteHook(hook func(ctx context.Context, toolName string, args map[string]any) error) HookID
- func (r *ToolRegistry) AddOnRegisterHook(hook func(tool *RegisteredTool)) HookID
- func (r *ToolRegistry) AddOnUnregisterHook(hook func(toolName string)) HookID
- func (r *ToolRegistry) Disable(toolName string) error
- func (r *ToolRegistry) Enable(toolName string) error
- func (r *ToolRegistry) Execute(ctx context.Context, call schema.ToolCall) schema.ToolResult
- func (r *ToolRegistry) ExecuteBatch(ctx context.Context, calls []schema.ToolCall) []schema.ToolResult
- func (r *ToolRegistry) Get(toolName string) (*RegisteredTool, bool)
- func (r *ToolRegistry) GetAllTools() []*RegisteredTool
- func (r *ToolRegistry) GetByCategory(category string) []*RegisteredTool
- func (r *ToolRegistry) GetByPermission(perm ToolPermission) []*RegisteredTool
- func (r *ToolRegistry) GetByTag(tag string) []*RegisteredTool
- func (r *ToolRegistry) GetEnabledTools() []schema.Tool
- func (r *ToolRegistry) MustRegister(meta ToolMetadata, handler ToolHandler)
- func (r *ToolRegistry) Register(meta ToolMetadata, handler ToolHandler) error
- func (r *ToolRegistry) RemoveAfterExecuteHook(id HookID)
- func (r *ToolRegistry) RemoveBeforeExecuteHook(id HookID)
- func (r *ToolRegistry) RemoveOnRegisterHook(id HookID)
- func (r *ToolRegistry) RemoveOnUnregisterHook(id HookID)
- func (r *ToolRegistry) ToToolMessages(results []schema.ToolResult) []*schema.Message
- func (r *ToolRegistry) Unregister(toolName string) error
- func (r *ToolRegistry) Update(toolName string, meta ToolMetadata, handler ToolHandler) error
Constants ¶
This section is empty.
Variables ¶
var (
ConfigDBPath = "./user_config.db" // 可在 init 时修改
)
var SafeBaseDir = "." // 可在 init 时修改,比如 tools.SafeBaseDir = "/home/user/workspace"
Functions ¶
func GetWorkDirTool ¶
GetWorkDirTool 工具:返回当前工作目录
func RegisterCommandTools ¶
func RegisterCommandTools(registry *ToolRegistry)
func RegisterUserConfigTools ¶
func RegisterUserConfigTools(registry *ToolRegistry)
func RegisterWebTools ¶
func RegisterWebTools(registry *ToolRegistry)
func SetDefaultSearchProvider ¶
func SetDefaultSearchProvider(provider SearchProvider)
SetDefaultSearchProvider 设置默认搜索 provider
Types ¶
type Config ¶
type DynamicToolLoader ¶
type DynamicToolLoader struct {
// contains filtered or unexported fields
}
DynamicToolLoader 动态工具加载器
func NewDynamicToolLoader ¶
func NewDynamicToolLoader(registry *ToolRegistry) *DynamicToolLoader
func (*DynamicToolLoader) Load ¶
func (d *DynamicToolLoader) Load( name string, description string, handler ToolHandler, parameters map[string]any, opts ...ToolOption, ) error
func (*DynamicToolLoader) LoadSimple ¶
func (d *DynamicToolLoader) LoadSimple( name string, description string, handler ToolHandler, opts ...ToolOption, ) error
type PromptLoader ¶
type PromptLoader struct {
// contains filtered or unexported fields
}
PromptLoader Markdown 配置加载器
func NewPromptLoader ¶
func NewPromptLoader(configDir string) *PromptLoader
func (*PromptLoader) LoadAllDefaultPrompt ¶
func (pl *PromptLoader) LoadAllDefaultPrompt() (string, error)
func (*PromptLoader) LoadRules ¶
func (pl *PromptLoader) LoadRules() (string, error)
LoadRules 加载运行规则
func (*PromptLoader) LoadSafetyRules ¶
func (pl *PromptLoader) LoadSafetyRules() (string, error)
LoadSafetyRules 加载安全策略
func (*PromptLoader) LoadSystemPrompt ¶
func (pl *PromptLoader) LoadSystemPrompt() (string, error)
LoadSystemPrompt 加载系统提示词
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 SearchProvider ¶
type SearchProvider interface {
Search(ctx context.Context, query string, opts SearchOptions) (*SearchResult, error)
}
SearchProvider 搜索提供商接口
func GetDefaultSearchProvider ¶
func GetDefaultSearchProvider() SearchProvider
GetDefaultSearchProvider 获取默认搜索 provider,未设置时从环境变量自动创建 Serper
type SearchResult ¶
type SearchResult struct {
Provider string `json:"provider"`
Query string `json:"query"`
Count int `json:"count"`
Results []SearchResultItem `json:"results"`
}
SearchResult 搜索结果
type SearchResultItem ¶
type SearchResultItem struct {
Title string `json:"title"`
URL string `json:"url"`
Snippet string `json:"snippet"`
Position int `json:"position"`
}
SearchResultItem 单条搜索结果
type SerperConfig ¶
type SerperConfig struct {
APIKey string
BaseURL string // 默认 https://google.serper.dev/search
GL string // 默认搜索地区
HL string // 默认搜索语言
}
SerperConfig Serper.dev 配置
type SerperProvider ¶
type SerperProvider struct {
// contains filtered or unexported fields
}
SerperProvider 基于 Serper.dev 的搜索实现
func NewSerperProvider ¶
func NewSerperProvider(config SerperConfig) *SerperProvider
func (*SerperProvider) Search ¶
func (s *SerperProvider) Search(ctx context.Context, query string, opts SearchOptions) (*SearchResult, error)
type ToolHandler ¶
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 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 (*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) Execute ¶
func (r *ToolRegistry) Execute(ctx context.Context, call schema.ToolCall) schema.ToolResult
Execute 执行工具(带生命周期钩子)
func (*ToolRegistry) ExecuteBatch ¶
func (r *ToolRegistry) ExecuteBatch(ctx context.Context, calls []schema.ToolCall) []schema.ToolResult
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) 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 更新工具