| title | Actions |
|---|
Actions are callable functions that can be invoked by workflows, conversations, other actions, and external API clients. They encapsulate reusable logic and can be called from anywhere in your agent.
Create an action in src/actions/:
import { Action, z } from "@botpress/runtime";
export default new Action({
name: "myAction",
input: z.object({}), // Input schema
output: z.object({}), // Output schema
handler: async ({ input }) => {
// Function logic
return {}
},
});Define input and output schemas using TypeScript types or Zod schemas:
export default new Action({
name: "calculateTotal",
input: z.object({
items: z.array(z.object({
price: z.number(),
quantity: z.number(),
})),
}),
output: z.object({
total: z.number()
}),
handler: async ({ input }) => {
const total = input.items.reduce(
(sum, item) => sum + item.price * item.quantity,
0
);
return { total };
},
});Actions can be called from anywhere in your agent's source. Just import actions and call them directly:
import { Workflow, actions } from "@botpress/runtime";
export default new Workflow({
name: "someWorkflow",
handler: async ({}) => {
const result = await actions.doSomething();
// Other logic
},
});import { Trigger, actions } from "@botpress/runtime";
export default new Trigger({
name: "webchatConversationStarted",
events: [
"webchat:conversationStarted",
],
handler: async ({ event }) => {
const result = await actions.doSomething()
// Other logic
},
});import { Autonomous, actions, z } from "@botpress/runtime"
export default new Autonomous.Tool({
name: "someTool",
description: "A tool that does something",
input: z.object({}),
output: z.object({ result: z.string() }),
handler: async () => {
const result = await actions.doSomething()
return { result }
},
})You can provide an action to your agent as a tool using the asTool method:
import { Conversation, actions } from "@botpress/runtime";
export default new Conversation({
channel: "*",
handler: async ({ execute }) => {
await execute({
instructions: "You are a helpful assistant.",
tools: [actions.calculateTotal.asTool()]
});
},
});When you install an integration, its actions become available anywhere in your agent's source:
import { Trigger, actions } from "@botpress/runtime";
export default new Trigger({
name: "webchatConversationStarted",
events: [
"webchat:conversationStarted",
],
handler: async ({ event }) => {
let conversationId = event.conversationId
if (conversationId) {
await actions.webchat.showWebchat({
conversationId
})
}
},
});- Keep actions focused on a single responsibility
- Use actions for reusable logic that's needed across multiple parts of your agent
The handler function receives an object with input and client properties: