1package server
2
3import (
4 "encoding/json"
5 "net/http"
6
7 "github.com/charmbracelet/crush/internal/proto"
8)
9
10// handlePostWorkspaceConfigSet sets a configuration field.
11//
12// @Summary Set a config field
13// @Tags config
14// @Accept json
15// @Param id path string true "Workspace ID"
16// @Param request body proto.ConfigSetRequest true "Config set request"
17// @Success 200
18// @Failure 400 {object} proto.Error
19// @Failure 404 {object} proto.Error
20// @Failure 500 {object} proto.Error
21// @Router /workspaces/{id}/config/set [post]
22func (c *controllerV1) handlePostWorkspaceConfigSet(w http.ResponseWriter, r *http.Request) {
23 id := r.PathValue("id")
24
25 var req proto.ConfigSetRequest
26 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
27 c.server.logError(r, "Failed to decode request", "error", err)
28 jsonError(w, http.StatusBadRequest, "failed to decode request")
29 return
30 }
31
32 if err := c.backend.SetConfigField(id, req.Scope, req.Key, req.Value); err != nil {
33 c.handleError(w, r, err)
34 return
35 }
36 w.WriteHeader(http.StatusOK)
37}
38
39// handlePostWorkspaceConfigRemove removes a configuration field.
40//
41// @Summary Remove a config field
42// @Tags config
43// @Accept json
44// @Param id path string true "Workspace ID"
45// @Param request body proto.ConfigRemoveRequest true "Config remove request"
46// @Success 200
47// @Failure 400 {object} proto.Error
48// @Failure 404 {object} proto.Error
49// @Failure 500 {object} proto.Error
50// @Router /workspaces/{id}/config/remove [post]
51func (c *controllerV1) handlePostWorkspaceConfigRemove(w http.ResponseWriter, r *http.Request) {
52 id := r.PathValue("id")
53
54 var req proto.ConfigRemoveRequest
55 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
56 c.server.logError(r, "Failed to decode request", "error", err)
57 jsonError(w, http.StatusBadRequest, "failed to decode request")
58 return
59 }
60
61 if err := c.backend.RemoveConfigField(id, req.Scope, req.Key); err != nil {
62 c.handleError(w, r, err)
63 return
64 }
65 w.WriteHeader(http.StatusOK)
66}
67
68// handlePostWorkspaceConfigModel updates the preferred model.
69//
70// @Summary Set the preferred model
71// @Tags config
72// @Accept json
73// @Param id path string true "Workspace ID"
74// @Param request body proto.ConfigModelRequest true "Config model request"
75// @Success 200
76// @Failure 400 {object} proto.Error
77// @Failure 404 {object} proto.Error
78// @Failure 500 {object} proto.Error
79// @Router /workspaces/{id}/config/model [post]
80func (c *controllerV1) handlePostWorkspaceConfigModel(w http.ResponseWriter, r *http.Request) {
81 id := r.PathValue("id")
82
83 var req proto.ConfigModelRequest
84 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
85 c.server.logError(r, "Failed to decode request", "error", err)
86 jsonError(w, http.StatusBadRequest, "failed to decode request")
87 return
88 }
89
90 if err := c.backend.UpdatePreferredModel(id, req.Scope, req.ModelType, req.Model); err != nil {
91 c.handleError(w, r, err)
92 return
93 }
94 w.WriteHeader(http.StatusOK)
95}
96
97// handlePostWorkspaceConfigCompact sets compact mode.
98//
99// @Summary Set compact mode
100// @Tags config
101// @Accept json
102// @Param id path string true "Workspace ID"
103// @Param request body proto.ConfigCompactRequest true "Config compact request"
104// @Success 200
105// @Failure 400 {object} proto.Error
106// @Failure 404 {object} proto.Error
107// @Failure 500 {object} proto.Error
108// @Router /workspaces/{id}/config/compact [post]
109func (c *controllerV1) handlePostWorkspaceConfigCompact(w http.ResponseWriter, r *http.Request) {
110 id := r.PathValue("id")
111
112 var req proto.ConfigCompactRequest
113 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
114 c.server.logError(r, "Failed to decode request", "error", err)
115 jsonError(w, http.StatusBadRequest, "failed to decode request")
116 return
117 }
118
119 if err := c.backend.SetCompactMode(id, req.Scope, req.Enabled); err != nil {
120 c.handleError(w, r, err)
121 return
122 }
123 w.WriteHeader(http.StatusOK)
124}
125
126// handlePostWorkspaceConfigProviderKey sets a provider API key.
127//
128// @Summary Set provider API key
129// @Tags config
130// @Accept json
131// @Param id path string true "Workspace ID"
132// @Param request body proto.ConfigProviderKeyRequest true "Config provider key request"
133// @Success 200
134// @Failure 400 {object} proto.Error
135// @Failure 404 {object} proto.Error
136// @Failure 500 {object} proto.Error
137// @Router /workspaces/{id}/config/provider-key [post]
138func (c *controllerV1) handlePostWorkspaceConfigProviderKey(w http.ResponseWriter, r *http.Request) {
139 id := r.PathValue("id")
140
141 var req proto.ConfigProviderKeyRequest
142 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
143 c.server.logError(r, "Failed to decode request", "error", err)
144 jsonError(w, http.StatusBadRequest, "failed to decode request")
145 return
146 }
147
148 apiKey, err := req.DecodeAPIKey()
149 if err != nil {
150 c.server.logError(r, "Failed to decode api key", "error", err, "kind", req.Kind)
151 jsonError(w, http.StatusBadRequest, err.Error())
152 return
153 }
154
155 if err := c.backend.SetProviderAPIKey(id, req.Scope, req.ProviderID, apiKey); err != nil {
156 c.handleError(w, r, err)
157 return
158 }
159 w.WriteHeader(http.StatusOK)
160}
161
162// handlePostWorkspaceConfigImportCopilot imports Copilot credentials.
163//
164// @Summary Import Copilot credentials
165// @Tags config
166// @Produce json
167// @Param id path string true "Workspace ID"
168// @Success 200 {object} proto.ImportCopilotResponse
169// @Failure 404 {object} proto.Error
170// @Failure 500 {object} proto.Error
171// @Router /workspaces/{id}/config/import-copilot [post]
172func (c *controllerV1) handlePostWorkspaceConfigImportCopilot(w http.ResponseWriter, r *http.Request) {
173 id := r.PathValue("id")
174 token, ok, err := c.backend.ImportCopilot(id)
175 if err != nil {
176 c.handleError(w, r, err)
177 return
178 }
179 jsonEncode(w, proto.ImportCopilotResponse{Token: token, Success: ok})
180}
181
182// handlePostWorkspaceConfigRefreshOAuth refreshes an OAuth token for a provider.
183//
184// @Summary Refresh OAuth token
185// @Tags config
186// @Accept json
187// @Param id path string true "Workspace ID"
188// @Param request body proto.ConfigRefreshOAuthRequest true "Refresh OAuth request"
189// @Success 200
190// @Failure 400 {object} proto.Error
191// @Failure 404 {object} proto.Error
192// @Failure 500 {object} proto.Error
193// @Router /workspaces/{id}/config/refresh-oauth [post]
194func (c *controllerV1) handlePostWorkspaceConfigRefreshOAuth(w http.ResponseWriter, r *http.Request) {
195 id := r.PathValue("id")
196
197 var req proto.ConfigRefreshOAuthRequest
198 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
199 c.server.logError(r, "Failed to decode request", "error", err)
200 jsonError(w, http.StatusBadRequest, "failed to decode request")
201 return
202 }
203
204 if err := c.backend.RefreshOAuthToken(r.Context(), id, req.Scope, req.ProviderID); err != nil {
205 c.handleError(w, r, err)
206 return
207 }
208 w.WriteHeader(http.StatusOK)
209}
210
211// handleGetWorkspaceProjectNeedsInit reports whether a project needs initialization.
212//
213// @Summary Check if project needs initialization
214// @Tags project
215// @Produce json
216// @Param id path string true "Workspace ID"
217// @Success 200 {object} proto.ProjectNeedsInitResponse
218// @Failure 404 {object} proto.Error
219// @Failure 500 {object} proto.Error
220// @Router /workspaces/{id}/project/needs-init [get]
221func (c *controllerV1) handleGetWorkspaceProjectNeedsInit(w http.ResponseWriter, r *http.Request) {
222 id := r.PathValue("id")
223 needs, err := c.backend.ProjectNeedsInitialization(id)
224 if err != nil {
225 c.handleError(w, r, err)
226 return
227 }
228 jsonEncode(w, proto.ProjectNeedsInitResponse{NeedsInit: needs})
229}
230
231// handlePostWorkspaceProjectInit marks the project as initialized.
232//
233// @Summary Mark project as initialized
234// @Tags project
235// @Param id path string true "Workspace ID"
236// @Success 200
237// @Failure 404 {object} proto.Error
238// @Failure 500 {object} proto.Error
239// @Router /workspaces/{id}/project/init [post]
240func (c *controllerV1) handlePostWorkspaceProjectInit(w http.ResponseWriter, r *http.Request) {
241 id := r.PathValue("id")
242 if err := c.backend.MarkProjectInitialized(id); err != nil {
243 c.handleError(w, r, err)
244 return
245 }
246 w.WriteHeader(http.StatusOK)
247}
248
249// handleGetWorkspaceProjectInitPrompt returns the project initialization prompt.
250//
251// @Summary Get project initialization prompt
252// @Tags project
253// @Produce json
254// @Param id path string true "Workspace ID"
255// @Success 200 {object} proto.ProjectInitPromptResponse
256// @Failure 404 {object} proto.Error
257// @Failure 500 {object} proto.Error
258// @Router /workspaces/{id}/project/init-prompt [get]
259func (c *controllerV1) handleGetWorkspaceProjectInitPrompt(w http.ResponseWriter, r *http.Request) {
260 id := r.PathValue("id")
261 prompt, err := c.backend.InitializePrompt(id)
262 if err != nil {
263 c.handleError(w, r, err)
264 return
265 }
266 jsonEncode(w, proto.ProjectInitPromptResponse{Prompt: prompt})
267}
268
269// handlePostWorkspaceMCPEnableDocker enables the Docker MCP server.
270//
271// @Summary Enable Docker MCP
272// @Tags mcp
273// @Param id path string true "Workspace ID"
274// @Success 200
275// @Failure 404 {object} proto.Error
276// @Failure 500 {object} proto.Error
277// @Router /workspaces/{id}/mcp/docker/enable [post]
278func (c *controllerV1) handlePostWorkspaceMCPEnableDocker(w http.ResponseWriter, r *http.Request) {
279 id := r.PathValue("id")
280 if err := c.backend.EnableDockerMCP(r.Context(), id); err != nil {
281 c.handleError(w, r, err)
282 return
283 }
284 w.WriteHeader(http.StatusOK)
285}
286
287// handlePostWorkspaceMCPDisableDocker disables the Docker MCP server.
288//
289// @Summary Disable Docker MCP
290// @Tags mcp
291// @Param id path string true "Workspace ID"
292// @Success 200
293// @Failure 404 {object} proto.Error
294// @Failure 500 {object} proto.Error
295// @Router /workspaces/{id}/mcp/docker/disable [post]
296func (c *controllerV1) handlePostWorkspaceMCPDisableDocker(w http.ResponseWriter, r *http.Request) {
297 id := r.PathValue("id")
298 if err := c.backend.DisableDockerMCP(id); err != nil {
299 c.handleError(w, r, err)
300 return
301 }
302 w.WriteHeader(http.StatusOK)
303}
304
305// handlePostWorkspaceMCPRefreshTools refreshes tools for a named MCP server.
306//
307// @Summary Refresh MCP tools
308// @Tags mcp
309// @Accept json
310// @Param id path string true "Workspace ID"
311// @Param request body proto.MCPNameRequest true "MCP name request"
312// @Success 200
313// @Failure 400 {object} proto.Error
314// @Failure 404 {object} proto.Error
315// @Failure 500 {object} proto.Error
316// @Router /workspaces/{id}/mcp/refresh-tools [post]
317func (c *controllerV1) handlePostWorkspaceMCPRefreshTools(w http.ResponseWriter, r *http.Request) {
318 id := r.PathValue("id")
319
320 var req proto.MCPNameRequest
321 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
322 c.server.logError(r, "Failed to decode request", "error", err)
323 jsonError(w, http.StatusBadRequest, "failed to decode request")
324 return
325 }
326
327 if err := c.backend.RefreshMCPTools(r.Context(), id, req.Name); err != nil {
328 c.handleError(w, r, err)
329 return
330 }
331 w.WriteHeader(http.StatusOK)
332}
333
334// handlePostWorkspaceMCPReadResource reads a resource from an MCP server.
335//
336// @Summary Read MCP resource
337// @Tags mcp
338// @Accept json
339// @Produce json
340// @Param id path string true "Workspace ID"
341// @Param request body proto.MCPReadResourceRequest true "MCP read resource request"
342// @Success 200 {object} object
343// @Failure 400 {object} proto.Error
344// @Failure 404 {object} proto.Error
345// @Failure 500 {object} proto.Error
346// @Router /workspaces/{id}/mcp/read-resource [post]
347func (c *controllerV1) handlePostWorkspaceMCPReadResource(w http.ResponseWriter, r *http.Request) {
348 id := r.PathValue("id")
349
350 var req proto.MCPReadResourceRequest
351 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
352 c.server.logError(r, "Failed to decode request", "error", err)
353 jsonError(w, http.StatusBadRequest, "failed to decode request")
354 return
355 }
356
357 contents, err := c.backend.ReadMCPResource(r.Context(), id, req.Name, req.URI)
358 if err != nil {
359 c.handleError(w, r, err)
360 return
361 }
362 jsonEncode(w, contents)
363}
364
365// handlePostWorkspaceMCPGetPrompt retrieves a prompt from an MCP server.
366//
367// @Summary Get MCP prompt
368// @Tags mcp
369// @Accept json
370// @Produce json
371// @Param id path string true "Workspace ID"
372// @Param request body proto.MCPGetPromptRequest true "MCP get prompt request"
373// @Success 200 {object} proto.MCPGetPromptResponse
374// @Failure 400 {object} proto.Error
375// @Failure 404 {object} proto.Error
376// @Failure 500 {object} proto.Error
377// @Router /workspaces/{id}/mcp/get-prompt [post]
378func (c *controllerV1) handlePostWorkspaceMCPGetPrompt(w http.ResponseWriter, r *http.Request) {
379 id := r.PathValue("id")
380
381 var req proto.MCPGetPromptRequest
382 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
383 c.server.logError(r, "Failed to decode request", "error", err)
384 jsonError(w, http.StatusBadRequest, "failed to decode request")
385 return
386 }
387
388 prompt, err := c.backend.GetMCPPrompt(id, req.ClientID, req.PromptID, req.Args)
389 if err != nil {
390 c.handleError(w, r, err)
391 return
392 }
393 jsonEncode(w, proto.MCPGetPromptResponse{Prompt: prompt})
394}
395
396// handleGetWorkspaceMCPStates returns the state of all MCP clients.
397//
398// @Summary Get MCP client states
399// @Tags mcp
400// @Produce json
401// @Param id path string true "Workspace ID"
402// @Success 200 {object} map[string]proto.MCPClientInfo
403// @Failure 404 {object} proto.Error
404// @Failure 500 {object} proto.Error
405// @Router /workspaces/{id}/mcp/states [get]
406func (c *controllerV1) handleGetWorkspaceMCPStates(w http.ResponseWriter, r *http.Request) {
407 id := r.PathValue("id")
408 states := c.backend.MCPGetStates(id)
409 result := make(map[string]proto.MCPClientInfo, len(states))
410 for k, v := range states {
411 result[k] = proto.MCPClientInfo{
412 Name: v.Name,
413 State: proto.MCPState(v.State),
414 Error: v.Error,
415 ToolCount: v.Counts.Tools,
416 PromptCount: v.Counts.Prompts,
417 ResourceCount: v.Counts.Resources,
418 ConnectedAt: v.ConnectedAt,
419 }
420 }
421 jsonEncode(w, result)
422}
423
424// handlePostWorkspaceMCPRefreshPrompts refreshes prompts for a named MCP server.
425//
426// @Summary Refresh MCP prompts
427// @Tags mcp
428// @Accept json
429// @Param id path string true "Workspace ID"
430// @Param request body proto.MCPNameRequest true "MCP name request"
431// @Success 200
432// @Failure 400 {object} proto.Error
433// @Failure 404 {object} proto.Error
434// @Failure 500 {object} proto.Error
435// @Router /workspaces/{id}/mcp/refresh-prompts [post]
436func (c *controllerV1) handlePostWorkspaceMCPRefreshPrompts(w http.ResponseWriter, r *http.Request) {
437 id := r.PathValue("id")
438
439 var req proto.MCPNameRequest
440 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
441 c.server.logError(r, "Failed to decode request", "error", err)
442 jsonError(w, http.StatusBadRequest, "failed to decode request")
443 return
444 }
445
446 c.backend.MCPRefreshPrompts(r.Context(), id, req.Name)
447 w.WriteHeader(http.StatusOK)
448}
449
450// handlePostWorkspaceMCPRefreshResources refreshes resources for a named MCP server.
451//
452// @Summary Refresh MCP resources
453// @Tags mcp
454// @Accept json
455// @Param id path string true "Workspace ID"
456// @Param request body proto.MCPNameRequest true "MCP name request"
457// @Success 200
458// @Failure 400 {object} proto.Error
459// @Failure 404 {object} proto.Error
460// @Failure 500 {object} proto.Error
461// @Router /workspaces/{id}/mcp/refresh-resources [post]
462func (c *controllerV1) handlePostWorkspaceMCPRefreshResources(w http.ResponseWriter, r *http.Request) {
463 id := r.PathValue("id")
464
465 var req proto.MCPNameRequest
466 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
467 c.server.logError(r, "Failed to decode request", "error", err)
468 jsonError(w, http.StatusBadRequest, "failed to decode request")
469 return
470 }
471
472 c.backend.MCPRefreshResources(r.Context(), id, req.Name)
473 w.WriteHeader(http.StatusOK)
474}