1package proto
2
3import (
4 "encoding/json"
5 "fmt"
6
7 "github.com/charmbracelet/crush/internal/config"
8 "github.com/charmbracelet/crush/internal/oauth"
9)
10
11// ConfigSetRequest represents a request to set a config field.
12type ConfigSetRequest struct {
13 Scope config.Scope `json:"scope"`
14 Key string `json:"key"`
15 Value any `json:"value"`
16}
17
18// ConfigRemoveRequest represents a request to remove a config field.
19type ConfigRemoveRequest struct {
20 Scope config.Scope `json:"scope"`
21 Key string `json:"key"`
22}
23
24// ConfigModelRequest represents a request to update the preferred model.
25type ConfigModelRequest struct {
26 Scope config.Scope `json:"scope"`
27 ModelType config.SelectedModelType `json:"model_type"`
28 Model config.SelectedModel `json:"model"`
29}
30
31// ConfigCompactRequest represents a request to set compact mode.
32type ConfigCompactRequest struct {
33 Scope config.Scope `json:"scope"`
34 Enabled bool `json:"enabled"`
35}
36
37// APIKeyKind discriminates the kind of credential carried in a
38// ConfigProviderKeyRequest. JSON's `any` loses Go type information, so
39// the wire format names the kind explicitly and the server decodes
40// APIKey accordingly.
41type APIKeyKind string
42
43const (
44 // APIKeyKindString is a plain string API key.
45 APIKeyKindString APIKeyKind = "string"
46 // APIKeyKindOAuth is an oauth.Token credential.
47 APIKeyKindOAuth APIKeyKind = "oauth"
48)
49
50// ConfigProviderKeyRequest represents a request to set a provider API
51// key. APIKey is the raw JSON for the credential; Kind selects the
52// concrete Go type APIKey should be decoded into via DecodeAPIKey.
53type ConfigProviderKeyRequest struct {
54 Scope config.Scope `json:"scope"`
55 ProviderID string `json:"provider_id"`
56 Kind APIKeyKind `json:"kind"`
57 APIKey json.RawMessage `json:"api_key"`
58}
59
60// DecodeAPIKey decodes APIKey into the Go type indicated by Kind. It
61// returns a string for APIKeyKindString and a *oauth.Token for
62// APIKeyKindOAuth. An unknown kind or malformed payload is reported
63// as an error.
64func (r ConfigProviderKeyRequest) DecodeAPIKey() (any, error) {
65 switch r.Kind {
66 case APIKeyKindString:
67 var s string
68 if err := json.Unmarshal(r.APIKey, &s); err != nil {
69 return nil, fmt.Errorf("decode api key string: %w", err)
70 }
71 return s, nil
72 case APIKeyKindOAuth:
73 var tok oauth.Token
74 if err := json.Unmarshal(r.APIKey, &tok); err != nil {
75 return nil, fmt.Errorf("decode api key oauth token: %w", err)
76 }
77 return &tok, nil
78 default:
79 return nil, fmt.Errorf("unsupported api key kind %q", r.Kind)
80 }
81}
82
83// ConfigRefreshOAuthRequest represents a request to refresh an OAuth token.
84type ConfigRefreshOAuthRequest struct {
85 Scope config.Scope `json:"scope"`
86 ProviderID string `json:"provider_id"`
87}
88
89// ImportCopilotResponse represents the response from importing Copilot credentials.
90type ImportCopilotResponse struct {
91 Token any `json:"token"`
92 Success bool `json:"success"`
93}
94
95// ProjectNeedsInitResponse represents whether a project needs initialization.
96type ProjectNeedsInitResponse struct {
97 NeedsInit bool `json:"needs_init"`
98}
99
100// ProjectInitPromptResponse represents the project initialization prompt.
101type ProjectInitPromptResponse struct {
102 Prompt string `json:"prompt"`
103}
104
105// LSPStartRequest represents a request to start an LSP for a path.
106type LSPStartRequest struct {
107 Path string `json:"path"`
108}
109
110// FileTrackerReadRequest represents a request to record a file read.
111type FileTrackerReadRequest struct {
112 SessionID string `json:"session_id"`
113 Path string `json:"path"`
114}
115
116// MCPNameRequest represents a request targeting a named MCP server.
117type MCPNameRequest struct {
118 Name string `json:"name"`
119}
120
121// MCPReadResourceRequest represents a request to read an MCP resource.
122type MCPReadResourceRequest struct {
123 Name string `json:"name"`
124 URI string `json:"uri"`
125}
126
127// MCPGetPromptRequest represents a request to get an MCP prompt.
128type MCPGetPromptRequest struct {
129 ClientID string `json:"client_id"`
130 PromptID string `json:"prompt_id"`
131 Args map[string]string `json:"args"`
132}
133
134// MCPGetPromptResponse represents the response from getting an MCP prompt.
135type MCPGetPromptResponse struct {
136 Prompt string `json:"prompt"`
137}