メインコンテンツまでスキップ

Chat

The custom-chat view type renders a chat screen built from MonoChat's own pieces: a header, an optional context area, a scrolling message list and a composer fixed at the bottom. It fills the drawer or page it opens in. A typical use is an AI assistant that opens from the inbox, reads the conversation and drafts a reply for the agent.

The same pieces are also items you can use in any other view (a form, a tab pane, a workspace): chat-bubble, chat-message-list and chat-composer.


Where a chat shows​

A custom-chat view works at every customViewLocation: navbar (a screen of its own), settings, report-menu, dashboard-widget, session-widget, or session-footer-action and message-action, which open it in a drawer. For those two, customViewDrawerSize in the @monochat-sync block picks the drawer size: lg (the default), xl or full.

/* @monochat-sync
{
"type": "custom-view",
"name": "assistant.js",
"title": "Assistant",
"sdkVersion": 1,
"customViewType": "custom-chat",
"customViewLocation": "session-footer-action",
"customViewIconName": "sparkle",
"customViewDrawerSize": "xl"
}
*/

Example: an AI assistant in the inbox​

The view reads the open conversation, sends the agent's question to one of the workspace's AI models and shows the answer as an ai bubble with Put in reply and Send actions.

CustomFunction(async function ({ sdk }, reject, resolve) {
resolve(async function CustomFunction({ sdk }) {
const session = sdk.templateInstance.data._payload.session
const aiModelClientId = 'YOUR_AI_MODEL_ID'
const bubbles = new ReactiveVar([])
const isLoading = new ReactiveVar(false)

const conversation = await sdk.method({ name: 'business.tenants.chat.sessions.messages.listBySessionId' }).callAsync({
payload: { sessionId: session._id, options: { pagination: { currentPage: 1, pageItems: 50 }, sorting: { 'payload.createdAt': -1 } } },
})

const transcript = (conversation?.messages || [])
.filter((message) => message.type === 'text')
.reverse()
.map((message) => `${message.direction === 'incoming' ? 'Customer' : 'Agent'}: ${message.payload.text.text}`)
.join('\n')

const replyActions = function (message) {
return [
{
type: 'atoms-button',
text: 'Put in reply',
iconName: 'arrow-bend-up-left',
size: 'sm',
variant: 'primary-ghost',
events: { onClick: () => sdk.chat.composer.put(message) },
},
{
type: 'atoms-button',
text: 'Send',
iconName: 'paper-plane-tilt',
size: 'sm',
variant: 'primary-ghost',
isHidden: !session.connector().messagesRules[message.type]?.enabled,
events: {
onClick: () => sdk.method({ name: 'business.tenants.chat.sessions.messages.create' }).callAsync({ payload: { sessionId: session._id, type: message.type, isWhisper: false, payload: message.payload } }),
},
},
]
}

const ask = async function (question) {
bubbles.set([...bubbles.get(), { type: 'chat-bubble', direction: 'outgoing', message: { type: 'text', payload: { text: { text: question } } }, time: new Date() }])
isLoading.set(true)

const response = await sdk.method({ name: 'business.tenants.automation.ai.inferences.chatWithCustomMessages' }).callAsync({
payload: {
aiModelClientId,
systemPrompt: `You help an agent answer a customer. The conversation so far:\n${transcript}`,
aiMessages: [{ role: 'user', content: question }],
},
})

isLoading.set(false)

if (!response) {
return
}

const message = { type: 'text', payload: { text: { text: response.result } } }
bubbles.set([...bubbles.get(), { type: 'chat-bubble', variant: 'ai', isMarkdown: true, author: { name: 'Assistant', iconName: 'sparkle' }, message, time: new Date(), actions: replyActions(message) }])
}

sdk.options = function () {
return {
header: { title: 'Assistant', iconName: 'sparkle' },
messages: isLoading.get() ? [...bubbles.get(), { type: 'chat-bubble', variant: 'ai', isTyping: true }] : bubbles.get(),
notFound: { iconName: 'sparkle', title: 'Ask about this conversation' },
suggestions: [{ text: 'What does the customer want?', isSend: true }],
isLoading: isLoading.get(),
composer: { events: { onSend: (text) => ask(text) } },
}
}
})
})

response.result is the model's answer. Which AI model an app calls, and how it tells a user who lacks the permission to call it, is the app's own choice.


custom-chat options​

KeyDescription
header{ title, text, iconName, iconColor, titleSize, badgeText, badgeVariant, actions }; actions is an array of items.
contextAn array of items shown between the header and the list, such as an atoms-alert or the customer's name.
messagesAn array of items for the list. An item without type is a chat-bubble. Read it from a ReactiveVar so the list updates.
notFound{ iconName, title, text }, shown while messages is empty.
events{ onLoadMore(event, templateInstance) }, called when the user scrolls to the top of the list.
composerThe chat-composer keys below, without type.
suggestionsThe composer's suggestion chips (see below).
isLoadingShows the composer's loading state while the app waits; the send button is disabled.

chat-bubble​

A bubble renders a message through the same templates the inbox uses, so every MonoChat message type looks the way it does in a conversation. The inbox's message menu, read status and reactions are left out; the bubble shows its own time, status and actions instead.

KeyDescription
message{ type, payload } in the shape of a MonoChat message: { type: 'text', payload: { text: { text } } }. A message read with listBySessionId can be passed as it is.
sessionOptional. With a session (for example sdk.env.session), a type is shown the way it looks on that session's channel.
directionincoming (the default) or outgoing.
variantdefault, ai, note or error.
author{ name, avatarSrc, iconName }, shown above the bubble.
timeA Date, shown under the bubble. The message list also uses it for date separators.
statussending, sent or failed.
actionsAn array of items, usually atoms-button, shown under the bubble.
isTypingShows a typing indicator instead of a message.
isMarkdownFor a text message: renders the text as sanitized markdown. Use it for AI replies.
classNameA class on the bubble.

Media URLs are read signed, as in the inbox. A type the inbox cannot show appears as it does there, as not supported.

Sample messages​

{ type: 'text', payload: { text: { text: 'Hello!' } } }
{ type: 'image', payload: { image: { fileUrl, caption: 'Our new menu' } } }
{ type: 'video', payload: { video: { fileUrl, caption } } }
{ type: 'document', payload: { document: { fileUrl, fileName: 'invoice.pdf', caption } } }
{ type: 'audio', payload: { audio: { fileUrl } } }
{ type: 'location', payload: { location: { latitude: 41.04, longitude: 29.0, address: 'Office' } } }

Every other type takes its payload under its own key, the same way: the payload of a message of that type in a conversation.


chat-message-list​

KeyDescription
itemsThe items to show; an item without type is a chat-bubble.
notFound{ iconName, title, text } for an empty list.
events{ onLoadMore(event, templateInstance) }, called at the top of the list.
classNameA class on the list.

The list stays at the bottom as messages arrive, unless the user has scrolled up; then a scroll-down button shows. It fills the height of its container, so give it one (className) when you use it in a form.


chat-composer​

KeyDescription
events{ onSend(text, templateInstance) }. Enter sends, Shift+Enter starts a new line. The composer clears itself after sending.
valueA ReactiveVar holding the text; set it to fill or clear the composer.
placeholderThe placeholder text.
maxlengthThe longest text, with a counter.
isDisabledDisables the composer.
isLoadingShows the loading state; the send button is disabled.
suggestionsChips above the input: [{ text, prompt, iconName, isSend }]. A click fills the input with prompt (or text), or sends it when isSend is true.
leftActions, rightActionsItems beside the input.
name, classNameThe textarea's name and a class on the composer.

The composer is the app's own input. It does not touch the inbox's reply box.


Putting a draft in the inbox reply box​

sdk.chat.composer.put(message, { includeMedia, mode }) hands a draft to the open session for the agent to check and send. Nothing is sent. See Utils.

Sending a message​

Send any type with business.tenants.chat.sessions.messages.create, using the bubble's type and payload. Offer Send only when the session's channel supports the type: sdk.env.session.connector().messagesRules[type]?.enabled.