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:
@@ -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
|
||||
|
||||
@@ -88,6 +88,8 @@ MCPHUB_AUTH=<your own hub token from your Core: the part after "Basic ">
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
+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>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user