Message Type
The type of the content of a message or an attachment.
Options
AUDIO_URL
The content
of the message or attachment is the URL to an audio file.
CODE
The content
of the message is a piece of code to be executed.
This message type is currently not being used.
Attachments with this message type should be ignored.
ERROR_MESSAGE
The content
of the message or attachment is an error message.
Applications displaying this message type are encouraged to give some sort of error cue to the users.
FILE_URL
The content
of the message or attachment is the URL to a file that is not an audio file or an image file (or a video file, in the future).
FUNCTION_CALL
The content
of the message is a stringified JSON object that contains the name and arguments of a function to be executed.
Applications are encouraged not to display this message but to execute the named function in a safe manner. This is essentially a remote invocation, and should be treated with caution.
Attachments with this message type should be ignored.
One use case for this message type is to allow the ProteusAI chat widget communicate with the host web page.
For instance, we may want to allow a user to change the background color of a web page by typing “change the background color to red” into a ProteusAI chat widget hosted on that page.
This can be achieved by adding the following function to the AI character that powers the chat widget:
async function changeBackgroundColor() {
const character = ctx.character;
const funcCall = {
func: "changeBackgroundColor",
args: ctx.args.color,
};
const funcCallStr = JSON.stringify(funcCall);
if (ctx.conversation) {
await character.sendMessage(funcCallStr, "FUNCTION_CALL");
}
}
changeBackgroundColor().catch(console.log);
Remember to add a color
parameter to the function and set its trigger to ON_CHARACTER_CALL
.
Now, when the user types “change the background color to red” in the chat widget, the AI character will execute the function changeBackgroundColor
, with the value of ctx.args.color
being red
.
Eventually, the below message will be sent by the AI character to the chat widget:
{
content: '{ "func": "changeBackgroundColor", "args": "red" }',
type: 'FUNCTION_CALL'
}
The way the ProteusAI chat widget handles messages of type FUNCTION_CALL
is to make a call to the host page using:
window.parent.postMessage(JSON.parse(message.content), "*");
That is:
window.parent.postMessage({ func: "changeBackgroundColor", args: "red" }, "*");
The host web page may register a window event listener as shown below:
window.addEventListener('message', function (event) {
const key = event.message ? 'message' : 'data';
const data = event[key];
if (data.func === 'changeBackgroundColor') {
changeBackgroundColor(data.args || 'white');
}
}, false);
function changeBackgroundColor(color) {
document.body.style.backgroundColor = color;
}
Watch a quick demo of this capability.
Note that clients can also send FUNCTION_CALL
messages to AI characters.
The AI character must have a function with the same name as the value
of func
in the FUNCTION_CALL
message.
I_DO_NOT_KNOW
The message with this message type represents an acknowledgement from an AI character that it cannot process a request or answer a question.
The content
of the message may or may not contain more information.
Attachments with this message type should be ignored.
IMAGE_URL
The content
of the message or attachment is the URL to an image file.
TEXT
The content
of the message or attachment is a markdown (opens in a new tab) text.
UI_FORM
The content
of the message or attachment is a stringified JSON object that represents a UI form that users can fill and submit.
Forms are currently being reworked.