Dataflow and automation
ESP-Claw is an event-driven Agent framework: an Event is the trigger for Agent messages and automation.
Definitions
Section titled “Definitions”An Event is the message carrier in ESP-Claw: id, source, target, type, chat id, sender, text or JSON payload, timestamp, session policy, and more.
claw_event_t (see claw_event.h) packs those fields.
Event Router
Section titled “Event Router”claw_event_router is the event scheduler: it dispatches Events according to rules.
router_rules.json is ESP-Claw’s automation rules file: it defines how Events that enter claw_event_router are handled.
The order of rules in router_rules.json is evaluation order. Each rule typically has:
id: unique rule id.ack: optional acknowledgement after a match.consume_on_match: whether matching stops further evaluation.match: match conditions.actions: action list, for example:Action Typical use call_capRun a capability without the LLM run_agentAsynchronously submit to claw_core; Agent response is published back viaagent_responseeventrun_scriptRun a Lua script send_messageSend IM via outbound binding emit_eventEmit a new Event back into claw_event_routerdropDrop the Event
basic_demo ships a starter router_rules.json for basic IM flows. See router_rules.json.
Full syntax is captured in the JSON Schema.
router_rules.json JSON Schema
Event Router workflow
Section titled “Event Router workflow”Rule matching flow
Section titled “Rule matching flow”After an Event enters the router, it is dequeued and walked against the rule set. The diagram shows the full logic:
agent_response Event and Outbound Messages
Section titled “agent_response Event and Outbound Messages”When the run_agent action is triggered, the Agent’s response is not returned synchronously. Instead, it is published back to the Event Router as an asynchronous event of type agent_response.
To send the Agent’s response to an IM channel, you need to configure a routing rule for agent_response, for example:
The channel / chat_id fields usually come from target_channel / target_chat_id passed when triggering run_agent.
Agent-request cancellation and queue cleanup
Section titled “Agent-request cancellation and queue cleanup”After a run_agent action is submitted to claw_core, a request_id is allocated and recorded for later cancellation or tracking.
Accordingly, the framework offers two cancellation layers (C APIs):
claw_core_cancel_request(request_id): cancel the currently executing (in-flight) Agent request.claw_event_router_cancel_event(event_id): mark and skip one pending Event in the queue.claw_event_router_purge_queue(event_type_filter, source_cap_filter, &out_cancelled): batch-mark and skip pending queue Events (optional filters by event type and source cap).
agent_stage Event and Execution Progress Notifications
Section titled “agent_stage Event and Execution Progress Notifications”Besides the final agent_response, claw_core can also publish agent_stage events during multi-round tool calling, which can be used to push staged tool-call progress information.
The agent_stage event structure is:
source_cap:claw_coreevent_type:agent_stageevent.text: staged information during tool calls
To auto-publish agent_stage events, adjust this menuconfig option: (Top) → Component config → ESP-Claw Core → Agent stage notification verbosity
- With Verbose,
claw_corepublishesagent_stageevents to Event Router on turns that contain tool calls. - With Simple,
agent_stageevents are not published at the routing layer.
agent_stage events can be routed to channels such as IM to provide real-time progress updates.
For example, basic_demo includes rule agent_stage_im_notify in router_rules.json. When Verbose is enabled and this rule matches, it sends {{event.text}} from agent_stage back to the source IM conversation.
Scheduled Dispatch
Section titled “Scheduled Dispatch”cap_scheduler provides time-based event triggering, supporting three types: once, interval (repeating), and cron (calendar expression, 5 segments, no seconds).
The scheduler is only responsible for publishing Events on time; the specific behavior after triggering is determined by the Event Router’s rules. Therefore, adding a scheduled task typically requires two steps:
- Add a schedule item (via
schedules.jsonorscheduler_add). - Add a matching routing rule (via
router_rules.jsonoradd_router_rule).
Typical matching: the schedule item sets event_type: "schedule" + event_key, and the routing rule hits it using match.event_type + match.event_key.
Scheduler Console Commands
Section titled “Scheduler Console Commands”Scheduler LLM Tools
Section titled “Scheduler LLM Tools”cap_scheduler registers tools like scheduler_list, scheduler_get, scheduler_add, scheduler_enable, scheduler_disable, scheduler_pause, scheduler_resume, scheduler_trigger_now, and scheduler_reload. These tools are available after activating the cap_scheduler Skill.
cap_router_mgr registers tools like list_router_rules, get_router_rule, add_router_rule, update_router_rule, delete_router_rule, and reload_router_rules.
Event Router Console commands
Section titled “Event Router Console commands”Event Router provides two sets of Console commands. The auto command comes from basic_demo, and the event_router command comes from cap_router_mgr. Both are functionally equivalent and operate on the same set of rules.
-
auto emit_messagesynthesizes an IM-style message Event:Arg Meaning Example source_capSource capability name qq_gatewaychannelSource channel qqchat_idChat id (group / topic, etc.) 123456text...Message text (spaces allowed; words concatenate) hello world -
auto emit_triggersynthesizes a trigger Event:Arg Meaning Example source_capSource capability name testerevent_typeEvent type for match.event_typetriggerevent_keyEvent key for match.event_keysmoke_testpayload_jsonJSON object payload '{"ok":true}' -
auto lastprints the last routing result, e.g.:Field Meaning matchedAny rule matched matched_rulesNumber of matched rules action_countActions executed failed_actionsActions that failed route0= PASS (not consumed),1= CONSUMEDfirst_rule_ididof first matched ruleackRendered ackfrom the matched rulelast_errorLast action error code