Important: This guide provides comprehensive documentation for migrating fromcomposeContextto the new prompt composition methods, and from the old generation functions touseModel.
Table of Contents
- Overview of Changes
- Prompt Composition Migration
- Text Generation Migration
- Template Format Migration
- Complete Migration Examples
- Benefits of the New Approach
- Real-World Migration Example: Gitcoin Passport Score Action
Overview of Changes
The v1 migration introduces several key improvements:- Prompt Composition:
composeContextsplit into two specialized functions - Unified Model Interface: All generation functions consolidated into
runtime.useModel - Template Format: JSON responses replaced with XML for better parsing
- Better Type Safety: Improved TypeScript support throughout
Prompt Composition Migration
composeContext → composePrompt/composePromptFromState
v0: Single composeContext Function
v1: Two Specialized Functions
Key Differences
- Handlebars by Default: v1 always uses Handlebars (no simple replacement mode)
- Auto HTML Escaping: v1 automatically converts
{{var}}to{{{var}}}to prevent HTML escaping - State Handling:
composePromptFromStateintelligently flattens complex State objects - Random User Names: Both functions automatically replace
{{name1}},{{name2}}, etc. with random names
Migration Examples
Simple State Objects
Complex Runtime State
Dynamic Templates
Text Generation Migration
generateText → useModel
v0: Standalone Function
v1: Runtime Method
generateObject → useModel
v0: Object Generation
v1: Using useModel with XML
generateMessageResponse → useModel
v0: Message Response Generation
v1: Using useModel with XML Template
Template Format Migration
JSON → XML Templates
The most significant change is moving from JSON to XML format for structured responses.v0: JSON Template
v1: XML Template
Parsing Changes
v0: JSON Parsing
v1: XML Parsing
Template Examples
Complex Object Extraction
Complete Migration Examples
Example 1: Simple Action Handler
Example 2: Complex State Handling
Example 3: Custom Model Parameters
Benefits of the New Approach
1. Unified Interface
v0 Problems:- Multiple generation functions (
generateText,generateObject,generateMessageResponse) - Inconsistent parameter names
- Different return types
- Single
useModelmethod for all model interactions - Consistent parameter interface
- Predictable return types
2. Better State Management
v0 Problems:- Manual state flattening required
- Confusion between State object and simple key-value objects
- No intelligent handling of nested data
composePromptFromStateintelligently handles State objects- Automatic flattening of relevant fields
- Preserves state.values for template access
3. XML Over JSON
v0 Problems:- JSON parsing often failed with markdown code blocks
- Complex escaping issues
- Inconsistent formatting from LLMs
- XML is more forgiving and easier to parse
- Better handling of special characters
- More consistent LLM outputs
4. Type Safety
v0 Problems:- Loose typing on generation functions
- Runtime errors from type mismatches
- Poor IDE support
- Strong TypeScript types throughout
ModelTypeenum for model selection- Better IDE autocomplete and error detection
5. Extensibility
v0 Problems:- Hard-coded model providers
- Limited customization options
- Difficult to add new models
- Pluggable model system via
runtime.registerModel - Easy to add custom model providers
- Standardized model interface
6. Performance
v0 Problems:- Multiple parsing attempts for JSON
- Redundant context building
- No caching mechanism
- Single-pass XML parsing
- Efficient state composition
- Built-in caching support in
composeState
Real-World Migration Example: Gitcoin Passport Score Action
Here’s a complete migration of a real action from the Gitcoin Passport plugin:Original v0 Action
Migrated v1 Action
Memory Creation Migration
Complete Action Migration Summary
- Imports: Replace old functions with new equivalents
- Template: Convert JSON format to XML
- State Management: Use
composeStatewith filtering - Generation: Replace
generateMessageResponsewithuseModel - Parsing: Use
parseKeyValueXmlinstead of direct object access - Memory: Update to use
entityIdand new creation method
Migration Checklist
- Replace
composeContextwithcomposePromptorcomposePromptFromState - Update all templates from JSON to XML format
- Replace
generateTextwithruntime.useModel(ModelType.TEXT_*) - Replace
generateObjectwithruntime.useModel+parseKeyValueXml - Replace
generateMessageResponsewithruntime.useModel+parseKeyValueXml - Update
ModelClasstoModelTypeenum values - Replace
parseJSONObjectFromTextwithparseKeyValueXml - Update import statements to use new functions
- Test XML parsing with your specific use cases
- Consider using state filtering for performance optimization
- Update Memory objects to use
entityIdinstead ofuserId - Replace
runtime.updateRecentMessageStatewith filteredcomposeState - Remove
getEmbeddingZeroVector- embeddings are handled automatically

