Documentation
¶
Index ¶
- Constants
- Variables
- func BenchmarkCompareShardedVsNormal()
- func ExampleCacheOperations()
- func ExampleDynamicConfiguration()
- func ExampleMonitoringAndTuning()
- func ExampleShardedCacheUsage()
- func WithMiddleware(tool interfaces.Tool, middlewares ...interface{}) interfaces.Tool
- type BaseRuntimeTool
- type BaseTool
- type CacheStats
- type CachedTool
- func (c *CachedTool) ArgsSchema() string
- func (c *CachedTool) Description() string
- func (c *CachedTool) InvalidateCacheByPrefix(ctx context.Context, prefix string) error
- func (c *CachedTool) Invoke(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)
- func (c *CachedTool) Name() string
- type CleanupIntervalRecommendation
- type CleanupStrategy
- type ErrorHandler
- type EvictionPolicy
- type ExecutorOption
- type FunctionTool
- func NewFunctionTool(name string, description string, argsSchema string, ...) *FunctionTool
- func NewSimpleFunctionTool(name string, description string, fn SimpleFunction) *FunctionTool
- func NewTypedFunctionTool[I, O any](name string, description string, argsSchema string, fn TypedFunction[I, O]) *FunctionTool
- type FunctionToolBuilder
- func (b *FunctionToolBuilder) Build() (*FunctionTool, error)
- func (b *FunctionToolBuilder) MustBuild() *FunctionTool
- func (b *FunctionToolBuilder) WithArgsSchema(schema string) *FunctionToolBuilder
- func (b *FunctionToolBuilder) WithArgsSchemaFromStruct(v interface{}) *FunctionToolBuilder
- func (b *FunctionToolBuilder) WithDescription(description string) *FunctionToolBuilder
- func (b *FunctionToolBuilder) WithFunction(fn func(context.Context, map[string]interface{}) (interface{}, error)) *FunctionToolBuilder
- type LoadBalancingStrategy
- type MemoryCacheConfig
- type MemoryToolCache
- func (c *MemoryToolCache) AddDependency(dependentTool, dependsOnTool string)
- func (c *MemoryToolCache) Clear() error
- func (c *MemoryToolCache) Close()
- func (c *MemoryToolCache) Delete(ctx context.Context, key string) error
- func (c *MemoryToolCache) Get(ctx context.Context, key string) (*interfaces.ToolOutput, bool)
- func (c *MemoryToolCache) GetStats() CacheStats
- func (c *MemoryToolCache) GetVersion() int64
- func (c *MemoryToolCache) InvalidateByPattern(ctx context.Context, pattern string) (int, error)
- func (c *MemoryToolCache) InvalidateByTool(ctx context.Context, toolName string) (int, error)
- func (c *MemoryToolCache) RemoveDependency(dependentTool, dependsOnTool string)
- func (c *MemoryToolCache) Set(ctx context.Context, key string, output *interfaces.ToolOutput, ...) error
- func (c *MemoryToolCache) Size() int
- type MiddlewareTool
- type PerformanceProfile
- type Registry
- type RetryPolicy
- type RuntimeConfig
- type RuntimeTool
- type RuntimeToolAdapter
- type SavePreferenceTool
- type ShardCountRecommendation
- type ShardedCacheConfig
- type ShardedCacheOption
- func WithAutoTuning(enabled bool) ShardedCacheOption
- func WithCapacity(capacity int) ShardedCacheOption
- func WithCleanupInterval(interval time.Duration) ShardedCacheOption
- func WithCleanupStrategy(strategy CleanupStrategy) ShardedCacheOption
- func WithCompressionThreshold(bytes int) ShardedCacheOption
- func WithDefaultTTL(ttl time.Duration) ShardedCacheOption
- func WithEvictionPolicy(policy EvictionPolicy) ShardedCacheOption
- func WithLoadBalancing(strategy LoadBalancingStrategy) ShardedCacheOption
- func WithMaxEntrySize(bytes int) ShardedCacheOption
- func WithMaxShardConcurrency(max int) ShardedCacheOption
- func WithMemoryLimit(bytes int64) ShardedCacheOption
- func WithMetrics(enabled bool) ShardedCacheOption
- func WithPerformanceProfile(profile PerformanceProfile) ShardedCacheOption
- func WithShardCount(count uint32) ShardedCacheOption
- func WithWarmup(entries map[string]*interfaces.ToolOutput) ShardedCacheOption
- func WithWorkloadType(workload WorkloadType) ShardedCacheOption
- type ShardedToolCache
- func (c *ShardedToolCache) AddDependency(dependentTool, dependsOnTool string)
- func (c *ShardedToolCache) Clear() error
- func (c *ShardedToolCache) Close()
- func (c *ShardedToolCache) Delete(ctx context.Context, key string) error
- func (c *ShardedToolCache) Get(ctx context.Context, key string) (*interfaces.ToolOutput, bool)
- func (c *ShardedToolCache) GetStats() CacheStats
- func (c *ShardedToolCache) GetStatsValues() (hits, misses, evicts, invalidations int64)
- func (c *ShardedToolCache) GetVersion() int64
- func (c *ShardedToolCache) InvalidateByPattern(ctx context.Context, pattern string) (int, error)
- func (c *ShardedToolCache) InvalidateByTool(ctx context.Context, toolName string) (int, error)
- func (c *ShardedToolCache) RemoveDependency(dependentTool, dependsOnTool string)
- func (c *ShardedToolCache) Set(ctx context.Context, key string, output *interfaces.ToolOutput, ...) error
- func (c *ShardedToolCache) Size() int
- type SimpleFunction
- type ToolCache
- type ToolCall
- type ToolError
- type ToolExecutor
- func (e *ToolExecutor) ExecuteBatch(ctx context.Context, tool interfaces.Tool, inputs []*interfaces.ToolInput) ([]*ToolResult, error)
- func (e *ToolExecutor) ExecuteParallel(ctx context.Context, calls []*ToolCall) ([]*ToolResult, error)
- func (e *ToolExecutor) ExecuteSequential(ctx context.Context, calls []*ToolCall) ([]*ToolResult, error)
- func (e *ToolExecutor) ExecuteWithDependencies(ctx context.Context, graph *ToolGraph) ([]*ToolResult, error)
- type ToolGraph
- func (g *ToolGraph) AddEdge(from, to string) error
- func (g *ToolGraph) AddNode(node *ToolNode) error
- func (g *ToolGraph) Clear()
- func (g *ToolGraph) Clone() *ToolGraph
- func (g *ToolGraph) GetDependencies(id string) []string
- func (g *ToolGraph) GetDependents(id string) []string
- func (g *ToolGraph) GetNode(id string) *ToolNode
- func (g *ToolGraph) GetNodes() []*ToolNode
- func (g *ToolGraph) HasNode(id string) bool
- func (g *ToolGraph) RemoveNode(id string) error
- func (g *ToolGraph) Size() int
- func (g *ToolGraph) TopologicalSort() ([]string, error)
- func (g *ToolGraph) Validate() error
- type ToolGraphBuilder
- type ToolNode
- type ToolResult
- type ToolRuntime
- func (r *ToolRuntime) Clone() *ToolRuntime
- func (r *ToolRuntime) GetFromStore(namespace []string, key string) (interface{}, error)
- func (r *ToolRuntime) GetMetadata(key string) (interface{}, bool)
- func (r *ToolRuntime) GetState(key string) (interface{}, error)
- func (r *ToolRuntime) PutToStore(namespace []string, key string, value interface{}) error
- func (r *ToolRuntime) SetState(key string, value interface{}) error
- func (r *ToolRuntime) Stream(data interface{}) error
- func (r *ToolRuntime) WithConfig(config *RuntimeConfig) *ToolRuntime
- func (r *ToolRuntime) WithMetadata(key string, value interface{}) *ToolRuntime
- func (r *ToolRuntime) WithStreamWriter(writer func(interface{}) error) *ToolRuntime
- type ToolRuntimeManager
- type TypedFunction
- type UpdateStateTool
- type UserInfoTool
- type WorkloadType
Constants ¶
const ( // Compute tools ToolCalculator = "calculator" // HTTP tools ToolAPI = "api" // Search tools ToolSearch = "search" // Shell tools ToolShell = "shell" // Practical tools ToolFileOperations = "file_operations" ToolDatabaseQuery = "database_query" ToolWebScraper = "web_scraper" ToolAPICaller = "api_caller" )
Tool names
const ( // Compute tools DescCalculator = "计算器工具,支持基本数学运算(加减乘除、幂运算、括号)" // HTTP tools DescAPI = "HTTP API 调用工具,支持 GET、POST、PUT、DELETE、PATCH 等方法" // Search tools DescSearch = "搜索工具,提供网络搜索功能" // Shell tools DescShell = "Shell 命令执行工具,安全地执行白名单中的系统命令" // Practical tools DescFileOperations = "文件操作工具,支持读取、写入、删除文件" DescDatabaseQuery = "数据库查询工具,支持执行 SQL 查询" DescWebScraper = "网页抓取工具,提取网页内容和结构化数据" DescAPICaller = "API 调用工具,简化 RESTful API 调用流程" )
Tool descriptions
const ( ErrToolNotFound = "tool not found" ErrInvalidInput = "invalid tool input" ErrExecutionFailed = "tool execution failed" ErrTimeout = "tool execution timeout" ErrToolAlreadyExists = "tool already exists" )
Tool error messages
const ( DefaultToolTimeout = 30 // seconds MaxConcurrentToolCalls = 10 MaxRetries = 3 )
Tool execution constants
Variables ¶
var ( ErrKeyNotFound = errors.New("key not found") ErrValueNotFound = errors.New("value not found") ErrStateAccessDenied = errors.New("state access is disabled") ErrStoreAccessDenied = errors.New("store access is disabled") )
Sentinel errors
Functions ¶
func BenchmarkCompareShardedVsNormal ¶
func BenchmarkCompareShardedVsNormal()
BenchmarkCompareShardedVsNormal 基准测试对比分片缓存和普通缓存
func ExampleDynamicConfiguration ¶
func ExampleDynamicConfiguration()
ExampleDynamicConfiguration 展示如何根据系统资源动态配置
func ExampleMonitoringAndTuning ¶
func ExampleMonitoringAndTuning()
ExampleMonitoringAndTuning 展示监控和调优
func ExampleShardedCacheUsage ¶
func ExampleShardedCacheUsage()
ExampleShardedCacheUsage 展示如何使用分片缓存的选项模式
func WithMiddleware ¶ added in v0.2.0
func WithMiddleware(tool interfaces.Tool, middlewares ...interface{}) interfaces.Tool
WithMiddleware 为工具应用中间件
支持两种中间件类型:
- ToolMiddleware: 基于接口的中间件(旧接口,已废弃)
- ToolMiddlewareFunc: 基于函数的中间件(推荐)
参数:
- tool: 要包装的工具
- middlewares: 中间件列表,按顺序应用
返回:
- interfaces.Tool: 包装后的工具
使用示例:
// 使用函数式中间件(推荐)
wrappedTool := WithMiddleware(tool,
middleware.Logging(),
middleware.Caching(),
)
// 使用接口式中间件(旧接口)
wrappedTool := WithMiddleware(tool,
middleware.NewLoggingMiddleware(),
middleware.NewCachingMiddleware(),
)
Types ¶
type BaseRuntimeTool ¶
type BaseRuntimeTool struct {
*BaseTool
// contains filtered or unexported fields
}
BaseRuntimeTool provides a base implementation for runtime tools
func (*BaseRuntimeTool) GetRuntime ¶
func (t *BaseRuntimeTool) GetRuntime() *ToolRuntime
GetRuntime returns the current runtime
func (*BaseRuntimeTool) SetRuntime ¶
func (t *BaseRuntimeTool) SetRuntime(runtime *ToolRuntime)
SetRuntime sets the runtime for the tool
type BaseTool ¶
type BaseTool struct {
// contains filtered or unexported fields
}
BaseTool 提供 Tool 的基础实现
实现了 interfaces.Tool 接口的通用功能 具体的执行逻辑通过 RunFunc 函数提供
func NewBaseTool ¶
func NewBaseTool( name string, description string, argsSchema string, runFunc func(context.Context, *interfaces.ToolInput) (*interfaces.ToolOutput, error), ) *BaseTool
NewBaseTool 创建基础工具
func (*BaseTool) Invoke ¶
func (t *BaseTool) Invoke(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)
Invoke 执行工具
type CacheStats ¶
type CacheStats struct {
Hits atomic.Int64
Misses atomic.Int64
Evicts atomic.Int64
Invalidations atomic.Int64 // 失效次数
}
CacheStats 缓存统计信息
type CachedTool ¶
type CachedTool struct {
// contains filtered or unexported fields
}
CachedTool 带缓存的工具包装器
func NewCachedTool ¶
func NewCachedTool(tool interfaces.Tool, cache ToolCache, ttl time.Duration) *CachedTool
NewCachedTool 创建带缓存的工具
func (*CachedTool) InvalidateCacheByPrefix ¶
func (c *CachedTool) InvalidateCacheByPrefix(ctx context.Context, prefix string) error
InvalidateCacheByPrefix 根据前缀失效缓存
func (*CachedTool) Invoke ¶
func (c *CachedTool) Invoke(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)
Invoke 执行工具(带缓存)
type CleanupIntervalRecommendation ¶
type CleanupIntervalRecommendation struct {
CacheSize int // Total cache capacity
TTL time.Duration // Default TTL
ExpectedChurn float64 // Expected percentage of entries changing per minute
RecommendedInterval time.Duration
Rationale string
}
CleanupIntervalRecommendation provides cleanup interval recommendations
func GetCleanupIntervalRecommendation ¶
func GetCleanupIntervalRecommendation(cacheSize int, ttl time.Duration, churnRate float64) CleanupIntervalRecommendation
GetCleanupIntervalRecommendation returns recommended cleanup interval
type CleanupStrategy ¶
type CleanupStrategy int
CleanupStrategy defines cleanup strategies
const ( // PeriodicCleanup performs cleanup at fixed intervals PeriodicCleanup CleanupStrategy = iota // LazyCleanup performs cleanup only on access LazyCleanup // AdaptiveCleanup adjusts cleanup frequency based on load AdaptiveCleanup // HybridCleanup combines periodic and lazy cleanup HybridCleanup )
type EvictionPolicy ¶
type EvictionPolicy int
EvictionPolicy defines cache eviction strategies
const ( // LRUEviction evicts least recently used items LRUEviction EvictionPolicy = iota // LFUEviction evicts least frequently used items LFUEviction // FIFOEviction evicts oldest items first FIFOEviction // RandomEviction evicts random items RandomEviction )
type ExecutorOption ¶
type ExecutorOption func(*ToolExecutor)
ExecutorOption 执行器选项
func WithErrorHandler ¶
func WithErrorHandler(handler ErrorHandler) ExecutorOption
WithErrorHandler 设置错误处理器
func WithMaxConcurrency ¶
func WithMaxConcurrency(max int) ExecutorOption
WithMaxConcurrency 设置最大并发数
func WithRetryPolicy ¶
func WithRetryPolicy(policy *RetryPolicy) ExecutorOption
WithRetryPolicy 设置重试策略
type FunctionTool ¶
type FunctionTool struct {
*BaseTool
// contains filtered or unexported fields
}
FunctionTool 函数包装工具
将普通 Go 函数包装成工具 支持自动参数转换和结果包装
func NewFunctionTool ¶
func NewFunctionTool( name string, description string, argsSchema string, fn func(context.Context, map[string]interface{}) (interface{}, error), ) *FunctionTool
NewFunctionTool 创建函数工具
Parameters:
- name: 工具名称
- description: 工具描述
- argsSchema: 参数 JSON Schema
- fn: 执行函数
func NewSimpleFunctionTool ¶
func NewSimpleFunctionTool( name string, description string, fn SimpleFunction, ) *FunctionTool
NewSimpleFunctionTool 创建简单函数工具
用于快速包装无参数或简单参数的函数
func NewTypedFunctionTool ¶
func NewTypedFunctionTool[I, O any]( name string, description string, argsSchema string, fn TypedFunction[I, O], ) *FunctionTool
NewTypedFunctionTool 创建类型安全的函数工具
使用泛型提供编译时类型检查
type FunctionToolBuilder ¶
type FunctionToolBuilder struct {
// contains filtered or unexported fields
}
FunctionToolBuilder 函数工具构建器
提供更灵活的函数工具创建方式
func NewFunctionToolBuilder ¶
func NewFunctionToolBuilder(name string) *FunctionToolBuilder
NewFunctionToolBuilder 创建函数工具构建器
func (*FunctionToolBuilder) Build ¶
func (b *FunctionToolBuilder) Build() (*FunctionTool, error)
Build 构建工具
Returns an error if the function is not set. This replaces the previous panic behavior with proper error handling.
func (*FunctionToolBuilder) MustBuild ¶
func (b *FunctionToolBuilder) MustBuild() *FunctionTool
MustBuild 构建工具,如果失败则 panic
用于初始化时确定配置正确的场景。 对于运行时构建,应使用 Build() 方法并处理错误。
func (*FunctionToolBuilder) WithArgsSchema ¶
func (b *FunctionToolBuilder) WithArgsSchema(schema string) *FunctionToolBuilder
WithArgsSchema 设置参数 Schema
func (*FunctionToolBuilder) WithArgsSchemaFromStruct ¶
func (b *FunctionToolBuilder) WithArgsSchemaFromStruct(v interface{}) *FunctionToolBuilder
WithArgsSchemaFromStruct 从结构体生成参数 Schema
func (*FunctionToolBuilder) WithDescription ¶
func (b *FunctionToolBuilder) WithDescription(description string) *FunctionToolBuilder
WithDescription 设置描述
func (*FunctionToolBuilder) WithFunction ¶
func (b *FunctionToolBuilder) WithFunction( fn func(context.Context, map[string]interface{}) (interface{}, error), ) *FunctionToolBuilder
WithFunction 设置执行函数
type LoadBalancingStrategy ¶
type LoadBalancingStrategy int
LoadBalancingStrategy defines how keys are distributed across shards
const ( // HashBasedBalancing uses hash function for distribution HashBasedBalancing LoadBalancingStrategy = iota // ConsistentHashBalancing uses consistent hashing ConsistentHashBalancing // RoundRobinBalancing distributes evenly in round-robin fashion RoundRobinBalancing // LeastLoadedBalancing routes to least loaded shard LeastLoadedBalancing )
type MemoryCacheConfig ¶
type MemoryCacheConfig struct {
// Capacity 最大容量(条目数)
Capacity int
// DefaultTTL 默认 TTL
DefaultTTL time.Duration
// CleanupInterval 清理间隔
CleanupInterval time.Duration
}
MemoryCacheConfig 内存缓存配置
type MemoryToolCache ¶
type MemoryToolCache struct {
// contains filtered or unexported fields
}
MemoryToolCache 内存工具缓存
线程安全的 LRU 缓存实现,使用 context 进行生命周期管理
func NewMemoryToolCache ¶
func NewMemoryToolCache(config MemoryCacheConfig) *MemoryToolCache
NewMemoryToolCache 创建内存工具缓存
使用 context 进行生命周期管理,确保清理 goroutine 可以优雅关闭
func (*MemoryToolCache) AddDependency ¶
func (c *MemoryToolCache) AddDependency(dependentTool, dependsOnTool string)
AddDependency 添加工具依赖关系
声明 dependentTool 依赖 dependsOnTool。 当 dependsOnTool 的缓存失效时,dependentTool 的缓存也会自动失效。
func (*MemoryToolCache) Close ¶
func (c *MemoryToolCache) Close()
Close 关闭缓存,清理资源
使用 context cancellation 优雅关闭清理 goroutine。 使用 atomic 操作确保 Close 的幂等性(多次调用是安全的)。
func (*MemoryToolCache) Delete ¶
func (c *MemoryToolCache) Delete(ctx context.Context, key string) error
Delete 删除缓存
func (*MemoryToolCache) Get ¶
func (c *MemoryToolCache) Get(ctx context.Context, key string) (*interfaces.ToolOutput, bool)
Get 获取缓存结果
func (*MemoryToolCache) GetVersion ¶
func (c *MemoryToolCache) GetVersion() int64
GetVersion 获取当前缓存版本号
func (*MemoryToolCache) InvalidateByPattern ¶
InvalidateByPattern 根据正则表达式模式失效缓存
支持正则表达式模式匹配缓存键。返回失效的条目数量。 只删除匹配的条目,不影响其他缓存项。
func (*MemoryToolCache) InvalidateByTool ¶
InvalidateByTool 根据工具名称失效缓存
失效指定工具的所有缓存条目,并级联失效依赖该工具的其他工具。 只删除相关的条目,不影响其他缓存项。 返回失效的条目数量。
func (*MemoryToolCache) RemoveDependency ¶
func (c *MemoryToolCache) RemoveDependency(dependentTool, dependsOnTool string)
RemoveDependency 移除工具依赖关系
func (*MemoryToolCache) Set ¶
func (c *MemoryToolCache) Set(ctx context.Context, key string, output *interfaces.ToolOutput, ttl time.Duration) error
Set 设置缓存结果
type MiddlewareTool ¶ added in v0.2.0
type MiddlewareTool struct {
// contains filtered or unexported fields
}
MiddlewareTool wraps a tool with middleware support.
It implements the Tool interface and applies middleware to tool invocations. This allows adding cross-cutting concerns like logging, caching, and rate limiting.
使用示例:
tool := NewCalculatorTool()
wrappedTool := tools.WithMiddleware(tool,
middleware.Logging(),
middleware.Caching(middleware.WithTTL(5*time.Minute)),
middleware.RateLimit(middleware.WithQPS(10)),
)
func (*MiddlewareTool) ArgsSchema ¶ added in v0.2.0
func (w *MiddlewareTool) ArgsSchema() string
ArgsSchema 返回参数模式
func (*MiddlewareTool) Description ¶ added in v0.2.0
func (w *MiddlewareTool) Description() string
Description 返回工具描述
func (*MiddlewareTool) Invoke ¶ added in v0.2.0
func (w *MiddlewareTool) Invoke(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)
Invoke 执行工具调用(通过中间件)
func (*MiddlewareTool) Unwrap ¶ added in v0.2.0
func (w *MiddlewareTool) Unwrap() interfaces.Tool
Unwrap 返回被包装的原始工具
这允许访问原始工具的方法或检查工具类型
type PerformanceProfile ¶
type PerformanceProfile int
PerformanceProfile defines preset configurations for different workloads
const ( // LowLatencyProfile optimizes for minimal response time LowLatencyProfile PerformanceProfile = iota // HighThroughputProfile optimizes for maximum throughput HighThroughputProfile // BalancedProfile provides balanced performance BalancedProfile // MemoryEfficientProfile minimizes memory usage MemoryEfficientProfile )
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages tool registration and lookup
func (*Registry) Get ¶
func (r *Registry) Get(name string) interfaces.Tool
Get retrieves a tool by name
func (*Registry) List ¶
func (r *Registry) List() []interfaces.Tool
List returns all registered tools
type RetryPolicy ¶
type RetryPolicy struct {
// MaxRetries 最大重试次数
MaxRetries int
// InitialDelay 初始延迟
InitialDelay time.Duration
// MaxDelay 最大延迟
MaxDelay time.Duration
// Multiplier 延迟倍数
Multiplier float64
// RetryableErrors 可重试的错误类型
RetryableErrors []string
}
RetryPolicy 重试策略
type RuntimeConfig ¶
type RuntimeConfig struct {
// EnableStateAccess allows tools to read/write agent state
EnableStateAccess bool
// EnableStoreAccess allows tools to access long-term store
EnableStoreAccess bool
// EnableStreaming allows tools to stream data
EnableStreaming bool
// MaxExecutionTime limits tool execution time
MaxExecutionTime int // seconds
// AllowedNamespaces restricts store access to specific namespaces
AllowedNamespaces []string
}
RuntimeConfig configures the tool runtime
func DefaultRuntimeConfig ¶
func DefaultRuntimeConfig() *RuntimeConfig
DefaultRuntimeConfig returns default configuration
type RuntimeTool ¶
type RuntimeTool interface {
interfaces.Tool
// ExecuteWithRuntime executes the tool with runtime context
ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *ToolRuntime) (*interfaces.ToolOutput, error)
}
RuntimeTool interface for tools that use runtime
type RuntimeToolAdapter ¶
type RuntimeToolAdapter struct {
*BaseTool
// contains filtered or unexported fields
}
RuntimeToolAdapter adapts a RuntimeTool to the standard Tool interface
func NewRuntimeToolAdapter ¶
func NewRuntimeToolAdapter(tool RuntimeTool, runtime *ToolRuntime) *RuntimeToolAdapter
NewRuntimeToolAdapter creates a new adapter
func (*RuntimeToolAdapter) Invoke ¶
func (a *RuntimeToolAdapter) Invoke(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)
Invoke implements the Tool interface through BaseTool
type SavePreferenceTool ¶
type SavePreferenceTool struct {
*BaseRuntimeTool
}
SavePreferenceTool saves user preferences using runtime
func NewSavePreferenceTool ¶
func NewSavePreferenceTool() *SavePreferenceTool
NewSavePreferenceTool creates a new save preference tool
func (*SavePreferenceTool) ExecuteWithRuntime ¶
func (t *SavePreferenceTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *ToolRuntime) (*interfaces.ToolOutput, error)
ExecuteWithRuntime saves a preference using runtime
type ShardCountRecommendation ¶
type ShardCountRecommendation struct {
ExpectedQPS int // Expected queries per second
RecommendedCount uint32 // Recommended shard count
Rationale string // Explanation for the recommendation
}
ShardCountRecommendation provides shard count recommendations based on expected load
func GetShardCountRecommendation ¶
func GetShardCountRecommendation(expectedQPS int) ShardCountRecommendation
GetShardCountRecommendation returns recommended shard count based on expected QPS
type ShardedCacheConfig ¶
type ShardedCacheConfig struct {
// ShardCount 分片数量(建议为 2 的幂,默认 32)
ShardCount uint32
// Capacity 总容量(每个分片的容量 = Capacity / ShardCount)
Capacity int
// DefaultTTL 默认 TTL
DefaultTTL time.Duration
// CleanupInterval 清理间隔
CleanupInterval time.Duration
// EvictionPolicy 淘汰策略
EvictionPolicy EvictionPolicy
// CleanupStrategy 清理策略
CleanupStrategy CleanupStrategy
// LoadBalancing 负载均衡策略
LoadBalancing LoadBalancingStrategy
// AutoTuning 自动调优
AutoTuning bool
// MetricsEnabled 是否启用指标收集
MetricsEnabled bool
// MaxConcurrency 每个分片的最大并发数
MaxConcurrency int
// WarmupEntries 预热条目
WarmupEntries map[string]*interfaces.ToolOutput
// CompressionThreshold 压缩阈值(字节)
CompressionThreshold int
// MaxEntrySize 单个条目最大大小(字节)
MaxEntrySize int
// MemoryLimit 内存限制(字节)
MemoryLimit int64
// WorkloadType 工作负载类型
WorkloadType WorkloadType
}
ShardedCacheConfig 分片缓存配置
func DefaultShardedCacheConfig ¶
func DefaultShardedCacheConfig() ShardedCacheConfig
DefaultShardedCacheConfig returns the default configuration
type ShardedCacheOption ¶
type ShardedCacheOption func(*ShardedCacheConfig)
ShardedCacheOption is a functional option for configuring ShardedToolCache
func WithAutoTuning ¶
func WithAutoTuning(enabled bool) ShardedCacheOption
WithAutoTuning enables automatic performance tuning The cache will automatically adjust parameters based on workload patterns
func WithCapacity ¶
func WithCapacity(capacity int) ShardedCacheOption
WithCapacity sets the total cache capacity The capacity is distributed evenly across shards
func WithCleanupInterval ¶
func WithCleanupInterval(interval time.Duration) ShardedCacheOption
WithCleanupInterval sets the interval for periodic cleanup
func WithCleanupStrategy ¶
func WithCleanupStrategy(strategy CleanupStrategy) ShardedCacheOption
WithCleanupStrategy sets the cleanup strategy
func WithCompressionThreshold ¶
func WithCompressionThreshold(bytes int) ShardedCacheOption
WithCompressionThreshold enables compression for entries larger than threshold
func WithDefaultTTL ¶
func WithDefaultTTL(ttl time.Duration) ShardedCacheOption
WithDefaultTTL sets the default TTL for cache entries
func WithEvictionPolicy ¶
func WithEvictionPolicy(policy EvictionPolicy) ShardedCacheOption
WithEvictionPolicy sets the eviction policy for the cache
func WithLoadBalancing ¶
func WithLoadBalancing(strategy LoadBalancingStrategy) ShardedCacheOption
WithLoadBalancing sets the load balancing strategy for shard selection
func WithMaxEntrySize ¶
func WithMaxEntrySize(bytes int) ShardedCacheOption
WithMaxEntrySize sets the maximum size for a single cache entry
func WithMaxShardConcurrency ¶
func WithMaxShardConcurrency(max int) ShardedCacheOption
WithMaxShardConcurrency sets the maximum concurrent operations per shard
func WithMemoryLimit ¶
func WithMemoryLimit(bytes int64) ShardedCacheOption
WithMemoryLimit sets the maximum memory usage for the cache
func WithMetrics ¶
func WithMetrics(enabled bool) ShardedCacheOption
WithMetrics enables or disables metrics collection
func WithPerformanceProfile ¶
func WithPerformanceProfile(profile PerformanceProfile) ShardedCacheOption
WithPerformanceProfile applies a preset performance profile
func WithShardCount ¶
func WithShardCount(count uint32) ShardedCacheOption
WithShardCount sets the number of shards Recommended values: - Light load (< 100 req/s): 8-16 shards - Medium load (100-1000 req/s): 32-64 shards - Heavy load (> 1000 req/s): 128-256 shards - Auto: 0 (will use CPU cores * 4)
func WithWarmup ¶
func WithWarmup(entries map[string]*interfaces.ToolOutput) ShardedCacheOption
WithWarmup enables cache warmup with specified entries
func WithWorkloadType ¶
func WithWorkloadType(workload WorkloadType) ShardedCacheOption
WithWorkloadType optimizes the cache for specific workload patterns
type ShardedToolCache ¶
type ShardedToolCache struct {
// contains filtered or unexported fields
}
ShardedToolCache 分片工具缓存 通过将缓存分成多个分片来减少锁竞争,提升并发性能
func NewShardedToolCache ¶
func NewShardedToolCache(config ShardedCacheConfig) *ShardedToolCache
NewShardedToolCache 创建分片工具缓存(使用配置结构体)
func NewShardedToolCacheWithOptions ¶
func NewShardedToolCacheWithOptions(opts ...ShardedCacheOption) *ShardedToolCache
NewShardedToolCacheWithOptions 使用选项模式创建分片工具缓存
func (*ShardedToolCache) AddDependency ¶
func (c *ShardedToolCache) AddDependency(dependentTool, dependsOnTool string)
AddDependency 添加工具依赖关系
func (*ShardedToolCache) Delete ¶
func (c *ShardedToolCache) Delete(ctx context.Context, key string) error
Delete 删除缓存
func (*ShardedToolCache) Get ¶
func (c *ShardedToolCache) Get(ctx context.Context, key string) (*interfaces.ToolOutput, bool)
Get 获取缓存结果
func (*ShardedToolCache) GetStats ¶
func (c *ShardedToolCache) GetStats() CacheStats
GetStats 获取统计信息
func (*ShardedToolCache) GetStatsValues ¶
func (c *ShardedToolCache) GetStatsValues() (hits, misses, evicts, invalidations int64)
GetStatsValues 获取统计信息的数值
func (*ShardedToolCache) GetVersion ¶
func (c *ShardedToolCache) GetVersion() int64
GetVersion 获取当前缓存版本号(分片缓存不使用版本号)
func (*ShardedToolCache) InvalidateByPattern ¶
InvalidateByPattern 根据正则表达式模式失效缓存
func (*ShardedToolCache) InvalidateByTool ¶
InvalidateByTool 根据工具名称失效缓存
func (*ShardedToolCache) RemoveDependency ¶
func (c *ShardedToolCache) RemoveDependency(dependentTool, dependsOnTool string)
RemoveDependency 移除工具依赖关系
func (*ShardedToolCache) Set ¶
func (c *ShardedToolCache) Set(ctx context.Context, key string, output *interfaces.ToolOutput, ttl time.Duration) error
Set 设置缓存结果
type SimpleFunction ¶
SimpleFunction 简单函数类型 用于快速创建不需要复杂参数的工具
type ToolCache ¶
type ToolCache interface {
// Get 获取缓存结果
Get(ctx context.Context, key string) (*interfaces.ToolOutput, bool)
// Set 设置缓存结果
Set(ctx context.Context, key string, output *interfaces.ToolOutput, ttl time.Duration) error
// Delete 删除缓存
Delete(ctx context.Context, key string) error
// Clear 清空所有缓存
Clear() error
// Size 返回缓存大小
Size() int
// InvalidateByPattern 根据正则表达式模式失效缓存
InvalidateByPattern(ctx context.Context, pattern string) (int, error)
// InvalidateByTool 根据工具名称失效缓存
InvalidateByTool(ctx context.Context, toolName string) (int, error)
}
ToolCache 工具缓存接口
type ToolCall ¶
type ToolCall struct {
// Tool 要调用的工具
Tool interfaces.Tool
// Input 输入参数
Input *interfaces.ToolInput
// ID 调用标识符(用于追踪)
ID string
// Dependencies 依赖的其他工具调用 ID
Dependencies []string
}
ToolCall 工具调用
type ToolExecutor ¶
type ToolExecutor struct {
// contains filtered or unexported fields
}
ToolExecutor 工具执行器
支持并发和顺序执行工具,提供超时、重试等功能
func NewToolExecutor ¶
func NewToolExecutor(opts ...ExecutorOption) *ToolExecutor
NewToolExecutor 创建工具执行器
func (*ToolExecutor) ExecuteBatch ¶
func (e *ToolExecutor) ExecuteBatch(ctx context.Context, tool interfaces.Tool, inputs []*interfaces.ToolInput) ([]*ToolResult, error)
ExecuteBatch 批量执行相同工具的不同输入
func (*ToolExecutor) ExecuteParallel ¶
func (e *ToolExecutor) ExecuteParallel(ctx context.Context, calls []*ToolCall) ([]*ToolResult, error)
ExecuteParallel 并行执行多个工具
func (*ToolExecutor) ExecuteSequential ¶
func (e *ToolExecutor) ExecuteSequential(ctx context.Context, calls []*ToolCall) ([]*ToolResult, error)
ExecuteSequential 顺序执行工具
func (*ToolExecutor) ExecuteWithDependencies ¶
func (e *ToolExecutor) ExecuteWithDependencies(ctx context.Context, graph *ToolGraph) ([]*ToolResult, error)
ExecuteWithDependencies 根据依赖关系执行
type ToolGraph ¶
type ToolGraph struct {
// contains filtered or unexported fields
}
ToolGraph 工具依赖图
管理工具之间的依赖关系,支持 DAG (有向无环图) 执行
func (*ToolGraph) GetDependencies ¶
GetDependencies 获取节点的所有依赖
func (*ToolGraph) GetDependents ¶
GetDependents 获取依赖某个节点的所有节点
type ToolGraphBuilder ¶
type ToolGraphBuilder struct {
// contains filtered or unexported fields
}
ToolGraphBuilder 工具图构建器
func NewToolGraphBuilder ¶
func NewToolGraphBuilder() *ToolGraphBuilder
NewToolGraphBuilder 创建工具图构建器
func (*ToolGraphBuilder) AddDependency ¶
func (b *ToolGraphBuilder) AddDependency(from, to string) *ToolGraphBuilder
AddDependency 添加依赖
func (*ToolGraphBuilder) AddTool ¶
func (b *ToolGraphBuilder) AddTool(id string, tool interfaces.Tool, input *interfaces.ToolInput) *ToolGraphBuilder
AddTool 添加工具
type ToolNode ¶
type ToolNode struct {
// ID 节点标识符
ID string
// Tool 工具实例
Tool interfaces.Tool
// Input 输入参数
Input *interfaces.ToolInput
// Dependencies 依赖的节点 ID 列表
Dependencies []string
// Metadata 元数据
Metadata map[string]interface{}
}
ToolNode 工具节点
type ToolResult ¶
type ToolResult struct {
// CallID 调用标识符
CallID string
// Output 输出结果
Output *interfaces.ToolOutput
// Duration 执行耗时
Duration time.Duration
// Error 错误信息
Error error
}
ToolResult 工具执行结果
type ToolRuntime ¶
type ToolRuntime struct {
// Core components
State core.State // Agent's current state
Context context.Context // Request context
Store store.Store // Long-term memory store
Config *RuntimeConfig // Runtime configuration
// Execution context
ToolCallID string // Current tool call ID
AgentID string // ID of the agent executing the tool
SessionID string // Session ID for tracking
// Streaming support
StreamWriter func(interface{}) error // Stream custom data
// Additional context
Metadata map[string]interface{} // Additional metadata
// contains filtered or unexported fields
}
ToolRuntime provides access to agent state and context from within tools
func NewToolRuntime ¶
NewToolRuntime creates a new tool runtime
func (*ToolRuntime) Clone ¶
func (r *ToolRuntime) Clone() *ToolRuntime
Clone creates a copy of the runtime
func (*ToolRuntime) GetFromStore ¶
func (r *ToolRuntime) GetFromStore(namespace []string, key string) (interface{}, error)
GetFromStore retrieves data from long-term store
func (*ToolRuntime) GetMetadata ¶
func (r *ToolRuntime) GetMetadata(key string) (interface{}, bool)
GetMetadata retrieves metadata value
func (*ToolRuntime) GetState ¶
func (r *ToolRuntime) GetState(key string) (interface{}, error)
GetState retrieves a value from agent state
func (*ToolRuntime) PutToStore ¶
func (r *ToolRuntime) PutToStore(namespace []string, key string, value interface{}) error
PutToStore saves data to long-term store
func (*ToolRuntime) SetState ¶
func (r *ToolRuntime) SetState(key string, value interface{}) error
SetState updates a value in agent state
func (*ToolRuntime) Stream ¶
func (r *ToolRuntime) Stream(data interface{}) error
Stream sends data to the stream writer
func (*ToolRuntime) WithConfig ¶
func (r *ToolRuntime) WithConfig(config *RuntimeConfig) *ToolRuntime
WithConfig sets the runtime configuration
func (*ToolRuntime) WithMetadata ¶
func (r *ToolRuntime) WithMetadata(key string, value interface{}) *ToolRuntime
WithMetadata adds metadata to the runtime
func (*ToolRuntime) WithStreamWriter ¶
func (r *ToolRuntime) WithStreamWriter(writer func(interface{}) error) *ToolRuntime
WithStreamWriter sets the stream writer
type ToolRuntimeManager ¶
type ToolRuntimeManager struct {
// contains filtered or unexported fields
}
ToolRuntimeManager manages runtime instances for tools
func NewToolRuntimeManager ¶
func NewToolRuntimeManager() *ToolRuntimeManager
NewToolRuntimeManager creates a new manager
func (*ToolRuntimeManager) CreateRuntimeWithContext ¶
func (m *ToolRuntimeManager) CreateRuntimeWithContext(ctx context.Context, callID string, state core.State, store store.Store) *ToolRuntime
CreateRuntimeWithContext creates a new runtime for a tool call with a parent context
func (*ToolRuntimeManager) GetRuntime ¶
func (m *ToolRuntimeManager) GetRuntime(callID string) (*ToolRuntime, bool)
GetRuntime retrieves a runtime by call ID
func (*ToolRuntimeManager) RemoveRuntime ¶
func (m *ToolRuntimeManager) RemoveRuntime(callID string)
RemoveRuntime removes a runtime
type TypedFunction ¶
TypedFunction 类型安全的函数类型 使用泛型提供类型安全
type UpdateStateTool ¶
type UpdateStateTool struct {
*BaseRuntimeTool
}
UpdateStateTool modifies agent state directly
func NewUpdateStateTool ¶
func NewUpdateStateTool() *UpdateStateTool
NewUpdateStateTool creates a new update state tool
func (*UpdateStateTool) ExecuteWithRuntime ¶
func (t *UpdateStateTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *ToolRuntime) (*interfaces.ToolOutput, error)
ExecuteWithRuntime updates state using runtime
type UserInfoTool ¶
type UserInfoTool struct {
*BaseRuntimeTool
}
UserInfoTool retrieves user information using runtime
func NewUserInfoTool ¶
func NewUserInfoTool() *UserInfoTool
NewUserInfoTool creates a new user info tool
func (*UserInfoTool) ExecuteWithRuntime ¶
func (t *UserInfoTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *ToolRuntime) (*interfaces.ToolOutput, error)
ExecuteWithRuntime retrieves user info using runtime
type WorkloadType ¶
type WorkloadType int
WorkloadType defines the type of workload the cache will handle
const ( // ReadHeavyWorkload for read-dominated workloads (90%+ reads) ReadHeavyWorkload WorkloadType = iota // WriteHeavyWorkload for write-dominated workloads WriteHeavyWorkload // MixedWorkload for balanced read/write workloads MixedWorkload // BurstyWorkload for workloads with traffic bursts BurstyWorkload )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package plugingen provides code generation tools for type-safe plugin boundaries.
|
Package plugingen provides code generation tools for type-safe plugin boundaries. |
|
cmd/plugingen
command
plugingen is a command-line tool for generating type-safe Go code from plugin schemas.
|
plugingen is a command-line tool for generating type-safe Go code from plugin schemas. |