A real-time collaborative workspace where teams write documents together, upload source material, and query an LLM that answers with citations from those sources.
Part 5 of 6 — auth, workspaces, documents edited live over a WebSocket, an AI assistant whose responses stream over server-sent events and are inserted as ordinary versioned edits, and uploaded sources it answers from with citations. Password recovery by emailed code. A word-processor toolbar, Word import and export, and a viewer that opens a citation at the chunk it cites. English and Russian. 515 backend tests and 65 client tests.
| Layer | Choice |
|---|---|
| API | FastAPI, Python 3.11+ |
| ORM | SQLAlchemy 2.0 (async) |
| Migrations | Alembic |
| Database | PostgreSQL 15 with pgvector 0.8.7 |
| Presence, pub/sub, job queue | Redis |
| Background jobs | Arq |
| Validation | Pydantic v2 |
| Client | React 18, TypeScript, Vite; react-pdf for the source viewer |
| Routing | React Router |
| Server state | TanStack Query |
| Editor | Tiptap 3 |
| LLM | Gemini via google-genai, or a scripted fake |
| Embeddings | gemini-embedding-001, or a local sentence-transformers model |
| File parsing | pypdf, python-docx (also Word import and export) |
| Tests | pytest, httpx; Vitest and jsdom for the client |
Requires Docker.
docker compose up -d --buildOpen http://localhost:5173. No .env is needed: every setting has a working
default, and a development signing key is generated on first start.
Only that one port is published, so the stack starts even when the machine
already runs its own Postgres, Redis or API on the usual ports. If 5173 is
taken, pick another in .env (WEB_PORT=5174) and run the command again.
Uploaded sources are embedded with Gemini by default, which needs
GEMINI_API_KEY; to run with no key at all, see
Running with no key below.
Coming from Part 4, the same command moves Postgres to an image with pgvector. It is the same major version (15), so the data volume carries over, and the migration enables the extension.
Verification codes are logged rather than mailed until SMTP is configured:
docker compose logs api | grep -A 4 "email:console"To send real email, copy .env.example to .env, set EMAIL_BACKEND=smtp
and the SMTP_* settings (Gmail, Yandex and Mail.ru examples are in the file),
then docker compose up -d. To check them, send yourself a test message:
docker compose exec api python -m api.mailcheck you@example.comThe API also logs at startup whether email is really being delivered.
Tests, migrations and the Vite dev server run on the host and need Postgres,
Redis and the API reachable from it. docker-compose.dev.yml
publishes them, bound to localhost:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d --build| API docs | http://localhost:8000/docs |
| PostgreSQL | localhost:5433 |
| Redis | localhost:6381 |
Each port moves with API_PORT, POSTGRES_PORT and REDIS_PORT in .env
-- change DATABASE_URL and TEST_DATABASE_URL to match. To make the overlay
the default, uncomment COMPOSE_FILE in .env.
Settings are commented in .env.example.
| Rebuild the client | docker compose up -d --build web |
| Client with hot reload | npm run dev --prefix web |
| Backing services only (needs the dev overlay) | docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d postgres redis |
| Host dependencies | pip install -r requirements-dev.txt |
| Migrations | alembic upgrade head |
| API on the host | uvicorn api.main:app --reload |
| Worker on the host | arq api.worker.WorkerSettings |
| Worker logs | docker compose logs -f worker |
| Tests | pytest |
| Client tests | npm test --prefix web |
Editors and owners get a toolbar over the document: text style, font and size, bold, italic, underline, strikethrough, inline code, superscript and subscript, text and highlight colour, links, alignment, line spacing and indent, bulleted, numbered and check lists (nested with Tab or the indent button), quotes, code blocks, horizontal lines, tables and page breaks. While the cursor is in a table, a table group adds and removes rows and columns, merges and splits cells and toggles the header row. On a narrow screen the groups that do not fit move into a More menu. The keyboard button, or Ctrl+/ (⌘/ on a Mac), lists every shortcut; Ctrl+Enter inserts a page break.
Everything is a Tiptap extension from the official set, except two of our own: the page break, an atomic node that docx export turns into a real page break, and indent and line height, which are paragraph attributes as Word keeps them (Tiptap's own line height marks the text instead).
Every node, mark and attribute is listed in
web/src/editor/schema.json under a version
number, and SCHEMA_VERSION in api/services/prosemirror.py must match it;
the client tests fail when the extensions and the list disagree, and the
API tests when the two numbers do. Changing the schema means bumping
both.
A client declares its version when it opens a document's socket. The
server refuses any other version, or none, before sending the document:
an outdated tab would otherwise replay steps it cannot parse, or drop the
nodes it does not know and save the document back without them. The
client also checks the version in init, so a new client talking to an
old server stops too. Either way the page offers a reload, and nothing is
applied or sent. Documents saved under an older version still open: new
attributes take their defaults.
Export — every document has an Export to Word button (any role):
GET /workspaces/{id}/documents/{document_id}/export answers with the
.docx itself, named after the title. Import — editors and owners
choose Import Word file on the workspace page:
POST /workspaces/{id}/documents/import reads the file and creates a new
document named after it, and answers with the document and dropped, the
codes of whatever the file held that the editor cannot; the page then says
in the reader's language what was left out.
Both run inside the request, on a worker thread: at the sizes allowed
(DOCX_IMPORT_MAX_BYTES, 10 MiB) that takes well under a second. A very
large document would want the Arq worker and a download link instead.
Only .docx is read. A Word 97-2003 .doc is refused with
import.legacy_doc and a hint to save it as .docx; nothing converts
it. The type comes from the bytes, not the name: a .doc and a
password-protected .docx share one container, and the streams inside it
tell them apart (import.encrypted). Damaged files are import.corrupt;
the ZIP checks are the same as for uploaded sources.
The importer walks the document's XML itself. mammoth, which turns a
.docx into clean HTML, was the alternative, but by design it leaves out
alignment, colour, fonts, sizes, highlight, spacing and indent -- half of
what export writes -- and python-docx's own paragraph and run objects skip
content controls and tracked insertions, losing their text.
A document exported and imported again comes back identical. From other Word documents:
| Export writes | Import reads | |
|---|---|---|
| Headings 1–6 | Heading 1–6 styles | Heading styles; Title as heading 1. Heading numbers are dropped |
| Alignment, indent | Paragraph alignment; half an inch per indent level | Same; indents round to the nearest level, up to 8 |
| Line spacing | As a multiple | Multiples; exact and at-least spacing dropped |
| Bold, italic, underline, strikethrough, superscript, subscript | Run formatting | Same, also from character styles; double strikethrough becomes single |
| Inline code | An "Inline Code" character style | That style |
| Text colour, font, size | Run formatting | Same; theme fonts and colours are not resolved |
| Highlight | Shading, in any colour | Shading, or Word's 16 highlight colours |
| Links | Hyperlinks | Web and mail links; others keep their text only |
| Bulleted and numbered lists, nested | A numbering definition per list, so each restarts | Levels, formats (numbers, letters, roman), starts, continued numbering, and Word's "List Bullet 2"-style names |
| Checklists | Bulleted items opening ☐ or ☒ | Those, and Word's checkbox content controls |
| Quotes, code blocks | Quote and "Code Block" styles | Those and Word's similar styles |
| Horizontal line | An empty paragraph with a bottom border | The same, as Word's own --- makes |
| Tables | Grid style, merged cells, a repeating header row | Merges both ways and the header row; ragged rows are padded |
| Page break | A real page break | Page breaks and "page break before"; a section break becomes a page break and is reported |
| Pictures, text boxes, equations, embedded objects, footnotes, comments, headers and footers, hidden text, column breaks, text effects | — | Dropped, and named |
| Tracked changes | — | Insertions kept, deletions removed, and reported |
| Page size, margins, paragraph spacing, first-line indent, custom styles | Word's defaults | Not read and not reported: page layout, which the editor does not model |
Beside the editor, editors and owners get a panel with five modes: continue from the cursor, rewrite the selection, summarize the document, ask about it and the workspace's sources, or answer from the sources alone. Tokens render as they arrive; the finished response is a preview, and nothing enters the document until Insert.
LLM_PROVIDER=gemini
GEMINI_API_KEY=... # https://aistudio.google.com/apikey
GEMINI_MODEL=gemini-2.5-flashWithout a key the app still starts; it logs a warning and each query fails with
ai.invalid_key. The key is a SecretStr and is never logged.