组件/AI Elements

AI Elements

用于 AI 对话界面的会话与消息基础组件

Verified自 0.3.9

用法

基础

showAvatar 显示头像列;没有 avatar 时显示名称首字母。

Loading demo...

最佳实践

  • 在宿主维护 provider 的流式状态,再映射到 status。
  • id 用对话存储中的稳定 id,不用数组下标。
  • 助手输出保持 markdown 开启;不可信的 Markdown 在数据边界清洗。
  • 推理、工具调用、附件与来源用 parts 描述,不要往 content 里塞 HTML。

API 参考

TxAiConversation

属性

属性名类型默认值说明
messagesAiElementMessage[]必填对话数据,按顺序渲染。
markdownbooleantrue用 TxMarkdownView 渲染正文。
compactbooleanfalse收紧消息间距。
emptyTextstring'No messages yet'没有可渲染消息时的空状态文案。
showAvatarbooleanfalse为每条消息显示头像列。
typingLabelstring-转发给每条消息:输入中指示器的可访问名称。

事件

事件名参数说明
open-source(source: AiSourceItem)转发自被点击来源所在的消息。

插槽

插槽名Props说明
empty-替换内置空状态。
default / avatar / markdown-renderer / tool-result同 TxAiMessage转发给每条 TxAiMessage。

TxAiMessage

属性

属性名类型默认值说明
messageAiElementMessage必填单条消息。
markdownbooleantrue用 TxMarkdownView 渲染正文;false 时按纯文本保留换行。
compactbooleanfalse紧凑行距与更小的头像。
showAvatarbooleanfalse显示头像。
typingLabelstring'AI is typing'输入中指示器的可访问名称。

事件

事件名参数说明
open-source(source: AiSourceItem)点击 sources 片段中的来源时触发,默认跳转已阻止。

插槽

插槽名Props说明
default{ message }替换正文区域,用于工具卡片、附件或自定义渲染。
avatar{ message }showAvatar 开启时替换头像。
markdown-renderer{ part, message, streaming }替换 text 片段的渲染;只有流式消息的最后一个文本片段 streaming 为真。
tool-result{ part }替换 tool-call 片段在 done 后的结果区。

类型

AiElementMessage

字段类型默认值说明
idstring必填稳定 key。
role'user' | 'assistant' | 'system' | 'tool'必填视觉与语义角色。
contentstring必填文本或 Markdown 源。
createdAtnumber | string | Date-宿主的时间戳。
namestring-显示名称;缺省为 You / AI / Tool / System。
avatarstring-头像图片 URL。
status'pending' | 'streaming' | 'complete' | 'error'-消息生命周期状态。
partsAiMessagePart[]-非空时按顺序渲染这些片段,取代 content。

AiMessagePart

EXAMPLE.TS
type AiMessagePart =
  | { type: 'text', text: string } // markdown-renderer 插槽或 TxMarkdownView
  | { type: 'reasoning', text: string, done?: boolean, durationMs?: number } // TxReasoningDisclosure
  | AiToolCallPart // type: 'tool-call',由 TxToolCallCard 渲染
  | { type: 'attachment', attachments: AiAttachment[] } // 只读的 TxAttachmentTray
  | { type: 'sources', sources: AiSourceItem[] } // TxSources

概述

  • content 为空的消息不渲染,带 parts 或 status 为 pending、streaming 时除外。
  • 这类空消息若没有 parts,显示输入中指示器。
  • TxAiConversation 是 aria-live="polite" 区域:追加的消息会被播报,不抢焦点。

技术实现

  • 源码:packages/tuffex/packages/components/src/ai-elements/。
查看源码
packages/tuffex/packages/components/src/ai-elements/index.ts