ESC
Start typing to search...

Events & Hooks

Subscribe to agent events and hook into the lifecycle for custom logic.

Lifecycle Hooks

HookDescription
onLoadCalled when plugin is loaded
onReadyCalled when agent is fully initialized
onShutdownCalled 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

EventDescription
message:incomingNew message received
message:outgoingMessage sent by agent
message:editedMessage was edited
message:deletedMessage 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

EventDescription
wallet:transactionTransaction confirmed
wallet:incomingReceived funds
wallet:outgoingSent 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

EventDescription
tool:beforeBefore tool execution
tool:afterAfter tool execution
tool:errorTool 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);
});