Creating a conversation
Create a conversation handler insrc/conversations/:
Report incorrect code
Copy
import { Conversation } from "@botpress/runtime";
export default new Conversation({
channel: "*",
handler: async ({ execute, message }) => {
await execute({
instructions: "You are a helpful assistant.",
});
},
});
Channel matching
Thechannel property determines which integration channels the Conversation handles:
Report incorrect code
Copy
export default new Conversation({
channel: "*",
handler: async ({ execute }) => {
await execute({
instructions: "You are a helpful assistant.",
});
},
});
Conversation handler
The handler function receives context about the incoming message and provides APIs to execute your agent’s logic:Report incorrect code
Copy
export default new Conversation({
channel: "*",
handler: async ({ execute, message }) => {
const userMessage = message.text;
await execute({
instructions: `You are a helpful assistant. The user said: ${userMessage}`,
});
},
});
Using knowledge bases
Provide knowledge to your agent’s AI model:Report incorrect code
Copy
import { WebsiteKB } from "../knowledge/docs";
export default new Conversation({
channel: "*",
handler: async ({ execute }) => {
await execute({
instructions: "You are a helpful assistant.",
knowledge: [WebsiteKB],
});
},
});
Using hooks
Add hooks to customize behavior at different stages:Report incorrect code
Copy
export default new Conversation({
channel: "*",
handler: async ({ execute }) => {
await execute({
instructions: "You are a helpful assistant.",
hooks: {
onBeforeTool: async (props) => {
// Custom logic before tool execution
},
onTrace: (props) => {
// Log or monitor trace events
},
},
});
},
});
Multiple conversations
You can create multiple conversation handlers for different channels or use cases:Report incorrect code
Copy
src/conversations/
├── webchat.ts # Webchat-specific handler
├── slack.ts # Slack-specific handler
└── support.ts # Support-focused handler
Reference
Conversation props
Show Conversation props
Show Conversation props
Channel specification. Can be a single channel name, an array of channel names, or ’*’ to match all channels.
Handler function that processes incoming messages and events. The props object structure is documented in the handler parameters section below.
Optional Zod schema defining the conversation state structure. Defaults to an empty object if not provided.
Optional function to determine if a conversation should start from a trigger event. Returns a conversation ID or false.
Handler parameters
The handler function receives different parameters depending on the incoming request type. The handler uses a discriminated union based on thetype field:
Message handler
Whentype is "message":
Show Message handler parameters
Show Message handler parameters
Indicates this is a message request.
Message object containing the user’s message data. Typed by
ConversationInstance at runtime based on the channel.Conversation instance providing methods to interact with the conversation (send messages, manage state, etc.).
Mutable conversation state object. Changes to this object are automatically persisted. Typed based on your state schema.
Botpress client for making API calls (tables, events, etc.).
Function to execute autonomous AI logic. Used to run the agent with instructions, tools, knowledge bases, etc.
Show Execute props
Show Execute props
Instructions for the AI agent. Can be a string or a function that returns a string.
Optional array of tools the agent can use.
Optional array of objects the agent can interact with.
Optional record of exit handlers.
Optional abort signal to cancel execution.
Optional hooks for customizing behavior (
onBeforeTool, onAfterTool, onTrace, etc.).Optional temperature for the AI model. Defaults to 0.7.
Optional model name(s) to use. Can be a string, array of strings, or a function that returns either.
Optional array of knowledge bases to use.
Optional maximum number of iterations (loops). Defaults to 10.
Event handler
Whentype is "event":
Show Event handler parameters
Show Event handler parameters
Indicates this is an event request.
Event object containing event data from integrations or system events. Typed by
ConversationInstance at runtime based on the channel.Conversation instance providing methods to interact with the conversation (send messages, manage state, etc.).
Mutable conversation state object. Changes to this object are automatically persisted. Typed based on your state schema.
Botpress client for making API calls (tables, events, etc.).
Function to execute autonomous AI logic. Used to run the agent with instructions, tools, knowledge bases, etc.
Show Execute props
Show Execute props
Instructions for the AI agent. Can be a string or a function that returns a string.
Optional array of tools the agent can use.
Optional array of objects the agent can interact with.
Optional record of exit handlers.
Optional abort signal to cancel execution.
Optional hooks for customizing behavior (
onBeforeTool, onAfterTool, onTrace, etc.).Optional temperature for the AI model. Defaults to 0.7.
Optional model name(s) to use. Can be a string, array of strings, or a function that returns either.
Optional array of knowledge bases to use.
Optional maximum number of iterations (loops). Defaults to 10.
Workflow request handler
Whentype is "workflow_request":
Show Workflow request handler parameters
Show Workflow request handler parameters
Indicates this is a workflow data request.
Conversation instance providing methods to interact with the conversation (send messages, manage state, etc.).
Mutable conversation state object. Changes to this object are automatically persisted. Typed based on your state schema.
Botpress client for making API calls (tables, events, etc.).
Function to execute autonomous AI logic. Used to run the agent with instructions, tools, knowledge bases, etc.
Show Execute props
Show Execute props
Instructions for the AI agent. Can be a string or a function that returns a string.
Optional array of tools the agent can use.
Optional array of objects the agent can interact with.
Optional record of exit handlers.
Optional abort signal to cancel execution.
Optional hooks for customizing behavior (
onBeforeTool, onAfterTool, onTrace, etc.).Optional temperature for the AI model. Defaults to 0.7.
Optional model name(s) to use. Can be a string, array of strings, or a function that returns either.
Optional array of knowledge bases to use.
Optional maximum number of iterations (loops). Defaults to 10.
Each conversation file should export a default
Conversation instance. The ADK automatically discovers and registers all conversations in the src/conversations/ directory.