# AgentForms β€” Form Infrastructure for AI Agents

## Positioning

AgentForms is form infrastructure for AI agents. Agents are the primary builders β€” they create, configure, and manage forms, documents, and workflows programmatically. Humans can interact when needed: inspecting forms, monitoring submissions, overriding config, or building manually for quick one-offs.

**What agents do:**
- "Create an intake form for a client onboarding"
- "Generate a quote document for this prospect"
- "Build a survey for our users"
- "Set up automated follow-up emails after form submission"

**What humans do when needed:**
- Inspect agent-built forms and submissions
- Override field config or action settings
- Build forms manually via the web UI for quick one-offs
- Monitor analytics and campaign performance

The entire form lifecycle is API-first. Every form is a JSON schema. Every action is a webhook hook. Every workflow is an LLM-generated pipeline. The human UI shares the same backend β€” no separate data paths.

## Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AI Agent (Claude, Codex, Hermes, Custom)               β”‚
β”‚                                                         β”‚
β”‚  "Create a client intake form with fields:              β”‚
β”‚   name, email, company, budget, timeline"               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚  /api/agent/build
                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AgentForms Relay (Flask + Gunicorn)                    β”‚
β”‚                                                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Workflow     β”‚  β”‚ Action      β”‚  β”‚ Form Logic     β”‚  β”‚
β”‚  β”‚ Builder (P2) β”‚  β”‚ Pipeline    β”‚  β”‚ Engine (P3)    β”‚  β”‚
β”‚  β”‚             β”‚  β”‚ (P1)        β”‚  β”‚                β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚         β”‚                β”‚                   β”‚          β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ LLM Client  β”‚  β”‚ Webhook     β”‚  β”‚ Validation     β”‚  β”‚
β”‚  β”‚ (Qwen 4B)   β”‚  β”‚ Dispatch    β”‚  β”‚ Conditions     β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚ Calculationsβ”‚  β”‚ Multi-step     β”‚  β”‚
β”‚                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  AgentForms Worker (Celery + Redis)                     β”‚
β”‚                                                         β”‚
β”‚  Async action execution: email delivery,                β”‚
β”‚  webhook dispatch, document generation                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Agent API Surface

### Phase 1 β€” Action Pipeline

Post-submission automation. When a form is submitted, configured actions execute:

| Action Type | Description |
|---|---|
| `webhook` | POST to any URL with submission data |
| `email` | Send formatted email via configured provider |
| `log` | Store in internal audit log |
| `redirect` | Redirect user to specified URL |
| `document` | Generate document (PDF, DOCX) from template |

Actions are ordered by `execution_order`, triggered by `trigger_event` (default: `submission`).

### Phase 2 β€” Workflow Builder

Agent tells the LLM what it needs. LLM returns a complete form schema:

```
POST /api/agent/build
{
  "prompt": "Create a contractor NDA with fields: party names, effective date, scope of work, confidentiality period, governing state",
  "form_type": "contract",
  "theme": { "primary": "#2563eb" }
}
```

Returns: complete `field_config` JSON + action suggestions.

### Phase 3 β€” Form Logic Engine

Server-side evaluation of form logic:

- **Validation** β€” email, phone, pattern, min/max, required
- **Conditional fields** β€” show/hide based on other field values (11 operators)
- **Calculated fields** β€” `{{price}} * {{quantity}}` formula engine
- **Multi-step forms** β€” step grouping with navigation

All logic is evaluable by headless agents β€” no client-side dependency.

### Phase 4 β€” Event Stream + SDK

- **SSE Event Stream** β€” real-time submission events for agent consumption
- **JS SDK** β€” client-side embed for human-facing forms

## API Reference

### Agent Build Endpoint

```
POST /api/agent/build
Authorization: Bearer <token>
```

**Request:**
```json
{
  "prompt": "string (required)",
  "form_type": "string (optional, default: 'form')",
  "theme": {
    "primary": "string (hex color)"
  }
}
```

**Response:**
```json
{
  "success": true,
  "site_id": 1,
  "fields": [...],
  "actions_suggested": [...],
  "embed_url": "https://agentforms.io/embed/abc123",
  "token": "abc123"
}
```

### Site CRUD

```
POST /api/sites              β€” Create site
GET  /api/sites              β€” List sites
GET  /api/sites/:id          β€” Get site
PUT  /api/sites/:id          β€” Update site
DELETE /api/sites/:id        β€” Delete site

POST /api/sites/:id/actions  β€” Create action
GET  /api/sites/:id/actions  β€” List actions
PUT  /api/actions/:id        β€” Update action
DELETE /api/actions/:id      β€” Delete action
```

### Form Submission

```
POST /api/submit?token=<token>
Content-Type: application/x-www-form-urlencoded
```

Returns `200 OK` on success, `422` with validation errors.

## Data Model

```
users
  └── sites
        β”œβ”€β”€ field_config (JSON)
        β”œβ”€β”€ actions
        β”‚     β”œβ”€β”€ type
        β”‚     β”œβ”€β”€ config (JSON)
        β”‚     β”œβ”€β”€ trigger_event
        β”‚     └── execution_order
        └── submissions
              └── data (JSON)
```

## Authentication

- **API Key** β€” `X-API-Key` header or `Bearer` token
- **Session** β€” SameSite+Secure cookies for human UI
- **CSRF** β€” Exempted for JSON API endpoints; required for HTML forms

## Deployment

- **Relay** β€” Flask + Gunicorn on `0.0.0.0:5060`
- **Worker** β€” Celery + Redis for async actions
- **Database** β€” SQLite (`relay.db`), Postgres migration planned
- **LLM** β€” Qwen3.5-4B via llama.cpp on port 8086 (configurable via `BUILDER_LLM_HOST`)
- **Docker** β€” `docker compose up -d`

## Phase Roadmap

| Phase | Status | Description |
|---|---|---|
| P1: Action Pipeline | βœ… Done | Webhook/email/log/redirect/document actions |
| P2: Workflow Builder | βœ… Done | LLM-generated forms from prompts |
| P3: Form Logic Engine | βœ… Done | Validation, conditions, calculations, multi-step |
| P4: SSE + JS SDK | βœ… Done | Real-time streaming build, EventSource SDK |
| P5: Document Generation | πŸ”œ Planned | PDF/DOCX from templates |
| P6: Postgres Migration | πŸ”œ Planned | Production-scale database |

## Testing

```bash
cd /home/vincent/projects/agentforms
python -m pytest tests/ -v
```