How anentrypoint/casey works
Purpose and entry points
Casey is a Node.js orchestration service for animal-disease reports received over messaging channels. It creates and manages cases, runs an agent through Freddie, persists workflow/audit data in Thatcher, and exposes an operator dashboard.
Primary commands are implemented in bin/casey.js; supervised production startup is coordinated by bin/worker.js, src/supervisor.js, and src/supervisor-machine.js.
Runtime assembly and channel ingress
src/casey.js is the top-level composition root: it assembles the store, agent host, channel gateway/adapters, hooks, and logging. src/gateway-hooks.js supplies the case-aware inbound handler, while src/hooks/handler.js and src/hooks/heuristics.js cover message handling paths and deterministic interaction behavior.
Ingress normalizes a channel message into case context, deduplicates it using the channel message ID, appends an inbound event, then either handles suitable fixed interactions or invokes an agent turn. Channel credentials and WhatsApp webhook verification/HMAC boundary are environment-driven.
Case persistence, workflow, and audit boundary
Thatcher is the system of record, configured in thatcher.config.yml. src/case-store.js is the principal adapter around it: it owns find-or-create behavior, locking, event persistence, transitions, pagination, and compatibility handling. src/case-runtime.js exposes the live store to the plugin/runtime.
The durable model is case/contact/event oriented. The key architectural invariant is append-only observability: inbound and outbound messages, agent actions, observations, transitions, and operator actions become event records. Core record shaping and write semantics are separated in src/core/event-log.js, src/core/write-path.js, and src/core/raw-log.js.
Agent execution and controlled autonomy
Freddie provides the agent harness, sessions, channel adapters, and model-provider resolution; Casey contributes domain context and tools. plugins/case-tools/plugin.js registers Casey capabilities, and src/case-tools.js implements case read/list/update/observe/transition tools.
src/attn.js, src/conversation-state.js, and src/conversation-spec.js define the conversation/case-context layer supplied to turns. Tool operations are constrained by each case’s autonomy mode (auto, assisted, observe), making the tool boundary—not free-form model text—the control point for mutations. Model errors, timeouts, or empty responses are recorded rather than converted into scripted contact replies.
Domain interpretation and rule packs
The repository has a modular domain-analysis layer. src/engine/rule-engine.js evaluates configured rules; reusable case interpretation primitives live in src/core/interpretation.js and src/core/observation.js. Domain packs, including animal-health and water-point definitions, are in src/packs/animal-health.js and src/packs/water-point.js.
This layer can enrich observations, quality flags, aggregation, and reporting without making it the authority for escalation: operators retain workflow and priority control. Related analytical outputs are produced through src/report.js, src/report-analytics.js, and src/workload.js.
Operator dashboard and outbound control
src/dashboard/server.js serves an Express-backed, no-build SPA. API routes split operational concerns across src/dashboard/routes/cases.js, src/dashboard/routes/operations.js, and src/dashboard/routes/reports.js; session authentication is handled by src/dashboard/auth.js.
The browser client in src/dashboard/public/app.js reads cases/timelines and lets operators edit records, override transitions, claim work, and send human replies. Operator mutations pass back through the store/workflow boundary and are audited. Replies cross the channel boundary only through controlled outbound handling; opt-out and autonomy state must be respected.
Infrastructure and external dependencies
Material runtimes/services are Node.js, Freddie (agent/channel runtime), Thatcher (CRUD/workflow/RBAC/audit), Express, Discord, and Meta WhatsApp Graph/webhooks. Optional media processing in src/hooks/media.js and src/llm.js can use OpenAI, Anthropic, or ElevenLabs and is explicitly opt-in because it sends user media to external providers.
src/supervisor.js isolates the parent process from application imports, forks a worker, restarts on crashes, and can watch Casey/Freddie source paths for reload. CI runs the repository’s dependency-light lint gate through .github/workflows/ci.yml.
Main entry points: case-store.js Case store · gateway-hooks.js Gateway hooks · server.js Dashboard server · rule-engine.js Rule engine
Components 18
Runtime & ingress
- Supervisor · Forks and restarts the isolated worker.
src/supervisor.js - Worker startup
bin/worker.js - Casey composition · Assembles stores, channels, hooks, and agent host.
src/casey.js - Discord & WhatsApp · WhatsApp webhook verification and HMAC are enforced at this boundary.
- Gateway hooks · Normalizes inbound messages into case-aware handling.
src/gateway-hooks.js - Optional media LLM · Opt-in media processing may send user media externally.
src/llm.js
Case system
- Case store · Owns find-or-create, locking, events, transitions, and pagination.
src/case-store.js - Event write path · Shapes append-only events and raw audit records.
src/core/write-path.js - Thatcher · Provides CRUD, workflow, RBAC, and audit persistence.
thatcher.config.yml
Controlled agent
- Case runtime · Exposes the live store to the agent plugin.
src/case-runtime.js - Freddie · Provides sessions, channel adapters, and model resolution.
- Case tools plugin · Registers constrained case read and mutation capabilities.
plugins/case-tools/plugin.js - Conversation context · Supplies attention and case state to agent turns.
src/conversation-state.js
Domain analysis
- Rule engine · Evaluates configured interpretation rules.
src/engine/rule-engine.js - Domain packs · Animal-health and water-point interpretation definitions.
src/packs/animal-health.js - Reporting analytics · Produces analytical and workload outputs.
src/report-analytics.js
Operator dashboard
- Dashboard server · Serves the SPA and operational case, operation, and report routes.
src/dashboard/server.js - Operator SPA · Lets operators inspect timelines, control workflow, claim work, and reply.
src/dashboard/public/app.js
Connections 21
- Supervisor → Worker startup: forks and restarts
- Worker startup → Casey composition: starts
- Casey composition → Gateway hooks: installs
- Discord & WhatsApp → Gateway hooks: inbound messages
- Gateway hooks → Case store: deduplicate and append inbound event
- Gateway hooks → Freddie: agent turn when needed
- Gateway hooks → Optional media LLM: optional media processing
- Case store → Event write path: writes audit events
- Event write path → Thatcher: append-only persistence
- Case store → Thatcher: cases, contacts, workflow
- Case runtime → Case store: live store access
- Freddie → Case tools plugin: loads plugin
- Case tools plugin → Case runtime: reads and mutates by autonomy mode
- Conversation context → Freddie: case context for turns
- Case store → Rule engine: case observations
- Domain packs → Rule engine: configured rules
- Rule engine → Reporting analytics: enriched observations
- Dashboard server → Case store: audited operator mutations
- Dashboard server → Reporting analytics: report APIs
- Operator SPA → Dashboard server: authenticated API requests
- Case store → Discord & WhatsApp: controlled outbound replies