ai-world-sdk 1.0.18 → 1.1.0
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/__tests__/example.test.js +1 -6
- package/dist/auth.js +2 -0
- package/dist/base.js +6 -0
- package/dist/config.d.ts +36 -3
- package/dist/config.js +115 -8
- package/dist/doubao-image-generation.js +2 -0
- package/dist/download.js +4 -0
- package/dist/gemini-image-generation.js +2 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +5 -2
- package/dist/video_generation.js +6 -0
- package/package.json +4 -2
|
@@ -850,12 +850,7 @@ describe("Langchain SDK Tests", () => {
|
|
|
850
850
|
expect(index_1.sdkConfig.getDebug()).toBe(true);
|
|
851
851
|
index_1.sdkConfig.setDebug(false);
|
|
852
852
|
expect(index_1.sdkConfig.getDebug()).toBe(false);
|
|
853
|
-
//
|
|
854
|
-
index_1.sdkConfig.reset();
|
|
855
|
-
expect(index_1.sdkConfig.getServerUrl()).toBeNull();
|
|
856
|
-
expect(index_1.sdkConfig.getToken()).toBeNull();
|
|
857
|
-
expect(Object.keys(index_1.sdkConfig.getHeaders()).length).toBe(0);
|
|
858
|
-
expect(index_1.sdkConfig.getDebug()).toBe(false);
|
|
853
|
+
// 注意: reset 方法已被移除,不再测试
|
|
859
854
|
// 恢复原始配置
|
|
860
855
|
if (originalBaseUrl)
|
|
861
856
|
index_1.sdkConfig.setBaseUrl(originalBaseUrl);
|
package/dist/auth.js
CHANGED
|
@@ -43,6 +43,8 @@ class AuthClient {
|
|
|
43
43
|
* @returns Promise that resolves to UserInfo
|
|
44
44
|
*/
|
|
45
45
|
async getCurrentUserInfo() {
|
|
46
|
+
// 检查版本兼容性
|
|
47
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
46
48
|
const url = `${this.baseUrl}/api/auth/me`;
|
|
47
49
|
(0, log_1.debugLog)("Get current user info request:", { url });
|
|
48
50
|
(0, log_1.logRequest)("GET", url, this.headers);
|
package/dist/base.js
CHANGED
|
@@ -33,6 +33,8 @@ class BaseChatModel {
|
|
|
33
33
|
* 遵循标准 LangChain API
|
|
34
34
|
*/
|
|
35
35
|
async invoke(messages) {
|
|
36
|
+
// 检查版本兼容性
|
|
37
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
36
38
|
const options = this._mergeOptions({});
|
|
37
39
|
const requestBody = {
|
|
38
40
|
messages,
|
|
@@ -82,6 +84,8 @@ class BaseChatModel {
|
|
|
82
84
|
* 遵循标准 LangChain API,返回 AIMessageChunk
|
|
83
85
|
*/
|
|
84
86
|
async *stream(messages) {
|
|
87
|
+
// 检查版本兼容性
|
|
88
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
85
89
|
const options = this._mergeOptions({});
|
|
86
90
|
const requestBody = {
|
|
87
91
|
messages,
|
|
@@ -153,6 +157,8 @@ class BaseChatModel {
|
|
|
153
157
|
* 遵循标准 LangChain API
|
|
154
158
|
*/
|
|
155
159
|
async batch(messagesList) {
|
|
160
|
+
// 检查版本兼容性
|
|
161
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
156
162
|
const requestBody = {
|
|
157
163
|
messages_list: messagesList,
|
|
158
164
|
config: {
|
package/dist/config.d.ts
CHANGED
|
@@ -2,11 +2,20 @@
|
|
|
2
2
|
* Global SDK Configuration
|
|
3
3
|
* 全局 SDK 配置
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* 版本兼容性错误
|
|
7
|
+
*/
|
|
8
|
+
export declare class VersionCompatibilityError extends Error {
|
|
9
|
+
constructor(message: string);
|
|
10
|
+
}
|
|
5
11
|
declare class SDKConfig {
|
|
6
12
|
private _baseUrl;
|
|
7
13
|
private _token;
|
|
8
14
|
private _headers;
|
|
9
15
|
private _debug;
|
|
16
|
+
private _pluginId;
|
|
17
|
+
private _versionCompatible;
|
|
18
|
+
private _versionCheckPromise;
|
|
10
19
|
constructor();
|
|
11
20
|
/**
|
|
12
21
|
* Set global base URL
|
|
@@ -49,10 +58,34 @@ declare class SDKConfig {
|
|
|
49
58
|
*/
|
|
50
59
|
getDebug(): boolean;
|
|
51
60
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
61
|
+
* Set plugin ID
|
|
62
|
+
* 设置插件 ID
|
|
63
|
+
*/
|
|
64
|
+
setPluginId(pluginId: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Get plugin ID
|
|
67
|
+
* 获取插件 ID(从 window.__INITIAL_DATA__ 自动获取或手动设置)
|
|
68
|
+
*/
|
|
69
|
+
getPluginId(): string | null;
|
|
70
|
+
/**
|
|
71
|
+
* Check version compatibility with backend
|
|
72
|
+
* 检查与后端的版本兼容性
|
|
73
|
+
*/
|
|
74
|
+
checkVersionCompatibility(): Promise<boolean>;
|
|
75
|
+
/**
|
|
76
|
+
* 执行版本兼容性检查
|
|
77
|
+
*/
|
|
78
|
+
private _performVersionCheck;
|
|
79
|
+
/**
|
|
80
|
+
* Get version compatibility status
|
|
81
|
+
* 获取版本兼容性状态
|
|
82
|
+
*/
|
|
83
|
+
isVersionCompatible(): boolean | null;
|
|
84
|
+
/**
|
|
85
|
+
* Ensure version is compatible, throw error if not
|
|
86
|
+
* 确保版本兼容,如果不兼容则抛出错误
|
|
54
87
|
*/
|
|
55
|
-
|
|
88
|
+
ensureVersionCompatible(): void;
|
|
56
89
|
}
|
|
57
90
|
export declare const sdkConfig: SDKConfig;
|
|
58
91
|
export {};
|
package/dist/config.js
CHANGED
|
@@ -4,13 +4,29 @@
|
|
|
4
4
|
* 全局 SDK 配置
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.sdkConfig = void 0;
|
|
7
|
+
exports.sdkConfig = exports.VersionCompatibilityError = void 0;
|
|
8
|
+
// SDK 版本号(构建时自动从 package.json 更新)
|
|
9
|
+
// 此版本号会在运行 npm run build 时自动从 package.json 读取并更新
|
|
10
|
+
const SDK_VERSION = "1.1.0";
|
|
11
|
+
/**
|
|
12
|
+
* 版本兼容性错误
|
|
13
|
+
*/
|
|
14
|
+
class VersionCompatibilityError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "VersionCompatibilityError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
exports.VersionCompatibilityError = VersionCompatibilityError;
|
|
8
21
|
class SDKConfig {
|
|
9
22
|
constructor() {
|
|
10
23
|
this._baseUrl = null;
|
|
11
24
|
this._token = null;
|
|
12
25
|
this._headers = {};
|
|
13
26
|
this._debug = false;
|
|
27
|
+
this._pluginId = null;
|
|
28
|
+
this._versionCompatible = null; // null 表示未检查,true/false 表示检查结果
|
|
29
|
+
this._versionCheckPromise = null; // 版本检查的 Promise,避免重复请求
|
|
14
30
|
// 通过cookie获取token
|
|
15
31
|
try {
|
|
16
32
|
const cookieString = typeof document !== 'undefined' ? document.cookie : '';
|
|
@@ -27,6 +43,23 @@ class SDKConfig {
|
|
|
27
43
|
// 通过cookie获取baseUrl
|
|
28
44
|
const baseUrl = typeof window !== 'undefined' ? window.location.origin : '';
|
|
29
45
|
this._baseUrl = baseUrl;
|
|
46
|
+
// 从 window.__INITIAL_DATA__ 获取 plugin_id(SSR 插件环境)
|
|
47
|
+
try {
|
|
48
|
+
if (typeof window !== 'undefined' && window.__INITIAL_DATA__) {
|
|
49
|
+
const initialData = window.__INITIAL_DATA__;
|
|
50
|
+
if (initialData.plugin_id) {
|
|
51
|
+
this._headers['X-Plugin-Id'] = this._pluginId = initialData.plugin_id;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.warn('Failed to read plugin_id from __INITIAL_DATA__:', error);
|
|
57
|
+
}
|
|
58
|
+
this._headers['X-SDK-Version'] = SDK_VERSION;
|
|
59
|
+
// 自动检查版本兼容性(异步,不阻塞初始化)
|
|
60
|
+
this.checkVersionCompatibility().catch((error) => {
|
|
61
|
+
console.warn('版本兼容性检查失败:', error);
|
|
62
|
+
});
|
|
30
63
|
}
|
|
31
64
|
/**
|
|
32
65
|
* Set global base URL
|
|
@@ -85,14 +118,88 @@ class SDKConfig {
|
|
|
85
118
|
return this._debug;
|
|
86
119
|
}
|
|
87
120
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
121
|
+
* Set plugin ID
|
|
122
|
+
* 设置插件 ID
|
|
90
123
|
*/
|
|
91
|
-
|
|
92
|
-
this.
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
124
|
+
setPluginId(pluginId) {
|
|
125
|
+
this._pluginId = pluginId;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get plugin ID
|
|
129
|
+
* 获取插件 ID(从 window.__INITIAL_DATA__ 自动获取或手动设置)
|
|
130
|
+
*/
|
|
131
|
+
getPluginId() {
|
|
132
|
+
return this._pluginId;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Check version compatibility with backend
|
|
136
|
+
* 检查与后端的版本兼容性
|
|
137
|
+
*/
|
|
138
|
+
async checkVersionCompatibility() {
|
|
139
|
+
// 如果已经有检查结果,直接返回
|
|
140
|
+
if (this._versionCompatible !== null) {
|
|
141
|
+
return this._versionCompatible;
|
|
142
|
+
}
|
|
143
|
+
// 如果正在检查中,返回现有的 Promise
|
|
144
|
+
if (this._versionCheckPromise) {
|
|
145
|
+
return this._versionCheckPromise;
|
|
146
|
+
}
|
|
147
|
+
// 开始新的检查
|
|
148
|
+
this._versionCheckPromise = this._performVersionCheck();
|
|
149
|
+
const result = await this._versionCheckPromise;
|
|
150
|
+
this._versionCheckPromise = null;
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 执行版本兼容性检查
|
|
155
|
+
*/
|
|
156
|
+
async _performVersionCheck() {
|
|
157
|
+
const baseUrl = this._baseUrl || (typeof window !== 'undefined' ? window.location.origin : '');
|
|
158
|
+
if (!baseUrl) {
|
|
159
|
+
this._versionCompatible = true;
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
const response = await fetch(`${baseUrl}/api/sdk/version-check`, {
|
|
164
|
+
method: 'POST',
|
|
165
|
+
headers: {
|
|
166
|
+
'Content-Type': 'application/json',
|
|
167
|
+
'X-SDK-Version': SDK_VERSION,
|
|
168
|
+
},
|
|
169
|
+
body: JSON.stringify({ sdk_version: SDK_VERSION }),
|
|
170
|
+
});
|
|
171
|
+
if (!response.ok) {
|
|
172
|
+
this._versionCompatible = true;
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
const data = await response.json();
|
|
176
|
+
this._versionCompatible = data.compatible;
|
|
177
|
+
if (!data.compatible) {
|
|
178
|
+
console.error(`SDK 版本不兼容: ${data.message}`);
|
|
179
|
+
}
|
|
180
|
+
return data.compatible;
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
console.warn('版本兼容性检查失败:', error);
|
|
184
|
+
this._versionCompatible = false;
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Get version compatibility status
|
|
190
|
+
* 获取版本兼容性状态
|
|
191
|
+
*/
|
|
192
|
+
isVersionCompatible() {
|
|
193
|
+
return this._versionCompatible;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Ensure version is compatible, throw error if not
|
|
197
|
+
* 确保版本兼容,如果不兼容则抛出错误
|
|
198
|
+
*/
|
|
199
|
+
ensureVersionCompatible() {
|
|
200
|
+
if (this._versionCompatible === false) {
|
|
201
|
+
throw new VersionCompatibilityError('请更新 ai-world-sdk 到最新版本, 当前版本: ' + SDK_VERSION);
|
|
202
|
+
}
|
|
96
203
|
}
|
|
97
204
|
}
|
|
98
205
|
// 导出单例实例
|
package/dist/download.js
CHANGED
|
@@ -43,6 +43,8 @@ class DownloadClient {
|
|
|
43
43
|
* @returns Promise that resolves to a Blob
|
|
44
44
|
*/
|
|
45
45
|
async download(options) {
|
|
46
|
+
// 检查版本兼容性
|
|
47
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
46
48
|
const { url, filename } = options;
|
|
47
49
|
if (!url) {
|
|
48
50
|
throw new Error("URL is required");
|
|
@@ -104,6 +106,8 @@ class DownloadClient {
|
|
|
104
106
|
* ```
|
|
105
107
|
*/
|
|
106
108
|
async *streamDownload(options) {
|
|
109
|
+
// 检查版本兼容性
|
|
110
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
107
111
|
const { url, filename } = options;
|
|
108
112
|
if (!url) {
|
|
109
113
|
throw new Error("URL is required");
|
|
@@ -36,6 +36,8 @@ class GeminiImageGenerationClient {
|
|
|
36
36
|
* - response_modalities: 响应模态,["TEXT", "IMAGE"] 或 ["IMAGE"]
|
|
37
37
|
*/
|
|
38
38
|
async generate(request) {
|
|
39
|
+
// 检查版本兼容性
|
|
40
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
39
41
|
const requestBody = {
|
|
40
42
|
prompt: request.prompt,
|
|
41
43
|
model: request.model || "gemini-2.0-flash-exp-image-generation",
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,6 @@ import { GeminiImageGenerationClient, type GeminiImageGenerationConfig, type Gem
|
|
|
10
10
|
import { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask } from "./video_generation";
|
|
11
11
|
import { DownloadClient, type DownloadConfig, type DownloadOptions, type StreamDownloadOptions } from "./download";
|
|
12
12
|
import { AuthClient, getCurrentUserInfo, type AuthConfig, type UserInfo } from "./auth";
|
|
13
|
-
import { sdkConfig } from "./config";
|
|
14
13
|
export { BaseMessage, HumanMessage, AIMessage, SystemMessage, AIMessageChunk, type MessageContent, type AIMessageChunkData, } from "./messages";
|
|
15
14
|
export { BaseChatModel, type BaseChatModelParams, type AIModelProvider, type ToolDefinition, type BindOptions, } from "./base";
|
|
16
15
|
export { ChatOpenAI } from "./chat_models/openai";
|
|
@@ -26,7 +25,7 @@ export { GeminiImageGenerationClient, type GeminiImageGenerationConfig, type Gem
|
|
|
26
25
|
export { VideoGenerationClient, type VideoGenerationConfig, type VideoGenerationRequest, type ContentGenerationTaskID, type ContentGenerationTask, };
|
|
27
26
|
export { DownloadClient, type DownloadConfig, type DownloadOptions, type StreamDownloadOptions, };
|
|
28
27
|
export { AuthClient, getCurrentUserInfo, type AuthConfig, type UserInfo, };
|
|
29
|
-
export { sdkConfig };
|
|
28
|
+
export { sdkConfig, VersionCompatibilityError } from "./config";
|
|
30
29
|
/**
|
|
31
30
|
* Create a chat model instance based on model name
|
|
32
31
|
*/
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @see https://github.com/langchain-ai/langchainjs
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.sdkConfig = exports.getCurrentUserInfo = exports.AuthClient = exports.DownloadClient = exports.VideoGenerationClient = exports.GeminiImageGenerationClient = exports.DoubaoImageGenerationClient = exports.ChatAnthropic = exports.ChatGoogleGenerativeAI = exports.ChatOpenAI = exports.BaseChatModel = exports.AIMessageChunk = exports.SystemMessage = exports.AIMessage = exports.HumanMessage = void 0;
|
|
9
|
+
exports.VersionCompatibilityError = exports.sdkConfig = exports.getCurrentUserInfo = exports.AuthClient = exports.DownloadClient = exports.VideoGenerationClient = exports.GeminiImageGenerationClient = exports.DoubaoImageGenerationClient = exports.ChatAnthropic = exports.ChatGoogleGenerativeAI = exports.ChatOpenAI = exports.BaseChatModel = exports.AIMessageChunk = exports.SystemMessage = exports.AIMessage = exports.HumanMessage = void 0;
|
|
10
10
|
exports.createChatModel = createChatModel;
|
|
11
11
|
const openai_1 = require("./chat_models/openai");
|
|
12
12
|
const google_1 = require("./chat_models/google");
|
|
@@ -22,7 +22,6 @@ const auth_1 = require("./auth");
|
|
|
22
22
|
Object.defineProperty(exports, "AuthClient", { enumerable: true, get: function () { return auth_1.AuthClient; } });
|
|
23
23
|
Object.defineProperty(exports, "getCurrentUserInfo", { enumerable: true, get: function () { return auth_1.getCurrentUserInfo; } });
|
|
24
24
|
const config_1 = require("./config");
|
|
25
|
-
Object.defineProperty(exports, "sdkConfig", { enumerable: true, get: function () { return config_1.sdkConfig; } });
|
|
26
25
|
// Re-export types and classes
|
|
27
26
|
var messages_1 = require("./messages");
|
|
28
27
|
Object.defineProperty(exports, "HumanMessage", { enumerable: true, get: function () { return messages_1.HumanMessage; } });
|
|
@@ -37,6 +36,10 @@ var google_2 = require("./chat_models/google");
|
|
|
37
36
|
Object.defineProperty(exports, "ChatGoogleGenerativeAI", { enumerable: true, get: function () { return google_2.ChatGoogleGenerativeAI; } });
|
|
38
37
|
var anthropic_1 = require("./chat_models/anthropic");
|
|
39
38
|
Object.defineProperty(exports, "ChatAnthropic", { enumerable: true, get: function () { return anthropic_1.ChatAnthropic; } });
|
|
39
|
+
// Export global configuration
|
|
40
|
+
var config_2 = require("./config");
|
|
41
|
+
Object.defineProperty(exports, "sdkConfig", { enumerable: true, get: function () { return config_2.sdkConfig; } });
|
|
42
|
+
Object.defineProperty(exports, "VersionCompatibilityError", { enumerable: true, get: function () { return config_2.VersionCompatibilityError; } });
|
|
40
43
|
/**
|
|
41
44
|
* Create a chat model instance based on model name
|
|
42
45
|
*/
|
package/dist/video_generation.js
CHANGED
|
@@ -24,6 +24,8 @@ class VideoGenerationClient {
|
|
|
24
24
|
* 创建视频生成任务
|
|
25
25
|
*/
|
|
26
26
|
async create(request) {
|
|
27
|
+
// 检查版本兼容性
|
|
28
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
27
29
|
const url = `${config_1.sdkConfig.getServerUrl()}/api/video-proxy/generate`;
|
|
28
30
|
(0, log_1.logRequest)("POST", url, this.headers, request);
|
|
29
31
|
const response = await fetch(url, {
|
|
@@ -45,6 +47,8 @@ class VideoGenerationClient {
|
|
|
45
47
|
* 查询视频生成任务状态
|
|
46
48
|
*/
|
|
47
49
|
async get(taskId) {
|
|
50
|
+
// 检查版本兼容性
|
|
51
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
48
52
|
const url = `${config_1.sdkConfig.getServerUrl()}/api/video-proxy/${taskId}`;
|
|
49
53
|
(0, log_1.logRequest)("GET", url, this.headers);
|
|
50
54
|
const response = await fetch(url, {
|
|
@@ -65,6 +69,8 @@ class VideoGenerationClient {
|
|
|
65
69
|
* 轮询视频生成任务直到完成
|
|
66
70
|
*/
|
|
67
71
|
async poll(taskId, options) {
|
|
72
|
+
// 检查版本兼容性
|
|
73
|
+
config_1.sdkConfig.ensureVersionCompatible();
|
|
68
74
|
const interval = options?.interval || 5000;
|
|
69
75
|
const timeout = options?.timeout || 300000;
|
|
70
76
|
const startTime = Date.now();
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-world-sdk",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
8
|
+
"update-version": "node scripts/update-version.js",
|
|
9
|
+
"build": "npm run update-version && rm -rf dist && tsc",
|
|
9
10
|
"prepublishOnly": "npm run build",
|
|
11
|
+
"publish": "npm run build && npm publish --access public",
|
|
10
12
|
"test": "jest",
|
|
11
13
|
"test:watch": "jest --watch",
|
|
12
14
|
"test:genimi-non-stream": "jest -t 'ChatGoogleGenerativeAI - 非流式调用'",
|