# coder/agentapi architecture

> Architecture diagram of the GitHub repository coder/agentapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/coder/agentapi
- Repository: https://github.com/coder/agentapi

## Overview

##### Project Type and Purpose
- AgentAPI is a full-stack application providing an HTTP API and CLI to control coding agents (Claude Code, Goose, Aider, Codex) via a terminal emulator.
- It includes:
- A Go-based HTTP server and CLI (in cmd/ and main.go)
- Core libraries for terminal handling, message parsing, formatting, logging
- A Next.js/React chat frontend under the chat/ directory

##### File-Structure Analysis
- Top-level
- cmd/attach: CLI command to attach to a running agent
- cmd/server: HTTP server command to launch and manage an agent process
- lib/httpapi: handlers for /message, /messages, /status, /events; OpenAPI schema
- lib/termexec: spawns agent subprocess in a pseudo-TTY
- lib/screentracker: captures and diffs terminal output into message chunks
- lib/msgfmt: strips user echo and TUI artifacts from agent output
- lib/logctx: structured logging context
- chat/: Next.js frontend—React components for chat UI
- openapi.json: API schema
- No persistent database; conversation stored in memory

##### README Insights
- Exposes four HTTP endpoints and an SSE stream for event updates
- Translates API calls into terminal keystrokes; parses output back into messages
- CLI “attach” uses the same HTTP API to stream messages and send keystrokes

a. Frontend
- Chat UI (Next.js web app)
- Components: MessageList, MessageInput, ChatInterface
- Connects via HTTP/SSE to the backend
b. Backend HTTP API
- HTTP server (cmd/server → lib/httpapi)
- Endpoints: POST /message, GET /messages, GET /status, GET /events (SSE)
- OpenAPI schema & docs UI
c. Terminal Engine
- lib/termexec: spawns the agent binary in a pseudo-terminal
- Sends keystrokes for user messages
d. Screen Tracker & Parser
- lib/screentracker: takes snapshots of terminal output, diffs to detect new agent messages
- In-memory storage of conversation state
e. Message Formatter
f. CLI “attach”
- cmd/attach: SSE subscription to /events and HTTP calls to send keystrokes
g. External Agent Processes
- Claude Code, Goose, Aider, Codex binaries on the host system

##### Interactions and Data Flow
1. User enters text in Chat UI → HTTP POST /message
2. HTTPAPI handler invokes lib/termexec to send keystrokes to agent subprocess
3. lib/screentracker snapshots terminal output, diffs against last state → new raw message
4. lib/msgfmt cleans up raw message → structured AgentMessage
5. Backend stores message in memory and:
- Returns 200 to POST /message
- Broadcasts update via SSE on /events
6. Chat UI listens on /events → renders new AgentMessage in MessageList
7. CLI “attach” works similarly: it subscribes to /events and sends keystrokes via /message

##### Architectural Patterns and Principles
- Layered architecture:
- Presentation (chat/ React)
- API layer (lib/httpapi)
- Domain logic (termexec, screentracker, msgfmt)
- In-memory state management (no external DB)
- Event-driven updates via SSE
- Single-process monolith with pluggable agent backends

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend
    subgraph "Frontend (React)" 
        UI["ChatInterface"]:::frontend
        Input["MessageInput"]:::frontend
        List["MessageList"]:::frontend
        Layout["App Layout"]:::frontend
        Page["App Page"]:::frontend
    end

    %% HTTP API Backend
    subgraph "HTTP API (Backend)"
        ServerCmd["CLI Server Cmd"]:::backend
        serverGo["httpapi/server.go"]:::backend
        claudeGo["httpapi/claude.go"]:::backend
        eventsGo["httpapi/events.go"]:::backend
        modelsGo["httpapi/models.go"]:::backend
        setupGo["httpapi/setup.go"]:::backend
        OpenAPI["openapi.json & Docs UI"]:::backend
    end

    %% Domain Services
    subgraph "Domain Services"
        TE["Terminal Engine (termexec.go)"]:::domain
        ST_conv["Screen Tracker (conversation.go)"]:::domain
        ST_ring["Screen Tracker (ringbuffer.go)"]:::domain
        MF_fmt["Message Formatter (msgfmt.go)"]:::domain
        MF_box["Message Formatter (message_box.go)"]:::domain
        LogCtx["Logging Context (logctx.go)"]:::domain
        Store["In-Memory Store (util.go)"]:::storage
    end

    %% CLI Attach
    CLI["CLI Attach"]:::frontend

    %% External Agents
    subgraph "External Agent Binaries"
        AgentProcs["Claude, Goose, Aider, Codex"]:::external
    end

    %% Data Flows
    UI -->|"POST /message"| serverGo
    Input -->|types| UI
    List -->|renders| UI
    serverGo -->|invokes| TE
    TE -->|PTY write/read| AgentProcs
    AgentProcs -->|raw output| ST_conv
    AgentProcs -->|raw output| ST_ring
    ST_conv -->|diff new| MF_fmt
    ST_ring -->|stores chunks| Store
    MF_fmt -->|clean message| Store
    Store -->|200 OK| serverGo
    serverGo -->|"SSE /events"| List
    CLI -->|"SSE /events & POST /message"| serverGo

    %% Click Events
    click UI "https://github.com/coder/agentapi/blob/main/chat/src/components/ChatInterface.tsx"
    click Input "https://github.com/coder/agentapi/blob/main/chat/src/components/MessageInput.tsx"
    click List "https://github.com/coder/agentapi/blob/main/chat/src/components/MessageList.tsx"
    click Layout "https://github.com/coder/agentapi/blob/main/chat/src/app/layout.tsx"
    click Page "https://github.com/coder/agentapi/blob/main/chat/src/app/page.tsx"
    click ServerCmd "https://github.com/coder/agentapi/blob/main/cmd/server/server.go"
    click serverGo "https://github.com/coder/agentapi/blob/main/lib/httpapi/server.go"
    click claudeGo "https://github.com/coder/agentapi/blob/main/lib/httpapi/claude.go"
    click eventsGo "https://github.com/coder/agentapi/blob/main/lib/httpapi/events.go"
    click modelsGo "https://github.com/coder/agentapi/blob/main/lib/httpapi/models.go"
    click setupGo "https://github.com/coder/agentapi/blob/main/lib/httpapi/setup.go"
    click CLI "https://github.com/coder/agentapi/blob/main/cmd/attach/attach.go"
    click TE "https://github.com/coder/agentapi/blob/main/lib/termexec/termexec.go"
    click ST_conv "https://github.com/coder/agentapi/blob/main/lib/screentracker/conversation.go"
    click ST_ring "https://github.com/coder/agentapi/blob/main/lib/screentracker/ringbuffer.go"
    click MF_fmt "https://github.com/coder/agentapi/blob/main/lib/msgfmt/msgfmt.go"
    click MF_box "https://github.com/coder/agentapi/blob/main/lib/msgfmt/message_box.go"
    click LogCtx "https://github.com/coder/agentapi/blob/main/lib/logctx/logctx.go"
    click Store "https://github.com/coder/agentapi/blob/main/lib/util/util.go"
    click OpenAPI "https://github.com/coder/agentapi/blob/main/openapi.json"

    %% Styles
    classDef frontend fill:#e0f7e9,stroke:#2e7d32,stroke-width:2px
    classDef backend fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
    classDef domain fill:#fff3e0,stroke:#ef6c00,stroke-width:2px
    classDef external fill:#f5f5f5,stroke:#9e9e9e,stroke-width:2px
    classDef storage fill:#fff9c4,stroke:#f9a825,stroke-width:2px
```
