> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react.md).

# 开始使用 React

在 React 应用中安装 Convai Web SDK，并添加一个现成的聊天和语音小组件，还可为你的角色选择性启用摄像头和屏幕共享。

使用以下内容将 Convai 角色添加到 React 应用： `useConvaiClient` hook 和 `ConvaiWidget` 组件。在首次设置 React 绑定时，请使用此页面。页面末尾会显示一个小部件按钮，点击后可与您的角色开启语音和文本对话。

### 先决条件

* 一个带有打包器的 React 18 或 19 应用。 `react` 18 或 19 是 `@convai/web-sdk/react`; `@convai/web-widget` （预构建 UI）也需要 `react-dom`.
* 一个 Convai 账户以及来自 <code class="expression">space.vars.dashboard\_url</code>.
* 要连接的角色 ID。

### 安装该包

React 绑定包含在 `@convai/web-sdk` 中，并从 `@convai/web-sdk/react` 子路径导入。没有单独的 React 包。

{% tabs %}
{% tab title="npm" %}

```bash
npm install @convai/web-sdk @convai/web-widget
```

{% endtab %}

{% tab title="pnpm" %}

```bash
pnpm add @convai/web-sdk @convai/web-widget
```

{% endtab %}

{% tab title="yarn" %}

```bash
yarn add @convai/web-sdk @convai/web-widget
```

{% endtab %}
{% endtabs %}

### 添加小部件

`useConvaiClient` 为该组件创建一个客户端并存储配置。 `ConvaiWidget` 渲染对话界面，并在有人首次选择其按钮时使用该已存储的配置进行连接，因此浏览器只会在用户操作之后请求麦克风权限。

{% code title="src/App.tsx" %}

```tsx
import { useConvaiClient } from '@convai/web-sdk/react';
import { ConvaiWidget } from '@convai/web-widget/react';

export default function App() {
  const convaiClient = useConvaiClient({
    apiKey: 'YOUR_API_KEY',
    characterId: 'YOUR_CHARACTER_ID',
    endUserId: 'user-123',
  });

  return <ConvaiWidget convaiClient={convaiClient} />;
}
```

{% endcode %}

该小部件会播放角色的声音，在语音模式下捕获麦克风，并在对话流式传输时显示对话内容。

`endUserId` 为可选项。请为每个人传入一个稳定且唯一的 ID，例如 UUID，以启用长期记忆和按用户分析。不传则会话为匿名。请参见 [使用长期记忆](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/long-term-memory.md).

{% hint style="danger" %}
客户端代码中的 API 密钥可被任何打开页面的人读取。对于公共页面，请在服务器上创建一个短期有效的身份验证令牌，并传入 `authToken` 而不是 `apiKey`时发送。参见 [使用认证令牌连接](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/auth-tokens.md).
{% endhint %}

### 启用摄像头和屏幕共享

设置 `enableVideo: true` 以连接到 `“video”` 连接类型，该类型会传输音频、摄像头视频和屏幕共享。 `startWithVideoOn` 连接时会开启摄像头；其默认值为 `false`。当连接类型为 `“video”`时，小部件会在其设置栏中显示摄像头和屏幕共享按钮；可通过以下方式隐藏任一按钮： `showVideo={false}` 或 `showScreenShare={false}`.

```tsx
const convaiClient = useConvaiClient({
  apiKey: 'YOUR_API_KEY',
  characterId: 'YOUR_CHARACTER_ID',
  enableVideo: true,
  startWithVideoOn: false,
});

return <ConvaiWidget convaiClient={convaiClient} showVideo showScreenShare />;
```

### 验证集成

启动开发服务器并打开页面。选择小部件按钮，允许麦克风访问，然后对角色说点什么。

{% hint style="success" %}
小部件会以语音模式打开，您的语音会作为消息显示，角色会以文本和语音进行回答。
{% endhint %}

### 后续步骤

<table data-view="cards"><thead><tr><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>useConvaiClient hook 参考</strong><br>该 hook 返回的所有内容，包括其仅限 React 的状态。</td><td><a href="/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react/useconvaiclient-hook.md">useConvaiClient Hook 参考</a></td></tr><tr><td><strong>ConvaiWidget 组件参考</strong><br>属性、默认值，以及小部件会代您执行的操作。</td><td><a href="/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react/convaiwidget.md">ConvaiWidget 组件参考</a></td></tr><tr><td><strong>在 React 中添加口型同步</strong><br>在渲染循环中根据 blendshape 流驱动 3D 面部。</td><td><a href="/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react/real-time-lipsync.md">在 React 中添加口型同步</a></td></tr><tr><td><strong>在 React 中构建自定义 UI</strong><br>用您自己的消息和控件替换该小部件。</td><td><a href="/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react/custom-ui-and-advanced-usage.md">在 React 中构建自定义 UI</a></td></tr><tr><td><strong>在 React 中添加音频可视化器</strong><br>使用 Web Audio API 绘制角色的声音级别。</td><td><a href="/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react/audio-visualizer-optional.md">在 React 中添加音频可视化器</a></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/react.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
