Metadata-Version: 2.5
Name: langstage-codraft
Version: 0.12.2
Summary: The document stage for your LangGraph agent: co-author paged documents block by block, with approval-gated agent edits, per-block version history, and PDF/HTML/PNG export.
Project-URL: Homepage, https://github.com/dkedar7/langstage-codraft
Project-URL: Documentation, https://dkedar7.github.io/langstage-docs/stages/codraft/
Project-URL: Issues, https://github.com/dkedar7/langstage-codraft/issues
Author-email: Kedar Dabhadkar <kdabhadk@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: ag-ui,agents,ai-writing,document-editor,langgraph,langstage
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Text Processing
Requires-Python: >=3.11
Requires-Dist: click>=8.0
Requires-Dist: ddgs>=9.0
Requires-Dist: fastapi>=0.115
Requires-Dist: httpx>=0.27
Requires-Dist: langchain-openai>=1.0
Requires-Dist: langchain>=1.0
Requires-Dist: langgraph-checkpoint-sqlite>=2.0
Requires-Dist: langgraph>=1.0
Requires-Dist: langstage-core[agui]>=1.0.64
Requires-Dist: mcp<3,>=2.0
Requires-Dist: pillow>=10
Requires-Dist: playwright>=1.45
Requires-Dist: pydantic>=2.0
Requires-Dist: pypdf>=4.0
Requires-Dist: pypdfium2>=4.30
Requires-Dist: python-docx>=1.1
Requires-Dist: python-multipart>=0.0.12
Requires-Dist: python-pptx>=1.0
Requires-Dist: uvicorn[standard]>=0.30
Provides-Extra: anthropic
Requires-Dist: langchain-anthropic>=1.0; extra == 'anthropic'
Provides-Extra: dev
Requires-Dist: imageio-ffmpeg>=0.5; extra == 'dev'
Requires-Dist: imageio>=2.30; extra == 'dev'
Requires-Dist: numpy>=1.26; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: search
Requires-Dist: ddgs>=9.0; extra == 'search'
Provides-Extra: video
Requires-Dist: imageio-ffmpeg>=0.5; extra == 'video'
Requires-Dist: imageio>=2.30; extra == 'video'
Requires-Dist: numpy>=1.26; extra == 'video'
Description-Content-Type: text/markdown

# langstage-codraft

**The document stage for your LangGraph agent.** Draft a document block by block on real pages.
Tag your agent in comments, approve every change it suggests, and export exactly what you see.

[![Tests](https://github.com/dkedar7/langstage-codraft/actions/workflows/test.yml/badge.svg)](https://github.com/dkedar7/langstage-codraft/actions/workflows/test.yml)
[![PyPI](https://img.shields.io/pypi/v/langstage-codraft)](https://pypi.org/project/langstage-codraft/)
[![Python](https://img.shields.io/pypi/pyversions/langstage-codraft)](https://pypi.org/project/langstage-codraft/)

<p align="center">
  <a href="https://dkedar.com/media/codraft/codraft-launch.mp4"><img src="https://dkedar.com/media/codraft/codraft-launch-play.jpg" alt="Play the 80-second Codraft launch video: comment for the agent, review its suggestions, restyle the document, step through versions, export" width="800" /></a>
  <br><sub>▶ Watch the 80-second launch video (a real run with Claude Sonnet 5, waits sped up)</sub>
</p>

![Codraft editor: an agent suggestion shown as a diff on the block it changes](https://raw.githubusercontent.com/dkedar7/langstage-codraft/main/docs/assets/editor.png)

<details><summary>Dark mode</summary>

![Codraft in dark mode](https://raw.githubusercontent.com/dkedar7/langstage-codraft/main/docs/assets/editor-dark.png)

</details>

You own the thinking. Your agent owns the styling, and helps with drafting and research when
you ask it to.

- **Blocks on pages.** Text, headings, images, tables, Vega-Lite charts, callouts, columns of
  cells side by side, number tiles and an automatic references list. Pages come in Letter, A4, Legal, A5 or 16:9 slide, with 1–3
  columns. You can also pin blocks so other blocks flow around them, repeat headers and logos
  on every page, and add cover and back pages. Pageless mode is available too.
- **Start from the draft you have.** Import a Markdown or Word file and it becomes blocks:
  headings, paragraphs, lists, tables, pictures and footnotes. A template supplies the look.
- **The agent edits by suggestion.** Every change the agent makes to your text arrives as a
  diff you accept or reject. A per-document switch turns on auto-apply.
- **Comments tag the agent.** Queue comments for later or ask now. "Address all comments"
  handles them strictly top to bottom.
- **Nothing is overwritten.** Each block keeps a version tree. Step back, step forward, and
  edit an old version to start a new branch. A deleted block can be restored, with its
  history: **Undo** on the toast, or **Document → Recently deleted** later (this also covers a
  deletion the agent suggested and you accepted). The agent can do the same when you ask for
  something back (`list_deleted_blocks`, `restore_block`).
- **Checkpoints.** Name the text as it is now ("Sent to legal"); later, see what changed since,
  block by block, and go back: all of it or one block. One is taken before each agent turn and
  each **Accept all**, so accepting thirty suggestions at once has an **Undo**.
- **Redlines for reviewers in Word.** Export what changed since a checkpoint, or the pending
  suggestions, as a Word file with real tracked changes, so legal can accept or reject them in
  Word without opening Codraft. See [Redline Word export](#redline-word-export).
- **The look has a history too.** The agent restyles without asking, so every change to the
  look is a step you can undo: **Design → History**, or **Undo** on the notice after a
  restyling turn. One agent turn is one step, and undoing it never touches your text.
- **Sources stay with each block.** The agent cites as it writes: `[@key]` in the text prints
  as the source's number, and a `references` block lists them with authors, year and
  publisher (see [References and citations](#references-and-citations)).
- **The agent sees the page.** `view_pages` gives a vision model screenshots of the rendered
  pages, so it can fix layout problems you'd spot by eye.
- **Export matches the screen.** PDF, PNG/JPG per page, or a self-contained HTML zip, all
  rendered by the same component as the editor (CI checks them pixel for pixel). Also Word
  (`.docx`, editable and reflowing), PowerPoint (`.pptx`, one slide per page, with editable text
  boxes where the paginator put them), and an animated GIF or MP4 page slideshow. Batch jobs
  export straight from the data directory, [with no server running](#export-without-a-running-server).
- **Sketch it, get an image.** Draw on an image block; the agent looks at the sketch
  (`view_sketch`) and turns it into an image with its image tool, or says it can't.
- **Video and GIF blocks.** They play in the editor and in the HTML export; PDF and images use
  the poster frame.
- **Write together, live.** Everyone in a document sees each other's cursor block, edits from
  other tabs, people and agents arrive instantly, and a block someone is editing is soft-locked.
  If two people still edit the same block, both versions are kept as branches.
- **Any agent, over MCP.** Claude Code, Cursor or any MCP client can co-author through the same
  approval-gated tools, at `http://localhost:8060/mcp` or `langstage-codraft mcp` (stdio).
- **Light and dark.** The interface uses langstage-web's design system, so the two stages look
  like one product.

## Quick start

```bash
pip install langstage-codraft
langstage-codraft install-browser          # Chromium for export and the agent's eyes (once)
langstage-codraft run --demo               # keyless scripted co-author, opens http://localhost:8060
```

With a real model:

```bash
export OPENROUTER_API_KEY=...              # or OPENAI_API_KEY / ANTHROPIC_API_KEY
langstage-codraft run                      # default agent: claude-haiku-5.5 via OpenRouter
langstage-codraft run -m openai:gpt-5-mini # any LangChain model spec
pip install "langstage-codraft[anthropic]"  # for ANTHROPIC_API_KEY / anthropic: models
pip install "langstage-codraft[video]"      # MP4 export (bundles ffmpeg)
```

## Bring in a document you already have

Most writing starts from a draft. Import a Markdown (`.md`) or Word (`.docx`) file and it
becomes a Codraft document made of ordinary blocks, which you then edit, restyle and hand to
the agent like any other:

```bash
langstage-codraft import report.md                       # prints the new document id
langstage-codraft import proposal.docx --template paper  # the template's look, your content
langstage-codraft import notes.md --title "Field notes" --theme modern --split section
```

In the editor, **New document → Import Markdown or Word file** does the same (for a Markdown
file with pictures, select its image files along with it). Over HTTP it is
`POST /api/docs/import` (multipart: `file`, and optionally `title`, `template`, `theme`,
`settings` as JSON, `split`, and `assets` for the image files).

| In the file | In Codraft |
|---|---|
| Headings (`#`, `Title`, `Heading 1–6`) | Heading blocks. A leading H1, a front-matter `title:` or Word's Title becomes the document title |
| Paragraphs, lists, block quotes, code | Text blocks, one per paragraph (`--split section` keeps the prose between two headings together) |
| Tables | Table blocks (cells as plain text) |
| A picture on its own | An image block. Embedded pictures and local files are stored with the document; a remote picture stays a link |
| `---` / `***`, `
ewpage`, Word page breaks | Divider and page-break blocks |
| GitHub alerts (`> [!NOTE]`) | Callouts |
| Footnotes | References on the block that cites them, listed in a References block |
| Reference-style links | Ordinary links |

The template you pick supplies the look and not its placeholder text: with `report` the title
goes on the cover page, with `paper` it runs across both columns.

Imported blocks are your content, not agent suggestions, so nothing waits for approval. The
import brings in structure and content, not Word's exact look (use *match its look* on an upload
for that). Whatever it can't represent (a text box, an equation, a picture it couldn't find)
is imported as plain text or left out, and listed for you: the editor shows the list before
it opens the document, the CLI prints it to stderr (`--json` includes it), and the API returns
it as `import.notes`.

The importer never fetches anything, reads a Markdown file's pictures only from that file's
own folder (CLI) or from the files you send with it (editor, API), sanitizes SVG, and caps
sizes: 25 MB per file, 5,000 blocks, and 300 MB for a Word file once unzipped.

## Bring your own agent

Codraft is a LangStage stage: you bring the LangGraph agent and Codraft adds what it needs to
work on documents. There are three ways to plug one in.

```python
# my_agent.py  (needs OPENAI_API_KEY; any LangChain chat model works)
from langchain.agents import create_agent
from langchain_core.tools import tool
from langstage_codraft import CodraftMiddleware


@tool
def glossary(term: str) -> str:
    """Look up an internal term."""
    return {"pilot": "A time-boxed trial with a go/no-go review."}.get(term.lower(), "No entry.")


# 1. A graph that includes the middleware (it registers the tools and the co-author prompt,
#    and attaches page screenshots for vision models).
graph = create_agent("openai:gpt-5-mini", tools=[glossary], middleware=[CodraftMiddleware()])


# 2. Or a factory: Codraft calls it with its tools and middleware.
def make(tools, middleware):
    return create_agent("openai:gpt-5-mini", tools=[*tools, glossary], middleware=middleware)
```

```bash
langstage-codraft run --agent my_agent.py:graph    # or my_agent.py:make
langstage-codraft check --agent my_agent.py:graph  # preflight; --live runs one real turn
```

A graph built without the Codraft tools still chats, but it can't touch the document. `check`
tells you when that's the case.

Name your agent and the editor uses that name wherever it would say "agent": "Ask Juniper",
"Suggestions from Juniper", replies and history credited to Juniper. Codraft reads `graph.name`
(`create_agent(model, name="Juniper")`); `[ui] agent_name` overrides it.

The agent's toolset (`CODRAFT_TOOLS`) has 31 tools. Each one reads and writes through the same
API the editor uses, so the agent has no private path, and nothing the agent can do is out of
the author's reach:

| Read | Write (suggestions) | Style (applies directly) | Other |
|---|---|---|---|
| `get_document`, `read_blocks`, `search_document`, `read_upload`, `list_comments`, `get_block_history`, `list_checkpoints`, `changes_since` | `propose_edit`, `propose_insert`, `propose_delete`, `propose_move`, `insert_blocks`, `draw_figure` | `set_block_layout`, `set_document_style`, `set_typography`, `set_cover`, `set_header_footer`, `match_upload_style`, `reserve_area` | `reply_to_comment`, `compile_references`, `view_pages`, `view_sketch`, `view_upload`, `calculate`, `list_deleted_blocks`, `restore_block`, `list_design_history`, `restore_design` |

**The agent sees what it has proposed.** While its suggestions wait for you, the agent works from
the document as it will be, not only the one you have accepted:

- `get_document` lists pending suggestions where they will land, marked PENDING. A pending insert
  goes by its suggestion id (`sug_…`), which `read_blocks`, `propose_edit` (to revise the draft),
  `propose_delete` (to withdraw it), `set_block_layout` and `after_block_id` all accept.
- `read_blocks` shows a block's accepted text next to the agent's pending edit of it.
- `view_pages` renders a preview with the pending suggestions applied, through the same print
  route as export, and says which blocks on each page are still pending. `pending=False` shows
  only what you have accepted. It returns at most four pages per call and says which it left out.
- A suggestion that changes nothing, or that repeats one still waiting, is not recorded; the
  agent is told so.
- A batch of new blocks stays in the order it was written, however you accept it, and **Accept
  all** applies chained moves in dependency order. Anything it can't apply is listed with the
  reason, never dropped.
- Each turn starts with a short note of what you did since the agent's last one: which
  suggestions you accepted or rejected, and the blocks you edited, added or deleted. The note
  survives a server restart.
- `restore_block` brings a deleted block back where it was, with its history. It applies
  immediately in either approval mode, because it only puts your own block back as it was.

`calculate` does the arithmetic (totals, percentages, growth, table cells) so figures in the
document are computed, not guessed.

**The agent sees the figure it drew.** `draw_figure` comes back with the figure as it sits on the
page (a screenshot of the block from the same print view as export, at twice its size) and a
label check measured on the rendered SVG at its printed size: labels cut off by the canvas, type
that prints under 6.5 px, labels on top of each other, a label wider than the box it sits in, and
lines or arrowheads that run through a label, and one arrow drawn along another. The agent fixes what the check lists in one redraw
and looks at the preview for what a check can't see. Arrowhead markers sized in stroke widths
(the usual cause of giant arrowheads on thick connectors) are brought back in proportion when the
figure is stored. One figure can be drawn five times in a turn; after that the agent is told to
simplify it or to say what is wrong.

**What the agent won't do to make a page fit.** It doesn't delete or shorten content, its own or
yours, to reach a page count. Deleting the reference list, or a block that cites a source, waits
for your approval in every approval mode, including `auto`. Word counts are in `get_document`,
`read_blocks` and every `propose_edit` result, so "tighten by a third" is checked against a
number. A long draft of several paragraphs becomes one block per paragraph, so you approve and
comment on each (`keep_together` keeps a draft whole).

A layout change can't hide content either. `set_block_layout` refuses a height, or a pin's box,
that would cut off a block's text, table or cells, and says how much room the content needs:
what is cut off doesn't print, so shortening a block is a content change, and that is a
suggestion you approve. The check is in the tool, so an MCP client and a bring-your-own agent
get it too. You can still give a block any height in the Block panel; the panel then says how
much is cut off and offers **Fit the content**.

**Research tools of the built-in agent.** `fetch_url` reads a page or a PDF. `web_search` is part
of the default install (the `ddgs` package). If it can't be imported, `langstage-codraft check`
says so and the agent is told it has no search and must not guess URLs. After six failed fetches
in a turn, only URLs that a search returned are opened. `generate_image` is offered when it can
work: `OPENROUTER_API_KEY` is set and either the model is an `openrouter:` one or
`LANGSTAGE_CODRAFT_IMAGE_MODEL` names the image model. A key that merely sits in the environment
beside another provider's model no longer gets the tool.

Tool results from earlier turns (pages the agent fetched, old outlines and block reads) are sent
to the model cut down in later turns; the thread keeps them whole. On the long documents of the
0.8.0 evaluation this was 40% of the text sent to the model. `CodraftMiddleware(trim_history=False)`
turns it off.

Each document has its own agent thread (the AG-UI thread id is the document id). Conversations
are stored in SQLite and survive restarts. The same agent is also served at `POST /agui` for any
AG-UI client.

## Magazine-quality reports

Codraft lays out more than memos. Give `examples/report_designer.py` (an OpenRouter agent with
image generation) a few pages of a report you like and a topic, and it builds a report with the
same look: custom page size, justified three-column type with hyphenation, drop caps, pull
quotes, page-anchored hero art that bleeds past the margins, framed figures (`FIGURE 1`, title,
source line), running footers with `{page}` numbers that alternate left and right, and a
hanging-indent reference list that flows across columns. Diagrams are drawn as SVG
(`draw_figure`) so labels stay crisp; photos and illustrations come from an image model.

![Two pages of a magazine-style feature on smart water networks, built by the report designer agent](https://raw.githubusercontent.com/dkedar7/langstage-codraft/main/docs/assets/report-example.png)

```bash
export OPENROUTER_API_KEY=...
langstage-codraft run --agent examples/report_designer.py:make --approval auto
# upload reference pages (PNG/JPG), then ask: "Make a report like the attached pages about <topic>"
```

Every layout feature is available to people too (Block and Document panels) and to any agent:
block styles (`lead`, `pullquote`, `kicker`, `byline`, `footer`, `bio`; headings `display`,
`section`, `rule`, `band`; `figure-box`; `hanging` references; `dark` and `band` callouts; `cards`
rows; `plain` number tiles), drop caps, custom page
sizes, page-anchored pins, and reserved areas (`reserve_area`) that keep part of a page empty.
Page numbers: put `{page}` (and `{pages}`) in a repeating footer; cover and closing pages are
skipped unless you turn on "Number cover and closing pages", and any page can be left unnumbered.

## Side by side: columns, number tiles and bands

A one-pager needs things next to each other: three value props, a row of big numbers, a header
band, a call-to-action box. These are blocks, so they flow with the document, export exactly as
they look, and need no pinning.

![A one-page sales sheet: an orange header band, three value props side by side, a row of three number tiles and a call-to-action band](https://raw.githubusercontent.com/dkedar7/langstage-codraft/main/docs/assets/one-pager.png)

- **Columns** (`row`): 2 to 4 cells side by side, each one Markdown. Cells are equal unless you
  give relative widths, and the gap is the document's column gap unless you set one. The
  `cards` style puts each cell in a tinted box.
- **Number tiles** (`stat`): a large value with a small label under it, up to six in a block.
  The `plain` style drops the tinted boxes.
- **Band**: a style for headings and callouts. A band heading is a solid header strip; a band
  callout is a call-to-action box with centred text.
- **Fill**: the colour behind a band, a row's cards or the tiles. It is one of the document's
  theme colours by name (`accent`, `text`, `muted`, `heading`, `callout`, `figure`, `rule`), so
  it follows the theme, or a hex colour. The text on it turns white or dark, whichever reads.

The page above was laid out by the built-in agent (GPT-5.4) in three turns, from the one-pager
template and a short brief.

In the editor: **Add block → Columns** or **Number tiles**, then edit the cells or tiles in the
Block panel; **Style → band** and **Fill** are in the same panel. The marketing one-pager
template starts with all of them.

```json
{"type": "heading", "content": {"text": "Acme Relay", "level": 1},
 "layout": {"span": "full", "variant": "band"}}
{"type": "row", "layout": {"span": "full", "variant": "cards"},
 "content": {"cells": [{"markdown": "### Reliable\n\nRetries for 72 hours."},
                       {"markdown": "### Observable\n\nA searchable delivery log."},
                       {"markdown": "### Fair\n\nPer-customer rate limits."}],
             "widths": [1, 1, 1], "gap": 6}}
{"type": "stat", "layout": {"span": "full"},
 "content": {"items": [{"value": "99.99%", "label": "delivered within 60 seconds"},
                       {"value": "38 ms", "label": "median added latency"}]}}
{"type": "callout", "content": {"markdown": "**Start a 14-day pilot.**"},
 "layout": {"span": "full", "variant": "band", "fill": "#ff5a1f"}}
```

The agent makes them with the tools it already has (`insert_blocks`, `propose_insert`,
`propose_edit`, and `fill` on `set_block_layout`), over MCP too, and its instructions say to use
these and never pins, raw HTML or drawn SVG for columns, tiles, bands and boxes. In a real-model
evaluation of 0.5.17, a one-pager like the one above took 266 tool calls, 190 of them pinning
blocks to pixel positions, and still came out wrong.

- A row or a set of tiles is one block. It moves to the next column or page as a whole and
  never splits; one taller than a page is flagged as overflow.
- Cells that would be narrower than an inch stack instead (a four-cell row put in one column of
  a three-column page), and six tiles that don't fit one line take two.
- Word export writes a row and the tiles as borderless tables and shades a band. PowerPoint
  shows a row, the tiles and a band as pictures, where the paginator placed them.
- Import never produces these blocks: a Markdown or Word file becomes text, tables and figures.

## Design the whole document

The **Design** tab, and the same tools for the agent, cover the look of the whole document. All
of it applies to this document only and exports exactly as you see it (PDF, PNG, HTML; Word gets
the fonts, colours, header and footer too).

- **Cover page.** A report's title and subtitle sit on their own cover page. Turn it off and they
  open page 1 as a full-width title block, with the summary starting right below. Give the cover
  a background colour or image, place its blocks at the top, middle or bottom, and pin art or a
  logo to an exact spot (light text switches on over dark backgrounds). "+ Add to cover" above
  the page adds to it, and so does anything inserted right after a cover block, by you or the
  agent. Back pages work the same way.
- **Fonts and colours.** Override the theme for this document: body, heading, title and label
  fonts; size, line spacing, heading scale, weight and case; alignment and hyphenation; and the
  text, heading, accent, rule, tint and paper colours. 17 families are bundled (Inter, Source Serif 4, Raleway,
  Lato, Open Sans, Montserrat, Roboto, Merriweather, Lora, Playfair Display, EB Garamond, Libre
  Baskerville, Carlito, Arimo, Tinos and more), and any other Google Fonts family loads on demand.
  **Save as theme** keeps the look (cover included) for every other document.
- **Heading styles follow your choices.** A heading block can wear a style: `display` (a
  title), `label` (a small label), `section` and `rule` (compact section headings: by default
  small bold capitals in the label font). The heading case, weight, scale or font you set for
  the document replaces that default look of `section` and `rule`, so "sentence case, please"
  changes every section heading. `display` and `label` keep their own size and case. The Block
  panel shows what each style does, and `GET /api/config` lists them (`block_variants`).
- **Header and footer.** Left, centre and right text in the page margins, with `{page}`,
  `{pages}`, `{title}` and `{year}`, an optional rule, and the first page left out if you like.
  They skip the cover and back pages; unnumbered pages drop the `{page}` slot.
- **Match a document's look.** Upload a PDF, a Word file or a page image. Codraft reads its page
  size, margins and columns, its fonts (mapped to open look-alikes: Calibri → Carlito, Arial →
  Arimo, Times → Tinos, Helvetica Neue → Inter, ...), its text, heading, accent and tint colours,
  whether it has a cover and what colour, and its running header and footer, then applies them.
  The agent sees the uploaded pages (`view_upload` renders PDFs) and matches the rest by eye
  with `match_upload_style`, `set_typography`, `set_cover` and `set_header_footer`.

```python
# What the Design tab writes, as document settings (PATCH /api/docs/{id}):
{
  "cover": {"enabled": True, "background": "#002f58", "align": "bottom"},
  "style": {"font_body": "'Lato', sans-serif", "color_heading": "#002f58", "color_accent": "#009fe3"},
  "footer": {"left": "{page}", "center": "{title}", "right": "Example Forum", "rule": True},
}
```

### Checkpoints: what changed since, and going back

Every block keeps its own version tree. A **checkpoint** ties the blocks together at a moment, so
you can ask of the whole document "what changed since the version legal reviewed?" and take a
long turn back when you accepted it wholesale and it flattened the argument.

```text
Sent to legal                         You          6 days ago    14 changes since…
Before Accept all (12 suggestions)    You          3 min ago     12 changes since…
Before the agent's turn               The agent    1 h ago       19 changes since…
```

- **Named and automatic.** You name a checkpoint in **Document → Checkpoints**; it is kept until
  you delete it. Codraft takes one itself before each agent turn that proposes content, before
  each **Accept all** and before a restore, and keeps the newest 100. Naming an automatic one
  keeps it.
- **No text is copied.** A checkpoint records which version of each block was showing and where
  the block stood. It is taken at the moment itself, so it is exact even for a block you had
  stepped back to an older version (stepping writes no version, so no timestamp could tell).
- **Changes since** lists the blocks edited, added, deleted and moved, with the word diff a
  suggestion gets and word counts. **Revert this block** takes one back; **Restore all** takes
  the document back.
- **Going back overwrites nothing.** An edited block checks out the version it had, and the
  newer text stays in its history, one step forward. A deleted block returns with its history.
  A block added since goes to **Recently deleted**. The order is put back. The restore takes a
  checkpoint first, so the notice's **Undo** (or that checkpoint) undoes it. The exported pages
  after a restore are the checkpoint's pages, pixel for pixel.
- **Undo accept all.** The notice after **Accept all** offers it, and the Review tab keeps
  **See changes** and **Undo** afterwards. The suggestions stay decided; to re-apply one, step
  that block forward.
- **Content only.** Settings, block layouts and reserved areas have their own history (below),
  and a checkpoint restore leaves them alone.
- **Suggestions and other people.** Pending suggestions stay pending: one written against text
  that is no longer showing is marked outdated, and one on a block the restore removed is
  closed. A block someone else is editing right now is left alone and listed, so you can revert
  it afterwards.
- **The agent reads, you restore.** `list_checkpoints` and `changes_since` (over MCP too) let
  the agent answer "what changed since Friday?" or write the cover note from the record. It has
  no tool to go back to a checkpoint: that changes your content, so it is yours to do.

```python
cp = httpx.post(f"{base}/api/docs/{doc_id}/checkpoints", json={"name": "Sent to legal"}).json()
# ... a week of edits and accepted suggestions ...
ch = httpx.get(f"{base}/api/docs/{doc_id}/checkpoints/{cp['id']}/changes").json()
# {"counts": {"edited": 11, "added": 2, "deleted": 1, "moved": 0, "total": 14}, "words_before": 4210,
#  "words_after": 4480, "edited": [{"block_id": "blk_…", "from_version": "ver_…", "to_version": "ver_…",
#  "words_before": 120, "words_after": 96, "before": {"content": {…}}, "after": {"content": {…}}}], …}
httpx.post(f"{base}/api/docs/{doc_id}/checkpoints/{cp['id']}/restore", json={"blocks": ["blk_…"]})  # one block
got = httpx.post(f"{base}/api/docs/{doc_id}/checkpoints/{cp['id']}/restore", json={}).json()["result"]  # all of it
httpx.post(f"{base}/api/docs/{doc_id}/checkpoints/{got['undo']}/restore", json={})                       # undo that
```

`POST /api/docs/{id}/suggestions/accept-all` returns `checkpoint`, the one that undoes it. In
Python: `Store.create_checkpoint(doc_id, name)`, `Store.list_checkpoints(doc_id)`,
`Store.checkpoint_changes(doc_id, checkpoint_id)` and
`Store.restore_checkpoint(doc_id, checkpoint_id, blocks=None)`.

### Redline Word export

A reviewer who works in Word can't see **Changes since…** inside Codraft, and a plain Word
export is a clean copy. A **redline** is the Word export with the changes written as Word's own
tracked changes, so the reviewer gets Word's **Accept** and **Reject**:

- **Changes since a checkpoint.** Under **Document → Checkpoints**, the file icon next to
  "N changes since…" (or **Export redline** in the changes view) downloads `<title>-redline.docx`.
  The Export menu's **Word: changes since…** takes you there.
- **The pending suggestions.** **Export → Word with suggestions** downloads
  `<title>-suggestions.docx`: each pending suggestion is a tracked change by the agent, where
  accepting it would put it.

```bash
langstage-codraft export DOC_ID -f docx --since "Sent to legal" -o redline.docx   # a checkpoint's name or id
langstage-codraft export DOC_ID -f docx --pending -o suggestions.docx
```

How each change is written:

| Change | In Word |
|---|---|
| Edited text, heading or callout | word by word: the removed words deleted, the new ones inserted; bold, italic and the rest kept |
| A paragraph added to or removed from a block | the whole paragraph inserted or deleted |
| Added block | inserted where it is now |
| Deleted block | deleted where it was |
| Moved block | Word's move marks: **Moved** from where it was to where it is now, accepted or rejected as one. Moved and edited too: the move, with the edit's words inserted and deleted inside it. A table (or text containing one) has no move marks, so it is deleted where it was and inserted where it is |
| Edited table, row, stat tiles, chart or image | the old block deleted and the new one inserted |

Each change is attributed to whoever made it, from the block's version records: a collaborator's
name, the agent's name (`[ui] agent_name` or the graph's name), or "Author" when the editor gave
no name. Accept every change in Word and the text reads as the document does now (or with the
suggestions accepted); reject every one and it reads as it did at the checkpoint (or as it is
now). A chart or image as it was, or one not yet accepted, has no picture to show, so the
redline names it (`[chart]`) next to its caption. Citation numbers follow the document as it is
now; a citation whose source went with a deleted block prints `[?]`.

`GET /api/docs/{id}/export?format=docx&since=<checkpoint id or name>` and `&pending=true` serve
the same files. In Python: `export_document(data_dir, doc_id, "docx", since="Sent to legal")`,
`export_document(..., pending=True)` and `Standalone.export(doc_id, "docx", since=...)`.
Reviewers' tracked changes don't come back into Codraft: answer them in Codraft or with the agent.

### Design history: undo a restyle

Styling is the agent's to change without approval, which only works if it is cheap to undo. So
the look has a history, like the text does. Every change to the document settings, typography,
cover, header and footer, block layouts and reserved areas is a **step**, with who made it, when,
and what it changed:

```text
The agent   3 min ago    1 → 2 columns, theme classic → modern, body font Lato, 14 block layouts
You         1 h ago      heading case uppercase
```

- **One turn is one step.** An agent turn that makes 30 or 200 styling calls is a single step.
  So is one drag of a colour picker or a size field. An MCP client has no turns, so its calls
  are grouped by time (a pause of two minutes starts a new step).
- **Undo** reverts that step alone and leaves later changes in place. A setting or a block that
  was restyled again since keeps its newer look, and the result says how many were left.
- **Back to before this** puts the document back to how it looked before the step: that step
  and every later change to the look are undone.
- **A restore is a step itself**, so it can be undone the same way. Nothing is overwritten.
- **Content is never touched.** A restore changes settings, block layouts and reserved areas.
  It doesn't edit, create or delete content blocks; the layout of a block deleted since is
  skipped, and a block added since keeps its layout. The exported page after an undo is the
  page from before the restyle, pixel for pixel.
- After a turn in which the agent changed the look, the editor says what changed and offers
  **Undo**. **Design → History** lists every step, with **Undo the agent's last restyle** at
  the top.
- The agent has `list_design_history` and `restore_design` (over MCP too), and is told to use
  them only when you ask ("undo that", "the old cover was better"). Its next turn is told when
  you undid its styling, so it doesn't apply it again.

```python
steps = httpx.get(f"{base}/api/docs/{doc_id}/design-history").json()["steps"]     # newest first
# {"id": "dsn_4e1c…", "by": "agent", "kind": "agent", "summary": "1 → 2 columns, 14 block layouts",
#  "settings": True, "blocks": 14, "undoable": True, "restore_of": None, "updated_at": 1763200000.0, …}
httpx.post(f"{base}/api/docs/{doc_id}/design-history/{steps[0]['id']}/restore", json={"only": True})  # undo it
httpx.post(f"{base}/api/docs/{doc_id}/design-history/{steps[0]['id']}/restore", json={})   # the look before it
```

The history keeps the newest 200 steps of a document. In Python: `Store.design_history(doc_id)`,
`Store.restore_design(doc_id, step_id, only=False, by="author")`. The approval mode, the brief and
the page count you asked for are not part of the look, and a restore leaves them alone.

## Bring any agent: MCP

The same tools are served over MCP, so agents outside LangGraph can co-author too. Their edits
are suggestions the author approves, exactly like the built-in co-author's.

```bash
# with `langstage-codraft run` going:
claude mcp add --transport http codraft http://localhost:8060/mcp

# or stdio, for clients that launch a command (no server needed):
langstage-codraft mcp --data-dir .codraft
```

MCP tools: `list_documents`, `create_document`, every `CODRAFT_TOOLS` tool with a `doc_id`
argument (except `calculate`, which is the built-in agent's own arithmetic; `draw_figure` returns
its preview image and label check here too), plus `view_pages`
and `view_sketch`, which return images.

Over stdio, `view_pages` works on its own: it uses the server on the configured port if one is
running, and otherwise starts a private in-process renderer the first time the tool is called
(the first call takes a few seconds; it needs `langstage-codraft install-browser` once). The
renderer is stopped when the client disconnects, and on SIGTERM or Ctrl+C, so nothing is left
running and nothing is printed on the way out.

## For deployers

Configuration follows the LangStage chain: defaults < `langstage.toml` < `LANGSTAGE_*` env < CLI
flags.

```toml
# langstage.toml
[agent]
spec = "my_agent.py:graph"

[codraft]
model = "openrouter:anthropic/claude-haiku-5.5"   # used when no agent spec is set
max_output_tokens = 16000                        # cap on one model call's output (0 = provider default)
reasoning = "auto"                               # auto | off | low | medium | high
approval = "required"                            # or "auto" for new documents
default_theme = "acme"
default_page_size = "a4"                         # new documents (default letter)
allowed_themes = ["acme", "academic"]            # others fall back to default_theme
templates_dir = "templates"                      # *.json: settings + a block skeleton
data_dir = ".codraft"

[ui]
theme = "auto"                                   # app chrome: light | dark | auto

[codraft.themes.acme]
label = "Acme"
font_body = "Inter, system-ui, sans-serif"
font_heading = "Inter, system-ui, sans-serif"
color_accent = "#d9480f"
color_heading = "#0b1021"
cover_background = "#0b1021"                     # a theme can style the cover too
cover_text = "light"

[codraft.doc_defaults]                           # settings every new document starts with
cover = { enabled = false }                      # house style: no separate cover page
footer = { left = "{page}", right = "Acme Corp" }
```

`max_output_tokens` and `reasoning` apply to the built-in agent (`model`); an agent you bring with
`[agent] spec` sets its own. The cap keeps a gateway from reserving the model's whole output
window for every call, which fails outright on a key with little credit. `reasoning = "auto"`
turns reasoning on at low effort where Codraft knows the model's switch (OpenRouter models,
OpenAI GPT-5 and o-series, Anthropic). For OpenAI's GPT-5 and o-series models the built-in agent
uses the Responses API, the one place those models reason together with tools. If the provider
refuses the setting, the agent carries on without it (over plain Chat Completions for OpenAI)
and logs why; it never fails a turn over it.

`web_fonts = false` (in `[codraft]`) keeps documents to the bundled fonts, with nothing fetched
from Google Fonts. `[ui.features] style_editor = false` hides the font and colour editor (the
theme list, the cover and the header and footer stay).

`langstage-codraft run --show-config` prints the resolved values and where each one came from.

There are seven built-in templates (ids in brackets): blank, report, proposal, memo, two-column academic
paper (`paper`), marketing one-pager (`one-pager`), and a three-column newsletter with a repeating
masthead (`newsletter`). Each new document
records the author's brief (writing style, objective, platform, audience, language, output
format), and the agent reads it before writing.

### Brand the app, or embed it in yours

The app's chrome (header, panels, buttons, chat) can take on your product's look; the documents keep
their own themes. The `[ui]` keys are the same as langstage-web's, so one `langstage.toml` brands both.

```toml
[ui]
title = "Acme Write"
subtitle = "drafting studio"
agent_name = "Acme Assistant"        # "Ask Acme Assistant…"
welcome_message = "Start a brief, a memo or a report."
icon_url = "https://acme.example/logo.svg"
font_family = "Raleway"
font_url = "https://fonts.googleapis.com/css2?family=Raleway:wght@400;600;700&display=swap"
starter_prompts = ["Draft an executive summary", "Turn my notes into a memo"]
custom_css = "brand.css"             # inline CSS or a .css file (relative to this toml), applied last
embedded = false                     # true: no Codraft header brand or theme toggle

[ui.colors]                          # any langstage-web token: primary, primary_dark, surface,
primary = "#002f58"                  # surface_2, surface_3, border, text, text_secondary,
primary_dark = "#001f3d"             # text_muted, panel, card, accent_soft, success, warning,
surface = "#faf7f1"                  # error, plus canvas, header, header_text
accent_soft = "#fff1d6"

[ui.colors_dark]                     # the same keys for dark mode

[ui.features]                        # switch parts of the app off
mcp = false                          # no /mcp endpoint (stdio `langstage-codraft mcp` still works)
chat = true                          # false hides the chat tab; the agent buttons (Ask, Address comments) still run
doc_list = true
theme_toggle = true
```

To put Codraft inside another FastAPI app, mount it and pass the branding in code. This is a
complete `host.py` (`pip install langstage-codraft deepagents`, then
`CODRAFT_PROXY_SECRET=... OPENROUTER_API_KEY=... uvicorn host:app --port 8080`):

```python
# host.py
import os

from deepagents import create_deep_agent
from fastapi import FastAPI
from langchain_openai import ChatOpenAI

from langstage_codraft import CodraftMiddleware
from langstage_codraft.config import CodraftConfig
from langstage_codraft.server import create_app, mount

app = FastAPI()  # your app

cfg = CodraftConfig.resolve(overrides={
    "data_dir": "codraft-data",
    "renderer_url": "http://127.0.0.1:8080/write",   # how exports reach the mounted print route
    "proxy_secret": os.environ["CODRAFT_PROXY_SECRET"],
    "user_header": "X-User-Email",                   # the signed-in user, set by your proxy
    "default_page_size": "a4",
})
model = ChatOpenAI(model="anthropic/claude-haiku-5.5", base_url="https://openrouter.ai/api/v1",
                   api_key=os.environ["OPENROUTER_API_KEY"])
# The middleware registers Codraft's tools and prompt; add your own tools alongside.
graph = create_deep_agent(model=model, tools=[], middleware=[CodraftMiddleware()])
mount(app, "/write", create_app(
    cfg, graph,
    ui={"agent_name": "Acme Assistant", "embedded": True, "colors": {"primary": "#002f58"}},
    doc_themes={"acme": {"label": "Acme", "font_heading": "Raleway, sans-serif",
                         "color_accent": "#002f58", "cover_background": "#002f58"}},
    doc_defaults={"footer": {"left": "{page}", "right": "Acme"}},   # every new document
))
```

`mount` runs Codraft's startup and shutdown inside your app's lifespan. The page works under any
URL prefix. If a proxy serves it under a different public path, it sends that path as
`X-Forwarded-Prefix`, which is honoured only alongside the proxy secret. `?embed=1` turns on
embedded mode for one page, for example in an `<iframe>`. `doc_themes` adds document themes
(the same keys as `[codraft.themes.*]`) and `doc_defaults` sets what new documents start with
(as `[codraft.doc_defaults]`). A theme can use any bundled font or Google Fonts family; Codraft
loads it in the editor and in exports.

In an `<iframe>`, Codraft tells the host page what's happening with `postMessage` (same origin
only), and navigates with `replaceState` so the host's Back button leaves the page:

| Message | When | Host page might |
|---|---|---|
| `{type: "codraft:location", path, doc, title}` | a document opens or closes | put `?doc=<id>` in its own URL, then frame `<prefix>/d/<id>` on load |
| `{type: "codraft:auth-required"}` | a request came back 401 or as an HTML login page | send the user to sign in again |

`CodraftMiddleware(tool_errors=True)` (the default) hands any tool's exception back to the model
as the tool result, so one failing lookup doesn't end a long drafting turn.

Access settings:

| Setting | Env | Effect |
|---|---|---|
| `proxy_secret` | `LANGSTAGE_CODRAFT_PROXY_SECRET` | Requests must carry `X-Codraft-Proxy-Secret`. Requests from the machine itself (the host app in front of Codraft) are exempt. |
| `auth_username` / `auth_password` | `LANGSTAGE_AUTH_USERNAME` / `LANGSTAGE_AUTH_PASSWORD` | HTTP basic auth, as in langstage-web. It applies to every request, from this machine too, and on top of `proxy_secret` when both are set. |
| `user_header` | `LANGSTAGE_CODRAFT_USER_HEADER` | A trusted header with the signed-in user: it names them on edits and in presence. |
| `allowed_hosts` | `LANGSTAGE_ALLOWED_HOSTS` (comma-separated) | `[server] allowed_hosts`. When `langstage-codraft run` binds to loopback (the default), it only answers to `localhost`, `127.0.0.1`, `[::1]`, `*.localhost` and these names, which guards against DNS rebinding. Add your proxy's public name here if the proxy forwards it as `Host` (Caddy and Traefik do by default). `*.example.org` matches subdomains, and `"*"` turns the check off. Apps that mount Codraft with `create_app` opt in with `host_guard=True`. |

The export renderer and `langstage-codraft export` send the configured secret and password
themselves. State-changing requests a browser sends from another website (a cross-site form
post) are refused with 403, as in langstage-web. `--show-config` masks the secrets.

## CLI

```
langstage-codraft run [--agent SPEC | --model SPEC | --demo] [--port 8060] [--approval auto]
langstage-codraft check [--live] [--json]
langstage-codraft new "Title" --template report
langstage-codraft import FILE.md|FILE.docx [--title T] [--template ID] [--theme NAME] [--split paragraph|section] [--json]
langstage-codraft list
langstage-codraft export DOC_ID... -f pdf|docx|pptx|png|jpg|html|gif|mp4 -o out.pdf [--data-dir DIR]
langstage-codraft mcp [--data-dir DIR]          # Codraft over MCP (stdio)
langstage-codraft install-browser
```

### Export without a running server

`export` doesn't need `langstage-codraft run` in the background. Point it at the data directory
and it renders in-process, so a batch job or CI step is one command:

```bash
langstage-codraft export doc_6e07ed1cea -f pdf -o report.pdf --data-dir .codraft

# several documents and formats share one browser launch; -o is then a directory
langstage-codraft export doc_6e07ed1cea doc_9a41c2d07b -f pdf -f png -o out/ --data-dir .codraft
```

It is the same renderer: the command starts the Codraft app inside its own process, prints
through the same page route the editor's Export button uses, and shuts down, so the output is
what a running server gives. That in-process app listens on `127.0.0.1` only, on a port the OS
picks, and every request to it needs a one-off password made for that run. It reads the same
`langstage.toml` (themes, `doc_defaults`, fonts) and is safe to run while a server is using the
same data directory.

Which path `export` takes:

| You pass | It exports |
|---|---|
| `--data-dir DIR` or `--standalone` | in-process, from that data directory (`--standalone`: the configured one) |
| `--port` or `--url` | through that running server |
| neither | through the server on the configured port if one is running, else in-process |

From Python:

```python
from langstage_codraft import Standalone, export_document

pdf = export_document(".codraft", "doc_6e07ed1cea", "pdf")                  # bytes
export_document(".codraft", "doc_6e07ed1cea", "docx", "out/report.docx")   # writes the file

with Standalone(".codraft") as codraft:       # one browser for many exports
    for doc_id in ("doc_6e07ed1cea", "doc_9a41c2d07b"):
        open(f"{doc_id}.pdf", "wb").write(codraft.export(doc_id, "pdf"))
```

PNG and JPG come back as the image for a one-page document and as a zip of page images
otherwise; HTML is always a zip. A failed export raises `ExportError` (`.status` 404 for an
unknown document, 503 when Chromium isn't installed).

**Cut-off content.** A block whose content is taller than the fixed height (or the pin's box)
it was given is cut off on the page, and what doesn't fit is missing from the export. Every
export says so:

```console
$ langstage-codraft export doc_6e07ed1cea -o report.pdf --data-dir .codraft
[ ok ] wrote report.pdf (48213 bytes)
[warn] doc_6e07ed1cea: 1 block is cut off in report.pdf, so part of the content does not print: blk_2b4f2daac7 (page 2). ...
$ langstage-codraft export doc_6e07ed1cea -o report.pdf --data-dir .codraft --strict   # exit 1, nothing written
```

| Where | What you get |
|---|---|
| `export` | a `[warn]` line per file; `--strict` fails the document (exit 1, no file); `--json` prints one object per export with `cut_off` |
| `GET /api/docs/{id}/export` | the header `X-Codraft-Cut-Off: blk_…@page,…`; `?strict=true` answers 409 with the list |
| `GET /api/docs/{id}/layout-check` | `{"ok": false, "cut_off": [{"block_id", "page", "type", "natural_px", "box_px", "preview"}]}` |
| `export_document`, `Standalone.export` | a `CutOffWarning`; `strict=True` raises `ExportError` (`.status` 409). `Standalone.check(doc_id)` returns the list |
| the editor's Export button | a notice naming the page, with **Show** to open the block |

A Word export has no such warning: Word reflows the text itself.

## Text, tables and figures on the page

- **Markdown dialect.** Text and callout blocks are GitHub-flavoured Markdown, plus superscript
  and subscript in Pandoc's form: `m^2^`, `H~2~O` (attached to the text before them, no spaces
  inside); `<sup>` and `<sub>` work too. `~~text~~` strikes text out; a single `~` is just a
  tilde. There is no maths syntax: write symbols as characters (°C, ±, ×, µ). The rich-text
  editor keeps all of this. A block with something it can't hold (a table, a picture, raw HTML)
  opens as Markdown instead, so nothing is lost by opening it.
- **Tables keep words whole.** A column is at least as wide as its longest word. When a table
  can't fit its column that way, long words break at a syllable with a hyphen, then the table
  is set smaller, and only then does a word break wherever it must. A Markdown table inside a
  text block looks like a table block, and its alignment row (`:--`, `:-:`, `--:`) is honoured.
  A table block takes `"align": ["left", "right", ...]`, one entry per column. Word export
  writes both as real tables.
- **Table cells are Markdown.** A cell of a table block is one line of Markdown: `**bold**`,
  `*italic*`, `` `code` ``, links, `m^2^`, `CO~2~` and citations. `"total": true` sets the last
  row as a total: bold, under a rule.
- **A long table continues on the next page.** A table splits between rows, in a column or
  across pages, and every continued piece repeats the header row. A split keeps at least two
  rows on each side, so a page never ends with a lone header or starts with a single row. A
  table with a fixed height stays one box. Word marks the header row to repeat as well.
- **Captions wrap.** A figure's caption, title and source line take the lines they need. A
  figure with a fixed height keeps that height: the picture or chart gives up the room.
- **Hyphenation.** Justified text is hyphenated by default, which evens out the word gaps
  (Design → Hyphenation turns it on or off for any document; `"hyphens": "auto" | "manual"` in
  `style`). Codraft finds the break points itself instead of leaving it to the browser, because
  the headless Chromium that renders exports has no hyphenation dictionaries: the editor and the
  PDF now break the same words. English ships with the app; German, Spanish, French, Italian,
  Dutch and Portuguese load when a document's language asks for them. Words that start with a
  capital are left whole. In the PDF each break point is tagged as a soft hyphen, which PDF
  viewers skip when you search or copy; a text extractor that ignores the tag (pypdf is one)
  shows a space there. Codraft's own upload reader uses PDFium for a browser-printed PDF, so
  an export you upload back as reference material reads as whole words.

## References and citations

A block's sources carry what a reference list prints:

```json
{"key": "rivera2024", "title": "Fleet costs", "authors": "Rivera, A. and Chen, B.", "year": "2024",
 "publisher": "Example Press", "url": "https://example.org/fleet", "accessed": "2026-01-05",
 "note": "supports the cost table"}
```

- **Cite by key.** `[@rivera2024]` in a text block, a callout, a row cell or a table cell prints
  as `[2]`, linked to entry 2 of the list; `[@rivera2024; @chen2023]` prints as `[2, 1]`. A
  source without a `key` gets one from its first author and year (or its title), shown in the
  Block panel under the source. A key no source has prints as `[?]`.
- **Numbers follow first appearance.** A source is numbered where the text first cites it, or
  where the first block that carries it stands. Move a block and the numbers follow; nothing is
  typed in by hand.
- **The list prints** `Authors (Year). Title. Publisher. URL. Accessed DATE.` in the
  `references` block (numbered, or `hanging` for a magazine-style list), in PDF, HTML, PNG,
  Word and PowerPoint.
- **`note` is private.** It is for you and the agent ("supports the cost table") and is never
  printed. Before 0.9.0 it was printed after the title.
- **Import** turns footnotes into sources cited by key, and reads a footnote shaped like
  `Rivera, A. (2024). Title. Publisher.` into authors, year, title and publisher.

## How the layout works

Blocks are measured in the browser at the width they'll occupy. A pure function then paginates
them (see `frontend/src/layout/paginate.ts`, which has its own unit tests):

- Blocks flow column-major. A full-width block balances the columns above it.
- A pinned block reserves a rectangle on the page instead of wrapping text around it. Content
  taller than a pinned or fixed-height block is flagged as overflow in the editor, and
  `view_pages` names the block to the agent.
- A block **anchored to the bottom** (`"layout": {"anchor": "bottom"}`, Block panel → *Stick to
  the bottom of the page*) sits at the foot of the page the flow has reached, and later blocks
  flow above it. It is how a call to action, a closing band or a sign-off goes at the bottom
  of a page: no pin and no reserved area. If it doesn't fit under what is already on the page,
  it goes to the foot of the next one.
- Text splits only at paragraph or line boundaries, keeping at least two lines on each side.
  A table splits between rows and repeats its header.
- **Back-fill.** A chart, picture, video, row or set of tiles that doesn't fit at the bottom of
  a page used to move whole and leave the rest of the page empty. With back-fill on, the text
  after it (up to the next heading) fills the gap and the figure opens the next column or page.
  One figure steps aside at a time, so figures keep their order. It is on for new documents
  (`"backfill": true` in settings; Document → *Let text fill the gap before a figure*). A
  document saved before 0.9.0 keeps its pages as they were until you turn it on.
- A block taller than a page is flagged rather than looping forever.
- A row of cells and a set of number tiles are each one block, measured at their width like a
  table. They need no logic of their own in the paginator.

The preview, the export and the agent's screenshots all print that same layout in headless
Chromium. Chromium never paginates anything again, so the editor and the PDF can't disagree.

![A three-column community newsletter page with a pinned masthead that repeats on every page](https://raw.githubusercontent.com/dkedar7/langstage-codraft/main/docs/assets/newsletter-page.png)

## Part of LangStage

| Stage | Package |
|---|---|
| Web workspace | [`langstage`](https://github.com/dkedar7/langstage) |
| Terminal | [`langstage-cli`](https://github.com/dkedar7/langstage-cli) |
| JupyterLab | [`langstage-jupyter`](https://github.com/dkedar7/langstage-jupyter) |
| VS Code | [`langstage-vscode`](https://github.com/dkedar7/langstage-vscode) |
| Hermes | [`langstage-hermes`](https://github.com/dkedar7/langstage-hermes) |
| **Documents** | **`langstage-codraft`** |
| Shared host + AG-UI bridge | [`langstage-core`](https://github.com/dkedar7/langstage-core) |

Docs: https://dkedar7.github.io/langstage-docs/stages/codraft/

## Development

```bash
uv venv && uv pip install -e ".[dev]"
cd frontend && npm ci && npm test && npm run build && cd ..
pytest                                   # unit + API + browser e2e (needs the built UI)
CODRAFT_LIVE=1 OPENROUTER_API_KEY=... pytest tests/live -s    # opt-in real-model checks
```

Run `npm run dev` in `frontend/` alongside `langstage-codraft run --demo --no-browser` to get
hot reload; Vite proxies `/api` to port 8060.

The design notes this started from are in `docs/notes/`, and the requirements are in
`docs/brainstorms/`.

## License

MIT
