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
+52 -4
View File
@@ -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: