practical

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2025 License: Apache-2.0 Imports: 32 Imported by: 0

README

Database Query Tool

⚠️ 安全警告

必须使用参数化查询!永远不要直接拼接用户输入到 SQL 查询中。

SQL 注入是最危险的安全漏洞之一,可能导致:

  • 数据泄露(窃取敏感信息)
  • 数据篡改(修改或删除数据)
  • 权限提升(获取管理员权限)
  • 服务器控制(执行系统命令)
✅ 正确用法 - 参数化查询
// 安全:使用参数化查询
output, err := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver": "mysql",
            "dsn":    "user:password@tcp(localhost:3306)/dbname",
        },
        "query":  "SELECT * FROM users WHERE id = ? AND status = ?",
        "params": []interface{}{userID, "active"},  // 参数独立传递
    },
})
❌ 错误用法 - 字符串拼接(危险!)
// 危险!容易受到 SQL 注入攻击
userInput := "1 OR 1=1" // 恶意输入
query := fmt.Sprintf("SELECT * FROM users WHERE id = %s", userInput)

// 这会执行:SELECT * FROM users WHERE id = 1 OR 1=1
// 结果:返回所有用户数据!

概述

Database Query Tool 是一个安全的数据库查询工具,支持 MySQL、PostgreSQL 和 SQLite 数据库。通过内置的安全检查机制和参数化查询支持,帮助防止 SQL 注入攻击。

核心特性:

  • 🔒 SQL 注入防护 - 基础的查询安全检查
  • 🗄️ 多数据库支持 - MySQL, PostgreSQL, SQLite
  • 🔄 连接池管理 - 自动管理数据库连接
  • 💼 事务支持 - 支持多语句事务执行
  • ⏱️ 超时控制 - 防止长时间查询
  • 📊 结果限制 - 可配置的最大返回行数

支持的数据库

数据库 Driver DSN 示例
MySQL mysql user:password@tcp(localhost:3306)/dbname
PostgreSQL postgres postgres://user:password@localhost/dbname?sslmode=disable
SQLite sqlite /path/to/database.db:memory:

安全特性

1. 基础 SQL 注入防护

工具包含 sanitizeQuery 函数,提供基础的安全检查:

  • ✅ 阻止多语句执行(检测 ;
  • ✅ 阻止 SQL 注释(检测 --/*

重要提示: 这些检查只是基础防护,不能完全防止所有 SQL 注入攻击。必须配合参数化查询使用。

2. 操作模式隔离

不同的操作使用不同的模式,防止误用:

  • query - 只允许 SELECT/SHOW/DESCRIBE 语句
  • execute - 只允许 INSERT/UPDATE/DELETE 语句
  • transaction - 事务模式,支持多语句
3. ���数化查询支持

通过 params 字段传递参数,参数会被安全地转义:

// 数据库驱动会自动转义参数
"query":  "SELECT * FROM users WHERE name = ? AND age > ?",
"params": []interface{}{"Alice", 18},

使用示例

基本查询
package main

import (
    "context"
    "fmt"

    "github.com/kart-io/goagent/interfaces"
    "github.com/kart-io/goagent/tools/practical"
)

func main() {
    tool := practical.NewDatabaseQueryTool()
    ctx := context.Background()

    // 执行 SELECT 查询
    output, err := tool.Execute(ctx, &interfaces.ToolInput{
        Args: map[string]interface{}{
            "connection": map[string]interface{}{
                "driver": "mysql",
                "dsn":    "user:password@tcp(localhost:3306)/mydb",
            },
            "query":     "SELECT id, name, email FROM users WHERE status = ?",
            "params":    []interface{}{"active"},
            "operation": "query",
            "max_rows":  100,
        },
    })

    if err != nil {
        fmt.Printf("Error: %v\n", err)
        return
    }

    result := output.Result.(map[string]interface{})
    columns := result["columns"].([]string)
    rows := result["rows"].([][]interface{})

    fmt.Printf("Columns: %v\n", columns)
    fmt.Printf("Row count: %d\n", len(rows))

    for i, row := range rows {
        fmt.Printf("Row %d: %v\n", i+1, row)
    }
}
插入数据
output, err := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver": "postgres",
            "dsn":    "postgres://user:pass@localhost/db?sslmode=disable",
        },
        "query":     "INSERT INTO users (name, email) VALUES ($1, $2)",
        "params":    []interface{}{"Alice", "[email protected]"},
        "operation": "execute",
    },
})

if err == nil {
    result := output.Result.(map[string]interface{})
    fmt.Printf("Rows affected: %d\n", result["rows_affected"])
    fmt.Printf("Last insert ID: %d\n", result["last_insert_id"])
}
更新数据
output, err := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver": "sqlite",
            "dsn":    "/tmp/test.db",
        },
        "query":     "UPDATE users SET status = ? WHERE id = ?",
        "params":    []interface{}{"inactive", 123},
        "operation": "execute",
    },
})
事务执行
output, err := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver":        "mysql",
            "dsn":           "user:pass@tcp(localhost:3306)/db",
            "connection_id": "my_connection", // 复用连接
        },
        "operation": "transaction",
        "transaction": []interface{}{
            map[string]interface{}{
                "query":  "INSERT INTO accounts (user_id, balance) VALUES (?, ?)",
                "params": []interface{}{1, 1000},
            },
            map[string]interface{}{
                "query":  "INSERT INTO transactions (from_account, amount) VALUES (?, ?)",
                "params": []interface{}{1, 1000},
            },
        },
    },
})

if err != nil {
    fmt.Println("Transaction rolled back:", err)
} else {
    result := output.Result.(map[string]interface{})
    fmt.Printf("Transaction committed: %d queries executed\n",
        result["queries_executed"])
}
连接复用
// 第一次请求,创建并缓存连接
output1, _ := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver":        "mysql",
            "dsn":           "user:pass@tcp(localhost:3306)/db",
            "connection_id": "shared_conn", // 连接标识符
        },
        "query": "SELECT * FROM users LIMIT 10",
    },
})

// ��二次请求,复用已有连接
output2, _ := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver":        "mysql",
            "connection_id": "shared_conn", // 复用相同标识符
            // dsn 可以省略,使用已缓存的连接
        },
        "query": "SELECT * FROM products LIMIT 10",
    },
})

// 使用完毕后关闭所有连接
tool.Close()
超时控制
output, err := tool.Execute(ctx, &interfaces.ToolInput{
    Args: map[string]interface{}{
        "connection": map[string]interface{}{
            "driver": "postgres",
            "dsn":    "postgres://user:pass@localhost/db",
        },
        "query":   "SELECT * FROM large_table",
        "timeout": 5, // 5 秒超时
    },
})

API 参考

NewDatabaseQueryTool
func NewDatabaseQueryTool() *DatabaseQueryTool

创建一个新的数据库查询工具实例。

默认配置:

  • maxRows: 1000
  • timeout: 30 秒
Execute
func (t *DatabaseQueryTool) Execute(ctx context.Context, input *interfaces.ToolInput) (*interfaces.ToolOutput, error)

执行数据库操作。

输入参数(input.Args)
connection (object, 必需)

数据库连接配置:

{
    "driver": "mysql",           // 必需:数据库驱动
    "dsn": "connection_string",  // DSN 连接字符串
    "connection_id": "my_conn"   // 可选:连接标识符,用于复用
}
其他参数
参数 类型 必需 默认值 说明
query string 条件必需 - SQL 查询语句(query/execute 模式)
params []interface{} 可选 [] 查询参数(参数化查询)
operation string 可选 "query" 操作类型:query/execute/transaction
transaction []object 条件必需 - 事务语句列表(transaction 模式)
max_rows int 可选 1000 最大返回行数
timeout int 可选 30 超时时间(秒)
输出格式
query 操作
{
    "columns": ["id", "name", "email"],
    "rows": [
        [1, "Alice", "[email protected]"],
        [2, "Bob", "[email protected]"]
    ],
    "execution_time_ms": 45
}
execute 操作
{
    "rows_affected": 1,
    "last_insert_id": 123,
    "execution_time_ms": 23
}
transaction 操作
{
    "transaction_results": [
        {"step": 0, "rows_affected": 1, "last_insert_id": 100},
        {"step": 1, "rows_affected": 1, "last_insert_id": 200}
    ],
    "total_rows_affected": 2,
    "queries_executed": 2,
    "execution_time_ms": 67
}
Close
func (t *DatabaseQueryTool) Close() error

关闭所有缓存的数据库连接。

连接池配置

工具自动配置连接池参数:

db.SetMaxOpenConns(10)               // 最大连接数
db.SetMaxIdleConns(5)                // 最大空闲连接
db.SetConnMaxLifetime(5 * time.Minute) // 连接最大生命周期

安全最佳实践

1. 始终使用参数化查询
// ✅ 安全
"query":  "SELECT * FROM users WHERE email = ?",
"params": []interface{}{userEmail}

// ❌ 危险
query := fmt.Sprintf("SELECT * FROM users WHERE email = '%s'", userEmail)
2. 验证表名和列名

参数化查询不能用于表名和列名:

// ❌ 不生效:参数化不适用于表名
"query":  "SELECT * FROM ? WHERE id = ?",
"params": []interface{}{tableName, id}  // tableName 不会被转义

// ✅ 推荐:使用白名单验证表名
allowedTables := map[string]bool{
    "users": true,
    "products": true,
}

if !allowedTables[tableName] {
    return errors.New("invalid table name")
}

query := fmt.Sprintf("SELECT * FROM %s WHERE id = ?", tableName)
3. 限制查询结果
// 防止返回过多数据
"max_rows": 100,  // 限制最多返回 100 行
4. 使用超时
// 防止长时间运行的查询
"timeout": 30,  // 30 秒超时
5. 最小权限原则
// 为应用创建专用数据库用户,只授予必要权限
// ✅ 推荐:只读用户
CREATE USER 'app_readonly'@'localhost' IDENTIFIED BY 'password';
GRANT SELECT ON mydb.* TO 'app_readonly'@'localhost';

// ⚠️ 避免:使用 root 或高权限账号
6. 敏感数据脱敏
// 查询后脱敏敏感数据
result := output.Result.(map[string]interface{})
rows := result["rows"].([][]interface{})

for _, row := range rows {
    // 假设第 3 列是手机号
    if phone, ok := row[2].(string); ok && len(phone) > 4 {
        row[2] = phone[:3] + "****" + phone[len(phone)-4:]
    }
}
7. 记录审计日志
// 记录所有数据库操作
logger.Info("database query",
    "operation", operation,
    "query", sanitizedQuery,  // 不要记录参数值!
    "user", userID,
    "timestamp", time.Now(),
)

常见 SQL 注入攻击示例

Union-based 注入
// 恶意输入
userInput := "1 UNION SELECT username, password FROM admin"

// ❌ 危险:字符串拼接
query := fmt.Sprintf("SELECT * FROM products WHERE id = %s", userInput)
// 执行:SELECT * FROM products WHERE id = 1 UNION SELECT username, password FROM admin
// 结果:泄露管理员账号密码

// ✅ 安全:参数化查询
"query":  "SELECT * FROM products WHERE id = ?",
"params": []interface{}{userInput}
// 参��会被转义为字符串 "1 UNION SELECT username, password FROM admin"
// 查询会失败或返回空结果
Boolean-based 注入
// 恶意输入
username := "admin' OR '1'='1"

// ❌ 危险
query := fmt.Sprintf("SELECT * FROM users WHERE username = '%s'", username)
// 执行:SELECT * FROM users WHERE username = 'admin' OR '1'='1'
// 结果:返回所有用户

// ✅ 安全
"query":  "SELECT * FROM users WHERE username = ?",
"params": []interface{}{username}
// username 会被转义,条件永远为 false
Comment-based 注入
// 恶意输入
password := "' OR 1=1--"

// ❌ 危险
query := fmt.Sprintf("SELECT * FROM users WHERE password = '%s' AND status = 'active'", password)
// 执行:SELECT * FROM users WHERE password = '' OR 1=1--' AND status = 'active'
// 注释掉了后面的条件

// ✅ 安全:sanitizeQuery 会检测 -- 并拒绝
Stacked Queries 注入
// 恶意输入
id := "1; DROP TABLE users"

// ❌ 危险
query := fmt.Sprintf("SELECT * FROM products WHERE id = %s", id)
// 执行:SELECT * FROM products WHERE id = 1; DROP TABLE users
// 结果:删除 users 表!

// ✅ 安全:sanitizeQuery 会检测多语句并拒绝

常见问题

Q1: 为什么我的查询被拒绝了?

A: 检查以下几点:

  1. 操作模式是否正确(query 用于 SELECT,execute 用于 INSERT/UPDATE/DELETE)
  2. 查询是否包含被禁止的字符(;, --, /*
  3. 是否提供了必需的参数
Q2: 如何处理动态表名或列名?

A: 参数化查询不适用于表名和列名,使用白名单:

func buildQuery(table string, column string, value interface{}) (string, []interface{}, error) {
    // 白名单验证
    allowedTables := map[string]bool{"users": true, "products": true}
    allowedColumns := map[string]bool{"id": true, "name": true, "status": true}

    if !allowedTables[table] {
        return "", nil, errors.New("invalid table")
    }
    if !allowedColumns[column] {
        return "", nil, errors.New("invalid column")
    }

    // 安全拼接表名和列名,参数化值
    query := fmt.Sprintf("SELECT * FROM %s WHERE %s = ?", table, column)
    return query, []interface{}{value}, nil
}
Q3: 事务失败后会自动回滚吗?

A: 是的,如果事务中的任何语句失败,整个事务会自动回滚:

// 如果第二条语句失败,第一条插入也会被回滚
"transaction": [
    {"query": "INSERT INTO accounts ...", "params": [...]},
    {"query": "INSERT INTO invalid_table ...", "params": [...]}, // 失败
]
Q4: 连接池是如何管理的?

A: 工具自动管理连接池:

  • 使用 connection_id 时,连接会被缓存
  • 未使用 connection_id 时,每次创建新连接
  • 调用 Close() 关闭所有缓存的连接
  • 连接会定期健康检查(ping)
Q5: 如何处理大结果集?

A: 使用以下策略:

// 1. 限制返回行数
"max_rows": 100

// 2. 使用分页
"query": "SELECT * FROM users LIMIT ? OFFSET ?",
"params": []interface{}{limit, offset}

// 3. 只查询需要的列
"query": "SELECT id, name FROM users"  // 而不是 SELECT *

相关文档

贡献

发现安全漏洞?

请通过私密渠道报告,不要公开披露:

  1. 发送邮件至安全团队
  2. 或通过 GitHub Security Advisory

改进建议:

  1. 提交 GitHub Issue
  2. 提交 Pull Request
  3. 参与代码审查

许可证

本项目遵循 GoAgent 项目的许可证。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APICallerRuntimeTool

type APICallerRuntimeTool struct {
	*APICallerTool
}

APICallerRuntimeTool extends APICallerTool with runtime support

func NewAPICallerRuntimeTool

func NewAPICallerRuntimeTool() *APICallerRuntimeTool

NewAPICallerRuntimeTool creates a runtime-aware API caller

func (*APICallerRuntimeTool) ExecuteWithRuntime

func (t *APICallerRuntimeTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *tools.ToolRuntime) (*interfaces.ToolOutput, error)

ExecuteWithRuntime executes with runtime support

type APICallerTool

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

APICallerTool makes HTTP API calls with authentication and retry logic

func NewAPICallerTool

func NewAPICallerTool() *APICallerTool

NewAPICallerTool creates a new API caller tool

func (*APICallerTool) ArgsSchema

func (t *APICallerTool) ArgsSchema() string

ArgsSchema returns the arguments schema as a JSON string

func (*APICallerTool) Batch

func (*APICallerTool) Description

func (t *APICallerTool) Description() string

Description returns the tool description

func (*APICallerTool) Execute

Execute makes the API call

func (*APICallerTool) Invoke

Implement Runnable interface

func (*APICallerTool) Name

func (t *APICallerTool) Name() string

Name returns the tool name

func (*APICallerTool) Stream

func (*APICallerTool) WithCallbacks

type DatabaseQueryRuntimeTool

type DatabaseQueryRuntimeTool struct {
	*DatabaseQueryTool
}

DatabaseQueryRuntimeTool extends DatabaseQueryTool with runtime support

func NewDatabaseQueryRuntimeTool

func NewDatabaseQueryRuntimeTool() *DatabaseQueryRuntimeTool

NewDatabaseQueryRuntimeTool creates a runtime-aware database query tool

func (*DatabaseQueryRuntimeTool) ExecuteWithRuntime

func (t *DatabaseQueryRuntimeTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *tools.ToolRuntime) (*interfaces.ToolOutput, error)

ExecuteWithRuntime executes with runtime support

type DatabaseQueryTool

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

DatabaseQueryTool executes SQL queries against various databases SECURITY NOTES: - Always use parameterized queries with the 'params' field - Table and column names cannot be parameterized - validate them separately - Consider implementing query templates or whitelists for production use - Enable query logging and monitoring for suspicious patterns

func NewDatabaseQueryTool

func NewDatabaseQueryTool() *DatabaseQueryTool

NewDatabaseQueryTool creates a new database query tool

func (*DatabaseQueryTool) ArgsSchema

func (t *DatabaseQueryTool) ArgsSchema() string

ArgsSchema returns the arguments schema as a JSON string

func (*DatabaseQueryTool) Batch

func (*DatabaseQueryTool) Close

func (t *DatabaseQueryTool) Close() error

Close closes all database connections

func (*DatabaseQueryTool) Description

func (t *DatabaseQueryTool) Description() string

Description returns the tool description

func (*DatabaseQueryTool) Execute

Execute runs the database query

func (*DatabaseQueryTool) Invoke

Implement Runnable interface

func (*DatabaseQueryTool) Name

func (t *DatabaseQueryTool) Name() string

Name returns the tool name

func (*DatabaseQueryTool) Stream

func (*DatabaseQueryTool) WithCallbacks

type FileCompressionTool added in v0.7.0

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

FileCompressionTool 处理文件压缩和解压操作

func NewFileCompressionTool added in v0.7.0

func NewFileCompressionTool(config *FileToolConfig) *FileCompressionTool

NewFileCompressionTool 创建文件压缩工具

func (*FileCompressionTool) ArgsSchema added in v0.7.0

func (t *FileCompressionTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileCompressionTool) Batch added in v0.7.0

Batch 实现 Runnable 接口

func (*FileCompressionTool) Description added in v0.7.0

func (t *FileCompressionTool) Description() string

Description 返回工具描述

func (*FileCompressionTool) Execute added in v0.7.0

Execute 执行文件压缩操作

func (*FileCompressionTool) Invoke added in v0.7.0

Invoke 实现 Runnable 接口

func (*FileCompressionTool) Name added in v0.7.0

func (t *FileCompressionTool) Name() string

Name 返回工具名称

func (*FileCompressionTool) Pipe added in v0.7.0

Pipe 实现 Runnable 接口

func (*FileCompressionTool) Stream added in v0.7.0

Stream 实现 Runnable 接口

func (*FileCompressionTool) WithCallbacks added in v0.7.0

WithCallbacks 实现 Runnable 接口

func (*FileCompressionTool) WithConfig added in v0.7.0

WithConfig 实现 Runnable 接口

type FileManagementTool added in v0.7.0

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

FileManagementTool 处理文件管理相关操作

func NewFileManagementTool added in v0.7.0

func NewFileManagementTool(config *FileToolConfig) *FileManagementTool

NewFileManagementTool 创建文件管理工具

func (*FileManagementTool) ArgsSchema added in v0.7.0

func (t *FileManagementTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileManagementTool) Batch added in v0.7.0

Batch 实现 Runnable 接口

func (*FileManagementTool) Description added in v0.7.0

func (t *FileManagementTool) Description() string

Description 返回工具描述

func (*FileManagementTool) Execute added in v0.7.0

Execute 执行文件管理操作

func (*FileManagementTool) Invoke added in v0.7.0

Invoke 实现 Runnable 接口

func (*FileManagementTool) Name added in v0.7.0

func (t *FileManagementTool) Name() string

Name 返回工具名称

func (*FileManagementTool) Pipe added in v0.7.0

Pipe 实现 Runnable 接口

func (*FileManagementTool) Stream added in v0.7.0

Stream 实现 Runnable 接口

func (*FileManagementTool) WithCallbacks added in v0.7.0

WithCallbacks 实现 Runnable 接口

func (*FileManagementTool) WithConfig added in v0.7.0

WithConfig 实现 Runnable 接口

type FileOperationsRuntimeTool

type FileOperationsRuntimeTool struct {
	*FileOperationsTool
}

FileOperationsRuntimeTool 扩展 FileOperationsTool 以支持运行时

func NewFileOperationsRuntimeTool

func NewFileOperationsRuntimeTool(basePath string) *FileOperationsRuntimeTool

NewFileOperationsRuntimeTool 创建支持运行时的文件操作工具

func (*FileOperationsRuntimeTool) ExecuteWithRuntime

func (t *FileOperationsRuntimeTool) ExecuteWithRuntime(ctx context.Context, input *interfaces.ToolInput, runtime *tools.ToolRuntime) (*interfaces.ToolOutput, error)

ExecuteWithRuntime 使用运行时支持执行

type FileOperationsTool

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

FileOperationsTool 保留用于向后兼容,内部委托给专门的工具 已废弃: 请直接使用 FileReadTool, FileWriteTool, FileManagementTool, FileCompressionTool, FileWatchTool Deprecated: Use specific tools (FileReadTool, FileWriteTool, FileManagementTool, FileCompressionTool, FileWatchTool) instead

func NewFileOperationsTool

func NewFileOperationsTool(basePath string) *FileOperationsTool

NewFileOperationsTool 创建文件操作工具(向后兼容) Deprecated: Use NewFileReadTool, NewFileWriteTool, etc. instead

func (*FileOperationsTool) ArgsSchema

func (t *FileOperationsTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileOperationsTool) Batch

Batch 实现 Runnable 接口

func (*FileOperationsTool) Description

func (t *FileOperationsTool) Description() string

Description 返回工具描述

func (*FileOperationsTool) Execute

Execute 执行文件操作(委托给专门工具)

func (*FileOperationsTool) Invoke

Invoke 实现 Runnable 接口

func (*FileOperationsTool) Name

func (t *FileOperationsTool) Name() string

Name 返回工具名称

func (*FileOperationsTool) Pipe

Pipe 实现 Runnable 接口

func (*FileOperationsTool) Stream

Stream 实现 Runnable 接口

func (*FileOperationsTool) WithCallbacks

WithCallbacks 实现 Runnable 接口

func (*FileOperationsTool) WithConfig

WithConfig 实现 Runnable 接口

type FileReadTool added in v0.7.0

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

FileReadTool 处理文件读取相关操作

func NewFileReadTool added in v0.7.0

func NewFileReadTool(config *FileToolConfig) *FileReadTool

NewFileReadTool 创建文件读取工具

func (*FileReadTool) ArgsSchema added in v0.7.0

func (t *FileReadTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileReadTool) Batch added in v0.7.0

func (t *FileReadTool) Batch(ctx context.Context, inputs []*interfaces.ToolInput) ([]*interfaces.ToolOutput, error)

Batch 实现 Runnable 接口

func (*FileReadTool) Description added in v0.7.0

func (t *FileReadTool) Description() string

Description 返回工具描述

func (*FileReadTool) Execute added in v0.7.0

Execute 执行文件读取操作

func (*FileReadTool) Invoke added in v0.7.0

Invoke 实现 Runnable 接口

func (*FileReadTool) Name added in v0.7.0

func (t *FileReadTool) Name() string

Name 返回工具名称

func (*FileReadTool) Pipe added in v0.7.0

Pipe 实现 Runnable 接口

func (*FileReadTool) Stream added in v0.7.0

Stream 实现 Runnable 接口

func (*FileReadTool) WithCallbacks added in v0.7.0

WithCallbacks 实现 Runnable 接口

func (*FileReadTool) WithConfig added in v0.7.0

WithConfig 实现 Runnable 接口

type FileToolConfig added in v0.7.0

type FileToolConfig struct {
	BasePath       string
	MaxFileSize    int64
	AllowedPaths   []string
	ForbiddenPaths []string
}

FileToolConfig 文件工具通用配置

func DefaultFileToolConfig added in v0.7.0

func DefaultFileToolConfig() *FileToolConfig

DefaultFileToolConfig 返回默认配置

type FileWatchTool added in v0.7.0

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

FileWatchTool 处理文件监控操作

func NewFileWatchTool added in v0.7.0

func NewFileWatchTool(config *FileToolConfig) *FileWatchTool

NewFileWatchTool 创建文件监控工具

func (*FileWatchTool) ArgsSchema added in v0.7.0

func (t *FileWatchTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileWatchTool) Batch added in v0.7.0

Batch 实现 Runnable 接口

func (*FileWatchTool) Description added in v0.7.0

func (t *FileWatchTool) Description() string

Description 返回工具描述

func (*FileWatchTool) Execute added in v0.7.0

Execute 执行文件监控操作

func (*FileWatchTool) Invoke added in v0.7.0

Invoke 实现 Runnable 接口

func (*FileWatchTool) Name added in v0.7.0

func (t *FileWatchTool) Name() string

Name 返回工具名称

func (*FileWatchTool) Pipe added in v0.7.0

Pipe 实现 Runnable 接口

func (*FileWatchTool) Stream added in v0.7.0

Stream 实现 Runnable 接口

func (*FileWatchTool) WithCallbacks added in v0.7.0

WithCallbacks 实现 Runnable 接口

func (*FileWatchTool) WithConfig added in v0.7.0

WithConfig 实现 Runnable 接口

type FileWriteTool added in v0.7.0

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

FileWriteTool 处理文件写入相关操作

func NewFileWriteTool added in v0.7.0

func NewFileWriteTool(config *FileToolConfig) *FileWriteTool

NewFileWriteTool 创建文件写入工具

func (*FileWriteTool) ArgsSchema added in v0.7.0

func (t *FileWriteTool) ArgsSchema() string

ArgsSchema 返回参数 JSON Schema

func (*FileWriteTool) Batch added in v0.7.0

Batch 实现 Runnable 接口

func (*FileWriteTool) Description added in v0.7.0

func (t *FileWriteTool) Description() string

Description 返回工具描述

func (*FileWriteTool) Execute added in v0.7.0

Execute 执行文件写入操作

func (*FileWriteTool) Invoke added in v0.7.0

Invoke 实现 Runnable 接口

func (*FileWriteTool) Name added in v0.7.0

func (t *FileWriteTool) Name() string

Name 返回工具名称

func (*FileWriteTool) Pipe added in v0.7.0

Pipe 实现 Runnable 接口

func (*FileWriteTool) Stream added in v0.7.0

Stream 实现 Runnable 接口

func (*FileWriteTool) WithCallbacks added in v0.7.0

WithCallbacks 实现 Runnable 接口

func (*FileWriteTool) WithConfig added in v0.7.0

WithConfig 实现 Runnable 接口

type RateLimiter

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

RateLimiter implements token bucket rate limiting

func NewRateLimiter

func NewRateLimiter(max int, interval time.Duration) *RateLimiter

NewRateLimiter creates a new rate limiter

func (*RateLimiter) Allow

func (r *RateLimiter) Allow() bool

Allow checks if a request is allowed

type ResponseCache

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

ResponseCache implements a simple LRU cache

func NewResponseCache

func NewResponseCache(maxSize int, ttl time.Duration) *ResponseCache

NewResponseCache creates a new response cache

func (*ResponseCache) Get

func (c *ResponseCache) Get(key string) interface{}

Get retrieves a cached value

func (*ResponseCache) Set

func (c *ResponseCache) Set(key string, value interface{})

Set stores a value in cache

type WebScraperTool

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

WebScraperTool scrapes web pages and extracts structured data

func NewWebScraperTool

func NewWebScraperTool() *WebScraperTool

NewWebScraperTool creates a new web scraper tool

func (*WebScraperTool) ArgsSchema

func (t *WebScraperTool) ArgsSchema() string

ArgsSchema returns the arguments schema as a JSON string

func (*WebScraperTool) Batch

func (*WebScraperTool) Description

func (t *WebScraperTool) Description() string

Description returns the tool description

func (*WebScraperTool) Execute

Execute runs the web scraper with ToolInput/ToolOutput

func (*WebScraperTool) Invoke

Implement Runnable interface

func (*WebScraperTool) Name

func (t *WebScraperTool) Name() string

Name returns the tool name

func (*WebScraperTool) Stream

func (*WebScraperTool) WithCallbacks

Jump to

Keyboard shortcuts

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