> 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/actions.md).

# 处理角色动作

声明角色可以使用的动作、对象和工具，接收它选择的动作，并返回你应用运行的工具结果。

让角色在你的场景中行动：在连接时声明它能做什么，并在对话过程中处理它所选择的动作。若角色需要移动、拾取物品或调用你应用中的函数，请使用此页面。你的应用仍负责授权并执行每一个动作。

该 SDK 支持两种契约。语义动作是默认方式，返回带名称的动作及可选目标。客户端工具需要显式启用，返回带类型的工具调用，由你的应用以结果进行响应。

### 前提条件

* 已安装 Convai Web SDK，并且有可连接的客户端。参见 [Web SDK 快速入门](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/quickstart.md).
* 你的场景支持的动作、对象和角色名称。

### 声明语义动作

传入 `actionConfig` 在创建客户端时。相同选项也适用于 `new ConvaiClient(...)` 和 `useConvaiClient(...)`.

```ts
import { ConvaiClient } from '@convai/web-sdk/core';

const client = new ConvaiClient({
  apiKey: 'YOUR_API_KEY',
  characterId: 'YOUR_CHARACTER_ID',
  actionConfig: {
    actions: ['Move To', 'Pick Up', 'Drop', 'Wave'],
    objects: [
      { name: 'fire extinguisher', description: '安装在门旁的红色灭火器' },
      { name: 'valve', description: '北墙上的燃气截止阀' },
    ],
    characters: [
      { name: 'Trainee', bio: '接受安全演练的人' },
    ],
    current_attention_object: 'valve',
  },
});
```

`actions`, `objects`，以及 `characters` 必须位于 `actionConfig`. `current_attention_object` 是可选的，且必须与 `objects`中的一个名称匹配。配置会在会话连接时发送，因此要更改它请重新连接。

### 接收语义动作

订阅 `actionResponse`。每个事件按顺序携带一轮中的动作。

```ts
import type { ActionResponseEvent } from '@convai/web-sdk/core';

client.on('actionResponse', ({ actions }: ActionResponseEvent) => {
  for (const action of actions) {
    if (action.target) {
      // 带参数："Move To" -> "valve"
      runTargetedAction(action.name, action.target);
    } else {
      // 简单："Wave"
      playAnimation(action.name);
    }
  }
});
```

* `name` 是来自 `actionConfig.actions`.
* `目标` 当角色对某个特定对象或角色执行动作时会出现；对于简单动作则不会出现。应基于 `目标`进行分支，而不是基于动作名称：同一个动作可能带着它出现，也可能不带。
* 一个空的 `actions` 数组也是一个没有动作的有效轮次。
* SDK 不会运行或调度动作。它还会在聊天消息中添加一条 `动作` 记录，并用动作名称作为标签。

像“拾起灭火器并把它带给受训者”这样的请求，可能会在一个事件中产生多个动作：

```json
{
  "actions": [
    { "name": "Move To", "target": "fire extinguisher" },
    { "name": "Pick Up", "target": "fire extinguisher" },
    { "name": "Move To", "target": "Trainee" },
    { "name": "Drop", "target": "Trainee" }
  ]
}
```

### 更新角色当前关注的对象

使用 `updateContext`告诉角色当前聚焦的是哪个对象，这样它就能解析“it”或“that”等词。该值必须与 `actionConfig.objects`中的一个名称匹配，空字符串会清除它。

```ts
// 静默更改焦点
client.updateContext({ current_attention_object: 'fire extinguisher', run_llm: 'false' });

// 更改焦点，并让 Convai 决定是否回应
client.updateContext({
  text: '受训者现在正看着阀门。',
  current_attention_object: 'valve',
  run_llm: 'auto',
});

// 清除焦点
client.updateContext({ current_attention_object: '', run_llm: 'false' });
```

若要描述环境但不将其变为可操作项，请使用 `updateSceneMetadata`。它不会改变 `actionConfig`，因此以这种方式发送的项目永远不会成为动作目标。参见 [在运行时更新角色上下文](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/dynamic-context.md).

### 无需输入即可触发回应

`sendTriggerMessage(triggerName?, triggerMessage?)` 让角色在没有来自应用使用者消息的情况下作出回应，例如在演练开始时。两个参数都是可选的。

```ts
client.sendTriggerMessage('drill_start');
client.sendTriggerMessage(undefined, '请让受训者找到最近的灭火器。');
```

### 启用客户端工具

客户端工具添加了带类型的参数和关联结果。请在 `capabilities`中请求 action 协议版本 2 和模型输出版本 2，并在 `actionConfig.tools`中声明这些工具。省略 `capabilities` 则会保留语义动作契约。

```ts
const client = new ConvaiClient({
  apiKey: 'YOUR_API_KEY',
  characterId: 'YOUR_CHARACTER_ID',
  capabilities: {
    actionProtocolVersion: 2,
    modelOutputVersion: 2,
  },
  actionConfig: {
    actions: [],
    objects: [],
    characters: [],
    tools: [
      {
        name: 'open_training_record',
        description: '在受训者确认后打开培训记录。',
        inputSchema: {
          type: 'object',
          properties: { recordId: { type: 'string' } },
          required: ['recordId'],
        },
      },
    ],
  },
});
```

`capabilities` 包含三个字段：

| 字段                      | 值                    | 省略时                       |
| ----------------------- | -------------------- | ------------------------- |
| `actionProtocolVersion` | `1` 或 `2`            | 语义动作                      |
| `modelOutputVersion`    | `1` 或 `2`            | 旧版输出消息，无 `modelOutput` 事件 |
| `botLlmTextMode`        | `'legacy'` 或 `'raw'` | 中的过滤文本 `bot-llm-text` 消息  |

`'raw'` 会切换 `bot-llm-text` 聊天消息到动作解析前的完整提供方输出。它是聊天文本，不是工具调用流：切勿从中解析或运行动作。

Convai 会在其连接响应中确认所选能力。 `connect()` 当所选内容与请求不匹配时会拒绝：

| 错误                                    | 原因                 |
| ------------------------------------- | ------------------ |
| `Core 未选择请求的 action 协议版本 2`           | 已请求版本 2，但未获授权。     |
| `Core 在未明确启用客户端的情况下选择了 action 协议版本 2` | 未请求却授予了版本 2。       |
| `Core 未选择请求的模型输出版本 2`                 | 已请求模型输出版本 2，但未获授权。 |
| `Core 未选择请求的原始机器人 LLM 文本模式`           | `'raw'` 已请求，但未获授权。 |

声明会在连接时发送，因此要更改工具请重新连接。

### 处理模型输出和工具调用

使用 `modelOutputVersion: 2`，订阅 `modelOutput`。每个事件都是一个已验证的信封，其 `条目` 按顺序包含消息、情绪、语义动作、工具调用和扩展。SDK 会抑制 `actionResponse` 在此模式下的重复处理，以免同一操作被处理两次；并会丢弃其 `output_id` 已投递过的信封。

```ts
import type {
  ActionResult,
  ModelOutputMessage,
  ModelOutputProtocolError,
  ModelOutputToolCallItem,
} from '@convai/web-sdk/core';

client.on('modelOutput', (output: ModelOutputMessage) => {
  for (const item of output.items) {
    if (item.type === 'message') renderMessage(item.content);
    if (item.type === 'semantic_action') runTargetedAction(item.name, item.target);
    if (item.type === 'tool_call') void runTool(item);
  }
});

client.on('modelOutputProtocolError', (error: ModelOutputProtocolError) => {
  console.error(error.code, error.message);
});

async function runTool(call: ModelOutputToolCallItem) {
  let result: ActionResult;
  try {
    // 在这里检查权限并请求确认。
    const output = await openTrainingRecord(call.arguments);
    result = { id: call.id, status: 'completed', output };
  } catch (error) {
    result = {
      id: call.id,
      status: 'error',
      error: { message: error instanceof Error ? error.message : '工具失败' },
    };
  }
  client.sendActionResult(result);
}
```

仅渲染并运行已验证的 `条目`. `原始` 保存提供方输出用于诊断，且绝不能执行。若信封格式不正确或包含不支持的条目，SDK 会拒绝整个信封并发出 `modelOutputProtocolError` ，代码为 `invalid_model_output`.

多个信封可以共享同一个 `logical_turn_id`，以及 `final: true` 它只会关闭一个信封，而不是整个轮次。SDK 不会跟踪待处理调用、在多个信封之间去重工具调用 ID、重试结果，也不保证恰好执行一次。请在你的应用中负责这些记录。

### 返回工具结果

`sendActionResult(result)` 验证结果并将其发送给 Convai。它不返回任何内容；没有异常的返回意味着结果已发送，而不是已被接受。

| 字段                   | 必填 | 说明                                        |
| -------------------- | -- | ----------------------------------------- |
| `id`                 | 是  | 此处的 `id` 属于该工具调用。                         |
| `状态`                 | 是  | `'completed'`, `'error'`，或 `'cancelled'`. |
| `输出`                 | 否  | 有限的 JSON 数据。                              |
| `error`              | 否  | 有限的 JSON 数据。不能与 `'completed'`.            |
| `characterSessionId` | 否  | 默认为当前会话。                                  |

当结果无效时，会在发送前抛出错误：

| 错误                               | 原因                                   |
| -------------------------------- | ------------------------------------ |
| `在会话就绪前无法发送操作结果`                 | 传输未连接。                               |
| `需要 action result id`            | `id` 为空。                             |
| `不支持的 action result 状态`          | `状态` 不是这三个值之一。                       |
| `已完成的 action result 不能包含 error`  | `状态` is `'completed'` 和 `error` 已设置。 |
| `action result 输出只能包含有限的 JSON 值` | `输出` 包含非 JSON 值。                     |
| `action result 错误只能包含有限的 JSON 值` | `error` 包含非 JSON 值。                  |

Convai 会通过以下内容确认结果： `serverResponse` 事件进行确认，其 `事件类型` is `'action-result'`。检查其 `状态` 和 `消息`.

```ts
client.on('serverResponse', (response) => {
  if (response.event_type === 'action-result' && response.status === 'error') {
    console.error('结果被拒绝：', response.message);
  }
});
```

若要将一个带类型的轮次与其输出关联，请传入 `sendUserTextMessage(text, { logicalTurnId })`。该 ID 不能为空，且最多为 128 个 UTF-8 字节，否则该方法会抛出错误 `logicalTurnId 必须非空且最多 128 个 UTF-8 字节`.

### 后续步骤

{% content-ref url="/pages/4c37a8e73c580fc9f3ebe503a43373bd3551a22e" %}
[在运行时更新角色上下文](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/dynamic-context.md)
{% endcontent-ref %}

{% content-ref url="/pages/8c6e3631e7e7da5b3c9acf5a0019d7737dc79e19" %}
[Web SDK 事件参考](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/event-reference.md)
{% endcontent-ref %}

{% content-ref url="/pages/dff296725774f9173a7391e28344f5f94cf35b60" %}
[配置和类型参考](/api-docs/zh/cha-jian-yu-ji-cheng/web-plugins/convai-web-sdk/vanilla-typescript/best-practices-and-type-definitions.md)
{% endcontent-ref %}

{% content-ref url="/pages/70d752d330612b0fbf1290588e718236d6a18f0e" %}
[连接 API](/api-docs/zh/api-can-kao/core-api-reference/live-apis-beta/connect-api.md)
{% endcontent-ref %}

{% content-ref url="/pages/138cedadc568671d93b18cf90042de5055cbffd5" %}
[客户端到服务器消息](/api-docs/zh/api-can-kao/core-api-reference/live-apis-beta/client-to-server-messages.md)
{% endcontent-ref %}


---

# 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/actions.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.
