diff --git a/PLAN.md b/PLAN.md index b7b1709..126085b 100644 --- a/PLAN.md +++ b/PLAN.md @@ -18,6 +18,7 @@ running a Federated Core can install as a CoreDesktop sibling. | Websites and components | The suite builds the company's **websites** from a reusable **component library** | | Publishing | Sites are pushed to a **Gitea repository**. The user sets up the hooks that publish it to the Hugo server | | Contact enrichment | Third-party services allowed, behind a pluggable interface (section 6) | +| AI for the app | Uses Hermes' Anthropic key and model when the user agrees (2026-11-20), copied to `hermes-ai.json` by `supermarket hermes` and `update.sh`. It calls the model directly, never through Hermes: post writing reads untrusted web text, which must not reach an agent with tools and CRM access | | Suite shape | One repo, one app with modules (Content, Prospects, SEO, Metrics, Sites), plus an MCP endpoint for agents | ## 2. Requirements from the notes diff --git a/README.md b/README.md index 8751747..f582ec5 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ MCPHUB_AUTH= Hermes' sandbox can only reach the hub once it's in `network.allowedDomains` in `~/.config/coredesktop/sandbox/hermes.json`. The script asks before adding it, then restarts Hermes' gateway. To enter a different hub token: `supermarket hermes --reconfigure`. +**AI for Supermarket itself.** If Hermes uses an Anthropic key, `supermarket hermes` offers to let Supermarket use the same key and model, so AI is set up once. Only the key and model are copied, to `hermes-ai.json` in the data folder (readable only by you), and `update.sh` keeps them in step. Supermarket still calls the model directly, not through Hermes: writing posts means reading text from the web, and that text shouldn't reach an agent that has tools and your CRM. A key saved under Settings → Credentials takes the place of Hermes'. + To check the connection, open **Core & Hermes** in Supermarket and ask Hermes the question shown there. Hermes lists the hub's tools and reports them back to Supermarket, which shows when it last heard from Hermes. ## Writing and publishing posts diff --git a/cmd/supermarket/main.go b/cmd/supermarket/main.go index af2d81f..1459bee 100644 --- a/cmd/supermarket/main.go +++ b/cmd/supermarket/main.go @@ -27,6 +27,7 @@ import ( "syscall" "time" + "supermarket/internal/ai" "supermarket/internal/content" "supermarket/internal/core" "supermarket/internal/db" @@ -86,6 +87,9 @@ func run(log *slog.Logger) error { log.Warn("drafts were being published when Supermarket stopped; marked failed for review", "count", n) } + // Hermes' Anthropic key and model, if the user chose to share them + ai.SharedFile = filepath.Join(dataDir, "hermes-ai.json") + sec, err := secrets.Open(filepath.Join(dataDir, "secrets.json")) if err != nil { return err diff --git a/hermes/setup.sh b/hermes/setup.sh index 761934e..6314578 100755 --- a/hermes/setup.sh +++ b/hermes/setup.sh @@ -203,6 +203,21 @@ if ! "${HERMES[@]}" config get mcp_servers.supermarket.args 2> /dev/null | grep echo "Warning: $HERMES_CONFIG doesn't point Hermes at $MCP_SOCKET as expected; check its mcp_servers section." fi +# --- Sharing Hermes' AI key with Supermarket --- +if [ -n "$(hermes_ai_key)" ] && [ "$(hermes_config model.provider)" = anthropic ]; then + if [ ! -f "$HERMES_AI_CHOICE" ] || [ "${1:-}" = "--reconfigure" ]; then + echo + echo "Hermes uses an Anthropic key and $(hermes_config model.default)." + echo "Supermarket can use them too (it calls the model directly, without Hermes' tools)." + if confirm_yes "Use Hermes' Anthropic key and model for Supermarket?"; then + echo yes > "$HERMES_AI_CHOICE" + else + echo no > "$HERMES_AI_CHOICE" + fi + fi + sync_hermes_ai -v +fi + # --- Hermes' sandbox --- need=() for host in $hub_host; do diff --git a/internal/ai/ai.go b/internal/ai/ai.go index 83b6e80..546e23e 100644 --- a/internal/ai/ai.go +++ b/internal/ai/ai.go @@ -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: diff --git a/internal/ai/ai_test.go b/internal/ai/ai_test.go index 76f7bfe..453db3b 100644 --- a/internal/ai/ai_test.go +++ b/internal/ai/ai_test.go @@ -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") + } +} diff --git a/internal/web/settings.go b/internal/web/settings.go index 838da06..0840350 100644 --- a/internal/web/settings.go +++ b/internal/web/settings.go @@ -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(), }}) } diff --git a/internal/web/templates/ai.html b/internal/web/templates/ai.html index 280f1e9..059ee4b 100644 --- a/internal/web/templates/ai.html +++ b/internal/web/templates/ai.html @@ -10,7 +10,10 @@
Provider + Anthropic (Claude) + {{if .HasAnthropicKey}}— with the key saved under Credentials + {{else if .Shared}}— with Hermes' key, shared by supermarket hermes (Hermes uses {{.Shared.Model}}). A key saved under Credentials would take its place. + {{else}}— add the API key under Credentials, or share Hermes' by running supermarket hermes{{end}}
diff --git a/lib.sh b/lib.sh index 90622a8..9c9215e 100644 --- a/lib.sh +++ b/lib.sh @@ -91,6 +91,59 @@ write_ai_servers() { } # Wait until the app answers, for up to $1 seconds. +# Supermarket can use the Anthropic key and model Hermes already has, so AI +# is set up once. hermes/setup.sh asks, and records the answer in +# $HERMES_AI_CHOICE; this copies just the key and model (never the rest of +# Hermes' .env) to $HERMES_AI, which the app reads when it has no key of its +# own. The app calls the model directly, without Hermes' tools. +HERMES_AI="$DATA/hermes-ai.json" +HERMES_AI_CHOICE="$DATA/hermes-ai.choice" + +# hermes_config KEY: a value from Hermes' config, read with the real command +# (CoreDesktop's `hermes` is the sandboxed one). +hermes_config() { + local unsandboxed="$COREDESKTOP/sandbox/bin/unsandboxed" + if [ -x "$unsandboxed" ]; then + PATH="$HOME/.local/bin:$PATH" "$unsandboxed" hermes config get "$1" 2> /dev/null + elif [ -x "$HOME/.local/bin/hermes" ]; then + "$HOME/.local/bin/hermes" config get "$1" 2> /dev/null + fi +} + +# hermes_ai_key prints Hermes' Anthropic key, if it has one. +hermes_ai_key() { + sed -n 's/^ANTHROPIC_API_KEY=//p' "${HERMES_HOME:-$HOME/.hermes}/.env" 2> /dev/null | tail -n 1 | tr -d "\"' \r" +} + +# sync_hermes_ai: refresh (or remove) the shared key. Quiet unless asked to +# report with "-v". +sync_hermes_ai() { + local key provider model tmp + if [ "$(cat "$HERMES_AI_CHOICE" 2> /dev/null)" != yes ]; then + rm -f "$HERMES_AI" + return 0 + fi + key="$(hermes_ai_key)" + provider="$(hermes_config model.provider)" + model="$(hermes_config model.default)" + if [ "$provider" != anthropic ] || [ -z "$key" ] || ! command -v jq > /dev/null; then + rm -f "$HERMES_AI" + [ "${1:-}" = -v ] && echo "Hermes doesn't use an Anthropic key, so there's none to share; set Supermarket's AI provider under Settings." + return 0 + fi + mkdir -p "$DATA" + tmp="$(mktemp "$HERMES_AI.XXXXXX")" + chmod 600 "$tmp" + # The key goes through the environment, not the command line. + if KEY="$key" MODEL="$model" jq -n '{provider: "anthropic", model: env.MODEL, api_key: env.KEY}' > "$tmp"; then + mv -f "$tmp" "$HERMES_AI" + [ "${1:-}" = -v ] && echo "Supermarket uses Hermes' Anthropic key and model ($model)." + else + rm -f "$tmp" + fi + return 0 +} + wait_until_up() { local i for ((i = 0; i < ${1:-60}; i++)); do diff --git a/update.sh b/update.sh index 88da52f..7443f78 100755 --- a/update.sh +++ b/update.sh @@ -18,6 +18,7 @@ fi write_compose_env copy_core_conf write_ai_servers +sync_hermes_ai # The `supermarket` command mkdir -p "$HOME/.local/bin"