Skip to content

Commit cc14ce6

Browse files
committed
feat: add tgo-device-agent and update RAG service configuration
- Implement `tgo-device-agent` in Go, providing file operations (read/write/edit) and shell execution tools for AI agents via TCP JSON-RPC 2.0. - Add security sandbox for `tgo-device-agent` with path whitelisting and command blacklisting. - Update `tgo-rag` service to support configurable default QA mode and change its default port to 18082 to avoid conflicts. - Remove obsolete `docs/GET_REAL_IP.md` and update environment templates.
1 parent eadd02d commit cc14ce6

25 files changed

Lines changed: 2184 additions & 142 deletions

File tree

.env.dev.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TGO_WIDGET_PORT=5174
6060
# ==========================================================
6161
API_SERVICE_URL=http://localhost:8000
6262
AI_SERVICE_URL=http://localhost:8081
63-
RAG_SERVICE_URL=http://localhost:8082
63+
RAG_SERVICE_URL=http://localhost:18082
6464
PLATFORM_SERVICE_URL=http://localhost:8003
6565
WORKFLOW_SERVICE_URL=http://localhost:8004
6666
PLUGIN_RUNTIME_URL=http://localhost:8090

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ WIDGET_DIR := repos/tgo-widget-app
3737
# Development ports (to avoid conflicts)
3838
API_PORT := 8000
3939
AI_PORT := 8081
40-
RAG_PORT := 8082
40+
RAG_PORT := 18082
4141
PLATFORM_PORT := 8003
4242
WORKFLOW_PORT := 8004
4343
PLUGIN_PORT := 8090

docs/GET_REAL_IP.md

Lines changed: 0 additions & 133 deletions
This file was deleted.

repos/tgo-device-agent/.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Binaries
2+
tgo-device-agent
3+
*.exe
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Build output
9+
/bin/
10+
/dist/
11+
/build/
12+
13+
# IDE
14+
.idea/
15+
.vscode/
16+
*.swp
17+
*.swo
18+
*~
19+
20+
# OS
21+
.DS_Store
22+
Thumbs.db
23+
24+
# Config with secrets
25+
configs/config.json
26+
*.env
27+
28+
# Token files
29+
device_token
30+
31+
# Build artifacts
32+
*.out

repos/tgo-device-agent/AGENTS.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 持久化确保身份不变。

repos/tgo-device-agent/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM golang:1.22-alpine AS builder
2+
3+
WORKDIR /build
4+
5+
COPY go.mod ./
6+
# COPY go.sum ./ # uncomment when external dependencies are added
7+
# RUN go mod download
8+
9+
COPY . .
10+
11+
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o /tgo-device-agent ./cmd/agent/
12+
13+
# ----
14+
FROM alpine:3.19
15+
16+
RUN apk add --no-cache ca-certificates bash
17+
18+
COPY --from=builder /tgo-device-agent /usr/local/bin/tgo-device-agent
19+
20+
RUN mkdir -p /workspace && \
21+
adduser -D -h /workspace agent && \
22+
chown agent:agent /workspace
23+
24+
USER agent
25+
WORKDIR /workspace
26+
27+
ENTRYPOINT ["tgo-device-agent"]
28+
CMD ["--help"]

repos/tgo-device-agent/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
APP_NAME := tgo-device-agent
2+
VERSION := 1.0.0
3+
GO := go
4+
GOFLAGS := -ldflags="-s -w -X main.version=$(VERSION)"
5+
BIN_DIR := bin
6+
7+
.PHONY: all build clean vet test run
8+
9+
all: build
10+
11+
build:
12+
@mkdir -p $(BIN_DIR)
13+
$(GO) build $(GOFLAGS) -o $(BIN_DIR)/$(APP_NAME) ./cmd/agent/
14+
15+
build-linux:
16+
@mkdir -p $(BIN_DIR)
17+
GOOS=linux GOARCH=amd64 $(GO) build $(GOFLAGS) -o $(BIN_DIR)/$(APP_NAME)-linux-amd64 ./cmd/agent/
18+
19+
build-darwin:
20+
@mkdir -p $(BIN_DIR)
21+
GOOS=darwin GOARCH=arm64 $(GO) build $(GOFLAGS) -o $(BIN_DIR)/$(APP_NAME)-darwin-arm64 ./cmd/agent/
22+
23+
vet:
24+
$(GO) vet ./...
25+
26+
test:
27+
$(GO) test -v -race ./...
28+
29+
clean:
30+
rm -rf $(BIN_DIR)
31+
32+
run: build
33+
$(BIN_DIR)/$(APP_NAME) --server localhost:9876 --bind-code TEST123 --log-level debug
34+
35+
docker-build:
36+
docker build -t $(APP_NAME):$(VERSION) .

0 commit comments

Comments
 (0)