







After the queue server was running stably, the next job was to teach the AI how Opkle Editor works. A model writing good sentences is a different problem from it doing proper work inside the editor. Inside the editor, the user is looking at a rendered EPUB preview, but what actually gets saved is the document markup and styles. If a user fixes a paragraph in front of them and the code behind it ends up different, that's a bug. The same applied to the AI. What it did on screen and the code that goes into the EPUB had to move in the same direction.
What I wanted to build was not “an AI that writes markup for you.” Users aren't looking at a code editor. They select a paragraph, click an image, change formatting and give commands right on the preview. The editor then bundles the selected target's current structure, the shared styles in effect, the chapter-level style context, the target address and the user's command, and hands them to the AI. The AI has to read that state and return replacement units and style changes that can actually go into the EPUB.
What mattered here wasn't producing a plausible-looking screen. It was producing edits that can be applied again. The change seen in the preview and the saved document representation have to be the same, and the editor string, the document state and even the local saved state have to follow from the same change. So instead of letting the AI explain freely, I made it accept only patches that can be applied. All the AI can do is fix the structure and styles of the selected target, or run UI actions the editor knows.
In Opkle Editor, the preview isn't just a screen for seeing results. It is the surface where editing actually happens. Users work while looking at rendered paragraphs, images and tables, without having to look at EPUB code. But an EPUB is, in the end, made of markup and styles. So an edit made in the preview must always be able to go back to code. If that link is loose, the preview stays a mere preview instead of becoming an editor.
So a selected target had to be not just a screen node but a target with a code address. When a user selects a paragraph, the editor captures that paragraph's address, its current full element representation and the style context in effect. It hands the AI exactly the same. Sending only “make this sentence a bit bigger” isn't enough. It has to be given what the target's structure is now and which shared and local rules are affecting it.
This makes the roles of the preview and the code editor clear. The preview is the surface people touch, and the markup and styles are the basis for saving and export. The AI doesn't stir the preview DOM directly; it has to produce a code patch that can be applied to the target selected in the preview. Only then does a result changed by the AI remain not just on screen but inside the EPUB file.
This approach also fit well with the isolated render environment. The rendering result inside the preview has its own style space, but the editor can connect the target selected in it to the source in the document state. What's passed to the AI is not a piece of text visible on screen but a target context that can be saved again. The preview is an editing screen for people and a structured work target for the AI.
For the AI to join preview editing, it needed a different space from the body-text generation path. Body generation writes or polishes sentences that go into the book. Selection-based editing fixes the structure and styles of the selected target. So the premise of this path was an action engine from the start. It runs on the assumption that the user has selected one or more targets in the document and that their current representation and applied styles are given.
The input is each target's full element representation and the style context. The element has to be a unit that can replace the current node wholesale, and styles are divided into book-wide shared rules and local rules close to the chapter. The AI reads this information and interprets the user's request. The result splits into changes that alter the selected target itself and actions that move the editor UI.
This distinction mattered more than I expected. If a user says “make this paragraph blue,” a structure or style patch must be returned. But if they say “open the formatting popup,” the document must not be touched. Only a UI action should be returned. And when two things mix, as in “emphasize this paragraph and open the formatting popup too,” a document change and a UI action can come out together. It is a structure where code edits and UI control can coexist within a single input.
So I restricted the output not to free-form sentences but to a fixed, applicable shape. No explanations, no markdown, no decoration. That's because the editor interprets and applies this result as is. It mattered more for the editor to get a result it could run right away than for the AI to explain things kindly.
In selection-based editing, I made structural changes always come as whole replacement units. If it only accepted partial fragments, the place to apply them would become ambiguous. Since the selected node has to be replaceable wholesale, the AI must return the complete element for that target. Unless the request asks for a tag change, the default is to keep the existing structure and change only the needed attributes and local styles.
The address must never change. The target address is the coordinate where the editor plugs the patch back in. If the AI drops the address or invents a new one, the result can't be applied. So I fixed it in the instructions to keep the address exactly, and in the cleanup stage a change without an address is discarded. A patch with no coordinate to plug back into is unusable however good it looks.
Even when only styles change, the shape of the replacement unit had to be the same. Even when the work is just adding an override to the shared stylesheet, a consistent result shape keeps the application path simple. In exchange, only targets that were actually changed are included in the result. If targets that weren't changed are repeated every time, needless replacements happen, and the selection state or the undo stack can get disturbed for nothing.
With this contract, a structure made by the AI becomes a stable patch unit between the preview and the document state. The user says “make this paragraph stand out a bit more,” but the server receives the target address and current representation, the AI returns the revised replacement unit, and the editor replaces that node. After that, the serialized document, the editor string and the local saved state follow from the same change.
I handled styles even more carefully. If you let the AI rewrite the stylesheet at will, the whole EPUB can wobble. So the channels through which selection-based editing can change styles were restricted. One is the local style attached only to an element, and the other is an override appended to the shared stylesheet. Chapter-level styles are given only as read-only context. The AI is blocked from returning a modified version of that channel.
A local style is used for changes that apply to just that one element. When changing the text color of one selected paragraph, or adjusting the margin of one particular image, putting it directly inside that element is more stable. The patch scope is clear. Since it affects only one target, other paragraphs and formatting don't change unexpectedly.
The shared stylesheet is used when a common selector has to change. If you must change a rule that applies to several elements together or the style of a particular class, you append only the new rule at the end. What matters here is append-only. It doesn't rewrite the existing stylesheet or repeat it as is. Since rules appended later win in the cascade, returning only the needed override is enough. It's a way of stacking changes without breaking existing styles.
I made the local context channel never modifiable. That information is merely background for the AI to understand the current state, not a write channel. Whatever value a local rule is setting, if it must change, it is handled through an element-local style or a shared override. Restricting channels like this makes it clear where a style patch goes, and the stylesheet structure at EPUB export becomes predictable too.
In preview editing, not every request ends in a structure or style patch. From the same input box, users can also say UI controls like opening the preview, turning on selection mode, adding a chapter or opening the formatting window. Such requests must not be mistaken for document edits. So I needed a finite set of actions the editor can actually perform.
This set is not a list of natural-language descriptions but an alphabet of executable identifiers. The AI can't make up new action names and must return only elements of the set. This alphabet is used as is in the selection-based editing path too. For a pure UI request, the document change is empty and only the action is included. For a request mixing a target edit and a UI command, both are returned. Thanks to that, whether what a user says on the preview is a code patch or a UI action is handled within the same pipeline.
The reason for restricting action identifiers to an alphabet is simple. A command that can't be executed means nothing to the editor. Natural-language requests can be accepted flexibly, but execution must be pinned down by deterministic identifiers. Put the alphabet in the instruction context, filter out unknown elements in the cleanup stage, and only executable commands remain. Even when the AI does the judging, the final output had to come down in the editor's language.
What I paid particular attention to was handling requests that can be settled before calling the AI. Input that directly matches an editor UI action, such as preview, new project, add chapter or the formatting window, doesn't need to go all the way to the model. So I built a rule-based router made of normalization and staged matching.
User input isn't taken at face value. First it strips decorative prefixes, tidies whitespace and punctuation, and corrects spellings that often vary. Differences in word spacing are absorbed into a compressed form. Because users don't always type Korean UI commands exactly, this cleanup stage mattered quite a bit.
But the wider the net, the more defense you need. A request to write body text must not be mistaken for a formatting command. So I added a writing veto. When generative intent is mixed in, such as writing, polishing, proofreading, translating, summarizing or continuing, it gives up on UI action matching and passes the request to the body-text path. Conversely, short UI words are managed as a strict set, so that one that happens to appear inside a long sentence isn't immediately routed as an action.
When there is a selected target, I used a more conservative gate. That's because it can be ambiguous whether the user's words are a target edit or a UI command. So UI matching before selection lets through only exact matches and core expressions. I deliberately excluded broad fuzzy matching. When in doubt, it's better to judge by looking at the selection context. This conservatism mattered in preview editing.
I didn't trust the model's response as is, either. A UI action response should in principle be a single identifier, but in practice quotation marks, decoration, arrays, objects, periods and explanations within sentences can get mixed in. So the normalization layer absorbs many appearances. It gathers candidates within a structure, and if a single identifier is embedded in a sentence, it catches that too. If multiple candidates appear, it's ambiguous, so it discards them.
Selection-based responses are trickier. Structure and style values can contain nested brackets, so you can't cut out an object with a plain substring. So an extraction that finds the top-level candidate while keeping brackets balanced is needed. It ignores brackets inside string literals and parses first the candidate holding the applicable changes and actions. Even with explanations attached before or after, it has to be able to pull out only the executable object.
The parsed change items are tidied again. If an address is empty, discard it, and if a structure value is wrong, correct or exclude it. The style channels accept only strings, and the action list passes through alphabet normalization to remove unknown elements. If parsing itself is impossible, it isn't accepted and triggers a retry.
Only with this layer does the editor's application rule hold even if the AI gets a little sloppy. We demand strict output from the AI, but the server does realistic recovery. In cases that can't be recovered, though, it doesn't force an application. Changes without an address, unknown actions and uninterpretable structures are discarded or retried. To keep the preview and the EPUB code in agreement, not applying an ambiguous patch is better than applying it.
Once this structure is in place, users can edit an EPUB without looking at code. They select a paragraph in the preview and say things like “make this paragraph smaller,” “center this image,” “make this table look good on mobile,” or “open the formatting popup.” The editor judges whether that is a code patch or a UI action, builds the selection context, tidies the AI response into an applicable form, and reflects only what is possible in the document state.
What matters is that it doesn't change only on screen. The replacement unit changes, an override is attached to the shared stylesheet, the UI action runs, and the result carries through to the editor string and the local saved state. The preview, the code editor and the storage share the same change. The AI is not a separately bolted-on decoration but moves inside the editor's serialization pipeline.
Deterministic routing and selection-based editing complement each other. Routing quickly handles definite UI actions, and the selection-based path produces structure and style patches for the target. Even a request that entered the body-writing path can, if it is judged to be a UI action after all, escape back into the action alphabet. The user types a single natural-language input, but internally body generation, UI routing and selection patches are separated.
As a result, the AI became a worker that follows Opkle Editor's way of writing EPUBs. Some styles must go only on the element, some must be appended after the shared stylesheet, and chapter-level styles must be treated as read-only. Some requests change the replacement unit, and some must run the action alphabet. The address must be kept, the structure must be a complete replacement unit, and the output must keep the strict applicable form. As these rules mesh, the preview edit and the actual EPUB code came to look at the same result.
This stage also came together reliably. The selection context in the preview, the applicable output contract, address-based replacement, append-only shared styles, the finite action alphabet, input normalization, the writing veto, the conservative pre-selection gate, and output normalization and sanitizer were organized into a single AI editing layer. In repeated reviews, preview editing, UI action routing, structure and style patch application, removal of unknown actions, retries and document state reflection all passed reliably. Opkle Editor can now keep the EPUB preview the user sees and the EPUB code that is actually saved in step within a single editing flow.
