1package client
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "net/http"
8
9 "github.com/charmbracelet/crush/internal/config"
10 "github.com/charmbracelet/crush/internal/oauth"
11 "github.com/charmbracelet/crush/internal/proto"
12)
13
14// SetConfigField sets a config key/value pair on the server.
15func (c *Client) SetConfigField(ctx context.Context, id string, scope config.Scope, key string, value any) error {
16 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/set", id), nil, jsonBody(struct {
17 Scope config.Scope `json:"scope"`
18 Key string `json:"key"`
19 Value any `json:"value"`
20 }{Scope: scope, Key: key, Value: value}), http.Header{"Content-Type": []string{"application/json"}})
21 if err != nil {
22 return fmt.Errorf("failed to set config field: %w", err)
23 }
24 defer rsp.Body.Close()
25 if rsp.StatusCode != http.StatusOK {
26 return fmt.Errorf("failed to set config field: status code %d", rsp.StatusCode)
27 }
28 return nil
29}
30
31// RemoveConfigField removes a config key on the server.
32func (c *Client) RemoveConfigField(ctx context.Context, id string, scope config.Scope, key string) error {
33 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/remove", id), nil, jsonBody(struct {
34 Scope config.Scope `json:"scope"`
35 Key string `json:"key"`
36 }{Scope: scope, Key: key}), http.Header{"Content-Type": []string{"application/json"}})
37 if err != nil {
38 return fmt.Errorf("failed to remove config field: %w", err)
39 }
40 defer rsp.Body.Close()
41 if rsp.StatusCode != http.StatusOK {
42 return fmt.Errorf("failed to remove config field: status code %d", rsp.StatusCode)
43 }
44 return nil
45}
46
47// UpdatePreferredModel updates the preferred model on the server.
48func (c *Client) UpdatePreferredModel(ctx context.Context, id string, scope config.Scope, modelType config.SelectedModelType, model config.SelectedModel) error {
49 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/model", id), nil, jsonBody(struct {
50 Scope config.Scope `json:"scope"`
51 ModelType config.SelectedModelType `json:"model_type"`
52 Model config.SelectedModel `json:"model"`
53 }{Scope: scope, ModelType: modelType, Model: model}), http.Header{"Content-Type": []string{"application/json"}})
54 if err != nil {
55 return fmt.Errorf("failed to update preferred model: %w", err)
56 }
57 defer rsp.Body.Close()
58 if rsp.StatusCode != http.StatusOK {
59 return fmt.Errorf("failed to update preferred model: status code %d", rsp.StatusCode)
60 }
61 return nil
62}
63
64// SetCompactMode sets compact mode on the server.
65func (c *Client) SetCompactMode(ctx context.Context, id string, scope config.Scope, enabled bool) error {
66 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/compact", id), nil, jsonBody(struct {
67 Scope config.Scope `json:"scope"`
68 Enabled bool `json:"enabled"`
69 }{Scope: scope, Enabled: enabled}), http.Header{"Content-Type": []string{"application/json"}})
70 if err != nil {
71 return fmt.Errorf("failed to set compact mode: %w", err)
72 }
73 defer rsp.Body.Close()
74 if rsp.StatusCode != http.StatusOK {
75 return fmt.Errorf("failed to set compact mode: status code %d", rsp.StatusCode)
76 }
77 return nil
78}
79
80// SetProviderAPIKey sets a provider API key on the server. The wire
81// format tags the credential with an explicit Kind so the server can
82// decode it back into the right Go type — JSON's `any` loses that
83// information across the socket.
84func (c *Client) SetProviderAPIKey(ctx context.Context, id string, scope config.Scope, providerID string, apiKey any) error {
85 var (
86 kind proto.APIKeyKind
87 raw json.RawMessage
88 )
89 switch v := apiKey.(type) {
90 case string:
91 kind = proto.APIKeyKindString
92 b, err := json.Marshal(v)
93 if err != nil {
94 return fmt.Errorf("failed to marshal api key string: %w", err)
95 }
96 raw = b
97 case *oauth.Token:
98 if v == nil {
99 return fmt.Errorf("oauth token is nil")
100 }
101 kind = proto.APIKeyKindOAuth
102 b, err := json.Marshal(v)
103 if err != nil {
104 return fmt.Errorf("failed to marshal oauth token: %w", err)
105 }
106 raw = b
107 default:
108 return fmt.Errorf("unsupported api key type %T", apiKey)
109 }
110
111 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/provider-key", id), nil, jsonBody(proto.ConfigProviderKeyRequest{
112 Scope: scope,
113 ProviderID: providerID,
114 Kind: kind,
115 APIKey: raw,
116 }), http.Header{"Content-Type": []string{"application/json"}})
117 if err != nil {
118 return fmt.Errorf("failed to set provider API key: %w", err)
119 }
120 defer rsp.Body.Close()
121 if rsp.StatusCode != http.StatusOK {
122 return fmt.Errorf("failed to set provider API key: status code %d", rsp.StatusCode)
123 }
124 return nil
125}
126
127// ImportCopilot attempts to import a GitHub Copilot token on the
128// server.
129func (c *Client) ImportCopilot(ctx context.Context, id string) (*oauth.Token, bool, error) {
130 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/import-copilot", id), nil, nil, nil)
131 if err != nil {
132 return nil, false, fmt.Errorf("failed to import copilot: %w", err)
133 }
134 defer rsp.Body.Close()
135 if rsp.StatusCode != http.StatusOK {
136 return nil, false, fmt.Errorf("failed to import copilot: status code %d", rsp.StatusCode)
137 }
138 var result struct {
139 Token *oauth.Token `json:"token"`
140 Success bool `json:"success"`
141 }
142 if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
143 return nil, false, fmt.Errorf("failed to decode import copilot response: %w", err)
144 }
145 return result.Token, result.Success, nil
146}
147
148// RefreshOAuthToken refreshes an OAuth token for a provider on the
149// server.
150func (c *Client) RefreshOAuthToken(ctx context.Context, id string, scope config.Scope, providerID string) error {
151 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/refresh-oauth", id), nil, jsonBody(struct {
152 Scope config.Scope `json:"scope"`
153 ProviderID string `json:"provider_id"`
154 }{Scope: scope, ProviderID: providerID}), http.Header{"Content-Type": []string{"application/json"}})
155 if err != nil {
156 return fmt.Errorf("failed to refresh OAuth token: %w", err)
157 }
158 defer rsp.Body.Close()
159 if rsp.StatusCode != http.StatusOK {
160 return fmt.Errorf("failed to refresh OAuth token: status code %d", rsp.StatusCode)
161 }
162 return nil
163}
164
165// ProjectNeedsInitialization checks if the project needs
166// initialization.
167func (c *Client) ProjectNeedsInitialization(ctx context.Context, id string) (bool, error) {
168 rsp, err := c.get(ctx, fmt.Sprintf("/workspaces/%s/project/needs-init", id), nil, nil)
169 if err != nil {
170 return false, fmt.Errorf("failed to check project init: %w", err)
171 }
172 defer rsp.Body.Close()
173 if rsp.StatusCode != http.StatusOK {
174 return false, fmt.Errorf("failed to check project init: status code %d", rsp.StatusCode)
175 }
176 var result struct {
177 NeedsInit bool `json:"needs_init"`
178 }
179 if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
180 return false, fmt.Errorf("failed to decode project init response: %w", err)
181 }
182 return result.NeedsInit, nil
183}
184
185// MarkProjectInitialized marks the project as initialized on the
186// server.
187func (c *Client) MarkProjectInitialized(ctx context.Context, id string) error {
188 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/project/init", id), nil, nil, nil)
189 if err != nil {
190 return fmt.Errorf("failed to mark project initialized: %w", err)
191 }
192 defer rsp.Body.Close()
193 if rsp.StatusCode != http.StatusOK {
194 return fmt.Errorf("failed to mark project initialized: status code %d", rsp.StatusCode)
195 }
196 return nil
197}
198
199// GetInitializePrompt retrieves the initialization prompt from the
200// server.
201func (c *Client) GetInitializePrompt(ctx context.Context, id string) (string, error) {
202 rsp, err := c.get(ctx, fmt.Sprintf("/workspaces/%s/project/init-prompt", id), nil, nil)
203 if err != nil {
204 return "", fmt.Errorf("failed to get init prompt: %w", err)
205 }
206 defer rsp.Body.Close()
207 if rsp.StatusCode != http.StatusOK {
208 return "", fmt.Errorf("failed to get init prompt: status code %d", rsp.StatusCode)
209 }
210 var result struct {
211 Prompt string `json:"prompt"`
212 }
213 if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
214 return "", fmt.Errorf("failed to decode init prompt response: %w", err)
215 }
216 return result.Prompt, nil
217}
218
219// MCPResourceContents holds the contents of an MCP resource.
220type MCPResourceContents struct {
221 URI string `json:"uri"`
222 MIMEType string `json:"mime_type,omitempty"`
223 Text string `json:"text,omitempty"`
224 Blob []byte `json:"blob,omitempty"`
225}
226
227// EnableDockerMCP enables the Docker MCP server on the workspace.
228func (c *Client) EnableDockerMCP(ctx context.Context, id string) error {
229 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/mcp/docker/enable", id), nil, nil, nil)
230 if err != nil {
231 return fmt.Errorf("failed to enable docker MCP: %w", err)
232 }
233 defer rsp.Body.Close()
234 if rsp.StatusCode != http.StatusOK {
235 return fmt.Errorf("failed to enable docker MCP: status code %d", rsp.StatusCode)
236 }
237 return nil
238}
239
240// DisableDockerMCP disables the Docker MCP server on the workspace.
241func (c *Client) DisableDockerMCP(ctx context.Context, id string) error {
242 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/mcp/docker/disable", id), nil, nil, nil)
243 if err != nil {
244 return fmt.Errorf("failed to disable docker MCP: %w", err)
245 }
246 defer rsp.Body.Close()
247 if rsp.StatusCode != http.StatusOK {
248 return fmt.Errorf("failed to disable docker MCP: status code %d", rsp.StatusCode)
249 }
250 return nil
251}
252
253// RefreshMCPTools refreshes tools for a named MCP server.
254func (c *Client) RefreshMCPTools(ctx context.Context, id, name string) error {
255 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/mcp/refresh-tools", id), nil, jsonBody(struct {
256 Name string `json:"name"`
257 }{Name: name}), http.Header{"Content-Type": []string{"application/json"}})
258 if err != nil {
259 return fmt.Errorf("failed to refresh MCP tools: %w", err)
260 }
261 defer rsp.Body.Close()
262 if rsp.StatusCode != http.StatusOK {
263 return fmt.Errorf("failed to refresh MCP tools: status code %d", rsp.StatusCode)
264 }
265 return nil
266}
267
268// ReadMCPResource reads a resource from a named MCP server.
269func (c *Client) ReadMCPResource(ctx context.Context, id, name, uri string) ([]MCPResourceContents, error) {
270 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/mcp/read-resource", id), nil, jsonBody(struct {
271 Name string `json:"name"`
272 URI string `json:"uri"`
273 }{Name: name, URI: uri}), http.Header{"Content-Type": []string{"application/json"}})
274 if err != nil {
275 return nil, fmt.Errorf("failed to read MCP resource: %w", err)
276 }
277 defer rsp.Body.Close()
278 if rsp.StatusCode != http.StatusOK {
279 return nil, fmt.Errorf("failed to read MCP resource: status code %d", rsp.StatusCode)
280 }
281 var contents []MCPResourceContents
282 if err := json.NewDecoder(rsp.Body).Decode(&contents); err != nil {
283 return nil, fmt.Errorf("failed to decode MCP resource: %w", err)
284 }
285 return contents, nil
286}
287
288// GetMCPPrompt retrieves a prompt from a named MCP server.
289func (c *Client) GetMCPPrompt(ctx context.Context, id, clientID, promptID string, args map[string]string) (string, error) {
290 rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/mcp/get-prompt", id), nil, jsonBody(struct {
291 ClientID string `json:"client_id"`
292 PromptID string `json:"prompt_id"`
293 Args map[string]string `json:"args"`
294 }{ClientID: clientID, PromptID: promptID, Args: args}), http.Header{"Content-Type": []string{"application/json"}})
295 if err != nil {
296 return "", fmt.Errorf("failed to get MCP prompt: %w", err)
297 }
298 defer rsp.Body.Close()
299 if rsp.StatusCode != http.StatusOK {
300 return "", fmt.Errorf("failed to get MCP prompt: status code %d", rsp.StatusCode)
301 }
302 var result struct {
303 Prompt string `json:"prompt"`
304 }
305 if err := json.NewDecoder(rsp.Body).Decode(&result); err != nil {
306 return "", fmt.Errorf("failed to decode MCP prompt response: %w", err)
307 }
308 return result.Prompt, nil
309}