mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-23 10:28:06 +00:00
feat: complete P0/P1/P2 — WebUI SPA, OpenClaw sidecar+simulator, healthcheck auto-sched+perf
P0: WebUI重构
- 完整 SPA 仪表盘 (7标签页), //go:embed dashboard.html
P1: OpenClaw兼容 (三通道: SKILL.md / sidecar / simulator)
- Node.js 模拟进程统一加载任意 OpenClaw 插件
- JSON-RPC 2.0 over stdio 协议, go:embed 内嵌
P2: Healthcheck 优化
- 定时自动执行 (startAutoCheck, 30min)
- healthcheck_perf 性能监控工具
其他: agentcli/cmd 插件, integration_test, status.go,
test_deepseek 清理, 多项 bug 修复
This commit is contained in:
115
internal/plugins/cmd/plugin.go
Normal file
115
internal/plugins/cmd/plugin.go
Normal file
@ -0,0 +1,115 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitcode.com/JianFeeeee/HomeAgent/internal/plugin"
|
||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
)
|
||||
|
||||
func init() {
|
||||
plugin.RegisterFactory("cmd", func(name string, config map[string]interface{}) (sdk.Plugin, error) {
|
||||
return New(name), nil
|
||||
})
|
||||
}
|
||||
|
||||
type Plugin struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func New(name string) *Plugin {
|
||||
return &Plugin{name: name}
|
||||
}
|
||||
|
||||
func (p *Plugin) Name() string { return p.name }
|
||||
|
||||
func (p *Plugin) Start(s *sdk.PluginSDK) error {
|
||||
s.RegisterTool("cmd_run", sdk.ToolDef{
|
||||
Name: "cmd_run",
|
||||
Description: "执行一条系统命令并返回输出。适用于查询系统信息、运行脚本、操作文件等单次命令场景。命令在临时 shell 中执行,不支持交互。如需交互式终端(如 vim、ssh、top),请使用 terminal_create 相关工具。",
|
||||
Parameters: map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"command": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "要执行的命令",
|
||||
},
|
||||
"timeout": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "超时时间,例如 10s, 1m, 30s(默认 30s)",
|
||||
},
|
||||
"workdir": map[string]interface{}{
|
||||
"type": "string",
|
||||
"description": "工作目录(可选,默认当前目录)",
|
||||
},
|
||||
},
|
||||
"required": []string{"command"},
|
||||
},
|
||||
}, func(args map[string]interface{}) (interface{}, error) {
|
||||
command, _ := args["command"].(string)
|
||||
if command == "" {
|
||||
return map[string]interface{}{"error": "command is required"}, nil
|
||||
}
|
||||
|
||||
timeoutStr, _ := args["timeout"].(string)
|
||||
if timeoutStr == "" {
|
||||
timeoutStr = "30s"
|
||||
}
|
||||
timeout, err := time.ParseDuration(timeoutStr)
|
||||
if err != nil {
|
||||
return map[string]interface{}{"error": fmt.Sprintf("invalid timeout %q: %v", timeoutStr, err)}, nil
|
||||
}
|
||||
|
||||
workdir, _ := args["workdir"].(string)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
cmd := exec.CommandContext(ctx, "sh", "-c", command)
|
||||
if workdir != "" {
|
||||
cmd.Dir = workdir
|
||||
}
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
if err := cmd.Run(); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return map[string]interface{}{
|
||||
"status": "timeout",
|
||||
"stdout": truncateOutput(stdout.String()),
|
||||
"stderr": truncateOutput(stderr.String()),
|
||||
"error": fmt.Sprintf("命令执行超时(%s)", timeoutStr),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"status": "ok",
|
||||
"stdout": truncateOutput(stdout.String()),
|
||||
"stderr": truncateOutput(stderr.String()),
|
||||
"exit_code": cmd.ProcessState.ExitCode(),
|
||||
"command": command,
|
||||
}, nil
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func truncateOutput(s string) string {
|
||||
const maxLen = 32000
|
||||
if len(s) > maxLen {
|
||||
return s[:maxLen] + fmt.Sprintf("\n... [输出被截断,共 %d 字节]", len(s))
|
||||
}
|
||||
return strings.TrimRight(s, "\n")
|
||||
}
|
||||
261
internal/plugins/cmd/plugin_test.go
Normal file
261
internal/plugins/cmd/plugin_test.go
Normal file
@ -0,0 +1,261 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
sdk "gitcode.com/JianFeeeee/HomeAgent/internal/sdk"
|
||||
)
|
||||
|
||||
type toolCapture struct {
|
||||
handlers map[string]sdk.ToolHandler
|
||||
defs map[string]sdk.ToolDef
|
||||
}
|
||||
|
||||
func newToolCapture() *toolCapture {
|
||||
return &toolCapture{
|
||||
handlers: make(map[string]sdk.ToolHandler),
|
||||
defs: make(map[string]sdk.ToolDef),
|
||||
}
|
||||
}
|
||||
|
||||
func (tc *toolCapture) RegisterTool(name string, def sdk.ToolDef, handler sdk.ToolHandler) error {
|
||||
tc.handlers[name] = handler
|
||||
tc.defs[name] = def
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tc *toolCapture) RegisterStage(stage sdk.Stage, handler sdk.StageHandler) {}
|
||||
|
||||
func (tc *toolCapture) RegisterAPI(name string) error { return nil }
|
||||
|
||||
func setupPlugin() (*Plugin, *toolCapture, error) {
|
||||
p := New("cmd")
|
||||
tc := newToolCapture()
|
||||
sdk := sdk.New("cmd", nil, nil, nil, nil, nil, nil, nil, nil, tc.RegisterTool, tc.RegisterStage, tc.RegisterAPI)
|
||||
if err := p.Start(sdk); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return p, tc, nil
|
||||
}
|
||||
|
||||
func TestCmdRunEcho(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler, ok := tc.handlers["cmd_run"]
|
||||
if !ok {
|
||||
t.Fatal("cmd_run tool not registered")
|
||||
}
|
||||
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "echo hello world",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %v", resp["status"])
|
||||
}
|
||||
if resp["stdout"] != "hello world" {
|
||||
t.Fatalf("expected 'hello world', got %v", resp["stdout"])
|
||||
}
|
||||
if resp["exit_code"].(float64) != 0 {
|
||||
t.Fatalf("expected exit code 0, got %v", resp["exit_code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunWithStderr(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "echo out && echo err >&2 && exit 1",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %v", resp["status"])
|
||||
}
|
||||
if resp["stdout"] != "out" {
|
||||
t.Fatalf("expected stdout 'out', got %v", resp["stdout"])
|
||||
}
|
||||
if resp["stderr"] != "err" {
|
||||
t.Fatalf("expected stderr 'err', got %v", resp["stderr"])
|
||||
}
|
||||
if resp["exit_code"].(float64) != 1 {
|
||||
t.Fatalf("expected exit code 1, got %v", resp["exit_code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunTimeout(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "sleep 10",
|
||||
"timeout": "1s",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "timeout" {
|
||||
t.Fatalf("expected status timeout, got %v", resp["status"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunWorkdir(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tmpDir, err := os.MkdirTemp("", "cmd_test_*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
marker := filepath.Join(tmpDir, "marker.txt")
|
||||
if err := os.WriteFile(marker, []byte("ok"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "cat marker.txt",
|
||||
"workdir": tmpDir,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %v", resp["status"])
|
||||
}
|
||||
if resp["stdout"] != "ok" {
|
||||
t.Fatalf("expected 'ok', got %v", resp["stdout"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunMissingCommand(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if _, ok := resp["error"]; !ok {
|
||||
t.Fatal("expected error for missing command")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunNonZeroExit(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "exit 42",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %v", resp["status"])
|
||||
}
|
||||
if resp["exit_code"].(float64) != 42 {
|
||||
t.Fatalf("expected exit code 42, got %v", resp["exit_code"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateOutput(t *testing.T) {
|
||||
short := "hello"
|
||||
if s := truncateOutput(short); s != short {
|
||||
t.Fatalf("expected %q, got %q", short, s)
|
||||
}
|
||||
|
||||
long := make([]byte, 40000)
|
||||
for i := range long {
|
||||
long[i] = 'x'
|
||||
}
|
||||
s := truncateOutput(string(long))
|
||||
if len(s) >= 40000 {
|
||||
t.Fatal("expected truncation")
|
||||
}
|
||||
if len(s) > 32100 {
|
||||
t.Fatalf("truncated string too long: %d", len(s))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdRunPipeFail(t *testing.T) {
|
||||
_, tc, err := setupPlugin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := tc.handlers["cmd_run"]
|
||||
result, err := handler(map[string]interface{}{
|
||||
"command": "false",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
data, _ := json.Marshal(result)
|
||||
var resp map[string]interface{}
|
||||
json.Unmarshal(data, &resp)
|
||||
|
||||
if resp["status"] != "ok" {
|
||||
t.Fatalf("expected status ok, got %v", resp["status"])
|
||||
}
|
||||
if resp["exit_code"].(float64) != 1 {
|
||||
t.Fatalf("expected exit code 1, got %v", resp["exit_code"])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user