# Document Agents

Document Agents are configurable AI generators for business documents — contracts, proposals, quotes, invoices, offer letters, and more — combined with an end-to-end e-signature workflow.

A **Document Agent** defines the *type* of document (the prompt, the tools, the model, the locked sections, the signer roles). A **Document** is one generated instance. **Signing sessions** track each recipient's path through the signing flow.

## Key Concepts

- **Document Agent** — A reusable configuration: prompt, tools, model, hardcoded sections, signer roles, output formats, page setup
- **Document** — An instance generated from an agent for a specific customer / counterparty / project
- **Hardcoded section** — Verbatim "carbon-copy" text the renderer always inserts (e.g., governing law, disclaimers). The AI is told they exist and is instructed not to duplicate them
- **Global section** — Reusable headers, footers, clauses, or disclaimers shared across many document types
- **Signing field** — A signature, initial, date, text, or checkbox placeholder the AI places inline; the renderer resolves it to pixel coordinates on the PDF
- **Signing session** — Per-recipient token-bearing record; captures consent, identity attribution, signing action, and a full audit trail

## Where to Find Them

- **Document agents:** Navigate to **Agents > Document Agents** to manage types and templates
- **Generated documents:** Navigate to **Documents** to see all generated instances and their signing status
- **Global sections:** Navigate to **Documents > Global Sections** to manage reusable headers/footers/clauses
- **Template library:** Navigate to **Document Agents > Templates** for the one-click preset gallery

## Creating a Document Agent

The fastest path is to install a preset and edit it. To start from scratch:

1. **Agents > Document Agents > New Document Agent**
2. **Name & description** — what this type of document is for
3. **Model** — `gemini-3.8-flash` is the default and recommended for most documents
4. **Instructions (system prompt)** — tell the AI what sections to produce, in what order, what tone, and how to format the JSON output envelope (the form documents this)
5. **Tools** — same catalog as task agents (vector search, web search, save-update-user, send-email, send-sms, plus any custom tools your tenant has registered)
6. **Output formats** — pick any subset of PDF, DOCX, HTML
7. **Hardcoded sections** — paste in any verbatim legal boilerplate (governing law, severability, force majeure). Choose where in the layout each section appears
8. **Global sections** — pick existing headers/footers from your tenant's global section library
9. **Default signers** — define roles (e.g., "Disclosing Party", "Recipient") and order; real names/emails are filled in per document
10. **Page setup** — size, margins, font, font size, brand color
11. **Attach to CRM records** — which entity types this document type can be generated from (contacts, leads, companies, deals). Each CRM detail page only offers templates whose `attachableTo` list includes that entity type.

Save the agent. It now appears in your document agents list and can be used to generate new documents.

## Generating from a CRM Record

Open any contact, lead, company, or deal. Scroll to the **Documents** card and click **New document**. The picker lists only the document types whose attachable-to list includes that entity. The generated document is tagged with `attachedTo` metadata and shows up in the same card on subsequent visits, plus on the global Documents list filtered by attachment.

## Generating a Document

1. **Documents > New Document** (or click "Generate" from a document agent)
2. Fill in the input data the agent expects (party names, key dates, prices, etc.) — these are passed to the prompt as `{placeholders}`
3. Assign real names and emails to the signer roles
4. Click **Generate with AI**

A streaming progress panel shows:
- `started` — agent loaded, prompt assembled
- `iteration` — each round of model thinking
- `tool_call` / `tool_result` — when the AI invokes a tool
- `parsed` — model returned a valid JSON envelope
- `section.delta` — each section as it lands
- `done` — generation finished, document is in `draft` status

You can edit any AI-authored section in place. Hardcoded and global sections are locked (shown with a lock icon) so they can't be accidentally edited.

## Placing Signing Fields

The AI auto-places signing fields based on the inline markers it emits during generation. You can refine or completely override that placement using the **Place fields** tab in the document editor:

- Each PDF page is rendered in-browser via PDF.js
- Pick the active signer from the toolbar (each signer gets its own color)
- Click an "Add" button (signature / initial / date / text / checkbox), then click anywhere on a page to drop the field
- Click and drag any existing field to move it; drag the bottom-right corner to resize
- Hover a field to reveal its delete button
- Click **Save** to persist; the fields are now stamped at exactly those coordinates when the document is signed

Field coordinates are stored in PDF point space (top-left origin), so they survive page-size and font changes consistently.

Field placement is locked once a document moves to `out_for_signature` — you can't reposition fields after invites are sent.

## Approving and Sending for Signature

1. Review the draft. Click **Approve** when you're satisfied — the status moves to `approved`
2. Verify all signers have names and emails
3. Click **Send for signature**
   - **Sequential mode** — only the first signer (by order) gets the email; the next signer gets theirs as soon as the previous one finishes
   - **Parallel mode** — every signer gets the email at the same time
4. As each signer completes, the document status flows through `out_for_signature` → `partially_signed` → `signed`
5. Once all signers sign, the system stamps the final PDF with each signature image (or typed name), appends a Certificate of Completion, computes a SHA256 integrity hash, and emails a signed copy to every party

If anyone declines, the document moves to `declined` and no further invites are sent. You can `void` a document at any time before it's fully signed.

## Audit Trail

Every meaningful event is recorded on the document with a timestamp, actor, IP address, and user agent:

- `created` — document was created
- `generated` — AI generation completed
- `section_edited` — a section was hand-edited
- `approved` — author approved the document
- `sent_for_signature` — sent to signers
- `invite_sent` — individual signer received an invite
- `viewed_by_signer` — signer opened their link
- `signer_signed` — signer completed their signature
- `signer_declined` — signer rejected the document
- `finalized` — final signed PDF stamped, hash recorded
- `voided` — document was voided by the author

This trail is embedded in the Certificate of Completion appended to the final PDF.

## Common Use Cases

- **Sales proposals** — generate a tailored proposal per opportunity, send the same day
- **NDA workflow** — install the mutual NDA preset, fire it whenever a new partner conversation starts
- **MSA + SOW pairing** — sign the MSA once with a customer, then issue SOWs against it for each engagement
- **Offer letters** — generate from a recruiting workflow, route to hiring manager then candidate (sequential)
- **Invoices** — produce a clean PDF invoice from CRM line items, no signing required

<!-- SCREENSHOT: document-agents-list.png — Document agents list -->
<!-- SCREENSHOT: document-editor.png — Document editor with streaming generation -->
