aitool

package
v1.4.4-beta12 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SchemaGeneratorExports = map[string]any{
	"ActionObject":         NewObjectSchemaWithAction,
	"Object":               NewObjectSchema,
	"ObjectArray":          newObjectArraySchema,
	"NewObjectSchema":      NewObjectSchema,
	"NewObjectArraySchema": newObjectArraySchema,

	"action":                  WithAction,
	"paramString":             WithStringParam,
	"paramInt":                WithIntegerParam,
	"paramBool":               WithBoolParam,
	"paramNumber":             WithNumberParam,
	"paramStringArray":        WithStringArrayParam,
	"paramNumberArray":        WithNumberArrayParam,
	"paramKeyValuePairsArray": WithKVPairsParam,
	"paramObjectArray":        _withObjectArray,
	"paramObjectArrayEx":      _withObjectArrayEx,
	"paramObject":             _withParamObject,
	"paramRaw":                WithRawParam,

	"description": WithParam_Description,
	"required":    WithParam_Required,
	"min":         WithParam_Min,
	"max":         WithParam_Max,
	"maxLength":   WithParam_MaxLength,
	"minLength":   WithParam_MinLength,
	"const":       WithParam_Const,
	"enum":        WithParam_Enum,
	"title":       WithParam_Title,
	"raw":         WithParam_Raw,
	"example":     WithParam_Example,
}

Functions

func CompareTools

func CompareTools(t1, t2 *Tool) []string

CompareTools compares two tools and returns a list of differences This is useful for debugging round-trip conversion issues

func NewObjectSchema

func NewObjectSchema(opts ...any) string

func NewObjectSchemaFrameOmap

func NewObjectSchemaFrameOmap(opts ...any) *omap.OrderedMap[string, any]

func NewObjectSchemaWithAction

func NewObjectSchemaWithAction(opts ...any) string

func NewObjectSchemaWithActionName

func NewObjectSchemaWithActionName(name string, opts ...any) string

func ValidateResult

func ValidateResult(resultJSON string) (bool, []string)

ValidateResult 验证结果是否符合JSON Schema

Types

type InvokeCallback

type InvokeCallback func(ctx context.Context, params InvokeParams, invokeExConfig *ToolRuntimeConfig, stdout io.Writer, stderr io.Writer) (any, error)

type InvokeParams

type InvokeParams map[string]any

func (InvokeParams) Dump

func (r InvokeParams) Dump() string

func (InvokeParams) GetAnyToString

func (p InvokeParams) GetAnyToString(key string, backups ...string) string

func (InvokeParams) GetBool

func (p InvokeParams) GetBool(key string, backups ...bool) bool

func (InvokeParams) GetFloat

func (p InvokeParams) GetFloat(key string, backups ...float64) float64

func (InvokeParams) GetInt

func (p InvokeParams) GetInt(key string, backups ...int64) int64

func (InvokeParams) GetInteger

func (p InvokeParams) GetInteger(key string, backups ...int) int

func (InvokeParams) GetObject

func (p InvokeParams) GetObject(key string) InvokeParams

func (InvokeParams) GetObjectArray

func (p InvokeParams) GetObjectArray(key string) []InvokeParams

func (InvokeParams) GetString

func (p InvokeParams) GetString(key string, backups ...string) string

func (InvokeParams) GetStringSlice

func (p InvokeParams) GetStringSlice(key string, backups ...[]string) []string

func (InvokeParams) Has

func (p InvokeParams) Has(key string) bool

func (InvokeParams) Set

func (p InvokeParams) Set(k string, v any) InvokeParams

type NoRuntimeInvokeCallback

type NoRuntimeInvokeCallback func(ctx context.Context, params InvokeParams, stdout io.Writer, stderr io.Writer) (any, error)

NoRuntimeInvokeCallback 定义工具调用回调函数的签名

type PropertyOption

type PropertyOption func(map[string]any)

PropertyOption 定义属性选项函数的类型

func WithParam_Const

func WithParam_Const(values ...any) PropertyOption

WithParam_Enum specifies a list of allowed values for a string property. The property value must be one of the specified enum values.

func WithParam_Default

func WithParam_Default(desc any) PropertyOption

WithParam_Default sets the default value for a property. This value will be used if the property is not explicitly provided.

func WithParam_Description

func WithParam_Description(desc string) PropertyOption

WithParam_Description adds a description to a property in the JSON Schema. The description should explain the purpose and expected values of the property.

func WithParam_Enum

func WithParam_Enum(values ...any) PropertyOption

WithParam_Enum specifies a list of allowed values for a string property. The property value must be one of the specified enum values.

func WithParam_EnumString

func WithParam_EnumString(values ...string) PropertyOption

func WithParam_Example

func WithParam_Example(i any) PropertyOption

WithParam_Example adds an example value for a property in the JSON Schema.

func WithParam_Max

func WithParam_Max(max float64) PropertyOption

WithParam_Max sets the maximum value for a number property. The number value must not exceed this maximum.

func WithParam_MaxLength

func WithParam_MaxLength(max int) PropertyOption

WithParam_MaxLength sets the maximum length for a string property. The string value must not exceed this length.

func WithParam_Min

func WithParam_Min(min float64) PropertyOption

WithParam_Min sets the minimum value for a number property. The number value must not be less than this minimum.

func WithParam_MinLength

func WithParam_MinLength(min int) PropertyOption

WithParam_MinLength sets the minimum length for a string property. The string value must be at least this length.

func WithParam_MultipleOf

func WithParam_MultipleOf(value float64) PropertyOption

WithParam_MultipleOf specifies that a number must be a multiple of the given value. The number value must be divisible by this value.

func WithParam_Pattern

func WithParam_Pattern(pattern string) PropertyOption

WithParam_Pattern sets a regex pattern that a string property must match. The string value must conform to the specified regular expression.

func WithParam_Raw

func WithParam_Raw(name string, v any) PropertyOption

WithParam_Raw adds a raw JSON schema object to the tool's input schema.

func WithParam_RequireTool

func WithParam_RequireTool(tool string) PropertyOption

WithParam_RequireTool adds a ATTENTION description to a property in the JSON Schema. require tool description means prerequisites for running this tool

func WithParam_Required

func WithParam_Required(required ...bool) PropertyOption

WithParam_Required marks a property as required in the tool's input schema. WithParam_Required properties must be provided when using the tool.

func WithParam_Title

func WithParam_Title(title string) PropertyOption

WithParam_Title adds a display-friendly title to a property in the JSON Schema. This title can be used by UI components to show a more readable property name.

type Tool

type Tool struct {
	*mcp.Tool
	// A list of keywords for tool indexing and searching.
	Keywords []string       `json:"keywords,omitempty"`
	Callback InvokeCallback // 添加回调函数字段
}

func LoadAIToolFromMCPServers

func LoadAIToolFromMCPServers(db *gorm.DB, ctx context.Context, name string) ([]*Tool, error)

LoadAIToolFromMCPServers 从数据库中加载指定名称的 MCP 服务器,并将其工具转换为 AITool name: MCP 服务器名称 db: 数据库连接,如果为 nil 则使用默认的 profile 数据库 返回: 从该 MCP 服务器加载的所有 AITool 列表

func LoadAllEnabledAIToolsFromMCPServers

func LoadAllEnabledAIToolsFromMCPServers(db *gorm.DB, ctx context.Context) ([]*Tool, error)

func LoadAllEnabledAIToolsFromMCPServersWithCallback

func LoadAllEnabledAIToolsFromMCPServersWithCallback(
	db *gorm.DB, ctx context.Context,
	onStart func(mcpServer *schema.MCPServer),
	onDone func(mcpServer *schema.MCPServer, tools []*Tool, err error),
	onAllDone func(tools []*Tool, err error),
) ([]*Tool, error)

func New

func New(name string, options ...ToolOption) (*Tool, error)

New 使用函数选项模式创建一个新的Tool实例

func NewFromMCPTool

func NewFromMCPTool(mt *mcp.Tool, opts ...ToolOption) (*Tool, error)

func NewToolFromJSON

func NewToolFromJSON(jsonStr string, callback func(params InvokeParams, stdout io.Writer, stderr io.Writer) (any, error)) (*Tool, error)

NewToolFromJSON 从JSON定义创建工具

func NewWithoutCallback

func NewWithoutCallback(name string, opts ...ToolOption) *Tool

func (*Tool) BuildParamsOptions

func (t *Tool) BuildParamsOptions() []ToolOption

BuildParamsOptions converts a Tool's InputSchema Properties back into ToolOptions This is the reverse operation of WithXxxParam functions

func (*Tool) ExecuteToolWithCapture

func (t *Tool) ExecuteToolWithCapture(ctx context.Context, params map[string]any, config *ToolInvokeConfig) (*ToolExecutionResult, error)

func (*Tool) GetDescription

func (t *Tool) GetDescription() string

func (*Tool) GetKeywords

func (t *Tool) GetKeywords() []string

func (*Tool) GetName

func (t *Tool) GetName() string

func (*Tool) InvokeWithJSON

func (t *Tool) InvokeWithJSON(jsonStr string, opts ...ToolInvokeOptions) (*ToolResult, error)

InvokeWithJSON 使用JSON字符串调用工具

func (*Tool) InvokeWithOrderedJSON

func (t *Tool) InvokeWithOrderedJSON(jsonStr string, opts ...ToolInvokeOptions) (*ToolResult, error)

InvokeWithOrderedJSON 使用JSON字符串调用工具,并保持参数顺序

func (*Tool) InvokeWithOrderedParams

func (t *Tool) InvokeWithOrderedParams(params *omap.OrderedMap[string, any], opts ...ToolInvokeOptions) (*ToolResult, error)

InvokeWithOrderedParams 使用 OrderedMap 参数调用工具

func (*Tool) InvokeWithParams

func (t *Tool) InvokeWithParams(params map[string]any, opts ...ToolInvokeOptions) (*ToolResult, error)

InvokeWithParams 使用参数映射调用工具

func (*Tool) InvokeWithRaw

func (t *Tool) InvokeWithRaw(raw string, opts ...ToolInvokeOptions) (*ToolResult, error)

func (*Tool) Params

func (t *Tool) Params() *omap.OrderedMap[string, any]

func (*Tool) ParamsJsonSchemaString

func (t *Tool) ParamsJsonSchemaString() string

func (*Tool) RebuildTool

func (t *Tool) RebuildTool() (*Tool, error)

RebuildTool creates a new Tool with the same configuration as the original This is useful for testing round-trip conversion

func (*Tool) ToJSONSchema

func (t *Tool) ToJSONSchema() *omap.OrderedMap[string, any]

ToJSONSchema 将整个Tool转换为符合JSON Schema Draft-07规范的格式

func (*Tool) ToJSONSchemaString

func (t *Tool) ToJSONSchemaString() string

ToJSONSchemaString 将Tool转换为JSON Schema字符串

func (*Tool) Validate

func (t *Tool) Validate(params map[string]any) (bool, []string)

func (*Tool) ValidateJSONString

func (t *Tool) ValidateJSONString(jsonStr string) (bool, []string)

ValidateJSONString 验证JSON字符串是否符合工具的要求

func (*Tool) ValidateOrderedParams

func (t *Tool) ValidateOrderedParams(params *omap.OrderedMap[string, any]) (bool, []string)

ValidateOrderedParams 验证 OrderedMap 参数

func (*Tool) ValidateParams

func (t *Tool) ValidateParams(params map[string]any) (bool, []string)

ValidateParams 验证普通 map 参数

type ToolCallCancelCallback

type ToolCallCancelCallback func(result *ToolExecutionResult, err error) (*ToolExecutionResult, error)

type ToolExecutionResult

type ToolExecutionResult struct {
	Stdout string      `json:"stdout"`
	Stderr string      `json:"stderr,omitempty"`
	Result interface{} `json:"result,omitempty"`
}

ToolExecutionResult 表示工具执行的完整结果

func (*ToolExecutionResult) GetJSONSchema

func (r *ToolExecutionResult) GetJSONSchema() map[string]interface{}

GetJSONSchema 获取结果的JSON Schema

func (*ToolExecutionResult) GetJSONSchemaString

func (r *ToolExecutionResult) GetJSONSchemaString() string

GetJSONSchemaString 获取结果的JSON Schema字符串

func (*ToolExecutionResult) ToJSON

func (r *ToolExecutionResult) ToJSON() (string, error)

ToJSON 将执行结果转换为JSON字符串

type ToolFactory

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

func NewFactory

func NewFactory() *ToolFactory

func (*ToolFactory) RegisterTool

func (f *ToolFactory) RegisterTool(toolName string, opts ...ToolOption) error

func (*ToolFactory) Tools

func (f *ToolFactory) Tools() []*Tool

type ToolInvokeConfig

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

func NewToolInvokeConfig

func NewToolInvokeConfig() *ToolInvokeConfig

func (*ToolInvokeConfig) GetCancelCallback

func (i *ToolInvokeConfig) GetCancelCallback() ToolCallCancelCallback

func (*ToolInvokeConfig) GetErrCallback

func (i *ToolInvokeConfig) GetErrCallback() func(error) (*ToolResult, error)

func (*ToolInvokeConfig) GetResCallback

func (i *ToolInvokeConfig) GetResCallback() func(result *ToolExecutionResult) (*ToolResult, error)

func (*ToolInvokeConfig) GetRuntimeConfig

func (i *ToolInvokeConfig) GetRuntimeConfig() *ToolRuntimeConfig

func (*ToolInvokeConfig) GetStderr

func (i *ToolInvokeConfig) GetStderr() io.Writer

func (*ToolInvokeConfig) GetStdout

func (i *ToolInvokeConfig) GetStdout() io.Writer

type ToolInvokeOptions

type ToolInvokeOptions func(*ToolInvokeConfig)

func WithCancelCallback

func WithCancelCallback(callback ToolCallCancelCallback) ToolInvokeOptions

func WithContext

func WithContext(ctx context.Context) ToolInvokeOptions

func WithErrorCallback

func WithErrorCallback(callback func(error) (*ToolResult, error)) ToolInvokeOptions

func WithResultCallback

func WithResultCallback(callback func(result *ToolExecutionResult) (*ToolResult, error)) ToolInvokeOptions

func WithRuntimeConfig

func WithRuntimeConfig(config *ToolRuntimeConfig) ToolInvokeOptions

func WithStderr

func WithStderr(stderr io.Writer) ToolInvokeOptions

func WithStdout

func WithStdout(stdout io.Writer) ToolInvokeOptions

type ToolInvokeParams

type ToolInvokeParams struct {
	Tool   string         `json:"tool"`
	Action string         `json:"@action"`
	Params map[string]any `json:"params,omitempty"`
}

ToolInvokeParams 表示工具调用的参数

type ToolOption

type ToolOption func(*Tool)

ToolOption 定义工具选项函数的类型

func ConvertYaklangCliCodeToToolOptions

func ConvertYaklangCliCodeToToolOptions(yakCode string) []ToolOption

ConvertYaklangCliCodeToToolOptions 将 Yaklang CLI 代码转换为 aitool.ToolOption

func WithAction

func WithAction(action string) ToolOption

func WithAnyOfStructParam

func WithAnyOfStructParam(name string, opts []PropertyOption, itemsOpt ...[]ToolOption) ToolOption

WithAnyOfStructParam

func WithArrayParam

func WithArrayParam(name string, itemType string, opts []PropertyOption, itemsOpt ...PropertyOption) ToolOption

func WithArrayParamEx

func WithArrayParamEx(name string, opts []PropertyOption, itemsOpt ToolOption) ToolOption

func WithBoolParam

func WithBoolParam(name string, opts ...PropertyOption) ToolOption

WithBoolParam adds a boolean property to the tool schema. It accepts property options to configure the boolean property's behavior and constraints.

func WithCallback

func WithCallback(callback InvokeCallback) ToolOption

func WithDangerousNoNeedTimelineRecorded

func WithDangerousNoNeedTimelineRecorded(i bool) ToolOption

WithDangerousNoNeedTimelineRecorded 设置工具是否需要时间线记录

func WithDangerousNoNeedUserReview

func WithDangerousNoNeedUserReview(i bool) ToolOption

WithDangerousNoNeedUserReview 设置工具是否不需要用户审核

func WithDescription

func WithDescription(description string) ToolOption

WithDescription 设置工具的描述信息

func WithIntegerParam

func WithIntegerParam(name string, opts ...PropertyOption) ToolOption

WithIntegerParam adds a integer property to the tool schema. It accepts property options to configure the integer property's behavior and constraints.

func WithKVPairsParam

func WithKVPairsParam(name string, opts ...PropertyOption) ToolOption

func WithKeywords

func WithKeywords(keywords []string) ToolOption

WithKeywords 设置工具索引关键词

func WithNoRuntimeCallback

func WithNoRuntimeCallback(callback NoRuntimeInvokeCallback) ToolOption

func WithNullParam

func WithNullParam(name string, opts ...PropertyOption) ToolOption

func WithNumberArrayParam

func WithNumberArrayParam(name string, opts ...PropertyOption) ToolOption

WithNumberArrayParam adds a number array property to the tool schema. It accepts property options to configure the number-array property's behavior and constraints.

func WithNumberParam

func WithNumberParam(name string, opts ...PropertyOption) ToolOption

WithNumberParam adds a number property to the tool schema. It accepts property options to configure the number property's behavior and constraints.

func WithOneOfStructParam

func WithOneOfStructParam(name string, opts []PropertyOption, itemsOpt ...[]ToolOption) ToolOption

WithOneOfStructParam

func WithPagingParam

func WithPagingParam(name string, fieldNames []string, opts ...PropertyOption) ToolOption

func WithRawParam

func WithRawParam(name string, object map[string]any, opts ...PropertyOption) ToolOption

WithRawParam adds a custom object property to the tool schema. It accepts property options to configure the object property's behavior and constraints.

func WithSimpleArrayParam

func WithSimpleArrayParam(name string, itemType string, opts ...PropertyOption) ToolOption

func WithSimpleCallback

func WithSimpleCallback(callback func(params InvokeParams, stdout io.Writer, stderr io.Writer) (any, error)) ToolOption

WithSimpleCallback 设置工具的回调函数

func WithStringArrayParam

func WithStringArrayParam(name string, opts ...PropertyOption) ToolOption

WithStringArrayParam adds a string array property to the tool schema. It accepts property options to configure the string-array property's behavior and constraints.

func WithStringArrayParamEx

func WithStringArrayParamEx(name string, opts []PropertyOption, itemsOpt ...PropertyOption) ToolOption

func WithStringParam

func WithStringParam(name string, opts ...PropertyOption) ToolOption

WithStringParam adds a string property to the tool schema. It accepts property options to configure the string property's behavior and constraints.

func WithStructArrayParam

func WithStructArrayParam(name string, opts []PropertyOption, structOpts []PropertyOption, structMembers ...ToolOption) ToolOption

func WithStructParam

func WithStructParam(name string, opts []PropertyOption, itemsOpt ...ToolOption) ToolOption

type ToolResult

type ToolResult struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	Param       any    `json:"param"`
	Success     bool   `json:"success"`
	Data        any    `json:"data,omitempty"`
	Error       string `json:"error,omitempty"`
	ToolCallID  string `json:"call_tool_id,omitempty"` // 用于标识调用的工具ID,通常是一个唯一标识符

	// shrink_similar_result 表示相似缩略信息,是相似度过高的工具调用引发的压缩。
	ShrinkSimilarResult string `json:"shrink_similar_result,omitempty"`

	// shrink_similar_result 表示缩略信息,是由于时间线内容过多引发的压缩。
	ShrinkResult string `json:"shrink_result,omitempty"`
}

ToolResult 表示工具调用的结果

func (*ToolResult) Dump

func (t *ToolResult) Dump() string

func (*ToolResult) DumpTimelineItem

func (t *ToolResult) DumpTimelineItem(buf io.Writer)

func (*ToolResult) GetID

func (t *ToolResult) GetID() int64

func (*ToolResult) GetShrinkResult

func (t *ToolResult) GetShrinkResult() string

func (*ToolResult) GetShrinkSimilarResult

func (t *ToolResult) GetShrinkSimilarResult() string

func (*ToolResult) QuoteDescription

func (t *ToolResult) QuoteDescription() string

func (*ToolResult) QuoteError

func (t *ToolResult) QuoteError() string

func (*ToolResult) QuoteName

func (t *ToolResult) QuoteName() string

func (*ToolResult) QuoteParams

func (t *ToolResult) QuoteParams() string

func (*ToolResult) QuoteResult

func (t *ToolResult) QuoteResult() string

func (*ToolResult) SetShrinkResult

func (t *ToolResult) SetShrinkResult(i string)

func (*ToolResult) String

func (t *ToolResult) String() string

func (*ToolResult) StringWithoutID

func (t *ToolResult) StringWithoutID() string

type ToolRuntimeConfig

type ToolRuntimeConfig struct {
	FeedBacker func(result *ypb.ExecResult) error
	RuntimeID  string
}

Jump to

Keyboard shortcuts

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