okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffauffeat(api)!: migrate daemon and clients to protocol 361c43ca

main
1.9 KiB63 linesraw
1package daemon
2
3import (
4	"encoding/json"
5	"errors"
6)
7
8// The daemon alone reads and writes creds.json. A client changes a profile's
9// api key or an MCP server's secrets through these routes and only ever learns
10// which secrets are saved, never what they are.
11
12// Credentials names the saved secrets.
13type Credentials struct {
14	MCP map[string]MCPSecretNames `json:"mcp"`
15	// Providers are the profiles with a saved api key.
16	Providers []string `json:"providers"`
17}
18
19// MCPSecretNames is what an MCP server has saved, by name only.
20type MCPSecretNames struct {
21	Headers     []string `json:"headers"`
22	Env         []string `json:"env"`
23	BearerToken bool     `json:"bearerToken"`
24}
25
26// Any reports whether the server has any secret saved.
27func (n MCPSecretNames) Any() bool {
28	return n.BearerToken || len(n.Headers) > 0 || len(n.Env) > 0
29}
30
31// MCPSecretsPatch keeps fields whose pointers or maps are nil. RemoveBearerToken
32// and the Clear flags remove whole fields; a nil map entry removes that name.
33type MCPSecretsPatch struct {
34	BearerToken       *string
35	RemoveBearerToken bool
36	Headers           map[string]*string
37	ClearHeaders      bool
38	Env               map[string]*string
39	ClearEnv          bool
40}
41
42func (patch MCPSecretsPatch) MarshalJSON() ([]byte, error) {
43	if (patch.BearerToken != nil && patch.RemoveBearerToken) || (patch.Headers != nil && patch.ClearHeaders) || (patch.Env != nil && patch.ClearEnv) {
44		return nil, errors.New("secret patch cannot replace and clear the same field")
45	}
46	fields := make(map[string]any)
47	if patch.BearerToken != nil {
48		fields["bearer_token"] = *patch.BearerToken
49	} else if patch.RemoveBearerToken {
50		fields["bearer_token"] = nil
51	}
52	if patch.Headers != nil {
53		fields["headers"] = patch.Headers
54	} else if patch.ClearHeaders {
55		fields["headers"] = nil
56	}
57	if patch.Env != nil {
58		fields["environment"] = patch.Env
59	} else if patch.ClearEnv {
60		fields["environment"] = nil
61	}
62	return json.Marshal(fields)
63}