Skip to content

3. Book Generator

Jimmy Bourque edited this page Nov 30, 2026 · 1 revision

Français

Important
The details presented in this document are generated in part automatically by AI. Because this document is not regenerated systematically, it may not reflect the latest version of the source code.

Book Generator

Technical identity Value
Audience Software developers and functional analysts
Namespace JbtiEvolution.OraklLearn.Generator.Books
Main class BookGenerator

The Book Generator turns an approved outline into a publishable learning book. It can produce written chapters and, when selected, enrich them with cover and chapter images, narrated audio, source references, and end-of-section quizzes. It preserves both the starting content and the released result so that a book can be reviewed, distributed, or used as the source for an assessment.

This page explains the implemented production path, its operational controls, and the principal code landmarks.

Table of contents

What the module delivers

Deliverable Purpose Produced when
Working book In-memory composition of sections, chapters, metadata, and media references. Every run.
Initial book snapshot Preserves the starting structured content before the released artifacts are created. This is the source used later by the Exam Generator. A successful book generation.
Released structured book Canonical serialized representation of the completed book. A successful run.
PDF and EPUB Portable publication formats rendered from the completed book. A successful run.
Optional media Cover, chapter illustrations, chapter/section audio, summary audio, title audio, and combined audiobook. The corresponding content type and setting are enabled.

The generator does not make publication decisions for a user. The calling flow is responsible for obtaining the selected structure, the presented cost, and the user’s approval before it starts the job.

Functional journey

flowchart LR
    A[Approved outline or edited book] --> B{Credit preauthorization}
    B -->|approved| C{Text enabled?}
    B -->|refused| X[Stop with trace]
    C -->|yes| D[Policy check and metadata generation]
    C -->|no| E[Reuse supplied structure and metadata]
    D --> F[Prepare working book]
    E --> F
    F --> G[Select or create cover]
    G --> H[Generate chapters with bounded parallelism]
    H --> I[Optional quizzes and audio book]
    I --> J[Save initial snapshot]
    J --> K[Render JSON, PDF and EPUB]
    K --> L[Publish artifacts and claim credits]
    L --> M[Completed]
Loading

The user-facing sequence is therefore: define the topic and structure, review it, choose the media options, approve the cost, then consult or edit the resulting book. The technical job begins only after those upstream decisions have been recorded.

Architecture and responsibilities

classDiagram
    class BookGenerator {
        +ProcessAsync()
        -GenerateBookAsync()
        -GenerateChaptersAsync()
        -GenerateChapterAsync()
    }
    class TextGenerationService {
        +generateMetadata()
        +generateChapters()
        +generateQuizzesAndSources()
    }
    class ImageGenerationService {
        +createBookAndChapterImages()
    }
    class AudioGenerationService {
        +createAndMergeNarration()
    }
    class Storage {
        +storeMediaAndArtifacts()
    }
    class Renderers {
        +renderStructuredBookPdfAndEpub()
    }
    class Pricing {
        +preauthorizeAndClaim()
    }
    class Tracing {
        +recordEventsAndBookTrace()
    }

    BookGenerator --> TextGenerationService
    BookGenerator --> ImageGenerationService
    BookGenerator --> AudioGenerationService
    BookGenerator --> Storage
    BookGenerator --> Renderers
    BookGenerator --> Pricing
    BookGenerator --> Tracing
Loading

ContentTypeProcessing is the first control point. It independently enables written content, the main book image, chapter-header images, and audiobook processing. Feature settings then refine that decision: for example, audio requires both the audio content type and AudioEnabled; generated chapter images require the chapter-image content type and ChapterImageEnabled.

Processing details

1. Admission, policy, and retry boundary

ProcessAsync(Guid jobId) is the public asynchronous entry point.

  1. It resets the pricing calculation and verifies that the job is preauthorized.
  2. When written content is enabled, it checks the subject against the configured content-policy service.
  3. It runs the production sequence.
  4. It retries recoverable failures up to SystemSettings.TryTolerance.
  5. It does not retry cancellation, HTTP failures, or failures classified as fatal.
  6. Between retry attempts, it clears the prior job artifacts and restores the original user-supplied book, when one exists.
  7. It always stops the book tracer and exposes Completed only after the released structured-book reference has been saved.

2. Build the working book

GenerateBookAsync creates a new working Book with the job identifier, configured language, and creation date.

Mode Source of sections and metadata Behavior
Text generation enabled The approved structure and generative text services. Prepares the table of contents, then produces the title, tags, and summary.
Text generation disabled The edited book supplied by the caller. Clones the supplied sections, tags, summary, and title before applying selected media and exports.

This separation is important for regeneration: a book can be rendered and enriched without asking a text-generation service to rewrite the editor’s content.

3. Select the cover

If the main-book-image content type is enabled, the cover follows this precedence:

  1. A user-selected cover is downloaded from the working cache and copied to catalog storage.
  2. Otherwise, a generated image is created when BookImageEnabled is enabled.
  3. Otherwise, the configured default cover is used.

The same principle is used for chapter images, except there is no default image: a user image takes precedence, then a generated image may be used, otherwise the chapter image is empty.

4. Produce sections and chapters

GenerateChaptersAsync schedules chapter work with a hard concurrency limit set by SystemSettings.ChapterParalellismTop. It stops admitting more tasks when a task faults, is cancelled, or returns failure.

For each content chapter, GenerateChapterAsync performs the following ordered work:

  1. Generate the chapter text.
  2. Optimize and simplify a content chapter; introduction and conclusion chapters instead receive Markdown normalization.
  3. In parallel, prepare a chapter summary for image generation and collect external references when those options are enabled.
  4. Calculate reading time.
  5. Generate chapter narration when audio is enabled.
  6. Select, copy, or generate the chapter image.
  7. Tag acronyms after narration and image generation, so the tags do not affect those transformations.

Section narration is generated only for content sections. Introduction and conclusion are identified from their position in the working book and are treated differently from the learning-content sections.

5. Complete enrichments and render

After all chapters succeed, the generator can create section quizzes. When audio is selected, it also creates the book summary and title narration in parallel, then assembles the audiobook.

Before publication, the generator writes an initial structured snapshot. It then renders the working book through three renderers:

flowchart TD
    B[Completed working Book] --> S[Initial structured snapshot]
    B --> J[Structured released book]
    B --> P[PDF]
    B --> E[EPUB]
    J --> C[Catalog storage]
    P --> C
    E --> C
    S --> C
    C --> U[User profile reference and credit claim]
Loading

The released structured artifact is the publication reference. PDF and EPUB are companion exports, while the initial snapshot is retained to anchor subsequent exam generation.

Operational controls

Control Implementation effect
Credit preauthorization Prevents generation before resource use begins.
Content-policy check Evaluates the subject before written content is generated.
Bounded parallelism Limits simultaneous chapter tasks and avoids unbounded inference or storage load.
User-media precedence Keeps an editor-selected image instead of replacing it with a generated one.
Trace per task Associates generation events with the book, section, or chapter task identifier.
Retry tolerance Repeats only failures that are eligible for retry and starts each retry from a clean job state.
Credit claim after publication Adds the book to the user profile only after its released structured artifact has been written.

Code map

Concern Main implementation point
Job lifecycle BookGenerator.ProcessAsync
Working-book assembly and final rendering BookGenerator.GenerateBookAsync
Controlled chapter scheduling BookGenerator.GenerateChaptersAsync
Chapter text, references, narration, illustration, and acronym handling BookGenerator.GenerateChapterAsync
Original-content retention BookGenerator.CanWriteInitialMDJsonBook
User-profile update and credit claim BookGenerator.UpdateUserProfile and BillProfile

The Book Generator is a production orchestrator, not merely a text writer: it coordinates generative text, image, and audio capabilities with storage, rendering, billing, cancellation, and traceability.

Clone this wiki locally