Phase 2 part 1: the CRM bridge to EspoCRM through Hermes

Supermarket queues CRM work in crm_actions and never writes to EspoCRM
itself. A Hermes cron job (supermarket-crm, set up by `supermarket
hermes`) runs every 15 minutes behind a monitor script that prints
Supermarket's crm-state file, so unchanged state skips the AI run. Hermes
calls the new MCP tools crm_pending_actions, crm_report_result and
crm_ingest, creating each record through the Core's MCP hub unless a
match exists, and copying Leads, Accounts and Contacts back every 6 hours
for duplicate checks. Schema 5 adds crm_actions and crm_records.

Prospects -> CRM queue shows the queue, a test Lead, sync, Retry and
Cancel. When an action fails with a connection error, the page checks
EspoCRM from this computer and says whether the CRM is down or the hub
can't reach it, with a note to send to the Core's maintainer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 16:28:46 -07:00
co-authored by Claude Opus 5.5
parent 5feb6a16d4
commit 1dc5476d43
15 changed files with 1586 additions and 30 deletions
+22
View File
@@ -30,6 +30,7 @@ import (
"supermarket/internal/ai"
"supermarket/internal/content"
"supermarket/internal/core"
"supermarket/internal/crm"
"supermarket/internal/db"
"supermarket/internal/fileutil"
"supermarket/internal/legacy"
@@ -89,6 +90,11 @@ func run(log *slog.Logger) error {
// Hermes' Anthropic key and model, if the user chose to share them
ai.SharedFile = filepath.Join(dataDir, "hermes-ai.json")
// What Hermes' CRM job checks before it runs; see internal/crm.
crm.StateFile = filepath.Join(dataDir, "crm-state")
if err := crm.WriteState(database, time.Now()); err != nil {
log.Error("writing the CRM state file", "err", err)
}
sec, err := secrets.Open(filepath.Join(dataDir, "secrets.json"))
if err != nil {
@@ -150,6 +156,22 @@ func run(log *slog.Logger) error {
}
}()
go srv.RunScheduler(ctx)
// A sync falls due, and a claim times out, without anything being
// written: refresh the state file each minute.
go func() {
t := time.NewTicker(time.Minute)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case now := <-t.C:
if err := crm.WriteState(database, now); err != nil {
log.Error("writing the CRM state file", "err", err)
}
}
}
}()
go func() {
log.Info("Supermarket is running", "version", version, "listen", addr, "open", publicURL)
errCh <- httpServer.ListenAndServe()