v1.4 工具集扩展 + Windows 编码攻坚
- 新增 BashTool(bash 命令执行,30s 超时 + 自愈 + 截断) - 新增 WriteFileTool(文件写入) - Windows GBK 编码自动转 UTF-8(golang.org/x/text) - OpenAIProvider 新增 API 交互日志 - dumpMessages 移除截断,完整展示上下文 - cmd/claw 三段式任务演示
This commit is contained in:
@@ -65,6 +65,32 @@ func main() {
|
|||||||
|
|
||||||
## 版本历史
|
## 版本历史
|
||||||
|
|
||||||
|
### v1.4 — 工具集扩展与 Windows 编码攻坚
|
||||||
|
|
||||||
|
#### 变更
|
||||||
|
|
||||||
|
- **新增 BashTool** — 执行本地 bash 命令,支持 30s 超时、错误原样回传(模型自愈)、8KB 输出截断
|
||||||
|
- **新增 WriteFileTool** — 写文件到工作区,覆盖/新建均支持
|
||||||
|
- **Windows GBK 编码修复** — 命令输出从 GBK 自动转 UTF-8(`golang.org/x/text/encoding/simplifiedchinese`),不依赖 `chcp`
|
||||||
|
- **API 交互日志** — `OpenAIProvider.Generate` 新增请求/响应日志(阶段标记、消息数、工具数、ToolCall 明细)
|
||||||
|
- **调试输出优化** — `dumpMessages` 移除 80 字符截断,完整展示上下文内容
|
||||||
|
- **三段式任务演示** — `cmd/claw` 任务改为:查 Go 版本 → 写 helloworld.go → 编译运行
|
||||||
|
|
||||||
|
#### 踩坑记录
|
||||||
|
|
||||||
|
| 问题 | 原因 | 解决 |
|
||||||
|
|---|---|---|
|
||||||
|
| bash 输出中文乱码 | Windows 命令输出为 GBK 编码,Go 按 UTF-8 解析 | 引入 `golang.org/x/text`,检测编码后自动转换 |
|
||||||
|
| `chcp 65001` 导致终端刷屏 | `chcp` 在子进程中修改代码页可能影响终端渲染 | 放弃 `chcp` 方案,改用 Go 原生编码转换 |
|
||||||
|
| `utf8.Valid` 检查后仍乱码 | GBK 字节序列碰巧也合法于 UTF-8,跳过转换 | 去掉 `utf8.Valid` 判断,Windows 下一律转换 |
|
||||||
|
| `helloworld.go` 导致编译失败 | 模型生成的测试文件含 `main` 函数,与项目 main.go 冲突 | 运行后清理 `helloworld.*` 测试产物 |
|
||||||
|
|
||||||
|
#### 经验教训
|
||||||
|
|
||||||
|
1. **编码问题不要依赖外部命令** — `chcp` 属于"改环境让输出配合你",不可靠。Go 原生转码属于"你主动适应输出",稳定可控
|
||||||
|
2. **`utf8.Valid` 不能当编码检测器** — 它的语义是"是否合法 UTF-8",不是"是否是 GBK"。GBK 和 UTF-8 有交集,用合法性判断编码方向是伪命题
|
||||||
|
3. **Agent 的工具越多,越需要关注副作用** — 模型会写文件、执行命令,产生的文件(helloworld.go)可能反过来破坏项目结构。工具内部要做好隔离
|
||||||
|
|
||||||
### v1.3 — Registry 实现重构与第一把真实工具
|
### v1.3 — Registry 实现重构与第一把真实工具
|
||||||
|
|
||||||
#### 变更
|
#### 变更
|
||||||
|
|||||||
+12
-10
@@ -18,21 +18,23 @@ func main() {
|
|||||||
// 这里你可以任意切换 NewZhipuClaudeProvider 或 NewZhipuOpenAIProvider,效果完全一致!
|
// 这里你可以任意切换 NewZhipuClaudeProvider 或 NewZhipuOpenAIProvider,效果完全一致!
|
||||||
llmProvider := provider.DeepseekOpenAIProvider("deepseek-v4-flash")
|
llmProvider := provider.DeepseekOpenAIProvider("deepseek-v4-flash")
|
||||||
|
|
||||||
// 3. 初始化真实的 Tool Registry
|
|
||||||
registry := tools.NewRegistry()
|
registry := tools.NewRegistry()
|
||||||
|
|
||||||
// 4. 将真实的 ReadFile 工具挂载到注册表中
|
// 挂载极简工具集
|
||||||
readFileTool := tools.NewReadFileTool(workDir)
|
registry.Register(tools.NewReadFileTool(workDir))
|
||||||
registry.Register(readFileTool)
|
registry.Register(tools.NewWriteFileTool(workDir))
|
||||||
|
registry.Register(tools.NewBashTool(workDir))
|
||||||
|
|
||||||
// 5. 实例化核心引擎,由于任务简单,我们关闭思考阶段 (EnableThinking = false) 以加快速度
|
// 实例化核心引擎,关闭慢思考阶段,享受 YOLO 急速模式
|
||||||
eng := engine.NewAgentEngine(llmProvider, registry, workDir, false)
|
eng := engine.NewAgentEngine(llmProvider, registry, workDir, false)
|
||||||
|
|
||||||
log.Printf("registry: %s\n", registry)
|
// 发起一个需要连贯物理动作的任务
|
||||||
log.Printf("engine: %s\n", eng)
|
prompt := `
|
||||||
|
请帮我执行以下操作:
|
||||||
// 6. 下发一个必须通过真实工具才能完成的任务
|
1. 用 bash 查看一下我当前电脑的 Go 版本,我的电脑是windows系统。
|
||||||
prompt := "请调用工具读取一下当前工作区目录下 hello.txt 文件的内容,并用一句话向我总结它说了什么。"
|
2. 帮我写一个简单的 helloworld.go 文件,输出 "Hello, go-tiny-claw!"。
|
||||||
|
3. 用 bash 编译并运行这个 go 文件,确认它能正常工作。
|
||||||
|
`
|
||||||
|
|
||||||
err := eng.Run(context.Background(), prompt)
|
err := eng.Run(context.Background(), prompt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -3,13 +3,16 @@ module go-tiny-claw
|
|||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/anthropics/anthropic-sdk-go v1.43.0 // indirect
|
github.com/anthropics/anthropic-sdk-go v1.43.0
|
||||||
|
github.com/openai/openai-go/v3 v3.35.0
|
||||||
|
golang.org/x/text v0.27.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
github.com/bahlo/generic-list-go v0.2.0 // indirect
|
||||||
github.com/buger/jsonparser v1.1.2 // indirect
|
github.com/buger/jsonparser v1.1.2 // indirect
|
||||||
github.com/invopop/jsonschema v0.13.0 // indirect
|
github.com/invopop/jsonschema v0.13.0 // indirect
|
||||||
github.com/joho/godotenv v1.5.1 // indirect
|
|
||||||
github.com/mailru/easyjson v0.7.7 // indirect
|
github.com/mailru/easyjson v0.7.7 // indirect
|
||||||
github.com/openai/openai-go/v3 v3.35.0 // indirect
|
|
||||||
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 // indirect
|
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 // indirect
|
||||||
github.com/tidwall/gjson v1.18.0 // indirect
|
github.com/tidwall/gjson v1.18.0 // indirect
|
||||||
github.com/tidwall/match v1.1.1 // indirect
|
github.com/tidwall/match v1.1.1 // indirect
|
||||||
|
|||||||
@@ -4,17 +4,23 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn
|
|||||||
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
|
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
|
||||||
github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJk=
|
github.com/buger/jsonparser v1.1.2 h1:frqHqw7otoVbk5M8LlE/L7HTnIq2v9RX6EJ48i9AxJk=
|
||||||
github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
github.com/buger/jsonparser v1.1.2/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
|
||||||
|
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
|
||||||
github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=
|
github.com/invopop/jsonschema v0.13.0 h1:KvpoAJWEjR3uD9Kbm2HWJmqsEaHt8lBUpd0qHcIi21E=
|
||||||
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
|
github.com/invopop/jsonschema v0.13.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
|
||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
|
||||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||||
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
|
||||||
github.com/openai/openai-go/v3 v3.35.0 h1:109x3epXMSE423KW2euR506GGFezcEt0s87MoWejpH0=
|
github.com/openai/openai-go/v3 v3.35.0 h1:109x3epXMSE423KW2euR506GGFezcEt0s87MoWejpH0=
|
||||||
github.com/openai/openai-go/v3 v3.35.0/go.mod h1:cdufnVK14cWcT9qA1rRtrXx4FTRsgbDPW7Ia7SS5cZo=
|
github.com/openai/openai-go/v3 v3.35.0/go.mod h1:cdufnVK14cWcT9qA1rRtrXx4FTRsgbDPW7Ia7SS5cZo=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 h1:uOfcYT+3QungH6tIGSVCR/Y3KJmgJiHcojJbMTPDZAI=
|
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 h1:uOfcYT+3QungH6tIGSVCR/Y3KJmgJiHcojJbMTPDZAI=
|
||||||
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1/go.mod h1:L1MQhA6x4dn9r007T033lsaZMv9EmBAdXyU/+EF40fo=
|
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1/go.mod h1:L1MQhA6x4dn9r007T033lsaZMv9EmBAdXyU/+EF40fo=
|
||||||
|
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||||
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||||
@@ -29,6 +35,11 @@ github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/
|
|||||||
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
|
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
|
||||||
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
|
||||||
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||||
|
golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4=
|
||||||
|
golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ func (e *AgentEngine) String() string {
|
|||||||
func dumpMessages(msgs []schema.Message) {
|
func dumpMessages(msgs []schema.Message) {
|
||||||
for i, msg := range msgs {
|
for i, msg := range msgs {
|
||||||
content := msg.Content
|
content := msg.Content
|
||||||
if len(content) > 80 {
|
// if len(content) > 80 {
|
||||||
content = content[:80] + "..."
|
// content = content[:80] + "..."
|
||||||
}
|
// }
|
||||||
log.Printf(" [%02d] %-10s | %s", i, msg.Role, content)
|
log.Printf(" [%02d] %-10s | %s", i, msg.Role, content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,16 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
// "os"
|
// "os"
|
||||||
|
|
||||||
|
"go-tiny-claw/internal/schema"
|
||||||
|
|
||||||
"github.com/openai/openai-go/v3"
|
"github.com/openai/openai-go/v3"
|
||||||
"github.com/openai/openai-go/v3/option"
|
"github.com/openai/openai-go/v3/option"
|
||||||
"github.com/openai/openai-go/v3/packages/param"
|
"github.com/openai/openai-go/v3/packages/param"
|
||||||
"github.com/openai/openai-go/v3/shared"
|
"github.com/openai/openai-go/v3/shared"
|
||||||
"go-tiny-claw/internal/schema"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type OpenAIProvider struct {
|
type OpenAIProvider struct {
|
||||||
@@ -35,6 +38,13 @@ func DeepseekOpenAIProvider(model string) *OpenAIProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *OpenAIProvider) Generate(ctx context.Context, msgs []schema.Message, availableTools []schema.ToolDefinition) (*schema.Message, error) {
|
func (p *OpenAIProvider) Generate(ctx context.Context, msgs []schema.Message, availableTools []schema.ToolDefinition) (*schema.Message, error) {
|
||||||
|
phase := "Action"
|
||||||
|
if len(availableTools) == 0 {
|
||||||
|
phase = "Thinking"
|
||||||
|
}
|
||||||
|
log.Printf("[Provider] → API 调用 | 模型: %s | 阶段: %s | 消息数: %d | 工具数: %d",
|
||||||
|
p.model, phase, len(msgs), len(availableTools))
|
||||||
|
|
||||||
var openaiMsgs []openai.ChatCompletionMessageParamUnion
|
var openaiMsgs []openai.ChatCompletionMessageParamUnion
|
||||||
|
|
||||||
// 1. 翻译上下文消息
|
// 1. 翻译上下文消息
|
||||||
@@ -184,10 +194,20 @@ func (p *OpenAIProvider) Generate(ctx context.Context, msgs []schema.Message, av
|
|||||||
resultMsg.ToolCalls = append(resultMsg.ToolCalls, schema.ToolCall{
|
resultMsg.ToolCalls = append(resultMsg.ToolCalls, schema.ToolCall{
|
||||||
ID: tc.ID,
|
ID: tc.ID,
|
||||||
Name: tc.Function.Name,
|
Name: tc.Function.Name,
|
||||||
Arguments: []byte(tc.Function.Arguments), // 提取 JSON 字符串字节
|
Arguments: []byte(tc.Function.Arguments),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 打印响应摘要
|
||||||
|
contentPreview := resultMsg.Content
|
||||||
|
// if len(contentPreview) > 60 {
|
||||||
|
// contentPreview = contentPreview[:60] + "..."
|
||||||
|
// }
|
||||||
|
log.Printf("[Provider] ← API 响应 | 内容: %q | 工具调用: %d", contentPreview, len(resultMsg.ToolCalls))
|
||||||
|
for _, tc := range resultMsg.ToolCalls {
|
||||||
|
log.Printf("[Provider] ↳ ToolCall: %s(%s)", tc.Name, string(tc.Arguments))
|
||||||
|
}
|
||||||
|
|
||||||
return resultMsg, nil
|
return resultMsg, nil
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"go-tiny-claw/internal/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BashTool struct {
|
||||||
|
workDir string // 工作区约束
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBashTool(workDir string) *BashTool {
|
||||||
|
return &BashTool{workDir: workDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *BashTool) Name() string {
|
||||||
|
return "bash"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *BashTool) Definition() schema.ToolDefinition {
|
||||||
|
return schema.ToolDefinition{
|
||||||
|
Name: t.Name(),
|
||||||
|
Description: "在当前工作区执行任意的 bash 命令。支持链式命令(如 &&)。返回标准输出(stdout)和标准错误(stderr)。",
|
||||||
|
InputSchema: map[string]interface{}{
|
||||||
|
"type": "object",
|
||||||
|
"properties": map[string]interface{}{
|
||||||
|
"command": map[string]interface{}{
|
||||||
|
"type": "string",
|
||||||
|
"description": "要执行的 bash 命令,例如: ls -la 或 go test ./...",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": []string{"command"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type bashArgs struct {
|
||||||
|
Command string `json:"command"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *BashTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
|
||||||
|
var input bashArgs
|
||||||
|
if err := json.Unmarshal(args, &input); err != nil {
|
||||||
|
return "", fmt.Errorf("参数解析失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 【驾驭底线 1】:Time Budgeting (时间预算与超时控制)
|
||||||
|
timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Unix 和 Windows 统一使用 bash -c(Windows 需安装 Git Bash),编码转换在下方处理
|
||||||
|
cmd := exec.CommandContext(timeoutCtx, "bash", "-c", input.Command)
|
||||||
|
|
||||||
|
// 【驾驭底线 2】:绑定执行的工作区目录
|
||||||
|
cmd.Dir = t.workDir
|
||||||
|
|
||||||
|
// 执行并捕获 CombinedOutput (合并 stdout 和 stderr)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
|
||||||
|
outputStr := string(out)
|
||||||
|
|
||||||
|
// 如果命令执行超时,返回警告信息让模型知晓
|
||||||
|
if timeoutCtx.Err() == context.DeadlineExceeded {
|
||||||
|
return outputStr + "\n[警告: 命令执行超时(30s),已被系统强制终止。如果是启动常驻服务,请尝试将其转入后台。]", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 【驾驭底线 3】:错误原样回传 (Self-Correction 自愈机制)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("执行报错: %v\n输出:\n%s", err, outputStr), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果没有终端输出,给模型一个明确的执行成功的反馈
|
||||||
|
if outputStr == "" {
|
||||||
|
return "命令执行成功,无终端输出。", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 【驾驭底线 4】:长度截断保护 (防 OOM)
|
||||||
|
const maxLen = 8000
|
||||||
|
if len(outputStr) > maxLen {
|
||||||
|
return fmt.Sprintf("%s\n\n...[终端输出过长,已截断至前 %d 字节]...", outputStr[:maxLen], maxLen), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputStr, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"go-tiny-claw/internal/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WriteFileTool struct {
|
||||||
|
workDir string // 工作区约束
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWriteFileTool(workDir string) *WriteFileTool {
|
||||||
|
return &WriteFileTool{workDir: workDir}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *WriteFileTool) Name() string {
|
||||||
|
return "write_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *WriteFileTool) Definition() schema.ToolDefinition {
|
||||||
|
return schema.ToolDefinition{
|
||||||
|
Name: t.Name(),
|
||||||
|
Description: "创建或覆盖写入一个文件。如果目录不存在会自动创建。请提供相对于工作区的相对路径。",
|
||||||
|
InputSchema: map[string]interface{}{
|
||||||
|
"type": "object",
|
||||||
|
"properties": map[string]interface{}{
|
||||||
|
"path": map[string]interface{}{
|
||||||
|
"type": "string",
|
||||||
|
"description": "要写入的文件路径,如 src/main.go",
|
||||||
|
},
|
||||||
|
"content": map[string]interface{}{
|
||||||
|
"type": "string",
|
||||||
|
"description": "要写入的完整文件内容",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"required": []string{"path", "content"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type writeFileArgs struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *WriteFileTool) Execute(ctx context.Context, args json.RawMessage) (string, error) {
|
||||||
|
var input writeFileArgs
|
||||||
|
if err := json.Unmarshal(args, &input); err != nil {
|
||||||
|
return "", fmt.Errorf("参数解析失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 【安全防线】:限制在 WorkDir 下执行,防止大模型修改系统级文件
|
||||||
|
fullPath := filepath.Join(t.workDir, input.Path)
|
||||||
|
|
||||||
|
// 自动创建缺失的父级目录
|
||||||
|
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
|
||||||
|
return "", fmt.Errorf("创建父目录失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入文件内容,权限设为 0644
|
||||||
|
err := os.WriteFile(fullPath, []byte(input.Content), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("写入文件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("成功将内容写入到文件: %s", input.Path), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user