The helpers module provides utility functions for working with messages, including ID generation, preview derivation, and message creation.
generateMessageId
Generates a unique message identifier with an optional prefix.
function generateMessageId(prefix = "msg"): string
Optional prefix for the generated ID
Returns: A unique string ID in the format {prefix}-{uuid} or {prefix}-{random} if crypto is unavailable
import { generateMessageId } from "@/lib/chat/helpers"
const id = generateMessageId()
// Returns: "msg-a3c5e8f2-1b4d-4e6f-9a8c-2d3e4f5a6b7c"
const customId = generateMessageId("custom")
// Returns: "custom-b4d6f9a3-2c5e-5f7a-0b9d-3e4f5a6b7c8d"
The function uses crypto.randomUUID() when available, falling back to Math.random() in environments without crypto support.
derivePreview
Derives a human-readable preview string from a message object.
function derivePreview(message: Message): string
The message object to derive a preview from
Returns: A preview string appropriate for the message content type
import { derivePreview } from "@/lib/chat/helpers"
const textMessage = {
contentType: "text",
text: "Hello world",
// ... other fields
}
derivePreview(textMessage)
// Returns: "Hello world"
const imageMessage = {
contentType: "image",
media: { caption: "Beach sunset" },
// ... other fields
}
derivePreview(imageMessage)
// Returns: "Beach sunset"
const imageWithoutCaption = {
contentType: "image",
media: {},
// ... other fields
}
derivePreview(imageWithoutCaption)
// Returns: "📷 Photo"
const audioMessage = {
contentType: "audio",
// ... other fields
}
derivePreview(audioMessage)
// Returns: "🎧 Audio message"
Preview rules:
- Image messages: Returns the media caption or
"📷 Photo" if no caption
- Audio messages: Returns
"🎧 Audio message"
- Text messages: Returns the message text or
"New message" if empty
createInboundTextMessage
Creates a complete inbound text message object with all required fields.
function createInboundTextMessage(options: {
chatId: string
authorId: string
text: string
createdAt?: Date
}): Message
Configuration object for the messageThe chat ID this message belongs to
The ID of the message author
Optional creation timestamp (defaults to current time)
Returns: A complete Message object with status set to "read"
import { createInboundTextMessage } from "@/lib/chat/helpers"
const message = createInboundTextMessage({
chatId: "chat-carlos",
authorId: "carlos",
text: "Hey, how are you?",
})
// Returns:
// {
// id: "msg-a3c5e8f2-1b4d-4e6f-9a8c-2d3e4f5a6b7c",
// chatId: "chat-carlos",
// authorId: "carlos",
// contentType: "text",
// text: "Hey, how are you?",
// status: "read",
// createdAt: "2024-05-18T16:42:00.000Z",
// updatedAt: "2024-05-18T16:42:00.000Z"
// }
const customTimestamp = createInboundTextMessage({
chatId: "chat-sofia",
authorId: "sofia",
text: "Meeting at 3pm",
createdAt: new Date("2024-05-18T15:00:00Z"),
})
All inbound messages are automatically marked with status "read" and both createdAt and updatedAt are set to the same timestamp.
Creates a complete inbound media message object with attachment data.
function createInboundMediaMessage(options: {
chatId: string
authorId: string
media: MediaAttachment
createdAt?: Date
}): Message
Configuration object for the media messageThe chat ID this message belongs to
The ID of the message author
The media attachment object containing type, url, dimensions, etc.
Optional creation timestamp (defaults to current time)
Returns: A complete Message object with contentType matching the media type and status set to "read"
import { createInboundMediaMessage } from "@/lib/chat/helpers"
const mediaMessage = createInboundMediaMessage({
chatId: "chat-li",
authorId: "li",
media: {
id: "media-123",
type: "image",
url: "https://example.com/photo.jpg",
thumbnailUrl: "https://example.com/photo-thumb.jpg",
width: 1920,
height: 1080,
caption: "Sunset in Kyoto",
},
})
// Returns:
// {
// id: "msg-b4d6f9a3-2c5e-5f7a-0b9d-3e4f5a6b7c8d",
// chatId: "chat-li",
// authorId: "li",
// contentType: "image",
// media: {
// id: "media-123",
// type: "image",
// url: "https://example.com/photo.jpg",
// thumbnailUrl: "https://example.com/photo-thumb.jpg",
// width: 1920,
// height: 1080,
// caption: "Sunset in Kyoto"
// },
// status: "read",
// createdAt: "2024-05-18T16:42:00.000Z",
// updatedAt: "2024-05-18T16:42:00.000Z"
// }
The contentType is automatically derived from the media.type field. The media attachment must include all required fields defined in the MediaAttachment type.