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
+90
View File
@@ -0,0 +1,90 @@
{{define "content"}}{{with .Data}}{{$job := .Job}}
<div class="page-head">
<h1>CRM queue</h1>
<p class="lede">What Supermarket has asked Hermes to do in EspoCRM. Supermarket never writes to the CRM itself: Hermes takes these actions, creates each record unless a matching one is already there, and reports back. It also copies your Leads, Accounts and Contacts here, so Supermarket can spot people and companies you already have.</p>
</div>
{{if .Unreachable}}<div hx-get="/crm/reach" hx-trigger="load" hx-swap="outerHTML"><p class="help">Hermes couldn't reach EspoCRM. Checking it from here…</p></div>{{end}}
<div class="grid">
<section class="card">
<h2>Queue</h2>
<p>{{if .Waiting}}<strong>{{.Waiting}} waiting</strong> for Hermes{{else}}Nothing waiting{{end}}{{with index .Counts "failed"}} · <span class="warn-text">{{.}} failed</span>{{end}}{{with index .Counts "done"}} · {{.}} done{{end}}.</p>
<p class="help">{{if .LastDone.IsZero}}Hermes hasn't reported a result yet.{{else}}Hermes last reported a result {{ago .LastDone}}.{{end}}</p>
<form method="post" action="/crm/test" class="inline">
<button type="submit" class="secondary">Send a test Lead</button>
</form>
<p class="help">Queues a Lead named “Supermarket Test …” to check the whole path. Delete it in EspoCRM afterwards.</p>
</section>
<section class="card">
<h2>Copied from the CRM</h2>
<p>{{range $i, $e := .Entities}}{{if $i}} · {{end}}{{index $.Data.Records $e}} {{$e}}{{end}}</p>
<p class="help">{{if .Sync.Last.IsZero}}Not synced yet.{{else}}Last synced {{ago .Sync.Last}}; Hermes copies changes every 6 hours.{{end}}{{if .SyncRequested}} A sync is requested.{{end}}</p>
<form method="post" action="/crm/sync" class="inline">
<button type="submit" class="secondary" {{if .SyncRequested}}disabled{{end}}>Sync on the next run</button>
</form>
</section>
</div>
<section class="card">
<h2>How Hermes runs it</h2>
{{if .JobID}}
<p>Hermes has a scheduled job, <code>{{$job}}</code>, set up by <code>supermarket hermes</code>. Every 15 minutes it checks a small file Supermarket keeps up to date, and only wakes the AI when there's something to do. To run it now, in a terminal:</p>
<div class="copy"><pre id="run-now">hermes cron run {{.JobID}}</pre><button type="button" class="secondary" data-copy="run-now">Copy</button></div>
<p class="help">Or ask Hermes: “Run the {{$job}} job.” Its runs are listed by <code>hermes cron runs {{.JobID}}</code>.</p>
{{else}}
<p class="warn-text">Hermes' CRM job isn't set up yet, so nothing queued here reaches your CRM. In a terminal, run:</p>
<div class="copy"><pre id="setup">supermarket hermes</pre><button type="button" class="secondary" data-copy="setup">Copy</button></div>
{{end}}
{{if not .Checkin}}<p class="help">Hermes hasn't checked in with Supermarket yet: see <a href="/settings/core">Settings → Core &amp; Hermes</a>.</p>{{end}}
</section>
{{if .Actions}}
<h2>Actions</h2>
<table class="list">
<thead><tr><th>Queued</th><th>Action</th><th>Status</th><th></th></tr></thead>
<tbody>
{{range .Actions}}
<tr>
<td>{{ago .CreatedAt}}{{if eq .Origin "test"}}<br><span class="muted small">test</span>{{end}}</td>
<td>{{if eq .Kind "create"}}Create{{else}}{{.Kind}}{{end}} {{.Entity}} <strong>{{.Name}}</strong>
{{with .Match}}<br><span class="muted small">unless one exists with {{range $k, $v := .}}{{$k}} = {{$v}} {{end}}</span>{{end}}</td>
<td>
{{if eq .Status "done"}}<span class="badge published">{{if eq .Outcome "existing"}}already there{{else}}created{{end}}</span> <span class="muted small">{{.EspoID}}</span>
{{else if eq .Status "failed"}}<span class="badge failed">failed</span><br><span class="small">{{.Error}}</span>
{{else if eq .Status "claimed"}}<span class="badge scheduled">with Hermes</span> <span class="muted small">since {{ago .ClaimedAt}}</span>
{{else if eq .Status "cancelled"}}<span class="badge">cancelled</span>
{{else}}<span class="badge draft">waiting</span>{{end}}
{{if gt .Attempts 1}}<br><span class="muted small">{{.Attempts}} tries</span>{{end}}
</td>
<td class="row-actions">
{{if eq .Status "failed"}}<form method="post" action="/crm/{{.ID}}/retry"><button type="submit" class="small">Retry</button></form>{{end}}
{{if or (eq .Status "pending") (eq .Status "failed") (eq .Status "claimed")}}<form method="post" action="/crm/{{.ID}}/cancel" data-confirm="Take this action back?{{if eq .Status "claimed"}} Hermes may already be carrying it out.{{end}}"><button type="submit" class="secondary small">Cancel</button></form>{{end}}
</td>
</tr>
{{end}}
</tbody>
</table>
{{else}}
<section class="card empty"><p>Nothing queued yet. Send a test Lead to check that Hermes can write to your CRM; prospects you approve will be queued here.</p></section>
{{end}}
{{end}}{{end}}
{{define "reach"}}{{with .}}
<section class="card warn">
<h2>Hermes couldn't reach EspoCRM</h2>
{{if .Check.OK}}
<p>EspoCRM <strong>answers from this computer</strong>, at <code>{{.Check.URL}}</code>. So it's running. The problem is between your Core's MCP hub and EspoCRM: Hermes never connects to EspoCRM itself, it asks the hub, and the hub connects with its own settings{{with .Tried}}, which sent it to <code>{{.}}</code>{{end}}.</p>
<p>Changing anything in Supermarket or Hermes won't fix this. Send this to whoever looks after your Core:</p>
<div class="copy"><pre id="hub-note">The MCP hub ({{.HubHost}}) can't reach EspoCRM. Its EspoCRM tools fail with:
{{.Error}}
EspoCRM itself answers at {{.Check.URL}}.{{with .Tried}} The hub's EspoCRM address, or the DNS it uses, points at {{.}} instead.{{end}}</pre><button type="button" class="secondary" data-copy="hub-note">Copy</button></div>
{{else}}
<p class="warn-text">EspoCRM doesn't answer from this computer either, at <code>{{.Check.URL}}</code> ({{.Check.Status}}).</p>
<p>It's probably down: check that it's running on your Core{{with .Tried}}; the hub tried <code>{{.}}</code>{{end}}. If EspoCRM lives at a different address, that's the hub's setting to change, not Supermarket's.</p>
{{end}}
<p class="help">When it's fixed, press Retry on the failed actions below.</p>
</section>
{{end}}{{end}}