Hello, how are you?
用法
ChatMessages 组件通过默认插槽或 messages
属性来显示一系列 ChatMessage 组件。
<template>
<UChatMessages>
<UChatMessage
v-for="(message, index) in messages"
:key="index"
v-bind="message"
/>
</UChatMessages>
</template>
此组件专为 AI 聊天机器人设计,具有以下功能:
- 加载时自动滚动到底部(
shouldScrollToBottom
). - 新消息到达时持续向下滚动(
shouldAutoScroll
). - 当用户向上滚动时,会出现一个“自动滚动”按钮,允许用户跳回最新消息(
autoScroll
). - 助手处理中时会显示加载指示器(
status
). - 已提交的消息将滚动到视口的顶部,并且最后一条用户消息的高度会动态调整。
消息
使用 messages
属性显示聊天消息列表。
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
},
{
id: '7a92b3c1-d5f8-4e76-b8a9-3c1e5fb2e0d8',
role: 'assistant',
content: 'I am doing well, thank you for asking! How can I assist you today?'
},
{
id: '9c84d6a7-8b23-4f12-a1d5-e7f3b9c05e2a',
role: 'user',
content: 'What is the current weather in Tokyo?'
},
{
id: 'b2e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
"Based on the latest data, Tokyo is currently experiencing sunny weather with temperatures around 24°C (75°F). It's a beautiful day with clear skies."
}
])
</script>
<template>
<UChatMessages :messages="messages" />
</template>
状态
使用 status
属性可在助手处理时显示视觉指示器。
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
}
])
</script>
<template>
<UChatMessages status="submitted" :messages="messages" />
</template>
以下是
useChat
组合式函数发送的不同状态的详细信息:submitted
: 消息已发送到 API,正在等待响应流的开始。streaming
: 响应正从 API 积极传输,接收数据块。ready
: 已接收并处理完整响应;可以提交新的用户消息。error
: API 请求期间发生错误,导致无法成功完成。
用户
使用 user
属性更改用户消息的 ChatMessage 属性。默认为:
side: 'right'
variant: 'soft'
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
},
{
id: '7a92b3c1-d5f8-4e76-b8a9-3c1e5fb2e0d8',
role: 'assistant',
content: 'I am doing well, thank you for asking! How can I assist you today?'
},
{
id: '9c84d6a7-8b23-4f12-a1d5-e7f3b9c05e2a',
role: 'user',
content: 'What is the current weather in Tokyo?'
},
{
id: 'b2e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
"Based on the latest data, Tokyo is currently experiencing sunny weather with temperatures around 24°C (75°F). It's a beautiful day with clear skies."
}
])
</script>
<template>
<UChatMessages
:user="{
side: 'left',
variant: 'solid',
avatar: {
src: 'https://github.com/benjamincanac.png'
}
}"
:messages="messages"
/>
</template>
助手
使用 assistant
属性更改助手消息的 ChatMessage 属性。默认为:
side: 'left'
variant: 'naked'
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
},
{
id: '7a92b3c1-d5f8-4e76-b8a9-3c1e5fb2e0d8',
role: 'assistant',
content: 'I am doing well, thank you for asking! How can I assist you today?'
},
{
id: '9c84d6a7-8b23-4f12-a1d5-e7f3b9c05e2a',
role: 'user',
content: 'What is the current weather in Tokyo?'
},
{
id: 'b2e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
"Based on the latest data, Tokyo is currently experiencing sunny weather with temperatures around 24°C (75°F). It's a beautiful day with clear skies."
}
])
</script>
<template>
<UChatMessages
:assistant="{
side: 'left',
variant: 'outline',
avatar: {
icon: 'i-lucide-bot'
},
actions: [
{
label: 'Copy to clipboard',
icon: 'i-lucide-copy'
}
]
}"
:messages="messages"
/>
</template>
自动滚动
使用 auto-scroll
属性可自定义或隐藏在聊天顶部滚动时显示的自动滚动按钮(使用 false
值可隐藏)。默认为:
color: 'neutral'
variant: 'outline'
您可以传入 Button 组件的任何属性来自定义它。
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
},
{
id: '7a92b3c1-d5f8-4e76-b8a9-3c1e5fb2e0d8',
role: 'assistant',
content: 'I am doing well, thank you for asking! How can I assist you today?'
},
{
id: '9c84d6a7-8b23-4f12-a1d5-e7f3b9c05e2a',
role: 'user',
content: 'What is the current weather in Tokyo?'
},
{
id: 'b2e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
"Based on the latest data, Tokyo is currently experiencing sunny weather with temperatures around 24°C (75°F). It's a beautiful day with clear skies. The forecast for the rest of the week shows a slight chance of rain on Thursday, with temperatures gradually rising to 28°C by the weekend. Humidity levels are moderate at around 65%, and wind speeds are light at 8 km/h from the southeast. Air quality is good with an index of 42. The UV index is high at 7, so it's recommended to wear sunscreen if you're planning to spend time outdoors. Sunrise was at 5:24 AM and sunset will be at 6:48 PM, giving Tokyo approximately 13 hours and 24 minutes of daylight today. The moon is currently in its waxing gibbous phase."
},
{
id: 'c3e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'user',
content: 'Can you recommend some popular tourist attractions in Kyoto?'
},
{
id: 'd4f5g8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
'Kyoto is known for its beautiful temples, traditional tea houses, and gardens. Some popular attractions include Kinkaku-ji (Golden Pavilion) with its stunning gold leaf exterior reflecting in the mirror pond, Fushimi Inari Shrine with its thousands of vermilion torii gates winding up the mountainside, Arashiyama Bamboo Grove where towering stalks create an otherworldly atmosphere, Kiyomizu-dera Temple perched on a hillside offering panoramic views of the city, and the historic Gion district where you might spot geisha hurrying to evening appointments through narrow stone-paved streets lined with traditional wooden machiya houses.'
}
])
</script>
<template>
<UChatMessages
:auto-scroll="{
color: 'neutral',
variant: 'outline'
}"
:should-scroll-to-bottom="false"
:messages="messages"
/>
</template>
自动滚动图标
使用 auto-scroll-icon
属性可自定义自动滚动按钮的 Icon。默认为 i-lucide-arrow-down
。
<script setup lang="ts">
const messages = ref([
{
id: '6045235a-a435-46b8-989d-2df38ca2eb47',
role: 'user',
content: 'Hello, how are you?'
},
{
id: '7a92b3c1-d5f8-4e76-b8a9-3c1e5fb2e0d8',
role: 'assistant',
content: 'I am doing well, thank you for asking! How can I assist you today?'
},
{
id: '9c84d6a7-8b23-4f12-a1d5-e7f3b9c05e2a',
role: 'user',
content: 'What is the current weather in Tokyo?'
},
{
id: 'b2e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
"Based on the latest data, Tokyo is currently experiencing sunny weather with temperatures around 24°C (75°F). It's a beautiful day with clear skies. The forecast for the rest of the week shows a slight chance of rain on Thursday, with temperatures gradually rising to 28°C by the weekend. Humidity levels are moderate at around 65%, and wind speeds are light at 8 km/h from the southeast. Air quality is good with an index of 42. The UV index is high at 7, so it's recommended to wear sunscreen if you're planning to spend time outdoors. Sunrise was at 5:24 AM and sunset will be at 6:48 PM, giving Tokyo approximately 13 hours and 24 minutes of daylight today. The moon is currently in its waxing gibbous phase."
},
{
id: 'c3e5f8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'user',
content: 'Can you recommend some popular tourist attractions in Kyoto?'
},
{
id: 'd4f5g8c3-a1d9-4e67-b3f2-c9d8e7a6b5f4',
role: 'assistant',
content:
'Kyoto is known for its beautiful temples, traditional tea houses, and gardens. Some popular attractions include Kinkaku-ji (Golden Pavilion) with its stunning gold leaf exterior reflecting in the mirror pond, Fushimi Inari Shrine with its thousands of vermilion torii gates winding up the mountainside, Arashiyama Bamboo Grove where towering stalks create an otherworldly atmosphere, Kiyomizu-dera Temple perched on a hillside offering panoramic views of the city, and the historic Gion district where you might spot geisha hurrying to evening appointments through narrow stone-paved streets lined with traditional wooden machiya houses.'
}
])
</script>
<template>
<UChatMessages
auto-scroll-icon="i-lucide-chevron-down"
:should-scroll-to-bottom="false"
:messages="messages"
/>
</template>
是否自动滚动
使用 should-auto-scroll
属性可启用/禁用消息流式传输时的持续自动滚动。默认为 false
。
<template>
<UChatMessages :messages="messages" should-auto-scroll />
</template>
是否滚动到底部
使用 should-scroll-to-bottom
属性可启用/禁用组件挂载时的底部自动滚动。默认为 true
。
<template>
<UChatMessages :messages="messages" :should-scroll-to-bottom="false" />
</template>
示例
在页面内
将 ChatMessages 组件与 useChat
组合式函数一起使用,以在页面中显示聊天消息列表。
传入 messages
属性以及用于自动滚动和指示器显示的 status
属性。
pages/[id].vue
<script setup lang="ts">
import { useChat } from '@ai-sdk/vue'
const { messages, input, handleSubmit, reload, stop, status, error } = useChat()
</script>
<template>
<UDashboardPanel>
<template #body>
<UContainer>
<UChatMessages :messages="messages" :status="status">
<template #content="{ message }">
<MDC :value="message.content" :cache-key="message.id" unwrap="p" />
</template>
</UChatMessages>
</UContainer>
</template>
<template #footer>
<UContainer>
<UChatPrompt v-model="input" :error="error" @submit="handleSubmit">
<UChatPromptSubmit :status="status" @stop="stop" @reload="reload" />
</UChatPrompt>
</UContainer>
</template>
</UDashboardPanel>
</template>
在此示例中,我们使用来自以下组件的 MDC 组件:
@nuxtjs/mdc
来渲染消息内容。由于 Nuxt UI Pro 提供了预设样式的散文(prose)组件,您的内容将自动获得样式。API
属性
属性 | 默认值 | 类型 |
---|---|---|
messages |
| |
status |
| |
shouldAutoScroll |
|
当消息流式传输时是否自动滚动到底部。 |
shouldScrollToBottom |
|
挂载时是否滚动到底部。 |
autoScroll |
|
显示一个自动滚动按钮。 |
autoScrollIcon |
|
自动滚动按钮中显示的图标。 |
user |
用户消息属性。 | |
assistant |
助手消息属性。 | |
紧凑 |
|
以紧凑样式渲染消息。在 |
spacingOffset |
|
最后一条消息的像素间距偏移。例如,当提示框是固定(sticky)时会很有用。 |
ui |
|
插槽
插槽 | 类型 |
---|---|
默认 |
|
指示器 |
|
视口 |
|
内容 |
|
前置 |
|
操作 |
|
您可以在 ChatMessages 内部使用
ChatMessage
组件的所有插槽,它们会自动转发,允许您在使用 messages
属性时自定义单个消息。<template>
<UChatMessages :messages="messages" :status="status">
<template #content="{ message }">
<MDC :value="message.content" :cache-key="message.id" unwrap="p" />
</template>
</UChatMessages>
</template>
主题
app.config.ts
export default defineAppConfig({
uiPro: {
chatMessages: {
slots: {
root: 'w-full flex flex-col gap-1 flex-1 px-2.5 [&>article]:last-of-type:min-h-(--last-message-height)',
indicator: 'h-6 flex items-center gap-1 py-3 *:size-2 *:rounded-full *:bg-elevated [&>*:nth-child(1)]:animate-[bounce_1s_infinite] [&>*:nth-child(2)]:animate-[bounce_1s_0.15s_infinite] [&>*:nth-child(3)]:animate-[bounce_1s_0.3s_infinite]',
viewport: 'absolute inset-x-0 top-[86%] data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]',
autoScroll: 'rounded-full absolute right-1/2 translate-x-1/2 bottom-0'
},
variants: {
compact: {
true: '',
false: ''
}
}
}
}
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
uiPro: {
chatMessages: {
slots: {
root: 'w-full flex flex-col gap-1 flex-1 px-2.5 [&>article]:last-of-type:min-h-(--last-message-height)',
indicator: 'h-6 flex items-center gap-1 py-3 *:size-2 *:rounded-full *:bg-elevated [&>*:nth-child(1)]:animate-[bounce_1s_infinite] [&>*:nth-child(2)]:animate-[bounce_1s_0.15s_infinite] [&>*:nth-child(3)]:animate-[bounce_1s_0.3s_infinite]',
viewport: 'absolute inset-x-0 top-[86%] data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]',
autoScroll: 'rounded-full absolute right-1/2 translate-x-1/2 bottom-0'
},
variants: {
compact: {
true: '',
false: ''
}
}
}
}
})
]
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
uiPro: {
chatMessages: {
slots: {
root: 'w-full flex flex-col gap-1 flex-1 px-2.5 [&>article]:last-of-type:min-h-(--last-message-height)',
indicator: 'h-6 flex items-center gap-1 py-3 *:size-2 *:rounded-full *:bg-elevated [&>*:nth-child(1)]:animate-[bounce_1s_infinite] [&>*:nth-child(2)]:animate-[bounce_1s_0.15s_infinite] [&>*:nth-child(3)]:animate-[bounce_1s_0.3s_infinite]',
viewport: 'absolute inset-x-0 top-[86%] data-[state=open]:animate-[fade-in_200ms_ease-out] data-[state=closed]:animate-[fade-out_200ms_ease-in]',
autoScroll: 'rounded-full absolute right-1/2 translate-x-1/2 bottom-0'
},
variants: {
compact: {
true: '',
false: ''
}
}
}
}
})
]
})