|
| 1 | +# tgo-device-agent – AI Agent Guide |
| 2 | + |
| 3 | +## 概述 |
| 4 | + |
| 5 | +`tgo-device-agent` 是一个 Go 编写的被控端程序,运行在受管设备上,通过 TCP JSON-RPC 2.0 协议连接到 `tgo-device-control` 服务。它向 AI Agent 暴露文件读写和 Shell 执行等工具能力。 |
| 6 | + |
| 7 | +## 架构 |
| 8 | + |
| 9 | +``` |
| 10 | +cmd/agent/main.go → 入口:CLI 参数解析、信号处理、启动 Client |
| 11 | +internal/config/config.go → 配置结构与加载 |
| 12 | +internal/protocol/ → JSON-RPC 2.0 消息类型(auth、tools/list、tools/call) |
| 13 | +internal/transport/client.go → TCP 客户端:连接、认证、心跳、重连、消息派发 |
| 14 | +internal/tools/registry.go → 工具注册中心 |
| 15 | +internal/tools/fs_read.go → fs_read 工具 |
| 16 | +internal/tools/fs_write.go → fs_write 工具 |
| 17 | +internal/tools/fs_edit.go → fs_edit 工具 |
| 18 | +internal/tools/shell_exec.go → shell_exec 工具 |
| 19 | +internal/sandbox/sandbox.go → 安全沙箱:路径验证、命令过滤 |
| 20 | +``` |
| 21 | + |
| 22 | +## 核心协议 |
| 23 | + |
| 24 | +- 协议基础文档: `../tgo-device-control/docs/json-rpc.md` |
| 25 | +- 消息格式: 换行符分隔的 JSON (`\n`-delimited JSON) |
| 26 | +- 认证方法: `auth` (bindCode 或 deviceToken) |
| 27 | +- 设备主动上报: `tools/list` 响应、`pong` 心跳 |
| 28 | +- 服务端下发: `tools/call`、`ping` |
| 29 | + |
| 30 | +## 关键接口 |
| 31 | + |
| 32 | +### Tool 接口 (internal/tools/registry.go) |
| 33 | + |
| 34 | +```go |
| 35 | +type Tool interface { |
| 36 | + Name() string |
| 37 | + Definition() protocol.ToolDefinition |
| 38 | + Execute(ctx context.Context, args map[string]interface{}) *protocol.ToolCallResult |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +添加新工具时实现此接口并在 `NewRegistry()` 中调用 `r.Register()`。 |
| 43 | + |
| 44 | +## 开发注意事项 |
| 45 | + |
| 46 | +1. **不使用外部依赖** – 当前版本仅使用 Go 标准库。 |
| 47 | +2. **类型安全** – 所有 JSON-RPC 消息均有对应的 Go struct,不使用 `interface{}`(工具参数除外,因为 MCP 规范要求动态 schema)。 |
| 48 | +3. **安全优先** – 所有文件/命令操作必须经过 sandbox 验证。 |
| 49 | +4. **幂等重连** – TCP 断线后自动重连,DeviceToken 持久化确保身份不变。 |
0 commit comments