聊天
使用 @zeus-web/chat 构建无样式约束的 ChatGPT 风格聊天界面,并复用 Web Component 及轻量 React/Vue 包装器。
@zeus-web/chat高级组件自动生成
交互演示
源码
请选择与你的应用框架匹配的入口。
html
<script type="module">
import '@zeus-web/chat/wc/auto'
</script>
<zw-chat>
<zw-chat-thread slot="thread">
<zw-chat-message role="assistant"> How can I help? </zw-chat-message>
</zw-chat-thread>
<zw-chat-composer slot="composer"></zw-chat-composer>
</zw-chat>tsx
import {
Chat,
ChatComposer,
ChatMessage,
ChatThread,
} from '@zeus-web/chat/react'
export function SupportChat() {
return (
<Chat
thread={
<ChatThread>
<ChatMessage role="assistant">How can I help?</ChatMessage>
</ChatThread>
}
composer={<ChatComposer />}
/>
)
}vue
<script setup lang="ts">
import { Chat, ChatComposer, ChatMessage, ChatThread } from '@zeus-web/chat/vue'
</script>
<template>
<Chat>
<template #thread>
<ChatThread>
<ChatMessage role="assistant">How can I help?</ChatMessage>
</ChatThread>
</template>
<template #composer>
<ChatComposer />
</template>
</Chat>
</template>何时使用
- 构建 AI 聊天、智能体控制台或对话式助手界面。
- 在原生 Web Component、React 和 Vue 中复用同一套聊天行为协议。
- 在保持产品样式自由的同时复用结构、事件、方法和无障碍语义。
何时不应使用
- 不要把它当作模型请求库。
- 不要在组件内部嵌入服务提供方请求逻辑。
- 不要把凭据、密钥或令牌放进组件属性。
- 当前阶段不要依赖 Markdown 解析、代码高亮或文件上传传输层。
安装
bash
pnpm add @zeus-web/chat组件族
zw-chatzw-chat-threadzw-chat-messagezw-chat-composerzw-chat-code-blockzw-chat-tool-callzw-chat-artifactzw-chat-typing
插槽
zw-chat
headersidebarthreadartifactcomposeremptyloadingContent
zw-chat-message
avatarheaderdefaultfooteractions
zw-chat-composer
prefixattachmentssubmitsuffix
zw-chat-code-block
filenameContentlanguageContentactionsdefault
zw-chat-tool-call
summaryinputoutputerroractions
zw-chat-artifact
headerdefaultfooteractions
事件
zw-chat
sendabortregeneratemessage-actionartifact-open
zw-chat-composer
sendvalue-changeattachment-change
zw-chat-message
message-action
zw-chat-code-block
code-action
zw-chat-artifact
artifact-open
方法
zw-chat
setMessagesappendMessageupdateMessageappendMessagePartcleargetMessagesscrollToBottomemitSendemitAbortemitRegenerateemitMessageActionemitArtifactOpen
zw-chat-thread
scrollToBottom
zw-chat-composer
focusclearsubmit
zw-chat-message
emitAction
zw-chat-code-block
emitAction
zw-chat-artifact
openArtifact
示例
原生 Web Component 用法
使用自动注册入口,并通过普通 DOM 组合聊天、会话、消息和输入区域。
ts
import '@zeus-web/chat/wc/auto'
const chat = document.createElement('zw-chat')
const thread = document.createElement('zw-chat-thread')
const composer = document.createElement('zw-chat-composer')
thread.slot = 'thread'
composer.slot = 'composer'
composer.addEventListener('send', event => {
console.log((event as CustomEvent).detail.value)
})
chat.append(thread, composer)
document.body.append(chat)React 包装器用法
宿主应用使用 React 时,请使用生成的 React 包装器。
tsx
import { Chat, ChatComposer, ChatThread } from '@zeus-web/chat/react'
export function App() {
return (
<Chat emptyText="No messages">
<ChatThread slot="thread" />
<ChatComposer slot="composer" placeholder="Type a message..." />
</Chat>
)
}AI 使用提示
- 生成聊天界面时组合使用 zw-chat、zw-chat-thread、zw-chat-message 和 zw-chat-composer。
- 代码内容使用 zw-chat-code-block;不要把代码高亮库放进聊天根组件。
- 产物或画布区域使用 zw-chat-artifact 的对应插槽。
- 使用 zw-chat-tool-call 展示工具调用。
- 业务请求逻辑应保留在应用层。