# sqliteai/adam architecture

> Architecture diagram of the GitHub repository sqliteai/adam, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sqliteai/adam
- Repository: https://github.com/sqliteai/adam

## Overview

#### Purpose and Public Surface
Adam is an embeddable C AI-agent library packaged around `#include "adam.h"`. Applications configure a provider/model, maintain conversation history, and invoke the synchronous agent loop through `adam_run()` or structured-output APIs. Public API and core orchestration are centered in `src/adam.h` and `src/adam.c`; `src/main.c` is an interactive executable entry point.

#### Agent Orchestration Loop
`adam_run()` builds contextual prompts, applies guardrails/cache checks, dispatches inference, processes tool calls iteratively, and returns a final response. The loop owns per-iteration arena lifetime while callers own long-lived settings/history/result cleanup. Core stages are implemented in `src/adam.c`, `src/arena.c`, and `src/adam_cache.c`.

#### Model and Network Boundary
One provider-facing interface supports cloud APIs, compatible HTTP endpoints, mocked behavior, and local GGUF inference. Remote calls flow through platform-specific networking: Apple Foundation in `src/adam_net_apple.m`, curl on other supported native platforms in `src/adam_net_curl.c`, and caller-provided HTTP callbacks for WASM/no-curl builds. Local LLM and vision execution is isolated in `src/adam_local.c` and vendored `modules/llama.cpp`.

#### Conversation, Persistence, and Memory
History carries multi-turn context; sessions persist and restore conversations; long-term memory is retrieved/injected into prompts and updated after responses. These distinct persistence/context layers reside in `src/adam_session.c` and `src/adam_memory.c`, with SQLite plus hybrid memory/vector modules under `modules/sqlite`, `modules/sqlite-memory`, and `modules/sqlite-vector`. Feature gates can remove this entire SQLite-dependent capability.

#### Tools and Autonomous Modes
The agent loop turns model tool-call output into registered/built-in tool execution and appends results before another inference pass. Tool registration/execution lives in `src/adam_tools.c`; research and self-improvement are higher-level iterative modes in `src/adam_research.c` and `src/adam_evolve.c`. This boundary is where filesystem, shell, SQL, web, memory, and delegation capabilities must be sandboxed or disabled.

#### Streaming, Voice, and Media
Streaming exposes incremental generated tokens through callbacks (`src/adam_stream.c`). Voice composes audio capture/playback, speech-to-text, agent execution, and TTS across `src/adam_voice.c`, `src/adam_audio.c`, and `src/adam_stt_local.c`; it depends on `modules/miniaudio` and optionally `modules/whisper.cpp`. System TTS has platform implementations in `src/adam_tts_system.c` and `src/adam_tts_system.m`.

#### Database Embedding and Distribution Targets
SQLite and PostgreSQL extensions expose Adam as SQL functions, allowing SQL-aware agents to inspect and query the host database. Shared extension adapter logic is in `extensions/src/adam_ext_chat.c` and `extensions/src/adam_ext_session.c`; database-specific entry points are `extensions/sqlite/sqlite_adam.c` and `extensions/postgres/pg_adam.c`. Native library consumers are complemented by Swift and Flutter bindings at `packages/swift/adam.swift` and `packages/flutter/lib/sqlite_adam.dart`, with Android packaging under `packages/android`.

Main entry points: [`src/adam.c`](https://github.com/sqliteai/adam/blob/main/src/adam.c) (Agent loop), [`src/adam.h`](https://github.com/sqliteai/adam/blob/main/src/adam.h) (Public C API), [`extensions/src/adam_ext_chat.c`](https://github.com/sqliteai/adam/blob/main/extensions/src/adam_ext_chat.c) (SQL extensions), [`src/adam_voice.c`](https://github.com/sqliteai/adam/blob/main/src/adam_voice.c) (Voice pipeline)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core Agent"]
  node_public_api["Public C API<br/>header<br/>[adam.h]"]
  node_agent_loop["Agent loop<br/>orchestrator<br/>[adam.c]"]
  node_cli["Interactive executable<br/>entry point<br/>[main.c]"]
  node_arena["Iteration arena<br/>memory management<br/>[arena.c]"]
  node_cache["Guardrails &amp; cache<br/>[adam_cache.c]"]
end

subgraph group_providers["Inference &amp; Network"]
  node_provider_interface{{"Provider dispatch<br/>model interface<br/>[adam.c]"}}
  node_native_network["Native HTTP transports<br/>network adapter<br/>[adam_net_curl.c]"]
  node_local_inference["Local GGUF inference<br/>local runtime<br/>[adam_local.c]"]
  node_llama["llama.cpp<br/>vendored runtime<br/>[llama.cpp]"]
end

subgraph group_context["Context &amp; Persistence"]
  node_history_session[("History &amp; sessions<br/>conversation persistence<br/>[adam_session.c]")]
  node_memory[("Long-term memory<br/>retrieval memory<br/>[adam_memory.c]")]
  node_sqlite_memory[("SQLite memory stack<br/>optional storage")]
end

subgraph group_capabilities["Tools &amp; Media"]
  node_tools["Tool execution<br/>tool runtime<br/>[adam_tools.c]"]
  node_research["Research mode<br/>autonomous mode<br/>[adam_research.c]"]
  node_evolve["Self-improvement mode<br/>autonomous mode<br/>[adam_evolve.c]"]
  node_streaming["Token streaming<br/>callback API<br/>[adam_stream.c]"]
  node_voice["Voice pipeline<br/>voice orchestration<br/>[adam_voice.c]"]
  node_audio["Audio I/O<br/>media adapter<br/>[adam_audio.c]"]
end

subgraph group_distribution["Extensions &amp; Bindings"]
  node_database_extensions[("SQL extensions<br/>database extension<br/>[adam_ext_chat.c]")]
  node_sqlite_extension["SQLite entry point<br/>extension entry point<br/>[sqlite_adam.c]"]
  node_postgres_extension["PostgreSQL entry point<br/>extension entry point<br/>[pg_adam.c]"]
  node_language_bindings["Swift &amp; Flutter bindings<br/>language bindings<br/>[adam.swift]"]
end

node_cli -->|"uses"| node_public_api
node_public_api -->|"starts"| node_agent_loop
node_agent_loop -->|"allocates per iteration"| node_arena
node_agent_loop -->|"checks"| node_cache
node_agent_loop -->|"reads and appends context"| node_history_session
node_agent_loop -->|"retrieves and updates"| node_memory
node_agent_loop -->|"dispatches inference"| node_provider_interface
node_provider_interface -->|"remote requests"| node_native_network
node_provider_interface -->|"local model requests"| node_local_inference
node_local_inference -->|"runs through"| node_llama
node_agent_loop -->|"executes tool calls"| node_tools
node_tools -->|"returns tool results"| node_agent_loop
node_research -->|"iterates with"| node_agent_loop
node_evolve -->|"iterates with"| node_agent_loop
node_agent_loop -->|"emits tokens"| node_streaming
node_voice -->|"runs agent"| node_agent_loop
node_voice -->|"captures and plays audio"| node_audio
node_history_session -.->|"optional persistence"| node_sqlite_memory
node_memory -.->|"optional vector storage"| node_sqlite_memory
node_database_extensions -->|"adapts"| node_public_api
node_sqlite_extension -->|"loads"| node_database_extensions
node_postgres_extension -->|"loads"| node_database_extensions
node_language_bindings -->|"binds"| node_public_api

click node_public_api "https://github.com/sqliteai/adam/blob/main/src/adam.h"
click node_agent_loop "https://github.com/sqliteai/adam/blob/main/src/adam.c"
click node_cli "https://github.com/sqliteai/adam/blob/main/src/main.c"
click node_arena "https://github.com/sqliteai/adam/blob/main/src/arena.c"
click node_cache "https://github.com/sqliteai/adam/blob/main/src/adam_cache.c"
click node_provider_interface "https://github.com/sqliteai/adam/blob/main/src/adam.c"
click node_native_network "https://github.com/sqliteai/adam/blob/main/src/adam_net_curl.c"
click node_local_inference "https://github.com/sqliteai/adam/blob/main/src/adam_local.c"
click node_llama "https://github.com/sqliteai/adam/blob/main/modules/llama.cpp"
click node_history_session "https://github.com/sqliteai/adam/blob/main/src/adam_session.c"
click node_memory "https://github.com/sqliteai/adam/blob/main/src/adam_memory.c"
click node_sqlite_memory "https://github.com/sqliteai/adam/tree/main/modules/sqlite-memory"
click node_tools "https://github.com/sqliteai/adam/blob/main/src/adam_tools.c"
click node_research "https://github.com/sqliteai/adam/blob/main/src/adam_research.c"
click node_evolve "https://github.com/sqliteai/adam/blob/main/src/adam_evolve.c"
click node_streaming "https://github.com/sqliteai/adam/blob/main/src/adam_stream.c"
click node_voice "https://github.com/sqliteai/adam/blob/main/src/adam_voice.c"
click node_audio "https://github.com/sqliteai/adam/blob/main/src/adam_audio.c"
click node_database_extensions "https://github.com/sqliteai/adam/blob/main/extensions/src/adam_ext_chat.c"
click node_sqlite_extension "https://github.com/sqliteai/adam/blob/main/extensions/sqlite/sqlite_adam.c"
click node_postgres_extension "https://github.com/sqliteai/adam/blob/main/extensions/postgres/pg_adam.c"
click node_language_bindings "https://github.com/sqliteai/adam/blob/main/packages/swift/adam.swift"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_public_api,node_agent_loop,node_cli,node_arena,node_cache toneBlue
class node_provider_interface,node_native_network,node_local_inference,node_llama toneAmber
class node_history_session,node_memory,node_sqlite_memory toneMint
class node_tools,node_research,node_evolve,node_streaming,node_voice,node_audio toneRose
class node_database_extensions,node_sqlite_extension,node_postgres_extension,node_language_bindings toneIndigo
```

## Components

### Core Agent

- [Public C API](https://github.com/sqliteai/adam/blob/main/src/adam.h): header · Embeddable API including adam_run and structured output. · `src/adam.h`
- [Agent loop](https://github.com/sqliteai/adam/blob/main/src/adam.c): orchestrator · Builds prompts, invokes inference, and iterates tool calls. · `src/adam.c`
- [Interactive executable](https://github.com/sqliteai/adam/blob/main/src/main.c): entry point · `src/main.c`
- [Iteration arena](https://github.com/sqliteai/adam/blob/main/src/arena.c): memory management · Owns temporary allocations for each agent iteration. · `src/arena.c`
- [Guardrails & cache](https://github.com/sqliteai/adam/blob/main/src/adam_cache.c): cache · `src/adam_cache.c`

### Inference & Network

- [Provider dispatch](https://github.com/sqliteai/adam/blob/main/src/adam.c): model interface · Routes cloud, compatible HTTP, mock, and local inference. · `src/adam.c`
- [Native HTTP transports](https://github.com/sqliteai/adam/blob/main/src/adam_net_curl.c): network adapter · Uses Foundation on Apple and curl on other native platforms. · `src/adam_net_curl.c`
- [Local GGUF inference](https://github.com/sqliteai/adam/blob/main/src/adam_local.c): local runtime · `src/adam_local.c`
- [llama.cpp](https://github.com/sqliteai/adam/blob/main/modules/llama.cpp): vendored runtime · `modules/llama.cpp`

### Context & Persistence

- [History & sessions](https://github.com/sqliteai/adam/blob/main/src/adam_session.c): conversation persistence · Maintains multi-turn context and restores saved conversations. · `src/adam_session.c`
- [Long-term memory](https://github.com/sqliteai/adam/blob/main/src/adam_memory.c): retrieval memory · Retrieves prompt context and updates memory after responses. · `src/adam_memory.c`
- [SQLite memory stack](https://github.com/sqliteai/adam/tree/main/modules/sqlite-memory): optional storage · SQLite, hybrid memory, and vector modules can be feature-gated out. · `modules/sqlite-memory`

### Tools & Media

- [Tool execution](https://github.com/sqliteai/adam/blob/main/src/adam_tools.c): tool runtime · Executes registered and built-in tool calls under sandbox policy. · `src/adam_tools.c`
- [Research mode](https://github.com/sqliteai/adam/blob/main/src/adam_research.c): autonomous mode · `src/adam_research.c`
- [Self-improvement mode](https://github.com/sqliteai/adam/blob/main/src/adam_evolve.c): autonomous mode · `src/adam_evolve.c`
- [Token streaming](https://github.com/sqliteai/adam/blob/main/src/adam_stream.c): callback API · `src/adam_stream.c`
- [Voice pipeline](https://github.com/sqliteai/adam/blob/main/src/adam_voice.c): voice orchestration · Composes capture, STT, agent execution, playback, and TTS. · `src/adam_voice.c`
- [Audio I/O](https://github.com/sqliteai/adam/blob/main/src/adam_audio.c): media adapter · `src/adam_audio.c`

### Extensions & Bindings

- [SQL extensions](https://github.com/sqliteai/adam/blob/main/extensions/src/adam_ext_chat.c): database extension · Exposes chat and session functions to SQLite and PostgreSQL hosts. · `extensions/src/adam_ext_chat.c`
- [SQLite entry point](https://github.com/sqliteai/adam/blob/main/extensions/sqlite/sqlite_adam.c): extension entry point · `extensions/sqlite/sqlite_adam.c`
- [PostgreSQL entry point](https://github.com/sqliteai/adam/blob/main/extensions/postgres/pg_adam.c): extension entry point · `extensions/postgres/pg_adam.c`
- [Swift & Flutter bindings](https://github.com/sqliteai/adam/blob/main/packages/swift/adam.swift): language bindings · Native consumers also include Android packaging. · `packages/swift/adam.swift`

## Connections

- Interactive executable → Public C API: uses
- Public C API → Agent loop: starts
- Agent loop → Iteration arena: allocates per iteration
- Agent loop → Guardrails & cache: checks
- Agent loop → History & sessions: reads and appends context
- Agent loop → Long-term memory: retrieves and updates
- Agent loop → Provider dispatch: dispatches inference
- Provider dispatch → Native HTTP transports: remote requests
- Provider dispatch → Local GGUF inference: local model requests
- Local GGUF inference → llama.cpp: runs through
- Agent loop → Tool execution: executes tool calls
- Tool execution → Agent loop: returns tool results
- Research mode → Agent loop: iterates with
- Self-improvement mode → Agent loop: iterates with
- Agent loop → Token streaming: emits tokens
- Voice pipeline → Agent loop: runs agent
- Voice pipeline → Audio I/O: captures and plays audio
- History & sessions → SQLite memory stack: optional persistence
- Long-term memory → SQLite memory stack: optional vector storage
- SQL extensions → Public C API: adapts
- SQLite entry point → SQL extensions: loads
- PostgreSQL entry point → SQL extensions: loads
- Swift & Flutter bindings → Public C API: binds
