How sqliteai/adam works
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: adam.c Agent loop · adam.h Public C API · adam_ext_chat.c SQL extensions · adam_voice.c Voice pipeline
Components 22
Core Agent
- Public C API · Embeddable API including adam_run and structured output.
src/adam.h - Agent loop · Builds prompts, invokes inference, and iterates tool calls.
src/adam.c - Interactive executable
src/main.c - Iteration arena · Owns temporary allocations for each agent iteration.
src/arena.c - Guardrails & cache
src/adam_cache.c
Inference & Network
- Provider dispatch · Routes cloud, compatible HTTP, mock, and local inference.
src/adam.c - Native HTTP transports · Uses Foundation on Apple and curl on other native platforms.
src/adam_net_curl.c - Local GGUF inference
src/adam_local.c - llama.cpp
modules/llama.cpp
Context & Persistence
- History & sessions · Maintains multi-turn context and restores saved conversations.
src/adam_session.c - Long-term memory · Retrieves prompt context and updates memory after responses.
src/adam_memory.c - SQLite memory stack · SQLite, hybrid memory, and vector modules can be feature-gated out.
modules/sqlite-memory
Tools & Media
- Tool execution · Executes registered and built-in tool calls under sandbox policy.
src/adam_tools.c - Research mode
src/adam_research.c - Self-improvement mode
src/adam_evolve.c - Token streaming
src/adam_stream.c - Voice pipeline · Composes capture, STT, agent execution, playback, and TTS.
src/adam_voice.c - Audio I/O
src/adam_audio.c
Extensions & Bindings
- SQL extensions · Exposes chat and session functions to SQLite and PostgreSQL hosts.
extensions/src/adam_ext_chat.c - SQLite entry point
extensions/sqlite/sqlite_adam.c - PostgreSQL entry point
extensions/postgres/pg_adam.c - Swift & Flutter bindings · Native consumers also include Android packaging.
packages/swift/adam.swift
Connections 23
- 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