聊天提示提交

ButtonGitHub
一个用于提交聊天提示并自动处理状态的按钮。

用法

ChatPromptSubmit 组件用于 ChatPrompt 组件内部,用于提交提示词。它会自动处理不同的 status 值来控制聊天。

它继承自 Button 组件,因此你可以传递任何属性,例如 colorvariantsize 等。

<template>
  <UChatPrompt>
    <UChatPromptSubmit />
  </UChatPrompt>
</template>
你也可以在 ChatPrompt 组件的 footer 插槽中使用它。

就绪

当其状态为 ready 时,使用 colorvarianticon 属性来自定义按钮。默认为

  • color="primary"
  • variant="solid"
  • icon="i-lucide-arrow-up"
<template>
  <UChatPromptSubmit color="primary" variant="solid" icon="i-lucide-arrow-up" />
</template>
你可以在 app.config.ts 中的 ui.icons.arrowUp 键下全局自定义此图标。
你可以在 vite.config.ts 中的 ui.icons.arrowUp 键下全局自定义此图标。

已提交

当其状态为 submitted 时,使用 submitted-colorsubmitted-variantsubmitted-icon 属性来自定义按钮。默认为

  • submittedColor="neutral"
  • submittedVariant="subtle"
  • submittedIcon="i-lucide-square"
当用户点击按钮时,会触发 stop 事件。
<template>
  <UChatPromptSubmit
    submitted-color="neutral"
    submitted-variant="subtle"
    submitted-icon="i-lucide-square"
    status="submitted"
  />
</template>
你可以在 app.config.ts 中的 ui.icons.stop 键下全局自定义此图标。
你可以在 vite.config.ts 中的 ui.icons.stop 键下全局自定义此图标。

正在流式传输

当其状态为 streaming 时,使用 streaming-colorstreaming-variantstreaming-icon 属性来自定义按钮。默认为

  • streamingColor="neutral"
  • streamingVariant="subtle"
  • streamingIcon="i-lucide-square"
当用户点击按钮时,会触发 stop 事件。
<template>
  <UChatPromptSubmit
    streaming-color="neutral"
    streaming-variant="subtle"
    streaming-icon="i-lucide-square"
    status="streaming"
  />
</template>
你可以在 app.config.ts 中的 ui.icons.stop 键下全局自定义此图标。
你可以在 vite.config.ts 中的 ui.icons.stop 键下全局自定义此图标。

错误

当其状态为 error 时,使用 error-colorerror-varianterror-icon 属性来自定义按钮。默认为

  • errorColor="error"
  • errorVariant="soft"
  • errorIcon="i-lucide-rotate-ccw"
当用户点击按钮时,会触发 reload 事件。
<template>
  <UChatPromptSubmit
    error-color="error"
    error-variant="soft"
    error-icon="i-lucide-rotate-ccw"
    status="error"
  />
</template>
你可以在 app.config.ts 中的 ui.icons.reload 键下全局自定义此图标。
你可以在 vite.config.ts 中的 ui.icons.reload 键下全局自定义此图标。

示例

这些聊天组件旨在与 Vercel AI SDKAI SDK v5 配合使用。
请查看 GitHub 上的 AI 聊天模板源代码,了解实际示例。

在页面内

将 ChatPromptSubmit 组件与 AI SDK v5 的 Chat 类一起使用,以在页面中显示聊天提示。

传递 status 属性并监听 stopreload 事件来控制聊天。

pages/[id].vue
<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>

API

属性

属性默认值类型
as'button'any

此组件在不是链接时应呈现的元素或组件。

status'ready'"error" | "submitted" | "streaming" | "ready"
图标appConfig.ui.icons.arrowUpany

当状态为 ready 时,按钮中显示的图标。

color'primary'"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"

当状态为 ready 时,按钮的颜色。

variant'solid'"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

当状态为 ready 时,按钮的变体。

streamingIconappConfig.ui.icons.stopany

当状态为 streaming 时,按钮中显示的图标。

streamingColor'neutral'"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"

当状态为 streaming 时,按钮的颜色。

streamingVariant'subtle'"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

当状态为 streaming 时,按钮的变体。

submittedIconappConfig.ui.icons.stopany

当状态为 submitted 时,按钮中显示的图标。

submittedColor'neutral'"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"

当状态为 submitted 时,按钮的颜色。

submittedVariant'subtle'"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

当状态为 submitted 时,按钮的变体。

errorIconappConfig.ui.icons.reloadany

当状态为 error 时,按钮中显示的图标。

errorColor'error'"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"

当状态为 error 时,按钮的颜色。

errorVariant'soft'"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"

当状态为 error 时,按钮的变体。

type'button'"reset" | "submit" | "button"

当不是链接时,按钮的类型。

autofocusfalse | true | "true" | "false"
disabledboolean
namestring
labelstring
activeColor"error" | "primary" | "secondary" | "success" | "info" | "warning" | "neutral"
activeVariant"solid" | "outline" | "soft" | "subtle" | "ghost" | "link"
尺寸'md'"md" | "xs" | "sm" | "lg" | "xl"
正方形boolean

以四边等距内边距渲染按钮。

blockboolean

渲染全宽按钮。

loadingAutoboolean

根据 @click promise 状态自动设置加载状态

avatarAvatarProps

在左侧显示头像。

前置boolean

当为 true 时,图标将显示在左侧。

leadingIconany

在左侧显示图标。

尾部boolean

当为 true 时,图标将显示在右侧。

trailingIconany

在右侧显示图标。

loadingboolean

当为 true 时,将显示加载图标。

loadingIconappConfig.ui.icons.loadingany

loading prop 为 true 时显示的图标。

ui{ base?: ClassNameValue; } & { base?: ClassNameValue; label?: ClassNameValue; leadingIcon?: ClassNameValue; leadingAvatar?: ClassNameValue; leadingAvatarSize?: ClassNameValue; trailingIcon?: ClassNameValue; }
此组件还支持所有原生 <button> HTML 属性。

插槽

插槽类型
前置{ ui: object; }
default{ ui: object; }
尾部{ ui: object; }

事件

事件类型
stop[event: MouseEvent]
reload[event: MouseEvent]

主题

app.config.ts
export default defineAppConfig({
  ui: {
    chatPromptSubmit: {
      slots: {
        base: ''
      }
    }
  }
})
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({
      ui: {
        chatPromptSubmit: {
          slots: {
            base: ''
          }
        }
      }
    })
  ]
})

更新日志

184ea— chore: 减少类型冗余,通过省略操作按钮中的链接属性

736a5— 修复:代理事件到 stopreload 触发 (#5400)

5b177— feat: 扩展原生 HTML 属性 (#5348)

61b60— 功能:允许传递组件而不是名称 (#4766)

de782— feat!: 升级 ai-sdk 到 v5 (#4698)

5cb65— 特性:导入 @nuxt/ui-pro 组件