Engineering case study

Writer's Room

A manuscript editing project at Sudowrite, developed with a small community of beta users, that helped inform Sudowrite's agentic chat experience.

Role

Software engineer, Project Owner, Occasional UX designer, Community Manager

Selected technologies
  • TypeScript
  • Next.js
  • Slate.js
  • React
  • Yjs
  • Hocuspocus
  • Anthropic
  • Mastra
  • Vercel AI SDK
  • PostgreSQL
  • Prisma
  • Valkey
  • Langfuse

We built Writer's Room at Sudowrite to give authors on-demand support throughout the editing process. The idea was to have an AI editing partner available whenever an author was ready to work, whether they wanted feedback on a chapter, help thinking through a revision, or someone to discuss a suggestion with before making a change. That ongoing conversation was an important part of the experience we wanted to explore.

Writer's Room helped authors organize and revise full-length manuscripts with AI editing partners. Writers could move between a book's structure and individual passages, request editorial feedback, work with an agent to implement suggested changes, and export their annotated manuscript to Word or PDF.

I worked across the full stack from the initial monorepo setup through beta launch and iteration. Much of my work focused on seamlessly connecting the editor, collaborative document state, and AI agent to the manuscript while the author revised their work. Alongside that development work, I collaborated with a small community of beta users whose experiences helped us understand how authors wanted to use AI during revision.

Writer's Room: manuscript feedback, beta reader perspectives, and a conversation with the AI editor. Silent video, about one minute.

Building Around Long-Form Writing

Books have a lot of structure but it's not all marked in obvious ways for an agent to easily search and manipulate. Things like the beats and scenes that are composed into chapters, story structures like Freytag's Pyramid that span the whole manuscript, and non-linear plots all use structure but they aren't easily identifiable by headlines or breaks in the text. They often require reading and interpreting large passages, which can be costly, expensive, and error prone with AI.

To help the agent work with these more implicit structures, I built a manuscript import pipeline that broke manuscripts into beats and scenes that helped the agent more quickly find relevant passages and provide focused edits. The import pipeline also identified the story structure and where key pieces of it occurred so that the agent could better advise the author about how changes could impact the placement of the climax, for instance. The pipeline combined deterministic chapter detection via headlines, page breaks, and other explicit markers with AI scene detection.

A Story Bible, project-level rules for the agent, and custom feedback types gave authors control over the context used in editorial reviews. I also built genre detection using BISAC categories so feedback could account for the conventions of the manuscript's genre.

Authors could export their work to Word or PDF with its formatting intact. For Word exports, I had also mapped collaborative comments and database-stored suggestions, goals, and action items into native Word comments. This let authors continue working with the feedback outside the app.

Choosing the Application Architecture

The application used Next.js, React, TypeScript, PostgreSQL, and Prisma, although several parts of the architecture changed as I developed the editing experience. Initially, I tried to use a beta version of Zero as a sync engine to keep application state in sync between the client and the server, but ran into issues where it wasn't reflecting server changes back to the client. That led me to pivot to TanStack Query, which solved the problem in a familiar way but did lead to more work managing caches.

Early on, I also evaluated TipTap Cloud as a solution for an integrated Yjs document manager with AI support, but it didn't leave enough room to experiment with customizing the AI features that could interact with the document and would have locked us into using TipTap. I also knew from Sudowrite that TipTap isn't the easiest editor to develop with, so Writers Room seemed like a good opportunity to explore alternatives. I opted to use Slate.js for the editor with Yjs and a standalone Hocuspocus collaboration server, which served the application's needs well. It was very developer friendly and allowed for quick editor development.

Initially, I had included the Mastra agent layer within the Next.js app for ease of CI setup and deployment. A few months into the project, though, I moved it out of the Next.js application into its own Dockerized HTTP service, using Clerk JWTs to authenticate requests. Having separate services meant we could scale collaboration and AI processing independently, while Sentry and Langfuse made it easier to follow errors and agent activity within each service. The separation also improved hot reloading during development, which made working across these parts of the application less cumbersome.

Connecting Revision Plans to the Manuscript

An author might want to discuss a revision before having the agent leave comments throughout a chapter. To support that workflow, I built tools around planning and agentic commenting. It was clear early on that authors had complex tasks they needed the agent to support and plans helped keep the agent on track. The assistant could propose a plan, wait for the author to approve or revise it, and then add comments and suggested rewrites anchored to relevant passages.

For longer reviews, I also added SSE heartbeats and improved incremental stream flushing to help preserve partial responses when a connection dropped, along with state synchronization when an agent became inactive partway through a plan.

Before an agent could do useful editorial work, it also needed enough context to understand what it was reviewing. I introduced a Gemini-backed manuscript exploration tool with context caching so the agent could inspect whole chapters and full manuscripts. Later, I added a fast Haiku orchestration step to gather shared context, including the document structure, Story Bible, and available feedback types, before passing the task to specialist agents. Using an orchestrator agent with specialist subagents reduced hallucinations by ensuring that the orchestrator agent only had the specific information it needed to reply to the user in its context. Context from reading the manuscript, for example, would stay in a specialist's context, keeping the orchestrator agent from thinking it "remembered" text that it usually didn't.

Checking What the Agent Actually Read

One of the difficulties with AI-generated feedback is that a response can sound reasonable even when it's discussing text that isn't in the manuscript. If an agent quotes a passage, for example, the author should be able to find that passage in their book. To help with performance for large manuscripts, the Slate.js editor cached nodes and refreshed the cache on specific actions. Sometimes, that could be buggy, though. To help identify these caching issues, I built a fabrication detector that extracted quotes from model responses and checked them against cached Slate document nodes, then surfaced flagged messages in the admin analytics view so we could investigate them. The detector also removed quotes from the agent's response if they were fabricated, so the user would never see them.

For suggested rewrites, I added exact substring matching on the server to check the text an edit was supposed to replace before inserting it. This was particularly important when the document had changed while the agent was working, since a suggestion still needed to refer to the correct passage. If the document changed while the agent was working, the agent then knew to recheck the text and determine if the rewrite still made sense.

Users tended to prefer staying in one long chat thread over creating multiple, separate threads. This meant that over time, the message history could cause a chat to exceed the agent's context window. I also integrated message compaction to help in this scenario, so that users could continue chatting. Since authors tend to do a lot of brainstorming in chat that's not stored anywhere else and usually needs to be revisited, I customized the compaction strategy so that key decisions were saved to the database and prioritized compacting messages for things that could easily be repeated, like reading saved documents.

Users also reported that the agent tended to say it was going to do something, but then stop without doing anything at all. To combat this, I added stall detection that showed the author a retry option instead of leaving them waiting for work that wasn't continuing.

Keeping the Editor Responsive

A manuscript with tens of thousands of words and hundreds of comments puts considerably more pressure on an editor than a short document does. As our documents grew, repeated rendering and DOM lookups made typing and scrolling sluggish, so I profiled those paths with Chrome dev tools to see which work we could avoid repeating.

Based on my findings, I memoized the document and comment components, cached node-path and annotation lookups, and reduced unnecessary updates from the client state store. I also paused heavier DOM work while the author was typing and virtualized the comments sidebar so it only needed to render the visible portion of the list. These changes targeted the work happening during everyday interactions, when even small delays could make writing feel awkward.

We ran into a different problem on the server, though: bursts of aborted requests were followed by out-of-memory crashes. To get to the root of the issue, I added diagnostic endpoints and traced the issue to document editor caches and client actors that were accumulating without being removed. To keep that memory bounded, I replaced the caches with an LRU cache that evicted older entries and extended an entry's expiration while it was still in use. I also carried cancellation through background processing when clients disconnected and adjusted Valkey expiration with a database fallback to reduce repeated processing. Together, these changes stabilized server memory under load.

Keeping Comments Attached as the Document Changes

Live editing added some interesting edge cases to inline feedback. An author could change a paragraph while an agent was commenting on it, and undoing an edit needed to account for the comments as well as the text. Since comments lived partly in the shared Yjs document and partly in PostgreSQL, I also needed to reconcile their identities across both stores.

I built custom Slate undo and redo handling that worked with comments, detected concurrent edits during comment generation, and added normalization to remove duplicated section breaks after merges. Browser tab changes could also trigger competing reconnection attempts, so I debounced and deduplicated the Hocuspocus reconnections. For Word exports, I reconciled annotation IDs across the two stores and appended comments whose anchors could no longer be found, keeping that feedback available even when it couldn't be placed beside its original passage.

Evaluating Changes as the Product Evolved

As I changed prompts, models, and agent workflows, I needed a way to check whether the changes were helping. I built internal analytics, chat and agent replay, and evaluation runners for genre guidance, plan execution, document exploration, and fabrication detection. Together, these tools let us follow a review through its individual steps and investigate where the result diverged from what the author requested.

I also built a user-facing Docusaurus documentation site that explained the app features. With the editor, collaboration server, and agent service evolving together, having a place to document their behavior made the system easier to work on and maintain.

Bringing the Learnings into Sudowrite Agentic Chat

Working with a small beta community gave us a chance to learn from how authors actually used the application. Their preference for long-running chats, for example, helped shape how I approached memory and compaction, while reports of agents stopping mid-task highlighted the need to detect incomplete work and make it easier to retry. These were product decisions as much as engineering problems, because they affected whether authors could keep working without losing their train of thought.

We ultimately took what we learned from Writer's Room and used it to inform Sudowrite Agentic Chat. My work there continued many of the same concerns: keeping the assistant connected to the author's intent, making proposed changes reviewable, and applying edits to the right part of a live manuscript. The Writer's Room beta helped connect those engineering decisions to the experience we wanted authors to have in Sudowrite.