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
+37 -26
View File
@@ -289,6 +289,42 @@ type serviceCheck struct {
OK bool
}
// probeClient checks whether a service answers. A redirect (e.g. to a
// login page) means the service is there.
var probeClient = &http.Client{
Timeout: 6 * time.Second,
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
// probe asks one service whether it answers, from this computer.
func probe(ctx context.Context, svc core.Service) serviceCheck {
c := serviceCheck{Name: svc.Name, URL: svc.URL}
ctx, cancel := context.WithTimeout(ctx, 6*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, svc.URL, nil)
if err != nil {
c.Status = "isn't a valid address"
return c
}
res, err := probeClient.Do(req)
switch {
case err != nil:
c.Status = "doesn't answer"
var dnsErr interface{ Timeout() bool }
if errors.As(err, &dnsErr) && dnsErr.Timeout() {
c.Status = "timed out"
}
case res.StatusCode >= 500:
c.Status = "error " + res.Status
res.Body.Close()
default:
c.OK = true
c.Status = "answers (" + res.Status + ")"
res.Body.Close()
}
return c
}
// coreChecks asks each Core service whether it answers. It's an htmx
// fragment, loaded after the page so a slow service doesn't hold it up.
func (s *Server) coreChecks(w http.ResponseWriter, r *http.Request) {
@@ -298,37 +334,12 @@ func (s *Server) coreChecks(w http.ResponseWriter, r *http.Request) {
}
services := s.Core.Services()
checks := make([]serviceCheck, len(services))
client := &http.Client{
Timeout: 6 * time.Second,
// A redirect (e.g. to a login page) means the service is there.
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
}
var wg sync.WaitGroup
for i, svc := range services {
wg.Add(1)
go func(i int, svc core.Service) {
defer wg.Done()
c := serviceCheck{Name: svc.Name, URL: svc.URL}
ctx, cancel := context.WithTimeout(r.Context(), 6*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, svc.URL, nil)
res, err := client.Do(req)
switch {
case err != nil:
c.Status = "doesn't answer"
var dnsErr interface{ Timeout() bool }
if errors.As(err, &dnsErr) && dnsErr.Timeout() {
c.Status = "timed out"
}
case res.StatusCode >= 500:
c.Status = "error " + res.Status
res.Body.Close()
default:
c.OK = true
c.Status = "answers (" + res.Status + ")"
res.Body.Close()
}
checks[i] = c
checks[i] = probe(r.Context(), svc)
}(i, svc)
}
wg.Wait()