How swarmclawai/swarmclaw works
1. Overall shape
SwarmClaw is a self-hosted, full-stack TypeScript control plane for autonomous agents. The same codebase serves three entrypoints: the Next.js dashboard/API, a Node CLI (swarmclaw), and an Electron desktop shell. Most behavior is implemented in shared server-side libraries so the UI, CLI, and desktop app all drive the same runtime.
2. Primary UI surface
The dashboard lives under src/app and uses Next.js App Router pages plus server route handlers. The UI is organized by domain: agents, chats, tasks, runs, schedules, memory, providers, connectors, skills, protocols, quality, missions, chatrooms, and settings. Components and feature modules mostly stay thin and call into shared query/client helpers rather than implementing business logic themselves.
3. API layer as the control plane
The src/app/api/**/route.ts tree is the main inbound API surface. It exposes CRUD and action endpoints for nearly every domain, plus special routes for health, auth, uploads/files, sharing, exports/imports, A2A discovery, OpenClaw, SwarmDock, SwarmFeed, MCP servers, and quality gates. These routes translate HTTP into domain services in src/lib/server/**.
4. Server-side domain services
The real architecture lives in src/lib/server. It is split into repositories, services, schedulers, and execution engines for each domain: agents, chats, tasks, runs, protocols, schedules, memory, connectors, gateways, skills, extensions, credentials, wallets, projects, and more. The pattern is consistent: route handlers call services, services call repositories and runtime helpers, and repositories isolate persistence details.
5. Agent/chat execution pipeline
The core runtime path is chat execution. src/lib/server/chat-execution/** and src/lib/server/agents/** handle prompt construction, tool routing, stream execution, turn finalization, continuation, compaction, and state tracking. This is where agent turns become model calls, tool events, persisted messages, follow-up actions, and connector deliveries. The chat UI is essentially a live view over this pipeline.
6. Autonomy and background work
Long-running behavior is handled by the runtime layer under src/lib/server/runtime/** and adjacent autonomy modules. This includes heartbeat loops, wake scheduling, queueing, orphan recovery, daemon control, estop/guardrails, scheduled-run preflight, and session-run management. The runtime is what turns agents from interactive chat participants into autonomous workers that can resume, retry, and recover after interruption.
7. Orchestration primitives
SwarmClaw supports structured execution beyond simple chat: tasks, runs, protocols, missions, schedules, and structured sessions. src/lib/server/protocols/** models DAG-style flows with branches, joins, loops, subflows, and swarms; tasks and runs provide durable execution records and handoff packets; schedules triggers work over time; missions wraps operational workflows with budgets and reports. This is the orchestration boundary diagrammatically: user intent enters, and one of these durable work primitives executes it.
8. Provider and gateway abstraction
Model access is abstracted through src/lib/providers/** and src/lib/server/provider*. The system supports both API providers and CLI-backed providers, plus OpenAI-compatible endpoints, local runtimes like Ollama, and OpenClaw gateways. Provider health, model discovery, diagnostics, and per-agent provider config are first-class because provider choice materially changes how execution streams, tool use, and context limits behave.
9. External integrations
Connectors are a major boundary: src/lib/server/connectors/** and the related UI/API manage Slack, Discord, Telegram, WhatsApp, Teams, Matrix, email, file queues, OpenClaw, SwarmDock, and SwarmFeed. The connector subsystem includes inbound delivery, outbound outboxes, pairing/access, policy checks, media handling, and connector-specific runtime state. It is effectively the event bridge between SwarmClaw and outside communication channels.
10. Memory, knowledge, and artifacts
Durable context is handled by the memory and knowledge subsystems plus documents/artifacts. src/lib/server/memory/** manages tiers, graph/retrieval, dreams/reflection, consolidation, and session memory scope; knowledge handles sources and hygiene; artifacts, documents, and files expose persisted outputs and shared references. This is the long-term persistence boundary for agent learning and operator recall, separate from transient chat transcripts.
11. Skills, extensions, and tool surfaces
Agent capabilities are assembled from skills, MCP servers, built-in tools, and extensions. src/lib/server/session-tools/** builds the per-session tool set, while skills, extensions, and mcp-* modules manage discovery, install/update, conformance, approvals, and managed resources. This is the capability layer that determines what an agent can actually do in a turn.
12. Persistence and local runtime model
The repo is designed around a local data directory and workspace model rather than a remote central database. Repositories under src/lib/server/*-repository.ts persist durable records for chats, agents, tasks, schedules, memory, connectors, shares, and configs, while runtime helpers create task/session workspaces and file-backed snapshots where needed. The architecture is therefore “local control plane with durable state,” not a stateless API server.
13. Desktop, CLI, and deployment wrappers
bin/, src/cli, and electron/ wrap the same application into different operating modes. The CLI exposes operational commands for setup, diagnostics, updates, schedules, tasks, runs, providers, and gateways. Electron manages the desktop shell, server lifecycle, updater, and app paths. Container and hosted deploy files (Dockerfile, docker-compose.yml, fly.toml, railway.json, render.yaml) all point to the same core runtime.
14. Observability, quality, and release safety
Quality and observability are not sidecars; they are built into the product. src/lib/server/observability, src/lib/server/quality, src/lib/server/eval, and the /quality APIs provide architecture-health checks, release-readiness scoring, eval gating, and trace export. This creates an operator feedback loop around the runtime so execution, provider health, and autonomy can be inspected and gated before release.
15. Key architectural boundary
The clearest diagram boundary is between the interactive shell layer and the durable runtime layer. UI pages, CLI commands, and Electron all converge on the same API routes and shared server libraries; those server libraries then fan out to provider adapters, connector bridges, storage repositories, and runtime workers. That shared-control-plane design is the central architectural idea of the repository.
Main entry points: server Server libs · chat-execution Chat engine · runtime Autonomy runtime · api API routes
Components 21
Entry points
User-facing and operational shells that drive the shared backend runtime.
- Dashboard · Primary UI surface and domain pages under the App Router.
src/app - CLI · Operational commands and binary wrapper for setup, diagnostics, updates, and runtime control.
src/cli - Desktop shell · Desktop wrapper, lifecycle management, paths, and updater integration.
electron
Control plane
Next.js routes and shared server services that implement the product.
- API routes · Main inbound API surface translating requests into domain services.
src/app/api - Server libs · Shared server-side services, repositories, and helpers used by UI, CLI, and desktop.
src/lib/server
Runtime
Execution, autonomy, orchestration, and persistence layers.
- Chat engine · Prompt building, tool routing, streaming, continuation, and turn finalization.
src/lib/server/chat-execution - Agent runtime · Agent lifecycle, delegation, swarms, guardrails, and thread/session state.
src/lib/server/agents - Autonomy runtime · Heartbeats, wake loops, queueing, recovery, daemon control, and session-run management.
src/lib/server/runtime - Protocols · DAG-style flows, branches, loops, subflows, swarms, and protocol runs.
src/lib/server/protocols - Tasks & runs · Durable task execution, run ledger, handoff packets, and workflow state.
src/lib/server/tasks - Persistence · Local durable storage, repositories, storage context, and workspace-backed state.
src/lib/server/persistence
Integrations
Providers, connectors, memory, and external capability surfaces.
- Memory · Tiered memory, retrieval, dreams, reflection, consolidation, and session scope.
src/lib/server/memory - Knowledge · Knowledge sources, import, hygiene, and retrieval-facing content.
src/lib/server/knowledge-sources.ts - Connectors · Slack, Discord, email, file queues, SwarmDock, SwarmFeed, and other external channels.
src/lib/server/connectors - Providers · OpenAI-compatible, CLI-backed, Ollama, OpenClaw, and provider diagnostics.
src/lib/providers - Session tools · Per-session tool assembly for files, web, memory, skills, delegates, and platform actions.
src/lib/server/session-tools - Skills · Skill discovery, runtime resolution, learned skills, and skill suggestions.
src/lib/server/skills - Extensions · Builtin and managed extensions plus approval and resource handling.
src/lib/server/extensions.ts
Ops
Quality, observability, deployment, and wrapper tooling.
- Quality · Architecture health, release readiness, eval suites, and operator safety checks.
src/lib/quality - Observability · OpenTelemetry, local observability, activity, and runtime diagnostics.
src/lib/server/observability - Deploy · Container and platform deployment manifests for hosted and self-hosted operation.
Dockerfile
Connections 30
- Dashboard → API routes: calls · UI actions resolve through route handlers and shared client helpers.
- CLI → Server libs: uses · CLI commands invoke the same server-side services as the UI.
- Desktop shell → Server libs: boots · Desktop shell starts and supervises the shared backend runtime.
- API routes → Server libs: routes to · HTTP endpoints dispatch into shared domain services.
- Server libs → Chat engine: drives · Chat requests flow into the execution pipeline.
- Server libs → Agent runtime: drives · Agent state, delegation, and lifecycle are handled here.
- Server libs → Autonomy runtime: coordinates · Background work and recovery are managed by the runtime layer.
- Server libs → Protocols: orchestrates · Structured workflows execute through protocol services.
- Server libs → Tasks & runs: persists · Durable task and run records are written and queried here.
- Server libs → Memory: stores · Long-term context is consolidated and retrieved from memory subsystems.
- Server libs → Knowledge: manages · Knowledge sources and hygiene are exposed as a server boundary.
- Server libs → Connectors: bridges · External channel delivery and inbound webhooks pass through connector services.
- Server libs → Providers: adapts · Model access and diagnostics are abstracted behind provider adapters.
- Server libs → Session tools: assembles · Session-specific tool sets are built before each turn.
- Server libs → Skills: resolves · Skill discovery and runtime eligibility feed agent capability selection.
- Server libs → Extensions: loads · Extensions contribute capabilities and managed resources.
- Server libs → Persistence: uses · Repositories and workspace-backed state provide durable storage.
- Server libs → Quality: checks · Quality services gate architecture and release readiness.
- Server libs → Observability: traces · Runtime activity, logs, and telemetry are emitted for inspection.
- Autonomy runtime → Agent runtime: wakes · Heartbeat and daemon loops resume or retry agent work.
- Autonomy runtime → Tasks & runs: queues · Background execution is coordinated through task and run queues.
- Chat engine → Providers: calls models · Prompted turns stream through model providers.
- Chat engine → Session tools: invokes · Tool calls are routed through the session tool layer.
- Chat engine → Memory: writes · Turns can mutate or retrieve durable context.
- Connectors → Chat engine: delivers · Inbound and outbound messages can attach to chat sessions.
- Protocols → Agent runtime: delegates · Protocol steps can spawn or coordinate agent work.
- Tasks & runs → Autonomy runtime: resumes · Durable work records are rehydrated by runtime recovery.
- Quality → API routes: exposed by · Operator-facing eval and readiness checks are surfaced via API routes.
- Deploy → Desktop shell: packages · Deployment artifacts include the desktop build path.
- Deploy → Server libs: hosts · Container and platform manifests run the shared backend.