Agents and Chatwork Router

How every message is routed to one Dot agent, grounded with built-in cited knowledge, logged and made explainable.

Chatwork Router is the bridge’s built-in agent-routing and knowledge layer. It is not an external service: it is a Python package (apps/bridge/cloudrag/) over PostgreSQL, called inside the LLM step of the pipeline. It is mandatory in the sense that every AI reply passes through it; it is resilient in the sense that no store or optional embeddings failure can block a reply.

The registry is the product

Each agent is one file in agents/registry/. The same file is read by:

  • the bridge at start-up (validated; a broken file refuses to start the router),
  • python -m cloudrag.seed, which upserts the agents into cloudrag_agents (and with --embed embeds their intents when an embeddings provider is enabled),
  • this site, which renders /agents and the routing playground from it,
  • the conformance fixtures (agents/fixtures/routing.json) that the Python router and the TypeScript mirror must both satisfy.

Fields that matter for routing: routing.priority (tie-break), routing.keywords with weights, routing.channel_boost, routing.min_score, routing.intents (sentences for the optional semantic layer), routing.always_handoff, and default: true on exactly one agent.

Two layers, one score

  1. Lexical layer (always on). Text and keywords are normalised (lowercase, Arabic diacritics and tatweel stripped, أإآ→ا, ى→ي, ة→ه, ؤ→و, ئ→ي). Arabic keywords match as substrings; Latin ones on word boundaries. lexical = min(1, Σ weight) plus the channel boost when something matched.
  2. Optional semantic layer (CLOUDRAG_EMBEDDINGS=provider). The message is embedded once (CLOUDRAG_EMBEDDING_MODEL, 1536 dimensions). Each agent’s score is the best cosine similarity to its embedded intents. final = 0.55 × semantic + 0.45 × lexical.

The best agent above its min_score wins; ties go to the lower priority number; otherwise the default agent (Concierge) takes the message. A tenant can pin an agent (cloudrag_pinned_agent on the tenant record) or disable one (PATCH /cloudrag/agents/{slug}); the default agent cannot be disabled.

Built-in knowledge base and grounding

By default (CLOUDRAG_EMBEDDINGS=off), the knowledge base runs entirely inside PostgreSQL with zero outbound calls: chunks are stored as text and retrieved by normalized term overlap for agents with knowledge.retrieval: true, then appended to the system prompt as numbered passages so the model cites them as [n] and says what it does not know when nothing matches. With CLOUDRAG_EMBEDDINGS=provider, hybrid pgvector similarity is merged in. Citations travel with the reply (routing.citations in the API response).

Sources are pasted text or public https pages (POST /api/v1/tenants/{slug}/cloudrag/sources). URLs pass the same SSRF guard as tenant MCP servers; pages are reduced to prose, chunked (~800 characters, paragraph-aware, 120-character overlap) and stored. Identical content for the same workspace is not indexed twice (deduplicated: true); new content replaces every chunk of that source in one transaction.

Policy handoff

Guardian has always_handoff: true. When it is chosen, the reply ends with a handoff even if the model forgot the tag: the pipeline opens the ticket, posts the private note with the agent’s name and sets the conversation to open. Keyword handoffs (“I want a human”) still run before any routing, exactly as before.

Explainability

Every decision is logged in cloudrag_routing_log without the message text (only its hash): agent, reason (keywords, semantic, default, pinned), score, layers used, the top candidates with their matched keywords, and latency. GET /api/v1/tenants/{slug}/cloudrag/decisions returns the recent log and a 30-day breakdown per agent; POST …/cloudrag/route previews a decision without calling the model.

Degradation, in order

Situation Behaviour
No DOT_DATABASE_URL Lexical layer on the registry files; no knowledge; source: registry_files in every trace.
Database up, agents not seeded Same as above until python -m cloudrag.seed runs.
CLOUDRAG_EMBEDDINGS=off (default) Lexical routing and the built-in knowledge base run inside PostgreSQL with no external call.
Embeddings provider down (when enabled) Lexical routing and built-in text retrieval continue; sources still index as searchable text.
Routing log unwritable Reply proceeds; a warning is logged.

Nothing in this list produces an error for the customer.

In the operator dashboard

The “Agents & knowledge” view lists the six agents with a per-workspace switch, previews a routing decision for any message, manages knowledge sources and shows the recent decisions. The overview shows the 30-day split of decisions per agent. Agent rows are cached per process for 30 seconds and invalidated on a switch, so routing costs one database read per workspace every half minute, not one per message.

Type a keyword to search titles, sections, headings and content.