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
+45
View File
@@ -218,6 +218,51 @@ if [ -n "$(hermes_ai_key)" ] && [ "$(hermes_config model.provider)" = anthropic
sync_hermes_ai -v
fi
# --- The CRM job ---
# Supermarket queues CRM work; a Hermes cron job carries it out every 15
# minutes. It first runs a cheap script that prints Supermarket's CRM state
# file; while that's unchanged, Hermes skips the run without using the AI.
CRM_JOB=supermarket-crm
CRM_JOB_FILE="$DATA/hermes-crm-job"
CRM_CHECK="$HERMES_HOME/scripts/supermarket-crm-check.sh"
CRM_PROMPT="Run Supermarket's CRM work: call the supermarket MCP server's crm_pending_actions tool and follow its instructions exactly, using the mcphub EspoCRM tools. Don't change anything in EspoCRM that it doesn't ask for. If it returns no actions and no sync, stop."
# crm_job_id: the ID of the existing job, if any
crm_job_id() {
"${HERMES[@]}" cron list --all 2> /dev/null \
| awk -v name="$CRM_JOB" '$1 ~ /^[0-9a-f]+$/ {id=$1} $1 == "Name:" && $2 == name {print id; exit}'
}
mkdir -p "$HERMES_HOME/scripts"
{
echo '#!/usr/bin/env bash'
echo "# Written by Supermarket (supermarket hermes). Hermes' $CRM_JOB job runs this"
echo "# before each run; while its output doesn't change, the run is skipped."
echo "if [ -r \"$DATA/crm-state\" ]; then cat \"$DATA/crm-state\"; else echo \"supermarket: not running\"; fi"
} > "$CRM_CHECK"
chmod 700 "$CRM_CHECK"
job="$(crm_job_id)"
if [ -n "$job" ] && [ "${1:-}" = "--reconfigure" ]; then
"${HERMES[@]}" cron remove "$job" > /dev/null && job=""
fi
if [ -z "$job" ]; then
out="$("${HERMES[@]}" cron create --name "$CRM_JOB" --deliver local --catch-up off \
--monitor-script "$(basename "$CRM_CHECK")" "every 15m" "$CRM_PROMPT" 2>&1)" || true
job="$(sed -nE 's/^Created job: ([0-9a-f]+).*/\1/p' <<< "$out")"
if [ -n "$job" ]; then
echo "Set up Hermes' $CRM_JOB job: every 15 minutes it carries out the CRM work Supermarket queues."
else
echo "Warning: couldn't set up Hermes' $CRM_JOB job:"
sed 's/^/ /' <<< "$out"
fi
fi
if [ -n "$job" ]; then
echo "$job" > "$CRM_JOB_FILE"
else
rm -f "$CRM_JOB_FILE"
fi
# --- Hermes' sandbox ---
need=()
for host in $hub_host; do