Events & Hooks
Subscribe to agent events and hook into the lifecycle for custom logic.
Lifecycle Hooks
| Hook | Description |
|---|---|
onLoad | Called when plugin is loaded |
onReady | Called when agent is fully initialized |
onShutdown | Called before agent shuts down |
Example
export default definePlugin({
name: 'my-plugin',
tools: [],
onLoad: async (ctx) => {
ctx.logger.info('Plugin loading...');
},
onReady: async (ctx) => {
ctx.logger.info('Agent is ready!');
},
onShutdown: async (ctx) => {
ctx.logger.info('Cleaning up...');
}
});Message Events
| Event | Description |
|---|---|
message:incoming | New message received |
message:outgoing | Message sent by agent |
message:edited | Message was edited |
message:deleted | Message was deleted |
Example
onReady: async (ctx) => {
ctx.events.on('message:incoming', async (message) => {
console.log(`New message from ${message.senderId}: ${message.text}`);
});
ctx.events.on('message:outgoing', async (message) => {
console.log(`Agent sent: ${message.text}`);
});
}Wallet Events
| Event | Description |
|---|---|
wallet:transaction | Transaction confirmed |
wallet:incoming | Received funds |
wallet:outgoing | Sent funds |
Example
ctx.events.on('wallet:incoming', async (tx) => {
const amount = tx.amount / 1e9; // nanotons to TON
ctx.logger.info(`Received ${amount} TON from ${tx.from}`);
// Notify admin
await ctx.telegram.sendMessage(
ctx.config.adminUsers[0],
`💰 Received ${amount} TON!`
);
});Tool Events
| Event | Description |
|---|---|
tool:before | Before tool execution |
tool:after | After tool execution |
tool:error | Tool execution failed |
Example
ctx.events.on('tool:before', async ({ name, params }) => {
ctx.logger.debug(`Executing tool: ${name}`, params);
});
ctx.events.on('tool:error', async ({ name, error }) => {
ctx.logger.error(`Tool ${name} failed:`, error);
});Custom Events
Plugins can emit and listen to custom events:
Example
// Emit custom event
ctx.events.emit('myPlugin:customEvent', { data: 'value' });
// Listen to custom event
ctx.events.on('myPlugin:customEvent', async (data) => {
console.log('Received:', data);
});