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 if err := c.backend.SetProviderAPIKey(id, req.Scope, req.ProviderID, req.APIKey); err != nil {
149 c.handleError(w, r, err)
150 return
151 }
152 w.WriteHeader(http.StatusOK)
153}
154
155// handlePostWorkspaceConfigImportCopilot imports Copilot credentials.
156//
157// @Summary Import Copilot credentials
158// @Tags config
159// @Produce json
160// @Param id path string true "Workspace ID"
161// @Success 200 {object} proto.ImportCopilotResponse
162// @Failure 404 {object} proto.Error
163// @Failure 500 {object} proto.Error
164// @Router /workspaces/{id}/config/import-copilot [post]
165func (c *controllerV1) handlePostWorkspaceConfigImportCopilot(w http.ResponseWriter, r *http.Request) {
166 id := r.PathValue("id")
167 token, ok, err := c.backend.ImportCopilot(id)
168 if err != nil {
169 c.handleError(w, r, err)
170 return
171 }
172 jsonEncode(w, proto.ImportCopilotResponse{Token: token, Success: ok})
173}
174
175// handlePostWorkspaceConfigRefreshOAuth refreshes an OAuth token for a provider.
176//
177// @Summary Refresh OAuth token
178// @Tags config
179// @Accept json
180// @Param id path string true "Workspace ID"
181// @Param request body proto.ConfigRefreshOAuthRequest true "Refresh OAuth request"
182// @Success 200
183// @Failure 400 {object} proto.Error
184// @Failure 404 {object} proto.Error
185// @Failure 500 {object} proto.Error
186// @Router /workspaces/{id}/config/refresh-oauth [post]
187func (c *controllerV1) handlePostWorkspaceConfigRefreshOAuth(w http.ResponseWriter, r *http.Request) {
188 id := r.PathValue("id")
189
190 var req proto.ConfigRefreshOAuthRequest
191 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
192 c.server.logError(r, "Failed to decode request", "error", err)
193 jsonError(w, http.StatusBadRequest, "failed to decode request")
194 return
195 }
196
197 if err := c.backend.RefreshOAuthToken(r.Context(), id, req.Scope, req.ProviderID); err != nil {
198 c.handleError(w, r, err)
199 return
200 }
201 w.WriteHeader(http.StatusOK)
202}
203
204// handleGetWorkspaceProjectNeedsInit reports whether a project needs initialization.
205//
206// @Summary Check if project needs initialization
207// @Tags project
208// @Produce json
209// @Param id path string true "Workspace ID"
210// @Success 200 {object} proto.ProjectNeedsInitResponse
211// @Failure 404 {object} proto.Error
212// @Failure 500 {object} proto.Error
213// @Router /workspaces/{id}/project/needs-init [get]
214func (c *controllerV1) handleGetWorkspaceProjectNeedsInit(w http.ResponseWriter, r *http.Request) {
215 id := r.PathValue("id")
216 needs, err := c.backend.ProjectNeedsInitialization(id)
217 if err != nil {
218 c.handleError(w, r, err)
219 return
220 }
221 jsonEncode(w, proto.ProjectNeedsInitResponse{NeedsInit: needs})
222}
223
224// handlePostWorkspaceProjectInit marks the project as initialized.
225//
226// @Summary Mark project as initialized
227// @Tags project
228// @Param id path string true "Workspace ID"
229// @Success 200
230// @Failure 404 {object} proto.Error
231// @Failure 500 {object} proto.Error
232// @Router /workspaces/{id}/project/init [post]
233func (c *controllerV1) handlePostWorkspaceProjectInit(w http.ResponseWriter, r *http.Request) {
234 id := r.PathValue("id")
235 if err := c.backend.MarkProjectInitialized(id); err != nil {
236 c.handleError(w, r, err)
237 return
238 }
239 w.WriteHeader(http.StatusOK)
240}
241
242// handleGetWorkspaceProjectInitPrompt returns the project initialization prompt.
243//
244// @Summary Get project initialization prompt
245// @Tags project
246// @Produce json
247// @Param id path string true "Workspace ID"
248// @Success 200 {object} proto.ProjectInitPromptResponse
249// @Failure 404 {object} proto.Error
250// @Failure 500 {object} proto.Error
251// @Router /workspaces/{id}/project/init-prompt [get]
252func (c *controllerV1) handleGetWorkspaceProjectInitPrompt(w http.ResponseWriter, r *http.Request) {
253 id := r.PathValue("id")
254 prompt, err := c.backend.InitializePrompt(id)
255 if err != nil {
256 c.handleError(w, r, err)
257 return
258 }
259 jsonEncode(w, proto.ProjectInitPromptResponse{Prompt: prompt})
260}
261
262// handlePostWorkspaceMCPRefreshTools refreshes tools for a named MCP server.
263//
264// @Summary Refresh MCP tools
265// @Tags mcp
266// @Accept json
267// @Param id path string true "Workspace ID"
268// @Param request body proto.MCPNameRequest true "MCP name request"
269// @Success 200
270// @Failure 400 {object} proto.Error
271// @Failure 404 {object} proto.Error
272// @Failure 500 {object} proto.Error
273// @Router /workspaces/{id}/mcp/refresh-tools [post]
274func (c *controllerV1) handlePostWorkspaceMCPRefreshTools(w http.ResponseWriter, r *http.Request) {
275 id := r.PathValue("id")
276
277 var req proto.MCPNameRequest
278 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
279 c.server.logError(r, "Failed to decode request", "error", err)
280 jsonError(w, http.StatusBadRequest, "failed to decode request")
281 return
282 }
283
284 if err := c.backend.RefreshMCPTools(r.Context(), id, req.Name); err != nil {
285 c.handleError(w, r, err)
286 return
287 }
288 w.WriteHeader(http.StatusOK)
289}
290
291// handlePostWorkspaceMCPReadResource reads a resource from an MCP server.
292//
293// @Summary Read MCP resource
294// @Tags mcp
295// @Accept json
296// @Produce json
297// @Param id path string true "Workspace ID"
298// @Param request body proto.MCPReadResourceRequest true "MCP read resource request"
299// @Success 200 {object} object
300// @Failure 400 {object} proto.Error
301// @Failure 404 {object} proto.Error
302// @Failure 500 {object} proto.Error
303// @Router /workspaces/{id}/mcp/read-resource [post]
304func (c *controllerV1) handlePostWorkspaceMCPReadResource(w http.ResponseWriter, r *http.Request) {
305 id := r.PathValue("id")
306
307 var req proto.MCPReadResourceRequest
308 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
309 c.server.logError(r, "Failed to decode request", "error", err)
310 jsonError(w, http.StatusBadRequest, "failed to decode request")
311 return
312 }
313
314 contents, err := c.backend.ReadMCPResource(r.Context(), id, req.Name, req.URI)
315 if err != nil {
316 c.handleError(w, r, err)
317 return
318 }
319 jsonEncode(w, contents)
320}
321
322// handlePostWorkspaceMCPGetPrompt retrieves a prompt from an MCP server.
323//
324// @Summary Get MCP prompt
325// @Tags mcp
326// @Accept json
327// @Produce json
328// @Param id path string true "Workspace ID"
329// @Param request body proto.MCPGetPromptRequest true "MCP get prompt request"
330// @Success 200 {object} proto.MCPGetPromptResponse
331// @Failure 400 {object} proto.Error
332// @Failure 404 {object} proto.Error
333// @Failure 500 {object} proto.Error
334// @Router /workspaces/{id}/mcp/get-prompt [post]
335func (c *controllerV1) handlePostWorkspaceMCPGetPrompt(w http.ResponseWriter, r *http.Request) {
336 id := r.PathValue("id")
337
338 var req proto.MCPGetPromptRequest
339 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
340 c.server.logError(r, "Failed to decode request", "error", err)
341 jsonError(w, http.StatusBadRequest, "failed to decode request")
342 return
343 }
344
345 prompt, err := c.backend.GetMCPPrompt(id, req.ClientID, req.PromptID, req.Args)
346 if err != nil {
347 c.handleError(w, r, err)
348 return
349 }
350 jsonEncode(w, proto.MCPGetPromptResponse{Prompt: prompt})
351}
352
353// handleGetWorkspaceMCPStates returns the state of all MCP clients.
354//
355// @Summary Get MCP client states
356// @Tags mcp
357// @Produce json
358// @Param id path string true "Workspace ID"
359// @Success 200 {object} map[string]proto.MCPClientInfo
360// @Failure 404 {object} proto.Error
361// @Failure 500 {object} proto.Error
362// @Router /workspaces/{id}/mcp/states [get]
363func (c *controllerV1) handleGetWorkspaceMCPStates(w http.ResponseWriter, r *http.Request) {
364 id := r.PathValue("id")
365 states := c.backend.MCPGetStates(id)
366 result := make(map[string]proto.MCPClientInfo, len(states))
367 for k, v := range states {
368 result[k] = proto.MCPClientInfo{
369 Name: v.Name,
370 State: proto.MCPState(v.State),
371 Error: v.Error,
372 ToolCount: v.Counts.Tools,
373 PromptCount: v.Counts.Prompts,
374 ResourceCount: v.Counts.Resources,
375 ConnectedAt: v.ConnectedAt,
376 }
377 }
378 jsonEncode(w, result)
379}
380
381// handlePostWorkspaceMCPRefreshPrompts refreshes prompts for a named MCP server.
382//
383// @Summary Refresh MCP prompts
384// @Tags mcp
385// @Accept json
386// @Param id path string true "Workspace ID"
387// @Param request body proto.MCPNameRequest true "MCP name request"
388// @Success 200
389// @Failure 400 {object} proto.Error
390// @Failure 404 {object} proto.Error
391// @Failure 500 {object} proto.Error
392// @Router /workspaces/{id}/mcp/refresh-prompts [post]
393func (c *controllerV1) handlePostWorkspaceMCPRefreshPrompts(w http.ResponseWriter, r *http.Request) {
394 id := r.PathValue("id")
395
396 var req proto.MCPNameRequest
397 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
398 c.server.logError(r, "Failed to decode request", "error", err)
399 jsonError(w, http.StatusBadRequest, "failed to decode request")
400 return
401 }
402
403 c.backend.MCPRefreshPrompts(r.Context(), id, req.Name)
404 w.WriteHeader(http.StatusOK)
405}
406
407// handlePostWorkspaceMCPRefreshResources refreshes resources for a named MCP server.
408//
409// @Summary Refresh MCP resources
410// @Tags mcp
411// @Accept json
412// @Param id path string true "Workspace ID"
413// @Param request body proto.MCPNameRequest true "MCP name request"
414// @Success 200
415// @Failure 400 {object} proto.Error
416// @Failure 404 {object} proto.Error
417// @Failure 500 {object} proto.Error
418// @Router /workspaces/{id}/mcp/refresh-resources [post]
419func (c *controllerV1) handlePostWorkspaceMCPRefreshResources(w http.ResponseWriter, r *http.Request) {
420 id := r.PathValue("id")
421
422 var req proto.MCPNameRequest
423 if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
424 c.server.logError(r, "Failed to decode request", "error", err)
425 jsonError(w, http.StatusBadRequest, "failed to decode request")
426 return
427 }
428
429 c.backend.MCPRefreshResources(r.Context(), id, req.Name)
430 w.WriteHeader(http.StatusOK)
431}