| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "path/filepath" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/sylvinus/agent-vm/internal/paths" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | func (s *Server) browserStateForHost(body []byte) ([]byte, error) { |
| 21 | var state map[string]json.RawMessage |
| 22 | if err := json.Unmarshal(body, &state); err != nil { |
| 23 | return nil, errors.New("浏览器状态无效") |
| 24 | } |
| 25 | |
| 26 | |
| 27 | var downloads []map[string]json.RawMessage |
| 28 | downloadsChanged := false |
| 29 | if json.Unmarshal(state["downloads"], &downloads) == nil { |
| 30 | root := s.environmentDir() |
| 31 | for _, record := range downloads { |
| 32 | encoded, _ := json.Marshal(record) |
| 33 | var item browserDownloadInfo |
| 34 | if json.Unmarshal(encoded, &item) != nil { |
| 35 | continue |
| 36 | } |
| 37 | if s.macosGuest != nil || s.windowsGuest != nil { |
| 38 | item.Target = "browser" |
| 39 | mapped := s.nativeDownloadForHost(item) |
| 40 | record["name"], _ = json.Marshal(mapped.Name) |
| 41 | record["path"], _ = json.Marshal(mapped.Path) |
| 42 | record["state"], _ = json.Marshal(mapped.State) |
| 43 | record["retryable"], _ = json.Marshal(mapped.Retryable) |
| 44 | delete(record, "error") |
| 45 | if mapped.Error != "" { |
| 46 | record["error"], _ = json.Marshal(mapped.Error) |
| 47 | } |
| 48 | downloadsChanged = true |
| 49 | continue |
| 50 | } |
| 51 | if item.State != "saved" || !browserFileID.MatchString(item.ID) || !browserDownloadName(item.Name) || item.Path != guestPath(filepath.Join(root, "Downloads", item.Name)) { |
| 52 | continue |
| 53 | } |
| 54 | hostPath := filepath.Join(root, "Downloads", item.Name) |
| 55 | if _, err := readSavedDownload(s.profile, item.ID); err != nil { |
| 56 | _ = rememberDownload(s.profile, SavedDownload{ID: item.ID, Name: item.Name, Path: hostPath}, true) |
| 57 | } |
| 58 | if paths.Windows() && hostPath != item.Path { |
| 59 | record["path"], _ = json.Marshal(hostPath) |
| 60 | downloadsChanged = true |
| 61 | } |
| 62 | } |
| 63 | if s.macosGuest != nil || s.windowsGuest != nil { |
| 64 | visible := make(map[string]bool, len(downloads)) |
| 65 | for _, record := range downloads { |
| 66 | var id string |
| 67 | if json.Unmarshal(record["id"], &id) == nil { |
| 68 | visible[id] = true |
| 69 | } |
| 70 | } |
| 71 | s.discardMissingNativeDownloads("browser", visible) |
| 72 | } |
| 73 | } |
| 74 | if !paths.Windows() && s.windowsGuest == nil && s.macosGuest == nil { |
| 75 | return body, nil |
| 76 | } |
| 77 | var tabs []map[string]json.RawMessage |
| 78 | if err := json.Unmarshal(state["tabs"], &tabs); err != nil { |
| 79 | return nil, errors.New("浏览器标签状态无效") |
| 80 | } |
| 81 | authorized, err := s.projectPaths() |
| 82 | if err != nil { |
| 83 | return nil, errors.New("无法读取项目列表") |
| 84 | } |
| 85 | byGuest := make(map[string]string, len(authorized)) |
| 86 | for _, hostPath := range authorized { |
| 87 | if s.macosGuest != nil { |
| 88 | guestPath := macOSProjectPath(s.macosGuest.User, hostPath) |
| 89 | byGuest[guestPath] = hostPath |
| 90 | byGuest[filepath.Base(guestPath)] = hostPath |
| 91 | } else { |
| 92 | byGuest[guestPath(hostPath)] = hostPath |
| 93 | } |
| 94 | } |
| 95 | if s.windowsGuest != nil { |
| 96 | allowed := map[string]bool{} |
| 97 | for _, hostPath := range authorized { |
| 98 | allowed[hostPath] = true |
| 99 | } |
| 100 | s.mu.Lock() |
| 101 | for guestPath, hostPath := range s.windowsProjectHistory { |
| 102 | if allowed[hostPath] { |
| 103 | byGuest[guestPath] = hostPath |
| 104 | } |
| 105 | } |
| 106 | s.mu.Unlock() |
| 107 | } |
| 108 | changed := downloadsChanged |
| 109 | for _, tab := range tabs { |
| 110 | var kind, workspace string |
| 111 | if json.Unmarshal(tab["kind"], &kind) != nil || kind != "editor" || json.Unmarshal(tab["workspace"], &workspace) != nil { |
| 112 | continue |
| 113 | } |
| 114 | if hostPath, ok := byGuest[workspace]; ok && hostPath != workspace { |
| 115 | tab["workspace"], _ = json.Marshal(hostPath) |
| 116 | changed = true |
| 117 | } |
| 118 | } |
| 119 | if !changed { |
| 120 | return body, nil |
| 121 | } |
| 122 | if downloadsChanged { |
| 123 | state["downloads"], _ = json.Marshal(downloads) |
| 124 | } |
| 125 | state["tabs"], err = json.Marshal(tabs) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return json.Marshal(state) |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | func (s *Server) writeBrowserEvents(ctx context.Context, w http.ResponseWriter, body io.Reader) error { |
| 136 | scanner := bufio.NewScanner(body) |
| 137 | scanner.Buffer(make([]byte, 16<<10), (1<<20)+16) |
| 138 | controller := http.NewResponseController(w) |
| 139 | eventName := "" |
| 140 | for scanner.Scan() { |
| 141 | if err := ctx.Err(); err != nil { |
| 142 | return err |
| 143 | } |
| 144 | line := scanner.Bytes() |
| 145 | if bytes.HasPrefix(line, []byte("event: ")) { |
| 146 | eventName = string(line[len("event: "):]) |
| 147 | } |
| 148 | |
| 149 | |
| 150 | if bytes.HasPrefix(line, []byte("data: ")) && eventName == "" { |
| 151 | mapped, err := s.browserStateForHost(line[len("data: "):]) |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | line = append([]byte("data: "), mapped...) |
| 156 | } |
| 157 | if len(line) > browserResponseLimit+len("data: ") { |
| 158 | return errors.New("浏览器事件超出大小限制") |
| 159 | } |
| 160 | _ = controller.SetWriteDeadline(time.Now().Add(10 * time.Second)) |
| 161 | if err := ctx.Err(); err != nil { |
| 162 | return err |
| 163 | } |
| 164 | if _, err := w.Write(append(line, '\n')); err != nil { |
| 165 | return err |
| 166 | } |
| 167 | if len(line) == 0 { |
| 168 | eventName = "" |
| 169 | if err := controller.Flush(); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return scanner.Err() |
| 175 | } |