Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:666ghj BettaFish DocumentComposer build document

From Leeroopedia
Knowledge Sources
Domains Report_Generation, Document_Assembly
Last Updated 2026-10-08 00:00 GMT

Overview

Concrete stitcher that assembles validated chapter JSONs into a single Document IR with ordered chapters, unique anchors, unified metadata, and theme tokens.

Description

The DocumentComposer.build_document method takes all validated chapter JSONs, the report metadata, and a report ID, then produces a complete Document IR dict. It:

  • Sorts chapters by their `order` field
  • Generates unique anchor IDs for each chapter (resolving conflicts)
  • Injects default chapterId if missing
  • Ensures chapters with error placeholders have heading blocks
  • Merges metadata, themeTokens, and assets
  • Adds IR version and generation timestamp

Usage

Import this class when you need to combine chapter JSONs into a single document for rendering. It runs after all ChapterGenerationNode calls complete.

Code Reference

Source Location

  • Repository: BettaFish
  • File: `ReportEngine/core/stitcher.py:L15-133`

Signature

class DocumentComposer:
    def __init__(self):
        """Initialize with empty seen-anchors set."""

    def build_document(
        self,
        report_id: str,
        metadata: Dict[str, object],
        chapters: List[Dict[str, object]],
    ) -> Dict[str, object]:
        """
        Assemble chapters into a complete Document IR.

        Args:
            report_id: Unique report ID
            metadata: Global metadata (title, theme, toc, hero)
            chapters: List of validated chapter JSON dicts

        Returns:
            Document IR dict with version, reportId, metadata,
            themeTokens, chapters (ordered), assets
        """

Import

from ReportEngine.core import DocumentComposer

I/O Contract

Inputs

Name Type Required Description
report_id str Yes Unique report ID
metadata Dict Yes Global metadata (title, theme, toc, hero)
chapters List[Dict] Yes Validated chapter JSON dicts

Outputs

Name Type Description
document_ir Dict Complete Document IR with version, chapters, metadata

Usage Examples

Assemble Document IR

from ReportEngine.core import DocumentComposer

composer = DocumentComposer()
document_ir = composer.build_document(
    report_id="report_20260101_120000",
    metadata=layout_design,
    chapters=[chapter1_json, chapter2_json, chapter3_json],
)
print(f"Document IR version: {document_ir['version']}")
print(f"Chapters: {len(document_ir['chapters'])}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment