@flutchai/chat-react

中文文档
0.1.9发布于 2026/11/03MITTypeScriptES Module近 30 天下载 2034

React chat UI for a Flutch agent: themeable with CSS variables, every part replaceable.

原始使用文档
文档目录4 节
  1. Quick start
  2. Theming
  3. Replacing parts
  4. Hooks

@flutchai/chat-react

React chat UI for a Flutch agent. Themeable with CSS variables; the markdown renderer, buttons, message rows and attachment renderers can all be replaced.

The UI talks to your backend, never to Flutch directly. Your backend holds the Flutch API key and forwards requests (see @flutchai/chat-core).

Browser (this package)  ──►  your backend (adds X-API-Key + userId)  ──►  api.flutch.ai

Quick start

import { ChatProvider, Chat } from "@flutchai/chat-react";
import { createFlutchClient } from "@flutchai/chat-core";
import "@flutchai/chat-react/styles.css";

const client = createFlutchClient({
  baseUrl: "/api/chat", // your backend's proxy
  agentId: "67b88f7ec6092d7bc711a934",
  credentials: "include", // if your proxy authenticates with a session cookie
});

export function Support() {
  return (
    <div style={{ height: 600 }}>
      <ChatProvider client={client}>
        <Chat showThreads />
      </ChatProvider>
    </div>
  );
}

The chat fills its parent, so give the parent a height. It restores the previous conversation on mount (the current thread id is kept in localStorage; pass storage to ChatProvider to use something else).

Theming

Override the variables on the chat element or any ancestor:

.my-chat {
  --flutch-primary: #0a7a5a;
  --flutch-on-primary: #fff;
  --flutch-radius: 8px;
  --flutch-font: "Inter", sans-serif;
}
VariableDefault
--flutch-primary / --flutch-on-primary#4f46e5 / #fffSend button, accents
--flutch-user-bg / --flutch-user-textprimary / on-primaryYour user's bubbles
--flutch-assistant-bg--flutch-surfaceAgent bubbles
--flutch-bg, --flutch-surface, --flutch-text, --flutch-muted, --flutch-borderlightSurfaces and text
--flutch-danger, --flutch-danger-bgredError banner
--flutch-radius, --flutch-gap, --flutch-font, --flutch-font-size14px, 12px, system, 15pxShape and type
--flutch-chart-1 … --flutch-chart-8primary, then a fixed paletteChart series

Dark mode follows the OS. Force one with <Chat theme="dark" />.

Replacing parts

import ReactMarkdown from "react-markdown";

<ChatProvider
  client={client}
  labels={ruLabels}                       // or your own strings
  components={{
    Markdown: ({ text }) => <ReactMarkdown>{text}</ReactMarkdown>,
    Button: MyDesignSystemButton,
    Message: ({ message }) => (           // wrap the default row
      <div className="row"><MessageItem message={message} /></div>
    ),
  }}
  attachmentRenderers={{
    product_link: ProductCard,            // add or override by attachment type
  }}
>
  • Markdown. The default shows plain text, so there is no markdown dependency and nothing to sanitize. Plug in react-markdown (it escapes HTML by default) if your agent answers with markdown. Do not render agent text with dangerouslySetInnerHTML.
  • Attachments. Built in: image, video, audio, voice, file, button (one button or an array of them), suggestion, citation, card, gallery, article_link (a report card: a link when the value has a url, otherwise just the title), question and chart (see below). Other types (product_link, checkout_link, payment_link, order_link, recipe_card, lead_form, deeplinks) have no default because they depend on your storefront: register a renderer. Unknown types are skipped, and one malformed attachment never breaks the conversation. All URLs coming from the agent go through safeUrl (only http, https, mailto, tel and relative links).
  • Questions. A question attachment renders as a card with one to four questions: single or multiple choice, plus a free-text "Other" unless allowOther: false. The answer goes out as the user's next message, in the format the agent expects (picked labels joined with , ; one header: answer line per question when there are several). The reply buttons that come with it for simpler clients (metadata.fallbackFor: "question") are hidden. Once the user has written anything after the question, the card is read-only, also after a reload. QuestionCard is exported if you want it outside the message list.
  • Charts. A chart attachment (line, area, bar, pie) is drawn as SVG, with no chart library. Bar charts take options.stacked and options.horizontal (with options.labelWidth); values are formatted with options.currency, options.percentage or options.unit; the grid hides with showGrid: false, and the legend shows for several named datasets or a pie (showLegend forces it either way). Hovering a bar, point or slice shows its value, and the values are also listed in a visually hidden table for screen readers. Colors come from the dataset (plain color values only) or from the --flutch-chart-* palette. The chart follows the width of the message bubble.
  • Thread list. ThreadList groups conversations by the day of their last activity (Today, Yesterday, Previous 7 days, Older), loads further pages with "Show more", shows the loading and empty states, and marks the conversation whose reply is being generated. A conversation gets its title when the server names it (thread_renamed) and moves to the top when a reply lands. Pass onSelect to react to a pick, e.g. to close a drawer on mobile. Renaming and deleting conversations are not in the API yet.
  • Layout. <Chat> is a convenience. Compose MessageList, Composer, ThreadList, ErrorBanner and EscalationNotice yourself for another layout, or read state with useChat(selector).

Hooks

const messages = useChat(s => s.messages);
const store = useChatStoreApi();
store.getState().send({ text: "Hello" }); // programmatic send

Full state and actions: see ChatStore in @flutchai/chat-core.

版本与发布记录

@flutchai/chat-react 共发布 11 个版本,首次发布于 2026/10/21;过去 12 个月发布了 11 个版本。

版本发布日期距今
0.1.9latest2026/11/0312 天前
0.1.82026/11/0114 天前
0.1.72026/11/0114 天前
0.1.62026/11/0114 天前
0.1.52026/11/0114 天前
0.1.42026/10/3115 天前

关于 @flutchai/chat-react 的常见问题

@flutchai/chat-react 是什么?

React chat UI for a Flutch agent: themeable with CSS variables, every part replaceable.

如何安装 @flutchai/chat-react?

在项目目录运行 npm install @flutchai/chat-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@flutchai/chat-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@flutchai/chat-react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@flutchai/chat-react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@flutchai/chat-react 可以商用吗?

@flutchai/chat-react 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@flutchai/chat-react 的下载量有多少?

根据 npm 官方下载统计,@flutchai/chat-react 在 2026/10/15 – 2026/11/13 这 30 天内被下载 2,034 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@flutchai/chat-react 还在维护吗?

最新版本 0.1.9 发布于 2026/11/03(12 天前);至今共发布 11 个版本,其中过去 12 个月发布了 11 个;首次发布于 2026/10/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。