@vivix-ai/ivi-frontend-sdk 0.3.7 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  用于前端应用集成 IVI 实时能力的 TypeScript SDK,包含:
4
4
 
5
5
  - 基于 `@vivix-ai/ivi-sdk-ts` 的 runtime 协调层
6
+ - 通过 API 网关创建 IVI Session 的 HTTP 辅助方法
6
7
  - 面向舞台渲染的 React 组件与 hooks
7
8
 
8
9
  ## 安装依赖
@@ -111,6 +112,108 @@ const runtime = sdk.createRuntimeCoordinator(
111
112
  await runtime.start();
112
113
  ```
113
114
 
115
+ ### 1.1)通过 API 网关创建 IVI Session 并启动 runtime
116
+
117
+ 如果业务侧已经接入 API 网关,可直接使用 SDK 内置的 `createIVISession` HTTP 封装。该方法会调用 `POST /v1/ivi/sessions`,拿到 `ivi_session_id` / `endpoint` 后创建 WebSocket runtime。
118
+
119
+ 当创建会话响应里包含 `prebuild_trtc_info` 时,SDK 会把它转换成一个临时的 bootstrap source,并在 websocket 真实 `stage/track/source` 事件到达前,提前通过 `IVITrackSlot slot="main"` 展示画面。真实 source ready 后会自动接管,业务 UI 不需要区分“快速开播”和“常规开播”。
120
+
121
+ ```ts
122
+ import {
123
+ createIVISession,
124
+ createRuntimeFromSession
125
+ } from "@vivix-ai/ivi-frontend-sdk";
126
+
127
+ const session = await createIVISession(
128
+ {
129
+ idempotencyKey: crypto.randomUUID(),
130
+ streamType: "TRTC",
131
+ iviVersion: "dev",
132
+ sessionParameters: {
133
+ resolution: "720p"
134
+ },
135
+ prebuiltCharacters: [
136
+ {
137
+ characterId: "character_claire",
138
+ characterPayload: characterJson
139
+ }
140
+ ],
141
+ prebuiltStream: {
142
+ streamId: "stream_claire",
143
+ mode: "character",
144
+ streamConfig: {
145
+ scene_description: "Claire on stage",
146
+ character_ids: ["character_claire"],
147
+ realtime_cvi_config: { enabled: false }
148
+ }
149
+ }
150
+ },
151
+ {
152
+ baseUrl: "https://api.example.com",
153
+ headers: async () => ({
154
+ Authorization: `Bearer ${token}`
155
+ })
156
+ }
157
+ );
158
+
159
+ const { runtime } = createRuntimeFromSession(session, {
160
+ fastStart: true,
161
+ bootstrapSlot: "main",
162
+ websocket: {
163
+ query: {
164
+ x_auth_jwt: authJwt
165
+ },
166
+ protocols: authJwt ? ["ivi", authJwt] : undefined
167
+ }
168
+ });
169
+
170
+ await runtime.start();
171
+ ```
172
+
173
+ 也可以一步完成创建 session 与 runtime:
174
+
175
+ ```ts
176
+ import { createIVISessionRuntime } from "@vivix-ai/ivi-frontend-sdk";
177
+
178
+ const { session, runtime } = await createIVISessionRuntime(request, {
179
+ http: {
180
+ baseUrl: "https://api.example.com",
181
+ headers: { Authorization: `Bearer ${token}` }
182
+ },
183
+ fastStart: true,
184
+ bootstrapSlot: "main"
185
+ });
186
+
187
+ await runtime.start();
188
+ ```
189
+
190
+ `createIVISession` 返回值会保留原始 response,同时提供 SDK 归一化字段:
191
+
192
+ | 字段 | 类型 | 说明 |
193
+ |------|------|------|
194
+ | `iviSessionId` | `string` | 会话 ID,对应后端 `ivi_session_id` |
195
+ | `endpoint` | `string` | IVI realtime websocket 地址 |
196
+ | `trtcInfo` / `livekitInfo` | — | 会话级播放/传输信息(如果后端返回) |
197
+ | `prebuildTrtcInfo` | — | 后端 `prebuild_trtc_info` 的 camelCase 视图 |
198
+ | `prebuiltPlayback` | — | SDK 归一化后的快速开播播放源;存在时可用于 bootstrap source |
199
+ | `raw` | `unknown` | 原始 HTTP response body |
200
+ | `requestBody` | `Record<string, unknown>` | 实际发给网关的 snake_case JSON |
201
+
202
+ `IviCreateIVISessionRequest` 使用 camelCase 字段,SDK 会在发送前转换为 CP transcoder 需要的 snake_case JSON:
203
+
204
+ | SDK 字段 | HTTP 字段 |
205
+ |----------|-----------|
206
+ | `idempotencyKey` | `idempotency_key` |
207
+ | `streamType` | `stream_type` |
208
+ | `iviVersion` | `ivi_version` |
209
+ | `sessionParameters` | `session_parameters` |
210
+ | `prebuiltCharacters` | `prebuilt_characters` |
211
+ | `prebuiltStream` | `prebuilt_stream` |
212
+ | `enableDecoderPublish` | `enable_decoder_publish` |
213
+ | `sessionRecording` | `session_recording` |
214
+
215
+ > 浏览器侧不应依赖 TRTC `secret_key`。SDK 在归一化 TRTC 信息时会丢弃 `secret_key` / `secretKey`,播放器只使用拉流所需的 `app_id`、`room_id`、`user_id`、`user_sig`。
216
+
114
217
  **使用自定义 WebSocket 运行时**(Node.js / 自定义环境):
115
218
 
116
219
  ```ts
@@ -219,6 +322,7 @@ runtime.sendSessionSourcePlaybackCompleted("source_001", "track_001");
219
322
  | `sources` | `Map<string, IviRuntimeSource>` | 所有 source(含 `status: created \| ready \| failed`) |
220
323
  | `conversationItems` | `Map<string, IviRuntimeConversationItem>` | 对话条目映射 |
221
324
  | `conversations` | `IviRuntimeConversationItem[]` | 对话有序列表 |
325
+ | `bootstrap` | `IviRuntimeBootstrapViewState \| null` | prebuild 快速开播的临时 slot/track/source 绑定状态 |
222
326
 
223
327
  ---
224
328
 
@@ -298,6 +402,59 @@ export function AutoStagePage({ livekitToken }: { livekitToken: string }) {
298
402
  3. 浏览器 LiveKit 场景可传 `LiveKitBrowserTransport`
299
403
  4. WebSocket、测试 fake transport 或其它 transport 也通过同一个 `transport` 字段传入
300
404
 
405
+ ### 方式 C:使用 `useIviSessionRuntime` 创建会话并管理 runtime
406
+
407
+ 当希望把 `createIVISession` 也放进 SDK 接入流程时,可以使用 `useIviSessionRuntime`。它会在挂载后创建 IVI Session、构造 WebSocket runtime,并在 `autoStart` 为 `true` 时自动启动。
408
+
409
+ ```tsx
410
+ import {
411
+ IVIStageView,
412
+ IVITrackSlot,
413
+ useIviSessionRuntime,
414
+ type IviCreateIVISessionRequest
415
+ } from "@vivix-ai/ivi-frontend-sdk";
416
+
417
+ export function FastStartStagePage({
418
+ request,
419
+ token,
420
+ authJwt
421
+ }: {
422
+ request: IviCreateIVISessionRequest;
423
+ token: string;
424
+ authJwt?: string;
425
+ }) {
426
+ const { runtime, session, status, error } = useIviSessionRuntime({
427
+ request,
428
+ http: {
429
+ baseUrl: "https://api.example.com",
430
+ headers: {
431
+ Authorization: `Bearer ${token}`
432
+ }
433
+ },
434
+ fastStart: true,
435
+ bootstrapSlot: "main",
436
+ websocket: {
437
+ query: {
438
+ x_auth_jwt: authJwt
439
+ },
440
+ protocols: authJwt ? ["ivi", authJwt] : undefined
441
+ }
442
+ });
443
+
444
+ if (error) return <div>{error.message}</div>;
445
+
446
+ return (
447
+ <IVIStageView runtime={runtime}>
448
+ <IVITrackSlot slot="main" showVolumeControl showSubtitle />
449
+ <span>{status}</span>
450
+ <span>{session?.iviSessionId}</span>
451
+ </IVIStageView>
452
+ );
453
+ }
454
+ ```
455
+
456
+ `request` 变化会重新创建 session/runtime;业务侧应使用 `useMemo` 或稳定引用控制重启时机。
457
+
301
458
  ---
302
459
 
303
460
  ## React Hooks
@@ -339,6 +496,42 @@ interface IviManagedRuntimeLogEntry {
339
496
  }
340
497
  ```
341
498
 
499
+ ### `useIviSessionRuntime`
500
+
501
+ ```ts
502
+ function useIviSessionRuntime(config: UseIviSessionRuntimeConfig): UseIviSessionRuntimeResult;
503
+ ```
504
+
505
+ 负责把 `createIVISession`、WebSocket transport 和 `IviRuntimeCoordinator` 组合起来。适合希望通过 API 网关创建会话,并在 prebuild 成功时自动快速开播的 React 接入。
506
+
507
+ `UseIviSessionRuntimeConfig` 关键字段:
508
+
509
+ | 字段 | 类型 | 默认值 | 说明 |
510
+ |------|------|--------|------|
511
+ | `request` | `IviCreateIVISessionRequest \| null \| undefined` | — | 创建会话请求;为空时不创建 runtime |
512
+ | `http` | `IviCreateIVISessionHttpOptions` | — | HTTP 网关配置,包含 `baseUrl` / `url` / `headers` / `fetch` |
513
+ | `fastStart` | `boolean` | `true` | 是否启用 prebuild bootstrap source |
514
+ | `bootstrapSlot` | `string` | `"main"` | 快速开播临时绑定的 slot |
515
+ | `bootstrapTrackId` | `string` | `"__ivi_bootstrap_track"` | 快速开播临时 track id |
516
+ | `websocket` | `IviRuntimeWebSocketOptions` | — | websocket 地址、query、protocols、socketFactory |
517
+ | `runtimeConfig` | `IviRuntimeCoordinatorConfig` | — | 透传给 runtime |
518
+ | `clientConfig` | `Omit<IviClientConfig, "transport">` | — | 透传给 `IviClient` 的非 transport 配置 |
519
+ | `enabled` | `boolean` | `true` | 是否启用 hook |
520
+ | `autoStart` | `boolean` | `true` | 是否创建后自动调用 `runtime.start()` |
521
+ | `onCreated` | `(session) => void` | — | HTTP 创建会话成功回调 |
522
+ | `onRuntimeReady` | `(runtime, client) => void` | — | runtime 构造完成回调 |
523
+ | `onError` | `(error) => void` | — | 创建或启动失败回调 |
524
+
525
+ 返回值:
526
+
527
+ | 字段 | 类型 | 说明 |
528
+ |------|------|------|
529
+ | `status` | `"idle" \| "creating" \| "connecting" \| "running" \| "error" \| "stopped"` | hook 生命周期状态 |
530
+ | `session` | `IviCreateIVISessionResult \| null` | 创建会话结果 |
531
+ | `runtime` | `IviRuntimeCoordinator \| null` | runtime 实例 |
532
+ | `client` | `IviClient \| null` | 底层 realtime client |
533
+ | `error` | `Error \| null` | 最近一次错误 |
534
+
342
535
  ### `useIviStageView`
343
536
 
344
537
  ```ts
@@ -440,7 +633,7 @@ function useApplyVolumeToSlot(
440
633
 
441
634
  ## React 组件
442
635
 
443
- 当前公开导出的 React 组件包括 `IVIStageView`、`IVITrackSlot`、`IVITrtcPlayer`、`IVILivekitPlayer` 与 `IVISubtitleOverlay`;当前公开导出的 React hooks 包括 `useManagedIviRuntime`、`useRuntimeState`、`useIviStageView` 与 `useIviSubtitles`。
636
+ 当前公开导出的 React 组件包括 `IVIStageView`、`IVITrackSlot`、`IVITrtcPlayer`、`IVILivekitPlayer` 与 `IVISubtitleOverlay`;当前公开导出的 React hooks 包括 `useManagedIviRuntime`、`useIviSessionRuntime`、`useRuntimeState`、`useIviStageView` 与 `useIviSubtitles`。
444
637
 
445
638
  ### `IVIStageView`
446
639