toolcall

package
v0.7.5 Latest Latest
Warning

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

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

Documentation

Overview

Package toolcall provides toolcall framework

Index

Constants

This section is empty.

Variables

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

DefaultToolResultTruncator 默认工具结果截断器

Functions

func CreateOrGetSessionID

func CreateOrGetSessionID(ctx context.Context) (sessionID int64, err error)

CreateOrGetSessionID 获取或创建会话ID

func Error

func Error(err error) string

func FixBrokenJSON

func FixBrokenJSON(broken string) (result string)

func GetCurrentSessionID

func GetCurrentSessionID(ctx context.Context) (sessionID int64)

func GetOrCreateTool

func GetOrCreateTool(name, description, category string) (int64, error)

GetOrCreateTool 获取或创建工具

func GetTokenFromNetrc

func GetTokenFromNetrc(host string) (string, error)

GetTokenFromNetrc 从.netrc文件获取指定主机的token

func GetToolDisplayName

func GetToolDisplayName(name string) string

func HandleToolCall

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

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

func JudgeHistory

func JudgeHistory(messages []*Message)

JudgeHistory - Cleanup the history

func RecordToolUsage

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

RecordToolUsage 记录工具使用

func RegisterTool

func RegisterTool(tool ToolDef)

RegisterTool 注册工具

func SaveMessages

func SaveMessages(ctx context.Context, msgs ...Message) error

SaveMessages 保存消息(事务)

func ToArrayType

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

func ToSQLNullString

func ToSQLNullString(tcs []ToolCall) (toolCalls sql.NullString)

func ToolArgsValue

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

ToolArgsValue 安全获取类型化参数值

func ToolCallsID

func ToolCallsID(tcs []ToolCall) string

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 截断工具执行结果

func UpdateContent

func UpdateContent(ctx context.Context, id int64, content string) (err error)

UpdateContent update message content

func UpdateHistory

func UpdateHistory(ctx context.Context, id int64) (err error)

UpdateHistory update message session_id to 0

func UpdateToolCalls

func UpdateToolCalls(ctx context.Context, id int64, tcs []ToolCall) (err error)

UpdateToolCalls update message content

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

func CleanupReverse(messages []Message) (cleaned []Message)

CleanupReverse - make the messages clean, remove the mistake message

func HandleToolCalls

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

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

func ListHistory

func ListHistory(ctx context.Context) ([]*Message, error)

ListHistory 加载指定会话的所有历史消息,按时间升序返回

func LoadHistory

func LoadHistory(ctx context.Context) ([]Message, error)

LoadHistory 加载指定会话的所有历史消息,按时间升序返回

func LoadPrompts

func LoadPrompts(ctx context.Context) ([]Message, error)

LoadPrompts loads the system prompt combined with skill prompts.

func ShowMessage

func ShowMessage(ctx context.Context, id int64) (message *Message, err error)

func (*Message) GetTokens

func (m *Message) GetTokens() int

type NetrcEntry

type NetrcEntry struct {
	Machine  string
	Login    string
	Password string
}

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 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调用)

func (*Tool) GetTokens

func (t *Tool) GetTokens() int

type ToolArgs

type ToolArgs map[string]any

ToolArgs 参数定义

type ToolCall

type ToolCall struct {
	ID       string           `json:"id"`
	Type     string           `json:"type"`
	Function ToolCallFunction `json:"function"`
}

type ToolCallFunction

type ToolCallFunction struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"` // JSON 字符串
}

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 工具定义

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(id int64) (*ToolDesc, error)

GetTool 根据ID获取工具

func GetToolByName

func GetToolByName(name string) (*ToolDesc, error)

GetToolByName 根据名称获取工具

func ListTools

func ListTools(category string) ([]ToolDesc, error)

ListTools 列出所有工具(可按分类过滤)

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(days int) ([]ToolUsageStat, error)

GetToolUsageStats 获取工具使用统计

Source Files

  • message.go
  • netrc.go
  • prompt.go
  • session.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
Package shell for shell tools

Jump to

Keyboard shortcuts

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