Reference
Messages

Messages

The MessagesAPI allows you to handle messaging-related operations on the messages instance proteus.messages.

Sending Messages

To send a message, use the send method on the messages instance proteus.messages:

const messagePayload = {
  conversationId: "conversation123",
  content: "Hello, ProteusAI!",
  type: "TEXT",
}
 
proteus.messages.send(messagePayload);

The send method is also available in the Conversation object you get from proteus.conversations. In this case there is no need to include a conversationId in the payload of the send method.

const conversation = await proteus.conversations.getById('conversation123');
conversation.send({
  content: "Hello, ProteusAI!",
  type: "TEXT",
})

The response from the send method is not the AI character's reply to the message sent. The response is the same message sent, but with a few more fields, including the ProteusAI id of the message.

const messagePayload = {
  conversationId: "conversation123",
  content: "Hello, ProteusAI!",
  type: "TEXT",
}
 
const messageResponse =  await proteus.messages.send(messagePayload);
console.log(`ProteusAI ID for this message is ${messageResponse.id}`);
console.log(`${messageResponse.conversationId} === ${messagePayload.conversationId}`);
console.log(`${messageResponse.content} === ${messagePayload.content}`);

Listening for Messages

To get AI responses to your messages you have to listen for the CHARACTER_MESSAGE_SENT events.

proteus.messages.on('CHARACTER_MESSAGE_SENT', (message: any) => {
  console.log(`The message with ID ${message.id} was sent by an AI character`);
});
 
const messagePayload = {
  conversationId: "conversation123",
  content: "Hello, ProteusAI!",
  type: "TEXT",
}
proteus.messages.send(messagePayload);

To get all messages sent, listen for the MESSAGE_SENT event.

proteus.messages.on('MESSAGE_SENT', (message: any) => {
  console.log(`The message with ID ${message.id} is either the original message sent or a response to the original message`);
});
 
const messagePayload = {
  conversationId: "conversation123",
  content: "Hello, ProteusAI!",
  type: "TEXT",
}
proteus.messages.send(messagePayload);

You can listen for messages sent to a specific conversation by using the Conversation object.

const conversation = await proteus.conversations.getById('conversation123');
 
conversation.on('CHARACTER_MESSAGE_SENT', (message: any) => {
  console.log(`The message with ID ${message.id} was sent by an AI character`);
  console.log(`${message.conversationId} === ${conversation.id} === conversation123`);
});
 
conversation.send({
  content: "Hello, ProteusAI!",
  type: "TEXT",
})

To get all messages sent to a specific conversation, use the Conversation object to listen for the MESSAGE_SENT event.

const conversation = await proteus.conversations.getById('conversation123');
 
conversation.on('MESSAGE_SENT', (message: any) => {
  console.log(`The message with ID ${message.id} was sent in this conversation`);
  console.log(`${message.conversationId} === ${conversation.id} === conversation123`);
});
 
conversation.send({
  content: "Hello, ProteusAI!",
  type: "TEXT",
})

NOTE: Ensure that the connection to ProteusAI is fully established before attempting to send a message. To guarantee this, you can call the send method inside the proteus.connected callback:

const messagePayload = {
  conversationId: "conversation123",
  type: "TEXT",
  content: "Hello, ProteusAI!"
}
 
// Send is called only when a connection is fully established
proteus.connected(() => {
  proteus.messages.send(messagePayload);
})

NOTE: You can only get or send messages to conversations that has been created within the SDK.

Message Streams

Messages from AI characters that are of type TEXT get streamed from the API to clients. This means we do not need to wait for the character to construct the entire message before sending it to clients; we continuously stream the message in packets until the entire message has been constructed. This improves user experience as users no longer have to wait a long time just to be hit by the entire message at once.

To know if a message is still being streamed, see if the isStreaming field is true.

To see the content generated in the current packet, see the contentDelta field.

import ProteusAI from '@proteus-ai/sdk';
const proteus = new ProteusAI({ apiKey: '66563d92e59961d11fc67a39' });
const conversation = await proteus.conversations.create({
    characterId: '659a21168ad10b77542a3587'
});
conversation.on('CHARACTER_MESSAGE_SENT', (message) => {
  if (message.isStreaming) {
    console.log('This message is still being streamed');
    console.log(`This portion of the message generated in the current packet is: ${message.contentDelta}`);
    console.log(`This message generated thus far is: ${message.content}`);
  } else {
    console.log('This message has now been fully constructed');
    console.log(`This complete message is: ${message.content}`);
  }
});

To get an AI character's response without streaming, set 'streamReply' to false in the request payload

const messagePayload = {
  conversationId: "conversation123",
  content: "Hello, ProteusAI!",
  type: "TEXT",
}
 
// To send a message without streaming set `streamReply` to false in the payload
proteus.messages.send({
  ...messagePayload,
  streamReply: false
});

© 2024 ProteusAI. All rights reserved