MCPcopy Index your code
hub / github.com/coder/coder / handleAction

Method handleAction

agent/x/agentdesktop/api.go:131–531  ·  view source on GitHub ↗
(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

129}
130
131func (a *API) handleAction(rw http.ResponseWriter, r *http.Request) {
132 ctx := r.Context()
133 logger := a.logger.With(agentchat.Fields(ctx)...)
134 handlerStart := a.clock.Now()
135
136 // Update last desktop action timestamp for idle recording monitor.
137 a.desktop.RecordActivity()
138
139 // Ensure the desktop is running and grab native dimensions.
140 cfg, err := a.desktop.Start(ctx)
141 if err != nil {
142 logger.Warn(ctx, "handleAction: desktop.Start failed",
143 slog.Error(err),
144 slog.F("elapsed_ms", a.clock.Since(handlerStart).Milliseconds()),
145 )
146 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
147 Message: "Failed to start desktop session.",
148 Detail: err.Error(),
149 })
150 return
151 }
152
153 var action DesktopAction
154 if err := json.NewDecoder(r.Body).Decode(&action); err != nil {
155 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
156 Message: "Failed to decode request body.",
157 Detail: err.Error(),
158 })
159 return
160 }
161
162 logger.Info(ctx, "handleAction: started",
163 slog.F("action", action.Action),
164 slog.F("elapsed_ms", a.clock.Since(handlerStart).Milliseconds()),
165 )
166
167 geometry := desktopGeometryForAction(cfg, action)
168 scaleXY := geometry.DeclaredPointToNative
169
170 var resp DesktopActionResponse
171
172 switch action.Action {
173 case "key":
174 if action.Text == nil {
175 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
176 Message: "Missing \"text\" for key action.",
177 })
178 return
179 }
180 if err := a.desktop.KeyPress(ctx, *action.Text); err != nil {
181 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
182 Message: "Key press failed.",
183 Detail: err.Error(),
184 })
185 return
186 }
187 resp.Output = "key action performed"
188

Callers

nothing calls this directly

Calls 15

FieldsFunction · 0.92
WriteFunction · 0.92
desktopGeometryForActionFunction · 0.85
coordFromActionFunction · 0.85
NativePointToDeclaredMethod · 0.80
DurationMethod · 0.80
ErrMethod · 0.80
ContextMethod · 0.65
RecordActivityMethod · 0.65
StartMethod · 0.65
KeyPressMethod · 0.65
KeyDownMethod · 0.65

Tested by

no test coverage detected