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

# W1: Streaming World

> 创建持续生成的故事，连接控制通道并播放生成结果。

Streaming World 让应用能够启动一个持续生成的故事，并在视频播放过程中引导后续情节。

你会使用三类公开接口：

| 接口 | 用途 |
| - | - |
| **HTTP** | 启动、查询和停止会话。 |
| **WebSocket** | 发送故事指令、添加参考素材、更新世界状态并接收进度事件。 |
| **HLS 播放** | 第一段可播放媒体准备好后，播放服务返回的视频。 |

```mermaid theme={null}
flowchart LR
  C[客户端]
  H[HTTP 会话 API]
  W[WebSocket 控制通道]
  P[HLS 播放器]
  C -->|API 密钥| H
  H -->|会话 ID + 控制密钥| C
  C <-->|指令、状态、事件| W
  W -->|播放 URL| P
```

## 从一个会话开始

一个会话包含一条连续的故事线。使用自然语言设定创建会话，然后保存返回的 `id` 和 `control` 对象。

**1. 上传图像素材**

先将图像上传到工作区。保存响应中的 `file_id` 和 `url`：命名资产使用 `file_id`，`opening_image` 使用返回的 `url`。

```bash theme={null}
curl -X POST https://api.vivix.ai/v1/files/images \\
  -H "Authorization: Bearer $VIVIX_API_KEY" \\
  -F "file=@locked-room.jpg"
```

```json theme={null}
{
  "code": 0,
  "message": "success",
  "data": {
    "file_id": "file_img_locked_room",
    "file_type": "image",
    "mime_type": "image/jpeg",
    "url": "https://static.vivi-x.ai/images/2026/08/03/650665979f5d095f.jpg"
  }
}
```

音频资产请使用对应的上传接口，并保存返回的 `file_id`：

```bash theme={null}
curl -X POST https://api.vivix.ai/v1/files/audios \
  -H "Authorization: Bearer $VIVIX_API_KEY" \
  -F "file=@witness-voice.wav"
```

**2. 创建会话**

```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. Keep the detective and the witness visually consistent, use Chinese dialogue, and begin outside the room.",
    "aspect_ratio": "16:9",
    "max_duration_seconds": 120
  }'
```

**3. 连接控制通道**

使用 `control.url` 和会话专属的 `control.client_secret` 建立 WebSocket 连接。该密钥仅用于本次会话，请妥善保管。

**4. 加载播放器**

等待 `session.ready`。其中的 `playback.url` 是 HLS 地址。将该 URL 交给支持 HLS 的播放器并开始播放。

## 创建请求

```json theme={null}
{
  "model": "vivix-w1-stream",
  "instructions": "A detective investigates a locked room during a storm. Keep the detective and the witness visually consistent, use Chinese dialogue, and begin outside the room.",
  "opening_image": {
    "url": "https://static.vivi-x.ai/images/2026/08/03/650665979f5d095f.jpg"
  },
  "assets": [
    {
      "name": "detective",
      "type": "image",
      "file_id": "file_img_detective",
      "description": "The detective's face and coat"
    },
    {
      "name": "witness_voice",
      "type": "audio",
      "file_id": "file_aud_witness",
      "description": "The witness's voice reference"
    }
  ],
  "aspect_ratio": "16:9",
  "max_duration_seconds": 120,
  "state": {
    "characters": {
      "detective": {"hp": 100, "location": "outside the room"},
      "witness": {"hp": 100, "location": "inside the room"}
    },
    "clue_count": 0
  }
}
```

| 字段 | 必填 | 说明 |
| - | - | - |
| `model` | 否 | 公开模型名称。请使用 `vivix-w1-stream`。省略时，服务使用默认公开模型。 |
| `instructions` | **是** | 初始故事设定。请描述世界、角色、基调、视角、开场动作，以及需要始终保持一致的规则。 |
| `opening_image.url` | 否 | `POST /v1/files/images` 返回的 `url`。服务会从已上传文件中获取开场图像。 |
| `assets` | 否 | 故事后续可能使用的命名图像和音频参考。会话创建成功后，这些参考即可使用。 |
| `aspect_ratio` | 否 | `16:9`、`9:16` 或 `1:1`。默认为 `16:9`。 |
| `max_duration_seconds` | 否 | 会话的视频时长上限。默认为 600 秒；当前最大值为 3600 秒。 |
| `state` | 否 | 用于保存应用状态的 JSON 对象，例如生命值、分数、位置、物品栏或任务进度。 |

> [!NOTE]
>
> 生成细节由服务选择。此 API 不提供公开的推理参数对象。

## 参考素材与开场图像

开场图像回答的是“**故事在视觉上应该从哪里开始？**”；命名资产回答的是“**后续故事片段应该引用哪张图像或哪段声音？**”。两者是不同的输入，可以同时使用。

资产既可以在第一帧生成前注册，也可以在播放期间添加。会话资产池可以包含多于单个生成片段所需数量的参考素材。服务会为每个片段选择需要的参考素材；应用应使用稳定的名称和说明，以便故事能够自然地引用它们。

每个资产只能使用一种来源：

| 来源 | 示例 |
| - | - |
| 已有文件 | `{"file_id":"file_img_detective"}` |
| 直接 URL | 可公开访问的 HTTP(S) URL。本页示例使用上传接口返回的 URL。 |

公开响应会返回不透明的 `asset_id`、名称和类型，不会返回来源 URL 或存储信息。

## 初始状态与数值规则

使用 `state` 保存应用需要展示或强制执行的值。例如，格斗游戏可以让双方角色都以 100 点生命值开始，再将一次状态更新附加到描述命中的指令上。

服务会在每个 `state.updated` 事件中发送完整的当前状态。客户端可以直接用该对象替换本地副本，无需尝试根据单次变更重建状态。

> [!WARNING]
>
> 状态对象属于应用数据，服务不会推断其数据结构。如果使用 `hp`，请在每次更新中保持路径和数据类型一致。

## 响应

创建成功时返回 HTTP `201`：

```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": [
      {"asset_id": "asset_detective", "name": "detective", "type": "image"}
    ]
  }
}
```

| 响应字段 | 用途 |
| - | - |
| `data.id` | 用于查询或停止会话。 |
| `data.status` | 通常先为 `starting`，之后变为 `live`、`ending` 或 `ended`。 |
| `data.control.url` | 本次会话的 WebSocket 端点。 |
| `data.control.client_secret` | 控制连接使用的密钥。不要将其暴露在日志或其他用户可以访问的 URL 中。 |
| `data.assets` | 后续指令可以使用的不透明资产 ID。 |

## 接下来会发生什么

1. 服务准备开场媒体。
2. 控制通道通过 `session.ready` 发送 HLS 播放 URL。
3. 客户端启动播放器，并开始监听指令和状态事件。
4. 新指令会影响后续故事内容；已经发布的视频不会被改写。

接下来请阅读 [Streaming World 控制](https://docs.vivix.ai/cn/streaming-world/control.md)，或查看完整的[会话 API 参考](https://docs.vivix.ai/cn/streaming-world/api-references/sessions.md)。

## 完整示例

```javascript theme={null}
const createResponse = await fetch(
  "https://api.vivix.ai/v1/streaming-world/sessions",
  {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${process.env.VIVIX_API_KEY}`,
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      model: "vivix-w1-stream",
      instructions: "A detective investigates a locked room during a storm. Use Chinese dialogue and keep the characters consistent.",
      aspect_ratio: "1:1",
      max_duration_seconds: 120,
      state: {
        detective: { hp: 100, location: "hallway" },
        witness: { hp: 100, location: "room" }
      }
    })
  }
);

const envelope = await createResponse.json();
const session = envelope.data;
console.log(session.id, session.status);
```


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