Skip to main content

addMessage

chat.addMessage({ 
id: TID,
message: {
id: "message-id-1",
role: "user",
content: "This is a new message",
typing: 0
}
});

The addMessage method adds a new message to the chat UI. This method takes an object with id and message properties. The message property itself is an object conforming to the Message type.

Message Type

type Message = {
id: TID;
role: "agent" | "user";
content: string;
typing?: number;
}

Properties:

  • id: Unique identifier for the message.
  • role: Role of the message sender. Can be either "agent" or "user".
  • content: The actual content of the message.
  • typing (optional): Specifies typing animation. Default value is 0.
    • 0: Without typing animation.
    • 1: With typing animation, streaming mode.
    • -1: With typing animation, finishing.

Use Cases

Adding a User Message Without Typing Animation

chat.addMessage({ 
id: "chat-1",
message: {
id: "message-id-1",
role: "user",
content: "This is a new message",
typing: 0
}
});

Adds a new user message to the chat without any typing animation.

Adding an Agent Message With Typing Animation (Streaming)

chat.addMessage({ 
id: "chat-2",
message: {
id: "message-id-2",
role: "agent",
content: "Fetching information...",
typing: 1
}
});

Adds a new agent message with typing animation in streaming mode.

Adding an Agent Message With Typing Animation (Finishing)

chat.addMessage({ 
id: "chat-3",
message: {
id: "message-id-3",
role: "agent",
content: "Here is the information you requested.",
typing: -1
}
});

Adds a new agent message indicating that the typing animation is finishing.