handleStartProcess starts a new process.
(rw http.ResponseWriter, r *http.Request)
| 62 | |
| 63 | // handleStartProcess starts a new process. |
| 64 | func (api *API) handleStartProcess(rw http.ResponseWriter, r *http.Request) { |
| 65 | ctx := r.Context() |
| 66 | |
| 67 | var req workspacesdk.StartProcessRequest |
| 68 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 69 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 70 | Message: "Request body must be valid JSON.", |
| 71 | Detail: err.Error(), |
| 72 | }) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | if req.Command == "" { |
| 77 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 78 | Message: "Command is required.", |
| 79 | }) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | var chatID string |
| 84 | if chatContext, ok := agentchat.FromContext(ctx); ok { |
| 85 | chatID = chatContext.ID.String() |
| 86 | } |
| 87 | |
| 88 | proc, err := api.manager.start(req, chatID) |
| 89 | if err != nil { |
| 90 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 91 | Message: "Failed to start process.", |
| 92 | Detail: err.Error(), |
| 93 | }) |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | // Notify git watchers after the process finishes so that |
| 98 | // file changes made by the command are visible in the scan. |
| 99 | // If a workdir is provided, track it as a path as well. |
| 100 | if api.pathStore != nil { |
| 101 | if chatContext, ok := agentchat.FromContext(ctx); ok { |
| 102 | allIDs := append([]uuid.UUID{chatContext.ID}, chatContext.AncestorIDs...) |
| 103 | go func() { |
| 104 | <-proc.done |
| 105 | if req.WorkDir != "" { |
| 106 | api.pathStore.AddPaths(allIDs, []string{req.WorkDir}) |
| 107 | } else { |
| 108 | api.pathStore.Notify(allIDs) |
| 109 | } |
| 110 | }() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | httpapi.Write(ctx, rw, http.StatusOK, workspacesdk.StartProcessResponse{ |
| 115 | ID: proc.id, |
| 116 | Started: true, |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | // handleListProcesses lists all tracked processes. |
| 121 | func (api *API) handleListProcesses(rw http.ResponseWriter, r *http.Request) { |