AAnvio
Backend

Folder Structure

How Anvio's monorepo is organized across apps, platform, packages, and core, and the dependency rule that keeps them decoupled.

5 min read

Anvio is a pnpm + Turborepo monorepo. The layout enforces one rule: domain logic never lives in an app. Apps only compose packages together.

Dependency rule

apps → platform → packages → core
  • apps/ — composition roots only (NestJS modules, CLI entry points). No business logic.
  • packages/platform — the single factory that wires a workspace's config into a running system (agent runtime, channel hub, tool gateway, MCP bridge, etc.).
  • packages/* — roughly 35 domain packages, each independently buildable, each owning one concern (channels, runtimes, models, tools, memory, souls, goals, kanban, ...).
  • packages/core — shared Zod schemas, TypeScript types, and ports (interfaces). Every other package depends on core; core depends on nothing else in the workspace.

The rule is structural, not just convention: packages/core has no dependency on any sibling package, and apps/* never import from each other. See ADR 0002 for why NestJS is confined to apps/.

Top-level layout

anvio/
  apps/           NestJS/CLI composition roots
  packages/       ~35 domain packages + core
  workspace/      Default local-first workspace (agents, memory, sessions, mcp config)
  configs/        Shared configs (observability dashboards, etc.)
  docker/         Container build assets
  docs/           Architecture docs, ADRs, phase notes
  scripts/        Repo automation (e.g. hermes skills import)
  tests/          Cross-package integration tests

apps/

AppPackageRole
apps/cli@anvio/cliPrimary entry point (anvio binary). Fully operational alone — no other app required.
apps/api@anvio/apiOptional REST layer (NestJS). Exposes agents, sessions, health, metrics, and channel webhook controllers (Matrix, WhatsApp, Teams).
apps/worker@anvio/workerOptional background consumer — drains the event bus and runs agent sessions detached from any client connection.
apps/gateway@anvio/gatewayUnified gateway daemon (anvio gateway start) — channels + worker + REST + WebSocket in one process, plus a static public/index.html.
apps/desktop—Scaffold only (apps/desktop/README.md). No Electron/Tauri build exists yet.

Each app has its own package.json, tsconfig.json, and src/main.ts. apps/api/src/ holds NestJS controllers directly (agents.controller.ts, sessions.controller.ts, health.controller.ts, metrics.controller.ts, matrix.controller.ts, whatsapp.controller.ts, teams.controller.ts) plus app.module.ts / app.service.ts.

Not yet shipped

apps/desktop is a README-only scaffold (tray icon, installer). A real Electron/Tauri build is tracked as a separate multi-week project — see docs/69-post-v1.17-gap-register.md, item R6.

packages/ (selected)

PackagePurpose
packages/coreZod schemas, ports, shared types. Depended on by everything.
packages/platformComposition factory — reads workspace/anvio.yaml and wires the full system (createPlatform, startUnifiedGateway, RuntimeRoutingAgentRuntime).
packages/agentsAgent runtime engine, orchestration, subagent delegation.
packages/runtimesRuntime providers: local, claude-code, cursor, codex, antigravity, docker, ssh, singularity, remote (daytona, modal).
packages/modelsModel provider adapters, ModelRouter, fallback chain, task classifier.
packages/channelsChannel adapters (Telegram, Discord, Slack, WhatsApp, ...) + ChannelHub.
packages/toolsBuilt-in tool gateway (ToolGateway, packages/tools/src/builtins/).
packages/integrationsMCP bridge, stdio client, integration registry, first-call approval gate.
packages/workspaceWorkspace loader, session store.
packages/storagePluggable storage backends (filesystem default; sqlite, postgresql, s3).
packages/memoryPluggable memory providers (filesystem default; sqlite, postgresql, qdrant, redis, honcho).
packages/eventsLocal in-process event bus + optional NATS.
packages/authOptional auth plugin, disabled by default.
packages/souls, packages/goals, packages/automation, packages/kanban, packages/blueprintsAdvanced Agent OS features (identity, persistent goals, cron/automation, kanban, workflow templates).
packages/executionCode execution sandbox (CodeExecutor, shell/python/node/go/docker).
packages/hooksEvent hook dispatcher.
packages/batchBatch processing engine.
packages/credentialsEncrypted credential pools for local runtime API keys.
packages/skillsSkills catalog and registry.
packages/personasBootstrap persona/profile templates.
packages/acpAgent Client Protocol server for editor integration (Cursor, etc.).
packages/voiceSTT/TTS pipeline.
packages/dbDrizzle ORM schema/migrations for PostgreSQL (Level 3).
packages/observabilityOpenTelemetry SDK init, span helper, Prometheus metrics registry.

Every package follows the same internal shape:

packages/<name>/
  src/
    index.ts       Public exports
    *.ts           Implementation
    *.spec.ts       Vitest unit tests
  package.json      @anvio/<name>
  tsconfig.json      extends tsconfig.base.json

Runtime provider subfolders

packages/runtimes/src/ mirrors the provider list directly:

packages/runtimes/src/
  local/            LocalRuntimeProvider — delegates to AgentRuntime directly
  claude-code/       ClaudeCodeRuntimeProvider (@anthropic-ai/claude-agent-sdk, OAuth)
  cursor/            CursorRuntimeProvider (ACP or `agent -p`)
  codex/             CodexRuntimeProvider (`codex exec`)
  antigravity/       AntigravityRuntimeProvider (`agy -p`)
  docker/            DockerRuntimeProvider (shells out to `docker`)
  ssh/               SshRuntimeProvider (execRemote over SSH)
  singularity/       SingularityRuntimeProvider (local HPC container exec)
  remote/            Daytona / Modal stubs (mock + HTTP exec)
  setup-token/       `anvio setup-token` vendor login wrappers
  external/          Shared external-CLI process helpers
  shared/            Common runtime provider utilities
  runtime-factory.ts           Instantiates every provider, resolves per-agent chain
  runtime-fallback-chain.ts    Chain building + auth-failure retry logic
  runtime-fallback-executor.ts Executes a runtime chain end to end

See Runtimes & Models overview for how these are selected at request time.

Naming and import conventions

  • Package names are always @anvio/<package-dir-name> (for example @anvio/channels, @anvio/runtimes).
  • Imports use NodeNext module resolution — relative imports inside a package must include the .js extension even though the source is .ts (see Coding Standards).
  • Barrel files: each package exports its public surface from src/index.ts; nothing outside core reaches into another package's internal files directly.
Edit on GitHub

Last updated on

On this page