Documentation
¶
Overview ¶
Package toolcall provides toolcall framework
Index ¶
- Variables
- func ArrangeArgs(ctx context.Context, cacheFile string) (context.Context, bool)
- func CreateOrGetSessionID(ctx context.Context) (sessionID int64, err error)
- func Error(err error) string
- func FixBrokenJSON(broken string) (result string)
- func GetCurrentSessionID(ctx context.Context) (sessionID int64)
- func GetOrCreateCacheFile(ctx context.Context, script string) (cacheFile string, err error)
- func GetOrCreateTool(name, description, category string) (int64, error)
- func GetTokenFromNetrc(host string) (string, error)
- func GetToolDisplayName(name string) string
- func GuessLanguage(path string) string
- func HandleToolCall(ctx context.Context, toolName string, argsRaw string) (result string, user string, err error)
- func JudgeHistory(messages []*Message)
- func RecordToolUsage(ctx context.Context, toolID int64, success bool, errorMsg string) error
- func RegisterTool(tool ToolDef)
- func RunShell(ctx context.Context, script string) (result string, err error)
- func SaveMessages(ctx context.Context, msgs ...Message) error
- func Shebang(script string) (name string, arg []string)
- func ShellExec(ctx context.Context, script string) (out string, err error)
- func ShortenShellScript(ctx context.Context, script string) string
- func ToArrayType[T PrimitiveType](anyValues []any, anyValuesLen int) []T
- func ToSQLNullString(tcs []ToolCall) (toolCalls sql.NullString)
- func ToolArgsValue[T Primitive](args ToolArgs, key string, defaultValue T) T
- func ToolCallsID(tcs []ToolCall) string
- func TruncateHead(s string, maxLen int) string
- func TruncateHeadTail(s string, maxLen int) string
- func TruncateJSON(content string, maxRunes int) string
- func TruncateMarkdown(content string, maxRunes int) string
- func TruncateString(s string, maxLen int) string
- func TruncateSummary(result string, maxRunes int) string
- func TruncateTail(s string, maxLen int) string
- func TruncateToolResult(result string) string
- func UpdateContent(ctx context.Context, id int64, content string) (err error)
- func UpdateHistory(ctx context.Context, id int64) (err error)
- func UpdateToolCalls(ctx context.Context, id int64, tcs []ToolCall) (err error)
- type ArrayType
- type FileStructure
- type Function
- type Message
- func CleanupReverse(messages []Message) (cleaned []Message)
- func HandleToolCalls(ctx context.Context, tcs []ToolCall) (inputs []Message)
- func ListHistory(ctx context.Context) ([]*Message, error)
- func LoadHistory(ctx context.Context) ([]Message, error)
- func LoadPrompts(ctx context.Context) ([]Message, error)
- func ShowMessage(ctx context.Context, id int64) (message *Message, err error)
- type NetrcEntry
- type Primitive
- type PrimitiveType
- type Symbol
- type Tool
- type ToolArgs
- type ToolCall
- type ToolCallFunction
- type ToolContent
- type ToolDef
- type ToolDesc
- type ToolResultTruncator
- type ToolUsage
- type ToolUsageStat
Constants ¶
This section is empty.
Variables ¶
var DefaultToolResultTruncator = &ToolResultTruncator{ MaxRunes: 8000, MaxBytes: 16000, KeepBothEnds: true, TruncationMarker: "\n\n[...内容已截断...]\n\n", }
DefaultToolResultTruncator 默认工具结果截断器
Functions ¶
func CreateOrGetSessionID ¶
CreateOrGetSessionID 获取或创建会话ID
func GetOrCreateCacheFile ¶
func GetOrCreateTool ¶
GetOrCreateTool 获取或创建工具
func GetTokenFromNetrc ¶
GetTokenFromNetrc 从.netrc文件获取指定主机的token
func HandleToolCall ¶
func HandleToolCall(ctx context.Context, toolName string, argsRaw string) (result string, user string, err error)
HandleToolCall 处理工具调用(带统计和超时)
func RecordToolUsage ¶
RecordToolUsage 记录工具使用
func SaveMessages ¶
SaveMessages 保存消息(事务)
func ShellExec ¶
ShellExec 是混合实现的 Shell 执行函数 对于 shell 命令使用新的 internal/shell 包 对于非 shell 命令回退到原始的 os/exec 实现
func ShortenShellScript ¶
ShortenShellScript 生成脚本的简短摘要
func ToArrayType ¶
func ToArrayType[T PrimitiveType](anyValues []any, anyValuesLen int) []T
func ToSQLNullString ¶
func ToSQLNullString(tcs []ToolCall) (toolCalls sql.NullString)
func ToolArgsValue ¶
ToolArgsValue 安全获取类型化参数值
func TruncateHead ¶
TruncateHead 截取字符串头部,超出部分用省略号代替。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则返回 "原串前若干字符" + "..."
func TruncateHeadTail ¶
TruncateHeadTail 截取字符串头部和尾部,中间用省略号连接。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则将可用字符数(maxLen - 省略号长度)平均分配给头尾(尾部可能多一个字符),返回 "头" + "..." + "尾"
func TruncateMarkdown ¶
TruncateMarkdown 截断Markdown内容
func TruncateString ¶
TruncateString 截断字符串,并在末尾添加省略号 参数:
s: 要截断的字符串 maxLen: 截断后的最大字符数(包括省略号)
返回值:
截断后的字符串。如果字符串长度不超过maxLen,则返回原字符串。 如果maxLen小于3,返回空字符串。 否则返回前(maxLen-3)个字符加上"..."。
注意:
- 使用[]rune处理Unicode字符,避免截断时出现乱码
- 省略号"..."占用3个字符位置
- 当maxLen < 3时,无法添加省略号,返回空字符串
示例:
TruncateString("Hello World", 8) // 返回 "Hello..."
TruncateString("你好世界", 4) // 返回 "你好世界"
TruncateString("Test", 2) // 返回 "" (maxLen < 3)
func TruncateSummary ¶
TruncateSummary 智能截断并添加摘要
func TruncateTail ¶
TruncateTail 截取字符串尾部,超出部分用省略号代替。 若原字符串长度 <= maxLen,直接返回原串。 若 maxLen <= 省略号长度,返回省略号的前 maxLen 个字符。 否则返回 "..." + "原串后若干字符"
func UpdateContent ¶
UpdateContent update message content
func UpdateHistory ¶
UpdateHistory update message session_id to 0
Types ¶
type ArrayType ¶
type FileStructure ¶
type FileStructure struct {
Language string `json:"language"`
FilePath string `json:"file_path"`
Package string `json:"package,omitempty"`
Functions []*Symbol `json:"functions"`
Classes []*Symbol `json:"classes"`
Imports []string `json:"imports"`
Errors []string `json:"errors,omitempty"`
}
FileStructure 表示文件结构
type Function ¶
type Message ¶
type Message struct {
ID int64 `json:"-"`
SessionID int64 `json:"-"`
ModelID int64 `json:"-"`
Role string `json:"role"`
ReasoningContent string `json:"reasoning_content"`
Content string `json:"content"` // 始终输出,即使为空字符串
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // 仅当有工具调用时输出
ToolCallID string `json:"tool_call_id,omitempty"` // 仅当 role="tool" 时输出
CreatedAt time.Time `json:"-"`
OK bool `json:"-"`
// contains filtered or unexported fields
}
Message 扩展,支持工具调用(注意:Content 字段不再使用 omitempty)
func CleanupReverse ¶
CleanupReverse - make the messages clean, remove the mistake message
func HandleToolCalls ¶
HandleToolCalls 处理工具调用(带统计)
func ListHistory ¶
ListHistory 加载指定会话的所有历史消息,按时间升序返回
func LoadHistory ¶
LoadHistory 加载指定会话的所有历史消息,按时间升序返回
func LoadPrompts ¶
LoadPrompts loads the system prompt combined with skill prompts.
type NetrcEntry ¶
NetrcEntry 表示.netrc文件中的一个条目
func ParseNetrc ¶
func ParseNetrc(path string) ([]NetrcEntry, error)
ParseNetrc 解析.netrc文件,支持单行格式:machine host login user password token
type Primitive ¶
type Primitive interface {
PrimitiveType | ArrayType
}
type PrimitiveType ¶
type Symbol ¶
type Symbol struct {
Name string `json:"name"`
Type string `json:"type"` // "function", "method", "struct", "interface", "type"
Line int `json:"line"`
Column int `json:"column"`
EndLine int `json:"end_line"`
EndColumn int `json:"end_column"`
Signature string `json:"signature,omitempty"` // 函数签名
Receiver string `json:"receiver,omitempty"` // 方法接收者
}
Symbol 表示一个代码符号(函数/类)
type Tool ¶
type Tool struct {
Type string `json:"type"`
Function Function `json:"function"`
// contains filtered or unexported fields
}
Tool 定义可调用的工具
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"`
Function ToolCallFunction `json:"function"`
}
type ToolCallFunction ¶
type ToolContent ¶
type ToolContent struct {
Result string `json:"result,omitzero"`
Error string `json:"error,omitzero"`
Suggestion string `json:"suggestion,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) (string, string, error)
}
ToolDef 工具定义
type ToolDesc ¶
type ToolDesc struct {
ID int64
Name string
Description string
Category string
UsageCount int
CreatedAt time.Time
UpdatedAt time.Time
}
ToolDesc 表示一个工具
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 表示工具使用记录
Source Files
¶
- message.go
- netrc.go
- parse.go
- prompt.go
- session.go
- shell.go
- tool.go
- tool_def.go
- truncate.go
Directories
¶
| Path | Synopsis |
|---|---|
|
Package alltools to load all tools
|
Package alltools to load all tools |
|
Package file provides file ops tool calls
|
Package file provides file ops tool calls |
|
Package issue to address issue create, list, show, assign, close
|
Package issue to address issue create, list, show, assign, close |
|
Package shell for shell tools like shell and python
|
Package shell for shell tools like shell and python |