> ## 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 消息。历史事件包含数字 `id`，该值在会话内单调递增。

## 通用结构

每个历史事件都具有以下结构：

```json theme={null}
{
  "id": 42,
  "type": "instruction.started",
  "time": "2026-10-28T08:00:24.123Z"
}
```

其余字段与 `id`、`type` 和 `time` 位于同一层级。

| 字段 | 含义 |
| - | - |
| `id` | 用于重连的事件游标。`state.snapshot` 没有历史事件 ID。 |
| `type` | 事件名称。 |
| `time` | 事件创建时间，采用 RFC 3339 格式。 |

## 会话生命周期

```mermaid theme={null}
stateDiagram-v2
  [*] --> starting
  starting --> live: session.ready
  live --> ending: session.ending
  ending --> ended: session.ended
  live --> ended: 立即停止或失败
```

### `session.ready`

开场媒体可以播放时发送。

```json theme={null}
{
  "id": 2,
  "type": "session.ready",
  "time": "2026-10-28T08:00:18.000Z",
  "playback": {
    "type": "hls",
    "url": "https://media.vivi-x.ai/streams/sw_example/master.m3u8"
  },
  "model": "vivix-w1-stream"
}
```

使用 `playback.url` 启动播放器。该播放 URL 属于本次会话，请勿自行拼接。

### `session.ending`

收尾指令或其他终止条件开始执行时发送。

```json theme={null}
{
  "id": 41,
  "type": "session.ending",
  "time": "2026-10-28T08:02:00.000Z",
  "reason": "stopped",
  "estimated_end_seconds": 128.0
}
```

收到该事件后，请停止发送新指令。会话进入结束流程后收到的消息可能会被取消。

### `session.ended`

会话进入终止状态时发送。

```json theme={null}
{
  "id": 47,
  "type": "session.ended",
  "time": "2026-10-28T08:02:08.000Z",
  "reason": "stopped",
  "end_seconds": 128.0
}
```

`reason` 可能为 `stopped`、`duration_limit`、`disconnect_timeout` 或 `error`。

## 指令生命周期

### `instruction.received`

```json theme={null}
{
  "id": 10,
  "type": "instruction.received",
  "time": "2026-10-28T08:00:30.000Z",
  "instruction_id": "ins_001",
  "client_request_id": "choice_001",
  "text": "The detective opens the hidden drawer.",
  "duration_seconds": 10,
  "estimated_start_seconds": 18.0
}
```

该指令已通过基本校验并进入会话队列。

### `instruction.scheduled`

```json theme={null}
{
  "id": 11,
  "type": "instruction.scheduled",
  "time": "2026-10-28T08:00:31.000Z",
  "instruction_id": "ins_001",
  "start_seconds": 18.0,
  "end_seconds": 28.0
}
```

这是指令当前在会话视频时间线上的位置估算。后续的 `instruction.scheduled` 事件可能会修正它。

### `instruction.started`

```json theme={null}
{
  "id": 18,
  "type": "instruction.started",
  "time": "2026-10-28T08:00:50.000Z",
  "instruction_id": "ins_001",
  "start_seconds": 18.0
}
```

会话播放位置已到达该指令的开始位置。

### `instruction.completed`

```json theme={null}
{
  "id": 26,
  "type": "instruction.completed",
  "time": "2026-10-28T08:01:00.000Z",
  "instruction_id": "ins_001",
  "start_seconds": 18.0,
  "end_seconds": 28.0
}
```

会话播放位置已到达该指令的结束位置。

### `instruction.cancelled` 和 `instruction.failed`

```json theme={null}
{
  "id": 29,
  "type": "instruction.cancelled",
  "time": "2026-10-28T08:01:04.000Z",
  "instruction_id": "ins_002",
  "reason": "session_ending"
}
```

```json theme={null}
{
  "id": 30,
  "type": "instruction.failed",
  "time": "2026-10-28T08:01:05.000Z",
  "instruction_id": "ins_003",
  "reason": "preparation_failed"
}
```

指令失败或取消后，不会应用与其开始或结束时间关联的状态变更。

## 资产事件

### `assets.added`

```json theme={null}
{
  "id": 34,
  "type": "assets.added",
  "time": "2026-10-28T08:01:10.000Z",
  "client_request_id": "asset_002",
  "assets": [
    {"asset_id":"asset_hidden_photo","name":"hidden_photo","type":"image"}
  ]
}
```

### `assets.failed`

```json theme={null}
{
  "id": 35,
  "type": "assets.failed",
  "time": "2026-10-28T08:01:11.000Z",
  "client_request_id": "asset_003",
  "error": {"code":"asset_preparation_failed","message":"could not prepare the requested assets"}
}
```

## 状态事件

### `state.updated`

一项或多项状态变更生效时发送。`state` 对象包含完整的当前状态。

```json theme={null}
{
  "id": 38,
  "type": "state.updated",
  "time": "2026-10-28T08:01:20.000Z",
  "state": {
    "characters": {
      "detective": {"hp": 85, "location": "warehouse"}
    },
    "clue_count": 1
  },
  "changes": [
    {
      "op": "set",
      "path": ["characters", "detective", "hp"],
      "value": 85,
      "instruction_id": "ins_001"
    }
  ],
  "media_seconds": 28.0
}
```

`changes` 可以包含用于关联的 `client_request_id` 和/或 `instruction_id`，但不包含内部处理来源信息。

### `state.snapshot`

仅返回给发送 `state.get` 的连接。该事件不属于事件历史，也没有 `id`。

```json theme={null}
{
  "type": "state.snapshot",
  "time": "2026-10-28T08:01:22.000Z",
  "client_request_id": "state_read_001",
  "state": {"clue_count": 1},
  "media_seconds": 28.0,
  "last_event_id": 38
}
```

## `error`

控制通道会以事件形式返回错误；如果请求提供了请求 ID，错误事件中也会包含该 ID。

```json theme={null}
{
  "id": 45,
  "type": "error",
  "time": "2026-10-28T08:01:40.000Z",
  "client_request_id": "state_004",
  "error": {
    "code": "invalid_request",
    "message": "invalid state update"
  }
}
```

应用行为请根据 `error.code` 决定。`message` 是简短的用户可读说明，内容可能会变化。

## 事件顺序与重连

* 按 `id` 递增顺序处理历史事件。
* 每处理完一个事件，都保存最后处理的 ID。
* 使用 `?last_event_id=<id>` 重连，以接收该游标之后的事件。
* 使用 `state.snapshot.last_event_id` 将某个时间点读取的状态与后续事件对齐。
* 安全地忽略未知事件类型，使客户端保持向前兼容。


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