ai-world-sdk 1.2.0 → 1.2.2

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.
@@ -0,0 +1,156 @@
1
+ /**
2
+ * MinIO Storage Client
3
+ * MinIO 对象存储客户端
4
+ * 通过后端 /api/minio 代理操作 MinIO 存储
5
+ */
6
+ export interface MinioStorageConfig {
7
+ baseUrl?: string;
8
+ token?: string;
9
+ headers?: Record<string, string>;
10
+ pluginId?: string;
11
+ }
12
+ export interface UploadResponse {
13
+ path: string;
14
+ object_path: string;
15
+ size: number;
16
+ etag: string;
17
+ bucket: string;
18
+ }
19
+ export interface ObjectInfo {
20
+ name: string;
21
+ size?: number;
22
+ last_modified?: string;
23
+ etag?: string;
24
+ is_dir?: boolean;
25
+ content_type?: string;
26
+ metadata?: Record<string, string>;
27
+ }
28
+ export interface PresignedUrlResponse {
29
+ url: string;
30
+ expires_in: number;
31
+ }
32
+ export interface UploadOptions {
33
+ /** MIME content type */
34
+ contentType?: string;
35
+ }
36
+ export interface ListOptions {
37
+ /** 路径前缀过滤 */
38
+ prefix?: string;
39
+ /** 是否递归列出子目录,默认 true */
40
+ recursive?: boolean;
41
+ }
42
+ export interface PresignedUploadRequest {
43
+ path: string;
44
+ expires_in?: number;
45
+ }
46
+ /**
47
+ * MinIO Storage Client
48
+ * MinIO 对象存储客户端类
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import { MinioStorageClient, sdkConfig } from 'ai-world-sdk';
53
+ *
54
+ * sdkConfig.setPluginId('my-plugin');
55
+ *
56
+ * const storage = new MinioStorageClient();
57
+ *
58
+ * // 上传文件
59
+ * const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });
60
+ * const result = await storage.upload('docs/hello.txt', file);
61
+ *
62
+ * // 列出文件
63
+ * const files = await storage.list({ prefix: 'docs/' });
64
+ *
65
+ * // 下载文件
66
+ * const blob = await storage.download('docs/hello.txt');
67
+ *
68
+ * // 获取预签名 URL
69
+ * const { url } = await storage.getPresignedUrl('docs/hello.txt');
70
+ *
71
+ * // 删除文件
72
+ * await storage.delete('docs/hello.txt');
73
+ * ```
74
+ */
75
+ export declare class MinioStorageClient {
76
+ private baseUrl;
77
+ private headers;
78
+ constructor(config?: MinioStorageConfig);
79
+ /**
80
+ * 确保 X-Plugin-Id 已设置
81
+ */
82
+ private ensurePluginId;
83
+ /**
84
+ * 处理错误响应
85
+ */
86
+ private handleErrorResponse;
87
+ /**
88
+ * Upload a file to MinIO storage
89
+ * 上传文件到 MinIO 存储
90
+ *
91
+ * @param path - 存储路径(不含 userId/pluginId 前缀)
92
+ * @param file - 要上传的文件 (File, Blob, 或 Buffer)
93
+ * @param options - 上传选项
94
+ * @returns 上传结果
95
+ */
96
+ upload(path: string, file: File | Blob, options?: UploadOptions): Promise<UploadResponse>;
97
+ /**
98
+ * Download a file from MinIO storage
99
+ * 从 MinIO 存储下载文件
100
+ *
101
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
102
+ * @returns 文件 Blob
103
+ */
104
+ download(path: string): Promise<Blob>;
105
+ /**
106
+ * List files in MinIO storage
107
+ * 列出 MinIO 存储中的文件
108
+ *
109
+ * @param options - 列表选项(prefix, recursive)
110
+ * @returns 对象信息列表
111
+ */
112
+ list(options?: ListOptions): Promise<ObjectInfo[]>;
113
+ /**
114
+ * Delete a file from MinIO storage
115
+ * 从 MinIO 存储中删除文件
116
+ *
117
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
118
+ */
119
+ delete(path: string): Promise<void>;
120
+ /**
121
+ * Get a presigned download URL
122
+ * 获取预签名下载 URL
123
+ *
124
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
125
+ * @param expiresIn - 过期时间(秒),默认 3600
126
+ * @returns 预签名 URL 信息
127
+ */
128
+ getPresignedUrl(path: string, expiresIn?: number): Promise<PresignedUrlResponse>;
129
+ /**
130
+ * Get a presigned upload URL
131
+ * 获取预签名上传 URL
132
+ *
133
+ * 返回一个可直接上传文件的临时 URL,客户端可直接 PUT 文件到该 URL。
134
+ *
135
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
136
+ * @param expiresIn - 过期时间(秒),默认 3600
137
+ * @returns 预签名 URL 信息
138
+ */
139
+ getPresignedUploadUrl(path: string, expiresIn?: number): Promise<PresignedUrlResponse>;
140
+ /**
141
+ * Get file information/metadata
142
+ * 获取文件元信息
143
+ *
144
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
145
+ * @returns 对象信息
146
+ */
147
+ info(path: string): Promise<ObjectInfo>;
148
+ /**
149
+ * Download a file and trigger browser download
150
+ * 下载文件并触发浏览器下载
151
+ *
152
+ * @param path - 文件路径
153
+ * @param filename - 可选的下载文件名
154
+ */
155
+ downloadAsFile(path: string, filename?: string): Promise<void>;
156
+ }
package/dist/minio.js ADDED
@@ -0,0 +1,322 @@
1
+ "use strict";
2
+ /**
3
+ * MinIO Storage Client
4
+ * MinIO 对象存储客户端
5
+ * 通过后端 /api/minio 代理操作 MinIO 存储
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.MinioStorageClient = void 0;
9
+ const config_1 = require("./config");
10
+ const log_1 = require("./log");
11
+ // ==================== MinioStorageClient ====================
12
+ /**
13
+ * MinIO Storage Client
14
+ * MinIO 对象存储客户端类
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { MinioStorageClient, sdkConfig } from 'ai-world-sdk';
19
+ *
20
+ * sdkConfig.setPluginId('my-plugin');
21
+ *
22
+ * const storage = new MinioStorageClient();
23
+ *
24
+ * // 上传文件
25
+ * const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });
26
+ * const result = await storage.upload('docs/hello.txt', file);
27
+ *
28
+ * // 列出文件
29
+ * const files = await storage.list({ prefix: 'docs/' });
30
+ *
31
+ * // 下载文件
32
+ * const blob = await storage.download('docs/hello.txt');
33
+ *
34
+ * // 获取预签名 URL
35
+ * const { url } = await storage.getPresignedUrl('docs/hello.txt');
36
+ *
37
+ * // 删除文件
38
+ * await storage.delete('docs/hello.txt');
39
+ * ```
40
+ */
41
+ class MinioStorageClient {
42
+ constructor(config = {}) {
43
+ // 使用配置的 baseUrl 或全局配置
44
+ this.baseUrl =
45
+ config.baseUrl ||
46
+ config_1.sdkConfig.getServerUrl() ||
47
+ (typeof window !== "undefined" ? window.location.origin : "");
48
+ // 合并 headers
49
+ const globalHeaders = config_1.sdkConfig.getHeaders();
50
+ const globalToken = config.token || config_1.sdkConfig.getToken();
51
+ this.headers = {
52
+ ...globalHeaders,
53
+ ...config.headers,
54
+ };
55
+ // 设置 plugin ID
56
+ const pluginId = config.pluginId || config_1.sdkConfig.getPluginId();
57
+ if (pluginId) {
58
+ this.headers["X-Plugin-Id"] = pluginId;
59
+ }
60
+ // 如果有 token,添加到 Authorization header
61
+ if (globalToken) {
62
+ this.headers["Authorization"] = `Bearer ${globalToken}`;
63
+ }
64
+ }
65
+ /**
66
+ * 确保 X-Plugin-Id 已设置
67
+ */
68
+ ensurePluginId() {
69
+ if (!this.headers["X-Plugin-Id"]) {
70
+ // 尝试从 sdkConfig 获取最新的 pluginId
71
+ const pluginId = config_1.sdkConfig.getPluginId();
72
+ if (pluginId) {
73
+ this.headers["X-Plugin-Id"] = pluginId;
74
+ }
75
+ else {
76
+ throw new Error("X-Plugin-Id is required. Set it via sdkConfig.setPluginId() or pass pluginId in MinioStorageConfig.");
77
+ }
78
+ }
79
+ }
80
+ /**
81
+ * 处理错误响应
82
+ */
83
+ async handleErrorResponse(response) {
84
+ let errorMessage = `Request failed: ${response.status} ${response.statusText}`;
85
+ try {
86
+ const errorText = await response.text();
87
+ const errorJson = JSON.parse(errorText);
88
+ errorMessage = errorJson.detail || errorMessage;
89
+ }
90
+ catch {
91
+ // 忽略解析错误
92
+ }
93
+ throw new Error(errorMessage);
94
+ }
95
+ /**
96
+ * Upload a file to MinIO storage
97
+ * 上传文件到 MinIO 存储
98
+ *
99
+ * @param path - 存储路径(不含 userId/pluginId 前缀)
100
+ * @param file - 要上传的文件 (File, Blob, 或 Buffer)
101
+ * @param options - 上传选项
102
+ * @returns 上传结果
103
+ */
104
+ async upload(path, file, options = {}) {
105
+ config_1.sdkConfig.ensureVersionCompatible();
106
+ this.ensurePluginId();
107
+ const apiUrl = `${this.baseUrl}/api/minio/upload?path=${encodeURIComponent(path)}`;
108
+ const formData = new FormData();
109
+ // 如果是 File 对象,保留文件名;否则使用 path 中的文件名
110
+ const filename = file instanceof File ? file.name : path.split("/").pop() || "file";
111
+ formData.append("file", file, filename);
112
+ // FormData 请求不应该设置 Content-Type,浏览器会自动添加 boundary
113
+ const headers = { ...this.headers };
114
+ delete headers["Content-Type"];
115
+ (0, log_1.debugLog)("MinIO upload request:", { path, filename, size: file.size });
116
+ (0, log_1.logRequest)("POST", apiUrl, headers);
117
+ const response = await fetch(apiUrl, {
118
+ method: "POST",
119
+ headers,
120
+ body: formData,
121
+ });
122
+ if (!response.ok) {
123
+ await this.handleErrorResponse(response);
124
+ }
125
+ const result = (await response.json());
126
+ (0, log_1.debugLog)("MinIO upload response:", result);
127
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, result);
128
+ return result;
129
+ }
130
+ /**
131
+ * Download a file from MinIO storage
132
+ * 从 MinIO 存储下载文件
133
+ *
134
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
135
+ * @returns 文件 Blob
136
+ */
137
+ async download(path) {
138
+ config_1.sdkConfig.ensureVersionCompatible();
139
+ this.ensurePluginId();
140
+ const apiUrl = `${this.baseUrl}/api/minio/download/${encodeURIComponent(path)}`;
141
+ (0, log_1.debugLog)("MinIO download request:", { path });
142
+ (0, log_1.logRequest)("GET", apiUrl, this.headers);
143
+ const response = await fetch(apiUrl, {
144
+ method: "GET",
145
+ headers: this.headers,
146
+ });
147
+ if (!response.ok) {
148
+ await this.handleErrorResponse(response);
149
+ }
150
+ const blob = await response.blob();
151
+ (0, log_1.debugLog)("MinIO download completed:", { size: blob.size, type: blob.type });
152
+ return blob;
153
+ }
154
+ /**
155
+ * List files in MinIO storage
156
+ * 列出 MinIO 存储中的文件
157
+ *
158
+ * @param options - 列表选项(prefix, recursive)
159
+ * @returns 对象信息列表
160
+ */
161
+ async list(options = {}) {
162
+ config_1.sdkConfig.ensureVersionCompatible();
163
+ this.ensurePluginId();
164
+ const params = new URLSearchParams();
165
+ if (options.prefix) {
166
+ params.append("prefix", options.prefix);
167
+ }
168
+ if (options.recursive !== undefined) {
169
+ params.append("recursive", String(options.recursive));
170
+ }
171
+ const queryString = params.toString();
172
+ const apiUrl = `${this.baseUrl}/api/minio/list${queryString ? `?${queryString}` : ""}`;
173
+ (0, log_1.debugLog)("MinIO list request:", options);
174
+ (0, log_1.logRequest)("GET", apiUrl, this.headers);
175
+ const response = await fetch(apiUrl, {
176
+ method: "GET",
177
+ headers: {
178
+ ...this.headers,
179
+ "Content-Type": "application/json",
180
+ },
181
+ });
182
+ if (!response.ok) {
183
+ await this.handleErrorResponse(response);
184
+ }
185
+ const result = (await response.json());
186
+ (0, log_1.debugLog)("MinIO list response:", { count: result.length });
187
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, result);
188
+ return result;
189
+ }
190
+ /**
191
+ * Delete a file from MinIO storage
192
+ * 从 MinIO 存储中删除文件
193
+ *
194
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
195
+ */
196
+ async delete(path) {
197
+ config_1.sdkConfig.ensureVersionCompatible();
198
+ this.ensurePluginId();
199
+ const apiUrl = `${this.baseUrl}/api/minio/delete/${encodeURIComponent(path)}`;
200
+ (0, log_1.debugLog)("MinIO delete request:", { path });
201
+ (0, log_1.logRequest)("DELETE", apiUrl, this.headers);
202
+ const response = await fetch(apiUrl, {
203
+ method: "DELETE",
204
+ headers: this.headers,
205
+ });
206
+ if (!response.ok) {
207
+ await this.handleErrorResponse(response);
208
+ }
209
+ (0, log_1.debugLog)("MinIO delete completed:", { path });
210
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, { path, deleted: true });
211
+ }
212
+ /**
213
+ * Get a presigned download URL
214
+ * 获取预签名下载 URL
215
+ *
216
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
217
+ * @param expiresIn - 过期时间(秒),默认 3600
218
+ * @returns 预签名 URL 信息
219
+ */
220
+ async getPresignedUrl(path, expiresIn = 3600) {
221
+ config_1.sdkConfig.ensureVersionCompatible();
222
+ this.ensurePluginId();
223
+ const apiUrl = `${this.baseUrl}/api/minio/presigned-url/${encodeURIComponent(path)}?expires_in=${expiresIn}`;
224
+ (0, log_1.debugLog)("MinIO presigned URL request:", { path, expiresIn });
225
+ (0, log_1.logRequest)("GET", apiUrl, this.headers);
226
+ const response = await fetch(apiUrl, {
227
+ method: "GET",
228
+ headers: this.headers,
229
+ });
230
+ if (!response.ok) {
231
+ await this.handleErrorResponse(response);
232
+ }
233
+ const result = (await response.json());
234
+ (0, log_1.debugLog)("MinIO presigned URL response:", result);
235
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, result);
236
+ return result;
237
+ }
238
+ /**
239
+ * Get a presigned upload URL
240
+ * 获取预签名上传 URL
241
+ *
242
+ * 返回一个可直接上传文件的临时 URL,客户端可直接 PUT 文件到该 URL。
243
+ *
244
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
245
+ * @param expiresIn - 过期时间(秒),默认 3600
246
+ * @returns 预签名 URL 信息
247
+ */
248
+ async getPresignedUploadUrl(path, expiresIn = 3600) {
249
+ config_1.sdkConfig.ensureVersionCompatible();
250
+ this.ensurePluginId();
251
+ const apiUrl = `${this.baseUrl}/api/minio/presigned-upload`;
252
+ const body = {
253
+ path,
254
+ expires_in: expiresIn,
255
+ };
256
+ (0, log_1.debugLog)("MinIO presigned upload URL request:", body);
257
+ (0, log_1.logRequest)("POST", apiUrl, this.headers);
258
+ const response = await fetch(apiUrl, {
259
+ method: "POST",
260
+ headers: {
261
+ ...this.headers,
262
+ "Content-Type": "application/json",
263
+ },
264
+ body: JSON.stringify(body),
265
+ });
266
+ if (!response.ok) {
267
+ await this.handleErrorResponse(response);
268
+ }
269
+ const result = (await response.json());
270
+ (0, log_1.debugLog)("MinIO presigned upload URL response:", result);
271
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, result);
272
+ return result;
273
+ }
274
+ /**
275
+ * Get file information/metadata
276
+ * 获取文件元信息
277
+ *
278
+ * @param path - 文件路径(不含 userId/pluginId 前缀)
279
+ * @returns 对象信息
280
+ */
281
+ async info(path) {
282
+ config_1.sdkConfig.ensureVersionCompatible();
283
+ this.ensurePluginId();
284
+ const apiUrl = `${this.baseUrl}/api/minio/info/${encodeURIComponent(path)}`;
285
+ (0, log_1.debugLog)("MinIO info request:", { path });
286
+ (0, log_1.logRequest)("GET", apiUrl, this.headers);
287
+ const response = await fetch(apiUrl, {
288
+ method: "GET",
289
+ headers: this.headers,
290
+ });
291
+ if (!response.ok) {
292
+ await this.handleErrorResponse(response);
293
+ }
294
+ const result = (await response.json());
295
+ (0, log_1.debugLog)("MinIO info response:", result);
296
+ (0, log_1.logResponse)(response.status, response.statusText, response.headers, result);
297
+ return result;
298
+ }
299
+ /**
300
+ * Download a file and trigger browser download
301
+ * 下载文件并触发浏览器下载
302
+ *
303
+ * @param path - 文件路径
304
+ * @param filename - 可选的下载文件名
305
+ */
306
+ async downloadAsFile(path, filename) {
307
+ const blob = await this.download(path);
308
+ // 确定下载文件名
309
+ const downloadFilename = filename || path.split("/").pop() || "download";
310
+ // 创建下载链接
311
+ const url = URL.createObjectURL(blob);
312
+ const a = document.createElement("a");
313
+ a.href = url;
314
+ a.download = downloadFilename;
315
+ document.body.appendChild(a);
316
+ a.click();
317
+ document.body.removeChild(a);
318
+ // 清理 URL
319
+ setTimeout(() => URL.revokeObjectURL(url), 100);
320
+ }
321
+ }
322
+ exports.MinioStorageClient = MinioStorageClient;
@@ -0,0 +1,137 @@
1
+ /**
2
+ * Resource Client
3
+ * 资源管理客户端
4
+ * 通过后端 /api/resources 代理操作资源管理系统
5
+ * 支持 private / specific_users / public 三级访问控制,默认按 plugin_id 隔离
6
+ */
7
+ export interface ResourceClientConfig {
8
+ baseUrl?: string;
9
+ token?: string;
10
+ headers?: Record<string, string>;
11
+ pluginId?: string;
12
+ }
13
+ export type AccessLevel = "private" | "specific_users" | "public";
14
+ export interface ResourceInfo {
15
+ id: number;
16
+ owner_id: number;
17
+ plugin_id: string;
18
+ path: string;
19
+ object_path: string;
20
+ filename: string;
21
+ size: number;
22
+ content_type: string;
23
+ access_level: AccessLevel;
24
+ description?: string;
25
+ created_at: string;
26
+ updated_at: string;
27
+ shared_users?: number[];
28
+ }
29
+ export interface UploadResourceOptions {
30
+ accessLevel?: AccessLevel;
31
+ description?: string;
32
+ contentType?: string;
33
+ }
34
+ export interface ListResourceOptions {
35
+ crossPlugin?: boolean;
36
+ }
37
+ /**
38
+ * Resource Client
39
+ * 资源管理客户端类
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * import { ResourceClient, sdkConfig } from 'ai-world-sdk';
44
+ *
45
+ * sdkConfig.setPluginId('my-plugin');
46
+ * const resources = new ResourceClient();
47
+ *
48
+ * // 上传并设为公开
49
+ * const file = new File(['hello'], 'hello.txt', { type: 'text/plain' });
50
+ * const res = await resources.upload('docs/hello.txt', file, { accessLevel: 'public' });
51
+ *
52
+ * // 列出我的资源
53
+ * const myFiles = await resources.listMy();
54
+ *
55
+ * // 查看某用户的公开资源
56
+ * const userFiles = await resources.listByUser(42);
57
+ *
58
+ * // 分享给指定用户
59
+ * await resources.addShare(res.id, 99);
60
+ *
61
+ * // 通过 userId + path 下载
62
+ * const blob = await resources.downloadByUser(42, 'docs/hello.txt');
63
+ * ```
64
+ */
65
+ export declare class ResourceClient {
66
+ private baseUrl;
67
+ private headers;
68
+ constructor(config?: ResourceClientConfig);
69
+ private ensurePluginId;
70
+ private handleErrorResponse;
71
+ /**
72
+ * 上传文件并创建资源记录
73
+ */
74
+ upload(path: string, file: File | Blob, options?: UploadResourceOptions): Promise<ResourceInfo>;
75
+ /**
76
+ * 列出当前用户的资源
77
+ */
78
+ listMy(options?: ListResourceOptions): Promise<ResourceInfo[]>;
79
+ /**
80
+ * 获取资源详情
81
+ */
82
+ getInfo(resourceId: number): Promise<ResourceInfo>;
83
+ /**
84
+ * 修改资源访问级别
85
+ */
86
+ updateAccess(resourceId: number, accessLevel: AccessLevel): Promise<ResourceInfo>;
87
+ /**
88
+ * 批量添加用户的访问授权
89
+ *
90
+ * @param resourceId - 资源 ID
91
+ * @param userIds - 被授权用户 ID 列表(支持单个或多个)
92
+ */
93
+ addShare(resourceId: number, userIds: number | number[]): Promise<{
94
+ resource_id: number;
95
+ added_user_ids: number[];
96
+ total_requested: number;
97
+ }>;
98
+ /**
99
+ * 批量移除用户的访问授权
100
+ *
101
+ * @param resourceId - 资源 ID
102
+ * @param userIds - 要移除授权的用户 ID 列表(支持单个或多个)
103
+ */
104
+ removeShare(resourceId: number, userIds: number | number[]): Promise<{
105
+ resource_id: number;
106
+ removed_user_ids: number[];
107
+ total_requested: number;
108
+ }>;
109
+ /**
110
+ * 通过资源 ID 下载
111
+ */
112
+ download(resourceId: number): Promise<Blob>;
113
+ /**
114
+ * 删除资源
115
+ */
116
+ delete(resourceId: number): Promise<void>;
117
+ /**
118
+ * 列出指定用户的可访问资源
119
+ */
120
+ listByUser(userId: number, options?: ListResourceOptions): Promise<ResourceInfo[]>;
121
+ /**
122
+ * 通过 userId + path 下载资源
123
+ */
124
+ downloadByUser(userId: number, path: string, pluginId?: string): Promise<Blob>;
125
+ /**
126
+ * 列出被分享给当前用户的资源
127
+ */
128
+ listSharedWithMe(options?: ListResourceOptions): Promise<ResourceInfo[]>;
129
+ /**
130
+ * 列出所有 public 资源
131
+ */
132
+ listPublic(options?: ListResourceOptions): Promise<ResourceInfo[]>;
133
+ /**
134
+ * 下载资源并触发浏览器下载
135
+ */
136
+ downloadAsFile(resourceId: number, filename?: string): Promise<void>;
137
+ }