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:
- Content Structure
- Scene organization
- Dialogue formatting
-
Description layout
-
Style Application
- Tone consistency
- Language adaptation
-
Theme maintenance
-
Output Formatting
- Markdown formatting
- HTML generation
- 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
- Content Generation
- Clear context building
- Style consistency
-
Error recovery
-
LLM Integration
- Structured prompts
- Response validation
-
Style control
-
Performance
- Content caching
- Batch processing
- 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
- Generation Optimization
- Prompt caching
- Response memoization
-
Batch processing
-
Formatting Strategy
- Template system
- Style sheets
-
Content reuse
-
State Management
- Context caching
- Style persistence
- Memory efficiency
Integration Points
- Story Graph
- Flow control
- Content requests
-
State updates
-
Rules Agent
- Content validation
- Context checking
-
Style enforcement
-
State Manager
- Context retrieval
- History tracking
- 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: