After the part
What a book does with a part once it was generated (notes, a redraft, a translation, the knowledge, audio) is the second phase of
the generation: when a generation of the author completed (from the page or the command bar; never for an agent, which does these
steps by its own commands), the engine goes through the stages of AfterPart on the server, with no page needed, and every step
bound to a stage runs in its turn. A plugin binds a step instead of decorating AutoAnnotate.finished (deprecated since 1.8.0).
@Autowired
private StoryGenerationService storyGenerationService;
private Registration afterPart;
// onExtensionLoad
afterPart = storyGenerationService.addAfterPartBinding(new AfterPart.Binding(KEY + ".translate",
() -> T.localized(TRANSLATE), AfterPart.Stage.TRANSFORM, AfterPart.Mode.SEQUENTIAL, new AfterPart.Handler() {
@Override
public boolean appliesTo(Manuscript book) throws Exception {
return settings(book).isAuto(); // the automatic switch of the book
}
@Override
public void run(AfterPart.Context context) throws Exception {
RunProgress progress = new RunProgress();
context.onStop(progress::stop);
Outcome outcome = translate(context.getManuscript(), context.getPart(), progress); // blocks until it is done
if (outcome.wroteSomething()) {
context.changed(); // the page that generated shows the part again
}
}
}));
// onExtensionUnload
afterPart.unregister();
Stages and modes
The stages run in this order, so what changes the text comes before what reads it. Inside a stage the SEQUENTIAL steps run one after
another in the order they were bound, each seeing what the one before left; then the PARALLEL ones start together and the stage ends
when all are done. A parallel step must not change the text of the part: its thread is marked Runs.readingOnly(), and a change through ChatMessageService there throws IllegalStateException (the step fails, which stops the phase). On a model with one connection parallel steps
take turns anyway: every request waits for a connection of its AI.
The handler
run(context) is plain blocking code on a worker thread, in the request scope of the user who generated, with the run of the phase
bound: the requests it makes to a model show in the panel of the runs, and a run of the registry it starts (PartRuns.run, a
TrackerRun of the trackers) is a part of the phase's entry (its text goes there, the phase's Stop stops it). appliesTo(book) says whether the
book uses the step (its switch); a step that does not apply is not shown. The context gives:
Throw to fail. A failure is a Stop: the phase ends where it is, the other steps of a parallel stage are stopped, what was applied
stays, and the author is told the step and the steps that did not run (onWarning of the generation's listener). There is no
resume: after a Stop the author does the rest by hand, so every binding needs a manual action for one part (and a command for an
agent). Do not call Notification from the handler: there is no page on its thread; use accessUI.
The phase as a run
The phase is one run of the registry, Runs.AFTER_PART (After part 14: Annotate ✓ · Redraft … · Translate): it holds the part in
STRUCTURE until the end of TRANSFORM (no delete, no move; the translators' WRITE runs are allowed), then in READ; it
setBlocksGeneration(true) during REWRITE. Its end has the outcome (FINISHED, STOPPED, FAILED). A regeneration of the part
stops it. The deprecated hook AutoAnnotate.finished is still called, in the page that generated, when the text-changing stages are
over (the listener's onPartFinal), not when they were stopped or failed.
See Generation pipeline → After the part and the runs.