ChatPrompt 组件渲染一个 <form> 元素,并扩展了 Textarea 组件,因此你可以传递任何属性,例如 icon、placeholder、autofocus 等。
close 事件。使用 variant 属性更改提示的样式。默认为 outline。
<template>
<UChatPrompt variant="soft" />
</template>
将 ChatPrompt 组件与 AI SDK v5 中的 Chat 类一起使用,以在页面中显示聊天提示。
当出现错误时,将 input 属性与 error 属性一起传递以禁用文本区域。
<script setup lang="ts">
import { Chat } from '@ai-sdk/vue'
import { getTextFromMessage } from '@nuxt/ui/utils/ai'
const input = ref('')
const chat = new Chat({
onError(error) {
console.error(error)
}
})
function onSubmit() {
chat.sendMessage({ text: input.value })
input.value = ''
}
</script>
<template>
<UDashboardPanel>
<template #body>
<UContainer>
<UChatMessages :messages="chat.messages" :status="chat.status">
<template #content="{ message }">
<MDC :value="getTextFromMessage(message)" :cache-key="message.id" class="*:first:mt-0 *:last:mb-0" />
</template>
</UChatMessages>
</UContainer>
</template>
<template #footer>
<UContainer class="pb-4 sm:pb-6">
<UChatPrompt v-model="input" :error="chat.error" @submit="onSubmit">
<UChatPromptSubmit :status="chat.status" @stop="chat.stop" @reload="chat.regenerate" />
</UChatPrompt>
</UContainer>
</template>
</UDashboardPanel>
</template>
你也可以将其用作聊天界面的起点。
<script setup lang="ts">
import { Chat } from '@ai-sdk/vue'
const input = ref('')
const chat = new Chat()
async function onSubmit() {
chat.sendMessage({ text: input.value })
// Navigate to chat page after first message
if (chat.messages.length === 1) {
await navigateTo('/chat')
}
}
</script>
<template>
<UDashboardPanel>
<template #body>
<UContainer>
<h1>How can I help you today?</h1>
<UChatPrompt v-model="input" @submit="onSubmit">
<UChatPromptSubmit :status="chat.status" />
</UChatPrompt>
</UContainer>
</template>
</UDashboardPanel>
</template>
| 属性 | 默认值 | 类型 |
|---|---|---|
as |
|
此组件应渲染为的元素或组件。 |
placeholder |
|
文本区域的占位符文本。 |
variant |
|
|
error |
| |
disabled |
| |
图标 |
根据 | |
avatar |
在左侧显示头像。
| |
loading |
当为 | |
loadingIcon |
|
当 |
rows |
|
|
autofocus |
|
|
autofocusDelay |
| |
autoresize |
|
|
autoresizeDelay |
| |
maxrows |
| |
modelValue |
|
|
ui |
|
| 插槽 | 类型 |
|---|---|
页头 |
|
页脚 |
|
前置 | |
default | |
尾部 |
|
| 事件 | 类型 |
|---|---|
close | |
submit |
|
update:modelValue |
|
export default defineAppConfig({
ui: {
chatPrompt: {
slots: {
root: 'relative flex flex-col items-stretch gap-2 px-2.5 py-2 w-full rounded-lg backdrop-blur',
header: 'flex items-center gap-1.5',
body: 'items-start',
footer: 'flex items-center justify-between gap-1.5',
base: 'text-base/5'
},
variants: {
variant: {
outline: {
root: 'bg-default/75 ring ring-default'
},
soft: {
root: 'bg-elevated/50'
},
subtle: {
root: 'bg-elevated/50 ring ring-default'
},
naked: {
root: ''
}
}
},
defaultVariants: {
variant: 'outline'
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
ui: {
chatPrompt: {
slots: {
root: 'relative flex flex-col items-stretch gap-2 px-2.5 py-2 w-full rounded-lg backdrop-blur',
header: 'flex items-center gap-1.5',
body: 'items-start',
footer: 'flex items-center justify-between gap-1.5',
base: 'text-base/5'
},
variants: {
variant: {
outline: {
root: 'bg-default/75 ring ring-default'
},
soft: {
root: 'bg-elevated/50'
},
subtle: {
root: 'bg-elevated/50 ring ring-default'
},
naked: {
root: ''
}
}
},
defaultVariants: {
variant: 'outline'
}
}
}
})
]
})