跳转到主内容
AI 组件 · AIChatDialogue
AI对话
用户展示 AI 聊天中的对话信息

使用场景 ​

AIChatDialogue 组件可搭配 AIChatInput 使用,实现更丰富的、功能覆盖更全面、定制更加便捷的 AI 会话场景。 组件消息格式以 OpenAI 的 Response Object 为原型,默认支持 OpenAI 社区 Response / Chat Completion 格式标准,对 GPT-5、GPT-4o 系列模型的响应均支持开箱即用,详见消息数据转换。

Vue 使用说明:从 @aifuxi/semi-ui-vue/ai-chat-dialogue 子路径引入组件,浏览器构建会自动按需加载默认主题。本页 Vue API 与示例以当前公开实现为准。文中功能版本与 API 表的版本列记录 Semi Design 上游历史。

代码演示 ​

如何引入 ​

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

基本用法 ​

通过 v-model:chats 实现基础对话显示和交互。

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

System
User
Assistant

消息状态 ​

chats 类型为 Message[], Message 包含对话的各种信息,如角色 role、内容 content、状态 status 、唯一标识 id、创建时间 createdAt 等,具体见 Message。其中 status 和 Response API Status 相同,存在 6 种状态,对应 3 种官方样式(成功 / 请求中 / 失败)。

Assistant
Assistant
请稍候...
Assistant

消息展示 ​

消息内容展示的类型为 ContentItem[],支持文本 text、文件 file、图片 image、代码 code、思考块 reasoning、参考来源 annotation、工具调用 tool call 等消息块的展示,同时提供 AIChatDialogue.Step 组件用于步骤等信息的分步展示。

Assistant
Assistant
已思考完成
get_weather {'location':'Paris, France'}
2 篇资料
创建一份全面的北京旅游攻略,包含景点、住宿、交通、美食和实用旅行建议
搜索北京旅游景点介绍及门票信息
正在搜索: 北京旅游景点介绍及门票信息
读取指定文件的指定行内容
正在创建文档: 北京旅游攻略
创建包含北京旅游攻略的文件
正在创建代码文件: beijing_travel_guide.html
总结北京旅游攻略的创建成果并呈现给用户

引用 ​

通过 references 字段定义当前消息引用的文件或者文本, showReference 配置当前消息是否显示可被引用样式,并通过 reference-click 事件监听引用按钮点击。具体和 AIChatInput 的搭配使用见 AI 组件构建对话

User
测试文本,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字,这里是一段很长的文字
飞书文档.docx
Music.mp4
Image.jpeg
code.json

选择 ​

System
User
Assistant
已选:

提示 ​

通过 hints 可设置提示区域内容, 点击提示内容后,提示内容将成为新的用户输入内容,并触发 hint-click 事件。

Assistant
Semi 组件库有哪些常用组件?
能否展示一个使用 Semi 组件库构建的页面示例?
Semi 组件库有官方文档吗?

自定义渲染提示 ​

通过 hint 插槽可自定义提示区域内容;迁移代码也可继续使用 renderHintBox callback prop。

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

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

自定义渲染会话框 ​

通过 dialogueRenderConfig 传入自定义渲染配置,或使用对应作用域插槽。

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

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

export interface RenderAvatarProps {
    message?: Message; 
    role?: Metadata, 
    defaultAvatar?: VNodeChild
}

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

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

export interface RenderActionProps {
    message?: Message;
    defaultActions?: VNodeChild | VNodeChild[];
    className: string;
    defaultActionsObj?: DefaultActionNodeObj;
};

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

export interface RenderFullDialogueProps {
    message?: Message;
    role?: Metadata;
    defaultNodes?: FullDialogueNodes;
    className: string
}


export interface DialogueRenderConfig {
    /* 自定义渲染标题 */
    renderDialogueAction?: (props: RenderActionProps) => VNodeChild;
    /* 自定义渲染头像 */
    renderDialogueAvatar?: (props: RenderAvatarProps) => VNodeChild;
    /* 自定义渲染内容区域 */
    renderDialogueContent?: (props: RenderContentProps) => VNodeChild;
    /* 自定义渲染消息操作栏 */
    renderDialogueTitle?: (props: RenderTitleProps) => VNodeChild;
    /* 完全自定义渲染整个聊天框 */
    renderFullDialogue?: (props: RenderFullDialogueProps) => VNodeChild
}

自定义渲染头像和标题,可使用 dialogue-avatar 和 dialogue-title 插槽。

My-System
My-User
My-Assistant

自定义渲染消息内容 ​

通过 renderDialogueContentItem 按照消息类型返回内容渲染,用法如下

User
你好
Assistant
你好呀,请问有什么可以帮助你的吗~
User
帮我生成类似的图片
Assistant
已思考完成
Function Tool Call: create_travel_guide { "city": "北京" }

消息数据转换 ​

当前组件的对话消息以 OpenAI 的 Response Object 为原型,为了支持用户更好地无缝集成 Chat Completion API 和 Response API,我们提供了四种 Adapter 转换函数,用户可直接使用该函数转换 API 的返回结果,得到可直接用于消息展示的数据,提供两种 Adapter 用于将 ChatInput 组件的数据处理成适配于 Response API 的 input Message 或者 Chat Completion API 中的 Input Message 格式。

查看代码 · ts
// 将 Chat Completion API 返回的数据转换为 Chat Dialogue 中的 Message 格式
function chatCompletionToMessage(chatCompletion: ChatCompletion): Message[]

// 将 Chat Completion API 流式返回数据转换为 Chat Dialogue 中的 Message 格式
function streamingChatCompletionToMessage(chatCompletionChunks: ChatCompletionChunk[], state?: StreamingChatState): { messages: Message[]; state?: StreamingChatState }

// 将 Response API 返回的数据转换为 Chat Dialogue 中的 Message 格式
function responseToMessage(response: Response): Message

// 将 Response API 返回流式数据转换为 Chat Dialogue 中的 Message 格式
function streamingResponseToMessage(chunks: ResponseChunk[], prevState: StreamingResponseState): { messages: Message[]; state?: StreamingResponseState }

// 将 Chat Input 数据转换为 Chat Dialogue 中的 Message 格式,(同 Response API Input Message 格式)
function chatInputToMessage(inputContent: MessageContent): Message

// 将 Chat Input 数据转换为 Chat Completion API 中的 Input Message 格式
function chatInputToChatCompletion(inputContent: MessageContent): ChatCompletionInput

比如,当用户使用 Chat Completion API 接口返回非流式数据时,可以通过 chatCompletionToMessage 函数将 Chat Completion Object 转换为 Dialogue Message 消息块格式。注意,因为 Chat Completion API 可以通过 n 来控制每条输入消息生成多少个结果所以该函数的返回值为数组。(注意:如果 n > 1,用户需要自行决定将哪条数据添加到 message 中展示)

Assistant
get_current_weather { "location": "Boston, MA" }

比如,当用户使用 Chat Completion API 接口返回流式数据时,可以通过 streamingChatCompletionToMessage 函数将 Chat Completion Chunk Object List 转换为 Dialogue Message 消息块格式。

当用户使用 Response API 接口返回非流式数据时,可以通过 responseToMessage 函数将 Response Object 转换为 Dialogue Message 消息块格式。

Assistant
已思考完成
3 篇资料
get_semi_page {"pageName":"AIChatDialogue"}

当用户使用 Response API 接口返回流式数据时,可以通过 streamingResponseToMessage 函数将 Response Chunk Object List 转换为 Dialogue Message 消息块格式。

API 参考 ​

Vue 契约 ​

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

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

Vue 用法 ​

  • dialogueRenderConfig、renderDialogueContentItem、renderHintBox 与 messageEditRender 是保留的 callback props,不是组件事件。
  • Reasoning、Step、Annotation 与 defaultComponents.code 同时作为静态成员提供;Reasoning、Step、Annotation、Code 也可使用具名导出。
  • Reasoning 接受 status/summary/content/markdownRenderProps/completedText/thinkingText;Step 接受 steps;Annotation 接受 annotation/maxCount/description/annotationText。

Vue 实例方法 ​

AIChatDialogueExpose

方法签名说明
selectAll() => void全选所有消息
deselectAll() => void取消全选所有消息
scrollToBottom(animation?: boolean) => void滚动到列表底部
scrollToTop(animation?: boolean) => void滚动到列表顶部
getContainerElement() => HTMLDivElement | null获取滚动容器

Vue 事件 ​

AIChatDialogue

事件参数说明
update:chats[chats: Message[]]更新 chats 绑定
chats-change[chats: Message[]]对话消息列表变化
select[selectedIds: string[]]选择项变化
annotation-click[annotation: Annotation[]]点击注释资料
file-click[file: InputFile]点击附件文件
image-click[image: InputImage]点击图片
hint-click[hint: string]点击提示词
reference-click[reference: Reference]点击引用
message-bad-feedback[message: Message]提交消息负向反馈
message-copy[message: Message]复制消息
message-delete[message: Message]删除消息
message-edit[message: Message]编辑消息
message-good-feedback[message: Message]提交消息正向反馈
message-reset[message: Message]重置消息
message-share[message: Message]分享消息

AIChatDialogueAnnotation

事件参数说明
click[annotation: Annotation[]]点击注释资料

Vue 插槽 ​

AIChatDialogue

插槽作用域参数说明
dialogue-avatarRenderAvatarProps自定义头像
dialogue-titleRenderTitleProps自定义标题
dialogue-contentRenderContentProps自定义消息内容
dialogue-actionRenderActionProps自定义操作区
full-dialogueRenderFullDialogueProps自定义完整对话项
hint{ content: string, index: number, onHintClick: () => void }自定义提示项
message-edit{ value: unknown }自定义消息编辑器

AIChatDialogueReasoning

插槽作用域参数说明
default{ raw: string, status?: string }自定义思考内容
属性说明类型默认值
align对话布局方式'leftRight' | 'leftAlign'leftRight
chats受控对话消息列表Message[][]
classVue class 入口HTMLAttributes['class']—
className自定义类名string-
disabledFileItemClick是否禁用文件点击booleanfalse
escapeHtml是否对用户消息中的 HTML 标签进行转义,防止被 Markdown 解析器当作 HTML 处理导致内容丢失booleantrue
hintCls提示区最外层样式类名string-
hints提示信息string[][]
hintStyle提示区最外层样式CSSProperties-
selecting是否开启选择模式booleanfalse
markdownRenderProps该参数将透传给对话框渲染所用的 MarkdownRender 组件,详见 MarkdownRenderProps。注意:当自定义 markdownRenderProps.components 时,如果包含 code 组件,会覆盖默认的代码块渲染组件。如需在自定义代码块渲染时保留默认功能,可通过 AIChatDialogue.defaultComponents.code 获取默认组件进行二次封装。MarkdownRenderProps-
messageEditRender消息编辑渲染 callback prop;message-edit 插槽优先(properties: unknown) => VNodeChild-
mode对话模式'bubble' | 'noBubble' | 'userBubble'bubble
roleConfig角色配置(user/assistant/system 等元数据)RoleConfig(必填)必填
style样式CSSProperties-
showReset是否展示重置操作booleantrue
showReference是否在文字或者文件消息中展示可被引用图标,仅对用户消息生效booleanfalse
dialogueRenderConfig各对话区块的渲染 callback 配置;同名插槽优先DialogueRenderConfig-
renderDialogueContentItem按消息类型配置内容渲染 callbackDialogueContentItemRendererMap-
renderHintBox提示项渲染 callback prop;hint 插槽优先(properties: { content: string; index: number; onHintClick: () => void; }) => VNodeChild-

RoleConfig ​

属性说明类型默认值
user用户信息AIChatDialogueMetadata | Map<string, AIChatDialogueMetadata>-
assistant助手信息AIChatDialogueMetadata | Map<string, AIChatDialogueMetadata>-
system系统信息AIChatDialogueMetadata | Map<string, AIChatDialogueMetadata>-

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 ​

属性说明类型默认值
id唯一标识string(必填)-
content消息内容string | ContentItem[]-
output_textResponse API 聚合文本string—
role角色string(必填)-
name名称string-
createdAt创建时间number-
updatedAt更新时间number—
model模型名称string-
status消息状态,可选值为 queued | in_progress | incomplete | completed | failed | cancelledstringcompleted
references消息引用列表Reference[]—
like是否已提交正向反馈boolean—
dislike是否已提交负向反馈boolean—
editing消息是否处于编辑状态boolean—

Reference ​

属性说明类型默认值
id唯一标识string | number-
type类型string-
name名称string-
url地址string-
content文本内容string-

Methods ​

方法说明
selectAll全选所有消息
deselectAll取消全选所有消息
scrollToBottom(animation: boolean)滚动到最底部, animation 为 true,则有动画,反之无动画
scrollToTop(animation: boolean)滚动到最顶部, animation 为 true,则有动画,反之无动画

Static Properties ​

属性说明类型
defaultComponents默认的 Markdown 渲染组件集合,包含增强版的 Code 组件,支持代码语言标识和复制功能。可用于 markdownRenderProps.components 的二次封装 >=2.94.0{ code: Component }

使用示例:

当需要在自定义代码块渲染时保留默认功能,可以通过 AIChatDialogue.defaultComponents.code 获取默认组件:

查看代码 · ts
import { AIChatDialogue } from '@aifuxi/semi-ui-vue/ai-chat-dialogue';

const markdownRenderProps = {
  components: {
    code: AIChatDialogue.defaultComponents.code,
  },
};

ContentItem ​

ContentItem 支持所有 OpenAI Response InputItem 和 OutputItem 类型,具体类型定义如下

查看代码 · ts
type ContentItem = InputContentItem | OutputContentItem;

type InputContentItem = InputMessage | ItemReference;
type OutputContentItem = OutputMessage | MCPToolCall | Reasoning | FileSearchToolCall | 
                        WebSearchToolCall | FunctionToolCall | CustomToolCall | ImageGenerationCall| CustomObject;

interface CommonContentItem {
    id?: string;
    type?: string;
    status?: string;
    role?: string
}

interface CustomObject {
    [key: string]: any
}

// input item
interface InputMessage extends CommonContentItem {
    content?: string | (InputText | InputImage | InputFile)[]
}

interface InputText extends CommonContentItem {
    text?: string
}

interface InputImage extends CommonContentItem {
    detail?: string;
    file_id?: string;
    image_url?: string
}

interface InputFile extends CommonContentItem {
    file_id?: string;
    file_data?: string;
    file_url?: string;
    filename?: string;
    size?: string;
    file_type?: string;
    fileInstance?: any
}

interface ItemReference extends CommonContentItem {
    file_id?: string
}


// output item
interface OutputMessage extends CommonContentItem {
    content?: (OutputText | Refusal)[]
}

interface OutputText {
    text?: string;
    type?: string;
    annotations?: Annotation[]
}

interface Refusal extends CommonContentItem {
    refusal?: string;
    type?: string
}

type Annotation = URLCitation | CustomObject;

interface URLCitation {
    end_index?: number;
    start_index?: number;
    title?: string;
    type?: string;
    url?: string
}

interface Reasoning extends CommonContentItem {
    summary?: {
        text?: string;
        type?: string
    }[];
    content?: {
        text?: string;
        type?: string
    }[]
}

interface FileSearchToolCallResult {
    attributes?: Map<string, string>;
    score?: number;
    file_id?: string;
    filename?: string;
    text?: string
}

interface FileSearchToolCall extends CommonContentItem {
    queries?: string[];
    results?: FileSearchToolCallResult[]
}

interface WebSearchToolCall extends CommonContentItem {
    action?: SearchAction | OpenPageAction | FindAction
}

interface SearchAction {
    type?: string;
    query?: string;
    sources?: { type: string; url: string }[]
}

interface OpenPageAction {
    type?: string;
    url?: string
}

interface FindAction {
    action?: string;
    type?: string;
    query?: string
}

interface FunctionToolCall extends CommonContentItem {
    call_id?: string;
    name?: string;
    arguments?: string
}

interface CustomToolCall extends CommonContentItem {
    call_id?: string;
    name?: string;
    input?: string
}

interface ImageGenerationCall extends CommonContentItem {
    result?: string
}

interface MCPToolCall extends CommonContentItem {
    arguments?: string;
    server_label?: string;
    name?: string;
    result?: string;
    output?: string
}

interface FileUploadToolCall extends CommonContentItem {
    result?: string
}