Events Guide
Emitting Events
@Tool({ name: 'create_order' })
async createOrder(input: any, ctx: ExecutionContext) {
const order = await this.orderService.create(input);
// Emit event
ctx.emit('order.created', {
orderId: order.id,
userId: ctx.auth?.subject,
total: order.total
});
return order;
}
Listening to Events
@Injectable()
export class NotificationService {
@OnEvent('order.created')
async handleOrderCreated(data: any) {
await this.emailService.sendOrderConfirmation(data.userId, data.orderId);
}
@OnEvent('user.registered')
async handleUserRegistered(data: any) {
await this.emailService.sendWelcomeEmail(data.email);
}
}
Event Patterns
Naming Convention
Use dot notation:
- `user.created`
- `order.updated`
- `payment.failed`
Event Data
Include relevant context:
ctx.emit('product.purchased', {
productId: product.id,
userId: ctx.auth?.subject,
price: product.price,
timestamp: new Date()
});
Best Practices
- Async handlers - Don't block tool execution
- Error handling - Catch errors in handlers
- Naming convention - Use consistent patterns
- Documentation - Document what events are emitted