ai-world-sdk 1.2.5 → 1.2.6
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/dist/config.d.ts +3 -3
- package/dist/config.js +2 -2
- package/package.json +3 -2
- package/skills/ai-world-sdk/SKILL.md +115 -0
- package/skills/ai-world-sdk/docs/common-mistakes.md +15 -0
- package/skills/ai-world-sdk/docs/image-generation.md +69 -0
- package/skills/ai-world-sdk/docs/minio-storage.md +55 -0
- package/skills/ai-world-sdk/docs/provider-and-models.md +63 -0
- package/skills/ai-world-sdk/docs/resource-management.md +71 -0
- package/skills/ai-world-sdk/docs/text-generation.md +57 -0
- package/skills/ai-world-sdk/docs/video-generation.md +47 -0
package/dist/config.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*
|
|
10
10
|
* 注意: {VERSION} 占位符会在构建时被替换为实际版本号
|
|
11
11
|
*/
|
|
12
|
-
export declare const SDK_SIGNATURE = "AI_WORLD_SDK_V:1.2.
|
|
12
|
+
export declare const SDK_SIGNATURE = "AI_WORLD_SDK_V:1.2.6";
|
|
13
13
|
/**
|
|
14
14
|
* 版本兼容性错误
|
|
15
15
|
*/
|
|
@@ -34,8 +34,8 @@ declare class SDKConfig {
|
|
|
34
34
|
private _authenticated;
|
|
35
35
|
private _authCheckPromise;
|
|
36
36
|
private _currentUser;
|
|
37
|
-
readonly sdkSignature = "AI_WORLD_SDK_V:1.2.
|
|
38
|
-
readonly sdkVersion = "1.2.
|
|
37
|
+
readonly sdkSignature = "AI_WORLD_SDK_V:1.2.6";
|
|
38
|
+
readonly sdkVersion = "1.2.6";
|
|
39
39
|
constructor();
|
|
40
40
|
/**
|
|
41
41
|
* Set global base URL
|
package/dist/config.js
CHANGED
|
@@ -7,7 +7,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.sdkConfig = exports.VersionCompatibilityError = exports.SDK_SIGNATURE = void 0;
|
|
8
8
|
// SDK 版本号(构建时自动从 package.json 更新)
|
|
9
9
|
// 此版本号会在运行 npm run build 时自动从 package.json 读取并更新
|
|
10
|
-
const SDK_VERSION = "1.2.
|
|
10
|
+
const SDK_VERSION = "1.2.6";
|
|
11
11
|
/**
|
|
12
12
|
* SDK 特征码 - 用于在构建后的 JS 文件中识别 SDK 版本
|
|
13
13
|
* 格式: AI_WORLD_SDK_V:版本号
|
|
@@ -15,7 +15,7 @@ const SDK_VERSION = "1.2.5";
|
|
|
15
15
|
*
|
|
16
16
|
* 注意: {VERSION} 占位符会在构建时被替换为实际版本号
|
|
17
17
|
*/
|
|
18
|
-
exports.SDK_SIGNATURE = "AI_WORLD_SDK_V:1.2.
|
|
18
|
+
exports.SDK_SIGNATURE = "AI_WORLD_SDK_V:1.2.6";
|
|
19
19
|
/**
|
|
20
20
|
* 版本兼容性错误
|
|
21
21
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-world-sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"description": "TypeScript SDK for AI World Platform - Chat Models, Image Generation, and Video Generation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -66,7 +66,8 @@
|
|
|
66
66
|
"typescript": "^5.0.0"
|
|
67
67
|
},
|
|
68
68
|
"files": [
|
|
69
|
-
"dist"
|
|
69
|
+
"dist",
|
|
70
|
+
"skills"
|
|
70
71
|
],
|
|
71
72
|
"dependencies": {
|
|
72
73
|
"@ai-sdk/anthropic": "^3.0.43",
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ai-world-sdk
|
|
3
|
+
description: Use when initializing ai-world-sdk or calling AI models in ai-world projects. Covers sdkConfig initialization, createProvider with Vercel AI SDK (generateText, streamText, generateImage), Doubao image/video generation, MinIO object storage (MinioStorageClient), and resource management with ACL (ResourceClient). Triggers on ai-world-sdk, createProvider, sdkConfig, generateText, streamText, generateImage, doubao, MinioStorageClient, minio, ResourceClient, resource management, access control, file sharing, or any AI model integration.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# AI World SDK
|
|
7
|
+
|
|
8
|
+
ai-world-sdk 是 AI World 平台的 TypeScript SDK,通过 `createProvider` 创建 Vercel AI SDK Provider,所有请求经后端代理转发,无需管理 API Key。
|
|
9
|
+
|
|
10
|
+
- Vercel AI SDK 官方文档: https://ai-sdk.dev/docs
|
|
11
|
+
- AI SDK Core API: https://ai-sdk.dev/docs/reference/ai-sdk-core
|
|
12
|
+
|
|
13
|
+
## When to Use
|
|
14
|
+
|
|
15
|
+
- 在 ai-world 插件中调用 AI 模型(文本、图像、视频)
|
|
16
|
+
- 需要初始化 ai-world-sdk 配置
|
|
17
|
+
- 使用 `createProvider` 调用 Vercel AI SDK
|
|
18
|
+
- 使用 Doubao 豆包图像/视频生成
|
|
19
|
+
- 使用 MinIO 对象存储(文件上传/下载/管理)
|
|
20
|
+
- 使用资源管理系统(权限控制: private/specific_users/public)
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install ai-world-sdk ai
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## SDK 初始化
|
|
29
|
+
|
|
30
|
+
### 浏览器环境(ai-world 插件内)
|
|
31
|
+
|
|
32
|
+
**自动初始化**,无需手动配置:
|
|
33
|
+
- `baseUrl` — 自动从 `window.location.origin`
|
|
34
|
+
- `token` — 自动从 cookie `auth_token`
|
|
35
|
+
- `pluginId` — 自动从 `window.__INITIAL_DATA__.plugin_id`
|
|
36
|
+
|
|
37
|
+
### Vite 开发模式(推荐:Vite Plugin 自动登录)
|
|
38
|
+
|
|
39
|
+
在 `vite.config.ts` 中添加 `aiWorldPlugin()`,dev server 启动时自动完成以下所有配置:
|
|
40
|
+
|
|
41
|
+
1. 检测 `.env.local` 中的 `DEBUG_TOKEN`,缺失或过期时通过后端 `/api/auth/login` 自动打开浏览器完成飞书登录
|
|
42
|
+
2. 登录后将 `DEBUG=true`、`DEBUG_TOKEN`、`PLUGIN_ID` 写入 `.env.local`
|
|
43
|
+
3. `PLUGIN_ID` 按优先级:`plugin.json` 的 `plugin_id` → `package.json` 的 `name` → 项目目录名
|
|
44
|
+
4. 通过 `window.__AI_WORLD_CONFIG__` 将配置注入到浏览器页面,SDK 自动读取
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { aiWorldPlugin } from 'ai-world-sdk/vite'
|
|
48
|
+
|
|
49
|
+
export default defineConfig({
|
|
50
|
+
plugins: [
|
|
51
|
+
vue(), // 或 react() 等
|
|
52
|
+
aiWorldPlugin() // baseUrl 默认 https://aiworld.local:8000
|
|
53
|
+
]
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
首次 `npm run dev` 时自动打开浏览器完成飞书登录,后续启动 token 有效则直接跳过。无需在代码中手动调用 `sdkConfig.setToken()` 或 `sdkConfig.setPluginId()`,一切自动完成。
|
|
58
|
+
|
|
59
|
+
如需自定义后端地址:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
aiWorldPlugin({ baseUrl: 'https://your-server:8000' })
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### 非浏览器环境(测试、脚本)
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { sdkConfig } from 'ai-world-sdk';
|
|
69
|
+
|
|
70
|
+
sdkConfig.setBaseUrl('https://aiworld.local:8000');
|
|
71
|
+
sdkConfig.setToken('your-jwt-token');
|
|
72
|
+
sdkConfig.setPluginId('my-plugin');
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
也可通过环境变量 `DEBUG_TOKEN`、`PLUGIN_ID`、`AI_WORLD_BASE_URL` 自动初始化。
|
|
76
|
+
|
|
77
|
+
## 功能模块
|
|
78
|
+
|
|
79
|
+
以下功能的详细用法和代码示例请查阅对应的子文档。
|
|
80
|
+
|
|
81
|
+
### Provider 与模型 → `docs/provider-and-models.md`
|
|
82
|
+
|
|
83
|
+
- `createProvider(provider, endpointType, pluginId)` 创建 Vercel AI SDK Provider
|
|
84
|
+
- Provider 对照表:`api2img`(推荐) / `gemini` / `aihubmix` / `shubiaobiao`
|
|
85
|
+
- 常用模型列表和 AI SDK 函数速查
|
|
86
|
+
|
|
87
|
+
### 文本生成 → `docs/text-generation.md`
|
|
88
|
+
|
|
89
|
+
- `generateText` / `streamText` / `generateObject` / `streamObject`
|
|
90
|
+
|
|
91
|
+
### 图像生成 → `docs/image-generation.md`
|
|
92
|
+
|
|
93
|
+
- AI SDK `generateImage`(DALL-E / GPT Image)
|
|
94
|
+
- `DoubaoImageGenerationClient`(豆包 Seedream)
|
|
95
|
+
- `GeminiImageGenerationClient`
|
|
96
|
+
|
|
97
|
+
### 视频生成 → `docs/video-generation.md`
|
|
98
|
+
|
|
99
|
+
- `VideoGenerationClient`(豆包 Seedance)
|
|
100
|
+
- `OpenAIVideoGenerationClient`(Sora)
|
|
101
|
+
|
|
102
|
+
### MinIO 对象存储 → `docs/minio-storage.md`
|
|
103
|
+
|
|
104
|
+
- `MinioStorageClient` — 文件上传/下载/列表/删除/预签名 URL
|
|
105
|
+
- 存储路径: `{pluginId}/{userId}/{path}`
|
|
106
|
+
|
|
107
|
+
### 资源管理 → `docs/resource-management.md`
|
|
108
|
+
|
|
109
|
+
- `ResourceClient` — 带 ACL 的资源管理
|
|
110
|
+
- 三级权限: `private` / `specific_users` / `public`
|
|
111
|
+
- 插件隔离与跨插件访问
|
|
112
|
+
|
|
113
|
+
### 常见错误 → `docs/common-mistakes.md`
|
|
114
|
+
|
|
115
|
+
- endpointType 匹配规则、AI SDK v6 用法变更、MinIO 路径规范等
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# 常见错误与正确做法
|
|
2
|
+
|
|
3
|
+
| 错误 | 正确做法 |
|
|
4
|
+
|------|---------|
|
|
5
|
+
| endpointType 与 provider 不匹配 | `gemini`→`gemini`,其余→`openai` |
|
|
6
|
+
| 手动传 API Key | 不需要,自动经后端代理鉴权 |
|
|
7
|
+
| `result.usage.promptTokens` | AI SDK v6 改为 `inputTokens` / `outputTokens` |
|
|
8
|
+
| `provider.image()` 类型报错 | 用 `(provider as any).image()` |
|
|
9
|
+
| 忘装 `ai` 包 | 需同时装 `ai-world-sdk` 和 `ai` |
|
|
10
|
+
| Vite dev 时调不通 | 在 `vite.config.ts` 中使用 `aiWorldPlugin()` 自动登录 |
|
|
11
|
+
| 用 `createProvider` 调豆包 | 豆包用专用客户端 `DoubaoImageGenerationClient` / `VideoGenerationClient` |
|
|
12
|
+
| MinIO 操作缺少 `X-Plugin-Id` | 先调 `sdkConfig.setPluginId('xxx')` 或在构造函数传入 `pluginId` |
|
|
13
|
+
| MinIO 路径包含前导斜杠 | 路径不要以 `/` 开头,如 `docs/file.txt` 而非 `/docs/file.txt` |
|
|
14
|
+
| 资源管理与 MinIO 混用 | `MinioStorageClient` 是原始存储(无权限控制),`ResourceClient` 带 ACL |
|
|
15
|
+
| 资源管理忘记权限 | 上传默认 `private`,需显式设 `accessLevel: 'public'` 才能被他人访问 |
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# 图像生成
|
|
2
|
+
|
|
3
|
+
## AI SDK 图像生成(DALL-E / GPT Image)
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { createProvider } from 'ai-world-sdk';
|
|
7
|
+
import { generateImage } from 'ai';
|
|
8
|
+
|
|
9
|
+
const provider = createProvider('api2img', 'openai', 'my-plugin');
|
|
10
|
+
const imageModel = (provider as any).image('dall-e-3'); // as any: ProviderV3 类型不含 .image()
|
|
11
|
+
|
|
12
|
+
const { images } = await generateImage({
|
|
13
|
+
model: imageModel,
|
|
14
|
+
prompt: '可爱的小猫',
|
|
15
|
+
size: '1024x1024',
|
|
16
|
+
n: 1,
|
|
17
|
+
});
|
|
18
|
+
// images[0].base64, images[0].mediaType
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 豆包(Doubao)图像生成
|
|
22
|
+
|
|
23
|
+
豆包不通过 `createProvider`,使用专用客户端 `DoubaoImageGenerationClient`。
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { DoubaoImageGenerationClient } from 'ai-world-sdk';
|
|
27
|
+
|
|
28
|
+
const client = new DoubaoImageGenerationClient({});
|
|
29
|
+
|
|
30
|
+
const result = await client.generate({
|
|
31
|
+
prompt: '一只可爱的小猫',
|
|
32
|
+
model: 'doubao-seedream-4-5-251128',
|
|
33
|
+
size: '2K', // '1K' | '2K' | '4K' | '2048x2048' | '2560x1440' 等
|
|
34
|
+
quality: 'hd', // 'standard' | 'hd'
|
|
35
|
+
n: 1, // 1-10
|
|
36
|
+
});
|
|
37
|
+
console.log(result.data[0]?.url);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 图文生图(单图输入)
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
await client.generate({
|
|
44
|
+
prompt: '转换为水彩画风格',
|
|
45
|
+
image: 'data:image/png;base64,...',
|
|
46
|
+
size: '2560x1440',
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 多图融合
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
await client.generate({
|
|
54
|
+
prompt: '合并这些图片',
|
|
55
|
+
image: ['data:image/png;base64,...', 'data:image/png;base64,...'],
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Gemini 图像生成
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { GeminiImageGenerationClient } from 'ai-world-sdk';
|
|
63
|
+
|
|
64
|
+
const client = new GeminiImageGenerationClient({});
|
|
65
|
+
const result = await client.generate({
|
|
66
|
+
prompt: '一只可爱的小猫',
|
|
67
|
+
provider: 'gemini',
|
|
68
|
+
});
|
|
69
|
+
```
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# MinIO 对象存储
|
|
2
|
+
|
|
3
|
+
通过 `MinioStorageClient` 操作后端 MinIO 对象存储。所有文件按 `{pluginId}/{userId}/{path}` 存储,需要设置 `pluginId`。
|
|
4
|
+
|
|
5
|
+
## 基本用法
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { MinioStorageClient, sdkConfig } from 'ai-world-sdk';
|
|
9
|
+
|
|
10
|
+
// pluginId 在 ai-world 插件环境自动从 window.__INITIAL_DATA__ 获取
|
|
11
|
+
// 手动设置: sdkConfig.setPluginId('my-plugin');
|
|
12
|
+
|
|
13
|
+
const storage = new MinioStorageClient();
|
|
14
|
+
|
|
15
|
+
// 上传文件
|
|
16
|
+
const file = new File(['hello world'], 'hello.txt', { type: 'text/plain' });
|
|
17
|
+
const result = await storage.upload('docs/hello.txt', file);
|
|
18
|
+
console.log(result.path, result.size, result.etag);
|
|
19
|
+
|
|
20
|
+
// 下载文件
|
|
21
|
+
const blob = await storage.download('docs/hello.txt');
|
|
22
|
+
const text = await blob.text();
|
|
23
|
+
|
|
24
|
+
// 列出文件
|
|
25
|
+
const files = await storage.list({ prefix: 'docs/' });
|
|
26
|
+
|
|
27
|
+
// 获取文件信息
|
|
28
|
+
const info = await storage.info('docs/hello.txt');
|
|
29
|
+
|
|
30
|
+
// 获取预签名下载 URL(可直接访问,无需认证)
|
|
31
|
+
const { url } = await storage.getPresignedUrl('docs/hello.txt', 3600);
|
|
32
|
+
|
|
33
|
+
// 获取预签名上传 URL(客户端直传到 MinIO)
|
|
34
|
+
const { url: uploadUrl } = await storage.getPresignedUploadUrl('docs/upload.txt', 3600);
|
|
35
|
+
|
|
36
|
+
// 删除文件
|
|
37
|
+
await storage.delete('docs/hello.txt');
|
|
38
|
+
|
|
39
|
+
// 下载并触发浏览器保存
|
|
40
|
+
await storage.downloadAsFile('docs/report.pdf', 'report.pdf');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## 后端 API 端点
|
|
44
|
+
|
|
45
|
+
| 方法 | 路径 | 说明 |
|
|
46
|
+
|------|------|------|
|
|
47
|
+
| POST | `/api/minio/upload` | 上传文件(multipart/form-data) |
|
|
48
|
+
| GET | `/api/minio/download/{path}` | 下载文件 |
|
|
49
|
+
| GET | `/api/minio/list` | 列出文件(?prefix=xxx) |
|
|
50
|
+
| DELETE | `/api/minio/delete/{path}` | 删除文件 |
|
|
51
|
+
| GET | `/api/minio/presigned-url/{path}` | 预签名下载 URL |
|
|
52
|
+
| POST | `/api/minio/presigned-upload` | 预签名上传 URL |
|
|
53
|
+
| GET | `/api/minio/info/{path}` | 文件元信息 |
|
|
54
|
+
|
|
55
|
+
所有请求需 `Authorization: Bearer <token>` 和 `X-Plugin-Id` 请求头。
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Provider 与模型
|
|
2
|
+
|
|
3
|
+
## Provider 对照表
|
|
4
|
+
|
|
5
|
+
| Provider | EndpointType | 说明 |
|
|
6
|
+
|----------|-------------|------|
|
|
7
|
+
| `api2img` | `openai` | **推荐**,聚合所有模型(GPT、Gemini、Claude 等) |
|
|
8
|
+
| `aihubmix` | `openai` | 多模型聚合 |
|
|
9
|
+
| `shubiaobiao` | `openai` | 数标标 |
|
|
10
|
+
| `gemini` | `gemini` | Google Gemini 原生 |
|
|
11
|
+
|
|
12
|
+
规则:`gemini` provider 用 `gemini` endpoint,其余全部用 `openai`。
|
|
13
|
+
|
|
14
|
+
> `doubao` 不通过 `createProvider` 使用,豆包有专用客户端(见 image-generation.md / video-generation.md)。
|
|
15
|
+
|
|
16
|
+
## createProvider 用法
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { createProvider } from 'ai-world-sdk';
|
|
20
|
+
|
|
21
|
+
// createProvider(provider, endpointType, pluginId)
|
|
22
|
+
const provider = createProvider('api2img', 'openai', 'my-plugin');
|
|
23
|
+
const model = provider.languageModel('gpt-4o-mini');
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### 不同供应商
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
// Gemini
|
|
30
|
+
createProvider('gemini', 'gemini', 'my-plugin').languageModel('gemini-2.5-flash');
|
|
31
|
+
|
|
32
|
+
// api2img 聚合(推荐,一个 provider 访问所有模型)
|
|
33
|
+
const p = createProvider('api2img', 'openai', 'my-plugin');
|
|
34
|
+
p.languageModel('gpt-4o-mini');
|
|
35
|
+
p.languageModel('gemini-2.5-flash');
|
|
36
|
+
p.languageModel('claude-sonnet-4-5');
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 检测模型可用性
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { checkModel } from 'ai-world-sdk';
|
|
43
|
+
const ok = await checkModel('api2img', 'openai', 'gpt-4o-mini', 'my-plugin');
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 常用模型
|
|
47
|
+
|
|
48
|
+
| 模型 | 类型 | Provider | EndpointType |
|
|
49
|
+
|------|------|----------|-------------|
|
|
50
|
+
| `gpt-4o-mini` / `gpt-4o` / `gpt-5.1` | 文本 | `api2img` | `openai` |
|
|
51
|
+
| `gemini-2.5-flash` / `gemini-2.5-pro` | 文本 | `gemini` | `gemini` |
|
|
52
|
+
| `dall-e-3` / `gpt-image-1` | 图像 | `api2img` | `openai` |
|
|
53
|
+
| `doubao-seedream-4-5-251128` | 图像 | — | 专用客户端 |
|
|
54
|
+
|
|
55
|
+
## AI SDK 函数速查
|
|
56
|
+
|
|
57
|
+
| 函数 | 用途 | 文档 |
|
|
58
|
+
|------|------|------|
|
|
59
|
+
| `generateText` | 一次性文本 | https://ai-sdk.dev/docs/reference/ai-sdk-core/generate-text |
|
|
60
|
+
| `streamText` | 流式文本 | https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text |
|
|
61
|
+
| `generateObject` | 结构化数据 | https://ai-sdk.dev/docs/reference/ai-sdk-core/generate-object |
|
|
62
|
+
| `streamObject` | 流式结构化 | https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-object |
|
|
63
|
+
| `generateImage` | 图像生成 | https://ai-sdk.dev/docs/reference/ai-sdk-core/generate-image |
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# 资源管理系统(访问权限控制)
|
|
2
|
+
|
|
3
|
+
在 MinIO 原始存储之上提供数据库驱动的访问权限控制,支持 private / specific_users / public 三级权限。
|
|
4
|
+
资源文件存储在独立 bucket `ai-world-res`(配置: `MINIO_RESOURCE_BUCKET`),与普通 MinIO 存储(`ai-world`)隔离。
|
|
5
|
+
|
|
6
|
+
## 基本用法
|
|
7
|
+
|
|
8
|
+
```typescript
|
|
9
|
+
import { ResourceClient, sdkConfig } from 'ai-world-sdk';
|
|
10
|
+
|
|
11
|
+
sdkConfig.setPluginId('my-plugin');
|
|
12
|
+
const resources = new ResourceClient();
|
|
13
|
+
|
|
14
|
+
// 上传并设为公开
|
|
15
|
+
const file = new File(['content'], 'doc.txt', { type: 'text/plain' });
|
|
16
|
+
const res = await resources.upload('docs/doc.txt', file, {
|
|
17
|
+
accessLevel: 'public',
|
|
18
|
+
description: '公开文档',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// 列出我的资源
|
|
22
|
+
const myFiles = await resources.listMy();
|
|
23
|
+
|
|
24
|
+
// 列出公开资源
|
|
25
|
+
const publicFiles = await resources.listPublic();
|
|
26
|
+
|
|
27
|
+
// 查看某用户的可访问资源
|
|
28
|
+
const userFiles = await resources.listByUser(42);
|
|
29
|
+
|
|
30
|
+
// 通过 userId + path 下载
|
|
31
|
+
const blob = await resources.downloadByUser(42, 'docs/doc.txt');
|
|
32
|
+
|
|
33
|
+
// 修改权限
|
|
34
|
+
await resources.updateAccess(res.id, 'specific_users');
|
|
35
|
+
|
|
36
|
+
// 批量分享给指定用户(支持单个 number 或 number[])
|
|
37
|
+
await resources.addShare(res.id, [99, 100, 101]);
|
|
38
|
+
|
|
39
|
+
// 批量移除分享
|
|
40
|
+
await resources.removeShare(res.id, [99, 100]);
|
|
41
|
+
|
|
42
|
+
// 列出被分享给我的资源
|
|
43
|
+
const shared = await resources.listSharedWithMe();
|
|
44
|
+
|
|
45
|
+
// 跨插件查看(默认按 plugin_id 隔离)
|
|
46
|
+
const allPublic = await resources.listPublic({ crossPlugin: true });
|
|
47
|
+
|
|
48
|
+
// 通过资源 ID 下载
|
|
49
|
+
const data = await resources.download(res.id);
|
|
50
|
+
|
|
51
|
+
// 删除资源(同时删除 MinIO 文件)
|
|
52
|
+
await resources.delete(res.id);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 权限模型
|
|
56
|
+
|
|
57
|
+
| 级别 | 说明 |
|
|
58
|
+
|------|------|
|
|
59
|
+
| `private` | 仅上传者自己可访问(默认) |
|
|
60
|
+
| `specific_users` | 上传者 + 被授权的指定用户 |
|
|
61
|
+
| `public` | 所有已登录用户 |
|
|
62
|
+
|
|
63
|
+
## 插件隔离
|
|
64
|
+
|
|
65
|
+
资源默认按 `plugin_id` 隔离。所有列表查询接口默认只返回当前插件下的资源。
|
|
66
|
+
传入 `{ crossPlugin: true }` 参数可查看所有插件的资源(仍受权限约束)。
|
|
67
|
+
|
|
68
|
+
## 与 MinIO 存储的区别
|
|
69
|
+
|
|
70
|
+
- `MinioStorageClient` — 原始对象存储,无权限控制
|
|
71
|
+
- `ResourceClient` — 带 ACL 的资源管理,数据库驱动权限检查
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# 文本生成
|
|
2
|
+
|
|
3
|
+
通过 Vercel AI SDK 的 `generateText` / `streamText` 调用文本模型。
|
|
4
|
+
|
|
5
|
+
## 一次性生成
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { createProvider } from 'ai-world-sdk';
|
|
9
|
+
import { generateText } from 'ai';
|
|
10
|
+
|
|
11
|
+
const provider = createProvider('api2img', 'openai', 'my-plugin');
|
|
12
|
+
const model = provider.languageModel('gpt-4o-mini');
|
|
13
|
+
|
|
14
|
+
const result = await generateText({ model, prompt: '你好' });
|
|
15
|
+
console.log(result.text);
|
|
16
|
+
// result.usage?.inputTokens / result.usage?.outputTokens
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## 流式生成
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createProvider } from 'ai-world-sdk';
|
|
23
|
+
import { streamText } from 'ai';
|
|
24
|
+
|
|
25
|
+
const provider = createProvider('api2img', 'openai', 'my-plugin');
|
|
26
|
+
const model = provider.languageModel('gpt-4o-mini');
|
|
27
|
+
|
|
28
|
+
const stream = streamText({ model, prompt: '写故事' });
|
|
29
|
+
for await (const chunk of stream.textStream) {
|
|
30
|
+
process.stdout.write(chunk);
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## 结构化数据生成
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { createProvider } from 'ai-world-sdk';
|
|
38
|
+
import { generateObject } from 'ai';
|
|
39
|
+
import { z } from 'zod';
|
|
40
|
+
|
|
41
|
+
const provider = createProvider('api2img', 'openai', 'my-plugin');
|
|
42
|
+
const model = provider.languageModel('gpt-4o-mini');
|
|
43
|
+
|
|
44
|
+
const { object } = await generateObject({
|
|
45
|
+
model,
|
|
46
|
+
schema: z.object({
|
|
47
|
+
name: z.string(),
|
|
48
|
+
age: z.number(),
|
|
49
|
+
}),
|
|
50
|
+
prompt: '生成一个虚拟用户',
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 注意事项
|
|
55
|
+
|
|
56
|
+
- AI SDK v6 中 usage 字段为 `inputTokens` / `outputTokens`(非 `promptTokens`)
|
|
57
|
+
- 推荐使用 `api2img` + `openai` endpoint,一个 provider 可访问所有模型
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# 视频生成
|
|
2
|
+
|
|
3
|
+
## 豆包视频生成(Seedance)
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { VideoGenerationClient } from 'ai-world-sdk';
|
|
7
|
+
|
|
8
|
+
const client = new VideoGenerationClient({});
|
|
9
|
+
|
|
10
|
+
// 创建任务
|
|
11
|
+
const task = await client.create({
|
|
12
|
+
prompt: '日落时分的海滩',
|
|
13
|
+
duration: 5, // 1-10 秒
|
|
14
|
+
aspect_ratio: '16:9', // '16:9' | '9:16' | '1:1'
|
|
15
|
+
resolution: '720p',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// 轮询直到完成
|
|
19
|
+
const result = await client.poll(task.id, {
|
|
20
|
+
interval: 3000,
|
|
21
|
+
timeout: 300000,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (result.status === 'succeeded') {
|
|
25
|
+
console.log(result.content?.video_url);
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## OpenAI 视频生成(Sora)
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { OpenAIVideoGenerationClient } from 'ai-world-sdk';
|
|
33
|
+
|
|
34
|
+
const client = new OpenAIVideoGenerationClient({});
|
|
35
|
+
|
|
36
|
+
const task = await client.create({
|
|
37
|
+
prompt: '一只猫在弹钢琴',
|
|
38
|
+
duration: 5,
|
|
39
|
+
aspect_ratio: '16:9',
|
|
40
|
+
resolution: '720p',
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const result = await client.poll(task.id, {
|
|
44
|
+
interval: 5000,
|
|
45
|
+
timeout: 600000,
|
|
46
|
+
});
|
|
47
|
+
```
|