Let Supermarket use Hermes' Anthropic key and model
Hermes is already set up with an Anthropic key, so AI needn't be set up twice. `supermarket hermes` asks once, then it and update.sh copy just the key and model to hermes-ai.json in the data folder (0600). The app uses them when it has no key of its own, re-reading the file on each use. Generation still calls the model directly rather than through Hermes' API: post writing feeds it text from the web, which must not reach an agent with a terminal, a browser and write access to the CRM. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+52
-4
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"supermarket/internal/db"
|
||||
@@ -47,15 +48,62 @@ type Config struct {
|
||||
|
||||
const settingKey = "ai"
|
||||
|
||||
// LoadConfig returns the saved AI config, or Anthropic's defaults.
|
||||
// SharedFile is the file with the Anthropic key and model shared from
|
||||
// Hermes (written by lib.sh's sync_hermes_ai), or "" for none. Set at start.
|
||||
var SharedFile string
|
||||
|
||||
// Shared is what Hermes shares.
|
||||
type Shared struct {
|
||||
Provider string `json:"provider"`
|
||||
Model string `json:"model"`
|
||||
APIKey string `json:"api_key"`
|
||||
}
|
||||
|
||||
// LoadShared reads the shared key, or nil if there's none. It's read on
|
||||
// every use, so a key changed in Hermes applies at the next update.sh.
|
||||
func LoadShared() *Shared {
|
||||
if SharedFile == "" {
|
||||
return nil
|
||||
}
|
||||
raw, err := os.ReadFile(SharedFile)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var sh Shared
|
||||
if json.Unmarshal(raw, &sh) != nil || sh.Provider != ProviderAnthropic || strings.TrimSpace(sh.APIKey) == "" {
|
||||
return nil
|
||||
}
|
||||
return &sh
|
||||
}
|
||||
|
||||
// LoadConfig returns the saved AI config. Until one is saved: Anthropic,
|
||||
// with Hermes' model if it shares one.
|
||||
func LoadConfig(d *db.DB) (Config, error) {
|
||||
c := Config{Provider: ProviderAnthropic, Model: DefaultAnthropicModel, Effort: "medium"}
|
||||
if _, err := d.GetJSON(settingKey, &c); err != nil {
|
||||
found, err := d.GetJSON(settingKey, &c)
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
if !found {
|
||||
if sh := LoadShared(); sh != nil && sh.Model != "" {
|
||||
c.Model = sh.Model
|
||||
}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// AnthropicKey is the key to use: Supermarket's own, else Hermes'. It
|
||||
// reports whether it's Hermes'.
|
||||
func AnthropicKey(sec *secrets.Store) (key string, shared bool) {
|
||||
if key = sec.Get(secrets.AnthropicAPIKey); key != "" {
|
||||
return key, false
|
||||
}
|
||||
if sh := LoadShared(); sh != nil {
|
||||
return sh.APIKey, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// SaveConfig stores the AI config.
|
||||
func SaveConfig(d *db.DB, c Config) error {
|
||||
return d.PutJSON(settingKey, c)
|
||||
@@ -92,9 +140,9 @@ var ErrNotConfigured = errors.New("AI provider not configured")
|
||||
func New(c Config, sec *secrets.Store) (Client, error) {
|
||||
switch c.Provider {
|
||||
case ProviderAnthropic, "":
|
||||
key := sec.Get(secrets.AnthropicAPIKey)
|
||||
key, _ := AnthropicKey(sec)
|
||||
if key == "" {
|
||||
return nil, fmt.Errorf("%w: add an Anthropic API key in Settings", ErrNotConfigured)
|
||||
return nil, fmt.Errorf("%w: add an Anthropic API key in Settings, or share Hermes' with `supermarket hermes`", ErrNotConfigured)
|
||||
}
|
||||
return newAnthropic(key, c), nil
|
||||
case ProviderOpenAI:
|
||||
|
||||
@@ -3,8 +3,10 @@ package ai
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -110,3 +112,43 @@ func mustSecrets(t *testing.T) *secrets.Store {
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestSharedKey(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
d, err := db.Open(filepath.Join(dir, "app.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer d.Close()
|
||||
sec, _ := secrets.Open(filepath.Join(dir, "secrets.json"))
|
||||
defer func() { SharedFile = "" }()
|
||||
|
||||
SharedFile = filepath.Join(dir, "hermes-ai.json")
|
||||
if _, err := New(Config{Provider: ProviderAnthropic}, sec); !errors.Is(err, ErrNotConfigured) {
|
||||
t.Fatalf("no key anywhere: %v", err)
|
||||
}
|
||||
os.WriteFile(SharedFile, []byte(`{"provider":"anthropic","model":"claude-sonnet-5-5","api_key":"sk-hermes"}`), 0600)
|
||||
if key, shared := AnthropicKey(sec); key != "sk-hermes" || !shared {
|
||||
t.Errorf("shared key = %q, %v", key, shared)
|
||||
}
|
||||
if _, err := New(Config{Provider: ProviderAnthropic}, sec); err != nil {
|
||||
t.Errorf("with Hermes' key: %v", err)
|
||||
}
|
||||
if c, _ := LoadConfig(d); c.Model != "claude-sonnet-5-5" {
|
||||
t.Errorf("default model = %q, want Hermes'", c.Model)
|
||||
}
|
||||
// A saved config, or a key of Supermarket's own, wins.
|
||||
_ = SaveConfig(d, Config{Provider: ProviderAnthropic, Model: "claude-opus-5-5"})
|
||||
if c, _ := LoadConfig(d); c.Model != "claude-opus-5-5" {
|
||||
t.Errorf("saved model = %q", c.Model)
|
||||
}
|
||||
_ = sec.Set(map[string]string{secrets.AnthropicAPIKey: "sk-own"})
|
||||
if key, shared := AnthropicKey(sec); key != "sk-own" || shared {
|
||||
t.Errorf("own key = %q, %v", key, shared)
|
||||
}
|
||||
// Anything but an Anthropic key is ignored.
|
||||
os.WriteFile(SharedFile, []byte(`{"provider":"openrouter","api_key":"x"}`), 0600)
|
||||
if LoadShared() != nil {
|
||||
t.Error("non-Anthropic share accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ func (s *Server) renderAI(w http.ResponseWriter, r *http.Request, cfg ai.Config,
|
||||
"Tasks": ai.Tasks,
|
||||
"Servers": s.aiServers(),
|
||||
"HasAnthropicKey": s.Secrets.Has(secrets.AnthropicAPIKey),
|
||||
"Shared": ai.LoadShared(),
|
||||
}})
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,10 @@
|
||||
<fieldset class="choice">
|
||||
<legend>Provider</legend>
|
||||
<label class="check"><input type="radio" name="provider" value="anthropic" {{if ne $cfg.Provider "openai"}}checked{{end}} data-toggle="provider">
|
||||
Anthropic (Claude) {{if not .HasAnthropicKey}}<span class="help">— add the API key under <a href="/settings/credentials">Credentials</a></span>{{end}}</label>
|
||||
Anthropic (Claude)
|
||||
{{if .HasAnthropicKey}}<span class="help">— with the key saved under <a href="/settings/credentials">Credentials</a></span>
|
||||
{{else if .Shared}}<span class="help">— with Hermes' key, shared by <code>supermarket hermes</code> (Hermes uses {{.Shared.Model}}). A key saved under <a href="/settings/credentials">Credentials</a> would take its place.</span>
|
||||
{{else}}<span class="help">— add the API key under <a href="/settings/credentials">Credentials</a>, or share Hermes' by running <code>supermarket hermes</code></span>{{end}}</label>
|
||||
<label class="check"><input type="radio" name="provider" value="openai" {{if eq $cfg.Provider "openai"}}checked{{end}} data-toggle="provider">
|
||||
A server on your network (Ollama, LM Studio, llama.cpp, vLLM) or another OpenAI-compatible service</label>
|
||||
</fieldset>
|
||||
|
||||
Reference in New Issue
Block a user