채팅 메시지
채팅 메시지 (ChatMessage)
아이콘, 아바타, 동작(actions)을 가진 채팅 메시지를 표시하는 컴포넌트예요. AI 채팅봇을 만들 때 사용자/어시스턴트 메시지를 렌더링합니다.
출처: Nuxt UI
본문
ChatMessage 컴포넌트는 user 또는 assistant 채팅 메시지를 위한 <article> 요소를 렌더링해요.
<template>
<u-chat-message :avatar="{
src: 'https://github.com/benjamincanac.png',
loading: 'lazy'
}" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" id="1" role="user" side="right" variant="soft" />
</template>
[!TIP] See: /docs/components/chat-messages
채팅 메시지 목록을 표시하려면
ChatMessages컴포넌트를 사용해 주세요.
파트 (Parts)
parts prop을 사용해 AI SDK 형식으로 메시지 콘텐츠를 표시해요.
<template>
<UChatMessage :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
[!NOTE]
partsprop은 AI SDK에 권장되는 형식이에요. 각 파트는type(예: 'text')과 그에 해당하는 콘텐츠를 가집니다. ChatMessage 컴포넌트는 하위 호환을 위해 더 이상 권장되지 않는contentprop도 지원해요.
측면 (Side)
side prop을 사용해 메시지를 왼쪽 또는 오른쪽에 표시해요.
<template>
<UChatMessage side="right" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
[!NOTE]
ChatMessages컴포넌트를 사용할 때sideprop은assistant메시지에서는left로,user메시지에서는right로 설정됩니다.
변형 (Variant)
variant prop을 사용해 메시지의 스타일을 바꿔요.
<template>
<UChatMessage variant="soft" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
[!NOTE]
ChatMessages컴포넌트를 사용할 때variantprop은assistant메시지에서는naked로,user메시지에서는soft로 설정됩니다.
색상 4.8+ (Color)
color prop을 사용해 메시지의 색상을 바꿔요.
<template>
<UChatMessage variant="soft" color="primary" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
아이콘 (Icon)
icon prop을 사용해 메시지 옆에 Icon 컴포넌트를 표시해요.
<template>
<UChatMessage icon="i-lucide-user" variant="soft" side="right" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
아바타 (Avatar)
avatar prop을 사용해 메시지 옆에 Avatar 컴포넌트를 표시해요.
<template>
<UChatMessage :avatar="{
src: 'https://github.com/benjamincanac.png',
loading: 'lazy'
}" variant="soft" side="right" :parts="[
{
type: 'text',
id: '1',
text: 'Hello! Tell me more about building AI chatbots with Nuxt UI.'
}
]" role="user" id="1" />
</template>
avatar.icon prop을 사용해 아바타로 아이콘을 표시할 수도 있어요.
<template>
<UChatMessage :avatar="{
icon: 'i-lucide-bot'
}" :parts="[
{
type: 'text',
id: '1',
text: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the Chat class from AI SDK, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
}
]" role="assistant" id="1" />
</template>
동작 (Actions)
actions prop을 사용해 메시지 아래에 동작을 표시할 수 있어요. 이 동작들은 메시지 위에 마우스를 올렸을 때 나타납니다.
<script setup lang="ts">
import type { ButtonProps } from '@nuxt/ui'
const actions = ref<ButtonProps[]>([
{
label: "Copy to clipboard",
icon: "i-lucide-copy"
}
])
</script>
<template>
<UChatMessage :actions="actions" :parts="[
{
type: 'text',
id: '1',
text: 'Nuxt UI offers several features for building AI chatbots including the ChatMessage, ChatMessages, and ChatPrompt components. Best practices include using the Chat class from AI SDK, implementing proper message styling with variants, and utilizing the built-in actions for message interactions. The components are fully customizable with theming support and responsive design.'
}
]" role="user" id="1" />
</template>
예시 (Examples)
[!TIP] See: /docs/components/chat
설치 방법, 서버 설정, 사용 예시는 Chat 개요 페이지에서 확인해 보세요.
API
Props
/**
* Props for the ChatMessage component
*/
interface ChatMessageProps {
/**
* A unique identifier for the message.
*/
id: string;
/**
* The role of the message.
*/
role: "system" | "user" | "assistant";
/**
* The parts of the message. Use this for rendering the message in the UI.
*
* System messages should be avoided (set the system prompt on the server instead).
* They can have text parts.
*
* User messages can have text parts and file parts.
*
* Assistant messages can have text, reasoning, tool invocation, and file parts.
*/
parts: UIMessagePart<TDataParts, TTools>[];
/**
* The element or component this component should render as.
* @default 'article'
*/
as?: any;
icon?: any;
avatar?: AvatarProps & { [key: string]: any; } | undefined;
/**
* @default 'naked'
*/
variant?: "solid" | "outline" | "soft" | "subtle" | "naked" | undefined;
/**
* @default 'neutral'
*/
color?: "primary" | "secondary" | "success" | "info" | "warning" | "error" | "neutral" | undefined;
/**
* @default 'left'
*/
side?: "left" | "right" | undefined;
/**
* Display a list of actions under the message.
* The `label` will be used in a tooltip.
* `{ size: 'xs', color: 'neutral', variant: 'ghost' }`{lang="ts-type"}
*/
actions?: (Omit<ButtonProps, "onClick"> & { onClick?: ((e: MouseEvent, message: UIMessage<TMetadata, TDataParts, TTools>) => void) | undefined; })[] | undefined;
/**
* Render the message in a compact style.
* This is done automatically when used inside a `UChatPalette`{lang="ts-type"}.
* @default false
*/
compact?: boolean | undefined;
content?: string | undefined;
ui?: { root?: SlotClass; header?: SlotClass; container?: SlotClass; body?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; leadingAvatar?: SlotClass; leadingAvatarSize?: SlotClass; files?: SlotClass; content?: SlotClass; actions?: SlotClass; } | undefined;
/**
* The metadata of the message.
*/
metadata?: TMetadata | undefined;
}
Slots
/**
* Slots for the ChatMessage component
*/
interface ChatMessageSlots {
header(): any;
leading(): any;
files(): any;
body(): any;
content(): any;
actions(): any;
}
테마 (Theme)
export default defineAppConfig({
ui: {
chatMessage: {
slots: {
root: 'group/message relative w-full',
header: 'flex mb-1.5',
container: 'relative flex items-start',
body: 'min-w-0',
leading: 'inline-flex items-center justify-center min-h-6',
leadingIcon: 'shrink-0',
leadingAvatar: 'shrink-0',
leadingAvatarSize: '',
files: 'flex items-center gap-1.5',
content: 'relative text-pretty wrap-break-word *:first:mt-0 *:last:mb-0',
actions: [
'[@media(hover:hover)]:opacity-0 group-hover/message:opacity-100 absolute bottom-0 flex items-center',
'transition-opacity ease-out'
]
},
variants: {
variant: {
solid: '',
outline: '',
soft: '',
subtle: '',
naked: ''
},
color: {
primary: '',
secondary: '',
success: '',
info: '',
warning: '',
error: '',
neutral: ''
},
side: {
left: {},
right: {
container: 'justify-end ms-auto max-w-[75%]',
header: 'justify-end',
actions: 'right-0'
}
},
leading: {
true: ''
},
actions: {
true: ''
},
compact: {
true: {
root: 'scroll-mt-3',
container: 'gap-1.5 pb-3',
content: 'space-y-2',
leadingIcon: 'size-5',
leadingAvatarSize: '2xs'
},
false: {
root: 'scroll-mt-4 sm:scroll-mt-6',
container: 'gap-3 pb-8',
content: 'space-y-4',
leadingIcon: 'size-8',
leadingAvatarSize: 'md'
}
}
},
compoundVariants: [
{
compact: true,
actions: true,
class: {
container: 'pb-8'
}
},
{
variant: [
'solid',
'outline',
'soft',
'subtle'
],
compact: false,
class: {
content: 'px-4 py-3 rounded-lg min-h-12',
leading: 'mt-2'
}
},
{
variant: [
'solid',
'outline',
'soft',
'subtle'
],
compact: true,
class: {
content: 'px-2 py-1 rounded-lg min-h-8',
leading: 'mt-1'
}
},
{
variant: 'naked',
side: 'left',
class: {
body: 'w-full',
content: 'w-full'
}
},
{
color: 'primary',
variant: 'solid',
class: {
content: 'bg-primary text-inverted'
}
},
{
color: 'secondary',
variant: 'solid',
class: {
content: 'bg-secondary text-inverted'
}
},
{
color: 'success',
variant: 'solid',
class: {
content: 'bg-success text-inverted'
}
},
{
color: 'info',
variant: 'solid',
class: {
content: 'bg-info text-inverted'
}
},
{
color: 'warning',
variant: 'solid',
class: {
content: 'bg-warning text-inverted'
}
},
{
color: 'error',
variant: 'solid',
class: {
content: 'bg-error text-inverted'
}
},
{
color: 'primary',
variant: 'outline',
class: {
content: 'text-primary ring ring-primary/25'
}
},
{
color: 'secondary',
variant: 'outline',
class: {
content: 'text-secondary ring ring-secondary/25'
}
},
{
color: 'success',
variant: 'outline',
class: {
content: 'text-success ring ring-success/25'
}
},
{
color: 'info',
variant: 'outline',
class: {
content: 'text-info ring ring-info/25'
}
},
{
color: 'warning',
variant: 'outline',
class: {
content: 'text-warning ring ring-warning/25'
}
},
{
color: 'error',
variant: 'outline',
class: {
content: 'text-error ring ring-error/25'
}
},
{
color: 'primary',
variant: 'soft',
class: {
content: 'bg-primary/10 text-primary'
}
},
{
color: 'secondary',
variant: 'soft',
class: {
content: 'bg-secondary/10 text-secondary'
}
},
{
color: 'success',
variant: 'soft',
class: {
content: 'bg-success/10 text-success'
}
},
{
color: 'info',
variant: 'soft',
class: {
content: 'bg-info/10 text-info'
}
},
{
color: 'warning',
variant: 'soft',
class: {
content: 'bg-warning/10 text-warning'
}
},
{
color: 'error',
variant: 'soft',
class: {
content: 'bg-error/10 text-error'
}
},
{
color: 'primary',
variant: 'subtle',
class: {
content: 'bg-primary/10 text-primary ring ring-primary/25'
}
},
{
color: 'secondary',
variant: 'subtle',
class: {
content: 'bg-secondary/10 text-secondary ring ring-secondary/25'
}
},
{
color: 'success',
variant: 'subtle',
class: {
content: 'bg-success/10 text-success ring ring-success/25'
}
},
{
color: 'info',
variant: 'subtle',
class: {
content: 'bg-info/10 text-info ring ring-info/25'
}
},
{
color: 'warning',
variant: 'subtle',
class: {
content: 'bg-warning/10 text-warning ring ring-warning/25'
}
},
{
color: 'error',
variant: 'subtle',
class: {
content: 'bg-error/10 text-error ring ring-error/25'
}
},
{
color: 'primary',
variant: 'naked',
class: {
content: 'text-primary'
}
},
{
color: 'secondary',
variant: 'naked',
class: {
content: 'text-secondary'
}
},
{
color: 'success',
variant: 'naked',
class: {
content: 'text-success'
}
},
{
color: 'info',
variant: 'naked',
class: {
content: 'text-info'
}
},
{
color: 'warning',
variant: 'naked',
class: {
content: 'text-warning'
}
},
{
color: 'error',
variant: 'naked',
class: {
content: 'text-error'
}
},
{
color: 'neutral',
variant: 'solid',
class: {
content: 'bg-inverted text-inverted'
}
},
{
color: 'neutral',
variant: 'outline',
class: {
content: 'bg-default ring ring-default'
}
},
{
color: 'neutral',
variant: 'soft',
class: {
content: 'bg-elevated/50'
}
},
{
color: 'neutral',
variant: 'subtle',
class: {
content: 'bg-elevated/50 ring ring-default'
}
}
],
defaultVariants: {
side: 'left',
variant: 'naked',
color: 'neutral'
}
}
}
})