toolcall

package
v0.8.8 Latest Latest
Warning

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

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

Documentation

Overview

Package toolcall provides toolcall framework

Index

Constants

This section is empty.

Variables

View Source
var DefaultToolResultTruncator = &ToolResultTruncator{
	MaxRunes:         1200000,
	MaxBytes:         1800000,
	KeepBothEnds:     true,
	TruncationMarker: "\n\n[...内容已截断...]\n\n",
}

DefaultToolResultTruncator 默认工具结果截断器。 1M 上下文时代(1,000K tokens),大幅提升截断阈值。 MaxRunes=1,200K(~600K tokens),MaxBytes=1800KB(~600K tokens), 仅为极端情况(如误输出超大文件)提供安全网。

View Source
var (

	// DispatchMCP dispatches a tool call to an MCP server.
	// Set by the mcphub package init (via web package init).
	// If nil, unknown tools fall back to the default error message.
	DispatchMCP func(ctx context.Context, toolName, argsRaw string) (result, warning string, err error)
)

Functions

func Error

func Error(err error) string

func FixBrokenJSON

func FixBrokenJSON(broken string) (result string)

func GetOrCreateTool

func GetOrCreateTool(ctx context.Context, name, description, category string) (int64, error)

GetOrCreateTool 获取或创建工具

func GetToolDisplayName

func GetToolDisplayName(name string) string

func HandleToolCall

func HandleToolCall(ctx context.Context, toolName, argsRaw string) (result, warning string, err error)

HandleToolCall 处理工具调用(带统计和超时)

func HandleToolCalls

func HandleToolCalls(ctx context.Context, tcs []prompt.ToolCall) (inputs []prompt.Message)

HandleToolCalls 处理工具调用(带统计)

func KnownToolNames

func KnownToolNames() []string

KnownToolNames returns all registered tool names from the in-memory registry.

func RecordToolUsage

func RecordToolUsage(ctx context.Context, toolID int64, success bool, errorMsg string) error

RecordToolUsage 记录工具使用

func RegisterTool

func RegisterTool(tool ToolDef) error

RegisterTool 注册工具 RegisterTool 注册工具

func ToArrayType

func ToArrayType[T PrimitiveType](anyValues []any, anyValuesLen int) []T

func ToolArgsValue

func ToolArgsValue[T Primitive](args ToolArgs, key string, defaultValue T) T

ToolArgsValue 安全获取类型化参数值

func TruncateHead

func TruncateHead(s string, maxLen int) string

TruncateHead 截取字符串头部,超出部分用省略号代替。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则返回 "原串前若干字符" + "..."

func TruncateHeadTail

func TruncateHeadTail(s string, maxLen int) string

TruncateHeadTail 截取字符串头部和尾部,中间用省略号连接。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则将可用字符数(maxLen - 省略号长度)平均分配给头尾(尾部可能多一个字符),返回 "头" + "..." + "尾"

func TruncateJSON

func TruncateJSON(content string, maxRunes int) string

TruncateJSON 截断JSON内容

func TruncateMarkdown

func TruncateMarkdown(content string, maxRunes int) string

TruncateMarkdown 截断Markdown内容

func TruncateSummary

func TruncateSummary(result string, maxRunes int) string

TruncateSummary 智能截断并添加摘要

func TruncateTail

func TruncateTail(s string, maxLen int) string

TruncateTail 截取字符串尾部,超出部分用省略号代替。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则返回 "..." + "原串后若干字符"

func TruncateToolResult

func TruncateToolResult(result string) string

TruncateToolResult 截断工具执行结果

Types

type ArrayType

type ArrayType interface {
	~[]string | ~[]float64 | ~[]float32 | ~[]int64 | ~[]int32 | ~[]int | ~[]bool
}

type Function

type Function struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Strict      bool           `json:"strict,omitempty"`
	Parameters  map[string]any `json:"parameters"` // JSON Schema 对象
}

type Primitive

type Primitive interface {
	PrimitiveType | ArrayType
}

type PrimitiveType

type PrimitiveType interface {
	~string | ~float64 | ~float32 | ~int64 | ~int32 | ~int | ~bool
}

type Tool

type Tool struct {
	Type     string   `json:"type"`
	Function Function `json:"function"`
	// contains filtered or unexported fields
}

Tool 定义可调用的工具

func GetAllTools

func GetAllTools(ctx context.Context) []Tool

GetAllTools 获取所有工具定义(用于API调用) GetAllTools returns tools available for the current role. Filters tools by role config from DB; falls back to hardcoded: dev gets all, others get none.

func (*Tool) GetTokens

func (t *Tool) GetTokens() int

type ToolArgs

type ToolArgs map[string]any

ToolArgs 参数定义

type ToolContent

type ToolContent struct {
	// Index and ToolName are not serialized to JSON — output is via String().
	Index    int    `json:"-"`
	ToolName string `json:"-"`
	Result   string `json:"result,omitzero"`
	Error    string `json:"error,omitzero"`
	Warning  string `json:"warning,omitzero"`
}

func (*ToolContent) String

func (tc *ToolContent) String() (content string)

type ToolDef

type ToolDef struct {
	Name        string
	DisplayName string
	Description string
	Strict      bool
	Parameters  map[string]any
	Category    string
	Timeout     time.Duration // 工具执行超时时间
	Handler     func(ctx context.Context, args ToolArgs) (result, warning string, err error)
}

ToolDef 工具定义

func GetToolDef

func GetToolDef(ctx context.Context, toolName string) (tool ToolDef, ok bool)

type ToolDesc

type ToolDesc struct {
	ID          int64
	Name        string
	Description string
	Category    string
	UsageCount  int
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

ToolDesc 表示一个工具

func GetTool

func GetTool(ctx context.Context, id int64) (*ToolDesc, error)

GetTool 根据ID获取工具

func GetToolByName

func GetToolByName(ctx context.Context, name string) (*ToolDesc, error)

GetToolByName 根据名称获取工具

func ListTools

func ListTools(ctx context.Context, category string) ([]ToolDesc, error)

ListTools 列出所有工具(可按分类过滤)。 以运行时注册表为权威来源,合并 DB 中的使用统计。

type ToolResultTruncator

type ToolResultTruncator struct {
	// 最大字符数(rune数量)
	MaxRunes int
	// 最大字节数
	MaxBytes int
	// 是否保留开头和结尾
	KeepBothEnds bool
	// 截断标记
	TruncationMarker string
}

ToolResultTruncator 工具结果截断器

func (*ToolResultTruncator) Truncate

func (t *ToolResultTruncator) Truncate(result string) string

Truncate 截断字符串

type ToolUsage

type ToolUsage struct {
	ID          int64
	ProjectPath string
	ToolID      int64
	UsedAt      time.Time
	Success     bool
	ErrorMsg    string
}

ToolUsage 表示工具使用记录

type ToolUsageStat

type ToolUsageStat struct {
	Name        string
	UsageCount  int
	SuccessRate float64
	LastUsed    time.Time
}

func GetProjectToolUsage

func GetProjectToolUsage(ctx context.Context, days int) ([]ToolUsageStat, error,
)

GetProjectToolUsage 获取项目工具使用情况

func GetToolUsageStats

func GetToolUsageStats(ctx context.Context, days int) ([]ToolUsageStat, error)

GetToolUsageStats 获取工具使用统计

Directories

Path Synopsis
Package ainap implements the ainap tool.
Package ainap implements the ainap tool.
Package aistatus implements the aistatus tool.
Package aistatus implements the aistatus tool.
Package alltools to load all tools
Package alltools to load all tools
Package cwd implements current-working-directory tools (cwd_get, cwd_push, cwd_pop)
Package cwd implements current-working-directory tools (cwd_get, cwd_push, cwd_pop)
Package file provides file ops tool calls
Package file provides file ops tool calls
Package flycheck registers the flycheck tool for LLM-driven syntax checking.
Package flycheck registers the flycheck tool for LLM-driven syntax checking.
Package history 注册 note 工具,供 LLM 在对话结束时记录笔记
Package history 注册 note 工具,供 LLM 在对话结束时记录笔记
Package mail registers mail tools (sendmail/readmail/mail_search/contacts) with the toolcall framework and parses LLM-issued tool arguments.
Package mail registers mail tools (sendmail/readmail/mail_search/contacts) with the toolcall framework and parses LLM-issued tool arguments.
Package memory registers memory tools (mem_save/update/search/delete/get_observation/stats) with the toolcall framework and parses LLM-issued tool arguments.
Package memory registers memory tools (mem_save/update/search/delete/get_observation/stats) with the toolcall framework and parses LLM-issued tool arguments.
Package shell for shell tools
Package shell for shell tools
Package wakeup implements the wakeup tool.
Package wakeup implements the wakeup tool.

Jump to

Keyboard shortcuts

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