tools

package
v0.6.4 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package tools 实现 Golem 的内置工具体系,并提供工具的注册、守卫与执行管理。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAppendDiaryTool added in v0.1.4

func NewAppendDiaryTool(workspacePath string) (tool.InvokableTool, error)

NewAppendDiaryTool 创建 append_diary 工具实例,用于在 memory/YYYY-MM-DD.md 中追加日记分录。

func NewAppendFileTool added in v0.3.1

func NewAppendFileTool(workspacePath string) (tool.InvokableTool, error)

NewAppendFileTool 创建 append_file 工具实例,用于在文件末尾追加内容。

func NewCronTool added in v0.2.0

func NewCronTool(service *cron.Service) (tool.InvokableTool, error)

NewCronTool 创建一个 manage_cron 工具实例,用于 Agent 调度和管理定时任务。

func NewEditFileTool added in v0.3.1

func NewEditFileTool(workspacePath string) (tool.InvokableTool, error)

NewEditFileTool 创建 edit_file 工具实例,用于精确替换文件中的特定片段。

func NewExecTool

func NewExecTool(timeoutSec int, restrictToWorkspace bool, workspaceDir string) (tool.InvokableTool, error)

NewExecTool creates the exec tool

func NewListDirTool

func NewListDirTool(workspacePath string) (tool.InvokableTool, error)

NewListDirTool 创建 list_dir 工具实例,用于列出目录下的所有文件和文件夹。

func NewMessageTool added in v0.3.0

func NewMessageTool(publisher interface {
	PublishOutbound(msg *bus.OutboundMessage)
}) (tool.InvokableTool, error)

NewMessageTool 创建一个允许 Agent 发送主动消息的工具实例。

func NewReadFileTool

func NewReadFileTool(workspacePath string) (tool.InvokableTool, error)

NewReadFileTool 创建 read_file 工具实例,用于读取工作区内的文件内容。

func NewReadMemoryTool added in v0.1.4

func NewReadMemoryTool(workspacePath string) (tool.InvokableTool, error)

NewReadMemoryTool 创建 read_memory 工具实例,用于从 memory/MEMORY.md 读取长期记忆。

func NewSpawnTool added in v0.3.0

func NewSpawnTool(executor SubagentExecutor) (tool.InvokableTool, error)

NewSpawnTool 创建一个异步子代理委托工具,任务在后台运行并通过异步回调上报完成。

func NewSubagentTool added in v0.3.0

func NewSubagentTool(executor SubagentExecutor) (tool.InvokableTool, error)

NewSubagentTool 创建一个同步子代理委托工具,任务同步运行并直接返回执行结果。

func NewWebFetchTool added in v0.1.4

func NewWebFetchTool() (tool.InvokableTool, error)

NewWebFetchTool 创建 web_fetch 工具实例,用于抓取并提取指定 URL 的文本内容。

func NewWebSearchTool added in v0.1.4

func NewWebSearchTool(apiKey string, maxResults int) (tool.InvokableTool, error)

NewWebSearchTool 创建 web_search 工具实例,用于在互联网上搜索最新信息。

func NewWorkflowTool added in v0.5.0

func NewWorkflowTool(executor WorkflowExecutor) (tool.InvokableTool, error)

NewWorkflowTool 创建一个工作流编排工具实例,用于将目标分解为子任务并委派执行。

func NewWriteFileTool

func NewWriteFileTool(workspacePath string) (tool.InvokableTool, error)

NewWriteFileTool 创建 write_file 工具实例,用于在工作区内写入新文件或覆盖已有文件。

func NewWriteMemoryTool added in v0.1.4

func NewWriteMemoryTool(workspacePath string) (tool.InvokableTool, error)

NewWriteMemoryTool 创建 write_memory 工具实例,用于将内容写入 memory/MEMORY.md 作为长期记忆。

func WithInvocationContext added in v0.3.0

func WithInvocationContext(ctx context.Context, meta InvocationContext) context.Context

WithInvocationContext 将调用元数据注入到 Context 中,供工具逻辑使用。

Types

type AppendDiaryInput added in v0.1.4

type AppendDiaryInput struct {
	Entry string `json:"entry" jsonschema:"required,description=Diary entry content to append"`
}

AppendDiaryInput 定义了 append_diary 工具的输入参数。

type AppendDiaryOutput added in v0.1.4

type AppendDiaryOutput struct {
	DiaryPath string `json:"diary_path"` // 写入的日记文件路径
}

AppendDiaryOutput 定义了 append_diary 工具的执行结果。

type AppendFileInput added in v0.3.1

type AppendFileInput struct {
	Path    string `json:"path" jsonschema:"required,description=Absolute path to the file"`
	Content string `json:"content" jsonschema:"required,description=Content to append to file end"`
}

AppendFileInput 定义了 append_file 工具的输入参数。

type CronToolInput added in v0.2.0

type CronToolInput struct {
	Action       string `` /* 153-byte string literal not displayed */
	Name         string `json:"name,omitempty" jsonschema:"description=Job name (required for add)"`
	Message      string `json:"message,omitempty" jsonschema:"description=Message to send to agent (required for add)"`
	EverySeconds int64  `json:"every_seconds,omitempty" jsonschema:"description=Repeat interval in seconds (for add with every schedule)"`
	CronExpr     string `json:"cron_expr,omitempty" jsonschema:"description=Cron expression like '0 9 * * *' (for add with cron schedule)"`
	AtTimestamp  string `json:"at_timestamp,omitempty" jsonschema:"description=RFC3339 timestamp for one-shot (for add with at schedule)"`
	JobID        string `json:"job_id,omitempty" jsonschema:"description=Job ID (required for remove/enable/disable)"`
	Deliver      bool   `json:"deliver,omitempty" jsonschema:"description=If true deliver response directly without agent processing"`
}

CronToolInput 定义了 manage_cron 工具的输入参数。

type EditFileInput added in v0.3.1

type EditFileInput struct {
	Path    string `json:"path" jsonschema:"required,description=Absolute path to the file"`
	OldText string `json:"old_text" jsonschema:"required,description=Exact existing text to replace"`
	NewText string `json:"new_text" jsonschema:"required,description=Replacement text"`
}

EditFileInput 定义了 edit_file 工具的输入参数。

type ExecInput

type ExecInput struct {
	Command    string `json:"command" jsonschema:"required,description=Shell command to execute"`
	WorkingDir string `json:"working_dir" jsonschema:"description=Working directory for the command"`
}

ExecInput parameters for exec tool

type ExecOutput

type ExecOutput struct {
	Stdout   string `json:"stdout"`
	Stderr   string `json:"stderr"`
	ExitCode int    `json:"exit_code"`
}

ExecOutput result of exec tool

type GuardAction added in v0.4.0

type GuardAction string

GuardAction 定义守卫函数对工具执行的决策动作。

const (
	GuardAllow           GuardAction = "allow"            // 允许执行
	GuardDeny            GuardAction = "deny"             // 拒绝执行
	GuardRequireApproval GuardAction = "require_approval" // 需要人工审批
)

type GuardFunc added in v0.4.0

type GuardFunc func(ctx context.Context, name, argsJSON string) (GuardResult, error)

GuardFunc 定义了工具执行前的守卫函数原型。

type GuardResult added in v0.4.0

type GuardResult struct {
	Action  GuardAction // 决策动作
	Message string      // 决策原因或给用户的提示
}

GuardResult 包含守卫决策的结果及相关提示消息。

type InvocationContext added in v0.3.0

type InvocationContext struct {
	Channel   string // 调用来源通道(如 telegram, cli)
	ChatID    string // 调用来源聊天会话 ID
	SenderID  string // 发起调用的发送者 ID
	RequestID string // 关联的请求追踪 ID
	SessionID string // 关联的会话 ID
}

InvocationContext 携带工具执行时的调用者元数据,如通道、聊天 ID 等。

func InvocationFromContext added in v0.3.0

func InvocationFromContext(ctx context.Context) InvocationContext

InvocationFromContext 从 Context 中提取调用元数据。如果 Context 中没有元数据,则返回空结构体。

type ListDirInput

type ListDirInput struct {
	Path string `json:"path" jsonschema:"required,description=Directory path to list"`
}

ListDirInput 定义了 list_dir 工具的输入参数。

type MessageInput added in v0.3.0

type MessageInput struct {
	Content string `json:"content" jsonschema:"required,description=Message content to send"`
	Channel string `json:"channel,omitempty" jsonschema:"description=Target channel (optional; defaults to current channel)"`
	ChatID  string `json:"chat_id,omitempty" jsonschema:"description=Target chat/session id (optional; defaults to current chat)"`
}

MessageInput 定义了 message 工具的输入参数。

type ReadFileInput

type ReadFileInput struct {
	Path   string `json:"path" jsonschema:"required,description=Absolute path to the file"`
	Offset int    `json:"offset" jsonschema:"description=Starting line number (0-based)"`
	Limit  int    `json:"limit" jsonschema:"description=Maximum number of lines to read"`
}

ReadFileInput 定义了 read_file 工具的输入参数。

type ReadFileOutput

type ReadFileOutput struct {
	Content    string `json:"content"`     // 文件内容
	TotalLines int    `json:"total_lines"` // 文件总行数
}

ReadFileOutput 定义了 read_file 工具的执行结果。

type ReadMemoryInput added in v0.1.4

type ReadMemoryInput struct{}

ReadMemoryInput 定义了 read_memory 工具的输入参数(无参数)。

type ReadMemoryOutput added in v0.1.4

type ReadMemoryOutput struct {
	Content string `json:"content"` // 长期记忆文件的完整内容
}

ReadMemoryOutput 定义了 read_memory 工具的执行结果。

type Registry

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

Registry 按名称统一管理所有可调用的工具实例。

func NewRegistry

func NewRegistry() *Registry

NewRegistry 创建并初始化一个新的工具注册表。

func (*Registry) Execute

func (r *Registry) Execute(ctx context.Context, name string, argsJSON string) (string, error)

Execute 根据名称运行指定的工具。在执行前会自动触发守卫函数进行检查。

func (*Registry) Get

func (r *Registry) Get(name string) (tool.InvokableTool, bool)

Get 根据名称从注册表中检索工具实例。

func (*Registry) GetToolInfos

func (r *Registry) GetToolInfos(ctx context.Context) ([]*schema.ToolInfo, error)

GetToolInfos 返回所有已注册工具的元数据(Schema),通常用于 LLM 的工具绑定。

func (*Registry) List

func (r *Registry) List() []tool.InvokableTool

List 返回所有已注册工具的实例列表。

func (*Registry) Names

func (r *Registry) Names() []string

Names 返回所有已注册工具的名称列表。

func (*Registry) Register

func (r *Registry) Register(t tool.InvokableTool) error

Register 向注册表中添加一个新的工具实例。如果同名工具已存在,将返回错误。

func (*Registry) SetGuard added in v0.4.0

func (r *Registry) SetGuard(fn GuardFunc)

SetGuard 设置全局工具执行守卫函数,用于权限控制或审计。

type SpawnInput added in v0.3.0

type SpawnInput struct {
	Task    string `json:"task" jsonschema:"required,description=Task to delegate to a background subagent"`
	Label   string `json:"label,omitempty" jsonschema:"description=Optional label for task tracking"`
	Channel string `json:"channel,omitempty" jsonschema:"description=Optional origin channel override"`
	ChatID  string `json:"chat_id,omitempty" jsonschema:"description=Optional origin chat id override"`
}

SpawnInput 定义了 spawn 工具(异步委派)的输入参数。

type SubagentExecutor added in v0.3.0

type SubagentExecutor interface {
	// Spawn 异步启动任务。
	Spawn(ctx context.Context, req SubagentRequest) (string, error)
	// RunSync 同步运行任务并等待结果。
	RunSync(ctx context.Context, req SubagentRequest) (string, error)
}

SubagentExecutor 定义了派生或同步运行子代理任务的接口。

type SubagentInput added in v0.3.0

type SubagentInput struct {
	Task    string `json:"task" jsonschema:"required,description=Task to execute via a delegated subagent"`
	Label   string `json:"label,omitempty" jsonschema:"description=Optional label for this delegated run"`
	Channel string `json:"channel,omitempty" jsonschema:"description=Optional origin channel override"`
	ChatID  string `json:"chat_id,omitempty" jsonschema:"description=Optional origin chat id override"`
}

SubagentInput 定义了 subagent 工具(同步委派)的输入参数。

type SubagentRequest added in v0.3.0

type SubagentRequest struct {
	Task           string // 需要子代理执行的任务描述
	Label          string // 任务的可选描述性标签
	OriginChannel  string // 原始请求通道
	OriginChatID   string // 原始聊天 ID
	OriginSenderID string // 原始发送者 ID
	RequestID      string // 请求追踪 ID
}

SubagentRequest 封装了子代理工具执行所需的规范化请求信息。

type WebFetchInput added in v0.1.4

type WebFetchInput struct {
	URL      string `json:"url" jsonschema:"required,description=The target URL to fetch"`
	MaxBytes int    `json:"max_bytes" jsonschema:"description=Optional maximum response bytes to keep"`
}

WebFetchInput 定义了 web_fetch 工具的输入参数。

type WebFetchOutput added in v0.1.4

type WebFetchOutput struct {
	URL         string `json:"url"`
	Status      int    `json:"status"`
	ContentType string `json:"content_type"`
	Content     string `json:"content"`
	Truncated   bool   `json:"truncated"`
}

WebFetchOutput 定义了 web_fetch 工具的执行结果。

type WebSearchInput added in v0.1.4

type WebSearchInput struct {
	Query      string `json:"query" jsonschema:"required,description=The search query"`
	MaxResults int    `json:"max_results" jsonschema:"description=Optional per-request result limit"`
}

WebSearchInput 定义了 web_search 工具的输入参数。

type WebSearchOutput added in v0.1.4

type WebSearchOutput struct {
	Query   string            `json:"query"`
	Results []WebSearchResult `json:"results"`
}

WebSearchOutput 定义了 web_search 工具的执行结果。

type WebSearchResult added in v0.1.4

type WebSearchResult struct {
	Title       string `json:"title"`
	URL         string `json:"url"`
	Description string `json:"description"`
}

WebSearchResult 表示单条搜索结果。

type WorkflowExecutor added in v0.5.0

type WorkflowExecutor interface {
	// RunWorkflow 执行委托的工作流并返回汇总结果。
	RunWorkflow(ctx context.Context, req WorkflowRequest) (string, error)
}

WorkflowExecutor 定义了执行子代理工作流的接口。

type WorkflowInput added in v0.5.0

type WorkflowInput struct {
	Goal     string   `json:"goal" jsonschema:"required,description=Overall workflow goal for decomposition and execution"`
	Mode     string   `json:"mode,omitempty" jsonschema:"description=Execution mode: sequential or parallel"`
	Subtasks []string `json:"subtasks,omitempty" jsonschema:"description=Optional predefined subtasks"`
	Label    string   `json:"label,omitempty" jsonschema:"description=Optional workflow label for tracking"`
}

WorkflowInput 定义了 workflow 工具的输入参数。

type WorkflowRequest added in v0.5.0

type WorkflowRequest struct {
	Goal           string   // 整个工作流的最终目标
	Mode           string   // 执行模式:sequential (顺序) 或 parallel (并行)
	Subtasks       []string // 预定义的子任务列表
	Label          string   // 用于追踪的工作流标签
	OriginChannel  string   // 原始请求通道
	OriginChatID   string   // 原始聊天 ID
	OriginSenderID string   // 原始发送者 ID
	RequestID      string   // 请求追踪 ID
}

WorkflowRequest 描述结构化的子代理工作流运行请求。

type WriteFileInput

type WriteFileInput struct {
	Path    string `json:"path" jsonschema:"required,description=Absolute path to the file"`
	Content string `json:"content" jsonschema:"required,description=Content to write"`
}

WriteFileInput 定义了 write_file 工具的输入参数。

type WriteMemoryInput added in v0.1.4

type WriteMemoryInput struct {
	Content string `json:"content" jsonschema:"required,description=Content to store as long-term memory"`
}

WriteMemoryInput 定义了 write_memory 工具的输入参数。

Jump to

Keyboard shortcuts

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