跳转到主内容
Plus 组件 · Chat
对话
用于快速搭建对话内容

使用场景 ​

Chat 组件可用于普通会话,AI 会话等场景。

对话内容渲染基于 MarkdownRender 组件,支持 Markdown 和 MDX (注:Chat 中的 MarkdownRender 的默认 format 模式是 md,如果需要使用 MDX 格式,可通过 markdownRenderProps API 设置),可实现图片,表格,链接,加粗,代码区等常用富文本功能。也可通过 Vue 组件映射实现更复杂的文档撰写与展示需求。

Vue 使用说明:从 @aifuxi/semi-ui-vue/chat 子路径引入组件,浏览器构建会自动按需加载默认主题。本页 Vue API 与示例以当前公开实现为准。

代码演示 ​

如何引入 ​

查看代码 · js
import { Chat } from '@aifuxi/semi-ui-vue';

基本用法 ​

通过 v-model:chats 展示并更新对话列表,通过 message-send 事件处理消息发送。

附件支持通过点击上传按钮,输入框粘贴,拖拽文件至 Chat 区域上传。通过 uploadProps 设置上传参数,详情参考 Upload。

上传按钮的提示文案可通过 uploadTipProps 设置,详情参考 Tooltip。

对话是多方参与,多轮交互的场景。可通过 roleConfig 传入角色信息(包括名称,头像等),具体参数细节 RoleConfig。

使用 align 属性可以设置对话的布局,支持左右分布(leftRight, 默认)和左对齐(leftAlign)。

模式
会话布局方式
System

Hello, I'm your AI assistant.

User

给一个 Semi Design 的 Button 组件的使用示例

Assistant

以下是一个 Vue 代码的使用示例:

vue
<script setup>
import { Button } from '@aifuxi/semi-ui-vue/button';
</script>
<template><Button>Click me</Button></template>

消息状态 ​

chats 类型为 Message[], Message 包含对话的各种信息,如角色(role)、内容(content)、附件(attachment)、状态(status) 、唯一标识(id)、创建时间(createAt)等,具体见 Message。其中 status 不同,会话样式不同。

Assistant

请求成功

请求错误

动态更新数据 ​

对于后台返回 Serve Side Event 数据情况,可将获取到的数据用于更新 chats,对话内容将实时更新。

showStopGenerate 可设置是否展示停止生成按钮,默认为 false;通过 stop-generator 事件处理停止生成逻辑。

System

Hello, I'm your AI assistant.

User

介绍一下 Semi design

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统。作为一个全面、易用、优质的现代应用UI解决方案,Semi Design从字节跳动各业务线的复杂场景中提炼而来,目前已经支撑了近千个平台产品,服务了内外部超过10万用户[1]。

Semi Design的特点包括:

  1. 设计简洁、现代化。

  2. 提供主题方案,可深度样式定制。

  3. 提供明暗色两套模式,切换方便。

  4. 国际化,覆盖了简/繁体中文、英语、日语、韩语、葡萄牙语等20+种语言,日期时间组件提供全球时区支持,全部组件可自动适配阿拉伯文RTL布局。

  5. 采用 Foundation 和 Adapter 跨框架技术方案,方便扩展。

Learn more:

  1. Introduction 介绍 - Semi Design

  2. Getting Started 快速开始 - Semi Design

  3. Semi D2C 设计稿转代码的演进之路 - 知乎

清除上下文 ​

通过 showClearContext 可以开启在输入框中显示清除上下文按钮,默认为 false。 也可以通过模板 ref 调用 clearContext 方法清除上下文。

System

Hello, I'm your AI assistant.

User

介绍一下 semi design

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统

自定义渲染会话框 ​

通过 chatBoxRenderConfig 传入自定义渲染配置, chatBoxRenderConfig 类型如下

查看代码 · ts
import type { VNodeChild } from 'vue';

export interface RenderTitleProps {
    message?: Message;
    role?: Metadata;
    defaultTitle?: VNodeChild
}

export interface RenderAvatarProps {
    message?: Message; /* Supported in  2.69.0*/
    role?: Metadata, 
    defaultAvatar?: VNodeChild
}

export interface RenderContentProps {
    message?: Message;
    role?: Metadata;
    defaultContent?: VNodeChild | VNodeChild[]; 
    className?: string;
}

export interface DefaultActionNodeObj {
    copyNode: VNodeChild;
    likeNode: VNodeChild;
    dislikeNode: VNodeChild;
    resetNode: VNodeChild;
    deleteNode: VNodeChild;
}

export interface RenderActionProps {
    message?: Message;
    defaultActions?: VNodeChild | VNodeChild[];
    className: string;
    defaultActionsObj?: DefaultActionNodeObj; /* Supported in  2.69.0*/
};

export interface FullChatBoxNodes {
    avatar?: VNodeChild;
    title?: VNodeChild; 
    content?: VNodeChild; 
    action?: VNodeChild
}

export interface RenderFullChatBoxProps {
    message?: Message;
    role?: Metadata;
    defaultNodes?: FullChatBoxNodes;
    className: string;
}

export interface ChatBoxRenderConfig {
    /* 自定义渲染标题 */
    renderChatBoxTitle?: (props: RenderTitleProps) => VNodeChild;
    /* 自定义渲染头像 */
    renderChatBoxAvatar?: (props: RenderAvatarProps) => VNodeChild;
    /* 自定义渲染内容区域 */
    renderChatBoxContent?: (props: RenderContentProps) => VNodeChild;
    /* 自定义渲染消息操作栏 */
    renderChatBoxAction?: (props: RenderActionProps) => VNodeChild;
    /* 完全自定义渲染整个聊天框 */
    renderFullChatBox?: (props: RenderFullChatBoxProps) => VNodeChild
}

自定义头像和标题优先使用 chat-box-avatar 与 chat-box-title 插槽;迁移代码也可继续使用对应 callback。

头像渲染模式
标题渲染模式

Hello, I'm your AI assistant.

这张图片里有什么?

图片中是本地示例插画。

鼠标移动到会话上即可显示操作区;优先使用 chat-box-action 插槽自定义,迁移代码也可继续使用 renderChatBoxAction callback prop。

System

Hello, I'm your AI assistant.

User

介绍一下 semi design

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统

优先使用 chat-box-content 插槽自定义会话内容,迁移代码也可继续使用 renderChatBoxContent callback prop。

Assistant

优先使用 chat-box 插槽自定义完整会话项,迁移代码也可继续使用 renderFullChatBox callback prop。

System

Hello, I'm your AI assistant.

介绍一下 semi design

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统

自定义渲染输入框 ​

优先使用 input-area 作用域插槽自定义输入区,迁移代码也可继续使用 renderInputArea callback prop。

查看代码 · ts
import type { VNodeChild } from 'vue';

export interface RenderInputAreaProps {
    /* 默认节点 */
    defaultNode?: VNodeChild;
    /* 如果自定义输入框,发送消息时需调用 */
    onSend?: (content?: string, attachment?: FileItem[]) => void;
    /* 如果自定义清除上下文按钮,点击清除上下文时需调用 */
    onClear?: (e?: any) => void;
    /* detailProps 自 2.69.0 版本开始支持 */
    detailProps: {
        /* 清除上下文按钮 */
        clearContextNode?: VNodeChild;
        /* 上传按钮 */
        uploadNOde?: VNodeChild;
        /* 文本输入框 */
        inputNode?: VNodeChild;
        /* 发送按钮 */
        sendNode?: VNodeChild;
        /* 点击触发聚焦文本输入框的处理函数*/
        onClick?: (e?: MouseEvent) => void;
    }
}

detailProps 的使用示例如下

查看代码 · ts
import { h, type VNodeChild } from 'vue';
import type { ChatRenderInputAreaProps } from '@aifuxi/semi-ui-vue/chat';

function renderInputArea({ detailProps }: ChatRenderInputAreaProps): VNodeChild {
  const { uploadNode, inputNode, sendNode, onClick } = detailProps;
  return h(
    'div',
    {
      style: {
        margin: '8px 16px',
        display: 'flex',
        alignItems: 'flex-end',
        padding: '10px',
        border: '1px solid var(--semi-color-border)',
        borderRadius: '16px',
      },
      onClick,
    },
    [uploadNode, inputNode, sendNode],
  );
}

其他使用示例如下

System

Hello, I'm your AI assistant.

输入信息

提示信息 ​

通过 hints 设置提示区域内容;点击提示项后会触发 hint-click 事件。

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统,你可以向我提问任何关于 Semi 的问题。

自定义提示信息渲染 ​

优先使用 hint 作用域插槽自定义提示项;迁移代码也可继续使用 renderHintBox callback prop。

查看代码 · ts
import type { VNodeChild } from 'vue';

type renderHintBox = (props: {content: string; index: number,onHintClick: () => void}) => VNodeChild;

使用示例如下:

Assistant

Semi Design 是由抖音前端团队和MED产品设计团队设计、开发并维护的设计系统,你可以向我提问任何关于 Semi 的问题。

API 参考 ​

Vue 契约 ​

以下内容以 packages/ui/src/chat/types.ts、packages/ui/src/chat/index.ts 的公开类型为准。

  • v-model:chats 对应 chats 与 update:chats;列表变更时同时触发 chats-change。

Vue 用法 ​

  • chatBoxRenderConfig、renderDivider、renderHintBox 与 renderInputArea 是保留的 callback props,不是组件事件。
  • topSlot、bottomSlot 兼容 VNode prop;Vue 代码优先使用 top、bottom 及其他作用域插槽。
  • enableUpload 对象可分别控制 pasteUpload、dragUpload、clickUpload;uploadProps 与 uploadTipProps 继续作为嵌套配置传入。

Vue 实例方法 ​

ChatExposed

方法签名说明
resetMessage() => void重置最后一条消息
clearContext() => void清除上下文
scrollToBottom(animation?: boolean) => void滚动到列表底部
sendMessage(content: string, attachment?: UploadFileItem[]) => void发送消息
getContainerElement() => HTMLDivElement | null获取滚动容器

Vue 事件 ​

Chat

事件参数说明
update:chats[chats: ChatMessage[]]更新 chats 绑定
chats-change[chats: ChatMessage[]]对话消息列表变化
message-delete[message?: ChatMessage]删除消息
message-reset[message?: ChatMessage]重置消息
message-copy[message?: ChatMessage]复制消息
message-good-feedback[message?: ChatMessage]提交消息正向反馈
message-bad-feedback[message?: ChatMessage]提交消息负向反馈
message-send[content: string, attachment: UploadFileItem[]]发送消息
input-change[payload: ChatInputChangePayload]输入内容或附件变化
hint-click[hint: string]点击提示词
clear[]清除上下文
stop-generator[event?: Event]停止生成

Vue 插槽 ​

Chat

插槽作用域参数说明
top{}顶部内容
bottom{}底部内容
hint{ content: string, index: number, onHintClick: () => void }自定义提示项
divider{ message: ChatMessage }自定义分割线
input-areaChatRenderInputAreaProps自定义输入区
chat-box-titleChatRenderTitleProps自定义会话标题
chat-box-avatarChatRenderAvatarProps自定义会话头像
chat-box-contentChatRenderContentProps自定义会话内容
chat-box-actionChatRenderActionProps自定义会话操作区
chat-boxChatRenderFullBoxProps自定义完整会话项
属性说明类型默认值
classVue class 入口HTMLAttributes['class']—
className自定义类名HTMLAttributes['class']-
style—CSSProperties—
align对话布局方式,支持 leftRight、leftAlignChatAlignleftRight
mode对话模式,支持 bubble | noBubble | userBubbleChatModebubble
chats受控对话列表ChatMessage[][]
canSend发送按钮是否可以发送。通常无需设置,由内部逻辑决定。如有设置,以此设置为准,v2.90.0 新增boolean—
hints提示信息string[][]
roleConfig角色信息配置,具体见 RoleConfigChatRoleConfig-
chatBoxRenderConfig会话框各区域的渲染 callback 配置;同名插槽优先ChatBoxRenderConfig-
customMarkDownComponents传给 MarkdownRender 的自定义 Vue 组件映射Record<string, unknown>-
markdownRenderProps该参数将透传给对话框渲染所用的 MarkdownRender 组件,详见 MarkdownRenderProps,Chat 中的 markdownRender 默认 format 为 md,如果需要使用 mdx 格式,可通过此参数设置ChatMarkdownRenderProps-
escapeHtml是否对用户消息中的 HTML 标签进行转义,防止被 Markdown 解析器当作 HTML 处理导致内容丢失booleantrue
enableUpload是否启用上传, 自 v2.76.0 支持,支持 boolean 类型及对象类型,传入 boolean 类型将同时控制拖拽,点击上传按钮,在输入框中粘贴的上传行为,传入对象可分别设置,传入对象类型时未设置的项默认为 trueboolean | ChatEnableUploadPropstrue
uploadProps上传组件属性, 详情参考 UploadPartial<UploadProps>-
uploadTipProps上传组件提示属性, 详情参考 TooltipRecord<string, unknown>-
renderHintBox提示项渲染 callback prop;hint 插槽优先(props: { content: string; index: number; onHintClick: () => void; }) => VNodeChild-
renderDivider分割线渲染 callback prop;divider 插槽优先(message?: ChatMessage) => VNodeChild-
renderInputArea输入区渲染 callback prop;input-area 插槽优先(props: ChatRenderInputAreaProps) => VNodeChild-
topSlot顶部 VNode;top 插槽优先VNodeChild-
bottomSlot底部 VNode;bottom 插槽优先VNodeChild-
showStopGenerate是否展示停止生成按钮booleanfalse
showClearContext是否展示清除上下文按钮booleanfalse
sendHotKey发送输入内容的键盘快捷键,支持 enter | shift+enter。前者在单独按下 enter 将发送输入框中的消息, shift 和 enter 按键同时按下时,仅换行,不发送。后者相反ChatSendHotKeyenter
placeholder输入框占位符string-
inputBoxStyle输入框样式CSSProperties-
inputBoxCls输入框类名string-
hintStyle提示区最外层样式CSSProperties-
hintCls提示区最外层样式类名string-

RoleConfig ​

属性说明类型默认值
user用户信息ChatRoleMetadata-
assistant助手信息ChatRoleMetadata-
system系统信息ChatRoleMetadata-

Metadata ​

属性说明类型默认值
name名称string-
avatar头像地址或 VNodestring | VNodeChild-
color头像背景色,同 Avatar 组件的 color 参数, 支持 amber、 blue、 cyan、 green、 grey、 indigo、 light-blue、 light-green、 lime、 orange、 pink、 purple、 red、 teal、 violet、 yellowstringgrey

Message ​

属性说明类型默认值
role角色string-
name名称string-
id唯一标识string | number-
content文本内容string | ChatContentItem[]—
parentId父节点idstring-
createAt创建时间number-
status消息状态,可选值为 loading | incomplete | complete | errorChatMessageStatuscomplete
like是否已提交正向反馈boolean—
dislike是否已提交负向反馈boolean—

Content ​

属性说明类型默认值
type类型, 可选值text | image_url | file_urlstring-
text当类型为 text 时的内容数据string-
image_url当类型为 image_url 时的内容数据{ url: string }-
file_url当类型为 file_url 时的内容数据{ url: string; name: string; size: string; type: string }-

Methods ​

方法说明
resetMessage重置消息
scrollToBottom(animation?: boolean)滚动到最底部,animation 为 true 时使用动画
clearContext清除上下文
sendMessage(content: string, attachment?: UploadFileItem[])发送消息
getContainerElement获取滚动容器