?

接口

internal/tools/tool.go(意译):

type Tool interface {
    Name() string
    Description() string
    InputSchema() map[string]any
    Execute(ctx context.Context, input json.RawMessage) (string, error)
}

四个方法,无生命周期。从循环角度看,工具是无状态的 —— 工具需要的任何状态(编译后的正则缓存、进程内索引)都是具体类型上的私有字段。

新工具骨架

让我们添加一个假想的 http_get 工具,它抓取一个 URL 并返回其 body。

步骤 1 —— 类型

package builtin

import (
    "context"
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "time"

    "github.com/sebastienrousseau/rousseau-agent/internal/tools"
)

// HTTPGetTool fetches a URL over HTTPS and returns the response body.
type HTTPGetTool struct {
    Timeout time.Duration
    client  *http.Client
}

// NewHTTPGetTool constructs an HTTPGetTool. Zero timeout uses 30s.
func NewHTTPGetTool(timeout time.Duration) *HTTPGetTool {
    if timeout == 0 {
        timeout = 30 * time.Second
    }
    return &HTTPGetTool{
        Timeout: timeout,
        client:  &http.Client{Timeout: timeout},
    }
}

步骤 2 —— 元数据

// Name satisfies tools.Tool.
func (*HTTPGetTool) Name() string { return "http_get" }

// Description satisfies tools.Tool.
func (*HTTPGetTool) Description() string {
    return "Fetch an HTTPS URL and return the response body. Input: url (string). Redirects are followed up to 10 hops. Response is capped at 1 MiB."
}

描述面向模型。它应读起来像写给另一位工程师的简短 docstring —— 工具做什么、其输入的含义、输出的形态。

步骤 3 —— 输入 schema

// InputSchema satisfies tools.Tool.
func (*HTTPGetTool) InputSchema() map[string]any {
    return map[string]any{
        "type": "object",
        "properties": map[string]any{
            "url": map[string]any{
                "type":        "string",
                "description": "Absolute HTTPS URL to fetch.",
            },
        },
        "required": []string{"url"},
    }
}

保持 schema 严格。每个属性都有 description。required 数组由模型的 tool-use 校验器强制执行 —— 缺失字段会导致 tool_use 重试,而非运行时错误。

步骤 4 —— Execute

type httpGetInput struct {
    URL string `json:"url"`
}

// Execute satisfies tools.Tool.
func (t *HTTPGetTool) Execute(ctx context.Context, raw json.RawMessage) (string, error) {
    var in httpGetInput
    if err := json.Unmarshal(raw, &in); err != nil {
        return "", fmt.Errorf("http_get: parse input: %w", err)
    }
    if in.URL == "" {
        return "", fmt.Errorf("http_get: url is required")
    }
    // Refuse plaintext HTTP; refuse non-http schemes.
    if !strings.HasPrefix(in.URL, "https://") {
        return "", fmt.Errorf("http_get: only https:// URLs are permitted")
    }

    req, err := http.NewRequestWithContext(ctx, http.MethodGet, in.URL, nil)
    if err != nil {
        return "", fmt.Errorf("http_get: build request: %w", err)
    }
    req.Header.Set("user-agent", "rousseau-agent/http_get")

    resp, err := t.client.Do(req)
    if err != nil {
        return "", fmt.Errorf("http_get: transport: %w", err)
    }
    defer func() { _ = resp.Body.Close() }()

    body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
    if err != nil {
        return "", fmt.Errorf("http_get: read body: %w", err)
    }
    return fmt.Sprintf("HTTP %d\n%s", resp.StatusCode, string(body)), nil
}

// Compile-time interface satisfaction check.
var _ tools.Tool = (*HTTPGetTool)(nil)

步骤 5 —— 注册

在 internal/cli/chat.go 中接线(以及其他每个构造 registry 的命令 —— grep registry.MustRegister 找到它们):

registry.MustRegister(builtin.NewHTTPGetTool(30 * time.Second))

注册之后,工具在每一轮都可供模型使用。

步骤 6 —— 测试

模式请参考 internal/tools/builtin/read_test.go:

func TestHTTPGetTool_Execute_Success(t *testing.T) {
    srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
        _, _ = w.Write([]byte("hello"))
    }))
    defer srv.Close()

    // The tool refuses plaintext HTTP; wrap the test server behind httptest.NewTLSServer instead
    // for a real integration test, or expose an internal seam that permits `http://` in tests only.
    // The skeleton here is illustrative.
}

func TestHTTPGetTool_Execute_RejectsPlaintextHTTP(t *testing.T) {
    tool := builtin.NewHTTPGetTool(0)
    _, err := tool.Execute(context.Background(), json.RawMessage(`{"url":"http://example.com"}`))
    require.Error(t, err)
    require.Contains(t, err.Error(), "only https")
}

步骤 7 —— 审批策略

现在工具已对模型可用,受 审批策略 约束。请在文档中建议一条默认姿态的 deny 规则:

deny:
  - {tool: http_get, match: "\"url\":\"https://(169\\.254|10\\.|192\\.168\\.|172\\.(1[6-9]|2[0-9]|3[01])\\.)"}

这可阻止工具调用 AWS IMDS 或私有 RFC1918 段 —— 对于抓取 HTTP 的工具这是一个常见诉求。

步骤 8 —— 文档

在 content/user-guide/tools.md 中添加一节描述新工具:schema、语义、安全注释。遵循既有五个工具的形态。

契约细节

  • 无状态:Execute 不得在调用之间携带非明确私有于工具自身字段的状态。两个会话上的两次并发轮次可能同时调用同一工具。
  • 尊重 context:Execute 必须遵循 ctx 取消。长时间工作应周期性检查 ctx.Err(),或通过 context 感知的库调用路由。
  • 不 panic:改为返回错误。代理循环会把错误转换为 IsError: true 的 tool_result,模型可据此适配。
  • 返回形态:输出是一个纯字符串,下一轮回喂给模型。包含足够结构(例如行号、状态码)以便模型推理。

不改动源码的自定义工具

若您不想 fork rousseau,请将代理循环嵌入到您自己的二进制中,并在那里注册您的工具:

registry := tools.NewRegistry()
registry.MustRegister(builtin.NewReadTool())
registry.MustRegister(builtin.NewWriteTool())
// ...
registry.MustRegister(mypkg.NewMyTool())

ag := agent.New(provider, registry, logger, agent.Options{})

完整的嵌入示例参见源码树中的 examples/embed-agent/。

常见坑

  • 过于宽泛的 schema。 只要求 type: object 对模型毫无帮助。请枚举每个属性、描述每个字段。
  • 阻塞 I/O 而无 deadline。 请始终使用 NewRequestWithContext,始终设置 http.Client{Timeout: ...},始终尊重 ctx。
  • 返回过多。 输出会在下一轮回喂给模型。1 MB 响应会烧掉 token;请设定上限。
  • 副作用溢出。 会改变外部世界的工具应在返回字符串中记录其所做的事,以让审批器的审计链条完整。
  • 忘记编译期接口检查。 在 package 作用域加上 var _ tools.Tool = (*MyTool)(nil),可在构建期捕获接口漂移。

下一步

Type to search 150+ pages. Ranking: BM25 with title/description boost.