跳转到内容

聊天 ​

使用 @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-chat
  • zw-chat-thread
  • zw-chat-message
  • zw-chat-composer
  • zw-chat-code-block
  • zw-chat-tool-call
  • zw-chat-artifact
  • zw-chat-typing

插槽 ​

zw-chat ​

  • header
  • sidebar
  • thread
  • artifact
  • composer
  • empty
  • loadingContent

zw-chat-message ​

  • avatar
  • header
  • default
  • footer
  • actions

zw-chat-composer ​

  • prefix
  • attachments
  • submit
  • suffix

zw-chat-code-block ​

  • filenameContent
  • languageContent
  • actions
  • default

zw-chat-tool-call ​

  • summary
  • input
  • output
  • error
  • actions

zw-chat-artifact ​

  • header
  • default
  • footer
  • actions

事件 ​

zw-chat ​

  • send
  • abort
  • regenerate
  • message-action
  • artifact-open

zw-chat-composer ​

  • send
  • value-change
  • attachment-change

zw-chat-message ​

  • message-action

zw-chat-code-block ​

  • code-action

zw-chat-artifact ​

  • artifact-open

方法 ​

zw-chat ​

  • setMessages
  • appendMessage
  • updateMessage
  • appendMessagePart
  • clear
  • getMessages
  • scrollToBottom
  • emitSend
  • emitAbort
  • emitRegenerate
  • emitMessageAction
  • emitArtifactOpen

zw-chat-thread ​

  • scrollToBottom

zw-chat-composer ​

  • focus
  • clear
  • submit

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 展示工具调用。
  • 业务请求逻辑应保留在应用层。

基于 MIT 许可证发布。