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:
2026-11-20 14:58:06 -07:00
co-authored by Claude Opus 5.5
parent 434a9093b0
commit 81a7d0596c
10 changed files with 175 additions and 5 deletions
+42
View File
@@ -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")
}
}