ai-world-sdk 1.1.1 → 1.1.3
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 +285 -5
- package/dist/__tests__/example.test.js +397 -4
- package/dist/base.d.ts +1 -1
- package/dist/base.js +60 -8
- package/dist/config.d.ts +3 -3
- package/dist/config.js +2 -2
- package/dist/gemini-image-generation.d.ts +2 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +17 -16
- package/dist/log.js +1 -1
- package/dist/openai_video_generation.d.ts +188 -0
- package/dist/openai_video_generation.js +287 -0
- package/dist/provider_config.d.ts +8 -0
- package/dist/provider_config.js +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ TypeScript SDK for AI World Platform - 一个功能完整的 AI 应用开发 SDK
|
|
|
9
9
|
|
|
10
10
|
- 🤖 **聊天模型**: 兼容 LangChain.js 接口(OpenAI、Gemini、Claude、Doubao)
|
|
11
11
|
- 🎨 **图像生成**: 支持豆包 Seedream 和 Google Gemini
|
|
12
|
-
- 🎬 **视频生成**: 支持豆包 Seedance
|
|
12
|
+
- 🎬 **视频生成**: 支持豆包 Seedance 和 OpenAI Sora
|
|
13
13
|
- 📥 **下载代理**: 支持流式下载和普通下载任意 URL 的二进制文件
|
|
14
14
|
- 🔐 **用户认证**: 支持获取当前登录用户信息
|
|
15
15
|
- ⚙️ **全局配置**: 自动从浏览器环境获取配置,简化初始化
|
|
@@ -113,6 +113,8 @@ console.log('图像 URL:', geminiResult.data[0]?.url);
|
|
|
113
113
|
|
|
114
114
|
### 4. 视频生成
|
|
115
115
|
|
|
116
|
+
#### 豆包视频生成(Seedance)
|
|
117
|
+
|
|
116
118
|
```typescript
|
|
117
119
|
import { VideoGenerationClient } from 'ai-world-sdk';
|
|
118
120
|
|
|
@@ -136,6 +138,90 @@ if (result.status === 'succeeded') {
|
|
|
136
138
|
}
|
|
137
139
|
```
|
|
138
140
|
|
|
141
|
+
#### OpenAI 视频生成(Sora)
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { OpenAIVideoGenerationClient } from 'ai-world-sdk';
|
|
145
|
+
|
|
146
|
+
const openaiVideoClient = new OpenAIVideoGenerationClient({});
|
|
147
|
+
|
|
148
|
+
// 方式1: 文本生成视频
|
|
149
|
+
const task = await openaiVideoClient.generate({
|
|
150
|
+
prompt: 'A beautiful sunset over the ocean with waves crashing on the shore',
|
|
151
|
+
model: 'sora-2', // 或 'sora-2-pro'
|
|
152
|
+
provider: 'aihubmix', // 或 'api2img'
|
|
153
|
+
seconds: '4', // 4, 8, 或 12 秒
|
|
154
|
+
size: '1280x720' // '720x1280', '1280x720', '1024x1792', '1792x1024'
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
console.log('任务 ID:', task.id);
|
|
158
|
+
console.log('任务状态:', task.status);
|
|
159
|
+
|
|
160
|
+
// 方式2: 图像生成视频(图生视频)
|
|
161
|
+
const taskWithImage = await openaiVideoClient.generate({
|
|
162
|
+
prompt: 'Animate this scene with gentle movements',
|
|
163
|
+
input_reference: 'data:image/png;base64,iVBORw0KGgo...', // Base64 图像或 URL
|
|
164
|
+
model: 'sora-2',
|
|
165
|
+
provider: 'aihubmix',
|
|
166
|
+
seconds: '4'
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// 查询任务状态
|
|
170
|
+
const status = await openaiVideoClient.getTask(task.id, 'aihubmix');
|
|
171
|
+
console.log('当前状态:', status.status);
|
|
172
|
+
|
|
173
|
+
// 轮询等待完成
|
|
174
|
+
const result = await openaiVideoClient.waitForCompletion(task.id, 'aihubmix', {
|
|
175
|
+
maxAttempts: 60,
|
|
176
|
+
interval: 5000,
|
|
177
|
+
onProgress: (status) => {
|
|
178
|
+
console.log('当前状态:', status.status);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
if (result.status === 'completed') {
|
|
183
|
+
console.log('视频生成成功!');
|
|
184
|
+
|
|
185
|
+
// 流式下载视频
|
|
186
|
+
const videoBlob = await openaiVideoClient.downloadVideo(
|
|
187
|
+
result.id,
|
|
188
|
+
'aihubmix', // provider
|
|
189
|
+
'video' // 'video' 或 'thumbnail'
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
console.log('视频大小:', (videoBlob.size / 1024 / 1024).toFixed(2), 'MB');
|
|
193
|
+
|
|
194
|
+
// 在浏览器中下载
|
|
195
|
+
const url = URL.createObjectURL(videoBlob);
|
|
196
|
+
const a = document.createElement('a');
|
|
197
|
+
a.href = url;
|
|
198
|
+
a.download = 'sora_video.mp4';
|
|
199
|
+
a.click();
|
|
200
|
+
URL.revokeObjectURL(url);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 一键生成并等待(简化版)
|
|
204
|
+
const completeResult = await openaiVideoClient.generateAndWait({
|
|
205
|
+
prompt: 'A futuristic city skyline',
|
|
206
|
+
model: 'sora-2',
|
|
207
|
+
provider: 'aihubmix',
|
|
208
|
+
seconds: '4'
|
|
209
|
+
}, {
|
|
210
|
+
maxAttempts: 60,
|
|
211
|
+
interval: 5000,
|
|
212
|
+
onProgress: (status) => console.log('状态:', status.status)
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
if (completeResult.status === 'completed') {
|
|
216
|
+
const videoBlob = await openaiVideoClient.downloadVideo(
|
|
217
|
+
completeResult.id,
|
|
218
|
+
'aihubmix',
|
|
219
|
+
'video'
|
|
220
|
+
);
|
|
221
|
+
console.log('一键生成完成,视频大小:', videoBlob.size);
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
139
225
|
### 5. 用户认证
|
|
140
226
|
|
|
141
227
|
```typescript
|
|
@@ -378,7 +464,7 @@ const secondResponse = await client.chat({
|
|
|
378
464
|
|
|
379
465
|
### 视频生成
|
|
380
466
|
|
|
381
|
-
#### VideoGenerationClient
|
|
467
|
+
#### VideoGenerationClient(豆包 Seedance)
|
|
382
468
|
|
|
383
469
|
```typescript
|
|
384
470
|
const client = new VideoGenerationClient({});
|
|
@@ -403,6 +489,53 @@ const result = await client.poll(task.id, {
|
|
|
403
489
|
});
|
|
404
490
|
```
|
|
405
491
|
|
|
492
|
+
#### OpenAIVideoGenerationClient(OpenAI Sora)
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
const client = new OpenAIVideoGenerationClient({});
|
|
496
|
+
|
|
497
|
+
// 文本生成视频
|
|
498
|
+
const task = await client.generate({
|
|
499
|
+
prompt: 'A beautiful sunset',
|
|
500
|
+
model: 'sora-2', // 可选: sora-2, sora-2-pro
|
|
501
|
+
provider: 'openai', // 可选: openai, aihubmix, api2img
|
|
502
|
+
seconds: '4', // 可选: 4, 8, 12
|
|
503
|
+
size: '1280x720', // 可选: 720x1280, 1280x720, 1024x1792, 1792x1024
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
// 图像生成视频
|
|
507
|
+
const taskFromImage = await client.generate({
|
|
508
|
+
prompt: 'Animate this scene',
|
|
509
|
+
input_reference: 'data:image/png;base64,iVBORw0KGgo...', // Base64 或 URL
|
|
510
|
+
model: 'sora-2',
|
|
511
|
+
seconds: '8',
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
// 查询状态
|
|
515
|
+
const status = await client.getTask(task.id);
|
|
516
|
+
|
|
517
|
+
// 轮询直到完成
|
|
518
|
+
const result = await client.waitForCompletion(task.id, {
|
|
519
|
+
maxAttempts: 60,
|
|
520
|
+
interval: 5000,
|
|
521
|
+
onProgress: (status) => {
|
|
522
|
+
console.log('状态:', status.status);
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// 下载视频
|
|
527
|
+
const videoBlob = await client.downloadVideo(task.id, {
|
|
528
|
+
variant: 'video' // 或 'thumbnail'
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// 一键生成并等待
|
|
532
|
+
const finalResult = await client.generateAndWait({
|
|
533
|
+
prompt: 'A futuristic city',
|
|
534
|
+
model: 'sora-2',
|
|
535
|
+
seconds: '4'
|
|
536
|
+
});
|
|
537
|
+
```
|
|
538
|
+
|
|
406
539
|
### 下载代理
|
|
407
540
|
|
|
408
541
|
#### DownloadClient
|
|
@@ -1064,6 +1197,8 @@ if (userInfo.avatar_url) {
|
|
|
1064
1197
|
|
|
1065
1198
|
### 视频生成工作流
|
|
1066
1199
|
|
|
1200
|
+
#### 豆包视频生成(Doubao Seedance)
|
|
1201
|
+
|
|
1067
1202
|
```typescript
|
|
1068
1203
|
import { VideoGenerationClient } from 'ai-world-sdk';
|
|
1069
1204
|
|
|
@@ -1093,6 +1228,131 @@ if (result.status === 'succeeded') {
|
|
|
1093
1228
|
}
|
|
1094
1229
|
```
|
|
1095
1230
|
|
|
1231
|
+
#### OpenAI 视频生成(Sora)
|
|
1232
|
+
|
|
1233
|
+
```typescript
|
|
1234
|
+
import { OpenAIVideoGenerationClient } from 'ai-world-sdk';
|
|
1235
|
+
|
|
1236
|
+
const client = new OpenAIVideoGenerationClient({});
|
|
1237
|
+
|
|
1238
|
+
// 1. 文本生成视频
|
|
1239
|
+
const task = await client.generate({
|
|
1240
|
+
prompt: 'A serene mountain landscape with a flowing river',
|
|
1241
|
+
model: 'sora-2', // 'sora-2' 或 'sora-2-pro'
|
|
1242
|
+
provider: 'aihubmix', // 'aihubmix' 或 'api2img'
|
|
1243
|
+
seconds: '4', // '4', '8', 或 '12'
|
|
1244
|
+
size: '1280x720' // '720x1280', '1280x720', '1024x1792', '1792x1024'
|
|
1245
|
+
});
|
|
1246
|
+
|
|
1247
|
+
console.log('任务已创建:', task.id);
|
|
1248
|
+
console.log('任务状态:', task.status);
|
|
1249
|
+
|
|
1250
|
+
// 2. 查询任务状态
|
|
1251
|
+
const status = await client.getTask(task.id, 'aihubmix');
|
|
1252
|
+
console.log('当前状态:', status.status);
|
|
1253
|
+
|
|
1254
|
+
// 3. 轮询等待完成
|
|
1255
|
+
const result = await client.waitForCompletion(
|
|
1256
|
+
task.id,
|
|
1257
|
+
'aihubmix', // provider 参数
|
|
1258
|
+
{
|
|
1259
|
+
maxAttempts: 60,
|
|
1260
|
+
interval: 5000,
|
|
1261
|
+
onProgress: (status) => {
|
|
1262
|
+
console.log(`状态: ${status.status}`);
|
|
1263
|
+
if (status.status === 'in_progress') {
|
|
1264
|
+
console.log('正在生成...');
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
);
|
|
1269
|
+
|
|
1270
|
+
if (result.status === 'completed') {
|
|
1271
|
+
console.log('✅ 视频生成成功!');
|
|
1272
|
+
|
|
1273
|
+
// 4. 流式下载视频
|
|
1274
|
+
const videoBlob = await client.downloadVideo(
|
|
1275
|
+
result.id,
|
|
1276
|
+
'aihubmix', // provider
|
|
1277
|
+
'video' // 'video' 或 'thumbnail'
|
|
1278
|
+
);
|
|
1279
|
+
|
|
1280
|
+
console.log('视频大小:', (videoBlob.size / 1024 / 1024).toFixed(2), 'MB');
|
|
1281
|
+
console.log('视频类型:', videoBlob.type);
|
|
1282
|
+
|
|
1283
|
+
// 在浏览器中创建下载链接
|
|
1284
|
+
const url = URL.createObjectURL(videoBlob);
|
|
1285
|
+
const a = document.createElement('a');
|
|
1286
|
+
a.href = url;
|
|
1287
|
+
a.download = `sora_video_${result.id}.mp4`;
|
|
1288
|
+
a.click();
|
|
1289
|
+
URL.revokeObjectURL(url);
|
|
1290
|
+
|
|
1291
|
+
// 也可以下载缩略图
|
|
1292
|
+
const thumbnail = await client.downloadVideo(
|
|
1293
|
+
result.id,
|
|
1294
|
+
'aihubmix',
|
|
1295
|
+
'thumbnail'
|
|
1296
|
+
);
|
|
1297
|
+
console.log('缩略图大小:', (thumbnail.size / 1024).toFixed(2), 'KB');
|
|
1298
|
+
} else if (result.status === 'failed') {
|
|
1299
|
+
console.error('❌ 生成失败:', result.error);
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
// 一键生成并等待(简化版)
|
|
1303
|
+
try {
|
|
1304
|
+
const result2 = await client.generateAndWait({
|
|
1305
|
+
prompt: 'A futuristic city with flying cars at night',
|
|
1306
|
+
model: 'sora-2',
|
|
1307
|
+
provider: 'aihubmix',
|
|
1308
|
+
seconds: '8',
|
|
1309
|
+
size: '1280x720'
|
|
1310
|
+
}, {
|
|
1311
|
+
maxAttempts: 60,
|
|
1312
|
+
interval: 5000,
|
|
1313
|
+
onProgress: (status) => console.log('进度:', status.status)
|
|
1314
|
+
});
|
|
1315
|
+
|
|
1316
|
+
console.log('视频生成完成:', result2.id);
|
|
1317
|
+
|
|
1318
|
+
// 流式下载
|
|
1319
|
+
const video = await client.downloadVideo(
|
|
1320
|
+
result2.id,
|
|
1321
|
+
'aihubmix',
|
|
1322
|
+
'video'
|
|
1323
|
+
);
|
|
1324
|
+
console.log('视频大小:', (video.size / 1024 / 1024).toFixed(2), 'MB');
|
|
1325
|
+
} catch (error) {
|
|
1326
|
+
console.error('视频生成失败:', error);
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
// 图像生成视频示例
|
|
1330
|
+
const imageToVideoTask = await client.generate({
|
|
1331
|
+
prompt: 'Make this scene come alive with gentle movements',
|
|
1332
|
+
input_reference: 'data:image/png;base64,iVBORw0KGgo...', // 图像 base64 或 URL
|
|
1333
|
+
model: 'sora-2',
|
|
1334
|
+
provider: 'aihubmix',
|
|
1335
|
+
seconds: '4'
|
|
1336
|
+
});
|
|
1337
|
+
|
|
1338
|
+
const imageToVideoResult = await client.waitForCompletion(
|
|
1339
|
+
imageToVideoTask.id,
|
|
1340
|
+
'aihubmix'
|
|
1341
|
+
);
|
|
1342
|
+
|
|
1343
|
+
if (imageToVideoResult.status === 'completed') {
|
|
1344
|
+
console.log('图生视频完成:', imageToVideoResult.id);
|
|
1345
|
+
|
|
1346
|
+
// 下载视频
|
|
1347
|
+
const videoBlob = await client.downloadVideo(
|
|
1348
|
+
imageToVideoResult.id,
|
|
1349
|
+
'aihubmix',
|
|
1350
|
+
'video'
|
|
1351
|
+
);
|
|
1352
|
+
console.log('视频已下载,大小:', videoBlob.size);
|
|
1353
|
+
}
|
|
1354
|
+
```
|
|
1355
|
+
|
|
1096
1356
|
### 用户认证工作流
|
|
1097
1357
|
|
|
1098
1358
|
```typescript
|
|
@@ -1229,7 +1489,7 @@ for await (const chunk of client.streamDownload({
|
|
|
1229
1489
|
| 提供商 | Provider | 模型示例 | 模型类 |
|
|
1230
1490
|
|--------|----------|----------|--------|
|
|
1231
1491
|
| OpenAI | `aihubmix` | `gpt-4o-mini`, `gpt-4`, `o1-preview` | `ChatOpenAI` |
|
|
1232
|
-
| Google Gemini | `gemini` 或 `aihubmix` | `gemini-2.5-flash-image`, `gemini-
|
|
1492
|
+
| Google Gemini | `gemini` 或 `aihubmix` | `gemini-2.5-flash-image`, `gemini-3-pro-image-preview` | `ChatGoogleGenerativeAI` |
|
|
1233
1493
|
| Anthropic Claude | `aihubmix` | `claude-3-sonnet-20240229`, `claude-3-opus-20240229` | `ChatAnthropic` |
|
|
1234
1494
|
| Doubao | `doubao` 或 `aihubmix` | `doubao-pro-4k`, `doubao-seedream-4-5-251128` | `ChatOpenAI` |
|
|
1235
1495
|
|
|
@@ -1245,7 +1505,6 @@ for await (const chunk of client.streamDownload({
|
|
|
1245
1505
|
- **Google Gemini**:
|
|
1246
1506
|
- `gemini-2.5-flash-image` (Nano Banana) - **推荐**,快速、高效,1024px 分辨率,支持所有宽高比
|
|
1247
1507
|
- `gemini-3-pro-image-preview` (Nano Banana Pro) - 专业级,支持 1K/2K/4K 分辨率,支持 Google 搜索、思考模式,最多 14 张参考图片
|
|
1248
|
-
- `gemini-2.0-flash-exp-image-generation` (已弃用,建议使用 `gemini-2.5-flash-image`)
|
|
1249
1508
|
|
|
1250
1509
|
**模型选择建议:**
|
|
1251
1510
|
- **日常使用**: `gemini-2.5-flash-image` - 速度快,成本低
|
|
@@ -1253,7 +1512,28 @@ for await (const chunk of client.streamDownload({
|
|
|
1253
1512
|
|
|
1254
1513
|
### 视频生成模型
|
|
1255
1514
|
|
|
1256
|
-
|
|
1515
|
+
#### 豆包 Seedance
|
|
1516
|
+
|
|
1517
|
+
| 模型 | Provider | 说明 | 参数 |
|
|
1518
|
+
|------|----------|------|------|
|
|
1519
|
+
| `doubao-seedance-1-0-pro-fast-251015` | `doubao` | 快速版(推荐) | 时长: 1-10秒,宽高比: 16:9/9:16/1:1 |
|
|
1520
|
+
| `doubao-seedance-1-0-pro-250528` | `doubao` | 专业版 | 时长: 1-10秒,宽高比: 16:9/9:16/1:1 |
|
|
1521
|
+
| `doubao-seedance-1-0-lite-t2v-250428` | `doubao` | 轻量版(文生视频) | 时长: 1-10秒 |
|
|
1522
|
+
| `doubao-seedance-1-0-lite-i2v-250428` | `doubao` | 轻量版(图生视频) | 时长: 1-10秒 |
|
|
1523
|
+
|
|
1524
|
+
#### OpenAI Sora
|
|
1525
|
+
|
|
1526
|
+
| 模型 | Provider | 说明 | 参数 |
|
|
1527
|
+
|------|----------|------|------|
|
|
1528
|
+
| `sora-2` | `aihubmix` / `api2img` | 标准模型(默认) | 时长: 4/8/12秒,分辨率: 720x1280, 1280x720, 1024x1792, 1792x1024 |
|
|
1529
|
+
| `sora-2-pro` | `aihubmix` / `api2img` | 专业版模型 | 时长: 4/8/12秒,分辨率: 720x1280, 1280x720, 1024x1792, 1792x1024 |
|
|
1530
|
+
|
|
1531
|
+
**特性说明:**
|
|
1532
|
+
- **豆包 Seedance**: 支持文本生成视频和图像生成视频,URL 直接返回
|
|
1533
|
+
- **OpenAI Sora**: 支持文本生成视频和图像生成视频(Base64/URL),需要流式下载
|
|
1534
|
+
- **Provider 选择**:
|
|
1535
|
+
- 豆包: 仅支持 `doubao` provider
|
|
1536
|
+
- OpenAI: 支持 `aihubmix` 和 `api2img` provider(推荐使用 `aihubmix`)
|
|
1257
1537
|
|
|
1258
1538
|
## 错误处理
|
|
1259
1539
|
|