Skip to main content

agents

type ResponseHandler = (data: Message[]) => Message | Promise<Message>;
type Agent = {
id: TID;
name: string;
avatar?: string;
response?: ResponseHandler;
}
type AgentsConfig = Agent[];

Controls the list of agents available in the chat. Default value is an empty array ([]).

Example of usage:

const agents: AgentsConfig = [
{ id: 1, name: "Support Bot", avatar: "https://some.com/i.png" }
];

const chat = new ChatBot(node, { agents });

Sub-properties in Agent object:

  • id: Unique identifier for the agent (mandatory).
  • name: Display name of the agent (mandatory).
  • avatar: URL to the agent's avatar image (optional). Default value is undefined.
  • response: A function that handles responses when the agent sees a new message (optional). Default value is undefined.

How to provide a list of agents for the chat

const agents: AgentsConfig = [
{ id: 1, name: "Support Bot", avatar: "https://some.com/i.png" }
];

chat.setConfig({ agents });

How to get the value of agents for the chat

const chat = new ChatBot(node, { agents });

const currentAgents = chat.getConfig().agents;

Using the Response Handler

Example of a response handler returning a direct message:

const agents: AgentsConfig = [
{
id: 1,
name: "Support Bot",
avatar: "https://some.com/i.png",
response: (data) => {
return { msg: { content: "Hello! How can I help you?" } };
}
}
];

const chat = new ChatBot(node, { agents });

Example of a response handler returning a promise:

const agents: AgentsConfig = [
{
id: 1,
name: "Support Bot",
avatar: "https://some.com/i.png",
response: async (data) => {
const response = await fetch("https://api.example.com/response");
const json = await response.json();
return { msg: { content: json.reply } };
}
}
];

const chat = new ChatBot(node, { agents });