What are Macros?
Macros allow you to add custom methods to classes at runtime. This is a powerful extensibility pattern borrowed from Laravel that lets you extend Vizra ADK’s functionality without modifying the core code.Custom Tracking
Add analytics and tracking to your agents
Third-party Integrations
Integrate with external services seamlessly
Domain Logic
Add business-specific functionality
Reusable Patterns
Create patterns you can use across your app
Supported Classes
The following Vizra ADK classes support macros:Basic Usage
Registering a Macro
Register macros in yourAppServiceProvider::boot() method:
app/Providers/AppServiceProvider.php
Using Your Macro
Real-World Examples
Analytics Tracking
Track which models are using AI features:Conditional Execution
Add conditional logic to your builder:Metadata Tagging
Add metadata for logging or debugging:Cost Tracking
Track estimated costs for budget monitoring:Using Mixins
Mixins allow you to add multiple macros at once by grouping related functionality in a class:app/Mixins/AnalyticsMixin.php
app/Providers/AppServiceProvider.php
Mixins are ideal when you have a group of related macros that logically belong together, such as analytics, auditing, or domain-specific operations.
Advanced Patterns
Integration with Events
Combine macros with Laravel events for powerful side effects:Dynamic Configuration
Load configuration dynamically from your models:Workflow Macros
Add custom workflow types using theWorkflowManager:
Best Practices
Return $this
Always return
$this to maintain the fluent interfaceRegister Early
Register macros in
AppServiceProvider::boot()Descriptive Names
Use clear, descriptive names for your macros
Document Well
Document macros for other developers on your team
- Consider scope - Macros are global, so namespace them if needed to avoid conflicts
- Test your macros - Write tests to ensure macros work as expected
- Group related macros - Use mixins to organize related functionality
Testing Macros
Test your macros using Pest or PHPUnit:tests/Feature/MacroTest.php
Learn More
Laravel Macros Docs
Official Laravel documentation on macros
Agent Builder API
Complete AgentBuilder reference