tools

package
v0.0.0-...-451c6e3 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ConfigDBPath = "./user_config.db" // 可在 init 时修改
)
View Source
var SafeBaseDir = "." // 可在 init 时修改,比如 tools.SafeBaseDir = "/home/user/workspace"

Functions

func CommandExec

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

func FileList

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

FileList 列出目录(带安全限制+错误处理)

func FileRead

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

FileRead 读取文件(带安全限制+大小限制)

func FileWrite

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

FileWrite 写入文件(带安全限制+自动创建父目录)

func GetWorkDir

func GetWorkDir() string

GetWorkDir 获取当前工作目录

func GetWorkDirTool

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

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

func RegisterAll

func RegisterAll(registry *ToolRegistry)

RegisterAll 注册所有基础工具到 ToolRegistry

func RegisterCommandTools

func RegisterCommandTools(registry *ToolRegistry)

func RegisterEnvTools

func RegisterEnvTools(registry *ToolRegistry)

RegisterEnvTools 注册环境工具

func RegisterFileTools

func RegisterFileTools(registry *ToolRegistry)

RegisterFileTools 注册文件工具

func RegisterUserConfigTools

func RegisterUserConfigTools(registry *ToolRegistry)

func RegisterWebTools

func RegisterWebTools(registry *ToolRegistry)

func SetDefaultSearchProvider

func SetDefaultSearchProvider(provider SearchProvider)

SetDefaultSearchProvider 设置默认搜索 provider

func UserConfig

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

func WebSearch

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

WebSearch 联网搜索工具

Types

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 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 HookID

type HookID int

HookID 钩子标识

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 SearchOptions

type SearchOptions struct {
	Count int
	GL    string
	HL    string
}

SearchOptions 搜索选项

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

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 批量执行

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 更新工具

Jump to

Keyboard shortcuts

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