> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vivix.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 客户端消息

> 应用通过 Streaming World 控制 WebSocket 发送的消息。

客户端消息是发送到会话控制 WebSocket 的 JSON 指令。当前协议将本页与[服务端事件](https://docs.vivix.ai/cn/streaming-world/api-references/server-events.md)分开，因为两个方向承担不同职责：客户端请求执行操作，服务端报告哪些操作已接受、已调度、已开始、已完成或失败。

## 通用规则

* 每条 WebSocket 消息发送一个 JSON 对象。
* 重试同一个逻辑请求时，保持 `client_request_id` 不变。
* 使用相同 ID 和请求内容重复发送 `assets.add` 时，服务会重放原结果；相同 ID 对应不同请求内容时返回 `request_conflict`。
* `state.update` 请求被接受后，重复的请求 ID 会被忽略。`instruction.create` 不会去重，因此除非确实要创建另一条指令，否则不要重发。
* 忽略当前消息类型未列出的字段。
* 服务端事件使用另一套数字 `id`；重连时请使用该 ID。

## `instruction.create`

创建一段新的故事指引。它会影响尚未发布的内容。

```json theme={null}
{
  "type": "instruction.create",
  "client_request_id": "choice_001",
  "text": "The detective opens the hidden drawer, finds a torn photograph, and asks the witness who is missing from it.",
  "duration_seconds": 10,
  "state_ops": [
    {
      "op": "set",
      "path": ["clue_count"],
      "value": 1,
      "when": {"type": "instruction_end"}
    }
  ]
}
```

| 字段 | 必填 | 说明 |
| - | - | - |
| `type` | 是 | `instruction.create`。 |
| `client_request_id` | 建议提供 | 由应用生成的关联 ID。重试同一个请求时复用。 |
| `text` | **是** | 使用自然语言描述的故事指引。 |
| `duration_seconds` | 否 | 请求的视频时长。省略时使用服务默认值。 |
| `state_ops` | 否 | 附加到此指令的状态变更。 |

## `assets.add`

向进行中的会话添加图像或音频参考。收到 `assets.added` 后，整批资产才可用。

```json theme={null}
{
  "type": "assets.add",
  "client_request_id": "asset_002",
  "assets": [
    {
      "name": "hidden_photo",
      "type": "image",
      "file_id": "file_img_hidden_photo",
      "description": "A torn photograph found in the drawer"
    }
  ]
}
```

每个资产必须只包含一种来源：`file_id` 或 `url`。名称在会话内必须唯一；需要替换图像或声音时，请使用新名称。

## `state.update`

立即应用状态变更，或将其安排到已有指令的时间点。

```json theme={null}
{
  "type": "state.update",
  "client_request_id": "state_001",
  "ops": [
    {
      "op": "set",
      "path": ["characters", "detective", "hp"],
      "value": 85,
      "when": {"type": "immediate"}
    }
  ]
}
```

`ops` 作为一个批次接受。支持以下操作：

| `op` | 行为 |
| - | - |
| `set` | 在 `path` 创建或替换值。 |
| `delete` | 删除 `path` 处的值。 |
| `append` | 将 `value` 追加到 `path` 处的列表。 |
| `remove` | 从 `path` 处的列表中删除匹配值。 |

`when.type` 可以是 `immediate`、`instruction_start` 或 `instruction_end`。如果目标不是当前正在创建的指令，后两个值还需要提供 `instruction_id`。

## `state.get`

为当前 WebSocket 连接请求状态快照：

```json theme={null}
{"type":"state.get","client_request_id":"state_read_001"}
```

服务端通过 `state.snapshot` 响应。该响应仅发送给发起请求的连接，不会加入会话事件历史。

## `session.stop`

立即停止会话，或请求生成一条收尾指令。

```json theme={null}
{
  "type": "session.stop",
  "closing": {
    "text": "The detective stores the photograph and leaves before the storm clears.",
    "duration_seconds": 8
  }
}
```

立即停止：

```json theme={null}
{"type":"session.stop","immediate":true}
```

## 消息校验

无效消息会收到 `error` 事件。服务端会拒绝整条消息或整批操作，不会只应用无效 `state_ops` 或 `ops` 列表中的一部分。

| 情况 | 公开错误码 |
| - | - |
| 字段缺失或无效 | `invalid_request` |
| 会话正在结束 | `session_ending` |
| 会话已经结束 | `session_ended` |
| 排队指令过多 | `queue_full` |
| 无法准备资产 | `asset_preparation_failed` |
| 同一请求 ID 与另一个请求冲突 | `request_conflict` |

响应结构和生命周期事件请参阅[服务端事件](https://docs.vivix.ai/cn/streaming-world/api-references/server-events.md)。为兼容现有链接，本页路径仍为 `client-events`。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.