Skip to content

Narrator Agent

Overview

The Narrator Agent is responsible for generating and managing game content, formatting responses, and maintaining narrative consistency using LLM-based generation.

Core Architecture

graph TD
    subgraph Narrator Agent
        NA[Narrator Agent] --> NM[Narrator Manager]
        NA --> GM[Generation Module]
    end

    subgraph Components
        GM --> CG[Content Generator]
        GM --> TF[Text Formatter]
        GM --> SC[Style Controller]
    end

    NA --> |Validates| Rules[Rules Agent]
    NA --> |Updates| State[Game State]

Key Components

Content Generation

  • Narrative Generation

    • Scene description
    • Character dialogue
    • Action narration
  • Content Adaptation

    • Context awareness
    • Style consistency
    • Tone management
  • LLM Integration

    • Prompt engineering
    • Response formatting
    • Style control
class NarratorAgent:
    async def generate_content(
        self,
        context: NarrativeContext,
        style: NarrativeStyle
    ) -> NarrativeContent:
        # Build prompt with context and style
        prompt = self._build_narrative_prompt(context, style)

        # Generate content using LLM
        response = await self.llm.ainvoke(prompt)

        # Format and structure the response
        return await self._format_narrative(response, style)

Formatting System

The Narrator Agent formats content through multiple stages:

  1. Content Structure
  2. Scene organization
  3. Dialogue formatting
  4. Description layout

  5. Style Application

  6. Tone consistency
  7. Language adaptation
  8. Theme maintenance

  9. Output Formatting

  10. Markdown formatting
  11. HTML generation
  12. Text styling

Narrative Flow

sequenceDiagram
    participant SG as Story Graph
    participant NA as Narrator Agent
    participant LLM as LLM Generator
    participant ST as State

    SG->>NA: Request Content
    NA->>ST: Get Context
    NA->>LLM: Generate Content

    par Generation
        LLM->>LLM: Process Context
        LLM->>LLM: Apply Style
        LLM->>LLM: Generate Text
    end

    LLM-->>NA: Raw Content
    NA->>NA: Format Content
    NA->>ST: Update State
    NA-->>SG: Formatted Response

Best Practices

  1. Content Generation
  2. Clear context building
  3. Style consistency
  4. Error recovery

  5. LLM Integration

  6. Structured prompts
  7. Response validation
  8. Style control

  9. Performance

  10. Content caching
  11. Batch processing
  12. Memory management

Error Handling

The Narrator Agent implements comprehensive error handling:

try:
    # Generate content
    content = await self._generate_content(context)

    # Format and validate
    if await self._validate_content(content):
        return await self._format_content(content)
    raise NarratorError("Invalid content generated")

except NarratorError as e:
    logger.error("Narration error: {}", str(e))
    return NarrativeContent(
        valid=False,
        error=str(e),
        fallback_content=self._get_fallback_content()
    )

Performance Considerations

  1. Generation Optimization
  2. Prompt caching
  3. Response memoization
  4. Batch processing

  5. Formatting Strategy

  6. Template system
  7. Style sheets
  8. Content reuse

  9. State Management

  10. Context caching
  11. Style persistence
  12. Memory efficiency

Integration Points

  1. Story Graph
  2. Flow control
  3. Content requests
  4. State updates

  5. Rules Agent

  6. Content validation
  7. Context checking
  8. Style enforcement

  9. State Manager

  10. Context retrieval
  11. History tracking
  12. Style management

Style System

The Narrator manages different narrative styles:

class NarrativeStyle:
    def __init__(self):
        self.tone: str  # e.g., "dramatic", "humorous"
        self.language: str  # e.g., "formal", "casual"
        self.perspective: str  # e.g., "first_person", "third_person"
        self.formatting: Dict[str, str]  # Style-specific formatting

Content Templates

The agent uses templates for consistent formatting:

TEMPLATES = {
    "scene": """
    # {scene_title}

    {scene_description}

    ## Available Actions
    {actions}
    """,

    "dialogue": """
    **{speaker}**: {dialogue}
    """,

    "action": """
    > {action_description}
    """
}