> 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/vanilla-typescript/events-and-message-handling.md).

# 事件与消息处理

## 事件

### 状态变化

```ts
client.on('stateChange', (state) => {
  console.log(state.agentState, state.isConnected);
});
```

### 新消息

```ts
client.on('message', (message) => {
  console.log('消息：', message.type, message.content);
});
```

### 消息已更新

```ts
client.on('messagesChange', (messages) => {
  console.log('消息总数：', messages.length);
});
```

### 实时转写

```ts
client.on('userTranscriptionChange', (text) => {
  console.log('你说了：', text);
});
```

### 生命周期事件

```ts
client.on('connect', () => console.log('已连接'));
client.on('disconnect', () => console.log('已断开连接'));
client.on('botReady', () => console.log('机器人已就绪'));
```

### 错误

```ts
client.on('error', (err) => {
  console.error('Convai 错误：', err);
});
```

***

## 消息类型

Convai 消息包括：

* `user-transcription`
* `bot-llm-text`
* `bot-emotion`
* `action`
* `behavior-tree`

只有部分会在 UI 中显示（通常是转写内容 + 机器人文本）。

***

## 移除监听器

```ts
const unsub = client.on('message', handler);
unsub(); // 移除监听器
```


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/vanilla-typescript/events-and-message-handling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
