> ## 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 会话的 HTTP 接口。

一个会话就是一条持续生成的故事。通过 HTTP 启动，通过 WebSocket 控制，再通过 HTTP 查询最终时间线。

> [!NOTE]
>
> HTTP 响应使用 API 平台统一的 `{code, message, data}` 封装。下文描述的接口数据位于 `data` 中。

## 接口

| 方法 | 路径 | 用途 |
| - | - | - |
| `POST` | `/v1/streaming-world/sessions` | 启动会话。 |
| `GET` | `/v1/streaming-world/sessions/{session_id}` | 读取状态、最终视频信息和时间线。 |
| `POST` | `/v1/streaming-world/sessions/{session_id}/stop` | 立即停止会话，或请求生成收尾场景。 |

## 创建会话

**POST** `/v1/streaming-world/sessions`

该接口会准备请求中的资产并启动视频会话。成功时返回 `201 Created`。

### 请求字段

**model** `string`

公开模型名称。请使用 `vivix-w1-stream`。省略时，服务使用默认公开模型。

**instructions** `string` *required*

初始故事设定。请使用自然语言描述场景、角色、基调、视觉方向、开场动作和连续性规则。

**assets** `array`

会话的命名资产。每一项必须在 `file_id` 和 `url` 中选择一种来源。

**资产项**

```json theme={null}
{
  "name": "detective",
  "type": "image",
  "file_id": "file_img_detective",
  "description": "The detective's face and coat"
}
```

| 字段 | 必填 | 说明 |
| - | - | - |
| `name` | 是 | 本次会话内唯一的名称。 |
| `type` | 是 | `image` 或 `audio`。 |
| `file_id` | 与 `url` 二选一 | 已上传的文件。 |
| `url` | 与 `file_id` 二选一 | HTTP 或 HTTPS 来源 URL。 |
| `description` | 否 | 资产用途的简短说明。 |

**opening\_image** `object`

可选的视觉起点。先通过 `POST /v1/files/images` 上传图像，再将返回的 `data.url` 传入此处。该对象只包含一个 `url` 字段。

**aspect\_ratio** `string`

`16:9`、`9:16` 或 `1:1`。默认为 `16:9`。

**max\_duration\_seconds** `integer`

视频时长上限。默认为 600 秒，当前最大值为 3600 秒。

**state** `object`

初始应用状态。用于保存客户端需要展示或更新的数值和结构化规则。该对象必须满足请求大小限制。

### 请求示例

```bash theme={null}
curl -X POST https://api.vivix.ai/v1/streaming-world/sessions \\
  -H "Authorization: Bearer $VIVIX_API_KEY" \\
  -H "Content-Type: application/json" \\
  -d '{
    "model": "vivix-w1-stream",
    "instructions": "A detective investigates a locked room during a storm. Use Chinese dialogue and keep both characters consistent.",
    "opening_image": {"url": "https://static.vivi-x.ai/images/2026/08/03/650665979f5d095f.jpg"},
    "aspect_ratio": "16:9",
    "max_duration_seconds": 120,
    "state": {
      "detective": {"hp": 100},
      "witness": {"hp": 100}
    }
  }'
```

### 响应示例

```json theme={null}
{
  "code": 0,
  "message": "success",
  "data": {
    "id": "sw_example",
    "model": "vivix-w1-stream",
    "status": "starting",
    "control": {
      "url": "wss://sw.vivix.ai/v1/streaming-world/sessions/sw_example/control",
      "client_secret": "session_secret_example",
      "expires_at": "2026-10-28T08:00:00Z"
    },
    "assets": []
  }
}
```

**响应字段**

| 字段 | 说明 |
| - | - |
| `id` | 不透明的会话 ID。 |
| `model` | 本次会话选择的公开模型名称。 |
| `status` | `starting`、`live`、`ending` 或 `ended`。 |
| `control.url` | 本次会话的 WebSocket URL。 |
| `control.client_secret` | 会话专属的控制凭证。 |
| `control.expires_at` | 控制凭证的过期时间。 |
| `assets` | 已准备资产的不透明 `asset_id`、名称和类型。 |

## 查询会话

**GET** `/v1/streaming-world/sessions/{session_id}`

返回最近一次持久化的会话状态。重连后或收到 `session.ended` 后，可通过该接口获取最终时间线。

### 响应字段

| 字段 | 说明 |
| - | - |
| `id` | 会话 ID。 |
| `model` | 公开模型名称。 |
| `status` | 当前状态。 |
| `ended_reason` | 会话结束时为 `stopped`、`duration_limit`、`disconnect_timeout` 或 `error`。 |
| `created_at` | 会话创建时间。 |
| `ended_at` | 结束时间（如有）。 |
| `video.status` | `pending`、`processing`、`completed` 或 `failed`。 |
| `video.url` | 完整视频可用时提供。 |
| `timeline` | 包含视频时间位置和说明的故事条目。当一条指令仍在准备时，服务可能先播放一个延续当前镜头但不推进故事的短暂过渡片段；该条目没有 `instruction_id`，说明为 `过渡`。 |

```json theme={null}
{
  "code": 0,
  "message": "success",
  "data": {
    "id": "sw_example",
    "model": "vivix-w1-stream",
    "status": "ended",
    "ended_reason": "stopped",
    "created_at": "2026-10-28T08:00:00Z",
    "ended_at": "2026-10-28T08:02:08Z",
    "video": {"status": "completed", "url": "https://media.vivi-x.ai/videos/sw_example.mp4"},
    "timeline": [
      {
        "start_seconds": 0,
        "end_seconds": 8,
        "instruction_id": "",
        "instruction_text": "",
        "description": "The detective approaches the locked room."
      }
    ]
  }
}
```

> [!NOTE]
>
> `video` 对象异步生成。`pending` 和 `processing` 都是正常的中间状态；仅在 `video.url` 存在时使用该 URL。

## 停止会话

**POST** `/v1/streaming-world/sessions/{session_id}/stop`

请求被接受时返回 HTTP `202`。

### 立即停止

```bash theme={null}
curl -X POST "https://api.vivix.ai/v1/streaming-world/sessions/sw_example/stop" \\
  -H "Authorization: Bearer $VIVIX_API_KEY" \\
  -H "Content-Type: application/json" \\
  -d '{"immediate":true}'
```

### 生成收尾场景后停止

```json theme={null}
{
  "closing": {
    "text": "The detective secures the evidence and leaves the room.",
    "duration_seconds": 8
  }
}
```

通过控制通道监听 `session.ending` 和 `session.ended`。

## HTTP 错误

| HTTP | `code` | 含义 |
| - | - | - |
| `400` | `invalid_request` | 请求结构或参数值无效。 |
| `401` | `unauthorized` | API 密钥或请求凭证无效。 |
| `404` | `not_found` | 会话不存在或当前请求无权访问。 |
| `429` | `concurrency_limit` | 当前账户暂时无法再启动新会话。 |
| `503` | `capacity_unavailable` | 视频容量暂时不可用，请稍后重试。 |
| `502` | `upstream_failed` | 无法启动会话。 |
| `500` | `internal_error` | 服务无法完成请求。 |

程序逻辑请使用稳定的 `code`。将 `message` 视为展示文本，不要解析它来决定路由或重试策略。


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