| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | "os" |
| 12 | "os/exec" |
| 13 | "os/signal" |
| 14 | "path/filepath" |
| 15 | "syscall" |
| 16 | |
| 17 | "github.com/sylvinus/agent-vm/internal/agentcli" |
| 18 | "github.com/sylvinus/agent-vm/internal/cage" |
| 19 | "github.com/sylvinus/agent-vm/internal/diagnostics" |
| 20 | "github.com/sylvinus/agent-vm/internal/host" |
| 21 | "github.com/sylvinus/agent-vm/internal/version" |
| 22 | "github.com/sylvinus/agent-vm/internal/workbench" |
| 23 | ) |
| 24 | |
| 25 | func main() { |
| 26 | if len(os.Args) > 1 && os.Args[1] == "agent" { |
| 27 | os.Exit(agentcli.ClientMain(os.Args[2:], os.Stdin, os.Stdout)) |
| 28 | } |
| 29 | if err := run(os.Args[1:]); err != nil { |
| 30 | diagnostics.Log("error", "client", err.Error()) |
| 31 | _ = json.NewEncoder(os.Stdout).Encode(map[string]string{"status": "error", "message": err.Error()}) |
| 32 | os.Exit(1) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func run(args []string) error { |
| 37 | if len(args) == 0 || args[0] == "help" || args[0] == "--help" { |
| 38 | fmt.Println("Cage remote client " + version.Version + "\nCommands: desktop --json, serve, prepare-update. Workloads run on the selected remote host.") |
| 39 | return nil |
| 40 | } |
| 41 | if args[0] == "verify-web-assets" { |
| 42 | if len(args) != 1 { |
| 43 | return errors.New("verify-web-assets does not accept arguments") |
| 44 | } |
| 45 | return workbench.VerifyWebAssets() |
| 46 | } |
| 47 | config := os.Getenv(cage.ConfigEnv) |
| 48 | if config == "" { |
| 49 | home, err := os.UserHomeDir() |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | config = filepath.Join(home, ".claude-cage", "client", "config.json") |
| 54 | } |
| 55 | config, err := filepath.Abs(config) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | diagnostics.SetDefault(diagnostics.New(filepath.Dir(config))) |
| 60 | printJSON := func(value any, err error) error { |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | return json.NewEncoder(os.Stdout).Encode(value) |
| 65 | } |
| 66 | switch args[0] { |
| 67 | case "serve": |
| 68 | return workbench.ServeClient(config) |
| 69 | case "log-relay": |
| 70 | if len(args) != 2 || args[1] != "controller" { |
| 71 | return errors.New("unsupported client service") |
| 72 | } |
| 73 | return relay(config) |
| 74 | case "desktop": |
| 75 | options := workbench.DesktopOptions{} |
| 76 | for _, argument := range args[1:] { |
| 77 | switch argument { |
| 78 | case "--json": |
| 79 | case "--existing": |
| 80 | options.UseExisting = true |
| 81 | case "--complete-update": |
| 82 | options.CompleteUpdate = true |
| 83 | default: |
| 84 | return errors.New("unsupported desktop option") |
| 85 | } |
| 86 | } |
| 87 | return printJSON(workbench.Desktop(config, options)) |
| 88 | case "prepare-update": |
| 89 | stop := false |
| 90 | for _, argument := range args[1:] { |
| 91 | switch argument { |
| 92 | case "--json": |
| 93 | case "--stop": |
| 94 | stop = true |
| 95 | default: |
| 96 | return errors.New("unsupported update option") |
| 97 | } |
| 98 | } |
| 99 | return printJSON(workbench.PrepareUpdate(config, stop)) |
| 100 | case "diagnostic-event": |
| 101 | var event struct { |
| 102 | Source string `json:"source"` |
| 103 | Message string `json:"message"` |
| 104 | } |
| 105 | if json.NewDecoder(io.LimitReader(os.Stdin, 32<<10)).Decode(&event) != nil || event.Source != "native" { |
| 106 | return errors.New("invalid diagnostic event") |
| 107 | } |
| 108 | return diagnostics.New(filepath.Dir(config)).Record("error", event.Source, event.Message) |
| 109 | case "diagnostics": |
| 110 | connection := "" |
| 111 | if len(args) == 5 && args[1] == "--json" && args[2] == "--connection" { |
| 112 | connection = args[3] |
| 113 | args = []string{args[0], args[1], args[4]} |
| 114 | } |
| 115 | if len(args) != 3 || args[1] != "--json" { |
| 116 | return errors.New("diagnostics --json /absolute/path.zip") |
| 117 | } |
| 118 | if err := workbench.ExportDiagnosticsForConnection(config, connection, args[2]); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | return printJSON(map[string]string{"status": "ok"}, nil) |
| 122 | case "share-project": |
| 123 | if len(args) != 6 || args[1] != "--json" || args[2] != "--connection" || args[3] == "" || (args[4] != "read" && args[4] != "write") { |
| 124 | return errors.New("share-project --json --connection ID read|write DIRECTORY") |
| 125 | } |
| 126 | return printJSON(workbench.ShareNativeCloudProject(config, args[3], args[5], args[4] == "read")) |
| 127 | case "desktop-file": |
| 128 | if len(args) != 5 || args[1] != "--json" || args[2] != "--connection" || args[3] == "" { |
| 129 | return errors.New("desktop-file --json --connection ID REQUEST") |
| 130 | } |
| 131 | return printJSON(workbench.DesktopFileForConnection(config, args[3], args[4])) |
| 132 | case "clipboard-files", "upload-files", "browser-file": |
| 133 | if len(args) < 4 || args[1] != "--json" { |
| 134 | return errors.New("invalid file command") |
| 135 | } |
| 136 | connection := "" |
| 137 | if args[2] == "--connection" { |
| 138 | if len(args) < 6 || args[3] == "" { |
| 139 | return errors.New("missing connection identity") |
| 140 | } |
| 141 | connection = args[3] |
| 142 | args = append(append([]string{}, args[:2]...), args[4:]...) |
| 143 | } |
| 144 | if args[0] == "clipboard-files" { |
| 145 | if len(args) < 4 { |
| 146 | return errors.New("clipboard-files --json --connection ID send|receive PATH...") |
| 147 | } |
| 148 | return printJSON(workbench.ClipboardFilesForConnection(config, connection, args[2], args[3:])) |
| 149 | } |
| 150 | if args[0] == "upload-files" { |
| 151 | if len(args) < 5 { |
| 152 | return errors.New("no selected files") |
| 153 | } |
| 154 | return printJSON(workbench.UploadHostFilesForConnection(config, connection, args[2], args[3], args[4:])) |
| 155 | } |
| 156 | if len(args) < 4 || len(args) > 5 { |
| 157 | return errors.New("invalid download command") |
| 158 | } |
| 159 | destination := "" |
| 160 | if len(args) == 5 { |
| 161 | destination = args[4] |
| 162 | } |
| 163 | return printJSON(workbench.BrowserFileForConnection(config, connection, args[2], args[3], destination)) |
| 164 | case "app-update": |
| 165 | return printJSON(map[string]any{"phase": "manual", "currentVersion": version.Version, "message": "轻量客户端使用独立安装包更新;连接配置会保留,云端任务不受影响。"}, nil) |
| 166 | default: |
| 167 | return errors.New("轻量客户端只负责连接;请在远程工作台管理运行环境") |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func relay(config string) error { |
| 172 | executable, err := os.Executable() |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | cmd := exec.Command(executable, "serve") |
| 177 | cmd.Env = append(os.Environ(), cage.ConfigEnv+"="+config) |
| 178 | store := diagnostics.New(filepath.Dir(config)) |
| 179 | out, stderr := store.Stream("client", "info"), store.Stream("client", "error") |
| 180 | defer out.Close() |
| 181 | defer stderr.Close() |
| 182 | cmd.Stdout, cmd.Stderr = out, stderr |
| 183 | host.HideCommandWindow(cmd) |
| 184 | if err := cmd.Start(); err != nil { |
| 185 | return err |
| 186 | } |
| 187 | signals := make(chan os.Signal, 1) |
| 188 | signal.Notify(signals, os.Interrupt, syscall.SIGTERM) |
| 189 | defer signal.Stop(signals) |
| 190 | done := make(chan struct{}) |
| 191 | defer close(done) |
| 192 | go func() { |
| 193 | select { |
| 194 | case sig := <-signals: |
| 195 | _ = cmd.Process.Signal(sig) |
| 196 | case <-done: |
| 197 | } |
| 198 | }() |
| 199 | return cmd.Wait() |
| 200 | } |